1096 lines · 41.2 KB · C
RawDownload
/* * AAC encoder * Copyright (C) 2008 Konstantin Shishkov * * This file is part of FFmpeg. * * 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. * * 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 FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * AAC encoder */ /*********************************** *              TODOs: * add sane pulse detection ***********************************/ #include "libavutil/libm.h"#include "libavutil/thread.h"#include "libavutil/float_dsp.h"#include "libavutil/opt.h"#include "avcodec.h"#include "put_bits.h"#include "internal.h"#include "mpeg4audio.h"#include "kbdwin.h"#include "sinewin.h" #include "aac.h"#include "aactab.h"#include "aacenc.h"#include "aacenctab.h"#include "aacenc_utils.h" #include "psymodel.h" static AVOnce aac_table_init = AV_ONCE_INIT; /** * Make AAC audio config object. * @see 1.6.2.1 "Syntax - AudioSpecificConfig" */static void put_audio_specific_config(AVCodecContext *avctx){    PutBitContext pb;    AACEncContext *s = avctx->priv_data;    int channels = s->channels - (s->channels == 8 ? 1 : 0);     init_put_bits(&pb, avctx->extradata, avctx->extradata_size);    put_bits(&pb, 5, s->profile+1); //profile    put_bits(&pb, 4, s->samplerate_index); //sample rate index    put_bits(&pb, 4, channels);    //GASpecificConfig    put_bits(&pb, 1, 0); //frame length - 1024 samples    put_bits(&pb, 1, 0); //does not depend on core coder    put_bits(&pb, 1, 0); //is not extension     //Explicitly Mark SBR absent    put_bits(&pb, 11, 0x2b7); //sync extension    put_bits(&pb, 5,  AOT_SBR);    put_bits(&pb, 1,  0);    flush_put_bits(&pb);} void ff_quantize_band_cost_cache_init(struct AACEncContext *s){    ++s->quantize_band_cost_cache_generation;    if (s->quantize_band_cost_cache_generation == 0) {        memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));        s->quantize_band_cost_cache_generation = 1;    }} #define WINDOW_FUNC(type) \static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \                                    SingleChannelElement *sce, \                                    const float *audio) WINDOW_FUNC(only_long){    const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;    const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;    float *out = sce->ret_buf;     fdsp->vector_fmul        (out,        audio,        lwindow, 1024);    fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);} WINDOW_FUNC(long_start){    const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;    const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;    float *out = sce->ret_buf;     fdsp->vector_fmul(out, audio, lwindow, 1024);    memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);    fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);    memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);} WINDOW_FUNC(long_stop){    const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;    const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;    float *out = sce->ret_buf;     memset(out, 0, sizeof(out[0]) * 448);    fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);    memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);    fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);} WINDOW_FUNC(eight_short){    const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;    const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;    const float *in = audio + 448;    float *out = sce->ret_buf;    int w;     for (w = 0; w < 8; w++) {        fdsp->vector_fmul        (out, in, w ? pwindow : swindow, 128);        out += 128;        in  += 128;        fdsp->vector_fmul_reverse(out, in, swindow, 128);        out += 128;    }} static void (*const apply_window[4])(AVFloatDSPContext *fdsp,                                     SingleChannelElement *sce,                                     const float *audio) = {    [ONLY_LONG_SEQUENCE]   = apply_only_long_window,    [LONG_START_SEQUENCE]  = apply_long_start_window,    [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,    [LONG_STOP_SEQUENCE]   = apply_long_stop_window}; static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,                                  float *audio){    int i;    const float *output = sce->ret_buf;     apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)        s->mdct1024.mdct_calc(&s->mdct1024, sce->coeffs, output);    else        for (i = 0; i < 1024; i += 128)            s->mdct128.mdct_calc(&s->mdct128, &sce->coeffs[i], output + i*2);    memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);    memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));} /** * Encode ics_info element. * @see Table 4.6 (syntax of ics_info) */static void put_ics_info(AACEncContext *s, IndividualChannelStream *info){    int w;     put_bits(&s->pb, 1, 0);                // ics_reserved bit    put_bits(&s->pb, 2, info->window_sequence[0]);    put_bits(&s->pb, 1, info->use_kb_window[0]);    if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {        put_bits(&s->pb, 6, info->max_sfb);        put_bits(&s->pb, 1, !!info->predictor_present);    } else {        put_bits(&s->pb, 4, info->max_sfb);        for (w = 1; w < 8; w++)            put_bits(&s->pb, 1, !info->group_len[w]);    }} /** * Encode MS data. * @see 4.6.8.1 "Joint Coding - M/S Stereo" */static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe){    int i, w;     put_bits(pb, 2, cpe->ms_mode);    if (cpe->ms_mode == 1)        for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])            for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)                put_bits(pb, 1, cpe->ms_mask[w*16 + i]);} /** * Produce integer coefficients from scalefactors provided by the model. */static void adjust_frame_information(ChannelElement *cpe, int chans){    int i, w, w2, g, ch;    int maxsfb, cmaxsfb;     for (ch = 0; ch < chans; ch++) {        IndividualChannelStream *ics = &cpe->ch[ch].ics;        maxsfb = 0;        cpe->ch[ch].pulse.num_pulse = 0;        for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {            for (w2 =  0; w2 < ics->group_len[w]; w2++) {                for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)                    ;                maxsfb = FFMAX(maxsfb, cmaxsfb);            }        }        ics->max_sfb = maxsfb;         //adjust zero bands for window groups        for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {            for (g = 0; g < ics->max_sfb; g++) {                i = 1;                for (w2 = w; w2 < w + ics->group_len[w]; w2++) {                    if (!cpe->ch[ch].zeroes[w2*16 + g]) {                        i = 0;                        break;                    }                }                cpe->ch[ch].zeroes[w*16 + g] = i;            }        }    }     if (chans > 1 && cpe->common_window) {        IndividualChannelStream *ics0 = &cpe->ch[0].ics;        IndividualChannelStream *ics1 = &cpe->ch[1].ics;        int msc = 0;        ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);        ics1->max_sfb = ics0->max_sfb;        for (w = 0; w < ics0->num_windows*16; w += 16)            for (i = 0; i < ics0->max_sfb; i++)                if (cpe->ms_mask[w+i])                    msc++;        if (msc == 0 || ics0->max_sfb == 0)            cpe->ms_mode = 0;        else            cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;    }} static void apply_intensity_stereo(ChannelElement *cpe){    int w, w2, g, i;    IndividualChannelStream *ics = &cpe->ch[0].ics;    if (!cpe->common_window)        return;    for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {        for (w2 =  0; w2 < ics->group_len[w]; w2++) {            int start = (w+w2) * 128;            for (g = 0; g < ics->num_swb; g++) {                int p  = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);                float scale = cpe->ch[0].is_ener[w*16+g];                if (!cpe->is_mask[w*16 + g]) {                    start += ics->swb_sizes[g];                    continue;                }                if (cpe->ms_mask[w*16 + g])                    p *= -1;                for (i = 0; i < ics->swb_sizes[g]; i++) {                    float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;                    cpe->ch[0].coeffs[start+i] = sum;                    cpe->ch[1].coeffs[start+i] = 0.0f;                }                start += ics->swb_sizes[g];            }        }    }} static void apply_mid_side_stereo(ChannelElement *cpe){    int w, w2, g, i;    IndividualChannelStream *ics = &cpe->ch[0].ics;    if (!cpe->common_window)        return;    for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {        for (w2 =  0; w2 < ics->group_len[w]; w2++) {            int start = (w+w2) * 128;            for (g = 0; g < ics->num_swb; g++) {                /* ms_mask can be used for other purposes in PNS and I/S,                 * so must not apply M/S if any band uses either, even if                 * ms_mask is set.                 */                if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g]                    || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT                    || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) {                    start += ics->swb_sizes[g];                    continue;                }                for (i = 0; i < ics->swb_sizes[g]; i++) {                    float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;                    float R = L - cpe->ch[1].coeffs[start+i];                    cpe->ch[0].coeffs[start+i] = L;                    cpe->ch[1].coeffs[start+i] = R;                }                start += ics->swb_sizes[g];            }        }    }} /** * Encode scalefactor band coding type. */static void encode_band_info(AACEncContext *s, SingleChannelElement *sce){    int w;     if (s->coder->set_special_band_scalefactors)        s->coder->set_special_band_scalefactors(s, sce);     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])        s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);} /** * Encode scalefactors. */static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,                                 SingleChannelElement *sce){    int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;    int off_is = 0, noise_flag = 1;    int i, w;     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {        for (i = 0; i < sce->ics.max_sfb; i++) {            if (!sce->zeroes[w*16 + i]) {                if (sce->band_type[w*16 + i] == NOISE_BT) {                    diff = sce->sf_idx[w*16 + i] - off_pns;                    off_pns = sce->sf_idx[w*16 + i];                    if (noise_flag-- > 0) {                        put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);                        continue;                    }                } else if (sce->band_type[w*16 + i] == INTENSITY_BT  ||                           sce->band_type[w*16 + i] == INTENSITY_BT2) {                    diff = sce->sf_idx[w*16 + i] - off_is;                    off_is = sce->sf_idx[w*16 + i];                } else {                    diff = sce->sf_idx[w*16 + i] - off_sf;                    off_sf = sce->sf_idx[w*16 + i];                }                diff += SCALE_DIFF_ZERO;                av_assert0(diff >= 0 && diff <= 120);                put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);            }        }    }} /** * Encode pulse data. */static void encode_pulses(AACEncContext *s, Pulse *pulse){    int i;     put_bits(&s->pb, 1, !!pulse->num_pulse);    if (!pulse->num_pulse)        return;     put_bits(&s->pb, 2, pulse->num_pulse - 1);    put_bits(&s->pb, 6, pulse->start);    for (i = 0; i < pulse->num_pulse; i++) {        put_bits(&s->pb, 5, pulse->pos[i]);        put_bits(&s->pb, 4, pulse->amp[i]);    }} /** * Encode spectral coefficients processed by psychoacoustic model. */static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce){    int start, i, w, w2;     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {        start = 0;        for (i = 0; i < sce->ics.max_sfb; i++) {            if (sce->zeroes[w*16 + i]) {                start += sce->ics.swb_sizes[i];                continue;            }            for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {                s->coder->quantize_and_encode_band(s, &s->pb,                                                   &sce->coeffs[start + w2*128],                                                   NULL, sce->ics.swb_sizes[i],                                                   sce->sf_idx[w*16 + i],                                                   sce->band_type[w*16 + i],                                                   s->lambda,                                                   sce->ics.window_clipping[w]);            }            start += sce->ics.swb_sizes[i];        }    }} /** * Downscale spectral coefficients for near-clipping windows to avoid artifacts */static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce){    int start, i, j, w;     if (sce->ics.clip_avoidance_factor < 1.0f) {        for (w = 0; w < sce->ics.num_windows; w++) {            start = 0;            for (i = 0; i < sce->ics.max_sfb; i++) {                float *swb_coeffs = &sce->coeffs[start + w*128];                for (j = 0; j < sce->ics.swb_sizes[i]; j++)                    swb_coeffs[j] *= sce->ics.clip_avoidance_factor;                start += sce->ics.swb_sizes[i];            }        }    }} /** * Encode one channel of audio data. */static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,                                     SingleChannelElement *sce,                                     int common_window){    put_bits(&s->pb, 8, sce->sf_idx[0]);    if (!common_window) {        put_ics_info(s, &sce->ics);        if (s->coder->encode_main_pred)            s->coder->encode_main_pred(s, sce);        if (s->coder->encode_ltp_info)            s->coder->encode_ltp_info(s, sce, 0);    }    encode_band_info(s, sce);    encode_scale_factors(avctx, s, sce);    encode_pulses(s, &sce->pulse);    put_bits(&s->pb, 1, !!sce->tns.present);    if (s->coder->encode_tns_info)        s->coder->encode_tns_info(s, sce);    put_bits(&s->pb, 1, 0); //ssr    encode_spectral_coeffs(s, sce);    return 0;} /** * Write some auxiliary information about the created AAC file. */static void put_bitstream_info(AACEncContext *s, const char *name){    int i, namelen, padbits;     namelen = strlen(name) + 2;    put_bits(&s->pb, 3, TYPE_FIL);    put_bits(&s->pb, 4, FFMIN(namelen, 15));    if (namelen >= 15)        put_bits(&s->pb, 8, namelen - 14);    put_bits(&s->pb, 4, 0); //extension type - filler    padbits = -put_bits_count(&s->pb) & 7;    avpriv_align_put_bits(&s->pb);    for (i = 0; i < namelen - 2; i++)        put_bits(&s->pb, 8, name[i]);    put_bits(&s->pb, 12 - padbits, 0);} /* * Copy input samples. * Channels are reordered from libavcodec's default order to AAC order. */static void copy_input_samples(AACEncContext *s, const AVFrame *frame){    int ch;    int end = 2048 + (frame ? frame->nb_samples : 0);    const uint8_t *channel_map = aac_chan_maps[s->channels - 1];     /* copy and remap input samples */    for (ch = 0; ch < s->channels; ch++) {        /* copy last 1024 samples of previous frame to the start of the current frame */        memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));         /* copy new samples and zero any remaining samples */        if (frame) {            memcpy(&s->planar_samples[ch][2048],

Lines 1–500 of 1096.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t