| Line | Branch | Exec | Source |
|---|---|---|---|
| 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" | ||
| 32 | #include "libavutil/channel_layout.h" | ||
| 33 | #include "libavutil/error.h" | ||
| 34 | #include "libavutil/frame.h" | ||
| 35 | #include "libavutil/opt.h" | ||
| 36 | #include "libavutil/samplefmt.h" | ||
| 37 | |||
| 38 | 25 | static int check_double(AVCodecContext *ctx, const char *name, double expected) | |
| 39 | { | ||
| 40 | 25 | double value = -12345.0; | |
| 41 | 25 | int ret = av_opt_get_double(ctx->priv_data, name, 0, &value); | |
| 42 | |||
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | 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 | 25 | return 0; | |
| 54 | } | ||
| 55 | |||
| 56 | 28 | static int check_int(AVCodecContext *ctx, const char *name, int64_t expected) | |
| 57 | { | ||
| 58 | 28 | int64_t value = -12345; | |
| 59 | 28 | int ret = av_opt_get_int(ctx->priv_data, name, 0, &value); | |
| 60 | |||
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (value != expected) { |
| 67 | ✗ | fprintf(stderr, "%s: %s = %"PRId64", expected %"PRId64"\n", | |
| 68 | ✗ | ctx->codec->name, name, value, expected); | |
| 69 | ✗ | return 1; | |
| 70 | } | ||
| 71 | 28 | return 0; | |
| 72 | } | ||
| 73 | |||
| 74 | 9 | static int check_downmix(AVCodecContext *ctx) | |
| 75 | { | ||
| 76 | 9 | AVChannelLayout layout = { 0 }; | |
| 77 | char buf[64]; | ||
| 78 | 9 | int ret = av_opt_get_chlayout(ctx->priv_data, "downmix", 0, &layout); | |
| 79 | |||
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | 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 | 9 | av_channel_layout_describe(&layout, buf, sizeof(buf)); | |
| 86 | 9 | av_channel_layout_uninit(&layout); | |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (strcmp(buf, "stereo") != 0) { |
| 88 | ✗ | fprintf(stderr, "%s: downmix = %s, expected stereo\n", | |
| 89 | ✗ | ctx->codec->name, buf); | |
| 90 | ✗ | return 1; | |
| 91 | } | ||
| 92 | 9 | return 0; | |
| 93 | } | ||
| 94 | |||
| 95 | 9 | static int check_options(AVCodecContext *ctx, int has_target_level) | |
| 96 | { | ||
| 97 | 9 | int ret = 0; | |
| 98 | |||
| 99 | 9 | ret |= check_double(ctx, "drc_scale", 0.5); | |
| 100 | 9 | ret |= check_int(ctx, "cons_noisegen", 1); | |
| 101 | 9 | ret |= check_int(ctx, "heavy_compr", 1); | |
| 102 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | if (has_target_level) |
| 103 | 6 | ret |= check_int(ctx, "target_level", -20); | |
| 104 | 9 | ret |= check_downmix(ctx); | |
| 105 | 9 | return ret; | |
| 106 | } | ||
| 107 | |||
| 108 | struct ExportValues { | ||
| 109 | int64_t dmix_mode; | ||
| 110 | double ltrt_cmixlev; | ||
| 111 | double ltrt_surmixlev; | ||
| 112 | double loro_cmixlev; | ||
| 113 | double loro_surmixlev; | ||
| 114 | }; | ||
| 115 | |||
| 116 | 3 | static int get_export_values(AVCodecContext *ctx, struct ExportValues *values) | |
| 117 | { | ||
| 118 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if (!av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0)) |
| 119 | 1 | return 0; | |
| 120 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (av_opt_get_int(ctx->priv_data, "dmix_mode", 0, |
| 121 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | &values->dmix_mode) < 0 || |
| 122 | 2 | av_opt_get_double(ctx->priv_data, "ltrt_cmixlev", 0, | |
| 123 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | &values->ltrt_cmixlev) < 0 || |
| 124 | 2 | av_opt_get_double(ctx->priv_data, "ltrt_surmixlev", 0, | |
| 125 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | &values->ltrt_surmixlev) < 0 || |
| 126 | 2 | av_opt_get_double(ctx->priv_data, "loro_cmixlev", 0, | |
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | &values->loro_cmixlev) < 0 || |
| 128 | 2 | av_opt_get_double(ctx->priv_data, "loro_surmixlev", 0, | |
| 129 | &values->loro_surmixlev) < 0) | ||
| 130 | ✗ | return 1; | |
| 131 | 2 | return 0; | |
| 132 | } | ||
| 133 | |||
| 134 | 1 | static int generate_ac3_packet(AVPacket *pkt) | |
| 135 | { | ||
| 136 | 1 | const AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AC3); | |
| 137 | 1 | AVCodecContext *ctx = NULL; | |
| 138 | 1 | AVFrame *frame = NULL; | |
| 139 | 1 | int ret = AVERROR_ENCODER_NOT_FOUND; | |
| 140 | |||
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!codec) |
| 142 | ✗ | return ret; | |
| 143 | |||
| 144 | 1 | ctx = avcodec_alloc_context3(codec); | |
| 145 | 1 | frame = av_frame_alloc(); | |
| 146 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!ctx || !frame) { |
| 147 | ✗ | ret = AVERROR(ENOMEM); | |
| 148 | ✗ | goto end; | |
| 149 | } | ||
| 150 | |||
| 151 | 1 | ctx->sample_rate = 48000; | |
| 152 | 1 | ctx->bit_rate = 128000; | |
| 153 | 1 | ctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
| 154 | 1 | ctx->time_base = (AVRational){ 1, ctx->sample_rate }; | |
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if ((ret = av_channel_layout_copy(&ctx->ch_layout, |
| 156 | 1 | &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0) | |
| 157 | ✗ | goto end; | |
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = avcodec_open2(ctx, codec, NULL)) < 0) |
| 159 | ✗ | goto end; | |
| 160 | |||
| 161 | 1 | frame->nb_samples = ctx->frame_size; | |
| 162 | 1 | frame->format = ctx->sample_fmt; | |
| 163 | 1 | frame->pts = 0; | |
| 164 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = av_channel_layout_copy(&frame->ch_layout, &ctx->ch_layout)) < 0) |
| 165 | ✗ | goto end; | |
| 166 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
2 | if ((ret = av_frame_get_buffer(frame, 0)) < 0 || |
| 167 | 1 | (ret = av_frame_make_writable(frame)) < 0) | |
| 168 | ✗ | goto end; | |
| 169 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (int ch = 0; ch < ctx->ch_layout.nb_channels; ch++) { |
| 170 | 2 | float *samples = (float *)frame->extended_data[ch]; | |
| 171 |
2/2✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 2 times.
|
3074 | for (int i = 0; i < frame->nb_samples; i++) |
| 172 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1536 times.
|
3072 | samples[i] = 0.25f * ((i % 64) / 32.0f - 1.0f) * (ch ? -1.0f : 1.0f); |
| 173 | } | ||
| 174 | |||
| 175 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = avcodec_send_frame(ctx, frame)) < 0) |
| 176 | ✗ | goto end; | |
| 177 | 1 | ret = avcodec_receive_packet(ctx, pkt); | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 179 | ✗ | goto end; | |
| 180 | |||
| 181 | /* Drain the encoder so that closing it stays silent. */ | ||
| 182 | 1 | avcodec_send_frame(ctx, NULL); | |
| 183 | { | ||
| 184 | 1 | AVPacket *tmp = av_packet_alloc(); | |
| 185 | |||
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!tmp) { |
| 187 | ✗ | ret = AVERROR(ENOMEM); | |
| 188 | ✗ | goto end; | |
| 189 | } | ||
| 190 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while (avcodec_receive_packet(ctx, tmp) == 0) |
| 191 | 1 | av_packet_unref(tmp); | |
| 192 | 1 | av_packet_free(&tmp); | |
| 193 | } | ||
| 194 | 1 | ret = 0; | |
| 195 | 1 | end: | |
| 196 | 1 | av_frame_free(&frame); | |
| 197 | 1 | avcodec_free_context(&ctx); | |
| 198 | 1 | return ret; | |
| 199 | } | ||
| 200 | |||
| 201 | 3 | static int decode_one_frame(AVCodecContext *ctx, const AVPacket *src_pkt) | |
| 202 | { | ||
| 203 | 3 | AVPacket *pkt = av_packet_alloc(); | |
| 204 | 3 | AVFrame *frame = av_frame_alloc(); | |
| 205 | int ret; | ||
| 206 | |||
| 207 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!pkt || !frame) { |
| 208 | ✗ | ret = AVERROR(ENOMEM); | |
| 209 | ✗ | goto end; | |
| 210 | } | ||
| 211 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = av_packet_ref(pkt, src_pkt)) < 0) |
| 212 | ✗ | goto end; | |
| 213 | 3 | ret = avcodec_send_packet(ctx, pkt); | |
| 214 | 3 | av_packet_unref(pkt); | |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 216 | ✗ | goto end; | |
| 217 | 3 | ret = avcodec_receive_frame(ctx, frame); | |
| 218 | 3 | end: | |
| 219 | 3 | av_packet_free(&pkt); | |
| 220 | 3 | av_frame_free(&frame); | |
| 221 | 3 | return ret; | |
| 222 | } | ||
| 223 | |||
| 224 | 6 | static int check_export_values(AVCodecContext *ctx, | |
| 225 | const struct ExportValues *expected) | ||
| 226 | { | ||
| 227 | 6 | int ret = 0; | |
| 228 | |||
| 229 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
|
6 | if (!av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0)) |
| 230 | 2 | return 0; | |
| 231 | 4 | ret |= check_int(ctx, "dmix_mode", expected->dmix_mode); | |
| 232 | 4 | ret |= check_double(ctx, "ltrt_cmixlev", expected->ltrt_cmixlev); | |
| 233 | 4 | ret |= check_double(ctx, "ltrt_surmixlev", expected->ltrt_surmixlev); | |
| 234 | 4 | ret |= check_double(ctx, "loro_cmixlev", expected->loro_cmixlev); | |
| 235 | 4 | ret |= check_double(ctx, "loro_surmixlev", expected->loro_surmixlev); | |
| 236 | 4 | return ret; | |
| 237 | } | ||
| 238 | |||
| 239 | 3 | static int run_test(const char *name, const AVPacket *pkt) | |
| 240 | { | ||
| 241 | 3 | const AVCodec *codec = avcodec_find_decoder_by_name(name); | |
| 242 | AVCodecContext *ctx; | ||
| 243 | 3 | struct ExportValues export_values = { 0 }; | |
| 244 | int has_target_level; | ||
| 245 | 3 | int ret = 1; | |
| 246 | |||
| 247 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!codec) |
| 248 | ✗ | return 0; | |
| 249 | |||
| 250 | 3 | ctx = avcodec_alloc_context3(codec); | |
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!ctx) |
| 252 | ✗ | return 1; | |
| 253 | 3 | ctx->pkt_timebase = (AVRational){ 1, 48000 }; | |
| 254 | |||
| 255 | 3 | has_target_level = !!av_opt_find(ctx->priv_data, "target_level", NULL, 0, 0); | |
| 256 | |||
| 257 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
6 | if (av_opt_set_double(ctx->priv_data, "drc_scale", 0.5, 0) < 0 || |
| 258 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | av_opt_set_int(ctx->priv_data, "cons_noisegen", 1, 0) < 0 || |
| 259 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
6 | av_opt_set_int(ctx->priv_data, "heavy_compr", 1, 0) < 0 || |
| 260 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | (has_target_level && |
| 261 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
5 | av_opt_set_int(ctx->priv_data, "target_level", -20, 0) < 0) || |
| 262 | 3 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (avcodec_open2(ctx, codec, NULL) < 0) { |
| 268 | ✗ | fprintf(stderr, "%s: avcodec_open2() failed\n", name); | |
| 269 | ✗ | goto end; | |
| 270 | } | ||
| 271 | |||
| 272 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (decode_one_frame(ctx, pkt) < 0) { |
| 279 | ✗ | fprintf(stderr, "%s: failed to decode a frame\n", name); | |
| 280 | ✗ | goto end; | |
| 281 | } | ||
| 282 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (get_export_values(ctx, &export_values)) |
| 283 | ✗ | goto end; | |
| 284 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (av_opt_find(ctx->priv_data, "dmix_mode", NULL, 0, 0) && |
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | 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 | |||
| 294 | 3 | avcodec_flush_buffers(ctx); | |
| 295 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
6 | if (check_options(ctx, has_target_level) || |
| 296 | 3 | check_export_values(ctx, &export_values)) | |
| 297 | ✗ | goto end; | |
| 298 | |||
| 299 | /* Flushing twice must not change anything either. */ | ||
| 300 | 3 | avcodec_flush_buffers(ctx); | |
| 301 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
6 | if (check_options(ctx, has_target_level) || |
| 302 | 3 | check_export_values(ctx, &export_values)) | |
| 303 | ✗ | goto end; | |
| 304 | |||
| 305 | 3 | printf("%s: options preserved across avcodec_flush_buffers()\n", name); | |
| 306 | 3 | ret = 0; | |
| 307 | 3 | end: | |
| 308 | 3 | avcodec_free_context(&ctx); | |
| 309 | 3 | return ret; | |
| 310 | } | ||
| 311 | |||
| 312 | 1 | int main(void) | |
| 313 | { | ||
| 314 | static const char *const decoder_names[] = { "ac3", "eac3", "ac3_fixed" }; | ||
| 315 | 1 | AVPacket *pkt = av_packet_alloc(); | |
| 316 | 1 | int tested = 0; | |
| 317 | int ret; | ||
| 318 | |||
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!pkt) |
| 320 | ✗ | return 1; | |
| 321 | 1 | ret = generate_ac3_packet(pkt); | |
| 322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 323 | ✗ | fprintf(stderr, "failed to generate an AC-3 packet: %s\n", | |
| 324 | ✗ | av_err2str(ret)); | |
| 325 | ✗ | av_packet_free(&pkt); | |
| 326 | ✗ | return 1; | |
| 327 | } | ||
| 328 | |||
| 329 | 1 | ret = 0; | |
| 330 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (int i = 0; i < (int)(sizeof(decoder_names) / sizeof(decoder_names[0])); i++) { |
| 331 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (!avcodec_find_decoder_by_name(decoder_names[i])) |
| 332 | ✗ | continue; | |
| 333 | 3 | tested++; | |
| 334 | 3 | ret |= run_test(decoder_names[i], pkt); | |
| 335 | } | ||
| 336 | 1 | av_packet_free(&pkt); | |
| 337 | |||
| 338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!tested) { |
| 339 | ✗ | fprintf(stderr, "no (E-)AC-3 decoder available\n"); | |
| 340 | ✗ | return 1; | |
| 341 | } | ||
| 342 | 1 | return ret; | |
| 343 | } | ||
| 344 |