af_aresample: use extended_data
[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 + 1;
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(insamplesref->pts != AV_NOPTS_VALUE) {
186         aresample->next_pts =
187         outsamplesref->pts  =  av_rescale_q(insamplesref->pts, inlink->time_base, outlink->time_base)
188                              - swr_get_delay(aresample->swr, outlink->time_base.den);
189         av_assert0(outlink->time_base.num == 1);
190     } else{
191         outsamplesref->pts  = AV_NOPTS_VALUE; //aresample->next_pts;
192     }
193     if(aresample->next_pts != AV_NOPTS_VALUE)
194         aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
195
196     ff_filter_samples(outlink, outsamplesref);
197     avfilter_unref_buffer(insamplesref);
198 }
199
200 static int request_frame(AVFilterLink *outlink)
201 {
202     AVFilterContext *ctx = outlink->src;
203     AResampleContext *aresample = ctx->priv;
204     int ret = avfilter_request_frame(ctx->inputs[0]);
205
206     if (ret == AVERROR_EOF) {
207         AVFilterBufferRef *outsamplesref;
208         int n_out = 4096;
209
210         outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
211         if (!outsamplesref)
212             return AVERROR(ENOMEM);
213         n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
214         if (n_out <= 0) {
215             avfilter_unref_buffer(outsamplesref);
216             return (n_out == 0) ? AVERROR_EOF : n_out;
217         }
218
219         outsamplesref->audio->sample_rate = outlink->sample_rate;
220         outsamplesref->audio->nb_samples  = n_out;
221         outsamplesref->pts = aresample->next_pts;
222         if(aresample->next_pts != AV_NOPTS_VALUE)
223             aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
224
225         ff_filter_samples(outlink, outsamplesref);
226         return 0;
227     }
228     return ret;
229 }
230
231 AVFilter avfilter_af_aresample = {
232     .name          = "aresample",
233     .description   = NULL_IF_CONFIG_SMALL("Resample audio data."),
234     .init          = init,
235     .uninit        = uninit,
236     .query_formats = query_formats,
237     .priv_size     = sizeof(AResampleContext),
238
239     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
240                                     .type            = AVMEDIA_TYPE_AUDIO,
241                                     .filter_samples  = filter_samples,
242                                     .min_perms       = AV_PERM_READ, },
243                                   { .name = NULL}},
244     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
245                                     .config_props    = config_output,
246                                     .request_frame   = request_frame,
247                                     .type            = AVMEDIA_TYPE_AUDIO, },
248                                   { .name = NULL}},
249 };