[FFmpeg-devel] [PATCH 6/7] lavc/flacenc: add sse4 version of the 32-bit lpc encoder

James Darnley james.darnley at gmail.com
Sat Feb 15 02:56:53 CET 2014


>From 1.3 to 2.5 times faster.  Runtime reduced by 4 to 58%.  As with the
16-bit version the speed-up generally increases with compression_level.

Also like the 16-bit version, it is not used with levels less than 3.
---
 libavcodec/x86/flac_dsp_gpl.asm |   93 +++++++++++++++++++++++++++++++++++++++
 libavcodec/x86/flacdsp_init.c   |    5 ++
 2 files changed, 98 insertions(+), 0 deletions(-)

diff --git a/libavcodec/x86/flac_dsp_gpl.asm b/libavcodec/x86/flac_dsp_gpl.asm
index ad847a1..9b2ffe3 100644
--- a/libavcodec/x86/flac_dsp_gpl.asm
+++ b/libavcodec/x86/flac_dsp_gpl.asm
@@ -22,6 +22,12 @@
 
 %include "libavutil/x86/x86util.asm"
 
+SECTION_RODATA
+
+pd_0_int_min: times  2 dd 0, -2147483648
+pq_int_min:   times  2 dq -2147483648
+pq_int_max:   times  2 dq  2147483647
+
 SECTION_TEXT
 
 INIT_XMM sse4
@@ -92,3 +98,90 @@ neg  orderq
     sub lenmp, (3*mmsize)/4
 jg .looplen
 RET
+
+%macro PMINSQ 3
+    pcmpgtq %3, %2, %1
+    pand    %1, %3
+    pandn   %3, %2
+    por     %1, %3
+%endmacro
+
+%macro PMAXSQ 3
+    pcmpgtq %3, %1, %2
+    pand    %1, %3
+    pandn   %3, %2
+    por     %1, %3
+%endmacro
+
+%macro CLIPQ 4 ; reg, min, max, tmp
+    PMAXSQ %1, %2, %4
+    PMINSQ %1, %3, %4
+%endmacro
+
+%macro HACK_PSRAQ 4 ; dst, src (shift), sign extend mask, tmp = 0
+    pcmpgtq %4, %1 ; mask where 0 > dst
+    pand    %4, %3 ; mask & sign extend mask
+    psrlq   %1, %2 ; dst >>= shift
+    por     %1, %4 ; dst | mask
+%endmacro
+
+INIT_XMM sse42
+%if ARCH_X86_64
+    cglobal flac_enc_lpc_32, 5, 7, 4, mmsize, res, smp, len, order, coefs, shift
+    %define posj r5
+    %define negj r6
+%else
+    cglobal flac_enc_lpc_32, 5, 6, 4, mmsize, res, smp, len, order, coefs, shift
+    %define posj r2
+    %define negj r5
+%endif
+
+; Is it worth looping correctly over the first samples?  The most that ever need
+; to be copied is 32 so we might as well just unroll the loop and do all 32.
+%assign iter 0
+%rep 32/(mmsize/4)
+    movu  m0,         [smpq+iter]
+    movu [resq+iter],  m0
+    %assign iter iter+mmsize
+%endrep
+
+lea    resq,   [resq+orderq*4]
+lea    smpq,   [smpq+orderq*4]
+lea    coefsq, [coefsq+orderq*4]
+sub    lenmp,   orderq
+movd   m3,      shiftmp
+neg    orderq
+movu   m4,     [pd_0_int_min]
+psrad  m4,      m3
+pslld  m4,      1
+mova  [rsp],    m4 ; sign extend mask
+
+.looplen:
+    pxor m0,   m0
+    mov  posj, orderq
+    xor  negj, negj
+    .looporder:
+        movd   m2,  [coefsq+posj*4] ; c = coefs[j]
+        SPLATD m2
+        movh   m1,  [smpq+negj*4-4] ; s = smp[i-j-1]
+        pshufd m1,   m1, q3130
+        pmuldq m1,   m2
+        paddq  m0,   m1             ; p += c * s
+
+        dec    negj
+        inc    posj
+    jnz .looporder
+
+    pxor    m2,    m2
+    HACK_PSRAQ m0, m3, [rsp], m2     ; p >>= shift
+    CLIPQ   m0,   [pq_int_min], [pq_int_max], m2 ; clip(p >> shift)
+    pshufd  m0,    m0, q0020 ; pack into first 2 dwords
+    movh    m1,   [smpq]
+    psubd   m1,    m0                ; smp[i] - p
+    movh   [resq], m1                ; res[i] = smp[i] - (p >> shift)
+
+    add    resq,  mmsize/2
+    add    smpq,  mmsize/2
+    sub    lenmp, mmsize/8
+jg .looplen
+RET
diff --git a/libavcodec/x86/flacdsp_init.c b/libavcodec/x86/flacdsp_init.c
index 61c61ab..8044438 100644
--- a/libavcodec/x86/flacdsp_init.c
+++ b/libavcodec/x86/flacdsp_init.c
@@ -28,6 +28,7 @@ void ff_flac_lpc_32_xop(int32_t *samples, const int coeffs[32], int order,
                         int qlevel, int len);
 
 void ff_flac_enc_lpc_16_sse4(int32_t *, const int32_t *, int, int, const int32_t *,int);
+void ff_flac_enc_lpc_32_sse42(int32_t *, const int32_t *, int, int, const int32_t *,int);
 
 av_cold void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt,
                                  int bps)
@@ -45,5 +46,9 @@ av_cold void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt,
         if (bps > 16)
             c->lpc = ff_flac_lpc_32_xop;
     }
+    if (EXTERNAL_SSE42(cpu_flags)) {
+        if (CONFIG_GPL && bps > 16)
+            c->lpc_encode = ff_flac_enc_lpc_32_sse42;
+    }
 #endif
 }
-- 
1.7.9



More information about the ffmpeg-devel mailing list