FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aresample.c
Date: 2025-07-04 04:13:13
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 2342 static av_cold int preinit(AVFilterContext *ctx)
49 {
50 2342 AResampleContext *aresample = ctx->priv;
51
52 2342 aresample->next_pts = AV_NOPTS_VALUE;
53 2342 aresample->swr = swr_alloc();
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2342 times.
2342 if (!aresample->swr)
55 return AVERROR(ENOMEM);
56
57 2342 return 0;
58 }
59
60 2342 static av_cold void uninit(AVFilterContext *ctx)
61 {
62 2342 AResampleContext *aresample = ctx->priv;
63 2342 swr_free(&aresample->swr);
64 2342 }
65
66 1486 static int query_formats(const AVFilterContext *ctx,
67 AVFilterFormatsConfig **cfg_in,
68 AVFilterFormatsConfig **cfg_out)
69 {
70 1486 const AResampleContext *aresample = ctx->priv;
71 enum AVSampleFormat out_format;
72 1486 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 640 times.
✓ Branch 1 taken 846 times.
1486 if (aresample->sample_rate_arg > 0)
81 640 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
82 1486 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
83 1486 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
84
85 1486 in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 if ((ret = ff_formats_ref(in_formats, &cfg_in[0]->formats)) < 0)
87 return ret;
88
89 1486 in_samplerates = ff_all_samplerates();
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 if ((ret = ff_formats_ref(in_samplerates, &cfg_in[0]->samplerates)) < 0)
91 return ret;
92
93 1486 in_layouts = ff_all_channel_counts();
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 if ((ret = ff_channel_layouts_ref(in_layouts, &cfg_in[0]->channel_layouts)) < 0)
95 return ret;
96
97
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 846 times.
1486 if(out_rate > 0) {
98 640 int ratelist[] = { out_rate, -1 };
99 640 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 1486 times.
1486 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 1486 times.
1486 if(out_format != AV_SAMPLE_FMT_NONE) {
108 int formatlist[] = { out_format, -1 };
109 out_formats = ff_make_format_list(formatlist);
110 } else
111 1486 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 if ((ret = ff_formats_ref(out_formats, &cfg_out[0]->formats)) < 0)
113 return ret;
114
115 1486 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 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 1486 out_layouts = ff_all_channel_counts();
121 1486 av_channel_layout_uninit(&out_layout);
122
123 1486 return ff_channel_layouts_ref(out_layouts, &cfg_out[0]->channel_layouts);
124 }
125
126 #define SWR_CH_MAX 64
127
128 1486 static int config_output(AVFilterLink *outlink)
129 {
130 int ret;
131 1486 AVFilterContext *ctx = outlink->src;
132 1486 AVFilterLink *inlink = ctx->inputs[0];
133 1486 AResampleContext *aresample = ctx->priv;
134 1486 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 1486 ret = swr_alloc_set_opts2(&aresample->swr,
141 1486 &outlink->ch_layout, outlink->format, outlink->sample_rate,
142 1486 &inlink->ch_layout, inlink->format, inlink->sample_rate,
143 0, ctx);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1486 times.
1486 if (ret < 0)
145 return ret;
146
147 1486 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 1481 times.
1486 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 1486 ret = swr_init(aresample->swr);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1486 times.
1486 if (ret < 0)
187 return ret;
188
189 1486 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
190 1486 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
191 1486 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
192 1486 outlink->time_base = (AVRational) {1, out_rate};
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1486 times.
1486 av_assert0(outlink->sample_rate == out_rate);
195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
1486 av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout));
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1486 times.
1486 av_assert0(outlink->format == out_format);
197
198 1486 av_channel_layout_uninit(&out_layout);
199
200 1486 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
201
202 1486 av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf));
203 1486 av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf));
204
205 1486 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
206 1486 inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
207 1486 outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
208 1486 return 0;
209 }
210
211 213056 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref, AVFrame **outsamplesref_ret)
212 {
213 213056 AVFilterContext *ctx = inlink->dst;
214 213056 AResampleContext *aresample = ctx->priv;
215 213056 const int n_in = insamplesref->nb_samples;
216 int64_t delay;
217 213056 int n_out = n_in * aresample->ratio + 32;
218 213056 AVFilterLink *const outlink = inlink->dst->outputs[0];
219 AVFrame *outsamplesref;
220 int ret;
221
222 213056 *outsamplesref_ret = NULL;
223 213056 delay = swr_get_delay(aresample->swr, outlink->sample_rate);
224
2/2
✓ Branch 0 taken 7763 times.
✓ Branch 1 taken 205293 times.
213056 if (delay > 0)
225 7763 n_out += FFMIN(delay, FFMAX(4096, n_out));
226
227 213056 outsamplesref = ff_get_audio_buffer(outlink, n_out);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213056 times.
213056 if (!outsamplesref)
229 return AVERROR(ENOMEM);
230
231 213056 av_frame_copy_props(outsamplesref, insamplesref);
232 213056 outsamplesref->format = outlink->format;
233 213056 ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213056 times.
213056 if (ret < 0) {
235 av_frame_free(&outsamplesref);
236 return ret;
237 }
238 213056 outsamplesref->sample_rate = outlink->sample_rate;
239
240
2/2
✓ Branch 1 taken 535 times.
✓ Branch 2 taken 212521 times.
213056 if (av_channel_layout_compare(&outsamplesref->ch_layout, &insamplesref->ch_layout))
241 535 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 213056 times.
✗ Branch 1 not taken.
213056 if(insamplesref->pts != AV_NOPTS_VALUE) {
245 213056 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
246 213056 int64_t outpts= swr_next_pts(aresample->swr, inpts);
247 213056 aresample->next_pts =
248
2/2
✓ Branch 0 taken 213054 times.
✓ Branch 1 taken 2 times.
213056 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
249 } else {
250 outsamplesref->pts = AV_NOPTS_VALUE;
251 }
252 213056 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
253 213056 (void *)insamplesref->extended_data, n_in);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213056 times.
213056 if (n_out <= 0) {
255 av_frame_free(&outsamplesref);
256 return 0;
257 }
258
259 213056 aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers
260
261 213056 outsamplesref->nb_samples = n_out;
262
263 213056 *outsamplesref_ret = outsamplesref;
264 213056 return 1;
265 }
266
267 2037 static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret)
268 {
269 2037 AVFilterContext *ctx = outlink->src;
270 2037 AResampleContext *aresample = ctx->priv;
271 2037 AVFilterLink *const inlink = outlink->src->inputs[0];
272 AVFrame *outsamplesref;
273 2037 int n_out = 4096;
274 int64_t pts;
275
276 2037 outsamplesref = ff_get_audio_buffer(outlink, n_out);
277 2037 *outsamplesref_ret = outsamplesref;
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2037 times.
2037 if (!outsamplesref)
279 return AVERROR(ENOMEM);
280
281 2037 pts = swr_next_pts(aresample->swr, INT64_MIN);
282
1/2
✓ Branch 0 taken 2037 times.
✗ Branch 1 not taken.
2037 pts = ROUNDED_DIV(pts, inlink->sample_rate);
283
284
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1910 times.
2037 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 759 times.
2037 if (n_out <= 0) {
286 1278 av_frame_free(&outsamplesref);
287 1278 return n_out;
288 }
289
290 759 outsamplesref->sample_rate = outlink->sample_rate;
291 759 outsamplesref->nb_samples = n_out;
292
293 759 outsamplesref->pts = pts;
294
295 759 return 1;
296 }
297
298 430485 static int activate(AVFilterContext *ctx)
299 {
300 430485 AVFilterLink *inlink = ctx->inputs[0];
301 430485 AVFilterLink *outlink = ctx->outputs[0];
302 430485 AResampleContext *aresample = ctx->priv;
303 AVFrame *frame;
304 430485 int ret = 0, status;
305 int64_t pts;
306
307
2/2
✓ Branch 1 taken 1098 times.
✓ Branch 2 taken 429387 times.
430485 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 429260 times.
429387 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 429264 aresample->more_data = 0;
320
321 // Then consume frames from inlink
322
2/2
✓ Branch 1 taken 213056 times.
✓ Branch 2 taken 216208 times.
429264 while ((ret = ff_inlink_consume_frame(inlink, &frame))) {
323 AVFrame *outsamplesref;
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213056 times.
213056 if (ret < 0)
325 213056 return ret;
326
327 213056 ret = filter_frame(inlink, frame, &outsamplesref);
328 213056 av_frame_free(&frame);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213056 times.
213056 if (ret < 0)
330 return ret;
331
1/2
✓ Branch 0 taken 213056 times.
✗ Branch 1 not taken.
213056 if (ret > 0)
332 213056 return ff_filter_frame(outlink, outsamplesref);
333 }
334
335 // If we hit the end flush
336
2/2
✓ Branch 1 taken 1910 times.
✓ Branch 2 taken 214298 times.
216208 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
337 AVFrame *outsamplesref;
338
339 1910 ret = flush_frame(outlink, 1, &outsamplesref);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1910 times.
1910 if (ret < 0)
341 return ret;
342
2/2
✓ Branch 0 taken 636 times.
✓ Branch 1 taken 1274 times.
1910 if (ret > 0)
343 636 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 214298 times.
✗ Branch 2 not taken.
214298 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 6689 static void *resample_child_next(void *obj, void *prev)
362 {
363 6689 AResampleContext *s = obj;
364
2/2
✓ Branch 0 taken 5409 times.
✓ Branch 1 taken 1280 times.
6689 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