]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder.h
4afb5751d47dda6362647ac107367be7d4208853
[vlc] / src / ac3_decoder / ac3_decoder.h
1 /*****************************************************************************
2  * ac3_decoder.h : ac3 decoder interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Michel Kaempf <maxx@via.ecp.fr>
7  *          Renaud Dartus <reno@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /**** ac3 decoder API - public ac3 decoder structures */
25
26 typedef struct ac3dec_s ac3dec_t;
27
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 */
32 } ac3_sync_info_t;
33
34 typedef struct ac3_byte_stream_s {
35     u8 * p_byte;
36     u8 * p_end;
37     void * info;
38 } ac3_byte_stream_t;
39
40 /**** ac3 decoder API - functions publically provided by the ac3 decoder ****/
41
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);
46
47 /**** ac3 decoder API - user functions to be provided to the ac3 decoder ****/
48
49 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream);
50
51 /**** EVERYTHING AFTER THIS POINT IS PRIVATE ! DO NOT USE DIRECTLY ****/
52
53 /**** ac3 decoder internal structures ****/
54
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
58  * conditional fields.
59  */
60
61 typedef struct syncinfo_s {
62     /* Sync word == 0x0B77 */
63     /* u16 syncword; */
64     /* crc for the first 5/8 of the sync block */
65     /* u16 crc1; */
66     /* Stream Sampling Rate (kHz) 0 = 48, 1 = 44.1, 2 = 32, 3 = reserved */
67     u16 fscod;
68     /* Frame size code */
69     u16 frmsizecod;
70
71     /* Information not in the AC-3 bitstream, but derived */
72     /* Frame size in 16 bit words */
73     u16 frame_size;
74     /* Bit rate in kilobits */
75     //u16 bit_rate;
76 } syncinfo_t;
77
78 typedef struct bsi_s {
79     /* Bit stream identification == 0x8 */
80     u16 bsid;
81     /* Bit stream mode */
82     u16 bsmod;
83     /* Audio coding mode */
84     u16 acmod;
85     /* If we're using the centre channel then */
86         /* centre mix level */
87         u16 cmixlev;
88     /* If we're using the surround channel then */
89         /* surround mix level */
90         u16 surmixlev;
91     /* If we're in 2/0 mode then */
92         /* Dolby surround mix level - NOT USED - */
93         u16 dsurmod;
94     /* Low frequency effects on */
95     u16 lfeon;
96     /* Dialogue Normalization level */
97     u16 dialnorm;
98     /* Compression exists */
99     u16 compre;
100         /* Compression level */
101         u16 compr;
102     /* Language code exists */
103     u16 langcode;
104         /* Language code */
105         u16 langcod;
106     /* Audio production info exists*/
107     u16 audprodie;
108         u16 mixlevel;
109         u16 roomtyp;
110     /* If we're in dual mono mode (acmod == 0) then extra stuff */
111         u16 dialnorm2;
112         u16 compr2e;
113             u16 compr2;
114         u16 langcod2e;
115             u16 langcod2;
116         u16 audprodi2e;
117             u16 mixlevel2;
118             u16 roomtyp2;
119     /* Copyright bit */
120     u16 copyrightb;
121     /* Original bit */
122     u16 origbs;
123     /* Timecode 1 exists */
124     u16 timecod1e;
125         /* Timecode 1 */
126         u16 timecod1;
127     /* Timecode 2 exists */
128     u16 timecod2e;
129         /* Timecode 2 */
130         u16 timecod2;
131     /* Additional bit stream info exists */
132     u16 addbsie;
133         /* Additional bit stream length - 1 (in bytes) */
134         u16 addbsil;
135         /* Additional bit stream information (max 64 bytes) */
136         u8 addbsi[64];
137
138     /* Information not in the AC-3 bitstream, but derived */
139     /* Number of channels (excluding LFE)
140      * Derived from acmod */
141     u16 nfchans;
142 } bsi_t;
143
144 /* more pain */
145 typedef struct audblk_s {
146     /* block switch bit indexed by channel num */
147     u16 blksw[5];
148     /* dither enable bit indexed by channel num */
149     u16 dithflag[5];
150     /* dynamic range gain exists */
151     u16 dynrnge;
152         /* dynamic range gain */
153         u16 dynrng;
154     /* if acmod==0 then */
155     /* dynamic range 2 gain exists */
156     u16 dynrng2e;
157         /* dynamic range 2 gain */
158         u16 dynrng2;
159     /* coupling strategy exists */
160     u16 cplstre;
161         /* coupling in use */
162         u16 cplinu;
163             /* channel coupled */
164             u16 chincpl[5];
165             /* if acmod==2 then */
166                 /* Phase flags in use */
167                 u16 phsflginu;
168             /* coupling begin frequency code */
169             u16 cplbegf;
170             /* coupling end frequency code */
171             u16 cplendf;
172             /* coupling band structure bits */
173             u16 cplbndstrc[18];
174             /* Do coupling co-ords exist for this channel? */
175             u16 cplcoe[5];
176             /* Master coupling co-ordinate */
177             u16 mstrcplco[5];
178             /* Per coupling band coupling co-ordinates */
179             u16 cplcoexp[5][18];
180             u16 cplcomant[5][18];
181             /* Phase flags for dual mono */
182             u16 phsflg[18];
183     /* Is there a rematrixing strategy */
184     u16 rematstr;
185         /* Rematrixing bits */
186         u16 rematflg[4];
187     /* Coupling exponent strategy */
188     u16 cplexpstr;
189     /* Exponent strategy for full bandwidth channels */
190     u16 chexpstr[5];
191     /* Exponent strategy for lfe channel */
192     u16 lfeexpstr;
193     /* Channel bandwidth for independent channels */
194     u16 chbwcod[5];
195         /* The absolute coupling exponent */
196         u16 cplabsexp;
197         /* Coupling channel exponents (D15 mode gives 18 * 12 /3  encoded exponents */
198         u16 cplexps[18 * 12 / 3];
199     /* Sanity checking constant */
200     u32 magic2;
201     /* fbw channel exponents */
202     u16 exps[5][252 / 3];
203     /* channel gain range */
204     u16 gainrng[5];
205     /* low frequency exponents */
206     u16 lfeexps[3];
207
208     /* Bit allocation info */
209     u16 baie;
210         /* Slow decay code */
211         u16 sdcycod;
212         /* Fast decay code */
213         u16 fdcycod;
214         /* Slow gain code */
215         u16 sgaincod;
216         /* dB per bit code */
217         u16 dbpbcod;
218         /* masking floor code */
219         u16 floorcod;
220
221     /* SNR offset info */
222     u16 snroffste;
223         /* coarse SNR offset */
224         u16 csnroffst;
225         /* coupling fine SNR offset */
226         u16 cplfsnroffst;
227         /* coupling fast gain code */
228         u16 cplfgaincod;
229         /* fbw fine SNR offset */
230         u16 fsnroffst[5];
231         /* fbw fast gain code */
232         u16 fgaincod[5];
233         /* lfe fine SNR offset */
234         u16 lfefsnroffst;
235         /* lfe fast gain code */
236         u16 lfefgaincod;
237
238     /* Coupling leak info */
239     u16 cplleake;
240         /* coupling fast leak initialization */
241         u16 cplfleak;
242         /* coupling slow leak initialization */
243         u16 cplsleak;
244
245     /* delta bit allocation info */
246     u16 deltbaie;
247         /* coupling delta bit allocation exists */
248         u16 cpldeltbae;
249         /* fbw delta bit allocation exists */
250         u16 deltbae[5];
251         /* number of cpl delta bit segments */
252         u16 cpldeltnseg;
253             /* coupling delta bit allocation offset */
254             u16 cpldeltoffst[8];
255             /* coupling delta bit allocation length */
256             u16 cpldeltlen[8];
257             /* coupling delta bit allocation length */
258             u16 cpldeltba[8];
259         /* number of delta bit segments */
260         u16 deltnseg[5];
261             /* fbw delta bit allocation offset */
262             u16 deltoffst[5][8];
263             /* fbw delta bit allocation length */
264             u16 deltlen[5][8];
265             /* fbw delta bit allocation length */
266             u16 deltba[5][8];
267
268     /* skip length exists */
269     u16 skiple;
270         /* skip length */
271         u16 skipl;
272
273     /* channel mantissas */
274 //      u16 chmant[5][256];
275
276     /* coupling mantissas */
277     float cplfbw[ 256 ];
278 //      u16 cplmant[256];
279
280     /* coupling mantissas */
281 //      u16 lfemant[7];
282
283     /* -- Information not in the bitstream, but derived thereof -- */
284
285     /* Number of coupling sub-bands */
286     u16 ncplsubnd;
287
288     /* Number of combined coupling sub-bands
289      * Derived from ncplsubnd and cplbndstrc */
290     u16 ncplbnd;
291
292     /* Number of exponent groups by channel
293      * Derived from strmant, endmant */
294     u16 nchgrps[5];
295
296     /* Number of coupling exponent groups
297      * Derived from cplbegf, cplendf, cplexpstr */
298     u16 ncplgrps;
299
300     /* End mantissa numbers of fbw channels */
301     u16 endmant[5];
302
303     /* Start and end mantissa numbers for the coupling channel */
304     u16 cplstrtmant;
305     u16 cplendmant;
306
307     /* Decoded exponent info */
308     u16 fbw_exp[5][256];
309     u16 cpl_exp[256];
310     u16 lfe_exp[7];
311
312     /* Bit allocation pointer results */
313     u16 fbw_bap[5][256];
314     /* FIXME?? figure out exactly how many entries there should be (253-37?) */
315     u16 cpl_bap[256];
316     u16 lfe_bap[7];
317 } audblk_t;
318
319 /* Everything you wanted to know about band structure */
320 /*
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.
325  *
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.
329  *
330  * This coupling band is broken up into 18 sub-bands starting
331  * at mantissa number 37. Each sub-band is 12 bins wide.
332  *
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.
336  */
337
338 typedef struct stream_coeffs_s
339 {
340     float fbw[5][256];
341     float lfe[256];
342 } stream_coeffs_t;
343
344 typedef struct stream_samples_s
345 {
346     float channel[6][256];
347 } stream_samples_t;
348
349 typedef struct ac3_bit_stream_s
350 {
351     u32 buffer;
352     int i_available;
353     ac3_byte_stream_t byte_stream;
354
355     unsigned int total_bits_read; /* temporary */
356 } ac3_bit_stream_t;
357
358 typedef struct bit_allocate_s
359 {
360     s16 psd[256];
361     s16 bndpsd[256];
362     s16 excite[256];
363     s16 mask[256];
364     s16 sdecay;
365     s16 fdecay;
366     s16 sgain;
367     s16 dbknee;
368     s16 floor;
369 } bit_allocate_t;
370
371 /* These store the persistent state of the packed mantissas */
372 typedef struct mantissa_s
373 {
374     float q_1[2];
375     float q_2[2];
376     float q_4[1];
377     s32 q_1_pointer;
378     s32 q_2_pointer;
379     s32 q_4_pointer;
380     u16 lfsr_state;
381 } mantissa_t;
382
383 typedef struct complex_s {
384     float real;
385     float imag;
386 } complex_t;
387
388 #define N 512
389
390 typedef struct imdct_s
391 {
392     complex_t buf[N/4];
393
394     /* Delay buffer for time domain interleaving */
395     float delay[6][256];
396
397     /* Twiddle factors for IMDCT */
398     float xcos1[N/4];
399     float xsin1[N/4];
400     float xcos2[N/8];
401     float xsin2[N/8];
402     
403     /* Twiddle factor LUT */
404     complex_t *w[7];
405     complex_t w_1[1];
406     complex_t w_2[2];
407     complex_t w_4[4];
408     complex_t w_8[8];
409     complex_t w_16[16];
410     complex_t w_32[32];
411     complex_t w_64[64];
412
413 } imdct_t;
414
415 struct ac3dec_s
416 {
417     /*
418      * Input properties
419      */
420
421     /* The bit stream structure handles the PES stream at the bit level */
422     ac3_bit_stream_t    bit_stream;
423
424     /*
425      * Decoder properties
426      */
427     syncinfo_t          syncinfo;
428     bsi_t               bsi;
429     audblk_t            audblk;
430
431     stream_coeffs_t     coeffs;
432     stream_samples_t    samples;
433
434     bit_allocate_t      bit_allocate;
435     mantissa_t          mantissa;
436     imdct_t             imdct;
437 };
438
439 /**** ac3 decoder inline functions ****/
440
441 static ac3_byte_stream_t * ac3_byte_stream (ac3dec_t * p_ac3dec)
442 {
443     return &(p_ac3dec->bit_stream.byte_stream);
444 }