[FFmpeg-devel] [PATCH] Less CPU use in hwaccel MJPEG decoding (v2)

Lluís Batlle i Rossell viric at viric.name
Sat Aug 17 19:08:20 EEST 2024


v2 attached.

I had crippled the indentation.

On Sat, Aug 17, 2024 at 06:05:24PM +0200, Lluís Batlle i Rossell wrote:
> attached
-------------- next part --------------
>From ea2d702daa88bf3b1bde1f03c26c69bb8ab9e2b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= <viric at viric.name>
Date: Sat, 17 Aug 2024 18:03:37 +0200
Subject: [PATCH] Less CPU use in hwaccel MJPEG decoding

It skips the unnecessary unescaping of the entropy-encoded data in case
of using a hardware accelerator.

This is 50% less of CPU use in my benchmark.
---
 libavcodec/mjpegdec.c | 11 ++++++++++-
 libavcodec/mjpegdec.h |  1 +
 libavcodec/mxpegdec.c |  2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 8676155ecf..43583c1ccf 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2235,6 +2235,7 @@ found:
 }
 
 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
+                         const struct AVHWAccel *hwaccel,
                          const uint8_t **buf_ptr, const uint8_t *buf_end,
                          const uint8_t **unescaped_buf_ptr,
                          int *unescaped_buf_size)
@@ -2242,6 +2243,14 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s,
     int start_code;
     start_code = find_marker(buf_ptr, buf_end);
 
+    if (start_code == SOS && hwaccel)
+    {
+        /* hardware accelerators don't need unescaping */
+        *unescaped_buf_ptr  = *buf_ptr;
+        *unescaped_buf_size = buf_end - *buf_ptr;
+        return start_code;
+    }
+
     av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
     if (!s->buffer)
         return AVERROR(ENOMEM);
@@ -2399,7 +2408,7 @@ redo_for_pal8:
     buf_end = buf + buf_size;
     while (buf_ptr < buf_end) {
         /* find start next marker */
-        start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
+        start_code = ff_mjpeg_find_marker(s, avctx->hwaccel, &buf_ptr, buf_end,
                                           &unescaped_buf_ptr,
                                           &unescaped_buf_size);
         /* EOF */
diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h
index 13c524d597..bc21eab647 100644
--- a/libavcodec/mjpegdec.h
+++ b/libavcodec/mjpegdec.h
@@ -184,6 +184,7 @@ int ff_mjpeg_decode_sos(MJpegDecodeContext *s,
                         const uint8_t *mb_bitmask,int mb_bitmask_size,
                         const AVFrame *reference);
 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
+						 const struct AVHWAccel *hwaccel,
                          const uint8_t **buf_ptr, const uint8_t *buf_end,
                          const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size);
 
diff --git a/libavcodec/mxpegdec.c b/libavcodec/mxpegdec.c
index 73df2ff9ff..d18d1541ed 100644
--- a/libavcodec/mxpegdec.c
+++ b/libavcodec/mxpegdec.c
@@ -202,7 +202,7 @@ static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
     s->got_mxm_bitmask = 0;
     s->got_sof_data = !!s->got_sof_data;
     while (buf_ptr < buf_end) {
-        start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
+        start_code = ff_mjpeg_find_marker(jpg, NULL, &buf_ptr, buf_end,
                                           &unescaped_buf_ptr, &unescaped_buf_size);
         if (start_code < 0)
             goto the_end;
-- 
2.44.1



More information about the ffmpeg-devel mailing list