Mercurial > libavcodec.hg
comparison dca.c @ 11911:180c6aca1f6e libavcodec
Support DTS-ES extension (XCh) in dca: add code to handle DTS-ES extension
Patch by Nick Brereton, nick at nbrereton dot net
author | mstorsjo |
---|---|
date | Tue, 22 Jun 2010 08:34:51 +0000 |
parents | 284f85e281fc |
children | 6db8c8adf407 |
comparison
equal
deleted
inserted
replaced
11910:284f85e281fc | 11911:180c6aca1f6e |
---|---|
39 #include "synth_filter.h" | 39 #include "synth_filter.h" |
40 #include "dcadsp.h" | 40 #include "dcadsp.h" |
41 | 41 |
42 //#define TRACE | 42 //#define TRACE |
43 | 43 |
44 #define DCA_PRIM_CHANNELS_MAX (5) | 44 #define DCA_PRIM_CHANNELS_MAX (7) |
45 #define DCA_SUBBANDS (32) | 45 #define DCA_SUBBANDS (32) |
46 #define DCA_ABITS_MAX (32) /* Should be 28 */ | 46 #define DCA_ABITS_MAX (32) /* Should be 28 */ |
47 #define DCA_SUBSUBFRAMES_MAX (4) | 47 #define DCA_SUBSUBFRAMES_MAX (4) |
48 #define DCA_SUBFRAMES_MAX (16) | |
48 #define DCA_BLOCKS_MAX (16) | 49 #define DCA_BLOCKS_MAX (16) |
49 #define DCA_LFE_MAX (3) | 50 #define DCA_LFE_MAX (3) |
50 | 51 |
51 enum DCAMode { | 52 enum DCAMode { |
52 DCA_MONO = 0, | 53 DCA_MONO = 0, |
244 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select | 245 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select |
245 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select | 246 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select |
246 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment | 247 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment |
247 | 248 |
248 /* Primary audio coding side information */ | 249 /* Primary audio coding side information */ |
249 int subsubframes; ///< number of subsubframes | 250 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes |
250 int partial_samples; ///< partial subsubframe samples count | 251 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count |
251 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not) | 252 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not) |
252 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs | 253 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs |
253 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index | 254 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index |
254 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) | 255 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) |
255 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) | 256 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) |
273 int output; ///< type of output | 274 int output; ///< type of output |
274 float add_bias; ///< output bias | 275 float add_bias; ///< output bias |
275 float scale_bias; ///< output scale | 276 float scale_bias; ///< output scale |
276 | 277 |
277 DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; | 278 DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; |
278 DECLARE_ALIGNED(16, float, samples)[1536]; /* 6 * 256 = 1536, might only need 5 */ | 279 DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256]; |
279 const float *samples_chanptr[6]; | 280 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1]; |
280 | 281 |
281 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE]; | 282 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE]; |
282 int dca_buffer_size; ///< how much data is in the dca_buffer | 283 int dca_buffer_size; ///< how much data is in the dca_buffer |
283 | 284 |
284 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe | 285 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe |
360 { | 361 { |
361 while(len--) | 362 while(len--) |
362 *dst++ = get_bits(gb, bits); | 363 *dst++ = get_bits(gb, bits); |
363 } | 364 } |
364 | 365 |
365 static int dca_parse_audio_coding_header(DCAContext * s) | 366 static int dca_parse_audio_coding_header(DCAContext * s, int base_channel) |
366 { | 367 { |
367 int i, j; | 368 int i, j; |
368 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 }; | 369 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 }; |
369 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 }; | 370 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 }; |
370 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 }; | 371 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 }; |
371 | 372 |
372 s->total_channels = get_bits(&s->gb, 3) + 1; | 373 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel; |
373 s->prim_channels = s->total_channels; | 374 s->prim_channels = s->total_channels; |
375 | |
374 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX) | 376 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX) |
375 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */ | 377 s->prim_channels = DCA_PRIM_CHANNELS_MAX; |
376 | 378 |
377 | 379 |
378 for (i = 0; i < s->prim_channels; i++) { | 380 for (i = base_channel; i < s->prim_channels; i++) { |
379 s->subband_activity[i] = get_bits(&s->gb, 5) + 2; | 381 s->subband_activity[i] = get_bits(&s->gb, 5) + 2; |
380 if (s->subband_activity[i] > DCA_SUBBANDS) | 382 if (s->subband_activity[i] > DCA_SUBBANDS) |
381 s->subband_activity[i] = DCA_SUBBANDS; | 383 s->subband_activity[i] = DCA_SUBBANDS; |
382 } | 384 } |
383 for (i = 0; i < s->prim_channels; i++) { | 385 for (i = base_channel; i < s->prim_channels; i++) { |
384 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1; | 386 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1; |
385 if (s->vq_start_subband[i] > DCA_SUBBANDS) | 387 if (s->vq_start_subband[i] > DCA_SUBBANDS) |
386 s->vq_start_subband[i] = DCA_SUBBANDS; | 388 s->vq_start_subband[i] = DCA_SUBBANDS; |
387 } | 389 } |
388 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3); | 390 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3); |
389 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2); | 391 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2); |
390 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3); | 392 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3); |
391 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3); | 393 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3); |
392 | 394 |
393 /* Get codebooks quantization indexes */ | 395 /* Get codebooks quantization indexes */ |
394 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman)); | 396 if (!base_channel) |
397 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman)); | |
395 for (j = 1; j < 11; j++) | 398 for (j = 1; j < 11; j++) |
396 for (i = 0; i < s->prim_channels; i++) | 399 for (i = base_channel; i < s->prim_channels; i++) |
397 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]); | 400 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]); |
398 | 401 |
399 /* Get scale factor adjustment */ | 402 /* Get scale factor adjustment */ |
400 for (j = 0; j < 11; j++) | 403 for (j = 0; j < 11; j++) |
401 for (i = 0; i < s->prim_channels; i++) | 404 for (i = base_channel; i < s->prim_channels; i++) |
402 s->scalefactor_adj[i][j] = 1; | 405 s->scalefactor_adj[i][j] = 1; |
403 | 406 |
404 for (j = 1; j < 11; j++) | 407 for (j = 1; j < 11; j++) |
405 for (i = 0; i < s->prim_channels; i++) | 408 for (i = base_channel; i < s->prim_channels; i++) |
406 if (s->quant_index_huffman[i][j] < thr[j]) | 409 if (s->quant_index_huffman[i][j] < thr[j]) |
407 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)]; | 410 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)]; |
408 | 411 |
409 if (s->crc_present) { | 412 if (s->crc_present) { |
410 /* Audio header CRC check */ | 413 /* Audio header CRC check */ |
415 s->current_subsubframe = 0; | 418 s->current_subsubframe = 0; |
416 | 419 |
417 #ifdef TRACE | 420 #ifdef TRACE |
418 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes); | 421 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes); |
419 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels); | 422 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels); |
420 for (i = 0; i < s->prim_channels; i++){ | 423 for (i = base_channel; i < s->prim_channels; i++){ |
421 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]); | 424 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]); |
422 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]); | 425 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]); |
423 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]); | 426 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]); |
424 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]); | 427 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]); |
425 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]); | 428 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]); |
529 #endif | 532 #endif |
530 | 533 |
531 /* Primary audio coding header */ | 534 /* Primary audio coding header */ |
532 s->subframes = get_bits(&s->gb, 4) + 1; | 535 s->subframes = get_bits(&s->gb, 4) + 1; |
533 | 536 |
534 return dca_parse_audio_coding_header(s); | 537 return dca_parse_audio_coding_header(s, 0); |
535 } | 538 } |
536 | 539 |
537 | 540 |
538 static inline int get_scale(GetBitContext *gb, int level, int value) | 541 static inline int get_scale(GetBitContext *gb, int level, int value) |
539 { | 542 { |
543 } else if(level < 8) | 546 } else if(level < 8) |
544 value = get_bits(gb, level + 1); | 547 value = get_bits(gb, level + 1); |
545 return value; | 548 return value; |
546 } | 549 } |
547 | 550 |
548 static int dca_subframe_header(DCAContext * s, int block_index) | 551 static int dca_subframe_header(DCAContext * s, int base_channel, int block_index) |
549 { | 552 { |
550 /* Primary audio coding side information */ | 553 /* Primary audio coding side information */ |
551 int j, k; | 554 int j, k; |
552 | 555 |
553 s->subsubframes = get_bits(&s->gb, 2) + 1; | 556 if (!base_channel) { |
554 s->partial_samples = get_bits(&s->gb, 3); | 557 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1; |
555 for (j = 0; j < s->prim_channels; j++) { | 558 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3); |
559 } | |
560 | |
561 for (j = base_channel; j < s->prim_channels; j++) { | |
556 for (k = 0; k < s->subband_activity[j]; k++) | 562 for (k = 0; k < s->subband_activity[j]; k++) |
557 s->prediction_mode[j][k] = get_bits(&s->gb, 1); | 563 s->prediction_mode[j][k] = get_bits(&s->gb, 1); |
558 } | 564 } |
559 | 565 |
560 /* Get prediction codebook */ | 566 /* Get prediction codebook */ |
561 for (j = 0; j < s->prim_channels; j++) { | 567 for (j = base_channel; j < s->prim_channels; j++) { |
562 for (k = 0; k < s->subband_activity[j]; k++) { | 568 for (k = 0; k < s->subband_activity[j]; k++) { |
563 if (s->prediction_mode[j][k] > 0) { | 569 if (s->prediction_mode[j][k] > 0) { |
564 /* (Prediction coefficient VQ address) */ | 570 /* (Prediction coefficient VQ address) */ |
565 s->prediction_vq[j][k] = get_bits(&s->gb, 12); | 571 s->prediction_vq[j][k] = get_bits(&s->gb, 12); |
566 } | 572 } |
567 } | 573 } |
568 } | 574 } |
569 | 575 |
570 /* Bit allocation index */ | 576 /* Bit allocation index */ |
571 for (j = 0; j < s->prim_channels; j++) { | 577 for (j = base_channel; j < s->prim_channels; j++) { |
572 for (k = 0; k < s->vq_start_subband[j]; k++) { | 578 for (k = 0; k < s->vq_start_subband[j]; k++) { |
573 if (s->bitalloc_huffman[j] == 6) | 579 if (s->bitalloc_huffman[j] == 6) |
574 s->bitalloc[j][k] = get_bits(&s->gb, 5); | 580 s->bitalloc[j][k] = get_bits(&s->gb, 5); |
575 else if (s->bitalloc_huffman[j] == 5) | 581 else if (s->bitalloc_huffman[j] == 5) |
576 s->bitalloc[j][k] = get_bits(&s->gb, 4); | 582 s->bitalloc[j][k] = get_bits(&s->gb, 4); |
590 } | 596 } |
591 } | 597 } |
592 } | 598 } |
593 | 599 |
594 /* Transition mode */ | 600 /* Transition mode */ |
595 for (j = 0; j < s->prim_channels; j++) { | 601 for (j = base_channel; j < s->prim_channels; j++) { |
596 for (k = 0; k < s->subband_activity[j]; k++) { | 602 for (k = 0; k < s->subband_activity[j]; k++) { |
597 s->transition_mode[j][k] = 0; | 603 s->transition_mode[j][k] = 0; |
598 if (s->subsubframes > 1 && | 604 if (s->subsubframes[s->current_subframe] > 1 && |
599 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) { | 605 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) { |
600 s->transition_mode[j][k] = | 606 s->transition_mode[j][k] = |
601 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); | 607 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); |
602 } | 608 } |
603 } | 609 } |
604 } | 610 } |
605 | 611 |
606 for (j = 0; j < s->prim_channels; j++) { | 612 for (j = base_channel; j < s->prim_channels; j++) { |
607 const uint32_t *scale_table; | 613 const uint32_t *scale_table; |
608 int scale_sum; | 614 int scale_sum; |
609 | 615 |
610 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); | 616 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); |
611 | 617 |
630 } | 636 } |
631 } | 637 } |
632 } | 638 } |
633 | 639 |
634 /* Joint subband scale factor codebook select */ | 640 /* Joint subband scale factor codebook select */ |
635 for (j = 0; j < s->prim_channels; j++) { | 641 for (j = base_channel; j < s->prim_channels; j++) { |
636 /* Transmitted only if joint subband coding enabled */ | 642 /* Transmitted only if joint subband coding enabled */ |
637 if (s->joint_intensity[j] > 0) | 643 if (s->joint_intensity[j] > 0) |
638 s->joint_huff[j] = get_bits(&s->gb, 3); | 644 s->joint_huff[j] = get_bits(&s->gb, 3); |
639 } | 645 } |
640 | 646 |
641 /* Scale factors for joint subband coding */ | 647 /* Scale factors for joint subband coding */ |
642 for (j = 0; j < s->prim_channels; j++) { | 648 for (j = base_channel; j < s->prim_channels; j++) { |
643 int source_channel; | 649 int source_channel; |
644 | 650 |
645 /* Transmitted only if joint subband coding enabled */ | 651 /* Transmitted only if joint subband coding enabled */ |
646 if (s->joint_intensity[j] > 0) { | 652 if (s->joint_intensity[j] > 0) { |
647 int scale = 0; | 653 int scale = 0; |
663 } | 669 } |
664 } | 670 } |
665 } | 671 } |
666 | 672 |
667 /* Stereo downmix coefficients */ | 673 /* Stereo downmix coefficients */ |
668 if (s->prim_channels > 2) { | 674 if (!base_channel && s->prim_channels > 2) { |
669 if(s->downmix) { | 675 if(s->downmix) { |
670 for (j = 0; j < s->prim_channels; j++) { | 676 for (j = base_channel; j < s->prim_channels; j++) { |
671 s->downmix_coef[j][0] = get_bits(&s->gb, 7); | 677 s->downmix_coef[j][0] = get_bits(&s->gb, 7); |
672 s->downmix_coef[j][1] = get_bits(&s->gb, 7); | 678 s->downmix_coef[j][1] = get_bits(&s->gb, 7); |
673 } | 679 } |
674 } else { | 680 } else { |
675 int am = s->amode & DCA_CHANNEL_MASK; | 681 int am = s->amode & DCA_CHANNEL_MASK; |
676 for (j = 0; j < s->prim_channels; j++) { | 682 for (j = base_channel; j < s->prim_channels; j++) { |
677 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; | 683 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; |
678 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; | 684 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; |
679 } | 685 } |
680 } | 686 } |
681 } | 687 } |
692 /* | 698 /* |
693 * Primary audio data arrays | 699 * Primary audio data arrays |
694 */ | 700 */ |
695 | 701 |
696 /* VQ encoded high frequency subbands */ | 702 /* VQ encoded high frequency subbands */ |
697 for (j = 0; j < s->prim_channels; j++) | 703 for (j = base_channel; j < s->prim_channels; j++) |
698 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | 704 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) |
699 /* 1 vector -> 32 samples */ | 705 /* 1 vector -> 32 samples */ |
700 s->high_freq_vq[j][k] = get_bits(&s->gb, 10); | 706 s->high_freq_vq[j][k] = get_bits(&s->gb, 10); |
701 | 707 |
702 /* Low frequency effect data */ | 708 /* Low frequency effect data */ |
703 if (s->lfe) { | 709 if (!base_channel && s->lfe) { |
704 /* LFE samples */ | 710 /* LFE samples */ |
705 int lfe_samples = 2 * s->lfe * (4 + block_index); | 711 int lfe_samples = 2 * s->lfe * (4 + block_index); |
706 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes); | 712 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); |
707 float lfe_scale; | 713 float lfe_scale; |
708 | 714 |
709 for (j = lfe_samples; j < lfe_end_sample; j++) { | 715 for (j = lfe_samples; j < lfe_end_sample; j++) { |
710 /* Signed 8 bits int */ | 716 /* Signed 8 bits int */ |
711 s->lfe_data[j] = get_sbits(&s->gb, 8); | 717 s->lfe_data[j] = get_sbits(&s->gb, 8); |
720 for (j = lfe_samples; j < lfe_end_sample; j++) | 726 for (j = lfe_samples; j < lfe_end_sample; j++) |
721 s->lfe_data[j] *= lfe_scale; | 727 s->lfe_data[j] *= lfe_scale; |
722 } | 728 } |
723 | 729 |
724 #ifdef TRACE | 730 #ifdef TRACE |
725 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes); | 731 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]); |
726 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n", | 732 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n", |
727 s->partial_samples); | 733 s->partial_samples[s->current_subframe]); |
728 for (j = 0; j < s->prim_channels; j++) { | 734 for (j = base_channel; j < s->prim_channels; j++) { |
729 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:"); | 735 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:"); |
730 for (k = 0; k < s->subband_activity[j]; k++) | 736 for (k = 0; k < s->subband_activity[j]; k++) |
731 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]); | 737 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]); |
732 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 738 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
733 } | 739 } |
734 for (j = 0; j < s->prim_channels; j++) { | 740 for (j = base_channel; j < s->prim_channels; j++) { |
735 for (k = 0; k < s->subband_activity[j]; k++) | 741 for (k = 0; k < s->subband_activity[j]; k++) |
736 av_log(s->avctx, AV_LOG_DEBUG, | 742 av_log(s->avctx, AV_LOG_DEBUG, |
737 "prediction coefs: %f, %f, %f, %f\n", | 743 "prediction coefs: %f, %f, %f, %f\n", |
738 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, | 744 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, |
739 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, | 745 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, |
740 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, | 746 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, |
741 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); | 747 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); |
742 } | 748 } |
743 for (j = 0; j < s->prim_channels; j++) { | 749 for (j = base_channel; j < s->prim_channels; j++) { |
744 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: "); | 750 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: "); |
745 for (k = 0; k < s->vq_start_subband[j]; k++) | 751 for (k = 0; k < s->vq_start_subband[j]; k++) |
746 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]); | 752 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]); |
747 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 753 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
748 } | 754 } |
749 for (j = 0; j < s->prim_channels; j++) { | 755 for (j = base_channel; j < s->prim_channels; j++) { |
750 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:"); | 756 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:"); |
751 for (k = 0; k < s->subband_activity[j]; k++) | 757 for (k = 0; k < s->subband_activity[j]; k++) |
752 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]); | 758 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]); |
753 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 759 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
754 } | 760 } |
755 for (j = 0; j < s->prim_channels; j++) { | 761 for (j = base_channel; j < s->prim_channels; j++) { |
756 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:"); | 762 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:"); |
757 for (k = 0; k < s->subband_activity[j]; k++) { | 763 for (k = 0; k < s->subband_activity[j]; k++) { |
758 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) | 764 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) |
759 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); | 765 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); |
760 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) | 766 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) |
761 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); | 767 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); |
762 } | 768 } |
763 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 769 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
764 } | 770 } |
765 for (j = 0; j < s->prim_channels; j++) { | 771 for (j = base_channel; j < s->prim_channels; j++) { |
766 if (s->joint_intensity[j] > 0) { | 772 if (s->joint_intensity[j] > 0) { |
767 int source_channel = s->joint_intensity[j] - 1; | 773 int source_channel = s->joint_intensity[j] - 1; |
768 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n"); | 774 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n"); |
769 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) | 775 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) |
770 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]); | 776 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]); |
771 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 777 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
772 } | 778 } |
773 } | 779 } |
774 if (s->prim_channels > 2 && s->downmix) { | 780 if (!base_channel && s->prim_channels > 2 && s->downmix) { |
775 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); | 781 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); |
776 for (j = 0; j < s->prim_channels; j++) { | 782 for (j = 0; j < s->prim_channels; j++) { |
777 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); | 783 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); |
778 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); | 784 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); |
779 } | 785 } |
780 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | 786 av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
781 } | 787 } |
782 for (j = 0; j < s->prim_channels; j++) | 788 for (j = base_channel; j < s->prim_channels; j++) |
783 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | 789 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) |
784 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); | 790 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); |
785 if(s->lfe){ | 791 if (!base_channel && s->lfe) { |
786 int lfe_samples = 2 * s->lfe * (4 + block_index); | 792 int lfe_samples = 2 * s->lfe * (4 + block_index); |
787 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); | 793 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); |
788 | 794 |
789 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); | 795 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); |
790 for (j = lfe_samples; j < lfe_end_sample; j++) | 796 for (j = lfe_samples; j < lfe_end_sample; j++) |
952 } | 958 } |
953 | 959 |
954 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; | 960 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; |
955 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; | 961 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; |
956 | 962 |
957 static int dca_subsubframe(DCAContext * s, int block_index) | 963 static int dca_subsubframe(DCAContext * s, int base_channel, int block_index) |
958 { | 964 { |
959 int k, l; | 965 int k, l; |
960 int subsubframe = s->current_subsubframe; | 966 int subsubframe = s->current_subsubframe; |
961 | 967 |
962 const float *quant_step_table; | 968 const float *quant_step_table; |
973 if (s->bit_rate_index == 0x1f) | 979 if (s->bit_rate_index == 0x1f) |
974 quant_step_table = lossless_quant_d; | 980 quant_step_table = lossless_quant_d; |
975 else | 981 else |
976 quant_step_table = lossy_quant_d; | 982 quant_step_table = lossy_quant_d; |
977 | 983 |
978 for (k = 0; k < s->prim_channels; k++) { | 984 for (k = base_channel; k < s->prim_channels; k++) { |
979 for (l = 0; l < s->vq_start_subband[k]; l++) { | 985 for (l = 0; l < s->vq_start_subband[k]; l++) { |
980 int m; | 986 int m; |
981 | 987 |
982 /* Select the mid-tread linear quantizer */ | 988 /* Select the mid-tread linear quantizer */ |
983 int abits = s->bitalloc[k][l]; | 989 int abits = s->bitalloc[k][l]; |
1070 } | 1076 } |
1071 } | 1077 } |
1072 } | 1078 } |
1073 | 1079 |
1074 /* Check for DSYNC after subsubframe */ | 1080 /* Check for DSYNC after subsubframe */ |
1075 if (s->aspf || subsubframe == s->subsubframes - 1) { | 1081 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) { |
1076 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */ | 1082 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */ |
1077 #ifdef TRACE | 1083 #ifdef TRACE |
1078 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n"); | 1084 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n"); |
1079 #endif | 1085 #endif |
1080 } else { | 1086 } else { |
1081 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n"); | 1087 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n"); |
1082 } | 1088 } |
1083 } | 1089 } |
1084 | 1090 |
1085 /* Backup predictor history for adpcm */ | 1091 /* Backup predictor history for adpcm */ |
1086 for (k = 0; k < s->prim_channels; k++) | 1092 for (k = base_channel; k < s->prim_channels; k++) |
1087 for (l = 0; l < s->vq_start_subband[k]; l++) | 1093 for (l = 0; l < s->vq_start_subband[k]; l++) |
1088 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4], | 1094 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4], |
1089 4 * sizeof(subband_samples[0][0][0])); | 1095 4 * sizeof(subband_samples[0][0][0])); |
1090 | 1096 |
1091 return 0; | 1097 return 0; |
1121 | 1127 |
1122 return 0; | 1128 return 0; |
1123 } | 1129 } |
1124 | 1130 |
1125 | 1131 |
1126 static int dca_subframe_footer(DCAContext * s) | 1132 static int dca_subframe_footer(DCAContext * s, int base_channel) |
1127 { | 1133 { |
1128 int aux_data_count = 0, i; | 1134 int aux_data_count = 0, i; |
1129 | 1135 |
1130 /* | 1136 /* |
1131 * Unpack optional information | 1137 * Unpack optional information |
1132 */ | 1138 */ |
1133 | 1139 |
1140 /* presumably optional information only appears in the core? */ | |
1141 if (!base_channel) { | |
1134 if (s->timestamp) | 1142 if (s->timestamp) |
1135 get_bits(&s->gb, 32); | 1143 get_bits(&s->gb, 32); |
1136 | 1144 |
1137 if (s->aux_data) | 1145 if (s->aux_data) |
1138 aux_data_count = get_bits(&s->gb, 6); | 1146 aux_data_count = get_bits(&s->gb, 6); |
1140 for (i = 0; i < aux_data_count; i++) | 1148 for (i = 0; i < aux_data_count; i++) |
1141 get_bits(&s->gb, 8); | 1149 get_bits(&s->gb, 8); |
1142 | 1150 |
1143 if (s->crc_present && (s->downmix || s->dynrange)) | 1151 if (s->crc_present && (s->downmix || s->dynrange)) |
1144 get_bits(&s->gb, 16); | 1152 get_bits(&s->gb, 16); |
1153 } | |
1145 | 1154 |
1146 return 0; | 1155 return 0; |
1147 } | 1156 } |
1148 | 1157 |
1149 /** | 1158 /** |
1150 * Decode a dca frame block | 1159 * Decode a dca frame block |
1151 * | 1160 * |
1152 * @param s pointer to the DCAContext | 1161 * @param s pointer to the DCAContext |
1153 */ | 1162 */ |
1154 | 1163 |
1155 static int dca_decode_block(DCAContext * s, int block_index) | 1164 static int dca_decode_block(DCAContext * s, int base_channel, int block_index) |
1156 { | 1165 { |
1157 | 1166 |
1158 /* Sanity check */ | 1167 /* Sanity check */ |
1159 if (s->current_subframe >= s->subframes) { | 1168 if (s->current_subframe >= s->subframes) { |
1160 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i", | 1169 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i", |
1165 if (!s->current_subsubframe) { | 1174 if (!s->current_subsubframe) { |
1166 #ifdef TRACE | 1175 #ifdef TRACE |
1167 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n"); | 1176 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n"); |
1168 #endif | 1177 #endif |
1169 /* Read subframe header */ | 1178 /* Read subframe header */ |
1170 if (dca_subframe_header(s, block_index)) | 1179 if (dca_subframe_header(s, base_channel, block_index)) |
1171 return -1; | 1180 return -1; |
1172 } | 1181 } |
1173 | 1182 |
1174 /* Read subsubframe */ | 1183 /* Read subsubframe */ |
1175 #ifdef TRACE | 1184 #ifdef TRACE |
1176 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n"); | 1185 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n"); |
1177 #endif | 1186 #endif |
1178 if (dca_subsubframe(s, block_index)) | 1187 if (dca_subsubframe(s, base_channel, block_index)) |
1179 return -1; | 1188 return -1; |
1180 | 1189 |
1181 /* Update state */ | 1190 /* Update state */ |
1182 s->current_subsubframe++; | 1191 s->current_subsubframe++; |
1183 if (s->current_subsubframe >= s->subsubframes) { | 1192 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) { |
1184 s->current_subsubframe = 0; | 1193 s->current_subsubframe = 0; |
1185 s->current_subframe++; | 1194 s->current_subframe++; |
1186 } | 1195 } |
1187 if (s->current_subframe >= s->subframes) { | 1196 if (s->current_subframe >= s->subframes) { |
1188 #ifdef TRACE | 1197 #ifdef TRACE |
1189 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n"); | 1198 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n"); |
1190 #endif | 1199 #endif |
1191 /* Read subframe footer */ | 1200 /* Read subframe footer */ |
1192 if (dca_subframe_footer(s)) | 1201 if (dca_subframe_footer(s, base_channel)) |
1193 return -1; | 1202 return -1; |
1194 } | 1203 } |
1195 | 1204 |
1196 return 0; | 1205 return 0; |
1197 } | 1206 } |
1247 { | 1256 { |
1248 const uint8_t *buf = avpkt->data; | 1257 const uint8_t *buf = avpkt->data; |
1249 int buf_size = avpkt->size; | 1258 int buf_size = avpkt->size; |
1250 | 1259 |
1251 int lfe_samples; | 1260 int lfe_samples; |
1261 int num_core_channels = 0; | |
1252 int i; | 1262 int i; |
1263 int xch_present = 0; | |
1253 int16_t *samples = data; | 1264 int16_t *samples = data; |
1254 DCAContext *s = avctx->priv_data; | 1265 DCAContext *s = avctx->priv_data; |
1255 int channels; | 1266 int channels; |
1256 | 1267 |
1257 | 1268 |
1270 //set AVCodec values with parsed data | 1281 //set AVCodec values with parsed data |
1271 avctx->sample_rate = s->sample_rate; | 1282 avctx->sample_rate = s->sample_rate; |
1272 avctx->bit_rate = s->bit_rate; | 1283 avctx->bit_rate = s->bit_rate; |
1273 | 1284 |
1274 for (i = 0; i < (s->sample_blocks / 8); i++) { | 1285 for (i = 0; i < (s->sample_blocks / 8); i++) { |
1275 dca_decode_block(s, i); | 1286 dca_decode_block(s, 0, i); |
1287 } | |
1288 | |
1289 /* record number of core channels incase less than max channels are requested */ | |
1290 num_core_channels = s->prim_channels; | |
1291 | |
1292 /* extensions start at 32-bit boundaries into bitstream */ | |
1293 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 31); | |
1294 | |
1295 while(get_bits_left(&s->gb) >= 32) { | |
1296 uint32_t bits = get_bits(&s->gb, 32); | |
1297 | |
1298 switch(bits) { | |
1299 case 0x5a5a5a5a: { | |
1300 int ext_base_ch = s->prim_channels; | |
1301 int ext_amode; | |
1302 | |
1303 /* skip length-to-end-of-frame field for the moment */ | |
1304 skip_bits(&s->gb, 10); | |
1305 | |
1306 /* extension amode should == 1, number of channels in extension */ | |
1307 /* AFAIK XCh is not used for more channels */ | |
1308 if ((ext_amode = get_bits(&s->gb, 4)) != 1) { | |
1309 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not" | |
1310 " supported!\n",ext_amode); | |
1311 continue; | |
1312 } | |
1313 | |
1314 /* much like core primary audio coding header */ | |
1315 dca_parse_audio_coding_header(s, ext_base_ch); | |
1316 | |
1317 for (i = 0; i < (s->sample_blocks / 8); i++) { | |
1318 dca_decode_block(s, ext_base_ch, i); | |
1319 } | |
1320 | |
1321 xch_present = 1; | |
1322 break; | |
1323 } | |
1324 case 0x1d95f262: | |
1325 av_log(avctx, AV_LOG_DEBUG, "Possible X96 extension found at %d bits\n", get_bits_count(&s->gb)); | |
1326 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", get_bits(&s->gb, 12)+1); | |
1327 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4)); | |
1328 break; | |
1329 } | |
1330 | |
1331 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 31); | |
1276 } | 1332 } |
1277 | 1333 |
1278 channels = s->prim_channels + !!s->lfe; | 1334 channels = s->prim_channels + !!s->lfe; |
1279 | 1335 |
1280 if (s->amode<16) { | 1336 if (s->amode<16) { |
1281 avctx->channel_layout = dca_core_channel_layout[s->amode]; | 1337 avctx->channel_layout = dca_core_channel_layout[s->amode]; |
1282 | 1338 |
1283 if (s->lfe) { | 1339 if (xch_present && (!avctx->request_channels || |
1284 avctx->channel_layout |= CH_LOW_FREQUENCY; | 1340 avctx->request_channels > num_core_channels)) { |
1285 s->channel_order_tab = dca_channel_reorder_lfe[s->amode]; | 1341 avctx->channel_layout |= CH_BACK_CENTER; |
1286 } else | 1342 if (s->lfe) { |
1287 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode]; | 1343 avctx->channel_layout |= CH_LOW_FREQUENCY; |
1344 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode]; | |
1345 } else { | |
1346 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode]; | |
1347 } | |
1348 } else { | |
1349 if (s->lfe) { | |
1350 avctx->channel_layout |= CH_LOW_FREQUENCY; | |
1351 s->channel_order_tab = dca_channel_reorder_lfe[s->amode]; | |
1352 } else | |
1353 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode]; | |
1354 } | |
1288 | 1355 |
1289 if (s->prim_channels > 0 && | 1356 if (s->prim_channels > 0 && |
1290 s->channel_order_tab[s->prim_channels - 1] < 0) | 1357 s->channel_order_tab[s->prim_channels - 1] < 0) |
1291 return -1; | 1358 return -1; |
1292 | 1359 |