]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder.h
* AC3 IMDCT and downmix functions are now in plugins, --imdct and
[vlc] / src / ac3_decoder / ac3_decoder.h
1 /*****************************************************************************
2  * ac3_decoder.h : ac3 decoder interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: ac3_decoder.h,v 1.9 2001/05/15 16:19:42 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Renaud Dartus <reno@videolan.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /**** ac3 decoder API - public ac3 decoder structures */
26
27 typedef struct ac3dec_s ac3dec_t;
28
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 */
33 } ac3_sync_info_t;
34
35
36 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
37
38 int ac3_init (ac3dec_t * p_ac3dec);
39 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info);
40 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer);
41
42 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
43
44 /**** ac3 decoder internal structures ****/
45
46 /* The following structures are filled in by their corresponding parse_*
47  * functions. See http://www.atsc.org/Standards/A52/a_52.pdf for
48  * full details on each field. Indented fields are used to denote
49  * conditional fields.
50  */
51
52 typedef struct syncinfo_s {
53     /* Sync word == 0x0B77 */
54     /* u16 syncword; */
55     /* crc for the first 5/8 of the sync block */
56     /* u16 crc1; */
57     /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
58     u16 fscod;
59     /* Frame size code */
60     u16 frmsizecod;
61
62     /* Information not in the AC-3 bitstream, but derived */
63     /* Frame size in 16 bit words */
64     u16 frame_size;
65     /* Bit rate in kilobits */
66     //u16 bit_rate;
67 } syncinfo_t;
68
69 typedef struct bsi_s {
70     /* Bit stream identification == 0x8 */
71     u16 bsid;
72     /* Bit stream mode */
73     u16 bsmod;
74     /* Audio coding mode */
75     u16 acmod;
76     /* If we're using the centre channel then */
77         /* centre mix level */
78         u16 cmixlev;
79     /* If we're using the surround channel then */
80         /* surround mix level */
81         u16 surmixlev;
82     /* If we're in 2/0 mode then */
83         /* Dolby surround mix level - NOT USED - */
84         u16 dsurmod;
85     /* Low frequency effects on */
86     u16 lfeon;
87     /* Dialogue Normalization level */
88     u16 dialnorm;
89     /* Compression exists */
90     u16 compre;
91         /* Compression level */
92         u16 compr;
93     /* Language code exists */
94     u16 langcode;
95         /* Language code */
96         u16 langcod;
97     /* Audio production info exists*/
98     u16 audprodie;
99         u16 mixlevel;
100         u16 roomtyp;
101     /* If we're in dual mono mode (acmod == 0) then extra stuff */
102         u16 dialnorm2;
103         u16 compr2e;
104             u16 compr2;
105         u16 langcod2e;
106             u16 langcod2;
107         u16 audprodi2e;
108             u16 mixlevel2;
109             u16 roomtyp2;
110     /* Copyright bit */
111     u16 copyrightb;
112     /* Original bit */
113     u16 origbs;
114     /* Timecode 1 exists */
115     u16 timecod1e;
116         /* Timecode 1 */
117         u16 timecod1;
118     /* Timecode 2 exists */
119     u16 timecod2e;
120         /* Timecode 2 */
121         u16 timecod2;
122     /* Additional bit stream info exists */
123     u16 addbsie;
124         /* Additional bit stream length - 1 (in bytes) */
125         u16 addbsil;
126         /* Additional bit stream information (max 64 bytes) */
127         u8 addbsi[64];
128
129     /* Information not in the AC-3 bitstream, but derived */
130     /* Number of channels (excluding LFE)
131      * Derived from acmod */
132     u16 nfchans;
133 } bsi_t;
134
135 /* more pain */
136 typedef struct audblk_s {
137     /* block switch bit indexed by channel num */
138     u16 blksw[5];
139     /* dither enable bit indexed by channel num */
140     u16 dithflag[5];
141     /* dynamic range gain exists */
142     u16 dynrnge;
143         /* dynamic range gain */
144         u16 dynrng;
145     /* if acmod==0 then */
146     /* dynamic range 2 gain exists */
147     u16 dynrng2e;
148         /* dynamic range 2 gain */
149         u16 dynrng2;
150     /* coupling strategy exists */
151     u16 cplstre;
152         /* coupling in use */
153         u16 cplinu;
154             /* channel coupled */
155             u16 chincpl[5];
156             /* if acmod==2 then */
157                 /* Phase flags in use */
158                 u16 phsflginu;
159             /* coupling begin frequency code */
160             u16 cplbegf;
161             /* coupling end frequency code */
162             u16 cplendf;
163             /* coupling band structure bits */
164             u16 cplbndstrc[18];
165             /* Do coupling co-ords exist for this channel? */
166             u16 cplcoe[5];
167             /* Master coupling co-ordinate */
168             u16 mstrcplco[5];
169             /* Per coupling band coupling co-ordinates */
170             u16 cplcoexp[5][18];
171             u16 cplcomant[5][18];
172             /* Phase flags for dual mono */
173             u16 phsflg[18];
174     /* Is there a rematrixing strategy */
175     u16 rematstr;
176         /* Rematrixing bits */
177         u16 rematflg[4];
178     /* Coupling exponent strategy */
179     u16 cplexpstr;
180     /* Exponent strategy for full bandwidth channels */
181     u16 chexpstr[5];
182     /* Exponent strategy for lfe channel */
183     u16 lfeexpstr;
184     /* Channel bandwidth for independent channels */
185     u16 chbwcod[5];
186         /* The absolute coupling exponent */
187         u16 cplabsexp;
188         /* Coupling channel exponents (D15 mode gives 18 * 12 /3  encoded exponents */
189         u16 cplexps[18 * 12 / 3];
190     /* Sanity checking constant */
191     u32 magic2;
192     /* fbw channel exponents */
193     u16 exps[5][252 / 3];
194     /* channel gain range */
195     u16 gainrng[5];
196     /* low frequency exponents */
197     u16 lfeexps[3];
198
199     /* Bit allocation info */
200     u16 baie;
201         /* Slow decay code */
202         u16 sdcycod;
203         /* Fast decay code */
204         u16 fdcycod;
205         /* Slow gain code */
206         u16 sgaincod;
207         /* dB per bit code */
208         u16 dbpbcod;
209         /* masking floor code */
210         u16 floorcod;
211
212     /* SNR offset info */
213     u16 snroffste;
214         /* coarse SNR offset */
215         u16 csnroffst;
216         /* coupling fine SNR offset */
217         u16 cplfsnroffst;
218         /* coupling fast gain code */
219         u16 cplfgaincod;
220         /* fbw fine SNR offset */
221         u16 fsnroffst[5];
222         /* fbw fast gain code */
223         u16 fgaincod[5];
224         /* lfe fine SNR offset */
225         u16 lfefsnroffst;
226         /* lfe fast gain code */
227         u16 lfefgaincod;
228
229     /* Coupling leak info */
230     u16 cplleake;
231         /* coupling fast leak initialization */
232         u16 cplfleak;
233         /* coupling slow leak initialization */
234         u16 cplsleak;
235
236     /* delta bit allocation info */
237     u16 deltbaie;
238         /* coupling delta bit allocation exists */
239         u16 cpldeltbae;
240         /* fbw delta bit allocation exists */
241         u16 deltbae[5];
242         /* number of cpl delta bit segments */
243         u16 cpldeltnseg;
244             /* coupling delta bit allocation offset */
245             u16 cpldeltoffst[8];
246             /* coupling delta bit allocation length */
247             u16 cpldeltlen[8];
248             /* coupling delta bit allocation length */
249             u16 cpldeltba[8];
250         /* number of delta bit segments */
251         u16 deltnseg[5];
252             /* fbw delta bit allocation offset */
253             u16 deltoffst[5][8];
254             /* fbw delta bit allocation length */
255             u16 deltlen[5][8];
256             /* fbw delta bit allocation length */
257             u16 deltba[5][8];
258
259     /* skip length exists */
260     u16 skiple;
261         /* skip length */
262         u16 skipl;
263
264     /* channel mantissas */
265 //      u16 chmant[5][256];
266
267     /* coupling mantissas */
268     float cpl_flt[ 256 ];
269 //      u16 cplmant[256];
270
271     /* coupling mantissas */
272 //      u16 lfemant[7];
273
274     /* -- Information not in the bitstream, but derived thereof -- */
275
276     /* Number of coupling sub-bands */
277     u16 ncplsubnd;
278
279     /* Number of combined coupling sub-bands
280      * Derived from ncplsubnd and cplbndstrc */
281     u16 ncplbnd;
282
283     /* Number of exponent groups by channel
284      * Derived from strmant, endmant */
285     u16 nchgrps[5];
286
287     /* Number of coupling exponent groups
288      * Derived from cplbegf, cplendf, cplexpstr */
289     u16 ncplgrps;
290
291     /* End mantissa numbers of fbw channels */
292     u16 endmant[5];
293
294     /* Start and end mantissa numbers for the coupling channel */
295     u16 cplstrtmant;
296     u16 cplendmant;
297
298     /* Decoded exponent info */
299     u16 fbw_exp[5][256];
300     u16 cpl_exp[256];
301     u16 lfe_exp[7];
302
303     /* Bit allocation pointer results */
304     u16 fbw_bap[5][256];
305     /* FIXME?? figure out exactly how many entries there should be (253-37?) */
306     u16 cpl_bap[256];
307     u16 lfe_bap[7];
308 } audblk_t;
309
310 /* Everything you wanted to know about band structure */
311
312 /*
313  * The entire frequency domain is represented by 256 real
314  * floating point fourier coefficients. Only the lower 253
315  * coefficients are actually utilized however. We use arrays
316  * of 256 to be efficient in some cases.
317  *
318  * The 5 full bandwidth channels (fbw) can have their higher
319  * frequencies coupled together. These coupled channels then
320  * share their high frequency components.
321  *
322  * This coupling band is broken up into 18 sub-bands starting
323  * at mantissa number 37. Each sub-band is 12 bins wide.
324  *
325  * There are 50 bit allocation sub-bands which cover the entire
326  * frequency range. The sub-bands are of non-uniform width, and
327  * approximate a 1/6 octave scale.
328  */
329
330 typedef struct bit_allocate_s
331 {
332     s16 psd[256];
333     s16 bndpsd[256];
334     s16 excite[256];
335     s16 mask[256];
336     s16 sdecay;
337     s16 fdecay;
338     s16 sgain;
339     s16 dbknee;
340     s16 floor;
341 } bit_allocate_t;
342
343 /* These store the persistent state of the packed mantissas */
344 typedef struct mantissa_s
345 {
346     float q_1[2];
347     float q_2[2];
348     float q_4[1];
349     s32 q_1_pointer;
350     s32 q_2_pointer;
351     s32 q_4_pointer;
352     u16 lfsr_state;
353 } mantissa_t;
354
355 struct ac3dec_s
356 {
357     /*
358      * Input properties
359      */
360
361     /* The bit stream structure handles the PES stream at the bit level */
362     bit_stream_t        bit_stream;
363     int                 i_available;
364     unsigned int        total_bits_read; /* temporary */
365                 
366     /*
367      * Decoder properties
368      */
369     syncinfo_t          syncinfo;
370     bsi_t               bsi;
371     audblk_t            audblk;
372
373     float               samples[6][256];
374     dm_par_t            dm_par;
375
376     bit_allocate_t      bit_allocate;
377     mantissa_t          mantissa;
378     imdct_t             imdct;
379     downmix_t           downmix;
380
381 };
382