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