[FFmpeg-devel] [PATCH v3] aacenc: add SIMD optimizations for abs_pow34 and quantization

Rostislav Pehlivanov atomnuker at gmail.com
Tue Oct 18 23:42:54 EEST 2016


On 18 October 2016 at 21:04, Michael Niedermayer <michael at niedermayer.cc>
wrote:

> On Tue, Oct 18, 2016 at 05:33:13PM +0100, Rostislav Pehlivanov wrote:
> > On 18 October 2016 at 16:32, James Almer <jamrial at gmail.com> wrote:
> >
> > > On 10/18/2016 12:07 PM, Rostislav Pehlivanov wrote:
> > > > diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
> > > > index ee3cbf8..622f0ba 100644
> > > > --- a/libavcodec/aacenc.c
> > > > +++ b/libavcodec/aacenc.c
> > > > @@ -1033,6 +1033,12 @@ static av_cold int
> aac_encode_init(AVCodecContext
> > > *avctx)
> > > >      ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER,
> > > FF_LPC_TYPE_LEVINSON);
> > > >      s->random_state = 0x1f2e3d4c;
> > > >
> > > > +    s->abs_pow34   = &abs_pow34_v;
> > > > +    s->quant_bands = &quantize_bands;
> > >
> > > No need for & in these.
> > >
> > > > +
> > > > +    if (ARCH_X86)
> > > > +        ff_aac_dsp_init_x86(s);
> > > > +
> > > >      if (HAVE_MIPSDSP)
> > > >          ff_aac_coder_init_mips(s);
> > >
> > > [...]
> > >
> > > > diff --git a/libavcodec/x86/aacencdsp.asm
> b/libavcodec/x86/aacencdsp.asm
> > > > new file mode 100644
> > > > index 0000000..dd7b022
> > > > --- /dev/null
> > > > +++ b/libavcodec/x86/aacencdsp.asm
> > > > @@ -0,0 +1,88 @@
> > > > +;**********************************************************
> > > ********************
> > > > +;* SIMD optimized AAC encoder DSP functions
> > > > +;*
> > > > +;* Copyright (C) 2016 Rostislav Pehlivanov <atomnuker at gmail.com>
> > > > +;*
> > > > +;* 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
> > > > +;**********************************************************
> > > ********************
> > > > +
> > > > +%include "libavutil/x86/x86util.asm"
> > > > +
> > > > +SECTION_RODATA
> > > > +
> > > > +float_abs_mask: times 4 dd 0x7fffffff
> > > > +
> > > > +SECTION .text
> > > > +
> > > > +;**********************************************************
> *********
> > > > +;void ff_abs_pow34(float *out, const float *in, const int size);
> > > > +;**********************************************************
> *********
> > > > +INIT_XMM sse
> > > > +cglobal abs_pow34, 3, 3, 3, out, in, size
> > > > +    mova   m2, [float_abs_mask]
> > > > +    shl    sizeq, 2
> > > > +    add    inq, sizeq
> > > > +    add    outq, sizeq
> > > > +    neg    sizeq
> > > > +.loop:
> > > > +    movaps m0, [inq+sizeq]
> > > > +    andps  m0, m2
> > >
> > > Remove the movaps and do
> > >
> > > andps  m0, m2, [inq+sizeq]
> > >
> > > Instead. Sorry i didn't notice this last time.
> > >
> > > > +    sqrtps m1, m0
> > > > +    mulps  m0, m1
> > > > +    sqrtps m0, m0
> > > > +    mova   [outq+sizeq], m0
> > > > +    add    sizeq, mmsize
> > > > +    jl    .loop
> > > > +    RET
> > > > +
> > > > +;**********************************************************
> *********
> > > > +;void ff_aac_quantize_bands(int *out, const float *in, const float
> > > *scaled,
> > > > +;                           int size, int is_signed, int maxval,
> const
> > > float Q34,
> > > > +;                           const float rounding)
> > > > +;**********************************************************
> *********
> > > > +INIT_XMM sse2
> > > > +cglobal aac_quantize_bands, 5, 5, 6, out, in, scaled, size,
> is_signed,
> > > maxval, Q34, rounding
> > > > +%if UNIX64 == 0
> > > > +    movss     m0, Q34m
> > > > +    movss     m1, roundingm
> > > > +    cvtsi2ss  m3, maxvald
> > > > +%else
> > > > +    cvtsi2ss  m3, dword maxvalm
> > > > +%endif
> > >
> > > The other way around. Unix64 is the one that has maxval on a reg
> regardless
> > > of how you init the function, whereas win64 and any x86_32 target have
> it
> > > on stack.
> > >
> > > > +    shufps    m0, m0, 0
> > > > +    shufps    m1, m1, 0
> > > > +    shufps    m3, m3, 0
> > > > +    shl       is_signedd, 31
> > > > +    movd      m4, is_signedd
> > > > +    shufps    m4, m4, 0
> > > > +    shl       sized,   2
> > > > +    add       inq, sizeq
> > > > +    add       outq, sizeq
> > > > +    add       scaledq, sizeq
> > > > +    neg       sizeq
> > > > +.loop:
> > > > +    mulps     m2, m0, [scaledq+sizeq]
> > > > +    addps     m2, m1
> > > > +    minps     m2, m3
> > >
> > > > +    movaps    m5, [inq+sizeq]
> > > > +    andps     m5, m4
> > >
> > > Same as in abs_pow34, remove movaps and do
> > >
> > > andps     m5, m4, [inq+sizeq]
> > >
> > > > +    orps      m2, m5
> > > > +    cvttps2dq m2, m2
> > > > +    mova      [outq+sizeq], m2
> > > > +    add       sizeq, mmsize
> > > > +    jl       .loop
> > > > +    RET
> > > > diff --git a/libavcodec/x86/aacencdsp_init.c
> b/libavcodec/x86/aacencdsp_
> > > init.c
> > > > new file mode 100644
> > > > index 0000000..aefaa15
> > > > --- /dev/null
> > > > +++ b/libavcodec/x86/aacencdsp_init.c
> > > > @@ -0,0 +1,43 @@
> > > > +/*
> > > > + * AAC encoder assembly optimizations
> > > > + * Copyright (C) 2016 Rostislav Pehlivanov <atomnuker at gmail.com>
> > > > + *
> > > > + * 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
> > > > + */
> > > > +
> > > > +#include "config.h"
> > > > +
> > > > +#include "libavutil/float_dsp.h"
> > > > +#include "libavutil/x86/cpu.h"
> > > > +#include "libavcodec/aacenc.h"
> > > > +
> > > > +void ff_abs_pow34_sse(float *out, const float *in, const int size);
> > > > +
> > > > +void ff_aac_quantize_bands_sse2(int *out, const float *in, const
> float
> > > *scaled,
> > > > +                                int size, int is_signed, int maxval,
> > > const float Q34,
> > > > +                                const float rounding);
> > > > +
> > > > +av_cold void ff_aac_dsp_init_x86(AACEncContext *s)
> > > > +{
> > > > +    int cpu_flags = av_get_cpu_flags();
> > > > +
> > > > +    if (EXTERNAL_SSE(cpu_flags))
> > > > +        s->abs_pow34   = &ff_abs_pow34_sse;
> > > > +
> > > > +    if (EXTERNAL_SSE2(cpu_flags))
> > > > +        s->quant_bands = &ff_aac_quantize_bands_sse2;
> > >
> > > Same with these.
> > >
> > > Passes fate on mingw-w64 (x86_64) after fixing the maxval mixup above.
> > > Crashes otherwise.
> > >
> > > _______________________________________________
> > > ffmpeg-devel mailing list
> > > ffmpeg-devel at ffmpeg.org
> > > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > >
> >
> > Hopefully I didn't mess anything up and the attached patch is the last
> > revision of it.
>
> >  aaccoder.c            |   27 ++++++++-------
> >  aaccoder_trellis.h    |    2 -
> >  aaccoder_twoloop.h    |    2 -
> >  aacenc.c              |    6 +++
> >  aacenc.h              |    6 +++
> >  aacenc_is.c           |    6 +--
> >  aacenc_ltp.c          |    4 +-
> >  aacenc_pred.c         |    6 +--
> >  aacenc_quantization.h |    4 +-
> >  aacenc_utils.h        |    2 -
> >  x86/Makefile          |    2 +
> >  x86/aacencdsp.asm     |   86 ++++++++++++++++++++++++++++++
> ++++++++++++++++++++
> >  x86/aacencdsp_init.c  |   43 +++++++++++++++++++++++++
> >  13 files changed, 170 insertions(+), 26 deletions(-)
> > e9878e2480cd612e6852783003f47768f5ced052  0001-aacenc-add-SIMD-
> optimizations-for-abs_pow34-and-quan.patch
> > From eb95cf5aeacdc7d191c5b7cf8df40a1e5306b733 Mon Sep 17 00:00:00 2001
> > From: Rostislav Pehlivanov <atomnuker at gmail.com>
> > Date: Sat, 8 Oct 2016 15:59:14 +0100
> > Subject: [PATCH] aacenc: add SIMD optimizations for abs_pow34 and
> quantization
>
> tested on x86 linux32/64 mingw32/64, arm qemu and mips qemu
> all work now
>
> [...]
>
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> It is dangerous to be right in matters on which the established authorities
> are wrong. -- Voltaire
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
Pushed
Thanks to both of you for the review and testing


More information about the ffmpeg-devel mailing list