Commit 0d947ae41f for ffmpeg
commit 0d947ae41f88d9a87157cac22a05c69d40d1e4cf
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Sat Nov 28 10:09:57 2026 -0600
tests/api/enc-info: print requested fields, check libx265 has_b_frames
Let the encoder info test take the AVCodecContext fields to print and
avcodec_open2() options, and handle video encoders, so new checks do
not change existing references. Without a field list it prints the
same line as before.
Use it to check libx265's has_b_frames with several B-frame setups,
including all-intra where x265_encoder_open() turns B-frames off.
diff --git a/tests/api/api-enc-info-test.c b/tests/api/api-enc-info-test.c
index 32bd3ec2bb..c454df96a6 100644
--- a/tests/api/api-enc-info-test.c
+++ b/tests/api/api-enc-info-test.c
@@ -17,38 +17,94 @@
*/
/**
- * Print encoder properties (bit_rate, block_align, bits_per_coded_sample,
- * frame_size) after init.
+ * Print encoder properties after init.
* Useful for verifying that encoders correctly set their codec parameters.
*
- * Usage: encinfo <codec_name> [sample_rate] [channels]
+ * Usage: encinfo <codec_name> [fields [options]]
+ *
+ * fields: ','-separated AVCodecContext option names to print, e.g.
+ * "has_b_frames,frame_size". When empty or missing, prints
+ * bit_rate, block_align, bits_per_coded_sample and frame_size.
+ * options: ','-separated key=value pairs passed to avcodec_open2(),
+ * e.g. "ar=48000,ch_layout=mono".
*/
#include <stdio.h>
-#include <stdlib.h>
#include "libavcodec/avcodec.h"
+#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
+#include "libavutil/dict.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
+static int set_defaults(AVCodecContext *ctx, const AVCodec *codec)
+{
+ const void *fmts;
+ int ret;
+
+ if (codec->type == AVMEDIA_TYPE_VIDEO) {
+ ctx->width = 64;
+ ctx->height = 64;
+ ctx->time_base = (AVRational){ 1, 25 };
+ ret = avcodec_get_supported_config(ctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT, 0, &fmts, NULL);
+ if (ret < 0)
+ return ret;
+ ctx->pix_fmt = fmts ? *(const enum AVPixelFormat *)fmts : AV_PIX_FMT_YUV420P;
+ return 0;
+ }
+
+ ctx->sample_rate = 44100;
+ ret = avcodec_get_supported_config(ctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, &fmts, NULL);
+ if (ret < 0)
+ return ret;
+ ctx->sample_fmt = fmts ? *(const enum AVSampleFormat *)fmts : AV_SAMPLE_FMT_S16;
+ av_channel_layout_default(&ctx->ch_layout, 2);
+ return 0;
+}
+
+static int print_fields(AVCodecContext *ctx, const char *fields)
+{
+ char *list = av_strdup(fields);
+ char *saveptr = NULL;
+ int ret = 0;
+
+ if (!list)
+ return AVERROR(ENOMEM);
+
+ printf("%s", ctx->codec->name);
+ for (char *name = av_strtok(list, ",", &saveptr); name;
+ name = av_strtok(NULL, ",", &saveptr)) {
+ uint8_t *value;
+
+ ret = av_opt_get(ctx, name, 0, &value);
+ if (ret < 0) {
+ fprintf(stderr, "Cannot read field '%s'\n", name);
+ break;
+ }
+ printf(" %s=%s", name, value);
+ av_free(value);
+ }
+ printf("\n");
+
+ av_free(list);
+ return ret;
+}
int main(int argc, char **argv)
{
+ const char *fields = argc >= 3 ? argv[2] : "";
+ const char *options = argc >= 4 ? argv[3] : NULL;
const AVCodec *codec;
- AVCodecContext *ctx;
- int sample_rate = 44100;
- int channels = 2;
+ AVCodecContext *ctx = NULL;
+ AVDictionary *opts = NULL;
int ret;
if (argc < 2) {
- fprintf(stderr, "Usage: %s <codec_name> [sample_rate] [channels]\n",
- argv[0]);
+ fprintf(stderr, "Usage: %s <codec_name> [fields [options]]\n", argv[0]);
return 1;
}
- if (argc >= 3)
- sample_rate = atoi(argv[2]);
- if (argc >= 4)
- channels = atoi(argv[3]);
-
codec = avcodec_find_encoder_by_name(argv[1]);
if (!codec) {
fprintf(stderr, "Encoder '%s' not found\n", argv[1]);
@@ -57,30 +113,43 @@ int main(int argc, char **argv)
ctx = avcodec_alloc_context3(codec);
if (!ctx)
- return AVERROR(ENOMEM);
+ return 1;
- ctx->sample_rate = sample_rate;
- const void *sample_fmts;
- ret = avcodec_get_supported_config(ctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, &sample_fmts, NULL);
+ ret = set_defaults(ctx, codec);
if (ret < 0) {
- fprintf(stderr, "avcodec_get_supported_config failed: %d\n", ret);
- avcodec_free_context(&ctx);
- return 1;
+ fprintf(stderr, "Cannot set encoder defaults: %d\n", ret);
+ goto end;
+ }
+
+ if (options) {
+ ret = av_dict_parse_string(&opts, options, "=", ",", 0);
+ if (ret < 0) {
+ fprintf(stderr, "Cannot parse options '%s': %d\n", options, ret);
+ goto end;
+ }
}
- ctx->sample_fmt = sample_fmts ? *(const enum AVSampleFormat*)sample_fmts : AV_SAMPLE_FMT_S16;
- av_channel_layout_default(&ctx->ch_layout, channels);
- ret = avcodec_open2(ctx, codec, NULL);
+ ret = avcodec_open2(ctx, codec, &opts);
if (ret < 0) {
fprintf(stderr, "avcodec_open2 failed: %d\n", ret);
- avcodec_free_context(&ctx);
- return 1;
+ goto end;
}
- printf("%s bit_rate=%"PRId64" block_align=%d bits_per_coded_sample=%d frame_size=%d\n",
- codec->name, ctx->bit_rate, ctx->block_align,
- ctx->bits_per_coded_sample, ctx->frame_size);
+ if (av_dict_count(opts)) {
+ fprintf(stderr, "Unused option '%s'\n", av_dict_iterate(opts, NULL)->key);
+ ret = AVERROR(EINVAL);
+ goto end;
+ }
+
+ if (*fields)
+ ret = print_fields(ctx, fields);
+ else
+ printf("%s bit_rate=%"PRId64" block_align=%d bits_per_coded_sample=%d frame_size=%d\n",
+ codec->name, ctx->bit_rate, ctx->block_align,
+ ctx->bits_per_coded_sample, ctx->frame_size);
+end:
+ av_dict_free(&opts);
avcodec_free_context(&ctx);
- return 0;
+ return ret < 0;
}
diff --git a/tests/fate/enc_external.mak b/tests/fate/enc_external.mak
index ec6ae83373..f5b8100d06 100644
--- a/tests/fate/enc_external.mak
+++ b/tests/fate/enc_external.mak
@@ -29,3 +29,13 @@ $(FATE_LIBSPEEX-yes): CMD = run_with_temp \
FATE_FFMPEG_FFPROBE += $(FATE_LIBSPEEX-yes)
fate-enc-external: $(FATE_LIBSPEEX-yes)
+
+# has_b_frames must match the B-frame setup x265_encoder_open() settles on
+FATE_LIBX265_ENCINFO = default b-pyramid=0 bframes=1 bframes=0 keyint=1
+FATE_ENC_EXTERNAL_ENCINFO-$(CONFIG_LIBX265_ENCODER) += fate-libx265-encinfo
+fate-libx265-encinfo: $(APITESTSDIR)/api-enc-info-test$(EXESUF)
+fate-libx265-encinfo: CMD = $(foreach p,$(FATE_LIBX265_ENCINFO),echo $(p): && \
+ run $(APITESTSDIR)/api-enc-info-test$(EXESUF) libx265 has_b_frames x265-params=log-level=error:pools=none:$(if $(filter default,$(p)),,$(p)) &&) true
+
+FATE-yes += $(FATE_ENC_EXTERNAL_ENCINFO-yes)
+fate-enc-external: $(FATE_ENC_EXTERNAL_ENCINFO-yes)
diff --git a/tests/ref/fate/libx265-encinfo b/tests/ref/fate/libx265-encinfo
new file mode 100644
index 0000000000..659e50b391
--- /dev/null
+++ b/tests/ref/fate/libx265-encinfo
@@ -0,0 +1,10 @@
+default:
+libx265 has_b_frames=2
+b-pyramid=0:
+libx265 has_b_frames=1
+bframes=1:
+libx265 has_b_frames=2
+bframes=0:
+libx265 has_b_frames=0
+keyint=1:
+libx265 has_b_frames=0