FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aresample.c
Date: 2024-03-29 01:21:52
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 2166 static av_cold int preinit(AVFilterContext *ctx)
50 {
51 2166 AResampleContext *aresample = ctx->priv;
52
53 2166 aresample->next_pts = AV_NOPTS_VALUE;
54 2166 aresample->swr = swr_alloc();
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2166 times.
2166 if (!aresample->swr)
56 return AVERROR(ENOMEM);
57
58 2166 return 0;
59 }
60
61 2166 static av_cold void uninit(AVFilterContext *ctx)
62 {
63 2166 AResampleContext *aresample = ctx->priv;
64 2166 swr_free(&aresample->swr);
65 2166 }
66
67 1315 static int query_formats(AVFilterContext *ctx)
68 {
69 1315 AResampleContext *aresample = ctx->priv;
70 enum AVSampleFormat out_format;
71 1315 AVChannelLayout out_layout = { 0 };
72 int64_t out_rate;
73
74 1315 AVFilterLink *inlink = ctx->inputs[0];
75 1315 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 676 times.
1315 if (aresample->sample_rate_arg > 0)
83 639 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
84 1315 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
85 1315 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
86
87 1315 in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 if ((ret = ff_formats_ref(in_formats, &inlink->outcfg.formats)) < 0)
89 return ret;
90
91 1315 in_samplerates = ff_all_samplerates();
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 if ((ret = ff_formats_ref(in_samplerates, &inlink->outcfg.samplerates)) < 0)
93 return ret;
94
95 1315 in_layouts = ff_all_channel_counts();
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 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 676 times.
1315 if(out_rate > 0) {
100 639 int ratelist[] = { out_rate, -1 };
101 639 out_samplerates = ff_make_format_list(ratelist);
102 } else {
103 676 out_samplerates = ff_all_samplerates();
104 }
105
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 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 1315 times.
1315 if(out_format != AV_SAMPLE_FMT_NONE) {
110 int formatlist[] = { out_format, -1 };
111 out_formats = ff_make_format_list(formatlist);
112 } else
113 1315 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 if ((ret = ff_formats_ref(out_formats, &outlink->incfg.formats)) < 0)
115 return ret;
116
117 1315 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 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 1315 out_layouts = ff_all_channel_counts();
123 1315 av_channel_layout_uninit(&out_layout);
124
125 1315 return ff_channel_layouts_ref(out_layouts, &outlink->incfg.channel_layouts);
126 }
127
128
129 1315 static int config_output(AVFilterLink *outlink)
130 {
131 int ret;
132 1315 AVFilterContext *ctx = outlink->src;
133 1315 AVFilterLink *inlink = ctx->inputs[0];
134 1315 AResampleContext *aresample = ctx->priv;
135 1315 AVChannelLayout out_layout = { 0 };
136 int64_t out_rate;
137 enum AVSampleFormat out_format;
138 char inchl_buf[128], outchl_buf[128];
139
140 1315 ret = swr_alloc_set_opts2(&aresample->swr,
141 1315 &outlink->ch_layout, outlink->format, outlink->sample_rate,
142 1315 &inlink->ch_layout, inlink->format, inlink->sample_rate,
143 0, ctx);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
1315 if (ret < 0)
145 return ret;
146
147 1315 ret = swr_init(aresample->swr);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
1315 if (ret < 0)
149 return ret;
150
151 1315 av_opt_get_int(aresample->swr, "osr", 0, &out_rate);
152 1315 av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout);
153 1315 av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format);
154 1315 outlink->time_base = (AVRational) {1, out_rate};
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
1315 av_assert0(outlink->sample_rate == out_rate);
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1315 times.
1315 av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout));
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
1315 av_assert0(outlink->format == out_format);
159
160 1315 av_channel_layout_uninit(&out_layout);
161
162 1315 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
163
164 1315 av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf));
165 1315 av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf));
166
167 1315 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
168 1315 inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
169 1315 outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
170 1315 return 0;
171 }
172
173 208798 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
174 {
175 208798 AVFilterContext *ctx = inlink->dst;
176 208798 AResampleContext *aresample = ctx->priv;
177 208798 const int n_in = insamplesref->nb_samples;
178 int64_t delay;
179 208798 int n_out = n_in * aresample->ratio + 32;
180 208798 AVFilterLink *const outlink = inlink->dst->outputs[0];
181 AVFrame *outsamplesref;
182 int ret;
183
184 208798 delay = swr_get_delay(aresample->swr, outlink->sample_rate);
185
2/2
✓ Branch 0 taken 7740 times.
✓ Branch 1 taken 201058 times.
208798 if (delay > 0)
186 7740 n_out += FFMIN(delay, FFMAX(4096, n_out));
187
188 208798 outsamplesref = ff_get_audio_buffer(outlink, n_out);
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208798 times.
208798 if(!outsamplesref) {
191 av_frame_free(&insamplesref);
192 return AVERROR(ENOMEM);
193 }
194
195 208798 av_frame_copy_props(outsamplesref, insamplesref);
196 208798 outsamplesref->format = outlink->format;
197 208798 ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208798 times.
208798 if (ret < 0)
199 return ret;
200 208798 outsamplesref->sample_rate = outlink->sample_rate;
201
202
1/2
✓ Branch 0 taken 208798 times.
✗ Branch 1 not taken.
208798 if(insamplesref->pts != AV_NOPTS_VALUE) {
203 208798 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
204 208798 int64_t outpts= swr_next_pts(aresample->swr, inpts);
205 208798 aresample->next_pts =
206
2/2
✓ Branch 0 taken 208796 times.
✓ Branch 1 taken 2 times.
208798 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
207 } else {
208 outsamplesref->pts = AV_NOPTS_VALUE;
209 }
210 208798 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
211 208798 (void *)insamplesref->extended_data, n_in);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208798 times.
208798 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 208798 aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers
220
221 208798 outsamplesref->nb_samples = n_out;
222
223 208798 ret = ff_filter_frame(outlink, outsamplesref);
224 208798 av_frame_free(&insamplesref);
225 208798 return ret;
226 }
227
228 1966 static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret)
229 {
230 1966 AVFilterContext *ctx = outlink->src;
231 1966 AResampleContext *aresample = ctx->priv;
232 1966 AVFilterLink *const inlink = outlink->src->inputs[0];
233 AVFrame *outsamplesref;
234 1966 int n_out = 4096;
235 int64_t pts;
236
237 1966 outsamplesref = ff_get_audio_buffer(outlink, n_out);
238 1966 *outsamplesref_ret = outsamplesref;
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1966 times.
1966 if (!outsamplesref)
240 return AVERROR(ENOMEM);
241
242 1966 pts = swr_next_pts(aresample->swr, INT64_MIN);
243
1/2
✓ Branch 0 taken 1966 times.
✗ Branch 1 not taken.
1966 pts = ROUNDED_DIV(pts, inlink->sample_rate);
244
245
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1839 times.
1966 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, final ? NULL : (void*)outsamplesref->extended_data, 0);
246
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 758 times.
1966 if (n_out <= 0) {
247 1208 av_frame_free(&outsamplesref);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1208 times.
1208 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 210050 static int request_frame(AVFilterLink *outlink)
260 {
261 210050 AVFilterContext *ctx = outlink->src;
262 210050 AVFilterLink *inlink = ctx->inputs[0];
263 210050 AResampleContext *aresample = ctx->priv;
264 210050 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 209923 times.
210050 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 209927 aresample->more_data = 0;
276
277
4/4
✓ Branch 0 taken 209292 times.
✓ Branch 1 taken 635 times.
✓ Branch 3 taken 1204 times.
✓ Branch 4 taken 208088 times.
209927 if (!aresample->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
278 1204 aresample->eof = 1;
279
280 // Second request more data from the input
281
2/2
✓ Branch 0 taken 208088 times.
✓ Branch 1 taken 1839 times.
209927 if (!aresample->eof)
282
1/2
✓ Branch 1 taken 208088 times.
✗ Branch 2 not taken.
208088 FF_FILTER_FORWARD_WANTED(outlink, inlink);
283
284 // Third if we hit the end flush
285
1/2
✓ Branch 0 taken 1839 times.
✗ Branch 1 not taken.
1839 if (aresample->eof) {
286 AVFrame *outsamplesref;
287
288
2/2
✓ Branch 1 taken 1204 times.
✓ Branch 2 taken 635 times.
1839 if ((ret = flush_frame(outlink, 1, &outsamplesref)) < 0) {
289
1/2
✓ Branch 0 taken 1204 times.
✗ Branch 1 not taken.
1204 if (ret == AVERROR_EOF) {
290 1204 ff_outlink_set_status(outlink, AVERROR_EOF, aresample->next_pts);
291 1204 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 419778 static int activate(AVFilterContext *ctx)
304 {
305 419778 AResampleContext *aresample = ctx->priv;
306 419778 AVFilterLink *inlink = ctx->inputs[0];
307 419778 AVFilterLink *outlink = ctx->outputs[0];
308
309
2/2
✓ Branch 1 taken 930 times.
✓ Branch 2 taken 418848 times.
419778 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
310
311
4/4
✓ Branch 0 taken 418213 times.
✓ Branch 1 taken 635 times.
✓ Branch 3 taken 208798 times.
✓ Branch 4 taken 209415 times.
418848 if (!aresample->eof && ff_inlink_queued_frames(inlink)) {
312 208798 AVFrame *frame = NULL;
313 int ret;
314
315 208798 ret = ff_inlink_consume_frame(inlink, &frame);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208798 times.
208798 if (ret < 0)
317 208798 return ret;
318
1/2
✓ Branch 0 taken 208798 times.
✗ Branch 1 not taken.
208798 if (ret > 0)
319 208798 return filter_frame(inlink, frame);
320 }
321
322 210050 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