imc: make IMDCT support stereo output
[ffmpeg.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/mathematics.h"
28 #include "libavutil/opt.h"
29
30 #include "libavresample/avresample.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36
37 typedef struct ResampleContext {
38     AVAudioResampleContext *avr;
39
40     int64_t next_pts;
41 } ResampleContext;
42
43 static av_cold void uninit(AVFilterContext *ctx)
44 {
45     ResampleContext *s = ctx->priv;
46
47     if (s->avr) {
48         avresample_close(s->avr);
49         avresample_free(&s->avr);
50     }
51 }
52
53 static int query_formats(AVFilterContext *ctx)
54 {
55     AVFilterLink *inlink  = ctx->inputs[0];
56     AVFilterLink *outlink = ctx->outputs[0];
57
58     AVFilterFormats        *in_formats      = ff_all_formats(AVMEDIA_TYPE_AUDIO);
59     AVFilterFormats        *out_formats     = ff_all_formats(AVMEDIA_TYPE_AUDIO);
60     AVFilterFormats        *in_samplerates  = ff_all_samplerates();
61     AVFilterFormats        *out_samplerates = ff_all_samplerates();
62     AVFilterChannelLayouts *in_layouts      = ff_all_channel_layouts();
63     AVFilterChannelLayouts *out_layouts     = ff_all_channel_layouts();
64
65     ff_formats_ref(in_formats,  &inlink->out_formats);
66     ff_formats_ref(out_formats, &outlink->in_formats);
67
68     ff_formats_ref(in_samplerates,  &inlink->out_samplerates);
69     ff_formats_ref(out_samplerates, &outlink->in_samplerates);
70
71     ff_channel_layouts_ref(in_layouts,  &inlink->out_channel_layouts);
72     ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
73
74     return 0;
75 }
76
77 static int config_output(AVFilterLink *outlink)
78 {
79     AVFilterContext *ctx = outlink->src;
80     AVFilterLink *inlink = ctx->inputs[0];
81     ResampleContext   *s = ctx->priv;
82     char buf1[64], buf2[64];
83     int ret;
84
85     if (s->avr) {
86         avresample_close(s->avr);
87         avresample_free(&s->avr);
88     }
89
90     if (inlink->channel_layout == outlink->channel_layout &&
91         inlink->sample_rate    == outlink->sample_rate    &&
92         inlink->format         == outlink->format)
93         return 0;
94
95     if (!(s->avr = avresample_alloc_context()))
96         return AVERROR(ENOMEM);
97
98     av_opt_set_int(s->avr,  "in_channel_layout", inlink ->channel_layout, 0);
99     av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
100     av_opt_set_int(s->avr,  "in_sample_fmt",     inlink ->format,         0);
101     av_opt_set_int(s->avr, "out_sample_fmt",     outlink->format,         0);
102     av_opt_set_int(s->avr,  "in_sample_rate",    inlink ->sample_rate,    0);
103     av_opt_set_int(s->avr, "out_sample_rate",    outlink->sample_rate,    0);
104
105     /* if both the input and output formats are s16 or u8, use s16 as
106        the internal sample format */
107     if (av_get_bytes_per_sample(inlink->format)  <= 2 &&
108         av_get_bytes_per_sample(outlink->format) <= 2)
109         av_opt_set_int(s->avr, "internal_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
110
111     if ((ret = avresample_open(s->avr)) < 0)
112         return ret;
113
114     outlink->time_base = (AVRational){ 1, outlink->sample_rate };
115     s->next_pts        = AV_NOPTS_VALUE;
116
117     av_get_channel_layout_string(buf1, sizeof(buf1),
118                                  -1, inlink ->channel_layout);
119     av_get_channel_layout_string(buf2, sizeof(buf2),
120                                  -1, outlink->channel_layout);
121     av_log(ctx, AV_LOG_VERBOSE,
122            "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
123            av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
124            av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
125
126     return 0;
127 }
128
129 static int request_frame(AVFilterLink *outlink)
130 {
131     AVFilterContext *ctx = outlink->src;
132     ResampleContext   *s = ctx->priv;
133     int ret = ff_request_frame(ctx->inputs[0]);
134
135     /* flush the lavr delay buffer */
136     if (ret == AVERROR_EOF && s->avr) {
137         AVFilterBufferRef *buf;
138         int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
139                                         outlink->sample_rate,
140                                         ctx->inputs[0]->sample_rate,
141                                         AV_ROUND_UP);
142
143         if (!nb_samples)
144             return ret;
145
146         buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
147         if (!buf)
148             return AVERROR(ENOMEM);
149
150         ret = avresample_convert(s->avr, (void**)buf->extended_data,
151                                  buf->linesize[0], nb_samples,
152                                  NULL, 0, 0);
153         if (ret <= 0) {
154             avfilter_unref_buffer(buf);
155             return (ret == 0) ? AVERROR_EOF : ret;
156         }
157
158         buf->pts = s->next_pts;
159         ff_filter_samples(outlink, buf);
160         return 0;
161     }
162     return ret;
163 }
164
165 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
166 {
167     AVFilterContext  *ctx = inlink->dst;
168     ResampleContext    *s = ctx->priv;
169     AVFilterLink *outlink = ctx->outputs[0];
170
171     if (s->avr) {
172         AVFilterBufferRef *buf_out;
173         int delay, nb_samples, ret;
174
175         /* maximum possible samples lavr can output */
176         delay      = avresample_get_delay(s->avr);
177         nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
178                                     outlink->sample_rate, inlink->sample_rate,
179                                     AV_ROUND_UP);
180
181         buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
182         ret     = avresample_convert(s->avr, (void**)buf_out->extended_data,
183                                      buf_out->linesize[0], nb_samples,
184                                      (void**)buf->extended_data, buf->linesize[0],
185                                      buf->audio->nb_samples);
186
187         av_assert0(!avresample_available(s->avr));
188
189         if (s->next_pts == AV_NOPTS_VALUE) {
190             if (buf->pts == AV_NOPTS_VALUE) {
191                 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
192                        "assuming 0.\n");
193                 s->next_pts = 0;
194             } else
195                 s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
196                                            outlink->time_base);
197         }
198
199         if (ret > 0) {
200             buf_out->audio->nb_samples = ret;
201             if (buf->pts != AV_NOPTS_VALUE) {
202                 buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
203                                             outlink->time_base) -
204                                av_rescale(delay, outlink->sample_rate,
205                                           inlink->sample_rate);
206             } else
207                 buf_out->pts = s->next_pts;
208
209             s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
210
211             ff_filter_samples(outlink, buf_out);
212         }
213         avfilter_unref_buffer(buf);
214     } else
215         ff_filter_samples(outlink, buf);
216 }
217
218 AVFilter avfilter_af_resample = {
219     .name          = "resample",
220     .description   = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
221     .priv_size     = sizeof(ResampleContext),
222
223     .uninit         = uninit,
224     .query_formats  = query_formats,
225
226     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
227                                           .type            = AVMEDIA_TYPE_AUDIO,
228                                           .filter_samples  = filter_samples,
229                                           .min_perms       = AV_PERM_READ },
230                                         { .name = NULL}},
231     .outputs   = (const AVFilterPad[]) {{ .name          = "default",
232                                           .type          = AVMEDIA_TYPE_AUDIO,
233                                           .config_props  = config_output,
234                                           .request_frame = request_frame },
235                                         { .name = NULL}},
236 };