FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2026-04-21 13:47:36
Exec Total Coverage
Lines: 206 277 74.4%
Functions: 15 16 93.8%
Branches: 146 240 60.8%

Line Branch Exec Source
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
28 #include "libavutil/channel_layout.h"
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
44 typedef struct BufferSourceContext {
45 const AVClass *class;
46 AVRational time_base; ///< time_base to set in the output link
47 AVRational frame_rate; ///< frame_rate to set in the output link
48 unsigned nb_failed_requests;
49 unsigned warning_limit;
50
51 /* video only */
52 int w, h, prev_w, prev_h;
53 enum AVPixelFormat pix_fmt, prev_pix_fmt;
54 enum AVColorSpace color_space, prev_color_space;
55 enum AVColorRange color_range, prev_color_range;
56 enum AVAlphaMode alpha_mode, prev_alpha_mode;
57 AVRational pixel_aspect;
58
59 AVBufferRef *hw_frames_ctx;
60
61 /* audio only */
62 int sample_rate;
63 enum AVSampleFormat sample_fmt;
64 int channels;
65 AVChannelLayout ch_layout;
66 AVFrameSideData **side_data;
67 int nb_side_data;
68
69 int eof;
70 int64_t last_pts;
71 int link_delta, prev_delta;
72 } BufferSourceContext;
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
108 7075 AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
109 {
110 7075 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7075 times.
7075 if (!par)
112 return NULL;
113
114 7075 par->format = -1;
115 7075 par->color_range = AVCOL_RANGE_UNSPECIFIED;
116 7075 par->color_space = AVCOL_SPC_UNSPECIFIED;
117 7075 par->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
118
119 7075 return par;
120 }
121
122 7075 int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
123 {
124 7075 BufferSourceContext *s = ctx->priv;
125
126
3/4
✓ Branch 0 taken 5766 times.
✓ Branch 1 taken 1309 times.
✓ Branch 2 taken 5766 times.
✗ Branch 3 not taken.
7075 if (param->time_base.num > 0 && param->time_base.den > 0)
127 5766 s->time_base = param->time_base;
128
129
2/3
✓ Branch 0 taken 5766 times.
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
7075 switch (ctx->filter->outputs[0].type) {
130 5766 case AVMEDIA_TYPE_VIDEO:
131
1/2
✓ Branch 0 taken 5766 times.
✗ Branch 1 not taken.
5766 if (param->format != AV_PIX_FMT_NONE) {
132 5766 s->pix_fmt = s->prev_pix_fmt = param->format;
133 }
134
1/2
✓ Branch 0 taken 5766 times.
✗ Branch 1 not taken.
5766 if (param->width > 0)
135 5766 s->w = s->prev_w = param->width;
136
1/2
✓ Branch 0 taken 5766 times.
✗ Branch 1 not taken.
5766 if (param->height > 0)
137 5766 s->h = s->prev_h = param->height;
138
3/4
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 5064 times.
✓ Branch 2 taken 702 times.
✗ Branch 3 not taken.
5766 if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
139 702 s->pixel_aspect = param->sample_aspect_ratio;
140
3/4
✓ Branch 0 taken 5761 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5761 times.
✗ Branch 3 not taken.
5766 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
141 5761 s->frame_rate = param->frame_rate;
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5766 times.
5766 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 }
148
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 5380 times.
5766 if (param->color_space != AVCOL_SPC_UNSPECIFIED)
149 386 s->color_space = s->prev_color_space = param->color_space;
150
2/2
✓ Branch 0 taken 4432 times.
✓ Branch 1 taken 1334 times.
5766 if (param->color_range != AVCOL_RANGE_UNSPECIFIED)
151 4432 s->color_range = s->prev_color_range = param->color_range;
152
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 5688 times.
5766 if (param->alpha_mode != AVALPHA_MODE_UNSPECIFIED)
153 78 s->alpha_mode = s->prev_alpha_mode = param->alpha_mode;
154 5766 break;
155 1309 case AVMEDIA_TYPE_AUDIO:
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (param->format != AV_SAMPLE_FMT_NONE) {
157 s->sample_fmt = param->format;
158 }
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (param->sample_rate > 0)
160 s->sample_rate = param->sample_rate;
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 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 1309 break;
167 default:
168 return AVERROR_BUG;
169 }
170
171
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 6830 times.
7075 if (param->nb_side_data > 0)
172 245 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
173
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 7075 times.
7327 for (int i = 0; i < param->nb_side_data; i++) {
174 252 int ret = av_frame_side_data_clone(&s->side_data, &s->nb_side_data,
175 252 param->side_data[i], 0);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 if (ret < 0) {
177 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
178 return ret;
179 }
180 }
181
182 7075 return 0;
183 }
184
185 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
186 {
187 return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
188 AV_BUFFERSRC_FLAG_KEEP_REF);
189 }
190
191 164 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
192 {
193 164 return av_buffersrc_add_frame_flags(ctx, frame, 0);
194 }
195
196 417498 static int push_frame(AVFilterGraph *graph)
197 {
198 int ret;
199
200 while (1) {
201 2607543 ret = ff_filter_graph_run_once(graph);
202
2/2
✓ Branch 0 taken 417498 times.
✓ Branch 1 taken 2190045 times.
2607543 if (ret == AVERROR(EAGAIN))
203 417498 break;
204
3/4
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 2187220 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2825 times.
2190045 if (ret < 0 && ret != FFERROR_BUFFERSRC_EMPTY)
205 return ret;
206 }
207 417498 return 0;
208 }
209
210 414300 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
211 {
212 414300 BufferSourceContext *s = ctx->priv;
213 AVFrame *copy;
214 int refcounted, ret;
215
216 414300 s->nb_failed_requests = 0;
217
218
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 414279 times.
414300 if (!frame)
219 21 return av_buffersrc_close(ctx, s->last_pts, flags);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414279 times.
414279 if (s->eof)
221 return AVERROR_EOF;
222
223 414279 s->last_pts = frame->pts + frame->duration;
224
225 414279 refcounted = !!frame->buf[0];
226
227
1/2
✓ Branch 0 taken 414279 times.
✗ Branch 1 not taken.
414279 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
228
229
2/3
✓ Branch 0 taken 117967 times.
✓ Branch 1 taken 296312 times.
✗ Branch 2 not taken.
414279 switch (ctx->outputs[0]->type) {
230 117967 case AVMEDIA_TYPE_VIDEO:
231
17/32
✓ Branch 0 taken 117967 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117967 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 117967 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 117967 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 117967 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 117967 times.
✓ Branch 12 taken 117967 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 117967 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 117967 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 112587 times.
✓ Branch 19 taken 5380 times.
✓ Branch 20 taken 112587 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 112587 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 117967 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 37 taken 5380 times.
✓ Branch 38 taken 112587 times.
✓ Branch 39 taken 5380 times.
✗ Branch 40 not taken.
123347 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 117967 break;
235 296312 case AVMEDIA_TYPE_AUDIO:
236 /* For layouts unknown on input but known on link after negotiation. */
237
3/4
✓ Branch 0 taken 35192 times.
✓ Branch 1 taken 261120 times.
✓ Branch 2 taken 35192 times.
✗ Branch 3 not taken.
296312 if (frame->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC && frame->ch_layout.nb_channels == s->ch_layout.nb_channels) {
238 35192 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35192 times.
35192 if (ret < 0)
240 return ret;
241 }
242
4/12
✓ Branch 0 taken 296312 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 296312 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 296312 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 296312 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
296312 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
243 frame->format, frame->pts);
244 296312 break;
245 default:
246 return AVERROR(EINVAL);
247 }
248
249 }
250
251
3/4
✓ Branch 0 taken 414279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414008 times.
✓ Branch 3 taken 271 times.
414279 if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 414008 times.
414008 if (!(copy = av_frame_alloc()))
253 return AVERROR(ENOMEM);
254 414008 av_frame_move_ref(copy, frame);
255 } else {
256 271 copy = av_frame_clone(frame);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
271 if (!copy)
258 return AVERROR(ENOMEM);
259 }
260
261
2/2
✓ Branch 0 taken 110096 times.
✓ Branch 1 taken 304183 times.
414279 if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
262 110096 copy->colorspace = ctx->outputs[0]->colorspace;
263
2/2
✓ Branch 0 taken 352998 times.
✓ Branch 1 taken 61281 times.
414279 if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
264 352998 copy->color_range = ctx->outputs[0]->color_range;
265
2/2
✓ Branch 0 taken 414124 times.
✓ Branch 1 taken 155 times.
414279 if (copy->alpha_mode == AVALPHA_MODE_UNSPECIFIED)
266 414124 copy->alpha_mode = ctx->outputs[0]->alpha_mode;
267
268 414279 ret = ff_filter_frame(ctx->outputs[0], copy);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414279 times.
414279 if (ret < 0)
270 return ret;
271
272
2/2
✓ Branch 0 taken 414136 times.
✓ Branch 1 taken 143 times.
414279 if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
273 414136 ret = push_frame(ctx->graph);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414136 times.
414136 if (ret < 0)
275 return ret;
276 }
277
278 414279 FilterLinkInternal *const li = ff_link_internal(ctx->outputs[0]);
279
1/2
✓ Branch 0 taken 414279 times.
✗ Branch 1 not taken.
414279 if (s->warning_limit &&
280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 414279 times.
414279 ff_framequeue_queued_frames(&li->fifo) >= s->warning_limit) {
281 av_log(s, AV_LOG_WARNING,
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 414279 return 0;
289 }
290
291 3383 int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
292 {
293 3383 BufferSourceContext *s = ctx->priv;
294
295 3383 s->eof = 1;
296 3383 ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
297
2/2
✓ Branch 0 taken 3362 times.
✓ Branch 1 taken 21 times.
3383 return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
298 }
299
300 426284 int av_buffersrc_get_status(AVFilterContext *ctx)
301 {
302 426284 BufferSourceContext *s = ctx->priv;
303
304
3/4
✓ Branch 0 taken 424833 times.
✓ Branch 1 taken 1451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 424833 times.
426284 if (!s->eof && ff_outlink_get_status(ctx->outputs[0]))
305 s->eof = 1;
306
307 426284 return s->eof ? AVERROR(EOF) : 0;
308 }
309
310 7075 static av_cold int common_init(AVFilterContext *ctx)
311 {
312 7075 BufferSourceContext *c = ctx->priv;
313
314 7075 c->warning_limit = 100;
315 7075 return 0;
316 }
317
318 5766 static av_cold int init_video(AVFilterContext *ctx)
319 {
320 5766 BufferSourceContext *c = ctx->priv;
321
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5766 times.
5766 if (c->pix_fmt == AV_PIX_FMT_NONE) {
323 av_log(ctx, AV_LOG_ERROR, "Unspecified pixel format\n");
324 return AVERROR(EINVAL);
325 }
326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5766 times.
5766 if (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
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
2/4
✓ Branch 0 taken 5766 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5766 times.
5766 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5766 times.
5766 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 5766 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 5766 return common_init(ctx);
350 }
351
352 425072 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
353 {
354 425072 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
361 static 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
405 AVFILTER_DEFINE_CLASS(buffer);
406
407 static 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
416 AVFILTER_DEFINE_CLASS(abuffer);
417
418 1309 static av_cold int init_audio(AVFilterContext *ctx)
419 {
420 1309 BufferSourceContext *s = ctx->priv;
421 char buf[128];
422
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 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
2/2
✓ Branch 1 taken 1289 times.
✓ Branch 2 taken 20 times.
1309 if (av_channel_layout_check(&s->ch_layout)) {
429 int n;
430
431 1289 n = s->ch_layout.nb_channels;
432 1289 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1289 times.
1289 if (s->channels) {
434 if (n != s->channels) {
435 av_log(ctx, AV_LOG_ERROR,
436 "Mismatching channel count %d and layout '%s' "
437 "(%d channels)\n",
438 s->channels, buf, n);
439 return AVERROR(EINVAL);
440 }
441 }
442 1289 s->channels = n;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 } 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 20 s->ch_layout = FF_COUNT2LAYOUT(s->channels);
449 20 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
450 }
451
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (s->sample_rate <= 0) {
453 av_log(ctx, AV_LOG_ERROR, "Sample rate not set\n");
454 return AVERROR(EINVAL);
455 }
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (!s->time_base.num)
458 s->time_base = (AVRational){1, s->sample_rate};
459
460 1309 av_log(ctx, AV_LOG_VERBOSE,
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 1309 return common_init(ctx);
466 }
467
468 7075 static av_cold void uninit(AVFilterContext *ctx)
469 {
470 7075 BufferSourceContext *s = ctx->priv;
471 7075 av_buffer_unref(&s->hw_frames_ctx);
472 7075 av_channel_layout_uninit(&s->ch_layout);
473 7075 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
474 7075 }
475
476 7075 static int query_formats(const AVFilterContext *ctx,
477 AVFilterFormatsConfig **cfg_in,
478 AVFilterFormatsConfig **cfg_out)
479 {
480 7075 const BufferSourceContext *c = ctx->priv;
481 7075 AVFilterChannelLayouts *channel_layouts = NULL;
482 7075 AVFilterFormats *formats = NULL;
483 7075 AVFilterFormats *samplerates = NULL;
484 7075 AVFilterFormats *color_spaces = NULL;
485 7075 AVFilterFormats *color_ranges = NULL;
486 7075 AVFilterFormats *alpha_modes = NULL;
487 int ret;
488
489
2/3
✓ Branch 0 taken 5766 times.
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
7075 switch (ctx->outputs[0]->type) {
490 5766 case AVMEDIA_TYPE_VIDEO: {
491 5766 enum AVPixelFormat swfmt = c->pix_fmt;
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5766 times.
5766 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL)
493 swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
494
2/4
✓ Branch 1 taken 5766 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5766 times.
11532 if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
495 5766 (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
2/2
✓ Branch 1 taken 5053 times.
✓ Branch 2 taken 713 times.
5766 if (ff_fmt_is_regular_yuv(swfmt)) {
499
2/4
✓ Branch 1 taken 5053 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5053 times.
10106 if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
500 5053 (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, color_spaces)) < 0)
501 return ret;
502
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 4979 times.
5053 if (ff_fmt_is_forced_full_range(swfmt)) {
503
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_JPEG)) < 0)
504 return ret;
505 } else {
506
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4979 times.
4979 if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
507 return ret;
508
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 4274 times.
4979 if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
509 /* allow implicitly promoting unspecified to mpeg */
510
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 705 times.
705 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
511 return ret;
512 }
513 }
514
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5053 times.
5053 if ((ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, color_ranges)) < 0)
515 return ret;
516 }
517
2/2
✓ Branch 1 taken 311 times.
✓ Branch 2 taken 5455 times.
5766 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_ALPHA) {
518
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
311 if ((ret = ff_add_format(&alpha_modes, c->alpha_mode)) < 0)
519 return ret;
520
2/2
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 70 times.
311 if (c->alpha_mode == AVALPHA_MODE_UNSPECIFIED) {
521 /* allow implicitly promoting unspecified to straight */
522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 241 times.
241 if ((ret = ff_add_format(&alpha_modes, AVALPHA_MODE_STRAIGHT)) < 0)
523 return ret;
524 }
525
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
311 if ((ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, alpha_modes)) < 0)
526 return ret;
527 }
528 5766 break;
529 }
530 1309 case AVMEDIA_TYPE_AUDIO:
531
2/4
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1309 times.
✗ Branch 4 not taken.
2618 if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
532
1/2
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
2618 (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, formats)) < 0 ||
533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1309 times.
2618 (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
534 1309 (ret = ff_set_common_samplerates2(ctx, cfg_in, cfg_out, samplerates)) < 0)
535 return ret;
536
537
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1309 times.
1309 if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
538 return ret;
539
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1309 times.
1309 if ((ret = ff_set_common_channel_layouts2(ctx, cfg_in, cfg_out, channel_layouts)) < 0)
540 return ret;
541 1309 break;
542 default:
543 return AVERROR(EINVAL);
544 }
545
546 7075 return 0;
547 }
548
549 7075 static int config_props(AVFilterLink *link)
550 {
551 7075 FilterLink *l = ff_filter_link(link);
552 7075 BufferSourceContext *c = link->src->priv;
553
554
2/3
✓ Branch 0 taken 5766 times.
✓ Branch 1 taken 1309 times.
✗ Branch 2 not taken.
7075 switch (link->type) {
555 5766 case AVMEDIA_TYPE_VIDEO:
556 5766 link->w = c->w;
557 5766 link->h = c->h;
558 5766 link->sample_aspect_ratio = c->pixel_aspect;
559
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5766 times.
5766 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 5766 break;
566 1309 case AVMEDIA_TYPE_AUDIO:
567
3/4
✓ Branch 0 taken 1309 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 1289 times.
1309 if (!c->ch_layout.nb_channels || c->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
568 20 int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
570 return ret;
571 }
572 1309 break;
573 default:
574 return AVERROR(EINVAL);
575 }
576
577
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 7075 times.
7327 for (int i = 0; i < c->nb_side_data; i++) {
578 int ret;
579
580 252 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
581 252 c->side_data[i], 0);
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 if (ret < 0) {
583 av_frame_side_data_free(&link->side_data, &link->nb_side_data);
584 return ret;
585 }
586 }
587
588 7075 link->time_base = c->time_base;
589 7075 l->frame_rate = c->frame_rate;
590 7075 return 0;
591 }
592
593 417971 static int activate(AVFilterContext *ctx)
594 {
595 417971 AVFilterLink *outlink = ctx->outputs[0];
596 417971 BufferSourceContext *c = ctx->priv;
597
598
4/4
✓ Branch 0 taken 415231 times.
✓ Branch 1 taken 2740 times.
✓ Branch 3 taken 593 times.
✓ Branch 4 taken 414638 times.
417971 if (!c->eof && ff_outlink_get_status(outlink)) {
599 593 c->eof = 1;
600 593 return 0;
601 }
602
603
2/2
✓ Branch 0 taken 2740 times.
✓ Branch 1 taken 414638 times.
417378 if (c->eof) {
604 2740 ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
605 2740 return 0;
606 }
607 414638 c->nb_failed_requests++;
608 414638 return FFERROR_BUFFERSRC_EMPTY;
609 }
610
611 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
612 {
613 .name = "default",
614 .type = AVMEDIA_TYPE_VIDEO,
615 .config_props = config_props,
616 },
617 };
618
619 const FFFilter ff_vsrc_buffer = {
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),
624 .activate = activate,
625 .init = init_video,
626 .uninit = uninit,
627
628 FILTER_OUTPUTS(avfilter_vsrc_buffer_outputs),
629 FILTER_QUERY_FUNC2(query_formats),
630 };
631
632 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
633 {
634 .name = "default",
635 .type = AVMEDIA_TYPE_AUDIO,
636 .config_props = config_props,
637 },
638 };
639
640 const FFFilter ff_asrc_abuffer = {
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),
645 .activate = activate,
646 .init = init_audio,
647 .uninit = uninit,
648
649 FILTER_OUTPUTS(avfilter_asrc_abuffer_outputs),
650 FILTER_QUERY_FUNC2(query_formats),
651 };
652