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 "internal.h"
36 
37 typedef struct ANullContext {
38  const AVClass *class;
40  uint64_t channel_layout;
43  int nb_samples; ///< number of samples per requested frame
44  int64_t pts;
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(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
52  { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
53  { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
54  { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
55  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
56  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
57  { NULL }
58 };
59 
60 AVFILTER_DEFINE_CLASS(anullsrc);
61 
63 {
64  ANullContext *null = ctx->priv;
65  int ret;
66 
67  if ((ret = ff_parse_sample_rate(&null->sample_rate,
68  null->sample_rate_str, ctx)) < 0)
69  return ret;
70 
71  if ((ret = ff_parse_channel_layout(&null->channel_layout, NULL,
72  null->channel_layout_str, ctx)) < 0)
73  return ret;
74 
75  return 0;
76 }
77 
79 {
80  ANullContext *null = ctx->priv;
81  int64_t chlayouts[] = { null->channel_layout, -1 };
82  int sample_rates[] = { null->sample_rate, -1 };
83  int ret;
84 
85  if ((ret = ff_set_common_formats (ctx, ff_all_formats (AVMEDIA_TYPE_AUDIO))) < 0 ||
86  (ret = ff_set_common_channel_layouts (ctx, avfilter_make_format64_list (chlayouts ))) < 0 ||
87  (ret = ff_set_common_samplerates (ctx, ff_make_format_list (sample_rates ))) < 0)
88  return ret;
89 
90  return 0;
91 }
92 
93 static int config_props(AVFilterLink *outlink)
94 {
95  ANullContext *null = outlink->src->priv;
96  char buf[128];
97 
98  av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout);
99  av_log(outlink->src, AV_LOG_VERBOSE,
100  "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
101  null->sample_rate, buf, null->nb_samples);
102 
103  return 0;
104 }
105 
106 static int request_frame(AVFilterLink *outlink)
107 {
108  int ret;
109  ANullContext *null = outlink->src->priv;
110  AVFrame *samplesref;
111 
112  samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
113  if (!samplesref)
114  return AVERROR(ENOMEM);
115 
116  samplesref->pts = null->pts;
117  samplesref->channel_layout = null->channel_layout;
118  samplesref->sample_rate = outlink->sample_rate;
119 
120  ret = ff_filter_frame(outlink, av_frame_clone(samplesref));
121  av_frame_free(&samplesref);
122  if (ret < 0)
123  return ret;
124 
125  null->pts += null->nb_samples;
126  return ret;
127 }
128 
130  {
131  .name = "default",
132  .type = AVMEDIA_TYPE_AUDIO,
133  .config_props = config_props,
134  .request_frame = request_frame,
135  },
136  { NULL }
137 };
138 
140  .name = "anullsrc",
141  .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
142  .init = init,
143  .query_formats = query_formats,
144  .priv_size = sizeof(ANullContext),
145  .inputs = NULL,
146  .outputs = avfilter_asrc_anullsrc_outputs,
147  .priv_class = &anullsrc_class,
148 };
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
AVOption.
Definition: opt.h:246
char * channel_layout_str
Definition: asrc_anullsrc.c:39
Main libavfilter public API header.
#define OFFSET(x)
Definition: asrc_anullsrc.c:47
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:361
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
static int config_props(AVFilterLink *outlink)
Definition: asrc_anullsrc.c:93
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:350
A filter pad used for either input or output.
Definition: internal.h:54
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:568
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVFilter ff_asrc_anullsrc
void * priv
private data for use by the filter
Definition: avfilter.h:353
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:662
int nb_samples
number of samples per requested frame
Definition: asrc_anullsrc.c:43
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:445
common internal API header
audio channel layout utility functions
AVFormatContext * ctx
Definition: movenc.c:48
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
#define FLAGS
Definition: asrc_anullsrc.c:48
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
static const AVFilterPad avfilter_asrc_anullsrc_outputs[]
static int query_formats(AVFilterContext *ctx)
Definition: asrc_anullsrc.c:78
uint64_t channel_layout
Definition: asrc_anullsrc.c:40
void * buf
Definition: avisynth_c.h:766
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
Describe the class of an AVClass context structure.
Definition: log.h:67
int sample_rate
Sample rate of the audio data.
Definition: frame.h:440
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
static const AVOption anullsrc_options[]
Definition: asrc_anullsrc.c:50
static int request_frame(AVFilterLink *outlink)
sample_rates
AVFILTER_DEFINE_CLASS(anullsrc)
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
Parse a sample rate.
Definition: formats.c:650
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_anullsrc.c:62
An instance of a filter.
Definition: avfilter.h:338
internal API functions
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
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
char * sample_rate_str
Definition: asrc_anullsrc.c:41