diff mbox series

[FFmpeg-devel,4/7] avcodec/mediacodecenc: use bsf to handle crop

Message ID tencent_B3C0EAA81332DC4A4B8DE61290A726E26706@qq.com
State New
Headers show
Series [FFmpeg-devel,1/7] avcodec/mediacodecenc: add bitrate_mode option | expand

Checks

Context Check Description
andriy/configure_x86 warning Failed to apply patch
yinshiyou/configure_loongarch64 warning Failed to apply patch

Commit Message

zhilizhao(赵志立) Dec. 4, 2022, 5:12 p.m. UTC
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 mbox series

Patch

diff --git a/configure b/configure
index 0d754e7ae9..3bb0a091d4 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 d098880941..dce4a96d1f 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;