[FFmpeg-devel] [PATCH v2] avcodec/mediacodec: Add AV1 encoder

Samuel Raposo Vieira Mira samuel.mira at qt.io
Tue May 2 15:22:32 EEST 2023


Connected FFmpeg to Mediacodec AV1 encoder
Minor version bump.
Minor whitespaces fix.

Signed-off-by: Samuel Mira samuel.mira at qt.io<mailto:samuel.mira at qt.io>
---
configure                       |   1 +
libavcodec/Makefile             |   1 +
libavcodec/allcodecs.c          |   1 +
libavcodec/mediacodec_wrapper.c |  12 ++++
libavcodec/mediacodecenc.c      | 115 ++++++++++++++++++++++++++++----
libavcodec/version.h            |   2 +-
6 files changed, 119 insertions(+), 13 deletions(-)

diff --git a/configure b/configure
index bb7be67676..0a60deac65 100755
--- a/configure
+++ b/configure
@@ -3162,6 +3162,7 @@ aac_mf_encoder_deps="mediafoundation"
ac3_mf_encoder_deps="mediafoundation"
av1_cuvid_decoder_deps="cuvid CUVIDAV1PICPARAMS"
av1_mediacodec_decoder_deps="mediacodec"
+av1_mediacodec_encoder_deps="mediacodec"
av1_nvenc_encoder_deps="nvenc NV_ENC_PIC_PARAMS_AV1"
av1_nvenc_encoder_select="atsc_a53"
h263_v4l2m2m_decoder_deps="v4l2_m2m h263_v4l2_m2m"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b0971ce833..166f77f12a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -253,6 +253,7 @@ OBJS-$(CONFIG_AURA2_DECODER)           += aura.o
OBJS-$(CONFIG_AV1_DECODER)             += av1dec.o
OBJS-$(CONFIG_AV1_CUVID_DECODER)       += cuviddec.o
OBJS-$(CONFIG_AV1_MEDIACODEC_DECODER)  += mediacodecdec.o
+OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o
OBJS-$(CONFIG_AV1_NVENC_ENCODER)       += nvenc_av1.o nvenc.o
OBJS-$(CONFIG_AV1_QSV_ENCODER)         += qsvenc_av1.o
OBJS-$(CONFIG_AVRN_DECODER)            += avrndec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 8eeed34e57..f583aad860 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -836,6 +836,7 @@ extern const FFCodec ff_libaom_av1_decoder;
extern const FFCodec ff_av1_decoder;
extern const FFCodec ff_av1_cuvid_decoder;
extern const FFCodec ff_av1_mediacodec_decoder;
+extern const FFCodec ff_av1_mediacodec_encoder;
extern const FFCodec ff_av1_nvenc_encoder;
extern const FFCodec ff_av1_qsv_decoder;
extern const FFCodec ff_av1_qsv_encoder;
diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index 1c29bb7406..015f275a0f 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -35,6 +35,8 @@
#include "ffjni.h"
#include "mediacodec_wrapper.h"
+#include "libavutil/pixdesc.h"
+
struct JNIAMediaCodecListFields {
     jclass mediacodec_list_class;
@@ -345,6 +347,11 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
     static const int MPEG4ProfileAdvancedScalable = 0x4000;
     static const int MPEG4ProfileAdvancedSimple   = 0x8000;
+    static const int AV1ProfileMain8  = 0x1;
+    static const int AV1ProfileMain10 = 0x2;
+    static const int AV1ProfileMain10HDR10     = 0x1000;
+    static const int AV1ProfileMain10HDR10Plus = 0x2000;
+
     // Unused yet.
     (void)AVCProfileConstrainedHigh;
     (void)HEVCProfileMain10HDR10;
@@ -353,6 +360,8 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
     (void)VP9Profile3HDR;
     (void)VP9Profile2HDR10Plus;
     (void)VP9Profile3HDR10Plus;
+    (void)AV1ProfileMain10HDR10;
+    (void)AV1ProfileMain10HDR10Plus;
     if (avctx->codec_id == AV_CODEC_ID_H264) {
         switch(avctx->profile) {
@@ -436,6 +445,9 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
         default:
             break;
         }
+    } else if(avctx->codec_id == AV_CODEC_ID_AV1) {
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+        return desc != NULL && desc->comp[0].depth == 8? AV1ProfileMain8 : AV1ProfileMain10;
     }
     return -1;
diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index e4b583a542..10da43c3e7 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -170,6 +170,9 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
     case AV_CODEC_ID_MPEG4:
         codec_mime = "video/mp4v-es";
         break;
+    case AV_CODEC_ID_AV1:
+        codec_mime = "video/av01";
+        break;
     default:
         av_assert0(0);
     }
