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