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

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


From: Zhao Zhili <zhilizhao at tencent.com>

---
 libavcodec/mediacodecenc.c | 25 +++++++++++++++++++++++++
 libavcodec/version.h       |  2 +-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 4f9185342f..ec0e0b3a86 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -39,6 +39,17 @@
 #define INPUT_DEQUEUE_TIMEOUT_US 8000
 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
 
+enum BitrateMode {
+    /* Constant quality mode */
+    BITRATE_MODE_CQ = 0,
+    /* Variable bitrate mode */
+    BITRATE_MODE_VBR = 1,
+    /* Constant bitrate mode */
+    BITRATE_MODE_CBR = 2,
+    /* Constant bitrate mode with frame drops */
+    BITRATE_MODE_CBR_FD = 3,
+};
+
 typedef struct MediaCodecEncContext {
     AVClass *avclass;
     FFAMediaCodec *codec;
@@ -67,6 +78,8 @@ typedef struct MediaCodecEncContext {
     int eof_sent;
 
     AVFrame *frame;
+
+    int bitrate_mode;
 } MediaCodecEncContext;
 
 enum {
@@ -193,6 +206,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 
     if (avctx->bit_rate)
         ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate);
+    if (s->bitrate_mode >= 0)
+        ff_AMediaFormat_setInt32(format, "bitrate-mode", s->bitrate_mode);
     // frame-rate and i-frame-interval are required to configure codec
     if (avctx->framerate.num >= avctx->framerate.den && avctx->framerate.den > 0) {
         s->fps = avctx->framerate.num / avctx->framerate.den;
@@ -485,6 +500,16 @@ static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = {
                     OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE },                      \
     { "codec_name", "Select codec by name",                                                                 \
                     OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE },                                      \
+    { "bitrate_mode", "Bitrate control method",                                                             \
+                    OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" },  \
+    { "cq", "Constant quality mode",                                                                        \
+                    0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" },             \
+    { "vbr", "Variable bitrate mode",                                                                       \
+                    0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" },            \
+    { "cbr", "Constant bitrate mode",                                                                       \
+                    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" },         \
 
 
 #define MEDIACODEC_ENCODER_CLASS(name)              \
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 9e66920593..527b4dbd0b 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 101
+#define LIBAVCODEC_VERSION_MICRO 102
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1



More information about the ffmpeg-devel mailing list