[FFmpeg-devel] [PATCH] avutil/imgutils: optimize image copying

xufuji456 839789740 at qq.com
Fri May 5 11:31:41 EEST 2023


It makes sense when copying 4K/8K video frame, got 2.7% speed up.
When the condition is no padding and src_linesize equals to dst,
we could copy plane instead of line by line.
before    after   rate
4K video:
9145      8998    1.6%
9804      9465    3.4%
9645      9394    2.6%
8K video:
28411     27718   2.4%
31288     30056   3.9%
32336     31571   2.4%

Signed-off-by: xufuji456 <839789740 at qq.com>
---
 libavutil/imgutils.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
index 9ab5757cf6..f5898756ba 100644
--- a/libavutil/imgutils.c
+++ b/libavutil/imgutils.c
@@ -525,10 +525,18 @@ int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
         const uint8_t *src = src_data[i];
         h = (height + (1 << shift) - 1) >> shift;
 
-        for (j = 0; j < h; j++) {
-            memcpy(dst, src, linesize[i]);
-            dst += FFALIGN(linesize[i], align);
-            src += src_linesize[i];
+        // When src's linesize equals to dst, we could copy plane directly
+        if (src_linesize[i] == linesize[i] && FFALIGN(linesize[i], align) == linesize[i]) {
+            int len = linesize[i] * h;
+            memcpy(dst, src, len);
+            dst += len;
+            src += len;
+        } else {
+            for (j = 0; j < h; j++) {
+                memcpy(dst, src, linesize[i]);
+                dst += FFALIGN(linesize[i], align);
+                src += src_linesize[i];
+            }
         }
     }
 
-- 
2.32.0 (Apple Git-132)



More information about the ffmpeg-devel mailing list