af_aresample: use new swr API to pass and compensate PTS
[ffmpeg.git] / libavfilter / af_aresample.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2011 Mina Nagy Zaki
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * resampling audio filter
25  */
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/samplefmt.h"
30 #include "libavutil/avassert.h"
31 #include "libswresample/swresample.h"
32 #include "avfilter.h"
33 #include "audio.h"
34 #include "internal.h"
35
36 typedef struct {
37     double ratio;
38     struct SwrContext *swr;
39     int64_t next_pts;
40 } AResampleContext;
41
42 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
43 {
44     AResampleContext *aresample = ctx->priv;
45     int ret = 0;
46     char *argd = av_strdup(args);
47
48     aresample->next_pts = AV_NOPTS_VALUE;
49     aresample->swr = swr_alloc();
50     if (!aresample->swr)
51         return AVERROR(ENOMEM);
52
53     if (args) {
54         char *ptr=argd, *token;
55
56         while(token = av_strtok(ptr, ":", &ptr)) {
57             char *value;
58             av_strtok(token, "=", &value);
59
60             if(value) {
61                 if((ret=av_opt_set(aresample->swr, token, value, 0)) < 0)
62                     goto end;
63             } else {
64                 int out_rate;
65                 if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
66                     goto end;
67                 if((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0)
68                     goto end;
69             }
70         }
71     }
72 end:
73     av_free(argd);
74     return ret;
75 }
76
77 static av_cold void uninit(AVFilterContext *ctx)
78 {
79     AResampleContext *aresample = ctx->priv;
80     swr_free(&aresample->swr);
81 }
82
83 static int query_formats(AVFilterContext *ctx)
84 {
85     AResampleContext *aresample = ctx->priv;
86     int out_rate                   = av_get_int(aresample->swr, "osr", NULL);
87     uint64_t out_layout            = av_get_int(aresample->swr, "ocl", NULL);
88     enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
89
90     AVFilterLink *inlink  = ctx->inputs[0];
91     AVFilterLink *outlink = ctx->outputs[0];
92
93     AVFilterFormats        *in_formats      = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
94     AVFilterFormats        *out_formats;
95     AVFilterFormats        *in_samplerates  = ff_all_samplerates();
96     AVFilterFormats        *out_samplerates;
97     AVFilterChannelLayouts *in_layouts      = ff_all_channel_layouts();
98     AVFilterChannelLayouts *out_layouts;
99
100     avfilter_formats_ref  (in_formats,      &inlink->out_formats);
101     avfilter_formats_ref  (in_samplerates,  &inlink->out_samplerates);
102     ff_channel_layouts_ref(in_layouts,      &inlink->out_channel_layouts);
103
104     if(out_rate > 0) {
105         out_samplerates = avfilter_make_format_list((int[]){ out_rate, -1 });
106     } else {
107         out_samplerates = ff_all_samplerates();
108     }
109     avfilter_formats_ref(out_samplerates, &outlink->in_samplerates);
110
111     if(out_format != AV_SAMPLE_FMT_NONE) {
112         out_formats = avfilter_make_format_list((int[]){ out_format, -1 });
113     } else
114         out_formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
115     avfilter_formats_ref(out_formats, &outlink->in_formats);
116
117     if(out_layout) {
118         out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
119     } else
120         out_layouts = ff_all_channel_layouts();
121     ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
122
123     return 0;
124 }
125
126
127 static int config_output(AVFilterLink *outlink)
128 {
129     int ret;
130     AVFilterContext *ctx = outlink->src;
131     AVFilterLink *inlink = ctx->inputs[0];
132     AResampleContext *aresample = ctx->priv;
133     int out_rate;
134     uint64_t out_layout;
135     enum AVSampleFormat out_format;
136
137     aresample->swr = swr_alloc_set_opts(aresample->swr,
138                                         outlink->channel_layout, outlink->format, outlink->sample_rate,
139                                         inlink->channel_layout, inlink->format, inlink->sample_rate,
140                                         0, ctx);
141     if (!aresample->swr)
142         return AVERROR(ENOMEM);
143
144     ret = swr_init(aresample->swr);
145     if (ret < 0)
146         return ret;
147
148     out_rate   = av_get_int(aresample->swr, "osr", NULL);
149     out_layout = av_get_int(aresample->swr, "ocl", NULL);
150     out_format = av_get_int(aresample->swr, "osf", NULL);
151     outlink->time_base = (AVRational) {1, out_rate};
152
153     av_assert0(outlink->sample_rate == out_rate);
154     av_assert0(outlink->channel_layout == out_layout);
155     av_assert0(outlink->format == out_format);
156
157     aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
158
159     av_log(ctx, AV_LOG_INFO, "r:%"PRId64"Hz -> r:%"PRId64"Hz\n",
160            inlink->sample_rate, outlink->sample_rate);
161     return 0;
162 }
163
164 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
165 {
166     AResampleContext *aresample = inlink->dst->priv;
167     const int n_in  = insamplesref->audio->nb_samples;
168     int n_out       = n_in * aresample->ratio * 2 ;
169     AVFilterLink *const outlink = inlink->dst->outputs[0];
170     AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
171
172     n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
173                                  (void *)insamplesref->extended_data, n_in);
174     if (n_out <= 0) {
175         avfilter_unref_buffer(outsamplesref);
176         avfilter_unref_buffer(insamplesref);
177         return;
178     }
179
180     avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
181
182     outsamplesref->audio->sample_rate = outlink->sample_rate;
183     outsamplesref->audio->nb_samples  = n_out;
184
185 #if 0
186     if(insamplesref->pts != AV_NOPTS_VALUE) {
187         aresample->next_pts =
188         outsamplesref->pts  =  av_rescale_q(insamplesref->pts, inlink->time_base, outlink->time_base)
189                              - swr_get_delay(aresample->swr, outlink->time_base.den);
190         av_assert0(outlink->time_base.num == 1);
191     } else{
192         outsamplesref->pts  = AV_NOPTS_VALUE; //aresample->next_pts;
193     }
194     if(aresample->next_pts != AV_NOPTS_VALUE)
195         aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
196 #else
197     if(insamplesref->pts != AV_NOPTS_VALUE) {
198         int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
199         int64_t outpts= swr_next_pts(aresample->swr, inpts);
200         aresample->next_pts =
201         outsamplesref->pts  = (outpts + inlink->sample_rate/2) / inlink->sample_rate;
202     } else {
203         outsamplesref->pts  = AV_NOPTS_VALUE;
204     }
205 #endif
206     ff_filter_samples(outlink, outsamplesref);
207     avfilter_unref_buffer(insamplesref);
208 }
209
210 static int request_frame(AVFilterLink *outlink)
211 {
212     AVFilterContext *ctx = outlink->src;
213     AResampleContext *aresample = ctx->priv;
214     AVFilterLink *const inlink = outlink->src->inputs[0];
215     int ret = avfilter_request_frame(ctx->inputs[0]);
216
217     if (ret == AVERROR_EOF) {
218         AVFilterBufferRef *outsamplesref;
219         int n_out = 4096;
220
221         outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
222         if (!outsamplesref)
223             return AVERROR(ENOMEM);
224         n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
225         if (n_out <= 0) {
226             avfilter_unref_buffer(outsamplesref);
227             return (n_out == 0) ? AVERROR_EOF : n_out;
228         }
229
230         outsamplesref->audio->sample_rate = outlink->sample_rate;
231         outsamplesref->audio->nb_samples  = n_out;
232 #if 0
233         outsamplesref->pts = aresample->next_pts;
234         if(aresample->next_pts != AV_NOPTS_VALUE)
235             aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
236 #else
237         outsamplesref->pts = (swr_next_pts(aresample->swr, INT64_MIN) + inlink->sample_rate/2) / inlink->sample_rate;
238 #endif
239
240         ff_filter_samples(outlink, outsamplesref);
241         return 0;
242     }
243     return ret;
244 }
245
246 AVFilter avfilter_af_aresample = {
247     .name          = "aresample",
248     .description   = NULL_IF_CONFIG_SMALL("Resample audio data."),
249     .init          = init,
250     .uninit        = uninit,
251     .query_formats = query_formats,
252     .priv_size     = sizeof(AResampleContext),
253
254     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
255                                     .type            = AVMEDIA_TYPE_AUDIO,
256                                     .filter_samples  = filter_samples,
257                                     .min_perms       = AV_PERM_READ, },
258                                   { .name = NULL}},
259     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
260                                     .config_props    = config_output,
261                                     .request_frame   = request_frame,
262                                     .type            = AVMEDIA_TYPE_AUDIO, },
263                                   { .name = NULL}},
264 };