Commit | Line | Data |
---|---|---|
d371e7b9 AK |
1 | /* |
2 | * | |
3 | * This file is part of Libav. | |
4 | * | |
5 | * Libav is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * Libav is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with Libav; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | /** | |
21 | * @file | |
22 | * sample format and channel layout conversion audio filter | |
23 | */ | |
24 | ||
25 | #include "libavutil/avassert.h" | |
26 | #include "libavutil/avstring.h" | |
1d9c2dc8 | 27 | #include "libavutil/common.h" |
9f122356 | 28 | #include "libavutil/dict.h" |
d371e7b9 AK |
29 | #include "libavutil/mathematics.h" |
30 | #include "libavutil/opt.h" | |
31 | ||
32 | #include "libavresample/avresample.h" | |
33 | ||
34 | #include "audio.h" | |
35 | #include "avfilter.h" | |
ff1f51a8 | 36 | #include "formats.h" |
d371e7b9 AK |
37 | #include "internal.h" |
38 | ||
39 | typedef struct ResampleContext { | |
4fa1f52e | 40 | const AVClass *class; |
d371e7b9 | 41 | AVAudioResampleContext *avr; |
9f122356 | 42 | AVDictionary *options; |
d371e7b9 AK |
43 | |
44 | int64_t next_pts; | |
1ffb6456 | 45 | |
565e4993 | 46 | /* set by filter_frame() to signal an output frame to request_frame() */ |
1ffb6456 | 47 | int got_output; |
d371e7b9 AK |
48 | } ResampleContext; |
49 | ||
4fa1f52e | 50 | static av_cold int init(AVFilterContext *ctx, AVDictionary **opts) |
9f122356 JR |
51 | { |
52 | ResampleContext *s = ctx->priv; | |
4fa1f52e AK |
53 | const AVClass *avr_class = avresample_get_class(); |
54 | AVDictionaryEntry *e = NULL; | |
9f122356 | 55 | |
4fa1f52e AK |
56 | while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) { |
57 | if (av_opt_find(&avr_class, e->key, NULL, 0, | |
58 | AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN)) | |
59 | av_dict_set(&s->options, e->key, e->value, 0); | |
9f122356 JR |
60 | } |
61 | ||
4fa1f52e AK |
62 | e = NULL; |
63 | while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX))) | |
64 | av_dict_set(opts, e->key, NULL, 0); | |
65 | ||
66 | /* do not allow the user to override basic format options */ | |
67 | av_dict_set(&s->options, "in_channel_layout", NULL, 0); | |
68 | av_dict_set(&s->options, "out_channel_layout", NULL, 0); | |
69 | av_dict_set(&s->options, "in_sample_fmt", NULL, 0); | |
70 | av_dict_set(&s->options, "out_sample_fmt", NULL, 0); | |
71 | av_dict_set(&s->options, "in_sample_rate", NULL, 0); | |
72 | av_dict_set(&s->options, "out_sample_rate", NULL, 0); | |
73 | ||
9f122356 JR |
74 | return 0; |
75 | } | |
76 | ||
d371e7b9 AK |
77 | static av_cold void uninit(AVFilterContext *ctx) |
78 | { | |
79 | ResampleContext *s = ctx->priv; | |
80 | ||
81 | if (s->avr) { | |
82 | avresample_close(s->avr); | |
83 | avresample_free(&s->avr); | |
84 | } | |
9f122356 | 85 | av_dict_free(&s->options); |
d371e7b9 AK |
86 | } |
87 | ||
88 | static int query_formats(AVFilterContext *ctx) | |
89 | { | |
90 | AVFilterLink *inlink = ctx->inputs[0]; | |
91 | AVFilterLink *outlink = ctx->outputs[0]; | |
92 | ||
b74a1da4 AK |
93 | AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); |
94 | AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); | |
ff1f51a8 AK |
95 | AVFilterFormats *in_samplerates = ff_all_samplerates(); |
96 | AVFilterFormats *out_samplerates = ff_all_samplerates(); | |
97 | AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts(); | |
98 | AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts(); | |
d371e7b9 | 99 | |
b74a1da4 AK |
100 | ff_formats_ref(in_formats, &inlink->out_formats); |
101 | ff_formats_ref(out_formats, &outlink->in_formats); | |
d371e7b9 | 102 | |
b74a1da4 AK |
103 | ff_formats_ref(in_samplerates, &inlink->out_samplerates); |
104 | ff_formats_ref(out_samplerates, &outlink->in_samplerates); | |
ff1f51a8 AK |
105 | |
106 | ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts); | |
107 | ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts); | |
108 | ||
d371e7b9 AK |
109 | return 0; |
110 | } | |
111 | ||
112 | static int config_output(AVFilterLink *outlink) | |
113 | { | |
114 | AVFilterContext *ctx = outlink->src; | |
115 | AVFilterLink *inlink = ctx->inputs[0]; | |
116 | ResampleContext *s = ctx->priv; | |
117 | char buf1[64], buf2[64]; | |
118 | int ret; | |
119 | ||
120 | if (s->avr) { | |
121 | avresample_close(s->avr); | |
122 | avresample_free(&s->avr); | |
123 | } | |
124 | ||
125 | if (inlink->channel_layout == outlink->channel_layout && | |
126 | inlink->sample_rate == outlink->sample_rate && | |
7b556be6 JR |
127 | (inlink->format == outlink->format || |
128 | (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 && | |
129 | av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 && | |
130 | av_get_planar_sample_fmt(inlink->format) == | |
131 | av_get_planar_sample_fmt(outlink->format)))) | |
d371e7b9 AK |
132 | return 0; |
133 | ||
134 | if (!(s->avr = avresample_alloc_context())) | |
135 | return AVERROR(ENOMEM); | |
136 | ||
9f122356 JR |
137 | if (s->options) { |
138 | AVDictionaryEntry *e = NULL; | |
139 | while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX))) | |
140 | av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value); | |
141 | ||
142 | av_opt_set_dict(s->avr, &s->options); | |
143 | } | |
144 | ||
d371e7b9 AK |
145 | av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0); |
146 | av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0); | |
147 | av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0); | |
148 | av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0); | |
149 | av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0); | |
150 | av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0); | |
151 | ||
d371e7b9 AK |
152 | if ((ret = avresample_open(s->avr)) < 0) |
153 | return ret; | |
154 | ||
155 | outlink->time_base = (AVRational){ 1, outlink->sample_rate }; | |
156 | s->next_pts = AV_NOPTS_VALUE; | |
157 | ||
158 | av_get_channel_layout_string(buf1, sizeof(buf1), | |
159 | -1, inlink ->channel_layout); | |
160 | av_get_channel_layout_string(buf2, sizeof(buf2), | |
161 | -1, outlink->channel_layout); | |
162 | av_log(ctx, AV_LOG_VERBOSE, | |
e0d8427d | 163 | "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n", |
d371e7b9 AK |
164 | av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1, |
165 | av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2); | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | static int request_frame(AVFilterLink *outlink) | |
171 | { | |
172 | AVFilterContext *ctx = outlink->src; | |
173 | ResampleContext *s = ctx->priv; | |
1ffb6456 AK |
174 | int ret = 0; |
175 | ||
176 | s->got_output = 0; | |
177 | while (ret >= 0 && !s->got_output) | |
178 | ret = ff_request_frame(ctx->inputs[0]); | |
d371e7b9 AK |
179 | |
180 | /* flush the lavr delay buffer */ | |
181 | if (ret == AVERROR_EOF && s->avr) { | |
7e350379 | 182 | AVFrame *frame; |
d371e7b9 AK |
183 | int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr), |
184 | outlink->sample_rate, | |
185 | ctx->inputs[0]->sample_rate, | |
186 | AV_ROUND_UP); | |
187 | ||
188 | if (!nb_samples) | |
189 | return ret; | |
190 | ||
7e350379 AK |
191 | frame = ff_get_audio_buffer(outlink, nb_samples); |
192 | if (!frame) | |
d371e7b9 AK |
193 | return AVERROR(ENOMEM); |
194 | ||
7e350379 AK |
195 | ret = avresample_convert(s->avr, frame->extended_data, |
196 | frame->linesize[0], nb_samples, | |
d371e7b9 AK |
197 | NULL, 0, 0); |
198 | if (ret <= 0) { | |
7e350379 | 199 | av_frame_free(&frame); |
d371e7b9 AK |
200 | return (ret == 0) ? AVERROR_EOF : ret; |
201 | } | |
202 | ||
7e350379 AK |
203 | frame->pts = s->next_pts; |
204 | return ff_filter_frame(outlink, frame); | |
d371e7b9 AK |
205 | } |
206 | return ret; | |
207 | } | |
208 | ||
7e350379 | 209 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
d371e7b9 AK |
210 | { |
211 | AVFilterContext *ctx = inlink->dst; | |
212 | ResampleContext *s = ctx->priv; | |
213 | AVFilterLink *outlink = ctx->outputs[0]; | |
cd991462 | 214 | int ret; |
d371e7b9 AK |
215 | |
216 | if (s->avr) { | |
7e350379 | 217 | AVFrame *out; |
cd991462 | 218 | int delay, nb_samples; |
d371e7b9 AK |
219 | |
220 | /* maximum possible samples lavr can output */ | |
221 | delay = avresample_get_delay(s->avr); | |
7e350379 | 222 | nb_samples = av_rescale_rnd(in->nb_samples + delay, |
d371e7b9 AK |
223 | outlink->sample_rate, inlink->sample_rate, |
224 | AV_ROUND_UP); | |
225 | ||
7e350379 AK |
226 | out = ff_get_audio_buffer(outlink, nb_samples); |
227 | if (!out) { | |
cd991462 AK |
228 | ret = AVERROR(ENOMEM); |
229 | goto fail; | |
230 | } | |
231 | ||
7e350379 AK |
232 | ret = avresample_convert(s->avr, out->extended_data, out->linesize[0], |
233 | nb_samples, in->extended_data, in->linesize[0], | |
234 | in->nb_samples); | |
ac9a8956 | 235 | if (ret <= 0) { |
7e350379 | 236 | av_frame_free(&out); |
ac9a8956 JG |
237 | if (ret < 0) |
238 | goto fail; | |
cd991462 | 239 | } |
d371e7b9 AK |
240 | |
241 | av_assert0(!avresample_available(s->avr)); | |
242 | ||
243 | if (s->next_pts == AV_NOPTS_VALUE) { | |
7e350379 | 244 | if (in->pts == AV_NOPTS_VALUE) { |
d371e7b9 AK |
245 | av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, " |
246 | "assuming 0.\n"); | |
247 | s->next_pts = 0; | |
248 | } else | |
7e350379 | 249 | s->next_pts = av_rescale_q(in->pts, inlink->time_base, |
d371e7b9 AK |
250 | outlink->time_base); |
251 | } | |
252 | ||
253 | if (ret > 0) { | |
7e350379 AK |
254 | out->nb_samples = ret; |
255 | if (in->pts != AV_NOPTS_VALUE) { | |
256 | out->pts = av_rescale_q(in->pts, inlink->time_base, | |
d371e7b9 AK |
257 | outlink->time_base) - |
258 | av_rescale(delay, outlink->sample_rate, | |
259 | inlink->sample_rate); | |
260 | } else | |
7e350379 | 261 | out->pts = s->next_pts; |
d371e7b9 | 262 | |
7e350379 | 263 | s->next_pts = out->pts + out->nb_samples; |
d371e7b9 | 264 | |
7e350379 | 265 | ret = ff_filter_frame(outlink, out); |
1ffb6456 | 266 | s->got_output = 1; |
d371e7b9 | 267 | } |
cd991462 AK |
268 | |
269 | fail: | |
7e350379 | 270 | av_frame_free(&in); |
1ffb6456 | 271 | } else { |
7e350379 AK |
272 | in->format = outlink->format; |
273 | ret = ff_filter_frame(outlink, in); | |
1ffb6456 AK |
274 | s->got_output = 1; |
275 | } | |
cd991462 AK |
276 | |
277 | return ret; | |
d371e7b9 AK |
278 | } |
279 | ||
4fa1f52e AK |
280 | static const AVClass *resample_child_class_next(const AVClass *prev) |
281 | { | |
282 | return prev ? NULL : avresample_get_class(); | |
283 | } | |
284 | ||
285 | static void *resample_child_next(void *obj, void *prev) | |
286 | { | |
287 | ResampleContext *s = obj; | |
288 | return prev ? NULL : s->avr; | |
289 | } | |
290 | ||
291 | static const AVClass resample_class = { | |
292 | .class_name = "resample", | |
293 | .item_name = av_default_item_name, | |
294 | .version = LIBAVUTIL_VERSION_INT, | |
295 | .child_class_next = resample_child_class_next, | |
296 | .child_next = resample_child_next, | |
297 | }; | |
298 | ||
568c70e7 MR |
299 | static const AVFilterPad avfilter_af_resample_inputs[] = { |
300 | { | |
301 | .name = "default", | |
302 | .type = AVMEDIA_TYPE_AUDIO, | |
565e4993 | 303 | .filter_frame = filter_frame, |
568c70e7 MR |
304 | }, |
305 | { NULL } | |
306 | }; | |
307 | ||
308 | static const AVFilterPad avfilter_af_resample_outputs[] = { | |
309 | { | |
310 | .name = "default", | |
311 | .type = AVMEDIA_TYPE_AUDIO, | |
312 | .config_props = config_output, | |
313 | .request_frame = request_frame | |
314 | }, | |
315 | { NULL } | |
316 | }; | |
317 | ||
cd43ca04 | 318 | AVFilter ff_af_resample = { |
d371e7b9 AK |
319 | .name = "resample", |
320 | .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."), | |
321 | .priv_size = sizeof(ResampleContext), | |
4fa1f52e | 322 | .priv_class = &resample_class, |
d371e7b9 | 323 | |
4fa1f52e | 324 | .init_dict = init, |
d371e7b9 AK |
325 | .uninit = uninit, |
326 | .query_formats = query_formats, | |
327 | ||
568c70e7 MR |
328 | .inputs = avfilter_af_resample_inputs, |
329 | .outputs = avfilter_af_resample_outputs, | |
d371e7b9 | 330 | }; |