comparison ac3dec.c @ 5386:d6347c3cb329 libavcodec

Use shared AC-3 header parsing. Move some initializations to block parsing function.
author jbr
date Sat, 21 Jul 2007 16:28:12 +0000
parents bc14cca988aa
children 1194da3349cb
comparison
equal deleted inserted replaced
5385:bc14cca988aa 5386:d6347c3cb329
31 #include <stddef.h> 31 #include <stddef.h>
32 #include <math.h> 32 #include <math.h>
33 #include <string.h> 33 #include <string.h>
34 34
35 #include "avcodec.h" 35 #include "avcodec.h"
36 #include "ac3.h" 36 #include "ac3_parser.h"
37 #include "ac3tab.h"
38 #include "bitstream.h" 37 #include "bitstream.h"
39 #include "dsputil.h" 38 #include "dsputil.h"
40 #include "random.h" 39 #include "random.h"
41 40
42 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; 41 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
87 #define AC3_OUTPUT_STEREO 0x04 86 #define AC3_OUTPUT_STEREO 0x04
88 #define AC3_OUTPUT_DOLBY 0x08 87 #define AC3_OUTPUT_DOLBY 0x08
89 #define AC3_OUTPUT_LFEON 0x10 88 #define AC3_OUTPUT_LFEON 0x10
90 89
91 typedef struct { 90 typedef struct {
92 uint16_t crc1;
93 uint8_t fscod;
94
95 uint8_t acmod; 91 uint8_t acmod;
96 uint8_t cmixlev; 92 uint8_t cmixlev;
97 uint8_t surmixlev; 93 uint8_t surmixlev;
98 uint8_t dsurmod; 94 uint8_t dsurmod;
99 95
139 /* Derived Attributes. */ 135 /* Derived Attributes. */
140 int sampling_rate; 136 int sampling_rate;
141 int bit_rate; 137 int bit_rate;
142 int frame_size; 138 int frame_size;
143 139
144 int nfchans; //number of channels 140 int nchans; //number of total channels
141 int nfchans; //number of full-bandwidth channels
145 int lfeon; //lfe channel in use 142 int lfeon; //lfe channel in use
146 143
147 float dynrng; //dynamic range gain 144 float dynrng; //dynamic range gain
148 float dynrng2; //dynamic range gain for 1+1 mode 145 float dynrng2; //dynamic range gain for 1+1 mode
149 float chcoeffs[6]; //normalized channel coefficients 146 float chcoeffs[6]; //normalized channel coefficients
318 315
319 return 0; 316 return 0;
320 } 317 }
321 /*********** END INIT FUNCTIONS ***********/ 318 /*********** END INIT FUNCTIONS ***********/
322 319
323 /* Parse the 'sync_info' from the ac3 bitstream. 320 /**
324 * This function extracts the sync_info from ac3 bitstream. 321 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
325 * GetBitContext within AC3DecodeContext must point to 322 * GetBitContext within AC3DecodeContext must point to
326 * start of the synchronized ac3 bitstream. 323 * start of the synchronized ac3 bitstream.
327 *
328 * @param ctx AC3DecodeContext
329 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
330 */ 324 */
331 static int ac3_parse_sync_info(AC3DecodeContext *ctx) 325 static int ac3_parse_header(AC3DecodeContext *ctx)
332 { 326 {
327 AC3HeaderInfo hdr;
333 GetBitContext *gb = &ctx->gb; 328 GetBitContext *gb = &ctx->gb;
334 int frmsizecod, bsid; 329 int err, i;
335 330
331 err = ff_ac3_parse_header(gb->buffer, &hdr);
332 if(err)
333 return err;
334
335 /* get decoding parameters from header info */
336 ctx->bit_alloc_params.fscod = hdr.fscod;
337 ctx->acmod = hdr.acmod;
338 ctx->cmixlev = hdr.cmixlev;
339 ctx->surmixlev = hdr.surmixlev;
340 ctx->dsurmod = hdr.dsurmod;
341 ctx->lfeon = hdr.lfeon;
342 ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
343 ctx->sampling_rate = hdr.sample_rate;
344 ctx->bit_rate = hdr.bit_rate;
345 ctx->nchans = hdr.channels;
346 ctx->nfchans = ctx->nchans - ctx->lfeon;
347 ctx->frame_size = hdr.frame_size;
348 ctx->blkoutput = nfchans_tbl[ctx->acmod];
349 if(ctx->lfeon)
350 ctx->blkoutput |= AC3_OUTPUT_LFEON;
351
352 /* skip over portion of header which has already been read */
336 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16); 353 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
337 ctx->crc1 = get_bits(gb, 16); 354 skip_bits(gb, 16); // skip crc1
338 ctx->fscod = get_bits(gb, 2); 355 skip_bits(gb, 8); // skip fscod and frmsizecod
339 if (ctx->fscod == 0x03) 356 skip_bits(gb, 11); // skip bsid, bsmod, and acmod
340 return 0; 357 if(ctx->acmod == AC3_ACMOD_STEREO) {
341 frmsizecod = get_bits(gb, 6); 358 skip_bits(gb, 2); // skip dsurmod
342 if (frmsizecod >= 38) 359 } else {
343 return 0; 360 if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
344 ctx->sampling_rate = ff_ac3_freqs[ctx->fscod]; 361 skip_bits(gb, 2); // skip cmixlev
345 ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1]; 362 if(ctx->acmod & 4)
346 363 skip_bits(gb, 2); // skip surmixlev
347 /* we include it here in order to determine validity of ac3 frame */ 364 }
348 bsid = get_bits(gb, 5); 365 skip_bits1(gb); // skip lfeon
349 if (bsid > 0x08) 366
350 return 0; 367 /* read the rest of the bsi. read twice for dual mono mode. */
351 skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
352
353 switch (ctx->fscod) {
354 case 0x00:
355 ctx->frame_size = 4 * ctx->bit_rate;
356 return ctx->frame_size;
357 case 0x01:
358 ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
359 return ctx->frame_size;
360 case 0x02:
361 ctx->frame_size = 6 * ctx->bit_rate;
362 return ctx->frame_size;
363 }
364
365 /* never reached */
366 return 0;
367 }
368
369 /* Parse bsi from ac3 bitstream.
370 * This function extracts the bitstream information (bsi) from ac3 bitstream.
371 *
372 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
373 */
374 static void ac3_parse_bsi(AC3DecodeContext *ctx)
375 {
376 GetBitContext *gb = &ctx->gb;
377 int i;
378
379 ctx->cmixlev = 0;
380 ctx->surmixlev = 0;
381 ctx->dsurmod = 0;
382 ctx->nfchans = 0;
383 ctx->cpldeltbae = DBA_NONE;
384 ctx->cpldeltnseg = 0;
385 for (i = 0; i < 5; i++) {
386 ctx->deltbae[i] = DBA_NONE;
387 ctx->deltnseg[i] = 0;
388 }
389 ctx->dynrng = 1.0;
390 ctx->dynrng2 = 1.0;
391
392 ctx->acmod = get_bits(gb, 3);
393 ctx->nfchans = nfchans_tbl[ctx->acmod];
394
395 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
396 ctx->cmixlev = get_bits(gb, 2);
397 if (ctx->acmod & 0x04)
398 ctx->surmixlev = get_bits(gb, 2);
399 if (ctx->acmod == 0x02)
400 ctx->dsurmod = get_bits(gb, 2);
401
402 ctx->lfeon = get_bits1(gb);
403
404 i = !(ctx->acmod); 368 i = !(ctx->acmod);
405 do { 369 do {
406 skip_bits(gb, 5); //skip dialog normalization 370 skip_bits(gb, 5); //skip dialog normalization
407 if (get_bits1(gb)) 371 if (get_bits1(gb))
408 skip_bits(gb, 8); //skip compression 372 skip_bits(gb, 8); //skip compression
412 skip_bits(gb, 7); //skip audio production information 376 skip_bits(gb, 7); //skip audio production information
413 } while (i--); 377 } while (i--);
414 378
415 skip_bits(gb, 2); //skip copyright bit and original bitstream bit 379 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
416 380
381 /* FIXME: read & use the xbsi1 downmix levels */
417 if (get_bits1(gb)) 382 if (get_bits1(gb))
418 skip_bits(gb, 14); //skip timecode1 383 skip_bits(gb, 14); //skip timecode1
419 if (get_bits1(gb)) 384 if (get_bits1(gb))
420 skip_bits(gb, 14); //skip timecode2 385 skip_bits(gb, 14); //skip timecode2
421 386
423 i = get_bits(gb, 6); //additional bsi length 388 i = get_bits(gb, 6); //additional bsi length
424 do { 389 do {
425 skip_bits(gb, 8); 390 skip_bits(gb, 8);
426 } while(i--); 391 } while(i--);
427 } 392 }
393
394 return 0;
428 } 395 }
429 396
430 /** 397 /**
431 * Decodes the grouped exponents. 398 * Decodes the grouped exponents.
432 * This function decodes the coded exponents according to exponent strategy 399 * This function decodes the coded exponents according to exponent strategy
1337 /* Parse the audio block from ac3 bitstream. 1304 /* Parse the audio block from ac3 bitstream.
1338 * This function extract the audio block from the ac3 bitstream 1305 * This function extract the audio block from the ac3 bitstream
1339 * and produces the output for the block. This function must 1306 * and produces the output for the block. This function must
1340 * be called for each of the six audio block in the ac3 bitstream. 1307 * be called for each of the six audio block in the ac3 bitstream.
1341 */ 1308 */
1342 static int ac3_parse_audio_block(AC3DecodeContext * ctx) 1309 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
1343 { 1310 {
1344 int nfchans = ctx->nfchans; 1311 int nfchans = ctx->nfchans;
1345 int acmod = ctx->acmod; 1312 int acmod = ctx->acmod;
1346 int i, bnd, rbnd, seg, grpsize; 1313 int i, bnd, rbnd, seg, grpsize;
1347 GetBitContext *gb = &ctx->gb; 1314 GetBitContext *gb = &ctx->gb;
1359 ctx->dithflag |= get_bits1(gb) << i; 1326 ctx->dithflag |= get_bits1(gb) << i;
1360 1327
1361 if (get_bits1(gb)) { /* dynamic range */ 1328 if (get_bits1(gb)) { /* dynamic range */
1362 dynrng = get_sbits(gb, 8); 1329 dynrng = get_sbits(gb, 8);
1363 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 1330 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1364 } 1331 } else if(blk == 0) {
1365 1332 ctx->dynrng = 1.0;
1366 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */ 1333 }
1334
1335 if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
1336 if(get_bits1(gb)) {
1367 dynrng = get_sbits(gb, 8); 1337 dynrng = get_sbits(gb, 8);
1368 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 1338 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1339 } else if(blk == 0) {
1340 ctx->dynrng2 = 1.0;
1341 }
1369 } 1342 }
1370 1343
1371 get_downmix_coeffs(ctx); 1344 get_downmix_coeffs(ctx);
1372 1345
1373 if (get_bits1(gb)) { /* coupling strategy */ 1346 if (get_bits1(gb)) { /* coupling strategy */
1557 ctx->deltoffst[i][seg] = get_bits(gb, 5); 1530 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1558 ctx->deltlen[i][seg] = get_bits(gb, 4); 1531 ctx->deltlen[i][seg] = get_bits(gb, 4);
1559 ctx->deltba[i][seg] = get_bits(gb, 3); 1532 ctx->deltba[i][seg] = get_bits(gb, 3);
1560 } 1533 }
1561 } 1534 }
1535 } else if(blk == 0) {
1536 if(ctx->cplinu)
1537 ctx->cpldeltbae = DBA_NONE;
1538 for(i=0; i<nfchans; i++) {
1539 ctx->deltbae[i] = DBA_NONE;
1540 }
1562 } 1541 }
1563 1542
1564 if (bit_alloc_flags) { 1543 if (bit_alloc_flags) {
1565 /* set bit allocation parameters */ 1544 /* set bit allocation parameters */
1566 ctx->bit_alloc_params.fscod = ctx->fscod;
1567 ctx->bit_alloc_params.halfratecod = 0;
1568 ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod]; 1545 ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1569 ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod]; 1546 ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1570 ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod]; 1547 ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1571 ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod]; 1548 ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1572 ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod]; 1549 ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1636 1613
1637 //Initialize the GetBitContext with the start of valid AC3 Frame. 1614 //Initialize the GetBitContext with the start of valid AC3 Frame.
1638 init_get_bits(&ctx->gb, buf, buf_size * 8); 1615 init_get_bits(&ctx->gb, buf, buf_size * 8);
1639 1616
1640 //Parse the syncinfo. 1617 //Parse the syncinfo.
1641 //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard. 1618 if (ac3_parse_header(ctx)) {
1642 if (!ac3_parse_sync_info(ctx)) {
1643 av_log(avctx, AV_LOG_ERROR, "\n"); 1619 av_log(avctx, AV_LOG_ERROR, "\n");
1644 *data_size = 0; 1620 *data_size = 0;
1645 return buf_size; 1621 return buf_size;
1646 } 1622 }
1647
1648 //Parse the BSI.
1649 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1650 ac3_parse_bsi(ctx);
1651 1623
1652 avctx->sample_rate = ctx->sampling_rate; 1624 avctx->sample_rate = ctx->sampling_rate;
1653 avctx->bit_rate = ctx->bit_rate; 1625 avctx->bit_rate = ctx->bit_rate;
1654 1626
1655 if (avctx->channels == 0) { 1627 if (avctx->channels == 0) {
1677 1649
1678 //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate); 1650 //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
1679 1651
1680 //Parse the Audio Blocks. 1652 //Parse the Audio Blocks.
1681 for (i = 0; i < NB_BLOCKS; i++) { 1653 for (i = 0; i < NB_BLOCKS; i++) {
1682 if (ac3_parse_audio_block(ctx)) { 1654 if (ac3_parse_audio_block(ctx, i)) {
1683 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); 1655 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1684 *data_size = 0; 1656 *data_size = 0;
1685 return ctx->frame_size; 1657 return ctx->frame_size;
1686 } 1658 }
1687 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1; 1659 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;