[FFmpeg-devel] [PATCH] v210enc: Add x86 SIMD

Michael Niedermayer michaelni at gmx.at
Sun Nov 23 02:01:15 CET 2014


From: Kieran Kunhya <kierank at obe.tv>

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavcodec/v210enc.c          |   78 +++++++++++++++++++++++++++++------------
 libavcodec/v210enc.h          |   31 ++++++++++++++++
 libavcodec/x86/Makefile       |    2 ++
 libavcodec/x86/v210enc.asm    |   75 +++++++++++++++++++++++++++++++++++++++
 libavcodec/x86/v210enc_init.c |   31 ++++++++++++++++
 5 files changed, 194 insertions(+), 23 deletions(-)
 create mode 100644 libavcodec/v210enc.h
 create mode 100644 libavcodec/x86/v210enc.asm
 create mode 100644 libavcodec/x86/v210enc_init.c

diff --git a/libavcodec/v210enc.c b/libavcodec/v210enc.c
index 1e53bdb..f4fc1fe 100644
--- a/libavcodec/v210enc.c
+++ b/libavcodec/v210enc.c
@@ -24,9 +24,37 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "v210enc.h"
+
+#define CLIP(v) av_clip(v, 4, 1019)
+
+#define WRITE_PIXELS(a, b, c)           \
+    do {                                \
+        val =   CLIP(*a++);             \
+        val |= (CLIP(*b++) << 10) |     \
+               (CLIP(*c++) << 20);      \
+        AV_WL32(dst, val);              \
+        dst += 4;                       \
+    } while (0)
+
+static void v210_planar_pack_c(const uint16_t *y, const uint16_t *u,
+                               const uint16_t *v, uint8_t *dst, ptrdiff_t width)
+{
+    uint32_t val;
+    int i;
+
+    for( i = 0; i < width-5; i += 6 ){
+        WRITE_PIXELS(u, y, v);
+        WRITE_PIXELS(y, u, y);
+        WRITE_PIXELS(v, y, u);
+        WRITE_PIXELS(y, v, y);
+    }
+}
 
 static av_cold int encode_init(AVCodecContext *avctx)
 {
+    V210EncContext *s = avctx->priv_data;
+
     if (avctx->width & 1) {
         av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
         return AVERROR(EINVAL);
@@ -42,12 +70,19 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
 
+    s->pack_line            = v210_planar_pack_c;
+
+    if (HAVE_MMX)
+        v210enc_x86_init(s);
+
     return 0;
 }
 
 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                         const AVFrame *pic, int *got_packet)
 {
+    V210EncContext *s = avctx->priv_data;
+
     int aligned_width = ((avctx->width + 47) / 48) * 48;
     int stride = aligned_width * 8 / 3;
     int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
@@ -55,47 +90,43 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     const uint16_t *y = (const uint16_t*)pic->data[0];
     const uint16_t *u = (const uint16_t*)pic->data[1];
     const uint16_t *v = (const uint16_t*)pic->data[2];
-    PutByteContext p;
+    uint8_t *dst;
 
     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->height * stride)) < 0)
         return ret;
 
