2 * Copyright 2010 S.N. Hemanth Meenakshisundaram <smeenaks ucsd edu>
3 * Copyright 2010 Stefano Sabatini <stefano.sabatini-lala poste it>
5 * This file is part of FFmpeg.
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.
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.
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
28 #include "libavutil/audioconvert.h"
29 #include "libavutil/opt.h"
37 char *channel_layout_str;
38 uint64_t channel_layout;
39 char *sample_rate_str;
41 int nb_samples; ///< number of samples per requested frame
45 #define OFFSET(x) offsetof(ANullContext, x)
47 static const AVOption anullsrc_options[]= {
48 { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
49 { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
50 { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
51 { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
52 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
53 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
57 AVFILTER_DEFINE_CLASS(anullsrc);
59 static int init(AVFilterContext *ctx, const char *args)
61 ANullContext *null = ctx->priv;
64 null->class = &anullsrc_class;
65 av_opt_set_defaults(null);
67 if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0)
70 if ((ret = ff_parse_sample_rate(&null->sample_rate,
71 null->sample_rate_str, ctx)) < 0)
74 if ((ret = ff_parse_channel_layout(&null->channel_layout,
75 null->channel_layout_str, ctx)) < 0)
81 static int config_props(AVFilterLink *outlink)
83 ANullContext *null = outlink->src->priv;
87 outlink->sample_rate = null->sample_rate;
88 outlink->channel_layout = null->channel_layout;
90 chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
91 av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
92 av_log(outlink->src, AV_LOG_VERBOSE,
93 "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
94 null->sample_rate, buf, null->nb_samples);
99 static int request_frame(AVFilterLink *outlink)
101 ANullContext *null = outlink->src->priv;
102 AVFilterBufferRef *samplesref;
105 ff_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
106 samplesref->pts = null->pts;
107 samplesref->pos = -1;
108 samplesref->audio->channel_layout = null->channel_layout;
109 samplesref->audio->sample_rate = outlink->sample_rate;
111 ff_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
112 avfilter_unref_buffer(samplesref);
114 null->pts += null->nb_samples;
118 AVFilter avfilter_asrc_anullsrc = {
120 .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
123 .priv_size = sizeof(ANullContext),
125 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
127 .outputs = (const AVFilterPad[]) {{ .name = "default",
128 .type = AVMEDIA_TYPE_AUDIO,
129 .config_props = config_props,
130 .request_frame = request_frame, },