Mercurial > libavcodec.hg
comparison aac.c @ 7581:6fdffa4836a7 libavcodec
More OKed AAC decoder hunks
| author | superdump |
|---|---|
| date | Fri, 15 Aug 2008 08:01:31 +0000 |
| parents | c49ab52db74c |
| children | 3f6fc60f1ed3 |
comparison
equal
deleted
inserted
replaced
| 7580:c49ab52db74c | 7581:6fdffa4836a7 |
|---|---|
| 88 #include <assert.h> | 88 #include <assert.h> |
| 89 #include <errno.h> | 89 #include <errno.h> |
| 90 #include <math.h> | 90 #include <math.h> |
| 91 #include <string.h> | 91 #include <string.h> |
| 92 | 92 |
| 93 #ifndef CONFIG_HARDCODED_TABLES | |
| 94 static float ff_aac_pow2sf_tab[316]; | |
| 95 #endif /* CONFIG_HARDCODED_TABLES */ | |
| 96 | |
| 97 static VLC vlc_scalefactors; | 93 static VLC vlc_scalefactors; |
| 98 static VLC vlc_spectral[11]; | 94 static VLC vlc_spectral[11]; |
| 99 | 95 |
| 100 | 96 |
| 101 /** | 97 /** |
| 411 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), | 407 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), |
| 412 352); | 408 352); |
| 413 | 409 |
| 414 ff_mdct_init(&ac->mdct, 11, 1); | 410 ff_mdct_init(&ac->mdct, 11, 1); |
| 415 ff_mdct_init(&ac->mdct_small, 8, 1); | 411 ff_mdct_init(&ac->mdct_small, 8, 1); |
| 412 // window initialization | |
| 413 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024); | |
| 414 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128); | |
| 415 ff_sine_window_init(ff_sine_1024, 1024); | |
| 416 ff_sine_window_init(ff_sine_128, 128); | |
| 417 | |
| 416 return 0; | 418 return 0; |
| 417 } | 419 } |
| 418 | 420 |
| 419 /** | 421 /** |
| 420 * Skip data_stream_element; reference: table 4.10. | 422 * Skip data_stream_element; reference: table 4.10. |
| 444 ics->window_sequence[0] = get_bits(gb, 2); | 446 ics->window_sequence[0] = get_bits(gb, 2); |
| 445 ics->use_kb_window[1] = ics->use_kb_window[0]; | 447 ics->use_kb_window[1] = ics->use_kb_window[0]; |
| 446 ics->use_kb_window[0] = get_bits1(gb); | 448 ics->use_kb_window[0] = get_bits1(gb); |
| 447 ics->num_window_groups = 1; | 449 ics->num_window_groups = 1; |
| 448 ics->group_len[0] = 1; | 450 ics->group_len[0] = 1; |
| 449 | 451 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { |
| 452 int i; | |
| 453 ics->max_sfb = get_bits(gb, 4); | |
| 454 for (i = 0; i < 7; i++) { | |
| 455 if (get_bits1(gb)) { | |
| 456 ics->group_len[ics->num_window_groups-1]++; | |
| 457 } else { | |
| 458 ics->num_window_groups++; | |
| 459 ics->group_len[ics->num_window_groups-1] = 1; | |
| 460 } | |
| 461 } | |
| 462 ics->num_windows = 8; | |
| 463 ics->swb_offset = swb_offset_128[ac->m4ac.sampling_index]; | |
| 464 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index]; | |
| 465 ics->tns_max_bands = tns_max_bands_128[ac->m4ac.sampling_index]; | |
| 466 } else { | |
| 467 ics->max_sfb = get_bits(gb, 6); | |
| 468 ics->num_windows = 1; | |
| 469 ics->swb_offset = swb_offset_1024[ac->m4ac.sampling_index]; | |
| 470 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index]; | |
| 471 ics->tns_max_bands = tns_max_bands_1024[ac->m4ac.sampling_index]; | |
| 450 if (get_bits1(gb)) { | 472 if (get_bits1(gb)) { |
| 451 av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1); | 473 av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1); |
| 452 memset(ics, 0, sizeof(IndividualChannelStream)); | 474 memset(ics, 0, sizeof(IndividualChannelStream)); |
| 453 return -1; | 475 return -1; |
| 454 } | 476 } |
| 493 if (sect_len > ics->max_sfb) { | 515 if (sect_len > ics->max_sfb) { |
| 494 av_log(ac->avccontext, AV_LOG_ERROR, | 516 av_log(ac->avccontext, AV_LOG_ERROR, |
| 495 "Number of bands (%d) exceeds limit (%d).\n", | 517 "Number of bands (%d) exceeds limit (%d).\n", |
| 496 sect_len, ics->max_sfb); | 518 sect_len, ics->max_sfb); |
| 497 return -1; | 519 return -1; |
| 520 } | |
| 521 for (; k < sect_len; k++) { | |
| 522 band_type [idx] = sect_band_type; | |
| 523 band_type_run_end[idx++] = sect_len; | |
| 498 } | 524 } |
| 499 } | 525 } |
| 500 } | 526 } |
| 501 return 0; | 527 return 0; |
| 502 } | 528 } |
| 595 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0])); | 621 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0])); |
| 596 } | 622 } |
| 597 } | 623 } |
| 598 | 624 |
| 599 /** | 625 /** |
| 626 * Decode spectral data; reference: table 4.50. | |
| 627 * Dequantize and scale spectral data; reference: 4.6.3.3. | |
| 628 * | |
| 629 * @param coef array of dequantized, scaled spectral data | |
| 630 * @param sf array of scalefactors or intensity stereo positions | |
| 631 * @param pulse_present set if pulses are present | |
| 632 * @param pulse pointer to pulse data struct | |
| 633 * @param band_type array of the used band type | |
| 634 * | |
| 635 * @return Returns error status. 0 - OK, !0 - error | |
| 636 */ | |
| 637 static int decode_spectrum_and_dequant(AACContext * ac, float coef[1024], GetBitContext * gb, float sf[120], | |
| 638 int pulse_present, const Pulse * pulse, const IndividualChannelStream * ics, enum BandType band_type[120]) { | |
| 639 int i, k, g, idx = 0; | |
| 640 const int c = 1024/ics->num_windows; | |
| 641 const uint16_t * offsets = ics->swb_offset; | |
| 642 float *coef_base = coef; | |
| 643 | |
| 644 for (g = 0; g < ics->num_windows; g++) | |
| 645 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float)*(c - offsets[ics->max_sfb])); | |
| 646 | |
| 647 for (g = 0; g < ics->num_window_groups; g++) { | |
| 648 for (i = 0; i < ics->max_sfb; i++, idx++) { | |
| 649 const int cur_band_type = band_type[idx]; | |
| 650 const int dim = cur_band_type >= FIRST_PAIR_BT ? 2 : 4; | |
| 651 const int is_cb_unsigned = IS_CODEBOOK_UNSIGNED(cur_band_type); | |
| 652 int group; | |
| 653 if (cur_band_type == ZERO_BT) { | |
| 654 for (group = 0; group < ics->group_len[g]; group++) { | |
| 655 memset(coef + group * 128 + offsets[i], 0, (offsets[i+1] - offsets[i])*sizeof(float)); | |
| 656 } | |
| 657 }else if (cur_band_type == NOISE_BT) { | |
| 658 const float scale = sf[idx] / ((offsets[i+1] - offsets[i]) * PNS_MEAN_ENERGY); | |
| 659 for (group = 0; group < ics->group_len[g]; group++) { | |
| 660 for (k = offsets[i]; k < offsets[i+1]; k++) { | |
| 661 ac->random_state = lcg_random(ac->random_state); | |
| 662 coef[group*128+k] = ac->random_state * scale; | |
| 663 } | |
| 664 } | |
| 665 }else if (cur_band_type != INTENSITY_BT2 && cur_band_type != INTENSITY_BT) { | |
| 666 for (group = 0; group < ics->group_len[g]; group++) { | |
| 667 for (k = offsets[i]; k < offsets[i+1]; k += dim) { | |
| 668 const int index = get_vlc2(gb, vlc_spectral[cur_band_type - 1].table, 6, 3); | |
| 669 const int coef_tmp_idx = (group << 7) + k; | |
| 670 const float *vq_ptr; | |
| 671 int j; | |
| 672 if(index >= ff_aac_spectral_sizes[cur_band_type - 1]) { | |
| 673 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 674 "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n", | |
| 675 cur_band_type - 1, index, ff_aac_spectral_sizes[cur_band_type - 1]); | |
| 676 return -1; | |
| 677 } | |
| 678 vq_ptr = &ff_aac_codebook_vectors[cur_band_type - 1][index * dim]; | |
| 679 if (is_cb_unsigned) { | |
| 680 for (j = 0; j < dim; j++) | |
| 681 if (vq_ptr[j]) | |
| 682 coef[coef_tmp_idx + j] = 1 - 2*(int)get_bits1(gb); | |
| 683 }else { | |
| 684 for (j = 0; j < dim; j++) | |
| 685 coef[coef_tmp_idx + j] = 1.0f; | |
| 686 } | |
| 687 if (cur_band_type == ESC_BT) { | |
| 688 for (j = 0; j < 2; j++) { | |
| 689 if (vq_ptr[j] == 64.0f) { | |
| 690 int n = 4; | |
| 691 /* The total length of escape_sequence must be < 22 bits according | |
| 692 to the specification (i.e. max is 11111111110xxxxxxxxxx). */ | |
| 693 while (get_bits1(gb) && n < 15) n++; | |
| 694 if(n == 15) { | |
| 695 av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n"); | |
| 696 return -1; | |
| 697 } | |
| 698 n = (1<<n) + get_bits(gb, n); | |
| 699 coef[coef_tmp_idx + j] *= cbrtf(fabsf(n)) * n; | |
| 700 }else | |
| 701 coef[coef_tmp_idx + j] *= vq_ptr[j]; | |
| 702 } | |
| 703 }else | |
| 704 for (j = 0; j < dim; j++) | |
| 705 coef[coef_tmp_idx + j] *= vq_ptr[j]; | |
| 706 for (j = 0; j < dim; j++) | |
| 707 coef[coef_tmp_idx + j] *= sf[idx]; | |
| 708 } | |
| 709 } | |
| 710 } | |
| 711 } | |
| 712 coef += ics->group_len[g]<<7; | |
| 713 } | |
| 714 | |
| 715 if (pulse_present) { | |
| 716 for(i = 0; i < pulse->num_pulse; i++){ | |
| 717 float co = coef_base[ pulse->pos[i] ]; | |
| 718 float ico = co / sqrtf(sqrtf(fabsf(co))) + pulse->amp[i]; | |
| 719 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico; | |
| 720 } | |
| 721 } | |
| 722 return 0; | |
| 723 } | |
| 724 | |
| 725 /** | |
| 600 * Decode an individual_channel_stream payload; reference: table 4.44. | 726 * Decode an individual_channel_stream payload; reference: table 4.44. |
| 601 * | 727 * |
| 602 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. | 728 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. |
| 603 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.) | 729 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.) |
| 604 * | 730 * |
| 646 } | 772 } |
| 647 | 773 |
| 648 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0) | 774 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0) |
| 649 return -1; | 775 return -1; |
| 650 return 0; | 776 return 0; |
| 777 } | |
| 778 | |
| 779 /** | |
| 780 * Mid/Side stereo decoding; reference: 4.6.8.1.3. | |
| 781 */ | |
| 782 static void apply_mid_side_stereo(ChannelElement * cpe) { | |
| 783 const IndividualChannelStream * ics = &cpe->ch[0].ics; | |
| 784 float *ch0 = cpe->ch[0].coeffs; | |
| 785 float *ch1 = cpe->ch[1].coeffs; | |
| 786 int g, i, k, group, idx = 0; | |
| 787 const uint16_t * offsets = ics->swb_offset; | |
| 788 for (g = 0; g < ics->num_window_groups; g++) { | |
| 789 for (i = 0; i < ics->max_sfb; i++, idx++) { | |
| 790 if (cpe->ms_mask[idx] && | |
| 791 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) { | |
| 792 for (group = 0; group < ics->group_len[g]; group++) { | |
| 793 for (k = offsets[i]; k < offsets[i+1]; k++) { | |
| 794 float tmp = ch0[group*128 + k] - ch1[group*128 + k]; | |
| 795 ch0[group*128 + k] += ch1[group*128 + k]; | |
| 796 ch1[group*128 + k] = tmp; | |
| 797 } | |
| 798 } | |
| 799 } | |
| 800 } | |
| 801 ch0 += ics->group_len[g]*128; | |
| 802 ch1 += ics->group_len[g]*128; | |
| 803 } | |
| 804 } | |
| 805 | |
| 806 /** | |
| 807 * intensity stereo decoding; reference: 4.6.8.2.3 | |
| 808 * | |
| 809 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s; | |
| 810 * [1] mask is decoded from bitstream; [2] mask is all 1s; | |
| 811 * [3] reserved for scalable AAC | |
| 812 */ | |
| 813 static void apply_intensity_stereo(ChannelElement * cpe, int ms_present) { | |
| 814 const IndividualChannelStream * ics = &cpe->ch[1].ics; | |
| 815 SingleChannelElement * sce1 = &cpe->ch[1]; | |
| 816 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs; | |
| 817 const uint16_t * offsets = ics->swb_offset; | |
| 818 int g, group, i, k, idx = 0; | |
| 819 int c; | |
| 820 float scale; | |
| 821 for (g = 0; g < ics->num_window_groups; g++) { | |
| 822 for (i = 0; i < ics->max_sfb;) { | |
| 823 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) { | |
| 824 const int bt_run_end = sce1->band_type_run_end[idx]; | |
| 825 for (; i < bt_run_end; i++, idx++) { | |
| 826 c = -1 + 2 * (sce1->band_type[idx] - 14); | |
| 827 if (ms_present) | |
| 828 c *= 1 - 2 * cpe->ms_mask[idx]; | |
| 829 scale = c * sce1->sf[idx]; | |
| 830 for (group = 0; group < ics->group_len[g]; group++) | |
| 831 for (k = offsets[i]; k < offsets[i+1]; k++) | |
| 832 coef1[group*128 + k] = scale * coef0[group*128 + k]; | |
| 833 } | |
| 834 } else { | |
| 835 int bt_run_end = sce1->band_type_run_end[idx]; | |
| 836 idx += bt_run_end - i; | |
| 837 i = bt_run_end; | |
| 838 } | |
| 839 } | |
| 840 coef0 += ics->group_len[g]*128; | |
| 841 coef1 += ics->group_len[g]*128; | |
| 842 } | |
| 651 } | 843 } |
| 652 | 844 |
| 653 /** | 845 /** |
| 654 * Decode a channel_pair_element; reference: table 4.4. | 846 * Decode a channel_pair_element; reference: table 4.4. |
| 655 * | 847 * |
| 685 apply_mid_side_stereo(cpe); | 877 apply_mid_side_stereo(cpe); |
| 686 | 878 |
| 687 apply_intensity_stereo(cpe, ms_present); | 879 apply_intensity_stereo(cpe, ms_present); |
| 688 return 0; | 880 return 0; |
| 689 } | 881 } |
| 882 | |
| 883 /** | |
| 884 * Decode coupling_channel_element; reference: table 4.8. | |
| 885 * | |
| 886 * @param elem_id Identifies the instance of a syntax element. | |
| 887 * | |
| 888 * @return Returns error status. 0 - OK, !0 - error | |
| 889 */ | |
| 890 static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) { | |
| 891 int num_gain = 0; | |
| 892 int c, g, sfb, ret, idx = 0; | |
| 893 int sign; | |
| 894 float scale; | |
| 895 SingleChannelElement * sce = &che->ch[0]; | |
| 896 ChannelCoupling * coup = &che->coup; | |
| 690 | 897 |
| 691 coup->coupling_point = 2*get_bits1(gb); | 898 coup->coupling_point = 2*get_bits1(gb); |
| 692 coup->num_coupled = get_bits(gb, 3); | 899 coup->num_coupled = get_bits(gb, 3); |
| 693 for (c = 0; c <= coup->num_coupled; c++) { | 900 for (c = 0; c <= coup->num_coupled; c++) { |
| 694 num_gain++; | 901 num_gain++; |
| 964 int i; | 1171 int i; |
| 965 for (i = 0; i < 1024; i++) | 1172 for (i = 0; i < 1024; i++) |
| 966 sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); | 1173 sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); |
| 967 } | 1174 } |
| 968 | 1175 |
| 1176 /** | |
| 1177 * channel coupling transformation interface | |
| 1178 * | |
| 1179 * @param index index into coupling gain array | |
| 1180 * @param apply_coupling_method pointer to (in)dependent coupling function | |
| 1181 */ | |
| 1182 static void apply_channel_coupling(AACContext * ac, ChannelElement * cc, | |
| 1183 void (*apply_coupling_method)(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index)) | |
| 1184 { | |
| 1185 int c; | |
| 1186 int index = 0; | |
| 1187 ChannelCoupling * coup = &cc->coup; | |
| 1188 for (c = 0; c <= coup->num_coupled; c++) { | |
| 1189 if (ac->che[coup->type[c]][coup->id_select[c]]) { | |
| 1190 if (coup->ch_select[c] != 2) { | |
| 1191 apply_coupling_method(ac, &ac->che[coup->type[c]][coup->id_select[c]]->ch[0], cc, index); | |
| 1192 if (coup->ch_select[c] != 0) | |
| 1193 index++; | |
| 1194 } | |
| 1195 if (coup->ch_select[c] != 1) | |
| 1196 apply_coupling_method(ac, &ac->che[coup->type[c]][coup->id_select[c]]->ch[1], cc, index++); | |
| 1197 } else { | |
| 1198 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 1199 "coupling target %sE[%d] not available\n", | |
| 1200 coup->type[c] == TYPE_CPE ? "CP" : "SC", coup->id_select[c]); | |
| 1201 break; | |
| 1202 } | |
| 1203 } | |
| 1204 } | |
| 1205 | |
| 1206 /** | |
| 1207 * Convert spectral data to float samples, applying all supported tools as appropriate. | |
| 1208 */ | |
| 1209 static void spectral_to_sample(AACContext * ac) { | |
| 1210 int i, type; | |
| 1211 for (i = 0; i < MAX_ELEM_ID; i++) { | |
| 1212 for(type = 0; type < 4; type++) { | |
| 1213 ChannelElement *che = ac->che[type][i]; | |
| 1214 if(che) { | |
| 1215 if(che->coup.coupling_point == BEFORE_TNS) | |
| 1216 apply_channel_coupling(ac, che, apply_dependent_coupling); | |
| 1217 if(che->ch[0].tns.present) | |
| 1218 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1); | |
| 1219 if(che->ch[1].tns.present) | |
| 1220 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1); | |
| 1221 if(che->coup.coupling_point == BETWEEN_TNS_AND_IMDCT) | |
| 1222 apply_channel_coupling(ac, che, apply_dependent_coupling); | |
| 1223 imdct_and_windowing(ac, &che->ch[0]); | |
| 1224 if(type == TYPE_CPE) | |
| 1225 imdct_and_windowing(ac, &che->ch[1]); | |
| 1226 if(che->coup.coupling_point == AFTER_IMDCT) | |
| 1227 apply_channel_coupling(ac, che, apply_independent_coupling); | |
| 969 } | 1228 } |
| 970 } | 1229 } |
| 971 } | 1230 } |
| 972 } | 1231 } |
| 973 | 1232 |
