diff mbox series

[FFmpeg-devel,2/2] avcodec/mediacodec: add mpeg4 encoder

Message ID DBAPR02MB6165762C3E66BDC164E593A3ED8B9@DBAPR02MB6165.eurprd02.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,1/2] avcodec/mediacodec add vp9 encoder using mediacodec | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Samuel Raposo Vieira Mira March 27, 2023, 3:21 p.m. UTC
This patch will add MPEG4 encoder using Android Mediacodec

Signed-off-by: Samuel Mira <samuel.mira@qt.io<mailto:samuel.mira@qt.io>>
---
 configure                       |  2 ++
 libavcodec/Makefile             |  1 +
 libavcodec/allcodecs.c          |  1 +
 libavcodec/mediacodec_wrapper.c | 55 +++++++++++++++++++++++++++++++++
 libavcodec/mediacodecenc.c      | 49 +++++++++++++++++++++++++++++
 5 files changed, 108 insertions(+)

--
2.35.2


Samuel Mira
Senior Software Developer
The Qt Company
Tutkijantie 4C
FI-90590 Oulu
Finland
samuel.mira@qt.io<mailto:samuel.mira@qt.io>
www.qt.io<https://www.qt.io>
[signature_4074551992]<https://www.qt.io/>
[signature_183607293]<https://www.facebook.com/qt/>
[signature_465813548]<https://twitter.com/qtproject>
[signature_1516411291]<https://www.linkedin.com/company/the-qt-company/>
[signature_1076561480]<https://www.youtube.com/QtStudios>
diff mbox series

Patch

diff --git a/configure b/configure
index 101bc7b2f1..be0c201414 100755
--- a/configure
+++ b/configure
@@ -3226,6 +3226,8 @@  mpeg2_v4l2m2m_decoder_deps="v4l2_m2m mpeg2_v4l2_m2m"
 mpeg4_crystalhd_decoder_select="crystalhd"
 mpeg4_cuvid_decoder_deps="cuvid"
 mpeg4_mediacodec_decoder_deps="mediacodec"
+mpeg4_mediacodec_encoder_deps="mediacodec"
+mpeg4_mediacodec_encoder_extralibs="-landroid"
 mpeg4_mmal_decoder_deps="mmal"
 mpeg4_omx_encoder_deps="omx"
 mpeg4_v4l2m2m_decoder_deps="v4l2_m2m mpeg4_v4l2_m2m"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3d213014c6..77b7c988c5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -542,6 +542,7 @@  OBJS-$(CONFIG_MPEG4_DECODER)           += mpeg4videodsp.o xvididct.o
 OBJS-$(CONFIG_MPEG4_ENCODER)           += mpeg4videoenc.o
 OBJS-$(CONFIG_MPEG4_CUVID_DECODER)     += cuviddec.o
 OBJS-$(CONFIG_MPEG4_MEDIACODEC_DECODER) += mediacodecdec.o
+OBJS-$(CONFIG_MPEG4_MEDIACODEC_ENCODER) += mediacodecenc.o
 OBJS-$(CONFIG_MPEG4_OMX_ENCODER)       += omx.o
 OBJS-$(CONFIG_MPEG4_V4L2M2M_DECODER)   += v4l2_m2m_dec.o
 OBJS-$(CONFIG_MPEG4_V4L2M2M_ENCODER)   += v4l2_m2m_enc.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 6333844868..24fd935211 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -871,6 +871,7 @@  extern const FFCodec ff_mpeg2_qsv_encoder;
 extern const FFCodec ff_mpeg2_vaapi_encoder;
 extern const FFCodec ff_mpeg4_cuvid_decoder;
 extern const FFCodec ff_mpeg4_mediacodec_decoder;
+extern const FFCodec ff_mpeg4_mediacodec_encoder;
 extern const FFCodec ff_mpeg4_omx_encoder;
 extern const FFCodec ff_mpeg4_v4l2m2m_encoder;
 extern const FFCodec ff_prores_videotoolbox_encoder;
diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index b13211d435..46445bc7d6 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -327,6 +327,23 @@  int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
     static const int VP9Profile3HDR = 0x2000;
     static const int VP9Profile2HDR10Plus = 0x4000;
     static const int VP9Profile3HDR10Plus = 0x8000;
+
+    static const int MPEG4ProfileSimple           = 0x01;
+    static const int MPEG4ProfileSimpleScalable   = 0x02;
+    static const int MPEG4ProfileCore             = 0x04;
+    static const int MPEG4ProfileMain             = 0x08;
+    static const int MPEG4ProfileNbit             = 0x10;
+    static const int MPEG4ProfileScalableTexture  = 0x20;
+    static const int MPEG4ProfileSimpleFBA        = 0x80;
+    static const int MPEG4ProfileSimpleFace       = 0x40;
+    static const int MPEG4ProfileBasicAnimated    = 0x100;
+    static const int MPEG4ProfileHybrid           = 0x200;
+    static const int MPEG4ProfileAdvancedRealTime = 0x400;
+    static const int MPEG4ProfileCoreScalable     = 0x800;
+    static const int MPEG4ProfileAdvancedCoding   = 0x1000;
+    static const int MPEG4ProfileAdvancedCore     = 0x2000;
+    static const int MPEG4ProfileAdvancedScalable = 0x4000;
+    static const int MPEG4ProfileAdvancedSimple   = 0x8000;

     // Unused yet.
     (void)AVCProfileConstrainedHigh;
