* [FFmpeg-devel] [PR] Minor libvpx fixes and cosmetics (PR #21455)
@ 2026-01-13 22:49 Marton Balint via ffmpeg-devel
0 siblings, 0 replies; only message in thread
From: Marton Balint via ffmpeg-devel @ 2026-01-13 22:49 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Marton Balint
PR #21455 opened by Marton Balint (cus)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21455
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21455.patch
Nothing major here, some minor, mostly cosmetic issues I found.
>From 9efac7687553394a52589a96d5393db6d91614d7 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 11 Jan 2026 18:50:56 +0100
Subject: [PATCH 1/4] avcodec/libvpxenc: log the error message from the correct
encoder
It is possible that the error happens with the alpha encoder, not the normal
one, so let's always pass the affected encoder to the logging function.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
libavcodec/libvpxenc.c | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index af73966141..082709c41f 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -202,15 +202,14 @@ static const char *const ctlidstr[] = {
#endif
};
-static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
+static av_cold void log_encoder_error(void *logctx, struct vpx_codec_ctx *encoder, const char *desc)
{
- VPxContext *ctx = avctx->priv_data;
- const char *error = vpx_codec_error(&ctx->encoder);
- const char *detail = vpx_codec_error_detail(&ctx->encoder);
+ const char *error = vpx_codec_error(encoder);
+ const char *detail = vpx_codec_error_detail(encoder);
- av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
+ av_log(logctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
if (detail)
- av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
+ av_log(logctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
}
static av_cold void dump_enc_cfg(AVCodecContext *avctx,
@@ -477,7 +476,7 @@ static av_cold int codecctl_int(AVCodecContext *avctx,
if (res != VPX_CODEC_OK) {
snprintf(buf, sizeof(buf), "Failed to set %s codec control",
ctlidstr[id]);
- log_encoder_error(avctx, buf);
+ log_encoder_error(avctx, &ctx->encoder, buf);
return AVERROR(EINVAL);
}
@@ -486,7 +485,7 @@ static av_cold int codecctl_int(AVCodecContext *avctx,
if (res_alpha != VPX_CODEC_OK) {
snprintf(buf, sizeof(buf), "Failed to set %s alpha codec control",
ctlidstr[id]);
- log_encoder_error(avctx, buf);
+ log_encoder_error(avctx, &ctx->encoder_alpha, buf);
return AVERROR(EINVAL);
}
}
@@ -510,7 +509,7 @@ static av_cold int codecctl_intp(AVCodecContext *avctx,
if (res != VPX_CODEC_OK) {
snprintf(buf, sizeof(buf), "Failed to set %s codec control",
ctlidstr[id]);
- log_encoder_error(avctx, buf);
+ log_encoder_error(avctx, &ctx->encoder, buf);
return AVERROR(EINVAL);
}
@@ -519,7 +518,7 @@ static av_cold int codecctl_intp(AVCodecContext *avctx,
if (res_alpha != VPX_CODEC_OK) {
snprintf(buf, sizeof(buf), "Failed to set %s alpha codec control",
ctlidstr[id]);
- log_encoder_error(avctx, buf);
+ log_encoder_error(avctx, &ctx->encoder_alpha, buf);
return AVERROR(EINVAL);
}
}
@@ -1183,7 +1182,7 @@ static av_cold int vpx_init(AVCodecContext *avctx,
res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
if (res != VPX_CODEC_OK) {
dump_enc_cfg(avctx, &enccfg, AV_LOG_WARNING);
- log_encoder_error(avctx, "Failed to initialize encoder");
+ log_encoder_error(avctx, &ctx->encoder, "Failed to initialize encoder");
return AVERROR(EINVAL);
}
dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG);
@@ -1207,7 +1206,7 @@ static av_cold int vpx_init(AVCodecContext *avctx,
enccfg_alpha = enccfg;
res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags);
if (res != VPX_CODEC_OK) {
- log_encoder_error(avctx, "Failed to initialize alpha encoder");
+ log_encoder_error(avctx, &ctx->encoder_alpha, "Failed to initialize alpha encoder");
return AVERROR(EINVAL);
}
}
@@ -1637,14 +1636,14 @@ static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_
ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
if (ret) {
- log_encoder_error(avctx, "Failed to set_roi_map.\n");
+ log_encoder_error(avctx, &ctx->encoder, "Failed to set_roi_map.\n");
return ret;
}
memset(roi_map.ref_frame, -1, sizeof(roi_map.ref_frame));
if (vpx_codec_control(&ctx->encoder, VP9E_SET_ROI_MAP, &roi_map)) {
- log_encoder_error(avctx, "Failed to set VP9E_SET_ROI_MAP codec control.\n");
+ log_encoder_error(avctx, &ctx->encoder, "Failed to set VP9E_SET_ROI_MAP codec control.\n");
ret = AVERROR_INVALIDDATA;
}
av_freep(&roi_map.roi_map);
@@ -1669,12 +1668,12 @@ static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_
int ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
if (ret) {
- log_encoder_error(avctx, "Failed to set_roi_map.\n");
+ log_encoder_error(avctx, &ctx->encoder, "Failed to set_roi_map.\n");
return ret;
}
if (vpx_codec_control(&ctx->encoder, VP8E_SET_ROI_MAP, &roi_map)) {
- log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n");
+ log_encoder_error(avctx, &ctx->encoder, "Failed to set VP8E_SET_ROI_MAP codec control.\n");
ret = AVERROR_INVALIDDATA;
}
@@ -1729,7 +1728,7 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
cfg.rc_max_quantizer = avctx->qmax;
res = vpx_codec_enc_config_set(&ctx->encoder, &cfg);
if (res != VPX_CODEC_OK) {
- log_encoder_error(avctx, "Error reconfiguring encoder");
+ log_encoder_error(avctx, &ctx->encoder, "Error reconfiguring encoder");
return AVERROR_INVALIDDATA;
}
}
@@ -1867,7 +1866,7 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
duration, flags, ctx->deadline);
if (res != VPX_CODEC_OK) {
- log_encoder_error(avctx, "Error encoding frame");
+ log_encoder_error(avctx, &ctx->encoder, "Error encoding frame");
return AVERROR_INVALIDDATA;
}
@@ -1875,7 +1874,7 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp,
duration, flags, ctx->deadline);
if (res != VPX_CODEC_OK) {
- log_encoder_error(avctx, "Error encoding alpha frame");
+ log_encoder_error(avctx, &ctx->encoder_alpha, "Error encoding alpha frame");
return AVERROR_INVALIDDATA;
}
}
--
2.49.1
>From f1a60ec6070b50608a214b58abb8975a6dbfd4d1 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 11 Jan 2026 21:39:05 +0100
Subject: [PATCH 2/4] avcodec/libvpxdec: cache the decoder interface
This saves us some #ifdefry.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
libavcodec/libvpxdec.c | 25 +++++++++----------------
1 file changed, 9 insertions(+), 16 deletions(-)
diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c
index c6187fd5a1..b5898c6951 100644
--- a/libavcodec/libvpxdec.c
+++ b/libavcodec/libvpxdec.c
@@ -41,6 +41,7 @@
#include "profiles.h"
typedef struct VPxDecoderContext {
+ const struct vpx_codec_iface *iface;
struct vpx_codec_ctx decoder;
struct vpx_codec_ctx decoder_alpha;
AVBufferPool *pool;
@@ -84,9 +85,9 @@ static int release_frame_buffer(void *priv, vpx_codec_frame_buffer_t *fb)
}
static av_cold int vpx_init(AVCodecContext *avctx,
- struct vpx_codec_ctx* decoder,
- const struct vpx_codec_iface *iface)
+ struct vpx_codec_ctx* decoder)
{
+ VPxContext *ctx = avctx->priv_data;
struct vpx_codec_dec_cfg deccfg = {
.threads = FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), MAX_VPX_THREADS)
};
@@ -94,7 +95,7 @@ static av_cold int vpx_init(AVCodecContext *avctx,
av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
- if (vpx_codec_dec_init(decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
+ if (vpx_codec_dec_init(decoder, ctx->iface, &deccfg, 0) != VPX_CODEC_OK) {
const char *error = vpx_codec_error(decoder);
av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
error);
@@ -239,17 +240,7 @@ static int vpx_decode(AVCodecContext *avctx, AVFrame *picture,
if (additional_id == 1) { // 1 stands for alpha channel data.
if (!ctx->has_alpha_channel) {
ctx->has_alpha_channel = 1;
- ret = vpx_init(avctx,
- &ctx->decoder_alpha,
-#if CONFIG_LIBVPX_VP8_DECODER && CONFIG_LIBVPX_VP9_DECODER
- (avctx->codec_id == AV_CODEC_ID_VP8) ?
- vpx_codec_vp8_dx() : vpx_codec_vp9_dx()
-#elif CONFIG_LIBVPX_VP8_DECODER
- vpx_codec_vp8_dx()
-#else
- vpx_codec_vp9_dx()
-#endif
- );
+ ret = vpx_init(avctx, &ctx->decoder_alpha);
if (ret)
return ret;
}
@@ -349,7 +340,8 @@ static av_cold int vpx_free(AVCodecContext *avctx)
static av_cold int vp8_init(AVCodecContext *avctx)
{
VPxContext *ctx = avctx->priv_data;
- return vpx_init(avctx, &ctx->decoder, vpx_codec_vp8_dx());
+ ctx->iface = vpx_codec_vp8_dx();
+ return vpx_init(avctx, &ctx->decoder);
}
const FFCodec ff_libvpx_vp8_decoder = {
@@ -372,7 +364,8 @@ const FFCodec ff_libvpx_vp8_decoder = {
static av_cold int vp9_init(AVCodecContext *avctx)
{
VPxContext *ctx = avctx->priv_data;
- return vpx_init(avctx, &ctx->decoder, vpx_codec_vp9_dx());
+ ctx->iface = vpx_codec_vp9_dx();
+ return vpx_init(avctx, &ctx->decoder);
}
const FFCodec ff_libvpx_vp9_decoder = {
--
2.49.1
>From 28c6c9d49cb6bf293accd3d4d679eaff033e24b7 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 11 Jan 2026 22:15:27 +0100
Subject: [PATCH 3/4] avcodec/libvpxdec: use codec capabilities to determine if
external frame buffer can be used
Previously we used the codec or at the time of decoding fb_priv for this, but
fb_priv can be nonzero even if an external frame buffer is not set, so it's
cleaner to use the capability flag directly.
Also check the result of vpx_codec_set_frame_buffer_functions.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
libavcodec/libvpxdec.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c
index b5898c6951..5b926b82ba 100644
--- a/libavcodec/libvpxdec.c
+++ b/libavcodec/libvpxdec.c
@@ -102,8 +102,12 @@ static av_cold int vpx_init(AVCodecContext *avctx,
return AVERROR(EINVAL);
}
- if (avctx->codec_id == AV_CODEC_ID_VP9)
- vpx_codec_set_frame_buffer_functions(decoder, get_frame_buffer, release_frame_buffer, avctx->priv_data);
+ if (vpx_codec_get_caps(ctx->iface) & VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER) {
+ if (vpx_codec_set_frame_buffer_functions(decoder, get_frame_buffer, release_frame_buffer, avctx->priv_data)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to set frame buffer.\n");
+ return AVERROR_EXTERNAL;
+ }
+ }
return 0;
}
@@ -299,7 +303,7 @@ static int vpx_decode(AVCodecContext *avctx, AVFrame *picture,
linesizes[3] =
ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
- if (img->fb_priv && (!ctx->has_alpha_channel || img_alpha->fb_priv)) {
+ if (vpx_codec_get_caps(ctx->iface) & VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER) {
ret = ff_decode_frame_props(avctx, picture);
if (ret < 0)
return ret;
--
2.49.1
>From bd5e37146405f842349d424d0daba001263eff5b Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Mon, 12 Jan 2026 00:58:00 +0100
Subject: [PATCH 4/4] avcodec/libvpxdec: only set has_alpha_channel if alpha
decoder initialization was successful
Otherwise we might crash on a subsequent call to vpx_decode().
Signed-off-by: Marton Balint <cus@passwd.hu>
---
libavcodec/libvpxdec.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c
index 5b926b82ba..b3871a7b6c 100644
--- a/libavcodec/libvpxdec.c
+++ b/libavcodec/libvpxdec.c
@@ -243,10 +243,10 @@ static int vpx_decode(AVCodecContext *avctx, AVFrame *picture,
side_data_size -= 8;
if (additional_id == 1) { // 1 stands for alpha channel data.
if (!ctx->has_alpha_channel) {
- ctx->has_alpha_channel = 1;
ret = vpx_init(avctx, &ctx->decoder_alpha);
if (ret)
return ret;
+ ctx->has_alpha_channel = 1;
}
ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
side_data_size);
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-01-13 22:49 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-13 22:49 [FFmpeg-devel] [PR] Minor libvpx fixes and cosmetics (PR #21455) Marton Balint via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git