FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersink.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 86 148 58.1%
Functions: 17 24 70.8%
Branches: 52 124 41.9%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * buffer sink
24 */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/opt.h"
32
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "avfilter_internal.h"
36 #include "buffersink.h"
37 #include "filters.h"
38 #include "formats.h"
39 #include "framequeue.h"
40 #include "internal.h"
41 #include "video.h"
42
43 typedef struct BufferSinkContext {
44 const AVClass *class;
45 unsigned warning_limit;
46
47 /* only used for video */
48 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats
49 int pixel_fmts_size;
50 enum AVColorSpace *color_spaces; ///< list of accepted color spaces
51 int color_spaces_size;
52 enum AVColorRange *color_ranges; ///< list of accepted color ranges
53 int color_ranges_size;
54
55 /* only used for audio */
56 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats
57 int sample_fmts_size;
58 char *channel_layouts_str; ///< list of accepted channel layouts
59 int all_channel_counts;
60 int *sample_rates; ///< list of accepted sample rates
61 int sample_rates_size;
62
63 AVFrame *peeked_frame;
64 } BufferSinkContext;
65
66 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
67
68 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
69 {
70 return av_buffersink_get_frame_flags(ctx, frame, 0);
71 }
72
73 736190 static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
74 {
75
2/2
✓ Branch 0 taken 368380 times.
✓ Branch 1 taken 367810 times.
736190 if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
76 368380 buf->peeked_frame = in;
77
2/2
✓ Branch 0 taken 10685 times.
✓ Branch 1 taken 357695 times.
368380 return out ? av_frame_ref(out, in) : 0;
78 } else {
79 av_assert1(out);
80 367810 buf->peeked_frame = NULL;
81 367810 av_frame_move_ref(out, in);
82 367810 av_frame_free(&in);
83 367810 return 0;
84 }
85 }
86
87 1453936 static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
88 {
89 1453936 BufferSinkContext *buf = ctx->priv;
90 1453936 AVFilterLink *inlink = ctx->inputs[0];
91 int status, ret;
92 AVFrame *cur_frame;
93 int64_t pts;
94
95
2/2
✓ Branch 0 taken 1085556 times.
✓ Branch 1 taken 368380 times.
1453936 if (buf->peeked_frame)
96 368380 return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
97
98 while (1) {
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2721215 times.
2721215 ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
100 2721215 ff_inlink_consume_frame(inlink, &cur_frame);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2721215 times.
2721215 if (ret < 0) {
102 return ret;
103
2/2
✓ Branch 0 taken 367810 times.
✓ Branch 1 taken 2353405 times.
2721215 } else if (ret) {
104 /* TODO return the frame instead of copying it */
105 367810 return return_or_keep_frame(buf, frame, cur_frame, flags);
106
2/2
✓ Branch 1 taken 4215 times.
✓ Branch 2 taken 2349190 times.
2353405 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
107 4215 return status;
108
2/2
✓ Branch 0 taken 360306 times.
✓ Branch 1 taken 1988884 times.
2349190 } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
109 360306 return AVERROR(EAGAIN);
110
2/2
✓ Branch 0 taken 1624444 times.
✓ Branch 1 taken 364440 times.
1988884 } else if (inlink->frame_wanted_out) {
111 1624444 ret = ff_filter_graph_run_once(ctx->graph);
112
2/2
✓ Branch 0 taken 353225 times.
✓ Branch 1 taken 1271219 times.
1624444 if (ret < 0)
113 353225 return ret;
114 } else {
115 364440 ff_inlink_request_frame(inlink);
116 }
117 }
118 }
119
120 1453936 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
121 {
122 1453936 return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
123 }
124
125 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
126 AVFrame *frame, int nb_samples)
127 {
128 return get_frame_internal(ctx, frame, 0, nb_samples);
129 }
130
131 6520 static av_cold int common_init(AVFilterContext *ctx)
132 {
133 6520 BufferSinkContext *buf = ctx->priv;
134
135 6520 buf->warning_limit = 100;
136 6520 return 0;
137 }
138
139 6520 static void uninit(AVFilterContext *ctx)
140 {
141 6520 BufferSinkContext *buf = ctx->priv;
142
143 6520 av_frame_free(&buf->peeked_frame);
144 6520 }
145
146 371009 static int activate(AVFilterContext *ctx)
147 {
148 371009 BufferSinkContext *buf = ctx->priv;
149 371009 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[0]);
150
151
1/2
✓ Branch 0 taken 371009 times.
✗ Branch 1 not taken.
371009 if (buf->warning_limit &&
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 371009 times.
371009 ff_framequeue_queued_frames(&li->fifo) >= buf->warning_limit) {
153 av_log(ctx, AV_LOG_WARNING,
154 "%d buffers queued in %s, something may be wrong.\n",
155 buf->warning_limit,
156 (char *)av_x_if_null(ctx->name, ctx->filter->name));
157 buf->warning_limit *= 10;
158 }
159
160 /* The frame is queued, the rest is up to get_frame_internal */
161 371009 return 0;
162 }
163
164 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
165 {
166 AVFilterLink *inlink = ctx->inputs[0];
167
168 inlink->min_samples = inlink->max_samples = frame_size;
169 }
170
171 #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
172 type av_buffersink_get_##field(const AVFilterContext *ctx) { \
173 av_assert0(ctx->filter->activate == activate); \
174 return ctx->inputs[0]->field; \
175 }
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type )
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374356 times.
374356 MAKE_AVFILTERLINK_ACCESSOR(AVRational , time_base )
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6520 times.
6520 MAKE_AVFILTERLINK_ACCESSOR(int , format )
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8448 times.
8448 MAKE_AVFILTERLINK_ACCESSOR(AVRational , frame_rate )
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6496 times.
6496 MAKE_AVFILTERLINK_ACCESSOR(int , w )
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6496 times.
6496 MAKE_AVFILTERLINK_ACCESSOR(int , h )
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6496 times.
6496 MAKE_AVFILTERLINK_ACCESSOR(AVRational , sample_aspect_ratio)
185 MAKE_AVFILTERLINK_ACCESSOR(enum AVColorSpace, colorspace)
186 MAKE_AVFILTERLINK_ACCESSOR(enum AVColorRange, color_range)
187
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6499 times.
6499 MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
189
190 MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef * , hw_frames_ctx )
191
192 int av_buffersink_get_channels(const AVFilterContext *ctx)
193 {
194 av_assert0(ctx->filter->activate == activate);
195 return ctx->inputs[0]->ch_layout.nb_channels;
196 }
197
198 6499 int av_buffersink_get_ch_layout(const AVFilterContext *ctx, AVChannelLayout *out)
199 {
200 6499 AVChannelLayout ch_layout = { 0 };
201 int ret;
202
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6499 times.
6499 av_assert0(ctx->filter->activate == activate);
204 6499 ret = av_channel_layout_copy(&ch_layout, &ctx->inputs[0]->ch_layout);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6499 times.
6499 if (ret < 0)
206 return ret;
207 6499 *out = ch_layout;
208 6499 return 0;
209 }
210
211 #define CHECK_LIST_SIZE(field) \
212 if (buf->field ## _size % sizeof(*buf->field)) { \
213 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
214 "should be multiple of %d\n", \
215 buf->field ## _size, (int)sizeof(*buf->field)); \
216 return AVERROR(EINVAL); \
217 }
218 5253 static int vsink_query_formats(AVFilterContext *ctx)
219 {
220 5253 BufferSinkContext *buf = ctx->priv;
221 unsigned i;
222 int ret;
223
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 CHECK_LIST_SIZE(pixel_fmts)
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 CHECK_LIST_SIZE(color_spaces)
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 CHECK_LIST_SIZE(color_ranges)
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 if (buf->pixel_fmts_size) {
228 AVFilterFormats *formats = NULL;
229 for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
230 if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
231 return ret;
232 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
233 return ret;
234 }
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 if (buf->color_spaces_size) {
237 AVFilterFormats *formats = NULL;
238 for (i = 0; i < NB_ITEMS(buf->color_spaces); i++)
239 if ((ret = ff_add_format(&formats, buf->color_spaces[i])) < 0)
240 return ret;
241 if ((ret = ff_set_common_color_spaces(ctx, formats)) < 0)
242 return ret;
243 }
244
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5253 times.
5253 if (buf->color_ranges_size) {
246 AVFilterFormats *formats = NULL;
247 for (i = 0; i < NB_ITEMS(buf->color_ranges); i++)
248 if ((ret = ff_add_format(&formats, buf->color_ranges[i])) < 0)
249 return ret;
250 if ((ret = ff_set_common_color_ranges(ctx, formats)) < 0)
251 return ret;
252 }
253
254 5253 return 0;
255 }
256
257 1267 static int asink_query_formats(AVFilterContext *ctx)
258 {
259 1267 BufferSinkContext *buf = ctx->priv;
260 1267 AVFilterFormats *formats = NULL;
261 1267 AVChannelLayout layout = { 0 };
262 1267 AVFilterChannelLayouts *layouts = NULL;
263 unsigned i;
264 int ret;
265
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 CHECK_LIST_SIZE(sample_fmts)
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 CHECK_LIST_SIZE(sample_rates)
268
269
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1243 times.
1267 if (buf->sample_fmts_size) {
270
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
144 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
272 return ret;
273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
274 return ret;
275 }
276
277
2/4
✓ Branch 0 taken 1267 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1267 times.
✗ Branch 3 not taken.
1267 if (buf->channel_layouts_str || buf->all_channel_counts) {
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 if (buf->channel_layouts_str) {
279 const char *cur = buf->channel_layouts_str;
280
281 while (cur) {
282 char *next = strchr(cur, '|');
283 if (next)
284 *next++ = 0;
285
286 ret = av_channel_layout_from_string(&layout, cur);
287 if (ret < 0) {
288 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur);
289 return ret;
290 }
291 ret = ff_add_channel_layout(&layouts, &layout);
292 av_channel_layout_uninit(&layout);
293 if (ret < 0)
294 return ret;
295
296 cur = next;
297 }
298 }
299
300
1/2
✓ Branch 0 taken 1267 times.
✗ Branch 1 not taken.
1267 if (buf->all_channel_counts) {
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 if (layouts)
302 av_log(ctx, AV_LOG_WARNING,
303 "Conflicting all_channel_counts and list in options\n");
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1267 times.
1267 else if (!(layouts = ff_all_channel_counts()))
305 return AVERROR(ENOMEM);
306 }
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1267 times.
1267 if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
308 return ret;
309 }
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267 times.
1267 if (buf->sample_rates_size) {
312 formats = NULL;
313 for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
314 if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
315 return ret;
316 if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
317 return ret;
318 }
319
320 1267 return 0;
321 }
322
323 #define OFFSET(x) offsetof(BufferSinkContext, x)
324 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
325 static const AVOption buffersink_options[] = {
326 { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
327 { "color_spaces", "set the supported color spaces", OFFSET(color_spaces), AV_OPT_TYPE_BINARY, .flags = FLAGS },
328 { "color_ranges", "set the supported color ranges", OFFSET(color_ranges), AV_OPT_TYPE_BINARY, .flags = FLAGS },
329 { NULL },
330 };
331 #undef FLAGS
332 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
333 static const AVOption abuffersink_options[] = {
334 { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
335 { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
336 { "ch_layouts", "set a '|'-separated list of supported channel layouts",
337 OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
338 { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
339 { NULL },
340 };
341 #undef FLAGS
342
343 AVFILTER_DEFINE_CLASS(buffersink);
344 AVFILTER_DEFINE_CLASS(abuffersink);
345
346 const AVFilter ff_vsink_buffer = {
347 .name = "buffersink",
348 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
349 .priv_size = sizeof(BufferSinkContext),
350 .priv_class = &buffersink_class,
351 .init = common_init,
352 .uninit = uninit,
353 .activate = activate,
354 FILTER_INPUTS(ff_video_default_filterpad),
355 .outputs = NULL,
356 FILTER_QUERY_FUNC(vsink_query_formats),
357 };
358
359 const AVFilter ff_asink_abuffer = {
360 .name = "abuffersink",
361 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
362 .priv_class = &abuffersink_class,
363 .priv_size = sizeof(BufferSinkContext),
364 .init = common_init,
365 .uninit = uninit,
366 .activate = activate,
367 FILTER_INPUTS(ff_audio_default_filterpad),
368 .outputs = NULL,
369 FILTER_QUERY_FUNC(asink_query_formats),
370 };
371