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