diff mbox series

[FFmpeg-devel,05/10] doc/examples/transcode: switch to avcodec_get_supported_config()

Message ID 20240925132921.11203-5-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/10] fftools/ffmpeg_filter: stop using avfilter_graph_alloc_filter() incorrectly | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov Sept. 25, 2024, 1:29 p.m. UTC
---
 doc/examples/transcode.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/doc/examples/transcode.c b/doc/examples/transcode.c
index 7d1e1826a3..54fb315236 100644
--- a/doc/examples/transcode.c
+++ b/doc/examples/transcode.c
@@ -171,23 +171,38 @@  static int open_output_file(const char *filename)
              * sample rate etc.). These properties can be changed for output
              * streams easily using filters */
             if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+                const enum AVPixelFormat *pix_fmts = NULL;
+
                 enc_ctx->height = dec_ctx->height;
                 enc_ctx->width = dec_ctx->width;
                 enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
+
+                ret = avcodec_get_supported_config(dec_ctx, NULL,
+                                                   AV_CODEC_CONFIG_PIX_FORMAT, 0,
+                                                   (const void**)&pix_fmts, NULL);
+
                 /* take first format from list of supported formats */
-                if (encoder->pix_fmts)
-                    enc_ctx->pix_fmt = encoder->pix_fmts[0];
-                else
-                    enc_ctx->pix_fmt = dec_ctx->pix_fmt;
+                enc_ctx->pix_fmt = (ret >= 0 && pix_fmts) ?
+                                   pix_fmts[0] : dec_ctx->pix_fmt;
+
                 /* video time_base can be set to whatever is handy and supported by encoder */
                 enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
             } else {
+                const enum AVSampleFormat *sample_fmts = NULL;
+
                 enc_ctx->sample_rate = dec_ctx->sample_rate;
                 ret = av_channel_layout_copy(&enc_ctx->ch_layout, &dec_ctx->ch_layout);
                 if (ret < 0)
                     return ret;
+
+                ret = avcodec_get_supported_config(dec_ctx, NULL,
+                                                   AV_CODEC_CONFIG_SAMPLE_FORMAT, 0,
+                                                   (const void**)&sample_fmts, NULL);
+
                 /* take first format from list of supported formats */
-                enc_ctx->sample_fmt = encoder->sample_fmts[0];
+                enc_ctx->sample_fmt = (ret >= 0 && sample_fmts) ?
+                                      sample_fmts[0] : dec_ctx->sample_fmt;
+
                 enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
             }