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 "get_bits.h"
00082 #include "dsputil.h"
00083 #include "fft.h"
00084 #include "lpc.h"
00085
00086 #include "aac.h"
00087 #include "aactab.h"
00088 #include "aacdectab.h"
00089 #include "cbrt_tablegen.h"
00090 #include "sbr.h"
00091 #include "aacsbr.h"
00092 #include "mpeg4audio.h"
00093 #include "aacadtsdec.h"
00094
00095 #include <assert.h>
00096 #include <errno.h>
00097 #include <math.h>
00098 #include <string.h>
00099
00100 #if ARCH_ARM
00101 # include "arm/aac.h"
00102 #endif
00103
00104 union float754 {
00105 float f;
00106 uint32_t i;
00107 };
00108
00109 static VLC vlc_scalefactors;
00110 static VLC vlc_spectral[11];
00111
00112 static const char overread_err[] = "Input buffer exhausted before END element found\n";
00113
00114 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00115 {
00116
00117
00118
00119
00120 int err_printed = 0;
00121 while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) {
00122 if (ac->output_configured < OC_LOCKED && !err_printed) {
00123 av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n");
00124 err_printed = 1;
00125 }
00126 elem_id++;
00127 }
00128 if (elem_id == MAX_ELEM_ID)
00129 return NULL;
00130 ac->tags_seen_this_frame[type][elem_id] = 1;
00131
00132 if (ac->tag_che_map[type][elem_id]) {
00133 return ac->tag_che_map[type][elem_id];
00134 }
00135 if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
00136 return NULL;
00137 }
00138 switch (ac->m4ac.chan_config) {
00139 case 7:
00140 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00141 ac->tags_mapped++;
00142 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00143 }
00144 case 6:
00145
00146
00147
00148 if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00149 ac->tags_mapped++;
00150 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00151 }
00152 case 5:
00153 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00154 ac->tags_mapped++;
00155 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00156 }
00157 case 4:
00158 if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00159 ac->tags_mapped++;
00160 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00161 }
00162 case 3:
00163 case 2:
00164 if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00165 ac->tags_mapped++;
00166 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00167 } else if (ac->m4ac.chan_config == 2) {
00168 return NULL;
00169 }
00170 case 1:
00171 if (!ac->tags_mapped && type == TYPE_SCE) {
00172 ac->tags_mapped++;
00173 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00174 }
00175 default:
00176 return NULL;
00177 }
00178 }
00179
00192 static av_cold int che_configure(AACContext *ac,
00193 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00194 int type, int id,
00195 int *channels)
00196 {
00197 if (che_pos[type][id]) {
00198 if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00199 return AVERROR(ENOMEM);
00200 ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
00201 if (type != TYPE_CCE) {
00202 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
00203 if (type == TYPE_CPE ||
00204 (type == TYPE_SCE && ac->m4ac.ps == 1)) {
00205 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
00206 }
00207 }
00208 } else {
00209 if (ac->che[type][id])
00210 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
00211 av_freep(&ac->che[type][id]);
00212 }
00213 return 0;
00214 }
00215
00224 static av_cold int output_configure(AACContext *ac,
00225 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00226 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00227 int channel_config, enum OCStatus oc_type)
00228 {
00229 AVCodecContext *avctx = ac->avctx;
00230 int i, type, channels = 0, ret;
00231
00232 if (new_che_pos != che_pos)
00233 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00234
00235 if (channel_config) {
00236 for (i = 0; i < tags_per_config[channel_config]; i++) {
00237 if ((ret = che_configure(ac, che_pos,
00238 aac_channel_layout_map[channel_config - 1][i][0],
00239 aac_channel_layout_map[channel_config - 1][i][1],
00240 &channels)))
00241 return ret;
00242 }
00243
00244 memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00245 ac->tags_mapped = 0;
00246
00247 avctx->channel_layout = aac_channel_layout[channel_config - 1];
00248 } else {
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 for (i = 0; i < MAX_ELEM_ID; i++) {
00259 for (type = 0; type < 4; type++) {
00260 if ((ret = che_configure(ac, che_pos, type, i, &channels)))
00261 return ret;
00262 }
00263 }
00264
00265 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00266 ac->tags_mapped = 4 * MAX_ELEM_ID;
00267
00268 avctx->channel_layout = 0;
00269 }
00270
00271 avctx->channels = channels;
00272
00273 ac->output_configured = oc_type;
00274
00275 return 0;
00276 }
00277
00285 static void decode_channel_map(enum ChannelPosition *cpe_map,
00286 enum ChannelPosition *sce_map,
00287 enum ChannelPosition type,
00288 GetBitContext *gb, int n)
00289 {
00290 while (n--) {
00291 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map;
00292 map[get_bits(gb, 4)] = type;
00293 }
00294 }
00295
00303 static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00304 GetBitContext *gb)
00305 {
00306 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00307 int comment_len;
00308
00309 skip_bits(gb, 2);
00310
00311 sampling_index = get_bits(gb, 4);
00312 if (ac->m4ac.sampling_index != sampling_index)
00313 av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
00314
00315 num_front = get_bits(gb, 4);
00316 num_side = get_bits(gb, 4);
00317 num_back = get_bits(gb, 4);
00318 num_lfe = get_bits(gb, 2);
00319 num_assoc_data = get_bits(gb, 3);
00320 num_cc = get_bits(gb, 4);
00321
00322 if (get_bits1(gb))
00323 skip_bits(gb, 4);
00324 if (get_bits1(gb))
00325 skip_bits(gb, 4);
00326
00327 if (get_bits1(gb))
00328 skip_bits(gb, 3);
00329
00330 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00331 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
00332 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
00333 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
00334
00335 skip_bits_long(gb, 4 * num_assoc_data);
00336
00337 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
00338
00339 align_get_bits(gb);
00340
00341
00342 comment_len = get_bits(gb, 8) * 8;
00343 if (get_bits_left(gb) < comment_len) {
00344 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00345 return -1;
00346 }
00347 skip_bits_long(gb, comment_len);
00348 return 0;
00349 }
00350
00359 static av_cold int set_default_channel_config(AACContext *ac,
00360 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00361 int channel_config)
00362 {
00363 if (channel_config < 1 || channel_config > 7) {
00364 av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00365 channel_config);
00366 return -1;
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 if (channel_config != 2)
00381 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT;
00382 if (channel_config > 1)
00383 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT;
00384 if (channel_config == 4)
00385 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;
00386 if (channel_config > 4)
00387 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00388 = AAC_CHANNEL_BACK;
00389 if (channel_config > 5)
00390 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;
00391 if (channel_config == 7)
00392 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT;
00393
00394 return 0;
00395 }
00396
00402 static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
00403 int channel_config)
00404 {
00405 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00406 int extension_flag, ret;
00407
00408 if (get_bits1(gb)) {
00409 av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1);
00410 return -1;
00411 }
00412
00413 if (get_bits1(gb))
00414 skip_bits(gb, 14);
00415 extension_flag = get_bits1(gb);
00416
00417 if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
00418 ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
00419 skip_bits(gb, 3);
00420
00421 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00422 if (channel_config == 0) {
00423 skip_bits(gb, 4);
00424 if ((ret = decode_pce(ac, new_che_pos, gb)))
00425 return ret;
00426 } else {
00427 if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
00428 return ret;
00429 }
00430 if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
00431 return ret;
00432
00433 if (extension_flag) {
00434 switch (ac->m4ac.object_type) {
00435 case AOT_ER_BSAC:
00436 skip_bits(gb, 5);
00437 skip_bits(gb, 11);
00438 break;
00439 case AOT_ER_AAC_LC:
00440 case AOT_ER_AAC_LTP:
00441 case AOT_ER_AAC_SCALABLE:
00442 case AOT_ER_AAC_LD:
00443 skip_bits(gb, 3);
00444
00445
00446
00447 break;
00448 }
00449 skip_bits1(gb);
00450 }
00451 return 0;
00452 }
00453
00462 static int decode_audio_specific_config(AACContext *ac, void *data,
00463 int data_size)
00464 {
00465 GetBitContext gb;
00466 int i;
00467
00468 init_get_bits(&gb, data, data_size * 8);
00469
00470 if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
00471 return -1;
00472 if (ac->m4ac.sampling_index > 12) {
00473 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
00474 return -1;
00475 }
00476 if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1)
00477 ac->m4ac.ps = 1;
00478
00479 skip_bits_long(&gb, i);
00480
00481 switch (ac->m4ac.object_type) {
00482 case AOT_AAC_MAIN:
00483 case AOT_AAC_LC:
00484 if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
00485 return -1;
00486 break;
00487 default:
00488 av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00489 ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
00490 return -1;
00491 }
00492 return 0;
00493 }
00494
00502 static av_always_inline int lcg_random(int previous_val)
00503 {
00504 return previous_val * 1664525 + 1013904223;
00505 }
00506
00507 static av_always_inline void reset_predict_state(PredictorState *ps)
00508 {
00509 ps->r0 = 0.0f;
00510 ps->r1 = 0.0f;
00511 ps->cor0 = 0.0f;
00512 ps->cor1 = 0.0f;
00513 ps->var0 = 1.0f;
00514 ps->var1 = 1.0f;
00515 }
00516
00517 static void reset_all_predictors(PredictorState *ps)
00518 {
00519 int i;
00520 for (i = 0; i < MAX_PREDICTORS; i++)
00521 reset_predict_state(&ps[i]);
00522 }
00523
00524 static void reset_predictor_group(PredictorState *ps, int group_num)
00525 {
00526 int i;
00527 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00528 reset_predict_state(&ps[i]);
00529 }
00530
00531 #define AAC_INIT_VLC_STATIC(num, size) \
00532 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00533 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00534 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00535 size);
00536
00537 static av_cold int aac_decode_init(AVCodecContext *avctx)
00538 {
00539 AACContext *ac = avctx->priv_data;
00540
00541 ac->avctx = avctx;
00542 ac->m4ac.sample_rate = avctx->sample_rate;
00543
00544 if (avctx->extradata_size > 0) {
00545 if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size))
00546 return -1;
00547 }
00548
00549 avctx->sample_fmt = SAMPLE_FMT_S16;
00550
00551 AAC_INIT_VLC_STATIC( 0, 304);
00552 AAC_INIT_VLC_STATIC( 1, 270);
00553 AAC_INIT_VLC_STATIC( 2, 550);
00554 AAC_INIT_VLC_STATIC( 3, 300);
00555 AAC_INIT_VLC_STATIC( 4, 328);
00556 AAC_INIT_VLC_STATIC( 5, 294);
00557 AAC_INIT_VLC_STATIC( 6, 306);
00558 AAC_INIT_VLC_STATIC( 7, 268);
00559 AAC_INIT_VLC_STATIC( 8, 510);
00560 AAC_INIT_VLC_STATIC( 9, 366);
00561 AAC_INIT_VLC_STATIC(10, 462);
00562
00563 ff_aac_sbr_init();
00564
00565 dsputil_init(&ac->dsp, avctx);
00566
00567 ac->random_state = 0x1f2e3d4c;
00568
00569
00570
00571
00572
00573 if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
00574 ac->add_bias = 385.0f;
00575 ac->sf_scale = 1. / (-1024. * 32768.);
00576 ac->sf_offset = 0;
00577 } else {
00578 ac->add_bias = 0.0f;
00579 ac->sf_scale = 1. / -1024.;
00580 ac->sf_offset = 60;
00581 }
00582
00583 ff_aac_tableinit();
00584
00585 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00586 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00587 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00588 352);
00589
00590 ff_mdct_init(&ac->mdct, 11, 1, 1.0);
00591 ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
00592
00593 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00594 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00595 ff_init_ff_sine_windows(10);
00596 ff_init_ff_sine_windows( 7);
00597
00598 cbrt_tableinit();
00599
00600 return 0;
00601 }
00602
00606 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00607 {
00608 int byte_align = get_bits1(gb);
00609 int count = get_bits(gb, 8);
00610 if (count == 255)
00611 count += get_bits(gb, 8);
00612 if (byte_align)
00613 align_get_bits(gb);
00614
00615 if (get_bits_left(gb) < 8 * count) {
00616 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00617 return -1;
00618 }
00619 skip_bits_long(gb, 8 * count);
00620 return 0;
00621 }
00622
00623 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00624 GetBitContext *gb)
00625 {
00626 int sfb;
00627 if (get_bits1(gb)) {
00628 ics->predictor_reset_group = get_bits(gb, 5);
00629 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00630 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00631 return -1;
00632 }
00633 }
00634 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00635 ics->prediction_used[sfb] = get_bits1(gb);
00636 }
00637 return 0;
00638 }
00639
00645 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
00646 GetBitContext *gb, int common_window)
00647 {
00648 if (get_bits1(gb)) {
00649 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
00650 memset(ics, 0, sizeof(IndividualChannelStream));
00651 return -1;
00652 }
00653 ics->window_sequence[1] = ics->window_sequence[0];
00654 ics->window_sequence[0] = get_bits(gb, 2);
00655 ics->use_kb_window[1] = ics->use_kb_window[0];
00656 ics->use_kb_window[0] = get_bits1(gb);
00657 ics->num_window_groups = 1;
00658 ics->group_len[0] = 1;
00659 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00660 int i;
00661 ics->max_sfb = get_bits(gb, 4);
00662 for (i = 0; i < 7; i++) {
00663 if (get_bits1(gb)) {
00664 ics->group_len[ics->num_window_groups - 1]++;
00665 } else {
00666 ics->num_window_groups++;
00667 ics->group_len[ics->num_window_groups - 1] = 1;
00668 }
00669 }
00670 ics->num_windows = 8;
00671 ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
00672 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
00673 ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
00674 ics->predictor_present = 0;
00675 } else {
00676 ics->max_sfb = get_bits(gb, 6);
00677 ics->num_windows = 1;
00678 ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
00679 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00680 ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
00681 ics->predictor_present = get_bits1(gb);
00682 ics->predictor_reset_group = 0;
00683 if (ics->predictor_present) {
00684 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00685 if (decode_prediction(ac, ics, gb)) {
00686 memset(ics, 0, sizeof(IndividualChannelStream));
00687 return -1;
00688 }
00689 } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00690 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00691 memset(ics, 0, sizeof(IndividualChannelStream));
00692 return -1;
00693 } else {
00694 av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
00695 memset(ics, 0, sizeof(IndividualChannelStream));
00696 return -1;
00697 }
00698 }
00699 }
00700
00701 if (ics->max_sfb > ics->num_swb) {
00702 av_log(ac->avctx, AV_LOG_ERROR,
00703 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00704 ics->max_sfb, ics->num_swb);
00705 memset(ics, 0, sizeof(IndividualChannelStream));
00706 return -1;
00707 }
00708
00709 return 0;
00710 }
00711
00720 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
00721 int band_type_run_end[120], GetBitContext *gb,
00722 IndividualChannelStream *ics)
00723 {
00724 int g, idx = 0;
00725 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00726 for (g = 0; g < ics->num_window_groups; g++) {
00727 int k = 0;
00728 while (k < ics->max_sfb) {
00729 uint8_t sect_end = k;
00730 int sect_len_incr;
00731 int sect_band_type = get_bits(gb, 4);
00732 if (sect_band_type == 12) {
00733 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
00734 return -1;
00735 }
00736 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
00737 sect_end += sect_len_incr;
00738 sect_end += sect_len_incr;
00739 if (get_bits_left(gb) < 0) {
00740 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00741 return -1;
00742 }
00743 if (sect_end > ics->max_sfb) {
00744 av_log(ac->avctx, AV_LOG_ERROR,
00745 "Number of bands (%d) exceeds limit (%d).\n",
00746 sect_end, ics->max_sfb);
00747 return -1;
00748 }
00749 for (; k < sect_end; k++) {
00750 band_type [idx] = sect_band_type;
00751 band_type_run_end[idx++] = sect_end;
00752 }
00753 }
00754 }
00755 return 0;
00756 }
00757
00768 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
00769 unsigned int global_gain,
00770 IndividualChannelStream *ics,
00771 enum BandType band_type[120],
00772 int band_type_run_end[120])
00773 {
00774 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00775 int g, i, idx = 0;
00776 int offset[3] = { global_gain, global_gain - 90, 100 };
00777 int noise_flag = 1;
00778 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00779 for (g = 0; g < ics->num_window_groups; g++) {
00780 for (i = 0; i < ics->max_sfb;) {
00781 int run_end = band_type_run_end[idx];
00782 if (band_type[idx] == ZERO_BT) {
00783 for (; i < run_end; i++, idx++)
00784 sf[idx] = 0.;
00785 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00786 for (; i < run_end; i++, idx++) {
00787 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00788 if (offset[2] > 255U) {
00789 av_log(ac->avctx, AV_LOG_ERROR,
00790 "%s (%d) out of range.\n", sf_str[2], offset[2]);
00791 return -1;
00792 }
00793 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
00794 }
00795 } else if (band_type[idx] == NOISE_BT) {
00796 for (; i < run_end; i++, idx++) {
00797 if (noise_flag-- > 0)
00798 offset[1] += get_bits(gb, 9) - 256;
00799 else
00800 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00801 if (offset[1] > 255U) {
00802 av_log(ac->avctx, AV_LOG_ERROR,
00803 "%s (%d) out of range.\n", sf_str[1], offset[1]);
00804 return -1;
00805 }
00806 sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
00807 }
00808 } else {
00809 for (; i < run_end; i++, idx++) {
00810 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00811 if (offset[0] > 255U) {
00812 av_log(ac->avctx, AV_LOG_ERROR,
00813 "%s (%d) out of range.\n", sf_str[0], offset[0]);
00814 return -1;
00815 }
00816 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00817 }
00818 }
00819 }
00820 }
00821 return 0;
00822 }
00823
00827 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
00828 const uint16_t *swb_offset, int num_swb)
00829 {
00830 int i, pulse_swb;
00831 pulse->num_pulse = get_bits(gb, 2) + 1;
00832 pulse_swb = get_bits(gb, 6);
00833 if (pulse_swb >= num_swb)
00834 return -1;
00835 pulse->pos[0] = swb_offset[pulse_swb];
00836 pulse->pos[0] += get_bits(gb, 5);
00837 if (pulse->pos[0] > 1023)
00838 return -1;
00839 pulse->amp[0] = get_bits(gb, 4);
00840 for (i = 1; i < pulse->num_pulse; i++) {
00841 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
00842 if (pulse->pos[i] > 1023)
00843 return -1;
00844 pulse->amp[i] = get_bits(gb, 4);
00845 }
00846 return 0;
00847 }
00848
00854 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
00855 GetBitContext *gb, const IndividualChannelStream *ics)
00856 {
00857 int w, filt, i, coef_len, coef_res, coef_compress;
00858 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00859 const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00860 for (w = 0; w < ics->num_windows; w++) {
00861 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00862 coef_res = get_bits1(gb);
00863
00864 for (filt = 0; filt < tns->n_filt[w]; filt++) {
00865 int tmp2_idx;
00866 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
00867
00868 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
00869 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
00870 tns->order[w][filt], tns_max_order);
00871 tns->order[w][filt] = 0;
00872 return -1;
00873 }
00874 if (tns->order[w][filt]) {
00875 tns->direction[w][filt] = get_bits1(gb);
00876 coef_compress = get_bits1(gb);
00877 coef_len = coef_res + 3 - coef_compress;
00878 tmp2_idx = 2 * coef_compress + coef_res;
00879
00880 for (i = 0; i < tns->order[w][filt]; i++)
00881 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00882 }
00883 }
00884 }
00885 }
00886 return 0;
00887 }
00888
00896 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
00897 int ms_present)
00898 {
00899 int idx;
00900 if (ms_present == 1) {
00901 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00902 cpe->ms_mask[idx] = get_bits1(gb);
00903 } else if (ms_present == 2) {
00904 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00905 }
00906 }
00907
00908 #ifndef VMUL2
00909 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
00910 const float *scale)
00911 {
00912 float s = *scale;
00913 *dst++ = v[idx & 15] * s;
00914 *dst++ = v[idx>>4 & 15] * s;
00915 return dst;
00916 }
00917 #endif
00918
00919 #ifndef VMUL4
00920 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
00921 const float *scale)
00922 {
00923 float s = *scale;
00924 *dst++ = v[idx & 3] * s;
00925 *dst++ = v[idx>>2 & 3] * s;
00926 *dst++ = v[idx>>4 & 3] * s;
00927 *dst++ = v[idx>>6 & 3] * s;
00928 return dst;
00929 }
00930 #endif
00931
00932 #ifndef VMUL2S
00933 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
00934 unsigned sign, const float *scale)
00935 {
00936 union float754 s0, s1;
00937
00938 s0.f = s1.f = *scale;
00939 s0.i ^= sign >> 1 << 31;
00940 s1.i ^= sign << 31;
00941
00942 *dst++ = v[idx & 15] * s0.f;
00943 *dst++ = v[idx>>4 & 15] * s1.f;
00944
00945 return dst;
00946 }
00947 #endif
00948
00949 #ifndef VMUL4S
00950 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
00951 unsigned sign, const float *scale)
00952 {
00953 unsigned nz = idx >> 12;
00954 union float754 s = { .f = *scale };
00955 union float754 t;
00956
00957 t.i = s.i ^ (sign & 1<<31);
00958 *dst++ = v[idx & 3] * t.f;
00959
00960 sign <<= nz & 1; nz >>= 1;
00961 t.i = s.i ^ (sign & 1<<31);
00962 *dst++ = v[idx>>2 & 3] * t.f;
00963
00964 sign <<= nz & 1; nz >>= 1;
00965 t.i = s.i ^ (sign & 1<<31);
00966 *dst++ = v[idx>>4 & 3] * t.f;
00967
00968 sign <<= nz & 1; nz >>= 1;
00969 t.i = s.i ^ (sign & 1<<31);
00970 *dst++ = v[idx>>6 & 3] * t.f;
00971
00972 return dst;
00973 }
00974 #endif
00975
00988 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
00989 GetBitContext *gb, const float sf[120],
00990 int pulse_present, const Pulse *pulse,
00991 const IndividualChannelStream *ics,
00992 enum BandType band_type[120])
00993 {
00994 int i, k, g, idx = 0;
00995 const int c = 1024 / ics->num_windows;
00996 const uint16_t *offsets = ics->swb_offset;
00997 float *coef_base = coef;
00998
00999 for (g = 0; g < ics->num_windows; g++)
01000 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01001
01002 for (g = 0; g < ics->num_window_groups; g++) {
01003 unsigned g_len = ics->group_len[g];
01004
01005 for (i = 0; i < ics->max_sfb; i++, idx++) {
01006 const unsigned cbt_m1 = band_type[idx] - 1;
01007 float *cfo = coef + offsets[i];
01008 int off_len = offsets[i + 1] - offsets[i];
01009 int group;
01010
01011 if (cbt_m1 >= INTENSITY_BT2 - 1) {
01012 for (group = 0; group < g_len; group++, cfo+=128) {
01013 memset(cfo, 0, off_len * sizeof(float));
01014 }
01015 } else if (cbt_m1 == NOISE_BT - 1) {
01016 for (group = 0; group < g_len; group++, cfo+=128) {
01017 float scale;
01018 float band_energy;
01019
01020 for (k = 0; k < off_len; k++) {
01021 ac->random_state = lcg_random(ac->random_state);
01022 cfo[k] = ac->random_state;
01023 }
01024
01025 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01026 scale = sf[idx] / sqrtf(band_energy);
01027 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01028 }
01029 } else {
01030 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01031 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01032 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01033 OPEN_READER(re, gb);
01034
01035 switch (cbt_m1 >> 1) {
01036 case 0:
01037 for (group = 0; group < g_len; group++, cfo+=128) {
01038 float *cf = cfo;
01039 int len = off_len;
01040
01041 do {
01042 int code;
01043 unsigned cb_idx;
01044
01045 UPDATE_CACHE(re, gb);
01046 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01047 cb_idx = cb_vector_idx[code];
01048 cf = VMUL4(cf, vq, cb_idx, sf + idx);
01049 } while (len -= 4);
01050 }
01051 break;
01052
01053 case 1:
01054 for (group = 0; group < g_len; group++, cfo+=128) {
01055 float *cf = cfo;
01056 int len = off_len;
01057
01058 do {
01059 int code;
01060 unsigned nnz;
01061 unsigned cb_idx;
01062 uint32_t bits;
01063
01064 UPDATE_CACHE(re, gb);
01065 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01066 #if MIN_CACHE_BITS < 20
01067 UPDATE_CACHE(re, gb);
01068 #endif
01069 cb_idx = cb_vector_idx[code];
01070 nnz = cb_idx >> 8 & 15;
01071 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01072 LAST_SKIP_BITS(re, gb, nnz);
01073 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01074 } while (len -= 4);
01075 }
01076 break;
01077
01078 case 2:
01079 for (group = 0; group < g_len; group++, cfo+=128) {
01080 float *cf = cfo;
01081 int len = off_len;
01082
01083 do {
01084 int code;
01085 unsigned cb_idx;
01086
01087 UPDATE_CACHE(re, gb);
01088 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01089 cb_idx = cb_vector_idx[code];
01090 cf = VMUL2(cf, vq, cb_idx, sf + idx);
01091 } while (len -= 2);
01092 }
01093 break;
01094
01095 case 3:
01096 case 4:
01097 for (group = 0; group < g_len; group++, cfo+=128) {
01098 float *cf = cfo;
01099 int len = off_len;
01100
01101 do {
01102 int code;
01103 unsigned nnz;
01104 unsigned cb_idx;
01105 unsigned sign;
01106
01107 UPDATE_CACHE(re, gb);
01108 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01109 cb_idx = cb_vector_idx[code];
01110 nnz = cb_idx >> 8 & 15;
01111 sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
01112 LAST_SKIP_BITS(re, gb, nnz);
01113 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01114 } while (len -= 2);
01115 }
01116 break;
01117
01118 default:
01119 for (group = 0; group < g_len; group++, cfo+=128) {
01120 float *cf = cfo;
01121 uint32_t *icf = (uint32_t *) cf;
01122 int len = off_len;
01123
01124 do {
01125 int code;
01126 unsigned nzt, nnz;
01127 unsigned cb_idx;
01128 uint32_t bits;
01129 int j;
01130
01131 UPDATE_CACHE(re, gb);
01132 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01133
01134 if (!code) {
01135 *icf++ = 0;
01136 *icf++ = 0;
01137 continue;
01138 }
01139
01140 cb_idx = cb_vector_idx[code];
01141 nnz = cb_idx >> 12;
01142 nzt = cb_idx >> 8;
01143 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01144 LAST_SKIP_BITS(re, gb, nnz);
01145
01146 for (j = 0; j < 2; j++) {
01147 if (nzt & 1<<j) {
01148 uint32_t b;
01149 int n;
01150
01151
01152 UPDATE_CACHE(re, gb);
01153 b = GET_CACHE(re, gb);
01154 b = 31 - av_log2(~b);
01155
01156 if (b > 8) {
01157 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01158 return -1;
01159 }
01160
01161 #if MIN_CACHE_BITS < 21
01162 LAST_SKIP_BITS(re, gb, b + 1);
01163 UPDATE_CACHE(re, gb);
01164 #else
01165 SKIP_BITS(re, gb, b + 1);
01166 #endif
01167 b += 4;
01168 n = (1 << b) + SHOW_UBITS(re, gb, b);
01169 LAST_SKIP_BITS(re, gb, b);
01170 *icf++ = cbrt_tab[n] | (bits & 1<<31);
01171 bits <<= 1;
01172 } else {
01173 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01174 *icf++ = (bits & 1<<31) | v;
01175 bits <<= !!v;
01176 }
01177 cb_idx >>= 4;
01178 }
01179 } while (len -= 2);
01180
01181 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01182 }
01183 }
01184
01185 CLOSE_READER(re, gb);
01186 }
01187 }
01188 coef += g_len << 7;
01189 }
01190
01191 if (pulse_present) {
01192 idx = 0;
01193 for (i = 0; i < pulse->num_pulse; i++) {
01194 float co = coef_base[ pulse->pos[i] ];
01195 while (offsets[idx + 1] <= pulse->pos[i])
01196 idx++;
01197 if (band_type[idx] != NOISE_BT && sf[idx]) {
01198 float ico = -pulse->amp[i];
01199 if (co) {
01200 co /= sf[idx];
01201 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01202 }
01203 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01204 }
01205 }
01206 }
01207 return 0;
01208 }
01209
01210 static av_always_inline float flt16_round(float pf)
01211 {
01212 union float754 tmp;
01213 tmp.f = pf;
01214 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01215 return tmp.f;
01216 }
01217
01218 static av_always_inline float flt16_even(float pf)
01219 {
01220 union float754 tmp;
01221 tmp.f = pf;
01222 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01223 return tmp.f;
01224 }
01225
01226 static av_always_inline float flt16_trunc(float pf)
01227 {
01228 union float754 pun;
01229 pun.f = pf;
01230 pun.i &= 0xFFFF0000U;
01231 return pun.f;
01232 }
01233
01234 static av_always_inline void predict(PredictorState *ps, float *coef,
01235 float sf_scale, float inv_sf_scale,
01236 int output_enable)
01237 {
01238 const float a = 0.953125;
01239 const float alpha = 0.90625;
01240 float e0, e1;
01241 float pv;
01242 float k1, k2;
01243 float r0 = ps->r0, r1 = ps->r1;
01244 float cor0 = ps->cor0, cor1 = ps->cor1;
01245 float var0 = ps->var0, var1 = ps->var1;
01246
01247 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
01248 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
01249
01250 pv = flt16_round(k1 * r0 + k2 * r1);
01251 if (output_enable)
01252 *coef += pv * sf_scale;
01253
01254 e0 = *coef * inv_sf_scale;
01255 e1 = e0 - k1 * r0;
01256
01257 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
01258 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
01259 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
01260 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
01261
01262 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
01263 ps->r0 = flt16_trunc(a * e0);
01264 }
01265
01269 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01270 {
01271 int sfb, k;
01272 float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
01273
01274 if (!sce->ics.predictor_initialized) {
01275 reset_all_predictors(sce->predictor_state);
01276 sce->ics.predictor_initialized = 1;
01277 }
01278
01279 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01280 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
01281 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01282 predict(&sce->predictor_state[k], &sce->coeffs[k],
01283 sf_scale, inv_sf_scale,
01284 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01285 }
01286 }
01287 if (sce->ics.predictor_reset_group)
01288 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01289 } else
01290 reset_all_predictors(sce->predictor_state);
01291 }
01292
01301 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01302 GetBitContext *gb, int common_window, int scale_flag)
01303 {
01304 Pulse pulse;
01305 TemporalNoiseShaping *tns = &sce->tns;
01306 IndividualChannelStream *ics = &sce->ics;
01307 float *out = sce->coeffs;
01308 int global_gain, pulse_present = 0;
01309
01310
01311
01312
01313 pulse.num_pulse = 0;
01314
01315 global_gain = get_bits(gb, 8);
01316
01317 if (!common_window && !scale_flag) {
01318 if (decode_ics_info(ac, ics, gb, 0) < 0)
01319 return -1;
01320 }
01321
01322 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01323 return -1;
01324 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01325 return -1;
01326
01327 pulse_present = 0;
01328 if (!scale_flag) {
01329 if ((pulse_present = get_bits1(gb))) {
01330 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01331 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01332 return -1;
01333 }
01334 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01335 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01336 return -1;
01337 }
01338 }
01339 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01340 return -1;
01341 if (get_bits1(gb)) {
01342 av_log_missing_feature(ac->avctx, "SSR", 1);
01343 return -1;
01344 }
01345 }
01346
01347 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01348 return -1;
01349
01350 if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01351 apply_prediction(ac, sce);
01352
01353 return 0;
01354 }
01355
01359 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01360 {
01361 const IndividualChannelStream *ics = &cpe->ch[0].ics;
01362 float *ch0 = cpe->ch[0].coeffs;
01363 float *ch1 = cpe->ch[1].coeffs;
01364 int g, i, group, idx = 0;
01365 const uint16_t *offsets = ics->swb_offset;
01366 for (g = 0; g < ics->num_window_groups; g++) {
01367 for (i = 0; i < ics->max_sfb; i++, idx++) {
01368 if (cpe->ms_mask[idx] &&
01369 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01370 for (group = 0; group < ics->group_len[g]; group++) {
01371 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01372 ch1 + group * 128 + offsets[i],
01373 offsets[i+1] - offsets[i]);
01374 }
01375 }
01376 }
01377 ch0 += ics->group_len[g] * 128;
01378 ch1 += ics->group_len[g] * 128;
01379 }
01380 }
01381
01389 static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
01390 {
01391 const IndividualChannelStream *ics = &cpe->ch[1].ics;
01392 SingleChannelElement *sce1 = &cpe->ch[1];
01393 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01394 const uint16_t *offsets = ics->swb_offset;
01395 int g, group, i, k, idx = 0;
01396 int c;
01397 float scale;
01398 for (g = 0; g < ics->num_window_groups; g++) {
01399 for (i = 0; i < ics->max_sfb;) {
01400 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01401 const int bt_run_end = sce1->band_type_run_end[idx];
01402 for (; i < bt_run_end; i++, idx++) {
01403 c = -1 + 2 * (sce1->band_type[idx] - 14);
01404 if (ms_present)
01405 c *= 1 - 2 * cpe->ms_mask[idx];
01406 scale = c * sce1->sf[idx];
01407 for (group = 0; group < ics->group_len[g]; group++)
01408 for (k = offsets[i]; k < offsets[i + 1]; k++)
01409 coef1[group * 128 + k] = scale * coef0[group * 128 + k];
01410 }
01411 } else {
01412 int bt_run_end = sce1->band_type_run_end[idx];
01413 idx += bt_run_end - i;
01414 i = bt_run_end;
01415 }
01416 }
01417 coef0 += ics->group_len[g] * 128;
01418 coef1 += ics->group_len[g] * 128;
01419 }
01420 }
01421
01427 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01428 {
01429 int i, ret, common_window, ms_present = 0;
01430
01431 common_window = get_bits1(gb);
01432 if (common_window) {
01433 if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01434 return -1;
01435 i = cpe->ch[1].ics.use_kb_window[0];
01436 cpe->ch[1].ics = cpe->ch[0].ics;
01437 cpe->ch[1].ics.use_kb_window[1] = i;
01438 ms_present = get_bits(gb, 2);
01439 if (ms_present == 3) {
01440 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01441 return -1;
01442 } else if (ms_present)
01443 decode_mid_side_stereo(cpe, gb, ms_present);
01444 }
01445 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01446 return ret;
01447 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01448 return ret;
01449
01450 if (common_window) {
01451 if (ms_present)
01452 apply_mid_side_stereo(ac, cpe);
01453 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01454 apply_prediction(ac, &cpe->ch[0]);
01455 apply_prediction(ac, &cpe->ch[1]);
01456 }
01457 }
01458
01459 apply_intensity_stereo(cpe, ms_present);
01460 return 0;
01461 }
01462
01463 static const float cce_scale[] = {
01464 1.09050773266525765921,
01465 1.18920711500272106672,
01466 M_SQRT2,
01467 2,
01468 };
01469
01475 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01476 {
01477 int num_gain = 0;
01478 int c, g, sfb, ret;
01479 int sign;
01480 float scale;
01481 SingleChannelElement *sce = &che->ch[0];
01482 ChannelCoupling *coup = &che->coup;
01483
01484 coup->coupling_point = 2 * get_bits1(gb);
01485 coup->num_coupled = get_bits(gb, 3);
01486 for (c = 0; c <= coup->num_coupled; c++) {
01487 num_gain++;
01488 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01489 coup->id_select[c] = get_bits(gb, 4);
01490 if (coup->type[c] == TYPE_CPE) {
01491 coup->ch_select[c] = get_bits(gb, 2);
01492 if (coup->ch_select[c] == 3)
01493 num_gain++;
01494 } else
01495 coup->ch_select[c] = 2;
01496 }
01497 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01498
01499 sign = get_bits(gb, 1);
01500 scale = cce_scale[get_bits(gb, 2)];
01501
01502 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01503 return ret;
01504
01505 for (c = 0; c < num_gain; c++) {
01506 int idx = 0;
01507 int cge = 1;
01508 int gain = 0;
01509 float gain_cache = 1.;
01510 if (c) {
01511 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01512 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01513 gain_cache = powf(scale, -gain);
01514 }
01515 if (coup->coupling_point == AFTER_IMDCT) {
01516 coup->gain[c][0] = gain_cache;
01517 } else {
01518 for (g = 0; g < sce->ics.num_window_groups; g++) {
01519 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01520 if (sce->band_type[idx] != ZERO_BT) {
01521 if (!cge) {
01522 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01523 if (t) {
01524 int s = 1;
01525 t = gain += t;
01526 if (sign) {
01527 s -= 2 * (t & 0x1);
01528 t >>= 1;
01529 }
01530 gain_cache = powf(scale, -t) * s;
01531 }
01532 }
01533 coup->gain[c][idx] = gain_cache;
01534 }
01535 }
01536 }
01537 }
01538 }
01539 return 0;
01540 }
01541
01547 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01548 GetBitContext *gb)
01549 {
01550 int i;
01551 int num_excl_chan = 0;
01552
01553 do {
01554 for (i = 0; i < 7; i++)
01555 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01556 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01557
01558 return num_excl_chan / 7;
01559 }
01560
01568 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01569 GetBitContext *gb, int cnt)
01570 {
01571 int n = 1;
01572 int drc_num_bands = 1;
01573 int i;
01574
01575
01576 if (get_bits1(gb)) {
01577 che_drc->pce_instance_tag = get_bits(gb, 4);
01578 skip_bits(gb, 4);
01579 n++;
01580 }
01581
01582
01583 if (get_bits1(gb)) {
01584 n += decode_drc_channel_exclusions(che_drc, gb);
01585 }
01586
01587
01588 if (get_bits1(gb)) {
01589 che_drc->band_incr = get_bits(gb, 4);
01590 che_drc->interpolation_scheme = get_bits(gb, 4);
01591 n++;
01592 drc_num_bands += che_drc->band_incr;
01593 for (i = 0; i < drc_num_bands; i++) {
01594 che_drc->band_top[i] = get_bits(gb, 8);
01595 n++;
01596 }
01597 }
01598
01599
01600 if (get_bits1(gb)) {
01601 che_drc->prog_ref_level = get_bits(gb, 7);
01602 skip_bits1(gb);
01603 n++;
01604 }
01605
01606 for (i = 0; i < drc_num_bands; i++) {
01607 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01608 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01609 n++;
01610 }
01611
01612 return n;
01613 }
01614
01622 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
01623 ChannelElement *che, enum RawDataBlockType elem_type)
01624 {
01625 int crc_flag = 0;
01626 int res = cnt;
01627 switch (get_bits(gb, 4)) {
01628 case EXT_SBR_DATA_CRC:
01629 crc_flag++;
01630 case EXT_SBR_DATA:
01631 if (!che) {
01632 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
01633 return res;
01634 } else if (!ac->m4ac.sbr) {
01635 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
01636 skip_bits_long(gb, 8 * cnt - 4);
01637 return res;
01638 } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
01639 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
01640 skip_bits_long(gb, 8 * cnt - 4);
01641 return res;
01642 } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
01643 ac->m4ac.sbr = 1;
01644 ac->m4ac.ps = 1;
01645 output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
01646 } else {
01647 ac->m4ac.sbr = 1;
01648 }
01649 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
01650 break;
01651 case EXT_DYNAMIC_RANGE:
01652 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01653 break;
01654 case EXT_FILL:
01655 case EXT_FILL_DATA:
01656 case EXT_DATA_ELEMENT:
01657 default:
01658 skip_bits_long(gb, 8 * cnt - 4);
01659 break;
01660 };
01661 return res;
01662 }
01663
01670 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
01671 IndividualChannelStream *ics, int decode)
01672 {
01673 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01674 int w, filt, m, i;
01675 int bottom, top, order, start, end, size, inc;
01676 float lpc[TNS_MAX_ORDER];
01677
01678 for (w = 0; w < ics->num_windows; w++) {
01679 bottom = ics->num_swb;
01680 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01681 top = bottom;
01682 bottom = FFMAX(0, top - tns->length[w][filt]);
01683 order = tns->order[w][filt];
01684 if (order == 0)
01685 continue;
01686
01687
01688 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01689
01690 start = ics->swb_offset[FFMIN(bottom, mmm)];
01691 end = ics->swb_offset[FFMIN( top, mmm)];
01692 if ((size = end - start) <= 0)
01693 continue;
01694 if (tns->direction[w][filt]) {
01695 inc = -1;
01696 start = end - 1;
01697 } else {
01698 inc = 1;
01699 }
01700 start += w * 128;
01701
01702
01703 for (m = 0; m < size; m++, start += inc)
01704 for (i = 1; i <= FFMIN(m, order); i++)
01705 coef[start] -= coef[start - i * inc] * lpc[i - 1];
01706 }
01707 }
01708 }
01709
01713 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
01714 {
01715 IndividualChannelStream *ics = &sce->ics;
01716 float *in = sce->coeffs;
01717 float *out = sce->ret;
01718 float *saved = sce->saved;
01719 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01720 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01721 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01722 float *buf = ac->buf_mdct;
01723 float *temp = ac->temp;
01724 int i;
01725
01726
01727 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01728 for (i = 0; i < 1024; i += 128)
01729 ff_imdct_half(&ac->mdct_small, buf + i, in + i);
01730 } else
01731 ff_imdct_half(&ac->mdct, buf, in);
01732
01733
01734
01735
01736
01737
01738
01739 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01740 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01741 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, bias, 512);
01742 } else {
01743 for (i = 0; i < 448; i++)
01744 out[i] = saved[i] + bias;
01745
01746 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01747 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, bias, 64);
01748 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, bias, 64);
01749 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, bias, 64);
01750 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, bias, 64);
01751 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, bias, 64);
01752 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
01753 } else {
01754 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, bias, 64);
01755 for (i = 576; i < 1024; i++)
01756 out[i] = buf[i-512] + bias;
01757 }
01758 }
01759
01760
01761 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01762 for (i = 0; i < 64; i++)
01763 saved[i] = temp[64 + i] - bias;
01764 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
01765 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
01766 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
01767 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01768 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01769 memcpy( saved, buf + 512, 448 * sizeof(float));
01770 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01771 } else {
01772 memcpy( saved, buf + 512, 512 * sizeof(float));
01773 }
01774 }
01775
01781 static void apply_dependent_coupling(AACContext *ac,
01782 SingleChannelElement *target,
01783 ChannelElement *cce, int index)
01784 {
01785 IndividualChannelStream *ics = &cce->ch[0].ics;
01786 const uint16_t *offsets = ics->swb_offset;
01787 float *dest = target->coeffs;
01788 const float *src = cce->ch[0].coeffs;
01789 int g, i, group, k, idx = 0;
01790 if (ac->m4ac.object_type == AOT_AAC_LTP) {
01791 av_log(ac->avctx, AV_LOG_ERROR,
01792 "Dependent coupling is not supported together with LTP\n");
01793 return;
01794 }
01795 for (g = 0; g < ics->num_window_groups; g++) {
01796 for (i = 0; i < ics->max_sfb; i++, idx++) {
01797 if (cce->ch[0].band_type[idx] != ZERO_BT) {
01798 const float gain = cce->coup.gain[index][idx];
01799 for (group = 0; group < ics->group_len[g]; group++) {
01800 for (k = offsets[i]; k < offsets[i + 1]; k++) {
01801
01802 dest[group * 128 + k] += gain * src[group * 128 + k];
01803 }
01804 }
01805 }
01806 }
01807 dest += ics->group_len[g] * 128;
01808 src += ics->group_len[g] * 128;
01809 }
01810 }
01811
01817 static void apply_independent_coupling(AACContext *ac,
01818 SingleChannelElement *target,
01819 ChannelElement *cce, int index)
01820 {
01821 int i;
01822 const float gain = cce->coup.gain[index][0];
01823 const float bias = ac->add_bias;
01824 const float *src = cce->ch[0].ret;
01825 float *dest = target->ret;
01826 const int len = 1024 << (ac->m4ac.sbr == 1);
01827
01828 for (i = 0; i < len; i++)
01829 dest[i] += gain * (src[i] - bias);
01830 }
01831
01837 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
01838 enum RawDataBlockType type, int elem_id,
01839 enum CouplingPoint coupling_point,
01840 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
01841 {
01842 int i, c;
01843
01844 for (i = 0; i < MAX_ELEM_ID; i++) {
01845 ChannelElement *cce = ac->che[TYPE_CCE][i];
01846 int index = 0;
01847
01848 if (cce && cce->coup.coupling_point == coupling_point) {
01849 ChannelCoupling *coup = &cce->coup;
01850
01851 for (c = 0; c <= coup->num_coupled; c++) {
01852 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01853 if (coup->ch_select[c] != 1) {
01854 apply_coupling_method(ac, &cc->ch[0], cce, index);
01855 if (coup->ch_select[c] != 0)
01856 index++;
01857 }
01858 if (coup->ch_select[c] != 2)
01859 apply_coupling_method(ac, &cc->ch[1], cce, index++);
01860 } else
01861 index += 1 + (coup->ch_select[c] == 3);
01862 }
01863 }
01864 }
01865 }
01866
01870 static void spectral_to_sample(AACContext *ac)
01871 {
01872 int i, type;
01873 float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
01874 for (type = 3; type >= 0; type--) {
01875 for (i = 0; i < MAX_ELEM_ID; i++) {
01876 ChannelElement *che = ac->che[type][i];
01877 if (che) {
01878 if (type <= TYPE_CPE)
01879 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01880 if (che->ch[0].tns.present)
01881 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
01882 if (che->ch[1].tns.present)
01883 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
01884 if (type <= TYPE_CPE)
01885 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
01886 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
01887 imdct_and_windowing(ac, &che->ch[0], imdct_bias);
01888 if (type == TYPE_CPE) {
01889 imdct_and_windowing(ac, &che->ch[1], imdct_bias);
01890 }
01891 if (ac->m4ac.sbr > 0) {
01892 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
01893 }
01894 }
01895 if (type <= TYPE_CCE)
01896 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
01897 }
01898 }
01899 }
01900 }
01901
01902 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
01903 {
01904 int size;
01905 AACADTSHeaderInfo hdr_info;
01906
01907 size = ff_aac_parse_header(gb, &hdr_info);
01908 if (size > 0) {
01909 if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
01910 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
01911 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
01912 ac->m4ac.chan_config = hdr_info.chan_config;
01913 if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
01914 return -7;
01915 if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
01916 return -7;
01917 } else if (ac->output_configured != OC_LOCKED) {
01918 ac->output_configured = OC_NONE;
01919 }
01920 if (ac->output_configured != OC_LOCKED) {
01921 ac->m4ac.sbr = -1;
01922 ac->m4ac.ps = -1;
01923 }
01924 ac->m4ac.sample_rate = hdr_info.sample_rate;
01925 ac->m4ac.sampling_index = hdr_info.sampling_index;
01926 ac->m4ac.object_type = hdr_info.object_type;
01927 if (!ac->avctx->sample_rate)
01928 ac->avctx->sample_rate = hdr_info.sample_rate;
01929 if (hdr_info.num_aac_frames == 1) {
01930 if (!hdr_info.crc_absent)
01931 skip_bits(gb, 16);
01932 } else {
01933 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
01934 return -1;
01935 }
01936 }
01937 return size;
01938 }
01939
01940 static int aac_decode_frame(AVCodecContext *avctx, void *data,
01941 int *data_size, AVPacket *avpkt)
01942 {
01943 const uint8_t *buf = avpkt->data;
01944 int buf_size = avpkt->size;
01945 AACContext *ac = avctx->priv_data;
01946 ChannelElement *che = NULL, *che_prev = NULL;
01947 GetBitContext gb;
01948 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
01949 int err, elem_id, data_size_tmp;
01950 int buf_consumed;
01951 int samples = 0, multiplier;
01952 int buf_offset;
01953
01954 init_get_bits(&gb, buf, buf_size * 8);
01955
01956 if (show_bits(&gb, 12) == 0xfff) {
01957 if (parse_adts_frame_header(ac, &gb) < 0) {
01958 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
01959 return -1;
01960 }
01961 if (ac->m4ac.sampling_index > 12) {
01962 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
01963 return -1;
01964 }
01965 }
01966
01967 memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame));
01968
01969 while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
01970 elem_id = get_bits(&gb, 4);
01971
01972 if (elem_type < TYPE_DSE) {
01973 if (!(che=get_che(ac, elem_type, elem_id))) {
01974 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
01975 elem_type, elem_id);
01976 return -1;
01977 }
01978 samples = 1024;
01979 }
01980
01981 switch (elem_type) {
01982
01983 case TYPE_SCE:
01984 err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
01985 break;
01986
01987 case TYPE_CPE:
01988 err = decode_cpe(ac, &gb, che);
01989 break;
01990
01991 case TYPE_CCE:
01992 err = decode_cce(ac, &gb, che);
01993 break;
01994
01995 case TYPE_LFE:
01996 err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
01997 break;
01998
01999 case TYPE_DSE:
02000 err = skip_data_stream_element(ac, &gb);
02001 break;
02002
02003 case TYPE_PCE: {
02004 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02005 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02006 if ((err = decode_pce(ac, new_che_pos, &gb)))
02007 break;
02008 if (ac->output_configured > OC_TRIAL_PCE)
02009 av_log(avctx, AV_LOG_ERROR,
02010 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02011 else
02012 err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
02013 break;
02014 }
02015
02016 case TYPE_FIL:
02017 if (elem_id == 15)
02018 elem_id += get_bits(&gb, 8) - 1;
02019 if (get_bits_left(&gb) < 8 * elem_id) {
02020 av_log(avctx, AV_LOG_ERROR, overread_err);
02021 return -1;
02022 }
02023 while (elem_id > 0)
02024 elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
02025 err = 0;
02026 break;
02027
02028 default:
02029 err = -1;
02030 break;
02031 }
02032
02033 che_prev = che;
02034 elem_type_prev = elem_type;
02035
02036 if (err)
02037 return err;
02038
02039 if (get_bits_left(&gb) < 3) {
02040 av_log(avctx, AV_LOG_ERROR, overread_err);
02041 return -1;
02042 }
02043 }
02044
02045 spectral_to_sample(ac);
02046
02047 multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
02048 samples <<= multiplier;
02049 if (ac->output_configured < OC_LOCKED) {
02050 avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
02051 avctx->frame_size = samples;
02052 }
02053
02054 data_size_tmp = samples * avctx->channels * sizeof(int16_t);
02055 if (*data_size < data_size_tmp) {
02056 av_log(avctx, AV_LOG_ERROR,
02057 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
02058 *data_size, data_size_tmp);
02059 return -1;
02060 }
02061 *data_size = data_size_tmp;
02062
02063 if (samples)
02064 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
02065
02066 if (ac->output_configured)
02067 ac->output_configured = OC_LOCKED;
02068
02069 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02070 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02071 if (buf[buf_offset])
02072 break;
02073
02074 return buf_size > buf_offset ? buf_consumed : buf_size;
02075 }
02076
02077 static av_cold int aac_decode_close(AVCodecContext *avctx)
02078 {
02079 AACContext *ac = avctx->priv_data;
02080 int i, type;
02081
02082 for (i = 0; i < MAX_ELEM_ID; i++) {
02083 for (type = 0; type < 4; type++) {
02084 if (ac->che[type][i])
02085 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02086 av_freep(&ac->che[type][i]);
02087 }
02088 }
02089
02090 ff_mdct_end(&ac->mdct);
02091 ff_mdct_end(&ac->mdct_small);
02092 return 0;
02093 }
02094
02095 AVCodec aac_decoder = {
02096 "aac",
02097 AVMEDIA_TYPE_AUDIO,
02098 CODEC_ID_AAC,
02099 sizeof(AACContext),
02100 aac_decode_init,
02101 NULL,
02102 aac_decode_close,
02103 aac_decode_frame,
02104 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
02105 .sample_fmts = (const enum SampleFormat[]) {
02106 SAMPLE_FMT_S16,SAMPLE_FMT_NONE
02107 },
02108 .channel_layouts = aac_channel_layout,
02109 };