FFmpeg
asrc_anullsrc.c
Go to the documentation of this file.
1 /*
2  * Copyright 2010 S.N. Hemanth Meenakshisundaram <smeenaks ucsd edu>
3  * Copyright 2010 Stefano Sabatini <stefano.sabatini-lala poste it>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * null audio source
25  */
26 
27 #include <inttypes.h>
28 #include <stdio.h>
29 
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "filters.h"
36 #include "formats.h"
37 
38 typedef struct ANullContext {
39  const AVClass *class;
43  int nb_samples; ///< number of samples per requested frame
45 } ANullContext;
46 
47 #define OFFSET(x) offsetof(ANullContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 
50 static const AVOption anullsrc_options[]= {
51  { "channel_layout", "set channel_layout", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, FLAGS },
52  { "cl", "set channel_layout", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, FLAGS },
53  { "sample_rate", "set sample rate", OFFSET(sample_rate) , AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT_MAX, FLAGS },
54  { "r", "set sample rate", OFFSET(sample_rate) , AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT_MAX, FLAGS },
55  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
56  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
57  { "duration", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
58  { "d", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
59  { NULL }
60 };
61 
62 AVFILTER_DEFINE_CLASS(anullsrc);
63 
64 static int query_formats(const AVFilterContext *ctx,
65  AVFilterFormatsConfig **cfg_in,
66  AVFilterFormatsConfig **cfg_out)
67 {
68  const ANullContext *null = ctx->priv;
69  const AVChannelLayout chlayouts[] = { null->ch_layout, { 0 } };
70  int sample_rates[] = { null->sample_rate, -1 };
71  int ret;
72 
74  if (ret < 0)
75  return ret;
76 
77  return ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts);
78 }
79 
80 static av_cold int config_props(AVFilterLink *outlink)
81 {
82  ANullContext *null = outlink->src->priv;
83 
84  if (null->duration >= 0)
85  null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE);
86 
87  return 0;
88 }
89 
91 {
92  ANullContext *null = ctx->priv;
93  AVFilterLink *outlink = ctx->outputs[0];
94 
95  if (null->duration >= 0 && null->pts >= null->duration) {
96  ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
97  return 0;
98  }
99 
100  if (ff_outlink_frame_wanted(outlink)) {
101  AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples);
102 
103  if (!samplesref)
104  return AVERROR(ENOMEM);
105 
106  samplesref->pts = null->pts;
107  null->pts += samplesref->nb_samples;
108 
109  return ff_filter_frame(outlink, samplesref);
110  }
111 
112  return FFERROR_NOT_READY;
113 }
114 
116  {
117  .name = "default",
118  .type = AVMEDIA_TYPE_AUDIO,
119  .config_props = config_props,
120  },
121 };
122 
124  .name = "anullsrc",
125  .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
126  .priv_size = sizeof(ANullContext),
127  .inputs = NULL,
130  .activate = activate,
131  .priv_class = &anullsrc_class,
132 };
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:98
ANullContext::pts
int64_t pts
Definition: asrc_anullsrc.c:44
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
anullsrc_options
static const AVOption anullsrc_options[]
Definition: asrc_anullsrc.c:50
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
activate
static int activate(AVFilterContext *ctx)
Definition: asrc_anullsrc.c:90
int64_t
long long int64_t
Definition: coverity.c:34
sample_rates
static const int sample_rates[]
Definition: dcaenc.h:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
AVOption
AVOption.
Definition: opt.h:429
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(anullsrc)
ANullContext::nb_samples
int nb_samples
number of samples per requested frame
Definition: asrc_anullsrc.c:43
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition: opt.h:319
ff_set_common_channel_layouts_from_list2
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition: formats.c:920
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
formats.h
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
avfilter_asrc_anullsrc_outputs
static const AVFilterPad avfilter_asrc_anullsrc_outputs[]
Definition: asrc_anullsrc.c:115
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
av_cold
#define av_cold
Definition: attributes.h:90
duration
int64_t duration
Definition: movenc.c:65
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:424
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ff_set_common_samplerates_from_list2
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition: formats.c:944
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ANullContext::duration
int64_t duration
Definition: asrc_anullsrc.c:42
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: asrc_anullsrc.c:64
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
inputs
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
Definition: filter_design.txt:243
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition: opt.h:331
ANullContext::ch_layout
AVChannelLayout ch_layout
Definition: asrc_anullsrc.c:40
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
internal.h
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
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:201
ret
ret
Definition: filter_design.txt:187
ff_asrc_anullsrc
const AVFilter ff_asrc_anullsrc
Definition: asrc_anullsrc.c:123
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ANullContext
Definition: asrc_anullsrc.c:38
avfilter.h
OFFSET
#define OFFSET(x)
Definition: asrc_anullsrc.c:47
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
audio.h
FLAGS
#define FLAGS
Definition: asrc_anullsrc.c:48
ANullContext::sample_rate
int sample_rate
Definition: asrc_anullsrc.c:41
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_anullsrc.c:80