yading@10: /* yading@10: * Copyright 2010 S.N. Hemanth Meenakshisundaram yading@10: * Copyright 2010 Stefano Sabatini yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * null audio source yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/internal.h" yading@10: #include "libavutil/opt.h" yading@10: #include "audio.h" yading@10: #include "avfilter.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct { yading@10: const AVClass *class; yading@10: char *channel_layout_str; yading@10: uint64_t channel_layout; yading@10: char *sample_rate_str; yading@10: int sample_rate; yading@10: int nb_samples; ///< number of samples per requested frame yading@10: int64_t pts; yading@10: } ANullContext; yading@10: yading@10: #define OFFSET(x) offsetof(ANullContext, x) yading@10: #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM yading@10: yading@10: static const AVOption anullsrc_options[]= { yading@10: { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS }, yading@10: { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS }, yading@10: { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS }, yading@10: { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS }, yading@10: { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS }, yading@10: { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS }, yading@10: { NULL }, yading@10: }; yading@10: yading@10: AVFILTER_DEFINE_CLASS(anullsrc); yading@10: yading@10: static int init(AVFilterContext *ctx) yading@10: { yading@10: ANullContext *null = ctx->priv; yading@10: int ret; yading@10: yading@10: if ((ret = ff_parse_sample_rate(&null->sample_rate, yading@10: null->sample_rate_str, ctx)) < 0) yading@10: return ret; yading@10: yading@10: if ((ret = ff_parse_channel_layout(&null->channel_layout, yading@10: null->channel_layout_str, ctx)) < 0) yading@10: return ret; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int query_formats(AVFilterContext *ctx) yading@10: { yading@10: ANullContext *null = ctx->priv; yading@10: int64_t chlayouts[] = { null->channel_layout, -1 }; yading@10: int sample_rates[] = { null->sample_rate, -1 }; yading@10: yading@10: ff_set_common_formats (ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO)); yading@10: ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts)); yading@10: ff_set_common_samplerates (ctx, ff_make_format_list(sample_rates)); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int config_props(AVFilterLink *outlink) yading@10: { yading@10: ANullContext *null = outlink->src->priv; yading@10: char buf[128]; yading@10: yading@10: av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout); yading@10: av_log(outlink->src, AV_LOG_VERBOSE, yading@10: "sample_rate:%d channel_layout:'%s' nb_samples:%d\n", yading@10: null->sample_rate, buf, null->nb_samples); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int request_frame(AVFilterLink *outlink) yading@10: { yading@10: int ret; yading@10: ANullContext *null = outlink->src->priv; yading@10: AVFrame *samplesref; yading@10: yading@10: samplesref = ff_get_audio_buffer(outlink, null->nb_samples); yading@10: if (!samplesref) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: samplesref->pts = null->pts; yading@10: samplesref->channel_layout = null->channel_layout; yading@10: samplesref->sample_rate = outlink->sample_rate; yading@10: yading@10: ret = ff_filter_frame(outlink, av_frame_clone(samplesref)); yading@10: av_frame_free(&samplesref); yading@10: if (ret < 0) yading@10: return ret; yading@10: yading@10: null->pts += null->nb_samples; yading@10: return ret; yading@10: } yading@10: yading@10: static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .config_props = config_props, yading@10: .request_frame = request_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFilter avfilter_asrc_anullsrc = { yading@10: .name = "anullsrc", yading@10: .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."), yading@10: yading@10: .init = init, yading@10: .query_formats = query_formats, yading@10: .priv_size = sizeof(ANullContext), yading@10: yading@10: .inputs = NULL, yading@10: yading@10: .outputs = avfilter_asrc_anullsrc_outputs, yading@10: .priv_class = &anullsrc_class, yading@10: };