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