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