[FFmpeg-devel] [RFC] AAC Encoder
Michael Niedermayer
michaelni
Mon Aug 18 03:26:33 CEST 2008
On Sun, Aug 17, 2008 at 02:57:48PM +0300, Kostya wrote:
> On Sun, Aug 17, 2008 at 03:08:58AM +0200, Michael Niedermayer wrote:
[..]
> /*
> * AAC encoder psychoacoustic model
> * 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
> */
>
> #ifndef FFMPEG_AACPSY_H
> #define FFMPEG_AACPSY_H
>
> #include "avcodec.h"
> #include "aac.h"
> #include "lowpass.h"
>
> enum AACPsyModelType{
> AAC_PSY_TEST, ///< a sample model to exercise encoder
> AAC_PSY_3GPP, ///< model following recommendations from 3GPP TS 26.403
>
> AAC_NB_PSY_MODELS ///< total number of psychoacoustic models, since it's not a part of the ABI new models can be added freely
> };
ok
[...]
> /**
> * context used by psychoacoustic model
> */
> typedef struct AACPsyContext {
> AVCodecContext *avctx; ///< encoder context
ok
[...]
> /**
> * Cleanup model context at the end.
> *
> * @param ctx model context
> */
> void ff_aac_psy_end(AACPsyContext *ctx);
>
> #endif /* FFMPEG_AACPSY_H */
>
ok
> /*
> * AAC encoder psychoacoustic model
> * 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 aacpsy.c
> * AAC encoder psychoacoustic model
> */
>
> #include "avcodec.h"
> #include "aacpsy.h"
> #include "aactab.h"
>
> /***********************************
> * TODOs:
> * General:
> * better audio preprocessing (add DC highpass filter?)
> * more psy models
> * maybe improve coefficient quantization function in some way
> *
> * 3GPP-based psy model:
> * thresholds linearization after their modifications for attaining given bitrate
> * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
> * control quality for quality-based output
> **********************************/
>
ok
> /**
> * Quantize one coefficient.
> * @return absolute value of the quantized coefficient
> * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
> */
> static av_always_inline int quant(float coef, const float Q)
> {
> return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
> }
ok (note this does not mean that RD optimal quantization wouldnt be needed
its just that a simple quant is usefull as start value)
[...]
> static inline float calc_distortion(float *c, int size, int scale_idx)
s/calc_distortion/get_approximate_quant_error/
> {
> int i;
> int q;
> float coef, unquant, sum = 0.0f;
> const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
> const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
> for(i = 0; i < size; i++){
> coef = fabs(c[i]);
> q = quant(c[i], Q);
> unquant = (q * cbrt(q)) * IQ;
> sum += (coef - unquant) * (coef - unquant);
> }
> return sum;
> }
ok, this maybe can be usefull for some fast approximation of the
quantization error.
[...]
> 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 ? 1 : 2;
not RD optimal either though this one is rather minor, i dont belive much
can be gained by calculating this optimally
[...]
>
> /**
> * constants for 3GPP AAC psychoacoustic model
> * @{
> */
ok
[...]
> #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
> #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
ok
[...]
> /**
> * @}
> */
>
> /**
> * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
> */
> typedef struct Psy3gppBand{
> float energy; ///< band energy
> float ffac; ///< form factor
ok
[...]
> }Psy3gppBand;
ok
[...]
> /**
> * psychoacoustic model frame type-dependent coefficients
> */
> typedef struct Psy3gppCoeffs{
> float ath [64]; ///< absolute threshold of hearing per bands
> float barks [64]; ///< Bark value for each spectral band in long frame
> float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
> float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
> }Psy3gppCoeffs;
ok
[...]
> /**
> * Calculate Bark value for given line.
> */
> static inline float calc_bark(float f)
> {
> return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
> }
ok
[...]
> for(ch = 0; ch < chans; ch++){
> IndividualChannelStream *ics = &cpe->ch[ch].ics;
> pch->a[ch] = pch->b[ch] = pch->pe[ch] = pch->thr[ch] = 0.0f;
> for(w = 0; w < ics->num_windows*16; w += 16){
> for(g = 0; g < ics->num_swb; g++){
> Psy3gppBand *band = &pch->band[ch][w+g];
> if(band->energy != 0.0)
> calc_pe(band, ics->swb_sizes[g]);
> if(band->thr < band->energy){
> pch->a[ch] += band->a;
> pch->b[ch] += band->b;
> pch->pe[ch] += band->pe;
> pch->thr[ch] += band->thr;
> }
> }
> }
> }
[...]
> //add correction factor to thresholds and recalculate perceptual entropy
> for(ch = 0; ch < chans; ch++){
> IndividualChannelStream *ics = &cpe->ch[ch].ics;
> pch->a[ch] = pch->b[ch] = pch->pe[ch] = pch->thr[ch] = 0.0;
> pe = 0.0f;
> for(w = 0; w < ics->num_windows*16; w += 16){
> for(g = 0; g < ics->num_swb; g++){
> Psy3gppBand *band = &pch->band[ch][w+g];
> band->thr = modify_thr(band->thr, r);
> calc_pe(band, ics->swb_sizes[g]);
> if(band->thr < band->energy){
> pch->a[ch] += band->a;
> pch->b[ch] += band->b;
> pch->pe[ch] += band->pe;
> pch->thr[ch] += band->thr;
> }
> }
this code looks dupicated
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
No human being will ever know the Truth, for even if they happen to say it
by chance, they would not even known they had done so. -- Xenophanes
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20080818/cc96eb50/attachment.pgp>
More information about the ffmpeg-devel
mailing list