FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aresample.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 146 174 83.9%
Functions: 9 10 90.0%
Branches: 57 84 67.9%

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/opt.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/avassert.h"
32 #include "libswresample/swresample.h"
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "filters.h"
36 #include "formats.h"
37 #include "internal.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 int eof;
47 } AResampleContext;
48
49 2168 static av_cold int preinit(AVFilterContext *ctx)
50 {
51 2168 AResampleContext *aresample = ctx->priv;
52
53 2168 aresample->next_pts = AV_NOPTS_VALUE;
54 2168 aresample->swr = swr_alloc();
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2168 times.
2168 if (!aresample->swr)
56 return AVERROR(ENOMEM);
57
58 2168 return 0;
59 }
60
61 2168 static av_cold void uninit(AVFilterContext *ctx)
62 {
63 2168 AResampleContext *aresample = ctx->priv;
64 2168 swr_free(&aresample->swr);
65 2168 }
66
67 1316 static int query_formats(AVFilterContext *ctx)
68 {
69 1316 AResampleContext *aresample = ctx->priv;
70 enum AVSampleFormat out_format;
71 1316 AVChannelLayout out_layout = { 0 };
72 int64_t out_rate;
73
74 1316 AVFilterLink *inlink = ctx->inputs[0];
75 1316 AVFilterLink *outlink = ctx->outputs[0];
76
77 AVFilterFormats *in_formats, *out_formats;
78 AVFilterFormats *in_samplerates, *out_samplerates;
79 AVFilterChannelLayouts *in_layouts, *out_layouts;
80 int ret;
81
82
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 677 times.
1316 if (aresample->sample_rate_arg > 0)
83 639 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
84 1316 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
85 1316 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
86
87 1316 in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if ((ret = ff_formats_ref(in_formats, &inlink->outcfg.formats)) < 0)
89 return ret;
90
91 1316 in_samplerates = ff_all_samplerates();
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if ((ret = ff_formats_ref(in_samplerates, &inlink->outcfg.samplerates)) < 0)
93 return ret;
94
95 1316 in_layouts = ff_all_channel_counts();
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if ((ret = ff_channel_layouts_ref(in_layouts, &inlink->outcfg.channel_layouts)) < 0)
97 return ret;
98
99
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 677 times.
1316 if(out_rate > 0) {
100 639 int ratelist[] = { out_rate, -1 };
101 639 out_samplerates = ff_make_format_list(ratelist);
102 } else {
103 677 out_samplerates = ff_all_samplerates();
104 }
105
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if ((ret = ff_formats_ref(out_samplerates, &outlink->incfg.samplerates)) < 0)
107 return ret;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
1316 if(out_format != AV_SAMPLE_FMT_NONE) {
110 int formatlist[] = { out_format, -1 };
111 out_formats = ff_make_format_list(formatlist);
112 } else
113 1316 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if ((ret = ff_formats_ref(out_formats, &outlink->incfg.formats)) < 0)
115 return ret;
116
117 1316 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 if (av_channel_layout_check(&out_layout)) {
119 const AVChannelLayout layout_list[] = { out_layout, { 0 } };
120 out_layouts = ff_make_channel_layout_list(layout_list);
121 } else
122 1316 out_layouts = ff_all_channel_counts();
123 1316 av_channel_layout_uninit(&out_layout);
124
125 1316 return ff_channel_layouts_ref(out_layouts, &outlink->incfg.channel_layouts);
126 }
127
128
129 1316 static int config_output(AVFilterLink *outlink)
130 {
131 int ret;
132 1316 AVFilterContext *ctx = outlink->src;
133 1316 AVFilterLink *inlink = ctx->inputs[0];
134 1316 AResampleContext *aresample = ctx->priv;
135 1316 AVChannelLayout out_layout = { 0 };
136 int64_t out_rate;
137 enum AVSampleFormat out_format;
138 char inchl_buf[128], outchl_buf[128];
139
140 1316 ret = swr_alloc_set_opts2(&aresample->swr,
141 1316 &outlink->ch_layout, outlink->format, outlink->sample_rate,
142 1316 &inlink->ch_layout, inlink->format, inlink->sample_rate,
143 0, ctx);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
1316 if (ret < 0)
145 return ret;
146
147 1316 ret = swr_init(aresample->swr);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
1316 if (ret < 0)
149 return ret;
150
151 1316 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
152 1316 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
153 1316 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
154 1316 outlink->time_base = (AVRational) {1, out_rate};
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
1316 av_assert0(outlink->sample_rate == out_rate);
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1316 times.
1316 av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout));
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
1316 av_assert0(outlink->format == out_format);
159
160 1316 av_channel_layout_uninit(&out_layout);
161
162 1316 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
163
164 1316 av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf));
165 1316 av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf));
166
167 1316 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
168 1316 inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
169 1316 outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
170 1316 return 0;
171 }
172
173 208876 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
174 {
175 208876 AVFilterContext *ctx = inlink->dst;
176 208876 AResampleContext *aresample = ctx->priv;
177 208876 const int n_in = insamplesref->nb_samples;
178 int64_t delay;
179 208876 int n_out = n_in * aresample->ratio + 32;
180 208876 AVFilterLink *const outlink = inlink->dst->outputs[0];
181 AVFrame *outsamplesref;
182 int ret;
183
184 208876 delay = swr_get_delay(aresample->swr, outlink->sample_rate);
185
2/2
✓ Branch 0 taken 7740 times.
✓ Branch 1 taken 201136 times.
208876 if (delay > 0)
186 7740 n_out += FFMIN(delay, FFMAX(4096, n_out));
187
188 208876 outsamplesref = ff_get_audio_buffer(outlink, n_out);
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208876 times.
208876 if(!outsamplesref) {
191 av_frame_free(&insamplesref);
192 return AVERROR(ENOMEM);
193 }
194
195 208876 av_frame_copy_props(outsamplesref, insamplesref);
196 208876 outsamplesref->format = outlink->format;
197 208876 ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208876 times.
208876 if (ret < 0)
199 return ret;
200 208876 outsamplesref->sample_rate = outlink->sample_rate;
201
202
1/2
✓ Branch 0 taken 208876 times.
✗ Branch 1 not taken.
208876 if(insamplesref->pts != AV_NOPTS_VALUE) {
203 208876 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
204 208876 int64_t outpts= swr_next_pts(aresample->swr, inpts);
205 208876 aresample->next_pts =
206
2/2
✓ Branch 0 taken 208874 times.
✓ Branch 1 taken 2 times.
208876 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
207 } else {
208 outsamplesref->pts = AV_NOPTS_VALUE;
209 }
210 208876 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
211 208876 (void *)insamplesref->extended_data, n_in);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208876 times.
208876 if (n_out <= 0) {
213 av_frame_free(&outsamplesref);
214 av_frame_free(&insamplesref);
215 ff_inlink_request_frame(inlink);
216 return 0;
217 }
218
219 208876 aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers
220
221 208876 outsamplesref->nb_samples = n_out;
222
223 208876 ret = ff_filter_frame(outlink, outsamplesref);
224 208876 av_frame_free(&insamplesref);
225 208876 return ret;
226 }
227
228 1967 static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret)
229 {
230 1967 AVFilterContext *ctx = outlink->src;
231 1967 AResampleContext *aresample = ctx->priv;
232 1967 AVFilterLink *const inlink = outlink->src->inputs[0];
233 AVFrame *outsamplesref;
234 1967 int n_out = 4096;
235 int64_t pts;
236
237 1967 outsamplesref = ff_get_audio_buffer(outlink, n_out);
238 1967 *outsamplesref_ret = outsamplesref;
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1967 times.
1967 if (!outsamplesref)
240 return AVERROR(ENOMEM);
241
242 1967 pts = swr_next_pts(aresample->swr, INT64_MIN);
243
1/2
✓ Branch 0 taken 1967 times.
✗ Branch 1 not taken.
1967 pts = ROUNDED_DIV(pts, inlink->sample_rate);
244
245
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1840 times.
1967 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, final ? NULL : (void*)outsamplesref->extended_data, 0);
246
2/2
✓ Branch 0 taken 1209 times.
✓ Branch 1 taken 758 times.
1967 if (n_out <= 0) {
247 1209 av_frame_free(&outsamplesref);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
1209 return (n_out == 0) ? AVERROR_EOF : n_out;
249 }
250
251 758 outsamplesref->sample_rate = outlink->sample_rate;
252 758 outsamplesref->nb_samples = n_out;
253
254 758 outsamplesref->pts = pts;
255
256 758 return 0;
257 }
258
259 210168 static int request_frame(AVFilterLink *outlink)
260 {
261 210168 AVFilterContext *ctx = outlink->src;
262 210168 AVFilterLink *inlink = ctx->inputs[0];
263 210168 AResampleContext *aresample = ctx->priv;
264 210168 int ret = 0, status;
265 int64_t pts;
266
267 // First try to get data from the internal buffers
268
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 210041 times.
210168 if (aresample->more_data) {
269 AVFrame *outsamplesref;
270
271
2/2
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 4 times.
127 if (flush_frame(outlink, 0, &outsamplesref) >= 0) {
272 123 return ff_filter_frame(outlink, outsamplesref);
273 }
274 }
275 210045 aresample->more_data = 0;
276
277
4/4
✓ Branch 0 taken 209410 times.
✓ Branch 1 taken 635 times.
✓ Branch 3 taken 1205 times.
✓ Branch 4 taken 208205 times.
210045 if (!aresample->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
278 1205 aresample->eof = 1;
279
280 // Second request more data from the input
281
2/2
✓ Branch 0 taken 208205 times.
✓ Branch 1 taken 1840 times.
210045 if (!aresample->eof)
282
1/2
✓ Branch 1 taken 208205 times.
✗ Branch 2 not taken.
208205 FF_FILTER_FORWARD_WANTED(outlink, inlink);
283
284 // Third if we hit the end flush
285
1/2
✓ Branch 0 taken 1840 times.
✗ Branch 1 not taken.
1840 if (aresample->eof) {
286 AVFrame *outsamplesref;
287
288
2/2
✓ Branch 1 taken 1205 times.
✓ Branch 2 taken 635 times.
1840 if ((ret = flush_frame(outlink, 1, &outsamplesref)) < 0) {
289
1/2
✓ Branch 0 taken 1205 times.
✗ Branch 1 not taken.
1205 if (ret == AVERROR_EOF) {
290 1205 ff_outlink_set_status(outlink, AVERROR_EOF, aresample->next_pts);
291 1205 return 0;
292 }
293 return ret;
294 }
295
296 635 return ff_filter_frame(outlink, outsamplesref);
297 }
298
299 ff_filter_set_ready(ctx, 100);
300 return 0;
301 }
302
303 419975 static int activate(AVFilterContext *ctx)
304 {
305 419975 AResampleContext *aresample = ctx->priv;
306 419975 AVFilterLink *inlink = ctx->inputs[0];
307 419975 AVFilterLink *outlink = ctx->outputs[0];
308
309
2/2
✓ Branch 1 taken 931 times.
✓ Branch 2 taken 419044 times.
419975 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
310
311
4/4
✓ Branch 0 taken 418409 times.
✓ Branch 1 taken 635 times.
✓ Branch 3 taken 208876 times.
✓ Branch 4 taken 209533 times.
419044 if (!aresample->eof && ff_inlink_queued_frames(inlink)) {
312 208876 AVFrame *frame = NULL;
313 int ret;
314
315 208876 ret = ff_inlink_consume_frame(inlink, &frame);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208876 times.
208876 if (ret < 0)
317 208876 return ret;
318
1/2
✓ Branch 0 taken 208876 times.
✗ Branch 1 not taken.
208876 if (ret > 0)
319 208876 return filter_frame(inlink, frame);
320 }
321
322 210168 return request_frame(outlink);
323 }
324
325 static const AVClass *resample_child_class_iterate(void **iter)
326 {
327 const AVClass *c = *iter ? NULL : swr_get_class();
328 *iter = (void*)(uintptr_t)c;
329 return c;
330 }
331
332 6685 static void *resample_child_next(void *obj, void *prev)
333 {
334 6685 AResampleContext *s = obj;
335
2/2
✓ Branch 0 taken 5407 times.
✓ Branch 1 taken 1278 times.
6685 return prev ? NULL : s->swr;
336 }
337
338 #define OFFSET(x) offsetof(AResampleContext, x)
339 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
340
341 static const AVOption options[] = {
342 {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
343 {NULL}
344 };
345
346 static const AVClass aresample_class = {
347 .class_name = "aresample",
348 .item_name = av_default_item_name,
349 .option = options,
350 .version = LIBAVUTIL_VERSION_INT,
351 .child_class_iterate = resample_child_class_iterate,
352 .child_next = resample_child_next,
353 };
354
355 static const AVFilterPad aresample_outputs[] = {
356 {
357 .name = "default",
358 .config_props = config_output,
359 .type = AVMEDIA_TYPE_AUDIO,
360 },
361 };
362
363 const AVFilter ff_af_aresample = {
364 .name = "aresample",
365 .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
366 .preinit = preinit,
367 .activate = activate,
368 .uninit = uninit,
369 .priv_size = sizeof(AResampleContext),
370 .priv_class = &aresample_class,
371 FILTER_INPUTS(ff_audio_default_filterpad),
372 FILTER_OUTPUTS(aresample_outputs),
373 FILTER_QUERY_FUNC(query_formats),
374 };
375