| 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 | |
| 30 | #include "libavutil/channel_layout.h" |
| 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 { |
| 38 | const AVClass *class; |
| 39 | char *channel_layout_str; |
| 40 | uint64_t channel_layout; |
| 41 | char *sample_rate_str; |
| 42 | int sample_rate; |
| 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 | |
| 62 | static av_cold int init(AVFilterContext *ctx) |
| 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 | |
| 78 | static int query_formats(AVFilterContext *ctx) |
| 79 | { |
| 80 | ANullContext *null = ctx->priv; |
| 81 | int64_t chlayouts[] = { null->channel_layout, -1 }; |
| 82 | int sample_rates[] = { null->sample_rate, -1 }; |
| 83 | |
| 84 | ff_set_common_formats (ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO)); |
| 85 | ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts)); |
| 86 | ff_set_common_samplerates (ctx, ff_make_format_list(sample_rates)); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int config_props(AVFilterLink *outlink) |
| 92 | { |
| 93 | ANullContext *null = outlink->src->priv; |
| 94 | char buf[128]; |
| 95 | |
| 96 | av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout); |
| 97 | av_log(outlink->src, AV_LOG_VERBOSE, |
| 98 | "sample_rate:%d channel_layout:'%s' nb_samples:%d\n", |
| 99 | null->sample_rate, buf, null->nb_samples); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int request_frame(AVFilterLink *outlink) |
| 105 | { |
| 106 | int ret; |
| 107 | ANullContext *null = outlink->src->priv; |
| 108 | AVFrame *samplesref; |
| 109 | |
| 110 | samplesref = ff_get_audio_buffer(outlink, null->nb_samples); |
| 111 | if (!samplesref) |
| 112 | return AVERROR(ENOMEM); |
| 113 | |
| 114 | samplesref->pts = null->pts; |
| 115 | samplesref->channel_layout = null->channel_layout; |
| 116 | samplesref->sample_rate = outlink->sample_rate; |
| 117 | |
| 118 | ret = ff_filter_frame(outlink, av_frame_clone(samplesref)); |
| 119 | av_frame_free(&samplesref); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | |
| 123 | null->pts += null->nb_samples; |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = { |
| 128 | { |
| 129 | .name = "default", |
| 130 | .type = AVMEDIA_TYPE_AUDIO, |
| 131 | .config_props = config_props, |
| 132 | .request_frame = request_frame, |
| 133 | }, |
| 134 | { NULL } |
| 135 | }; |
| 136 | |
| 137 | AVFilter ff_asrc_anullsrc = { |
| 138 | .name = "anullsrc", |
| 139 | .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."), |
| 140 | .init = init, |
| 141 | .query_formats = query_formats, |
| 142 | .priv_size = sizeof(ANullContext), |
| 143 | .inputs = NULL, |
| 144 | .outputs = avfilter_asrc_anullsrc_outputs, |
| 145 | .priv_class = &anullsrc_class, |
| 146 | }; |