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