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