diff mbox series

[FFmpeg-devel,v3,03/17] avcodec/encode: switch to avcodec_get_supported_config()

Message ID 20240830102402.18149-4-ffmpeg@haasn.xyz
State New
Headers show
Series Add avcodec_get_supported_config() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Niklas Haas Aug. 30, 2024, 10:15 a.m. UTC
From: Niklas Haas <git@haasn.dev>

---
 libavcodec/encode.c | 90 ++++++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 33 deletions(-)

Comments

Michael Niedermayer Aug. 31, 2024, 8:16 p.m. UTC | #1
On Fri, Aug 30, 2024 at 12:15:26PM +0200, Niklas Haas wrote:
> From: Niklas Haas <git@haasn.dev>
> 
> ---
>  libavcodec/encode.c | 90 ++++++++++++++++++++++++++++-----------------
>  1 file changed, 57 insertions(+), 33 deletions(-)

breaks:
make V=2 fate-png-icc

  Stream #0:0: Video: png, rgb24(pc, gbr/bt709/unknown), 128x128 [SAR 2835:2835 DAR 1:1], 25 fps, 25 tbr, 25 tbn
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> rawvideo (native))
==1696388== Thread 3 vf#0:0:
==1696388== Invalid read of size 4
==1696388==    at 0x7BEA43: ff_default_get_supported_config (in ffmpeg/ffmpeg_g)
==1696388==    by 0x7BEBF4: avcodec_get_supported_config (in ffmpeg/ffmpeg_g)
==1696388==    by 0x8C6BED: ff_encode_preinit (in ffmpeg/ffmpeg_g)
==1696388==    by 0x7BE1FA: avcodec_open2 (in ffmpeg/ffmpeg_g)
==1696388==    by 0x30172F: enc_open (in ffmpeg/ffmpeg_g)
==1696388==    by 0x319CC8: send_to_enc (in ffmpeg/ffmpeg_g)
==1696388==    by 0x30531F: fg_output_frame (in ffmpeg/ffmpeg_g)
==1696388==    by 0x3062C9: read_frames.part.0 (in ffmpeg/ffmpeg_g)
==1696388==    by 0x308823: filter_thread (in ffmpeg/ffmpeg_g)
==1696388==    by 0x31A5EC: task_wrapper (in ffmpeg/ffmpeg_g)
==1696388==    by 0x4A04608: start_thread (pthread_create.c:477)
==1696388==    by 0x7728352: clone (clone.S:95)
==1696388==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

