FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aresample.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 132 152 86.8%
Branches: 44 64 68.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/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 "internal.h"
36
37 typedef struct AResampleContext {
38 const AVClass *class;
39 int sample_rate_arg;
40 double ratio;
41 struct SwrContext *swr;
42 int64_t next_pts;
43 int more_data;
44 } AResampleContext;
45
46 1242 static av_cold int preinit(AVFilterContext *ctx)
47 {
48 1242 AResampleContext *aresample = ctx->priv;
49
50 1242 aresample->next_pts = AV_NOPTS_VALUE;
51 1242 aresample->swr = swr_alloc();
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 if (!aresample->swr)
53 return AVERROR(ENOMEM);
54
55 1242 return 0;
56 }
57
58 1242 static av_cold void uninit(AVFilterContext *ctx)
59 {
60 1242 AResampleContext *aresample = ctx->priv;
61 1242 swr_free(&aresample->swr);
62 1242 }
63
64 1242 static int query_formats(AVFilterContext *ctx)
65 {
66 1242 AResampleContext *aresample = ctx->priv;
67 enum AVSampleFormat out_format;
68 1242 AVChannelLayout out_layout = { 0 };
69 int64_t out_rate;
70
71 1242 AVFilterLink *inlink = ctx->inputs[0];
72 1242 AVFilterLink *outlink = ctx->outputs[0];
73
74 AVFilterFormats *in_formats, *out_formats;
75 AVFilterFormats *in_samplerates, *out_samplerates;
76 AVFilterChannelLayouts *in_layouts, *out_layouts;
77 int ret;
78
79
2/2
✓ Branch 0 taken 628 times.
✓ Branch 1 taken 614 times.
1242 if (aresample->sample_rate_arg > 0)
80 628 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
81 1242 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
82 1242 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
83
84 1242 in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = ff_formats_ref(in_formats, &inlink->outcfg.formats)) < 0)
86 return ret;
87
88 1242 in_samplerates = ff_all_samplerates();
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = ff_formats_ref(in_samplerates, &inlink->outcfg.samplerates)) < 0)
90 return ret;
91
92 1242 in_layouts = ff_all_channel_counts();
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = ff_channel_layouts_ref(in_layouts, &inlink->outcfg.channel_layouts)) < 0)
94 return ret;
95
96
2/2
✓ Branch 0 taken 628 times.
✓ Branch 1 taken 614 times.
1242 if(out_rate > 0) {
97 628 int ratelist[] = { out_rate, -1 };
98 628 out_samplerates = ff_make_format_list(ratelist);
99 } else {
100 614 out_samplerates = ff_all_samplerates();
101 }
102
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = ff_formats_ref(out_samplerates, &outlink->incfg.samplerates)) < 0)
104 return ret;
105
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 if(out_format != AV_SAMPLE_FMT_NONE) {
107 int formatlist[] = { out_format, -1 };
108 out_formats = ff_make_format_list(formatlist);
109 } else
110 1242 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = ff_formats_ref(out_formats, &outlink->incfg.formats)) < 0)
112 return ret;
113
114 1242 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if (av_channel_layout_check(&out_layout)) {
116 const AVChannelLayout layout_list[] = { out_layout, { 0 } };
117 out_layouts = ff_make_channel_layout_list(layout_list);
118 } else
119 1242 out_layouts = ff_all_channel_counts();
120 1242 av_channel_layout_uninit(&out_layout);
121
122 1242 return ff_channel_layouts_ref(out_layouts, &outlink->incfg.channel_layouts);
123 }
124
125
126 1242 static int config_output(AVFilterLink *outlink)
127 {
128 int ret;
129 1242 AVFilterContext *ctx = outlink->src;
130 1242 AVFilterLink *inlink = ctx->inputs[0];
131 1242 AResampleContext *aresample = ctx->priv;
132 1242 AVChannelLayout out_layout = { 0 };
133 int64_t out_rate;
134 enum AVSampleFormat out_format;
135 char inchl_buf[128], outchl_buf[128];
136
137 1242 ret = swr_alloc_set_opts2(&aresample->swr,
138 1242 &outlink->ch_layout, outlink->format, outlink->sample_rate,
139 1242 &inlink->ch_layout, inlink->format, inlink->sample_rate,
140 0, ctx);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 if (ret < 0)
142 return ret;
143
144 1242 ret = swr_init(aresample->swr);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 if (ret < 0)
146 return ret;
147
148 1242 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
149 1242 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
150 1242 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
151 1242 outlink->time_base = (AVRational) {1, out_rate};
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 av_assert0(outlink->sample_rate == out_rate);
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout));
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 av_assert0(outlink->format == out_format);
156
157 1242 av_channel_layout_uninit(&out_layout);
158
159 1242 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
160
161 1242 av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf));
162 1242 av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf));
163
164 1242 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
165 1242 inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
166 1242 outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
167 1242 return 0;
168 }
169
170 236920 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
171 {
172 236920 AResampleContext *aresample = inlink->dst->priv;
173 236920 const int n_in = insamplesref->nb_samples;
174 int64_t delay;
175 236920 int n_out = n_in * aresample->ratio + 32;
176 236920 AVFilterLink *const outlink = inlink->dst->outputs[0];
177 AVFrame *outsamplesref;
178 int ret;
179
180 236920 delay = swr_get_delay(aresample->swr, outlink->sample_rate);
181
2/2
✓ Branch 0 taken 4256 times.
✓ Branch 1 taken 232664 times.
236920 if (delay > 0)
182 4256 n_out += FFMIN(delay, FFMAX(4096, n_out));
183
184 236920 outsamplesref = ff_get_audio_buffer(outlink, n_out);
185
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236920 times.
236920 if(!outsamplesref) {
187 av_frame_free(&insamplesref);
188 return AVERROR(ENOMEM);
189 }
190
191 236920 av_frame_copy_props(outsamplesref, insamplesref);
192 236920 outsamplesref->format = outlink->format;
193 #if FF_API_OLD_CHANNEL_LAYOUT
194 FF_DISABLE_DEPRECATION_WARNINGS
195 236920 outsamplesref->channels = outlink->ch_layout.nb_channels;
196 236920 outsamplesref->channel_layout = outlink->channel_layout;
197 FF_ENABLE_DEPRECATION_WARNINGS
198 #endif
199 236920 ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236920 times.
236920 if (ret < 0)
201 return ret;
202 236920 outsamplesref->sample_rate = outlink->sample_rate;
203
204
1/2
✓ Branch 0 taken 236920 times.
✗ Branch 1 not taken.
236920 if(insamplesref->pts != AV_NOPTS_VALUE) {
205 236920 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
206 236920 int64_t outpts= swr_next_pts(aresample->swr, inpts);
207 236920 aresample->next_pts =
208
2/2
✓ Branch 0 taken 236918 times.
✓ Branch 1 taken 2 times.
236920 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
209 } else {
210 outsamplesref->pts = AV_NOPTS_VALUE;
211 }
212 236920 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
213 236920 (void *)insamplesref->extended_data, n_in);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236920 times.
236920 if (n_out <= 0) {
215 av_frame_free(&outsamplesref);
216 av_frame_free(&insamplesref);
217 return 0;
218 }
219
220 236920 aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers
221
222 236920 outsamplesref->nb_samples = n_out;
223
224 236920 ret = ff_filter_frame(outlink, outsamplesref);
225 236920 av_frame_free(&insamplesref);
226 236920 return ret;
227 }
228
229 1994 static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret)
230 {
231 1994 AVFilterContext *ctx = outlink->src;
232 1994 AResampleContext *aresample = ctx->priv;
233 1994 AVFilterLink *const inlink = outlink->src->inputs[0];
234 AVFrame *outsamplesref;
235 1994 int n_out = 4096;
236 int64_t pts;
237
238 1994 outsamplesref = ff_get_audio_buffer(outlink, n_out);
239 1994 *outsamplesref_ret = outsamplesref;
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1994 times.
1994 if (!outsamplesref)
241 return AVERROR(ENOMEM);
242
243 1994 pts = swr_next_pts(aresample->swr, INT64_MIN);
244
1/2
✓ Branch 0 taken 1994 times.
✗ Branch 1 not taken.
1994 pts = ROUNDED_DIV(pts, inlink->sample_rate);
245
246
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 1871 times.
1994 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, final ? NULL : (void*)outsamplesref->extended_data, 0);
247
2/2
✓ Branch 0 taken 1232 times.
✓ Branch 1 taken 762 times.
1994 if (n_out <= 0) {
248 1232 av_frame_free(&outsamplesref);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1232 times.
1232 return (n_out == 0) ? AVERROR_EOF : n_out;
250 }
251
252 762 outsamplesref->sample_rate = outlink->sample_rate;
253 762 outsamplesref->nb_samples = n_out;
254
255 762 outsamplesref->pts = pts;
256
257 762 return 0;
258 }
259
260 217111 static int request_frame(AVFilterLink *outlink)
261 {
262 217111 AVFilterContext *ctx = outlink->src;
263 217111 AResampleContext *aresample = ctx->priv;
264 int ret;
265
266 // First try to get data from the internal buffers
267
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 216988 times.
217111 if (aresample->more_data) {
268 AVFrame *outsamplesref;
269
270
2/2
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 2 times.
123 if (flush_frame(outlink, 0, &outsamplesref) >= 0) {
271 121 return ff_filter_frame(outlink, outsamplesref);
272 }
273 }
274 216990 aresample->more_data = 0;
275
276 // Second request more data from the input
277 216990 ret = ff_request_frame(ctx->inputs[0]);
278
279 // Third if we hit the end flush
280
2/2
✓ Branch 0 taken 1871 times.
✓ Branch 1 taken 215119 times.
216990 if (ret == AVERROR_EOF) {
281 AVFrame *outsamplesref;
282
283
2/2
✓ Branch 1 taken 1230 times.
✓ Branch 2 taken 641 times.
1871 if ((ret = flush_frame(outlink, 1, &outsamplesref)) < 0)
284 1230 return ret;
285
286 641 return ff_filter_frame(outlink, outsamplesref);
287 }
288 215119 return ret;
289 }
290
291 3312 static const AVClass *resample_child_class_iterate(void **iter)
292 {
293
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 628 times.
3312 const AVClass *c = *iter ? NULL : swr_get_class();
294 3312 *iter = (void*)(uintptr_t)c;
295 3312 return c;
296 }
297
298 3312 static void *resample_child_next(void *obj, void *prev)
299 {
300 3312 AResampleContext *s = obj;
301
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 628 times.
3312 return prev ? NULL : s->swr;
302 }
303
304 #define OFFSET(x) offsetof(AResampleContext, x)
305 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
306
307 static const AVOption options[] = {
308 {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
309 {NULL}
310 };
311
312 static const AVClass aresample_class = {
313 .class_name = "aresample",
314 .item_name = av_default_item_name,
315 .option = options,
316 .version = LIBAVUTIL_VERSION_INT,
317 .child_class_iterate = resample_child_class_iterate,
318 .child_next = resample_child_next,
319 };
320
321 static const AVFilterPad aresample_inputs[] = {
322 {
323 .name = "default",
324 .type = AVMEDIA_TYPE_AUDIO,
325 .filter_frame = filter_frame,
326 },
327 };
328
329 static const AVFilterPad aresample_outputs[] = {
330 {
331 .name = "default",
332 .config_props = config_output,
333 .request_frame = request_frame,
334 .type = AVMEDIA_TYPE_AUDIO,
335 },
336 };
337
338 const AVFilter ff_af_aresample = {
339 .name = "aresample",
340 .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
341 .preinit = preinit,
342 .uninit = uninit,
343 .priv_size = sizeof(AResampleContext),
344 .priv_class = &aresample_class,
345 FILTER_INPUTS(aresample_inputs),
346 FILTER_OUTPUTS(aresample_outputs),
347 FILTER_QUERY_FUNC(query_formats),
348 };
349