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
cleanup_redundant_layouts(AVFilterContext * ctx)70 static void cleanup_redundant_layouts(AVFilterContext *ctx)
71 {
72 BufferSinkContext *buf = ctx->priv;
73 int nb_layouts = NB_ITEMS(buf->channel_layouts);
74 int nb_counts = NB_ITEMS(buf->channel_counts);
75 uint64_t counts = 0;
76 int i, lc, n;
77
78 for (i = 0; i < nb_counts; i++)
79 if (buf->channel_counts[i] < 64)
80 counts |= (uint64_t)1 << buf->channel_counts[i];
81 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 buf->channel_layouts_size = lc * sizeof(*buf->channel_layouts);
91 }
92 #endif
93
av_buffersink_get_frame(AVFilterContext * ctx,AVFrame * frame)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
return_or_keep_frame(BufferSinkContext * buf,AVFrame * out,AVFrame * in,int flags)99 static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
100 {
101 if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
102 buf->peeked_frame = in;
103 return out ? av_frame_ref(out, in) : 0;
104 } else {
105 av_assert1(out);
106 buf->peeked_frame = NULL;
107 av_frame_move_ref(out, in);
108 av_frame_free(&in);
109 return 0;
110 }
111 }
112
get_frame_internal(AVFilterContext * ctx,AVFrame * frame,int flags,int samples)113 static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
114 {
115 BufferSinkContext *buf = ctx->priv;
116 AVFilterLink *inlink = ctx->inputs[0];
117 int status, ret;
118 AVFrame *cur_frame;
119 int64_t pts;
120
121 if (buf->peeked_frame)
122 return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
123
124 while (1) {
125 ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
126 ff_inlink_consume_frame(inlink, &cur_frame);
127 if (ret < 0) {
128 return ret;
129 } else if (ret) {
130 /* TODO return the frame instead of copying it */
131 return return_or_keep_frame(buf, frame, cur_frame, flags);
132 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
133 return status;
134 } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
135 return AVERROR(EAGAIN);
136 } else if (inlink->frame_wanted_out) {
137 ret = ff_filter_graph_run_once(ctx->graph);
138 if (ret < 0)
139 return ret;
140 } else {
141 ff_inlink_request_frame(inlink);
142 }
143 }
144 }
145
av_buffersink_get_frame_flags(AVFilterContext * ctx,AVFrame * frame,int flags)146 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
147 {
148 return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
149 }
150
av_buffersink_get_samples(AVFilterContext * ctx,AVFrame * frame,int nb_samples)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
av_buffersink_params_alloc(void)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
av_abuffersink_params_alloc(void)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
common_init(AVFilterContext * ctx)179 static av_cold int common_init(AVFilterContext *ctx)
180 {
181 BufferSinkContext *buf = ctx->priv;
182
183 buf->warning_limit = 100;
184 return 0;
185 }
186
activate(AVFilterContext * ctx)187 static int activate(AVFilterContext *ctx)
188 {
189 BufferSinkContext *buf = ctx->priv;
190
191 if (buf->warning_limit &&
192 ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
193 av_log(ctx, AV_LOG_WARNING,
194 "%d buffers queued in %s, something may be wrong.\n",
195 buf->warning_limit,
196 (char *)av_x_if_null(ctx->name, ctx->filter->name));
197 buf->warning_limit *= 10;
198 }
199
200 /* The frame is queued, the rest is up to get_frame_internal */
201 return 0;
202 }
203
av_buffersink_set_frame_size(AVFilterContext * ctx,unsigned frame_size)204 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
205 {
206 AVFilterLink *inlink = ctx->inputs[0];
207
208 inlink->min_samples = inlink->max_samples = frame_size;
209 }
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
MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType,type)217 MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type )
218 MAKE_AVFILTERLINK_ACCESSOR(AVRational , time_base )
219 MAKE_AVFILTERLINK_ACCESSOR(int , format )
220
221 MAKE_AVFILTERLINK_ACCESSOR(AVRational , frame_rate )
222 MAKE_AVFILTERLINK_ACCESSOR(int , w )
223 MAKE_AVFILTERLINK_ACCESSOR(int , h )
224 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 MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
232
233 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
av_buffersink_get_ch_layout(const AVFilterContext * ctx,AVChannelLayout * out)241 int av_buffersink_get_ch_layout(const AVFilterContext *ctx, AVChannelLayout *out)
242 {
243 AVChannelLayout ch_layout = { 0 };
244 int ret;
245
246 av_assert0(ctx->filter->activate == activate);
247 ret = av_channel_layout_copy(&ch_layout, &ctx->inputs[0]->ch_layout);
248 if (ret < 0)
249 return ret;
250 *out = ch_layout;
251 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 }
vsink_query_formats(AVFilterContext * ctx)261 static int vsink_query_formats(AVFilterContext *ctx)
262 {
263 BufferSinkContext *buf = ctx->priv;
264 AVFilterFormats *formats = NULL;
265 unsigned i;
266 int ret;
267
268 CHECK_LIST_SIZE(pixel_fmts)
269 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 if ((ret = ff_default_query_formats(ctx)) < 0)
277 return ret;
278 }
279
280 return 0;
281 }
282
asink_query_formats(AVFilterContext * ctx)283 static int asink_query_formats(AVFilterContext *ctx)
284 {
285 BufferSinkContext *buf = ctx->priv;
286 AVFilterFormats *formats = NULL;
287 AVChannelLayout layout = { 0 };
288 AVFilterChannelLayouts *layouts = NULL;
289 unsigned i;
290 int ret;
291
292 CHECK_LIST_SIZE(sample_fmts)
293 CHECK_LIST_SIZE(sample_rates)
294 #if FF_API_OLD_CHANNEL_LAYOUT
295 CHECK_LIST_SIZE(channel_layouts)
296 CHECK_LIST_SIZE(channel_counts)
297 #endif
298
299 if (buf->sample_fmts_size) {
300 for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
301 if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
302 return ret;
303 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
304 return ret;
305 }
306
307 if (
308 #if FF_API_OLD_CHANNEL_LAYOUT
309 buf->channel_layouts_size || buf->channel_counts_size ||
310 #endif
311 buf->channel_layouts_str || buf->all_channel_counts) {
312 #if FF_API_OLD_CHANNEL_LAYOUT
313 cleanup_redundant_layouts(ctx);
314 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 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 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 if (buf->all_channel_counts) {
353 if (layouts)
354 av_log(ctx, AV_LOG_WARNING,
355 "Conflicting all_channel_counts and list in options\n");
356 else if (!(layouts = ff_all_channel_counts()))
357 return AVERROR(ENOMEM);
358 }
359 if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
360 return ret;
361 }
362
363 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 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