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