23 #include "config_components.h"
41 #define INPUT_DEQUEUE_TIMEOUT_US 8000
42 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
119 int crop_right =
s->width - avctx->
width;
120 int crop_bottom =
s->height - avctx->
height;
122 if (!crop_right && !crop_bottom)
126 ret =
snprintf(str,
sizeof(str),
"h264_metadata=crop_right=%d:crop_bottom=%d",
127 crop_right, crop_bottom);
129 ret =
snprintf(str,
sizeof(str),
"hevc_metadata=crop_right=%d:crop_bottom=%d",
130 crop_right, crop_bottom);
134 if (
ret >=
sizeof(str))
152 const char *codec_mime =
NULL;
158 if (
s->use_ndk_codec < 0)
163 codec_mime =
"video/avc";
166 codec_mime =
"video/hevc";
169 codec_mime =
"video/x-vnd.on2.vp8";
172 codec_mime =
"video/x-vnd.on2.vp9";
175 codec_mime =
"video/mp4v-es";
178 codec_mime =
"video/av01";
209 if (
s->width % 16 ||
s->height % 16)
211 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
212 s->width,
s->height);
227 dev_ctx = device_ctx->
hwctx;
231 if (!
s->window && user_ctx && user_ctx->
surface)
236 av_log(avctx,
AV_LOG_ERROR,
"Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
243 if (!
s->use_ndk_codec && !
s->window->surface) {
246 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
271 if (
s->bitrate_mode >= 0)
284 "Use %d as the default MediaFormat i-frame-interval, "
285 "please set gop_size properly (>= fps)\n", gop);
305 "Enabling B frames will produce packets with no DTS. "
306 "Use -strict experimental to use it anyway.\n");
312 if (
s->pts_as_dts == -1)
335 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
336 "Use extract_extradata bsf when necessary.\n");
358 int extradata_size = 0;
392 s->extradata_size = out_info.
size;
393 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
403 if (
s->extradata_size) {
404 extradata_size =
s->extradata_size;
405 s->extradata_size = 0;
406 memcpy(
pkt->
data,
s->extradata, extradata_size);
418 " flags %d extradata %d\n",
429 uint8_t *dst_data[4] = {};
430 int dst_linesize[4] = {};
434 dst_data[1] = dst +
s->width *
s->height;
435 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
437 dst_linesize[0] =
s->width;
438 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
441 dst_data[1] = dst +
s->width *
s->height;
443 dst_linesize[0] =
s->width;
444 dst_linesize[1] =
s->width;
458 uint8_t *input_buf =
NULL;
459 size_t input_size = 0;
533 if (!
s->frame->buf[0]) {
582 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
583 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
584 #define COMMON_OPTION \
585 { "ndk_codec", "Use MediaCodec from NDK", \
586 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
587 { "codec_name", "Select codec by name", \
588 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
589 { "bitrate_mode", "Bitrate control method", \
590 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" }, \
591 { "cq", "Constant quality mode", \
592 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" }, \
593 { "vbr", "Variable bitrate mode", \
594 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" }, \
595 { "cbr", "Constant bitrate mode", \
596 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \
597 { "cbr_fd", "Constant bitrate mode with frame drops", \
598 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "bitrate_mode" }, \
599 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
600 "since most of Android devices don't output B frames by default.", \
601 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
604 #define MEDIACODEC_ENCODER_CLASS(name) \
605 static const AVClass name ## _mediacodec_class = { \
606 .class_name = #name "_mediacodec", \
607 .item_name = av_default_item_name, \
608 .option = name ## _options, \
609 .version = LIBAVUTIL_VERSION_INT, \
612 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
613 MEDIACODEC_ENCODER_CLASS(short_name) \
614 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
615 .p.name = #short_name "_mediacodec", \
616 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
617 .p.type = AVMEDIA_TYPE_VIDEO, \
619 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY \
620 | AV_CODEC_CAP_HARDWARE, \
621 .priv_data_size = sizeof(MediaCodecEncContext), \
622 .p.pix_fmts = avc_pix_fmts, \
623 .init = mediacodec_init, \
624 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
625 .close = mediacodec_close, \
626 .p.priv_class = &short_name ## _mediacodec_class, \
627 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
628 .p.wrapper_name = "mediacodec", \
629 .hw_configs = mediacodec_hw_configs, \
632 #if CONFIG_H264_MEDIACODEC_ENCODER
634 enum MediaCodecAvcLevel {
651 AVCLevel52 = 0x10000,
653 AVCLevel61 = 0x40000,
654 AVCLevel62 = 0x80000,
669 {
"level",
"Specify level",
696 #endif // CONFIG_H264_MEDIACODEC_ENCODER
698 #if CONFIG_HEVC_MEDIACODEC_ENCODER
700 enum MediaCodecHevcLevel {
701 HEVCMainTierLevel1 = 0x1,
702 HEVCHighTierLevel1 = 0x2,
703 HEVCMainTierLevel2 = 0x4,
704 HEVCHighTierLevel2 = 0x8,
705 HEVCMainTierLevel21 = 0x10,
706 HEVCHighTierLevel21 = 0x20,
707 HEVCMainTierLevel3 = 0x40,
708 HEVCHighTierLevel3 = 0x80,
709 HEVCMainTierLevel31 = 0x100,
710 HEVCHighTierLevel31 = 0x200,
711 HEVCMainTierLevel4 = 0x400,
712 HEVCHighTierLevel4 = 0x800,
713 HEVCMainTierLevel41 = 0x1000,
714 HEVCHighTierLevel41 = 0x2000,
715 HEVCMainTierLevel5 = 0x4000,
716 HEVCHighTierLevel5 = 0x8000,
717 HEVCMainTierLevel51 = 0x10000,
718 HEVCHighTierLevel51 = 0x20000,
719 HEVCMainTierLevel52 = 0x40000,
720 HEVCHighTierLevel52 = 0x80000,
721 HEVCMainTierLevel6 = 0x100000,
722 HEVCHighTierLevel6 = 0x200000,
723 HEVCMainTierLevel61 = 0x400000,
724 HEVCHighTierLevel61 = 0x800000,
725 HEVCMainTierLevel62 = 0x1000000,
726 HEVCHighTierLevel62 = 0x2000000,
735 {
"level",
"Specify tier and level",
737 {
"m1",
"Main tier level 1",
739 {
"h1",
"High tier level 1",
741 {
"m2",
"Main tier level 2",
743 {
"h2",
"High tier level 2",
745 {
"m2.1",
"Main tier level 2.1",
747 {
"h2.1",
"High tier level 2.1",
749 {
"m3",
"Main tier level 3",
751 {
"h3",
"High tier level 3",
753 {
"m3.1",
"Main tier level 3.1",
755 {
"h3.1",
"High tier level 3.1",
757 {
"m4",
"Main tier level 4",
759 {
"h4",
"High tier level 4",
761 {
"m4.1",
"Main tier level 4.1",
763 {
"h4.1",
"High tier level 4.1",
765 {
"m5",
"Main tier level 5",
767 {
"h5",
"High tier level 5",
769 {
"m5.1",
"Main tier level 5.1",
771 {
"h5.1",
"High tier level 5.1",
773 {
"m5.2",
"Main tier level 5.2",
775 {
"h5.2",
"High tier level 5.2",
777 {
"m6",
"Main tier level 6",
779 {
"h6",
"High tier level 6",
781 {
"m6.1",
"Main tier level 6.1",
783 {
"h6.1",
"High tier level 6.1",
785 {
"m6.2",
"Main tier level 6.2",
787 {
"h6.2",
"High tier level 6.2",
794 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
796 #if CONFIG_VP8_MEDIACODEC_ENCODER
798 enum MediaCodecVP8Level {
799 VP8Level_Version0 = 0x01,
800 VP8Level_Version1 = 0x02,
801 VP8Level_Version2 = 0x04,
802 VP8Level_Version3 = 0x08,
805 static const AVOption vp8_options[] = {
807 {
"level",
"Specify tier and level",
809 {
"V0",
"Level Version 0",
811 {
"V1",
"Level Version 1",
813 {
"V2",
"Level Version 2",
815 {
"V3",
"Level Version 3",
822 #endif // CONFIG_VP8_MEDIACODEC_ENCODER
824 #if CONFIG_VP9_MEDIACODEC_ENCODER
826 enum MediaCodecVP9Level {
843 static const AVOption vp9_options[] = {
851 {
"level",
"Specify tier and level",
855 {
"1.1",
"Level 1.1",
859 {
"2.1",
"Level 2.1",
863 {
"3.1",
"Level 3.1",
867 {
"4.1",
"Level 4.1",
871 {
"5.1",
"Level 5.1",
873 {
"5.2",
"Level 5.2",
877 {
"6.1",
"Level 4.1",
879 {
"6.2",
"Level 6.2",
886 #endif // CONFIG_VP9_MEDIACODEC_ENCODER
888 #if CONFIG_MPEG4_MEDIACODEC_ENCODER
890 enum MediaCodecMpeg4Level {
908 {
"level",
"Specify tier and level",
935 #endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
937 #if CONFIG_AV1_MEDIACODEC_ENCODER
939 enum MediaCodecAV1Level {
957 AV1Level61 = 0x20000,
958 AV1Level62 = 0x40000,
959 AV1Level63 = 0x80000,
960 AV1Level7 = 0x100000,
961 AV1Level71 = 0x200000,
962 AV1Level72 = 0x400000,
963 AV1Level73 = 0x800000,
971 {
"level",
"Specify tier and level",
975 {
"2.1",
"Level 2.1",
977 {
"2.2",
"Level 2.2",
979 {
"2.3",
"Level 2.3",
983 {
"3.1",
"Level 3.1",
985 {
"3.2",
"Level 3.2",
987 {
"3.3",
"Level 3.3",
991 {
"4.1",
"Level 4.1",
993 {
"4.2",
"Level 4.2",
995 {
"4.3",
"Level 4.3",
999 {
"5.1",
"Level 5.1",
1001 {
"5.2",
"Level 5.2",
1003 {
"5.3",
"Level 5.3",
1007 {
"6.1",
"Level 6.1",
1009 {
"6.2",
"Level 6.2",
1011 {
"6.3",
"Level 6.3",
1015 {
"7.1",
"Level 7.1",
1017 {
"7.2",
"Level 7.2",
1019 {
"7.3",
"Level 7.3",
1026 #endif // CONFIG_AV1_MEDIACODEC_ENCODER