FFmpeg
Loading...
Searching...
No Matches
buffersrc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Vitor Sessak
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 * memory buffer source filter
24 */
25
26#include <float.h>
27
29#include "libavutil/frame.h"
30#include "libavutil/hwcontext.h"
31#include "libavutil/internal.h"
32#include "libavutil/mem.h"
33#include "libavutil/opt.h"
34#include "libavutil/pixdesc.h"
35#include "libavutil/samplefmt.h"
36#include "libavutil/timestamp.h"
37#include "avfilter.h"
38#include "avfilter_internal.h"
39#include "buffersrc.h"
40#include "filters.h"
41#include "formats.h"
42#include "video.h"
43
73
74#define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format, csp, range, alpha, pts)\
75 c->link_delta = c->w != width || c->h != height || c->pix_fmt != format ||\
76 c->color_space != csp || c->color_range != range || c->alpha_mode != alpha;\
77 c->prev_delta = c->prev_w != width || c->prev_h != height || c->prev_pix_fmt != format ||\
78 c->prev_color_space != csp || c->prev_color_range != range || c->prev_alpha_mode != alpha;\
79 if (c->link_delta) {\
80 int loglevel = c->prev_delta ? AV_LOG_WARNING : AV_LOG_DEBUG;\
81 av_log(s, loglevel, "Changing video frame properties on the fly is not supported by all filters.\n");\
82 av_log(s, loglevel, "filter context - w: %d h: %d fmt: %d csp: %s range: %s alpha: %s, incoming frame - w: %d h: %d fmt: %d csp: %s range: %s alpha: %s pts_time: %s\n",\
83 c->w, c->h, c->pix_fmt, av_color_space_name(c->color_space), av_color_range_name(c->color_range), av_alpha_mode_name(c->alpha_mode),\
84 width, height, format, av_color_space_name(csp), av_color_range_name(range), av_alpha_mode_name(alpha),\
85 av_ts2timestr(pts, &s->outputs[0]->time_base));\
86 }\
87 if (c->prev_delta) {\
88 if (!c->link_delta)\
89 av_log(s, AV_LOG_VERBOSE, "video frame properties congruent with link at pts_time: %s\n", av_ts2timestr(pts, &s->outputs[0]->time_base));\
90 c->prev_w = width;\
91 c->prev_h = height;\
92 c->prev_pix_fmt = format;\
93 c->prev_color_space = csp;\
94 c->prev_color_range = range;\
95 c->prev_alpha_mode = alpha;\
96 }
97
98#define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, layout, format, pts)\
99 if (c->sample_fmt != format || c->sample_rate != srate ||\
100 av_channel_layout_compare(&c->ch_layout, &layout) || c->channels != layout.nb_channels) {\
101 av_log(s, AV_LOG_INFO, "filter context - fmt: %s r: %d layout: %"PRIX64" ch: %d, incoming frame - fmt: %s r: %d layout: %"PRIX64" ch: %d pts_time: %s\n",\
102 av_get_sample_fmt_name(c->sample_fmt), c->sample_rate, c->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? c->ch_layout.u.mask : 0, c->channels,\
103 av_get_sample_fmt_name(format), srate, layout.order == AV_CHANNEL_ORDER_NATIVE ? layout.u.mask : 0, layout.nb_channels, av_ts2timestr(pts, &s->outputs[0]->time_base));\
104 av_log(s, AV_LOG_ERROR, "Changing audio frame properties on the fly is not supported.\n");\
105 return AVERROR(EINVAL);\
106 }
107
109{
110 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
111 if (!par)
112 return NULL;
113
114 par->format = -1;
118
119 return par;
120}
121
123{
124 BufferSourceContext *s = ctx->priv;
125
126 if (param->time_base.num > 0 && param->time_base.den > 0)
127 s->time_base = param->time_base;
128
129 switch (ctx->filter->outputs[0].type) {
131 if (param->format != AV_PIX_FMT_NONE) {
132 s->pix_fmt = s->prev_pix_fmt = param->format;
133 }
134 if (param->width > 0)
135 s->w = s->prev_w = param->width;
136 if (param->height > 0)
137 s->h = s->prev_h = param->height;
138 if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
139 s->pixel_aspect = param->sample_aspect_ratio;
140 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
141 s->frame_rate = param->frame_rate;
142 if (param->hw_frames_ctx) {
143 av_buffer_unref(&s->hw_frames_ctx);
144 s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
145 if (!s->hw_frames_ctx)
146 return AVERROR(ENOMEM);
147 }
149 s->color_space = s->prev_color_space = param->color_space;
151 s->color_range = s->prev_color_range = param->color_range;
153 s->alpha_mode = s->prev_alpha_mode = param->alpha_mode;
154 break;
156 if (param->format != AV_SAMPLE_FMT_NONE) {
157 s->sample_fmt = param->format;
158 }
159 if (param->sample_rate > 0)
160 s->sample_rate = param->sample_rate;
161 if (param->ch_layout.nb_channels) {
162 int ret = av_channel_layout_copy(&s->ch_layout, &param->ch_layout);
163 if (ret < 0)
164 return ret;
165 }
166 break;
167 default:
168 return AVERROR_BUG;
169 }
170
171 if (param->nb_side_data > 0)
172 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
173 for (int i = 0; i < param->nb_side_data; i++) {
174 int ret = av_frame_side_data_clone(&s->side_data, &s->nb_side_data,
175 param->side_data[i], 0);
176 if (ret < 0) {
177 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
178 return ret;
179 }
180 }
181
182 return 0;
183}
184
190
195
196static int push_frame(AVFilterGraph *graph)
197{
198 int ret;
199
200 while (1) {
201 ret = ff_filter_graph_run_once(graph);
202 if (ret == AVERROR(EAGAIN))
203 break;
204 if (ret < 0 && ret != FFERROR_BUFFERSRC_EMPTY)
205 return ret;
206 }
207 return 0;
208}
209
211{
212 BufferSourceContext *s = ctx->priv;
213 AVFrame *copy;
214 int refcounted, ret;
215
216 s->nb_failed_requests = 0;
217
218 if (!frame)
219 return av_buffersrc_close(ctx, s->last_pts, flags);
220 if (s->eof)
221 return AVERROR_EOF;
222
223 s->last_pts = frame->pts + frame->duration;
224
225 refcounted = !!frame->buf[0];
226
228
229 switch (ctx->outputs[0]->type) {
231 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
232 frame->format, frame->colorspace,
233 frame->color_range, frame->alpha_mode, frame->pts);
234 break;
236 /* For layouts unknown on input but known on link after negotiation. */
237 if (frame->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC && frame->ch_layout.nb_channels == s->ch_layout.nb_channels) {
238 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
239 if (ret < 0)
240 return ret;
241 }
242 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
243 frame->format, frame->pts);
244 break;
245 default:
246 return AVERROR(EINVAL);
247 }
248
249 }
250
251 if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
252 if (!(copy = av_frame_alloc()))
253 return AVERROR(ENOMEM);
255 } else {
257 if (!copy)
258 return AVERROR(ENOMEM);
259 }
260
261 if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
262 copy->colorspace = ctx->outputs[0]->colorspace;
263 if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
264 copy->color_range = ctx->outputs[0]->color_range;
265 if (copy->alpha_mode == AVALPHA_MODE_UNSPECIFIED)
266 copy->alpha_mode = ctx->outputs[0]->alpha_mode;
267
268 ret = ff_filter_frame(ctx->outputs[0], copy);
269 if (ret < 0)
270 return ret;
271
273 ret = push_frame(ctx->graph);
274 if (ret < 0)
275 return ret;
276 }
277
278 FilterLinkInternal *const li = ff_link_internal(ctx->outputs[0]);
279 if (s->warning_limit &&
280 ff_framequeue_queued_frames(&li->fifo) >= s->warning_limit) {
282 "%d buffers queued in %s, something may be wrong.\n",
283 s->warning_limit,
284 (char *)av_x_if_null(ctx->name, ctx->filter->name));
285 s->warning_limit *= 10;
286 }
287
288 return 0;
289}
290
292{
293 BufferSourceContext *s = ctx->priv;
294
295 s->eof = 1;
297 return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
298}
299
301{
302 BufferSourceContext *s = ctx->priv;
303
304 if (!s->eof && ff_outlink_get_status(ctx->outputs[0]))
305 s->eof = 1;
306
307 return s->eof ? AVERROR(EOF) : 0;
308}
309
311{
312 BufferSourceContext *c = ctx->priv;
313
314 c->warning_limit = 100;
315 return 0;
316}
317
319{
320 BufferSourceContext *c = ctx->priv;
321
322 if (c->pix_fmt == AV_PIX_FMT_NONE) {
323 av_log(ctx, AV_LOG_ERROR, "Unspecified pixel format\n");
324 return AVERROR(EINVAL);
325 }
327 if (!c->hw_frames_ctx) {
328 av_log(ctx, AV_LOG_ERROR, "Setting BufferSourceContext.pix_fmt "
329 "to a HW format requires hw_frames_ctx to be non-NULL!\n");
330 return AVERROR(EINVAL);
331 }
332 }
333 if (c->w <= 0 || c->h <= 0) {
334 av_log(ctx, AV_LOG_ERROR, "Invalid size %dx%d\n", c->w, c->h);
335 return AVERROR(EINVAL);
336 }
337 if (av_q2d(c->time_base) <= 0) {
338 av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
339 return AVERROR(EINVAL);
340 }
341
342 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d csp:%s range:%s alpha:%s\n",
343 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
344 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
345 c->pixel_aspect.num, c->pixel_aspect.den,
346 av_color_space_name(c->color_space), av_color_range_name(c->color_range),
347 av_alpha_mode_name(c->alpha_mode));
348
349 return common_init(ctx);
350}
351
353{
354 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
355}
356
357#define OFFSET(x) offsetof(BufferSourceContext, x)
358#define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
359#define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
360
361static const AVOption buffer_options[] = {
362 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
363 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
364 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
365 { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, .min = AV_PIX_FMT_NONE, .max = INT_MAX, .flags = V },
366 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
367 { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
368 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
369 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
370 { "colorspace", "select colorspace", OFFSET(color_space), AV_OPT_TYPE_INT, {.i64=AVCOL_SPC_UNSPECIFIED}, 0, AVCOL_SPC_NB-1, V, .unit = "colorspace"},
371 { "gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
372 { "bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
373 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
374 { "fcc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_FCC}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
375 { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
376 { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
377 { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
378 { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
379 { "ycgco-re", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RE}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
380 { "ycgco-ro", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
381 { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
382 { "bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
383 { "smpte2085", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE2085}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
384 { "chroma-derived-nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_NCL},INT_MIN, INT_MAX, V, .unit = "colorspace"},
385 { "chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
386 { "ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
387 { "ipt-c2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_IPT_C2}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
388 { "range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "range"},
389 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
390 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
391 { "limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
392 { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
393 { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
394 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
395 { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
396 { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
397 { "alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "alpha"},
398 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, V, .unit = "alpha"},
399 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, V, .unit = "alpha"},
400 { "straight", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_STRAIGHT}, 0, 0, V, .unit = "alpha"},
401 { "premultiplied", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_PREMULTIPLIED}, 0, 0, V, .unit = "alpha"},
402 { NULL },
403};
404
406
407static const AVOption abuffer_options[] = {
408 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
409 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
410 { "sample_fmt", NULL, OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_NONE }, .min = AV_SAMPLE_FMT_NONE, .max = INT_MAX, .flags = A },
411 { "channel_layout", NULL, OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, .flags = A },
412 { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
413 { NULL },
414};
415
417
419{
420 BufferSourceContext *s = ctx->priv;
421 char buf[128];
422
423 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
424 av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
425 return AVERROR(EINVAL);
426 }
427
428 if (av_channel_layout_check(&s->ch_layout)) {
429 int n;
430
431 n = s->ch_layout.nb_channels;
432 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
433 if (s->channels) {
434 if (n != s->channels) {
436 "Mismatching channel count %d and layout '%s' "
437 "(%d channels)\n",
438 s->channels, buf, n);
439 return AVERROR(EINVAL);
440 }
441 }
442 s->channels = n;
443 } else if (!s->channels) {
444 av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
445 "channel layout specified\n");
446 return AVERROR(EINVAL);
447 } else {
448 s->ch_layout = FF_COUNT2LAYOUT(s->channels);
449 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
450 }
451
452 if (s->sample_rate <= 0) {
453 av_log(ctx, AV_LOG_ERROR, "Sample rate not set\n");
454 return AVERROR(EINVAL);
455 }
456
457 if (!s->time_base.num)
458 s->time_base = (AVRational){1, s->sample_rate};
459
461 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
462 s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
463 s->sample_rate, buf);
464
465 return common_init(ctx);
466}
467
469{
470 BufferSourceContext *s = ctx->priv;
471 av_buffer_unref(&s->hw_frames_ctx);
472 av_channel_layout_uninit(&s->ch_layout);
473 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
474}
475
477 AVFilterFormatsConfig **cfg_in,
478 AVFilterFormatsConfig **cfg_out)
479{
480 const BufferSourceContext *c = ctx->priv;
483 AVFilterFormats *samplerates = NULL;
484 AVFilterFormats *color_spaces = NULL;
485 AVFilterFormats *color_ranges = NULL;
486 AVFilterFormats *alpha_modes = NULL;
487 int ret;
488
489 switch (ctx->outputs[0]->type) {
490 case AVMEDIA_TYPE_VIDEO: {
491 enum AVPixelFormat swfmt = c->pix_fmt;
493 swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
494 if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
495 (ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, formats)) < 0)
496 return ret;
497 /* force specific colorspace/range downstream only for ordinary YUV */
498 if (ff_fmt_is_regular_yuv(swfmt)) {
499 if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
500 (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, color_spaces)) < 0)
501 return ret;
502 if (ff_fmt_is_forced_full_range(swfmt)) {
503 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_JPEG)) < 0)
504 return ret;
505 } else {
506 if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
507 return ret;
508 if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
509 /* allow implicitly promoting unspecified to mpeg */
510 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
511 return ret;
512 }
513 }
514 if ((ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, color_ranges)) < 0)
515 return ret;
516 }
518 if ((ret = ff_add_format(&alpha_modes, c->alpha_mode)) < 0)
519 return ret;
520 if (c->alpha_mode == AVALPHA_MODE_UNSPECIFIED) {
521 /* allow implicitly promoting unspecified to straight */
522 if ((ret = ff_add_format(&alpha_modes, AVALPHA_MODE_STRAIGHT)) < 0)
523 return ret;
524 }
525 if ((ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, alpha_modes)) < 0)
526 return ret;
527 }
528 break;
529 }
531 if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
532 (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, formats)) < 0 ||
533 (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
534 (ret = ff_set_common_samplerates2(ctx, cfg_in, cfg_out, samplerates)) < 0)
535 return ret;
536
537 if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
538 return ret;
539 if ((ret = ff_set_common_channel_layouts2(ctx, cfg_in, cfg_out, channel_layouts)) < 0)
540 return ret;
541 break;
542 default:
543 return AVERROR(EINVAL);
544 }
545
546 return 0;
547}
548
549static int config_props(AVFilterLink *link)
550{
551 FilterLink *l = ff_filter_link(link);
552 BufferSourceContext *c = link->src->priv;
553
554 switch (link->type) {
556 link->w = c->w;
557 link->h = c->h;
558 link->sample_aspect_ratio = c->pixel_aspect;
559
560 if (c->hw_frames_ctx) {
561 l->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
562 if (!l->hw_frames_ctx)
563 return AVERROR(ENOMEM);
564 }
565 break;
567 if (!c->ch_layout.nb_channels || c->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
568 int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
569 if (ret < 0)
570 return ret;
571 }
572 break;
573 default:
574 return AVERROR(EINVAL);
575 }
576
577 for (int i = 0; i < c->nb_side_data; i++) {
578 int ret;
579
581 c->side_data[i], 0);
582 if (ret < 0) {
584 return ret;
585 }
586 }
587
588 link->time_base = c->time_base;
589 l->frame_rate = c->frame_rate;
590 return 0;
591}
592
594{
595 AVFilterLink *outlink = ctx->outputs[0];
596 BufferSourceContext *c = ctx->priv;
597
598 if (!c->eof && ff_outlink_get_status(outlink)) {
599 c->eof = 1;
600 return 0;
601 }
602
603 if (c->eof) {
604 ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
605 return 0;
606 }
607 c->nb_failed_requests++;
609}
610
612 {
613 .name = "default",
614 .type = AVMEDIA_TYPE_VIDEO,
615 .config_props = config_props,
616 },
617};
618
620 .p.name = "buffer",
621 .p.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
622 .p.priv_class = &buffer_class,
623 .priv_size = sizeof(BufferSourceContext),
625 .init = init_video,
626 .uninit = uninit,
627
630};
631
633 {
634 .name = "default",
635 .type = AVMEDIA_TYPE_AUDIO,
636 .config_props = config_props,
637 },
638};
639
641 .p.name = "abuffer",
642 .p.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
643 .p.priv_class = &abuffer_class,
644 .priv_size = sizeof(BufferSourceContext),
646 .init = init_audio,
647 .uninit = uninit,
648
651};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vsrc_buffer
Definition buffersrc.c:619
const FFFilter ff_asrc_abuffer
Definition buffersrc.c:640
channels
Definition aptx.h:31
#define A(x)
Definition vpx_arith.h:28
#define V
Definition avdct.c:32
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition avfilter.c:1648
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition avfilter.c:250
Main libavfilter public API header.
int ff_filter_graph_run_once(AVFilterGraph *graph)
Run one round of processing on a filter graph.
int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
Returns true if a YUV pixel format is forced full range (i.e.
int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
Returns true if a pixel format is "regular YUV", which includes all pixel formats that are affected b...
static BitStreamFilterLinkInternal * ff_link_internal(AVBitStreamFilterLink *link)
static int init_audio(AVFilterContext *ctx)
Definition buffersink.c:176
static int init_video(AVFilterContext *ctx)
Definition buffersink.c:164
static const AVFilterPad avfilter_asrc_abuffer_outputs[]
Definition buffersrc.c:632
static int push_frame(AVFilterGraph *graph)
Definition buffersrc.c:196
#define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format, csp, range, alpha, pts)
Definition buffersrc.c:74
static av_cold int common_init(AVFilterContext *ctx)
Definition buffersrc.c:310
static const AVFilterPad avfilter_vsrc_buffer_outputs[]
Definition buffersrc.c:611
#define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, layout, format, pts)
Definition buffersrc.c:98
static av_cold int init_audio(AVFilterContext *ctx)
Definition buffersrc.c:418
static av_cold int init_video(AVFilterContext *ctx)
Definition buffersrc.c:318
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition buffersrc.c:476
static int activate(AVFilterContext *ctx)
Definition buffersrc.c:593
static av_cold void uninit(AVFilterContext *ctx)
Definition buffersrc.c:468
#define OFFSET(x)
Definition buffersrc.c:357
static const AVOption abuffer_options[]
Definition buffersrc.c:407
static int config_props(AVFilterLink *link)
Definition buffersrc.c:549
Memory buffer source API.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const uint16_t channel_layouts[7]
Definition dca_lbr.c:112
static enum AVPixelFormat pix_fmt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_set_common_samplerates2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *samplerates)
Definition formats.c:1041
int ff_set_common_color_ranges2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_ranges)
Definition formats.c:1089
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:571
int ff_set_common_channel_layouts2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats2() which set all free audio links to the same list of channel layouts/sampl...
Definition formats.c:1017
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition formats.c:588
int ff_set_common_color_spaces2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_spaces)
Definition formats.c:1065
int ff_set_common_alpha_modes2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *alpha_modes)
Definition formats.c:1113
#define FF_COUNT2LAYOUT(c)
Encode a channel count as a channel layout.
Definition formats.h:102
reference-counted frame API
static size_t ff_framequeue_queued_frames(const FFFrameQueue *fq)
Get the number of queued frames.
Definition framequeue.h:158
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition opt.h:306
@ AV_OPT_TYPE_SAMPLE_FMT
Underlying C type is enum AVSampleFormat.
Definition opt.h:310
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
int av_buffersrc_get_status(AVFilterContext *ctx)
Returns 0 or a negative AVERROR code.
Definition buffersrc.c:300
int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
Initialize the buffersrc or abuffersrc filter with the provided parameters.
Definition buffersrc.c:122
int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Add a frame to the buffer source.
Definition buffersrc.c:210
int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
Add a frame to the buffer source.
Definition buffersrc.c:185
int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
Close the buffer source after EOF.
Definition buffersrc.c:291
int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
Add a frame to the buffer source.
Definition buffersrc.c:191
unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
Get the number of failed requests.
Definition buffersrc.c:352
AVBufferSrcParameters * av_buffersrc_parameters_alloc(void)
Allocate a new AVBufferSrcParameters instance.
Definition buffersrc.c:108
@ AV_BUFFERSRC_FLAG_KEEP_REF
Keep a reference to the frame.
Definition buffersrc.h:53
@ AV_BUFFERSRC_FLAG_PUSH
Immediately push the frame to the output.
Definition buffersrc.h:46
@ AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT
Do not check for format changes.
Definition buffersrc.h:41
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
Free all side data entries and their contents, then zeroes out the values which the pointers are poin...
Definition side_data.c:139
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:523
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags)
Add a new side data entry to an array based on existing side data, taking a reference towards the con...
Definition side_data.c:254
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition avutil.h:311
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition samplefmt.c:51
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FFERROR_BUFFERSRC_EMPTY
Definition filters.h:35
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define attribute_align_arg
Definition internal.h:50
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
AVOptions.
const char * av_color_space_name(enum AVColorSpace space)
Definition pixdesc.c:3860
const char * av_color_range_name(enum AVColorRange range)
Definition pixdesc.c:3776
const char * av_alpha_mode_name(enum AVAlphaMode mode)
Definition pixdesc.c:3925
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
AVColorRange
Visual content value range.
Definition pixfmt.h:748
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVCOL_RANGE_NB
Not part of ABI.
Definition pixfmt.h:784
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:816
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition pixfmt.h:819
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition pixfmt.h:817
@ AVALPHA_MODE_PREMULTIPLIED
Alpha channel is multiplied into color values.
Definition pixfmt.h:818
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_YCGCO_RE
YCgCo-R, even addition of bits.
Definition pixfmt.h:724
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:708
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:712
@ AVCOL_SPC_CHROMA_DERIVED_CL
Chromaticity-derived constant luminance system.
Definition pixfmt.h:721
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition pixfmt.h:718
@ AVCOL_SPC_NB
Not part of ABI.
Definition pixfmt.h:726
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:717
@ AVCOL_SPC_SMPTE2085
SMPTE 2085, Y'D'zD'x.
Definition pixfmt.h:719
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:713
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition pixfmt.h:711
@ AVCOL_SPC_CHROMA_DERIVED_NCL
Chromaticity-derived non-constant luminance system.
Definition pixfmt.h:720
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition pixfmt.h:714
@ AVCOL_SPC_YCGCO_RO
YCgCo-R, odd addition of bits.
Definition pixfmt.h:725
@ AVCOL_SPC_YCGCO
used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
Definition pixfmt.h:715
@ AVCOL_SPC_ICTCP
ITU-R BT.2100-0, ICtCp.
Definition pixfmt.h:722
@ AVCOL_SPC_IPT_C2
SMPTE ST 2128, IPT-C2.
Definition pixfmt.h:723
formats
Definition signature.h:47
static const AVOption buffer_options[]
Definition source.c:168
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
A reference to a data buffer.
Definition buffer.h:82
This structure contains the parameters describing the frames that will be passed to this filter.
Definition buffersrc.h:73
AVRational frame_rate
Video only, the frame rate of the input video.
Definition buffersrc.h:100
int sample_rate
Audio only, the audio sampling rate in samples per second.
Definition buffersrc.h:111
AVFrameSideData ** side_data
Definition buffersrc.h:124
enum AVColorRange color_range
Definition buffersrc.h:122
AVChannelLayout ch_layout
Audio only, the audio channel layout.
Definition buffersrc.h:116
int format
video: the pixel format, value corresponds to enum AVPixelFormat audio: the sample format,...
Definition buffersrc.h:78
int width
Video only, the display dimensions of the input frames.
Definition buffersrc.h:87
enum AVColorSpace color_space
Video only, the YUV colorspace and range.
Definition buffersrc.h:121
enum AVAlphaMode alpha_mode
Video only, the alpha mode.
Definition buffersrc.h:130
AVRational time_base
The timebase to be used for the timestamps on the input frames.
Definition buffersrc.h:82
AVBufferRef * hw_frames_ctx
Video with a hwaccel pixel format only.
Definition buffersrc.h:106
AVRational sample_aspect_ratio
Video only, the sample (pixel) aspect ratio.
Definition buffersrc.h:92
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
A list of supported channel layouts.
Definition formats.h:85
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
Structure to hold side data for an AVFrame.
Definition frame.h:327
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
AVOption.
Definition opt.h:428
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
unsigned warning_limit
Definition buffersrc.c:49
AVFrameSideData ** side_data
Definition buffersrc.c:66
AVRational pixel_aspect
Definition buffersrc.c:57
enum AVPixelFormat pix_fmt prev_pix_fmt
Definition buffersrc.c:53
enum AVColorRange color_range prev_color_range
Definition buffersrc.c:55
enum AVColorSpace color_space prev_color_space
Definition buffersrc.c:54
AVRational time_base
time_base to set in the output link
Definition buffersrc.c:46
unsigned nb_failed_requests
Definition buffersrc.c:48
AVBufferRef * hw_frames_ctx
Definition buffersrc.c:59
enum AVSampleFormat sample_fmt
Definition buffersrc.c:63
enum AVAlphaMode alpha_mode prev_alpha_mode
Definition buffersrc.c:56
AVChannelLayout ch_layout
Definition buffersrc.c:65
AVRational frame_rate
frame_rate to set in the output link
Definition buffersrc.c:47
FFFrameQueue fifo
Queue of frames waiting to be filtered.
#define av_mallocz(s)
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
timestamp utils, mostly useful for debugging/logging purposes
static int64_t pts
color_range
static void copy(const float *p1, float *p2, const int length)
static double c[64]