FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_anullsrc.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 0 27 0.0%
Functions: 0 3 0.0%
Branches: 0 16 0.0%

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 #include "internal.h"
38
39 typedef struct ANullContext {
40 const AVClass *class;
41 AVChannelLayout ch_layout;
42 int sample_rate;
43 int64_t duration;
44 int nb_samples; ///< number of samples per requested frame
45 int64_t pts;
46 } ANullContext;
47
48 #define OFFSET(x) offsetof(ANullContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
51 static const AVOption anullsrc_options[]= {
52 { "channel_layout", "set channel_layout", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, FLAGS },
53 { "cl", "set channel_layout", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, FLAGS },
54 { "sample_rate", "set sample rate", OFFSET(sample_rate) , AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT_MAX, FLAGS },
55 { "r", "set sample rate", OFFSET(sample_rate) , AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT_MAX, FLAGS },
56 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
57 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_MAX, FLAGS },
58 { "duration", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
59 { "d", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
60 { NULL }
61 };
62
63 AVFILTER_DEFINE_CLASS(anullsrc);
64
65 static int query_formats(AVFilterContext *ctx)
66 {
67 ANullContext *null = ctx->priv;
68 const AVChannelLayout chlayouts[] = { null->ch_layout, { 0 } };
69 int sample_rates[] = { null->sample_rate, -1 };
70 int ret;
71
72 if ((ret = ff_set_common_formats (ctx, ff_all_formats (AVMEDIA_TYPE_AUDIO))) < 0 ||
73 (ret = ff_set_common_samplerates_from_list(ctx, sample_rates)) < 0)
74 return ret;
75
76 return ff_set_common_channel_layouts_from_list(ctx, chlayouts);
77 }
78
79 static av_cold int config_props(AVFilterLink *outlink)
80 {
81 ANullContext *null = outlink->src->priv;
82
83 if (null->duration >= 0)
84 null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE);
85
86 return 0;
87 }
88
89 static int activate(AVFilterContext *ctx)
90 {
91 ANullContext *null = ctx->priv;
92 AVFilterLink *outlink = ctx->outputs[0];
93
94 if (null->duration >= 0 && null->pts >= null->duration) {
95 ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
96 return 0;
97 }
98
99 if (ff_outlink_frame_wanted(outlink)) {
100 AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples);
101
102 if (!samplesref)
103 return AVERROR(ENOMEM);
104
105 samplesref->pts = null->pts;
106 null->pts += samplesref->nb_samples;
107
108 return ff_filter_frame(outlink, samplesref);
109 }
110
111 return FFERROR_NOT_READY;
112 }
113
114 static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
115 {
116 .name = "default",
117 .type = AVMEDIA_TYPE_AUDIO,
118 .config_props = config_props,
119 },
120 };
121
122 const AVFilter ff_asrc_anullsrc = {
123 .name = "anullsrc",
124 .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
125 .priv_size = sizeof(ANullContext),
126 .inputs = NULL,
127 FILTER_OUTPUTS(avfilter_asrc_anullsrc_outputs),
128 FILTER_QUERY_FUNC(query_formats),
129 .activate = activate,
130 .priv_class = &anullsrc_class,
131 };
132