3 * This file is part of Libav.
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.
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.
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
22 * sample format and channel layout conversion audio filter
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/common.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
32 #include "libavresample/avresample.h"
39 typedef struct ResampleContext {
41 AVAudioResampleContext *avr;
42 AVDictionary *options;
48 /* set by filter_frame() to signal an output frame to request_frame() */
52 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
54 ResampleContext *s = ctx->priv;
55 const AVClass *avr_class = avresample_get_class();
56 AVDictionaryEntry *e = NULL;
58 while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
59 if (av_opt_find(&avr_class, e->key, NULL, 0,
60 AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
61 av_dict_set(&s->options, e->key, e->value, 0);
65 while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
66 av_dict_set(opts, e->key, NULL, 0);
68 /* do not allow the user to override basic format options */
69 av_dict_set(&s->options, "in_channel_layout", NULL, 0);
70 av_dict_set(&s->options, "out_channel_layout", NULL, 0);
71 av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
72 av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
73 av_dict_set(&s->options, "in_sample_rate", NULL, 0);
74 av_dict_set(&s->options, "out_sample_rate", NULL, 0);
79 static av_cold void uninit(AVFilterContext *ctx)
81 ResampleContext *s = ctx->priv;
84 avresample_close(s->avr);
85 avresample_free(&s->avr);
87 av_dict_free(&s->options);
90 static int query_formats(AVFilterContext *ctx)
92 AVFilterLink *inlink = ctx->inputs[0];
93 AVFilterLink *outlink = ctx->outputs[0];
95 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
96 AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
97 AVFilterFormats *in_samplerates = ff_all_samplerates();
98 AVFilterFormats *out_samplerates = ff_all_samplerates();
99 AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
100 AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
102 ff_formats_ref(in_formats, &inlink->out_formats);
103 ff_formats_ref(out_formats, &outlink->in_formats);
105 ff_formats_ref(in_samplerates, &inlink->out_samplerates);
106 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
108 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
109 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
114 static int config_output(AVFilterLink *outlink)
116 AVFilterContext *ctx = outlink->src;
117 AVFilterLink *inlink = ctx->inputs[0];
118 ResampleContext *s = ctx->priv;
119 char buf1[64], buf2[64];
122 int64_t resampling_forced;
125 avresample_close(s->avr);
126 avresample_free(&s->avr);
129 if (inlink->channel_layout == outlink->channel_layout &&
130 inlink->sample_rate == outlink->sample_rate &&
131 (inlink->format == outlink->format ||
132 (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
133 av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
134 av_get_planar_sample_fmt(inlink->format) ==
135 av_get_planar_sample_fmt(outlink->format))))
138 if (!(s->avr = avresample_alloc_context()))
139 return AVERROR(ENOMEM);
143 AVDictionaryEntry *e = NULL;
144 while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
145 av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
147 ret = av_opt_set_dict(s->avr, &s->options);
152 av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
153 av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
154 av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
155 av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
156 av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
157 av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
159 if ((ret = avresample_open(s->avr)) < 0)
162 av_opt_get_int(s->avr, "force_resampling", 0, &resampling_forced);
163 s->resampling = resampling_forced || (inlink->sample_rate != outlink->sample_rate);
166 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
167 s->next_pts = AV_NOPTS_VALUE;
168 s->next_in_pts = AV_NOPTS_VALUE;
170 outlink->time_base = inlink->time_base;
172 av_get_channel_layout_string(buf1, sizeof(buf1),
173 -1, inlink ->channel_layout);
174 av_get_channel_layout_string(buf2, sizeof(buf2),
175 -1, outlink->channel_layout);
176 av_log(ctx, AV_LOG_VERBOSE,
177 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
178 av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
179 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
184 static int request_frame(AVFilterLink *outlink)
186 AVFilterContext *ctx = outlink->src;
187 ResampleContext *s = ctx->priv;
191 while (ret >= 0 && !s->got_output)
192 ret = ff_request_frame(ctx->inputs[0]);
194 /* flush the lavr delay buffer */
195 if (ret == AVERROR_EOF && s->avr) {
197 int nb_samples = avresample_get_out_samples(s->avr, 0);
202 frame = ff_get_audio_buffer(outlink, nb_samples);
204 return AVERROR(ENOMEM);
206 ret = avresample_convert(s->avr, frame->extended_data,
207 frame->linesize[0], nb_samples,
210 av_frame_free(&frame);
211 return (ret == 0) ? AVERROR_EOF : ret;
214 frame->nb_samples = ret;
215 frame->pts = s->next_pts;
216 return ff_filter_frame(outlink, frame);
221 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
223 AVFilterContext *ctx = inlink->dst;
224 ResampleContext *s = ctx->priv;
225 AVFilterLink *outlink = ctx->outputs[0];
230 int delay, nb_samples;
232 /* maximum possible samples lavr can output */
233 delay = avresample_get_delay(s->avr);
234 nb_samples = avresample_get_out_samples(s->avr, in->nb_samples);
236 out = ff_get_audio_buffer(outlink, nb_samples);
238 ret = AVERROR(ENOMEM);
242 ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
243 nb_samples, in->extended_data, in->linesize[0],
251 av_assert0(!avresample_available(s->avr));
253 if (s->resampling && s->next_pts == AV_NOPTS_VALUE) {
254 if (in->pts == AV_NOPTS_VALUE) {
255 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
259 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
264 out->nb_samples = ret;
266 ret = av_frame_copy_props(out, in);
273 out->sample_rate = outlink->sample_rate;
274 /* Only convert in->pts if there is a discontinuous jump.
275 This ensures that out->pts tracks the number of samples actually
276 output by the resampler in the absence of such a jump.
277 Otherwise, the rounding in av_rescale_q() and av_rescale()
278 causes off-by-1 errors. */
279 if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
280 out->pts = av_rescale_q(in->pts, inlink->time_base,
281 outlink->time_base) -
282 av_rescale(delay, outlink->sample_rate,
283 inlink->sample_rate);
285 out->pts = s->next_pts;
287 s->next_pts = out->pts + out->nb_samples;
288 s->next_in_pts = in->pts + in->nb_samples;
292 ret = ff_filter_frame(outlink, out);
299 in->format = outlink->format;
300 ret = ff_filter_frame(outlink, in);
307 static const AVClass *resample_child_class_next(const AVClass *prev)
309 return prev ? NULL : avresample_get_class();
312 static void *resample_child_next(void *obj, void *prev)
314 ResampleContext *s = obj;
315 return prev ? NULL : s->avr;
318 static const AVClass resample_class = {
319 .class_name = "resample",
320 .item_name = av_default_item_name,
321 .version = LIBAVUTIL_VERSION_INT,
322 .child_class_next = resample_child_class_next,
323 .child_next = resample_child_next,
326 static const AVFilterPad avfilter_af_resample_inputs[] = {
329 .type = AVMEDIA_TYPE_AUDIO,
330 .filter_frame = filter_frame,
335 static const AVFilterPad avfilter_af_resample_outputs[] = {
338 .type = AVMEDIA_TYPE_AUDIO,
339 .config_props = config_output,
340 .request_frame = request_frame
345 AVFilter ff_af_resample = {
347 .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
348 .priv_size = sizeof(ResampleContext),
349 .priv_class = &resample_class,
353 .query_formats = query_formats,
355 .inputs = avfilter_af_resample_inputs,
356 .outputs = avfilter_af_resample_outputs,