diff mbox series

[FFmpeg-devel,4/4] lavc/mediacodecenc: List supported pixel formats on configuration error

Message ID 015df2df4818b87922ab0e358d1949d50698a71b.camel@haerdin.se
State New
Headers show
Series [FFmpeg-devel,1/4] lavc/mediacodecenc: Add pix2color_fmt() and color2pix_fmt() | expand

Checks

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

Commit Message

Tomas Härdin Feb. 24, 2023, 9:03 a.m. UTC
This makes the situation a bit better for users, even if we can't
reliably probe pixel formats in init_static_data. I decided to keep
this patchset separate from that probing, since printing pixel formats
this way should be reliable.

/Tomas

Comments

Zhao Zhili Feb. 28, 2023, 12:25 p.m. UTC | #1
> This patch has been released by Epic Games' legal department.
> ---
>  libavcodec/mediacodecenc.c | 46 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
> index 03c80cbf99..1b8a2837c4 100644
> --- a/libavcodec/mediacodecenc.c
> +++ b/libavcodec/mediacodecenc.c
> @@ -97,6 +97,12 @@ static const enum AVPixelFormat avc_pix_fmts[] = {
>      AV_PIX_FMT_NONE
>  };
>  
> +static const int in_formats[] = {
> +    COLOR_FormatYUV420Planar,
> +    COLOR_FormatYUV420SemiPlanar,
> +    COLOR_FormatSurface,
> +};
> +
>  static void mediacodec_output_format(AVCodecContext *avctx)
>  {
>      MediaCodecEncContext *s = avctx->priv_data;
> @@ -131,6 +137,45 @@ static enum AVPixelFormat color2pix_fmt(AVCodecContext *avctx, int color_format)
>      av_assert0(0);
>  }
>  
> +// list pixel formats if the user tried to use one that isn't supported on this device
> +static void list_pix_fmts(AVCodecContext *avctx, const char *mime)
> +{
> +    MediaCodecEncContext *s = avctx->priv_data;
> +    int out_formats[FF_ARRAY_ELEMS(in_formats)], n;
> +    char *name = ff_AMediaCodec_getName(s->codec);
> +
> +    if (!name) {
> +        // API level likely below 28
> +        return;
> +    }
> +
> +    if ((n = ff_AMediaCodec_color_formats_intersect(name, mime, in_formats,
> +                                                    FF_ARRAY_ELEMS(in_formats),
> +                                                    out_formats, avctx)) < 0) {
> +        goto done;
> +    }
> +
> +    for (int i = 0; i < n; i++) {
> +        if (color2pix_fmt(avctx, out_formats[i]) == avctx->pix_fmt) {
> +            // user specified a pixel format that is actually supported,
> +            // no need to print anything
> +            goto done;
> +        }
> +    }
> +
> +    AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);

Missing ‘const’ and triggered -Wdeclaration-after-statement.

> +    av_log(avctx, AV_LOG_ERROR, "pixel format %s not supported by MediaCodec %s\n", desc->name, name);
> +    av_log(avctx, AV_LOG_INFO, "supported formats are:");
> +    for (int i = 0; i < n; i++) {
> +        desc = av_pix_fmt_desc_get(color2pix_fmt(avctx, out_formats[i]));
> +        av_log(avctx, AV_LOG_INFO, " %s", desc->name);
> +    }
> +    av_log(avctx, AV_LOG_INFO, "\n");

It’s not thread safe, it can be interrupted by other threads' log message.

> +
> +done:
> +    av_free(name);
> +}
> +
>  static int mediacodec_init_bsf(AVCodecContext *avctx)
>  {
>      MediaCodecEncContext *s = avctx->priv_data;
> @@ -308,6 +353,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
>      ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
>      if (ret) {
>          av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret));
> +        list_pix_fmts(avctx, codec_mime);
>          goto bailout;
>      }
>  
> -- 
> 2.30.2
>
diff mbox series

Patch

From eed0ed202bf8c496ab945b32e2e7fc7cad062c30 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 10 Jan 2023 20:38:56 +0100
Subject: [PATCH 4/4] lavc/mediacodecenc: List supported pixel formats on
 configuration error

This patch has been released by Epic Games' legal department.
---
 libavcodec/mediacodecenc.c | 46 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 03c80cbf99..1b8a2837c4 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -97,6 +97,12 @@  static const enum AVPixelFormat avc_pix_fmts[] = {
     AV_PIX_FMT_NONE
 };
 
+static const int in_formats[] = {
+    COLOR_FormatYUV420Planar,
+    COLOR_FormatYUV420SemiPlanar,
+    COLOR_FormatSurface,
+};
+
 static void mediacodec_output_format(AVCodecContext *avctx)
 {
     MediaCodecEncContext *s = avctx->priv_data;
@@ -131,6 +137,45 @@  static enum AVPixelFormat color2pix_fmt(AVCodecContext *avctx, int color_format)
     av_assert0(0);
 }
 
+// list pixel formats if the user tried to use one that isn't supported on this device
+static void list_pix_fmts(AVCodecContext *avctx, const char *mime)
+{
+    MediaCodecEncContext *s = avctx->priv_data;
+    int out_formats[FF_ARRAY_ELEMS(in_formats)], n;
+    char *name = ff_AMediaCodec_getName(s->codec);
+
+    if (!name) {
+        // API level likely below 28
+        return;
+    }
+
+    if ((n = ff_AMediaCodec_color_formats_intersect(name, mime, in_formats,
+                                                    FF_ARRAY_ELEMS(in_formats),
+                                                    out_formats, avctx)) < 0) {
+        goto done;
+    }
+
+    for (int i = 0; i < n; i++) {
+        if (color2pix_fmt(avctx, out_formats[i]) == avctx->pix_fmt) {
+            // user specified a pixel format that is actually supported,
+            // no need to print anything
+            goto done;
+        }
+    }
+
+    AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+    av_log(avctx, AV_LOG_ERROR, "pixel format %s not supported by MediaCodec %s\n", desc->name, name);
+    av_log(avctx, AV_LOG_INFO, "supported formats are:");
+    for (int i = 0; i < n; i++) {
+        desc = av_pix_fmt_desc_get(color2pix_fmt(avctx, out_formats[i]));
+        av_log(avctx, AV_LOG_INFO, " %s", desc->name);
+    }
+    av_log(avctx, AV_LOG_INFO, "\n");
+
+done:
+    av_free(name);
+}
+
 static int mediacodec_init_bsf(AVCodecContext *avctx)
 {
     MediaCodecEncContext *s = avctx->priv_data;
@@ -308,6 +353,7 @@  static av_cold int mediacodec_init(AVCodecContext *avctx)
     ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
     if (ret) {
         av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret));
+        list_pix_fmts(avctx, codec_mime);
         goto bailout;
     }
 
-- 
2.30.2