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