mp3: Properly use AVCodecContext API
[platform/upstream/libav.git] / 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     int64_t next_pts;
45     int64_t next_in_pts;
46
47     /* set by filter_frame() to signal an output frame to request_frame() */
48     int got_output;
49 } ResampleContext;
50
51 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
52 {
53     ResampleContext *s = ctx->priv;
54     const AVClass *avr_class = avresample_get_class();
55     AVDictionaryEntry *e = NULL;
56
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);
61     }
62
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
75     return 0;
76 }
77
78 static 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     }
86     av_dict_free(&s->options);
87 }
88
89 static int query_formats(AVFilterContext *ctx)
90 {
91     AVFilterLink *inlink  = ctx->inputs[0];
92     AVFilterLink *outlink = ctx->outputs[0];
93
94     AVFilterFormats        *in_formats      = ff_all_formats(AVMEDIA_TYPE_AUDIO);
95     AVFilterFormats        *out_formats     = ff_all_formats(AVMEDIA_TYPE_AUDIO);
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();
100
101     ff_formats_ref(in_formats,  &inlink->out_formats);
102     ff_formats_ref(out_formats, &outlink->in_formats);
103
104     ff_formats_ref(in_samplerates,  &inlink->out_samplerates);
105     ff_formats_ref(out_samplerates, &outlink->in_samplerates);
106
107     ff_channel_layouts_ref(in_layouts,  &inlink->out_channel_layouts);
108     ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
109
110     return 0;
111 }
112
113 static 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
121     if (s->avr) {
122         avresample_close(s->avr);
123         avresample_free(&s->avr);
124     }
125
126     if (inlink->channel_layout == outlink->channel_layout &&
127         inlink->sample_rate    == outlink->sample_rate    &&
128         (inlink->format        == outlink->format ||
129         (av_get_channel_layout_nb_channels(inlink->channel_layout)  == 1 &&
130          av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
131          av_get_planar_sample_fmt(inlink->format) ==
132          av_get_planar_sample_fmt(outlink->format))))
133         return 0;
134
135     if (!(s->avr = avresample_alloc_context()))
136         return AVERROR(ENOMEM);
137
138     if (s->options) {
139         int ret;
140         AVDictionaryEntry *e = NULL;
141         while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
142             av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
143
144         ret = av_opt_set_dict(s->avr, &s->options);
145         if (ret < 0)
146             return ret;
147     }
148
149     av_opt_set_int(s->avr,  "in_channel_layout", inlink ->channel_layout, 0);
150     av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
151     av_opt_set_int(s->avr,  "in_sample_fmt",     inlink ->format,         0);
152     av_opt_set_int(s->avr, "out_sample_fmt",     outlink->format,         0);
153     av_opt_set_int(s->avr,  "in_sample_rate",    inlink ->sample_rate,    0);
154     av_opt_set_int(s->avr, "out_sample_rate",    outlink->sample_rate,    0);
155
156     if ((ret = avresample_open(s->avr)) < 0)
157         return ret;
158
159     outlink->time_base = (AVRational){ 1, outlink->sample_rate };
160     s->next_pts        = AV_NOPTS_VALUE;
161     s->next_in_pts     = AV_NOPTS_VALUE;
162
163     av_get_channel_layout_string(buf1, sizeof(buf1),
164                                  -1, inlink ->channel_layout);
165     av_get_channel_layout_string(buf2, sizeof(buf2),
166                                  -1, outlink->channel_layout);
167     av_log(ctx, AV_LOG_VERBOSE,
168            "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
169            av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
170            av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
171
172     return 0;
173 }
174
175 static int request_frame(AVFilterLink *outlink)
176 {
177     AVFilterContext *ctx = outlink->src;
178     ResampleContext   *s = ctx->priv;
179     int ret = 0;
180
181     s->got_output = 0;
182     while (ret >= 0 && !s->got_output)
183         ret = ff_request_frame(ctx->inputs[0]);
184
185     /* flush the lavr delay buffer */
186     if (ret == AVERROR_EOF && s->avr) {
187         AVFrame *frame;
188         int nb_samples = avresample_get_out_samples(s->avr, 0);
189
190         if (!nb_samples)
191             return ret;
192
193         frame = ff_get_audio_buffer(outlink, nb_samples);
194         if (!frame)
195             return AVERROR(ENOMEM);
196
197         ret = avresample_convert(s->avr, frame->extended_data,
198                                  frame->linesize[0], nb_samples,
199                                  NULL, 0, 0);
200         if (ret <= 0) {
201             av_frame_free(&frame);
202             return (ret == 0) ? AVERROR_EOF : ret;
203         }
204
205         frame->pts = s->next_pts;
206         return ff_filter_frame(outlink, frame);
207     }
208     return ret;
209 }
210
211 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
212 {
213     AVFilterContext  *ctx = inlink->dst;
214     ResampleContext    *s = ctx->priv;
215     AVFilterLink *outlink = ctx->outputs[0];
216     int ret;
217
218     if (s->avr) {
219         AVFrame *out;
220         int delay, nb_samples;
221
222         /* maximum possible samples lavr can output */
223         delay      = avresample_get_delay(s->avr);
224         nb_samples = avresample_get_out_samples(s->avr, in->nb_samples);
225
226         out = ff_get_audio_buffer(outlink, nb_samples);
227         if (!out) {
228             ret = AVERROR(ENOMEM);
229             goto fail;
230         }
231
232         ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
233                                  nb_samples, in->extended_data, in->linesize[0],
234                                  in->nb_samples);
235         if (ret <= 0) {
236             av_frame_free(&out);
237             if (ret < 0)
238                 goto fail;
239         }
240
241         av_assert0(!avresample_available(s->avr));
242
243         if (s->next_pts == AV_NOPTS_VALUE) {
244             if (in->pts == AV_NOPTS_VALUE) {
245                 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
246                        "assuming 0.\n");
247                 s->next_pts = 0;
248             } else
249                 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
250                                            outlink->time_base);
251         }
252
253         if (ret > 0) {
254             out->nb_samples = ret;
255
256             ret = av_frame_copy_props(out, in);
257             if (ret < 0) {
258                 av_frame_free(&out);
259                 goto fail;
260             }
261
262             out->sample_rate = outlink->sample_rate;
263             /* Only convert in->pts if there is a discontinuous jump.
264                This ensures that out->pts tracks the number of samples actually
265                output by the resampler in the absence of such a jump.
266                Otherwise, the rounding in av_rescale_q() and av_rescale()
267                causes off-by-1 errors. */
268             if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
269                 out->pts = av_rescale_q(in->pts, inlink->time_base,
270                                             outlink->time_base) -
271                                av_rescale(delay, outlink->sample_rate,
272                                           inlink->sample_rate);
273             } else
274                 out->pts = s->next_pts;
275
276             s->next_pts = out->pts + out->nb_samples;
277             s->next_in_pts = in->pts + in->nb_samples;
278
279             ret = ff_filter_frame(outlink, out);
280             s->got_output = 1;
281         }
282
283 fail:
284         av_frame_free(&in);
285     } else {
286         in->format = outlink->format;
287         ret = ff_filter_frame(outlink, in);
288         s->got_output = 1;
289     }
290
291     return ret;
292 }
293
294 static const AVClass *resample_child_class_next(const AVClass *prev)
295 {
296     return prev ? NULL : avresample_get_class();
297 }
298
299 static void *resample_child_next(void *obj, void *prev)
300 {
301     ResampleContext *s = obj;
302     return prev ? NULL : s->avr;
303 }
304
305 static const AVClass resample_class = {
306     .class_name       = "resample",
307     .item_name        = av_default_item_name,
308     .version          = LIBAVUTIL_VERSION_INT,
309     .child_class_next = resample_child_class_next,
310     .child_next       = resample_child_next,
311 };
312
313 static const AVFilterPad avfilter_af_resample_inputs[] = {
314     {
315         .name           = "default",
316         .type           = AVMEDIA_TYPE_AUDIO,
317         .filter_frame   = filter_frame,
318     },
319     { NULL }
320 };
321
322 static const AVFilterPad avfilter_af_resample_outputs[] = {
323     {
324         .name          = "default",
325         .type          = AVMEDIA_TYPE_AUDIO,
326         .config_props  = config_output,
327         .request_frame = request_frame
328     },
329     { NULL }
330 };
331
332 AVFilter ff_af_resample = {
333     .name          = "resample",
334     .description   = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
335     .priv_size     = sizeof(ResampleContext),
336     .priv_class    = &resample_class,
337
338     .init_dict      = init,
339     .uninit         = uninit,
340     .query_formats  = query_formats,
341
342     .inputs    = avfilter_af_resample_inputs,
343     .outputs   = avfilter_af_resample_outputs,
344 };