[FFmpeg-devel] [PATCH v2 7/7] avcodec/mediacodecenc: add pts_as_dts option

Zhao Zhili quinkblack at foxmail.com
Wed Dec 7 11:31:22 EET 2022


From: Zhao Zhili <zhilizhao at tencent.com>

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.
---
 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 e9cff8167a..41b7afe23d 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 {
@@ -266,6 +267,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);
@@ -358,6 +361,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;
@@ -548,6 +553,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 61bdf5806b..dd90cd1335 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  54
-#define LIBAVCODEC_VERSION_MICRO 103
+#define LIBAVCODEC_VERSION_MICRO 104
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1



More information about the ffmpeg-devel mailing list