summaryrefslogtreecommitdiff
path: root/ac3dec/parse.c
blob: 3560bc5abd860848d0bd87ae179ff99b273a0687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/* 
 *    parse.c
 *
 *	Copyright (C) Aaron Holtzman - May 1999
 *
 *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
 *	
 *  ac3dec is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  ac3dec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include "ac3.h"
#include "ac3_internal.h"


#include "bitstream.h"
#include "stats.h"
#include "debug.h"
#include "parse.h"

/* Misc LUT */
static const uint_16 nfchans[8] = {2,1,2,3,3,4,4,5};

struct frmsize_s
{
	uint_16 bit_rate;
	uint_16 frm_size[3];
};

static const struct frmsize_s frmsizecod_tbl[64] = 
{
	{ 32  ,{64   ,69   ,96   } },
	{ 32  ,{64   ,70   ,96   } },
	{ 40  ,{80   ,87   ,120  } },
	{ 40  ,{80   ,88   ,120  } },
	{ 48  ,{96   ,104  ,144  } },
	{ 48  ,{96   ,105  ,144  } },
	{ 56  ,{112  ,121  ,168  } },
	{ 56  ,{112  ,122  ,168  } },
	{ 64  ,{128  ,139  ,192  } },
	{ 64  ,{128  ,140  ,192  } },
	{ 80  ,{160  ,174  ,240  } },
	{ 80  ,{160  ,175  ,240  } },
	{ 96  ,{192  ,208  ,288  } },
	{ 96  ,{192  ,209  ,288  } },
	{ 112 ,{224  ,243  ,336  } },
	{ 112 ,{224  ,244  ,336  } },
	{ 128 ,{256  ,278  ,384  } },
	{ 128 ,{256  ,279  ,384  } },
	{ 160 ,{320  ,348  ,480  } },
	{ 160 ,{320  ,349  ,480  } },
	{ 192 ,{384  ,417  ,576  } },
	{ 192 ,{384  ,418  ,576  } },
	{ 224 ,{448  ,487  ,672  } },
	{ 224 ,{448  ,488  ,672  } },
	{ 256 ,{512  ,557  ,768  } },
	{ 256 ,{512  ,558  ,768  } },
	{ 320 ,{640  ,696  ,960  } },
	{ 320 ,{640  ,697  ,960  } },
	{ 384 ,{768  ,835  ,1152 } },
	{ 384 ,{768  ,836  ,1152 } },
	{ 448 ,{896  ,975  ,1344 } },
	{ 448 ,{896  ,976  ,1344 } },
	{ 512 ,{1024 ,1114 ,1536 } },
	{ 512 ,{1024 ,1115 ,1536 } },
	{ 576 ,{1152 ,1253 ,1728 } },
	{ 576 ,{1152 ,1254 ,1728 } },
	{ 640 ,{1280 ,1393 ,1920 } },
	{ 640 ,{1280 ,1394 ,1920 } }
};

/* Parse a syncinfo structure, minus the sync word */
void
parse_syncinfo(syncinfo_t *syncinfo,uint_8 *data)
{
	//
	// We need to read in the entire syncinfo struct (0x0b77 + 24 bits)
	// in order to determine how big the frame is
	//

	// Get the sampling rate 
	syncinfo->fscod  = (data[2] >> 6) & 0x3;

	if(syncinfo->fscod == 3)
	{
		//invalid sampling rate code
		error_flag = 1;	
		return;
	}
	else if(syncinfo->fscod == 2)
		syncinfo->sampling_rate = 32000;
	else if(syncinfo->fscod == 1)
		syncinfo->sampling_rate = 44100;
	else
		syncinfo->sampling_rate = 48000;

	// Get the frame size code 
	syncinfo->frmsizecod = data[2] & 0x3f;

	// Calculate the frame size and bitrate
	syncinfo->frame_size = 
		frmsizecod_tbl[syncinfo->frmsizecod].frm_size[syncinfo->fscod];
	syncinfo->bit_rate = frmsizecod_tbl[syncinfo->frmsizecod].bit_rate;

}

/*
 * This routine fills a bsi struct from the AC3 stream
 */

