Mercurial > libavcodec.hg
comparison aac.c @ 7578:a05954c505ab libavcodec
More OKed sections of AAC decoder code
author | superdump |
---|---|
date | Fri, 15 Aug 2008 00:05:09 +0000 |
parents | 5298e05f931d |
children | c49ab52db74c |
comparison
equal
deleted
inserted
replaced
7577:ed956c3c2cf3 | 7578:a05954c505ab |
---|---|
98 static VLC vlc_scalefactors; | 98 static VLC vlc_scalefactors; |
99 static VLC vlc_spectral[11]; | 99 static VLC vlc_spectral[11]; |
100 | 100 |
101 | 101 |
102 /** | 102 /** |
103 * Configure output channel order based on the current program configuration element. | |
104 * | |
105 * @param che_pos current channel position configuration | |
106 * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. | |
107 * | |
108 * @return Returns error status. 0 - OK, !0 - error | |
109 */ | |
110 static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID], | |
111 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]) { | |
112 AVCodecContext *avctx = ac->avccontext; | |
113 int i, type, channels = 0; | |
114 | |
115 if(!memcmp(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]))) | |
116 return 0; /* no change */ | |
117 | |
118 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); | |
119 | |
120 /* Allocate or free elements depending on if they are in the | |
121 * current program configuration. | |
122 * | |
123 * Set up default 1:1 output mapping. | |
124 * | |
125 * For a 5.1 stream the output order will be: | |
126 * [ Front Left ] [ Front Right ] [ Center ] [ LFE ] [ Surround Left ] [ Surround Right ] | |
127 */ | |
128 | |
129 for(i = 0; i < MAX_ELEM_ID; i++) { | |
130 for(type = 0; type < 4; type++) { | |
131 if(che_pos[type][i]) { | |
132 if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement)))) | |
133 return AVERROR(ENOMEM); | |
134 if(type != TYPE_CCE) { | |
135 ac->output_data[channels++] = ac->che[type][i]->ch[0].ret; | |
136 if(type == TYPE_CPE) { | |
137 ac->output_data[channels++] = ac->che[type][i]->ch[1].ret; | |
138 } | |
139 } | |
140 } else | |
141 av_freep(&ac->che[type][i]); | |
142 } | |
143 } | |
144 | |
145 avctx->channels = channels; | |
146 return 0; | |
147 } | |
148 | |
149 /** | |
103 * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit. | 150 * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit. |
104 * | 151 * |
105 * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present. | 152 * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present. |
106 * @param sce_map mono (Single Channel Element) map | 153 * @param sce_map mono (Single Channel Element) map |
107 * @param type speaker type/position for these channels | 154 * @param type speaker type/position for these channels |
207 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right | 254 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right |
208 | 255 |
209 return 0; | 256 return 0; |
210 } | 257 } |
211 | 258 |
259 /** | |
260 * Decode GA "General Audio" specific configuration; reference: table 4.1. | |
261 * | |
262 * @return Returns error status. 0 - OK, !0 - error | |
263 */ | |
264 static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) { | |
265 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]; | |
266 int extension_flag, ret; | |
267 | |
268 if(get_bits1(gb)) { // frameLengthFlag | |
269 av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1); | |
212 return -1; | 270 return -1; |
213 } | 271 } |
214 | 272 |
215 if (get_bits1(gb)) // dependsOnCoreCoder | 273 if (get_bits1(gb)) // dependsOnCoreCoder |
216 skip_bits(gb, 14); // coreCoderDelay | 274 skip_bits(gb, 14); // coreCoderDelay |
287 return -1; | 345 return -1; |
288 } | 346 } |
289 return 0; | 347 return 0; |
290 } | 348 } |
291 | 349 |
350 /** | |
351 * linear congruential pseudorandom number generator | |
352 * | |
353 * @param previous_val pointer to the current state of the generator | |
354 * | |
355 * @return Returns a 32-bit pseudorandom integer | |
356 */ | |
357 static av_always_inline int lcg_random(int previous_val) { | |
358 return previous_val * 1664525 + 1013904223; | |
359 } | |
360 | |
292 static av_cold int aac_decode_init(AVCodecContext * avccontext) { | 361 static av_cold int aac_decode_init(AVCodecContext * avccontext) { |
293 AACContext * ac = avccontext->priv_data; | 362 AACContext * ac = avccontext->priv_data; |
294 int i; | 363 int i; |
295 | 364 |
296 ac->avccontext = avccontext; | 365 ac->avccontext = avccontext; |
378 ics->window_sequence[0] = get_bits(gb, 2); | 447 ics->window_sequence[0] = get_bits(gb, 2); |
379 ics->use_kb_window[1] = ics->use_kb_window[0]; | 448 ics->use_kb_window[1] = ics->use_kb_window[0]; |
380 ics->use_kb_window[0] = get_bits1(gb); | 449 ics->use_kb_window[0] = get_bits1(gb); |
381 ics->num_window_groups = 1; | 450 ics->num_window_groups = 1; |
382 ics->group_len[0] = 1; | 451 ics->group_len[0] = 1; |
452 | |
453 if (get_bits1(gb)) { | |
454 av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1); | |
455 memset(ics, 0, sizeof(IndividualChannelStream)); | |
456 return -1; | |
457 } | |
458 } | |
459 | |
460 if(ics->max_sfb > ics->num_swb) { | |
461 av_log(ac->avccontext, AV_LOG_ERROR, | |
462 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n", | |
463 ics->max_sfb, ics->num_swb); | |
464 memset(ics, 0, sizeof(IndividualChannelStream)); | |
465 return -1; | |
466 } | |
383 | 467 |
384 return 0; | 468 return 0; |
385 } | 469 } |
386 | 470 |
387 /** | 471 /** |
518 * [1] mask is decoded from bitstream; [2] mask is all 1s; | 602 * [1] mask is decoded from bitstream; [2] mask is all 1s; |
519 * [3] reserved for scalable AAC | 603 * [3] reserved for scalable AAC |
520 */ | 604 */ |
521 static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb, | 605 static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb, |
522 int ms_present) { | 606 int ms_present) { |
607 int idx; | |
608 if (ms_present == 1) { | |
609 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++) | |
610 cpe->ms_mask[idx] = get_bits1(gb); | |
611 } else if (ms_present == 2) { | |
612 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0])); | |
613 } | |
614 } | |
523 | 615 |
524 /** | 616 /** |
525 * Add pulses with particular amplitudes to the quantized spectral data; reference: 4.6.3.3. | 617 * Add pulses with particular amplitudes to the quantized spectral data; reference: 4.6.3.3. |
526 * | 618 * |
527 * @param pulse pointer to pulse data struct | 619 * @param pulse pointer to pulse data struct |
633 if (cpe->ch[1].ics.intensity_present) | 725 if (cpe->ch[1].ics.intensity_present) |
634 apply_intensity_stereo(cpe, ms_present); | 726 apply_intensity_stereo(cpe, ms_present); |
635 return 0; | 727 return 0; |
636 } | 728 } |
637 | 729 |
730 coup->coupling_point = 2*get_bits1(gb); | |
731 coup->num_coupled = get_bits(gb, 3); | |
732 for (c = 0; c <= coup->num_coupled; c++) { | |
733 num_gain++; | |
734 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE; | |
735 coup->id_select[c] = get_bits(gb, 4); | |
736 if (coup->type[c] == TYPE_CPE) { | |
737 coup->ch_select[c] = get_bits(gb, 2); | |
738 if (coup->ch_select[c] == 3) | |
739 num_gain++; | |
740 } else | |
741 coup->ch_select[c] = 1; | |
742 } | |
743 coup->coupling_point += get_bits1(gb); | |
744 | |
745 if (coup->coupling_point == 2) { | |
746 av_log(ac->avccontext, AV_LOG_ERROR, | |
747 "Independently switched CCE with 'invalid' domain signalled.\n"); | |
748 memset(coup, 0, sizeof(ChannelCoupling)); | |
749 return -1; | |
750 } | |
751 | |
752 sign = get_bits(gb, 1); | |
753 scale = pow(2., pow(2., get_bits(gb, 2) - 3)); | |
754 | |
755 if ((ret = decode_ics(ac, sce, gb, 0, 0))) | |
756 return ret; | |
757 | |
758 for (c = 0; c < num_gain; c++) { | |
759 int cge = 1; | |
760 int gain = 0; | |
761 float gain_cache = 1.; | |
762 if (c) { | |
763 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb); | |
764 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0; | |
765 gain_cache = pow(scale, gain); | |
766 } | |
767 for (g = 0; g < sce->ics.num_window_groups; g++) | |
768 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) | |
769 if (sce->band_type[idx] != ZERO_BT) { | |
770 if (!cge) { | |
771 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
772 if (t) { | |
773 int s = 1; | |
774 if (sign) { | |
775 s -= 2 * (t & 0x1); | |
776 t >>= 1; | |
777 } | |
778 gain += t; | |
779 gain_cache = pow(scale, gain) * s; | |
780 } | |
781 } | |
782 coup->gain[c][idx] = gain_cache; | |
783 } | |
784 } | |
785 return 0; | |
786 } | |
787 | |
638 /** | 788 /** |
639 * Decode Spectral Band Replication extension data; reference: table 4.55. | 789 * Decode Spectral Band Replication extension data; reference: table 4.55. |
640 * | 790 * |
641 * @param crc flag indicating the presence of CRC checksum | 791 * @param crc flag indicating the presence of CRC checksum |
642 * @param cnt length of TYPE_FIL syntactic element in bytes | 792 * @param cnt length of TYPE_FIL syntactic element in bytes |
646 static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) { | 796 static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) { |
647 // TODO : sbr_extension implementation | 797 // TODO : sbr_extension implementation |
648 av_log_missing_feature(ac->avccontext, "SBR", 0); | 798 av_log_missing_feature(ac->avccontext, "SBR", 0); |
649 skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type | 799 skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type |
650 return cnt; | 800 return cnt; |
801 } | |
802 | |
803 /** | |
804 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53. | |
805 * | |
806 * @return Returns number of bytes consumed. | |
807 */ | |
808 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) { | |
809 int i; | |
810 int num_excl_chan = 0; | |
811 | |
812 do { | |
813 for (i = 0; i < 7; i++) | |
814 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb); | |
815 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb)); | |
816 | |
817 return num_excl_chan / 7; | |
651 } | 818 } |
652 | 819 |
653 /** | 820 /** |
654 * Decode dynamic range information; reference: table 4.52. | 821 * Decode dynamic range information; reference: table 4.52. |
655 * | 822 * |
744 const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024; | 911 const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024; |
745 const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128; | 912 const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128; |
746 float * buf = ac->buf_mdct; | 913 float * buf = ac->buf_mdct; |
747 int i; | 914 int i; |
748 | 915 |
916 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { | |
917 if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) | |
918 av_log(ac->avccontext, AV_LOG_WARNING, | |
919 "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. " | |
920 "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n"); | |
921 for (i = 0; i < 2048; i += 256) { | |
922 ff_imdct_calc(&ac->mdct_small, buf + i, in + i/2); | |
923 ac->dsp.vector_fmul_reverse(ac->revers + i/2, buf + i + 128, swindow, 128); | |
924 } | |
925 for (i = 0; i < 448; i++) out[i] = saved[i] + ac->add_bias; | |
926 | |
927 ac->dsp.vector_fmul_add_add(out + 448 + 0*128, buf + 0*128, swindow_prev, saved + 448 , ac->add_bias, 128, 1); | |
928 ac->dsp.vector_fmul_add_add(out + 448 + 1*128, buf + 2*128, swindow, ac->revers + 0*128, ac->add_bias, 128, 1); | |
929 ac->dsp.vector_fmul_add_add(out + 448 + 2*128, buf + 4*128, swindow, ac->revers + 1*128, ac->add_bias, 128, 1); | |
930 ac->dsp.vector_fmul_add_add(out + 448 + 3*128, buf + 6*128, swindow, ac->revers + 2*128, ac->add_bias, 128, 1); | |
931 ac->dsp.vector_fmul_add_add(out + 448 + 4*128, buf + 8*128, swindow, ac->revers + 3*128, ac->add_bias, 64, 1); | |
932 | |
933 #if 0 | |
934 vector_fmul_add_add_add(&ac->dsp, out + 448 + 1*128, buf + 2*128, swindow, saved + 448 + 1*128, ac->revers + 0*128, ac->add_bias, 128); | |
935 vector_fmul_add_add_add(&ac->dsp, out + 448 + 2*128, buf + 4*128, swindow, saved + 448 + 2*128, ac->revers + 1*128, ac->add_bias, 128); | |
936 vector_fmul_add_add_add(&ac->dsp, out + 448 + 3*128, buf + 6*128, swindow, saved + 448 + 3*128, ac->revers + 2*128, ac->add_bias, 128); | |
937 vector_fmul_add_add_add(&ac->dsp, out + 448 + 4*128, buf + 8*128, swindow, saved + 448 + 4*128, ac->revers + 3*128, ac->add_bias, 64); | |
938 #endif | |
939 | |
940 ac->dsp.vector_fmul_add_add(saved, buf + 1024 + 64, swindow + 64, ac->revers + 3*128+64, 0, 64, 1); | |
941 ac->dsp.vector_fmul_add_add(saved + 64, buf + 1024 + 2*128, swindow, ac->revers + 4*128, 0, 128, 1); | |
942 ac->dsp.vector_fmul_add_add(saved + 192, buf + 1024 + 4*128, swindow, ac->revers + 5*128, 0, 128, 1); | |
943 ac->dsp.vector_fmul_add_add(saved + 320, buf + 1024 + 6*128, swindow, ac->revers + 6*128, 0, 128, 1); | |
944 memcpy( saved + 448, ac->revers + 7*128, 128 * sizeof(float)); | |
945 memset( saved + 576, 0, 448 * sizeof(float)); | |
946 } else { | |
947 ff_imdct_calc(&ac->mdct, buf, in); | |
948 if (ics->window_sequence[0] == LONG_STOP_SEQUENCE) { | |
949 for (i = 0; i < 448; i++) out[i] = saved[i] + ac->add_bias; | |
950 ac->dsp.vector_fmul_add_add(out + 448, buf + 448, swindow_prev, saved + 448, ac->add_bias, 128, 1); | |
951 for (i = 576; i < 1024; i++) out[i] = buf[i] + saved[i] + ac->add_bias; | |
952 } else { | |
953 ac->dsp.vector_fmul_add_add(out, buf, lwindow_prev, saved, ac->add_bias, 1024, 1); | |
954 } | |
955 if (ics->window_sequence[0] == LONG_START_SEQUENCE) { | |
956 memcpy(saved, buf + 1024, 448 * sizeof(float)); | |
957 ac->dsp.vector_fmul_reverse(saved + 448, buf + 1024 + 448, swindow, 128); | |
958 memset(saved + 576, 0, 448 * sizeof(float)); | |
959 } else { | |
960 ac->dsp.vector_fmul_reverse(saved, buf + 1024, lwindow, 1024); | |
961 } | |
962 } | |
963 } | |
964 | |
749 /** | 965 /** |
750 * Apply dependent channel coupling (applied before IMDCT). | 966 * Apply dependent channel coupling (applied before IMDCT). |
751 * | 967 * |
752 * @param index index into coupling gain array | 968 * @param index index into coupling gain array |
753 */ | 969 */ |
786 static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { | 1002 static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { |
787 int i; | 1003 int i; |
788 for (i = 0; i < 1024; i++) | 1004 for (i = 0; i < 1024; i++) |
789 sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); | 1005 sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); |
790 } | 1006 } |
1007 | |
1008 } | |
1009 } | |
1010 } | |
1011 } | |
1012 | |
1013 static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, const uint8_t * buf, int buf_size) { | |
1014 AACContext * ac = avccontext->priv_data; | |
1015 GetBitContext gb; | |
1016 enum RawDataBlockType elem_type; | |
1017 int err, elem_id, data_size_tmp; | |
1018 | |
1019 init_get_bits(&gb, buf, buf_size*8); | |
1020 | |
1021 // parse | |
1022 while ((elem_type = get_bits(&gb, 3)) != TYPE_END) { | |
1023 elem_id = get_bits(&gb, 4); | |
1024 err = -1; | |
1025 | |
1026 if(elem_type == TYPE_SCE && elem_id == 1 && | |
1027 !ac->che[TYPE_SCE][elem_id] && ac->che[TYPE_LFE][0]) { | |
1028 /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1] | |
1029 instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have | |
1030 encountered such a stream, transfer the LFE[0] element to SCE[1] */ | |
1031 ac->che[TYPE_SCE][elem_id] = ac->che[TYPE_LFE][0]; | |
1032 ac->che[TYPE_LFE][0] = NULL; | |
1033 } | |
1034 if(elem_type && elem_type < TYPE_DSE) { | |
1035 if(!ac->che[elem_type][elem_id]) | |
1036 return -1; | |
1037 if(elem_type != TYPE_CCE) | |
1038 ac->che[elem_type][elem_id]->coup.coupling_point = 4; | |
1039 } | |
1040 | |
1041 switch (elem_type) { | |
1042 | |
1043 case TYPE_SCE: | |
1044 err = decode_ics(ac, &ac->che[TYPE_SCE][elem_id]->ch[0], &gb, 0, 0); | |
1045 break; | |
1046 | |
1047 case TYPE_CPE: | |
1048 err = decode_cpe(ac, &gb, elem_id); | |
1049 break; | |
1050 | |
1051 case TYPE_CCE: | |
1052 err = decode_cce(ac, &gb, ac->che[TYPE_SCE][elem_id]); | |
1053 break; | |
1054 | |
1055 case TYPE_LFE: | |
1056 err = decode_ics(ac, &ac->che[TYPE_LFE][elem_id]->ch[0], &gb, 0, 0); | |
1057 break; | |
1058 | |
1059 case TYPE_DSE: | |
1060 skip_data_stream_element(&gb); | |
1061 err = 0; | |
1062 break; | |
1063 | |
1064 case TYPE_PCE: | |
1065 { | |
1066 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]; | |
1067 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); | |
1068 if((err = decode_pce(ac, new_che_pos, &gb))) | |
1069 break; | |
1070 err = output_configure(ac, ac->che_pos, new_che_pos); | |
1071 break; | |
1072 } | |
1073 | |
1074 case TYPE_FIL: | |
1075 if (elem_id == 15) | |
1076 elem_id += get_bits(&gb, 8) - 1; | |
1077 while (elem_id > 0) | |
1078 elem_id -= decode_extension_payload(ac, &gb, elem_id); | |
1079 err = 0; /* FIXME */ | |
1080 break; | |
1081 | |
1082 default: | |
1083 err = -1; /* should not happen, but keeps compiler happy */ | |
1084 break; | |
1085 } | |
1086 | |
1087 if(err) | |
1088 return err; | |
1089 } | |
1090 | |
1091 spectral_to_sample(ac); | |
791 | 1092 |
792 if (!ac->is_saved) { | 1093 if (!ac->is_saved) { |
793 ac->is_saved = 1; | 1094 ac->is_saved = 1; |
794 *data_size = 0; | 1095 *data_size = 0; |
795 return 0; | 1096 return 0; |