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
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "psymodel.h"
|
|
|
|
#include "iirfilter.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/mem.h"
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
extern const FFPsyModel ff_aac_psy_model;
|
|
|
|
|
2011-06-15 11:50:25 +03:00
|
|
|
av_cold 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)
|
2009-07-08 23:01:31 +03:00
|
|
|
{
|
2011-06-15 11:50:25 +03:00
|
|
|
int i, j, k = 0;
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
ctx->avctx = avctx;
|
2014-04-14 00:31:12 +03:00
|
|
|
ctx->ch = av_mallocz_array(sizeof(ctx->ch[0]), avctx->channels * 2);
|
|
|
|
ctx->group = av_mallocz_array(sizeof(ctx->group[0]), num_groups);
|
|
|
|
ctx->bands = av_malloc_array (sizeof(ctx->bands[0]), num_lens);
|
|
|
|
ctx->num_bands = av_malloc_array (sizeof(ctx->num_bands[0]), num_lens);
|
2015-05-31 14:54:55 +02:00
|
|
|
|
|
|
|
if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
|
|
|
|
ff_psy_end(ctx);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
|
|
|
|
memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
|
2011-06-15 11:50:25 +03:00
|
|
|
|
|
|
|
/* assign channels to groups (with virtual channels for coupling) */
|
|
|
|
for (i = 0; i < num_groups; i++) {
|
|
|
|
/* NOTE: Add 1 to handle the AAC chan_config without modification.
|
|
|
|
* This has the side effect of allowing an array of 0s to map
|
|
|
|
* to one channel per group.
|
|
|
|
*/
|
|
|
|
ctx->group[i].num_ch = group_map[i] + 1;
|
|
|
|
for (j = 0; j < ctx->group[i].num_ch * 2; j++)
|
|
|
|
ctx->group[i].ch[j] = &ctx->ch[k++];
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:36:45 +03:00
|
|
|
switch (ctx->avctx->codec_id) {
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_AAC:
|
2009-07-08 23:01:31 +03:00
|
|
|
ctx->model = &ff_aac_psy_model;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-08 23:36:45 +03:00
|
|
|
if (ctx->model->init)
|
2009-07-08 23:01:31 +03:00
|
|
|
return ctx->model->init(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:50:25 +03:00
|
|
|
FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel)
|
|
|
|
{
|
|
|
|
int i = 0, ch = 0;
|
|
|
|
|
|
|
|
while (ch <= channel)
|
|
|
|
ch += ctx->group[i++].num_ch;
|
|
|
|
|
|
|
|
return &ctx->group[i-1];
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
av_cold void ff_psy_end(FFPsyContext *ctx)
|
|
|
|
{
|
2013-07-01 00:25:18 +03:00
|
|
|
if (ctx->model && ctx->model->end)
|
2009-07-08 23:01:31 +03:00
|
|
|
ctx->model->end(ctx);
|
|
|
|
av_freep(&ctx->bands);
|
|
|
|
av_freep(&ctx->num_bands);
|
2011-06-15 11:50:25 +03:00
|
|
|
av_freep(&ctx->group);
|
|
|
|
av_freep(&ctx->ch);
|
2009-07-08 23:01:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct FFPsyPreprocessContext{
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
float stereo_att;
|
|
|
|
struct FFIIRFilterCoeffs *fcoeffs;
|
|
|
|
struct FFIIRFilterState **fstate;
|
2013-03-06 16:55:06 +03:00
|
|
|
struct FFIIRFilterContext fiir;
|
2009-07-08 23:01:31 +03:00
|
|
|
}FFPsyPreprocessContext;
|
|
|
|
|
|
|
|
#define FILT_ORDER 4
|
|
|
|
|
|
|
|
av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FFPsyPreprocessContext *ctx;
|
|
|
|
int i;
|
2010-04-02 17:19:39 +03:00
|
|
|
float cutoff_coeff = 0;
|
2009-07-09 00:16:06 +03:00
|
|
|
ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
|
2015-05-31 14:54:55 +02:00
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
2009-07-08 23:01:31 +03:00
|
|
|
ctx->avctx = avctx;
|
|
|
|
|
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
|
|
|
/* AAC has its own LP method */
|
|
|
|
if (avctx->codec_id != AV_CODEC_ID_AAC) {
|
2009-07-14 08:22:23 +03:00
|
|
|
if (avctx->cutoff > 0)
|
|
|
|
cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
|
2009-07-08 23:01:31 +03:00
|
|
|
|
2013-03-07 21:28:30 +03:00
|
|
|
if (cutoff_coeff && cutoff_coeff < 0.98)
|
2011-01-20 20:28:17 +02:00
|
|
|
ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
|
|
|
|
FF_FILTER_MODE_LOWPASS, FILT_ORDER,
|
|
|
|
cutoff_coeff, 0.0, 0.0);
|
2009-07-08 23:36:45 +03:00
|
|
|
if (ctx->fcoeffs) {
|
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
|
|
|
ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
|
2009-07-08 23:36:45 +03:00
|
|
|
for (i = 0; i < avctx->channels; i++)
|
2009-07-08 23:01:31 +03:00
|
|
|
ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
|
|
|
|
}
|
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
|
|
|
}
|
2013-03-06 16:55:06 +03:00
|
|
|
|
|
|
|
ff_iir_filter_init(&ctx->fiir);
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-12-15 05:43:56 +03:00
|
|
|
int ch;
|
|
|
|
int frame_size = ctx->avctx->frame_size;
|
2013-03-06 16:55:06 +03:00
|
|
|
FFIIRFilterContext *iir = &ctx->fiir;
|
2011-12-15 05:43:56 +03:00
|
|
|
|
2009-07-08 23:36:45 +03:00
|
|
|
if (ctx->fstate) {
|
2009-07-09 00:36:33 +03:00
|
|
|
for (ch = 0; ch < channels; ch++)
|
2013-03-06 16:55:06 +03:00
|
|
|
iir->filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
|
|
|
|
&audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
|
2009-07-08 23:01:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
2014-10-14 19:30:52 +03:00
|
|
|
ff_iir_filter_free_coeffsp(&ctx->fcoeffs);
|
2009-07-08 23:01:31 +03:00
|
|
|
if (ctx->fstate)
|
|
|
|
for (i = 0; i < ctx->avctx->channels; i++)
|
2014-10-14 19:52:51 +03:00
|
|
|
ff_iir_filter_free_statep(&ctx->fstate[i]);
|
2009-07-08 23:01:31 +03:00
|
|
|
av_freep(&ctx->fstate);
|
2010-07-10 12:14:19 +03:00
|
|
|
av_free(ctx);
|
2009-07-08 23:01:31 +03:00
|
|
|
}
|