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