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