From b4eca5cce2a1a2872f6525ba50082536ee080445 Mon Sep 17 00:00:00 2001 From: Samuel Raposo Vieira Mira Date: Mon, 27 Mar 2023 15:21:27 +0000 Subject: [PATCH] avcodec/mediacodec: add mpeg4 encoder This patch will add MPEG4 encoder using Android Mediacodec Signed-off-by: Samuel Mira > Signed-off-by: Zhao Zhili --- configure | 2 ++ libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/mediacodec_wrapper.c | 55 +++++++++++++++++++++++++++++++++ libavcodec/mediacodecenc.c | 49 +++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 6 files changed, 109 insertions(+), 1 deletion(-) diff --git a/configure b/configure index f4afa00a6e..16b94103b5 100755 --- a/configure +++ b/configure @@ -3228,6 +3228,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 fd0a89aa7f..d23d4d4a96 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 ea6edd0195..a73d1c9fcc 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -873,6 +873,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..1c29bb7406 100644 --- a/libavcodec/mediacodec_wrapper.c +++ b/libavcodec/mediacodec_wrapper.c @@ -328,6 +328,23 @@ int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx) 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; (void)HEVCProfileMain10HDR10; @@ -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..0767cd0165 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 diff --git a/libavcodec/version.h b/libavcodec/version.h index 37c4c39451..230d5fa13e 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #include "version_major.h" -#define LIBAVCODEC_VERSION_MINOR 8 +#define LIBAVCODEC_VERSION_MINOR 9 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \