23 #include "config_components.h"
43 #define INPUT_DEQUEUE_TIMEOUT_US 8000
44 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
139 int crop_right =
s->width - avctx->
width;
140 int crop_bottom =
s->height - avctx->
height;
148 if (!crop_right && !crop_bottom && !
s->extract_extradata)
152 if (crop_right || crop_bottom) {
154 ret =
snprintf(str,
sizeof(str),
"h264_metadata=crop_right=%d:crop_bottom=%d",
155 crop_right, crop_bottom);
161 ret =
snprintf(str,
sizeof(str),
"hevc_metadata=width=%d:height=%d",
163 if (
ret >=
sizeof(str))
167 if (
s->extract_extradata) {
169 if (
ret >=
sizeof(str))
190 const char *codec_mime =
NULL;
196 if (
s->use_ndk_codec < 0)
201 codec_mime =
"video/avc";
204 codec_mime =
"video/hevc";
207 codec_mime =
"video/x-vnd.on2.vp8";
210 codec_mime =
"video/x-vnd.on2.vp9";
213 codec_mime =
"video/mp4v-es";
216 codec_mime =
"video/av01";
249 if (
s->width % 16 ||
s->height % 16)
251 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
252 s->width,
s->height);
267 dev_ctx = device_ctx->
hwctx;
271 if (!
s->window && user_ctx && user_ctx->
surface)
276 av_log(avctx,
AV_LOG_ERROR,
"Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
283 if (!
s->use_ndk_codec && !
s->window->surface) {
286 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
311 if (
s->bitrate_mode >= 0) {
327 "Use %d as the default MediaFormat i-frame-interval, "
328 "please set gop_size properly (>= fps)\n", gop);
348 "Enabling B frames will produce packets with no DTS. "
349 "Use -strict experimental to use it anyway.\n");
355 if (
s->pts_as_dts == -1)
364 "support yuv420p as encoder input format.\n");
402 int extradata_size = 0;
436 s->extradata_size = out_info.
size;
437 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
447 if (
s->extradata_size) {
448 extradata_size =
s->extradata_size;
449 s->extradata_size = 0;
450 memcpy(
pkt->
data,
s->extradata, extradata_size);
461 " flags %d extradata %d\n",
472 uint8_t *dst_data[4] = {};
473 int dst_linesize[4] = {};
477 dst_data[1] =
dst +
s->width *
s->height;
478 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
480 dst_linesize[0] =
s->width;
481 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
484 dst_data[1] =
dst +
s->width *
s->height;
486 dst_linesize[0] =
s->width;
487 dst_linesize[1] =
s->width;
501 uint8_t *input_buf =
NULL;
502 size_t input_size = 0;
575 if (!
s->frame->buf[0]) {
596 s->frame->width = avctx->
width;
597 s->frame->height = avctx->
height;
655 if (!
s->extract_extradata) {
657 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
658 "Use extract_extradata bsf when necessary.\n");
674 if (side && side_size > 0) {
681 memcpy(avctx->
extradata, side, side_size);
737 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
738 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
739 #define COMMON_OPTION \
740 { "ndk_codec", "Use MediaCodec from NDK", \
741 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
742 { "codec_name", "Select codec by name", \
743 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
744 { "bitrate_mode", "Bitrate control method", \
745 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, .unit = "bitrate_mode" }, \
746 { "cq", "Constant quality mode", \
747 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, .unit = "bitrate_mode" }, \
748 { "vbr", "Variable bitrate mode", \
749 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
750 { "cbr", "Constant bitrate mode", \
751 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
752 { "cbr_fd", "Constant bitrate mode with frame drops", \
753 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
754 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
755 "since most of Android devices don't output B frames by default.", \
756 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
759 #define MEDIACODEC_ENCODER_CLASS(name) \
760 static const AVClass name ## _mediacodec_class = { \
761 .class_name = #name "_mediacodec", \
762 .item_name = av_default_item_name, \
763 .option = name ## _options, \
764 .version = LIBAVUTIL_VERSION_INT, \
767 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
768 MEDIACODEC_ENCODER_CLASS(short_name) \
769 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
770 .p.name = #short_name "_mediacodec", \
771 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
772 .p.type = AVMEDIA_TYPE_VIDEO, \
774 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
775 AV_CODEC_CAP_HARDWARE | \
776 AV_CODEC_CAP_ENCODER_FLUSH, \
777 .priv_data_size = sizeof(MediaCodecEncContext), \
778 .p.pix_fmts = avc_pix_fmts, \
779 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, \
780 .init = mediacodec_init, \
781 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
782 .close = mediacodec_close, \
783 .flush = mediacodec_flush, \
784 .p.priv_class = &short_name ## _mediacodec_class, \
785 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
786 .p.wrapper_name = "mediacodec", \
787 .hw_configs = mediacodec_hw_configs, \
790 #if CONFIG_H264_MEDIACODEC_ENCODER
792 enum MediaCodecAvcLevel {
809 AVCLevel52 = 0x10000,
811 AVCLevel61 = 0x40000,
812 AVCLevel62 = 0x80000,
827 {
"level",
"Specify level",
854 #endif // CONFIG_H264_MEDIACODEC_ENCODER
856 #if CONFIG_HEVC_MEDIACODEC_ENCODER
858 enum MediaCodecHevcLevel {
859 HEVCMainTierLevel1 = 0x1,
860 HEVCHighTierLevel1 = 0x2,
861 HEVCMainTierLevel2 = 0x4,
862 HEVCHighTierLevel2 = 0x8,
863 HEVCMainTierLevel21 = 0x10,
864 HEVCHighTierLevel21 = 0x20,
865 HEVCMainTierLevel3 = 0x40,
866 HEVCHighTierLevel3 = 0x80,
867 HEVCMainTierLevel31 = 0x100,
868 HEVCHighTierLevel31 = 0x200,
869 HEVCMainTierLevel4 = 0x400,
870 HEVCHighTierLevel4 = 0x800,
871 HEVCMainTierLevel41 = 0x1000,
872 HEVCHighTierLevel41 = 0x2000,
873 HEVCMainTierLevel5 = 0x4000,
874 HEVCHighTierLevel5 = 0x8000,
875 HEVCMainTierLevel51 = 0x10000,
876 HEVCHighTierLevel51 = 0x20000,
877 HEVCMainTierLevel52 = 0x40000,
878 HEVCHighTierLevel52 = 0x80000,
879 HEVCMainTierLevel6 = 0x100000,
880 HEVCHighTierLevel6 = 0x200000,
881 HEVCMainTierLevel61 = 0x400000,
882 HEVCHighTierLevel61 = 0x800000,
883 HEVCMainTierLevel62 = 0x1000000,
884 HEVCHighTierLevel62 = 0x2000000,
893 {
"level",
"Specify tier and level",
895 {
"m1",
"Main tier level 1",
897 {
"h1",
"High tier level 1",
899 {
"m2",
"Main tier level 2",
901 {
"h2",
"High tier level 2",
903 {
"m2.1",
"Main tier level 2.1",
905 {
"h2.1",
"High tier level 2.1",
907 {
"m3",
"Main tier level 3",
909 {
"h3",
"High tier level 3",
911 {
"m3.1",
"Main tier level 3.1",
913 {
"h3.1",
"High tier level 3.1",
915 {
"m4",
"Main tier level 4",
917 {
"h4",
"High tier level 4",
919 {
"m4.1",
"Main tier level 4.1",
921 {
"h4.1",
"High tier level 4.1",
923 {
"m5",
"Main tier level 5",
925 {
"h5",
"High tier level 5",
927 {
"m5.1",
"Main tier level 5.1",
929 {
"h5.1",
"High tier level 5.1",
931 {
"m5.2",
"Main tier level 5.2",
933 {
"h5.2",
"High tier level 5.2",
935 {
"m6",
"Main tier level 6",
937 {
"h6",
"High tier level 6",
939 {
"m6.1",
"Main tier level 6.1",
941 {
"h6.1",
"High tier level 6.1",
943 {
"m6.2",
"Main tier level 6.2",
945 {
"h6.2",
"High tier level 6.2",
952 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
954 #if CONFIG_VP8_MEDIACODEC_ENCODER
956 enum MediaCodecVP8Level {
957 VP8Level_Version0 = 0x01,
958 VP8Level_Version1 = 0x02,
959 VP8Level_Version2 = 0x04,
960 VP8Level_Version3 = 0x08,
963 static const AVOption vp8_options[] = {
965 {
"level",
"Specify tier and level",
967 {
"V0",
"Level Version 0",
969 {
"V1",
"Level Version 1",
971 {
"V2",
"Level Version 2",
973 {
"V3",
"Level Version 3",
980 #endif // CONFIG_VP8_MEDIACODEC_ENCODER
982 #if CONFIG_VP9_MEDIACODEC_ENCODER
984 enum MediaCodecVP9Level {
1001 static const AVOption vp9_options[] = {
1009 {
"level",
"Specify tier and level",
1013 {
"1.1",
"Level 1.1",
1017 {
"2.1",
"Level 2.1",
1021 {
"3.1",
"Level 3.1",
1025 {
"4.1",
"Level 4.1",
1029 {
"5.1",
"Level 5.1",
1031 {
"5.2",
"Level 5.2",
1035 {
"6.1",
"Level 4.1",
1037 {
"6.2",
"Level 6.2",
1044 #endif // CONFIG_VP9_MEDIACODEC_ENCODER
1046 #if CONFIG_MPEG4_MEDIACODEC_ENCODER
1048 enum MediaCodecMpeg4Level {
1050 MPEG4Level0b = 0x02,
1054 MPEG4Level3b = 0x18,
1056 MPEG4Level4a = 0x40,
1058 MPEG4Level6 = 0x100,
1066 {
"level",
"Specify tier and level",
1093 #endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
1095 #if CONFIG_AV1_MEDIACODEC_ENCODER
1097 enum MediaCodecAV1Level {
1111 AV1Level51 = 0x2000,
1112 AV1Level52 = 0x4000,
1113 AV1Level53 = 0x8000,
1114 AV1Level6 = 0x10000,
1115 AV1Level61 = 0x20000,
1116 AV1Level62 = 0x40000,
1117 AV1Level63 = 0x80000,
1118 AV1Level7 = 0x100000,
1119 AV1Level71 = 0x200000,
1120 AV1Level72 = 0x400000,
1121 AV1Level73 = 0x800000,
1129 {
"level",
"Specify tier and level",
1133 {
"2.1",
"Level 2.1",
1135 {
"2.2",
"Level 2.2",
1137 {
"2.3",
"Level 2.3",
1141 {
"3.1",
"Level 3.1",
1143 {
"3.2",
"Level 3.2",
1145 {
"3.3",
"Level 3.3",
1149 {
"4.1",
"Level 4.1",
1151 {
"4.2",
"Level 4.2",
1153 {
"4.3",
"Level 4.3",
1157 {
"5.1",
"Level 5.1",
1159 {
"5.2",
"Level 5.2",
1161 {
"5.3",
"Level 5.3",
1165 {
"6.1",
"Level 6.1",
1167 {
"6.2",
"Level 6.2",
1169 {
"6.3",
"Level 6.3",
1173 {
"7.1",
"Level 7.1",
1175 {
"7.2",
"Level 7.2",
1177 {
"7.3",
"Level 7.3",
1184 #endif // CONFIG_AV1_MEDIACODEC_ENCODER