FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asrc_anullsrc.c
Go to the documentation of this file.
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 
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;
40  uint64_t channel_layout;
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 int init(AVFilterContext *ctx, const char *args)
63 {
64  ANullContext *null = ctx->priv;
65  int ret;
66 
67  null->class = &anullsrc_class;
68  av_opt_set_defaults(null);
69 
70  if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0)
71  return ret;
72 
73  if ((ret = ff_parse_sample_rate(&null->sample_rate,
74  null->sample_rate_str, ctx)) < 0)
75  return ret;
76 
77  if ((ret = ff_parse_channel_layout(&null->channel_layout,
78  null->channel_layout_str, ctx)) < 0)
79  return ret;
80 
81  return 0;
82 }
83 
84 static int config_props(AVFilterLink *outlink)
85 {
86  ANullContext *null = outlink->src->priv;
87  char buf[128];
88  int chans_nb;
89 
90  outlink->sample_rate = null->sample_rate;
91  outlink->channel_layout = null->channel_layout;
92 
94  av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
95  av_log(outlink->src, AV_LOG_VERBOSE,
96  "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
97  null->sample_rate, buf, null->nb_samples);
98 
99  return 0;
100 }
101 
102 static int request_frame(AVFilterLink *outlink)
103 {
104  ANullContext *null = outlink->src->priv;
105  AVFilterBufferRef *samplesref;
106 
107  samplesref =
109  samplesref->pts = null->pts;
110  samplesref->pos = -1;
111  samplesref->audio->channel_layout = null->channel_layout;
112  samplesref->audio->sample_rate = outlink->sample_rate;
113 
114  ff_filter_frame(outlink, avfilter_ref_buffer(samplesref, ~0));
115  avfilter_unref_buffer(samplesref);
116 
117  null->pts += null->nb_samples;
118  return 0;
119 }
120 
122  {
123  .name = "default",
124  .type = AVMEDIA_TYPE_AUDIO,
125  .config_props = config_props,
126  .request_frame = request_frame,
127  },
128  { NULL }
129 };
130 
132  .name = "anullsrc",
133  .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
134 
135  .init = init,
136  .priv_size = sizeof(ANullContext),
137 
138  .inputs = NULL,
139 
140  .outputs = avfilter_asrc_anullsrc_outputs,
141  .priv_class = &anullsrc_class,
142 };