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)
126 crop_right, crop_bottom);
129 crop_right, crop_bottom);
151 const char *codec_mime =
NULL;
157 if (
s->use_ndk_codec < 0)
162 codec_mime =
"video/avc";
165 codec_mime =
"video/hevc";
196 if (
s->width % 16 ||
s->height % 16)
198 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
199 s->width,
s->height);
214 dev_ctx = device_ctx->
hwctx;
218 if (!
s->window && user_ctx && user_ctx->
surface)
223 av_log(avctx,
AV_LOG_ERROR,
"Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
230 if (!
s->use_ndk_codec && !
s->window->surface) {
233 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
248 if (
s->bitrate_mode >= 0)
261 "Use %d as the default MediaFormat i-frame-interval, "
262 "please set gop_size properly (>= fps)\n", gop);
282 "Enabling B frames will produce packets with no DTS. "
283 "Use -strict experimental to use it anyway.\n");
289 if (
s->pts_as_dts == -1)
331 int extradata_size = 0;
365 s->extradata_size = out_info.
size;
366 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
376 if (
s->extradata_size) {
377 extradata_size =
s->extradata_size;
378 s->extradata_size = 0;
379 memcpy(
pkt->
data,
s->extradata, extradata_size);
391 " flags %d extradata %d\n",
402 uint8_t *dst_data[4] = {};
403 int dst_linesize[4] = {};
404 const uint8_t *src_data[4] = {
410 dst_data[1] = dst +
s->width *
s->height;
411 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
413 dst_linesize[0] =
s->width;
414 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
417 dst_data[1] = dst +
s->width *
s->height;
419 dst_linesize[0] =
s->width;
420 dst_linesize[1] =
s->width;
434 uint8_t *input_buf =
NULL;
435 size_t input_size = 0;
509 if (!
s->frame->buf[0]) {
558 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
559 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
560 #define COMMON_OPTION \
561 { "ndk_codec", "Use MediaCodec from NDK", \
562 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
563 { "codec_name", "Select codec by name", \
564 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
565 { "bitrate_mode", "Bitrate control method", \
566 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" }, \
567 { "cq", "Constant quality mode", \
568 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" }, \
569 { "vbr", "Variable bitrate mode", \
570 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" }, \
571 { "cbr", "Constant bitrate mode", \
572 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \
573 { "cbr_fd", "Constant bitrate mode with frame drops", \
574 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "bitrate_mode" }, \
575 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
576 "since most of Android devices don't output B frames by default.", \
577 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
580 #define MEDIACODEC_ENCODER_CLASS(name) \
581 static const AVClass name ## _mediacodec_class = { \
582 .class_name = #name "_mediacodec", \
583 .item_name = av_default_item_name, \
584 .option = name ## _options, \
585 .version = LIBAVUTIL_VERSION_INT, \
588 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
589 MEDIACODEC_ENCODER_CLASS(short_name) \
590 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
591 .p.name = #short_name "_mediacodec", \
592 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
593 .p.type = AVMEDIA_TYPE_VIDEO, \
595 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY \
596 | AV_CODEC_CAP_HARDWARE, \
597 .priv_data_size = sizeof(MediaCodecEncContext), \
598 .p.pix_fmts = avc_pix_fmts, \
599 .init = mediacodec_init, \
600 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
601 .close = mediacodec_close, \
602 .p.priv_class = &short_name ## _mediacodec_class, \
603 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
604 .p.wrapper_name = "mediacodec", \
605 .hw_configs = mediacodec_hw_configs, \
608 #if CONFIG_H264_MEDIACODEC_ENCODER
610 enum MediaCodecAvcLevel {
627 AVCLevel52 = 0x10000,
629 AVCLevel61 = 0x40000,
630 AVCLevel62 = 0x80000,
635 {
"level",
"Specify level",
662 #endif // CONFIG_H264_MEDIACODEC_ENCODER
664 #if CONFIG_HEVC_MEDIACODEC_ENCODER
666 enum MediaCodecHevcLevel {
667 HEVCMainTierLevel1 = 0x1,
668 HEVCHighTierLevel1 = 0x2,
669 HEVCMainTierLevel2 = 0x4,
670 HEVCHighTierLevel2 = 0x8,
671 HEVCMainTierLevel21 = 0x10,
672 HEVCHighTierLevel21 = 0x20,
673 HEVCMainTierLevel3 = 0x40,
674 HEVCHighTierLevel3 = 0x80,
675 HEVCMainTierLevel31 = 0x100,
676 HEVCHighTierLevel31 = 0x200,
677 HEVCMainTierLevel4 = 0x400,
678 HEVCHighTierLevel4 = 0x800,
679 HEVCMainTierLevel41 = 0x1000,
680 HEVCHighTierLevel41 = 0x2000,
681 HEVCMainTierLevel5 = 0x4000,
682 HEVCHighTierLevel5 = 0x8000,
683 HEVCMainTierLevel51 = 0x10000,
684 HEVCHighTierLevel51 = 0x20000,
685 HEVCMainTierLevel52 = 0x40000,
686 HEVCHighTierLevel52 = 0x80000,
687 HEVCMainTierLevel6 = 0x100000,
688 HEVCHighTierLevel6 = 0x200000,
689 HEVCMainTierLevel61 = 0x400000,
690 HEVCHighTierLevel61 = 0x800000,
691 HEVCMainTierLevel62 = 0x1000000,
692 HEVCHighTierLevel62 = 0x2000000,
697 {
"level",
"Specify tier and level",
699 {
"m1",
"Main tier level 1",
701 {
"h1",
"High tier level 1",
703 {
"m2",
"Main tier level 2",
705 {
"h2",
"High tier level 2",
707 {
"m2.1",
"Main tier level 2.1",
709 {
"h2.1",
"High tier level 2.1",
711 {
"m3",
"Main tier level 3",
713 {
"h3",
"High tier level 3",
715 {
"m3.1",
"Main tier level 3.1",
717 {
"h3.1",
"High tier level 3.1",
719 {
"m4",
"Main tier level 4",
721 {
"h4",
"High tier level 4",
723 {
"m4.1",
"Main tier level 4.1",
725 {
"h4.1",
"High tier level 4.1",
727 {
"m5",
"Main tier level 5",
729 {
"h5",
"High tier level 5",
731 {
"m5.1",
"Main tier level 5.1",
733 {
"h5.1",
"High tier level 5.1",
735 {
"m5.2",
"Main tier level 5.2",
737 {
"h5.2",
"High tier level 5.2",
739 {
"m6",
"Main tier level 6",
741 {
"h6",
"High tier level 6",
743 {
"m6.1",
"Main tier level 6.1",
745 {
"h6.1",
"High tier level 6.1",
747 {
"m6.2",
"Main tier level 6.2",
749 {
"h6.2",
"High tier level 6.2",
756 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER