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 #ifndef IXHEAACD_BITBUFFER_H
21 #define IXHEAACD_BITBUFFER_H
22 
23 #include <setjmp.h>
24 
25 #define CRC_ADTS_HEADER_LEN 56
26 #define CRC_ADTS_RAW_DATA_BLK_LEN 192
27 #define CRC_ADTS_RAW_IIND_ICS 128
28 #define CRC_ADTS_LEN_ALL -1
29 
30 #define MAX_REG_SIZE 192
31 #define MAX_CRC_REGS 7
32 
33 struct ia_crc_bit_buf_struct {
34   UWORD8 *ptr_bit_buf_base;
35   UWORD8 *ptr_bit_buf_end;
36 
37   UWORD8 *ptr_read_next;
38   WORD16 bit_pos;
39   WORD32 cnt_bits;
40 
41   WORD32 size;
42 };
43 
44 typedef struct {
45   UWORD8 active;
46   WORD32 buf_size;
47   WORD32 max_bits;
48   UWORD32 bit_cnt;
49   WORD32 bit_buf_cnt;
50   struct ia_crc_bit_buf_struct str_bit_buf;
51 } ia_crc_reg_data_struct;
52 
53 typedef struct {
54   UWORD8 crc_active;
55   UWORD16 no_reg;
56   UWORD16 file_value;
57   UWORD16 crc_lookup[256];
58   ia_crc_reg_data_struct str_crc_reg_data[MAX_CRC_REGS];
59 } ia_adts_crc_info_struct;
60 
61 typedef struct ia_bit_buf_struct {
62   UWORD8 *ptr_bit_buf_base;
63   UWORD8 *ptr_bit_buf_end;
64 
65   UWORD8 *ptr_read_next;
66 
67   WORD32 bit_pos;
68   WORD32 cnt_bits;
69 
70   WORD32 size;
71 
72   WORD32 adts_header_present;
73   WORD32 crc_check;
74   WORD8 protection_absent;
75   WORD8 no_raw_data_blocks;
76   ia_adts_crc_info_struct str_adts_crc_info;
77   ia_adts_crc_info_struct *pstr_adts_crc_info;
78 
79   WORD32 initial_cnt_bits;
80   WORD32 audio_mux_align;
81   WORD32 bit_count;
82   WORD32 valid_bits;
83   UWORD8 byte;
84   UWORD8 *byte_ptr;
85   UWORD8 *ptr_start;
86   WORD32 write_bit_count;
87   WORD32 max_size;
88   jmp_buf *xaac_jmp_buf;
89 
90 } ia_bit_buf_struct;
91 
92 typedef struct ia_bit_buf_struct *ia_handle_bit_buf_struct;
93 
94 typedef struct ia_crc_bit_buf_struct *ia_crc_bit_buf_struct_handle;
95 
96 VOID ixheaacd_byte_align(ia_bit_buf_struct *it_bit_buff,
97                          WORD32 *ptr_byte_align_bits);
98 
99 ia_bit_buf_struct *ixheaacd_create_bit_buf(ia_bit_buf_struct *it_bit_buff,
100                                            UWORD8 *ptr_bit_buf_base,
101                                            WORD32 bit_buf_size);
102 
103 VOID ixheaacd_create_init_bit_buf(ia_bit_buf_struct *it_bit_buff,
104                                   UWORD8 *ptr_bit_buf_base,
105                                   WORD32 bit_buf_size);
106 
107 WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits);
108 
109 WORD32 ixheaacd_skip_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits);
110 
111 WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits);
112 
113 VOID ixheaacd_read_bidirection(ia_bit_buf_struct *it_bit_buff,
114                                WORD32 ixheaacd_drc_offset);
115 
116 UWORD32 ixheaacd_aac_showbits_32(UWORD8 *ptr_read_next, WORD32 cnt_bits,
117                                  WORD32 *increment);
118 
119 VOID ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos,
120                             WORD32 *readword);
121 
122 VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
123                                  WORD32 *readword, UWORD8 *p_bit_buf_end);
124 
125 VOID ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
126                                   WORD32 *readword, UWORD8 *p_bit_buf_end);
127 
128 #define get_no_bits_available(it_bit_buff) ((it_bit_buff)->cnt_bits)
129 #define ixheaacd_no_bits_read(it_bit_buff) \
130   ((it_bit_buff)->size - (it_bit_buff)->cnt_bits)
131 
132 WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff);
133 WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff);
134 
135 VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
136                         WORD32 no_of_bits);
137 
138 WORD32 ixheaacd_read_bit(ia_bit_buf_struct *data, WORD32 no_of_bits);
139 
140 #endif /* IXHEAACD_BITBUFFER_H */
141