[FFmpeg-devel] [PATCH] x86/flacdsp: add SSE2 and AVX decorrelate functions
James Almer
jamrial at gmail.com
Tue Nov 4 08:22:51 CET 2014
Two to four times faster depending on instruction set, block size and channel count.
Signed-off-by: James Almer <jamrial at gmail.com>
---
Now also with 16 bits indep4 and indep6.
libavcodec/arm/flacdsp_init_arm.c | 2 +-
libavcodec/flacdec.c | 6 +-
libavcodec/flacdsp.c | 6 +-
libavcodec/flacdsp.h | 6 +-
libavcodec/flacenc.c | 2 +-
libavcodec/x86/flacdsp.asm | 261 ++++++++++++++++++++++++++++++++++++++
libavcodec/x86/flacdsp_init.c | 52 +++++++-
7 files changed, 323 insertions(+), 12 deletions(-)
diff --git a/libavcodec/arm/flacdsp_init_arm.c b/libavcodec/arm/flacdsp_init_arm.c
index 9ddb268..df1b19c 100644
--- a/libavcodec/arm/flacdsp_init_arm.c
+++ b/libavcodec/arm/flacdsp_init_arm.c
@@ -24,7 +24,7 @@
void ff_flac_lpc_16_arm(int32_t *samples, const int coeffs[32], int order,
int qlevel, int len);
-av_cold void ff_flacdsp_init_arm(FLACDSPContext *c, enum AVSampleFormat fmt,
+av_cold void ff_flacdsp_init_arm(FLACDSPContext *c, enum AVSampleFormat fmt, int channels,
int bps)
{
if (bps <= 16 && CONFIG_FLAC_DECODER)
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index 1333972..01ae717 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -111,7 +111,7 @@ static av_cold int flac_decode_init(AVCodecContext *avctx)
if (ret < 0)
return ret;
flac_set_bps(s);
- ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->bps);
+ ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->channels, s->bps);
s->got_streaminfo = 1;
return 0;
@@ -173,7 +173,7 @@ static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
if (ret < 0)
return ret;
flac_set_bps(s);
- ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
+ ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->channels, s->bps);
s->got_streaminfo = 1;
return 0;
@@ -472,7 +472,7 @@ static int decode_frame(FLACContext *s)
ret = allocate_buffers(s);
if (ret < 0)
return ret;
- ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
+ ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->channels, s->bps);
s->got_streaminfo = 1;
dump_headers(s->avctx, (FLACStreaminfo *)s);
}
diff --git a/libavcodec/flacdsp.c b/libavcodec/flacdsp.c
index b15bc74..a83eb83 100644
--- a/libavcodec/flacdsp.c
+++ b/libavcodec/flacdsp.c
@@ -85,7 +85,7 @@ static void flac_lpc_32_c(int32_t *decoded, const int coeffs[32],
}
-av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt,
+av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels,
int bps)
{
if (bps > 16) {
@@ -127,7 +127,7 @@ av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt,
}
if (ARCH_ARM)
- ff_flacdsp_init_arm(c, fmt, bps);
+ ff_flacdsp_init_arm(c, fmt, channels, bps);
if (ARCH_X86)
- ff_flacdsp_init_x86(c, fmt, bps);
+ ff_flacdsp_init_x86(c, fmt, channels, bps);
}
diff --git a/libavcodec/flacdsp.h b/libavcodec/flacdsp.h
index 14f3466..417381c 100644
--- a/libavcodec/flacdsp.h
+++ b/libavcodec/flacdsp.h
@@ -31,8 +31,8 @@ typedef struct FLACDSPContext {
const int32_t coefs[32], int shift);
} FLACDSPContext;
-void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int bps);
-void ff_flacdsp_init_arm(FLACDSPContext *c, enum AVSampleFormat fmt, int bps);
-void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt, int bps);
+void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels, int bps);
+void ff_flacdsp_init_arm(FLACDSPContext *c, enum AVSampleFormat fmt, int channels, int bps);
+void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt, int channels, int bps);
#endif /* AVCODEC_FLACDSP_H */
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 3b72888..e66ef3d 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -428,7 +428,7 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
ff_bswapdsp_init(&s->bdsp);
- ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
+ ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt, channels,
avctx->bits_per_raw_sample);
dprint_compression_options(s);
diff --git a/libavcodec/x86/flacdsp.asm b/libavcodec/x86/flacdsp.asm
index 37ee87b..afbe199 100644
--- a/libavcodec/x86/flacdsp.asm
+++ b/libavcodec/x86/flacdsp.asm
@@ -72,3 +72,264 @@ ALIGN 16
LPC_32 xop
%endif
LPC_32 sse4
+
+;---------------------------------------------------------------------------------------------
+;void ff_flac_decorrelate_{ls,rs,ms,indep2}_16_sse2(uint8_t **out, int32_t **in, int channels,
+; int len, int shift);
+;---------------------------------------------------------------------------------------------
+%macro FLAC_DECORRELATE_16 3-4
+cglobal flac_decorrelate_%1_16, 2, 4, 3, out, in0, in1, len
+%if ARCH_X86_32
+ mov lend, lenm
+%endif
+ shl lend, 2
+ mov in1q, [in0q+gprsize]
+ mov in0q, [in0q]
+ mov outq, [outq]
+ add in1q, lenq
+ add in0q, lenq
+ add outq, lenq
+ neg lenq
+
+align 16
+.loop:
+ mova m0, [in0q+lenq]
+ mova m1, [in1q+lenq]
+%ifidn %1, ms
+ psrad m2, m1, 1
+ psubd m0, m2
+%endif
+%ifnidn %1, indep2
+ p%4d m2, m0, m1
+%endif
+ packssdw m%2, m%2
+ packssdw m%3, m%3
+ punpcklwd m%2, m%3
+ mova [outq+lenq], m%2
+ add lenq, 16
+ jl .loop
+ REP_RET
+%endmacro
+
+INIT_XMM sse2
+FLAC_DECORRELATE_16 ls, 0, 2, sub
+FLAC_DECORRELATE_16 rs, 2, 1, add
+FLAC_DECORRELATE_16 ms, 2, 0, add
+FLAC_DECORRELATE_16 indep2, 0, 1
+
+;-------------------------------------------------------------------------------------
+;void ff_flac_decorrelate_indep[46]_16_sse2(uint8_t **out, int32_t **in, int channels,
+; int len, int shift);
+;-------------------------------------------------------------------------------------
+%macro FLAC_DECORRELATE_INDEP_16 1
+cglobal flac_decorrelate_indep%1_16, 2, %1+2, %1+1, out, in0, in1, len, in2, in3, in4, in5
+%if ARCH_X86_32
+%if %1 == 6
+ DEFINE_ARGS out, in0, in1, in2, in3, in4, in5
+ %define lend dword r3m
+%else
+ mov lend, lenm
+%endif
+%endif
+
+%assign %%i 1
+%rep %1-1
+ mov in %+ %%i %+ q, [in0q+%%i*gprsize]
+%assign %%i %%i+1
+%endrep
+
+ mov in0q, [in0q]
+ mov outq, [outq]
+
+%assign %%i 1
+%rep %1-1
+ sub in %+ %%i %+ q, in0q
+%assign %%i %%i+1
+%endrep
+
+align 16
+.loop:
+ mova m0, [in0q]
+
+%assign %%i 1
+%rep %1/2-1
+ mova m %+ %%i, [in0q+in %+ %%i %+ q]
+%assign %%i %%i+1
+%endrep
+
+%if %1 == 6
+ packssdw m0, [in0q+in3q]
+ packssdw m1, [in0q+in4q]
+ packssdw m2, [in0q+in5q]
+ pshufd m3, m0, q1032
+ punpcklwd m0, m1
+ punpckhwd m1, m2
+ punpcklwd m2, m3
+
+ shufps m3, m0, m2, q2020
+ shufps m0, m1, q2031
+ shufps m2, m1, q3131
+ shufps m1, m2, m3, q3120
+ shufps m3, m0, q0220
+ shufps m0, m2, q3113
+ SWAP 2, 0, 3
+%else ; %1 == 4
+ packssdw m0, [in0q+in2q]
+ packssdw m1, [in0q+in3q]
+ SBUTTERFLY wd, 0, 1, 3
+ SBUTTERFLY dq, 0, 1, 3
+%endif
+
+%assign %%i 0
+%rep %1/2
+ mova [outq+%%i*mmsize], m %+ %%i
+%assign %%i %%i+1
+%endrep
+
+ add in0q, mmsize
+ add outq, mmsize*(%1/2)
+ sub lend, mmsize/4
+ jg .loop
+ REP_RET
+%endmacro
+
+INIT_XMM sse2
+FLAC_DECORRELATE_INDEP_16 4
+FLAC_DECORRELATE_INDEP_16 6
+
+;----------------------------------------------------------------------------------
+;void ff_flac_decorrelate_[lrm]s_32_sse2(uint8_t **out, int32_t **in, int channels,
+; int len, int shift);
+;----------------------------------------------------------------------------------
+%macro FLAC_DECORRELATE_32 5
+cglobal flac_decorrelate_%1_32, 2, 4, 4, out, in0, in1, len
+%if ARCH_X86_32 || WIN64
+ movd m3, r4m
+%if ARCH_X86_32
+ mov lend, lenm
+%endif
+%else ; UNIX64
+ movd m3, r4d
+%endif
+ mov in1q, [in0q+gprsize]
+ mov in0q, [in0q]
+ mov outq, [outq]
+ sub in1q, in0q
+
+align 16
+.loop:
+ mova m0, [in0q ]
+ mova m1, [in0q+in1q]
+%ifidn %1, ms
+ psrad m2, m1, 1
+ psubd m0, m2
+%endif
+ p%5d m2, m0, m1
+ pslld m%2, m3
+ pslld m%3, m3
+
+ SBUTTERFLY dq, %2, %3, %4
+
+ mova [outq ], m%2
+ mova [outq+mmsize], m%3
+
+ add in0q, mmsize
+ add outq, mmsize*2
+ sub lend, mmsize/4
+ jg .loop
+ REP_RET
+%endmacro
+
+INIT_XMM sse2
+FLAC_DECORRELATE_32 ls, 0, 2, 1, sub
+FLAC_DECORRELATE_32 rs, 2, 1, 0, add
+FLAC_DECORRELATE_32 ms, 2, 0, 1, add
+
+;--------------------------------------------------------------------------------------
+;void ff_flac_decorrelate_indep[246]_32_sse2(uint8_t **out, int32_t **in, int channels,
+; int len, int shift);
+;--------------------------------------------------------------------------------------
+%macro FLAC_DECORRELATE_INDEP_32 2
+cglobal flac_decorrelate_indep%1_32, 2, %1+2, %1+2, out, in0, in1, len, in2, in3, in4, in5
+%if ARCH_X86_32
+ movd m%2, r4m
+%if %1 == 6
+ DEFINE_ARGS out, in0, in1, in2, in3, in4, in5
+ %define lend dword r3m
+%else
+ mov lend, lenm
+%endif
+%elif WIN64
+ movd m%2, r4m
+%else ; UNIX64
+ movd m%2, r4d
+%endif
+
+%assign %%i 1
+%rep %1-1
+ mov in %+ %%i %+ q, [in0q+%%i*gprsize]
+%assign %%i %%i+1
+%endrep
+
+ mov in0q, [in0q]
+ mov outq, [outq]
+
+%assign %%i 1
+%rep %1-1
+ sub in %+ %%i %+ q, in0q
+%assign %%i %%i+1
+%endrep
+
+align 16
+.loop:
+ mova m0, [in0q]
+%assign %%i 1
+%rep %1-1
+ mova m %+ %%i, [in0q+in %+ %%i %+ q]
+%assign %%i %%i+1
+%endrep
+
+%assign %%i 0
+%rep %1
+ pslld m %+ %%i, m%2
+%assign %%i %%i+1
+%endrep
+
+%if %1 == 6
+ SBUTTERFLY dq, 0, 1, 6
+ SBUTTERFLY dq, 2, 3, 6
+ SBUTTERFLY dq, 4, 5, 6
+
+ punpcklqdq m6, m0, m2
+ punpckhqdq m2, m4
+ shufps m4, m0, 0xe4
+ punpcklqdq m0, m1, m3
+ punpckhqdq m3, m5
+ shufps m5, m1, 0xe4
+ SWAP 0,6,1,4,5,3
+%elif %1 == 4
+ TRANSPOSE4x4D 0, 1, 2, 3, 4
+%else ; %1 == 2
+ SBUTTERFLY dq, 0, 1, 2
+%endif
+
+%assign %%i 0
+%rep %1
+ mova [outq+%%i*mmsize], m %+ %%i
+%assign %%i %%i+1
+%endrep
+
+ add in0q, mmsize
+ add outq, mmsize*%1
+ sub lend, mmsize/4
+ jg .loop
+ REP_RET
+%endmacro
+
+INIT_XMM sse2
+FLAC_DECORRELATE_INDEP_32 2, 3
+FLAC_DECORRELATE_INDEP_32 4, 5
+FLAC_DECORRELATE_INDEP_32 6, 7
+INIT_XMM avx
+FLAC_DECORRELATE_INDEP_32 4, 5
+FLAC_DECORRELATE_INDEP_32 6, 7
diff --git a/libavcodec/x86/flacdsp_init.c b/libavcodec/x86/flacdsp_init.c
index ad88e5b..9c980f0 100644
--- a/libavcodec/x86/flacdsp_init.c
+++ b/libavcodec/x86/flacdsp_init.c
@@ -29,18 +29,68 @@ void ff_flac_lpc_32_xop(int32_t *samples, const int coeffs[32], int order,
void ff_flac_enc_lpc_16_sse4(int32_t *, const int32_t *, int, int, const int32_t *,int);
-av_cold void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt,
+#define DECORRELATE_FUNCS(fmt, opt) \
+void ff_flac_decorrelate_ls_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift); \
+void ff_flac_decorrelate_rs_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift); \
+void ff_flac_decorrelate_ms_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift); \
+void ff_flac_decorrelate_indep2_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift); \
+void ff_flac_decorrelate_indep4_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift); \
+void ff_flac_decorrelate_indep6_##fmt##_##opt(uint8_t **out, int32_t **in, int channels, \
+ int len, int shift)
+
+DECORRELATE_FUNCS(16, sse2);
+DECORRELATE_FUNCS(32, sse2);
+DECORRELATE_FUNCS(32, avx);
+
+av_cold void ff_flacdsp_init_x86(FLACDSPContext *c, enum AVSampleFormat fmt, int channels,
int bps)
{
#if HAVE_YASM
int cpu_flags = av_get_cpu_flags();
+ if (EXTERNAL_SSE2(cpu_flags)) {
+ if (fmt == AV_SAMPLE_FMT_S16 && CONFIG_FLAC_DECODER) {
+ if (channels == 2)
+ c->decorrelate[0] = ff_flac_decorrelate_indep2_16_sse2;
+ else if (channels == 4)
+ c->decorrelate[0] = ff_flac_decorrelate_indep4_16_sse2;
+ else if (channels == 6)
+ c->decorrelate[0] = ff_flac_decorrelate_indep6_16_sse2;
+ c->decorrelate[1] = ff_flac_decorrelate_ls_16_sse2;
+ c->decorrelate[2] = ff_flac_decorrelate_rs_16_sse2;
+ c->decorrelate[3] = ff_flac_decorrelate_ms_16_sse2;
+ }
+ if (fmt == AV_SAMPLE_FMT_S32 && CONFIG_FLAC_DECODER) {
+ if (channels == 2)
+ c->decorrelate[0] = ff_flac_decorrelate_indep2_32_sse2;
+ else if (channels == 4)
+ c->decorrelate[0] = ff_flac_decorrelate_indep4_32_sse2;
+ else if (channels == 6)
+ c->decorrelate[0] = ff_flac_decorrelate_indep6_32_sse2;
+ c->decorrelate[1] = ff_flac_decorrelate_ls_32_sse2;
+ c->decorrelate[2] = ff_flac_decorrelate_rs_32_sse2;
+ c->decorrelate[3] = ff_flac_decorrelate_ms_32_sse2;
+ }
+ }
if (EXTERNAL_SSE4(cpu_flags)) {
if (bps > 16 && CONFIG_FLAC_DECODER)
c->lpc = ff_flac_lpc_32_sse4;
if (bps == 16 && CONFIG_FLAC_ENCODER && CONFIG_GPL)
c->lpc_encode = ff_flac_enc_lpc_16_sse4;
}
+ if (EXTERNAL_AVX(cpu_flags)) {
+ if (fmt == AV_SAMPLE_FMT_S32 && CONFIG_FLAC_DECODER) {
+ if (channels == 4)
+ c->decorrelate[0] = ff_flac_decorrelate_indep4_32_avx;
+ else if (channels == 6)
+ c->decorrelate[0] = ff_flac_decorrelate_indep6_32_avx;
+ }
+ }
if (EXTERNAL_XOP(cpu_flags)) {
if (bps > 16 && CONFIG_FLAC_DECODER)
c->lpc = ff_flac_lpc_32_xop;
--
2.0.4
More information about the ffmpeg-devel
mailing list