Commit 59b759319b for ffmpeg
commit 59b759319b454ce47e947536334e2f6283cc9a82
Author: Zhao Zhili <quinkblack@foxmail.com>
Date: Thu Oct 1 18:06:32 2026 +0800
avcodec/videotoolboxenc: derive dts with the shared helper
The encoder took dts from VideoToolbox and subtracted a fixed offset,
measured once from the first input frames. The offset only fits a
constant frame interval: when the interval grows later in the stream,
dts ends up above pts.
Derive dts from the pts of the output packets instead. The result
follows the actual pts values, so it also holds at a variable frame
rate. The first packets are no longer withheld until the offset has
been measured.
VideoToolbox reports whether it reorders but not the depth, so
REORDER_DELAY is the delay when reorder_delay is auto. Add the
reorder_delay option to override it.
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 5183deb68b..e60d3c1afc 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
#include "version_major.h"
#define LIBAVCODEC_VERSION_MINOR 29
-#define LIBAVCODEC_VERSION_MICRO 100
+#define LIBAVCODEC_VERSION_MICRO 101
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 36a08d0a09..5a8552a5a9 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -36,6 +36,7 @@
#include "atsc_a53.h"
#include "codec_internal.h"
+#include "dts_gen.h"
#include "encode.h"
#include "h264.h"
#include "h264_sei.h"
@@ -66,18 +67,17 @@ enum { kVTQPModulationLevel_Disable = 0 };
/*
* Upper bound of the reorder depth of VideoToolbox H.264 and HEVC
- * output, in frames. The encoder shifts the decode timestamps
- * VideoToolbox reports by the pts distance of this many frames, and
- * the value is published as AVCodecContext.has_b_frames; understating
- * it outputs frames with dts greater than pts.
+ * output, in frames. It is published as AVCodecContext.has_b_frames
+ * and is the reorder delay used to derive dts when reorder_delay is
+ * auto.
*
* The VideoToolbox API does not report the depth, so the value was
* measured from encoder output on macOS 15 (Apple silicon). It bounds
* the real depth instead of matching it everywhere: the software
* H.264 encoder (require_sw) reorders by one frame, and untested
* devices may reorder less. Keeping one value for every encoder is
- * safe because overstating only shifts dts earlier, and it is simpler
- * than branching per encoder.
+ * safe because overstating only lowers dts, while understating makes
+ * dts non-monotonic, and it is simpler than branching per encoder.
*/
#define REORDER_DELAY 2
@@ -283,8 +283,7 @@ typedef struct VTEncContext {
int64_t frame_ct_out;
int64_t frame_ct_in;
- int64_t first_pts;
- int64_t dts_delta;
+ FFDTSGen dts_gen;
int profile;
int level;
@@ -310,6 +309,7 @@ typedef struct VTEncContext {
int power_efficient;
int max_ref_frames;
int spatialaq;
+ int reorder_delay;
} VTEncContext;
static void vtenc_free_buf_node(BufNode *info)
@@ -1802,8 +1802,6 @@ static int vtenc_configure_encoder(AVCodecContext *avctx)
goto init_cleanup;
}
- vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
-
get_cv_gamma(avctx, &gamma_level);
vtctx->transfer_function = av_map_videotoolbox_color_trc_from_av(avctx->color_trc);
vtctx->ycbcr_matrix = av_map_videotoolbox_color_matrix_from_av(avctx->colorspace);
@@ -1847,6 +1845,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
VTEncContext *vtctx = avctx->priv_data;
CFBooleanRef has_b_frames_cfbool;
int status;
+ int delay;
pthread_once(&once_ctrl, loadVTEncSymbols);
@@ -1874,6 +1873,17 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
}
avctx->has_b_frames = vtctx->has_b_frames;
+ delay = vtctx->reorder_delay;
+ if (delay == FF_DTS_GEN_DELAY_AUTO)
+ delay = vtctx->has_b_frames;
+
+ status = ff_dts_gen_init(avctx, &vtctx->dts_gen, delay);
+ if (status < 0)
+ return status;
+
+ if (vtctx->dts_gen.delay >= 0)
+ avctx->has_b_frames = vtctx->dts_gen.delay;
+
return 0;
}
@@ -2291,10 +2301,8 @@ static int vtenc_cm_to_avpacket(
size_t in_buf_size;
size_t out_buf_size;
size_t sei_nalu_size = 0;
- int64_t dts_delta;
int nalu_count;
CMTime pts;
- CMTime dts;
CMVideoFormatDescriptionRef vid_fmt;
vtenc_get_frame_info(sample_buffer, &is_key_frame);
@@ -2381,26 +2389,14 @@ static int vtenc_cm_to_avpacket(
}
pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
- dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
if (CMTIME_IS_INVALID(pts)) {
av_log(avctx, AV_LOG_ERROR, "PTS is invalid.\n");
return AVERROR_EXTERNAL;
}
- if (CMTIME_IS_INVALID(dts)) {
- if (!vtctx->has_b_frames) {
- dts = pts;
- } else {
- av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
- return AVERROR_EXTERNAL;
- }
- }
-
- dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
pkt->pts = av_rescale_q(pts.value, (AVRational){1, pts.timescale}, avctx->time_base);
- pkt->dts = av_rescale_q(dts.value, (AVRational){1, dts.timescale}, avctx->time_base)
- - dts_delta;
+ ff_dts_gen_fill_pkt(avctx, &vtctx->dts_gen, pkt);
return 0;
}
@@ -2618,7 +2614,6 @@ static av_cold int vtenc_frame(
int *got_packet)
{
VTEncContext *vtctx = avctx->priv_data;
- bool get_frame;
int status;
CMSampleBufferRef buf = NULL;
ExtraSEI sei = {0};
@@ -2631,11 +2626,7 @@ static av_cold int vtenc_frame(
goto end_nopkt;
}
- if (vtctx->frame_ct_in == 0) {
- vtctx->first_pts = frame->pts;
- } else if(vtctx->frame_ct_in == vtctx->has_b_frames) {
- vtctx->dts_delta = frame->pts - vtctx->first_pts;
- }
+ ff_dts_gen_record_pts(&vtctx->dts_gen, frame->pts);
vtctx->frame_ct_in++;
} else if(!vtctx->flushing) {
@@ -2652,11 +2643,6 @@ static av_cold int vtenc_frame(
}
*got_packet = 0;
- get_frame = vtctx->dts_delta >= 0 || !frame;
- if (!get_frame) {
- status = 0;
- goto end_nopkt;
- }
status = vtenc_q_pop(vtctx, !frame, &buf, &sei);
if (status) goto end_nopkt;
@@ -2916,6 +2902,7 @@ static const AVOption h264_options[] = {
{ "constant_bit_rate", "Require constant bit rate (macOS 13 or newer)", OFFSET(constant_bit_rate), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "max_slice_bytes", "Set the maximum number of bytes in an H.264 slice.", OFFSET(max_slice_bytes), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
+ FF_DTS_GEN_REORDER_DELAY_OPTION(OFFSET(reorder_delay), VE),
COMMON_OPTIONS
{ NULL },
};
@@ -2964,6 +2951,7 @@ static const AVOption hevc_options[] = {
{ "constant_bit_rate", "Require constant bit rate (macOS 13 or newer)", OFFSET(constant_bit_rate), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+ FF_DTS_GEN_REORDER_DELAY_OPTION(OFFSET(reorder_delay), VE),
COMMON_OPTIONS
{ NULL },
};