FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
38  const AVClass *class;
39  int64_t next_pts;
40 
42  int64_t pad_len, pad_len_left;
43  int64_t whole_len, whole_len_left;
44 } APadContext;
45 
46 #define OFFSET(x) offsetof(APadContext, x)
47 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48 
49 static const AVOption apad_options[] = {
50  { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
51  { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
52  { "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 },
53  { NULL }
54 };
55 
57 
58 static av_cold int init(AVFilterContext *ctx)
59 {
60  APadContext *apad = ctx->priv;
61 
62  apad->next_pts = AV_NOPTS_VALUE;
63  if (apad->whole_len >= 0 && apad->pad_len >= 0) {
64  av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
65  return AVERROR(EINVAL);
66  }
67  apad->pad_len_left = apad->pad_len;
68  apad->whole_len_left = apad->whole_len;
69 
70  return 0;
71 }
72 
73 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
74 {
75  AVFilterContext *ctx = inlink->dst;
76  APadContext *apad = ctx->priv;
77 
78  if (apad->whole_len >= 0) {
79  apad->whole_len_left = FFMAX(apad->whole_len_left - frame->nb_samples, 0);
80  av_log(ctx, AV_LOG_DEBUG,
81  "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, apad->whole_len_left);
82  }
83 
84  apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
85  return ff_filter_frame(ctx->outputs[0], frame);
86 }
87 
88 static int request_frame(AVFilterLink *outlink)
89 {
90  AVFilterContext *ctx = outlink->src;
91  APadContext *apad = ctx->priv;
92  int ret;
93 
94  ret = ff_request_frame(ctx->inputs[0]);
95 
96  if (ret == AVERROR_EOF && !ctx->is_disabled) {
97  int n_out = apad->packet_size;
98  AVFrame *outsamplesref;
99 
100  if (apad->whole_len >= 0 && apad->pad_len < 0) {
101  apad->pad_len = apad->pad_len_left = apad->whole_len_left;
102  }
103  if (apad->pad_len >=0 || apad->whole_len >= 0) {
104  n_out = FFMIN(n_out, apad->pad_len_left);
105  apad->pad_len_left -= n_out;
106  av_log(ctx, AV_LOG_DEBUG,
107  "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, apad->pad_len_left);
108  }
109 
110  if (!n_out)
111  return AVERROR_EOF;
112 
113  outsamplesref = ff_get_audio_buffer(outlink, n_out);
114  if (!outsamplesref)
115  return AVERROR(ENOMEM);
116 
117  av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
118  av_assert0(outsamplesref->nb_samples == n_out);
119 
120  av_samples_set_silence(outsamplesref->extended_data, 0,
121  n_out,
122  av_frame_get_channels(outsamplesref),
123  outsamplesref->format);
124 
125  outsamplesref->pts = apad->next_pts;
126  if (apad->next_pts != AV_NOPTS_VALUE)
127  apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
128 
129  return ff_filter_frame(outlink, outsamplesref);
130  }
131  return ret;
132 }
133 
134 static const AVFilterPad apad_inputs[] = {
135  {
136  .name = "default",
137  .type = AVMEDIA_TYPE_AUDIO,
138  .filter_frame = filter_frame,
139  },
140  { NULL }
141 };
142 
143 static const AVFilterPad apad_outputs[] = {
144  {
145  .name = "default",
146  .request_frame = request_frame,
147  .type = AVMEDIA_TYPE_AUDIO,
148  },
149  { NULL }
150 };
151 
153  .name = "apad",
154  .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
155  .init = init,
156  .priv_size = sizeof(APadContext),
157  .inputs = apad_inputs,
158  .outputs = apad_outputs,
159  .priv_class = &apad_class,
161 };
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: af_apad.c:46
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static const AVFilterPad apad_inputs[]
Definition: af_apad.c:134
int64_t pad_len
Definition: af_apad.c:42
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:686
const char * name
Pad name.
Definition: internal.h:67
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
#define av_cold
Definition: attributes.h:74
AVOptions.
int64_t next_pts
Definition: af_apad.c:39
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
static AVFrame * frame
int packet_size
Definition: af_apad.c:41
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
static const AVFilterPad apad_outputs[]
Definition: af_apad.c:143
A filter pad used for either input or output.
Definition: internal.h:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:235
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:70
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_apad.c:73
#define FFMAX(a, b)
Definition: common.h:64
#define A
Definition: af_apad.c:47
audio channel layout utility functions
int64_t pad_len_left
Definition: af_apad.c:42
#define FFMIN(a, b)
Definition: common.h:66
ret
Definition: avfilter.c:974
static int request_frame(AVFilterLink *outlink)
Definition: af_apad.c:88
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
static av_cold int init(AVFilterContext *ctx)
Definition: af_apad.c:58
Describe the class of an AVClass context structure.
Definition: log.h:67
int sample_rate
Sample rate of the audio data.
Definition: frame.h:422
int av_frame_get_channels(const AVFrame *frame)
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:474
#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:459
static int flags
Definition: cpu.c:47
int64_t whole_len_left
Definition: af_apad.c:43
static const AVOption apad_options[]
Definition: af_apad.c:49
int64_t whole_len
Definition: af_apad.c:43
AVFILTER_DEFINE_CLASS(apad)
An instance of a filter.
Definition: avfilter.h:633
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:343
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
AVFilter ff_af_apad
Definition: af_apad.c:152
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241