diff mbox series

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

Message ID tencent_9C68AE009969DE7CF2AE86393DC0F9B5E40A@qq.com
State New
Headers show
Series [FFmpeg-devel,1/7] avcodec/mediacodecenc: add bitrate_mode option | expand

Checks

Context Check Description
andriy/configure_x86 warning Failed to apply patch
yinshiyou/configure_loongarch64 warning Failed to apply patch

Commit Message

Zhao Zhili Dec. 4, 2022, 5:12 p.m. UTC
From: Zhao Zhili <zhilizhao@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 mbox series

Patch

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 9378563fac..3893ca8dee 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)              \
 static const AVClass name ## _mediacodec_class = {  \
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, \