FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersink.c
Date: 2023-10-02 11:06:47
Exec Total Coverage
Lines: 98 159 61.6%
Functions: 17 23 73.9%
Branches: 58 134 43.3%

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