FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_aresample.c
Go to the documentation of this file.
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"
29 #include "libavutil/opt.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/avassert.h"
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "internal.h"
36 
37 typedef struct {
38  const AVClass *class;
40  double ratio;
41  struct SwrContext *swr;
42  int64_t next_pts;
45 
47 {
48  AResampleContext *aresample = ctx->priv;
49  int ret = 0;
50 
51  aresample->next_pts = AV_NOPTS_VALUE;
52  aresample->swr = swr_alloc();
53  if (!aresample->swr) {
54  ret = AVERROR(ENOMEM);
55  goto end;
56  }
57 
58  if (opts) {
59  AVDictionaryEntry *e = NULL;
60 
61  while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
62  if ((ret = av_opt_set(aresample->swr, e->key, e->value, 0)) < 0)
63  goto end;
64  }
65  av_dict_free(opts);
66  }
67  if (aresample->sample_rate_arg > 0)
68  av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
69 end:
70  return ret;
71 }
72 
73 static av_cold void uninit(AVFilterContext *ctx)
74 {
75  AResampleContext *aresample = ctx->priv;
76  swr_free(&aresample->swr);
77 }
78 
80 {
81  AResampleContext *aresample = ctx->priv;
82  int out_rate = av_get_int(aresample->swr, "osr", NULL);
83  uint64_t out_layout = av_get_int(aresample->swr, "ocl", NULL);
84  enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
85 
86  AVFilterLink *inlink = ctx->inputs[0];
87  AVFilterLink *outlink = ctx->outputs[0];
88 
90  AVFilterFormats *out_formats;
91  AVFilterFormats *in_samplerates = ff_all_samplerates();
92  AVFilterFormats *out_samplerates;
94  AVFilterChannelLayouts *out_layouts;
95 
96  ff_formats_ref (in_formats, &inlink->out_formats);
97  ff_formats_ref (in_samplerates, &inlink->out_samplerates);
98  ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
99 
100  if(out_rate > 0) {
101  int ratelist[] = { out_rate, -1 };
102  out_samplerates = ff_make_format_list(ratelist);
103  } else {
104  out_samplerates = ff_all_samplerates();
105  }
106  if (!out_samplerates) {
107  av_log(ctx, AV_LOG_ERROR, "Cannot allocate output samplerates.\n");
108  return AVERROR(ENOMEM);
109  }
110 
111  ff_formats_ref(out_samplerates, &outlink->in_samplerates);
112 
113  if(out_format != AV_SAMPLE_FMT_NONE) {
114  int formatlist[] = { out_format, -1 };
115  out_formats = ff_make_format_list(formatlist);
116  } else
117  out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
118  ff_formats_ref(out_formats, &outlink->in_formats);
119 
120  if(out_layout) {
121  int64_t layout_list[] = { out_layout, -1 };
122  out_layouts = avfilter_make_format64_list(layout_list);
123  } else
124  out_layouts = ff_all_channel_counts();
125  ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
126 
127  return 0;
128 }
129 
130 
131 static int config_output(AVFilterLink *outlink)
132 {
133  int ret;
134  AVFilterContext *ctx = outlink->src;
135  AVFilterLink *inlink = ctx->inputs[0];
136  AResampleContext *aresample = ctx->priv;
137  int out_rate;
138  uint64_t out_layout;
139  enum AVSampleFormat out_format;
140  char inchl_buf[128], outchl_buf[128];
141 
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,
145  0, ctx);
146  if (!aresample->swr)
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);
152 
153  ret = swr_init(aresample->swr);
154  if (ret < 0)
155  return ret;
156 
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};
161 
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);
165 
166  aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
167 
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);
170 
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);
174  return 0;
175 }
176 
177 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
178 {
179  AResampleContext *aresample = inlink->dst->priv;
180  const int n_in = insamplesref->nb_samples;
181  int64_t delay;
182  int n_out = n_in * aresample->ratio + 32;
183  AVFilterLink *const outlink = inlink->dst->outputs[0];
184  AVFrame *outsamplesref;
185  int ret;
186 
187  delay = swr_get_delay(aresample->swr, outlink->sample_rate);
188  if (delay > 0)
189  n_out += delay;
190 
191  outsamplesref = ff_get_audio_buffer(outlink, n_out);
192 
193  if(!outsamplesref)
194  return AVERROR(ENOMEM);
195 
196  av_frame_copy_props(outsamplesref, insamplesref);
197  outsamplesref->format = outlink->format;
198  av_frame_set_channels(outsamplesref, outlink->channels);
199  outsamplesref->channel_layout = outlink->channel_layout;
200  outsamplesref->sample_rate = outlink->sample_rate;
201 
202  if(insamplesref->pts != AV_NOPTS_VALUE) {
203  int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
204  int64_t outpts= swr_next_pts(aresample->swr, inpts);
205  aresample->next_pts =
206  outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
207  } else {
208  outsamplesref->pts = AV_NOPTS_VALUE;
209  }
210  n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
211  (void *)insamplesref->extended_data, n_in);
212  if (n_out <= 0) {
213  av_frame_free(&outsamplesref);
214  av_frame_free(&insamplesref);
215  return 0;
216  }
217 
218  outsamplesref->nb_samples = n_out;
219 
220  ret = ff_filter_frame(outlink, outsamplesref);
221  aresample->req_fullfilled= 1;
222  av_frame_free(&insamplesref);
223  return ret;
224 }
225 
226 static int request_frame(AVFilterLink *outlink)
227 {
228  AVFilterContext *ctx = outlink->src;
229  AResampleContext *aresample = ctx->priv;
230  AVFilterLink *const inlink = outlink->src->inputs[0];
231  int ret;
232 
233  aresample->req_fullfilled = 0;
234  do{
235  ret = ff_request_frame(ctx->inputs[0]);
236  }while(!aresample->req_fullfilled && ret>=0);
237 
238  if (ret == AVERROR_EOF) {
239  AVFrame *outsamplesref;
240  int n_out = 4096;
241  int64_t pts;
242 
243  outsamplesref = ff_get_audio_buffer(outlink, n_out);
244  if (!outsamplesref)
245  return AVERROR(ENOMEM);
246 
247  pts = swr_next_pts(aresample->swr, INT64_MIN);
248  pts = ROUNDED_DIV(pts, inlink->sample_rate);
249 
250  n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
251  if (n_out <= 0) {
252  av_frame_free(&outsamplesref);
253  return (n_out == 0) ? AVERROR_EOF : n_out;
254  }
255 
256  outsamplesref->sample_rate = outlink->sample_rate;
257  outsamplesref->nb_samples = n_out;
258 
259  outsamplesref->pts = pts;
260 
261  return ff_filter_frame(outlink, outsamplesref);
262  }
263  return ret;
264 }
265 
266 static const AVClass *resample_child_class_next(const AVClass *prev)
267 {
268  return prev ? NULL : swr_get_class();
269 }
270 
271 static void *resample_child_next(void *obj, void *prev)
272 {
273  AResampleContext *s = obj;
274  return prev ? NULL : s->swr;
275 }
276 
277 #define OFFSET(x) offsetof(AResampleContext, x)
278 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
279 
280 static const AVOption options[] = {
281  {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
282  {NULL}
283 };
284 
285 static const AVClass aresample_class = {
286  .class_name = "aresample",
287  .item_name = av_default_item_name,
288  .option = options,
289  .version = LIBAVUTIL_VERSION_INT,
290  .child_class_next = resample_child_class_next,
292 };
293 
294 static const AVFilterPad aresample_inputs[] = {
295  {
296  .name = "default",
297  .type = AVMEDIA_TYPE_AUDIO,
298  .filter_frame = filter_frame,
299  },
300  { NULL }
301 };
302 
303 static const AVFilterPad aresample_outputs[] = {
304  {
305  .name = "default",
306  .config_props = config_output,
307  .request_frame = request_frame,
308  .type = AVMEDIA_TYPE_AUDIO,
309  },
310  { NULL }
311 };
312 
314  .name = "aresample",
315  .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
316  .init_dict = init_dict,
317  .uninit = uninit,
318  .query_formats = query_formats,
319  .priv_size = sizeof(AResampleContext),
320  .priv_class = &aresample_class,
321  .inputs = aresample_inputs,
322  .outputs = aresample_outputs,
323 };