]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_parse.c
Encore un commit venu tout droit des abysses de l'enfer, d�sol� pour
[vlc] / src / ac3_decoder / ac3_parse.c
1 /*****************************************************************************
2  * ac3_parse.c: ac3 parsing procedures
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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 #include "int_types.h"
25 #include "ac3_decoder.h"
26 #include "ac3_internal.h"
27 #include "ac3_bit_stream.h"
28
29 /* Misc LUT */
30 static u16 nfchans[] = { 2, 1, 2, 3, 3, 4, 4, 5 };
31
32 struct frmsize_s {
33     u16 bit_rate;
34     u16 frm_size[3];
35 };
36
37 static struct frmsize_s frmsizecod_tbl[] = {
38       { 32  ,{64   ,69   ,96   } },
39       { 32  ,{64   ,70   ,96   } },
40       { 40  ,{80   ,87   ,120  } },
41       { 40  ,{80   ,88   ,120  } },
42       { 48  ,{96   ,104  ,144  } },
43       { 48  ,{96   ,105  ,144  } },
44       { 56  ,{112  ,121  ,168  } },
45       { 56  ,{112  ,122  ,168  } },
46       { 64  ,{128  ,139  ,192  } },
47       { 64  ,{128  ,140  ,192  } },
48       { 80  ,{160  ,174  ,240  } },
49       { 80  ,{160  ,175  ,240  } },
50       { 96  ,{192  ,208  ,288  } },
51       { 96  ,{192  ,209  ,288  } },
52       { 112 ,{224  ,243  ,336  } },
53       { 112 ,{224  ,244  ,336  } },
54       { 128 ,{256  ,278  ,384  } },
55       { 128 ,{256  ,279  ,384  } },
56       { 160 ,{320  ,348  ,480  } },
57       { 160 ,{320  ,349  ,480  } },
58       { 192 ,{384  ,417  ,576  } },
59       { 192 ,{384  ,418  ,576  } },
60       { 224 ,{448  ,487  ,672  } },
61       { 224 ,{448  ,488  ,672  } },
62       { 256 ,{512  ,557  ,768  } },
63       { 256 ,{512  ,558  ,768  } },
64       { 320 ,{640  ,696  ,960  } },
65       { 320 ,{640  ,697  ,960  } },
66       { 384 ,{768  ,835  ,1152 } },
67       { 384 ,{768  ,836  ,1152 } },
68       { 448 ,{896  ,975  ,1344 } },
69       { 448 ,{896  ,976  ,1344 } },
70       { 512 ,{1024 ,1114 ,1536 } },
71       { 512 ,{1024 ,1115 ,1536 } },
72       { 576 ,{1152 ,1253 ,1728 } },
73       { 576 ,{1152 ,1254 ,1728 } },
74       { 640 ,{1280 ,1393 ,1920 } },
75       { 640 ,{1280 ,1394 ,1920 } }};
76
77 static int fscod_tbl[] = {48000, 44100, 32000};
78
79 /* Parse a syncinfo structure */
80 int ac3_sync_frame (ac3dec_t * p_ac3dec, ac3_sync_info_t * p_sync_info) 
81 {
82     int buf;
83
84     p_ac3dec->bit_stream.total_bits_read = 0;
85     p_ac3dec->bit_stream.i_available = 0;
86
87     /* sync word - should be 0x0b77 */
88     NeedBits (&(p_ac3dec->bit_stream), 16);
89     buf =  p_ac3dec->bit_stream.buffer >> (32 - 16);
90     DumpBits (&(p_ac3dec->bit_stream), 16);
91     if (buf != 0x0b77)
92         return 1;
93
94     /* Get crc1 - we don't actually use this data though */
95     NeedBits (&(p_ac3dec->bit_stream), 16);
96     DumpBits (&(p_ac3dec->bit_stream), 16);
97
98     /* Get the sampling rate */
99     NeedBits (&(p_ac3dec->bit_stream), 2);
100     p_ac3dec->syncinfo.fscod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
101     DumpBits (&(p_ac3dec->bit_stream), 2);
102
103     if (p_ac3dec->syncinfo.fscod >= 3)
104         return 1;
105
106     /* Get the frame size code */
107     NeedBits (&(p_ac3dec->bit_stream), 6);
108     p_ac3dec->syncinfo.frmsizecod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 6));
109     DumpBits (&(p_ac3dec->bit_stream), 6);
110
111     if (p_ac3dec->syncinfo.frmsizecod >= 38)
112         return 1;
113
114     p_sync_info->bit_rate = frmsizecod_tbl[p_ac3dec->syncinfo.frmsizecod].bit_rate;
115
116     p_ac3dec->syncinfo.frame_size = frmsizecod_tbl[p_ac3dec->syncinfo.frmsizecod].frm_size[p_ac3dec->syncinfo.fscod];
117     p_sync_info->frame_size = 2 * p_ac3dec->syncinfo.frame_size;
118
119     p_sync_info->sample_rate = fscod_tbl[p_ac3dec->syncinfo.fscod];
120
121     return 0;
122 }
123
124 /*
125  * This routine fills a bsi struct from the AC3 stream
126  */
127 int parse_bsi (ac3dec_t * p_ac3dec)
128 {
129     u32 i;
130
131     /* Check the AC-3 version number */
132     NeedBits (&(p_ac3dec->bit_stream), 5);
133     p_ac3dec->bsi.bsid = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
134     DumpBits (&(p_ac3dec->bit_stream), 5);
135
136     if (p_ac3dec->bsi.bsid > 8)
137         return 1;
138
139     /* Get the audio service provided by the steram */
140     NeedBits (&(p_ac3dec->bit_stream), 3);
141     p_ac3dec->bsi.bsmod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
142     DumpBits (&(p_ac3dec->bit_stream), 3);
143
144     /* Get the audio coding mode (ie how many channels)*/
145     NeedBits (&(p_ac3dec->bit_stream), 3);
146     p_ac3dec->bsi.acmod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
147     DumpBits (&(p_ac3dec->bit_stream), 3);
148     /* Predecode the number of full bandwidth channels as we use this
149      * number a lot */
150     p_ac3dec->bsi.nfchans = nfchans[p_ac3dec->bsi.acmod];
151
152     /* If it is in use, get the centre channel mix level */
153     if ((p_ac3dec->bsi.acmod & 0x1) && (p_ac3dec->bsi.acmod != 0x1)) {
154         NeedBits (&(p_ac3dec->bit_stream), 2);
155         p_ac3dec->bsi.cmixlev = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
156         DumpBits (&(p_ac3dec->bit_stream), 2);
157     }
158
159     /* If it is in use, get the surround channel mix level */
160     if (p_ac3dec->bsi.acmod & 0x4) {
161         NeedBits (&(p_ac3dec->bit_stream), 2);
162         p_ac3dec->bsi.surmixlev = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
163         DumpBits (&(p_ac3dec->bit_stream), 2);
164     }
165
166     /* Get the dolby surround mode if in 2/0 mode */
167     if (p_ac3dec->bsi.acmod == 0x2) {
168         NeedBits (&(p_ac3dec->bit_stream), 2);
169         p_ac3dec->bsi.dsurmod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
170         DumpBits (&(p_ac3dec->bit_stream), 2);
171     }
172
173     /* Is the low frequency effects channel on? */
174     NeedBits (&(p_ac3dec->bit_stream), 1);
175     p_ac3dec->bsi.lfeon = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
176     DumpBits (&(p_ac3dec->bit_stream), 1);
177
178     /* Get the dialogue normalization level */
179     NeedBits (&(p_ac3dec->bit_stream), 5);
180     p_ac3dec->bsi.dialnorm = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
181     DumpBits (&(p_ac3dec->bit_stream), 5);
182
183     /* Does compression gain exist? */
184     NeedBits (&(p_ac3dec->bit_stream), 1);
185     p_ac3dec->bsi.compre = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
186     DumpBits (&(p_ac3dec->bit_stream), 1);
187     if (p_ac3dec->bsi.compre) {
188         /* Get compression gain */
189         NeedBits (&(p_ac3dec->bit_stream), 8);
190         p_ac3dec->bsi.compr = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
191         DumpBits (&(p_ac3dec->bit_stream), 8);
192     }
193
194     /* Does language code exist? */
195     NeedBits (&(p_ac3dec->bit_stream), 1);
196     p_ac3dec->bsi.langcode = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
197     DumpBits (&(p_ac3dec->bit_stream), 1);
198     if (p_ac3dec->bsi.langcode) {
199         /* Get langauge code */
200         NeedBits (&(p_ac3dec->bit_stream), 8);
201         p_ac3dec->bsi.langcod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
202         DumpBits (&(p_ac3dec->bit_stream), 8);
203     }
204
205     /* Does audio production info exist? */
206     NeedBits (&(p_ac3dec->bit_stream), 1);
207     p_ac3dec->bsi.audprodie = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
208     DumpBits (&(p_ac3dec->bit_stream), 1);
209     if (p_ac3dec->bsi.audprodie) {
210         /* Get mix level */
211         NeedBits (&(p_ac3dec->bit_stream), 5);
212         p_ac3dec->bsi.mixlevel = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
213         DumpBits (&(p_ac3dec->bit_stream), 5);
214
215         /* Get room type */
216         NeedBits (&(p_ac3dec->bit_stream), 2);
217         p_ac3dec->bsi.roomtyp = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
218         DumpBits (&(p_ac3dec->bit_stream), 2);
219     }
220
221     /* If we're in dual mono mode then get some extra info */
222     if (p_ac3dec->bsi.acmod ==0) {
223         /* Get the dialogue normalization level two */
224         NeedBits (&(p_ac3dec->bit_stream), 5);
225         p_ac3dec->bsi.dialnorm2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
226         DumpBits (&(p_ac3dec->bit_stream), 5);
227
228         /* Does compression gain two exist? */
229         NeedBits (&(p_ac3dec->bit_stream), 1);
230         p_ac3dec->bsi.compr2e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
231         DumpBits (&(p_ac3dec->bit_stream), 1);
232         if (p_ac3dec->bsi.compr2e) {
233             /* Get compression gain two */
234             NeedBits (&(p_ac3dec->bit_stream), 8);
235             p_ac3dec->bsi.compr2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
236             DumpBits (&(p_ac3dec->bit_stream), 8);
237         }
238
239         /* Does language code two exist? */
240         NeedBits (&(p_ac3dec->bit_stream), 1);
241         p_ac3dec->bsi.langcod2e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
242         DumpBits (&(p_ac3dec->bit_stream), 1);
243         if (p_ac3dec->bsi.langcod2e) {
244             /* Get langauge code two */
245             NeedBits (&(p_ac3dec->bit_stream), 8);
246             p_ac3dec->bsi.langcod2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
247             DumpBits (&(p_ac3dec->bit_stream), 8);
248         }
249
250         /* Does audio production info two exist? */
251         NeedBits (&(p_ac3dec->bit_stream), 1);
252         p_ac3dec->bsi.audprodi2e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
253         DumpBits (&(p_ac3dec->bit_stream), 1);
254         if (p_ac3dec->bsi.audprodi2e) {
255             /* Get mix level two */
256             NeedBits (&(p_ac3dec->bit_stream), 5);
257             p_ac3dec->bsi.mixlevel2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
258             DumpBits (&(p_ac3dec->bit_stream), 5);
259
260             /* Get room type two */
261             NeedBits (&(p_ac3dec->bit_stream), 2);
262             p_ac3dec->bsi.roomtyp2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
263             DumpBits (&(p_ac3dec->bit_stream), 2);
264         }
265     }
266
267     /* Get the copyright bit */
268     NeedBits (&(p_ac3dec->bit_stream), 1);
269     p_ac3dec->bsi.copyrightb = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
270     DumpBits (&(p_ac3dec->bit_stream), 1);
271
272     /* Get the original bit */
273     NeedBits (&(p_ac3dec->bit_stream), 1);
274     p_ac3dec->bsi.origbs = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
275     DumpBits (&(p_ac3dec->bit_stream), 1);
276
277     /* Does timecode one exist? */
278     NeedBits (&(p_ac3dec->bit_stream), 1);
279     p_ac3dec->bsi.timecod1e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
280     DumpBits (&(p_ac3dec->bit_stream), 1);
281
282     if (p_ac3dec->bsi.timecod1e) {
283         NeedBits (&(p_ac3dec->bit_stream), 14);
284         p_ac3dec->bsi.timecod1 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 14));
285         DumpBits (&(p_ac3dec->bit_stream), 14);
286     }
287
288     /* Does timecode two exist? */
289     NeedBits (&(p_ac3dec->bit_stream), 1);
290     p_ac3dec->bsi.timecod2e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
291     DumpBits (&(p_ac3dec->bit_stream), 1);
292
293     if (p_ac3dec->bsi.timecod2e) {
294         NeedBits (&(p_ac3dec->bit_stream), 14);
295         p_ac3dec->bsi.timecod2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 14));
296         DumpBits (&(p_ac3dec->bit_stream), 14);
297     }
298
299     /* Does addition info exist? */
300     NeedBits (&(p_ac3dec->bit_stream), 1);
301     p_ac3dec->bsi.addbsie = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
302     DumpBits (&(p_ac3dec->bit_stream), 1);
303
304     if (p_ac3dec->bsi.addbsie) {
305         /* Get how much info is there */
306         NeedBits (&(p_ac3dec->bit_stream), 6);
307         p_ac3dec->bsi.addbsil = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 6));
308         DumpBits (&(p_ac3dec->bit_stream), 6);
309
310         /* Get the additional info */
311         for (i=0;i<(p_ac3dec->bsi.addbsil + 1);i++) {
312             NeedBits (&(p_ac3dec->bit_stream), 8);
313             p_ac3dec->bsi.addbsi[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
314             DumpBits (&(p_ac3dec->bit_stream), 8);
315         }
316     }
317
318     return 0;
319 }
320
321 /* More pain inducing parsing */
322 int parse_audblk (ac3dec_t * p_ac3dec, int blknum)
323 {
324     int i, j;
325
326     for (i=0; i < p_ac3dec->bsi.nfchans; i++) {
327         /* Is this channel an interleaved 256 + 256 block ? */
328         NeedBits (&(p_ac3dec->bit_stream), 1);
329         p_ac3dec->audblk.blksw[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
330         DumpBits (&(p_ac3dec->bit_stream), 1);
331     }
332
333     for (i=0; i < p_ac3dec->bsi.nfchans; i++) {
334         /* Should we dither this channel? */
335         NeedBits (&(p_ac3dec->bit_stream), 1);
336         p_ac3dec->audblk.dithflag[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
337         DumpBits (&(p_ac3dec->bit_stream), 1);
338     }
339
340     /* Does dynamic range control exist? */
341     NeedBits (&(p_ac3dec->bit_stream), 1);
342     p_ac3dec->audblk.dynrnge = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
343     DumpBits (&(p_ac3dec->bit_stream), 1);
344     if (p_ac3dec->audblk.dynrnge) {
345         /* Get dynamic range info */
346         NeedBits (&(p_ac3dec->bit_stream), 8);
347         p_ac3dec->audblk.dynrng = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
348         DumpBits (&(p_ac3dec->bit_stream), 8);
349     }
350
351     /* If we're in dual mono mode then get the second channel DR info */
352     if (p_ac3dec->bsi.acmod == 0) {
353         /* Does dynamic range control two exist? */
354         NeedBits (&(p_ac3dec->bit_stream), 1);
355         p_ac3dec->audblk.dynrng2e = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
356         DumpBits (&(p_ac3dec->bit_stream), 1);
357         if (p_ac3dec->audblk.dynrng2e) {
358             /* Get dynamic range info */
359             NeedBits (&(p_ac3dec->bit_stream), 8);
360             p_ac3dec->audblk.dynrng2 = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 8));
361             DumpBits (&(p_ac3dec->bit_stream), 8);
362         }
363     }
364
365     /* Does coupling strategy exist? */
366     NeedBits (&(p_ac3dec->bit_stream), 1);
367     p_ac3dec->audblk.cplstre = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
368     DumpBits (&(p_ac3dec->bit_stream), 1);
369
370     if ((!blknum) && (!p_ac3dec->audblk.cplstre))
371         return 1;
372
373     if (p_ac3dec->audblk.cplstre) {
374         /* Is coupling turned on? */
375         NeedBits (&(p_ac3dec->bit_stream), 1);
376         p_ac3dec->audblk.cplinu = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
377         DumpBits (&(p_ac3dec->bit_stream), 1);
378         if (p_ac3dec->audblk.cplinu) {
379             int nb_coupled_channels;
380
381             nb_coupled_channels = 0;
382             for (i=0; i < p_ac3dec->bsi.nfchans; i++) {
383                 NeedBits (&(p_ac3dec->bit_stream), 1);
384                 p_ac3dec->audblk.chincpl[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
385                 DumpBits (&(p_ac3dec->bit_stream), 1);
386                 if (p_ac3dec->audblk.chincpl[i])
387                     nb_coupled_channels++;
388             }
389             if (nb_coupled_channels < 2)
390                 return 1;
391
392             if (p_ac3dec->bsi.acmod == 0x2) {
393                 NeedBits (&(p_ac3dec->bit_stream), 1);
394                 p_ac3dec->audblk.phsflginu = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
395                 DumpBits (&(p_ac3dec->bit_stream), 1);
396             }
397             NeedBits (&(p_ac3dec->bit_stream), 4);
398             p_ac3dec->audblk.cplbegf = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
399             DumpBits (&(p_ac3dec->bit_stream), 4);
400             NeedBits (&(p_ac3dec->bit_stream), 4);
401             p_ac3dec->audblk.cplendf = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
402             DumpBits (&(p_ac3dec->bit_stream), 4);
403
404             if (p_ac3dec->audblk.cplbegf > p_ac3dec->audblk.cplendf + 2)
405                 return 1;
406
407             p_ac3dec->audblk.ncplsubnd = (p_ac3dec->audblk.cplendf + 2) - p_ac3dec->audblk.cplbegf + 1;
408
409             /* Calculate the start and end bins of the coupling channel */
410             p_ac3dec->audblk.cplstrtmant = (p_ac3dec->audblk.cplbegf * 12) + 37 ;
411             p_ac3dec->audblk.cplendmant = ((p_ac3dec->audblk.cplendf + 3) * 12) + 37;
412
413             /* The number of combined subbands is ncplsubnd minus each combined
414              * band */
415             p_ac3dec->audblk.ncplbnd = p_ac3dec->audblk.ncplsubnd;
416
417             for (i=1; i< p_ac3dec->audblk.ncplsubnd; i++) {
418                 NeedBits (&(p_ac3dec->bit_stream), 1);
419                 p_ac3dec->audblk.cplbndstrc[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
420                 DumpBits (&(p_ac3dec->bit_stream), 1);
421                 p_ac3dec->audblk.ncplbnd -= p_ac3dec->audblk.cplbndstrc[i];
422             }
423         }
424     }
425
426     if (p_ac3dec->audblk.cplinu) {
427         /* Loop through all the channels and get their coupling co-ords */
428         for (i=0; i < p_ac3dec->bsi.nfchans;i++) {
429             if (!p_ac3dec->audblk.chincpl[i])
430                 continue;
431
432             /* Is there new coupling co-ordinate info? */
433             NeedBits (&(p_ac3dec->bit_stream), 1);
434             p_ac3dec->audblk.cplcoe[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
435             DumpBits (&(p_ac3dec->bit_stream), 1);
436
437             if ((!blknum) && (!p_ac3dec->audblk.cplcoe[i]))
438                 return 1;
439
440             if (p_ac3dec->audblk.cplcoe[i]) {
441                 NeedBits (&(p_ac3dec->bit_stream), 2);
442                 p_ac3dec->audblk.mstrcplco[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
443                 DumpBits (&(p_ac3dec->bit_stream), 2);
444                 for (j=0;j < p_ac3dec->audblk.ncplbnd; j++) {
445                     NeedBits (&(p_ac3dec->bit_stream), 4);
446                     p_ac3dec->audblk.cplcoexp[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
447                     DumpBits (&(p_ac3dec->bit_stream), 4);
448                     NeedBits (&(p_ac3dec->bit_stream), 4);
449                     p_ac3dec->audblk.cplcomant[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
450                     DumpBits (&(p_ac3dec->bit_stream), 4);
451                 }
452             }
453         }
454
455         /* If we're in dual mono mode, there's going to be some phase info */
456         if ((p_ac3dec->bsi.acmod == 0x2) && p_ac3dec->audblk.phsflginu &&
457            (p_ac3dec->audblk.cplcoe[0] || p_ac3dec->audblk.cplcoe[1])) {
458             for (j=0; j < p_ac3dec->audblk.ncplbnd; j++) {
459                 NeedBits (&(p_ac3dec->bit_stream), 1);
460                 p_ac3dec->audblk.phsflg[j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
461                 DumpBits (&(p_ac3dec->bit_stream), 1);
462             }
463
464         }
465     }
466
467     /* If we're in dual mono mode, there may be a rematrix strategy */
468     if (p_ac3dec->bsi.acmod == 0x2) {
469         NeedBits (&(p_ac3dec->bit_stream), 1);
470         p_ac3dec->audblk.rematstr = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
471         DumpBits (&(p_ac3dec->bit_stream), 1);
472
473         if ((!blknum) && (!p_ac3dec->audblk.rematstr))
474             return 1;
475
476         if (p_ac3dec->audblk.rematstr) {
477             if (p_ac3dec->audblk.cplinu == 0) {
478                 for (i = 0; i < 4; i++) {
479                     NeedBits (&(p_ac3dec->bit_stream), 1);
480                     p_ac3dec->audblk.rematflg[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
481                     DumpBits (&(p_ac3dec->bit_stream), 1);
482                 }
483             }
484             if ((p_ac3dec->audblk.cplbegf > 2) && p_ac3dec->audblk.cplinu) {
485                 for (i = 0; i < 4; i++) {
486                     NeedBits (&(p_ac3dec->bit_stream), 1);
487                     p_ac3dec->audblk.rematflg[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
488                     DumpBits (&(p_ac3dec->bit_stream), 1);
489                 }
490             }
491             if ((p_ac3dec->audblk.cplbegf <= 2) && p_ac3dec->audblk.cplinu) {
492                 for (i = 0; i < 3; i++) {
493                     NeedBits (&(p_ac3dec->bit_stream), 1);
494                     p_ac3dec->audblk.rematflg[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
495                     DumpBits (&(p_ac3dec->bit_stream), 1);
496                 }
497             }
498             if ((p_ac3dec->audblk.cplbegf == 0) && p_ac3dec->audblk.cplinu)
499                 for (i = 0; i < 2; i++) {
500                     NeedBits (&(p_ac3dec->bit_stream), 1);
501                     p_ac3dec->audblk.rematflg[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
502                     DumpBits (&(p_ac3dec->bit_stream), 1);
503                 }
504         }
505     }
506
507     if (p_ac3dec->audblk.cplinu)
508     {
509         /* Get the coupling channel exponent strategy */
510         NeedBits (&(p_ac3dec->bit_stream), 2);
511         p_ac3dec->audblk.cplexpstr = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
512         DumpBits (&(p_ac3dec->bit_stream), 2);
513
514         if ((!blknum) && (p_ac3dec->audblk.cplexpstr == EXP_REUSE))
515             return 1;
516
517         if (p_ac3dec->audblk.cplexpstr==0)
518             p_ac3dec->audblk.ncplgrps = 0;
519         else
520             p_ac3dec->audblk.ncplgrps = (p_ac3dec->audblk.cplendmant - p_ac3dec->audblk.cplstrtmant) /
521                 (3 << (p_ac3dec->audblk.cplexpstr-1));
522
523     }
524
525     for (i = 0; i < p_ac3dec->bsi.nfchans; i++) {
526         NeedBits (&(p_ac3dec->bit_stream), 2);
527         p_ac3dec->audblk.chexpstr[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
528         DumpBits (&(p_ac3dec->bit_stream), 2);
529
530         if ((!blknum) && (p_ac3dec->audblk.chexpstr[i] == EXP_REUSE))
531             return 1;
532     }
533
534     /* Get the exponent strategy for lfe channel */
535     if (p_ac3dec->bsi.lfeon) {
536         NeedBits (&(p_ac3dec->bit_stream), 1);
537         p_ac3dec->audblk.lfeexpstr = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
538         DumpBits (&(p_ac3dec->bit_stream), 1);
539
540         if ((!blknum) && (p_ac3dec->audblk.lfeexpstr == EXP_REUSE))
541             return 1;
542     }
543
544     /* Determine the bandwidths of all the fbw channels */
545     for (i = 0; i < p_ac3dec->bsi.nfchans; i++) {
546         u16 grp_size;
547
548         if (p_ac3dec->audblk.chexpstr[i] != EXP_REUSE) {
549             if (p_ac3dec->audblk.cplinu && p_ac3dec->audblk.chincpl[i]) {
550                 p_ac3dec->audblk.endmant[i] = p_ac3dec->audblk.cplstrtmant;
551             } else {
552                 NeedBits (&(p_ac3dec->bit_stream), 6);
553                 p_ac3dec->audblk.chbwcod[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 6));
554                 DumpBits (&(p_ac3dec->bit_stream), 6);
555
556                 if (p_ac3dec->audblk.chbwcod[i] > 60)
557                     return 1;
558
559                 p_ac3dec->audblk.endmant[i] = ((p_ac3dec->audblk.chbwcod[i] + 12) * 3) + 37;
560             }
561
562             /* Calculate the number of exponent groups to fetch */
563             grp_size =  3 * (1 << (p_ac3dec->audblk.chexpstr[i] - 1));
564             p_ac3dec->audblk.nchgrps[i] = (p_ac3dec->audblk.endmant[i] - 1 + (grp_size - 3)) / grp_size;
565         }
566     }
567
568     /* Get the coupling exponents if they exist */
569     if (p_ac3dec->audblk.cplinu && (p_ac3dec->audblk.cplexpstr != EXP_REUSE)) {
570         NeedBits (&(p_ac3dec->bit_stream), 4);
571         p_ac3dec->audblk.cplabsexp = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
572         DumpBits (&(p_ac3dec->bit_stream), 4);
573         for (i=0; i< p_ac3dec->audblk.ncplgrps;i++) {
574             NeedBits (&(p_ac3dec->bit_stream), 7);
575             p_ac3dec->audblk.cplexps[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 7));
576             DumpBits (&(p_ac3dec->bit_stream), 7);
577
578             if (p_ac3dec->audblk.cplexps[i] >= 125)
579                 return 1;
580         }
581     }
582
583     /* Get the fwb channel exponents */
584     for (i=0; i < p_ac3dec->bsi.nfchans; i++) {
585         if (p_ac3dec->audblk.chexpstr[i] != EXP_REUSE) {
586             NeedBits (&(p_ac3dec->bit_stream), 4);
587             p_ac3dec->audblk.exps[i][0] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
588             DumpBits (&(p_ac3dec->bit_stream), 4);
589             for (j=1; j<=p_ac3dec->audblk.nchgrps[i];j++) {
590                 NeedBits (&(p_ac3dec->bit_stream), 7);
591                 p_ac3dec->audblk.exps[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 7));
592                 DumpBits (&(p_ac3dec->bit_stream), 7);
593                 if (p_ac3dec->audblk.exps[i][j] >= 125)
594                     return 1;
595             }
596             NeedBits (&(p_ac3dec->bit_stream), 2);
597             p_ac3dec->audblk.gainrng[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
598             DumpBits (&(p_ac3dec->bit_stream), 2);
599         }
600     }
601
602     /* Get the lfe channel exponents */
603     if (p_ac3dec->bsi.lfeon && (p_ac3dec->audblk.lfeexpstr != EXP_REUSE)) {
604         NeedBits (&(p_ac3dec->bit_stream), 4);
605         p_ac3dec->audblk.lfeexps[0] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
606         DumpBits (&(p_ac3dec->bit_stream), 4);
607         NeedBits (&(p_ac3dec->bit_stream), 7);
608         p_ac3dec->audblk.lfeexps[1] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 7));
609         DumpBits (&(p_ac3dec->bit_stream), 7);
610         if (p_ac3dec->audblk.lfeexps[1] >= 125)
611             return 1;
612         NeedBits (&(p_ac3dec->bit_stream), 7);
613         p_ac3dec->audblk.lfeexps[2] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 7));
614         DumpBits (&(p_ac3dec->bit_stream), 7);
615         if (p_ac3dec->audblk.lfeexps[2] >= 125)
616             return 1;
617     }
618
619     /* Get the parametric bit allocation parameters */
620     NeedBits (&(p_ac3dec->bit_stream), 1);
621     p_ac3dec->audblk.baie = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
622     DumpBits (&(p_ac3dec->bit_stream), 1);
623
624     if ((!blknum) && (!p_ac3dec->audblk.baie))
625         return 1;
626
627     if (p_ac3dec->audblk.baie) {
628         NeedBits (&(p_ac3dec->bit_stream), 2);
629         p_ac3dec->audblk.sdcycod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
630         DumpBits (&(p_ac3dec->bit_stream), 2);
631         NeedBits (&(p_ac3dec->bit_stream), 2);
632         p_ac3dec->audblk.fdcycod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
633         DumpBits (&(p_ac3dec->bit_stream), 2);
634         NeedBits (&(p_ac3dec->bit_stream), 2);
635         p_ac3dec->audblk.sgaincod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
636         DumpBits (&(p_ac3dec->bit_stream), 2);
637         NeedBits (&(p_ac3dec->bit_stream), 2);
638         p_ac3dec->audblk.dbpbcod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
639         DumpBits (&(p_ac3dec->bit_stream), 2);
640         NeedBits (&(p_ac3dec->bit_stream), 3);
641         p_ac3dec->audblk.floorcod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
642         DumpBits (&(p_ac3dec->bit_stream), 3);
643     }
644
645     /* Get the SNR off set info if it exists */
646     NeedBits (&(p_ac3dec->bit_stream), 1);
647     p_ac3dec->audblk.snroffste = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
648     DumpBits (&(p_ac3dec->bit_stream), 1);
649     if ((!blknum) && (!p_ac3dec->audblk.snroffste))
650         return 1;
651
652     if (p_ac3dec->audblk.snroffste) {
653         NeedBits (&(p_ac3dec->bit_stream), 6);
654         p_ac3dec->audblk.csnroffst = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 6));
655         DumpBits (&(p_ac3dec->bit_stream), 6);
656
657         if (p_ac3dec->audblk.cplinu) {
658             NeedBits (&(p_ac3dec->bit_stream), 4);
659             p_ac3dec->audblk.cplfsnroffst = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
660             DumpBits (&(p_ac3dec->bit_stream), 4);
661             NeedBits (&(p_ac3dec->bit_stream), 3);
662             p_ac3dec->audblk.cplfgaincod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
663             DumpBits (&(p_ac3dec->bit_stream), 3);
664         }
665
666         for (i = 0;i < p_ac3dec->bsi.nfchans; i++) {
667             NeedBits (&(p_ac3dec->bit_stream), 4);
668             p_ac3dec->audblk.fsnroffst[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
669             DumpBits (&(p_ac3dec->bit_stream), 4);
670             NeedBits (&(p_ac3dec->bit_stream), 3);
671             p_ac3dec->audblk.fgaincod[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
672             DumpBits (&(p_ac3dec->bit_stream), 3);
673         }
674         if (p_ac3dec->bsi.lfeon) {
675             NeedBits (&(p_ac3dec->bit_stream), 4);
676             p_ac3dec->audblk.lfefsnroffst = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
677             DumpBits (&(p_ac3dec->bit_stream), 4);
678             NeedBits (&(p_ac3dec->bit_stream), 3);
679             p_ac3dec->audblk.lfefgaincod = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
680             DumpBits (&(p_ac3dec->bit_stream), 3);
681         }
682     }
683
684     /* Get coupling leakage info if it exists */
685     if (p_ac3dec->audblk.cplinu) {
686         NeedBits (&(p_ac3dec->bit_stream), 1);
687         p_ac3dec->audblk.cplleake = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
688         DumpBits (&(p_ac3dec->bit_stream), 1);
689         if ((!blknum) && (!p_ac3dec->audblk.cplleake))
690             return 1;
691
692         if (p_ac3dec->audblk.cplleake) {
693             NeedBits (&(p_ac3dec->bit_stream), 3);
694             p_ac3dec->audblk.cplfleak = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
695             DumpBits (&(p_ac3dec->bit_stream), 3);
696             NeedBits (&(p_ac3dec->bit_stream), 3);
697             p_ac3dec->audblk.cplsleak = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
698             DumpBits (&(p_ac3dec->bit_stream), 3);
699         }
700     }
701
702     /* Get the delta bit alloaction info */
703     NeedBits (&(p_ac3dec->bit_stream), 1);
704     p_ac3dec->audblk.deltbaie = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
705     DumpBits (&(p_ac3dec->bit_stream), 1);
706
707     if (p_ac3dec->audblk.deltbaie) {
708         if (p_ac3dec->audblk.cplinu) {
709             NeedBits (&(p_ac3dec->bit_stream), 2);
710             p_ac3dec->audblk.cpldeltbae = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
711             DumpBits (&(p_ac3dec->bit_stream), 2);
712             if (p_ac3dec->audblk.cpldeltbae == 3)
713                 return 1;
714         }
715
716         for (i = 0;i < p_ac3dec->bsi.nfchans; i++) {
717             NeedBits (&(p_ac3dec->bit_stream), 2);
718             p_ac3dec->audblk.deltbae[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 2));
719             DumpBits (&(p_ac3dec->bit_stream), 2);
720             if (p_ac3dec->audblk.deltbae[i] == 3)
721                 return 1;
722         }
723
724         if (p_ac3dec->audblk.cplinu && (p_ac3dec->audblk.cpldeltbae == DELTA_BIT_NEW)) {
725             NeedBits (&(p_ac3dec->bit_stream), 3);
726             p_ac3dec->audblk.cpldeltnseg = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
727             DumpBits (&(p_ac3dec->bit_stream), 3);
728             for (i = 0;i < p_ac3dec->audblk.cpldeltnseg + 1; i++) {
729                 NeedBits (&(p_ac3dec->bit_stream), 5);
730                 p_ac3dec->audblk.cpldeltoffst[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
731                 DumpBits (&(p_ac3dec->bit_stream), 5);
732                 NeedBits (&(p_ac3dec->bit_stream), 4);
733                 p_ac3dec->audblk.cpldeltlen[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
734                 DumpBits (&(p_ac3dec->bit_stream), 4);
735                 NeedBits (&(p_ac3dec->bit_stream), 3);
736                 p_ac3dec->audblk.cpldeltba[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
737                 DumpBits (&(p_ac3dec->bit_stream), 3);
738             }
739         }
740
741         for (i = 0; i < p_ac3dec->bsi.nfchans; i++) {
742             if (p_ac3dec->audblk.deltbae[i] == DELTA_BIT_NEW) {
743                 NeedBits (&(p_ac3dec->bit_stream), 3);
744                 p_ac3dec->audblk.deltnseg[i] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
745                 DumpBits (&(p_ac3dec->bit_stream), 3);
746 //                if (p_ac3dec->audblk.deltnseg[i] >= 8)
747 //                    fprintf (stderr, "parse debug: p_ac3dec->audblk.deltnseg[%i] == %i\n", i, p_ac3dec->audblk.deltnseg[i]);
748                 for (j = 0; j < p_ac3dec->audblk.deltnseg[i] + 1; j++) {
749                     NeedBits (&(p_ac3dec->bit_stream), 5);
750                     p_ac3dec->audblk.deltoffst[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 5));
751                     DumpBits (&(p_ac3dec->bit_stream), 5);
752                     NeedBits (&(p_ac3dec->bit_stream), 4);
753                     p_ac3dec->audblk.deltlen[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 4));
754                     DumpBits (&(p_ac3dec->bit_stream), 4);
755                     NeedBits (&(p_ac3dec->bit_stream), 3);
756                     p_ac3dec->audblk.deltba[i][j] = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 3));
757                     DumpBits (&(p_ac3dec->bit_stream), 3);
758                 }
759             }
760         }
761     }
762
763     /* Check to see if there's any dummy info to get */
764     NeedBits (&(p_ac3dec->bit_stream), 1);
765     p_ac3dec->audblk.skiple = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 1));
766     DumpBits (&(p_ac3dec->bit_stream), 1);
767
768     if (p_ac3dec->audblk.skiple) {
769         NeedBits (&(p_ac3dec->bit_stream), 9);
770         p_ac3dec->audblk.skipl = (u16)(p_ac3dec->bit_stream.buffer >> (32 - 9));
771         DumpBits (&(p_ac3dec->bit_stream), 9);
772
773         for (i = 0; i < p_ac3dec->audblk.skipl ; i++) {
774             NeedBits (&(p_ac3dec->bit_stream), 8);
775             DumpBits (&(p_ac3dec->bit_stream), 8);
776         }
777     }
778
779     return 0;
780 }
781
782 void parse_auxdata (ac3dec_t * p_ac3dec)
783 {
784     int i;
785     int skip_length;
786
787     skip_length = (p_ac3dec->syncinfo.frame_size * 16) - p_ac3dec->bit_stream.total_bits_read - 17 - 1;
788 //    fprintf (stderr, "parse debug: skip_length == %i\n", skip_length);
789
790     for (i = 0; i < skip_length; i++) {
791         NeedBits (&(p_ac3dec->bit_stream), 1);
792         DumpBits (&(p_ac3dec->bit_stream), 1);
793     }
794
795     /* get the auxdata exists bit */
796     NeedBits (&(p_ac3dec->bit_stream), 1);
797     DumpBits (&(p_ac3dec->bit_stream), 1);
798
799     /* Skip the CRC reserved bit */
800     NeedBits (&(p_ac3dec->bit_stream), 1);
801     DumpBits (&(p_ac3dec->bit_stream), 1);
802
803     /* Get the crc */
804     NeedBits (&(p_ac3dec->bit_stream), 16);
805     DumpBits (&(p_ac3dec->bit_stream), 16);
806 }