From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 97E7544B6C for ; Wed, 7 Dec 2022 09:33:23 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EECED68BDE9; Wed, 7 Dec 2022 11:32:19 +0200 (EET) Received: from out162-62-57-64.mail.qq.com (out162-62-57-64.mail.qq.com [162.62.57.64]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 351C068BD8E for ; Wed, 7 Dec 2022 11:32:09 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1670405521; bh=ItDNF5q32h5/n3XtMsUKEfCo6/TAozQpggdXOkdNSl8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Wu7huhGCQfSOdssHz4iYxWrJ5sGAa43fRzgS6qOWQd66WxxW7kwIOEpo7q3Oovjqa zIANnDFJEzPWY9FUvU0JhLx52kzkNYXUwph+7c6fRXfhJu9QSp6vA6ekgnD4CsVVII uTAG246/Q9+luzqCWurcpAi4bCw11uARZxXqpBEY= Received: from localhost.localdomain ([59.37.125.102]) by newxmesmtplogicsvrszb1-0.qq.com (NewEsmtp) with SMTP id 7F9B046E; Wed, 07 Dec 2022 17:31:57 +0800 X-QQ-mid: xmsmtpt1670405520tvdy3f1vg Message-ID: X-QQ-XMAILINFO: M0PjjqbLT90w5K6yXZBuviyeH2cZ7DPt/NC2/A8r4oNx7vbMcykRhWU3oAb69l fcbdoTHww32gRc7QB9JGSwRbbjALdLvJE/pabty1hvp42X+X4h5VvvjW4VIoeAmU6JfJiqBqFV2w gn89pTm2Xm478jVReIIBfswDckv0wASqnyLStYdwvI/U7Ub8ncRaQkF8aFe17J8wbraJuISZWq1B QlZAgwT2/gNLveG5CC6gOQVnuz8WewHAcujFYYBqxqbscq6oMqxqmv6Mb7EbzVNGeA800Ldiajin RR9PYB7KW2GGT65OfRKyVYHRU2oR2TKYSDuWzdrrBRg4YU6JBh6108NH/VhTnvLMflCBObtzCoRj Jc2TXjUvU7jfKYXDipB+pydfSfn9ZhXYFbIkKbedbytIf8AcwPxbYY71SjP1v7HQsoljPvTu+eiq VluCC5KfVZiZ3mIAL8XsTRhohUQ4p0fDY+vdOvdVuGC+b02m2bnWbIIASi/zsm89YTZJF76rEiWt hRt9eYdQM0psj0b+UUqA2BWIeo2uOb50azCaGJXdHEKWnnu7JhJLz+KEJ0fBP8VYY/MthnhyD/mo fYmpoORifi9h2hdW6eqx4Uv7QaRfMQqPJ8F2fkjXcMZKUOzeJO0Wfl4OCsxTtoLnZVOM+oWZgSok GHXe3gN6vC7R9bIpTwvmGK6EDEyfWwN2fvpWqNz8ORGaqKnz2/8qnrHkKe7dI9yeoljHvEHZgdXM qPAWxqObDaeiA3KbgK0fDWlNHHpQlGS8kNRNvN5zQTsXJcnUtkYaX2QlCDLi4USV4CsUhW5ZKu1E lpnwxQflBby5A+q2CH8EluDiZ82S6HJGcpgswsBmk6IELOYZAIeegn2ZoAxwdzvyhI92Sr0ZO8uU MNMzhq5sH4PjLoiuhKf19xhi43GfP0ZUYG9wxJ0XcV9aGmMXnWt2Q/RNd2VnyRl0XuzEJsnCj3ud Zpai934JcVLbRWojxxlxIbr3tucBlq1Rd9S1OGttQzXqhM1G83ifNxqDQleGuE From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Dec 2022 17:31:19 +0800 X-OQ-MSGID: <20221207093122.553668-5-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221207093122.553668-1-quinkblack@foxmail.com> References: <20221207093122.553668-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 4/7] avcodec/mediacodecenc: use bsf to handle crop X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zhao Zhili Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Zhao Zhili It's well known that mediacodec encoder requires 16x16 alignment. Use our bsf to fix the crop info. --- configure | 2 ++ libavcodec/mediacodecenc.c | 65 +++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/configure b/configure index f4eedfc207..2180ebb4f1 100755 --- a/configure +++ b/configure @@ -3169,6 +3169,7 @@ h264_mediacodec_decoder_extralibs="-landroid" h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser" h264_mediacodec_encoder_deps="mediacodec" h264_mediacodec_encoder_extralibs="-landroid" +h264_mediacodec_encoder_select="h264_metadata" h264_mf_encoder_deps="mediafoundation" h264_mmal_decoder_deps="mmal" h264_nvenc_encoder_deps="nvenc" @@ -3190,6 +3191,7 @@ hevc_mediacodec_decoder_extralibs="-landroid" hevc_mediacodec_decoder_select="hevc_mp4toannexb_bsf hevc_parser" hevc_mediacodec_encoder_deps="mediacodec" hevc_mediacodec_encoder_extralibs="-landroid" +hevc_mediacodec_encoder_select="hevc_metadata" hevc_mf_encoder_deps="mediafoundation" hevc_nvenc_encoder_deps="nvenc" hevc_nvenc_encoder_select="atsc_a53" diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c index 2f78567451..7f2ae88285 100644 --- a/libavcodec/mediacodecenc.c +++ b/libavcodec/mediacodecenc.c @@ -28,6 +28,7 @@ #include "libavutil/opt.h" #include "avcodec.h" +#include "bsf.h" #include "codec_internal.h" #include "encode.h" #include "hwconfig.h" @@ -78,6 +79,7 @@ typedef struct MediaCodecEncContext { int eof_sent; AVFrame *frame; + AVBSFContext *bsf; int bitrate_mode; int level; @@ -119,6 +121,42 @@ static void mediacodec_output_format(AVCodecContext *avctx) ff_AMediaFormat_delete(out_format); } +static int mediacodec_init_bsf(AVCodecContext *avctx) +{ + MediaCodecEncContext *s = avctx->priv_data; + char str[128]; + int ret; + int crop_right = s->width - avctx->width; + int crop_bottom = s->height - avctx->height; + + if (!crop_right && !crop_bottom) + return 0; + + if (avctx->codec_id == AV_CODEC_ID_H264) + ret = snprintf(str, sizeof(str), "h264_metadata=crop_right=%d:crop_bottom=%d", + crop_right, crop_bottom); + else if (avctx->codec_id == AV_CODEC_ID_HEVC) + ret = snprintf(str, sizeof(str), "hevc_metadata=crop_right=%d:crop_bottom=%d", + crop_right, crop_bottom); + else + return 0; + + if (ret >= sizeof(str)) + return AVERROR_BUFFER_TOO_SMALL; + + ret = av_bsf_list_parse_str(str, &s->bsf); + if (ret < 0) + return ret; + + ret = avcodec_parameters_from_context(s->bsf->par_in, avctx); + if (ret < 0) + return ret; + s->bsf->time_base_in = avctx->time_base; + ret = av_bsf_init(s->bsf); + + return ret; +} + static av_cold int mediacodec_init(AVCodecContext *avctx) { const char *codec_mime = NULL; @@ -159,7 +197,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) ff_AMediaFormat_setString(format, "mime", codec_mime); s->width = FFALIGN(avctx->width, 16); - s->height = avctx->height; + s->height = FFALIGN(avctx->height, 16); ff_AMediaFormat_setInt32(format, "width", s->width); ff_AMediaFormat_setInt32(format, "height", s->height); @@ -252,6 +290,10 @@ static av_cold int mediacodec_init(AVCodecContext *avctx) goto bailout; } + ret = mediacodec_init_bsf(avctx); + if (ret) + goto bailout; + mediacodec_output_format(avctx); s->frame = av_frame_alloc(); @@ -444,10 +486,24 @@ static int mediacodec_encode(AVCodecContext *avctx, AVPacket *pkt) // 2. Got a packet success // 3. No AVFrame is available yet (don't return if get_frame return EOF) while (1) { + if (s->bsf) { + ret = av_bsf_receive_packet(s->bsf, pkt); + if (!ret) + return 0; + if (ret != AVERROR(EAGAIN)) + return ret; + } + ret = mediacodec_receive(avctx, pkt, &got_packet); - if (!ret) - return 0; - else if (ret != AVERROR(EAGAIN)) + if (s->bsf) { + if (!ret || ret == AVERROR_EOF) + ret = av_bsf_send_packet(s->bsf, pkt); + } else { + if (!ret) + return 0; + } + + if (ret != AVERROR(EAGAIN)) return ret; if (!s->frame->buf[0]) { @@ -480,6 +536,7 @@ static av_cold int mediacodec_close(AVCodecContext *avctx) s->window = NULL; } + av_bsf_free(&s->bsf); av_frame_free(&s->frame); return 0; -- 2.25.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".