huangcm
2024-08-23 d76fb8c8c6d079a3cee81da7072347dcb8bbbc70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/******************************************************************************
 *                                                                            *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <stdlib.h>
#include <stdio.h>
 
#include <ixheaacd_type_def.h>
#include "ixheaacd_interface.h"
 
#include "ixheaacd_bitbuffer.h"
#include "ixheaacd_info.h"
#include "ixheaacd_bitbuffer.h"
 
WORD32 ixheaacd_qsort_cmp(const VOID *va, const VOID *vb) {
  const ia_huff_code_word_struct *huff1, *huff2;
 
  huff1 = (ia_huff_code_word_struct *)va;
  huff2 = (ia_huff_code_word_struct *)vb;
  if (huff1->len < huff2->len) return -1;
  if ((huff1->len == huff2->len) && (huff1->code_word < huff2->code_word))
    return -1;
  return 1;
}
 
VOID ixheaacd_hufftab(ia_huff_code_book_struct *ptr_huff_code_book,
                      ia_huff_code_word_struct *ptr_huff_code_word,
                      WORD16 *code_book_tbl, WORD32 *index, WORD32 dim,
                      WORD32 lav, WORD32 lav_incr_esc, WORD32 sign_code_book,
                      UWORD8 max_code_word_len) {
  WORD32 i, num;
 
  if (!sign_code_book) {
    ptr_huff_code_book->huff_mode = lav + 1;
    ptr_huff_code_book->off = 0;
  } else {
    ptr_huff_code_book->huff_mode = 2 * lav + 1;
    ptr_huff_code_book->off = lav;
  }
  num = 1;
  for (i = 0; i < dim; i++) num *= ptr_huff_code_book->huff_mode;
 
  ptr_huff_code_book->num = num;
  ptr_huff_code_book->dim = dim;
  ptr_huff_code_book->lav = lav;
  ptr_huff_code_book->lav_incr_esc = lav_incr_esc;
  ptr_huff_code_book->sign_code_book = sign_code_book;
  ptr_huff_code_book->pstr_huff_code_word = ptr_huff_code_word;
  ptr_huff_code_book->code_book_tbl = code_book_tbl;
  ptr_huff_code_book->idx_tbl = index;
  ptr_huff_code_book->max_code_word_len = max_code_word_len;
 
  qsort(ptr_huff_code_word, num, sizeof(ia_huff_code_word_struct),
        ixheaacd_qsort_cmp);
}
 
WORD32 ixheaacd_huff_codeword(ia_huff_code_word_struct *ptr_huff_code_word,
                              UWORD16 data_present,
                              ia_bit_buf_struct *it_bit_buff)
 
{
  WORD32 i, j;
  UWORD32 code_word = 0;
  UWORD32 tmp = 0;
 
  i = ptr_huff_code_word->len;
  if (data_present == 0) {
    code_word = ixheaacd_read_bits_buf(it_bit_buff, i);
  }
 
  if (data_present == 1) {
    code_word = ixheaacd_read_bits_buf(it_bit_buff, i);
  }
  while (code_word != ptr_huff_code_word->code_word) {
    ptr_huff_code_word++;
    j = ptr_huff_code_word->len - i;
    if (j < 0) {
      return ptr_huff_code_word->index;
    }
 
    i += j;
    code_word <<= j;
 
    if (data_present == 0) {
      tmp = ixheaacd_read_bits_buf(it_bit_buff, j);
    }
 
    if (data_present == 1) {
      tmp = ixheaacd_read_bits_buf(it_bit_buff, j);
    }
 
    code_word |= tmp;
  }
  return (ptr_huff_code_word->index);
}