FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersink.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 103 170 60.6%
Functions: 19 25 76.0%
Branches: 60 138 43.5%

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 #define FF_INTERNAL_FIELDS 1
34 #include "framequeue.h"
35
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "buffersink.h"
39 #include "filters.h"
40 #include "internal.h"
41
42 typedef struct BufferSinkContext {
43 const AVClass *class;
44 unsigned warning_limit;
45
46 /* only used for video */
47 enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats
48 int pixel_fmts_size;
49
50 /* only used for audio */
51 enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats
52 int sample_fmts_size;
53 #if FF_API_OLD_CHANNEL_LAYOUT
54 int64_t *channel_layouts; ///< list of accepted channel layouts
55 int channel_layouts_size;
56 int *channel_counts; ///< list of accepted channel counts
57 int channel_counts_size;
58 #endif
59 char *channel_layouts_str; ///< list of accepted channel layouts
60 int all_channel_counts;
61 int *sample_rates; ///< list of accepted sample rates
62 int sample_rates_size;
63
64 AVFrame *peeked_frame;
65 } BufferSinkContext;
66
67 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
68
69 #if FF_API_OLD_CHANNEL_LAYOUT
70 1203 static void cleanup_redundant_layouts(AVFilterContext *ctx)
71 {
72 1203 BufferSinkContext *buf = ctx->priv;
73 1203 int nb_layouts = NB_ITEMS(buf->channel_layouts);
74 1203 int nb_counts = NB_ITEMS(buf->channel_counts);
75 1203 uint64_t counts = 0;
76 int i, lc, n;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 for (i = 0; i < nb_counts; i++)
79 if (buf->channel_counts[i] < 64)
80 counts |= (uint64_t)1 << buf->channel_counts[i];
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 for (i = lc = 0; i < nb_layouts; i++) {
82 n = av_popcount64(buf->channel_layouts[i]);
83 if (n < 64 && (counts & ((uint64_t)1 << n)))
84 av_log(ctx, AV_LOG_WARNING,
85 "Removing channel layout 0x%"PRIx64", redundant with %d channels\n",
86 buf->channel_layouts[i], n);
87 else
88 buf->channel_layouts[lc++] = buf->channel_layouts[i];
89 }
90 1203 buf->channel_layouts_size = lc * sizeof(*buf->channel_layouts);
91 1203 }
92 #endif
93
94 int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
95 {
96 return av_buffersink_get_frame_flags(ctx, frame, 0);
97 }
98
99 445170 static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
100 {
101
2/2
✓ Branch 0 taken 13375 times.
✓ Branch 1 taken 431795 times.
445170 if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
102 13375 buf->peeked_frame = in;
103
2/2
✓ Branch 0 taken 10469 times.
✓ Branch 1 taken 2906 times.
13375 return out ? av_frame_ref(out, in) : 0;
104 } else {
105 av_assert1(out);
106 431795 buf->peeked_frame = NULL;
107 431795 av_frame_move_ref(out, in);
108 431795 av_frame_free(&in);
109 431795 return 0;
110 }
111 }
112
113 1226734 static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
114 {
115 1226734 BufferSinkContext *buf = ctx->priv;
116 1226734 AVFilterLink *inlink = ctx->inputs[0];
117 int status, ret;
118 AVFrame *cur_frame;
119 int64_t pts;
120
121
2/2
✓ Branch 0 taken 1213359 times.
✓ Branch 1 taken 13375 times.
1226734 if (buf->peeked_frame)
122 13375 return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
123
124 while (1) {
125
2/2
✓ Branch 0 taken 339880 times.
✓ Branch 1 taken 2212044 times.
2551924 ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
126 2212044 ff_inlink_consume_frame(inlink, &cur_frame);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2551924 times.
2551924 if (ret < 0) {
128 return ret;
129
2/2
✓ Branch 0 taken 431795 times.
✓ Branch 1 taken 2120129 times.
2551924 } else if (ret) {
130 /* TODO return the frame instead of copying it */
131 431795 return return_or_keep_frame(buf, frame, cur_frame, flags);
132
2/2
✓ Branch 1 taken 7813 times.
✓ Branch 2 taken 2112316 times.
2120129 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
133 7813 return status;
134
2/2
✓ Branch 0 taken 395358 times.
✓ Branch 1 taken 1716958 times.
2112316 } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
135 395358 return AVERROR(EAGAIN);
136
2/2
✓ Branch 0 taken 1341323 times.
✓ Branch 1 taken 375635 times.
1716958 } else if (inlink->frame_wanted_out) {
137 1341323 ret = ff_filter_graph_run_once(ctx->graph);
138
2/2
✓ Branch 0 taken 378393 times.
✓ Branch 1 taken 962930 times.
1341323 if (ret < 0)
139 378393 return ret;
140 } else {
141 375635 ff_inlink_request_frame(inlink);
142 }
143 }
144 }
145
146 1226734 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
147 {
148 1226734 return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
149 }
150
151 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
152 AVFrame *frame, int nb_samples)
153 {
154 return get_frame_internal(ctx, frame, 0, nb_samples);
155 }
156
157 #if FF_API_BUFFERSINK_ALLOC
158 AVBufferSinkParams *av_buffersink_params_alloc(void)
159 {
160 static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
161 AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
162 if (!params)
163 return NULL;
164
165 params->pixel_fmts = pixel_fmts;
166 return params;
167 }
168
169 AVABufferSinkParams *av_abuffersink_params_alloc(void)
170 {
171 AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
172
173 if (!params)
174 return NULL;
175 return params;
176 }
177 #endif
178
179 6266 static av_cold int common_init(AVFilterContext *ctx)
180 {
181 6266 BufferSinkContext *buf = ctx->priv;
182
183 6266 buf->warning_limit = 100;
184 6266 return 0;
185 }
186
187 415108 static int activate(AVFilterContext *ctx)
188 {
189 415108 BufferSinkContext *buf = ctx->priv;
190
191
1/2
✓ Branch 0 taken 415108 times.
✗ Branch 1 not taken.
415108 if (buf->warning_limit &&
192
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 415103 times.
415108 ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
193 5 av_log(ctx, AV_LOG_WARNING,
194 "%d buffers queued in %s, something may be wrong.\n",
195 buf->warning_limit,
196 5 (char *)av_x_if_null(ctx->name, ctx->filter->name));
197 5 buf->warning_limit *= 10;
198 }
199
200 /* The frame is queued, the rest is up to get_frame_internal */
201 415108 return 0;
202 }
203
204 238 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
205 {
206 238 AVFilterLink *inlink = ctx->inputs[0];
207
208 238 inlink->min_samples = inlink->max_samples = frame_size;
209 238 }
210
211 #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
212 type av_buffersink_get_##field(const AVFilterContext *ctx) { \
213 av_assert0(ctx->filter->activate == activate); \
214 return ctx->inputs[0]->field; \
215 }
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209585 times.
1209585 MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type )
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 431901 times.
431901 MAKE_AVFILTERLINK_ACCESSOR(AVRational , time_base )
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12449 times.
12449 MAKE_AVFILTERLINK_ACCESSOR(int , format )
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111048 times.
111048 MAKE_AVFILTERLINK_ACCESSOR(AVRational , frame_rate )
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11247 times.
11247 MAKE_AVFILTERLINK_ACCESSOR(int , w )
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11247 times.
11247 MAKE_AVFILTERLINK_ACCESSOR(int , h )
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5024 times.
5024 MAKE_AVFILTERLINK_ACCESSOR(AVRational , sample_aspect_ratio)
225
226 #if FF_API_OLD_CHANNEL_LAYOUT
227 FF_DISABLE_DEPRECATION_WARNINGS
228 MAKE_AVFILTERLINK_ACCESSOR(uint64_t , channel_layout )
229 FF_ENABLE_DEPRECATION_WARNINGS
230 #endif
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7425 times.
7425 MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6183 times.
6183 MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef * , hw_frames_ctx )
234
235 int av_buffersink_get_channels(const AVFilterContext *ctx)
236 {
237 av_assert0(ctx->filter->activate == activate);
238 return ctx->inputs[0]->ch_layout.nb_channels;
239 }
240
241 7425 int av_buffersink_get_ch_layout(const AVFilterContext *ctx, AVChannelLayout *out)
242 {
243 7425 AVChannelLayout ch_layout = { 0 };
244 int ret;
245
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7425 times.
7425 av_assert0(ctx->filter->activate == activate);
247 7425 ret = av_channel_layout_copy(&ch_layout, &ctx->inputs[0]->ch_layout);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7425 times.
7425 if (ret < 0)
249 return ret;
250 7425 *out = ch_layout;
251 7425 return 0;
252 }
253
254 #define CHECK_LIST_SIZE(field) \
255 if (buf->field ## _size % sizeof(*buf->field)) { \
256 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
257 "should be multiple of %d\n", \
258 buf->field ## _size, (int)sizeof(*buf->field)); \
259 return AVERROR(EINVAL); \
260 }
261 5063 static int vsink_query_formats(AVFilterContext *ctx)
262 {
263 5063 BufferSinkContext *buf = ctx->priv;
264 5063 AVFilterFormats *formats = NULL;
265 unsigned i;
266 int ret;
267
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5063 times.
5063 CHECK_LIST_SIZE(pixel_fmts)
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5063 times.
5063 if (buf->pixel_fmts_size) {
270 for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
271 if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
272 return ret;
273 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
274 return ret;
275 } else {
276
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5063 times.
5063 if ((ret = ff_default_query_formats(ctx)) < 0)
277 return ret;
278 }
279
280 5063 return 0;
281 }
282
283 1203 static int asink_query_formats(AVFilterContext *ctx)
284 {
285 1203 BufferSinkContext *buf = ctx->priv;
286 1203 AVFilterFormats *formats = NULL;
287 1203 AVChannelLayout layout = { 0 };
288 1203 AVFilterChannelLayouts *layouts = NULL;
289 unsigned i;
290 int ret;
291
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 CHECK_LIST_SIZE(sample_fmts)
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 CHECK_LIST_SIZE(sample_rates)
294 #if FF_API_OLD_CHANNEL_LAYOUT
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 CHECK_LIST_SIZE(channel_layouts)
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 CHECK_LIST_SIZE(channel_counts)
297 #endif
298
299
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1180 times.
1203 if (buf->sample_fmts_size) {
300
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 23 times.
138 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
115 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
302 return ret;
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
304 return ret;
305 }
306
307 1203 if (
308 #if FF_API_OLD_CHANNEL_LAYOUT
309
2/4
✓ Branch 0 taken 1203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1203 times.
✗ Branch 3 not taken.
1203 buf->channel_layouts_size || buf->channel_counts_size ||
310 #endif
311
2/4
✓ Branch 0 taken 1203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1203 times.
✗ Branch 3 not taken.
1203 buf->channel_layouts_str || buf->all_channel_counts) {
312 #if FF_API_OLD_CHANNEL_LAYOUT
313 1203 cleanup_redundant_layouts(ctx);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
315 if ((ret = av_channel_layout_from_mask(&layout, buf->channel_layouts[i])) < 0 ||
316 (ret = ff_add_channel_layout(&layouts, &layout) < 0))
317 return ret;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 for (i = 0; i < NB_ITEMS(buf->channel_counts); i++) {
319 layout = FF_COUNT2LAYOUT(buf->channel_counts[i]);
320 if ((ret = ff_add_channel_layout(&layouts, &layout)) < 0)
321 return ret;
322 }
323 #endif
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 if (buf->channel_layouts_str) {
325 const char *cur = buf->channel_layouts_str;
326
327 #if FF_API_OLD_CHANNEL_LAYOUT
328 if (layouts)
329 av_log(ctx, AV_LOG_WARNING,
330 "Conflicting ch_layouts and list of channel_counts/channel_layouts. Ignoring the former\n");
331 else
332 #endif
333 while (cur) {
334 char *next = strchr(cur, '|');
335 if (next)
336 *next++ = 0;
337
338 ret = av_channel_layout_from_string(&layout, cur);
339 if (ret < 0) {
340 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur);
341 return ret;
342 }
343 ret = ff_add_channel_layout(&layouts, &layout);
344 av_channel_layout_uninit(&layout);
345 if (ret < 0)
346 return ret;
347
348 cur = next;
349 }
350 }
351
352
1/2
✓ Branch 0 taken 1203 times.
✗ Branch 1 not taken.
1203 if (buf->all_channel_counts) {
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 if (layouts)
354 av_log(ctx, AV_LOG_WARNING,
355 "Conflicting all_channel_counts and list in options\n");
356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1203 times.
1203 else if (!(layouts = ff_all_channel_counts()))
357 return AVERROR(ENOMEM);
358 }
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1203 times.
1203 if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
360 return ret;
361 }
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1203 times.
1203 if (buf->sample_rates_size) {
364 formats = NULL;
365 for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
366 if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
367 return ret;
368 if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
369 return ret;
370 }
371
372 1203 return 0;
373 }
374
375 #define OFFSET(x) offsetof(BufferSinkContext, x)
376 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
377 static const AVOption buffersink_options[] = {
378 { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
379 { NULL },
380 };
381 #undef FLAGS
382 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
383 static const AVOption abuffersink_options[] = {
384 { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
385 { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
386 #if FF_API_OLD_CHANNEL_LAYOUT
387 { "channel_layouts", "set the supported channel layouts (deprecated, use ch_layouts)",
388 OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS | AV_OPT_FLAG_DEPRECATED },
389 { "channel_counts", "set the supported channel counts (deprecated, use ch_layouts)",
390 OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS | AV_OPT_FLAG_DEPRECATED },
391 #endif
392 { "ch_layouts", "set a '|'-separated list of supported channel layouts",
393 OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
394 { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
395 { NULL },
396 };
397 #undef FLAGS
398
399 AVFILTER_DEFINE_CLASS(buffersink);
400 AVFILTER_DEFINE_CLASS(abuffersink);
401
402 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
403 {
404 .name = "default",
405 .type = AVMEDIA_TYPE_VIDEO,
406 },
407 };
408
409 const AVFilter ff_vsink_buffer = {
410 .name = "buffersink",
411 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
412 .priv_size = sizeof(BufferSinkContext),
413 .priv_class = &buffersink_class,
414 .init = common_init,
415 .activate = activate,
416 FILTER_INPUTS(avfilter_vsink_buffer_inputs),
417 .outputs = NULL,
418 FILTER_QUERY_FUNC(vsink_query_formats),
419 };
420
421 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
422 {
423 .name = "default",
424 .type = AVMEDIA_TYPE_AUDIO,
425 },
426 };
427
428 const AVFilter ff_asink_abuffer = {
429 .name = "abuffersink",
430 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
431 .priv_class = &abuffersink_class,
432 .priv_size = sizeof(BufferSinkContext),
433 .init = common_init,
434 .activate = activate,
435 FILTER_INPUTS(avfilter_asink_abuffer_inputs),
436 .outputs = NULL,
437 FILTER_QUERY_FUNC(asink_query_formats),
438 };
439