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