@@ -381,6 +398,44 @@  int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
          case FF_PROFILE_VP9_3:
             return VP9Profile3;
         }
+    } else if(avctx->codec_id == AV_CODEC_ID_MPEG4) {
+        switch (avctx->profile)
+        {
+        case FF_PROFILE_MPEG4_SIMPLE:
+            return MPEG4ProfileSimple;
+        case FF_PROFILE_MPEG4_SIMPLE_SCALABLE:
+            return MPEG4ProfileSimpleScalable;
+        case FF_PROFILE_MPEG4_CORE:
+            return MPEG4ProfileCore;
+        case FF_PROFILE_MPEG4_MAIN:
+            return MPEG4ProfileMain;
+        case FF_PROFILE_MPEG4_N_BIT:
+            return MPEG4ProfileNbit;
+        case FF_PROFILE_MPEG4_SCALABLE_TEXTURE:
+            return MPEG4ProfileScalableTexture;
+        case FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION:
+            return MPEG4ProfileSimpleFBA;
+        case FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE:
+            return MPEG4ProfileBasicAnimated;
+        case FF_PROFILE_MPEG4_HYBRID:
+            return MPEG4ProfileHybrid;
+        case FF_PROFILE_MPEG4_ADVANCED_REAL_TIME:
+            return MPEG4ProfileAdvancedRealTime;
+        case FF_PROFILE_MPEG4_CORE_SCALABLE:
+            return MPEG4ProfileCoreScalable;
+        case FF_PROFILE_MPEG4_ADVANCED_CODING:
+            return MPEG4ProfileAdvancedCoding;
+        case FF_PROFILE_MPEG4_ADVANCED_CORE:
+            return MPEG4ProfileAdvancedCore;
+        case FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE:
+            return MPEG4ProfileAdvancedScalable;
+        case FF_PROFILE_MPEG4_ADVANCED_SIMPLE:
+            return MPEG4ProfileAdvancedSimple;
+        case FF_PROFILE_MPEG4_SIMPLE_STUDIO:
+            // Studio profiles are not supported by mediacodec.
+        default:
+            break;
+        }
     }

     return -1;
diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index c7e2beb1ae..6baa7eda14 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -167,6 +167,9 @@  static av_cold int mediacodec_init(AVCodecContext *avctx)
     case AV_CODEC_ID_VP9:
         codec_mime = "video/x-vnd.on2.vp9";
         break;
+    case AV_CODEC_ID_MPEG4:
+        codec_mime = "video/mp4v-es";
+        break;
     default:
         av_assert0(0);
     }
@@ -825,3 +828,49 @@  static const AVOption vp9_options[] = {
 DECLARE_MEDIACODEC_ENCODER(vp9, "VP9", AV_CODEC_ID_VP9)

 #endif  // CONFIG_VP9_MEDIACODEC_ENCODER
+
+#if CONFIG_MPEG4_MEDIACODEC_ENCODER
+
+enum MediaCodecMpeg4Level {
+    MPEG4Level0  = 0x01,
+    MPEG4Level0b  = 0x02,
+    MPEG4Level1 = 0x04,
+    MPEG4Level2  = 0x08,
+    MPEG4Level3 = 0x10,
+    MPEG4Level3b = 0x18,
+    MPEG4Level4 = 0x20,
+    MPEG4Level4a  = 0x40,
+    MPEG4Level5  = 0x80,
+    MPEG4Level6 = 0x100,
+};
+
+static const AVOption mpeg4_options[] = {
+    COMMON_OPTION
+    { "level", "Specify tier and level",
+                OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
+    { "0",     "Level 0",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0  },  0, 0, VE,  "level" },
+    { "0b",    "Level 0b",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0b },  0, 0, VE,  "level" },
+    { "1",     "Level 1",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level1  },  0, 0, VE,  "level" },
+    { "2",     "Level 2",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level2 },  0, 0, VE,  "level" },
+    { "3",     "Level 3",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3  },  0, 0, VE,  "level" },
+    { "3b",    "Level 3b",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3b },  0, 0, VE,  "level" },
+    { "4",     "Level 4",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4  },  0, 0, VE,  "level" },
+    { "4a",    "Level 4a",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4a },  0, 0, VE,  "level" },
+    { "5",     "Level 5",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level5  },  0, 0, VE,  "level" },
+    { "6",     "Level 6",
+                0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level6  },  0, 0, VE,  "level" },
+    { NULL, }
+};
+
+DECLARE_MEDIACODEC_ENCODER(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4)
+
+#endif  // CONFIG_MPEG4_MEDIACODEC_ENCODER