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