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