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