diff mbox series

[FFmpeg-devel] Less CPU use in hwaccel MJPEG decoding

Message ID ZsDKRC5xDTMBBvXZ@vicerveza.homeunix.net
State New
Headers show
Series [FFmpeg-devel] Less CPU use in hwaccel MJPEG decoding | expand

Checks

Context Check Description
yinshiyou/commit_msg_loongarch64 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".

Commit Message

Lluís Batlle i Rossell Aug. 17, 2024, 4:05 p.m. UTC
attached
From 9e312c20d10839cb2c731acd23bc1b00177cd7dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= <viric@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 the entropy-encoded data in case of
using a hardware accelerator.
---
 libavcodec/mjpegdec.c | 19 ++++++++++++++-----
 libavcodec/mjpegdec.h |  1 +
 libavcodec/mxpegdec.c |  2 +-
 3 files changed, 16 insertions(+), 6 deletions(-)

Comments

Lluís Batlle i Rossell Aug. 17, 2024, 4:08 p.m. UTC | #1
v2 attached.

I had crippled the indentation.

On Sat, Aug 17, 2024 at 06:05:24PM +0200, Lluís Batlle i Rossell wrote:
> attached
From ea2d702daa88bf3b1bde1f03c26c69bb8ab9e2b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= <viric@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;
diff mbox series

Patch

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 8676155ecf..759a01b981 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2235,12 +2235,21 @@  found:
 }
 
 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
-                         const uint8_t **buf_ptr, const uint8_t *buf_end,
-                         const uint8_t **unescaped_buf_ptr,
-                         int *unescaped_buf_size)
+						 const struct AVHWAccel *hwaccel,
+						 const uint8_t **buf_ptr, const uint8_t *buf_end,
+						 const uint8_t **unescaped_buf_ptr,
+						 int *unescaped_buf_size)
 {
     int start_code;
-    start_code = find_marker(buf_ptr, buf_end);
+	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)
@@ -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;