1 /*****************************************************************************
2 * ac3_decoder.h : ac3 decoder interface
4 *****************************************************************************/
6 /**** ac3 decoder API - public ac3 decoder structures */
8 typedef struct ac3dec_s ac3dec_t;
10 typedef struct ac3_sync_info_s {
11 int sample_rate; /* sample rate in Hz */
12 int frame_size; /* frame size in bytes */
13 int bit_rate; /* nominal bit rate in kbps */
16 typedef struct ac3_byte_stream_s {
22 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
24 int ac3_init (ac3dec_t * p_ac3dec);
25 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info);
26 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer);
27 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec);
29 /**** ac3 decoder API - user functions to be provided to the ac3 decoder ****/
31 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream);
33 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
35 /**** ac3 decoder internal structures ****/
37 /* The following structures are filled in by their corresponding parse_*
38 * functions. See http://www.atsc.org/Standards/A52/a_52.pdf for
39 * full details on each field. Indented fields are used to denote
43 typedef struct syncinfo_s {
44 /* Sync word == 0x0B77 */
46 /* crc for the first 5/8 of the sync block */
48 /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
53 /* Information not in the AC-3 bitstream, but derived */
54 /* Frame size in 16 bit words */
56 /* Bit rate in kilobits */
60 typedef struct bsi_s {
61 /* Bit stream identification == 0x8 */
65 /* Audio coding mode */
67 /* If we're using the centre channel then */
68 /* centre mix level */
70 /* If we're using the surround channel then */
71 /* surround mix level */
73 /* If we're in 2/0 mode then */
74 /* Dolby surround mix level - NOT USED - */
76 /* Low frequency effects on */
78 /* Dialogue Normalization level */
80 /* Compression exists */
82 /* Compression level */
84 /* Language code exists */
88 /* Audio production info exists*/
92 /* If we're in dual mono mode (acmod == 0) then extra stuff */
105 /* Timecode 1 exists */
109 /* Timecode 2 exists */
113 /* Additional bit stream info exists */
115 /* Additional bit stream length - 1 (in bytes) */
117 /* Additional bit stream information (max 64 bytes) */
120 /* Information not in the AC-3 bitstream, but derived */
121 /* Number of channels (excluding LFE)
122 * Derived from acmod */
127 typedef struct audblk_s {
128 /* block switch bit indexed by channel num */
130 /* dither enable bit indexed by channel num */
132 /* dynamic range gain exists */
134 /* dynamic range gain */
136 /* if acmod==0 then */
137 /* dynamic range 2 gain exists */
139 /* dynamic range 2 gain */
141 /* coupling strategy exists */
143 /* coupling in use */
145 /* channel coupled */
147 /* if acmod==2 then */
148 /* Phase flags in use */
150 /* coupling begin frequency code */
152 /* coupling end frequency code */
154 /* coupling band structure bits */
156 /* Do coupling co-ords exist for this channel? */
158 /* Master coupling co-ordinate */
160 /* Per coupling band coupling co-ordinates */
162 u16 cplcomant[5][18];
163 /* Phase flags for dual mono */
165 /* Is there a rematrixing strategy */
167 /* Rematrixing bits */
169 /* Coupling exponent strategy */
171 /* Exponent strategy for full bandwidth channels */
173 /* Exponent strategy for lfe channel */
175 /* Channel bandwidth for independent channels */
177 /* The absolute coupling exponent */
179 /* Coupling channel exponents (D15 mode gives 18 * 12 /3 encoded exponents */
180 u16 cplexps[18 * 12 / 3];
181 /* Sanity checking constant */
183 /* fbw channel exponents */
184 u16 exps[5][252 / 3];
185 /* channel gain range */
187 /* low frequency exponents */
190 /* Bit allocation info */
192 /* Slow decay code */
194 /* Fast decay code */
198 /* dB per bit code */
200 /* masking floor code */
203 /* SNR offset info */
205 /* coarse SNR offset */
207 /* coupling fine SNR offset */
209 /* coupling fast gain code */
211 /* fbw fine SNR offset */
213 /* fbw fast gain code */
215 /* lfe fine SNR offset */
217 /* lfe fast gain code */
220 /* Coupling leak info */
222 /* coupling fast leak initialization */
224 /* coupling slow leak initialization */
227 /* delta bit allocation info */
229 /* coupling delta bit allocation exists */
231 /* fbw delta bit allocation exists */
233 /* number of cpl delta bit segments */
235 /* coupling delta bit allocation offset */
237 /* coupling delta bit allocation length */
239 /* coupling delta bit allocation length */
241 /* number of delta bit segments */
243 /* fbw delta bit allocation offset */
245 /* fbw delta bit allocation length */
247 /* fbw delta bit allocation length */
250 /* skip length exists */
255 /* channel mantissas */
256 // u16 chmant[5][256];
258 /* coupling mantissas */
262 /* coupling mantissas */
265 /* -- Information not in the bitstream, but derived thereof -- */
267 /* Number of coupling sub-bands */
270 /* Number of combined coupling sub-bands
271 * Derived from ncplsubnd and cplbndstrc */
274 /* Number of exponent groups by channel
275 * Derived from strmant, endmant */
278 /* Number of coupling exponent groups
279 * Derived from cplbegf, cplendf, cplexpstr */
282 /* End mantissa numbers of fbw channels */
285 /* Start and end mantissa numbers for the coupling channel */
289 /* Decoded exponent info */
294 /* Bit allocation pointer results */
296 /* FIXME?? figure out exactly how many entries there should be (253-37?) */
301 /* Everything you wanted to know about band structure */
303 * The entire frequency domain is represented by 256 real
304 * floating point fourier coefficients. Only the lower 253
305 * coefficients are actually utilized however. We use arrays
306 * of 256 to be efficient in some cases.
308 * The 5 full bandwidth channels (fbw) can have their higher
309 * frequencies coupled together. These coupled channels then
310 * share their high frequency components.
312 * This coupling band is broken up into 18 sub-bands starting
313 * at mantissa number 37. Each sub-band is 12 bins wide.
315 * There are 50 bit allocation sub-bands which cover the entire
316 * frequency range. The sub-bands are of non-uniform width, and
317 * approximate a 1/6 octave scale.
320 typedef struct stream_coeffs_s {
325 typedef struct stream_samples_s {
326 float channel[6][256];
329 typedef struct ac3_bit_stream_s {
332 ac3_byte_stream_t byte_stream;
334 unsigned int total_bits_read; /* temporary */
342 /* The bit stream structure handles the PES stream at the bit level */
343 ac3_bit_stream_t bit_stream;
352 stream_coeffs_t coeffs;
353 stream_samples_t samples;
356 /**** ac3 decoder inline functions ****/
358 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec)
360 return &(p_ac3dec->bit_stream.byte_stream);