@@ -779,16 +782,16 @@ DECLARE_MEDIACODEC_ENCODER(hevc, "H.265", AV_CODEC_ID_HEVC)
 enum MediaCodecVP9Level {
     VP9Level1  = 0x1,
-    VP9Level11  = 0x2,
+    VP9Level11 = 0x2,
     VP9Level2  = 0x4,
-    VP9Level21  = 0x8,
-    VP9Level3 = 0x10,
+    VP9Level21 = 0x8,
+    VP9Level3  = 0x10,
     VP9Level31 = 0x20,
     VP9Level4  = 0x40,
-    VP9Level41  = 0x80,
-    VP9Level5 = 0x100,
+    VP9Level41 = 0x80,
+    VP9Level5  = 0x100,
     VP9Level51 = 0x200,
-    VP9Level52  = 0x400,
+    VP9Level52 = 0x400,
     VP9Level6  = 0x800,
     VP9Level61 = 0x1000,
     VP9Level62 = 0x2000,
@@ -837,15 +840,15 @@ DECLARE_MEDIACODEC_ENCODER(vp9, "VP9", AV_CODEC_ID_VP9)
 enum MediaCodecMpeg4Level {
     MPEG4Level0  = 0x01,
-    MPEG4Level0b  = 0x02,
-    MPEG4Level1 = 0x04,
+    MPEG4Level0b = 0x02,
+    MPEG4Level1  = 0x04,
     MPEG4Level2  = 0x08,
-    MPEG4Level3 = 0x10,
+    MPEG4Level3  = 0x10,
     MPEG4Level3b = 0x18,
-    MPEG4Level4 = 0x20,
-    MPEG4Level4a  = 0x40,
+    MPEG4Level4  = 0x20,
+    MPEG4Level4a = 0x40,
     MPEG4Level5  = 0x80,
-    MPEG4Level6 = 0x100,
+    MPEG4Level6  = 0x100,
};
 static const AVOption mpeg4_options[] = {
@@ -878,3 +881,91 @@ static const AVOption mpeg4_options[] = {
DECLARE_MEDIACODEC_ENCODER(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4)
 #endif  // CONFIG_MPEG4_MEDIACODEC_ENCODER
+
+#if CONFIG_AV1_MEDIACODEC_ENCODER
+
+enum MediaCodecAV1Level {
+    AV1Level2  = 0x1,
+    AV1Level21 = 0x2,
+    AV1Level22 = 0x4,
+    AV1Level23 = 0x8,
+    AV1Level3  = 0x10,
+    AV1Level31 = 0x20,
+    AV1Level32 = 0x40,
+    AV1Level33 = 0x80,
+    AV1Level4  = 0x100,
+    AV1Level41 = 0x200,
+    AV1Level42 = 0x400,
+    AV1Level43 = 0x800,
+    AV1Level5  = 0x1000,
+    AV1Level51 = 0x2000,
+    AV1Level52 = 0x4000,
+    AV1Level53 = 0x8000,
+    AV1Level6  = 0x10000,
+    AV1Level61 = 0x20000,
+    AV1Level62 = 0x40000,
+    AV1Level63 = 0x80000,
+    AV1Level7  = 0x100000,
+    AV1Level71 = 0x200000,
+    AV1Level72 = 0x400000,
+    AV1Level73 = 0x800000,
+};
+
+static const AVOption av1_options[] = {
+    COMMON_OPTION
+    { "level", "Specify tier and level",
+                OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
+    { "2",     "Level 2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level2  },  0, 0, VE,  "level" },
+    { "2.1",    "Level 2.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level21 },  0, 0, VE,  "level" },
+    { "2.2",    "Level 2.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level22 },  0, 0, VE,  "level" },
+    { "2.3",    "Level 2.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level23 },  0, 0, VE,  "level" },
+    { "3",      "Level 3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level3  },  0, 0, VE,  "level" },
+    { "3.1",    "Level 3.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level31 },  0, 0, VE,  "level" },
+    { "3.2",    "Level 3.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level32 },  0, 0, VE,  "level" },
+    { "3.3",    "Level 3.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level33 },  0, 0, VE,  "level" },
+    { "4",      "Level 4",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level4  },  0, 0, VE,  "level" },
+    { "4.1",    "Level 4.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level41 },  0, 0, VE,  "level" },
+    { "4.2",    "Level 4.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level42 },  0, 0, VE,  "level" },
+    { "4.3",    "Level 4.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level43 },  0, 0, VE,  "level" },
+    { "5",      "Level 5",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level5  },  0, 0, VE,  "level" },
+    { "5.1",    "Level 5.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level51 },  0, 0, VE,  "level" },
+    { "5.2",    "Level 5.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level52 },  0, 0, VE,  "level" },
+    { "5.3",    "Level 5.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level53 },  0, 0, VE,  "level" },
+    { "6",      "Level 6",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level6  },  0, 0, VE,  "level" },
+    { "6.1",    "Level 6.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level61 },  0, 0, VE,  "level" },
+    { "6.2",    "Level 6.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level62 },  0, 0, VE,  "level" },
+    { "6.3",    "Level 6.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level63 },  0, 0, VE,  "level" },
+    { "7",      "Level 7",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level7  },  0, 0, VE,  "level" },
+    { "7.1",    "Level 7.1",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level71 },  0, 0, VE,  "level" },
+    { "7.2",    "Level 7.2",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level72 },  0, 0, VE,  "level" },
+    { "7.3",    "Level 7.3",
+                0, AV_OPT_TYPE_CONST, { .i64 = AV1Level73 },  0, 0, VE,  "level" },
+    { NULL, }
+};
+
+DECLARE_MEDIACODEC_ENCODER(av1, "AV1", AV_CODEC_ID_AV1)
+
+#endif  // CONFIG_AV1_MEDIACODEC_ENCODER
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 80e2ae630d..8b53586be1 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 #include "version_major.h"
-#define LIBAVCODEC_VERSION_MINOR  10
+#define LIBAVCODEC_VERSION_MINOR  12
#define LIBAVCODEC_VERSION_MICRO 100
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
--
2.35.2




More information about the ffmpeg-devel mailing list