]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_resample.c
Merge commit '19133e96d30e3f80dbae236ef081aedef419a6bf'
[ffmpeg] / libavfilter / af_resample.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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"
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
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"
35 #include "formats.h"
36 #include "internal.h"
37
38 typedef struct ResampleContext {
39     const AVClass *class;
40     AVAudioResampleContext *avr;
41     AVDictionary *options;
42
43     int64_t next_pts;
44
45     /* set by filter_frame() to signal an output frame to request_frame() */
46     int got_output;
47 } ResampleContext;
48
49 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
50 {
51     ResampleContext *s = ctx->priv;
52     const AVClass *avr_class = avresample_get_class();
53     AVDictionaryEntry *e = NULL;
54
55     while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
56         if (av_opt_find(&avr_class, e->key, NULL, 0,
57                         AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
58             av_dict_set(&s->options, e->key, e->value, 0);
59     }
60
61     e = NULL;
62     while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
63         av_dict_set(opts, e->key, NULL, 0);
64
65     /* do not allow the user to override basic format options */
66     av_dict_set(&s->options,  "in_channel_layout", NULL, 0);
67     av_dict_set(&s->options, "out_channel_layout", NULL, 0);
68     av_dict_set(&s->options,  "in_sample_fmt",     NULL, 0);
69     av_dict_set(&s->options, "out_sample_fmt",     NULL, 0);
70     av_dict_set(&s->options,  "in_sample_rate",    NULL, 0);
71     av_dict_set(&s->options, "out_sample_rate",    NULL, 0);
72
73     return 0;
74 }
75
76 static av_cold void uninit(AVFilterContext *ctx)
77 {
78     ResampleContext *s = ctx->priv;
79
80     if (s->avr) {
81         avresample_close(s->avr);
82         avresample_free(&s->avr);
83     }
84     av_dict_free(&s->options);
85 }
86
87 static int query_formats(AVFilterContext *ctx)
88 {
89     AVFilterLink *inlink  = ctx->inputs[0];
90     AVFilterLink *outlink = ctx->outputs[0];
91
92     AVFilterFormats        *in_formats      = ff_all_formats(AVMEDIA_TYPE_AUDIO);
93     AVFilterFormats        *out_formats     = ff_all_formats(AVMEDIA_TYPE_AUDIO);
94     AVFilterFormats        *in_samplerates  = ff_all_samplerates();
95     AVFilterFormats        *out_samplerates = ff_all_samplerates();
96     AVFilterChannelLayouts *in_layouts      = ff_all_channel_layouts();
97     AVFilterChannelLayouts *out_layouts     = ff_all_channel_layouts();
98
99     ff_formats_ref(in_formats,  &inlink->out_formats);
100     ff_formats_ref(out_formats, &outlink->in_formats);
101
102     ff_formats_ref(in_samplerates,  &inlink->out_samplerates);
103     ff_formats_ref(out_samplerates, &outlink->in_samplerates);
104
105     ff_channel_layouts_ref(in_layouts,  &inlink->out_channel_layouts);
106     ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
107
108     return 0;
109 }
110
111 static int config_output(AVFilterLink *outlink)
112 {
113     AVFilterContext *ctx = outlink->src;
114     AVFilterLink *inlink = ctx->inputs[0];
115     ResampleContext   *s = ctx->priv;
116     char buf1[64], buf2[64];
117     int ret;
118
119     if (s->avr) {
120         avresample_close(s->avr);
121         avresample_free(&s->avr);
122     }
123
124     if (inlink->channel_layout == outlink->channel_layout &&
125         inlink->sample_rate    == outlink->sample_rate    &&
126         (inlink->format        == outlink->format ||
127         (av_get_channel_layout_nb_channels(inlink->channel_layout)  == 1 &&
128          av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
129          av_get_planar_sample_fmt(inlink->format) ==
130          av_get_planar_sample_fmt(outlink->format))))
131         return 0;
132
133     if (!(s->avr = avresample_alloc_context()))
134         return AVERROR(ENOMEM);
135
136     if (s->options) {
137         AVDictionaryEntry *e = NULL;
138         while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
139             av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
140
141         av_opt_set_dict(s->avr, &s->options);
142     }
143
144     av_opt_set_int(s->avr,  "in_channel_layout", inlink ->channel_layout, 0);
145     av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
146     av_opt_set_int(s->avr,  "in_sample_fmt",     inlink ->format,         0);
147     av_opt_set_int(s->avr, "out_sample_fmt",     outlink->format,         0);
148     av_opt_set_int(s->avr,  "in_sample_rate",    inlink ->sample_rate,    0);
149     av_opt_set_int(s->avr, "out_sample_rate",    outlink->sample_rate,    0);
150
151     if ((ret = avresample_open(s->avr)) < 0)
152         return ret;
153
154     outlink->time_base = (AVRational){ 1, outlink->sample_rate };
155     s->next_pts        = AV_NOPTS_VALUE;
156
157     av_get_channel_layout_string(buf1, sizeof(buf1),
158                                  -1, inlink ->channel_layout);
159     av_get_channel_layout_string(buf2, sizeof(buf2),
160                                  -1, outlink->channel_layout);
161     av_log(ctx, AV_LOG_VERBOSE,
162            "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
163            av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
164            av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
165
166     return 0;
167 }
168
169 static int request_frame(AVFilterLink *outlink)
170 {
171     AVFilterContext *ctx = outlink->src;
172     ResampleContext   *s = ctx->priv;
173     int ret = 0;
174
175     s->got_output = 0;
176     while (ret >= 0 && !s->got_output)
177         ret = ff_request_frame(ctx->inputs[0]);
178
179     /* flush the lavr delay buffer */
180     if (ret == AVERROR_EOF && s->avr) {
181         AVFrame *frame;
182         int nb_samples = avresample_get_out_samples(s->avr, 0);
183
184         if (!nb_samples)
185             return ret;
186
187         frame = ff_get_audio_buffer(outlink, nb_samples);
188         if (!frame)
189             return AVERROR(ENOMEM);
190
191         ret = avresample_convert(s->avr, frame->extended_data,
192                                  frame->linesize[0], nb_samples,
193                                  NULL, 0, 0);
194         if (ret <= 0) {
195             av_frame_free(&frame);
196             return (ret == 0) ? AVERROR_EOF : ret;
197         }
198
199         frame->pts = s->next_pts;
200         return ff_filter_frame(outlink, frame);
201     }
202     return ret;
203 }
204
205 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
206 {
207     AVFilterContext  *ctx = inlink->dst;
208     ResampleContext    *s = ctx->priv;
209     AVFilterLink *outlink = ctx->outputs[0];
210     int ret;
211
212     if (s->avr) {
213         AVFrame *out;
214         int delay, nb_samples;
215
216         /* maximum possible samples lavr can output */
217         delay      = avresample_get_delay(s->avr);
218         nb_samples = avresample_get_out_samples(s->avr, in->nb_samples);
219
220         out = ff_get_audio_buffer(outlink, nb_samples);
221         if (!out) {
222             ret = AVERROR(ENOMEM);
223             goto fail;
224         }
225
226         ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
227                                  nb_samples, in->extended_data, in->linesize[0],
228                                  in->nb_samples);
229         if (ret <= 0) {
230             av_frame_free(&out);
231             if (ret < 0)
232                 goto fail;
233         }
234
235         av_assert0(!avresample_available(s->avr));
236
237         if (s->next_pts == AV_NOPTS_VALUE) {
238             if (in->pts == AV_NOPTS_VALUE) {
239                 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
240                        "assuming 0.\n");
241                 s->next_pts = 0;
242             } else
243                 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
244                                            outlink->time_base);
245         }
246
247         if (ret > 0) {
248             out->nb_samples = ret;
249
250             ret = av_frame_copy_props(out, in);
251             if (ret < 0) {
252                 av_frame_free(&out);
253                 goto fail;
254             }
255
256             out->sample_rate = outlink->sample_rate;
257             if (in->pts != AV_NOPTS_VALUE) {
258                 out->pts = av_rescale_q(in->pts, inlink->time_base,
259                                             outlink->time_base) -
260                                av_rescale(delay, outlink->sample_rate,
261                                           inlink->sample_rate);
262             } else
263                 out->pts = s->next_pts;
264
265             s->next_pts = out->pts + out->nb_samples;
266
267             ret = ff_filter_frame(outlink, out);
268             s->got_output = 1;
269         }
270
271 fail:
272         av_frame_free(&in);
273     } else {
274         in->format = outlink->format;
275         ret = ff_filter_frame(outlink, in);
276         s->got_output = 1;
277     }
278
279     return ret;
280 }
281
282 static const AVClass *resample_child_class_next(const AVClass *prev)
283 {
284     return prev ? NULL : avresample_get_class();
285 }
286
287 static void *resample_child_next(void *obj, void *prev)
288 {
289     ResampleContext *s = obj;
290     return prev ? NULL : s->avr;
291 }
292
293 static const AVClass resample_class = {
294     .class_name       = "resample",
295     .item_name        = av_default_item_name,
296     .version          = LIBAVUTIL_VERSION_INT,
297     .child_class_next = resample_child_class_next,
298     .child_next       = resample_child_next,
299 };
300
301 static const AVFilterPad avfilter_af_resample_inputs[] = {
302     {
303         .name          = "default",
304         .type          = AVMEDIA_TYPE_AUDIO,
305         .filter_frame  = filter_frame,
306     },
307     { NULL }
308 };
309
310 static const AVFilterPad avfilter_af_resample_outputs[] = {
311     {
312         .name          = "default",
313         .type          = AVMEDIA_TYPE_AUDIO,
314         .config_props  = config_output,
315         .request_frame = request_frame
316     },
317     { NULL }
318 };
319
320 AVFilter ff_af_resample = {
321     .name          = "resample",
322     .description   = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
323     .priv_size     = sizeof(ResampleContext),
324     .priv_class    = &resample_class,
325     .init_dict     = init,
326     .uninit        = uninit,
327     .query_formats = query_formats,
328     .inputs        = avfilter_af_resample_inputs,
329     .outputs       = avfilter_af_resample_outputs,
330 };