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
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
37 typedef struct ANullContext {
39 char *channel_layout_str;
40 uint64_t channel_layout;
41 char *sample_rate_str;
44 int nb_samples; ///< number of samples per requested frame
48 #define OFFSET(x) offsetof(ANullContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption anullsrc_options[]= {
52 { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
53 { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
54 { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
55 { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
56 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
57 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
58 { "duration", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
59 { "d", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
63 AVFILTER_DEFINE_CLASS(anullsrc);
65 static av_cold int init(AVFilterContext *ctx)
67 ANullContext *null = ctx->priv;
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, NULL,
75 null->channel_layout_str, ctx)) < 0)
81 static int query_formats(AVFilterContext *ctx)
83 ANullContext *null = ctx->priv;
84 int64_t chlayouts[] = { null->channel_layout, -1 };
85 int sample_rates[] = { null->sample_rate, -1 };
88 if ((ret = ff_set_common_formats (ctx, ff_all_formats (AVMEDIA_TYPE_AUDIO))) < 0 ||
89 (ret = ff_set_common_samplerates (ctx, ff_make_format_list (sample_rates ))) < 0)
92 return ff_set_common_channel_layouts(ctx, ff_make_format64_list(chlayouts));
95 static int config_props(AVFilterLink *outlink)
97 ANullContext *null = outlink->src->priv;
100 av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout);
101 av_log(outlink->src, AV_LOG_VERBOSE,
102 "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
103 null->sample_rate, buf, null->nb_samples);
108 static int request_frame(AVFilterLink *outlink)
111 ANullContext *null = outlink->src->priv;
114 if (null->duration >= 0 &&
115 av_rescale_q(null->pts, outlink->time_base, AV_TIME_BASE_Q) >= null->duration)
118 samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
120 return AVERROR(ENOMEM);
122 samplesref->pts = null->pts;
124 ret = ff_filter_frame(outlink, samplesref);
128 null->pts += null->nb_samples;
132 static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
135 .type = AVMEDIA_TYPE_AUDIO,
136 .config_props = config_props,
137 .request_frame = request_frame,
142 AVFilter ff_asrc_anullsrc = {
144 .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
146 .query_formats = query_formats,
147 .priv_size = sizeof(ANullContext),
149 .outputs = avfilter_asrc_anullsrc_outputs,
150 .priv_class = &anullsrc_class,