X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Faacenc.c;h=e60a778c6d1dce45c6305274cd79717c9e75e2e6;hb=bfadca1faf7eb131d15181a8d0df2bb5c2662b37;hp=385c6aa994dc89ebf3d7a2cde7d94cab78be5900;hpb=01344fe409da286cd377f9af610eb4c4888687ec;p=ffmpeg diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index 385c6aa994d..e60a778c6d1 100644 --- a/libavcodec/aacenc.c +++ b/libavcodec/aacenc.c @@ -2,20 +2,20 @@ * AAC encoder * Copyright (C) 2008 Konstantin Shishkov * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -135,6 +135,15 @@ static const uint8_t aac_chan_configs[6][5] = { {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE }; +static const uint8_t channel_maps[][AAC_MAX_CHANNELS] = { + { 0 }, + { 0, 1 }, + { 2, 0, 1 }, + { 2, 0, 1, 3 }, + { 2, 0, 1, 3, 4 }, + { 2, 0, 1, 4, 5, 3 }, +}; + /** * Make AAC audio config object. * @see 1.6.2.1 "Syntax - AudioSpecificConfig" @@ -165,6 +174,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) AACEncContext *s = avctx->priv_data; int i; const uint8_t *sizes[2]; + uint8_t grouping[AAC_MAX_CHANNELS]; int lengths[2]; avctx->frame_size = 1024; @@ -210,7 +220,9 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) sizes[1] = swb_size_128[i]; lengths[0] = ff_aac_num_swb_1024[i]; lengths[1] = ff_aac_num_swb_128[i]; - ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], &s->chan_map[1]); + for (i = 0; i < s->chan_map[0]; i++) + grouping[i] = s->chan_map[i + 1] == TYPE_CPE; + ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping); s->psypp = ff_psy_preprocess_init(avctx); s->coder = &ff_aac_coders[2]; @@ -364,7 +376,7 @@ static void adjust_frame_information(AACEncContext *apc, ChannelElement *cpe, in if (msc == 0 || ics0->max_sfb == 0) cpe->ms_mode = 0; else - cpe->ms_mode = msc < ics0->max_sfb ? 1 : 2; + cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2; } } @@ -499,15 +511,24 @@ static int aac_encode_frame(AVCodecContext *avctx, return 0; if (data) { if (!s->psypp) { - memcpy(s->samples + 1024 * avctx->channels, data, - 1024 * avctx->channels * sizeof(s->samples[0])); + if (avctx->channels <= 2) { + memcpy(s->samples + 1024 * avctx->channels, data, + 1024 * avctx->channels * sizeof(s->samples[0])); + } else { + for (i = 0; i < 1024; i++) + for (ch = 0; ch < avctx->channels; ch++) + s->samples[(i + 1024) * avctx->channels + ch] = + ((int16_t*)data)[i * avctx->channels + + channel_maps[avctx->channels-1][ch]]; + } } else { start_ch = 0; samples2 = s->samples + 1024 * avctx->channels; for (i = 0; i < s->chan_map[0]; i++) { tag = s->chan_map[i+1]; chans = tag == TYPE_CPE ? 2 : 1; - ff_psy_preprocess(s->psypp, (uint16_t*)data + start_ch, + ff_psy_preprocess(s->psypp, + (uint16_t*)data + channel_maps[avctx->channels-1][start_ch], samples2 + start_ch, start_ch, chans); start_ch += chans; } @@ -537,6 +558,12 @@ static int aac_encode_frame(AVCodecContext *avctx, wi[ch].window_shape = 0; wi[ch].num_windows = 1; wi[ch].grouping[0] = 1; + + /* Only the lowest 12 coefficients are used in a LFE channel. + * The expression below results in only the bottom 8 coefficients + * being used for 11.025kHz to 16kHz sample rates. + */ + ics->num_swb = s->samplerate_index >= 8 ? 1 : 3; } else { wi[ch] = s->psy.model->window(&s->psy, samples2, la, cur_channel, ics->window_sequence[0]); @@ -547,7 +574,7 @@ static int aac_encode_frame(AVCodecContext *avctx, ics->use_kb_window[0] = wi[ch].window_shape; ics->num_windows = wi[ch].num_windows; ics->swb_sizes = s->psy.bands [ics->num_windows == 8]; - ics->num_swb = tag == TYPE_LFE ? 12 : s->psy.num_bands[ics->num_windows == 8]; + ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8]; for (w = 0; w < ics->num_windows; w++) ics->group_len[w] = wi[ch].grouping[w]; @@ -572,7 +599,7 @@ static int aac_encode_frame(AVCodecContext *avctx, put_bits(&s->pb, 4, chan_el_counter[tag]++); for (ch = 0; ch < chans; ch++) coeffs[ch] = cpe->ch[ch].coeffs; - s->psy.model->analyze_group(&s->psy, start_ch, coeffs, wi); + s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); for (ch = 0; ch < chans; ch++) { s->cur_channel = start_ch * 2 + ch; s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); @@ -674,13 +701,13 @@ static const AVClass aacenc_class = { }; AVCodec ff_aac_encoder = { - "aac", - AVMEDIA_TYPE_AUDIO, - CODEC_ID_AAC, - sizeof(AACEncContext), - aac_encode_init, - aac_encode_frame, - aac_encode_end, + .name = "aac", + .type = AVMEDIA_TYPE_AUDIO, + .id = CODEC_ID_AAC, + .priv_data_size = sizeof(AACEncContext), + .init = aac_encode_init, + .encode = aac_encode_frame, + .close = aac_encode_end, .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),