From 10a56363a749610a44a30fd5200575f5bbc18562 Mon Sep 17 00:00:00 2001 From: Zhao Zhili Date: Wed, 7 Dec 2022 17:31:22 +0800 Subject: [PATCH] avcodec/mediacodecenc: add pts_as_dts option It works since most of Android devices don't output B frames by default. The behavior is documented by Android now, although there is some exception in history, which should have been fixed now. Signed-off-by: Zhao Zhili --- libavcodec/mediacodecenc.c | 8 ++++++++ libavcodec/version.h | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index 50994ce864..4c1809093c 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -71,6 +71,7 @@ typedef struct MediaCodecEncContext { int bitrate_mode; int level; + int pts_as_dts; } MediaCodecEncContext; enum { @@ -277,6 +278,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) } 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; ret = ff_AMediaCodec_getConfigureFlagEncode(s->codec); ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret); @@ -369,6 +372,8 @@ static int mediacodec_receive(AVCodecContext *avctx, } 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; if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec)) pkt->flags |= AV_PKT_FLAG_KEY; ret = 0; @@ -559,6 +564,9 @@ static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = { 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \ { "cbr_fd", "Constant bitrate mode with frame drops", \ 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "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.", \ + OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \ #define MEDIACODEC_ENCODER_CLASS(name) \ diff --git a/libavcodec/version.h b/libavcodec/version.h index e60339729f..eb95a0f827 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 55 -#define LIBAVCODEC_VERSION_MICRO 102 +#define LIBAVCODEC_VERSION_MICRO 103 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \