Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Michael Niedermayer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
21 /**
22  * @file
23  * audio pad filter.
24  *
25  * Based on af_aresample.c
26  */
28 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/samplefmt.h"
32 #include "libavutil/avassert.h"
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "internal.h"
37 typedef struct APadContext {
38  const AVClass *class;
39  int64_t next_pts;
44  int64_t pad_dur;
45  int64_t whole_dur;
46 } APadContext;
48 #define OFFSET(x) offsetof(APadContext, x)
51 static const AVOption apad_options[] = {
52  { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
53  { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
54  { "whole_len", "set minimum target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
55  { "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, A },
56  { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, A },
57  { NULL }
58 };
63 {
64  APadContext *s = ctx->priv;
67  if (s->whole_len >= 0 && s->pad_len >= 0) {
68  av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
69  return AVERROR(EINVAL);
70  }
72  return 0;
73 }
76 {
77  AVFilterContext *ctx = inlink->dst;
78  APadContext *s = ctx->priv;
80  if (s->whole_len >= 0) {
81  s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
82  av_log(ctx, AV_LOG_DEBUG,
83  "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
84  }
86  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
87  return ff_filter_frame(ctx->outputs[0], frame);
88 }
90 static int request_frame(AVFilterLink *outlink)
91 {
92  AVFilterContext *ctx = outlink->src;
93  APadContext *s = ctx->priv;
94  int ret;
96  ret = ff_request_frame(ctx->inputs[0]);
98  if (ret == AVERROR_EOF && !ctx->is_disabled) {
99  int n_out = s->packet_size;
100  AVFrame *outsamplesref;
102  if (s->whole_len >= 0 && s->pad_len < 0) {
103  s->pad_len = s->pad_len_left = s->whole_len_left;
104  }
105  if (s->pad_len >=0 || s->whole_len >= 0) {
106  n_out = FFMIN(n_out, s->pad_len_left);
107  s->pad_len_left -= n_out;
108  av_log(ctx, AV_LOG_DEBUG,
109  "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
110  }
112  if (!n_out)
113  return AVERROR_EOF;
115  outsamplesref = ff_get_audio_buffer(outlink, n_out);
116  if (!outsamplesref)
117  return AVERROR(ENOMEM);
119  av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
120  av_assert0(outsamplesref->nb_samples == n_out);
122  av_samples_set_silence(outsamplesref->extended_data, 0,
123  n_out,
124  outsamplesref->channels,
125  outsamplesref->format);
127  outsamplesref->pts = s->next_pts;
128  if (s->next_pts != AV_NOPTS_VALUE)
129  s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
131  return ff_filter_frame(outlink, outsamplesref);
132  }
133  return ret;
134 }
136 static int config_output(AVFilterLink *outlink)
137 {
138  AVFilterContext *ctx = outlink->src;
139  APadContext *s = ctx->priv;
141  if (s->pad_dur)
142  s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
143  if (s->whole_dur)
146  s->pad_len_left = s->pad_len;
147  s->whole_len_left = s->whole_len;
149  return 0;
150 }
152 static const AVFilterPad apad_inputs[] = {
153  {
154  .name = "default",
155  .type = AVMEDIA_TYPE_AUDIO,
156  .filter_frame = filter_frame,
157  },
158  { NULL }
159 };
161 static const AVFilterPad apad_outputs[] = {
162  {
163  .name = "default",
164  .request_frame = request_frame,
165  .config_props = config_output,
166  .type = AVMEDIA_TYPE_AUDIO,
167  },
168  { NULL }
169 };
172  .name = "apad",
173  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
174  .init = init,
175  .priv_size = sizeof(APadContext),
176  .inputs = apad_inputs,
177  .outputs = apad_outputs,
178  .priv_class = &apad_class,
180 };
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: af_apad.c:48
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
Definition: opt.h:248
Main libavfilter public API header.
static const AVFilterPad apad_inputs[]
Definition: af_apad.c:152
int64_t pad_len
Definition: af_apad.c:42
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:388
const char * name
Pad name.
Definition: internal.h:60
int64_t whole_dur
Definition: af_apad.c:45
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
#define av_cold
Definition: attributes.h:88
int64_t next_pts
Definition: af_apad.c:39
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:407
int64_t pad_dur
Definition: af_apad.c:44
int packet_size
Definition: af_apad.c:41
End of file.
Definition: error.h:55
#define av_log(a,...)
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:161
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:237
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
void * priv
private data for use by the filter
Definition: avfilter.h:356
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
simple assert() macros that are a bit more flexible than ISO C assert().
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:75
#define FFMAX(a, b)
Definition: common.h:103
#define A
Definition: af_apad.c:49
int channels
number of audio channels, only used for audio.
Definition: frame.h:620
audio channel layout utility functions
int64_t pad_len_left
Definition: af_apad.c:42
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:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:105
AVFormatContext * ctx
Definition: movenc.c:48
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
static int request_frame(AVFilterLink *outlink)
Definition: af_apad.c:90
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:387
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:62
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Describe the class of an AVClass context structure.
Definition: log.h:67
int sample_rate
Sample rate of the audio data.
Definition: frame.h:486
Filter definition.
Definition: avfilter.h:145
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:149
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:134
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
#define flags(name, subs,...)
Definition: cbs_av1.c:561
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
int64_t whole_len_left
Definition: af_apad.c:43
static const AVOption apad_options[]
Definition: af_apad.c:51
int64_t whole_len
Definition: af_apad.c:43
static int config_output(AVFilterLink *outlink)
Definition: af_apad.c:136
An instance of a filter.
Definition: avfilter.h:341
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:361
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:380
AVFilter ff_af_apad
Definition: af_apad.c:171
Undefined timestamp value.
Definition: avutil.h:248