FFmpeg
af_apad.c
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
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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  */
20 
21 /**
22  * @file
23  * audio pad filter.
24  *
25  * Based on af_aresample.c
26  */
27 
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 "filters.h"
36 
37 typedef struct APadContext {
38  const AVClass *class;
40 
41  int eof;
47 } APadContext;
48 
49 #define OFFSET(x) offsetof(APadContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption apad_options[] = {
53  { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
54  { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
55  { "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 },
56  { "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
57  { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
58  { NULL }
59 };
60 
62 
64 {
65  APadContext *s = ctx->priv;
66 
67  s->next_pts = AV_NOPTS_VALUE;
68  if (s->whole_len >= 0 && s->pad_len >= 0) {
69  av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
70  return AVERROR(EINVAL);
71  }
72 
73  return 0;
74 }
75 
77 {
78  AVFilterContext *ctx = inlink->dst;
79  APadContext *s = ctx->priv;
80 
81  if (s->whole_len >= 0) {
82  s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
84  "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
85  }
86 
87  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
88  return ff_filter_frame(ctx->outputs[0], frame);
89 }
90 
91 static int push_frame(AVFilterLink *outlink)
92 {
93  AVFilterContext *ctx = outlink->src;
94  APadContext *s = ctx->priv;
95  AVFrame *outsamplesref;
96  int n_out;
97 
98  if (ctx->is_disabled)
99  return 0;
100  n_out = s->packet_size;
101 
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;
109  "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
110  }
111 
112  if (!n_out)
113  return AVERROR_EOF;
114 
115  outsamplesref = ff_get_audio_buffer(outlink, n_out);
116  if (!outsamplesref)
117  return AVERROR(ENOMEM);
118 
119  av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
120  av_assert0(outsamplesref->nb_samples == n_out);
121 
122  av_samples_set_silence(outsamplesref->extended_data, 0,
123  n_out,
124  outsamplesref->ch_layout.nb_channels,
125  outsamplesref->format);
126 
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);
130 
131  return ff_filter_frame(outlink, outsamplesref);
132 }
133 
135 {
136  AVFilterLink *inlink = ctx->inputs[0];
137  AVFilterLink *outlink = ctx->outputs[0];
138  APadContext *s = ctx->priv;
139  int64_t pts;
140  int status;
141 
143 
144  if (!s->eof && ff_inlink_queued_frames(inlink)) {
145  AVFrame *frame = NULL;
146  int ret;
147 
149  if (ret < 0)
150  return ret;
151  if (ret > 0)
152  return filter_frame(inlink, frame);
153  }
154 
155  if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
156  s->eof = status == AVERROR_EOF;
157 
158  if (s->eof) {
159  int ret = push_frame(outlink);
160 
161  if (ret == AVERROR_EOF) {
162  ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
163  return 0;
164  }
165  return ret;
166  }
167 
169 
170  return FFERROR_NOT_READY;
171 }
172 
173 static int config_output(AVFilterLink *outlink)
174 {
175  AVFilterContext *ctx = outlink->src;
176  APadContext *s = ctx->priv;
177 
178  if (s->pad_dur >= 0)
179  s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
180  if (s->whole_dur >= 0)
181  s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE);
182 
183  s->pad_len_left = s->pad_len;
184  s->whole_len_left = s->whole_len;
185 
186  return 0;
187 }
188 
189 static const AVFilterPad apad_outputs[] = {
190  {
191  .name = "default",
192  .type = AVMEDIA_TYPE_AUDIO,
193  .config_props = config_output,
194  },
195 };
196 
198  .name = "apad",
199  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
200  .init = init,
201  .activate = activate,
202  .priv_size = sizeof(APadContext),
205  .priv_class = &apad_class,
207 };
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:98
AVERROR
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
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1023
APadContext::next_pts
int64_t next_pts
Definition: af_apad.c:39
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
int64_t
long long int64_t
Definition: coverity.c:34
inlink
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
Definition: filter_design.txt:212
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_apad.c:173
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
av_samples_set_silence
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:246
AVOption
AVOption.
Definition: opt.h:429
APadContext::whole_dur
int64_t whole_dur
Definition: af_apad.c:46
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition: opt.h:319
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
APadContext::whole_len
int64_t whole_len
Definition: af_apad.c:44
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:434
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1451
APadContext::pad_dur
int64_t pad_dur
Definition: af_apad.c:45
OFFSET
#define OFFSET(x)
Definition: af_apad.c:49
apad_options
static const AVOption apad_options[]
Definition: af_apad.c:52
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:63
APadContext::eof
int eof
Definition: af_apad.c:41
samplefmt.h
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:790
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
APadContext::pad_len_left
int64_t pad_len_left
Definition: af_apad.c:43
push_frame
static int push_frame(AVFilterLink *outlink)
Definition: af_apad.c:91
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
APadContext::whole_len_left
int64_t whole_len_left
Definition: af_apad.c:44
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:263
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
APadContext::pad_len
int64_t pad_len
Definition: af_apad.c:43
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(apad)
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_rescale_q
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
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1398
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1414
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:588
APadContext
Definition: af_apad.c:37
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:476
ff_af_apad
const AVFilter ff_af_apad
Definition: af_apad.c:197
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:450
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
av_rescale
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
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
frame
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
Definition: filter_design.txt:264
APadContext::packet_size
int packet_size
Definition: af_apad.c:42
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
apad_outputs
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:189
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
audio.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:76
activate
static int activate(AVFilterContext *ctx)
Definition: af_apad.c:134
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:190
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
A
#define A
Definition: af_apad.c:50
avstring.h