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/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 {
40 
41  int64_t next_pts;
42 
43  /* set by filter_frame() to signal an output frame to request_frame() */
46 
47 static av_cold void uninit(AVFilterContext *ctx)
48 {
49  ResampleContext *s = ctx->priv;
50 
51  if (s->avr) {
53  avresample_free(&s->avr);
54  }
55 }
56 
58 {
59  AVFilterLink *inlink = ctx->inputs[0];
60  AVFilterLink *outlink = ctx->outputs[0];
61 
64  AVFilterFormats *in_samplerates = ff_all_samplerates();
65  AVFilterFormats *out_samplerates = ff_all_samplerates();
68 
69  ff_formats_ref(in_formats, &inlink->out_formats);
70  ff_formats_ref(out_formats, &outlink->in_formats);
71 
72  ff_formats_ref(in_samplerates, &inlink->out_samplerates);
73  ff_formats_ref(out_samplerates, &outlink->in_samplerates);
74 
75  ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
76  ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
77 
78  return 0;
79 }
80 
81 static int config_output(AVFilterLink *outlink)
82 {
83  AVFilterContext *ctx = outlink->src;
84  AVFilterLink *inlink = ctx->inputs[0];
85  ResampleContext *s = ctx->priv;
86  char buf1[64], buf2[64];
87  int ret;
88 
89  if (s->avr) {
91  avresample_free(&s->avr);
92  }
93 
94  if (inlink->channel_layout == outlink->channel_layout &&
95  inlink->sample_rate == outlink->sample_rate &&
96  (inlink->format == outlink->format ||
100  av_get_planar_sample_fmt(outlink->format))))
101  return 0;
102 
103  if (!(s->avr = avresample_alloc_context()))
104  return AVERROR(ENOMEM);
105 
106  av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
107  av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
108  av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
109  av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
110  av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
111  av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
112 
113  if ((ret = avresample_open(s->avr)) < 0)
114  return ret;
115 
116  outlink->time_base = (AVRational){ 1, outlink->sample_rate };
118 
119  av_get_channel_layout_string(buf1, sizeof(buf1),
120  -1, inlink ->channel_layout);
121  av_get_channel_layout_string(buf2, sizeof(buf2),
122  -1, outlink->channel_layout);
123  av_log(ctx, AV_LOG_VERBOSE,
124  "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
125  av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
126  av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
127 
128  return 0;
129 }
130 
131 static int request_frame(AVFilterLink *outlink)
132 {
133  AVFilterContext *ctx = outlink->src;
134  ResampleContext *s = ctx->priv;
135  int ret = 0;
136 
137  s->got_output = 0;
138  while (ret >= 0 && !s->got_output)
139  ret = ff_request_frame(ctx->inputs[0]);
140 
141  /* flush the lavr delay buffer */
142  if (ret == AVERROR_EOF && s->avr) {
143  AVFilterBufferRef *buf;
145  outlink->sample_rate,
146  ctx->inputs[0]->sample_rate,
147  AV_ROUND_UP);
148 
149  if (!nb_samples)
150  return ret;
151 
152  buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
153  if (!buf)
154  return AVERROR(ENOMEM);
155 
156  ret = avresample_convert(s->avr, buf->extended_data,
157  buf->linesize[0], nb_samples,
158  NULL, 0, 0);
159  if (ret <= 0) {
161  return (ret == 0) ? AVERROR_EOF : ret;
162  }
163 
164  buf->pts = s->next_pts;
165  return ff_filter_frame(outlink, buf);
166  }
167  return ret;
168 }
169 
170 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
171 {
172  AVFilterContext *ctx = inlink->dst;
173  ResampleContext *s = ctx->priv;
174  AVFilterLink *outlink = ctx->outputs[0];
175  int ret;
176 
177  if (s->avr) {
178  AVFilterBufferRef *buf_out;
179  int delay, nb_samples;
180 
181  /* maximum possible samples lavr can output */
182  delay = avresample_get_delay(s->avr);
183  nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
184  outlink->sample_rate, inlink->sample_rate,
185  AV_ROUND_UP);
186 
187  buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
188  if (!buf_out) {
189  ret = AVERROR(ENOMEM);
190  goto fail;
191  }
192 
193  ret = avresample_convert(s->avr, buf_out->extended_data,
194  buf_out->linesize[0], nb_samples,
195  buf->extended_data, buf->linesize[0],
196  buf->audio->nb_samples);
197  if (ret <= 0) {
198  avfilter_unref_buffer(buf_out);
199  if (ret < 0)
200  goto fail;
201  }
202 
204 
205  if (s->next_pts == AV_NOPTS_VALUE) {
206  if (buf->pts == AV_NOPTS_VALUE) {
207  av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
208  "assuming 0.\n");
209  s->next_pts = 0;
210  } else
211  s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
212  outlink->time_base);
213  }
214 
215  if (ret > 0) {
216  buf_out->audio->nb_samples = ret;
217  if (buf->pts != AV_NOPTS_VALUE) {
218  buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
219  outlink->time_base) -
220  av_rescale(delay, outlink->sample_rate,
221  inlink->sample_rate);
222  } else
223  buf_out->pts = s->next_pts;
224 
225  s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
226 
227  ret = ff_filter_frame(outlink, buf_out);
228  s->got_output = 1;
229  }
230 
231 fail:
233  } else {
234  buf->format = outlink->format;
235  ret = ff_filter_frame(outlink, buf);
236  s->got_output = 1;
237  }
238 
239  return ret;
240 }
241 
243  {
244  .name = "default",
245  .type = AVMEDIA_TYPE_AUDIO,
246  .filter_frame = filter_frame,
247  .min_perms = AV_PERM_READ
248  },
249  { NULL }
250 };
251 
253  {
254  .name = "default",
255  .type = AVMEDIA_TYPE_AUDIO,
256  .config_props = config_output,
257  .request_frame = request_frame
258  },
259  { NULL }
260 };
261 
263  .name = "resample",
264  .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
265  .priv_size = sizeof(ResampleContext),
266 
267  .uninit = uninit,
269 
270  .inputs = avfilter_af_resample_inputs,
271  .outputs = avfilter_af_resample_outputs,
272 };
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
AVAudioResampleContext * avr
Definition: af_resample.c:39
AVFilterBufferRef * ff_get_audio_buffer(AVFilterLink *link, int perms, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:63
static const AVFilterPad avfilter_af_resample_outputs[]
Definition: af_resample.c:252
int nb_samples
number of audio samples
Definition: avfilter.h:111
struct ResampleContext ResampleContext
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
void avresample_free(AVAudioResampleContext **avr)
Free AVAudioResampleContext and associated AVOption values.
Definition: utils.c:202
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: buffer.c:75
AVFilter avfilter_af_resample
Definition: af_resample.c:262
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Round toward +infinity.
Definition: mathematics.h:53
AVOptions.
int64_t next_pts
Definition: af_resample.c:41
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:257
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition: samplefmt.c:73
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_resample.c:47
void avresample_close(AVAudioResampleContext *avr)
Close AVAudioResampleContext.
Definition: utils.c:188
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by Libav for the given media type.
Definition: formats.c:209
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
static int config_output(AVFilterLink *outlink)
Definition: af_resample.c:81
A filter pad used for either input or output.
Definition: internal.h:33
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:122
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:262
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
Definition: af_resample.c:170
struct AVRational AVRational
rational number numerator/denominator
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:110
external API header
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/...
Definition: formats.c:244
int avresample_get_delay(AVAudioResampleContext *avr)
Return the number of samples currently in the resampling delay buffer.
Definition: resample.c:462
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
int avresample_available(AVAudioResampleContext *avr)
Return the number of available samples in the output FIFO.
Definition: utils.c:471
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:262
out nb_samples
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t **input, int in_plane_size, int in_samples)
Convert input samples and write them to the output FIFO.
Definition: utils.c:252
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:238
static int request_frame(AVFilterLink *outlink)
Definition: af_resample.c:131
AVAudioResampleContext * avresample_alloc_context(void)
Allocate AVAudioResampleContext and set options.
Definition: options.c:82
static int query_formats(AVFilterContext *ctx)
Definition: af_resample.c:57
common internal and external API header
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
int format
media format
Definition: avfilter.h:170
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:418
static const AVFilterPad avfilter_af_resample_inputs[]
Definition: af_resample.c:242
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:239
internal API functions
int avresample_open(AVAudioResampleContext *avr)
Initialize AVAudioResampleContext.
Definition: utils.c:32
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.