23 #include "config_components.h"
40 #define INPUT_DEQUEUE_TIMEOUT_US 8000
41 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
118 int crop_right =
s->width - avctx->
width;
119 int crop_bottom =
s->height - avctx->
height;
121 if (!crop_right && !crop_bottom)
125 ret =
snprintf(str,
sizeof(str),
"h264_metadata=crop_right=%d:crop_bottom=%d",
126 crop_right, crop_bottom);
128 ret =
snprintf(str,
sizeof(str),
"hevc_metadata=crop_right=%d:crop_bottom=%d",
129 crop_right, crop_bottom);
133 if (
ret >=
sizeof(str))
151 const char *codec_mime =
NULL;
157 if (
s->use_ndk_codec < 0)
162 codec_mime =
"video/avc";
165 codec_mime =
"video/hevc";
168 codec_mime =
"video/x-vnd.on2.vp8";
171 codec_mime =
"video/x-vnd.on2.vp9";
174 codec_mime =
"video/mp4v-es";
177 codec_mime =
"video/av01";
208 if (
s->width % 16 ||
s->height % 16)
210 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
211 s->width,
s->height);
226 dev_ctx = device_ctx->
hwctx;
230 if (!
s->window && user_ctx && user_ctx->
surface)
235 av_log(avctx,
AV_LOG_ERROR,
"Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
242 if (!
s->use_ndk_codec && !
s->window->surface) {
245 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
270 if (
s->bitrate_mode >= 0)
283 "Use %d as the default MediaFormat i-frame-interval, "
284 "please set gop_size properly (>= fps)\n", gop);
304 "Enabling B frames will produce packets with no DTS. "
305 "Use -strict experimental to use it anyway.\n");
311 if (
s->pts_as_dts == -1)
334 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
335 "Use extract_extradata bsf when necessary.\n");
357 int extradata_size = 0;
391 s->extradata_size = out_info.
size;
392 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
402 if (
s->extradata_size) {
403 extradata_size =
s->extradata_size;
404 s->extradata_size = 0;
405 memcpy(
pkt->
data,
s->extradata, extradata_size);
417 " flags %d extradata %d\n",
428 uint8_t *dst_data[4] = {};
429 int dst_linesize[4] = {};
430 const uint8_t *src_data[4] = {
436 dst_data[1] = dst +
s->width *
s->height;
437 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
439 dst_linesize[0] =
s->width;
440 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
443 dst_data[1] = dst +
s->width *
s->height;
445 dst_linesize[0] =
s->width;
446 dst_linesize[1] =
s->width;
460 uint8_t *input_buf =
NULL;
461 size_t input_size = 0;
535 if (!
s->frame->buf[0]) {
584 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
585 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
586 #define COMMON_OPTION \
587 { "ndk_codec", "Use MediaCodec from NDK", \
588 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
589 { "codec_name", "Select codec by name", \
590 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
591 { "bitrate_mode", "Bitrate control method", \
592 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" }, \
593 { "cq", "Constant quality mode", \
594 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" }, \
595 { "vbr", "Variable bitrate mode", \
596 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" }, \
597 { "cbr", "Constant bitrate mode", \
598 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \
599 { "cbr_fd", "Constant bitrate mode with frame drops", \
600 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "bitrate_mode" }, \
601 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
602 "since most of Android devices don't output B frames by default.", \
603 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
606 #define MEDIACODEC_ENCODER_CLASS(name) \
607 static const AVClass name ## _mediacodec_class = { \
608 .class_name = #name "_mediacodec", \
609 .item_name = av_default_item_name, \
610 .option = name ## _options, \
611 .version = LIBAVUTIL_VERSION_INT, \
614 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
615 MEDIACODEC_ENCODER_CLASS(short_name) \
616 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
617 .p.name = #short_name "_mediacodec", \
618 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
619 .p.type = AVMEDIA_TYPE_VIDEO, \
621 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY \
622 | AV_CODEC_CAP_HARDWARE, \
623 .priv_data_size = sizeof(MediaCodecEncContext), \
624 .p.pix_fmts = avc_pix_fmts, \
625 .init = mediacodec_init, \
626 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
627 .close = mediacodec_close, \
628 .p.priv_class = &short_name ## _mediacodec_class, \
629 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
630 .p.wrapper_name = "mediacodec", \
631 .hw_configs = mediacodec_hw_configs, \
634 #if CONFIG_H264_MEDIACODEC_ENCODER
636 enum MediaCodecAvcLevel {
653 AVCLevel52 = 0x10000,
655 AVCLevel61 = 0x40000,
656 AVCLevel62 = 0x80000,
661 {
"level",
"Specify level",
688 #endif // CONFIG_H264_MEDIACODEC_ENCODER
690 #if CONFIG_HEVC_MEDIACODEC_ENCODER
692 enum MediaCodecHevcLevel {
693 HEVCMainTierLevel1 = 0x1,
694 HEVCHighTierLevel1 = 0x2,
695 HEVCMainTierLevel2 = 0x4,
696 HEVCHighTierLevel2 = 0x8,
697 HEVCMainTierLevel21 = 0x10,
698 HEVCHighTierLevel21 = 0x20,
699 HEVCMainTierLevel3 = 0x40,
700 HEVCHighTierLevel3 = 0x80,
701 HEVCMainTierLevel31 = 0x100,
702 HEVCHighTierLevel31 = 0x200,
703 HEVCMainTierLevel4 = 0x400,
704 HEVCHighTierLevel4 = 0x800,
705 HEVCMainTierLevel41 = 0x1000,
706 HEVCHighTierLevel41 = 0x2000,
707 HEVCMainTierLevel5 = 0x4000,
708 HEVCHighTierLevel5 = 0x8000,
709 HEVCMainTierLevel51 = 0x10000,
710 HEVCHighTierLevel51 = 0x20000,
711 HEVCMainTierLevel52 = 0x40000,
712 HEVCHighTierLevel52 = 0x80000,
713 HEVCMainTierLevel6 = 0x100000,
714 HEVCHighTierLevel6 = 0x200000,
715 HEVCMainTierLevel61 = 0x400000,
716 HEVCHighTierLevel61 = 0x800000,
717 HEVCMainTierLevel62 = 0x1000000,
718 HEVCHighTierLevel62 = 0x2000000,
723 {
"level",
"Specify tier and level",
725 {
"m1",
"Main tier level 1",
727 {
"h1",
"High tier level 1",
729 {
"m2",
"Main tier level 2",
731 {
"h2",
"High tier level 2",
733 {
"m2.1",
"Main tier level 2.1",
735 {
"h2.1",
"High tier level 2.1",
737 {
"m3",
"Main tier level 3",
739 {
"h3",
"High tier level 3",
741 {
"m3.1",
"Main tier level 3.1",
743 {
"h3.1",
"High tier level 3.1",
745 {
"m4",
"Main tier level 4",
747 {
"h4",
"High tier level 4",
749 {
"m4.1",
"Main tier level 4.1",
751 {
"h4.1",
"High tier level 4.1",
753 {
"m5",
"Main tier level 5",
755 {
"h5",
"High tier level 5",
757 {
"m5.1",
"Main tier level 5.1",
759 {
"h5.1",
"High tier level 5.1",
761 {
"m5.2",
"Main tier level 5.2",
763 {
"h5.2",
"High tier level 5.2",
765 {
"m6",
"Main tier level 6",
767 {
"h6",
"High tier level 6",
769 {
"m6.1",
"Main tier level 6.1",
771 {
"h6.1",
"High tier level 6.1",
773 {
"m6.2",
"Main tier level 6.2",
775 {
"h6.2",
"High tier level 6.2",
782 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
784 #if CONFIG_VP8_MEDIACODEC_ENCODER
786 enum MediaCodecVP8Level {
787 VP8Level_Version0 = 0x01,
788 VP8Level_Version1 = 0x02,
789 VP8Level_Version2 = 0x04,
790 VP8Level_Version3 = 0x08,
793 static const AVOption vp8_options[] = {
795 {
"level",
"Specify tier and level",
797 {
"V0",
"Level Version 0",
799 {
"V1",
"Level Version 1",
801 {
"V2",
"Level Version 2",
803 {
"V3",
"Level Version 3",
810 #endif // CONFIG_VP8_MEDIACODEC_ENCODER
812 #if CONFIG_VP9_MEDIACODEC_ENCODER
814 enum MediaCodecVP9Level {
831 static const AVOption vp9_options[] = {
833 {
"level",
"Specify tier and level",
837 {
"1.1",
"Level 1.1",
841 {
"2.1",
"Level 2.1",
845 {
"3.1",
"Level 3.1",
849 {
"4.1",
"Level 4.1",
853 {
"5.1",
"Level 5.1",
855 {
"5.2",
"Level 5.2",
859 {
"6.1",
"Level 4.1",
861 {
"6.2",
"Level 6.2",
868 #endif // CONFIG_VP9_MEDIACODEC_ENCODER
870 #if CONFIG_MPEG4_MEDIACODEC_ENCODER
872 enum MediaCodecMpeg4Level {
887 {
"level",
"Specify tier and level",
914 #endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
916 #if CONFIG_AV1_MEDIACODEC_ENCODER
918 enum MediaCodecAV1Level {
936 AV1Level61 = 0x20000,
937 AV1Level62 = 0x40000,
938 AV1Level63 = 0x80000,
939 AV1Level7 = 0x100000,
940 AV1Level71 = 0x200000,
941 AV1Level72 = 0x400000,
942 AV1Level73 = 0x800000,
947 {
"level",
"Specify tier and level",
951 {
"2.1",
"Level 2.1",
953 {
"2.2",
"Level 2.2",
955 {
"2.3",
"Level 2.3",
959 {
"3.1",
"Level 3.1",
961 {
"3.2",
"Level 3.2",
963 {
"3.3",
"Level 3.3",
967 {
"4.1",
"Level 4.1",
969 {
"4.2",
"Level 4.2",
971 {
"4.3",
"Level 4.3",
975 {
"5.1",
"Level 5.1",
977 {
"5.2",
"Level 5.2",
979 {
"5.3",
"Level 5.3",
983 {
"6.1",
"Level 6.1",
985 {
"6.2",
"Level 6.2",
987 {
"6.3",
"Level 6.3",
991 {
"7.1",
"Level 7.1",
993 {
"7.2",
"Level 7.2",
995 {
"7.3",
"Level 7.3",
1002 #endif // CONFIG_AV1_MEDIACODEC_ENCODER