void
parse_bsi(bsi_t *bsi)
{
	uint_32 i;

	/* Check the AC-3 version number */
	bsi->bsid = bitstream_get(5);

	/* Get the audio service provided by the steram */
	bsi->bsmod = bitstream_get(3);

	/* Get the audio coding mode (ie how many channels)*/
	bsi->acmod = bitstream_get(3);
	/* Predecode the number of full bandwidth channels as we use this
	 * number a lot */
	bsi->nfchans = nfchans[bsi->acmod];

	/* If it is in use, get the centre channel mix level */
	if ((bsi->acmod & 0x1) && (bsi->acmod != 0x1))
		bsi->cmixlev = bitstream_get(2);

	/* If it is in use, get the surround channel mix level */
	if (bsi->acmod & 0x4)
		bsi->surmixlev = bitstream_get(2);

	/* Get the dolby surround mode if in 2/0 mode */
	if(bsi->acmod == 0x2)
		bsi->dsurmod= bitstream_get(2);

	/* Is the low frequency effects channel on? */
	bsi->lfeon = bitstream_get(1);

	/* Get the dialogue normalization level */
	bsi->dialnorm = bitstream_get(5);

	/* Does compression gain exist? */
	bsi->compre = bitstream_get(1);
	if (bsi->compre)
	{
		/* Get compression gain */
		bsi->compr = bitstream_get(8);
	}

	/* Does language code exist? */
	bsi->langcode = bitstream_get(1);
	if (bsi->langcode)
	{
		/* Get langauge code */
		bsi->langcod = bitstream_get(8);
	}

	/* Does audio production info exist? */
	bsi->audprodie = bitstream_get(1);
	if (bsi->audprodie)
	{
		/* Get mix level */
		bsi->mixlevel = bitstream_get(5);

		/* Get room type */
		bsi->roomtyp = bitstream_get(2);
	}

	/* If we're in dual mono mode then get some extra info */
	if (bsi->acmod ==0)
	{
		/* Get the dialogue normalization level two */
		bsi->dialnorm2 = bitstream_get(5);

		/* Does compression gain two exist? */
		bsi->compr2e = bitstream_get(1);
		if (bsi->compr2e)
		{
			/* Get compression gain two */
			bsi->compr2 = bitstream_get(8);
		}

		/* Does language code two exist? */
		bsi->langcod2e = bitstream_get(1);
		if (bsi->langcod2e)
		{
			/* Get langauge code two */
			bsi->langcod2 = bitstream_get(8);
		}

		/* Does audio production info two exist? */
		bsi->audprodi2e = bitstream_get(1);
		if (bsi->audprodi2e)
		{
			/* Get mix level two */
			bsi->mixlevel2 = bitstream_get(5);

			/* Get room type two */
			bsi->roomtyp2 = bitstream_get(2);
		}
	}

	/* Get the copyright bit */
	bsi->copyrightb = bitstream_get(1);

	/* Get the original bit */
	bsi->origbs = bitstream_get(1);
	
	/* Does timecode one exist? */
	bsi->timecod1e = bitstream_get(1);

	if(bsi->timecod1e)
		bsi->timecod1 = bitstream_get(14);

	/* Does timecode two exist? */
	bsi->timecod2e = bitstream_get(1);

	if(bsi->timecod2e)
		bsi->timecod2 = bitstream_get(14);

	/* Does addition info exist? */
	bsi->addbsie = bitstream_get(1);

	if(bsi->addbsie)
	{
		/* Get how much info is there */
		bsi->addbsil = bitstream_get(6);

		/* Get the additional info */
		for(i=0;i<(bsi->addbsil + 1);i++)
			bsi->addbsi[i] = bitstream_get(8);
	}

	stats_print_bsi(bsi);
}

