FFmpeg
Loading...
Searching...
No Matches
api-ac3-flush-test.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Jun Zhao <barryjzhao@tencent.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Check that avcodec_flush_buffers() preserves the AVOptions of the
23 * AC-3, E-AC-3 and fixed-point AC-3 decoders.
24 */
25
26#include <inttypes.h>
27#include <math.h>
28#include <stdio.h>
29#include <string.h>
30
31#include "libavcodec/avcodec.h"
33#include "libavutil/error.h"
34#include "libavutil/frame.h"
35#include "libavutil/opt.h"
36#include "libavutil/samplefmt.h"
37
38static int check_double(AVCodecContext *ctx, const char *name, double expected)
39{
40 double value = -12345.0;
41 int ret = av_opt_get_double(ctx->priv_data, name, 0, &value);
42
43 if (ret < 0) {
44 fprintf(stderr, "%s: av_opt_get_double(\"%s\") failed: %s\n",
45 ctx->codec->name, name, av_err2str(ret));
46 return 1;
47 }
48 if (fabs(value - expected) > 1e-6) {
49 fprintf(stderr, "%s: %s = %g, expected %g\n",
50 ctx->codec->name, name, value, expected);
51 return 1;
52 }
53 return 0;
54}
55
56static int check_int(AVCodecContext *ctx, const char *name, int64_t expected)
57{
58 int64_t value = -12345;
59 int ret = av_opt_get_int(ctx->priv_data, name, 0, &value);
60
61 if (ret < 0) {
62 fprintf(stderr, "%s: av_opt_get_int(\"%s\") failed: %s\n",
63 ctx->codec->name, name, av_err2str(ret));
64 return 1;
65 }
66 if (value != expected) {
67 fprintf(stderr, "%s: %s = %"PRId64", expected %"PRId64"\n",
68 ctx->codec->name, name, value, expected);
69 return 1;
70 }
71 return 0;
72}
73
75{
76 AVChannelLayout layout = { 0 };
77 char buf[64];
78 int ret = av_opt_get_chlayout(ctx->priv_data, "downmix", 0, &layout);
79
80 if (ret < 0) {
81 fprintf(stderr, "%s: av_opt_get_chlayout(\"downmix\") failed: %s\n",
82 ctx->codec->name, av_err2str(ret));
83 return 1;
84 }
85 av_channel_layout_describe(&layout, buf, sizeof(buf));
87 if (strcmp(buf, "stereo") != 0) {
88 fprintf(stderr, "%s: downmix = %s, expected stereo\n",
89 ctx->codec->name, buf);
90 return 1;
91 }
92 return 0;
93}
94
95static int check_options(AVCodecContext *ctx, int has_target_level)
96{
97 int ret = 0;
98
99 ret |= check_double(ctx, "drc_scale", 0.5);
100 ret |= check_int(ctx, "cons_noisegen", 1);
101 ret |= check_int(ctx, "heavy_compr", 1);
102 if (has_target_level)
103 ret |= check_int(ctx, "target_level", -20);
105 return ret;
106}
107
115
117{
118 if (!av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0))
119 return 0;
120 if (av_opt_get_int(ctx->priv_data, "dmix_mode", 0,
121 &values->dmix_mode) < 0 ||
122 av_opt_get_double(ctx->priv_data, "ltrt_cmixlev", 0,
123 &values->ltrt_cmixlev) < 0 ||
124 av_opt_get_double(ctx->priv_data, "ltrt_surmixlev", 0,
125 &values->ltrt_surmixlev) < 0 ||
126 av_opt_get_double(ctx->priv_data, "loro_cmixlev", 0,
127 &values->loro_cmixlev) < 0 ||
128 av_opt_get_double(ctx->priv_data, "loro_surmixlev", 0,
129 &values->loro_surmixlev) < 0)
130 return 1;
131 return 0;
132}
133
135{
138 AVFrame *frame = NULL;
140
141 if (!codec)
142 return ret;
143
146 if (!ctx || !frame) {
147 ret = AVERROR(ENOMEM);
148 goto end;
149 }
150
151 ctx->sample_rate = 48000;
152 ctx->bit_rate = 128000;
153 ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
154 ctx->time_base = (AVRational){ 1, ctx->sample_rate };
155 if ((ret = av_channel_layout_copy(&ctx->ch_layout,
157 goto end;
158 if ((ret = avcodec_open2(ctx, codec, NULL)) < 0)
159 goto end;
160
161 frame->nb_samples = ctx->frame_size;
162 frame->format = ctx->sample_fmt;
163 frame->pts = 0;
164 if ((ret = av_channel_layout_copy(&frame->ch_layout, &ctx->ch_layout)) < 0)
165 goto end;
166 if ((ret = av_frame_get_buffer(frame, 0)) < 0 ||
168 goto end;
169 for (int ch = 0; ch < ctx->ch_layout.nb_channels; ch++) {
170 float *samples = (float *)frame->extended_data[ch];
171 for (int i = 0; i < frame->nb_samples; i++)
172 samples[i] = 0.25f * ((i % 64) / 32.0f - 1.0f) * (ch ? -1.0f : 1.0f);
173 }
174
175 if ((ret = avcodec_send_frame(ctx, frame)) < 0)
176 goto end;
178 if (ret < 0)
179 goto end;
180
181 /* Drain the encoder so that closing it stays silent. */
183 {
185
186 if (!tmp) {
187 ret = AVERROR(ENOMEM);
188 goto end;
189 }
190 while (avcodec_receive_packet(ctx, tmp) == 0)
193 }
194 ret = 0;
195end:
198 return ret;
199}
200
201static int decode_one_frame(AVCodecContext *ctx, const AVPacket *src_pkt)
202{
205 int ret;
206
207 if (!pkt || !frame) {
208 ret = AVERROR(ENOMEM);
209 goto end;
210 }
211 if ((ret = av_packet_ref(pkt, src_pkt)) < 0)
212 goto end;
215 if (ret < 0)
216 goto end;
218end:
221 return ret;
222}
223
225 const struct ExportValues *expected)
226{
227 int ret = 0;
228
229 if (!av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0))
230 return 0;
231 ret |= check_int(ctx, "dmix_mode", expected->dmix_mode);
232 ret |= check_double(ctx, "ltrt_cmixlev", expected->ltrt_cmixlev);
233 ret |= check_double(ctx, "ltrt_surmixlev", expected->ltrt_surmixlev);
234 ret |= check_double(ctx, "loro_cmixlev", expected->loro_cmixlev);
235 ret |= check_double(ctx, "loro_surmixlev", expected->loro_surmixlev);
236 return ret;
237}
238
239static int run_test(const char *name, const AVPacket *pkt)
240{
243 struct ExportValues export_values = { 0 };
244 int has_target_level;
245 int ret = 1;
246
247 if (!codec)
248 return 0;
249
251 if (!ctx)
252 return 1;
253 ctx->pkt_timebase = (AVRational){ 1, 48000 };
254
255 has_target_level = !!av_opt_find(ctx->priv_data, "target_level", NULL, 0, 0);
256
257 if (av_opt_set_double(ctx->priv_data, "drc_scale", 0.5, 0) < 0 ||
258 av_opt_set_int(ctx->priv_data, "cons_noisegen", 1, 0) < 0 ||
259 av_opt_set_int(ctx->priv_data, "heavy_compr", 1, 0) < 0 ||
260 (has_target_level &&
261 av_opt_set_int(ctx->priv_data, "target_level", -20, 0) < 0) ||
262 av_opt_set(ctx->priv_data, "downmix", "stereo", 0) < 0) {
263 fprintf(stderr, "%s: failed to set decoder options\n", name);
264 goto end;
265 }
266
267 if (avcodec_open2(ctx, codec, NULL) < 0) {
268 fprintf(stderr, "%s: avcodec_open2() failed\n", name);
269 goto end;
270 }
271
272 if (check_options(ctx, has_target_level))
273 goto end;
274
275 /* Decode a frame first so that the exported downmix fields hold
276 * values set from the frame header. Checking them across a flush
277 * is only meaningful once they differ from their initial values. */
278 if (decode_one_frame(ctx, pkt) < 0) {
279 fprintf(stderr, "%s: failed to decode a frame\n", name);
280 goto end;
281 }
282 if (get_export_values(ctx, &export_values))
283 goto end;
284 if (av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0) &&
285 export_values.ltrt_cmixlev == 0.0 &&
286 export_values.ltrt_surmixlev == 0.0 &&
287 export_values.loro_cmixlev == 0.0 &&
288 export_values.loro_surmixlev == 0.0) {
289 fprintf(stderr, "%s: exported downmix fields were not set by decoding\n",
290 name);
291 goto end;
292 }
293
295 if (check_options(ctx, has_target_level) ||
296 check_export_values(ctx, &export_values))
297 goto end;
298
299 /* Flushing twice must not change anything either. */
301 if (check_options(ctx, has_target_level) ||
302 check_export_values(ctx, &export_values))
303 goto end;
304
305 printf("%s: options preserved across avcodec_flush_buffers()\n", name);
306 ret = 0;
307end:
309 return ret;
310}
311
312int main(void)
313{
314 static const char *const decoder_names[] = { "ac3", "eac3", "ac3_fixed" };
316 int tested = 0;
317 int ret;
318
319 if (!pkt)
320 return 1;
322 if (ret < 0) {
323 fprintf(stderr, "failed to generate an AC-3 packet: %s\n",
324 av_err2str(ret));
326 return 1;
327 }
328
329 ret = 0;
330 for (int i = 0; i < (int)(sizeof(decoder_names) / sizeof(decoder_names[0])); i++) {
331 if (!avcodec_find_decoder_by_name(decoder_names[i]))
332 continue;
333 tested++;
334 ret |= run_test(decoder_names[i], pkt);
335 }
337
338 if (!tested) {
339 fprintf(stderr, "no (E-)AC-3 decoder available\n");
340 return 1;
341 }
342 return ret;
343}
static int get_export_values(AVCodecContext *ctx, struct ExportValues *values)
static int run_test(const char *name, const AVPacket *pkt)
static int check_downmix(AVCodecContext *ctx)
static int check_int(AVCodecContext *ctx, const char *name, int64_t expected)
static int decode_one_frame(AVCodecContext *ctx, const AVPacket *src_pkt)
int main(void)
static int check_options(AVCodecContext *ctx, int has_target_level)
static int check_double(AVCodecContext *ctx, const char *name, double expected)
static int check_export_values(AVCodecContext *ctx, const struct ExportValues *expected)
static int generate_ac3_packet(AVPacket *pkt)
static AVFormatContext * ctx
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static __device__ float fabs(float a)
static AVPacket * pkt
static AVFrame * frame
error code definitions
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
reference-counted frame API
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:145
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition allcodecs.c:1026
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition allcodecs.c:993
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
@ AV_CODEC_ID_AC3
Definition codec_id.h:461
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:721
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:738
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition encode.c:588
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition encode.c:555
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition avcodec.c:390
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:443
#define AV_CHANNEL_LAYOUT_STEREO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
Definition error.h:56
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition frame.c:555
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
Definition opt.c:1368
int av_opt_get_chlayout(void *obj, const char *name, int search_flags, AVChannelLayout *cl)
Definition opt.c:1451
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition opt.c:1353
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition opt.c:2075
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
Definition opt.c:947
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition opt.c:942
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition opt.c:895
uint64_t layout
AVOptions.
const char * name
Definition qsvenc.c:142
An AVChannelLayout holds information about the channel layout of audio data.
main external API structure.
Definition avcodec.h:443
AVCodec.
Definition codec.h:175
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
static uint8_t tmp[40]
Definition aes_ctr.c:52