PR #21195 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21195
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21195.patch


>From bc7d1847c5ffe4984bc65e6b800ab43913963d23 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Sun, 14 Dec 2025 16:05:09 +0100
Subject: [PATCH 1/5] avcodec/libsvtjpegxs{dec,enc}: Don't get log level
 multiple times

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavcodec/libsvtjpegxsdec.c | 9 +++------
 libavcodec/libsvtjpegxsenc.c | 9 +++------
 2 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/libavcodec/libsvtjpegxsdec.c b/libavcodec/libsvtjpegxsdec.c
index 45d9134cd4..56beff9edb 100644
--- a/libavcodec/libsvtjpegxsdec.c
+++ b/libavcodec/libsvtjpegxsdec.c
@@ -237,12 +237,9 @@ static av_cold int svt_jpegxs_dec_init(AVCodecContext* 
avctx)
 {
     SvtJpegXsDecodeContext* svt_dec = avctx->priv_data;
 
-    if (av_log_get_level() < AV_LOG_DEBUG)
-        svt_dec->decoder.verbose = VERBOSE_ERRORS;
-    else if (av_log_get_level() == AV_LOG_DEBUG)
-        svt_dec->decoder.verbose = VERBOSE_SYSTEM_INFO;
-    else
-        svt_dec->decoder.verbose = VERBOSE_WARNINGS;
+    int log_level = av_log_get_level();
+    svt_dec->decoder.verbose = log_level < AV_LOG_DEBUG ? VERBOSE_ERRORS :
+                                  log_level == AV_LOG_DEBUG ? 
VERBOSE_SYSTEM_INFO : VERBOSE_WARNINGS;
 
     if (svt_dec->proxy_mode == 1)
         svt_dec->decoder.proxy_mode = proxy_mode_half;
diff --git a/libavcodec/libsvtjpegxsenc.c b/libavcodec/libsvtjpegxsenc.c
index 2f7ec095df..dc4d238457 100644
--- a/libavcodec/libsvtjpegxsenc.c
+++ b/libavcodec/libsvtjpegxsenc.c
@@ -189,12 +189,9 @@ static av_cold int svt_jpegxs_enc_init(AVCodecContext* 
avctx) {
 
     svt_enc->encoder.threads_num = FFMIN(avctx->thread_count ? 
avctx->thread_count : av_cpu_count(), 64);
 
-    if (av_log_get_level() < AV_LOG_DEBUG)
-        svt_enc->encoder.verbose = VERBOSE_ERRORS;
-    else if (av_log_get_level() == AV_LOG_DEBUG)
-        svt_enc->encoder.verbose = VERBOSE_SYSTEM_INFO;
-    else
-        svt_enc->encoder.verbose = VERBOSE_WARNINGS;
+    int log_level = av_log_get_level();
+    svt_enc->encoder.verbose = log_level < AV_LOG_DEBUG ? VERBOSE_ERRORS :
+                                  log_level == AV_LOG_DEBUG ? 
VERBOSE_SYSTEM_INFO : VERBOSE_WARNINGS;
 
     if (avctx->bit_rate <= 0) {
         av_log(avctx, AV_LOG_ERROR, "bitrate can't be 0\n");
-- 
2.49.1


>From c261ea81eb6767e1e8581d51b3323cfbecc77f36 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Sun, 14 Dec 2025 16:23:35 +0100
Subject: [PATCH 2/5] avcodec/libsvtjpegxsenc: Remove dead code

The pixel format has already been checked generically.
This also fixes the bug that the earlier code ignored
the return value of set_pix_fmt().

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavcodec/libsvtjpegxsenc.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/libavcodec/libsvtjpegxsenc.c b/libavcodec/libsvtjpegxsenc.c
index dc4d238457..6b10a32d33 100644
--- a/libavcodec/libsvtjpegxsenc.c
+++ b/libavcodec/libsvtjpegxsenc.c
@@ -25,6 +25,7 @@
 
 #include <SvtJpegxsEnc.h>
 
+#include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
@@ -113,62 +114,61 @@ static av_cold int svt_jpegxs_enc_free(AVCodecContext* 
avctx) {
     return 0;
 }
 
-static int set_pix_fmt(AVCodecContext* avctx, svt_jpeg_xs_encoder_api_t 
*encoder)
+static void set_pix_fmt(AVCodecContext *avctx, svt_jpeg_xs_encoder_api_t 
*encoder)
 {
     switch (avctx->pix_fmt) {
     case AV_PIX_FMT_YUV420P:
         encoder->input_bit_depth = 8;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV420;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV422P:
         encoder->input_bit_depth = 8;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV422;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV444P:
         encoder->input_bit_depth = 8;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV444_OR_RGB;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV420P10LE:
         encoder->input_bit_depth = 10;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV420;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV422P10LE:
         encoder->input_bit_depth = 10;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV422;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV444P10LE:
         encoder->input_bit_depth = 10;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV444_OR_RGB;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV420P12LE:
         encoder->input_bit_depth = 12;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV420;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV422P12LE:
         encoder->input_bit_depth = 12;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV422;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV444P12LE:
         encoder->input_bit_depth = 12;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV444_OR_RGB;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV420P14LE:
         encoder->input_bit_depth = 14;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV420;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV422P14LE:
         encoder->input_bit_depth = 14;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV422;
-        return 0;
+        return;
     case AV_PIX_FMT_YUV444P14LE:
         encoder->input_bit_depth = 14;
         encoder->colour_format = COLOUR_FORMAT_PLANAR_YUV444_OR_RGB;
-        return 0;
+        return;
     default:
+        av_unreachable("Already checked via CODEC_PIXFMTS_ARRAY");
         break;
     }
-    av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
-    return AVERROR_INVALIDDATA;
 }
 
 static av_cold int svt_jpegxs_enc_init(AVCodecContext* avctx) {
-- 
2.49.1


>From 39f2635e34ba8e7b174d000f9a1f4ddf49b239d4 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Sun, 14 Dec 2025 16:47:12 +0100
Subject: [PATCH 3/5] avcodec/libsvtjpegxs{dec,enc}: Don't call av_cpu_count()
 multiple times

(Like the old code, the new code limits the number of threads to 64,
even when the user explicitly set a higher thread count. I don't know
whether it is intentional to apply this limit even when the user
explicitly supplied the number of threads.)

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavcodec/libsvtjpegxsdec.c | 3 ++-
 libavcodec/libsvtjpegxsenc.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libavcodec/libsvtjpegxsdec.c b/libavcodec/libsvtjpegxsdec.c
index 56beff9edb..2499f32059 100644
--- a/libavcodec/libsvtjpegxsdec.c
+++ b/libavcodec/libsvtjpegxsdec.c
@@ -248,7 +248,8 @@ static av_cold int svt_jpegxs_dec_init(AVCodecContext* 
avctx)
     else
         svt_dec->decoder.proxy_mode = proxy_mode_full;
 
-    svt_dec->decoder.threads_num = FFMIN(avctx->thread_count ? 
avctx->thread_count : av_cpu_count(), 64);
+    int thread_count = avctx->thread_count ? avctx->thread_count : 
av_cpu_count();
+    svt_dec->decoder.threads_num = FFMIN(thread_count, 64);
     svt_dec->decoder.use_cpu_flags = CPU_FLAGS_ALL;
 
     return 0;
diff --git a/libavcodec/libsvtjpegxsenc.c b/libavcodec/libsvtjpegxsenc.c
index 6b10a32d33..9daf226c9d 100644
--- a/libavcodec/libsvtjpegxsenc.c
+++ b/libavcodec/libsvtjpegxsenc.c
@@ -187,7 +187,8 @@ static av_cold int svt_jpegxs_enc_init(AVCodecContext* 
avctx) {
 
     set_pix_fmt(avctx, &svt_enc->encoder);
 
-    svt_enc->encoder.threads_num = FFMIN(avctx->thread_count ? 
avctx->thread_count : av_cpu_count(), 64);
+    int thread_count = avctx->thread_count ? avctx->thread_count : 
av_cpu_count();
+    svt_enc->encoder.threads_num = FFMIN(thread_count, 64);
 
     int log_level = av_log_get_level();
     svt_enc->encoder.verbose = log_level < AV_LOG_DEBUG ? VERBOSE_ERRORS :
-- 
2.49.1


>From 6b58bd0916024e77096b045cefb5124afb9c3466 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Sun, 14 Dec 2025 17:00:06 +0100
Subject: [PATCH 4/5] avcodec/libsvtjpegxsenc: Don't copy unnecessarily

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavcodec/libsvtjpegxsenc.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/libavcodec/libsvtjpegxsenc.c b/libavcodec/libsvtjpegxsenc.c
index 9daf226c9d..2a7c67fe44 100644
--- a/libavcodec/libsvtjpegxsenc.c
+++ b/libavcodec/libsvtjpegxsenc.c
@@ -55,9 +55,9 @@ static int svt_jpegxs_enc_encode(AVCodecContext* avctx, 
AVPacket* pkt,
 {
     SvtJpegXsEncodeContext* svt_enc = avctx->priv_data;
 
-    svt_jpeg_xs_bitstream_buffer_t out_buf;
-    svt_jpeg_xs_image_buffer_t in_buf;
     svt_jpeg_xs_frame_t enc_input;
+    svt_jpeg_xs_bitstream_buffer_t *const out_buf = &enc_input.bitstream;
+    svt_jpeg_xs_image_buffer_t *const in_buf = &enc_input.image;
     svt_jpeg_xs_frame_t enc_output;
 
     SvtJxsErrorType_t err = SvtJxsErrorNone;
@@ -67,19 +67,17 @@ static int svt_jpegxs_enc_encode(AVCodecContext* avctx, 
AVPacket* pkt,
     if (ret < 0)
         return ret;
 
-    out_buf.buffer = pkt->data;// output bitstream ptr
-    out_buf.allocation_size = pkt->size;// output bitstream size
-    out_buf.used_size = 0;
+    out_buf->buffer = pkt->data;// output bitstream ptr
+    out_buf->allocation_size = pkt->size;// output bitstream size
+    out_buf->used_size = 0;
 
     for (int comp = 0; comp < 3; comp++) {
         // svt-jpegxs require stride in pixel's not in bytes, this means that 
for 10 bit-depth, stride is half the linesize
-        in_buf.stride[comp] = frame->linesize[comp] / pixel_size;
-        in_buf.data_yuv[comp] = frame->data[comp];
-        in_buf.alloc_size[comp] = in_buf.stride[comp] * 
svt_enc->encoder.source_height * pixel_size;
+        in_buf->stride[comp]     = frame->linesize[comp] / pixel_size;
+        in_buf->data_yuv[comp]   = frame->data[comp];
+        in_buf->alloc_size[comp] = in_buf->stride[comp] * 
svt_enc->encoder.source_height * pixel_size;
     }
 
-    enc_input.bitstream = out_buf;
-    enc_input.image = in_buf;
     enc_input.user_prv_ctx_ptr = pkt;
 
     err = svt_jpeg_xs_encoder_send_picture(&svt_enc->encoder, &enc_input, 1 
/*blocking*/);
-- 
2.49.1


>From 265b645fd9ed30174e9fb0c6ba67e19f1d5eb546 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Sun, 14 Dec 2025 17:06:07 +0100
Subject: [PATCH 5/5] avcodec/libsvtjpegxsenc: Replace divisions by shifts

Also simplify setting alloc_size.

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavcodec/libsvtjpegxsenc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavcodec/libsvtjpegxsenc.c b/libavcodec/libsvtjpegxsenc.c
index 2a7c67fe44..ba626b03c0 100644
--- a/libavcodec/libsvtjpegxsenc.c
+++ b/libavcodec/libsvtjpegxsenc.c
@@ -61,7 +61,6 @@ static int svt_jpegxs_enc_encode(AVCodecContext* avctx, 
AVPacket* pkt,
     svt_jpeg_xs_frame_t enc_output;
 
     SvtJxsErrorType_t err = SvtJxsErrorNone;
-    uint32_t pixel_size = svt_enc->encoder.input_bit_depth <= 8 ? 1 : 2;
 
     int ret = ff_get_encode_buffer(avctx, pkt, svt_enc->bitstream_frame_size, 
0);
     if (ret < 0)
@@ -71,11 +70,12 @@ static int svt_jpegxs_enc_encode(AVCodecContext* avctx, 
AVPacket* pkt,
     out_buf->allocation_size = pkt->size;// output bitstream size
     out_buf->used_size = 0;
 
+    unsigned pixel_shift = svt_enc->encoder.input_bit_depth <= 8 ? 0 : 1;
     for (int comp = 0; comp < 3; comp++) {
         // svt-jpegxs require stride in pixel's not in bytes, this means that 
for 10 bit-depth, stride is half the linesize
-        in_buf->stride[comp]     = frame->linesize[comp] / pixel_size;
+        in_buf->stride[comp]     = frame->linesize[comp] >> pixel_shift;
         in_buf->data_yuv[comp]   = frame->data[comp];
-        in_buf->alloc_size[comp] = in_buf->stride[comp] * 
svt_enc->encoder.source_height * pixel_size;
+        in_buf->alloc_size[comp] = frame->linesize[comp] * 
svt_enc->encoder.source_height;
     }
 
     enc_input.user_prv_ctx_ptr = pkt;
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to