00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00080
00081
00082
00083 #include "avcodec.h"
00084 #include "internal.h"
00085 #include "get_bits.h"
00086 #include "dsputil.h"
00087 #include "fft.h"
00088 #include "fmtconvert.h"
00089 #include "lpc.h"
00090 #include "kbdwin.h"
00091 #include "sinewin.h"
00092
00093 #include "aac.h"
00094 #include "aactab.h"
00095 #include "aacdectab.h"
00096 #include "cbrt_tablegen.h"
00097 #include "sbr.h"
00098 #include "aacsbr.h"
00099 #include "mpeg4audio.h"
00100 #include "aacadtsdec.h"
00101
00102 #include <assert.h>
00103 #include <errno.h>
00104 #include <math.h>
00105 #include <string.h>
00106
00107 #if ARCH_ARM
00108 # include "arm/aac.h"
00109 #endif
00110
00111 union float754 {
00112 float f;
00113 uint32_t i;
00114 };
00115
00116 static VLC vlc_scalefactors;
00117 static VLC vlc_spectral[11];
00118
00119 static const char overread_err[] = "Input buffer exhausted before END element found\n";
00120
00121 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00122 {
00123
00124 if (!ac->m4ac.chan_config) {
00125 return ac->tag_che_map[type][elem_id];
00126 }
00127
00128 switch (ac->m4ac.chan_config) {
00129 case 7:
00130 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00131 ac->tags_mapped++;
00132 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00133 }
00134 case 6:
00135
00136
00137
00138 if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00139 ac->tags_mapped++;
00140 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00141 }
00142 case 5:
00143 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00144 ac->tags_mapped++;
00145 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00146 }
00147 case 4:
00148 if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
00149 ac->tags_mapped++;
00150 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00151 }
00152 case 3:
00153 case 2:
00154 if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
00155 ac->tags_mapped++;
00156 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00157 } else if (ac->m4ac.chan_config == 2) {
00158 return NULL;
00159 }
00160 case 1:
00161 if (!ac->tags_mapped && type == TYPE_SCE) {
00162 ac->tags_mapped++;
00163 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00164 }
00165 default:
00166 return NULL;
00167 }
00168 }
00169
00182 static av_cold int che_configure(AACContext *ac,
00183 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00184 int type, int id,
00185 int *channels)
00186 {
00187 if (che_pos[type][id]) {
00188 if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00189 return AVERROR(ENOMEM);
00190 ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
00191 if (type != TYPE_CCE) {
00192 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
00193 if (type == TYPE_CPE ||
00194 (type == TYPE_SCE && ac->m4ac.ps == 1)) {
00195 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
00196 }
00197 }
00198 } else {
00199 if (ac->che[type][id])
00200 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
00201 av_freep(&ac->che[type][id]);
00202 }
00203 return 0;
00204 }
00205
00214 static av_cold int output_configure(AACContext *ac,
00215 enum ChannelPosition che_pos[4][MAX_ELEM_ID],
00216 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00217 int channel_config, enum OCStatus oc_type)
00218 {
00219 AVCodecContext *avctx = ac->avctx;
00220 int i, type, channels = 0, ret;
00221
00222 if (new_che_pos != che_pos)
00223 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00224
00225 if (channel_config) {
00226 for (i = 0; i < tags_per_config[channel_config]; i++) {
00227 if ((ret = che_configure(ac, che_pos,
00228 aac_channel_layout_map[channel_config - 1][i][0],
00229 aac_channel_layout_map[channel_config - 1][i][1],
00230 &channels)))
00231 return ret;
00232 }
00233
00234 memset(ac->tag_che_map, 0, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00235
00236 avctx->channel_layout = aac_channel_layout[channel_config - 1];
00237 } else {
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 for (i = 0; i < MAX_ELEM_ID; i++) {
00248 for (type = 0; type < 4; type++) {
00249 if ((ret = che_configure(ac, che_pos, type, i, &channels)))
00250 return ret;
00251 }
00252 }
00253
00254 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00255
00256 avctx->channel_layout = 0;
00257 }
00258
00259 avctx->channels = channels;
00260
00261 ac->output_configured = oc_type;
00262
00263 return 0;
00264 }
00265
00273 static void decode_channel_map(enum ChannelPosition *cpe_map,
00274 enum ChannelPosition *sce_map,
00275 enum ChannelPosition type,
00276 GetBitContext *gb, int n)
00277 {
00278 while (n--) {
00279 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map;
00280 map[get_bits(gb, 4)] = type;
00281 }
00282 }
00283
00291 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
00292 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00293 GetBitContext *gb)
00294 {
00295 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00296 int comment_len;
00297
00298 skip_bits(gb, 2);
00299
00300 sampling_index = get_bits(gb, 4);
00301 if (m4ac->sampling_index != sampling_index)
00302 av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
00303
00304 num_front = get_bits(gb, 4);
00305 num_side = get_bits(gb, 4);
00306 num_back = get_bits(gb, 4);
00307 num_lfe = get_bits(gb, 2);
00308 num_assoc_data = get_bits(gb, 3);
00309 num_cc = get_bits(gb, 4);
00310
00311 if (get_bits1(gb))
00312 skip_bits(gb, 4);
00313 if (get_bits1(gb))
00314 skip_bits(gb, 4);
00315
00316 if (get_bits1(gb))
00317 skip_bits(gb, 3);
00318
00319 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
00320 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
00321 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
00322 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
00323
00324 skip_bits_long(gb, 4 * num_assoc_data);
00325
00326 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
00327
00328 align_get_bits(gb);
00329
00330
00331 comment_len = get_bits(gb, 8) * 8;
00332 if (get_bits_left(gb) < comment_len) {
00333 av_log(avctx, AV_LOG_ERROR, overread_err);
00334 return -1;
00335 }
00336 skip_bits_long(gb, comment_len);
00337 return 0;
00338 }
00339
00348 static av_cold int set_default_channel_config(AVCodecContext *avctx,
00349 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
00350 int channel_config)
00351 {
00352 if (channel_config < 1 || channel_config > 7) {
00353 av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00354 channel_config);
00355 return -1;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 if (channel_config != 2)
00370 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT;
00371 if (channel_config > 1)
00372 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT;
00373 if (channel_config == 4)
00374 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;
00375 if (channel_config > 4)
00376 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
00377 = AAC_CHANNEL_BACK;
00378 if (channel_config > 5)
00379 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;
00380 if (channel_config == 7)
00381 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT;
00382
00383 return 0;
00384 }
00385
00394 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
00395 GetBitContext *gb,
00396 MPEG4AudioConfig *m4ac,
00397 int channel_config)
00398 {
00399 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
00400 int extension_flag, ret;
00401
00402 if (get_bits1(gb)) {
00403 av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
00404 return -1;
00405 }
00406
00407 if (get_bits1(gb))
00408 skip_bits(gb, 14);
00409 extension_flag = get_bits1(gb);
00410
00411 if (m4ac->object_type == AOT_AAC_SCALABLE ||
00412 m4ac->object_type == AOT_ER_AAC_SCALABLE)
00413 skip_bits(gb, 3);
00414
00415 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
00416 if (channel_config == 0) {
00417 skip_bits(gb, 4);
00418 if ((ret = decode_pce(avctx, m4ac, new_che_pos, gb)))
00419 return ret;
00420 } else {
00421 if ((ret = set_default_channel_config(avctx, new_che_pos, channel_config)))
00422 return ret;
00423 }
00424 if (ac && (ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
00425 return ret;
00426
00427 if (extension_flag) {
00428 switch (m4ac->object_type) {
00429 case AOT_ER_BSAC:
00430 skip_bits(gb, 5);
00431 skip_bits(gb, 11);
00432 break;
00433 case AOT_ER_AAC_LC:
00434 case AOT_ER_AAC_LTP:
00435 case AOT_ER_AAC_SCALABLE:
00436 case AOT_ER_AAC_LD:
00437 skip_bits(gb, 3);
00438
00439
00440
00441 break;
00442 }
00443 skip_bits1(gb);
00444 }
00445 return 0;
00446 }
00447
00459 static int decode_audio_specific_config(AACContext *ac,
00460 AVCodecContext *avctx,
00461 MPEG4AudioConfig *m4ac,
00462 const uint8_t *data, int data_size)
00463 {
00464 GetBitContext gb;
00465 int i;
00466
00467 init_get_bits(&gb, data, data_size * 8);
00468
00469 if ((i = ff_mpeg4audio_get_config(m4ac, data, data_size)) < 0)
00470 return -1;
00471 if (m4ac->sampling_index > 12) {
00472 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
00473 return -1;
00474 }
00475 if (m4ac->sbr == 1 && m4ac->ps == -1)
00476 m4ac->ps = 1;
00477
00478 skip_bits_long(&gb, i);
00479
00480 switch (m4ac->object_type) {
00481 case AOT_AAC_MAIN:
00482 case AOT_AAC_LC:
00483 case AOT_AAC_LTP:
00484 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
00485 return -1;
00486 break;
00487 default:
00488 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00489 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
00490 return -1;
00491 }
00492
00493 return get_bits_count(&gb);
00494 }
00495
00503 static av_always_inline int lcg_random(int previous_val)
00504 {
00505 return previous_val * 1664525 + 1013904223;
00506 }
00507
00508 static av_always_inline void reset_predict_state(PredictorState *ps)
00509 {
00510 ps->r0 = 0.0f;
00511 ps->r1 = 0.0f;
00512 ps->cor0 = 0.0f;
00513 ps->cor1 = 0.0f;
00514 ps->var0 = 1.0f;
00515 ps->var1 = 1.0f;
00516 }
00517
00518 static void reset_all_predictors(PredictorState *ps)
00519 {
00520 int i;
00521 for (i = 0; i < MAX_PREDICTORS; i++)
00522 reset_predict_state(&ps[i]);
00523 }
00524
00525 static void reset_predictor_group(PredictorState *ps, int group_num)
00526 {
00527 int i;
00528 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00529 reset_predict_state(&ps[i]);
00530 }
00531
00532 #define AAC_INIT_VLC_STATIC(num, size) \
00533 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00534 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00535 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00536 size);
00537
00538 static av_cold int aac_decode_init(AVCodecContext *avctx)
00539 {
00540 AACContext *ac = avctx->priv_data;
00541
00542 ac->avctx = avctx;
00543 ac->m4ac.sample_rate = avctx->sample_rate;
00544
00545 if (avctx->extradata_size > 0) {
00546 if (decode_audio_specific_config(ac, ac->avctx, &ac->m4ac,
00547 avctx->extradata,
00548 avctx->extradata_size) < 0)
00549 return -1;
00550 }
00551
00552 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00553
00554 AAC_INIT_VLC_STATIC( 0, 304);
00555 AAC_INIT_VLC_STATIC( 1, 270);
00556 AAC_INIT_VLC_STATIC( 2, 550);
00557 AAC_INIT_VLC_STATIC( 3, 300);
00558 AAC_INIT_VLC_STATIC( 4, 328);
00559 AAC_INIT_VLC_STATIC( 5, 294);
00560 AAC_INIT_VLC_STATIC( 6, 306);
00561 AAC_INIT_VLC_STATIC( 7, 268);
00562 AAC_INIT_VLC_STATIC( 8, 510);
00563 AAC_INIT_VLC_STATIC( 9, 366);
00564 AAC_INIT_VLC_STATIC(10, 462);
00565
00566 ff_aac_sbr_init();
00567
00568 dsputil_init(&ac->dsp, avctx);
00569 ff_fmt_convert_init(&ac->fmt_conv, avctx);
00570
00571 ac->random_state = 0x1f2e3d4c;
00572
00573
00574
00575
00576 ac->sf_scale = 1. / -1024.;
00577 ac->sf_offset = 60;
00578
00579 ff_aac_tableinit();
00580
00581 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00582 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00583 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00584 352);
00585
00586 ff_mdct_init(&ac->mdct, 11, 1, 1.0);
00587 ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
00588 ff_mdct_init(&ac->mdct_ltp, 11, 0, 1.0);
00589
00590 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00591 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00592 ff_init_ff_sine_windows(10);
00593 ff_init_ff_sine_windows( 7);
00594
00595 cbrt_tableinit();
00596
00597 return 0;
00598 }
00599
00603 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00604 {
00605 int byte_align = get_bits1(gb);
00606 int count = get_bits(gb, 8);
00607 if (count == 255)
00608 count += get_bits(gb, 8);
00609 if (byte_align)
00610 align_get_bits(gb);
00611
00612 if (get_bits_left(gb) < 8 * count) {
00613 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00614 return -1;
00615 }
00616 skip_bits_long(gb, 8 * count);
00617 return 0;
00618 }
00619
00620 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00621 GetBitContext *gb)
00622 {
00623 int sfb;
00624 if (get_bits1(gb)) {
00625 ics->predictor_reset_group = get_bits(gb, 5);
00626 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00627 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00628 return -1;
00629 }
00630 }
00631 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
00632 ics->prediction_used[sfb] = get_bits1(gb);
00633 }
00634 return 0;
00635 }
00636
00640 static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
00641 GetBitContext *gb, uint8_t max_sfb)
00642 {
00643 int sfb;
00644
00645 ltp->lag = get_bits(gb, 11);
00646 ltp->coef = ltp_coef[get_bits(gb, 3)] * ac->sf_scale;
00647 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
00648 ltp->used[sfb] = get_bits1(gb);
00649 }
00650
00656 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
00657 GetBitContext *gb, int common_window)
00658 {
00659 if (get_bits1(gb)) {
00660 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
00661 memset(ics, 0, sizeof(IndividualChannelStream));
00662 return -1;
00663 }
00664 ics->window_sequence[1] = ics->window_sequence[0];
00665 ics->window_sequence[0] = get_bits(gb, 2);
00666 ics->use_kb_window[1] = ics->use_kb_window[0];
00667 ics->use_kb_window[0] = get_bits1(gb);
00668 ics->num_window_groups = 1;
00669 ics->group_len[0] = 1;
00670 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
00671 int i;
00672 ics->max_sfb = get_bits(gb, 4);
00673 for (i = 0; i < 7; i++) {
00674 if (get_bits1(gb)) {
00675 ics->group_len[ics->num_window_groups - 1]++;
00676 } else {
00677 ics->num_window_groups++;
00678 ics->group_len[ics->num_window_groups - 1] = 1;
00679 }
00680 }
00681 ics->num_windows = 8;
00682 ics->swb_offset = ff_swb_offset_128[ac->m4ac.sampling_index];
00683 ics->num_swb = ff_aac_num_swb_128[ac->m4ac.sampling_index];
00684 ics->tns_max_bands = ff_tns_max_bands_128[ac->m4ac.sampling_index];
00685 ics->predictor_present = 0;
00686 } else {
00687 ics->max_sfb = get_bits(gb, 6);
00688 ics->num_windows = 1;
00689 ics->swb_offset = ff_swb_offset_1024[ac->m4ac.sampling_index];
00690 ics->num_swb = ff_aac_num_swb_1024[ac->m4ac.sampling_index];
00691 ics->tns_max_bands = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
00692 ics->predictor_present = get_bits1(gb);
00693 ics->predictor_reset_group = 0;
00694 if (ics->predictor_present) {
00695 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
00696 if (decode_prediction(ac, ics, gb)) {
00697 memset(ics, 0, sizeof(IndividualChannelStream));
00698 return -1;
00699 }
00700 } else if (ac->m4ac.object_type == AOT_AAC_LC) {
00701 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
00702 memset(ics, 0, sizeof(IndividualChannelStream));
00703 return -1;
00704 } else {
00705 if ((ics->ltp.present = get_bits(gb, 1)))
00706 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
00707 }
00708 }
00709 }
00710
00711 if (ics->max_sfb > ics->num_swb) {
00712 av_log(ac->avctx, AV_LOG_ERROR,
00713 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
00714 ics->max_sfb, ics->num_swb);
00715 memset(ics, 0, sizeof(IndividualChannelStream));
00716 return -1;
00717 }
00718
00719 return 0;
00720 }
00721
00730 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
00731 int band_type_run_end[120], GetBitContext *gb,
00732 IndividualChannelStream *ics)
00733 {
00734 int g, idx = 0;
00735 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
00736 for (g = 0; g < ics->num_window_groups; g++) {
00737 int k = 0;
00738 while (k < ics->max_sfb) {
00739 uint8_t sect_end = k;
00740 int sect_len_incr;
00741 int sect_band_type = get_bits(gb, 4);
00742 if (sect_band_type == 12) {
00743 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
00744 return -1;
00745 }
00746 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
00747 sect_end += sect_len_incr;
00748 sect_end += sect_len_incr;
00749 if (get_bits_left(gb) < 0) {
00750 av_log(ac->avctx, AV_LOG_ERROR, overread_err);
00751 return -1;
00752 }
00753 if (sect_end > ics->max_sfb) {
00754 av_log(ac->avctx, AV_LOG_ERROR,
00755 "Number of bands (%d) exceeds limit (%d).\n",
00756 sect_end, ics->max_sfb);
00757 return -1;
00758 }
00759 for (; k < sect_end; k++) {
00760 band_type [idx] = sect_band_type;
00761 band_type_run_end[idx++] = sect_end;
00762 }
00763 }
00764 }
00765 return 0;
00766 }
00767
00778 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
00779 unsigned int global_gain,
00780 IndividualChannelStream *ics,
00781 enum BandType band_type[120],
00782 int band_type_run_end[120])
00783 {
00784 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
00785 int g, i, idx = 0;
00786 int offset[3] = { global_gain, global_gain - 90, 100 };
00787 int noise_flag = 1;
00788 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
00789 for (g = 0; g < ics->num_window_groups; g++) {
00790 for (i = 0; i < ics->max_sfb;) {
00791 int run_end = band_type_run_end[idx];
00792 if (band_type[idx] == ZERO_BT) {
00793 for (; i < run_end; i++, idx++)
00794 sf[idx] = 0.;
00795 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
00796 for (; i < run_end; i++, idx++) {
00797 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00798 if (offset[2] > 255U) {
00799 av_log(ac->avctx, AV_LOG_ERROR,
00800 "%s (%d) out of range.\n", sf_str[2], offset[2]);
00801 return -1;
00802 }
00803 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
00804 }
00805 } else if (band_type[idx] == NOISE_BT) {
00806 for (; i < run_end; i++, idx++) {
00807 if (noise_flag-- > 0)
00808 offset[1] += get_bits(gb, 9) - 256;
00809 else
00810 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00811 if (offset[1] > 255U) {
00812 av_log(ac->avctx, AV_LOG_ERROR,
00813 "%s (%d) out of range.\n", sf_str[1], offset[1]);
00814 return -1;
00815 }
00816 sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
00817 }
00818 } else {
00819 for (; i < run_end; i++, idx++) {
00820 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
00821 if (offset[0] > 255U) {
00822 av_log(ac->avctx, AV_LOG_ERROR,
00823 "%s (%d) out of range.\n", sf_str[0], offset[0]);
00824 return -1;
00825 }
00826 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
00827 }
00828 }
00829 }
00830 }
00831 return 0;
00832 }
00833
00837 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
00838 const uint16_t *swb_offset, int num_swb)
00839 {
00840 int i, pulse_swb;
00841 pulse->num_pulse = get_bits(gb, 2) + 1;
00842 pulse_swb = get_bits(gb, 6);
00843 if (pulse_swb >= num_swb)
00844 return -1;
00845 pulse->pos[0] = swb_offset[pulse_swb];
00846 pulse->pos[0] += get_bits(gb, 5);
00847 if (pulse->pos[0] > 1023)
00848 return -1;
00849 pulse->amp[0] = get_bits(gb, 4);
00850 for (i = 1; i < pulse->num_pulse; i++) {
00851 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
00852 if (pulse->pos[i] > 1023)
00853 return -1;
00854 pulse->amp[i] = get_bits(gb, 4);
00855 }
00856 return 0;
00857 }
00858
00864 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
00865 GetBitContext *gb, const IndividualChannelStream *ics)
00866 {
00867 int w, filt, i, coef_len, coef_res, coef_compress;
00868 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
00869 const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
00870 for (w = 0; w < ics->num_windows; w++) {
00871 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
00872 coef_res = get_bits1(gb);
00873
00874 for (filt = 0; filt < tns->n_filt[w]; filt++) {
00875 int tmp2_idx;
00876 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
00877
00878 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
00879 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
00880 tns->order[w][filt], tns_max_order);
00881 tns->order[w][filt] = 0;
00882 return -1;
00883 }
00884 if (tns->order[w][filt]) {
00885 tns->direction[w][filt] = get_bits1(gb);
00886 coef_compress = get_bits1(gb);
00887 coef_len = coef_res + 3 - coef_compress;
00888 tmp2_idx = 2 * coef_compress + coef_res;
00889
00890 for (i = 0; i < tns->order[w][filt]; i++)
00891 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
00892 }
00893 }
00894 }
00895 }
00896 return 0;
00897 }
00898
00906 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
00907 int ms_present)
00908 {
00909 int idx;
00910 if (ms_present == 1) {
00911 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
00912 cpe->ms_mask[idx] = get_bits1(gb);
00913 } else if (ms_present == 2) {
00914 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
00915 }
00916 }
00917
00918 #ifndef VMUL2
00919 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
00920 const float *scale)
00921 {
00922 float s = *scale;
00923 *dst++ = v[idx & 15] * s;
00924 *dst++ = v[idx>>4 & 15] * s;
00925 return dst;
00926 }
00927 #endif
00928
00929 #ifndef VMUL4
00930 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
00931 const float *scale)
00932 {
00933 float s = *scale;
00934 *dst++ = v[idx & 3] * s;
00935 *dst++ = v[idx>>2 & 3] * s;
00936 *dst++ = v[idx>>4 & 3] * s;
00937 *dst++ = v[idx>>6 & 3] * s;
00938 return dst;
00939 }
00940 #endif
00941
00942 #ifndef VMUL2S
00943 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
00944 unsigned sign, const float *scale)
00945 {
00946 union float754 s0, s1;
00947
00948 s0.f = s1.f = *scale;
00949 s0.i ^= sign >> 1 << 31;
00950 s1.i ^= sign << 31;
00951
00952 *dst++ = v[idx & 15] * s0.f;
00953 *dst++ = v[idx>>4 & 15] * s1.f;
00954
00955 return dst;
00956 }
00957 #endif
00958
00959 #ifndef VMUL4S
00960 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
00961 unsigned sign, const float *scale)
00962 {
00963 unsigned nz = idx >> 12;
00964 union float754 s = { .f = *scale };
00965 union float754 t;
00966
00967 t.i = s.i ^ (sign & 1<<31);
00968 *dst++ = v[idx & 3] * t.f;
00969
00970 sign <<= nz & 1; nz >>= 1;
00971 t.i = s.i ^ (sign & 1<<31);
00972 *dst++ = v[idx>>2 & 3] * t.f;
00973
00974 sign <<= nz & 1; nz >>= 1;
00975 t.i = s.i ^ (sign & 1<<31);
00976 *dst++ = v[idx>>4 & 3] * t.f;
00977
00978 sign <<= nz & 1; nz >>= 1;
00979 t.i = s.i ^ (sign & 1<<31);
00980 *dst++ = v[idx>>6 & 3] * t.f;
00981
00982 return dst;
00983 }
00984 #endif
00985
00998 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
00999 GetBitContext *gb, const float sf[120],
01000 int pulse_present, const Pulse *pulse,
01001 const IndividualChannelStream *ics,
01002 enum BandType band_type[120])
01003 {
01004 int i, k, g, idx = 0;
01005 const int c = 1024 / ics->num_windows;
01006 const uint16_t *offsets = ics->swb_offset;
01007 float *coef_base = coef;
01008
01009 for (g = 0; g < ics->num_windows; g++)
01010 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01011
01012 for (g = 0; g < ics->num_window_groups; g++) {
01013 unsigned g_len = ics->group_len[g];
01014
01015 for (i = 0; i < ics->max_sfb; i++, idx++) {
01016 const unsigned cbt_m1 = band_type[idx] - 1;
01017 float *cfo = coef + offsets[i];
01018 int off_len = offsets[i + 1] - offsets[i];
01019 int group;
01020
01021 if (cbt_m1 >= INTENSITY_BT2 - 1) {
01022 for (group = 0; group < g_len; group++, cfo+=128) {
01023 memset(cfo, 0, off_len * sizeof(float));
01024 }
01025 } else if (cbt_m1 == NOISE_BT - 1) {
01026 for (group = 0; group < g_len; group++, cfo+=128) {
01027 float scale;
01028 float band_energy;
01029
01030 for (k = 0; k < off_len; k++) {
01031 ac->random_state = lcg_random(ac->random_state);
01032 cfo[k] = ac->random_state;
01033 }
01034
01035 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01036 scale = sf[idx] / sqrtf(band_energy);
01037 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01038 }
01039 } else {
01040 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01041 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01042 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01043 OPEN_READER(re, gb);
01044
01045 switch (cbt_m1 >> 1) {
01046 case 0:
01047 for (group = 0; group < g_len; group++, cfo+=128) {
01048 float *cf = cfo;
01049 int len = off_len;
01050
01051 do {
01052 int code;
01053 unsigned cb_idx;
01054
01055 UPDATE_CACHE(re, gb);
01056 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01057 cb_idx = cb_vector_idx[code];
01058 cf = VMUL4(cf, vq, cb_idx, sf + idx);
01059 } while (len -= 4);
01060 }
01061 break;
01062
01063 case 1:
01064 for (group = 0; group < g_len; group++, cfo+=128) {
01065 float *cf = cfo;
01066 int len = off_len;
01067
01068 do {
01069 int code;
01070 unsigned nnz;
01071 unsigned cb_idx;
01072 uint32_t bits;
01073
01074 UPDATE_CACHE(re, gb);
01075 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01076 cb_idx = cb_vector_idx[code];
01077 nnz = cb_idx >> 8 & 15;
01078 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01079 LAST_SKIP_BITS(re, gb, nnz);
01080 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01081 } while (len -= 4);
01082 }
01083 break;
01084
01085 case 2:
01086 for (group = 0; group < g_len; group++, cfo+=128) {
01087 float *cf = cfo;
01088 int len = off_len;
01089
01090 do {
01091 int code;
01092 unsigned cb_idx;
01093
01094 UPDATE_CACHE(re, gb);
01095 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01096 cb_idx = cb_vector_idx[code];
01097 cf = VMUL2(cf, vq, cb_idx, sf + idx);
01098 } while (len -= 2);
01099 }
01100 break;
01101
01102 case 3:
01103 case 4:
01104 for (group = 0; group < g_len; group++, cfo+=128) {
01105 float *cf = cfo;
01106 int len = off_len;
01107
01108 do {
01109 int code;
01110 unsigned nnz;
01111 unsigned cb_idx;
01112 unsigned sign;
01113
01114 UPDATE_CACHE(re, gb);
01115 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01116 cb_idx = cb_vector_idx[code];
01117 nnz = cb_idx >> 8 & 15;
01118 sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
01119 LAST_SKIP_BITS(re, gb, nnz);
01120 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01121 } while (len -= 2);
01122 }
01123 break;
01124
01125 default:
01126 for (group = 0; group < g_len; group++, cfo+=128) {
01127 float *cf = cfo;
01128 uint32_t *icf = (uint32_t *) cf;
01129 int len = off_len;
01130
01131 do {
01132 int code;
01133 unsigned nzt, nnz;
01134 unsigned cb_idx;
01135 uint32_t bits;
01136 int j;
01137
01138 UPDATE_CACHE(re, gb);
01139 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01140
01141 if (!code) {
01142 *icf++ = 0;
01143 *icf++ = 0;
01144 continue;
01145 }
01146
01147 cb_idx = cb_vector_idx[code];
01148 nnz = cb_idx >> 12;
01149 nzt = cb_idx >> 8;
01150 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01151 LAST_SKIP_BITS(re, gb, nnz);
01152
01153 for (j = 0; j < 2; j++) {
01154 if (nzt & 1<<j) {
01155 uint32_t b;
01156 int n;
01157
01158
01159 UPDATE_CACHE(re, gb);
01160 b = GET_CACHE(re, gb);
01161 b = 31 - av_log2(~b);
01162
01163 if (b > 8) {
01164 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01165 return -1;
01166 }
01167
01168 SKIP_BITS(re, gb, b + 1);
01169 b += 4;
01170 n = (1 << b) + SHOW_UBITS(re, gb, b);
01171 LAST_SKIP_BITS(re, gb, b);
01172 *icf++ = cbrt_tab[n] | (bits & 1<<31);
01173 bits <<= 1;
01174 } else {
01175 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01176 *icf++ = (bits & 1<<31) | v;
01177 bits <<= !!v;
01178 }
01179 cb_idx >>= 4;
01180 }
01181 } while (len -= 2);
01182
01183 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01184 }
01185 }
01186
01187 CLOSE_READER(re, gb);
01188 }
01189 }
01190 coef += g_len << 7;
01191 }
01192
01193 if (pulse_present) {
01194 idx = 0;
01195 for (i = 0; i < pulse->num_pulse; i++) {
01196 float co = coef_base[ pulse->pos[i] ];
01197 while (offsets[idx + 1] <= pulse->pos[i])
01198 idx++;
01199 if (band_type[idx] != NOISE_BT && sf[idx]) {
01200 float ico = -pulse->amp[i];
01201 if (co) {
01202 co /= sf[idx];
01203 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01204 }
01205 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01206 }
01207 }
01208 }
01209 return 0;
01210 }
01211
01212 static av_always_inline float flt16_round(float pf)
01213 {
01214 union float754 tmp;
01215 tmp.f = pf;
01216 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01217 return tmp.f;
01218 }
01219
01220 static av_always_inline float flt16_even(float pf)
01221 {
01222 union float754 tmp;
01223 tmp.f = pf;
01224 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01225 return tmp.f;
01226 }
01227
01228 static av_always_inline float flt16_trunc(float pf)
01229 {
01230 union float754 pun;
01231 pun.f = pf;
01232 pun.i &= 0xFFFF0000U;
01233 return pun.f;
01234 }
01235
01236 static av_always_inline void predict(PredictorState *ps, float *coef,
01237 float sf_scale, float inv_sf_scale,
01238 int output_enable)
01239 {
01240 const float a = 0.953125;
01241 const float alpha = 0.90625;
01242 float e0, e1;
01243 float pv;
01244 float k1, k2;
01245 float r0 = ps->r0, r1 = ps->r1;
01246 float cor0 = ps->cor0, cor1 = ps->cor1;
01247 float var0 = ps->var0, var1 = ps->var1;
01248
01249 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
01250 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
01251
01252 pv = flt16_round(k1 * r0 + k2 * r1);
01253 if (output_enable)
01254 *coef += pv * sf_scale;
01255
01256 e0 = *coef * inv_sf_scale;
01257 e1 = e0 - k1 * r0;
01258
01259 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
01260 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
01261 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
01262 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
01263
01264 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
01265 ps->r0 = flt16_trunc(a * e0);
01266 }
01267
01271 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01272 {
01273 int sfb, k;
01274 float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
01275
01276 if (!sce->ics.predictor_initialized) {
01277 reset_all_predictors(sce->predictor_state);
01278 sce->ics.predictor_initialized = 1;
01279 }
01280
01281 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01282 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
01283 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01284 predict(&sce->predictor_state[k], &sce->coeffs[k],
01285 sf_scale, inv_sf_scale,
01286 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01287 }
01288 }
01289 if (sce->ics.predictor_reset_group)
01290 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01291 } else
01292 reset_all_predictors(sce->predictor_state);
01293 }
01294
01303 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01304 GetBitContext *gb, int common_window, int scale_flag)
01305 {
01306 Pulse pulse;
01307 TemporalNoiseShaping *tns = &sce->tns;
01308 IndividualChannelStream *ics = &sce->ics;
01309 float *out = sce->coeffs;
01310 int global_gain, pulse_present = 0;
01311
01312
01313
01314
01315 pulse.num_pulse = 0;
01316
01317 global_gain = get_bits(gb, 8);
01318
01319 if (!common_window && !scale_flag) {
01320 if (decode_ics_info(ac, ics, gb, 0) < 0)
01321 return -1;
01322 }
01323
01324 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01325 return -1;
01326 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01327 return -1;
01328
01329 pulse_present = 0;
01330 if (!scale_flag) {
01331 if ((pulse_present = get_bits1(gb))) {
01332 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01333 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01334 return -1;
01335 }
01336 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01337 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01338 return -1;
01339 }
01340 }
01341 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01342 return -1;
01343 if (get_bits1(gb)) {
01344 av_log_missing_feature(ac->avctx, "SSR", 1);
01345 return -1;
01346 }
01347 }
01348
01349 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01350 return -1;
01351
01352 if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
01353 apply_prediction(ac, sce);
01354
01355 return 0;
01356 }
01357
01361 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01362 {
01363 const IndividualChannelStream *ics = &cpe->ch[0].ics;
01364 float *ch0 = cpe->ch[0].coeffs;
01365 float *ch1 = cpe->ch[1].coeffs;
01366 int g, i, group, idx = 0;
01367 const uint16_t *offsets = ics->swb_offset;
01368 for (g = 0; g < ics->num_window_groups; g++) {
01369 for (i = 0; i < ics->max_sfb; i++, idx++) {
01370 if (cpe->ms_mask[idx] &&
01371 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01372 for (group = 0; group < ics->group_len[g]; group++) {
01373 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01374 ch1 + group * 128 + offsets[i],
01375 offsets[i+1] - offsets[i]);
01376 }
01377 }
01378 }
01379 ch0 += ics->group_len[g] * 128;
01380 ch1 += ics->group_len[g] * 128;
01381 }
01382 }
01383
01391 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
01392 {
01393 const IndividualChannelStream *ics = &cpe->ch[1].ics;
01394 SingleChannelElement *sce1 = &cpe->ch[1];
01395 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01396 const uint16_t *offsets = ics->swb_offset;
01397 int g, group, i, idx = 0;
01398 int c;
01399 float scale;
01400 for (g = 0; g < ics->num_window_groups; g++) {
01401 for (i = 0; i < ics->max_sfb;) {
01402 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01403 const int bt_run_end = sce1->band_type_run_end[idx];
01404 for (; i < bt_run_end; i++, idx++) {
01405 c = -1 + 2 * (sce1->band_type[idx] - 14);
01406 if (ms_present)
01407 c *= 1 - 2 * cpe->ms_mask[idx];
01408 scale = c * sce1->sf[idx];
01409 for (group = 0; group < ics->group_len[g]; group++)
01410 ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
01411 coef0 + group * 128 + offsets[i],
01412 scale,
01413 offsets[i + 1] - offsets[i]);
01414 }
01415 } else {
01416 int bt_run_end = sce1->band_type_run_end[idx];
01417 idx += bt_run_end - i;
01418 i = bt_run_end;
01419 }
01420 }
01421 coef0 += ics->group_len[g] * 128;
01422 coef1 += ics->group_len[g] * 128;
01423 }
01424 }
01425
01431 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01432 {
01433 int i, ret, common_window, ms_present = 0;
01434
01435 common_window = get_bits1(gb);
01436 if (common_window) {
01437 if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
01438 return -1;
01439 i = cpe->ch[1].ics.use_kb_window[0];
01440 cpe->ch[1].ics = cpe->ch[0].ics;
01441 cpe->ch[1].ics.use_kb_window[1] = i;
01442 if (cpe->ch[1].ics.predictor_present && (ac->m4ac.object_type != AOT_AAC_MAIN))
01443 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
01444 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
01445 ms_present = get_bits(gb, 2);
01446 if (ms_present == 3) {
01447 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01448 return -1;
01449 } else if (ms_present)
01450 decode_mid_side_stereo(cpe, gb, ms_present);
01451 }
01452 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01453 return ret;
01454 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01455 return ret;
01456
01457 if (common_window) {
01458 if (ms_present)
01459 apply_mid_side_stereo(ac, cpe);
01460 if (ac->m4ac.object_type == AOT_AAC_MAIN) {
01461 apply_prediction(ac, &cpe->ch[0]);
01462 apply_prediction(ac, &cpe->ch[1]);
01463 }
01464 }
01465
01466 apply_intensity_stereo(ac, cpe, ms_present);
01467 return 0;
01468 }
01469
01470 static const float cce_scale[] = {
01471 1.09050773266525765921,
01472 1.18920711500272106672,
01473 M_SQRT2,
01474 2,
01475 };
01476
01482 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01483 {
01484 int num_gain = 0;
01485 int c, g, sfb, ret;
01486 int sign;
01487 float scale;
01488 SingleChannelElement *sce = &che->ch[0];
01489 ChannelCoupling *coup = &che->coup;
01490
01491 coup->coupling_point = 2 * get_bits1(gb);
01492 coup->num_coupled = get_bits(gb, 3);
01493 for (c = 0; c <= coup->num_coupled; c++) {
01494 num_gain++;
01495 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01496 coup->id_select[c] = get_bits(gb, 4);
01497 if (coup->type[c] == TYPE_CPE) {
01498 coup->ch_select[c] = get_bits(gb, 2);
01499 if (coup->ch_select[c] == 3)
01500 num_gain++;
01501 } else
01502 coup->ch_select[c] = 2;
01503 }
01504 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01505
01506 sign = get_bits(gb, 1);
01507 scale = cce_scale[get_bits(gb, 2)];
01508
01509 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01510 return ret;
01511
01512 for (c = 0; c < num_gain; c++) {
01513 int idx = 0;
01514 int cge = 1;
01515 int gain = 0;
01516 float gain_cache = 1.;
01517 if (c) {
01518 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01519 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01520 gain_cache = powf(scale, -gain);
01521 }
01522 if (coup->coupling_point == AFTER_IMDCT) {
01523 coup->gain[c][0] = gain_cache;
01524 } else {
01525 for (g = 0; g < sce->ics.num_window_groups; g++) {
01526 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01527 if (sce->band_type[idx] != ZERO_BT) {
01528 if (!cge) {
01529 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01530 if (t) {
01531 int s = 1;
01532 t = gain += t;
01533 if (sign) {
01534 s -= 2 * (t & 0x1);
01535 t >>= 1;
01536 }
01537 gain_cache = powf(scale, -t) * s;
01538 }
01539 }
01540 coup->gain[c][idx] = gain_cache;
01541 }
01542 }
01543 }
01544 }
01545 }
01546 return 0;
01547 }
01548
01554 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01555 GetBitContext *gb)
01556 {
01557 int i;
01558 int num_excl_chan = 0;
01559
01560 do {
01561 for (i = 0; i < 7; i++)
01562 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01563 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01564
01565 return num_excl_chan / 7;
01566 }
01567
01575 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01576 GetBitContext *gb, int cnt)
01577 {
01578 int n = 1;
01579 int drc_num_bands = 1;
01580 int i;
01581
01582
01583 if (get_bits1(gb)) {
01584 che_drc->pce_instance_tag = get_bits(gb, 4);
01585 skip_bits(gb, 4);
01586 n++;
01587 }
01588
01589
01590 if (get_bits1(gb)) {
01591 n += decode_drc_channel_exclusions(che_drc, gb);
01592 }
01593
01594
01595 if (get_bits1(gb)) {
01596 che_drc->band_incr = get_bits(gb, 4);
01597 che_drc->interpolation_scheme = get_bits(gb, 4);
01598 n++;
01599 drc_num_bands += che_drc->band_incr;
01600 for (i = 0; i < drc_num_bands; i++) {
01601 che_drc->band_top[i] = get_bits(gb, 8);
01602 n++;
01603 }
01604 }
01605
01606
01607 if (get_bits1(gb)) {
01608 che_drc->prog_ref_level = get_bits(gb, 7);
01609 skip_bits1(gb);
01610 n++;
01611 }
01612
01613 for (i = 0; i < drc_num_bands; i++) {
01614 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01615 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01616 n++;
01617 }
01618
01619 return n;
01620 }
01621
01629 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
01630 ChannelElement *che, enum RawDataBlockType elem_type)
01631 {
01632 int crc_flag = 0;
01633 int res = cnt;
01634 switch (get_bits(gb, 4)) {
01635 case EXT_SBR_DATA_CRC:
01636 crc_flag++;
01637 case EXT_SBR_DATA:
01638 if (!che) {
01639 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
01640 return res;
01641 } else if (!ac->m4ac.sbr) {
01642 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
01643 skip_bits_long(gb, 8 * cnt - 4);
01644 return res;
01645 } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
01646 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
01647 skip_bits_long(gb, 8 * cnt - 4);
01648 return res;
01649 } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
01650 ac->m4ac.sbr = 1;
01651 ac->m4ac.ps = 1;
01652 output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
01653 } else {
01654 ac->m4ac.sbr = 1;
01655 }
01656 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
01657 break;
01658 case EXT_DYNAMIC_RANGE:
01659 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
01660 break;
01661 case EXT_FILL:
01662 case EXT_FILL_DATA:
01663 case EXT_DATA_ELEMENT:
01664 default:
01665 skip_bits_long(gb, 8 * cnt - 4);
01666 break;
01667 };
01668 return res;
01669 }
01670
01677 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
01678 IndividualChannelStream *ics, int decode)
01679 {
01680 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
01681 int w, filt, m, i;
01682 int bottom, top, order, start, end, size, inc;
01683 float lpc[TNS_MAX_ORDER];
01684 float tmp[TNS_MAX_ORDER];
01685
01686 for (w = 0; w < ics->num_windows; w++) {
01687 bottom = ics->num_swb;
01688 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01689 top = bottom;
01690 bottom = FFMAX(0, top - tns->length[w][filt]);
01691 order = tns->order[w][filt];
01692 if (order == 0)
01693 continue;
01694
01695
01696 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
01697
01698 start = ics->swb_offset[FFMIN(bottom, mmm)];
01699 end = ics->swb_offset[FFMIN( top, mmm)];
01700 if ((size = end - start) <= 0)
01701 continue;
01702 if (tns->direction[w][filt]) {
01703 inc = -1;
01704 start = end - 1;
01705 } else {
01706 inc = 1;
01707 }
01708 start += w * 128;
01709
01710 if (decode) {
01711
01712 for (m = 0; m < size; m++, start += inc)
01713 for (i = 1; i <= FFMIN(m, order); i++)
01714 coef[start] -= coef[start - i * inc] * lpc[i - 1];
01715 } else {
01716
01717 for (m = 0; m < size; m++, start += inc) {
01718 tmp[0] = coef[start];
01719 for (i = 1; i <= FFMIN(m, order); i++)
01720 coef[start] += tmp[i] * lpc[i - 1];
01721 for (i = order; i > 0; i--)
01722 tmp[i] = tmp[i - 1];
01723 }
01724 }
01725 }
01726 }
01727 }
01728
01733 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
01734 float *in, IndividualChannelStream *ics)
01735 {
01736 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01737 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01738 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01739 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01740
01741 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
01742 ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
01743 } else {
01744 memset(in, 0, 448 * sizeof(float));
01745 ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
01746 memcpy(in + 576, in + 576, 448 * sizeof(float));
01747 }
01748 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
01749 ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
01750 } else {
01751 memcpy(in + 1024, in + 1024, 448 * sizeof(float));
01752 ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
01753 memset(in + 1024 + 576, 0, 448 * sizeof(float));
01754 }
01755 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
01756 }
01757
01761 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
01762 {
01763 const LongTermPrediction *ltp = &sce->ics.ltp;
01764 const uint16_t *offsets = sce->ics.swb_offset;
01765 int i, sfb;
01766
01767 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01768 float *predTime = sce->ret;
01769 float *predFreq = ac->buf_mdct;
01770 int16_t num_samples = 2048;
01771
01772 if (ltp->lag < 1024)
01773 num_samples = ltp->lag + 1024;
01774 for (i = 0; i < num_samples; i++)
01775 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
01776 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
01777
01778 windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
01779
01780 if (sce->tns.present)
01781 apply_tns(predFreq, &sce->tns, &sce->ics, 0);
01782
01783 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
01784 if (ltp->used[sfb])
01785 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
01786 sce->coeffs[i] += predFreq[i];
01787 }
01788 }
01789
01793 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
01794 {
01795 IndividualChannelStream *ics = &sce->ics;
01796 float *saved = sce->saved;
01797 float *saved_ltp = sce->coeffs;
01798 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01799 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01800 int i;
01801
01802 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01803 memcpy(saved_ltp, saved, 512 * sizeof(float));
01804 memset(saved_ltp + 576, 0, 448 * sizeof(float));
01805 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
01806 for (i = 0; i < 64; i++)
01807 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
01808 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01809 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
01810 memset(saved_ltp + 576, 0, 448 * sizeof(float));
01811 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
01812 for (i = 0; i < 64; i++)
01813 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
01814 } else {
01815 ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
01816 for (i = 0; i < 512; i++)
01817 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
01818 }
01819
01820 memcpy(sce->ltp_state, &sce->ltp_state[1024], 1024 * sizeof(int16_t));
01821 ac->fmt_conv.float_to_int16(&(sce->ltp_state[1024]), sce->ret, 1024);
01822 ac->fmt_conv.float_to_int16(&(sce->ltp_state[2048]), saved_ltp, 1024);
01823 }
01824
01828 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
01829 {
01830 IndividualChannelStream *ics = &sce->ics;
01831 float *in = sce->coeffs;
01832 float *out = sce->ret;
01833 float *saved = sce->saved;
01834 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
01835 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
01836 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
01837 float *buf = ac->buf_mdct;
01838 float *temp = ac->temp;
01839 int i;
01840
01841
01842 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01843 for (i = 0; i < 1024; i += 128)
01844 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
01845 } else
01846 ac->mdct.imdct_half(&ac->mdct, buf, in);
01847
01848
01849
01850
01851
01852
01853
01854 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
01855 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
01856 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
01857 } else {
01858 memcpy( out, saved, 448 * sizeof(float));
01859
01860 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01861 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
01862 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
01863 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
01864 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
01865 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
01866 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
01867 } else {
01868 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
01869 memcpy( out + 576, buf + 64, 448 * sizeof(float));
01870 }
01871 }
01872
01873
01874 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01875 memcpy( saved, temp + 64, 64 * sizeof(float));
01876 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
01877 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
01878 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
01879 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01880 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
01881 memcpy( saved, buf + 512, 448 * sizeof(float));
01882 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
01883 } else {
01884 memcpy( saved, buf + 512, 512 * sizeof(float));
01885 }
01886 }
01887
01893 static void apply_dependent_coupling(AACContext *ac,
01894 SingleChannelElement *target,
01895 ChannelElement *cce, int index)
01896 {
01897 IndividualChannelStream *ics = &cce->ch[0].ics;
01898 const uint16_t *offsets = ics->swb_offset;
01899 float *dest = target->coeffs;
01900 const float *src = cce->ch[0].coeffs;
01901 int g, i, group, k, idx = 0;
01902 if (ac->m4ac.object_type == AOT_AAC_LTP) {
01903 av_log(ac->avctx, AV_LOG_ERROR,
01904 "Dependent coupling is not supported together with LTP\n");
01905 return;
01906 }
01907 for (g = 0; g < ics->num_window_groups; g++) {
01908 for (i = 0; i < ics->max_sfb; i++, idx++) {
01909 if (cce->ch[0].band_type[idx] != ZERO_BT) {
01910 const float gain = cce->coup.gain[index][idx];
01911 for (group = 0; group < ics->group_len[g]; group++) {
01912 for (k = offsets[i]; k < offsets[i + 1]; k++) {
01913
01914 dest[group * 128 + k] += gain * src[group * 128 + k];
01915 }
01916 }
01917 }
01918 }
01919 dest += ics->group_len[g] * 128;
01920 src += ics->group_len[g] * 128;
01921 }
01922 }
01923
01929 static void apply_independent_coupling(AACContext *ac,
01930 SingleChannelElement *target,
01931 ChannelElement *cce, int index)
01932 {
01933 int i;
01934 const float gain = cce->coup.gain[index][0];
01935 const float *src = cce->ch[0].ret;
01936 float *dest = target->ret;
01937 const int len = 1024 << (ac->m4ac.sbr == 1);
01938
01939 for (i = 0; i < len; i++)
01940 dest[i] += gain * src[i];
01941 }
01942
01948 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
01949 enum RawDataBlockType type, int elem_id,
01950 enum CouplingPoint coupling_point,
01951 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
01952 {
01953 int i, c;
01954
01955 for (i = 0; i < MAX_ELEM_ID; i++) {
01956 ChannelElement *cce = ac->che[TYPE_CCE][i];
01957 int index = 0;
01958
01959 if (cce && cce->coup.coupling_point == coupling_point) {
01960 ChannelCoupling *coup = &cce->coup;
01961
01962 for (c = 0; c <= coup->num_coupled; c++) {
01963 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
01964 if (coup->ch_select[c] != 1) {
01965 apply_coupling_method(ac, &cc->ch[0], cce, index);
01966 if (coup->ch_select[c] != 0)
01967 index++;
01968 }
01969 if (coup->ch_select[c] != 2)
01970 apply_coupling_method(ac, &cc->ch[1], cce, index++);
01971 } else
01972 index += 1 + (coup->ch_select[c] == 3);
01973 }
01974 }
01975 }
01976 }
01977
01981 static void spectral_to_sample(AACContext *ac)
01982 {
01983 int i, type;
01984 for (type = 3; type >= 0; type--) {
01985 for (i = 0; i < MAX_ELEM_ID; i++) {
01986 ChannelElement *che = ac->che[type][i];
01987 if (che) {
01988 if (type <= TYPE_CPE)
01989 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
01990 if (ac->m4ac.object_type == AOT_AAC_LTP) {
01991 if (che->ch[0].ics.predictor_present) {
01992 if (che->ch[0].ics.ltp.present)
01993 apply_ltp(ac, &che->ch[0]);
01994 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
01995 apply_ltp(ac, &che->ch[1]);
01996 }
01997 }
01998 if (che->ch[0].tns.present)
01999 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
02000 if (che->ch[1].tns.present)
02001 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
02002 if (type <= TYPE_CPE)
02003 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
02004 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
02005 imdct_and_windowing(ac, &che->ch[0]);
02006 if (ac->m4ac.object_type == AOT_AAC_LTP)
02007 update_ltp(ac, &che->ch[0]);
02008 if (type == TYPE_CPE) {
02009 imdct_and_windowing(ac, &che->ch[1]);
02010 if (ac->m4ac.object_type == AOT_AAC_LTP)
02011 update_ltp(ac, &che->ch[1]);
02012 }
02013 if (ac->m4ac.sbr > 0) {
02014 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
02015 }
02016 }
02017 if (type <= TYPE_CCE)
02018 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
02019 }
02020 }
02021 }
02022 }
02023
02024 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
02025 {
02026 int size;
02027 AACADTSHeaderInfo hdr_info;
02028
02029 size = ff_aac_parse_header(gb, &hdr_info);
02030 if (size > 0) {
02031 if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
02032 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02033 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02034 ac->m4ac.chan_config = hdr_info.chan_config;
02035 if (set_default_channel_config(ac->avctx, new_che_pos, hdr_info.chan_config))
02036 return -7;
02037 if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
02038 return -7;
02039 } else if (ac->output_configured != OC_LOCKED) {
02040 ac->output_configured = OC_NONE;
02041 }
02042 if (ac->output_configured != OC_LOCKED) {
02043 ac->m4ac.sbr = -1;
02044 ac->m4ac.ps = -1;
02045 }
02046 ac->m4ac.sample_rate = hdr_info.sample_rate;
02047 ac->m4ac.sampling_index = hdr_info.sampling_index;
02048 ac->m4ac.object_type = hdr_info.object_type;
02049 if (!ac->avctx->sample_rate)
02050 ac->avctx->sample_rate = hdr_info.sample_rate;
02051 if (hdr_info.num_aac_frames == 1) {
02052 if (!hdr_info.crc_absent)
02053 skip_bits(gb, 16);
02054 } else {
02055 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
02056 return -1;
02057 }
02058 }
02059 return size;
02060 }
02061
02062 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
02063 int *data_size, GetBitContext *gb)
02064 {
02065 AACContext *ac = avctx->priv_data;
02066 ChannelElement *che = NULL, *che_prev = NULL;
02067 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
02068 int err, elem_id, data_size_tmp;
02069 int samples = 0, multiplier;
02070
02071 if (show_bits(gb, 12) == 0xfff) {
02072 if (parse_adts_frame_header(ac, gb) < 0) {
02073 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
02074 return -1;
02075 }
02076 if (ac->m4ac.sampling_index > 12) {
02077 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
02078 return -1;
02079 }
02080 }
02081
02082 ac->tags_mapped = 0;
02083
02084 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
02085 elem_id = get_bits(gb, 4);
02086
02087 if (elem_type < TYPE_DSE) {
02088 if (!(che=get_che(ac, elem_type, elem_id))) {
02089 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
02090 elem_type, elem_id);
02091 return -1;
02092 }
02093 samples = 1024;
02094 }
02095
02096 switch (elem_type) {
02097
02098 case TYPE_SCE:
02099 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02100 break;
02101
02102 case TYPE_CPE:
02103 err = decode_cpe(ac, gb, che);
02104 break;
02105
02106 case TYPE_CCE:
02107 err = decode_cce(ac, gb, che);
02108 break;
02109
02110 case TYPE_LFE:
02111 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02112 break;
02113
02114 case TYPE_DSE:
02115 err = skip_data_stream_element(ac, gb);
02116 break;
02117
02118 case TYPE_PCE: {
02119 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
02120 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
02121 if ((err = decode_pce(avctx, &ac->m4ac, new_che_pos, gb)))
02122 break;
02123 if (ac->output_configured > OC_TRIAL_PCE)
02124 av_log(avctx, AV_LOG_ERROR,
02125 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02126 else
02127 err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
02128 break;
02129 }
02130
02131 case TYPE_FIL:
02132 if (elem_id == 15)
02133 elem_id += get_bits(gb, 8) - 1;
02134 if (get_bits_left(gb) < 8 * elem_id) {
02135 av_log(avctx, AV_LOG_ERROR, overread_err);
02136 return -1;
02137 }
02138 while (elem_id > 0)
02139 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
02140 err = 0;
02141 break;
02142
02143 default:
02144 err = -1;
02145 break;
02146 }
02147
02148 che_prev = che;
02149 elem_type_prev = elem_type;
02150
02151 if (err)
02152 return err;
02153
02154 if (get_bits_left(gb) < 3) {
02155 av_log(avctx, AV_LOG_ERROR, overread_err);
02156 return -1;
02157 }
02158 }
02159
02160 spectral_to_sample(ac);
02161
02162 multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
02163 samples <<= multiplier;
02164 if (ac->output_configured < OC_LOCKED) {
02165 avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
02166 avctx->frame_size = samples;
02167 }
02168
02169 data_size_tmp = samples * avctx->channels * sizeof(int16_t);
02170 if (*data_size < data_size_tmp) {
02171 av_log(avctx, AV_LOG_ERROR,
02172 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
02173 *data_size, data_size_tmp);
02174 return -1;
02175 }
02176 *data_size = data_size_tmp;
02177
02178 if (samples)
02179 ac->fmt_conv.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
02180
02181 if (ac->output_configured)
02182 ac->output_configured = OC_LOCKED;
02183
02184 return 0;
02185 }
02186
02187 static int aac_decode_frame(AVCodecContext *avctx, void *data,
02188 int *data_size, AVPacket *avpkt)
02189 {
02190 const uint8_t *buf = avpkt->data;
02191 int buf_size = avpkt->size;
02192 GetBitContext gb;
02193 int buf_consumed;
02194 int buf_offset;
02195 int err;
02196
02197 init_get_bits(&gb, buf, buf_size * 8);
02198
02199 if ((err = aac_decode_frame_int(avctx, data, data_size, &gb)) < 0)
02200 return err;
02201
02202 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02203 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02204 if (buf[buf_offset])
02205 break;
02206
02207 return buf_size > buf_offset ? buf_consumed : buf_size;
02208 }
02209
02210 static av_cold int aac_decode_close(AVCodecContext *avctx)
02211 {
02212 AACContext *ac = avctx->priv_data;
02213 int i, type;
02214
02215 for (i = 0; i < MAX_ELEM_ID; i++) {
02216 for (type = 0; type < 4; type++) {
02217 if (ac->che[type][i])
02218 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02219 av_freep(&ac->che[type][i]);
02220 }
02221 }
02222
02223 ff_mdct_end(&ac->mdct);
02224 ff_mdct_end(&ac->mdct_small);
02225 ff_mdct_end(&ac->mdct_ltp);
02226 return 0;
02227 }
02228
02229
02230 #define LOAS_SYNC_WORD 0x2b7
02231
02232 struct LATMContext {
02233 AACContext aac_ctx;
02234 int initialized;
02235
02236
02237 int audio_mux_version_A;
02238 int frame_length_type;
02239 int frame_length;
02240 };
02241
02242 static inline uint32_t latm_get_value(GetBitContext *b)
02243 {
02244 int length = get_bits(b, 2);
02245
02246 return get_bits_long(b, (length+1)*8);
02247 }
02248
02249 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
02250 GetBitContext *gb)
02251 {
02252 AVCodecContext *avctx = latmctx->aac_ctx.avctx;
02253 int config_start_bit = get_bits_count(gb);
02254 int bits_consumed, esize;
02255
02256 if (config_start_bit % 8) {
02257 av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
02258 "config not byte aligned.\n", 1);
02259 return AVERROR_INVALIDDATA;
02260 } else {
02261 bits_consumed =
02262 decode_audio_specific_config(&latmctx->aac_ctx, avctx,
02263 &latmctx->aac_ctx.m4ac,
02264 gb->buffer + (config_start_bit / 8),
02265 get_bits_left(gb) / 8);
02266
02267 if (bits_consumed < 0)
02268 return AVERROR_INVALIDDATA;
02269
02270 esize = (bits_consumed+7) / 8;
02271
02272 if (avctx->extradata_size <= esize) {
02273 av_free(avctx->extradata);
02274 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
02275 if (!avctx->extradata)
02276 return AVERROR(ENOMEM);
02277 }
02278
02279 avctx->extradata_size = esize;
02280 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
02281 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
02282
02283 skip_bits_long(gb, bits_consumed);
02284 }
02285
02286 return bits_consumed;
02287 }
02288
02289 static int read_stream_mux_config(struct LATMContext *latmctx,
02290 GetBitContext *gb)
02291 {
02292 int ret, audio_mux_version = get_bits(gb, 1);
02293
02294 latmctx->audio_mux_version_A = 0;
02295 if (audio_mux_version)
02296 latmctx->audio_mux_version_A = get_bits(gb, 1);
02297
02298 if (!latmctx->audio_mux_version_A) {
02299
02300 if (audio_mux_version)
02301 latm_get_value(gb);
02302
02303 skip_bits(gb, 1);
02304 skip_bits(gb, 6);
02305
02306 if (get_bits(gb, 4)) {
02307 av_log_missing_feature(latmctx->aac_ctx.avctx,
02308 "multiple programs are not supported\n", 1);
02309 return AVERROR_PATCHWELCOME;
02310 }
02311
02312
02313
02314
02315 if (get_bits(gb, 3)) {
02316 av_log_missing_feature(latmctx->aac_ctx.avctx,
02317 "multiple layers are not supported\n", 1);
02318 return AVERROR_PATCHWELCOME;
02319 }
02320
02321
02322 if (!audio_mux_version) {
02323 if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
02324 return ret;
02325 } else {
02326 int ascLen = latm_get_value(gb);
02327 if ((ret = latm_decode_audio_specific_config(latmctx, gb)) < 0)
02328 return ret;
02329 ascLen -= ret;
02330 skip_bits_long(gb, ascLen);
02331 }
02332
02333 latmctx->frame_length_type = get_bits(gb, 3);
02334 switch (latmctx->frame_length_type) {
02335 case 0:
02336 skip_bits(gb, 8);
02337 break;
02338 case 1:
02339 latmctx->frame_length = get_bits(gb, 9);
02340 break;
02341 case 3:
02342 case 4:
02343 case 5:
02344 skip_bits(gb, 6);
02345 break;
02346 case 6:
02347 case 7:
02348 skip_bits(gb, 1);
02349 break;
02350 }
02351
02352 if (get_bits(gb, 1)) {
02353 if (audio_mux_version) {
02354 latm_get_value(gb);
02355 } else {
02356 int esc;
02357 do {
02358 esc = get_bits(gb, 1);
02359 skip_bits(gb, 8);
02360 } while (esc);
02361 }
02362 }
02363
02364 if (get_bits(gb, 1))
02365 skip_bits(gb, 8);
02366 }
02367
02368 return 0;
02369 }
02370
02371 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
02372 {
02373 uint8_t tmp;
02374
02375 if (ctx->frame_length_type == 0) {
02376 int mux_slot_length = 0;
02377 do {
02378 tmp = get_bits(gb, 8);
02379 mux_slot_length += tmp;
02380 } while (tmp == 255);
02381 return mux_slot_length;
02382 } else if (ctx->frame_length_type == 1) {
02383 return ctx->frame_length;
02384 } else if (ctx->frame_length_type == 3 ||
02385 ctx->frame_length_type == 5 ||
02386 ctx->frame_length_type == 7) {
02387 skip_bits(gb, 2);
02388 }
02389 return 0;
02390 }
02391
02392 static int read_audio_mux_element(struct LATMContext *latmctx,
02393 GetBitContext *gb)
02394 {
02395 int err;
02396 uint8_t use_same_mux = get_bits(gb, 1);
02397 if (!use_same_mux) {
02398 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
02399 return err;
02400 } else if (!latmctx->aac_ctx.avctx->extradata) {
02401 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
02402 "no decoder config found\n");
02403 return AVERROR(EAGAIN);
02404 }
02405 if (latmctx->audio_mux_version_A == 0) {
02406 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
02407 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
02408 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
02409 return AVERROR_INVALIDDATA;
02410 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
02411 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02412 "frame length mismatch %d << %d\n",
02413 mux_slot_length_bytes * 8, get_bits_left(gb));
02414 return AVERROR_INVALIDDATA;
02415 }
02416 }
02417 return 0;
02418 }
02419
02420
02421 static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
02422 AVPacket *avpkt)
02423 {
02424 struct LATMContext *latmctx = avctx->priv_data;
02425 int muxlength, err;
02426 GetBitContext gb;
02427
02428 if (avpkt->size == 0)
02429 return 0;
02430
02431 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
02432
02433
02434 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
02435 return AVERROR_INVALIDDATA;
02436
02437 muxlength = get_bits(&gb, 13) + 3;
02438
02439 if (muxlength > avpkt->size)
02440 return AVERROR_INVALIDDATA;
02441
02442 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
02443 return err;
02444
02445 if (!latmctx->initialized) {
02446 if (!avctx->extradata) {
02447 *out_size = 0;
02448 return avpkt->size;
02449 } else {
02450 if ((err = aac_decode_init(avctx)) < 0)
02451 return err;
02452 latmctx->initialized = 1;
02453 }
02454 }
02455
02456 if (show_bits(&gb, 12) == 0xfff) {
02457 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02458 "ADTS header detected, probably as result of configuration "
02459 "misparsing\n");
02460 return AVERROR_INVALIDDATA;
02461 }
02462
02463 if ((err = aac_decode_frame_int(avctx, out, out_size, &gb)) < 0)
02464 return err;
02465
02466 return muxlength;
02467 }
02468
02469 av_cold static int latm_decode_init(AVCodecContext *avctx)
02470 {
02471 struct LATMContext *latmctx = avctx->priv_data;
02472 int ret;
02473
02474 ret = aac_decode_init(avctx);
02475
02476 if (avctx->extradata_size > 0) {
02477 latmctx->initialized = !ret;
02478 } else {
02479 latmctx->initialized = 0;
02480 }
02481
02482 return ret;
02483 }
02484
02485
02486 AVCodec ff_aac_decoder = {
02487 "aac",
02488 AVMEDIA_TYPE_AUDIO,
02489 CODEC_ID_AAC,
02490 sizeof(AACContext),
02491 aac_decode_init,
02492 NULL,
02493 aac_decode_close,
02494 aac_decode_frame,
02495 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
02496 .sample_fmts = (const enum AVSampleFormat[]) {
02497 AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
02498 },
02499 .channel_layouts = aac_channel_layout,
02500 };
02501
02502
02503
02504
02505
02506
02507 AVCodec ff_aac_latm_decoder = {
02508 .name = "aac_latm",
02509 .type = AVMEDIA_TYPE_AUDIO,
02510 .id = CODEC_ID_AAC_LATM,
02511 .priv_data_size = sizeof(struct LATMContext),
02512 .init = latm_decode_init,
02513 .close = aac_decode_close,
02514 .decode = latm_decode_frame,
02515 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
02516 .sample_fmts = (const enum AVSampleFormat[]) {
02517 AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE
02518 },
02519 .channel_layouts = aac_channel_layout,
02520 };