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 /**
23  * @file
24  * audio pad filter.
25  *
26  * Based on af_aresample.c
27  */
28 
29 #include "libavutil/avstring.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/samplefmt.h"
33 #include "libavutil/avassert.h"
34 #include "avfilter.h"
35 #include "audio.h"
36 #include "internal.h"
37 
38 typedef struct {
39  const AVClass *class;
40  int64_t next_pts;
41 
43  int64_t pad_len;
44  int64_t whole_len;
45 } APadContext;
46 
47 #define OFFSET(x) offsetof(APadContext, x)
48 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 
50 static const AVOption apad_options[] = {
51  { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
52  { "pad_len", "number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
53  { "whole_len", "target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
54  { NULL },
55 };
56 
58 
59 static av_cold int init(AVFilterContext *ctx)
60 {
61  APadContext *apad = ctx->priv;
62 
63  apad->next_pts = AV_NOPTS_VALUE;
64  if (apad->whole_len && apad->pad_len) {
65  av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
66  return AVERROR(EINVAL);
67  }
68 
69  return 0;
70 }
71 
72 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
73 {
74  AVFilterContext *ctx = inlink->dst;
75  APadContext *apad = ctx->priv;
76 
77  if (apad->whole_len)
78  apad->whole_len -= frame->nb_samples;
79 
80  apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
81  return ff_filter_frame(ctx->outputs[0], frame);
82 }
83 
84 static int request_frame(AVFilterLink *outlink)
85 {
86  AVFilterContext *ctx = outlink->src;
87  APadContext *apad = ctx->priv;
88  int ret;
89 
90  ret = ff_request_frame(ctx->inputs[0]);
91 
92  if (ret == AVERROR_EOF) {
93  int n_out = apad->packet_size;
94  AVFrame *outsamplesref;
95 
96  if (apad->whole_len > 0) {
97  apad->pad_len = apad->whole_len;
98  apad->whole_len = 0;
99  }
100  if (apad->pad_len > 0) {
101  n_out = FFMIN(n_out, apad->pad_len);
102  apad->pad_len -= n_out;
103  }
104 
105  if(!n_out)
106  return AVERROR_EOF;
107 
108  outsamplesref = ff_get_audio_buffer(outlink, n_out);
109  if (!outsamplesref)
110  return AVERROR(ENOMEM);
111 
112  av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
113  av_assert0(outsamplesref->nb_samples == n_out);
114 
115  av_samples_set_silence(outsamplesref->extended_data, 0,
116  n_out,
117  av_frame_get_channels(outsamplesref),
118  outsamplesref->format);
119 
120  outsamplesref->pts = apad->next_pts;
121  if (apad->next_pts != AV_NOPTS_VALUE)
122  apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
123 
124  return ff_filter_frame(outlink, outsamplesref);
125  }
126  return ret;
127 }
128 
129 static const AVFilterPad apad_inputs[] = {
130  {
131  .name = "default",
132  .type = AVMEDIA_TYPE_AUDIO,
133  .filter_frame = filter_frame,
134  },
135  { NULL },
136 };
137 
138 static const AVFilterPad apad_outputs[] = {
139  {
140  .name = "default",
141  .request_frame = request_frame,
142  .type = AVMEDIA_TYPE_AUDIO,
143  },
144  { NULL },
145 };
146 
148  .name = "apad",
149  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
150  .init = init,
151  .priv_size = sizeof(APadContext),
152  .inputs = apad_inputs,
153  .outputs = apad_outputs,
154  .priv_class = &apad_class,
155 };
#define OFFSET(x)
Definition: af_apad.c:47
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
static const AVFilterPad apad_inputs[]
Definition: af_apad.c:129
int64_t pad_len
Definition: af_apad.c:43
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
int64_t next_pts
Definition: af_apad.c:40
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
int packet_size
Definition: af_apad.c:42
#define AVERROR_EOF
End of file.
Definition: error.h:55
frame
Definition: stft.m:14
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:138
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:72
#define A
Definition: af_apad.c:48
audio channel layout utility functions
int av_frame_get_channels(const AVFrame *frame)
#define FFMIN(a, b)
Definition: common.h:58
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:249
ret
Definition: avfilter.c:821
static int request_frame(AVFilterLink *outlink)
Definition: af_apad.c:84
AVFilter avfilter_af_apad
Definition: af_apad.c:147
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:59
Describe the class of an AVClass context structure.
Definition: log.h:50
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
Filter definition.
Definition: avfilter.h:436
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static const AVOption apad_options[]
Definition: af_apad.c:50
int64_t whole_len
Definition: af_apad.c:44
AVFILTER_DEFINE_CLASS(apad)
An instance of a filter.
Definition: avfilter.h:524
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
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
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190