2009-07-08 23:01:31 +03:00
|
|
|
/*
|
|
|
|
* audio 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 AVCODEC_PSYMODEL_H
|
|
|
|
#define AVCODEC_PSYMODEL_H
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
/** maximum possible number of bands */
|
|
|
|
#define PSY_MAX_BANDS 128
|
2011-04-05 10:05:22 +03:00
|
|
|
/** maximum number of channels */
|
|
|
|
#define PSY_MAX_CHANS 20
|
2009-07-08 23:01:31 +03:00
|
|
|
|
2016-10-13 23:08:01 +02:00
|
|
|
/* cutoff for VBR is purposely increased, since LP filtering actually
|
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
|
|
|
* hinders VBR performance rather than the opposite
|
|
|
|
*/
|
|
|
|
#define AAC_CUTOFF_FROM_BITRATE(bit_rate,channels,sample_rate) (bit_rate ? FFMIN3(FFMIN3( \
|
|
|
|
FFMAX(bit_rate/channels/5, bit_rate/channels*15/32 - 5500), \
|
|
|
|
3000 + bit_rate/channels/4, \
|
|
|
|
12000 + bit_rate/channels/16), \
|
|
|
|
22000, \
|
|
|
|
sample_rate / 2): (sample_rate / 2))
|
|
|
|
#define AAC_CUTOFF(s) ( \
|
2017-03-26 02:35:15 +02:00
|
|
|
(s->flags & AV_CODEC_FLAG_QSCALE) \
|
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
|
|
|
? s->sample_rate / 2 \
|
|
|
|
: AAC_CUTOFF_FROM_BITRATE(s->bit_rate, s->channels, s->sample_rate) \
|
|
|
|
)
|
2012-07-29 20:49:22 +03:00
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
/**
|
|
|
|
* single band psychoacoustic information
|
|
|
|
*/
|
2009-07-08 23:36:45 +03:00
|
|
|
typedef struct FFPsyBand {
|
2009-07-08 23:01:31 +03:00
|
|
|
int bits;
|
|
|
|
float energy;
|
|
|
|
float threshold;
|
2015-06-26 22:16:35 +02:00
|
|
|
float spread; /* Energy spread over the band */
|
2009-07-08 23:36:45 +03:00
|
|
|
} FFPsyBand;
|
2009-07-08 23:01:31 +03:00
|
|
|
|
2011-06-15 11:50:25 +03:00
|
|
|
/**
|
|
|
|
* single channel psychoacoustic information
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyChannel {
|
|
|
|
FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
|
|
|
|
float entropy; ///< total PE for this channel
|
|
|
|
} FFPsyChannel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* psychoacoustic information for an arbitrary group of channels
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyChannelGroup {
|
|
|
|
FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
|
|
|
|
uint8_t num_ch; ///< number of channels in this group
|
|
|
|
uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
|
|
|
|
} FFPsyChannelGroup;
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
/**
|
|
|
|
* windowing related information
|
|
|
|
*/
|
2009-07-08 23:36:45 +03:00
|
|
|
typedef struct FFPsyWindowInfo {
|
2009-07-08 23:01:31 +03:00
|
|
|
int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
|
|
|
|
int window_shape; ///< window shape (sine/KBD/whatever)
|
|
|
|
int num_windows; ///< number of windows in a frame
|
|
|
|
int grouping[8]; ///< window grouping (for e.g. AAC)
|
2015-07-21 03:53:24 +02:00
|
|
|
float clipping[8]; ///< maximum absolute normalized intensity in the given window for clip avoidance
|
2009-07-08 23:01:31 +03:00
|
|
|
int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
|
2009-07-08 23:36:45 +03:00
|
|
|
} FFPsyWindowInfo;
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* context used by psychoacoustic model
|
|
|
|
*/
|
2009-07-08 23:36:45 +03:00
|
|
|
typedef struct FFPsyContext {
|
2009-07-08 23:01:31 +03:00
|
|
|
AVCodecContext *avctx; ///< encoder context
|
|
|
|
const struct FFPsyModel *model; ///< encoder-specific model functions
|
|
|
|
|
2011-06-15 11:50:25 +03:00
|
|
|
FFPsyChannel *ch; ///< single channel information
|
|
|
|
FFPsyChannelGroup *group; ///< channel group information
|
|
|
|
int num_groups; ///< number of channel groups
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 08:28:36 +02:00
|
|
|
int cutoff; ///< lowpass frequency cutoff for analysis
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
|
|
|
|
int *num_bands; ///< number of scalefactor bands for possible frame sizes
|
|
|
|
int num_lens; ///< number of scalefactor band sets
|
|
|
|
|
2011-04-05 10:05:22 +03:00
|
|
|
struct {
|
|
|
|
int size; ///< size of the bitresevoir in bits
|
|
|
|
int bits; ///< number of bits used in the bitresevoir
|
2015-09-23 07:13:56 +02:00
|
|
|
int alloc; ///< number of bits allocated by the psy, or -1 if no allocation was done
|
2011-04-05 10:05:22 +03:00
|
|
|
} bitres;
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
void* model_priv_data; ///< psychoacoustic model implementation private data
|
2009-07-08 23:36:45 +03:00
|
|
|
} FFPsyContext;
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* codec-specific psychoacoustic model implementation
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyModel {
|
|
|
|
const char *name;
|
|
|
|
int (*init) (FFPsyContext *apc);
|
2011-05-06 10:19:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Suggest window sequence for channel.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
* @param audio samples for the current frame
|
|
|
|
* @param la lookahead samples (NULL when unavailable)
|
|
|
|
* @param channel number of channel element to analyze
|
|
|
|
* @param prev_type previous window type
|
|
|
|
*
|
|
|
|
* @return suggested window information in a structure
|
|
|
|
*/
|
2011-12-18 04:45:55 +03:00
|
|
|
FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
|
2011-05-06 10:19:52 +03:00
|
|
|
|
|
|
|
/**
|
2011-06-15 11:50:25 +03:00
|
|
|
* Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
|
2011-05-06 10:19:52 +03:00
|
|
|
*
|
2011-06-15 11:50:25 +03:00
|
|
|
* @param ctx model context
|
|
|
|
* @param channel channel number of the first channel in the group to perform analysis on
|
|
|
|
* @param coeffs array of pointers to the transformed coefficients
|
|
|
|
* @param wi window information for the channels in the group
|
2011-05-06 10:19:52 +03:00
|
|
|
*/
|
2011-05-19 08:23:22 +03:00
|
|
|
void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
|
2011-05-06 10:19:52 +03:00
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
void (*end) (FFPsyContext *apc);
|
2009-07-08 23:36:45 +03:00
|
|
|
} FFPsyModel;
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize psychoacoustic model.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
* @param avctx codec context
|
|
|
|
* @param num_lens number of possible frame lengths
|
|
|
|
* @param bands scalefactor band lengths for all frame lengths
|
|
|
|
* @param num_bands number of scalefactor bands for all frame lengths
|
2011-06-15 11:50:25 +03:00
|
|
|
* @param num_groups number of channel groups
|
|
|
|
* @param group_map array with # of channels in group - 1, for each group
|
2009-07-08 23:01:31 +03:00
|
|
|
*
|
|
|
|
* @return zero if successful, a negative value if not
|
|
|
|
*/
|
2013-02-09 18:19:27 +03:00
|
|
|
int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
|
|
|
|
const uint8_t **bands, const int *num_bands,
|
|
|
|
int num_groups, const uint8_t *group_map);
|
2011-06-15 11:50:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what group a channel belongs to.
|
|
|
|
*
|
|
|
|
* @param ctx psymodel context
|
|
|
|
* @param channel channel to locate the group for
|
|
|
|
*
|
|
|
|
* @return pointer to the FFPsyChannelGroup this channel belongs to
|
|
|
|
*/
|
|
|
|
FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup model context at the end.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
*/
|
2013-02-09 18:19:27 +03:00
|
|
|
void ff_psy_end(FFPsyContext *ctx);
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
* Audio preprocessing stuff. *
|
|
|
|
* This should be moved into some audio filter eventually. *
|
|
|
|
**************************************************************************/
|
|
|
|
struct FFPsyPreprocessContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* psychoacoustic model audio preprocessing initialization
|
|
|
|
*/
|
2013-02-09 18:19:27 +03:00
|
|
|
struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preprocess several channel in audio frame in order to compress it better.
|
|
|
|
*
|
|
|
|
* @param ctx preprocessing context
|
2011-12-15 05:43:56 +03:00
|
|
|
* @param audio samples to be filtered (in place)
|
|
|
|
* @param channels number of channel to preprocess
|
2009-07-08 23:01:31 +03:00
|
|
|
*/
|
2011-12-15 05:43:56 +03:00
|
|
|
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup audio preprocessing module.
|
|
|
|
*/
|
2013-02-09 18:19:27 +03:00
|
|
|
void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
#endif /* AVCODEC_PSYMODEL_H */
|