[FFmpeg-devel] [PATCH] AAC Encoder: clipping avoidance

Michael Niedermayer michael at niedermayer.cc
Sat Jul 18 01:42:59 CEST 2015


On Fri, Jul 17, 2015 at 08:19:17PM -0300, Claudio Freire wrote:
> On Fri, Jul 17, 2015 at 7:36 PM, Michael Niedermayer
> <michael at niedermayer.cc> wrote:
> >> diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
> >> index f05f51b..6ff95b1 100644
> >> --- a/libavcodec/aacenc.c
> >> +++ b/libavcodec/aacenc.c
> >> @@ -46,6 +46,7 @@
> >>  #include "psymodel.h"
> >>
> >>  #define AAC_MAX_CHANNELS 6
> >> +#define CLIP_AVOIDANCE_FACTOR 0.95f
> >>
> >>  #define ERROR_IF(cond, ...) \
> >>      if (cond) { \
> >> @@ -473,7 +474,29 @@ static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
> >>                                                     sce->ics.swb_sizes[i],
> >>                                                     sce->sf_idx[w*16 + i],
> >>                                                     sce->band_type[w*16 + i],
> >> -                                                   s->lambda);
> >> +                                                   s->lambda, sce->ics.window_clipping[w]);
> >> +            start += sce->ics.swb_sizes[i];
> >> +        }
> >> +    }
> >> +}
> >> +
> >> +/**
> >> + * Downscale spectral coefficients for near-clipping windows to avoid artifacts
> >> + */
> >> +static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
> >> +{
> >> +    int start, i, j, w, w2;
> >> +
> >> +    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
> >> +        start = 0;
> >> +        for (i = 0; i < sce->ics.max_sfb; i++) {
> >> +            if (sce->ics.window_clipping[w]) {
> >> +                for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
> >> +                    float *swb_coeffs = sce->coeffs + start + w2*128;
> >> +                    for (j = 0; j < sce->ics.swb_sizes[i]; j++)
> >> +                        swb_coeffs[j] *= CLIP_AVOIDANCE_FACTOR;
> >> +                }
> >> +            }
> >
> > wouldnt it be better to transition smoothly instead of a hard
> > *0.95  vs. *1 ?
> 
> If you mean adjusting CLIP_AVOIDANCE_FACTOR to be the minimal factor
> that prevents clipping, it probably would, but it would be rather
> hard.

no, thats not what i meant, though this could be done in theory too
we do similar for dealing with VBV bitrate violations in video
encoding, that is if all fails reencode video frame with lower quality
until it fits, but iam not suggesting this here ..


[...]

> If you mean a transition in time, I don't think it makes any
> difference. 0.95 is a ~0.5db change in intensity, which ought to be
> inaudible, and windowing will already take care to make the transition
> smooth. And the logic wouldn't be completely free either to ramp
> gradually, as it would have to ramp fully to 0.95 by the time it
> reaches the first window marked as clipping hazard, and it could very
> well be the frist window.

what i meant was that whatever condition is used to hard switch
between 1.0 and 0.95 could be rather a soft transition that is
for example
insteda of
if (x > 0.0) f = 0.95

something like
if (x > 0.0 && x<1.0) f = 1.0 + (0.95 - 1.0)*x

so theres no discontinuity in the transition



[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Many that live deserve death. And some that die deserve life. Can you give
it to them? Then do not be too eager to deal out death in judgement. For
even the very wise cannot see all ends. -- Gandalf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150718/b0ab257b/attachment.sig>


More information about the ffmpeg-devel mailing list