[...]
Niklas Haas Sept. 1, 2024, 10:06 a.m. UTC | #2
On Sat, 31 Aug 2024 22:16:25 +0200 Michael Niedermayer <michael@niedermayer.cc> wrote:
> On Fri, Aug 30, 2024 at 12:15:26PM +0200, Niklas Haas wrote:
> > From: Niklas Haas <git@haasn.dev>
> > 
> > ---
> >  libavcodec/encode.c | 90 ++++++++++++++++++++++++++++-----------------
> >  1 file changed, 57 insertions(+), 33 deletions(-)
> 
> breaks:
> make V=2 fate-png-icc
> 
>   Stream #0:0: Video: png, rgb24(pc, gbr/bt709/unknown), 128x128 [SAR 2835:2835 DAR 1:1], 25 fps, 25 tbr, 25 tbn
> Stream mapping:
>   Stream #0:0 -> #0:0 (png (native) -> rawvideo (native))
> ==1696388== Thread 3 vf#0:0:
> ==1696388== Invalid read of size 4
> ==1696388==    at 0x7BEA43: ff_default_get_supported_config (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x7BEBF4: avcodec_get_supported_config (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x8C6BED: ff_encode_preinit (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x7BE1FA: avcodec_open2 (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x30172F: enc_open (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x319CC8: send_to_enc (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x30531F: fg_output_frame (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x3062C9: read_frames.part.0 (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x308823: filter_thread (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x31A5EC: task_wrapper (in ffmpeg/ffmpeg_g)
> ==1696388==    by 0x4A04608: start_thread (pthread_create.c:477)
> ==1696388==    by 0x7728352: clone (clone.S:95)
> ==1696388==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
> 
> [...]
> -- 
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
> 
> I know you won't believe me, but the highest form of Human Excellence is
> to question oneself and others. -- Socrates
> _______________________________________________
> 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".

Small oversight during rebasing. Fixed for v4.
diff mbox series

Patch

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 34658d13d0..bc5acf985d 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -563,7 +563,8 @@  static int encode_preinit_video(AVCodecContext *avctx)
 {
     const AVCodec *c = avctx->codec;
     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
-    int i;
+    const enum AVPixelFormat *pix_fmts;
+    int ret, i, num_pix_fmts;
 
     if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
         av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
@@ -571,28 +572,33 @@  static int encode_preinit_video(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (c->pix_fmts) {
-        for (i = 0; c->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
-            if (avctx->pix_fmt == c->pix_fmts[i])
+    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
+                                       0, (const void **) &pix_fmts, &num_pix_fmts);
+    if (ret < 0)
+        return ret;
+
+    if (pix_fmts) {
+        for (i = 0; i < num_pix_fmts; i++)
+            if (avctx->pix_fmt == pix_fmts[i])
                 break;
-        if (c->pix_fmts[i] == AV_PIX_FMT_NONE) {
+        if (i == num_pix_fmts) {
             av_log(avctx, AV_LOG_ERROR,
                    "Specified pixel format %s is not supported by the %s encoder.\n",
                    av_get_pix_fmt_name(avctx->pix_fmt), c->name);
 
             av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
-            for (int p = 0; c->pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
+            for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
                 av_log(avctx, AV_LOG_ERROR, "  %s\n",
-                       av_get_pix_fmt_name(c->pix_fmts[p]));
+                       av_get_pix_fmt_name(pix_fmts[p]));
             }
 
             return AVERROR(EINVAL);
         }
-        if (c->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
-            c->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
-            c->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
-            c->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
-            c->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
+        if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
+            pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
+            pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
+            pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
+            pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
             avctx->color_range = AVCOL_RANGE_JPEG;
     }
 
@@ -646,7 +652,10 @@  FF_ENABLE_DEPRECATION_WARNINGS
 static int encode_preinit_audio(AVCodecContext *avctx)
 {
     const AVCodec *c = avctx->codec;
-    int i;
+    const enum AVSampleFormat *sample_fmts;
+    const int *supported_samplerates;
+    const AVChannelLayout *ch_layouts;
+    int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
 
     if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
         av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
@@ -659,53 +668,68 @@  static int encode_preinit_audio(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (c->sample_fmts) {
-        for (i = 0; c->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
-            if (avctx->sample_fmt == c->sample_fmts[i])
+    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
+                                       0, (const void **) &sample_fmts,
+                                       &num_sample_fmts);
+    if (ret < 0)
+        return ret;
+    if (sample_fmts) {
+        for (i = 0; i < num_sample_fmts; i++) {
+            if (avctx->sample_fmt == sample_fmts[i])
                 break;
             if (avctx->ch_layout.nb_channels == 1 &&
                 av_get_planar_sample_fmt(avctx->sample_fmt) ==
-                av_get_planar_sample_fmt(c->sample_fmts[i])) {
-                avctx->sample_fmt = c->sample_fmts[i];
+                av_get_planar_sample_fmt(sample_fmts[i])) {
+                avctx->sample_fmt = sample_fmts[i];
                 break;
             }
         }
-        if (c->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
+        if (i == num_sample_fmts) {
             av_log(avctx, AV_LOG_ERROR,
                    "Specified sample format %s is not supported by the %s encoder\n",
                    av_get_sample_fmt_name(avctx->sample_fmt), c->name);
 
             av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
-            for (int p = 0; c->sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
+            for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
                 av_log(avctx, AV_LOG_ERROR, "  %s\n",
-                       av_get_sample_fmt_name(c->sample_fmts[p]));
+                       av_get_sample_fmt_name(sample_fmts[p]));
             }
 
             return AVERROR(EINVAL);
         }
     }
-    if (c->supported_samplerates) {
-        for (i = 0; c->supported_samplerates[i] != 0; i++)
-            if (avctx->sample_rate == c->supported_samplerates[i])
+
+    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
+                                       0, (const void **) &supported_samplerates,
+                                       &num_samplerates);
+    if (ret < 0)
+        return ret;
+    if (supported_samplerates) {
+        for (i = 0; i < num_samplerates; i++)
+            if (avctx->sample_rate == supported_samplerates[i])
                 break;
-        if (c->supported_samplerates[i] == 0) {
+        if (i == num_samplerates) {
             av_log(avctx, AV_LOG_ERROR,
                    "Specified sample rate %d is not supported by the %s encoder\n",
                    avctx->sample_rate, c->name);
 
             av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
-            for (int p = 0; c->supported_samplerates[p]; p++)
-                av_log(avctx, AV_LOG_ERROR, "  %d\n", c->supported_samplerates[p]);
+            for (int p = 0; supported_samplerates[p]; p++)
+                av_log(avctx, AV_LOG_ERROR, "  %d\n", supported_samplerates[p]);
 
             return AVERROR(EINVAL);
         }
     }
-    if (c->ch_layouts) {
-        for (i = 0; c->ch_layouts[i].nb_channels; i++) {
-            if (!av_channel_layout_compare(&avctx->ch_layout, &c->ch_layouts[i]))
+    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
+                                       0, (const void **) &ch_layouts, &num_ch_layouts);
+    if (ret < 0)
+        return ret;
+    if (ch_layouts) {
+        for (i = 0; i < num_ch_layouts; i++) {
+            if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
                 break;
         }
-        if (!c->ch_layouts[i].nb_channels) {
+        if (i == num_ch_layouts) {
             char buf[512];
             int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
             av_log(avctx, AV_LOG_ERROR,
@@ -713,8 +737,8 @@  static int encode_preinit_audio(AVCodecContext *avctx)
                    ret > 0 ? buf : "?", c->name);
 
             av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
-            for (int p = 0; c->ch_layouts[p].nb_channels; p++) {
-                ret = av_channel_layout_describe(&c->ch_layouts[p], buf, sizeof(buf));
+            for (int p = 0; ch_layouts[p].nb_channels; p++) {
+                ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
                 av_log(avctx, AV_LOG_ERROR, "  %s\n", ret > 0 ? buf : "?");
             }
             return AVERROR(EINVAL);