FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_resample.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * sample format and channel layout conversion audio filter
22  */
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
30 
32 
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 
38 typedef struct ResampleContext {
39  const AVClass *class;
42 
43  int64_t next_pts;
44  int64_t next_in_pts;
45 
46  /* set by filter_frame() to signal an output frame to request_frame() */
49 
50 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
51 {
52  ResampleContext *s = ctx->priv;
53  const AVClass *avr_class = avresample_get_class();
55 
56  while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
57  if (av_opt_find(&avr_class, e->key, NULL, 0,
59  av_dict_set(&s->options, e->key, e->value, 0);
60  }
61 
62  e = NULL;
63  while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
64  av_dict_set(opts, e->key, NULL, 0);
65 
66  /* do not allow the user to override basic format options */
67  av_dict_set(&s->options, "in_channel_layout", NULL, 0);
68  av_dict_set(&s->options, "out_channel_layout", NULL, 0);
69  av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
70  av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
71  av_dict_set(&s->options, "in_sample_rate", NULL, 0);
72  av_dict_set(&s->options, "out_sample_rate", NULL, 0);
73 
74  return 0;
75 }
76 
77 static av_cold void uninit(AVFilterContext *ctx)
78 {
79  ResampleContext *s = ctx->priv;
80 
81  if (s->avr) {
83  avresample_free(&s->avr);
84  }
85  av_dict_free(&s->options);
86 }
87 
89 {
90  AVFilterLink *inlink = ctx->inputs[0];
91  AVFilterLink *outlink = ctx->outputs[0];
92 
95  AVFilterFormats *in_samplerates = ff_all_samplerates();
96  AVFilterFormats *out_samplerates = ff_all_samplerates();
99 
100  ff_formats_ref(in_formats, &inlink->out_formats);
101  ff_formats_ref(out_formats, &outlink->in_formats);
102 
103  ff_formats_ref(in_samplerates, &inlink->out_samplerates);
104  ff_formats_ref(out_samplerates, &outlink->in_samplerates);
105 
106  ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
107  ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
108 
109  return 0;
110 }
111 
112 static int config_output(AVFilterLink *outlink)
113 {
114  AVFilterContext *ctx = outlink->src;
115  AVFilterLink *inlink = ctx->inputs[0];
116  ResampleContext *s = ctx->priv;
117  char buf1[64], buf2[64];
118  int ret;
119 
120  if (s->avr) {
121  avresample_close(s->avr);
122  avresample_free(&s->avr);
123  }
124 
125  if (inlink->channel_layout == outlink->channel_layout &&
126  inlink->sample_rate == outlink->sample_rate &&
127  (inlink->format == outlink->format ||
130  av_get_planar_sample_fmt(inlink->format) ==
131  av_get_planar_sample_fmt(outlink->format))))
132  return 0;
133 
134  if (!(s->avr = avresample_alloc_context()))
135  return AVERROR(ENOMEM);
136 
137  if (s->options) {
138  int ret;
139  AVDictionaryEntry *e = NULL;
140  while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
141  av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
142 
143  ret = av_opt_set_dict(s->avr, &s->options);
144  if (ret < 0)
145  return ret;
146  }
147 
148  av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
149  av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
150  av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
151  av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
152  av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
153  av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
154 
155  if ((ret = avresample_open(s->avr)) < 0)
156  return ret;
157 
158  outlink->time_base = (AVRational){ 1, outlink->sample_rate };
161 
162  av_get_channel_layout_string(buf1, sizeof(buf1),
163  -1, inlink ->channel_layout);
164  av_get_channel_layout_string(buf2, sizeof(buf2),
165  -1, outlink->channel_layout);
166  av_log(ctx, AV_LOG_VERBOSE,
167  "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
168  av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
169  av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
170 
171  return 0;
172 }
173 
174 static int request_frame(AVFilterLink *outlink)
175 {
176  AVFilterContext *ctx = outlink->src;
177  ResampleContext *s = ctx->priv;
178  int ret = 0;
179 
180  s->got_output = 0;
181  while (ret >= 0 && !s->got_output)
182  ret = ff_request_frame(ctx->inputs[0]);
183 
184  /* flush the lavr delay buffer */
185  if (ret == AVERROR_EOF && s->avr) {
186  AVFrame *frame;
187  int nb_samples = avresample_get_out_samples(s->avr, 0);
188 
189  if (!nb_samples)
190  return ret;
191 
192  frame = ff_get_audio_buffer(outlink, nb_samples);
193  if (!frame)
194  return AVERROR(ENOMEM);
195 
196  ret = avresample_convert(s->avr, frame->extended_data,
197  frame->linesize[0], nb_samples,
198  NULL, 0, 0);
199  if (ret <= 0) {
200  av_frame_free(&frame);
201  return (ret == 0) ? AVERROR_EOF : ret;
202  }
203 
204  frame->pts = s->next_pts;
205  return ff_filter_frame(outlink, frame);
206  }
207  return ret;
208 }
209 
210 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
211 {
212  AVFilterContext *ctx = inlink->dst;
213  ResampleContext *s = ctx->priv;
214  AVFilterLink *outlink = ctx->outputs[0];
215  int ret;
216 
217  if (s->avr) {
218  AVFrame *out;
219  int delay, nb_samples;
220 
221  /* maximum possible samples lavr can output */
222  delay = avresample_get_delay(s->avr);
223  nb_samples = avresample_get_out_samples(s->avr, in->nb_samples);
224 
225  out = ff_get_audio_buffer(outlink, nb_samples);
226  if (!out) {
227  ret = AVERROR(ENOMEM);
228  goto fail;
229  }
230 
231  ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
232  nb_samples, in->extended_data, in->linesize[0],
233  in->nb_samples);
234  if (ret <= 0) {
235  av_frame_free(&out);
236  if (ret < 0)
237  goto fail;
238  }
239 
241 
242  if (s->next_pts == AV_NOPTS_VALUE) {
243  if (in->pts == AV_NOPTS_VALUE) {
244  av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
245  "assuming 0.\n");
246  s->next_pts = 0;
247  } else
248  s->next_pts = av_rescale_q(in->pts, inlink->time_base,
249  outlink->time_base);
250  }
251 
252  if (ret > 0) {
253  out->nb_samples = ret;
254 
255  ret = av_frame_copy_props(out, in);
256  if (ret < 0) {
257  av_frame_free(&out);
258  goto fail;
259  }
260 
261  out->sample_rate = outlink->sample_rate;
262  /* Only convert in->pts if there is a discontinuous jump.
263  This ensures that out->pts tracks the number of samples actually
264  output by the resampler in the absence of such a jump.
265  Otherwise, the rounding in av_rescale_q() and av_rescale()
266  causes off-by-1 errors. */
267  if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
268  out->pts = av_rescale_q(in->pts, inlink->time_base,
269  outlink->time_base) -
270  av_rescale(delay, outlink->sample_rate,
271  inlink->sample_rate);
272  } else
273  out->pts = s->next_pts;
274 
275  s->next_pts = out->pts + out->nb_samples;
276  s->next_in_pts = in->pts + in->nb_samples;
277 
278  ret = ff_filter_frame(outlink, out);
279  s->got_output = 1;
280  }
281 
282 fail:
283  av_frame_free(&in);
284  } else {
285  in->format = outlink->format;
286  ret = ff_filter_frame(outlink, in);
287  s->got_output = 1;
288  }
289 
290  return ret;
291 }
292 
293 static const AVClass *resample_child_class_next(const AVClass *prev)
294 {
295  return prev ? NULL : avresample_get_class();
296 }
297 
298 static void *resample_child_next(void *obj, void *prev)
299 {
300  ResampleContext *s = obj;
301  return prev ? NULL : s->avr;
302 }
303 
304 static const AVClass resample_class = {
305  .class_name = "resample",
306  .item_name = av_default_item_name,
307  .version = LIBAVUTIL_VERSION_INT,
308  .child_class_next = resample_child_class_next,
310 };
311 
313  {
314  .name = "default",
315  .type = AVMEDIA_TYPE_AUDIO,
316  .filter_frame = filter_frame,
317  },
318  { NULL }
319 };
320 
322  {
323  .name = "default",
324  .type = AVMEDIA_TYPE_AUDIO,
325  .config_props = config_output,
326  .request_frame = request_frame
327  },
328  { NULL }
329 };
330 
332  .name = "resample",
333  .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
334  .priv_size = sizeof(ResampleContext),
335  .priv_class = &resample_class,
336  .init_dict = init,
337  .uninit = uninit,
339  .inputs = avfilter_af_resample_inputs,
340  .outputs = avfilter_af_resample_outputs,
341 };