1 /*****************************************************************************
2 * ac3_decoder.h : ac3 decoder interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: ac3_decoder.h,v 1.4 2001/03/21 13:42:34 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Renaud Dartus <reno@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /**** ac3 decoder API - public ac3 decoder structures */
27 typedef struct ac3dec_s ac3dec_t;
29 typedef struct ac3_sync_info_s {
30 int sample_rate; /* sample rate in Hz */
31 int frame_size; /* frame size in bytes */
32 int bit_rate; /* nominal bit rate in kbps */
35 typedef struct ac3_byte_stream_s {
41 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
43 int ac3_init (ac3dec_t * p_ac3dec);
44 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info);
45 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer);
46 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec);
48 /**** ac3 decoder API - user functions to be provided to the ac3 decoder ****/
50 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream);
52 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
54 /**** ac3 decoder internal structures ****/
56 /* The following structures are filled in by their corresponding parse_*
57 * functions. See http://www.atsc.org/Standards/A52/a_52.pdf for
58 * full details on each field. Indented fields are used to denote
62 typedef struct syncinfo_s {
63 /* Sync word == 0x0B77 */
65 /* crc for the first 5/8 of the sync block */
67 /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
72 /* Information not in the AC-3 bitstream, but derived */
73 /* Frame size in 16 bit words */
75 /* Bit rate in kilobits */
79 typedef struct bsi_s {
80 /* Bit stream identification == 0x8 */
84 /* Audio coding mode */
86 /* If we're using the centre channel then */
87 /* centre mix level */
89 /* If we're using the surround channel then */
90 /* surround mix level */
92 /* If we're in 2/0 mode then */
93 /* Dolby surround mix level - NOT USED - */
95 /* Low frequency effects on */
97 /* Dialogue Normalization level */
99 /* Compression exists */
101 /* Compression level */
103 /* Language code exists */
107 /* Audio production info exists*/
111 /* If we're in dual mono mode (acmod == 0) then extra stuff */
124 /* Timecode 1 exists */
128 /* Timecode 2 exists */
132 /* Additional bit stream info exists */
134 /* Additional bit stream length - 1 (in bytes) */
136 /* Additional bit stream information (max 64 bytes) */
139 /* Information not in the AC-3 bitstream, but derived */
140 /* Number of channels (excluding LFE)
141 * Derived from acmod */
146 typedef struct audblk_s {
147 /* block switch bit indexed by channel num */
149 /* dither enable bit indexed by channel num */
151 /* dynamic range gain exists */
153 /* dynamic range gain */
155 /* if acmod==0 then */
156 /* dynamic range 2 gain exists */
158 /* dynamic range 2 gain */
160 /* coupling strategy exists */
162 /* coupling in use */
164 /* channel coupled */
166 /* if acmod==2 then */
167 /* Phase flags in use */
169 /* coupling begin frequency code */
171 /* coupling end frequency code */
173 /* coupling band structure bits */
175 /* Do coupling co-ords exist for this channel? */
177 /* Master coupling co-ordinate */
179 /* Per coupling band coupling co-ordinates */
181 u16 cplcomant[5][18];
182 /* Phase flags for dual mono */
184 /* Is there a rematrixing strategy */
186 /* Rematrixing bits */
188 /* Coupling exponent strategy */
190 /* Exponent strategy for full bandwidth channels */
192 /* Exponent strategy for lfe channel */
194 /* Channel bandwidth for independent channels */
196 /* The absolute coupling exponent */
198 /* Coupling channel exponents (D15 mode gives 18 * 12 /3 encoded exponents */
199 u16 cplexps[18 * 12 / 3];
200 /* Sanity checking constant */
202 /* fbw channel exponents */
203 u16 exps[5][252 / 3];
204 /* channel gain range */
206 /* low frequency exponents */
209 /* Bit allocation info */
211 /* Slow decay code */
213 /* Fast decay code */
217 /* dB per bit code */
219 /* masking floor code */
222 /* SNR offset info */
224 /* coarse SNR offset */
226 /* coupling fine SNR offset */
228 /* coupling fast gain code */
230 /* fbw fine SNR offset */
232 /* fbw fast gain code */
234 /* lfe fine SNR offset */
236 /* lfe fast gain code */
239 /* Coupling leak info */
241 /* coupling fast leak initialization */
243 /* coupling slow leak initialization */
246 /* delta bit allocation info */
248 /* coupling delta bit allocation exists */
250 /* fbw delta bit allocation exists */
252 /* number of cpl delta bit segments */
254 /* coupling delta bit allocation offset */
256 /* coupling delta bit allocation length */
258 /* coupling delta bit allocation length */
260 /* number of delta bit segments */
262 /* fbw delta bit allocation offset */
264 /* fbw delta bit allocation length */
266 /* fbw delta bit allocation length */
269 /* skip length exists */
274 /* channel mantissas */
275 // u16 chmant[5][256];
277 /* coupling mantissas */
281 /* coupling mantissas */
284 /* -- Information not in the bitstream, but derived thereof -- */
286 /* Number of coupling sub-bands */
289 /* Number of combined coupling sub-bands
290 * Derived from ncplsubnd and cplbndstrc */
293 /* Number of exponent groups by channel
294 * Derived from strmant, endmant */
297 /* Number of coupling exponent groups
298 * Derived from cplbegf, cplendf, cplexpstr */
301 /* End mantissa numbers of fbw channels */
304 /* Start and end mantissa numbers for the coupling channel */
308 /* Decoded exponent info */
313 /* Bit allocation pointer results */
315 /* FIXME?? figure out exactly how many entries there should be (253-37?) */
320 /* Everything you wanted to know about band structure */
322 * The entire frequency domain is represented by 256 real
323 * floating point fourier coefficients. Only the lower 253
324 * coefficients are actually utilized however. We use arrays
325 * of 256 to be efficient in some cases.
327 * The 5 full bandwidth channels (fbw) can have their higher
328 * frequencies coupled together. These coupled channels then
329 * share their high frequency components.
331 * This coupling band is broken up into 18 sub-bands starting
332 * at mantissa number 37. Each sub-band is 12 bins wide.
334 * There are 50 bit allocation sub-bands which cover the entire
335 * frequency range. The sub-bands are of non-uniform width, and
336 * approximate a 1/6 octave scale.
339 typedef struct stream_coeffs_s
345 typedef struct stream_samples_s
347 float channel[6][256];
350 typedef struct ac3_bit_stream_s
354 ac3_byte_stream_t byte_stream;
356 unsigned int total_bits_read; /* temporary */
359 typedef struct bit_allocate_s
372 /* These store the persistent state of the packed mantissas */
373 typedef struct mantissa_s
384 typedef struct complex_s {
391 typedef struct imdct_s
395 /* Delay buffer for time domain interleaving */
398 /* Twiddle factors for IMDCT */
404 /* Twiddle factor LUT */
422 /* The bit stream structure handles the PES stream at the bit level */
423 ac3_bit_stream_t bit_stream;
432 stream_coeffs_t coeffs;
433 stream_samples_t samples;
435 bit_allocate_t bit_allocate;
440 /**** ac3 decoder inline functions ****/
442 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec)
444 return &(p_ac3dec->bit_stream.byte_stream);