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 #include "libavutil/float_dsp.h"
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 #include "libavutil/intfloat.h"
00102
00103 #include <assert.h>
00104 #include <errno.h>
00105 #include <math.h>
00106 #include <string.h>
00107
00108 #if ARCH_ARM
00109 # include "arm/aac.h"
00110 #endif
00111
00112 static VLC vlc_scalefactors;
00113 static VLC vlc_spectral[11];
00114
00115 #define overread_err "Input buffer exhausted before END element found\n"
00116
00117 static int count_channels(uint8_t (*layout)[3], int tags)
00118 {
00119 int i, sum = 0;
00120 for (i = 0; i < tags; i++) {
00121 int syn_ele = layout[i][0];
00122 int pos = layout[i][2];
00123 sum += (1 + (syn_ele == TYPE_CPE)) *
00124 (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
00125 }
00126 return sum;
00127 }
00128
00141 static av_cold int che_configure(AACContext *ac,
00142 enum ChannelPosition che_pos,
00143 int type, int id, int *channels)
00144 {
00145 if (che_pos) {
00146 if (!ac->che[type][id]) {
00147 if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
00148 return AVERROR(ENOMEM);
00149 ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
00150 }
00151 if (type != TYPE_CCE) {
00152 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
00153 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
00154 return AVERROR_INVALIDDATA;
00155 }
00156 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
00157 if (type == TYPE_CPE ||
00158 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
00159 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
00160 }
00161 }
00162 } else {
00163 if (ac->che[type][id])
00164 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
00165 av_freep(&ac->che[type][id]);
00166 }
00167 return 0;
00168 }
00169
00170 static int frame_configure_elements(AVCodecContext *avctx)
00171 {
00172 AACContext *ac = avctx->priv_data;
00173 int type, id, ch, ret;
00174
00175
00176 for (type = 0; type < 4; type++) {
00177 for (id = 0; id < MAX_ELEM_ID; id++) {
00178 ChannelElement *che = ac->che[type][id];
00179 if (che) {
00180 che->ch[0].ret = che->ch[0].ret_buf;
00181 che->ch[1].ret = che->ch[1].ret_buf;
00182 }
00183 }
00184 }
00185
00186
00187 ac->frame.nb_samples = 2048;
00188 if ((ret = ff_get_buffer(avctx, &ac->frame)) < 0) {
00189 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00190 return ret;
00191 }
00192
00193
00194 for (ch = 0; ch < avctx->channels; ch++) {
00195 if (ac->output_element[ch])
00196 ac->output_element[ch]->ret = (float *)ac->frame.extended_data[ch];
00197 }
00198
00199 return 0;
00200 }
00201
00202 struct elem_to_channel {
00203 uint64_t av_position;
00204 uint8_t syn_ele;
00205 uint8_t elem_id;
00206 uint8_t aac_position;
00207 };
00208
00209 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
00210 uint8_t (*layout_map)[3], int offset, uint64_t left,
00211 uint64_t right, int pos)
00212 {
00213 if (layout_map[offset][0] == TYPE_CPE) {
00214 e2c_vec[offset] = (struct elem_to_channel) {
00215 .av_position = left | right, .syn_ele = TYPE_CPE,
00216 .elem_id = layout_map[offset ][1], .aac_position = pos };
00217 return 1;
00218 } else {
00219 e2c_vec[offset] = (struct elem_to_channel) {
00220 .av_position = left, .syn_ele = TYPE_SCE,
00221 .elem_id = layout_map[offset ][1], .aac_position = pos };
00222 e2c_vec[offset + 1] = (struct elem_to_channel) {
00223 .av_position = right, .syn_ele = TYPE_SCE,
00224 .elem_id = layout_map[offset + 1][1], .aac_position = pos };
00225 return 2;
00226 }
00227 }
00228
00229 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos, int *current) {
00230 int num_pos_channels = 0;
00231 int first_cpe = 0;
00232 int sce_parity = 0;
00233 int i;
00234 for (i = *current; i < tags; i++) {
00235 if (layout_map[i][2] != pos)
00236 break;
00237 if (layout_map[i][0] == TYPE_CPE) {
00238 if (sce_parity) {
00239 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
00240 sce_parity = 0;
00241 } else {
00242 return -1;
00243 }
00244 }
00245 num_pos_channels += 2;
00246 first_cpe = 1;
00247 } else {
00248 num_pos_channels++;
00249 sce_parity ^= 1;
00250 }
00251 }
00252 if (sce_parity &&
00253 ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
00254 return -1;
00255 *current = i;
00256 return num_pos_channels;
00257 }
00258
00259 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
00260 {
00261 int i, n, total_non_cc_elements;
00262 struct elem_to_channel e2c_vec[4*MAX_ELEM_ID] = {{ 0 }};
00263 int num_front_channels, num_side_channels, num_back_channels;
00264 uint64_t layout;
00265
00266 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
00267 return 0;
00268
00269 i = 0;
00270 num_front_channels =
00271 count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
00272 if (num_front_channels < 0)
00273 return 0;
00274 num_side_channels =
00275 count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
00276 if (num_side_channels < 0)
00277 return 0;
00278 num_back_channels =
00279 count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
00280 if (num_back_channels < 0)
00281 return 0;
00282
00283 i = 0;
00284 if (num_front_channels & 1) {
00285 e2c_vec[i] = (struct elem_to_channel) {
00286 .av_position = AV_CH_FRONT_CENTER, .syn_ele = TYPE_SCE,
00287 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_FRONT };
00288 i++;
00289 num_front_channels--;
00290 }
00291 if (num_front_channels >= 4) {
00292 i += assign_pair(e2c_vec, layout_map, i,
00293 AV_CH_FRONT_LEFT_OF_CENTER,
00294 AV_CH_FRONT_RIGHT_OF_CENTER,
00295 AAC_CHANNEL_FRONT);
00296 num_front_channels -= 2;
00297 }
00298 if (num_front_channels >= 2) {
00299 i += assign_pair(e2c_vec, layout_map, i,
00300 AV_CH_FRONT_LEFT,
00301 AV_CH_FRONT_RIGHT,
00302 AAC_CHANNEL_FRONT);
00303 num_front_channels -= 2;
00304 }
00305 while (num_front_channels >= 2) {
00306 i += assign_pair(e2c_vec, layout_map, i,
00307 UINT64_MAX,
00308 UINT64_MAX,
00309 AAC_CHANNEL_FRONT);
00310 num_front_channels -= 2;
00311 }
00312
00313 if (num_side_channels >= 2) {
00314 i += assign_pair(e2c_vec, layout_map, i,
00315 AV_CH_SIDE_LEFT,
00316 AV_CH_SIDE_RIGHT,
00317 AAC_CHANNEL_FRONT);
00318 num_side_channels -= 2;
00319 }
00320 while (num_side_channels >= 2) {
00321 i += assign_pair(e2c_vec, layout_map, i,
00322 UINT64_MAX,
00323 UINT64_MAX,
00324 AAC_CHANNEL_SIDE);
00325 num_side_channels -= 2;
00326 }
00327
00328 while (num_back_channels >= 4) {
00329 i += assign_pair(e2c_vec, layout_map, i,
00330 UINT64_MAX,
00331 UINT64_MAX,
00332 AAC_CHANNEL_BACK);
00333 num_back_channels -= 2;
00334 }
00335 if (num_back_channels >= 2) {
00336 i += assign_pair(e2c_vec, layout_map, i,
00337 AV_CH_BACK_LEFT,
00338 AV_CH_BACK_RIGHT,
00339 AAC_CHANNEL_BACK);
00340 num_back_channels -= 2;
00341 }
00342 if (num_back_channels) {
00343 e2c_vec[i] = (struct elem_to_channel) {
00344 .av_position = AV_CH_BACK_CENTER, .syn_ele = TYPE_SCE,
00345 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_BACK };
00346 i++;
00347 num_back_channels--;
00348 }
00349
00350 if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
00351 e2c_vec[i] = (struct elem_to_channel) {
00352 .av_position = AV_CH_LOW_FREQUENCY, .syn_ele = TYPE_LFE,
00353 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
00354 i++;
00355 }
00356 while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
00357 e2c_vec[i] = (struct elem_to_channel) {
00358 .av_position = UINT64_MAX, .syn_ele = TYPE_LFE,
00359 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
00360 i++;
00361 }
00362
00363
00364 total_non_cc_elements = n = i;
00365 do {
00366 int next_n = 0;
00367 for (i = 1; i < n; i++) {
00368 if (e2c_vec[i-1].av_position > e2c_vec[i].av_position) {
00369 FFSWAP(struct elem_to_channel, e2c_vec[i-1], e2c_vec[i]);
00370 next_n = i;
00371 }
00372 }
00373 n = next_n;
00374 } while (n > 0);
00375
00376 layout = 0;
00377 for (i = 0; i < total_non_cc_elements; i++) {
00378 layout_map[i][0] = e2c_vec[i].syn_ele;
00379 layout_map[i][1] = e2c_vec[i].elem_id;
00380 layout_map[i][2] = e2c_vec[i].aac_position;
00381 if (e2c_vec[i].av_position != UINT64_MAX) {
00382 layout |= e2c_vec[i].av_position;
00383 }
00384 }
00385
00386 return layout;
00387 }
00388
00392 static void push_output_configuration(AACContext *ac) {
00393 if (ac->oc[1].status == OC_LOCKED) {
00394 ac->oc[0] = ac->oc[1];
00395 }
00396 ac->oc[1].status = OC_NONE;
00397 }
00398
00403 static void pop_output_configuration(AACContext *ac) {
00404 if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
00405 ac->oc[1] = ac->oc[0];
00406 ac->avctx->channels = ac->oc[1].channels;
00407 ac->avctx->channel_layout = ac->oc[1].channel_layout;
00408 }
00409 }
00410
00416 static int output_configure(AACContext *ac,
00417 uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
00418 enum OCStatus oc_type, int get_new_frame)
00419 {
00420 AVCodecContext *avctx = ac->avctx;
00421 int i, channels = 0, ret;
00422 uint64_t layout = 0;
00423
00424 if (ac->oc[1].layout_map != layout_map) {
00425 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
00426 ac->oc[1].layout_map_tags = tags;
00427 }
00428
00429
00430
00431 if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
00432 layout = sniff_channel_order(layout_map, tags);
00433 for (i = 0; i < tags; i++) {
00434 int type = layout_map[i][0];
00435 int id = layout_map[i][1];
00436 int position = layout_map[i][2];
00437
00438
00439 ret = che_configure(ac, position, type, id, &channels);
00440 if (ret < 0)
00441 return ret;
00442 }
00443 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
00444 if (layout == AV_CH_FRONT_CENTER) {
00445 layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
00446 } else {
00447 layout = 0;
00448 }
00449 }
00450
00451 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
00452 if (layout) avctx->channel_layout = layout;
00453 ac->oc[1].channel_layout = layout;
00454 avctx->channels = ac->oc[1].channels = channels;
00455 ac->oc[1].status = oc_type;
00456
00457 if (get_new_frame) {
00458 if ((ret = frame_configure_elements(ac->avctx)) < 0)
00459 return ret;
00460 }
00461
00462 return 0;
00463 }
00464
00465 static void flush(AVCodecContext *avctx)
00466 {
00467 AACContext *ac= avctx->priv_data;
00468 int type, i, j;
00469
00470 for (type = 3; type >= 0; type--) {
00471 for (i = 0; i < MAX_ELEM_ID; i++) {
00472 ChannelElement *che = ac->che[type][i];
00473 if (che) {
00474 for (j = 0; j <= 1; j++) {
00475 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
00476 }
00477 }
00478 }
00479 }
00480 }
00481
00488 static int set_default_channel_config(AVCodecContext *avctx,
00489 uint8_t (*layout_map)[3],
00490 int *tags,
00491 int channel_config)
00492 {
00493 if (channel_config < 1 || channel_config > 7) {
00494 av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
00495 channel_config);
00496 return -1;
00497 }
00498 *tags = tags_per_config[channel_config];
00499 memcpy(layout_map, aac_channel_layout_map[channel_config-1], *tags * sizeof(*layout_map));
00500 return 0;
00501 }
00502
00503 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
00504 {
00505
00506 if (!ac->oc[1].m4ac.chan_config) {
00507 return ac->tag_che_map[type][elem_id];
00508 }
00509
00510 if (!ac->tags_mapped && type == TYPE_CPE && ac->oc[1].m4ac.chan_config == 1) {
00511 uint8_t layout_map[MAX_ELEM_ID*4][3];
00512 int layout_map_tags;
00513 push_output_configuration(ac);
00514
00515 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
00516
00517 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
00518 2) < 0)
00519 return NULL;
00520 if (output_configure(ac, layout_map, layout_map_tags,
00521 OC_TRIAL_FRAME, 1) < 0)
00522 return NULL;
00523
00524 ac->oc[1].m4ac.chan_config = 2;
00525 ac->oc[1].m4ac.ps = 0;
00526 }
00527
00528 if (!ac->tags_mapped && type == TYPE_SCE && ac->oc[1].m4ac.chan_config == 2) {
00529 uint8_t layout_map[MAX_ELEM_ID*4][3];
00530 int layout_map_tags;
00531 push_output_configuration(ac);
00532
00533 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
00534
00535 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
00536 1) < 0)
00537 return NULL;
00538 if (output_configure(ac, layout_map, layout_map_tags,
00539 OC_TRIAL_FRAME, 1) < 0)
00540 return NULL;
00541
00542 ac->oc[1].m4ac.chan_config = 1;
00543 if (ac->oc[1].m4ac.sbr)
00544 ac->oc[1].m4ac.ps = -1;
00545 }
00546
00547 switch (ac->oc[1].m4ac.chan_config) {
00548 case 7:
00549 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
00550 ac->tags_mapped++;
00551 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
00552 }
00553 case 6:
00554
00555
00556
00557 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
00558 ac->tags_mapped++;
00559 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
00560 }
00561 case 5:
00562 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
00563 ac->tags_mapped++;
00564 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
00565 }
00566 case 4:
00567 if (ac->tags_mapped == 2 && ac->oc[1].m4ac.chan_config == 4 && type == TYPE_SCE) {
00568 ac->tags_mapped++;
00569 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
00570 }
00571 case 3:
00572 case 2:
00573 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) && type == TYPE_CPE) {
00574 ac->tags_mapped++;
00575 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
00576 } else if (ac->oc[1].m4ac.chan_config == 2) {
00577 return NULL;
00578 }
00579 case 1:
00580 if (!ac->tags_mapped && type == TYPE_SCE) {
00581 ac->tags_mapped++;
00582 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
00583 }
00584 default:
00585 return NULL;
00586 }
00587 }
00588
00594 static void decode_channel_map(uint8_t layout_map[][3],
00595 enum ChannelPosition type,
00596 GetBitContext *gb, int n)
00597 {
00598 while (n--) {
00599 enum RawDataBlockType syn_ele;
00600 switch (type) {
00601 case AAC_CHANNEL_FRONT:
00602 case AAC_CHANNEL_BACK:
00603 case AAC_CHANNEL_SIDE:
00604 syn_ele = get_bits1(gb);
00605 break;
00606 case AAC_CHANNEL_CC:
00607 skip_bits1(gb);
00608 syn_ele = TYPE_CCE;
00609 break;
00610 case AAC_CHANNEL_LFE:
00611 syn_ele = TYPE_LFE;
00612 break;
00613 default:
00614 av_assert0(0);
00615 }
00616 layout_map[0][0] = syn_ele;
00617 layout_map[0][1] = get_bits(gb, 4);
00618 layout_map[0][2] = type;
00619 layout_map++;
00620 }
00621 }
00622
00628 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
00629 uint8_t (*layout_map)[3],
00630 GetBitContext *gb)
00631 {
00632 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
00633 int comment_len;
00634 int tags;
00635
00636 skip_bits(gb, 2);
00637
00638 sampling_index = get_bits(gb, 4);
00639 if (m4ac->sampling_index != sampling_index)
00640 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");
00641
00642 num_front = get_bits(gb, 4);
00643 num_side = get_bits(gb, 4);
00644 num_back = get_bits(gb, 4);
00645 num_lfe = get_bits(gb, 2);
00646 num_assoc_data = get_bits(gb, 3);
00647 num_cc = get_bits(gb, 4);
00648
00649 if (get_bits1(gb))
00650 skip_bits(gb, 4);
00651 if (get_bits1(gb))
00652 skip_bits(gb, 4);
00653
00654 if (get_bits1(gb))
00655 skip_bits(gb, 3);
00656
00657 if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
00658 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
00659 return -1;
00660 }
00661 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
00662 tags = num_front;
00663 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
00664 tags += num_side;
00665 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
00666 tags += num_back;
00667 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
00668 tags += num_lfe;
00669
00670 skip_bits_long(gb, 4 * num_assoc_data);
00671
00672 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
00673 tags += num_cc;
00674
00675 align_get_bits(gb);
00676
00677
00678 comment_len = get_bits(gb, 8) * 8;
00679 if (get_bits_left(gb) < comment_len) {
00680 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
00681 return -1;
00682 }
00683 skip_bits_long(gb, comment_len);
00684 return tags;
00685 }
00686
00695 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
00696 GetBitContext *gb,
00697 MPEG4AudioConfig *m4ac,
00698 int channel_config)
00699 {
00700 int extension_flag, ret;
00701 uint8_t layout_map[MAX_ELEM_ID*4][3];
00702 int tags = 0;
00703
00704 if (get_bits1(gb)) {
00705 av_log_missing_feature(avctx, "960/120 MDCT window", 1);
00706 return AVERROR_PATCHWELCOME;
00707 }
00708
00709 if (get_bits1(gb))
00710 skip_bits(gb, 14);
00711 extension_flag = get_bits1(gb);
00712
00713 if (m4ac->object_type == AOT_AAC_SCALABLE ||
00714 m4ac->object_type == AOT_ER_AAC_SCALABLE)
00715 skip_bits(gb, 3);
00716
00717 if (channel_config == 0) {
00718 skip_bits(gb, 4);
00719 tags = decode_pce(avctx, m4ac, layout_map, gb);
00720 if (tags < 0)
00721 return tags;
00722 } else {
00723 if ((ret = set_default_channel_config(avctx, layout_map, &tags, channel_config)))
00724 return ret;
00725 }
00726
00727 if (count_channels(layout_map, tags) > 1) {
00728 m4ac->ps = 0;
00729 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
00730 m4ac->ps = 1;
00731
00732 if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
00733 return ret;
00734
00735 if (extension_flag) {
00736 switch (m4ac->object_type) {
00737 case AOT_ER_BSAC:
00738 skip_bits(gb, 5);
00739 skip_bits(gb, 11);
00740 break;
00741 case AOT_ER_AAC_LC:
00742 case AOT_ER_AAC_LTP:
00743 case AOT_ER_AAC_SCALABLE:
00744 case AOT_ER_AAC_LD:
00745 skip_bits(gb, 3);
00746
00747
00748
00749 break;
00750 }
00751 skip_bits1(gb);
00752 }
00753 return 0;
00754 }
00755
00768 static int decode_audio_specific_config(AACContext *ac,
00769 AVCodecContext *avctx,
00770 MPEG4AudioConfig *m4ac,
00771 const uint8_t *data, int bit_size,
00772 int sync_extension)
00773 {
00774 GetBitContext gb;
00775 int i;
00776
00777 av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
00778 for (i = 0; i < bit_size >> 3; i++)
00779 av_dlog(avctx, "%02x ", data[i]);
00780 av_dlog(avctx, "\n");
00781
00782 init_get_bits(&gb, data, bit_size);
00783
00784 if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
00785 return -1;
00786 if (m4ac->sampling_index > 12) {
00787 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
00788 return -1;
00789 }
00790
00791 skip_bits_long(&gb, i);
00792
00793 switch (m4ac->object_type) {
00794 case AOT_AAC_MAIN:
00795 case AOT_AAC_LC:
00796 case AOT_AAC_LTP:
00797 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
00798 return -1;
00799 break;
00800 default:
00801 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
00802 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
00803 return -1;
00804 }
00805
00806 av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
00807 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
00808 m4ac->sample_rate, m4ac->sbr, m4ac->ps);
00809
00810 return get_bits_count(&gb);
00811 }
00812
00820 static av_always_inline int lcg_random(unsigned previous_val)
00821 {
00822 union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
00823 return v.s;
00824 }
00825
00826 static av_always_inline void reset_predict_state(PredictorState *ps)
00827 {
00828 ps->r0 = 0.0f;
00829 ps->r1 = 0.0f;
00830 ps->cor0 = 0.0f;
00831 ps->cor1 = 0.0f;
00832 ps->var0 = 1.0f;
00833 ps->var1 = 1.0f;
00834 }
00835
00836 static void reset_all_predictors(PredictorState *ps)
00837 {
00838 int i;
00839 for (i = 0; i < MAX_PREDICTORS; i++)
00840 reset_predict_state(&ps[i]);
00841 }
00842
00843 static int sample_rate_idx (int rate)
00844 {
00845 if (92017 <= rate) return 0;
00846 else if (75132 <= rate) return 1;
00847 else if (55426 <= rate) return 2;
00848 else if (46009 <= rate) return 3;
00849 else if (37566 <= rate) return 4;
00850 else if (27713 <= rate) return 5;
00851 else if (23004 <= rate) return 6;
00852 else if (18783 <= rate) return 7;
00853 else if (13856 <= rate) return 8;
00854 else if (11502 <= rate) return 9;
00855 else if (9391 <= rate) return 10;
00856 else return 11;
00857 }
00858
00859 static void reset_predictor_group(PredictorState *ps, int group_num)
00860 {
00861 int i;
00862 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
00863 reset_predict_state(&ps[i]);
00864 }
00865
00866 #define AAC_INIT_VLC_STATIC(num, size) \
00867 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
00868 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
00869 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
00870 size);
00871
00872 static av_cold int aac_decode_init(AVCodecContext *avctx)
00873 {
00874 AACContext *ac = avctx->priv_data;
00875
00876 ac->avctx = avctx;
00877 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
00878
00879 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
00880
00881 if (avctx->extradata_size > 0) {
00882 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
00883 avctx->extradata,
00884 avctx->extradata_size*8, 1) < 0)
00885 return -1;
00886 } else {
00887 int sr, i;
00888 uint8_t layout_map[MAX_ELEM_ID*4][3];
00889 int layout_map_tags;
00890
00891 sr = sample_rate_idx(avctx->sample_rate);
00892 ac->oc[1].m4ac.sampling_index = sr;
00893 ac->oc[1].m4ac.channels = avctx->channels;
00894 ac->oc[1].m4ac.sbr = -1;
00895 ac->oc[1].m4ac.ps = -1;
00896
00897 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
00898 if (ff_mpeg4audio_channels[i] == avctx->channels)
00899 break;
00900 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
00901 i = 0;
00902 }
00903 ac->oc[1].m4ac.chan_config = i;
00904
00905 if (ac->oc[1].m4ac.chan_config) {
00906 int ret = set_default_channel_config(avctx, layout_map,
00907 &layout_map_tags, ac->oc[1].m4ac.chan_config);
00908 if (!ret)
00909 output_configure(ac, layout_map, layout_map_tags,
00910 OC_GLOBAL_HDR, 0);
00911 else if (avctx->err_recognition & AV_EF_EXPLODE)
00912 return AVERROR_INVALIDDATA;
00913 }
00914 }
00915
00916 AAC_INIT_VLC_STATIC( 0, 304);
00917 AAC_INIT_VLC_STATIC( 1, 270);
00918 AAC_INIT_VLC_STATIC( 2, 550);
00919 AAC_INIT_VLC_STATIC( 3, 300);
00920 AAC_INIT_VLC_STATIC( 4, 328);
00921 AAC_INIT_VLC_STATIC( 5, 294);
00922 AAC_INIT_VLC_STATIC( 6, 306);
00923 AAC_INIT_VLC_STATIC( 7, 268);
00924 AAC_INIT_VLC_STATIC( 8, 510);
00925 AAC_INIT_VLC_STATIC( 9, 366);
00926 AAC_INIT_VLC_STATIC(10, 462);
00927
00928 ff_aac_sbr_init();
00929
00930 ff_dsputil_init(&ac->dsp, avctx);
00931 ff_fmt_convert_init(&ac->fmt_conv, avctx);
00932 avpriv_float_dsp_init(&ac->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
00933
00934 ac->random_state = 0x1f2e3d4c;
00935
00936 ff_aac_tableinit();
00937
00938 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
00939 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
00940 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
00941 352);
00942
00943 ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
00944 ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
00945 ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
00946
00947 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
00948 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
00949 ff_init_ff_sine_windows(10);
00950 ff_init_ff_sine_windows( 7);
00951
00952 cbrt_tableinit();
00953
00954 avcodec_get_frame_defaults(&ac->frame);
00955 avctx->coded_frame = &ac->frame;
00956
00957 return 0;
00958 }
00959
00963 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
00964 {
00965 int byte_align = get_bits1(gb);
00966 int count = get_bits(gb, 8);
00967 if (count == 255)
00968 count += get_bits(gb, 8);
00969 if (byte_align)
00970 align_get_bits(gb);
00971
00972 if (get_bits_left(gb) < 8 * count) {
00973 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
00974 return -1;
00975 }
00976 skip_bits_long(gb, 8 * count);
00977 return 0;
00978 }
00979
00980 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
00981 GetBitContext *gb)
00982 {
00983 int sfb;
00984 if (get_bits1(gb)) {
00985 ics->predictor_reset_group = get_bits(gb, 5);
00986 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
00987 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
00988 return -1;
00989 }
00990 }
00991 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
00992 ics->prediction_used[sfb] = get_bits1(gb);
00993 }
00994 return 0;
00995 }
00996
01000 static void decode_ltp(LongTermPrediction *ltp,
01001 GetBitContext *gb, uint8_t max_sfb)
01002 {
01003 int sfb;
01004
01005 ltp->lag = get_bits(gb, 11);
01006 ltp->coef = ltp_coef[get_bits(gb, 3)];
01007 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
01008 ltp->used[sfb] = get_bits1(gb);
01009 }
01010
01014 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
01015 GetBitContext *gb)
01016 {
01017 if (get_bits1(gb)) {
01018 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
01019 return AVERROR_INVALIDDATA;
01020 }
01021 ics->window_sequence[1] = ics->window_sequence[0];
01022 ics->window_sequence[0] = get_bits(gb, 2);
01023 ics->use_kb_window[1] = ics->use_kb_window[0];
01024 ics->use_kb_window[0] = get_bits1(gb);
01025 ics->num_window_groups = 1;
01026 ics->group_len[0] = 1;
01027 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01028 int i;
01029 ics->max_sfb = get_bits(gb, 4);
01030 for (i = 0; i < 7; i++) {
01031 if (get_bits1(gb)) {
01032 ics->group_len[ics->num_window_groups - 1]++;
01033 } else {
01034 ics->num_window_groups++;
01035 ics->group_len[ics->num_window_groups - 1] = 1;
01036 }
01037 }
01038 ics->num_windows = 8;
01039 ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
01040 ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
01041 ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
01042 ics->predictor_present = 0;
01043 } else {
01044 ics->max_sfb = get_bits(gb, 6);
01045 ics->num_windows = 1;
01046 ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
01047 ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
01048 ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
01049 ics->predictor_present = get_bits1(gb);
01050 ics->predictor_reset_group = 0;
01051 if (ics->predictor_present) {
01052 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
01053 if (decode_prediction(ac, ics, gb)) {
01054 goto fail;
01055 }
01056 } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
01057 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
01058 goto fail;
01059 } else {
01060 if ((ics->ltp.present = get_bits(gb, 1)))
01061 decode_ltp(&ics->ltp, gb, ics->max_sfb);
01062 }
01063 }
01064 }
01065
01066 if (ics->max_sfb > ics->num_swb) {
01067 av_log(ac->avctx, AV_LOG_ERROR,
01068 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
01069 ics->max_sfb, ics->num_swb);
01070 goto fail;
01071 }
01072
01073 return 0;
01074 fail:
01075 ics->max_sfb = 0;
01076 return AVERROR_INVALIDDATA;
01077 }
01078
01087 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
01088 int band_type_run_end[120], GetBitContext *gb,
01089 IndividualChannelStream *ics)
01090 {
01091 int g, idx = 0;
01092 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
01093 for (g = 0; g < ics->num_window_groups; g++) {
01094 int k = 0;
01095 while (k < ics->max_sfb) {
01096 uint8_t sect_end = k;
01097 int sect_len_incr;
01098 int sect_band_type = get_bits(gb, 4);
01099 if (sect_band_type == 12) {
01100 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
01101 return -1;
01102 }
01103 do {
01104 sect_len_incr = get_bits(gb, bits);
01105 sect_end += sect_len_incr;
01106 if (get_bits_left(gb) < 0) {
01107 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
01108 return -1;
01109 }
01110 if (sect_end > ics->max_sfb) {
01111 av_log(ac->avctx, AV_LOG_ERROR,
01112 "Number of bands (%d) exceeds limit (%d).\n",
01113 sect_end, ics->max_sfb);
01114 return -1;
01115 }
01116 } while (sect_len_incr == (1 << bits) - 1);
01117 for (; k < sect_end; k++) {
01118 band_type [idx] = sect_band_type;
01119 band_type_run_end[idx++] = sect_end;
01120 }
01121 }
01122 }
01123 return 0;
01124 }
01125
01136 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
01137 unsigned int global_gain,
01138 IndividualChannelStream *ics,
01139 enum BandType band_type[120],
01140 int band_type_run_end[120])
01141 {
01142 int g, i, idx = 0;
01143 int offset[3] = { global_gain, global_gain - 90, 0 };
01144 int clipped_offset;
01145 int noise_flag = 1;
01146 for (g = 0; g < ics->num_window_groups; g++) {
01147 for (i = 0; i < ics->max_sfb;) {
01148 int run_end = band_type_run_end[idx];
01149 if (band_type[idx] == ZERO_BT) {
01150 for (; i < run_end; i++, idx++)
01151 sf[idx] = 0.;
01152 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
01153 for (; i < run_end; i++, idx++) {
01154 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01155 clipped_offset = av_clip(offset[2], -155, 100);
01156 if (offset[2] != clipped_offset) {
01157 av_log_ask_for_sample(ac->avctx, "Intensity stereo "
01158 "position clipped (%d -> %d).\nIf you heard an "
01159 "audible artifact, there may be a bug in the "
01160 "decoder. ", offset[2], clipped_offset);
01161 }
01162 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
01163 }
01164 } else if (band_type[idx] == NOISE_BT) {
01165 for (; i < run_end; i++, idx++) {
01166 if (noise_flag-- > 0)
01167 offset[1] += get_bits(gb, 9) - 256;
01168 else
01169 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01170 clipped_offset = av_clip(offset[1], -100, 155);
01171 if (offset[1] != clipped_offset) {
01172 av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
01173 "(%d -> %d).\nIf you heard an audible "
01174 "artifact, there may be a bug in the decoder. ",
01175 offset[1], clipped_offset);
01176 }
01177 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
01178 }
01179 } else {
01180 for (; i < run_end; i++, idx++) {
01181 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01182 if (offset[0] > 255U) {
01183 av_log(ac->avctx, AV_LOG_ERROR,
01184 "Scalefactor (%d) out of range.\n", offset[0]);
01185 return -1;
01186 }
01187 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
01188 }
01189 }
01190 }
01191 }
01192 return 0;
01193 }
01194
01198 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
01199 const uint16_t *swb_offset, int num_swb)
01200 {
01201 int i, pulse_swb;
01202 pulse->num_pulse = get_bits(gb, 2) + 1;
01203 pulse_swb = get_bits(gb, 6);
01204 if (pulse_swb >= num_swb)
01205 return -1;
01206 pulse->pos[0] = swb_offset[pulse_swb];
01207 pulse->pos[0] += get_bits(gb, 5);
01208 if (pulse->pos[0] > 1023)
01209 return -1;
01210 pulse->amp[0] = get_bits(gb, 4);
01211 for (i = 1; i < pulse->num_pulse; i++) {
01212 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
01213 if (pulse->pos[i] > 1023)
01214 return -1;
01215 pulse->amp[i] = get_bits(gb, 4);
01216 }
01217 return 0;
01218 }
01219
01225 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
01226 GetBitContext *gb, const IndividualChannelStream *ics)
01227 {
01228 int w, filt, i, coef_len, coef_res, coef_compress;
01229 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
01230 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
01231 for (w = 0; w < ics->num_windows; w++) {
01232 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
01233 coef_res = get_bits1(gb);
01234
01235 for (filt = 0; filt < tns->n_filt[w]; filt++) {
01236 int tmp2_idx;
01237 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
01238
01239 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
01240 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
01241 tns->order[w][filt], tns_max_order);
01242 tns->order[w][filt] = 0;
01243 return -1;
01244 }
01245 if (tns->order[w][filt]) {
01246 tns->direction[w][filt] = get_bits1(gb);
01247 coef_compress = get_bits1(gb);
01248 coef_len = coef_res + 3 - coef_compress;
01249 tmp2_idx = 2 * coef_compress + coef_res;
01250
01251 for (i = 0; i < tns->order[w][filt]; i++)
01252 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
01253 }
01254 }
01255 }
01256 }
01257 return 0;
01258 }
01259
01267 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
01268 int ms_present)
01269 {
01270 int idx;
01271 if (ms_present == 1) {
01272 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
01273 cpe->ms_mask[idx] = get_bits1(gb);
01274 } else if (ms_present == 2) {
01275 memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
01276 }
01277 }
01278
01279 #ifndef VMUL2
01280 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
01281 const float *scale)
01282 {
01283 float s = *scale;
01284 *dst++ = v[idx & 15] * s;
01285 *dst++ = v[idx>>4 & 15] * s;
01286 return dst;
01287 }
01288 #endif
01289
01290 #ifndef VMUL4
01291 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
01292 const float *scale)
01293 {
01294 float s = *scale;
01295 *dst++ = v[idx & 3] * s;
01296 *dst++ = v[idx>>2 & 3] * s;
01297 *dst++ = v[idx>>4 & 3] * s;
01298 *dst++ = v[idx>>6 & 3] * s;
01299 return dst;
01300 }
01301 #endif
01302
01303 #ifndef VMUL2S
01304 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
01305 unsigned sign, const float *scale)
01306 {
01307 union av_intfloat32 s0, s1;
01308
01309 s0.f = s1.f = *scale;
01310 s0.i ^= sign >> 1 << 31;
01311 s1.i ^= sign << 31;
01312
01313 *dst++ = v[idx & 15] * s0.f;
01314 *dst++ = v[idx>>4 & 15] * s1.f;
01315
01316 return dst;
01317 }
01318 #endif
01319
01320 #ifndef VMUL4S
01321 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
01322 unsigned sign, const float *scale)
01323 {
01324 unsigned nz = idx >> 12;
01325 union av_intfloat32 s = { .f = *scale };
01326 union av_intfloat32 t;
01327
01328 t.i = s.i ^ (sign & 1U<<31);
01329 *dst++ = v[idx & 3] * t.f;
01330
01331 sign <<= nz & 1; nz >>= 1;
01332 t.i = s.i ^ (sign & 1U<<31);
01333 *dst++ = v[idx>>2 & 3] * t.f;
01334
01335 sign <<= nz & 1; nz >>= 1;
01336 t.i = s.i ^ (sign & 1U<<31);
01337 *dst++ = v[idx>>4 & 3] * t.f;
01338
01339 sign <<= nz & 1;
01340 t.i = s.i ^ (sign & 1U<<31);
01341 *dst++ = v[idx>>6 & 3] * t.f;
01342
01343 return dst;
01344 }
01345 #endif
01346
01359 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
01360 GetBitContext *gb, const float sf[120],
01361 int pulse_present, const Pulse *pulse,
01362 const IndividualChannelStream *ics,
01363 enum BandType band_type[120])
01364 {
01365 int i, k, g, idx = 0;
01366 const int c = 1024 / ics->num_windows;
01367 const uint16_t *offsets = ics->swb_offset;
01368 float *coef_base = coef;
01369
01370 for (g = 0; g < ics->num_windows; g++)
01371 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
01372
01373 for (g = 0; g < ics->num_window_groups; g++) {
01374 unsigned g_len = ics->group_len[g];
01375
01376 for (i = 0; i < ics->max_sfb; i++, idx++) {
01377 const unsigned cbt_m1 = band_type[idx] - 1;
01378 float *cfo = coef + offsets[i];
01379 int off_len = offsets[i + 1] - offsets[i];
01380 int group;
01381
01382 if (cbt_m1 >= INTENSITY_BT2 - 1) {
01383 for (group = 0; group < g_len; group++, cfo+=128) {
01384 memset(cfo, 0, off_len * sizeof(float));
01385 }
01386 } else if (cbt_m1 == NOISE_BT - 1) {
01387 for (group = 0; group < g_len; group++, cfo+=128) {
01388 float scale;
01389 float band_energy;
01390
01391 for (k = 0; k < off_len; k++) {
01392 ac->random_state = lcg_random(ac->random_state);
01393 cfo[k] = ac->random_state;
01394 }
01395
01396 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
01397 scale = sf[idx] / sqrtf(band_energy);
01398 ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
01399 }
01400 } else {
01401 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
01402 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
01403 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
01404 OPEN_READER(re, gb);
01405
01406 switch (cbt_m1 >> 1) {
01407 case 0:
01408 for (group = 0; group < g_len; group++, cfo+=128) {
01409 float *cf = cfo;
01410 int len = off_len;
01411
01412 do {
01413 int code;
01414 unsigned cb_idx;
01415
01416 UPDATE_CACHE(re, gb);
01417 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01418 cb_idx = cb_vector_idx[code];
01419 cf = VMUL4(cf, vq, cb_idx, sf + idx);
01420 } while (len -= 4);
01421 }
01422 break;
01423
01424 case 1:
01425 for (group = 0; group < g_len; group++, cfo+=128) {
01426 float *cf = cfo;
01427 int len = off_len;
01428
01429 do {
01430 int code;
01431 unsigned nnz;
01432 unsigned cb_idx;
01433 uint32_t bits;
01434
01435 UPDATE_CACHE(re, gb);
01436 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01437 cb_idx = cb_vector_idx[code];
01438 nnz = cb_idx >> 8 & 15;
01439 bits = nnz ? GET_CACHE(re, gb) : 0;
01440 LAST_SKIP_BITS(re, gb, nnz);
01441 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
01442 } while (len -= 4);
01443 }
01444 break;
01445
01446 case 2:
01447 for (group = 0; group < g_len; group++, cfo+=128) {
01448 float *cf = cfo;
01449 int len = off_len;
01450
01451 do {
01452 int code;
01453 unsigned cb_idx;
01454
01455 UPDATE_CACHE(re, gb);
01456 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01457 cb_idx = cb_vector_idx[code];
01458 cf = VMUL2(cf, vq, cb_idx, sf + idx);
01459 } while (len -= 2);
01460 }
01461 break;
01462
01463 case 3:
01464 case 4:
01465 for (group = 0; group < g_len; group++, cfo+=128) {
01466 float *cf = cfo;
01467 int len = off_len;
01468
01469 do {
01470 int code;
01471 unsigned nnz;
01472 unsigned cb_idx;
01473 unsigned sign;
01474
01475 UPDATE_CACHE(re, gb);
01476 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01477 cb_idx = cb_vector_idx[code];
01478 nnz = cb_idx >> 8 & 15;
01479 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
01480 LAST_SKIP_BITS(re, gb, nnz);
01481 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
01482 } while (len -= 2);
01483 }
01484 break;
01485
01486 default:
01487 for (group = 0; group < g_len; group++, cfo+=128) {
01488 float *cf = cfo;
01489 uint32_t *icf = (uint32_t *) cf;
01490 int len = off_len;
01491
01492 do {
01493 int code;
01494 unsigned nzt, nnz;
01495 unsigned cb_idx;
01496 uint32_t bits;
01497 int j;
01498
01499 UPDATE_CACHE(re, gb);
01500 GET_VLC(code, re, gb, vlc_tab, 8, 2);
01501
01502 if (!code) {
01503 *icf++ = 0;
01504 *icf++ = 0;
01505 continue;
01506 }
01507
01508 cb_idx = cb_vector_idx[code];
01509 nnz = cb_idx >> 12;
01510 nzt = cb_idx >> 8;
01511 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
01512 LAST_SKIP_BITS(re, gb, nnz);
01513
01514 for (j = 0; j < 2; j++) {
01515 if (nzt & 1<<j) {
01516 uint32_t b;
01517 int n;
01518
01519
01520 UPDATE_CACHE(re, gb);
01521 b = GET_CACHE(re, gb);
01522 b = 31 - av_log2(~b);
01523
01524 if (b > 8) {
01525 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
01526 return -1;
01527 }
01528
01529 SKIP_BITS(re, gb, b + 1);
01530 b += 4;
01531 n = (1 << b) + SHOW_UBITS(re, gb, b);
01532 LAST_SKIP_BITS(re, gb, b);
01533 *icf++ = cbrt_tab[n] | (bits & 1U<<31);
01534 bits <<= 1;
01535 } else {
01536 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
01537 *icf++ = (bits & 1U<<31) | v;
01538 bits <<= !!v;
01539 }
01540 cb_idx >>= 4;
01541 }
01542 } while (len -= 2);
01543
01544 ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
01545 }
01546 }
01547
01548 CLOSE_READER(re, gb);
01549 }
01550 }
01551 coef += g_len << 7;
01552 }
01553
01554 if (pulse_present) {
01555 idx = 0;
01556 for (i = 0; i < pulse->num_pulse; i++) {
01557 float co = coef_base[ pulse->pos[i] ];
01558 while (offsets[idx + 1] <= pulse->pos[i])
01559 idx++;
01560 if (band_type[idx] != NOISE_BT && sf[idx]) {
01561 float ico = -pulse->amp[i];
01562 if (co) {
01563 co /= sf[idx];
01564 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
01565 }
01566 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
01567 }
01568 }
01569 }
01570 return 0;
01571 }
01572
01573 static av_always_inline float flt16_round(float pf)
01574 {
01575 union av_intfloat32 tmp;
01576 tmp.f = pf;
01577 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
01578 return tmp.f;
01579 }
01580
01581 static av_always_inline float flt16_even(float pf)
01582 {
01583 union av_intfloat32 tmp;
01584 tmp.f = pf;
01585 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
01586 return tmp.f;
01587 }
01588
01589 static av_always_inline float flt16_trunc(float pf)
01590 {
01591 union av_intfloat32 pun;
01592 pun.f = pf;
01593 pun.i &= 0xFFFF0000U;
01594 return pun.f;
01595 }
01596
01597 static av_always_inline void predict(PredictorState *ps, float *coef,
01598 int output_enable)
01599 {
01600 const float a = 0.953125;
01601 const float alpha = 0.90625;
01602 float e0, e1;
01603 float pv;
01604 float k1, k2;
01605 float r0 = ps->r0, r1 = ps->r1;
01606 float cor0 = ps->cor0, cor1 = ps->cor1;
01607 float var0 = ps->var0, var1 = ps->var1;
01608
01609 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
01610 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
01611
01612 pv = flt16_round(k1 * r0 + k2 * r1);
01613 if (output_enable)
01614 *coef += pv;
01615
01616 e0 = *coef;
01617 e1 = e0 - k1 * r0;
01618
01619 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
01620 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
01621 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
01622 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
01623
01624 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
01625 ps->r0 = flt16_trunc(a * e0);
01626 }
01627
01631 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
01632 {
01633 int sfb, k;
01634
01635 if (!sce->ics.predictor_initialized) {
01636 reset_all_predictors(sce->predictor_state);
01637 sce->ics.predictor_initialized = 1;
01638 }
01639
01640 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
01641 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
01642 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
01643 predict(&sce->predictor_state[k], &sce->coeffs[k],
01644 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
01645 }
01646 }
01647 if (sce->ics.predictor_reset_group)
01648 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
01649 } else
01650 reset_all_predictors(sce->predictor_state);
01651 }
01652
01661 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
01662 GetBitContext *gb, int common_window, int scale_flag)
01663 {
01664 Pulse pulse;
01665 TemporalNoiseShaping *tns = &sce->tns;
01666 IndividualChannelStream *ics = &sce->ics;
01667 float *out = sce->coeffs;
01668 int global_gain, pulse_present = 0;
01669
01670
01671
01672
01673 pulse.num_pulse = 0;
01674
01675 global_gain = get_bits(gb, 8);
01676
01677 if (!common_window && !scale_flag) {
01678 if (decode_ics_info(ac, ics, gb) < 0)
01679 return AVERROR_INVALIDDATA;
01680 }
01681
01682 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
01683 return -1;
01684 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
01685 return -1;
01686
01687 pulse_present = 0;
01688 if (!scale_flag) {
01689 if ((pulse_present = get_bits1(gb))) {
01690 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
01691 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
01692 return -1;
01693 }
01694 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
01695 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
01696 return -1;
01697 }
01698 }
01699 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
01700 return -1;
01701 if (get_bits1(gb)) {
01702 av_log_missing_feature(ac->avctx, "SSR", 1);
01703 return AVERROR_PATCHWELCOME;
01704 }
01705 }
01706
01707 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
01708 return -1;
01709
01710 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
01711 apply_prediction(ac, sce);
01712
01713 return 0;
01714 }
01715
01719 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
01720 {
01721 const IndividualChannelStream *ics = &cpe->ch[0].ics;
01722 float *ch0 = cpe->ch[0].coeffs;
01723 float *ch1 = cpe->ch[1].coeffs;
01724 int g, i, group, idx = 0;
01725 const uint16_t *offsets = ics->swb_offset;
01726 for (g = 0; g < ics->num_window_groups; g++) {
01727 for (i = 0; i < ics->max_sfb; i++, idx++) {
01728 if (cpe->ms_mask[idx] &&
01729 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
01730 for (group = 0; group < ics->group_len[g]; group++) {
01731 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
01732 ch1 + group * 128 + offsets[i],
01733 offsets[i+1] - offsets[i]);
01734 }
01735 }
01736 }
01737 ch0 += ics->group_len[g] * 128;
01738 ch1 += ics->group_len[g] * 128;
01739 }
01740 }
01741
01749 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
01750 {
01751 const IndividualChannelStream *ics = &cpe->ch[1].ics;
01752 SingleChannelElement *sce1 = &cpe->ch[1];
01753 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
01754 const uint16_t *offsets = ics->swb_offset;
01755 int g, group, i, idx = 0;
01756 int c;
01757 float scale;
01758 for (g = 0; g < ics->num_window_groups; g++) {
01759 for (i = 0; i < ics->max_sfb;) {
01760 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
01761 const int bt_run_end = sce1->band_type_run_end[idx];
01762 for (; i < bt_run_end; i++, idx++) {
01763 c = -1 + 2 * (sce1->band_type[idx] - 14);
01764 if (ms_present)
01765 c *= 1 - 2 * cpe->ms_mask[idx];
01766 scale = c * sce1->sf[idx];
01767 for (group = 0; group < ics->group_len[g]; group++)
01768 ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
01769 coef0 + group * 128 + offsets[i],
01770 scale,
01771 offsets[i + 1] - offsets[i]);
01772 }
01773 } else {
01774 int bt_run_end = sce1->band_type_run_end[idx];
01775 idx += bt_run_end - i;
01776 i = bt_run_end;
01777 }
01778 }
01779 coef0 += ics->group_len[g] * 128;
01780 coef1 += ics->group_len[g] * 128;
01781 }
01782 }
01783
01789 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
01790 {
01791 int i, ret, common_window, ms_present = 0;
01792
01793 common_window = get_bits1(gb);
01794 if (common_window) {
01795 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
01796 return AVERROR_INVALIDDATA;
01797 i = cpe->ch[1].ics.use_kb_window[0];
01798 cpe->ch[1].ics = cpe->ch[0].ics;
01799 cpe->ch[1].ics.use_kb_window[1] = i;
01800 if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
01801 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
01802 decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
01803 ms_present = get_bits(gb, 2);
01804 if (ms_present == 3) {
01805 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
01806 return -1;
01807 } else if (ms_present)
01808 decode_mid_side_stereo(cpe, gb, ms_present);
01809 }
01810 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
01811 return ret;
01812 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
01813 return ret;
01814
01815 if (common_window) {
01816 if (ms_present)
01817 apply_mid_side_stereo(ac, cpe);
01818 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
01819 apply_prediction(ac, &cpe->ch[0]);
01820 apply_prediction(ac, &cpe->ch[1]);
01821 }
01822 }
01823
01824 apply_intensity_stereo(ac, cpe, ms_present);
01825 return 0;
01826 }
01827
01828 static const float cce_scale[] = {
01829 1.09050773266525765921,
01830 1.18920711500272106672,
01831 M_SQRT2,
01832 2,
01833 };
01834
01840 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
01841 {
01842 int num_gain = 0;
01843 int c, g, sfb, ret;
01844 int sign;
01845 float scale;
01846 SingleChannelElement *sce = &che->ch[0];
01847 ChannelCoupling *coup = &che->coup;
01848
01849 coup->coupling_point = 2 * get_bits1(gb);
01850 coup->num_coupled = get_bits(gb, 3);
01851 for (c = 0; c <= coup->num_coupled; c++) {
01852 num_gain++;
01853 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
01854 coup->id_select[c] = get_bits(gb, 4);
01855 if (coup->type[c] == TYPE_CPE) {
01856 coup->ch_select[c] = get_bits(gb, 2);
01857 if (coup->ch_select[c] == 3)
01858 num_gain++;
01859 } else
01860 coup->ch_select[c] = 2;
01861 }
01862 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
01863
01864 sign = get_bits(gb, 1);
01865 scale = cce_scale[get_bits(gb, 2)];
01866
01867 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
01868 return ret;
01869
01870 for (c = 0; c < num_gain; c++) {
01871 int idx = 0;
01872 int cge = 1;
01873 int gain = 0;
01874 float gain_cache = 1.;
01875 if (c) {
01876 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
01877 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
01878 gain_cache = powf(scale, -gain);
01879 }
01880 if (coup->coupling_point == AFTER_IMDCT) {
01881 coup->gain[c][0] = gain_cache;
01882 } else {
01883 for (g = 0; g < sce->ics.num_window_groups; g++) {
01884 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
01885 if (sce->band_type[idx] != ZERO_BT) {
01886 if (!cge) {
01887 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
01888 if (t) {
01889 int s = 1;
01890 t = gain += t;
01891 if (sign) {
01892 s -= 2 * (t & 0x1);
01893 t >>= 1;
01894 }
01895 gain_cache = powf(scale, -t) * s;
01896 }
01897 }
01898 coup->gain[c][idx] = gain_cache;
01899 }
01900 }
01901 }
01902 }
01903 }
01904 return 0;
01905 }
01906
01912 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
01913 GetBitContext *gb)
01914 {
01915 int i;
01916 int num_excl_chan = 0;
01917
01918 do {
01919 for (i = 0; i < 7; i++)
01920 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
01921 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
01922
01923 return num_excl_chan / 7;
01924 }
01925
01931 static int decode_dynamic_range(DynamicRangeControl *che_drc,
01932 GetBitContext *gb)
01933 {
01934 int n = 1;
01935 int drc_num_bands = 1;
01936 int i;
01937
01938
01939 if (get_bits1(gb)) {
01940 che_drc->pce_instance_tag = get_bits(gb, 4);
01941 skip_bits(gb, 4);
01942 n++;
01943 }
01944
01945
01946 if (get_bits1(gb)) {
01947 n += decode_drc_channel_exclusions(che_drc, gb);
01948 }
01949
01950
01951 if (get_bits1(gb)) {
01952 che_drc->band_incr = get_bits(gb, 4);
01953 che_drc->interpolation_scheme = get_bits(gb, 4);
01954 n++;
01955 drc_num_bands += che_drc->band_incr;
01956 for (i = 0; i < drc_num_bands; i++) {
01957 che_drc->band_top[i] = get_bits(gb, 8);
01958 n++;
01959 }
01960 }
01961
01962
01963 if (get_bits1(gb)) {
01964 che_drc->prog_ref_level = get_bits(gb, 7);
01965 skip_bits1(gb);
01966 n++;
01967 }
01968
01969 for (i = 0; i < drc_num_bands; i++) {
01970 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
01971 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
01972 n++;
01973 }
01974
01975 return n;
01976 }
01977
01978 static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
01979 uint8_t buf[256];
01980 int i, major, minor;
01981
01982 if (len < 13+7*8)
01983 goto unknown;
01984
01985 get_bits(gb, 13); len -= 13;
01986
01987 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
01988 buf[i] = get_bits(gb, 8);
01989
01990 buf[i] = 0;
01991 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
01992 av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
01993
01994 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
01995 ac->avctx->internal->skip_samples = 1024;
01996 }
01997
01998 unknown:
01999 skip_bits_long(gb, len);
02000
02001 return 0;
02002 }
02003
02011 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
02012 ChannelElement *che, enum RawDataBlockType elem_type)
02013 {
02014 int crc_flag = 0;
02015 int res = cnt;
02016 switch (get_bits(gb, 4)) {
02017 case EXT_SBR_DATA_CRC:
02018 crc_flag++;
02019 case EXT_SBR_DATA:
02020 if (!che) {
02021 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
02022 return res;
02023 } else if (!ac->oc[1].m4ac.sbr) {
02024 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
02025 skip_bits_long(gb, 8 * cnt - 4);
02026 return res;
02027 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
02028 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
02029 skip_bits_long(gb, 8 * cnt - 4);
02030 return res;
02031 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
02032 ac->oc[1].m4ac.sbr = 1;
02033 ac->oc[1].m4ac.ps = 1;
02034 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
02035 ac->oc[1].status, 1);
02036 } else {
02037 ac->oc[1].m4ac.sbr = 1;
02038 }
02039 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
02040 break;
02041 case EXT_DYNAMIC_RANGE:
02042 res = decode_dynamic_range(&ac->che_drc, gb);
02043 break;
02044 case EXT_FILL:
02045 decode_fill(ac, gb, 8 * cnt - 4);
02046 break;
02047 case EXT_FILL_DATA:
02048 case EXT_DATA_ELEMENT:
02049 default:
02050 skip_bits_long(gb, 8 * cnt - 4);
02051 break;
02052 };
02053 return res;
02054 }
02055
02062 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
02063 IndividualChannelStream *ics, int decode)
02064 {
02065 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
02066 int w, filt, m, i;
02067 int bottom, top, order, start, end, size, inc;
02068 float lpc[TNS_MAX_ORDER];
02069 float tmp[TNS_MAX_ORDER+1];
02070
02071 for (w = 0; w < ics->num_windows; w++) {
02072 bottom = ics->num_swb;
02073 for (filt = 0; filt < tns->n_filt[w]; filt++) {
02074 top = bottom;
02075 bottom = FFMAX(0, top - tns->length[w][filt]);
02076 order = tns->order[w][filt];
02077 if (order == 0)
02078 continue;
02079
02080
02081 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
02082
02083 start = ics->swb_offset[FFMIN(bottom, mmm)];
02084 end = ics->swb_offset[FFMIN( top, mmm)];
02085 if ((size = end - start) <= 0)
02086 continue;
02087 if (tns->direction[w][filt]) {
02088 inc = -1;
02089 start = end - 1;
02090 } else {
02091 inc = 1;
02092 }
02093 start += w * 128;
02094
02095 if (decode) {
02096
02097 for (m = 0; m < size; m++, start += inc)
02098 for (i = 1; i <= FFMIN(m, order); i++)
02099 coef[start] -= coef[start - i * inc] * lpc[i - 1];
02100 } else {
02101
02102 for (m = 0; m < size; m++, start += inc) {
02103 tmp[0] = coef[start];
02104 for (i = 1; i <= FFMIN(m, order); i++)
02105 coef[start] += tmp[i] * lpc[i - 1];
02106 for (i = order; i > 0; i--)
02107 tmp[i] = tmp[i - 1];
02108 }
02109 }
02110 }
02111 }
02112 }
02113
02118 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
02119 float *in, IndividualChannelStream *ics)
02120 {
02121 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
02122 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
02123 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
02124 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
02125
02126 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
02127 ac->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
02128 } else {
02129 memset(in, 0, 448 * sizeof(float));
02130 ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
02131 }
02132 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
02133 ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
02134 } else {
02135 ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
02136 memset(in + 1024 + 576, 0, 448 * sizeof(float));
02137 }
02138 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
02139 }
02140
02144 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
02145 {
02146 const LongTermPrediction *ltp = &sce->ics.ltp;
02147 const uint16_t *offsets = sce->ics.swb_offset;
02148 int i, sfb;
02149
02150 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
02151 float *predTime = sce->ret;
02152 float *predFreq = ac->buf_mdct;
02153 int16_t num_samples = 2048;
02154
02155 if (ltp->lag < 1024)
02156 num_samples = ltp->lag + 1024;
02157 for (i = 0; i < num_samples; i++)
02158 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
02159 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
02160
02161 windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
02162
02163 if (sce->tns.present)
02164 apply_tns(predFreq, &sce->tns, &sce->ics, 0);
02165
02166 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
02167 if (ltp->used[sfb])
02168 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
02169 sce->coeffs[i] += predFreq[i];
02170 }
02171 }
02172
02176 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
02177 {
02178 IndividualChannelStream *ics = &sce->ics;
02179 float *saved = sce->saved;
02180 float *saved_ltp = sce->coeffs;
02181 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
02182 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
02183 int i;
02184
02185 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
02186 memcpy(saved_ltp, saved, 512 * sizeof(float));
02187 memset(saved_ltp + 576, 0, 448 * sizeof(float));
02188 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
02189 for (i = 0; i < 64; i++)
02190 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
02191 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
02192 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
02193 memset(saved_ltp + 576, 0, 448 * sizeof(float));
02194 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
02195 for (i = 0; i < 64; i++)
02196 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
02197 } else {
02198 ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
02199 for (i = 0; i < 512; i++)
02200 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
02201 }
02202
02203 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
02204 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
02205 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
02206 }
02207
02211 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
02212 {
02213 IndividualChannelStream *ics = &sce->ics;
02214 float *in = sce->coeffs;
02215 float *out = sce->ret;
02216 float *saved = sce->saved;
02217 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
02218 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
02219 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
02220 float *buf = ac->buf_mdct;
02221 float *temp = ac->temp;
02222 int i;
02223
02224
02225 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
02226 for (i = 0; i < 1024; i += 128)
02227 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
02228 } else
02229 ac->mdct.imdct_half(&ac->mdct, buf, in);
02230
02231
02232
02233
02234
02235
02236
02237 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
02238 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
02239 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
02240 } else {
02241 memcpy( out, saved, 448 * sizeof(float));
02242
02243 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
02244 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
02245 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
02246 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
02247 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
02248 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
02249 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
02250 } else {
02251 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
02252 memcpy( out + 576, buf + 64, 448 * sizeof(float));
02253 }
02254 }
02255
02256
02257 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
02258 memcpy( saved, temp + 64, 64 * sizeof(float));
02259 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
02260 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
02261 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
02262 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
02263 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
02264 memcpy( saved, buf + 512, 448 * sizeof(float));
02265 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
02266 } else {
02267 memcpy( saved, buf + 512, 512 * sizeof(float));
02268 }
02269 }
02270
02276 static void apply_dependent_coupling(AACContext *ac,
02277 SingleChannelElement *target,
02278 ChannelElement *cce, int index)
02279 {
02280 IndividualChannelStream *ics = &cce->ch[0].ics;
02281 const uint16_t *offsets = ics->swb_offset;
02282 float *dest = target->coeffs;
02283 const float *src = cce->ch[0].coeffs;
02284 int g, i, group, k, idx = 0;
02285 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
02286 av_log(ac->avctx, AV_LOG_ERROR,
02287 "Dependent coupling is not supported together with LTP\n");
02288 return;
02289 }
02290 for (g = 0; g < ics->num_window_groups; g++) {
02291 for (i = 0; i < ics->max_sfb; i++, idx++) {
02292 if (cce->ch[0].band_type[idx] != ZERO_BT) {
02293 const float gain = cce->coup.gain[index][idx];
02294 for (group = 0; group < ics->group_len[g]; group++) {
02295 for (k = offsets[i]; k < offsets[i + 1]; k++) {
02296
02297 dest[group * 128 + k] += gain * src[group * 128 + k];
02298 }
02299 }
02300 }
02301 }
02302 dest += ics->group_len[g] * 128;
02303 src += ics->group_len[g] * 128;
02304 }
02305 }
02306
02312 static void apply_independent_coupling(AACContext *ac,
02313 SingleChannelElement *target,
02314 ChannelElement *cce, int index)
02315 {
02316 int i;
02317 const float gain = cce->coup.gain[index][0];
02318 const float *src = cce->ch[0].ret;
02319 float *dest = target->ret;
02320 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
02321
02322 for (i = 0; i < len; i++)
02323 dest[i] += gain * src[i];
02324 }
02325
02331 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
02332 enum RawDataBlockType type, int elem_id,
02333 enum CouplingPoint coupling_point,
02334 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
02335 {
02336 int i, c;
02337
02338 for (i = 0; i < MAX_ELEM_ID; i++) {
02339 ChannelElement *cce = ac->che[TYPE_CCE][i];
02340 int index = 0;
02341
02342 if (cce && cce->coup.coupling_point == coupling_point) {
02343 ChannelCoupling *coup = &cce->coup;
02344
02345 for (c = 0; c <= coup->num_coupled; c++) {
02346 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
02347 if (coup->ch_select[c] != 1) {
02348 apply_coupling_method(ac, &cc->ch[0], cce, index);
02349 if (coup->ch_select[c] != 0)
02350 index++;
02351 }
02352 if (coup->ch_select[c] != 2)
02353 apply_coupling_method(ac, &cc->ch[1], cce, index++);
02354 } else
02355 index += 1 + (coup->ch_select[c] == 3);
02356 }
02357 }
02358 }
02359 }
02360
02364 static void spectral_to_sample(AACContext *ac)
02365 {
02366 int i, type;
02367 for (type = 3; type >= 0; type--) {
02368 for (i = 0; i < MAX_ELEM_ID; i++) {
02369 ChannelElement *che = ac->che[type][i];
02370 if (che) {
02371 if (type <= TYPE_CPE)
02372 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
02373 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
02374 if (che->ch[0].ics.predictor_present) {
02375 if (che->ch[0].ics.ltp.present)
02376 apply_ltp(ac, &che->ch[0]);
02377 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
02378 apply_ltp(ac, &che->ch[1]);
02379 }
02380 }
02381 if (che->ch[0].tns.present)
02382 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
02383 if (che->ch[1].tns.present)
02384 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
02385 if (type <= TYPE_CPE)
02386 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
02387 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
02388 imdct_and_windowing(ac, &che->ch[0]);
02389 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
02390 update_ltp(ac, &che->ch[0]);
02391 if (type == TYPE_CPE) {
02392 imdct_and_windowing(ac, &che->ch[1]);
02393 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
02394 update_ltp(ac, &che->ch[1]);
02395 }
02396 if (ac->oc[1].m4ac.sbr > 0) {
02397 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
02398 }
02399 }
02400 if (type <= TYPE_CCE)
02401 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
02402 }
02403 }
02404 }
02405 }
02406
02407 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
02408 {
02409 int size;
02410 AACADTSHeaderInfo hdr_info;
02411 uint8_t layout_map[MAX_ELEM_ID*4][3];
02412 int layout_map_tags;
02413
02414 size = avpriv_aac_parse_header(gb, &hdr_info);
02415 if (size > 0) {
02416 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
02417
02418
02419 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame", 0);
02420 ac->warned_num_aac_frames = 1;
02421 }
02422 push_output_configuration(ac);
02423 if (hdr_info.chan_config) {
02424 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
02425 if (set_default_channel_config(ac->avctx, layout_map,
02426 &layout_map_tags, hdr_info.chan_config))
02427 return -7;
02428 if (output_configure(ac, layout_map, layout_map_tags,
02429 FFMAX(ac->oc[1].status, OC_TRIAL_FRAME), 0))
02430 return -7;
02431 } else {
02432 ac->oc[1].m4ac.chan_config = 0;
02438 #if 0
02439 if (ac->enable_jp_dmono && ac->oc[0].status == OC_NONE) {
02440 layout_map_tags = 2;
02441 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
02442 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
02443 layout_map[0][1] = 0;
02444 layout_map[1][1] = 1;
02445 if (output_configure(ac, layout_map, layout_map_tags,
02446 OC_TRIAL_FRAME))
02447 return -7;
02448 }
02449 #endif
02450 }
02451 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
02452 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
02453 ac->oc[1].m4ac.object_type = hdr_info.object_type;
02454 if (ac->oc[0].status != OC_LOCKED ||
02455 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
02456 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
02457 ac->oc[1].m4ac.sbr = -1;
02458 ac->oc[1].m4ac.ps = -1;
02459 }
02460 if (!hdr_info.crc_absent)
02461 skip_bits(gb, 16);
02462 }
02463 return size;
02464 }
02465
02466 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
02467 int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
02468 {
02469 AACContext *ac = avctx->priv_data;
02470 ChannelElement *che = NULL, *che_prev = NULL;
02471 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
02472 int err, elem_id;
02473 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
02474 int is_dmono, sce_count = 0;
02475 float *tmp = NULL;
02476
02477 if (show_bits(gb, 12) == 0xfff) {
02478 if (parse_adts_frame_header(ac, gb) < 0) {
02479 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
02480 err = -1;
02481 goto fail;
02482 }
02483 if (ac->oc[1].m4ac.sampling_index > 12) {
02484 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
02485 err = -1;
02486 goto fail;
02487 }
02488 }
02489
02490 if (frame_configure_elements(avctx) < 0) {
02491 err = -1;
02492 goto fail;
02493 }
02494
02495 ac->tags_mapped = 0;
02496
02497 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
02498 elem_id = get_bits(gb, 4);
02499
02500 if (elem_type < TYPE_DSE) {
02501 if (!(che=get_che(ac, elem_type, elem_id))) {
02502 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
02503 elem_type, elem_id);
02504 err = -1;
02505 goto fail;
02506 }
02507 samples = 1024;
02508 }
02509
02510 switch (elem_type) {
02511
02512 case TYPE_SCE:
02513 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02514 audio_found = 1;
02515 sce_count++;
02516 break;
02517
02518 case TYPE_CPE:
02519 err = decode_cpe(ac, gb, che);
02520 audio_found = 1;
02521 break;
02522
02523 case TYPE_CCE:
02524 err = decode_cce(ac, gb, che);
02525 break;
02526
02527 case TYPE_LFE:
02528 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
02529 audio_found = 1;
02530 break;
02531
02532 case TYPE_DSE:
02533 err = skip_data_stream_element(ac, gb);
02534 break;
02535
02536 case TYPE_PCE: {
02537 uint8_t layout_map[MAX_ELEM_ID*4][3];
02538 int tags;
02539 push_output_configuration(ac);
02540 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
02541 if (tags < 0) {
02542 err = tags;
02543 break;
02544 }
02545 if (pce_found) {
02546 av_log(avctx, AV_LOG_ERROR,
02547 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
02548 pop_output_configuration(ac);
02549 } else {
02550 err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
02551 if (!err)
02552 ac->oc[1].m4ac.chan_config = 0;
02553 pce_found = 1;
02554 }
02555 break;
02556 }
02557
02558 case TYPE_FIL:
02559 if (elem_id == 15)
02560 elem_id += get_bits(gb, 8) - 1;
02561 if (get_bits_left(gb) < 8 * elem_id) {
02562 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
02563 err = -1;
02564 goto fail;
02565 }
02566 while (elem_id > 0)
02567 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
02568 err = 0;
02569 break;
02570
02571 default:
02572 err = -1;
02573 break;
02574 }
02575
02576 che_prev = che;
02577 elem_type_prev = elem_type;
02578
02579 if (err)
02580 goto fail;
02581
02582 if (get_bits_left(gb) < 3) {
02583 av_log(avctx, AV_LOG_ERROR, overread_err);
02584 err = -1;
02585 goto fail;
02586 }
02587 }
02588
02589 spectral_to_sample(ac);
02590
02591 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
02592 samples <<= multiplier;
02593 #if 0
02594
02595 is_dmono = ac->enable_jp_dmono && sce_count == 2 &&
02596 ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
02597
02598 if (is_dmono) {
02599 if (ac->dmono_mode == 0) {
02600 tmp = ac->output_data[1];
02601 ac->output_data[1] = ac->output_data[0];
02602 } else if (ac->dmono_mode == 1) {
02603 tmp = ac->output_data[0];
02604 ac->output_data[0] = ac->output_data[1];
02605 }
02606 }
02607 #endif
02608 if (samples) {
02609 ac->frame.nb_samples = samples;
02610 *(AVFrame *)data = ac->frame;
02611 }
02612 *got_frame_ptr = !!samples;
02613 #if 0
02614 if (is_dmono) {
02615 if (ac->dmono_mode == 0)
02616 ac->output_data[1] = tmp;
02617 else if (ac->dmono_mode == 1)
02618 ac->output_data[0] = tmp;
02619 }
02620 #endif
02621 if (ac->oc[1].status && audio_found) {
02622 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
02623 avctx->frame_size = samples;
02624 ac->oc[1].status = OC_LOCKED;
02625 }
02626
02627 if (multiplier) {
02628 int side_size;
02629 uint32_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
02630 if (side && side_size>=4)
02631 AV_WL32(side, 2*AV_RL32(side));
02632 }
02633 return 0;
02634 fail:
02635 pop_output_configuration(ac);
02636 return err;
02637 }
02638
02639 static int aac_decode_frame(AVCodecContext *avctx, void *data,
02640 int *got_frame_ptr, AVPacket *avpkt)
02641 {
02642 AACContext *ac = avctx->priv_data;
02643 const uint8_t *buf = avpkt->data;
02644 int buf_size = avpkt->size;
02645 GetBitContext gb;
02646 int buf_consumed;
02647 int buf_offset;
02648 int err;
02649 int new_extradata_size;
02650 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
02651 AV_PKT_DATA_NEW_EXTRADATA,
02652 &new_extradata_size);
02653 int jp_dualmono_size;
02654 const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
02655 AV_PKT_DATA_JP_DUALMONO,
02656 &jp_dualmono_size);
02657
02658 if (new_extradata && 0) {
02659 av_free(avctx->extradata);
02660 avctx->extradata = av_mallocz(new_extradata_size +
02661 FF_INPUT_BUFFER_PADDING_SIZE);
02662 if (!avctx->extradata)
02663 return AVERROR(ENOMEM);
02664 avctx->extradata_size = new_extradata_size;
02665 memcpy(avctx->extradata, new_extradata, new_extradata_size);
02666 push_output_configuration(ac);
02667 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
02668 avctx->extradata,
02669 avctx->extradata_size*8, 1) < 0) {
02670 pop_output_configuration(ac);
02671 return AVERROR_INVALIDDATA;
02672 }
02673 }
02674
02675 ac->enable_jp_dmono = !!jp_dualmono;
02676 ac->dmono_mode = 0;
02677 if (jp_dualmono && jp_dualmono_size > 0)
02678 ac->dmono_mode = *jp_dualmono;
02679
02680 init_get_bits(&gb, buf, buf_size * 8);
02681
02682 if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt)) < 0)
02683 return err;
02684
02685 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
02686 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
02687 if (buf[buf_offset])
02688 break;
02689
02690 return buf_size > buf_offset ? buf_consumed : buf_size;
02691 }
02692
02693 static av_cold int aac_decode_close(AVCodecContext *avctx)
02694 {
02695 AACContext *ac = avctx->priv_data;
02696 int i, type;
02697
02698 for (i = 0; i < MAX_ELEM_ID; i++) {
02699 for (type = 0; type < 4; type++) {
02700 if (ac->che[type][i])
02701 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
02702 av_freep(&ac->che[type][i]);
02703 }
02704 }
02705
02706 ff_mdct_end(&ac->mdct);
02707 ff_mdct_end(&ac->mdct_small);
02708 ff_mdct_end(&ac->mdct_ltp);
02709 return 0;
02710 }
02711
02712
02713 #define LOAS_SYNC_WORD 0x2b7
02714
02715 struct LATMContext {
02716 AACContext aac_ctx;
02717 int initialized;
02718
02719
02720 int audio_mux_version_A;
02721 int frame_length_type;
02722 int frame_length;
02723 };
02724
02725 static inline uint32_t latm_get_value(GetBitContext *b)
02726 {
02727 int length = get_bits(b, 2);
02728
02729 return get_bits_long(b, (length+1)*8);
02730 }
02731
02732 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
02733 GetBitContext *gb, int asclen)
02734 {
02735 AACContext *ac = &latmctx->aac_ctx;
02736 AVCodecContext *avctx = ac->avctx;
02737 MPEG4AudioConfig m4ac = { 0 };
02738 int config_start_bit = get_bits_count(gb);
02739 int sync_extension = 0;
02740 int bits_consumed, esize;
02741
02742 if (asclen) {
02743 sync_extension = 1;
02744 asclen = FFMIN(asclen, get_bits_left(gb));
02745 } else
02746 asclen = get_bits_left(gb);
02747
02748 if (config_start_bit % 8) {
02749 av_log_missing_feature(latmctx->aac_ctx.avctx,
02750 "Non-byte-aligned audio-specific config", 1);
02751 return AVERROR_PATCHWELCOME;
02752 }
02753 if (asclen <= 0)
02754 return AVERROR_INVALIDDATA;
02755 bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
02756 gb->buffer + (config_start_bit / 8),
02757 asclen, sync_extension);
02758
02759 if (bits_consumed < 0)
02760 return AVERROR_INVALIDDATA;
02761
02762 if (!latmctx->initialized ||
02763 ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
02764 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
02765
02766 if(latmctx->initialized) {
02767 av_log(avctx, AV_LOG_INFO, "audio config changed\n");
02768 } else {
02769 av_log(avctx, AV_LOG_INFO, "initializing latmctx\n");
02770 }
02771 latmctx->initialized = 0;
02772
02773 esize = (bits_consumed+7) / 8;
02774
02775 if (avctx->extradata_size < esize) {
02776 av_free(avctx->extradata);
02777 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
02778 if (!avctx->extradata)
02779 return AVERROR(ENOMEM);
02780 }
02781
02782 avctx->extradata_size = esize;
02783 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
02784 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
02785 }
02786 skip_bits_long(gb, bits_consumed);
02787
02788 return bits_consumed;
02789 }
02790
02791 static int read_stream_mux_config(struct LATMContext *latmctx,
02792 GetBitContext *gb)
02793 {
02794 int ret, audio_mux_version = get_bits(gb, 1);
02795
02796 latmctx->audio_mux_version_A = 0;
02797 if (audio_mux_version)
02798 latmctx->audio_mux_version_A = get_bits(gb, 1);
02799
02800 if (!latmctx->audio_mux_version_A) {
02801
02802 if (audio_mux_version)
02803 latm_get_value(gb);
02804
02805 skip_bits(gb, 1);
02806 skip_bits(gb, 6);
02807
02808 if (get_bits(gb, 4)) {
02809 av_log_missing_feature(latmctx->aac_ctx.avctx,
02810 "Multiple programs", 1);
02811 return AVERROR_PATCHWELCOME;
02812 }
02813
02814
02815
02816
02817 if (get_bits(gb, 3)) {
02818 av_log_missing_feature(latmctx->aac_ctx.avctx,
02819 "Multiple layers", 1);
02820 return AVERROR_PATCHWELCOME;
02821 }
02822
02823
02824 if (!audio_mux_version) {
02825 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
02826 return ret;
02827 } else {
02828 int ascLen = latm_get_value(gb);
02829 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
02830 return ret;
02831 ascLen -= ret;
02832 skip_bits_long(gb, ascLen);
02833 }
02834
02835 latmctx->frame_length_type = get_bits(gb, 3);
02836 switch (latmctx->frame_length_type) {
02837 case 0:
02838 skip_bits(gb, 8);
02839 break;
02840 case 1:
02841 latmctx->frame_length = get_bits(gb, 9);
02842 break;
02843 case 3:
02844 case 4:
02845 case 5:
02846 skip_bits(gb, 6);
02847 break;
02848 case 6:
02849 case 7:
02850 skip_bits(gb, 1);
02851 break;
02852 }
02853
02854 if (get_bits(gb, 1)) {
02855 if (audio_mux_version) {
02856 latm_get_value(gb);
02857 } else {
02858 int esc;
02859 do {
02860 esc = get_bits(gb, 1);
02861 skip_bits(gb, 8);
02862 } while (esc);
02863 }
02864 }
02865
02866 if (get_bits(gb, 1))
02867 skip_bits(gb, 8);
02868 }
02869
02870 return 0;
02871 }
02872
02873 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
02874 {
02875 uint8_t tmp;
02876
02877 if (ctx->frame_length_type == 0) {
02878 int mux_slot_length = 0;
02879 do {
02880 tmp = get_bits(gb, 8);
02881 mux_slot_length += tmp;
02882 } while (tmp == 255);
02883 return mux_slot_length;
02884 } else if (ctx->frame_length_type == 1) {
02885 return ctx->frame_length;
02886 } else if (ctx->frame_length_type == 3 ||
02887 ctx->frame_length_type == 5 ||
02888 ctx->frame_length_type == 7) {
02889 skip_bits(gb, 2);
02890 }
02891 return 0;
02892 }
02893
02894 static int read_audio_mux_element(struct LATMContext *latmctx,
02895 GetBitContext *gb)
02896 {
02897 int err;
02898 uint8_t use_same_mux = get_bits(gb, 1);
02899 if (!use_same_mux) {
02900 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
02901 return err;
02902 } else if (!latmctx->aac_ctx.avctx->extradata) {
02903 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
02904 "no decoder config found\n");
02905 return AVERROR(EAGAIN);
02906 }
02907 if (latmctx->audio_mux_version_A == 0) {
02908 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
02909 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
02910 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
02911 return AVERROR_INVALIDDATA;
02912 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
02913 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02914 "frame length mismatch %d << %d\n",
02915 mux_slot_length_bytes * 8, get_bits_left(gb));
02916 return AVERROR_INVALIDDATA;
02917 }
02918 }
02919 return 0;
02920 }
02921
02922
02923 static int latm_decode_frame(AVCodecContext *avctx, void *out,
02924 int *got_frame_ptr, AVPacket *avpkt)
02925 {
02926 struct LATMContext *latmctx = avctx->priv_data;
02927 int muxlength, err;
02928 GetBitContext gb;
02929
02930 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
02931
02932
02933 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
02934 return AVERROR_INVALIDDATA;
02935
02936 muxlength = get_bits(&gb, 13) + 3;
02937
02938 if (muxlength > avpkt->size)
02939 return AVERROR_INVALIDDATA;
02940
02941 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
02942 return err;
02943
02944 if (!latmctx->initialized) {
02945 if (!avctx->extradata) {
02946 *got_frame_ptr = 0;
02947 return avpkt->size;
02948 } else {
02949 push_output_configuration(&latmctx->aac_ctx);
02950 if ((err = decode_audio_specific_config(
02951 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
02952 avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
02953 pop_output_configuration(&latmctx->aac_ctx);
02954 return err;
02955 }
02956 latmctx->initialized = 1;
02957 }
02958 }
02959
02960 if (show_bits(&gb, 12) == 0xfff) {
02961 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
02962 "ADTS header detected, probably as result of configuration "
02963 "misparsing\n");
02964 return AVERROR_INVALIDDATA;
02965 }
02966
02967 if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt)) < 0)
02968 return err;
02969
02970 return muxlength;
02971 }
02972
02973 static av_cold int latm_decode_init(AVCodecContext *avctx)
02974 {
02975 struct LATMContext *latmctx = avctx->priv_data;
02976 int ret = aac_decode_init(avctx);
02977
02978 if (avctx->extradata_size > 0)
02979 latmctx->initialized = !ret;
02980
02981 return ret;
02982 }
02983
02984
02985 AVCodec ff_aac_decoder = {
02986 .name = "aac",
02987 .type = AVMEDIA_TYPE_AUDIO,
02988 .id = AV_CODEC_ID_AAC,
02989 .priv_data_size = sizeof(AACContext),
02990 .init = aac_decode_init,
02991 .close = aac_decode_close,
02992 .decode = aac_decode_frame,
02993 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
02994 .sample_fmts = (const enum AVSampleFormat[]) {
02995 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
02996 },
02997 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
02998 .channel_layouts = aac_channel_layout,
02999 .flush = flush,
03000 };
03001
03002
03003
03004
03005
03006
03007 AVCodec ff_aac_latm_decoder = {
03008 .name = "aac_latm",
03009 .type = AVMEDIA_TYPE_AUDIO,
03010 .id = AV_CODEC_ID_AAC_LATM,
03011 .priv_data_size = sizeof(struct LATMContext),
03012 .init = latm_decode_init,
03013 .close = aac_decode_close,
03014 .decode = latm_decode_frame,
03015 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
03016 .sample_fmts = (const enum AVSampleFormat[]) {
03017 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
03018 },
03019 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
03020 .channel_layouts = aac_channel_layout,
03021 .flush = flush,
03022 };