]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_anullsrc.c
f2c0799faa7b9dfebac6da9da5fbca6cc7354079
[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 "libavutil/channel_layout.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "filters.h"
36 #include "internal.h"
37
38 typedef struct ANullContext {
39     const AVClass *class;
40     char   *channel_layout_str;
41     uint64_t channel_layout;
42     char   *sample_rate_str;
43     int     sample_rate;
44     int64_t duration;
45     int nb_samples;             ///< number of samples per requested frame
46     int64_t pts;
47 } ANullContext;
48
49 #define OFFSET(x) offsetof(ANullContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption anullsrc_options[]= {
53     { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
54     { "cl",             "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
55     { "sample_rate",    "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
56     { "r",              "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
57     { "nb_samples",     "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
58     { "n",              "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
59     { "duration",       "set the audio duration",                        OFFSET(duration),   AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
60     { "d",              "set the audio duration",                        OFFSET(duration),   AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
61     { NULL }
62 };
63
64 AVFILTER_DEFINE_CLASS(anullsrc);
65
66 static av_cold int init(AVFilterContext *ctx)
67 {
68     ANullContext *null = ctx->priv;
69     int ret;
70
71     if ((ret = ff_parse_sample_rate(&null->sample_rate,
72                                      null->sample_rate_str, ctx)) < 0)
73         return ret;
74
75     if ((ret = ff_parse_channel_layout(&null->channel_layout, NULL,
76                                         null->channel_layout_str, ctx)) < 0)
77         return ret;
78
79     return 0;
80 }
81
82 static int query_formats(AVFilterContext *ctx)
83 {
84     ANullContext *null = ctx->priv;
85     int64_t chlayouts[] = { null->channel_layout, -1 };
86     int sample_rates[] = { null->sample_rate, -1 };
87     int ret;
88
89     if ((ret = ff_set_common_formats         (ctx, ff_all_formats              (AVMEDIA_TYPE_AUDIO))) < 0 ||
90         (ret = ff_set_common_samplerates     (ctx, ff_make_format_list         (sample_rates      ))) < 0)
91         return ret;
92
93     return ff_set_common_channel_layouts(ctx, ff_make_format64_list(chlayouts));
94 }
95
96 static av_cold int config_props(AVFilterLink *outlink)
97 {
98     ANullContext *null = outlink->src->priv;
99
100     if (null->duration >= 0)
101         null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE);
102
103     return 0;
104 }
105
106 static int activate(AVFilterContext *ctx)
107 {
108     ANullContext *null = ctx->priv;
109     AVFilterLink *outlink = ctx->outputs[0];
110
111     if (null->duration >= 0 && null->pts >= null->duration) {
112         ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
113         return 0;
114     }
115
116     if (ff_outlink_frame_wanted(outlink)) {
117         AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples);
118
119         if (!samplesref)
120             return AVERROR(ENOMEM);
121
122         samplesref->pts = null->pts;
123         null->pts += samplesref->nb_samples;
124
125         return ff_filter_frame(outlink, samplesref);
126     }
127
128     return FFERROR_NOT_READY;
129 }
130
131 static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
132     {
133         .name          = "default",
134         .type          = AVMEDIA_TYPE_AUDIO,
135         .config_props  = config_props,
136     },
137     { NULL }
138 };
139
140 AVFilter ff_asrc_anullsrc = {
141     .name          = "anullsrc",
142     .description   = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
143     .init          = init,
144     .query_formats = query_formats,
145     .priv_size     = sizeof(ANullContext),
146     .inputs        = NULL,
147     .outputs       = avfilter_asrc_anullsrc_outputs,
148     .activate      = activate,
149     .priv_class    = &anullsrc_class,
150 };