]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_anullsrc.c
Merge remote-tracking branch 'cus/stable'
[ffmpeg] / libavfilter / asrc_anullsrc.c
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 "audio.h"
31 #include "avfilter.h"
32 #include "internal.h"
33
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/opt.h"
37
38 typedef struct {
39     const AVClass *class;
40     char   *channel_layout_str;
41     uint64_t channel_layout;
42     char   *sample_rate_str;
43     int     sample_rate;
44     int nb_samples;             ///< number of samples per requested frame
45     int64_t pts;
46 } ANullContext;
47
48 #define OFFSET(x) offsetof(ANullContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
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     { NULL },
59 };
60
61 AVFILTER_DEFINE_CLASS(anullsrc);
62
63 static int init(AVFilterContext *ctx, const char *args)
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         return ret;
73
74     if ((ret = ff_parse_sample_rate(&null->sample_rate,
75                                      null->sample_rate_str, ctx)) < 0)
76         return ret;
77
78     if ((ret = ff_parse_channel_layout(&null->channel_layout,
79                                         null->channel_layout_str, ctx)) < 0)
80         return ret;
81
82     return 0;
83 }
84
85 static int config_props(AVFilterLink *outlink)
86 {
87     ANullContext *null = outlink->src->priv;
88     char buf[128];
89     int chans_nb;
90
91     outlink->sample_rate = null->sample_rate;
92     outlink->channel_layout = null->channel_layout;
93
94     chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
95     av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
96     av_log(outlink->src, AV_LOG_VERBOSE,
97            "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
98            null->sample_rate, buf, null->nb_samples);
99
100     return 0;
101 }
102
103 static int request_frame(AVFilterLink *outlink)
104 {
105     ANullContext *null = outlink->src->priv;
106     AVFilterBufferRef *samplesref;
107
108     samplesref =
109         ff_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
110     samplesref->pts = null->pts;
111     samplesref->pos = -1;
112     samplesref->audio->channel_layout = null->channel_layout;
113     samplesref->audio->sample_rate = outlink->sample_rate;
114
115     ff_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
116     avfilter_unref_buffer(samplesref);
117
118     null->pts += null->nb_samples;
119     return 0;
120 }
121
122 static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
123     {
124         .name          = "default",
125         .type          = AVMEDIA_TYPE_AUDIO,
126         .config_props  = config_props,
127         .request_frame = request_frame,
128     },
129     { NULL }
130 };
131
132 AVFilter avfilter_asrc_anullsrc = {
133     .name        = "anullsrc",
134     .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
135
136     .init        = init,
137     .priv_size   = sizeof(ANullContext),
138
139     .inputs      = NULL,
140
141     .outputs     = avfilter_asrc_anullsrc_outputs,
142     .priv_class = &anullsrc_class,
143 };