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