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 "internal.h"
36 
37 typedef struct APadContext {
38  const AVClass *class;
39  int64_t next_pts;
40 
44  int64_t pad_dur;
45  int64_t whole_dur;
46 } APadContext;
47 
48 #define OFFSET(x) offsetof(APadContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 
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 = -1 }, -1, INT64_MAX, A },
56  { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
57  { NULL }
58 };
59 
61 
63 {
64  APadContext *s = ctx->priv;
65 
66  s->next_pts = AV_NOPTS_VALUE;
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  }
71 
72  return 0;
73 }
74 
76 {
77  AVFilterContext *ctx = inlink->dst;
78  APadContext *s = ctx->priv;
79 
80  if (s->whole_len >= 0) {
81  s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
83  "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
84  }
85 
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 }
89 
90 static int request_frame(AVFilterLink *outlink)
91 {
92  AVFilterContext *ctx = outlink->src;
93  APadContext *s = ctx->priv;
94  int ret;
95 
96  ret = ff_request_frame(ctx->inputs[0]);
97 
98  if (ret == AVERROR_EOF && !ctx->is_disabled) {
99  int n_out = s->packet_size;
100  AVFrame *outsamplesref;
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->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  return ret;
134 }
135 
136 static int config_output(AVFilterLink *outlink)
137 {
138  AVFilterContext *ctx = outlink->src;
139  APadContext *s = ctx->priv;
140 
141  if (s->pad_dur >= 0)
142  s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
143  if (s->whole_dur >= 0)
144  s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE);
145 
146  s->pad_len_left = s->pad_len;
147  s->whole_len_left = s->whole_len;
148 
149  return 0;
150 }
151 
152 static const AVFilterPad apad_inputs[] = {
153  {
154  .name = "default",
155  .type = AVMEDIA_TYPE_AUDIO,
156  .filter_frame = filter_frame,
157  },
158 };
159 
160 static const AVFilterPad apad_outputs[] = {
161  {
162  .name = "default",
163  .request_frame = request_frame,
164  .config_props = config_output,
165  .type = AVMEDIA_TYPE_AUDIO,
166  },
167 };
168 
170  .name = "apad",
171  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
172  .init = init,
173  .priv_size = sizeof(APadContext),
176  .priv_class = &apad_class,
178 };
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:88
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:39
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
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:136
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:424
AVOption
AVOption.
Definition: opt.h:247
APadContext::whole_dur
int64_t whole_dur
Definition: af_apad.c:45
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:238
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:420
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
APadContext::whole_len
int64_t whole_len
Definition: af_apad.c:43
APadContext::pad_dur
int64_t pad_dur
Definition: af_apad.c:44
OFFSET
#define OFFSET(x)
Definition: af_apad.c:48
apad_options
static const AVOption apad_options[]
Definition: af_apad.c:51
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:62
samplefmt.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
APadContext::pad_len_left
int64_t pad_len_left
Definition: af_apad.c:42
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
APadContext::whole_len_left
int64_t whole_len_left
Definition: af_apad.c:43
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:628
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:225
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
APadContext::pad_len
int64_t pad_len
Definition: af_apad.c:42
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:141
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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
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:117
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:494
APadContext
Definition: af_apad.c:37
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
apad_inputs
static const AVFilterPad apad_inputs[]
Definition: af_apad.c:152
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:404
ff_af_apad
const AVFilter ff_af_apad
Definition: af_apad.c:169
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
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:378
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
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:128
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: af_apad.c:90
av_samples_set_silence
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:244
AVFilter
Filter definition.
Definition: avfilter.h:165
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:41
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
apad_outputs
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:160
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
audio.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:75
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
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:154
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
A
#define A
Definition: af_apad.c:49
avstring.h