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"
|
|
|
|
|
2015-08-21 19:43:09 +02:00
|
|
|
#include "lpc.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;
|
2015-08-21 20:27:38 +02:00
|
|
|
int tns;
|
2015-08-21 20:20:22 +02:00
|
|
|
int pred;
|
2015-07-02 20:13:07 +02:00
|
|
|
int intensity_stereo;
|
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);
|
2015-08-21 19:53:14 +02:00
|
|
|
void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
|
2015-07-21 03:53:24 +02:00
|
|
|
int scale_idx, int cb, const float lambda, int rtz);
|
2015-08-21 20:27:38 +02:00
|
|
|
void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
|
2015-08-21 20:38:05 +02:00
|
|
|
void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
|
|
|
|
void (*adjust_common_prediction)(struct AACEncContext *s, ChannelElement *cpe);
|
|
|
|
void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
|
2015-09-01 07:44:07 +02:00
|
|
|
void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
|
2015-07-02 20:13:04 +02:00
|
|
|
void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
|
2015-07-29 06:44:24 +02:00
|
|
|
void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 22:29:50 +02:00
|
|
|
void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
|
2015-08-21 20:27:38 +02:00
|
|
|
void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
|
2015-07-29 06:44:24 +02:00
|
|
|
void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
|
|
|
|
void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
|
2015-08-21 20:38:05 +02:00
|
|
|
void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
|
2009-07-08 22:36:45 +02:00
|
|
|
} AACCoefficientsEncoder;
|
2009-07-08 22:01:31 +02:00
|
|
|
|
|
|
|
extern AACCoefficientsEncoder ff_aac_coders[];
|
|
|
|
|
2015-10-12 08:56:22 +02:00
|
|
|
typedef struct AACQuantizeBandCostCacheEntry {
|
|
|
|
float rd;
|
|
|
|
float energy;
|
|
|
|
int bits; ///< -1 means uninitialized entry
|
|
|
|
char cb;
|
|
|
|
char rtz;
|
|
|
|
char padding[2]; ///< Keeps the entry size a multiple of 32 bits
|
|
|
|
} AACQuantizeBandCostCacheEntry;
|
|
|
|
|
2009-07-08 22:01:31 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2015-08-21 20:20:22 +02:00
|
|
|
int profile; ///< copied from avctx
|
2015-08-21 19:43:09 +02:00
|
|
|
LPCContext lpc; ///< used by TNS
|
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;
|
2015-09-23 07:13:56 +02:00
|
|
|
int cur_channel; ///< current channel for coder context
|
2009-07-08 22:01:31 +02:00
|
|
|
int last_frame;
|
2015-09-06 16:06:34 +02:00
|
|
|
int random_state;
|
2009-07-08 22:01:31 +02:00
|
|
|
float lambda;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 22:29:50 +02:00
|
|
|
float lambda_sum; ///< sum(lambda), for Qvg reporting
|
|
|
|
int lambda_count; ///< count(lambda), for Qvg reporting
|
2015-09-23 07:13:56 +02:00
|
|
|
enum RawDataBlockType cur_type; ///< channel group type cur_channel belongs to
|
|
|
|
|
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
|
|
|
|
2015-10-12 08:56:22 +02:00
|
|
|
AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
|
|
|
|
|
2011-12-15 03:43:56 +01:00
|
|
|
struct {
|
|
|
|
float *samples;
|
|
|
|
} buffer;
|
2009-07-08 22:01:31 +02:00
|
|
|
} AACEncContext;
|
|
|
|
|
2013-03-06 14:55:05 +01:00
|
|
|
void ff_aac_coder_init_mips(AACEncContext *c);
|
2015-10-12 08:56:22 +02:00
|
|
|
void ff_quantize_band_cost_cache_init(struct AACEncContext *s);
|
|
|
|
|
2013-03-06 14:55:05 +01:00
|
|
|
|
2009-07-08 22:01:31 +02:00
|
|
|
#endif /* AVCODEC_AACENC_H */
|