Commit 34ea6f715f for ffmpeg
commit 34ea6f715f243890d77e44dbd8f3e8d8887d4fb1
Author: Zhao Zhili <quinkblack@foxmail.com>
Date: Thu Oct 1 18:09:14 2026 +0800
avcodec/mediacodecenc: derive dts with the shared helper
MediaCodec reports only a presentation timestamp. The encoder left dts
unset when B frames were enabled and therefore demanded
-strict experimental.
Derive dts with the helper, using max_b_frames as the reorder depth:
the "max-bframes" format key limits the B frames between two anchors,
which bounds the depth. The -strict experimental requirement is no
longer needed. With pts_as_dts set, which is the default without B
frames, the depth stays zero and dts equals pts as before. Add the
reorder_delay option to override the depth.
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
diff --git a/doc/encoders.texi b/doc/encoders.texi
index d85d99776b..b452e806ec 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3727,8 +3727,22 @@ Constant bitrate mode with frame drops
@end table
@item pts_as_dts @var{boolean}
-Use PTS as DTS. This is a workaround since MediaCodec API doesn't provide
-decoding timestamp. It is enabled automatically if B frame is 0.
+Use PTS as DTS when @option{reorder_delay} is @samp{auto}. It is enabled
+automatically if B frame is 0.
+
+@item reorder_delay @var{integer}
+Reorder delay in frames used to generate DTS, since MediaCodec API doesn't
+provide decoding timestamp. A value below the real reorder depth makes DTS
+non-monotonic, a larger value lowers every DTS by the excess frames.
+
+@table @samp
+@item auto
+Use the number of B frames, or 0 when @option{pts_as_dts} is enabled. This is
+the default.
+@item skip
+For codecs that support reordering, leave DTS unset. For codecs without
+reordering, including AV1, DTS equals PTS regardless of this option.
+@end table
@item operating_rate @var{integer}
The desired operating rate that the codec will need to operate at, zero for
diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 631184cbee..7edb36967f 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -34,6 +34,7 @@
#include "avcodec.h"
#include "bsf.h"
#include "codec_internal.h"
+#include "dts_gen.h"
#include "encode.h"
#include "hwconfig.h"
#include "jni.h"
@@ -79,11 +80,13 @@ typedef struct MediaCodecEncContext {
AVFrame *frame;
AVBSFContext *bsf;
+ FFDTSGen dts_gen;
int bitrate_mode;
int level;
int pts_as_dts;
int extract_extradata;
+ int reorder_delay;
// Ref. MediaFormat KEY_OPERATING_RATE
int operating_rate;
int async_mode;
@@ -399,6 +402,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
FFAMediaFormat *format = NULL;
int ret;
int gop;
+ int delay;
// Init async state first, so we can do cleanup safely on error path.
ret = mediacodec_init_async_state(avctx);
@@ -556,18 +560,21 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
ff_AMediaFormat_setInt32(format, "level", s->level);
}
- if (avctx->max_b_frames > 0) {
- if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
- av_log(avctx, AV_LOG_ERROR,
- "Enabling B frames will produce packets with no DTS. "
- "Use -strict experimental to use it anyway.\n");
- ret = AVERROR(EINVAL);
- goto bailout;
- }
+ if (avctx->max_b_frames > 0)
ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
- }
if (s->pts_as_dts == -1)
s->pts_as_dts = avctx->max_b_frames <= 0;
+
+ /* MediaCodec provides no DTS. "max-bframes" limits the B pictures
+ * between two anchors, which bounds the reorder depth.
+ */
+ delay = s->reorder_delay;
+ if (delay == FF_DTS_GEN_DELAY_AUTO)
+ delay = s->pts_as_dts ? 0 : av_clip(avctx->max_b_frames, 0,
+ FF_DTS_GEN_DELAY_MAX);
+ if ((ret = ff_dts_gen_init(avctx, &s->dts_gen, delay)) < 0)
+ goto bailout;
+
if (s->operating_rate > 0)
ff_AMediaFormat_setInt32(format, "operating-rate", s->operating_rate);
@@ -738,8 +745,7 @@ static int mediacodec_receive(AVCodecContext *avctx, AVPacket *pkt)
}
memcpy(pkt->data + extradata_size, out_buf + out_info.offset, out_info.size);
pkt->pts = av_rescale_q(out_info.presentationTimeUs, AV_TIME_BASE_Q, avctx->time_base);
- if (s->pts_as_dts)
- pkt->dts = pkt->pts;
+ ff_dts_gen_fill_pkt(avctx, &s->dts_gen, pkt);
if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
pkt->flags |= AV_PKT_FLAG_KEY;
ret = 0;
@@ -806,6 +812,7 @@ static int mediacodec_send(AVCodecContext *avctx,
return ff_AMediaCodec_signalEndOfInputStream(codec);
}
+ ff_dts_gen_record_pts(&s->dts_gen, frame->pts);
if (frame->data[3])
av_mediacodec_release_buffer((AVMediaCodecBuffer *)frame->data[3], 1);
return 0;
@@ -828,6 +835,7 @@ static int mediacodec_send(AVCodecContext *avctx,
copy_frame_to_buffer(avctx, frame, input_buf, input_size);
pts = av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q);
+ ff_dts_gen_record_pts(&s->dts_gen, frame->pts);
} else {
flags |= ff_AMediaCodec_getBufferFlagEndOfStream(codec);
s->eof_sent = 1;
@@ -985,6 +993,7 @@ bailout:
if (s->eof_sent) {
s->eof_sent = 0;
ff_AMediaCodec_flush(s->codec);
+ ff_dts_gen_reset(&s->dts_gen);
}
av_bsf_flush(s->bsf);
av_packet_free(&pkt);
@@ -1021,6 +1030,7 @@ static av_cold void mediacodec_flush(AVCodecContext *avctx)
if (s->bsf)
av_bsf_flush(s->bsf);
av_frame_unref(s->frame);
+ ff_dts_gen_reset(&s->dts_gen);
s->flush_error = ff_AMediaCodec_flush(s->codec);
if (s->flush_error < 0) {
@@ -1091,8 +1101,9 @@ static const FFCodecDefault mediacodec_defaults[] = {
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
{ "cbr_fd", "Constant bitrate mode with frame drops", \
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
- { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
- "since most of Android devices don't output B frames by default.", \
+ { "pts_as_dts", "Use PTS as DTS when reorder_delay is auto. It is enabled automatically if " \
+ "avctx max_b_frames <= 0, since most of Android devices don't output B frames " \
+ "by default.", \
OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
{ "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", \
OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE }, \
@@ -1108,6 +1119,7 @@ static const FFCodecDefault mediacodec_defaults[] = {
OFFSET(qp_p_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
{ "qp_b_max", "maximum quantization parameter for B frame", \
OFFSET(qp_b_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
+ FF_DTS_GEN_REORDER_DELAY_OPTION(OFFSET(reorder_delay), VE), \
#define MEDIACODEC_ENCODER_CLASS(name) \