lavfi: switch to an AVOptions-based system.
[libav.git] / libavfilter / af_resample.c
CommitLineData
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
39typedef struct ResampleContext {
40 AVAudioResampleContext *avr;
9f122356 41 AVDictionary *options;
d371e7b9
AK
42
43 int64_t next_pts;
1ffb6456 44
565e4993 45 /* set by filter_frame() to signal an output frame to request_frame() */
1ffb6456 46 int got_output;
d371e7b9
AK
47} ResampleContext;
48
9f122356
JR
49static av_cold int init(AVFilterContext *ctx, const char *args)
50{
51 ResampleContext *s = ctx->priv;
52
53 if (args) {
54 int ret = av_dict_parse_string(&s->options, args, "=", ":", 0);
55 if (ret < 0) {
56 av_log(ctx, AV_LOG_ERROR, "error setting option string: %s\n", args);
57 return ret;
58 }
59
60 /* do not allow the user to override basic format options */
61 av_dict_set(&s->options, "in_channel_layout", NULL, 0);
62 av_dict_set(&s->options, "out_channel_layout", NULL, 0);
63 av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
64 av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
65 av_dict_set(&s->options, "in_sample_rate", NULL, 0);
66 av_dict_set(&s->options, "out_sample_rate", NULL, 0);
67 }
68
69 return 0;
70}
71
d371e7b9
AK
72static av_cold void uninit(AVFilterContext *ctx)
73{
74 ResampleContext *s = ctx->priv;
75
76 if (s->avr) {
77 avresample_close(s->avr);
78 avresample_free(&s->avr);
79 }
9f122356 80 av_dict_free(&s->options);
d371e7b9
AK
81}
82
83static int query_formats(AVFilterContext *ctx)
84{
85 AVFilterLink *inlink = ctx->inputs[0];
86 AVFilterLink *outlink = ctx->outputs[0];
87
b74a1da4
AK
88 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
89 AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
ff1f51a8
AK
90 AVFilterFormats *in_samplerates = ff_all_samplerates();
91 AVFilterFormats *out_samplerates = ff_all_samplerates();
92 AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
93 AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
d371e7b9 94
b74a1da4
AK
95 ff_formats_ref(in_formats, &inlink->out_formats);
96 ff_formats_ref(out_formats, &outlink->in_formats);
d371e7b9 97
b74a1da4
AK
98 ff_formats_ref(in_samplerates, &inlink->out_samplerates);
99 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
ff1f51a8
AK
100
101 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
102 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
103
d371e7b9
AK
104 return 0;
105}
106
107static int config_output(AVFilterLink *outlink)
108{
109 AVFilterContext *ctx = outlink->src;
110 AVFilterLink *inlink = ctx->inputs[0];
111 ResampleContext *s = ctx->priv;
112 char buf1[64], buf2[64];
113 int ret;
114
115 if (s->avr) {
116 avresample_close(s->avr);
117 avresample_free(&s->avr);
118 }
119
120 if (inlink->channel_layout == outlink->channel_layout &&
121 inlink->sample_rate == outlink->sample_rate &&
7b556be6
JR
122 (inlink->format == outlink->format ||
123 (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
124 av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
125 av_get_planar_sample_fmt(inlink->format) ==
126 av_get_planar_sample_fmt(outlink->format))))
d371e7b9
AK
127 return 0;
128
129 if (!(s->avr = avresample_alloc_context()))
130 return AVERROR(ENOMEM);
131
9f122356
JR
132 if (s->options) {
133 AVDictionaryEntry *e = NULL;
134 while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
135 av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
136
137 av_opt_set_dict(s->avr, &s->options);
138 }
139
d371e7b9
AK
140 av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
141 av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
142 av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
143 av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
144 av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
145 av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
146
d371e7b9
AK
147 if ((ret = avresample_open(s->avr)) < 0)
148 return ret;
149
150 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
151 s->next_pts = AV_NOPTS_VALUE;
152
153 av_get_channel_layout_string(buf1, sizeof(buf1),
154 -1, inlink ->channel_layout);
155 av_get_channel_layout_string(buf2, sizeof(buf2),
156 -1, outlink->channel_layout);
157 av_log(ctx, AV_LOG_VERBOSE,
e0d8427d 158 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
d371e7b9
AK
159 av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
160 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
161
162 return 0;
163}
164
165static int request_frame(AVFilterLink *outlink)
166{
167 AVFilterContext *ctx = outlink->src;
168 ResampleContext *s = ctx->priv;
1ffb6456
AK
169 int ret = 0;
170
171 s->got_output = 0;
172 while (ret >= 0 && !s->got_output)
173 ret = ff_request_frame(ctx->inputs[0]);
d371e7b9
AK
174
175 /* flush the lavr delay buffer */
176 if (ret == AVERROR_EOF && s->avr) {
7e350379 177 AVFrame *frame;
d371e7b9
AK
178 int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
179 outlink->sample_rate,
180 ctx->inputs[0]->sample_rate,
181 AV_ROUND_UP);
182
183 if (!nb_samples)
184 return ret;
185
7e350379
AK
186 frame = ff_get_audio_buffer(outlink, nb_samples);
187 if (!frame)
d371e7b9
AK
188 return AVERROR(ENOMEM);
189
7e350379
AK
190 ret = avresample_convert(s->avr, frame->extended_data,
191 frame->linesize[0], nb_samples,
d371e7b9
AK
192 NULL, 0, 0);
193 if (ret <= 0) {
7e350379 194 av_frame_free(&frame);
d371e7b9
AK
195 return (ret == 0) ? AVERROR_EOF : ret;
196 }
197
7e350379
AK
198 frame->pts = s->next_pts;
199 return ff_filter_frame(outlink, frame);
d371e7b9
AK
200 }
201 return ret;
202}
203
7e350379 204static int filter_frame(AVFilterLink *inlink, AVFrame *in)
d371e7b9
AK
205{
206 AVFilterContext *ctx = inlink->dst;
207 ResampleContext *s = ctx->priv;
208 AVFilterLink *outlink = ctx->outputs[0];
cd991462 209 int ret;
d371e7b9
AK
210
211 if (s->avr) {
7e350379 212 AVFrame *out;
cd991462 213 int delay, nb_samples;
d371e7b9
AK
214
215 /* maximum possible samples lavr can output */
216 delay = avresample_get_delay(s->avr);
7e350379 217 nb_samples = av_rescale_rnd(in->nb_samples + delay,
d371e7b9
AK
218 outlink->sample_rate, inlink->sample_rate,
219 AV_ROUND_UP);
220
7e350379
AK
221 out = ff_get_audio_buffer(outlink, nb_samples);
222 if (!out) {
cd991462
AK
223 ret = AVERROR(ENOMEM);
224 goto fail;
225 }
226
7e350379
AK
227 ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
228 nb_samples, in->extended_data, in->linesize[0],
229 in->nb_samples);
ac9a8956 230 if (ret <= 0) {
7e350379 231 av_frame_free(&out);
ac9a8956
JG
232 if (ret < 0)
233 goto fail;
cd991462 234 }
d371e7b9
AK
235
236 av_assert0(!avresample_available(s->avr));
237
238 if (s->next_pts == AV_NOPTS_VALUE) {
7e350379 239 if (in->pts == AV_NOPTS_VALUE) {
d371e7b9
AK
240 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
241 "assuming 0.\n");
242 s->next_pts = 0;
243 } else
7e350379 244 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
d371e7b9
AK
245 outlink->time_base);
246 }
247
248 if (ret > 0) {
7e350379
AK
249 out->nb_samples = ret;
250 if (in->pts != AV_NOPTS_VALUE) {
251 out->pts = av_rescale_q(in->pts, inlink->time_base,
d371e7b9
AK
252 outlink->time_base) -
253 av_rescale(delay, outlink->sample_rate,
254 inlink->sample_rate);
255 } else
7e350379 256 out->pts = s->next_pts;
d371e7b9 257
7e350379 258 s->next_pts = out->pts + out->nb_samples;
d371e7b9 259
7e350379 260 ret = ff_filter_frame(outlink, out);
1ffb6456 261 s->got_output = 1;
d371e7b9 262 }
cd991462
AK
263
264fail:
7e350379 265 av_frame_free(&in);
1ffb6456 266 } else {
7e350379
AK
267 in->format = outlink->format;
268 ret = ff_filter_frame(outlink, in);
1ffb6456
AK
269 s->got_output = 1;
270 }
cd991462
AK
271
272 return ret;
d371e7b9
AK
273}
274
568c70e7
MR
275static const AVFilterPad avfilter_af_resample_inputs[] = {
276 {
277 .name = "default",
278 .type = AVMEDIA_TYPE_AUDIO,
565e4993 279 .filter_frame = filter_frame,
568c70e7
MR
280 },
281 { NULL }
282};
283
284static const AVFilterPad avfilter_af_resample_outputs[] = {
285 {
286 .name = "default",
287 .type = AVMEDIA_TYPE_AUDIO,
288 .config_props = config_output,
289 .request_frame = request_frame
290 },
291 { NULL }
292};
293
d371e7b9
AK
294AVFilter avfilter_af_resample = {
295 .name = "resample",
296 .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
297 .priv_size = sizeof(ResampleContext),
298
9f122356 299 .init = init,
d371e7b9
AK
300 .uninit = uninit,
301 .query_formats = query_formats,
302
568c70e7
MR
303 .inputs = avfilter_af_resample_inputs,
304 .outputs = avfilter_af_resample_outputs,
d371e7b9 305};