2009-07-08 22:01:31 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVCODEC_AACENC_H
|
|
|
|
#define AVCODEC_AACENC_H
|
|
|
|
|
2012-05-21 18:58:41 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2009-07-08 22:01:31 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "put_bits.h"
|
|
|
|
|
|
|
|
#include "aac.h"
|
2012-01-28 18:28:01 +01:00
|
|
|
#include "audio_frame_queue.h"
|
2009-07-08 22:01:31 +02:00
|
|
|
#include "psymodel.h"
|
|
|
|
|
2013-09-12 17:25:30 +02:00
|
|
|
typedef enum AACCoder {
|
|
|
|
AAC_CODER_FAAC = 0,
|
|
|
|
AAC_CODER_ANMR,
|
|
|
|
AAC_CODER_TWOLOOP,
|
|
|
|
AAC_CODER_FAST,
|
2013-09-11 05:23:32 +02:00
|
|
|
|
2013-09-12 17:25:30 +02:00
|
|
|
AAC_CODER_NB,
|
|
|
|
}AACCoder;
|
2011-11-27 21:19:07 +01:00
|
|
|
|
2011-06-01 07:38:09 +02:00
|
|
|
typedef struct AACEncOptions {
|
|
|
|
int stereo_mode;
|
2011-11-27 21:19:30 +01:00
|
|
|
int aac_coder;
|
aaccoder: Implement Perceptual Noise Substitution for AAC
This commit implements the perceptual noise substitution AAC extension. This is a proof of concept
implementation, and as such, is not enabled by default. This is the fourth revision of this patch,
made after some problems were noted out. Any changes made since the previous revisions have been indicated.
In order to extend the encoder to use an additional codebook, the array holding each codebook has been
modified with two additional entries - 13 for the NOISE_BT codebook and 12 which has a placeholder function.
The cost system was modified to skip the 12th entry using an array to map the input and outputs it has. It
also does not accept using the 13th codebook for any band which is not marked as containing noise, thereby
restricting its ability to arbitrarily choose it for bands. The use of arrays allows the system to be easily
extended to allow for intensity stereo encoding, which uses additional codebooks.
The 12th entry in the codebook function array points to a function which stops the execution of the program
by calling an assert with an always 'false' argument. It was pointed out in an email discussion with
Claudio Freire that having a 'NULL' entry can result in unexpected behaviour and could be used as
a security hole. There is no danger of this function being called during encoding due to the codebook maps introduced.
Another change from version 1 of the patch is the addition of an argument to the encoder, '-aac_pns' to
enable and disable the PNS. This currently defaults to disable the PNS, as it is experimental.
The switch will be removed in the future, when the algorithm to select noise bands has been improved.
The current algorithm simply compares the energy to the threshold (multiplied by a constant) to determine
noise, however the FFPsyBand structure contains other useful figures to determine which bands carry noise more accurately.
Some of the sample files provided triggered an assertion when the parameter to tune the threshold was set to
a value of '2.2'. Claudio Freire reported the problem's source could be in the range of the scalefactor
indices for noise and advised to measure the minimal index and clip anything above the maximum allowed
value. This has been implemented and all the files which used to trigger the asserion now encode without error.
The third revision of the problem also removes unneded variabes and comparisons. All of them were
redundant and were of little use for when the PNS implementation would be extended.
The fourth revision moved the clipping of the noise scalefactors outside the second loop of the two-loop
algorithm in order to prevent their redundant calculations. Also, freq_mult has been changed to a float
variable due to the fact that rounding errors can prove to be a problem at low frequencies.
Considerations were taken whether the entire expression could be evaluated inside the expression
, but in the end it was decided that it would be for the best if just the type of the variable were
to change. Claudio Freire reported the two problems. There is no change of functionality
(except for low sampling frequencies) so the spectral demonstrations at the end of this commit's message were not updated.
Finally, the way energy values are converted to scalefactor indices has changed since the first commit,
as per the suggestion of Claudio Freire. This may still have some drawbacks, but unlike the first commit
it works without having redundant offsets and outputs what the decoder expects to have, in terms of the
ranges of the scalefactor indices.
Some spectral comparisons: https://trac.ffmpeg.org/attachment/wiki/Encode/AAC/Original.png (original),
https://trac.ffmpeg.org/attachment/wiki/Encode/AAC/PNS_NO.png (encoded without PNS),
https://trac.ffmpeg.org/attachment/wiki/Encode/AAC/PNS1.2.png (encoded with PNS, const = 1.2),
https://trac.ffmpeg.org/attachment/wiki/Encode/AAC/Difference1.png (spectral difference).
The constant is the value which multiplies the threshold when it gets compared to the energy, larger
values means more noise will be substituded by PNS values. Example when const = 2.2:
https://trac.ffmpeg.org/attachment/wiki/Encode/AAC/PNS_2.2.png
Reviewed-by: Claudio Freire <klaussfreire@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2015-04-15 13:18:42 +02:00
|
|
|
int pns;
|
2011-06-01 07:38:09 +02:00
|
|
|
} AACEncOptions;
|
|
|
|
|
2009-07-08 22:01:31 +02:00
|
|
|
struct AACEncContext;
|
|
|
|
|
2009-07-08 22:36:45 +02:00
|
|
|
typedef struct AACCoefficientsEncoder {
|
2009-07-08 22:01:31 +02:00
|
|
|
void (*search_for_quantizers)(AVCodecContext *avctx, struct AACEncContext *s,
|
|
|
|
SingleChannelElement *sce, const float lambda);
|
|
|
|
void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce,
|
|
|
|
int win, int group_len, const float lambda);
|
|
|
|
void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, int size,
|
|
|
|
int scale_idx, int cb, const float lambda);
|
2015-07-02 20:13:04 +02:00
|
|
|
void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
|
2009-07-08 22:01:31 +02:00
|
|
|
void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe, const float lambda);
|
2009-07-08 22:36:45 +02:00
|
|
|
} AACCoefficientsEncoder;
|
2009-07-08 22:01:31 +02:00
|
|
|
|
|
|
|
extern AACCoefficientsEncoder ff_aac_coders[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AAC encoder context
|
|
|
|
*/
|
|
|
|
typedef struct AACEncContext {
|
2011-06-01 07:38:09 +02:00
|
|
|
AVClass *av_class;
|
|
|
|
AACEncOptions options; ///< encoding options
|
2009-07-08 22:01:31 +02:00
|
|
|
PutBitContext pb;
|
2009-09-20 19:30:20 +02:00
|
|
|
FFTContext mdct1024; ///< long (1024 samples) frame transform context
|
|
|
|
FFTContext mdct128; ///< short (128 samples) frame transform context
|
2014-11-29 18:58:13 +01:00
|
|
|
AVFloatDSPContext *fdsp;
|
2011-12-15 03:43:56 +01:00
|
|
|
float *planar_samples[6]; ///< saved preprocessed input
|
2009-07-08 22:01:31 +02:00
|
|
|
|
|
|
|
int samplerate_index; ///< MPEG-4 samplerate index
|
2011-12-07 23:20:10 +01:00
|
|
|
int channels; ///< channel count
|
2011-06-29 23:33:33 +02:00
|
|
|
const uint8_t *chan_map; ///< channel configuration map
|
2009-07-08 22:01:31 +02:00
|
|
|
|
|
|
|
ChannelElement *cpe; ///< channel elements
|
|
|
|
FFPsyContext psy;
|
|
|
|
struct FFPsyPreprocessContext* psypp;
|
|
|
|
AACCoefficientsEncoder *coder;
|
|
|
|
int cur_channel;
|
|
|
|
int last_frame;
|
|
|
|
float lambda;
|
2012-01-28 18:28:01 +01:00
|
|
|
AudioFrameQueue afq;
|
2010-05-14 18:49:40 +02:00
|
|
|
DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients
|
2011-04-25 11:39:01 +02:00
|
|
|
DECLARE_ALIGNED(32, float, scoefs)[1024]; ///< scaled coefficients
|
2011-12-15 03:43:56 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
float *samples;
|
|
|
|
} buffer;
|
2009-07-08 22:01:31 +02:00
|
|
|
} AACEncContext;
|
|
|
|
|
2012-01-06 22:24:37 +01:00
|
|
|
extern float ff_aac_pow34sf_tab[428];
|
|
|
|
|
2013-03-06 14:55:05 +01:00
|
|
|
void ff_aac_coder_init_mips(AACEncContext *c);
|
|
|
|
|
2009-07-08 22:01:31 +02:00
|
|
|
#endif /* AVCODEC_AACENC_H */
|