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 30 WORD32 ixheaacd_qsort_cmp(const VOID *va, const VOID *vb) { 31 const ia_huff_code_word_struct *huff1, *huff2; 32 33 huff1 = (ia_huff_code_word_struct *)va; 34 huff2 = (ia_huff_code_word_struct *)vb; 35 if (huff1->len < huff2->len) return -1; 36 if ((huff1->len == huff2->len) && (huff1->code_word < huff2->code_word)) 37 return -1; 38 return 1; 39 } 40 41 VOID ixheaacd_hufftab(ia_huff_code_book_struct *ptr_huff_code_book, 42 ia_huff_code_word_struct *ptr_huff_code_word, 43 WORD16 *code_book_tbl, WORD32 *index, WORD32 dim, 44 WORD32 lav, WORD32 lav_incr_esc, WORD32 sign_code_book, 45 UWORD8 max_code_word_len) { 46 WORD32 i, num; 47 48 if (!sign_code_book) { 49 ptr_huff_code_book->huff_mode = lav + 1; 50 ptr_huff_code_book->off = 0; 51 } else { 52 ptr_huff_code_book->huff_mode = 2 * lav + 1; 53 ptr_huff_code_book->off = lav; 54 } 55 num = 1; 56 for (i = 0; i < dim; i++) num *= ptr_huff_code_book->huff_mode; 57 58 ptr_huff_code_book->num = num; 59 ptr_huff_code_book->dim = dim; 60 ptr_huff_code_book->lav = lav; 61 ptr_huff_code_book->lav_incr_esc = lav_incr_esc; 62 ptr_huff_code_book->sign_code_book = sign_code_book; 63 ptr_huff_code_book->pstr_huff_code_word = ptr_huff_code_word; 64 ptr_huff_code_book->code_book_tbl = code_book_tbl; 65 ptr_huff_code_book->idx_tbl = index; 66 ptr_huff_code_book->max_code_word_len = max_code_word_len; 67 68 qsort(ptr_huff_code_word, num, sizeof(ia_huff_code_word_struct), 69 ixheaacd_qsort_cmp); 70 } 71 72 WORD32 ixheaacd_huff_codeword(ia_huff_code_word_struct *ptr_huff_code_word, 73 UWORD16 data_present, 74 ia_bit_buf_struct *it_bit_buff) 75 76 { 77 WORD32 i, j; 78 UWORD32 code_word = 0; 79 UWORD32 tmp = 0; 80 81 i = ptr_huff_code_word->len; 82 if (data_present == 0) { 83 code_word = ixheaacd_read_bits_buf(it_bit_buff, i); 84 } 85 86 if (data_present == 1) { 87 code_word = ixheaacd_read_bits_buf(it_bit_buff, i); 88 } 89 while (code_word != ptr_huff_code_word->code_word) { 90 ptr_huff_code_word++; 91 j = ptr_huff_code_word->len - i; 92 if (j < 0) { 93 return ptr_huff_code_word->index; 94 } 95 96 i += j; 97 code_word <<= j; 98 99 if (data_present == 0) { 100 tmp = ixheaacd_read_bits_buf(it_bit_buff, j); 101 } 102 103 if (data_present == 1) { 104 tmp = ixheaacd_read_bits_buf(it_bit_buff, j); 105 } 106 107 code_word |= tmp; 108 } 109 return (ptr_huff_code_word->index); 110 } 111