-    bytestream2_init_writer(&p, pkt->data, pkt->size);
-
-#define CLIP(v) av_clip(v, 4, 1019)
-
-#define WRITE_PIXELS(a, b, c)           \
-    do {                                \
-        val =   CLIP(*a++);             \
-        val |= (CLIP(*b++) << 10) |     \
-               (CLIP(*c++) << 20);      \
-        bytestream2_put_le32u(&p, val); \
-    } while (0)
+    dst = pkt->data;
 
     for (h = 0; h < avctx->height; h++) {
         uint32_t val;
-        for (w = 0; w < avctx->width - 5; w += 6) {
-            WRITE_PIXELS(u, y, v);
-            WRITE_PIXELS(y, u, y);
-            WRITE_PIXELS(v, y, u);
-            WRITE_PIXELS(y, v, y);
-        }
+        w = (avctx->width / 6) * 6;
+        s->pack_line(y, u, v, dst, w);
+
+        y += w;
+        u += w >> 1;
+        v += w >> 1;
+        dst += (w / 6) * 16;
         if (w < avctx->width - 1) {
             WRITE_PIXELS(u, y, v);
 
             val = CLIP(*y++);
-            if (w == avctx->width - 2)
-                bytestream2_put_le32u(&p, val);
+            if (w == avctx->width - 2) {
+                AV_WL32(dst, val);
+                dst += 4;
+            }
             if (w < avctx->width - 3) {
                 val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
-                bytestream2_put_le32u(&p, val);
+                AV_WL32(dst, val);
+                dst += 4;
 
                 val = CLIP(*v++) | (CLIP(*y++) << 10);
-                bytestream2_put_le32u(&p, val);
+                AV_WL32(dst, val);
+                dst += 4;
             }
         }
 
-        bytestream2_set_buffer(&p, 0, line_padding);
+        memset(dst, 0, line_padding);
+        dst += line_padding;
 
         y += pic->linesize[0] / 2 - avctx->width;
         u += pic->linesize[1] / 2 - avctx->width / 2;
@@ -119,6 +150,7 @@ AVCodec ff_v210_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_V210,
+    .priv_data_size = sizeof(V210EncContext),
     .init           = encode_init,
     .encode2        = encode_frame,
     .close          = encode_close,
diff --git a/libavcodec/v210enc.h b/libavcodec/v210enc.h
new file mode 100644
index 0000000..c119caf
--- /dev/null
+++ b/libavcodec/v210enc.h
@@ -0,0 +1,31 @@
+/*
+ * 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
+ */
+
+#ifndef AVCOENC_V210ENC_H
+#define AVCOENC_V210ENC_H
+
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+
+typedef struct {
+    void (*pack_line)(const uint16_t *y, const uint16_t *u, const uint16_t *v, uint8_t *dst, ptrdiff_t width);
+} V210EncContext;
+
+void v210enc_x86_init(V210EncContext *s);
+
+#endif /* AVCOENC_V210ENC_H */
diff --git a/libavcodec/x86/Makefile b/libavcodec/x86/Makefile
index 8912df5..1ca0df0 100644
--- a/libavcodec/x86/Makefile
+++ b/libavcodec/x86/Makefile
@@ -53,6 +53,7 @@ OBJS-$(CONFIG_SVQ1_ENCODER)            += x86/svq1enc_init.o
 OBJS-$(CONFIG_TRUEHD_DECODER)          += x86/mlpdsp_init.o
 OBJS-$(CONFIG_TTA_DECODER)             += x86/ttadsp_init.o
 OBJS-$(CONFIG_V210_DECODER)            += x86/v210-init.o
+OBJS-$(CONFIG_V210_ENCODER)            += x86/v210enc_init.o
 OBJS-$(CONFIG_VC1_DECODER)             += x86/vc1dsp_init.o
 OBJS-$(CONFIG_VORBIS_DECODER)          += x86/vorbisdsp_init.o
 OBJS-$(CONFIG_VP6_DECODER)             += x86/vp6dsp_init.o
@@ -143,6 +144,7 @@ YASM-OBJS-$(CONFIG_SVQ1_ENCODER)       += x86/svq1enc.o
 YASM-OBJS-$(CONFIG_TRUEHD_DECODER)     += x86/mlpdsp.o
 YASM-OBJS-$(CONFIG_TTA_DECODER)        += x86/ttadsp.o
 YASM-OBJS-$(CONFIG_V210_DECODER)       += x86/v210.o
+YASM-OBJS-$(CONFIG_V210_ENCODER)       += x86/v210enc.o
 YASM-OBJS-$(CONFIG_VC1_DECODER)        += x86/vc1dsp.o
 YASM-OBJS-$(CONFIG_VORBIS_DECODER)     += x86/vorbisdsp.o
 YASM-OBJS-$(CONFIG_VP6_DECODER)        += x86/vp6dsp.o
diff --git a/libavcodec/x86/v210enc.asm b/libavcodec/x86/v210enc.asm
new file mode 100644
index 0000000..df72d71
--- /dev/null
+++ b/libavcodec/x86/v210enc.asm
@@ -0,0 +1,75 @@
+;******************************************************************************
+;* V210 SIMD pack
+;* Copyright (c) 2014 Kieran Kunhya <kierank at obe.tv>
+;*
+;* 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
+
+v210_enc_min: times 8 dw 0x4
+v210_enc_max: times 8 dw 0x3fb
+
+v210_enc_luma_mult: dw 4,1,16,4,1,16,0,0
+v210_enc_luma_shuf: db -1,0,1,-1,2,3,4,5,-1,6,7,-1,8,9,10,11
+
+v210_enc_chroma_mult: dw 1,4,16,0,16,1,4,0
+v210_enc_chroma_shuf: db 0,1,8,9,-1,2,3,-1,10,11,4,5,-1,12,13,-1
+
+SECTION .text
+
+%macro v210_planar_pack 0
+
+; v210_planar_pack(const uint16_t *y, const uint16_t *u, const uint16_t *v, uint8_t *dst, ptrdiff_t width)
+cglobal v210_planar_pack, 5, 5, 4, y, u, v, dst, width
+    lea     r0, [r0+2*widthq]
+    add     uq, widthq
+    add     vq, widthq
+    neg     widthq
+
+    movu    m2, [v210_enc_min]
+    movu    m3, [v210_enc_max]
+
+.loop
+    movu    m0, [yq+2*widthq]
+    CLIPW   m0, m2, m3
+
+    movq    m1, [uq+widthq]
+    movhps  m1, [vq+widthq]
+    CLIPW   m1, m2, m3
+
+    pmullw  m0, [v210_enc_luma_mult]
+    pshufb  m0, [v210_enc_luma_shuf]
+
+    pmullw  m1, [v210_enc_chroma_mult]
+    pshufb  m1, [v210_enc_chroma_shuf]
+
+    por     m0, m1
+
+    movu    [dstq], m0
+
+    add     dstq, mmsize
+    add     widthq, 6
+    jl .loop
+
+    REP_RET
+%endmacro
+
+INIT_XMM ssse3
+v210_planar_pack
diff --git a/libavcodec/x86/v210enc_init.c b/libavcodec/x86/v210enc_init.c
new file mode 100644
index 0000000..1afe399
--- /dev/null
+++ b/libavcodec/x86/v210enc_init.c
@@ -0,0 +1,31 @@
+/*
+ * 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/asm.h"
+#include "libavutil/x86/cpu.h"
+#include "libavcodec/v210enc.h"
+
+extern void ff_v210_planar_pack_ssse3(const uint16_t *y, const uint16_t *u, const uint16_t *v, uint8_t *dst, ptrdiff_t width);
+
+av_cold void v210enc_x86_init(V210EncContext *s)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if( EXTERNAL_SSSE3(cpu_flags) )
+        s->pack_line = ff_v210_planar_pack_ssse3;
+}
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list