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 "internal.h"
37 
38 typedef struct ANullContext {
39  const AVClass *class;
41  uint64_t channel_layout;
44  int64_t duration;
45  int nb_samples; ///< number of samples per requested frame
46  int64_t pts;
47 } ANullContext;
48 
49 #define OFFSET(x) offsetof(ANullContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption anullsrc_options[]= {
53  { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
54  { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
55  { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
56  { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
57  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
58  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
59  { "duration", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
60  { "d", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
61  { NULL }
62 };
63 
64 AVFILTER_DEFINE_CLASS(anullsrc);
65 
67 {
68  ANullContext *null = ctx->priv;
69  int ret;
70 
71  if ((ret = ff_parse_sample_rate(&null->sample_rate,
72  null->sample_rate_str, ctx)) < 0)
73  return ret;
74 
75  if ((ret = ff_parse_channel_layout(&null->channel_layout, NULL,
76  null->channel_layout_str, ctx)) < 0)
77  return ret;
78 
79  return 0;
80 }
81 
83 {
84  ANullContext *null = ctx->priv;
85  int64_t chlayouts[] = { null->channel_layout, -1 };
86  int sample_rates[] = { null->sample_rate, -1 };
87  int ret;
88 
91  return ret;
92 
94 }
95 
96 static av_cold int config_props(AVFilterLink *outlink)
97 {
98  ANullContext *null = outlink->src->priv;
99 
100  if (null->duration >= 0)
101  null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE);
102 
103  return 0;
104 }
105 
107 {
108  ANullContext *null = ctx->priv;
109  AVFilterLink *outlink = ctx->outputs[0];
110 
111  if (null->duration >= 0 && null->pts >= null->duration) {
112  ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
113  return 0;
114  }
115 
116  if (ff_outlink_frame_wanted(outlink)) {
117  AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples);
118 
119  if (!samplesref)
120  return AVERROR(ENOMEM);
121 
122  samplesref->pts = null->pts;
123  null->pts += samplesref->nb_samples;
124 
125  return ff_filter_frame(outlink, samplesref);
126  }
127 
128  return FFERROR_NOT_READY;
129 }
130 
132  {
133  .name = "default",
134  .type = AVMEDIA_TYPE_AUDIO,
135  .config_props = config_props,
136  },
137 };
138 
140  .name = "anullsrc",
141  .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
142  .init = init,
143  .priv_size = sizeof(ANullContext),
144  .inputs = NULL,
147  .activate = activate,
148  .priv_class = &anullsrc_class,
149 };
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
ANullContext::pts
int64_t pts
Definition: asrc_anullsrc.c:46
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:52
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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:106
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:683
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVOption
AVOption.
Definition: opt.h:247
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(anullsrc)
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
ANullContext::nb_samples
int nb_samples
number of samples per requested frame
Definition: asrc_anullsrc.c:45
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:238
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ff_set_common_channel_layouts_from_list
int ff_set_common_channel_layouts_from_list(AVFilterContext *ctx, const int64_t *fmts)
Equivalent to ff_set_common_channel_layouts(ctx, ff_make_format64_list(fmts))
Definition: formats.c:665
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:401
ANullContext::channel_layout
uint64_t channel_layout
Definition: asrc_anullsrc.c:41
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:439
avfilter_asrc_anullsrc_outputs
static const AVFilterPad avfilter_asrc_anullsrc_outputs[]
Definition: asrc_anullsrc.c:131
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_anullsrc.c:66
av_cold
#define av_cold
Definition: attributes.h:90
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
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:699
duration
int64_t duration
Definition: movenc.c:64
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:189
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ANullContext::duration
int64_t duration
Definition: asrc_anullsrc.c:44
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: asrc_anullsrc.c:82
ANullContext::channel_layout_str
char * channel_layout_str
Definition: asrc_anullsrc.c:40
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
ff_parse_channel_layout
int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: formats.c:789
sample_rates
sample_rates
Definition: ffmpeg_filter.c:156
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
internal.h
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
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
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
ANullContext::sample_rate_str
char * sample_rate_str
Definition: asrc_anullsrc.c:42
ff_asrc_anullsrc
const AVFilter ff_asrc_anullsrc
Definition: asrc_anullsrc.c:139
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
ANullContext
Definition: asrc_anullsrc.c:38
avfilter.h
OFFSET
#define OFFSET(x)
Definition: asrc_anullsrc.c:49
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
FLAGS
#define FLAGS
Definition: asrc_anullsrc.c:50
ANullContext::sample_rate
int sample_rate
Definition: asrc_anullsrc.c:43
ff_parse_sample_rate
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
Parse a sample rate.
Definition: formats.c:777
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
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_anullsrc.c:96