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