2 * Copyright 2010 S.N. Hemanth Meenakshisundaram <smeenaks ucsd edu>
3 * Copyright 2010 Stefano Sabatini <stefano.sabatini-lala poste it>
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
38 typedef struct ANullContext
{
40 AVChannelLayout ch_layout
;
43 int nb_samples
; ///< number of samples per requested frame
47 #define OFFSET(x) offsetof(ANullContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 static const AVOption anullsrc_options
[]= {
51 { "channel_layout", "set channel_layout", OFFSET(ch_layout
), AV_OPT_TYPE_CHLAYOUT
, {.str
= "stereo"}, 0, 0, FLAGS
},
52 { "cl", "set channel_layout", OFFSET(ch_layout
), AV_OPT_TYPE_CHLAYOUT
, {.str
= "stereo"}, 0, 0, FLAGS
},
53 { "sample_rate", "set sample rate", OFFSET(sample_rate
) , AV_OPT_TYPE_INT
, {.i64
= 44100}, 1, INT_MAX
, FLAGS
},
54 { "r", "set sample rate", OFFSET(sample_rate
) , AV_OPT_TYPE_INT
, {.i64
= 44100}, 1, INT_MAX
, FLAGS
},
55 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples
), AV_OPT_TYPE_INT
, {.i64
= 1024}, 1, UINT16_MAX
, FLAGS
},
56 { "n", "set the number of samples per requested frame", OFFSET(nb_samples
), AV_OPT_TYPE_INT
, {.i64
= 1024}, 1, UINT16_MAX
, FLAGS
},
57 { "duration", "set the audio duration", OFFSET(duration
), AV_OPT_TYPE_DURATION
, {.i64
= -1}, -1, INT64_MAX
, FLAGS
},
58 { "d", "set the audio duration", OFFSET(duration
), AV_OPT_TYPE_DURATION
, {.i64
= -1}, -1, INT64_MAX
, FLAGS
},
62 AVFILTER_DEFINE_CLASS(anullsrc
);
64 static int query_formats(const AVFilterContext
*ctx
,
65 AVFilterFormatsConfig
**cfg_in
,
66 AVFilterFormatsConfig
**cfg_out
)
68 const ANullContext
*null
= ctx
->priv
;
69 const AVChannelLayout chlayouts
[] = { null
->ch_layout
, { 0 } };
70 int sample_rates
[] = { null
->sample_rate
, -1 };
73 ret
= ff_set_common_samplerates_from_list2(ctx
, cfg_in
, cfg_out
, sample_rates
);
77 return ff_set_common_channel_layouts_from_list2(ctx
, cfg_in
, cfg_out
, chlayouts
);
80 static av_cold
int config_props(AVFilterLink
*outlink
)
82 ANullContext
*null
= outlink
->src
->priv
;
84 if (null
->duration
>= 0)
85 null
->duration
= av_rescale(null
->duration
, null
->sample_rate
, AV_TIME_BASE
);
90 static int activate(AVFilterContext
*ctx
)
92 ANullContext
*null
= ctx
->priv
;
93 AVFilterLink
*outlink
= ctx
->outputs
[0];
95 if (null
->duration
>= 0 && null
->pts
>= null
->duration
) {
96 ff_outlink_set_status(outlink
, AVERROR_EOF
, null
->pts
);
100 if (ff_outlink_frame_wanted(outlink
)) {
101 AVFrame
*samplesref
= ff_get_audio_buffer(outlink
, null
->duration
>= 0 ? FFMIN(null
->nb_samples
, null
->duration
- null
->pts
) : null
->nb_samples
);
104 return AVERROR(ENOMEM
);
106 samplesref
->pts
= null
->pts
;
107 null
->pts
+= samplesref
->nb_samples
;
109 return ff_filter_frame(outlink
, samplesref
);
112 return FFERROR_NOT_READY
;
115 static const AVFilterPad avfilter_asrc_anullsrc_outputs
[] = {
118 .type
= AVMEDIA_TYPE_AUDIO
,
119 .config_props
= config_props
,
123 const FFFilter ff_asrc_anullsrc
= {
124 .p
.name
= "anullsrc",
125 .p
.description
= NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
126 .p
.priv_class
= &anullsrc_class
,
127 .priv_size
= sizeof(ANullContext
),
128 FILTER_OUTPUTS(avfilter_asrc_anullsrc_outputs
),
129 FILTER_QUERY_FUNC2(query_formats
),
130 .activate
= activate
,