]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_resample.c
vocdec: do not create the stream in read_header()
[ffmpeg] / libavfilter / af_resample.c
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"
27 #include "libavutil/common.h"
28 #include "libavutil/dict.h"
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"
36 #include "formats.h"
37 #include "internal.h"
38
39 typedef struct ResampleContext {
40     const AVClass *class;
41     AVAudioResampleContext *avr;
42     AVDictionary *options;
43
44     int resampling;
45     int64_t next_pts;
46     int64_t next_in_pts;
47
48     /* set by filter_frame() to signal an output frame to request_frame() */
49     int got_output;
50 } ResampleContext;
51
52 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
53 {
54     ResampleContext *s = ctx->priv;
55     const AVClass *avr_class = avresample_get_class();
56     AVDictionaryEntry *e = NULL;
57
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);
62     }
63
64     e = NULL;
65     while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
66         av_dict_set(opts, e->key, NULL, 0);
67
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);
75
76     return 0;
77 }
78
79 static av_cold void uninit(AVFilterContext *ctx)
80 {
81     ResampleContext *s = ctx->priv;
82
83     if (s->avr) {
84         avresample_close(s->avr);
85         avresample_free(&s->avr);
86     }
87     av_dict_free(&s->options);
88 }
89
90 static int query_formats(AVFilterContext *ctx)
91 {
92     AVFilterLink *inlink  = ctx->inputs[0];
93     AVFilterLink *outlink = ctx->outputs[0];
94
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();
101
102     ff_formats_ref(in_formats,  &inlink->out_formats);
103     ff_formats_ref(out_formats, &outlink->in_formats);
104
105     ff_formats_ref(in_samplerates,  &inlink->out_samplerates);
106     ff_formats_ref(out_samplerates, &outlink->in_samplerates);
107
108     ff_channel_layouts_ref(in_layouts,  &inlink->out_channel_layouts);
109     ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
110
111     return 0;
112 }
113
114 static int config_output(AVFilterLink *outlink)
115 {
116     AVFilterContext *ctx = outlink->src;
117     AVFilterLink *inlink = ctx->inputs[0];
118     ResampleContext   *s = ctx->priv;
119     char buf1[64], buf2[64];
120     int ret;
121
122     int64_t resampling_forced;
123
124     if (s->avr) {
125         avresample_close(s->avr);
126         avresample_free(&s->avr);
127     }
128
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))))
136         return 0;
137
138     if (!(s->avr = avresample_alloc_context()))
139         return AVERROR(ENOMEM);
140
141     if (s->options) {
142         int ret;
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);
146
147         ret = av_opt_set_dict(s->avr, &s->options);
148         if (ret < 0)
149             return ret;
150     }
151
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);
158
159     if ((ret = avresample_open(s->avr)) < 0)
160         return ret;
161
162     av_opt_get_int(s->avr, "force_resampling", 0, &resampling_forced);
163     s->resampling = resampling_forced || (inlink->sample_rate != outlink->sample_rate);
164
165     if (s->resampling) {
166         outlink->time_base = (AVRational){ 1, outlink->sample_rate };
167         s->next_pts        = AV_NOPTS_VALUE;
168         s->next_in_pts     = AV_NOPTS_VALUE;
169     } else
170         outlink->time_base = inlink->time_base;
171
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);
180
181     return 0;
182 }
183
184 static int request_frame(AVFilterLink *outlink)
185 {
186     AVFilterContext *ctx = outlink->src;
187     ResampleContext   *s = ctx->priv;
188     int ret = 0;
189
190     s->got_output = 0;
191     while (ret >= 0 && !s->got_output)
192         ret = ff_request_frame(ctx->inputs[0]);
193
194     /* flush the lavr delay buffer */
195     if (ret == AVERROR_EOF && s->avr) {
196         AVFrame *frame;
197         int nb_samples = avresample_get_out_samples(s->avr, 0);
198
199         if (!nb_samples)
200             return ret;
201
202         frame = ff_get_audio_buffer(outlink, nb_samples);
203         if (!frame)
204             return AVERROR(ENOMEM);
205
206         ret = avresample_convert(s->avr, frame->extended_data,
207                                  frame->linesize[0], nb_samples,
208                                  NULL, 0, 0);
209         if (ret <= 0) {
210             av_frame_free(&frame);
211             return (ret == 0) ? AVERROR_EOF : ret;
212         }
213
214         frame->nb_samples = ret;
215         frame->pts = s->next_pts;
216         return ff_filter_frame(outlink, frame);
217     }
218     return ret;
219 }
220
221 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
222 {
223     AVFilterContext  *ctx = inlink->dst;
224     ResampleContext    *s = ctx->priv;
225     AVFilterLink *outlink = ctx->outputs[0];
226     int ret;
227
228     if (s->avr) {
229         AVFrame *out;
230         int delay, nb_samples;
231
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);
235
236         out = ff_get_audio_buffer(outlink, nb_samples);
237         if (!out) {
238             ret = AVERROR(ENOMEM);
239             goto fail;
240         }
241
242         ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
243                                  nb_samples, in->extended_data, in->linesize[0],
244                                  in->nb_samples);
245         if (ret <= 0) {
246             av_frame_free(&out);
247             if (ret < 0)
248                 goto fail;
249         }
250
251         av_assert0(!avresample_available(s->avr));
252
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, "
256                        "assuming 0.\n");
257                 s->next_pts = 0;
258             } else
259                 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
260                                            outlink->time_base);
261         }
262
263         if (ret > 0) {
264             out->nb_samples = ret;
265
266             ret = av_frame_copy_props(out, in);
267             if (ret < 0) {
268                 av_frame_free(&out);
269                 goto fail;
270             }
271
272             if (s->resampling) {
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);
284                 } else
285                     out->pts = s->next_pts;
286
287                 s->next_pts = out->pts + out->nb_samples;
288                 s->next_in_pts = in->pts + in->nb_samples;
289             } else
290                 out->pts = in->pts;
291
292             ret = ff_filter_frame(outlink, out);
293             s->got_output = 1;
294         }
295
296 fail:
297         av_frame_free(&in);
298     } else {
299         in->format = outlink->format;
300         ret = ff_filter_frame(outlink, in);
301         s->got_output = 1;
302     }
303
304     return ret;
305 }
306
307 static const AVClass *resample_child_class_next(const AVClass *prev)
308 {
309     return prev ? NULL : avresample_get_class();
310 }
311
312 static void *resample_child_next(void *obj, void *prev)
313 {
314     ResampleContext *s = obj;
315     return prev ? NULL : s->avr;
316 }
317
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,
324 };
325
326 static const AVFilterPad avfilter_af_resample_inputs[] = {
327     {
328         .name           = "default",
329         .type           = AVMEDIA_TYPE_AUDIO,
330         .filter_frame   = filter_frame,
331     },
332     { NULL }
333 };
334
335 static const AVFilterPad avfilter_af_resample_outputs[] = {
336     {
337         .name          = "default",
338         .type          = AVMEDIA_TYPE_AUDIO,
339         .config_props  = config_output,
340         .request_frame = request_frame
341     },
342     { NULL }
343 };
344
345 AVFilter ff_af_resample = {
346     .name          = "resample",
347     .description   = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
348     .priv_size     = sizeof(ResampleContext),
349     .priv_class    = &resample_class,
350
351     .init_dict      = init,
352     .uninit         = uninit,
353     .query_formats  = query_formats,
354
355     .inputs    = avfilter_af_resample_inputs,
356     .outputs   = avfilter_af_resample_outputs,
357 };