00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #include "avcodec.h"
00080 #include "internal.h"
00081 #include "bitstream.h"
00082 #include "dsputil.h"
00083 #include "lpc.h"
00084
00085 #include "aac.h"
00086 #include "aactab.h"
00087 #include "aacdectab.h"
00088 #include "mpeg4audio.h"
00089 #include "aac_parser.h"
00090
00091 #include <assert.h>
00092 #include <errno.h>
00093 #include <math.h>
00094 #include <string.h>
00095
00096 static VLC vlc_scalefactors;
00097 static VLC vlc_spectral[11];
00098
00099
00108 static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00109 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]) {
00110 AVCodecContext *avctx = ac->avccontext;
00111 int i, type, channels = 0;
00112
00113 if(!memcmp(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])))
00114 return 0;
00115
00116 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 for(i = 0; i < MAX_ELEM_ID; i++) {
00128 for(type = 0; type < 4; type++) {
00129 if(che_pos[type][i]) {
00130 if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement))))
00131 return AVERROR(ENOMEM);
00132 if(type != TYPE_CCE) {
00133 ac->output_data[channels++] = ac->che[type][i]->ch[0].ret;
00134 if(type == TYPE_CPE) {
00135 ac->output_data[channels++] = ac->che[type][i]->ch[1].ret;
00136 }
00137 }
00138 } else
00139 av_freep(&ac->che[type][i]);
00140 }
00141 }
00142
00143 avctx->channels = channels;
00144 return 0;
00145 }
00146
00154 static void decode_channel_map(enum ChannelPosition *cpe_map,
00155 enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) {
00156 while(n--) {
00157 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map;
00158 map[get_bits(gb, 4)] = type;
00159 }
00160 }
00161
00169 static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00170 GetBitContext * gb) {
00171 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00172
00173 skip_bits(gb, 2);
00174
00175 sampling_index = get_bits(gb, 4);
00176 if(sampling_index > 12) {
00177 av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00178 return -1;
00179 }
00180 ac->m4ac.sampling_index = sampling_index;
00181 ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index];
00182 num_front = get_bits(gb, 4);
00183 num_side = get_bits(gb, 4);
00184 num_back = get_bits(gb, 4);
00185 num_lfe = get_bits(gb, 2);
00186 num_assoc_data = get_bits(gb, 3);
00187 num_cc = get_bits(gb, 4);
00188
00189 if (get_bits1(gb))
00190 skip_bits(gb, 4);
00191 if (get_bits1(gb))
00192 skip_bits(gb, 4);
00193
00194 if (get_bits1(gb))
00195 skip_bits(gb, 3);
00196
00197 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00198 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
00199 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
00200 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
00201
00202 skip_bits_long(gb, 4 * num_assoc_data);
00203
00204 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
00205
00206 align_get_bits(gb);
00207
00208
00209 skip_bits_long(gb, 8 * get_bits(gb, 8));
00210 return 0;
00211 }
00212
00221 static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00222 int channel_config)
00223 {
00224 if(channel_config < 1 || channel_config > 7) {
00225 av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00226 channel_config);
00227 return -1;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 if(channel_config != 2)
00242 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT;
00243 if(channel_config > 1)
00244 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT;
00245 if(channel_config == 4)
00246 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;
00247 if(channel_config > 4)
00248 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00249 = AAC_CHANNEL_BACK;
00250 if(channel_config > 5)
00251 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;
00252 if(channel_config == 7)
00253 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT;
00254
00255 return 0;
00256 }
00257
00263 static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) {
00264 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00265 int extension_flag, ret;
00266
00267 if(get_bits1(gb)) {
00268 ff_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
00269 return -1;
00270 }
00271
00272 if (get_bits1(gb))
00273 skip_bits(gb, 14);
00274 extension_flag = get_bits1(gb);
00275
00276 if(ac->m4ac.object_type == AOT_AAC_SCALABLE ||
00277 ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
00278 skip_bits(gb, 3);
00279
00280 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00281 if (channel_config == 0) {
00282 skip_bits(gb, 4);
00283 if((ret = decode_pce(ac, new_che_pos, gb)))
00284 return ret;
00285 } else {
00286 if((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
00287 return ret;
00288 }
00289 if((ret = output_configure(ac, ac->che_pos, new_che_pos)))
00290 return ret;
00291
00292 if (extension_flag) {
00293 switch (ac->m4ac.object_type) {
00294 case AOT_ER_BSAC:
00295 skip_bits(gb, 5);
00296 skip_bits(gb, 11);
00297 break;
00298 case AOT_ER_AAC_LC:
00299 case AOT_ER_AAC_LTP:
00300 case AOT_ER_AAC_SCALABLE:
00301 case AOT_ER_AAC_LD:
00302 skip_bits(gb, 3);
00303
00304
00305
00306 break;
00307 }
00308 skip_bits1(gb);
00309 }
00310 return 0;
00311 }
00312
00321 static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) {
00322 GetBitContext gb;
00323 int i;
00324
00325 init_get_bits(&gb, data, data_size * 8);
00326
00327 if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
00328 return -1;
00329 if(ac->m4ac.sampling_index > 12) {
00330 av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00331 return -1;
00332 }
00333
00334 skip_bits_long(&gb, i);
00335
00336 switch (ac->m4ac.object_type) {
00337 case AOT_AAC_MAIN:
00338 case AOT_AAC_LC:
00339 if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
00340 return -1;
00341 break;
00342 default:
00343 av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00344 ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
00345 return -1;
00346 }
00347 return 0;
00348 }
00349
00357 static av_always_inline int lcg_random(int previous_val) {
00358 return previous_val * 1664525 + 1013904223;
00359 }
00360
00361 static void reset_predict_state(PredictorState * ps) {
00362 ps->r0 = 0.0f;
00363 ps->r1 = 0.0f;
00364 ps->cor0 = 0.0f;
00365 ps->cor1 = 0.0f;
00366 ps->var0 = 1.0f;
00367 ps->var1 = 1.0f;
00368 }
00369
00370 static void reset_all_predictors(PredictorState * ps) {
00371 int i;
00372 for (i = 0; i < MAX_PREDICTORS; i++)
00373 reset_predict_state(&ps[i]);
00374 }
00375
00376 static void reset_predictor_group(PredictorState * ps, int group_num) {
00377 int i;
00378 for (i = group_num-1; i < MAX_PREDICTORS; i+=30)
00379 reset_predict_state(&ps[i]);
00380 }
00381
00382 static av_cold int aac_decode_init(AVCodecContext * avccontext) {
00383 AACContext * ac = avccontext->priv_data;
00384 int i;
00385
00386 ac->avccontext = avccontext;
00387
00388 if (avccontext->extradata_size > 0) {
00389 if(decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
00390 return -1;
00391 avccontext->sample_rate = ac->m4ac.sample_rate;
00392 } else if (avccontext->channels > 0) {
00393 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00394 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00395 if(set_default_channel_config(ac, new_che_pos, avccontext->channels - (avccontext->channels == 8)))
00396 return -1;
00397 if(output_configure(ac, ac->che_pos, new_che_pos))
00398 return -1;
00399 ac->m4ac.sample_rate = avccontext->sample_rate;
00400 } else {
00401 ff_log_missing_feature(ac->avccontext, "Implicit channel configuration is", 0);
00402 return -1;
00403 }
00404
00405 avccontext->sample_fmt = SAMPLE_FMT_S16;
00406 avccontext->frame_size = 1024;
00407
00408 AAC_INIT_VLC_STATIC( 0, 144);
00409 AAC_INIT_VLC_STATIC( 1, 114);
00410 AAC_INIT_VLC_STATIC( 2, 188);
00411 AAC_INIT_VLC_STATIC( 3, 180);
00412 AAC_INIT_VLC_STATIC( 4, 172);
00413 AAC_INIT_VLC_STATIC( 5, 140);
00414 AAC_INIT_VLC_STATIC( 6, 168);
00415 AAC_INIT_VLC_STATIC( 7, 114);
00416 AAC_INIT_VLC_STATIC( 8, 262);
00417 AAC_INIT_VLC_STATIC( 9, 248);
00418 AAC_INIT_VLC_STATIC(10, 384);
00419
00420 dsputil_init(&ac->dsp, avccontext);
00421
00422 ac->random_state = 0x1f2e3d4c;
00423
00424
00425
00426
00427
00428 if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
00429 ac->add_bias = 385.0f;
00430 ac->sf_scale = 1. / (-1024. * 32768.);
00431 ac->sf_offset = 0;
00432 } else {
00433 ac->add_bias = 0.0f;
00434 ac->sf_scale = 1. / -1024.;
00435 ac->sf_offset = 60;
00436 }
00437
00438 #if !CONFIG_HARDCODED_TABLES
00439 for (i = 0; i < 428; i++)
00440 ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
00441 #endif
00442
00443 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00444 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00445 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00446 352);
00447
00448 ff_mdct_init(&ac->mdct, 11, 1);
00449 ff_mdct_init(&ac->mdct_small, 8, 1);
00450
00451 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00452 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00453 ff_sine_window_init(ff_sine_1024, 1024);
00454 ff_sine_window_init(ff_sine_128, 128);
00455
00456 return 0;
00457 }
00458
00462 static void skip_data_stream_element(GetBitContext * gb) {
00463 int byte_align = get_bits1(gb);
00464 int count = get_bits(gb, 8);
00465 if (count == 255)
00466 count += get_bits(gb, 8);
00467 if (byte_align)
00468 align_get_bits(gb);
00469 skip_bits_long(gb, 8 * count);
00470 }
00471
00472 static int decode_prediction(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb) {
00473 int sfb;
00474 if (get_bits1(gb)) {
00475 ics->predictor_reset_group = get_bits(gb, 5);
00476 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00477 av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00478 return -1;
00479 }
00480 }
00481 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00482 ics->prediction_used[sfb] = get_bits1(gb);
00483 }
00484 return 0;
00485 }
00486
00492 static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) {
00493 if (get_bits1(gb)) {
00494 av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
00495 memset(ics, 0, sizeof(IndividualChannelStream));
00496 return -1;
00497 }
00498 ics->window_sequence[1] = ics->window_sequence[0];
00499 ics->window_sequence[0] = get_bits(gb, 2);
00500 ics->use_kb_window[1] = ics->use_kb_window[0];
00501 ics->use_kb_window[0] = get_bits1(gb);
00502 ics->num_window_groups = 1;
00503 ics->group_len[0] = 1;
00504 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00505 int i;
00506 ics->max_sfb = get_bits(gb, 4);
00507 for (i = 0; i < 7; i++) {
00508 if (get_bits1(gb)) {
00509 ics->group_len[ics->num_window_groups-1]++;
00510 } else {
00511 ics->num_window_groups++;
00512 ics->group_len[ics->num_window_groups-1] = 1;
00513 }
00514 }
00515 ics->num_windows = 8;
00516 ics->swb_offset = swb_offset_128[ac->m4ac.sampling_index];
00517 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
00518 ics->tns_max_bands = tns_max_bands_128[ac->m4ac.sampling_index];
00519 ics->predictor_present = 0;
00520 } else {
00521 ics->max_sfb = get_bits(gb, 6);
00522 ics->num_windows = 1;
00523 ics->swb_offset = swb_offset_1024[ac->m4ac.sampling_index];
00524 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00525 ics->tns_max_bands = tns_max_bands_1024[ac->m4ac.sampling_index];
00526 ics->predictor_present = get_bits1(gb);
00527 ics->predictor_reset_group = 0;
00528 if (ics->predictor_present) {
00529 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00530 if (decode_prediction(ac, ics, gb)) {
00531 memset(ics, 0, sizeof(IndividualChannelStream));
00532 return -1;
00533 }
00534 } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00535 av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00536 memset(ics, 0, sizeof(IndividualChannelStream));
00537 return -1;
00538 } else {
00539 ff_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
00540 memset(ics, 0, sizeof(IndividualChannelStream));
00541 return -1;
00542 }
00543 }
00544 }
00545
00546 if(ics->max_sfb > ics->num_swb) {
00547 av_log(ac->avccontext, AV_LOG_ERROR,
00548 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00549 ics->max_sfb, ics->num_swb);
00550 memset(ics, 0, sizeof(IndividualChannelStream));
00551 return -1;
00552 }
00553
00554 return 0;
00555 }
00556
00565 static int decode_band_types(AACContext * ac, enum BandType band_type[120],
00566 int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) {
00567 int g, idx = 0;
00568 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00569 for (g = 0; g < ics->num_window_groups; g++) {
00570 int k = 0;
00571 while (k < ics->max_sfb) {
00572 uint8_t sect_len = k;
00573 int sect_len_incr;
00574 int sect_band_type = get_bits(gb, 4);
00575 if (sect_band_type == 12) {
00576 av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
00577 return -1;
00578 }
00579 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1)
00580 sect_len += sect_len_incr;
00581 sect_len += sect_len_incr;
00582 if (sect_len > ics->max_sfb) {
00583 av_log(ac->avccontext, AV_LOG_ERROR,
00584 "Number of bands (%d) exceeds limit (%d).\n",
00585 sect_len, ics->max_sfb);
00586 return -1;
00587 }
00588 for (; k < sect_len; k++) {
00589 band_type [idx] = sect_band_type;
00590 band_type_run_end[idx++] = sect_len;
00591 }
00592 }
00593 }
00594 return 0;
00595 }
00596
00607 static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb,
00608 unsigned int global_gain, IndividualChannelStream * ics,
00609 enum BandType band_type[120], int band_type_run_end[120]) {
00610 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00611 int g, i, idx = 0;
00612 int offset[3] = { global_gain, global_gain - 90, 100 };
00613 int noise_flag = 1;
00614 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00615 for (g = 0; g < ics->num_window_groups; g++) {
00616 for (i = 0; i < ics->max_sfb;) {
00617 int run_end = band_type_run_end[idx];
00618 if (band_type[idx] == ZERO_BT) {
00619 for(; i < run_end; i++, idx++)
00620 sf[idx] = 0.;
00621 }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00622 for(; i < run_end; i++, idx++) {
00623 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00624 if(offset[2] > 255U) {
00625 av_log(ac->avccontext, AV_LOG_ERROR,
00626 "%s (%d) out of range.\n", sf_str[2], offset[2]);
00627 return -1;
00628 }
00629 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
00630 }
00631 }else if(band_type[idx] == NOISE_BT) {
00632 for(; i < run_end; i++, idx++) {
00633 if(noise_flag-- > 0)
00634 offset[1] += get_bits(gb, 9) - 256;
00635 else
00636 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00637 if(offset[1] > 255U) {
00638 av_log(ac->avccontext, AV_LOG_ERROR,
00639 "%s (%d) out of range.\n", sf_str[1], offset[1]);
00640 return -1;
00641 }
00642 sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset + 100];
00643 }
00644 }else {
00645 for(; i < run_end; i++, idx++) {
00646 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00647 if(offset[0] > 255U) {
00648 av_log(ac->avccontext, AV_LOG_ERROR,
00649 "%s (%d) out of range.\n", sf_str[0], offset[0]);
00650 return -1;
00651 }
00652 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00653 }
00654 }
00655 }
00656 }
00657 return 0;
00658 }
00659
00663 static int decode_pulses(Pulse * pulse, GetBitContext * gb, const uint16_t * swb_offset, int num_swb) {
00664 int i, pulse_swb;
00665 pulse->num_pulse = get_bits(gb, 2) + 1;
00666 pulse_swb = get_bits(gb, 6);
00667 if (pulse_swb >= num_swb)
00668 return -1;
00669 pulse->pos[0] = swb_offset[pulse_swb];
00670 pulse->pos[0] += get_bits(gb, 5);
00671 if (pulse->pos[0] > 1023)
00672 return -1;
00673 pulse->amp[0] = get_bits(gb, 4);
00674 for (i = 1; i < pulse->num_pulse; i++) {
00675 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i-1];
00676 if (pulse->pos[i] > 1023)
00677 return -1;
00678 pulse->amp[i] = get_bits(gb, 4);
00679 }
00680 return 0;
00681 }
00682
00688 static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns,
00689 GetBitContext * gb, const IndividualChannelStream * ics) {
00690 int w, filt, i, coef_len, coef_res, coef_compress;
00691 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00692 const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00693 for (w = 0; w < ics->num_windows; w++) {
00694 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00695 coef_res = get_bits1(gb);
00696
00697 for (filt = 0; filt < tns->n_filt[w]; filt++) {
00698 int tmp2_idx;
00699 tns->length[w][filt] = get_bits(gb, 6 - 2*is8);
00700
00701 if ((tns->order[w][filt] = get_bits(gb, 5 - 2*is8)) > tns_max_order) {
00702 av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.",
00703 tns->order[w][filt], tns_max_order);
00704 tns->order[w][filt] = 0;
00705 return -1;
00706 }
00707 if (tns->order[w][filt]) {
00708 tns->direction[w][filt] = get_bits1(gb);
00709 coef_compress = get_bits1(gb);
00710 coef_len = coef_res + 3 - coef_compress;
00711 tmp2_idx = 2*coef_compress + coef_res;
00712
00713 for (i = 0; i < tns->order[w][filt]; i++)
00714 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00715 }
00716 }
00717 }
00718 }
00719 return 0;
00720 }
00721
00729 static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb,
00730 int ms_present) {
00731 int idx;
00732 if (ms_present == 1) {
00733 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00734 cpe->ms_mask[idx] = get_bits1(gb);
00735 } else if (ms_present == 2) {
00736 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00737 }
00738 }
00739
00752 static int decode_spectrum_and_dequant(AACContext * ac, float coef[1024], GetBitContext * gb, float sf[120],
00753 int pulse_present, const Pulse * pulse, const IndividualChannelStream * ics, enum BandType band_type[120]) {
00754 int i, k, g, idx = 0;
00755 const int c = 1024/ics->num_windows;
00756 const uint16_t * offsets = ics->swb_offset;
00757 float *coef_base = coef;
00758 static const float sign_lookup[] = { 1.0f, -1.0f };
00759
00760 for (g = 0; g < ics->num_windows; g++)
00761 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float)*(c - offsets[ics->max_sfb]));
00762
00763 for (g = 0; g < ics->num_window_groups; g++) {
00764 for (i = 0; i < ics->max_sfb; i++, idx++) {
00765 const int cur_band_type = band_type[idx];
00766 const int dim = cur_band_type >= FIRST_PAIR_BT ? 2 : 4;
00767 const int is_cb_unsigned = IS_CODEBOOK_UNSIGNED(cur_band_type);
00768 int group;
00769 if (cur_band_type == ZERO_BT || cur_band_type == INTENSITY_BT2 || cur_band_type == INTENSITY_BT) {
00770 for (group = 0; group < ics->group_len[g]; group++) {
00771 memset(coef + group * 128 + offsets[i], 0, (offsets[i+1] - offsets[i])*sizeof(float));
00772 }
00773 }else if (cur_band_type == NOISE_BT) {
00774 for (group = 0; group < ics->group_len[g]; group++) {
00775 float scale;
00776 float band_energy = 0;
00777 for (k = offsets[i]; k < offsets[i+1]; k++) {
00778 ac->random_state = lcg_random(ac->random_state);
00779 coef[group*128+k] = ac->random_state;
00780 band_energy += coef[group*128+k]*coef[group*128+k];
00781 }
00782 scale = sf[idx] / sqrtf(band_energy);
00783 for (k = offsets[i]; k < offsets[i+1]; k++) {
00784 coef[group*128+k] *= scale;
00785 }
00786 }
00787 }else {
00788 for (group = 0; group < ics->group_len[g]; group++) {
00789 for (k = offsets[i]; k < offsets[i+1]; k += dim) {
00790 const int index = get_vlc2(gb, vlc_spectral[cur_band_type - 1].table, 6, 3);
00791 const int coef_tmp_idx = (group << 7) + k;
00792 const float *vq_ptr;
00793 int j;
00794 if(index >= ff_aac_spectral_sizes[cur_band_type - 1]) {
00795 av_log(ac->avccontext, AV_LOG_ERROR,
00796 "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
00797 cur_band_type - 1, index, ff_aac_spectral_sizes[cur_band_type - 1]);
00798 return -1;
00799 }
00800 vq_ptr = &ff_aac_codebook_vectors[cur_band_type - 1][index * dim];
00801 if (is_cb_unsigned) {
00802 if (vq_ptr[0]) coef[coef_tmp_idx ] = sign_lookup[get_bits1(gb)];
00803 if (vq_ptr[1]) coef[coef_tmp_idx + 1] = sign_lookup[get_bits1(gb)];
00804 if (dim == 4) {
00805 if (vq_ptr[2]) coef[coef_tmp_idx + 2] = sign_lookup[get_bits1(gb)];
00806 if (vq_ptr[3]) coef[coef_tmp_idx + 3] = sign_lookup[get_bits1(gb)];
00807 }
00808 if (cur_band_type == ESC_BT) {
00809 for (j = 0; j < 2; j++) {
00810 if (vq_ptr[j] == 64.0f) {
00811 int n = 4;
00812
00813
00814 while (get_bits1(gb) && n < 15) n++;
00815 if(n == 15) {
00816 av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
00817 return -1;
00818 }
00819 n = (1<<n) + get_bits(gb, n);
00820 coef[coef_tmp_idx + j] *= cbrtf(n) * n;
00821 }else
00822 coef[coef_tmp_idx + j] *= vq_ptr[j];
00823 }
00824 }else
00825 {
00826 coef[coef_tmp_idx ] *= vq_ptr[0];
00827 coef[coef_tmp_idx + 1] *= vq_ptr[1];
00828 if (dim == 4) {
00829 coef[coef_tmp_idx + 2] *= vq_ptr[2];
00830 coef[coef_tmp_idx + 3] *= vq_ptr[3];
00831 }
00832 }
00833 }else {
00834 coef[coef_tmp_idx ] = vq_ptr[0];
00835 coef[coef_tmp_idx + 1] = vq_ptr[1];
00836 if (dim == 4) {
00837 coef[coef_tmp_idx + 2] = vq_ptr[2];
00838 coef[coef_tmp_idx + 3] = vq_ptr[3];
00839 }
00840 }
00841 coef[coef_tmp_idx ] *= sf[idx];
00842 coef[coef_tmp_idx + 1] *= sf[idx];
00843 if (dim == 4) {
00844 coef[coef_tmp_idx + 2] *= sf[idx];
00845 coef[coef_tmp_idx + 3] *= sf[idx];
00846 }
00847 }
00848 }
00849 }
00850 }
00851 coef += ics->group_len[g]<<7;
00852 }
00853
00854 if (pulse_present) {
00855 idx = 0;
00856 for(i = 0; i < pulse->num_pulse; i++){
00857 float co = coef_base[ pulse->pos[i] ];
00858 while(offsets[idx + 1] <= pulse->pos[i])
00859 idx++;
00860 if (band_type[idx] != NOISE_BT && sf[idx]) {
00861 float ico = -pulse->amp[i];
00862 if (co) {
00863 co /= sf[idx];
00864 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
00865 }
00866 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
00867 }
00868 }
00869 }
00870 return 0;
00871 }
00872
00873 static av_always_inline float flt16_round(float pf) {
00874 int exp;
00875 pf = frexpf(pf, &exp);
00876 pf = ldexpf(roundf(ldexpf(pf, 8)), exp-8);
00877 return pf;
00878 }
00879
00880 static av_always_inline float flt16_even(float pf) {
00881 int exp;
00882 pf = frexpf(pf, &exp);
00883 pf = ldexpf(rintf(ldexpf(pf, 8)), exp-8);
00884 return pf;
00885 }
00886
00887 static av_always_inline float flt16_trunc(float pf) {
00888 int exp;
00889 pf = frexpf(pf, &exp);
00890 pf = ldexpf(truncf(ldexpf(pf, 8)), exp-8);
00891 return pf;
00892 }
00893
00894 static void predict(AACContext * ac, PredictorState * ps, float* coef, int output_enable) {
00895 const float a = 0.953125;
00896 const float alpha = 0.90625;
00897 float e0, e1;
00898 float pv;
00899 float k1, k2;
00900
00901 k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
00902 k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
00903
00904 pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
00905 if (output_enable)
00906 *coef += pv * ac->sf_scale;
00907
00908 e0 = *coef / ac->sf_scale;
00909 e1 = e0 - k1 * ps->r0;
00910
00911 ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
00912 ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
00913 ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
00914 ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
00915
00916 ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
00917 ps->r0 = flt16_trunc(a * e0);
00918 }
00919
00923 static void apply_prediction(AACContext * ac, SingleChannelElement * sce) {
00924 int sfb, k;
00925
00926 if (!sce->ics.predictor_initialized) {
00927 reset_all_predictors(sce->predictor_state);
00928 sce->ics.predictor_initialized = 1;
00929 }
00930
00931 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
00932 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
00933 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
00934 predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
00935 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
00936 }
00937 }
00938 if (sce->ics.predictor_reset_group)
00939 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
00940 } else
00941 reset_all_predictors(sce->predictor_state);
00942 }
00943
00952 static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) {
00953 Pulse pulse;
00954 TemporalNoiseShaping * tns = &sce->tns;
00955 IndividualChannelStream * ics = &sce->ics;
00956 float * out = sce->coeffs;
00957 int global_gain, pulse_present = 0;
00958
00959
00960
00961
00962 pulse.num_pulse = 0;
00963
00964 global_gain = get_bits(gb, 8);
00965
00966 if (!common_window && !scale_flag) {
00967 if (decode_ics_info(ac, ics, gb, 0) < 0)
00968 return -1;
00969 }
00970
00971 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
00972 return -1;
00973 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
00974 return -1;
00975
00976 pulse_present = 0;
00977 if (!scale_flag) {
00978 if ((pulse_present = get_bits1(gb))) {
00979 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00980 av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
00981 return -1;
00982 }
00983 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
00984 av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
00985 return -1;
00986 }
00987 }
00988 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
00989 return -1;
00990 if (get_bits1(gb)) {
00991 ff_log_missing_feature(ac->avccontext, "SSR", 1);
00992 return -1;
00993 }
00994 }
00995
00996 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
00997 return -1;
00998
00999 if(ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01000 apply_prediction(ac, sce);
01001
01002 return 0;
01003 }
01004
01008 static void apply_mid_side_stereo(ChannelElement * cpe) {
01009 const IndividualChannelStream * ics = &cpe->ch[0].ics;
01010 float *ch0 = cpe->ch[0].coeffs;
01011 float *ch1 = cpe->ch[1].coeffs;
01012 int g, i, k, group, idx = 0;
01013 const uint16_t * offsets = ics->swb_offset;
01014 for (g = 0; g < ics->num_window_groups; g++) {
01015 for (i = 0; i < ics->max_sfb; i++, idx++) {
01016 if (cpe->ms_mask[idx] &&
01017 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01018 for (group = 0; group < ics->group_len[g]; group++) {
01019 for (k = offsets[i]; k < offsets[i+1]; k++) {
01020 float tmp = ch0[group*128 + k] - ch1[group*128 + k];
01021 ch0[group*128 + k] += ch1[group*128 + k];
01022 ch1[group*128 + k] = tmp;
01023 }
01024 }
01025 }
01026 }
01027 ch0 += ics->group_len[g]*128;
01028 ch1 += ics->group_len[g]*128;
01029 }
01030 }
01031
01039 static void apply_intensity_stereo(ChannelElement * cpe, int ms_present) {
01040 const IndividualChannelStream * ics = &cpe->ch[1].ics;
01041 SingleChannelElement * sce1 = &cpe->ch[1];
01042 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01043 const uint16_t * offsets = ics->swb_offset;
01044 int g, group, i, k, idx = 0;
01045 int c;
01046 float scale;
01047 for (g = 0; g < ics->num_window_groups; g++) {
01048 for (i = 0; i < ics->max_sfb;) {
01049 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01050 const int bt_run_end = sce1->band_type_run_end[idx];
01051 for (; i < bt_run_end; i++, idx++) {
01052 c = -1 + 2 * (sce1->band_type[idx] - 14);
01053 if (ms_present)
01054 c *= 1 - 2 * cpe->ms_mask[idx];
01055 scale = c * sce1->sf[idx];
01056 for (group = 0; group < ics->group_len[g]; group++)
01057 for (k = offsets[i]; k < offsets[i+1]; k++)
01058 coef1[group*128 + k] = scale * coef0[group*128 + k];
01059 }
01060 } else {
01061 int bt_run_end = sce1->band_type_run_end[idx];
01062 idx += bt_run_end - i;
01063 i = bt_run_end;
01064 }
01065 }
01066 coef0 += ics->group_len[g]*128;
01067 coef1 += ics->group_len[g]*128;
01068 }
01069 }
01070
01078 static int decode_cpe(AACContext * ac, GetBitContext * gb, ChannelElement * cpe) {
01079 int i, ret, common_window, ms_present = 0;
01080
01081 common_window = get_bits1(gb);
01082 if (common_window) {
01083 if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01084 return -1;
01085 i = cpe->ch[1].ics.use_kb_window[0];
01086 cpe->ch[1].ics = cpe->ch[0].ics;
01087 cpe->ch[1].ics.use_kb_window[1] = i;
01088 ms_present = get_bits(gb, 2);
01089 if(ms_present == 3) {
01090 av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01091 return -1;
01092 } else if(ms_present)
01093 decode_mid_side_stereo(cpe, gb, ms_present);
01094 }
01095 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01096 return ret;
01097 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01098 return ret;
01099
01100 if (common_window) {
01101 if (ms_present)
01102 apply_mid_side_stereo(cpe);
01103 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01104 apply_prediction(ac, &cpe->ch[0]);
01105 apply_prediction(ac, &cpe->ch[1]);
01106 }
01107 }
01108
01109 apply_intensity_stereo(cpe, ms_present);
01110 return 0;
01111 }
01112
01120 static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) {
01121 int num_gain = 0;
01122 int c, g, sfb, ret;
01123 int sign;
01124 float scale;
01125 SingleChannelElement * sce = &che->ch[0];
01126 ChannelCoupling * coup = &che->coup;
01127
01128 coup->coupling_point = 2*get_bits1(gb);
01129 coup->num_coupled = get_bits(gb, 3);
01130 for (c = 0; c <= coup->num_coupled; c++) {
01131 num_gain++;
01132 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01133 coup->id_select[c] = get_bits(gb, 4);
01134 if (coup->type[c] == TYPE_CPE) {
01135 coup->ch_select[c] = get_bits(gb, 2);
01136 if (coup->ch_select[c] == 3)
01137 num_gain++;
01138 } else
01139 coup->ch_select[c] = 2;
01140 }
01141 coup->coupling_point += get_bits1(gb);
01142
01143 if (coup->coupling_point == 2) {
01144 av_log(ac->avccontext, AV_LOG_ERROR,
01145 "Independently switched CCE with 'invalid' domain signalled.\n");
01146 memset(coup, 0, sizeof(ChannelCoupling));
01147 return -1;
01148 }
01149
01150 sign = get_bits(gb, 1);
01151 scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
01152
01153 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01154 return ret;
01155
01156 for (c = 0; c < num_gain; c++) {
01157 int idx = 0;
01158 int cge = 1;
01159 int gain = 0;
01160 float gain_cache = 1.;
01161 if (c) {
01162 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01163 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01164 gain_cache = pow(scale, -gain);
01165 }
01166 if (coup->coupling_point == AFTER_IMDCT) {
01167 coup->gain[c][0] = gain_cache;
01168 } else {
01169 for (g = 0; g < sce->ics.num_window_groups; g++) {
01170 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01171 if (sce->band_type[idx] != ZERO_BT) {
01172 if (!cge) {
01173 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01174 if (t) {
01175 int s = 1;
01176 t = gain += t;
01177 if (sign) {
01178 s -= 2 * (t & 0x1);
01179 t >>= 1;
01180 }
01181 gain_cache = pow(scale, -t) * s;
01182 }
01183 }
01184 coup->gain[c][idx] = gain_cache;
01185 }
01186 }
01187 }
01188 }
01189 }
01190 return 0;
01191 }
01192
01201 static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) {
01202
01203 ff_log_missing_feature(ac->avccontext, "SBR", 0);
01204 skip_bits_long(gb, 8*cnt - 4);
01205 return cnt;
01206 }
01207
01213 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) {
01214 int i;
01215 int num_excl_chan = 0;
01216
01217 do {
01218 for (i = 0; i < 7; i++)
01219 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01220 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01221
01222 return num_excl_chan / 7;
01223 }
01224
01232 static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) {
01233 int n = 1;
01234 int drc_num_bands = 1;
01235 int i;
01236
01237
01238 if(get_bits1(gb)) {
01239 che_drc->pce_instance_tag = get_bits(gb, 4);
01240 skip_bits(gb, 4);
01241 n++;
01242 }
01243
01244
01245 if(get_bits1(gb)) {
01246 n += decode_drc_channel_exclusions(che_drc, gb);
01247 }
01248
01249
01250 if (get_bits1(gb)) {
01251 che_drc->band_incr = get_bits(gb, 4);
01252 che_drc->interpolation_scheme = get_bits(gb, 4);
01253 n++;
01254 drc_num_bands += che_drc->band_incr;
01255 for (i = 0; i < drc_num_bands; i++) {
01256 che_drc->band_top[i] = get_bits(gb, 8);
01257 n++;
01258 }
01259 }
01260
01261
01262 if (get_bits1(gb)) {
01263 che_drc->prog_ref_level = get_bits(gb, 7);
01264 skip_bits1(gb);
01265 n++;
01266 }
01267
01268 for (i = 0; i < drc_num_bands; i++) {
01269 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01270 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01271 n++;
01272 }
01273
01274 return n;
01275 }
01276
01284 static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) {
01285 int crc_flag = 0;
01286 int res = cnt;
01287 switch (get_bits(gb, 4)) {
01288 case EXT_SBR_DATA_CRC:
01289 crc_flag++;
01290 case EXT_SBR_DATA:
01291 res = decode_sbr_extension(ac, gb, crc_flag, cnt);
01292 break;
01293 case EXT_DYNAMIC_RANGE:
01294 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01295 break;
01296 case EXT_FILL:
01297 case EXT_FILL_DATA:
01298 case EXT_DATA_ELEMENT:
01299 default:
01300 skip_bits_long(gb, 8*cnt - 4);
01301 break;
01302 };
01303 return res;
01304 }
01305
01312 static void apply_tns(float coef[1024], TemporalNoiseShaping * tns, IndividualChannelStream * ics, int decode) {
01313 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01314 int w, filt, m, i;
01315 int bottom, top, order, start, end, size, inc;
01316 float lpc[TNS_MAX_ORDER];
01317
01318 for (w = 0; w < ics->num_windows; w++) {
01319 bottom = ics->num_swb;
01320 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01321 top = bottom;
01322 bottom = FFMAX(0, top - tns->length[w][filt]);
01323 order = tns->order[w][filt];
01324 if (order == 0)
01325 continue;
01326
01327
01328 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01329
01330 start = ics->swb_offset[FFMIN(bottom, mmm)];
01331 end = ics->swb_offset[FFMIN( top, mmm)];
01332 if ((size = end - start) <= 0)
01333 continue;
01334 if (tns->direction[w][filt]) {
01335 inc = -1; start = end - 1;
01336 } else {
01337 inc = 1;
01338 }
01339 start += w * 128;
01340
01341
01342 for (m = 0; m < size; m++, start += inc)
01343 for (i = 1; i <= FFMIN(m, order); i++)
01344 coef[start] -= coef[start - i*inc] * lpc[i-1];
01345 }
01346 }
01347 }
01348
01352 static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) {
01353 IndividualChannelStream * ics = &sce->ics;
01354 float * in = sce->coeffs;
01355 float * out = sce->ret;
01356 float * saved = sce->saved;
01357 const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01358 const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01359 const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01360 float * buf = ac->buf_mdct;
01361 float * temp = ac->temp;
01362 int i;
01363
01364
01365 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01366 if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
01367 av_log(ac->avccontext, AV_LOG_WARNING,
01368 "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
01369 "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
01370 for (i = 0; i < 1024; i += 128)
01371 ff_imdct_half(&ac->mdct_small, buf + i, in + i);
01372 } else
01373 ff_imdct_half(&ac->mdct, buf, in);
01374
01375
01376
01377
01378
01379
01380
01381 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01382 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01383 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, ac->add_bias, 512);
01384 } else {
01385 for (i = 0; i < 448; i++)
01386 out[i] = saved[i] + ac->add_bias;
01387
01388 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01389 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, ac->add_bias, 64);
01390 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, ac->add_bias, 64);
01391 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, ac->add_bias, 64);
01392 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, ac->add_bias, 64);
01393 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, ac->add_bias, 64);
01394 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
01395 } else {
01396 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, ac->add_bias, 64);
01397 for (i = 576; i < 1024; i++)
01398 out[i] = buf[i-512] + ac->add_bias;
01399 }
01400 }
01401
01402
01403 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01404 for (i = 0; i < 64; i++)
01405 saved[i] = temp[64 + i] - ac->add_bias;
01406 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
01407 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
01408 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
01409 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01410 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01411 memcpy( saved, buf + 512, 448 * sizeof(float));
01412 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01413 } else {
01414 memcpy( saved, buf + 512, 512 * sizeof(float));
01415 }
01416 }
01417
01423 static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
01424 IndividualChannelStream * ics = &cce->ch[0].ics;
01425 const uint16_t * offsets = ics->swb_offset;
01426 float * dest = target->coeffs;
01427 const float * src = cce->ch[0].coeffs;
01428 int g, i, group, k, idx = 0;
01429 if(ac->m4ac.object_type == AOT_AAC_LTP) {
01430 av_log(ac->avccontext, AV_LOG_ERROR,
01431 "Dependent coupling is not supported together with LTP\n");
01432 return;
01433 }
01434 for (g = 0; g < ics->num_window_groups; g++) {
01435 for (i = 0; i < ics->max_sfb; i++, idx++) {
01436 if (cce->ch[0].band_type[idx] != ZERO_BT) {
01437 for (group = 0; group < ics->group_len[g]; group++) {
01438 for (k = offsets[i]; k < offsets[i+1]; k++) {
01439
01440 dest[group*128+k] += cce->coup.gain[index][idx] * src[group*128+k];
01441 }
01442 }
01443 }
01444 }
01445 dest += ics->group_len[g]*128;
01446 src += ics->group_len[g]*128;
01447 }
01448 }
01449
01455 static void apply_independent_coupling(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index) {
01456 int i;
01457 const float gain = cce->coup.gain[index][0];
01458 const float bias = ac->add_bias;
01459 const float* src = cce->ch[0].ret;
01460 float* dest = target->ret;
01461
01462 for (i = 0; i < 1024; i++)
01463 dest[i] += gain * (src[i] - bias);
01464 }
01465
01472 static void apply_channel_coupling(AACContext * ac, ChannelElement * cc,
01473 enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point,
01474 void (*apply_coupling_method)(AACContext * ac, SingleChannelElement * target, ChannelElement * cce, int index))
01475 {
01476 int i, c;
01477
01478 for (i = 0; i < MAX_ELEM_ID; i++) {
01479 ChannelElement *cce = ac->che[TYPE_CCE][i];
01480 int index = 0;
01481
01482 if (cce && cce->coup.coupling_point == coupling_point) {
01483 ChannelCoupling * coup = &cce->coup;
01484
01485 for (c = 0; c <= coup->num_coupled; c++) {
01486 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01487 if (coup->ch_select[c] != 1) {
01488 apply_coupling_method(ac, &cc->ch[0], cce, index);
01489 if (coup->ch_select[c] != 0)
01490 index++;
01491 }
01492 if (coup->ch_select[c] != 2)
01493 apply_coupling_method(ac, &cc->ch[1], cce, index++);
01494 } else
01495 index += 1 + (coup->ch_select[c] == 3);
01496 }
01497 }
01498 }
01499 }
01500
01504 static void spectral_to_sample(AACContext * ac) {
01505 int i, type;
01506 for(type = 3; type >= 0; type--) {
01507 for (i = 0; i < MAX_ELEM_ID; i++) {
01508 ChannelElement *che = ac->che[type][i];
01509 if(che) {
01510 if(type <= TYPE_CPE)
01511 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01512 if(che->ch[0].tns.present)
01513 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
01514 if(che->ch[1].tns.present)
01515 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
01516 if(type <= TYPE_CPE)
01517 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
01518 if(type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT)
01519 imdct_and_windowing(ac, &che->ch[0]);
01520 if(type == TYPE_CPE)
01521 imdct_and_windowing(ac, &che->ch[1]);
01522 if(type <= TYPE_CCE)
01523 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
01524 }
01525 }
01526 }
01527 }
01528
01529 static int parse_adts_frame_header(AACContext * ac, GetBitContext * gb) {
01530
01531 int size;
01532 AACADTSHeaderInfo hdr_info;
01533
01534 size = ff_aac_parse_header(gb, &hdr_info);
01535 if (size > 0) {
01536 if (hdr_info.chan_config)
01537 ac->m4ac.chan_config = hdr_info.chan_config;
01538 ac->m4ac.sample_rate = hdr_info.sample_rate;
01539 ac->m4ac.sampling_index = hdr_info.sampling_index;
01540 ac->m4ac.object_type = hdr_info.object_type;
01541 }
01542 if (hdr_info.num_aac_frames == 1) {
01543 if (!hdr_info.crc_absent)
01544 skip_bits(gb, 16);
01545 } else {
01546 ff_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0);
01547 return -1;
01548 }
01549 return size;
01550 }
01551
01552 static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, const uint8_t * buf, int buf_size) {
01553 AACContext * ac = avccontext->priv_data;
01554 GetBitContext gb;
01555 enum RawDataBlockType elem_type;
01556 int err, elem_id, data_size_tmp;
01557
01558 init_get_bits(&gb, buf, buf_size*8);
01559
01560 if (show_bits(&gb, 12) == 0xfff) {
01561 if ((err = parse_adts_frame_header(ac, &gb)) < 0) {
01562 av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
01563 return -1;
01564 }
01565 if (ac->m4ac.sampling_index > 12) {
01566 av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
01567 return -1;
01568 }
01569 }
01570
01571
01572 while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
01573 elem_id = get_bits(&gb, 4);
01574 err = -1;
01575
01576 if(elem_type == TYPE_SCE && elem_id == 1 &&
01577 !ac->che[TYPE_SCE][elem_id] && ac->che[TYPE_LFE][0]) {
01578
01579
01580
01581 ac->che[TYPE_SCE][elem_id] = ac->che[TYPE_LFE][0];
01582 ac->che[TYPE_LFE][0] = NULL;
01583 }
01584 if(elem_type < TYPE_DSE && !ac->che[elem_type][elem_id]) {
01585 av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
01586 return -1;
01587 }
01588
01589 switch (elem_type) {
01590
01591 case TYPE_SCE:
01592 err = decode_ics(ac, &ac->che[TYPE_SCE][elem_id]->ch[0], &gb, 0, 0);
01593 break;
01594
01595 case TYPE_CPE:
01596 err = decode_cpe(ac, &gb, ac->che[TYPE_CPE][elem_id]);
01597 break;
01598
01599 case TYPE_CCE:
01600 err = decode_cce(ac, &gb, ac->che[TYPE_CCE][elem_id]);
01601 break;
01602
01603 case TYPE_LFE:
01604 err = decode_ics(ac, &ac->che[TYPE_LFE][elem_id]->ch[0], &gb, 0, 0);
01605 break;
01606
01607 case TYPE_DSE:
01608 skip_data_stream_element(&gb);
01609 err = 0;
01610 break;
01611
01612 case TYPE_PCE:
01613 {
01614 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01615 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01616 if((err = decode_pce(ac, new_che_pos, &gb)))
01617 break;
01618 err = output_configure(ac, ac->che_pos, new_che_pos);
01619 break;
01620 }
01621
01622 case TYPE_FIL:
01623 if (elem_id == 15)
01624 elem_id += get_bits(&gb, 8) - 1;
01625 while (elem_id > 0)
01626 elem_id -= decode_extension_payload(ac, &gb, elem_id);
01627 err = 0;
01628 break;
01629
01630 default:
01631 err = -1;
01632 break;
01633 }
01634
01635 if(err)
01636 return err;
01637 }
01638
01639 spectral_to_sample(ac);
01640
01641 if (!ac->is_saved) {
01642 ac->is_saved = 1;
01643 *data_size = 0;
01644 return buf_size;
01645 }
01646
01647 data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
01648 if(*data_size < data_size_tmp) {
01649 av_log(avccontext, AV_LOG_ERROR,
01650 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
01651 *data_size, data_size_tmp);
01652 return -1;
01653 }
01654 *data_size = data_size_tmp;
01655
01656 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
01657
01658 return buf_size;
01659 }
01660
01661 static av_cold int aac_decode_close(AVCodecContext * avccontext) {
01662 AACContext * ac = avccontext->priv_data;
01663 int i, type;
01664
01665 for (i = 0; i < MAX_ELEM_ID; i++) {
01666 for(type = 0; type < 4; type++)
01667 av_freep(&ac->che[type][i]);
01668 }
01669
01670 ff_mdct_end(&ac->mdct);
01671 ff_mdct_end(&ac->mdct_small);
01672 return 0 ;
01673 }
01674
01675 AVCodec aac_decoder = {
01676 "aac",
01677 CODEC_TYPE_AUDIO,
01678 CODEC_ID_AAC,
01679 sizeof(AACContext),
01680 aac_decode_init,
01681 NULL,
01682 aac_decode_close,
01683 aac_decode_frame,
01684 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
01685 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
01686 };