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