]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_anullsrc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / asrc_anullsrc.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * null audio source
22  */
23
24 #include "libavutil/audioconvert.h"
25 #include "libavutil/opt.h"
26
27 #include "avfilter.h"
28 #include "internal.h"
29
30 typedef struct {
31     const AVClass *class;
32     char   *channel_layout_str;
33     uint64_t channel_layout;
34     char   *sample_rate_str;
35     int     sample_rate;
36     int nb_samples;             ///< number of samples per requested frame
37     int64_t pts;
38 } ANullContext;
39
40 #define OFFSET(x) offsetof(ANullContext, x)
41
42 static const AVOption anullsrc_options[]= {
43     { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
44     { "cl",             "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
45     { "sample_rate",    "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
46     { "r",              "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
47     { "nb_samples",     "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
48     { "n",              "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
49     { NULL },
50 };
51
52 static const char *anullsrc_get_name(void *ctx)
53 {
54     return "anullsrc";
55 }
56
57 static const AVClass anullsrc_class = {
58     "ANullSrcContext",
59     anullsrc_get_name,
60     anullsrc_options
61 };
62
63 static int init(AVFilterContext *ctx, const char *args, void *opaque)
64 {
65     ANullContext *null = ctx->priv;
66     int ret;
67
68     null->class = &anullsrc_class;
69     av_opt_set_defaults(null);
70
71     if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0) {
72         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
73         return ret;
74     }
75
76     if ((ret = ff_parse_sample_rate(&null->sample_rate,
77                                      null->sample_rate_str, ctx)) < 0)
78         return ret;
79
80     if ((ret = ff_parse_channel_layout(&null->channel_layout,
81                                         null->channel_layout_str, ctx)) < 0)
82         return ret;
83
84     return 0;
85 }
86
87 static int config_props(AVFilterLink *outlink)
88 {
89     ANullContext *null = outlink->src->priv;
90     char buf[128];
91     int chans_nb;
92
93     outlink->sample_rate = null->sample_rate;
94     outlink->channel_layout = null->channel_layout;
95
96     chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
97     av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
98     av_log(outlink->src, AV_LOG_INFO,
99            "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
100            null->sample_rate, buf, null->nb_samples);
101
102     return 0;
103 }
104
105 static int request_frame(AVFilterLink *outlink)
106 {
107     ANullContext *null = outlink->src->priv;
108     AVFilterBufferRef *samplesref;
109
110     samplesref =
111         avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
112     samplesref->pts = null->pts;
113     samplesref->pos = -1;
114     samplesref->audio->channel_layout = null->channel_layout;
115     samplesref->audio->sample_rate = outlink->sample_rate;
116
117     avfilter_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
118     avfilter_unref_buffer(samplesref);
119
120     null->pts += null->nb_samples;
121     return 0;
122 }
123
124 AVFilter avfilter_asrc_anullsrc = {
125     .name        = "anullsrc",
126     .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
127
128     .init        = init,
129     .priv_size   = sizeof(ANullContext),
130
131     .inputs      = (const AVFilterPad[]) {{ .name = NULL}},
132
133     .outputs     = (const AVFilterPad[]) {{ .name = "default",
134                                       .type = AVMEDIA_TYPE_AUDIO,
135                                       .config_props = config_props,
136                                       .request_frame = request_frame, },
137                                     { .name = NULL}},
138 };