Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Zhao Zhili <quinkblack@foxmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Zhao Zhili <zhilizhao@tencent.com>
Subject: [FFmpeg-devel] [PATCH v2 4/7] avcodec/mediacodecenc: use bsf to handle crop
Date: Wed,  7 Dec 2022 17:31:19 +0800
Message-ID: <tencent_BC9A5B401C9AD79F3EABAC4E7170572D3908@qq.com> (raw)
In-Reply-To: <20221207093122.553668-1-quinkblack@foxmail.com>

From: Zhao Zhili <zhilizhao@tencent.com>

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".

  parent reply	other threads:[~2022-12-07  9:33 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221207093122.553668-1-quinkblack@foxmail.com>
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 1/7] avcodec/mediacodecenc: make each encoder has its own option Zhao Zhili
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 2/7] avcodec/mediacodecenc: add bitrate_mode option Zhao Zhili
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 3/7] avcodec/mediacodecenc: add level option Zhao Zhili
2022-12-07  9:31 ` Zhao Zhili [this message]
2022-12-09 17:22   ` [FFmpeg-devel] [PATCH v3 3/7] avcodec/mediacodecenc: use bsf to handle crop Zhao Zhili
2022-12-12 15:27     ` Tomas Härdin
2022-12-13  3:20       ` "zhilizhao(赵志立)"
2022-12-14 17:08         ` Tomas Härdin
2022-12-14 17:19           ` Tomas Härdin
2022-12-14 17:37           ` Zhao Zhili
2022-12-14 17:43             ` Zhao Zhili
2022-12-20 18:24             ` Tomas Härdin
2022-12-21  7:17               ` "zhilizhao(赵志立)"
2022-12-21 10:06                 ` Tomas Härdin
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 5/7] avcodec/mediacodecenc: remove the strategy to create DTS Zhao Zhili
2022-12-12 15:28   ` Tomas Härdin
2022-12-13  2:55     ` "zhilizhao(赵志立)"
2022-12-14 17:31       ` Tomas Härdin
2023-01-04 10:16   ` Anton Khirnov
2023-01-04 11:31     ` [FFmpeg-devel] [Internet]Re: " "zhilizhao(赵志立)"
2023-01-04 13:59       ` Tomas Härdin
2023-01-04 14:46         ` Zhao Zhili
2023-01-04 15:15           ` Anton Khirnov
2023-01-04 16:12             ` Zhao Zhili
2023-01-05  8:07               ` Anton Khirnov
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 6/7] avcodec/mediacodecenc: add max-bframes support Zhao Zhili
2022-12-07  9:31 ` [FFmpeg-devel] [PATCH v2 7/7] avcodec/mediacodecenc: add pts_as_dts option Zhao Zhili

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_BC9A5B401C9AD79F3EABAC4E7170572D3908@qq.com \
    --to=quinkblack@foxmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=zhilizhao@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git