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