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