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

James Darnley james.darnley at gmail.com
Wed Feb 12 00:11:21 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 |   97 +++++++++++++++++++++++++++++++++++++++
 libavcodec/x86/flacdsp_init.c   |    5 ++
 2 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/libavcodec/x86/flac_dsp_gpl.asm b/libavcodec/x86/flac_dsp_gpl.asm
index 9e9249a..e36c76b 100644
--- a/libavcodec/x86/flac_dsp_gpl.asm
+++ b/libavcodec/x86/flac_dsp_gpl.asm
@@ -22,6 +22,14 @@
 
 %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
 %if ARCH_X86_64
     cglobal flac_enc_lpc_16, 6, 8, 8, 0, res, smp, len, order, coefs, shift
@@ -89,3 +97,92 @@ movd m3, shiftmp
     sub lenmp, (3*mmsize)/4
 jg .looplen
 RET
+
+%macro PMINSQ 3
+    mova    %3, %2
+    pcmpgtq %3, %1
+    pand    %1, %3
+    pandn   %3, %2
+    por     %1, %3
+%endmacro
+
+%macro PMAXSQ 3
+    mova    %3, %1
+    pcmpgtq %3, %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, 6, 8, 8, mmsize, res, smp, len, order, coefs, shift
+    %define posj r6
+    %define negj r7
+%else
+    cglobal flac_enc_lpc_32, 6, 6, 8, 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]
+sub lenmp, orderq
+movd m3, shiftmp
+movu m4, [pd_0_int_min]
+psrad m4, m3
+pslld m4, 1
+mova [rsp], m4 ; sign extend mask
+
+.looplen:
+    pxor m0,   m0
+    xor  posj,  posj
+    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
+
+        add    posj, 1
+        sub    negj, 1
+        cmp    posj, ordermp
+    jne .looporder
+
+
+    pxor m2, m2
+    HACK_PSRAQ m0, m3, [rsp], m2
+    CLIPQ   m0, [pq_int_min], [pq_int_max], m2
+    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 fdec2ca..37c56da 100644
--- a/libavcodec/x86/flacdsp_init.c
+++ b/libavcodec/x86/flacdsp_init.c
@@ -26,6 +26,7 @@ void ff_flac_lpc_32_sse4(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)
@@ -39,5 +40,9 @@ av_cold void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt,
         if (CONFIG_GPL && bps == 16)
             c->lpc_encode = ff_flac_enc_lpc_16_sse4;
     }
+    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