FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aresample.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 170 196 86.7%
Functions: 8 9 88.9%
Branches: 59 87 67.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2011 Mina Nagy Zaki
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * resampling audio filter
25 */
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/downmix_info.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/samplefmt.h"
32 #include "libavutil/avassert.h"
33 #include "libswresample/swresample.h"
34 #include "avfilter.h"
35 #include "audio.h"
36 #include "filters.h"
37 #include "formats.h"
38
39 typedef struct AResampleContext {
40 const AVClass *class;
41 int sample_rate_arg;
42 double ratio;
43 struct SwrContext *swr;
44 int64_t next_pts;
45 int more_data;
46 } AResampleContext;
47
48 2340 static av_cold int preinit(AVFilterContext *ctx)
49 {
50 2340 AResampleContext *aresample = ctx->priv;
51
52 2340 aresample->next_pts = AV_NOPTS_VALUE;
53 2340 aresample->swr = swr_alloc();
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2340 times.
2340 if (!aresample->swr)
55 return AVERROR(ENOMEM);
56
57 2340 return 0;
58 }
59
60 2340 static av_cold void uninit(AVFilterContext *ctx)
61 {
62 2340 AResampleContext *aresample = ctx->priv;
63 2340 swr_free(&aresample->swr);
64 2340 }
65
66 1485 static int query_formats(const AVFilterContext *ctx,
67 AVFilterFormatsConfig **cfg_in,
68 AVFilterFormatsConfig **cfg_out)
69 {
70 1485 const AResampleContext *aresample = ctx->priv;
71 enum AVSampleFormat out_format;
72 1485 AVChannelLayout out_layout = { 0 };
73 int64_t out_rate;
74
75 AVFilterFormats *in_formats, *out_formats;
76 AVFilterFormats *in_samplerates, *out_samplerates;
77 AVFilterChannelLayouts *in_layouts, *out_layouts;
78 int ret;
79
80
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 846 times.
1485 if (aresample->sample_rate_arg > 0)
81 639 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
82 1485 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
83 1485 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
84
85 1485 in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if ((ret = ff_formats_ref(in_formats, &cfg_in[0]->formats)) < 0)
87 return ret;
88
89 1485 in_samplerates = ff_all_samplerates();
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if ((ret = ff_formats_ref(in_samplerates, &cfg_in[0]->samplerates)) < 0)
91 return ret;
92
93 1485 in_layouts = ff_all_channel_counts();
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if ((ret = ff_channel_layouts_ref(in_layouts, &cfg_in[0]->channel_layouts)) < 0)
95 return ret;
96
97
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 846 times.
1485 if(out_rate > 0) {
98 639 int ratelist[] = { out_rate, -1 };
99 639 out_samplerates = ff_make_format_list(ratelist);
100 } else {
101 846 out_samplerates = ff_all_samplerates();
102 }
103
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if ((ret = ff_formats_ref(out_samplerates, &cfg_out[0]->samplerates)) < 0)
105 return ret;
106
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
1485 if(out_format != AV_SAMPLE_FMT_NONE) {
108 int formatlist[] = { out_format, -1 };
109 out_formats = ff_make_format_list(formatlist);
110 } else
111 1485 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if ((ret = ff_formats_ref(out_formats, &cfg_out[0]->formats)) < 0)
113 return ret;
114
115 1485 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 if (av_channel_layout_check(&out_layout)) {
117 const AVChannelLayout layout_list[] = { out_layout, { 0 } };
118 out_layouts = ff_make_channel_layout_list(layout_list);
119 } else
120 1485 out_layouts = ff_all_channel_counts();
121 1485 av_channel_layout_uninit(&out_layout);
122
123 1485 return ff_channel_layouts_ref(out_layouts, &cfg_out[0]->channel_layouts);
124 }
125
126 #define SWR_CH_MAX 64
127
128 1485 static int config_output(AVFilterLink *outlink)
129 {
130 int ret;
131 1485 AVFilterContext *ctx = outlink->src;
132 1485 AVFilterLink *inlink = ctx->inputs[0];
133 1485 AResampleContext *aresample = ctx->priv;
134 1485 AVChannelLayout out_layout = { 0 };
135 int64_t out_rate;
136 const AVFrameSideData *sd;
137 enum AVSampleFormat out_format;
138 char inchl_buf[128], outchl_buf[128];
139
140 1485 ret = swr_alloc_set_opts2(&aresample->swr,
141 1485 &outlink->ch_layout, outlink->format, outlink->sample_rate,
142 1485 &inlink->ch_layout, inlink->format, inlink->sample_rate,
143 0, ctx);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
1485 if (ret < 0)
145 return ret;
146
147 1485 sd = av_frame_side_data_get(inlink->side_data, inlink->nb_side_data,
148 AV_FRAME_DATA_DOWNMIX_INFO);
149
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1480 times.
1485 if (sd) {
150 5 const AVDownmixInfo *di = (AVDownmixInfo *)sd->data;
151 5 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
152 double center_mix_level, surround_mix_level;
153
154
3/3
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
5 switch (di->preferred_downmix_type) {
155 1 case AV_DOWNMIX_TYPE_LTRT:
156 1 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
157 1 center_mix_level = di->center_mix_level_ltrt;
158 1 surround_mix_level = di->surround_mix_level_ltrt;
159 1 break;
160 1 case AV_DOWNMIX_TYPE_DPLII:
161 1 matrix_encoding = AV_MATRIX_ENCODING_DPLII;
162 1 center_mix_level = di->center_mix_level_ltrt;
163 1 surround_mix_level = di->surround_mix_level_ltrt;
164 1 break;
165 3 default:
166 3 center_mix_level = di->center_mix_level;
167 3 surround_mix_level = di->surround_mix_level;
168 3 break;
169 }
170
171 5 av_log(ctx, AV_LOG_VERBOSE, "Mix levels: center %f - "
172 "surround %f - lfe %f.\n",
173 5 center_mix_level, surround_mix_level, di->lfe_mix_level);
174
175 5 av_opt_set_double(aresample->swr, "clev", center_mix_level, 0);
176 5 av_opt_set_double(aresample->swr, "slev", surround_mix_level, 0);
177 5 av_opt_set_double(aresample->swr, "lfe_mix_level", di->lfe_mix_level, 0);
178 5 av_opt_set_int(aresample->swr, "matrix_encoding", matrix_encoding, 0);
179
180
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (av_channel_layout_compare(&outlink->ch_layout, &out_layout))
181 5 av_frame_side_data_remove(&outlink->side_data, &outlink->nb_side_data,
182 AV_FRAME_DATA_DOWNMIX_INFO);
183 }
184
185 1485 ret = swr_init(aresample->swr);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
1485 if (ret < 0)
187 return ret;
188
189 1485 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
190 1485 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
191 1485 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
192 1485 outlink->time_base = (AVRational) {1, out_rate};
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
1485 av_assert0(outlink->sample_rate == out_rate);
195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1485 times.
1485 av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout));
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
1485 av_assert0(outlink->format == out_format);
197
198 1485 av_channel_layout_uninit(&out_layout);
199
200 1485 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
201
202 1485 av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf));
203 1485 av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf));
204
205 1485 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
206 1485 inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
207 1485 outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
208 1485 return 0;
209 }
210
211 212994 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref, AVFrame **outsamplesref_ret)
212 {
213 212994 AVFilterContext *ctx = inlink->dst;
214 212994 AResampleContext *aresample = ctx->priv;
215 212994 const int n_in = insamplesref->nb_samples;
216 int64_t delay;
217 212994 int n_out = n_in * aresample->ratio + 32;
218 212994 AVFilterLink *const outlink = inlink->dst->outputs[0];
219 AVFrame *outsamplesref;
220 int ret;
221
222 212994 *outsamplesref_ret = NULL;
223 212994 delay = swr_get_delay(aresample->swr, outlink->sample_rate);
224
2/2
✓ Branch 0 taken 7740 times.
✓ Branch 1 taken 205254 times.
212994 if (delay > 0)
225 7740 n_out += FFMIN(delay, FFMAX(4096, n_out));
226
227 212994 outsamplesref = ff_get_audio_buffer(outlink, n_out);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212994 times.
212994 if (!outsamplesref)
229 return AVERROR(ENOMEM);
230
231 212994 av_frame_copy_props(outsamplesref, insamplesref);
232 212994 outsamplesref->format = outlink->format;
233 212994 ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212994 times.
212994 if (ret < 0) {
235 av_frame_free(&outsamplesref);
236 return ret;
237 }
238 212994 outsamplesref->sample_rate = outlink->sample_rate;
239
240
2/2
✓ Branch 1 taken 534 times.
✓ Branch 2 taken 212460 times.
212994 if (av_channel_layout_compare(&outsamplesref->ch_layout, &insamplesref->ch_layout))
241 534 av_frame_side_data_remove_by_props(&outsamplesref->side_data, &outsamplesref->nb_side_data,
242 AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT);
243
244
1/2
✓ Branch 0 taken 212994 times.
✗ Branch 1 not taken.
212994 if(insamplesref->pts != AV_NOPTS_VALUE) {
245 212994 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
246 212994 int64_t outpts= swr_next_pts(aresample->swr, inpts);
247 212994 aresample->next_pts =
248
2/2
✓ Branch 0 taken 212992 times.
✓ Branch 1 taken 2 times.
212994 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
249 } else {
250 outsamplesref->pts = AV_NOPTS_VALUE;
251 }
252 212994 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
253 212994 (void *)insamplesref->extended_data, n_in);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212994 times.
212994 if (n_out <= 0) {
255 av_frame_free(&outsamplesref);
256 return 0;
257 }
258
259 212994 aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers
260
261 212994 outsamplesref->nb_samples = n_out;
262
263 212994 *outsamplesref_ret = outsamplesref;
264 212994 return 1;
265 }
266
267 2036 static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret)
268 {
269 2036 AVFilterContext *ctx = outlink->src;
270 2036 AResampleContext *aresample = ctx->priv;
271 2036 AVFilterLink *const inlink = outlink->src->inputs[0];
272 AVFrame *outsamplesref;
273 2036 int n_out = 4096;
274 int64_t pts;
275
276 2036 outsamplesref = ff_get_audio_buffer(outlink, n_out);
277 2036 *outsamplesref_ret = outsamplesref;
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2036 times.
2036 if (!outsamplesref)
279 return AVERROR(ENOMEM);
280
281 2036 pts = swr_next_pts(aresample->swr, INT64_MIN);
282
1/2
✓ Branch 0 taken 2036 times.
✗ Branch 1 not taken.
2036 pts = ROUNDED_DIV(pts, inlink->sample_rate);
283
284
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1909 times.
2036 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, final ? NULL : (void*)outsamplesref->extended_data, 0);
285
2/2
✓ Branch 0 taken 1278 times.
✓ Branch 1 taken 758 times.
2036 if (n_out <= 0) {
286 1278 av_frame_free(&outsamplesref);
287 1278 return n_out;
288 }
289
290 758 outsamplesref->sample_rate = outlink->sample_rate;
291 758 outsamplesref->nb_samples = n_out;
292
293 758 outsamplesref->pts = pts;
294
295 758 return 1;
296 }
297
298 428086 static int activate(AVFilterContext *ctx)
299 {
300 428086 AVFilterLink *inlink = ctx->inputs[0];
301 428086 AVFilterLink *outlink = ctx->outputs[0];
302 428086 AResampleContext *aresample = ctx->priv;
303 AVFrame *frame;
304 428086 int ret = 0, status;
305 int64_t pts;
306
307
2/2
✓ Branch 1 taken 1099 times.
✓ Branch 2 taken 426987 times.
428086 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
308
309 // First try to get data from the internal buffers
310
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 426860 times.
426987 if (aresample->more_data) {
311 AVFrame *outsamplesref;
312
313 127 ret = flush_frame(outlink, 0, &outsamplesref);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (ret < 0)
315 123 return ret;
316
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 4 times.
127 if (ret > 0)
317 123 return ff_filter_frame(outlink, outsamplesref);
318 }
319 426864 aresample->more_data = 0;
320
321 // Then consume frames from inlink
322
2/2
✓ Branch 1 taken 212994 times.
✓ Branch 2 taken 213870 times.
426864 while ((ret = ff_inlink_consume_frame(inlink, &frame))) {
323 AVFrame *outsamplesref;
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212994 times.
212994 if (ret < 0)
325 212994 return ret;
326
327 212994 ret = filter_frame(inlink, frame, &outsamplesref);
328 212994 av_frame_free(&frame);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212994 times.
212994 if (ret < 0)
330 return ret;
331
1/2
✓ Branch 0 taken 212994 times.
✗ Branch 1 not taken.
212994 if (ret > 0)
332 212994 return ff_filter_frame(outlink, outsamplesref);
333 }
334
335 // If we hit the end flush
336
2/2
✓ Branch 1 taken 1909 times.
✓ Branch 2 taken 211961 times.
213870 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
337 AVFrame *outsamplesref;
338
339 1909 ret = flush_frame(outlink, 1, &outsamplesref);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1909 times.
1909 if (ret < 0)
341 return ret;
342
2/2
✓ Branch 0 taken 635 times.
✓ Branch 1 taken 1274 times.
1909 if (ret > 0)
343 635 return ff_filter_frame(outlink, outsamplesref);
344 1274 ff_outlink_set_status(outlink, status, aresample->next_pts);
345 1274 return 0;
346 }
347
348 // If not, request more data from the input
349
1/2
✓ Branch 1 taken 211961 times.
✗ Branch 2 not taken.
211961 FF_FILTER_FORWARD_WANTED(outlink, inlink);
350
351 return FFERROR_NOT_READY;
352 }
353
354 static const AVClass *resample_child_class_iterate(void **iter)
355 {
356 const AVClass *c = *iter ? NULL : swr_get_class();
357 *iter = (void*)(uintptr_t)c;
358 return c;
359 }
360
361 6685 static void *resample_child_next(void *obj, void *prev)
362 {
363 6685 AResampleContext *s = obj;
364
2/2
✓ Branch 0 taken 5407 times.
✓ Branch 1 taken 1278 times.
6685 return prev ? NULL : s->swr;
365 }
366
367 #define OFFSET(x) offsetof(AResampleContext, x)
368 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
369
370 static const AVOption options[] = {
371 {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
372 {NULL}
373 };
374
375 static const AVClass aresample_class = {
376 .class_name = "aresample",
377 .item_name = av_default_item_name,
378 .option = options,
379 .version = LIBAVUTIL_VERSION_INT,
380 .child_class_iterate = resample_child_class_iterate,
381 .child_next = resample_child_next,
382 };
383
384 static const AVFilterPad aresample_outputs[] = {
385 {
386 .name = "default",
387 .config_props = config_output,
388 .type = AVMEDIA_TYPE_AUDIO,
389 },
390 };
391
392 const FFFilter ff_af_aresample = {
393 .p.name = "aresample",
394 .p.description = NULL_IF_CONFIG_SMALL("Resample audio data."),
395 .p.priv_class = &aresample_class,
396 .preinit = preinit,
397 .activate = activate,
398 .uninit = uninit,
399 .priv_size = sizeof(AResampleContext),
400 FILTER_INPUTS(ff_audio_default_filterpad),
401 FILTER_OUTPUTS(aresample_outputs),
402 FILTER_QUERY_FUNC2(query_formats),
403 };
404