FFmpeg
Loading...
Searching...
No Matches
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
48
49#define OFFSET(x) offsetof(APadContext, x)
50#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52static 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
91static 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
142 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
143
144 if (!s->eof && ff_inlink_queued_frames(inlink)) {
145 AVFrame *frame = NULL;
146 int ret;
147
148 ret = ff_inlink_consume_frame(inlink, &frame);
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
168 FF_FILTER_FORWARD_WANTED(outlink, inlink);
169
170 return FFERROR_NOT_READY;
171}
172
173static 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
189static const AVFilterPad apad_outputs[] = {
190 {
191 .name = "default",
192 .type = AVMEDIA_TYPE_AUDIO,
193 .config_props = config_output,
194 },
195};
196
198 .p.name = "apad",
199 .p.description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
200 .p.priv_class = &apad_class,
202 .init = init,
203 .activate = activate,
204 .priv_size = sizeof(APadContext),
207};
static const AVOption apad_options[]
Definition af_apad.c:52
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition af_apad.c:76
static int push_frame(AVFilterLink *outlink)
Definition af_apad.c:91
static int activate(AVFilterContext *ctx)
Definition af_apad.c:134
#define OFFSET(x)
Definition af_apad.c:49
static int config_output(AVFilterLink *outlink)
Definition af_apad.c:173
static const AVFilterPad apad_outputs[]
Definition af_apad.c:189
const FFFilter ff_af_apad
Definition af_apad.c:197
#define A(x)
Definition vpx_arith.h:28
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
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
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:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition avfilter.c:1483
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:1520
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#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:204
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
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:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
AVOptions.
int packet_size
Definition af_apad.c:42
int64_t whole_dur
Definition af_apad.c:46
int64_t next_pts
Definition af_apad.c:39
int64_t whole_len_left
Definition af_apad.c:44
int64_t pad_dur
Definition af_apad.c:45
int64_t pad_len
Definition af_apad.c:43
int64_t whole_len
Definition af_apad.c:44
int64_t pad_len_left
Definition af_apad.c:43
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int sample_rate
Sample rate of the audio data.
Definition frame.h:635
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static int64_t pts