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 GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /**** ac3 decoder API - public ac3 decoder structures */
26 typedef struct ac3dec_s ac3dec_t;
28 typedef struct ac3_sync_info_s {
29 int sample_rate; /* sample rate in Hz */
30 int frame_size; /* frame size in bytes */
31 int bit_rate; /* nominal bit rate in kbps */
34 typedef struct ac3_byte_stream_s {
40 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
42 int ac3_init (ac3dec_t * p_ac3dec);
43 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info);
44 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer);
45 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec);
47 /**** ac3 decoder API - user functions to be provided to the ac3 decoder ****/
49 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream);
51 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
53 /**** ac3 decoder internal structures ****/
55 /* The following structures are filled in by their corresponding parse_*
56 * functions. See http://www.atsc.org/Standards/A52/a_52.pdf for
57 * full details on each field. Indented fields are used to denote
61 typedef struct syncinfo_s {
62 /* Sync word == 0x0B77 */
64 /* crc for the first 5/8 of the sync block */
66 /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
71 /* Information not in the AC-3 bitstream, but derived */
72 /* Frame size in 16 bit words */
74 /* Bit rate in kilobits */
78 typedef struct bsi_s {
79 /* Bit stream identification == 0x8 */
83 /* Audio coding mode */
85 /* If we're using the centre channel then */
86 /* centre mix level */
88 /* If we're using the surround channel then */
89 /* surround mix level */
91 /* If we're in 2/0 mode then */
92 /* Dolby surround mix level - NOT USED - */
94 /* Low frequency effects on */
96 /* Dialogue Normalization level */
98 /* Compression exists */
100 /* Compression level */
102 /* Language code exists */
106 /* Audio production info exists*/
110 /* If we're in dual mono mode (acmod == 0) then extra stuff */
123 /* Timecode 1 exists */
127 /* Timecode 2 exists */
131 /* Additional bit stream info exists */
133 /* Additional bit stream length - 1 (in bytes) */
135 /* Additional bit stream information (max 64 bytes) */
138 /* Information not in the AC-3 bitstream, but derived */
139 /* Number of channels (excluding LFE)
140 * Derived from acmod */
145 typedef struct audblk_s {
146 /* block switch bit indexed by channel num */
148 /* dither enable bit indexed by channel num */
150 /* dynamic range gain exists */
152 /* dynamic range gain */
154 /* if acmod==0 then */
155 /* dynamic range 2 gain exists */
157 /* dynamic range 2 gain */
159 /* coupling strategy exists */
161 /* coupling in use */
163 /* channel coupled */
165 /* if acmod==2 then */
166 /* Phase flags in use */
168 /* coupling begin frequency code */
170 /* coupling end frequency code */
172 /* coupling band structure bits */
174 /* Do coupling co-ords exist for this channel? */
176 /* Master coupling co-ordinate */
178 /* Per coupling band coupling co-ordinates */
180 u16 cplcomant[5][18];
181 /* Phase flags for dual mono */
183 /* Is there a rematrixing strategy */
185 /* Rematrixing bits */
187 /* Coupling exponent strategy */
189 /* Exponent strategy for full bandwidth channels */
191 /* Exponent strategy for lfe channel */
193 /* Channel bandwidth for independent channels */
195 /* The absolute coupling exponent */
197 /* Coupling channel exponents (D15 mode gives 18 * 12 /3 encoded exponents */
198 u16 cplexps[18 * 12 / 3];
199 /* Sanity checking constant */
201 /* fbw channel exponents */
202 u16 exps[5][252 / 3];
203 /* channel gain range */
205 /* low frequency exponents */
208 /* Bit allocation info */
210 /* Slow decay code */
212 /* Fast decay code */
216 /* dB per bit code */
218 /* masking floor code */
221 /* SNR offset info */
223 /* coarse SNR offset */
225 /* coupling fine SNR offset */
227 /* coupling fast gain code */
229 /* fbw fine SNR offset */
231 /* fbw fast gain code */
233 /* lfe fine SNR offset */
235 /* lfe fast gain code */
238 /* Coupling leak info */
240 /* coupling fast leak initialization */
242 /* coupling slow leak initialization */
245 /* delta bit allocation info */
247 /* coupling delta bit allocation exists */
249 /* fbw delta bit allocation exists */
251 /* number of cpl delta bit segments */
253 /* coupling delta bit allocation offset */
255 /* coupling delta bit allocation length */
257 /* coupling delta bit allocation length */
259 /* number of delta bit segments */
261 /* fbw delta bit allocation offset */
263 /* fbw delta bit allocation length */
265 /* fbw delta bit allocation length */
268 /* skip length exists */
273 /* channel mantissas */
274 // u16 chmant[5][256];
276 /* coupling mantissas */
280 /* coupling mantissas */
283 /* -- Information not in the bitstream, but derived thereof -- */
285 /* Number of coupling sub-bands */
288 /* Number of combined coupling sub-bands
289 * Derived from ncplsubnd and cplbndstrc */
292 /* Number of exponent groups by channel
293 * Derived from strmant, endmant */
296 /* Number of coupling exponent groups
297 * Derived from cplbegf, cplendf, cplexpstr */
300 /* End mantissa numbers of fbw channels */
303 /* Start and end mantissa numbers for the coupling channel */
307 /* Decoded exponent info */
312 /* Bit allocation pointer results */
314 /* FIXME?? figure out exactly how many entries there should be (253-37?) */
319 /* Everything you wanted to know about band structure */
321 * The entire frequency domain is represented by 256 real
322 * floating point fourier coefficients. Only the lower 253
323 * coefficients are actually utilized however. We use arrays
324 * of 256 to be efficient in some cases.
326 * The 5 full bandwidth channels (fbw) can have their higher
327 * frequencies coupled together. These coupled channels then
328 * share their high frequency components.
330 * This coupling band is broken up into 18 sub-bands starting
331 * at mantissa number 37. Each sub-band is 12 bins wide.
333 * There are 50 bit allocation sub-bands which cover the entire
334 * frequency range. The sub-bands are of non-uniform width, and
335 * approximate a 1/6 octave scale.
338 typedef struct stream_coeffs_s {
343 typedef struct stream_samples_s {
344 float channel[6][256];
347 typedef struct ac3_bit_stream_s {
350 ac3_byte_stream_t byte_stream;
352 unsigned int total_bits_read; /* temporary */
360 /* The bit stream structure handles the PES stream at the bit level */
361 ac3_bit_stream_t bit_stream;
370 stream_coeffs_t coeffs;
371 stream_samples_t samples;
374 /**** ac3 decoder inline functions ****/
376 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec)
378 return &(p_ac3dec->bit_stream.byte_stream);