]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_anullsrc.c
lavfi/avcodec: make avfilter_fill_frame_from*() functions use avfilter_copy_buf_props()
[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 "internal.h"
28 #include "libavutil/audioconvert.h"
29 #include "libavutil/opt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "internal.h"
34
35 typedef struct {
36     const AVClass *class;
37     char   *channel_layout_str;
38     uint64_t channel_layout;
39     char   *sample_rate_str;
40     int     sample_rate;
41     int nb_samples;             ///< number of samples per requested frame
42     int64_t pts;
43 } ANullContext;
44
45 #define OFFSET(x) offsetof(ANullContext, x)
46
47 static const AVOption anullsrc_options[]= {
48     { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
49     { "cl",             "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
50     { "sample_rate",    "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
51     { "r",              "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
52     { "nb_samples",     "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
53     { "n",              "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
54     { NULL },
55 };
56
57 static const AVClass anullsrc_class = {
58     .class_name = "anullsrc",
59     .item_name  = av_default_item_name,
60     .option     = anullsrc_options,
61     .version    = LIBAVUTIL_VERSION_INT,
62     .category   = AV_CLASS_CATEGORY_FILTER,
63 };
64
65 static int init(AVFilterContext *ctx, const char *args, void *opaque)
66 {
67     ANullContext *null = ctx->priv;
68     int ret;
69
70     null->class = &anullsrc_class;
71     av_opt_set_defaults(null);
72
73     if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0) {
74         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
75         return ret;
76     }
77
78     if ((ret = ff_parse_sample_rate(&null->sample_rate,
79                                      null->sample_rate_str, ctx)) < 0)
80         return ret;
81
82     if ((ret = ff_parse_channel_layout(&null->channel_layout,
83                                         null->channel_layout_str, ctx)) < 0)
84         return ret;
85
86     return 0;
87 }
88
89 static int config_props(AVFilterLink *outlink)
90 {
91     ANullContext *null = outlink->src->priv;
92     char buf[128];
93     int chans_nb;
94
95     outlink->sample_rate = null->sample_rate;
96     outlink->channel_layout = null->channel_layout;
97
98     chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
99     av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
100     av_log(outlink->src, AV_LOG_INFO,
101            "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
102            null->sample_rate, buf, null->nb_samples);
103
104     return 0;
105 }
106
107 static int request_frame(AVFilterLink *outlink)
108 {
109     ANullContext *null = outlink->src->priv;
110     AVFilterBufferRef *samplesref;
111
112     samplesref =
113         ff_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
114     samplesref->pts = null->pts;
115     samplesref->pos = -1;
116     samplesref->audio->channel_layout = null->channel_layout;
117     samplesref->audio->sample_rate = outlink->sample_rate;
118
119     ff_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
120     avfilter_unref_buffer(samplesref);
121
122     null->pts += null->nb_samples;
123     return 0;
124 }
125
126 AVFilter avfilter_asrc_anullsrc = {
127     .name        = "anullsrc",
128     .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
129
130     .init        = init,
131     .priv_size   = sizeof(ANullContext),
132
133     .inputs      = (const AVFilterPad[]) {{ .name = NULL}},
134
135     .outputs     = (const AVFilterPad[]) {{ .name = "default",
136                                       .type = AVMEDIA_TYPE_AUDIO,
137                                       .config_props = config_props,
138                                       .request_frame = request_frame, },
139                                     { .name = NULL}},
140 };