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 #include "internal.h"
37 
38 typedef struct APadContext {
39  const AVClass *class;
40  int64_t next_pts;
41 
42  int eof;
46  int64_t pad_dur;
47  int64_t whole_dur;
48 } APadContext;
49 
50 #define OFFSET(x) offsetof(APadContext, x)
51 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 
53 static const AVOption apad_options[] = {
54  { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
55  { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
56  { "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 },
57  { "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
58  { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
59  { NULL }
60 };
61 
63 
65 {
66  APadContext *s = ctx->priv;
67 
68  s->next_pts = AV_NOPTS_VALUE;
69  if (s->whole_len >= 0 && s->pad_len >= 0) {
70  av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
71  return AVERROR(EINVAL);
72  }
73 
74  return 0;
75 }
76 
78 {
79  AVFilterContext *ctx = inlink->dst;
80  APadContext *s = ctx->priv;
81 
82  if (s->whole_len >= 0) {
83  s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
85  "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
86  }
87 
88  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
89  return ff_filter_frame(ctx->outputs[0], frame);
90 }
91 
92 static int push_frame(AVFilterLink *outlink)
93 {
94  AVFilterContext *ctx = outlink->src;
95  APadContext *s = ctx->priv;
96  AVFrame *outsamplesref;
97  int n_out;
98 
99  if (ctx->is_disabled)
100  return 0;
101  n_out = s->packet_size;
102 
103  if (s->whole_len >= 0 && s->pad_len < 0) {
104  s->pad_len = s->pad_len_left = s->whole_len_left;
105  }
106  if (s->pad_len >=0 || s->whole_len >= 0) {
107  n_out = FFMIN(n_out, s->pad_len_left);
108  s->pad_len_left -= n_out;
110  "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
111  }
112 
113  if (!n_out)
114  return AVERROR_EOF;
115 
116  outsamplesref = ff_get_audio_buffer(outlink, n_out);
117  if (!outsamplesref)
118  return AVERROR(ENOMEM);
119 
120  av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
121  av_assert0(outsamplesref->nb_samples == n_out);
122 
123  av_samples_set_silence(outsamplesref->extended_data, 0,
124  n_out,
125  outsamplesref->ch_layout.nb_channels,
126  outsamplesref->format);
127 
128  outsamplesref->pts = s->next_pts;
129  if (s->next_pts != AV_NOPTS_VALUE)
130  s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
131 
132  return ff_filter_frame(outlink, outsamplesref);
133 }
134 
136 {
137  AVFilterLink *inlink = ctx->inputs[0];
138  AVFilterLink *outlink = ctx->outputs[0];
139  APadContext *s = ctx->priv;
140  int64_t pts;
141  int status;
142 
144 
145  if (!s->eof && ff_inlink_queued_frames(inlink)) {
146  AVFrame *frame = NULL;
147  int ret;
148 
150  if (ret < 0)
151  return ret;
152  if (ret > 0)
153  return filter_frame(inlink, frame);
154  }
155 
156  if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
157  s->eof = status == AVERROR_EOF;
158 
159  if (s->eof) {
160  int ret = push_frame(outlink);
161 
162  if (ret == AVERROR_EOF) {
163  ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
164  return 0;
165  }
166  return ret;
167  }
168 
170 
171  return FFERROR_NOT_READY;
172 }
173 
174 static int config_output(AVFilterLink *outlink)
175 {
176  AVFilterContext *ctx = outlink->src;
177  APadContext *s = ctx->priv;
178 
179  if (s->pad_dur >= 0)
180  s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
181  if (s->whole_dur >= 0)
182  s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE);
183 
184  s->pad_len_left = s->pad_len;
185  s->whole_len_left = s->whole_len;
186 
187  return 0;
188 }
189 
190 static const AVFilterPad apad_outputs[] = {
191  {
192  .name = "default",
193  .type = AVMEDIA_TYPE_AUDIO,
194  .config_props = config_output,
195  },
196 };
197 
199  .name = "apad",
200  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
201  .init = init,
202  .activate = activate,
203  .priv_size = sizeof(APadContext),
206  .priv_class = &apad_class,
208 };
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:97
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:1018
APadContext::next_pts
int64_t next_pts
Definition: af_apad.c:40
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
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:174
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:487
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:346
APadContext::whole_dur
int64_t whole_dur
Definition: af_apad.c:47
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:249
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
APadContext::whole_len
int64_t whole_len
Definition: af_apad.c:45
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
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:199
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:1445
APadContext::pad_dur
int64_t pad_dur
Definition: af_apad.c:46
OFFSET
#define OFFSET(x)
Definition: af_apad.c:50
apad_options
static const AVOption apad_options[]
Definition: af_apad.c:53
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:64
APadContext::eof
int eof
Definition: af_apad.c:42
samplefmt.h
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:776
pts
static int64_t pts
Definition: transcode_aac.c:643
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
APadContext::pad_len_left
int64_t pad_len_left
Definition: af_apad.c:44
push_frame
static int push_frame(AVFilterLink *outlink)
Definition: af_apad.c:92
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:189
APadContext::whole_len_left
int64_t whole_len_left
Definition: af_apad.c:45
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
Definition: opt.h:236
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:44
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:48
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_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:33
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:1392
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1408
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:106
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:574
APadContext
Definition: af_apad.c:38
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:462
ff_af_apad
const AVFilter ff_af_apad
Definition: af_apad.c:198
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:455
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:436
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
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:166
ret
ret
Definition: filter_design.txt:187
APadContext::packet_size
int packet_size
Definition: af_apad.c:43
status
ov_status_e status
Definition: dnn_backend_openvino.c:120
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
apad_outputs
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:190
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
audio.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:77
activate
static int activate(AVFilterContext *ctx)
Definition: af_apad.c:135
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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:155
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
A
#define A
Definition: af_apad.c:51
avstring.h