/* More pain inducing parsing */
void
parse_audblk(bsi_t *bsi,audblk_t *audblk)
{
	int i,j;

	for (i=0;i < bsi->nfchans; i++)
	{
		/* Is this channel an interleaved 256 + 256 block ? */
		audblk->blksw[i] = bitstream_get(1);
	}

	for (i=0;i < bsi->nfchans; i++)
	{
		/* Should we dither this channel? */
		audblk->dithflag[i] = bitstream_get(1);
	}

	/* Does dynamic range control exist? */
	audblk->dynrnge = bitstream_get(1);
	if (audblk->dynrnge)
	{
		/* Get dynamic range info */
		audblk->dynrng = bitstream_get(8);
	}

	/* If we're in dual mono mode then get the second channel DR info */
	if (bsi->acmod == 0)
	{
		/* Does dynamic range control two exist? */
		audblk->dynrng2e = bitstream_get(1);
		if (audblk->dynrng2e)
		{
			/* Get dynamic range info */
			audblk->dynrng2 = bitstream_get(8);
		}
	}

	/* Does coupling strategy exist? */
	audblk->cplstre = bitstream_get(1);
	if (audblk->cplstre)
	{
		/* Is coupling turned on? */
		audblk->cplinu = bitstream_get(1);
		if(audblk->cplinu)
		{
			for(i=0;i < bsi->nfchans; i++)
				audblk->chincpl[i] = bitstream_get(1);
			if(bsi->acmod == 0x2)
				audblk->phsflginu = bitstream_get(1);
			audblk->cplbegf = bitstream_get(4);
			audblk->cplendf = bitstream_get(4);
			audblk->ncplsubnd = (audblk->cplendf + 2) - audblk->cplbegf + 1;

			/* Calculate the start and end bins of the coupling channel */
			audblk->cplstrtmant = (audblk->cplbegf * 12) + 37 ; 
			audblk->cplendmant =  ((audblk->cplendf + 3) * 12) + 37;

			/* The number of combined subbands is ncplsubnd minus each combined
			 * band */
			audblk->ncplbnd = audblk->ncplsubnd; 

			for(i=1; i< audblk->ncplsubnd; i++)
			{
				audblk->cplbndstrc[i] = bitstream_get(1);
				audblk->ncplbnd -= audblk->cplbndstrc[i];
			}
		}
	}

	if(audblk->cplinu)
	{
		/* Loop through all the channels and get their coupling co-ords */	
		for(i=0;i < bsi->nfchans;i++)
		{
			if(!audblk->chincpl[i])
				continue;

			/* Is there new coupling co-ordinate info? */
			audblk->cplcoe[i] = bitstream_get(1);

			if(audblk->cplcoe[i])
			{
				audblk->mstrcplco[i] = bitstream_get(2); 
				for(j=0;j < audblk->ncplbnd; j++)
				{
					audblk->cplcoexp[i][j] = bitstream_get(4); 
					audblk->cplcomant[i][j] = bitstream_get(4); 
				}
			}
		}

		/* If we're in dual mono mode, there's going to be some phase info */
		if( (bsi->acmod == 0x2) && audblk->phsflginu && 
				(audblk->cplcoe[0] || audblk->cplcoe[1]))
		{
			for(j=0;j < audblk->ncplbnd; j++)
				audblk->phsflg[j] = bitstream_get(1); 

		}
	}

	/* If we're in dual mono mode, there may be a rematrix strategy */
	if(bsi->acmod == 0x2)
	{
		audblk->rematstr = bitstream_get(1);
		if(audblk->rematstr)
		{
			if (audblk->cplinu == 0) 
			{ 
				for(i = 0; i < 4; i++) 
					audblk->rematflg[i] = bitstream_get(1);
			}
			if((audblk->cplbegf > 2) && audblk->cplinu) 
			{
				for(i = 0; i < 4; i++) 
					audblk->rematflg[i] = bitstream_get(1);
			}
			if((audblk->cplbegf <= 2) && audblk->cplinu) 
			{ 
				for(i = 0; i < 3; i++) 
					audblk->rematflg[i] = bitstream_get(1);
			} 
			if((audblk->cplbegf == 0) && audblk->cplinu) 
				for(i = 0; i < 2; i++) 
					audblk->rematflg[i] = bitstream_get(1);

		}
	}

	if (audblk->cplinu)
	{
		/* Get the coupling channel exponent strategy */
		audblk->cplexpstr = bitstream_get(2);
		audblk->ncplgrps = (audblk->cplendmant - audblk->cplstrtmant) / 
				(3 << (audblk->cplexpstr-1));
	}

	for(i = 0; i < bsi->nfchans; i++)
		audblk->chexpstr[i] = bitstream_get(2);

	/* Get the exponent strategy for lfe channel */
	if(bsi->lfeon) 
		audblk->lfeexpstr = bitstream_get(1);

	/* Determine the bandwidths of all the fbw channels */
	for(i = 0; i < bsi->nfchans; i++) 
	{ 
		uint_16 grp_size;

		if(audblk->chexpstr[i] != EXP_REUSE) 
		{ 
			if (audblk->cplinu && audblk->chincpl[i]) 
			{
				audblk->endmant[i] = audblk->cplstrtmant;
			}
			else
			{
				audblk->chbwcod[i] = bitstream_get(6); 
				audblk->endmant[i] = ((audblk->chbwcod[i] + 12) * 3) + 37;
			}

			/* Calculate the number of exponent groups to fetch */
			grp_size =  3 * (1 << (audblk->chexpstr[i] - 1));
			audblk->nchgrps[i] = (audblk->endmant[i] - 1 + (grp_size - 3)) / grp_size;
		}
	}

	/* Get the coupling exponents if they exist */
	if(audblk->cplinu && (audblk->cplexpstr != EXP_REUSE))
	{
		audblk->cplabsexp = bitstream_get(4);
		for(i=0;i< audblk->ncplgrps;i++)
			audblk->cplexps[i] = bitstream_get(7);
	}

	/* Get the fwb channel exponents */
	for(i=0;i < bsi->nfchans; i++)
	{
		if(audblk->chexpstr[i] != EXP_REUSE)
		{
			audblk->exps[i][0] = bitstream_get(4);			
			for(j=1;j<=audblk->nchgrps[i];j++)
				audblk->exps[i][j] = bitstream_get(7);
			audblk->gainrng[i] = bitstream_get(2);
		}
	}

	/* Get the lfe channel exponents */
	if(bsi->lfeon && (audblk->lfeexpstr != EXP_REUSE))
	{
		audblk->lfeexps[0] = bitstream_get(4);
		audblk->lfeexps[1] = bitstream_get(7);
		audblk->lfeexps[2] = bitstream_get(7);
	}

	/* Get the parametric bit allocation parameters */
	audblk->baie = bitstream_get(1);

	if(audblk->baie)
	{
		audblk->sdcycod = bitstream_get(2);
		audblk->fdcycod = bitstream_get(2);
		audblk->sgaincod = bitstream_get(2);
		audblk->dbpbcod = bitstream_get(2);
		audblk->floorcod = bitstream_get(3);
	}

	/* Get the SNR off set info if it exists */
	audblk->snroffste = bitstream_get(1);

	if(audblk->snroffste)
	{
		audblk->csnroffst = bitstream_get(6);

		if(audblk->cplinu)
		{
			audblk->cplfsnroffst = bitstream_get(4);
			audblk->cplfgaincod = bitstream_get(3);
		}

		for(i = 0;i < bsi->nfchans; i++)
		{
			audblk->fsnroffst[i] = bitstream_get(4);
			audblk->fgaincod[i] = bitstream_get(3);
		}
		if(bsi->lfeon)
		{

			audblk->lfefsnroffst = bitstream_get(4);
			audblk->lfefgaincod = bitstream_get(3);
		}
	}

	/* Get coupling leakage info if it exists */
	if(audblk->cplinu)
	{
		audblk->cplleake = bitstream_get(1);	
		
		if(audblk->cplleake)
		{
			audblk->cplfleak = bitstream_get(3);
			audblk->cplsleak = bitstream_get(3);
		}
	}
	
	/* Get the delta bit alloaction info */
	audblk->deltbaie = bitstream_get(1);	
	
	if(audblk->deltbaie)
	{
		if(audblk->cplinu)
			audblk->cpldeltbae = bitstream_get(2);

		for(i = 0;i < bsi->nfchans; i++)
			audblk->deltbae[i] = bitstream_get(2);

		if (audblk->cplinu && (audblk->cpldeltbae == DELTA_BIT_NEW))
		{
			audblk->cpldeltnseg = bitstream_get(3);
			for(i = 0;i < audblk->cpldeltnseg + 1; i++)
			{
				audblk->cpldeltoffst[i] = bitstream_get(5);
				audblk->cpldeltlen[i] = bitstream_get(4);
				audblk->cpldeltba[i] = bitstream_get(3);
			}
		}

		for(i = 0;i < bsi->nfchans; i++)
		{
			if (audblk->deltbae[i] == DELTA_BIT_NEW)
			{
				audblk->deltnseg[i] = bitstream_get(3);
				for(j = 0; j < audblk->deltnseg[i] + 1; j++)
				{
					audblk->deltoffst[i][j] = bitstream_get(5);
					audblk->deltlen[i][j] = bitstream_get(4);
					audblk->deltba[i][j] = bitstream_get(3);
				}
			}
		}
	}

	/* Check to see if there's any dummy info to get */
	if((audblk->skiple =  bitstream_get(1)))
	{
		uint_16 skip_data;

		audblk->skipl = bitstream_get(9);
		//XXX remove
		//fprintf(stderr,"(parse) skipping %d bytes\n",audblk->skipl);

		for(i = 0; i < audblk->skipl ; i++)
		{
			skip_data = bitstream_get(8);
			//XXX remove
			//fprintf(stderr,"skipped data %2x\n",skip_data);
			//if(skip_data != 0)
			//{	
				//dprintf("(parse) Invalid skipped data %2x\n",skip_data);
				//exit(1);
			//}
		}
	}

	stats_print_audblk(bsi,audblk);
}

void
parse_auxdata(syncinfo_t *syncinfo)
{
	//FIXME keep this now that we don't really need it?
#if 0
	int i;
	int skip_length;
	uint_16 crc;
	uint_16 auxdatae;

	skip_length = (syncinfo->frame_size * 16)  - bitstream_get_total_bits() - 17 - 1;

	//XXX remove
	//dprintf("(auxdata) skipping %d auxbits\n",skip_length);
	
	for(i=0; i <  skip_length; i++)
		//printf("Skipped bit %i\n",(uint_16)bitstream_get(1));
		bitstream_get(1);

	//get the auxdata exists bit
	auxdatae = bitstream_get(1);	

	//XXX remove
	//dprintf("auxdatae = %i\n",auxdatae);

	//Skip the CRC reserved bit
	bitstream_get(1);

	//Get the crc
	crc = bitstream_get(16);
#endif
}