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;
434 if (out_info.
size <= 4) {
446 s->extradata_size = out_info.
size;
447 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
457 if (
s->extradata_size) {
458 extradata_size =
s->extradata_size;
459 s->extradata_size = 0;
460 memcpy(
pkt->
data,
s->extradata, extradata_size);
471 " flags %d extradata %d\n",
482 uint8_t *dst_data[4] = {};
483 int dst_linesize[4] = {};
487 dst_data[1] =
dst +
s->width *
s->height;
488 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
490 dst_linesize[0] =
s->width;
491 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
494 dst_data[1] =
dst +
s->width *
s->height;
496 dst_linesize[0] =
s->width;
497 dst_linesize[1] =
s->width;
511 uint8_t *input_buf =
NULL;
512 size_t input_size = 0;
585 if (!
s->frame->buf[0]) {
606 s->frame->width = avctx->
width;
607 s->frame->height = avctx->
height;
665 if (!
s->extract_extradata) {
667 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
668 "Use extract_extradata bsf when necessary.\n");
684 if (side && side_size > 0) {
691 memcpy(avctx->
extradata, side, side_size);
747 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
748 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
749 #define COMMON_OPTION \
750 { "ndk_codec", "Use MediaCodec from NDK", \
751 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
752 { "codec_name", "Select codec by name", \
753 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
754 { "bitrate_mode", "Bitrate control method", \
755 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, .unit = "bitrate_mode" }, \
756 { "cq", "Constant quality mode", \
757 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, .unit = "bitrate_mode" }, \
758 { "vbr", "Variable bitrate mode", \
759 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
760 { "cbr", "Constant bitrate mode", \
761 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
762 { "cbr_fd", "Constant bitrate mode with frame drops", \
763 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
764 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
765 "since most of Android devices don't output B frames by default.", \
766 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
769 #define MEDIACODEC_ENCODER_CLASS(name) \
770 static const AVClass name ## _mediacodec_class = { \
771 .class_name = #name "_mediacodec", \
772 .item_name = av_default_item_name, \
773 .option = name ## _options, \
774 .version = LIBAVUTIL_VERSION_INT, \
777 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
778 MEDIACODEC_ENCODER_CLASS(short_name) \
779 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
780 .p.name = #short_name "_mediacodec", \
781 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
782 .p.type = AVMEDIA_TYPE_VIDEO, \
784 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
785 AV_CODEC_CAP_HARDWARE | \
786 AV_CODEC_CAP_ENCODER_FLUSH, \
787 .priv_data_size = sizeof(MediaCodecEncContext), \
788 .p.pix_fmts = avc_pix_fmts, \
789 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, \
790 .init = mediacodec_init, \
791 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
792 .close = mediacodec_close, \
793 .flush = mediacodec_flush, \
794 .p.priv_class = &short_name ## _mediacodec_class, \
795 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
796 .p.wrapper_name = "mediacodec", \
797 .hw_configs = mediacodec_hw_configs, \
800 #if CONFIG_H264_MEDIACODEC_ENCODER
802 enum MediaCodecAvcLevel {
819 AVCLevel52 = 0x10000,
821 AVCLevel61 = 0x40000,
822 AVCLevel62 = 0x80000,
837 {
"level",
"Specify level",
864 #endif // CONFIG_H264_MEDIACODEC_ENCODER
866 #if CONFIG_HEVC_MEDIACODEC_ENCODER
868 enum MediaCodecHevcLevel {
869 HEVCMainTierLevel1 = 0x1,
870 HEVCHighTierLevel1 = 0x2,
871 HEVCMainTierLevel2 = 0x4,
872 HEVCHighTierLevel2 = 0x8,
873 HEVCMainTierLevel21 = 0x10,
874 HEVCHighTierLevel21 = 0x20,
875 HEVCMainTierLevel3 = 0x40,
876 HEVCHighTierLevel3 = 0x80,
877 HEVCMainTierLevel31 = 0x100,
878 HEVCHighTierLevel31 = 0x200,
879 HEVCMainTierLevel4 = 0x400,
880 HEVCHighTierLevel4 = 0x800,
881 HEVCMainTierLevel41 = 0x1000,
882 HEVCHighTierLevel41 = 0x2000,
883 HEVCMainTierLevel5 = 0x4000,
884 HEVCHighTierLevel5 = 0x8000,
885 HEVCMainTierLevel51 = 0x10000,
886 HEVCHighTierLevel51 = 0x20000,
887 HEVCMainTierLevel52 = 0x40000,
888 HEVCHighTierLevel52 = 0x80000,
889 HEVCMainTierLevel6 = 0x100000,
890 HEVCHighTierLevel6 = 0x200000,
891 HEVCMainTierLevel61 = 0x400000,
892 HEVCHighTierLevel61 = 0x800000,
893 HEVCMainTierLevel62 = 0x1000000,
894 HEVCHighTierLevel62 = 0x2000000,
903 {
"level",
"Specify tier and level",
905 {
"m1",
"Main tier level 1",
907 {
"h1",
"High tier level 1",
909 {
"m2",
"Main tier level 2",
911 {
"h2",
"High tier level 2",
913 {
"m2.1",
"Main tier level 2.1",
915 {
"h2.1",
"High tier level 2.1",
917 {
"m3",
"Main tier level 3",
919 {
"h3",
"High tier level 3",
921 {
"m3.1",
"Main tier level 3.1",
923 {
"h3.1",
"High tier level 3.1",
925 {
"m4",
"Main tier level 4",
927 {
"h4",
"High tier level 4",
929 {
"m4.1",
"Main tier level 4.1",
931 {
"h4.1",
"High tier level 4.1",
933 {
"m5",
"Main tier level 5",
935 {
"h5",
"High tier level 5",
937 {
"m5.1",
"Main tier level 5.1",
939 {
"h5.1",
"High tier level 5.1",
941 {
"m5.2",
"Main tier level 5.2",
943 {
"h5.2",
"High tier level 5.2",
945 {
"m6",
"Main tier level 6",
947 {
"h6",
"High tier level 6",
949 {
"m6.1",
"Main tier level 6.1",
951 {
"h6.1",
"High tier level 6.1",
953 {
"m6.2",
"Main tier level 6.2",
955 {
"h6.2",
"High tier level 6.2",
962 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
964 #if CONFIG_VP8_MEDIACODEC_ENCODER
966 enum MediaCodecVP8Level {
967 VP8Level_Version0 = 0x01,
968 VP8Level_Version1 = 0x02,
969 VP8Level_Version2 = 0x04,
970 VP8Level_Version3 = 0x08,
973 static const AVOption vp8_options[] = {
975 {
"level",
"Specify tier and level",
977 {
"V0",
"Level Version 0",
979 {
"V1",
"Level Version 1",
981 {
"V2",
"Level Version 2",
983 {
"V3",
"Level Version 3",
990 #endif // CONFIG_VP8_MEDIACODEC_ENCODER
992 #if CONFIG_VP9_MEDIACODEC_ENCODER
994 enum MediaCodecVP9Level {
1007 VP9Level61 = 0x1000,
1008 VP9Level62 = 0x2000,
1011 static const AVOption vp9_options[] = {
1019 {
"level",
"Specify tier and level",
1023 {
"1.1",
"Level 1.1",
1027 {
"2.1",
"Level 2.1",
1031 {
"3.1",
"Level 3.1",
1035 {
"4.1",
"Level 4.1",
1039 {
"5.1",
"Level 5.1",
1041 {
"5.2",
"Level 5.2",
1045 {
"6.1",
"Level 4.1",
1047 {
"6.2",
"Level 6.2",
1054 #endif // CONFIG_VP9_MEDIACODEC_ENCODER
1056 #if CONFIG_MPEG4_MEDIACODEC_ENCODER
1058 enum MediaCodecMpeg4Level {
1060 MPEG4Level0b = 0x02,
1064 MPEG4Level3b = 0x18,
1066 MPEG4Level4a = 0x40,
1068 MPEG4Level6 = 0x100,
1076 {
"level",
"Specify tier and level",
1103 #endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
1105 #if CONFIG_AV1_MEDIACODEC_ENCODER
1107 enum MediaCodecAV1Level {
1121 AV1Level51 = 0x2000,
1122 AV1Level52 = 0x4000,
1123 AV1Level53 = 0x8000,
1124 AV1Level6 = 0x10000,
1125 AV1Level61 = 0x20000,
1126 AV1Level62 = 0x40000,
1127 AV1Level63 = 0x80000,
1128 AV1Level7 = 0x100000,
1129 AV1Level71 = 0x200000,
1130 AV1Level72 = 0x400000,
1131 AV1Level73 = 0x800000,
1139 {
"level",
"Specify tier and level",
1143 {
"2.1",
"Level 2.1",
1145 {
"2.2",
"Level 2.2",
1147 {
"2.3",
"Level 2.3",
1151 {
"3.1",
"Level 3.1",
1153 {
"3.2",
"Level 3.2",
1155 {
"3.3",
"Level 3.3",
1159 {
"4.1",
"Level 4.1",
1161 {
"4.2",
"Level 4.2",
1163 {
"4.3",
"Level 4.3",
1167 {
"5.1",
"Level 5.1",
1169 {
"5.2",
"Level 5.2",
1171 {
"5.3",
"Level 5.3",
1175 {
"6.1",
"Level 6.1",
1177 {
"6.2",
"Level 6.2",
1179 {
"6.3",
"Level 6.3",
1183 {
"7.1",
"Level 7.1",
1185 {
"7.2",
"Level 7.2",
1187 {
"7.3",
"Level 7.3",
1194 #endif // CONFIG_AV1_MEDIACODEC_ENCODER