Commit 900292fdf7 for ffmpeg
commit 900292fdf738fdd98011ab0e210ed62094636217
Author: Zhao Zhili <quinkblack@foxmail.com>
Date: Sat Nov 28 00:24:22 2026 +0800
avcodec/videotoolboxenc: signal full range for hardware frame input
The session's source image buffer attributes were only created for
software pixel formats. The parameter sets of the global header come
from a frame that VideoToolbox allocates from the session's pool, and
without the attributes that pool is in video range. Full range
hardware frames therefore got a global header signalling limited
range, while their in-band parameter sets signalled full range.
Create the attributes for hardware frames too, from sw_pix_fmt and the
same range policy as software input. VideoToolbox may convert a buffer
whose format differs from these attributes, so an unspecified range is
taken from the frames context, which allocates its buffers in that
range, and stored in the codec context for the muxer to signal. The
pool range selects a CoreVideo pixel format variant, so only YUV
source formats describe the encoded YUV range.
The attributes stay optional for hardware frames, and the session
keeps its defaults when sw_pix_fmt is unusable or when the requested
range has no CoreVideo format.
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 71ed2609ac..d3797204c5 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -32,6 +32,7 @@
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
+#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_videotoolbox.h"
#include "atsc_a53.h"
@@ -1117,16 +1118,27 @@ static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
CFNumberRef height_num = NULL;
CFMutableDictionaryRef pixel_buffer_info = NULL;
VTEncContext *vtctx = avctx->priv_data;
+ enum AVPixelFormat fmt = avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX ?
+ avctx->sw_pix_fmt : avctx->pix_fmt;
int range_guessed;
- int cv_color_format = get_cv_pixel_format(avctx->pix_fmt,
- avctx->color_range,
+ int cv_color_format = get_cv_pixel_format(fmt, avctx->color_range,
&range_guessed);
if (!cv_color_format) {
- const char *range_name = av_color_range_name(avctx->color_range);
+ const char *range_name;
+
+ /* The attributes are optional for hardware frames, so the session
+ * keeps its defaults rather than failing to open.
+ */
+ if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
+ *dict = NULL;
+ return 0;
+ }
+
+ range_name = av_color_range_name(avctx->color_range);
av_log(avctx, AV_LOG_ERROR,
"Could not get pixel format for color format '%s' range '%s'.\n",
- av_get_pix_fmt_name(avctx->pix_fmt),
+ av_get_pix_fmt_name(fmt),
range_name ? range_name : "Unknown");
return AVERROR(EINVAL);
}
@@ -1134,7 +1146,7 @@ static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
if (range_guessed && !vtctx->warned_color_range) {
vtctx->warned_color_range = true;
av_log(avctx, AV_LOG_WARNING, "Color range not set for %s. Using MPEG range.\n",
- av_get_pix_fmt_name(avctx->pix_fmt));
+ av_get_pix_fmt_name(fmt));
}
pixel_buffer_info = CFDictionaryCreateMutable(
@@ -1802,11 +1814,9 @@ static int vtenc_configure_encoder(AVCodecContext *avctx)
kCFBooleanTrue);
}
- if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
- status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
- if (status)
- goto init_cleanup;
- }
+ status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
+ if (status)
+ goto init_cleanup;
get_cv_gamma(avctx, &gamma_level);
vtctx->transfer_function = av_map_videotoolbox_color_trc_from_av(avctx->color_trc);
@@ -1861,6 +1871,25 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
// It can happen when user set avctx->profile directly.
if (vtctx->profile == AV_PROFILE_UNKNOWN)
vtctx->profile = avctx->profile;
+
+ /* The pool range selects a CoreVideo pixel format variant. The
+ * mapping check below keeps only formats in the CoreVideo table,
+ * where every non-YUV format carries the RGB flag.
+ */
+ if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX && avctx->hw_frames_ctx &&
+ avctx->color_range == AVCOL_RANGE_UNSPECIFIED) {
+ AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
+ AVVTFramesContext *vt_frames = frames_ctx->hwctx;
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frames_ctx->sw_format);
+ enum AVColorRange range = vt_frames->color_range;
+
+ if (desc && !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
+ (range == AVCOL_RANGE_MPEG || range == AVCOL_RANGE_JPEG) &&
+ av_map_videotoolbox_format_from_pixfmt2(frames_ctx->sw_format,
+ range == AVCOL_RANGE_JPEG))
+ avctx->color_range = range;
+ }
+
status = vtenc_configure_encoder(avctx);
if (status) return status;