[FFmpeg-devel] [PATCH 1/3] lavd/xv: speed up yuv420p write packet

Lukasz Marek lukasz.m.luki at gmail.com
Wed Nov 13 23:40:45 CET 2013


xv_write_packet do operations like multiplication that are not required.
Small optimizations allows to make function up to 10% faster.

Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
---
 libavdevice/xv.c |   31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/libavdevice/xv.c b/libavdevice/xv.c
index 50d72a5..fdc96c9 100644
--- a/libavdevice/xv.c
+++ b/libavdevice/xv.c
@@ -141,6 +141,17 @@ static int xv_write_header(AVFormatContext *s)
     return 0;
 }
 
+static av_always_inline void xv_copy_line(int h, const char *src, char *dst,
+                                          int src_linesize, int dst_linesize)
+{
+    int y, len = FFMIN(src_linesize, dst_linesize);
+    for (y = 0; y < h; ++y) {
+        memcpy(dst, src, len);
+        src += src_linesize;
+        dst += dst_linesize;
+    }
+}
+
 static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     XVContext *xv = s->priv_data;
@@ -148,22 +159,14 @@ static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
     XWindowAttributes window_attrs;
     AVPicture pict;
     AVCodecContext *ctx = s->streams[0]->codec;
-    int y, h;
-
-    h = img->height / 2;
 
     avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
-    for (y = 0; y < img->height; y++) {
-        memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])],
-               &pict.data[0][y * pict.linesize[0]], img->pitches[0]);
-    }
-
-    for (y = 0; y < h; ++y) {
-        memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])],
-               &pict.data[1][y * pict.linesize[1]], img->pitches[1]);
-        memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],
-               &pict.data[2][y * pict.linesize[2]], img->pitches[2]);
-    }
+    xv_copy_line(img->height, pict.data[0], &img->data[img->offsets[0]],
+                 pict.linesize[0], img->pitches[0]);
+    xv_copy_line(img->height / 2, pict.data[1], &img->data[img->offsets[1]],
+                 pict.linesize[1], img->pitches[1]);
+    xv_copy_line(img->height / 2, pict.data[2], &img->data[img->offsets[2]],
+                 pict.linesize[2], img->pitches[2]);
 
     XGetWindowAttributes(xv->display, xv->window, &window_attrs);
     if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list