2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2011 Mina Nagy Zaki
5 * This file is part of FFmpeg.
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.
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.
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
24 * resampling audio filter
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/avassert.h"
32 #include "libswresample/swresample.h"
39 struct SwrContext *swr;
44 static av_cold int init(AVFilterContext *ctx, const char *args)
46 AResampleContext *aresample = ctx->priv;
48 char *argd = av_strdup(args);
50 aresample->next_pts = AV_NOPTS_VALUE;
51 aresample->swr = swr_alloc();
52 if (!aresample->swr) {
53 ret = AVERROR(ENOMEM);
58 char *ptr = argd, *token;
60 while (token = av_strtok(ptr, ":", &ptr)) {
62 av_strtok(token, "=", &value);
65 if ((ret = av_opt_set(aresample->swr, token, value, 0)) < 0)
69 if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
71 if ((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0)
81 static av_cold void uninit(AVFilterContext *ctx)
83 AResampleContext *aresample = ctx->priv;
84 swr_free(&aresample->swr);
87 static int query_formats(AVFilterContext *ctx)
89 AResampleContext *aresample = ctx->priv;
90 int out_rate = av_get_int(aresample->swr, "osr", NULL);
91 uint64_t out_layout = av_get_int(aresample->swr, "ocl", NULL);
92 enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
94 AVFilterLink *inlink = ctx->inputs[0];
95 AVFilterLink *outlink = ctx->outputs[0];
97 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
98 AVFilterFormats *out_formats;
99 AVFilterFormats *in_samplerates = ff_all_samplerates();
100 AVFilterFormats *out_samplerates;
101 AVFilterChannelLayouts *in_layouts = ff_all_channel_counts();
102 AVFilterChannelLayouts *out_layouts;
104 ff_formats_ref (in_formats, &inlink->out_formats);
105 ff_formats_ref (in_samplerates, &inlink->out_samplerates);
106 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
109 out_samplerates = ff_make_format_list((int[]){ out_rate, -1 });
111 out_samplerates = ff_all_samplerates();
113 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
115 if(out_format != AV_SAMPLE_FMT_NONE) {
116 out_formats = ff_make_format_list((int[]){ out_format, -1 });
118 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
119 ff_formats_ref(out_formats, &outlink->in_formats);
122 out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
124 out_layouts = ff_all_channel_counts();
125 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
131 static int config_output(AVFilterLink *outlink)
134 AVFilterContext *ctx = outlink->src;
135 AVFilterLink *inlink = ctx->inputs[0];
136 AResampleContext *aresample = ctx->priv;
139 enum AVSampleFormat out_format;
140 char inchl_buf[128], outchl_buf[128];
142 aresample->swr = swr_alloc_set_opts(aresample->swr,
143 outlink->channel_layout, outlink->format, outlink->sample_rate,
144 inlink->channel_layout, inlink->format, inlink->sample_rate,
147 return AVERROR(ENOMEM);
148 if (!inlink->channel_layout)
149 av_opt_set_int(aresample->swr, "ich", inlink->channels, 0);
150 if (!outlink->channel_layout)
151 av_opt_set_int(aresample->swr, "och", outlink->channels, 0);
153 ret = swr_init(aresample->swr);
157 out_rate = av_get_int(aresample->swr, "osr", NULL);
158 out_layout = av_get_int(aresample->swr, "ocl", NULL);
159 out_format = av_get_int(aresample->swr, "osf", NULL);
160 outlink->time_base = (AVRational) {1, out_rate};
162 av_assert0(outlink->sample_rate == out_rate);
163 av_assert0(outlink->channel_layout == out_layout || !outlink->channel_layout);
164 av_assert0(outlink->format == out_format);
166 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
168 av_get_channel_layout_string(inchl_buf, sizeof(inchl_buf), inlink ->channels, inlink ->channel_layout);
169 av_get_channel_layout_string(outchl_buf, sizeof(outchl_buf), outlink->channels, outlink->channel_layout);
171 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
172 inlink ->channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
173 outlink->channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
177 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
179 AResampleContext *aresample = inlink->dst->priv;
180 const int n_in = insamplesref->nb_samples;
181 int n_out = n_in * aresample->ratio * 2 + 256;
182 AVFilterLink *const outlink = inlink->dst->outputs[0];
183 AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n_out);
187 return AVERROR(ENOMEM);
189 av_frame_copy_props(outsamplesref, insamplesref);
190 outsamplesref->format = outlink->format;
191 av_frame_set_channels(outsamplesref, outlink->channels);
192 outsamplesref->channel_layout = outlink->channel_layout;
193 outsamplesref->sample_rate = outlink->sample_rate;
195 if(insamplesref->pts != AV_NOPTS_VALUE) {
196 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
197 int64_t outpts= swr_next_pts(aresample->swr, inpts);
198 aresample->next_pts =
199 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
201 outsamplesref->pts = AV_NOPTS_VALUE;
203 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
204 (void *)insamplesref->extended_data, n_in);
206 av_frame_free(&outsamplesref);
207 av_frame_free(&insamplesref);
211 outsamplesref->nb_samples = n_out;
213 ret = ff_filter_frame(outlink, outsamplesref);
214 aresample->req_fullfilled= 1;
215 av_frame_free(&insamplesref);
219 static int request_frame(AVFilterLink *outlink)
221 AVFilterContext *ctx = outlink->src;
222 AResampleContext *aresample = ctx->priv;
223 AVFilterLink *const inlink = outlink->src->inputs[0];
226 aresample->req_fullfilled = 0;
228 ret = ff_request_frame(ctx->inputs[0]);
229 }while(!aresample->req_fullfilled && ret>=0);
231 if (ret == AVERROR_EOF) {
232 AVFrame *outsamplesref;
235 outsamplesref = ff_get_audio_buffer(outlink, n_out);
237 return AVERROR(ENOMEM);
238 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
240 av_frame_free(&outsamplesref);
241 return (n_out == 0) ? AVERROR_EOF : n_out;
244 outsamplesref->sample_rate = outlink->sample_rate;
245 outsamplesref->nb_samples = n_out;
247 outsamplesref->pts = aresample->next_pts;
248 if(aresample->next_pts != AV_NOPTS_VALUE)
249 aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
251 outsamplesref->pts = swr_next_pts(aresample->swr, INT64_MIN);
252 outsamplesref->pts = ROUNDED_DIV(outsamplesref->pts, inlink->sample_rate);
255 return ff_filter_frame(outlink, outsamplesref);
260 static const AVFilterPad aresample_inputs[] = {
263 .type = AVMEDIA_TYPE_AUDIO,
264 .filter_frame = filter_frame,
269 static const AVFilterPad aresample_outputs[] = {
272 .config_props = config_output,
273 .request_frame = request_frame,
274 .type = AVMEDIA_TYPE_AUDIO,
279 AVFilter avfilter_af_aresample = {
281 .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
284 .query_formats = query_formats,
285 .priv_size = sizeof(AResampleContext),
286 .inputs = aresample_inputs,
287 .outputs = aresample_outputs,