1 /*****************************************************************************
2 * ac3_decoder.h : ac3 decoder interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /**** ac3 decoder API - public ac3 decoder structures */
25 typedef struct ac3dec_s ac3dec_t;
27 typedef struct ac3_sync_info_s {
28 int sample_rate; /* sample rate in Hz */
29 int frame_size; /* frame size in bytes */
30 int bit_rate; /* nominal bit rate in kbps */
33 typedef struct ac3_byte_stream_s {
39 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
41 int ac3_init (ac3dec_t * p_ac3dec);
42 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info);
43 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer);
44 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec);
46 /**** ac3 decoder API - user functions to be provided to the ac3 decoder ****/
48 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream);
50 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
52 /**** ac3 decoder internal structures ****/
54 /* The following structures are filled in by their corresponding parse_*
55 * functions. See http://www.atsc.org/Standards/A52/a_52.pdf for
56 * full details on each field. Indented fields are used to denote
60 typedef struct syncinfo_s {
61 /* Sync word == 0x0B77 */
63 /* crc for the first 5/8 of the sync block */
65 /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
70 /* Information not in the AC-3 bitstream, but derived */
71 /* Frame size in 16 bit words */
73 /* Bit rate in kilobits */
77 typedef struct bsi_s {
78 /* Bit stream identification == 0x8 */
82 /* Audio coding mode */
84 /* If we're using the centre channel then */
85 /* centre mix level */
87 /* If we're using the surround channel then */
88 /* surround mix level */
90 /* If we're in 2/0 mode then */
91 /* Dolby surround mix level - NOT USED - */
93 /* Low frequency effects on */
95 /* Dialogue Normalization level */
97 /* Compression exists */
99 /* Compression level */
101 /* Language code exists */
105 /* Audio production info exists*/
109 /* If we're in dual mono mode (acmod == 0) then extra stuff */
122 /* Timecode 1 exists */
126 /* Timecode 2 exists */
130 /* Additional bit stream info exists */
132 /* Additional bit stream length - 1 (in bytes) */
134 /* Additional bit stream information (max 64 bytes) */
137 /* Information not in the AC-3 bitstream, but derived */
138 /* Number of channels (excluding LFE)
139 * Derived from acmod */
144 typedef struct audblk_s {
145 /* block switch bit indexed by channel num */
147 /* dither enable bit indexed by channel num */
149 /* dynamic range gain exists */
151 /* dynamic range gain */
153 /* if acmod==0 then */
154 /* dynamic range 2 gain exists */
156 /* dynamic range 2 gain */
158 /* coupling strategy exists */
160 /* coupling in use */
162 /* channel coupled */
164 /* if acmod==2 then */
165 /* Phase flags in use */
167 /* coupling begin frequency code */
169 /* coupling end frequency code */
171 /* coupling band structure bits */
173 /* Do coupling co-ords exist for this channel? */
175 /* Master coupling co-ordinate */
177 /* Per coupling band coupling co-ordinates */
179 u16 cplcomant[5][18];
180 /* Phase flags for dual mono */
182 /* Is there a rematrixing strategy */
184 /* Rematrixing bits */
186 /* Coupling exponent strategy */
188 /* Exponent strategy for full bandwidth channels */
190 /* Exponent strategy for lfe channel */
192 /* Channel bandwidth for independent channels */
194 /* The absolute coupling exponent */
196 /* Coupling channel exponents (D15 mode gives 18 * 12 /3 encoded exponents */
197 u16 cplexps[18 * 12 / 3];
198 /* Sanity checking constant */
200 /* fbw channel exponents */
201 u16 exps[5][252 / 3];
202 /* channel gain range */
204 /* low frequency exponents */
207 /* Bit allocation info */
209 /* Slow decay code */
211 /* Fast decay code */
215 /* dB per bit code */
217 /* masking floor code */
220 /* SNR offset info */
222 /* coarse SNR offset */
224 /* coupling fine SNR offset */
226 /* coupling fast gain code */
228 /* fbw fine SNR offset */
230 /* fbw fast gain code */
232 /* lfe fine SNR offset */
234 /* lfe fast gain code */
237 /* Coupling leak info */
239 /* coupling fast leak initialization */
241 /* coupling slow leak initialization */
244 /* delta bit allocation info */
246 /* coupling delta bit allocation exists */
248 /* fbw delta bit allocation exists */
250 /* number of cpl delta bit segments */
252 /* coupling delta bit allocation offset */
254 /* coupling delta bit allocation length */
256 /* coupling delta bit allocation length */
258 /* number of delta bit segments */
260 /* fbw delta bit allocation offset */
262 /* fbw delta bit allocation length */
264 /* fbw delta bit allocation length */
267 /* skip length exists */
272 /* channel mantissas */
273 // u16 chmant[5][256];
275 /* coupling mantissas */
279 /* coupling mantissas */
282 /* -- Information not in the bitstream, but derived thereof -- */
284 /* Number of coupling sub-bands */
287 /* Number of combined coupling sub-bands
288 * Derived from ncplsubnd and cplbndstrc */
291 /* Number of exponent groups by channel
292 * Derived from strmant, endmant */
295 /* Number of coupling exponent groups
296 * Derived from cplbegf, cplendf, cplexpstr */
299 /* End mantissa numbers of fbw channels */
302 /* Start and end mantissa numbers for the coupling channel */
306 /* Decoded exponent info */
311 /* Bit allocation pointer results */
313 /* FIXME?? figure out exactly how many entries there should be (253-37?) */
318 /* Everything you wanted to know about band structure */
320 * The entire frequency domain is represented by 256 real
321 * floating point fourier coefficients. Only the lower 253
322 * coefficients are actually utilized however. We use arrays
323 * of 256 to be efficient in some cases.
325 * The 5 full bandwidth channels (fbw) can have their higher
326 * frequencies coupled together. These coupled channels then
327 * share their high frequency components.
329 * This coupling band is broken up into 18 sub-bands starting
330 * at mantissa number 37. Each sub-band is 12 bins wide.
332 * There are 50 bit allocation sub-bands which cover the entire
333 * frequency range. The sub-bands are of non-uniform width, and
334 * approximate a 1/6 octave scale.
337 typedef struct stream_coeffs_s {
342 typedef struct stream_samples_s {
343 float channel[6][256];
346 typedef struct ac3_bit_stream_s {
349 ac3_byte_stream_t byte_stream;
351 unsigned int total_bits_read; /* temporary */
359 /* The bit stream structure handles the PES stream at the bit level */
360 ac3_bit_stream_t bit_stream;
369 stream_coeffs_t coeffs;
370 stream_samples_t samples;
373 /**** ac3 decoder inline functions ****/
375 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec)
377 return &(p_ac3dec->bit_stream.byte_stream);