diff mbox series

[FFmpeg-devel] Skip parsing of hwaccel mjpeg after decoding

Message ID ZsHnKG7A0KpzsMHg@vicerveza.homeunix.net
State New
Headers show
Series [FFmpeg-devel] Skip parsing of hwaccel mjpeg after 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: ".
andriy/commit_msg_x86 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: ".
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Lluís Batlle i Rossell Aug. 18, 2024, 12:20 p.m. UTC
Attached.

Together with previous patch "Less CPU use in hwaccel MJPEG decoding"
the hwaccel mjpeg decoding uses about 90% less cpu than before.
From 5960e16ae7561c6c6ad982c90f4e6ea1d30df91b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= <viric@viric.name>
Date: Sun, 18 Aug 2024 14:14:50 +0200
Subject: [PATCH 2/2] Skip parsing of hwaccel mjpeg after decoding

This avoids completely the heaviest cpu work, find_marker, in case of
hwaccel.
---
 libavcodec/mjpegdec.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 43583c1ccf..b835162fcd 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -1774,15 +1774,28 @@  next_field:
 
     if (s->avctx->hwaccel) {
         int bytes_to_start = get_bits_count(&s->gb) / 8;
+        int bytes_left = s->raw_scan_buffer_size - bytes_to_start;
         av_assert0(bytes_to_start >= 0 &&
                    s->raw_scan_buffer_size >= bytes_to_start);
 
         ret = FF_HW_CALL(s->avctx, decode_slice,
                          s->raw_scan_buffer      + bytes_to_start,
-                         s->raw_scan_buffer_size - bytes_to_start);
+                         bytes_left);
         if (ret < 0)
             return ret;
 
+        /* The raw_scan_buffer includes all until end of jfif,
+         * so we can skip until 0xFF EOI, to avoid find_markers.
+         * Due to alignment, there can be multiple padding 0x00 at the end. */
+        for(int i = bytes_left - 9; i <= bytes_left - 2; ++i) {
+            const uint8_t *ptr = s->raw_scan_buffer + bytes_to_start + i;
+            if (i > 0 && ptr[0] == 0xff && ptr[1] == EOI) {
+                skip_bits(&s->gb, 8 * i);
+                av_log(s->avctx, AV_LOG_DEBUG,
+                        "Skipping up to end EOI after hwaccel decode\n");
+                break;
+            }
+        }
     } else if (s->lossless) {
         av_assert0(s->picture_ptr == s->picture);
         if (CONFIG_JPEGLS_DECODER && s->ls) {