Line | Branch | Exec | Source |
---|---|---|---|
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 "formats.h" | ||
37 | |||
38 | typedef struct ANullContext { | ||
39 | const AVClass *class; | ||
40 | AVChannelLayout ch_layout; | ||
41 | int sample_rate; | ||
42 | int64_t duration; | ||
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(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 }, | ||
59 | { NULL } | ||
60 | }; | ||
61 | |||
62 | AVFILTER_DEFINE_CLASS(anullsrc); | ||
63 | |||
64 | 1 | static int query_formats(const AVFilterContext *ctx, | |
65 | AVFilterFormatsConfig **cfg_in, | ||
66 | AVFilterFormatsConfig **cfg_out) | ||
67 | { | ||
68 | 1 | const ANullContext *null = ctx->priv; | |
69 | 1 | const AVChannelLayout chlayouts[] = { null->ch_layout, { 0 } }; | |
70 | 1 | int sample_rates[] = { null->sample_rate, -1 }; | |
71 | int ret; | ||
72 | |||
73 | 1 | ret = ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates); | |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
75 | ✗ | return ret; | |
76 | |||
77 | 1 | return ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts); | |
78 | } | ||
79 | |||
80 | 1 | static av_cold int config_props(AVFilterLink *outlink) | |
81 | { | ||
82 | 1 | ANullContext *null = outlink->src->priv; | |
83 | |||
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (null->duration >= 0) |
85 | ✗ | null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE); | |
86 | |||
87 | 1 | return 0; | |
88 | } | ||
89 | |||
90 | 88 | static int activate(AVFilterContext *ctx) | |
91 | { | ||
92 | 88 | ANullContext *null = ctx->priv; | |
93 | 88 | AVFilterLink *outlink = ctx->outputs[0]; | |
94 | |||
95 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
88 | if (null->duration >= 0 && null->pts >= null->duration) { |
96 | ✗ | ff_outlink_set_status(outlink, AVERROR_EOF, null->pts); | |
97 | ✗ | return 0; | |
98 | } | ||
99 | |||
100 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | if (ff_outlink_frame_wanted(outlink)) { |
101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples); |
102 | |||
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (!samplesref) |
104 | ✗ | return AVERROR(ENOMEM); | |
105 | |||
106 | 88 | samplesref->pts = null->pts; | |
107 | 88 | null->pts += samplesref->nb_samples; | |
108 | |||
109 | 88 | return ff_filter_frame(outlink, samplesref); | |
110 | } | ||
111 | |||
112 | ✗ | return FFERROR_NOT_READY; | |
113 | } | ||
114 | |||
115 | static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = { | ||
116 | { | ||
117 | .name = "default", | ||
118 | .type = AVMEDIA_TYPE_AUDIO, | ||
119 | .config_props = config_props, | ||
120 | }, | ||
121 | }; | ||
122 | |||
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, | ||
131 | }; | ||
132 |