FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 197 263 74.9%
Functions: 13 14 92.9%
Branches: 142 232 61.2%

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
50 /* video only */
51 int w, h, prev_w, prev_h;
52 enum AVPixelFormat pix_fmt, prev_pix_fmt;
53 enum AVColorSpace color_space, prev_color_space;
54 enum AVColorRange color_range, prev_color_range;
55 enum AVAlphaMode alpha_mode, prev_alpha_mode;
56 AVRational pixel_aspect;
57
58 AVBufferRef *hw_frames_ctx;
59
60 /* audio only */
61 int sample_rate;
62 enum AVSampleFormat sample_fmt;
63 int channels;
64 AVChannelLayout ch_layout;
65 AVFrameSideData **side_data;
66 int nb_side_data;
67
68 int eof;
69 int64_t last_pts;
70 int link_delta, prev_delta;
71 } BufferSourceContext;
72
73 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format, csp, range, alpha, pts)\
74 c->link_delta = c->w != width || c->h != height || c->pix_fmt != format ||\
75 c->color_space != csp || c->color_range != range || c->alpha_mode != alpha;\
76 c->prev_delta = c->prev_w != width || c->prev_h != height || c->prev_pix_fmt != format ||\
77 c->prev_color_space != csp || c->prev_color_range != range || c->prev_alpha_mode != alpha;\
78 if (c->link_delta) {\
79 int loglevel = c->prev_delta ? AV_LOG_WARNING : AV_LOG_DEBUG;\
80 av_log(s, loglevel, "Changing video frame properties on the fly is not supported by all filters.\n");\
81 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",\
82 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),\
83 width, height, format, av_color_space_name(csp), av_color_range_name(range), av_alpha_mode_name(alpha),\
84 av_ts2timestr(pts, &s->outputs[0]->time_base));\
85 }\
86 if (c->prev_delta) {\
87 if (!c->link_delta)\
88 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));\
89 c->prev_w = width;\
90 c->prev_h = height;\
91 c->prev_pix_fmt = format;\
92 c->prev_color_space = csp;\
93 c->prev_color_range = range;\
94 c->prev_alpha_mode = alpha;\
95 }
96
97 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, layout, format, pts)\
98 if (c->sample_fmt != format || c->sample_rate != srate ||\
99 av_channel_layout_compare(&c->ch_layout, &layout) || c->channels != layout.nb_channels) {\
100 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",\
101 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,\
102 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));\
103 av_log(s, AV_LOG_ERROR, "Changing audio frame properties on the fly is not supported.\n");\
104 return AVERROR(EINVAL);\
105 }
106
107 6991 AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
108 {
109 6991 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6991 times.
6991 if (!par)
111 return NULL;
112
113 6991 par->format = -1;
114 6991 par->color_range = AVCOL_RANGE_UNSPECIFIED;
115 6991 par->color_space = AVCOL_SPC_UNSPECIFIED;
116 6991 par->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
117
118 6991 return par;
119 }
120
121 6991 int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
122 {
123 6991 BufferSourceContext *s = ctx->priv;
124
125
3/4
✓ Branch 0 taken 5721 times.
✓ Branch 1 taken 1270 times.
✓ Branch 2 taken 5721 times.
✗ Branch 3 not taken.
6991 if (param->time_base.num > 0 && param->time_base.den > 0)
126 5721 s->time_base = param->time_base;
127
128
2/3
✓ Branch 0 taken 5721 times.
✓ Branch 1 taken 1270 times.
✗ Branch 2 not taken.
6991 switch (ctx->filter->outputs[0].type) {
129 5721 case AVMEDIA_TYPE_VIDEO:
130
1/2
✓ Branch 0 taken 5721 times.
✗ Branch 1 not taken.
5721 if (param->format != AV_PIX_FMT_NONE) {
131 5721 s->pix_fmt = s->prev_pix_fmt = param->format;
132 }
133
1/2
✓ Branch 0 taken 5721 times.
✗ Branch 1 not taken.
5721 if (param->width > 0)
134 5721 s->w = s->prev_w = param->width;
135
1/2
✓ Branch 0 taken 5721 times.
✗ Branch 1 not taken.
5721 if (param->height > 0)
136 5721 s->h = s->prev_h = param->height;
137
3/4
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 5030 times.
✓ Branch 2 taken 691 times.
✗ Branch 3 not taken.
5721 if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
138 691 s->pixel_aspect = param->sample_aspect_ratio;
139
3/4
✓ Branch 0 taken 5716 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5716 times.
✗ Branch 3 not taken.
5721 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
140 5716 s->frame_rate = param->frame_rate;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5721 times.
5721 if (param->hw_frames_ctx) {
142 av_buffer_unref(&s->hw_frames_ctx);
143 s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
144 if (!s->hw_frames_ctx)
145 return AVERROR(ENOMEM);
146 }
147
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 5338 times.
5721 if (param->color_space != AVCOL_SPC_UNSPECIFIED)
148 383 s->color_space = s->prev_color_space = param->color_space;
149
2/2
✓ Branch 0 taken 4407 times.
✓ Branch 1 taken 1314 times.
5721 if (param->color_range != AVCOL_RANGE_UNSPECIFIED)
150 4407 s->color_range = s->prev_color_range = param->color_range;
151
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 5643 times.
5721 if (param->alpha_mode != AVALPHA_MODE_UNSPECIFIED)
152 78 s->alpha_mode = s->prev_alpha_mode = param->alpha_mode;
153 5721 break;
154 1270 case AVMEDIA_TYPE_AUDIO:
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (param->format != AV_SAMPLE_FMT_NONE) {
156 s->sample_fmt = param->format;
157 }
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (param->sample_rate > 0)
159 s->sample_rate = param->sample_rate;
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (param->ch_layout.nb_channels) {
161 int ret = av_channel_layout_copy(&s->ch_layout, &param->ch_layout);
162 if (ret < 0)
163 return ret;
164 }
165 1270 break;
166 default:
167 return AVERROR_BUG;
168 }
169
170
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 6742 times.
6991 if (param->nb_side_data > 0)
171 249 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
172
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 6991 times.
7260 for (int i = 0; i < param->nb_side_data; i++) {
173 269 int ret = av_frame_side_data_clone(&s->side_data, &s->nb_side_data,
174 269 param->side_data[i], 0);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (ret < 0) {
176 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
177 return ret;
178 }
179 }
180
181 6991 return 0;
182 }
183
184 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
185 {
186 return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
187 AV_BUFFERSRC_FLAG_KEEP_REF);
188 }
189
190 129 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
191 {
192 129 return av_buffersrc_add_frame_flags(ctx, frame, 0);
193 }
194
195 380552 static int push_frame(AVFilterGraph *graph)
196 {
197 int ret;
198
199 while (1) {
200 2350744 ret = ff_filter_graph_run_once(graph);
201
2/2
✓ Branch 0 taken 380552 times.
✓ Branch 1 taken 1970192 times.
2350744 if (ret == AVERROR(EAGAIN))
202 380552 break;
203
3/4
✓ Branch 0 taken 2564 times.
✓ Branch 1 taken 1967628 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2564 times.
1970192 if (ret < 0 && ret != FFERROR_BUFFERSRC_EMPTY)
204 return ret;
205 }
206 380552 return 0;
207 }
208
209 377369 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
210 {
211 377369 BufferSourceContext *s = ctx->priv;
212 AVFrame *copy;
213 int refcounted, ret;
214
215 377369 s->nb_failed_requests = 0;
216
217
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 377358 times.
377369 if (!frame)
218 11 return av_buffersrc_close(ctx, s->last_pts, flags);
219
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 377357 times.
377358 if (s->eof)
220 1 return AVERROR_EOF;
221
222 377357 s->last_pts = frame->pts + frame->duration;
223
224 377357 refcounted = !!frame->buf[0];
225
226
1/2
✓ Branch 0 taken 377357 times.
✗ Branch 1 not taken.
377357 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
227
228
2/3
✓ Branch 0 taken 117051 times.
✓ Branch 1 taken 260306 times.
✗ Branch 2 not taken.
377357 switch (ctx->outputs[0]->type) {
229 117051 case AVMEDIA_TYPE_VIDEO:
230
17/32
✓ Branch 0 taken 117051 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117051 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 117051 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 117051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 117051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 117051 times.
✓ Branch 12 taken 117051 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 117051 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 117051 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 111713 times.
✓ Branch 19 taken 5338 times.
✓ Branch 20 taken 111713 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 111713 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 117051 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 37 taken 5338 times.
✓ Branch 38 taken 111713 times.
✓ Branch 39 taken 5338 times.
✗ Branch 40 not taken.
122389 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
231 frame->format, frame->colorspace,
232 frame->color_range, frame->alpha_mode, frame->pts);
233 117051 break;
234 260306 case AVMEDIA_TYPE_AUDIO:
235 /* For layouts unknown on input but known on link after negotiation. */
236
3/4
✓ Branch 0 taken 1973 times.
✓ Branch 1 taken 258333 times.
✓ Branch 2 taken 1973 times.
✗ Branch 3 not taken.
260306 if (frame->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC && frame->ch_layout.nb_channels == s->ch_layout.nb_channels) {
237 1973 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1973 times.
1973 if (ret < 0)
239 return ret;
240 }
241
4/12
✓ Branch 0 taken 260306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 260306 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 260306 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 260306 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
260306 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
242 frame->format, frame->pts);
243 260306 break;
244 default:
245 return AVERROR(EINVAL);
246 }
247
248 }
249
250
3/4
✓ Branch 0 taken 377357 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 377086 times.
✓ Branch 3 taken 271 times.
377357 if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 377086 times.
377086 if (!(copy = av_frame_alloc()))
252 return AVERROR(ENOMEM);
253 377086 av_frame_move_ref(copy, frame);
254 } else {
255 271 copy = av_frame_clone(frame);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
271 if (!copy)
257 return AVERROR(ENOMEM);
258 }
259
260
2/2
✓ Branch 0 taken 109244 times.
✓ Branch 1 taken 268113 times.
377357 if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
261 109244 copy->colorspace = ctx->outputs[0]->colorspace;
262
2/2
✓ Branch 0 taken 316395 times.
✓ Branch 1 taken 60962 times.
377357 if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
263 316395 copy->color_range = ctx->outputs[0]->color_range;
264
2/2
✓ Branch 0 taken 377202 times.
✓ Branch 1 taken 155 times.
377357 if (copy->alpha_mode == AVALPHA_MODE_UNSPECIFIED)
265 377202 copy->alpha_mode = ctx->outputs[0]->alpha_mode;
266
267 377357 ret = ff_filter_frame(ctx->outputs[0], copy);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 377357 times.
377357 if (ret < 0)
269 return ret;
270
271
2/2
✓ Branch 0 taken 377239 times.
✓ Branch 1 taken 118 times.
377357 if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
272 377239 ret = push_frame(ctx->graph);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 377239 times.
377239 if (ret < 0)
274 return ret;
275 }
276
277 377357 return 0;
278 }
279
280 3324 int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
281 {
282 3324 BufferSourceContext *s = ctx->priv;
283
284 3324 s->eof = 1;
285 3324 ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
286
2/2
✓ Branch 0 taken 3313 times.
✓ Branch 1 taken 11 times.
3324 return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
287 }
288
289 5721 static av_cold int init_video(AVFilterContext *ctx)
290 {
291 5721 BufferSourceContext *c = ctx->priv;
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5721 times.
5721 if (c->pix_fmt == AV_PIX_FMT_NONE) {
294 av_log(ctx, AV_LOG_ERROR, "Unspecified pixel format\n");
295 return AVERROR(EINVAL);
296 }
297
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5721 times.
5721 if (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
298 if (!c->hw_frames_ctx) {
299 av_log(ctx, AV_LOG_ERROR, "Setting BufferSourceContext.pix_fmt "
300 "to a HW format requires hw_frames_ctx to be non-NULL!\n");
301 return AVERROR(EINVAL);
302 }
303 }
304
2/4
✓ Branch 0 taken 5721 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5721 times.
5721 if (c->w <= 0 || c->h <= 0) {
305 av_log(ctx, AV_LOG_ERROR, "Invalid size %dx%d\n", c->w, c->h);
306 return AVERROR(EINVAL);
307 }
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5721 times.
5721 if (av_q2d(c->time_base) <= 0) {
309 av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
310 return AVERROR(EINVAL);
311 }
312
313 5721 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",
314 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
315 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
316 c->pixel_aspect.num, c->pixel_aspect.den,
317 av_color_space_name(c->color_space), av_color_range_name(c->color_range),
318 av_alpha_mode_name(c->alpha_mode));
319
320 5721 return 0;
321 }
322
323 386162 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
324 {
325 386162 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
326 }
327
328 #define OFFSET(x) offsetof(BufferSourceContext, x)
329 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
330 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
331
332 static const AVOption buffer_options[] = {
333 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
334 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
335 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
336 { "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 },
337 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
338 { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
339 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
340 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
341 { "colorspace", "select colorspace", OFFSET(color_space), AV_OPT_TYPE_INT, {.i64=AVCOL_SPC_UNSPECIFIED}, 0, AVCOL_SPC_NB-1, V, .unit = "colorspace"},
342 { "gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
343 { "bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
344 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
345 { "fcc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_FCC}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
346 { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
347 { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
348 { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
349 { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
350 { "ycgco-re", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RE}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
351 { "ycgco-ro", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
352 { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
353 { "bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
354 { "smpte2085", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE2085}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
355 { "chroma-derived-nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_NCL},INT_MIN, INT_MAX, V, .unit = "colorspace"},
356 { "chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
357 { "ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
358 { "ipt-c2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_IPT_C2}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
359 { "range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "range"},
360 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
361 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
362 { "limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
363 { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
364 { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
365 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
366 { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
367 { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
368 { "alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "alpha"},
369 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, V, .unit = "alpha"},
370 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, 0, V, .unit = "alpha"},
371 { "straight", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_STRAIGHT}, 0, 0, V, .unit = "alpha"},
372 { "premultiplied", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_PREMULTIPLIED}, 0, 0, V, .unit = "alpha"},
373 { NULL },
374 };
375
376 AVFILTER_DEFINE_CLASS(buffer);
377
378 static const AVOption abuffer_options[] = {
379 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
380 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
381 { "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 },
382 { "channel_layout", NULL, OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, .flags = A },
383 { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
384 { NULL },
385 };
386
387 AVFILTER_DEFINE_CLASS(abuffer);
388
389 1270 static av_cold int init_audio(AVFilterContext *ctx)
390 {
391 1270 BufferSourceContext *s = ctx->priv;
392 char buf[128];
393 1270 int ret = 0;
394
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
396 av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
397 return AVERROR(EINVAL);
398 }
399
400
2/2
✓ Branch 1 taken 1257 times.
✓ Branch 2 taken 13 times.
1270 if (av_channel_layout_check(&s->ch_layout)) {
401 int n;
402
403 1257 n = s->ch_layout.nb_channels;
404 1257 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1257 times.
1257 if (s->channels) {
406 if (n != s->channels) {
407 av_log(ctx, AV_LOG_ERROR,
408 "Mismatching channel count %d and layout '%s' "
409 "(%d channels)\n",
410 s->channels, buf, n);
411 return AVERROR(EINVAL);
412 }
413 }
414 1257 s->channels = n;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 } else if (!s->channels) {
416 av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
417 "channel layout specified\n");
418 return AVERROR(EINVAL);
419 } else {
420 13 s->ch_layout = FF_COUNT2LAYOUT(s->channels);
421 13 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
422 }
423
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (s->sample_rate <= 0) {
425 av_log(ctx, AV_LOG_ERROR, "Sample rate not set\n");
426 return AVERROR(EINVAL);
427 }
428
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (!s->time_base.num)
430 s->time_base = (AVRational){1, s->sample_rate};
431
432 1270 av_log(ctx, AV_LOG_VERBOSE,
433 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
434 s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
435 s->sample_rate, buf);
436
437 1270 return ret;
438 }
439
440 6991 static av_cold void uninit(AVFilterContext *ctx)
441 {
442 6991 BufferSourceContext *s = ctx->priv;
443 6991 av_buffer_unref(&s->hw_frames_ctx);
444 6991 av_channel_layout_uninit(&s->ch_layout);
445 6991 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
446 6991 }
447
448 6991 static int query_formats(const AVFilterContext *ctx,
449 AVFilterFormatsConfig **cfg_in,
450 AVFilterFormatsConfig **cfg_out)
451 {
452 6991 const BufferSourceContext *c = ctx->priv;
453 6991 AVFilterChannelLayouts *channel_layouts = NULL;
454 6991 AVFilterFormats *formats = NULL;
455 6991 AVFilterFormats *samplerates = NULL;
456 6991 AVFilterFormats *color_spaces = NULL;
457 6991 AVFilterFormats *color_ranges = NULL;
458 6991 AVFilterFormats *alpha_modes = NULL;
459 int ret;
460
461
2/3
✓ Branch 0 taken 5721 times.
✓ Branch 1 taken 1270 times.
✗ Branch 2 not taken.
6991 switch (ctx->outputs[0]->type) {
462 5721 case AVMEDIA_TYPE_VIDEO: {
463 5721 enum AVPixelFormat swfmt = c->pix_fmt;
464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5721 times.
5721 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL)
465 swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
466
2/4
✓ Branch 1 taken 5721 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5721 times.
11442 if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
467 5721 (ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, formats)) < 0)
468 return ret;
469 /* force specific colorspace/range downstream only for ordinary YUV */
470
2/2
✓ Branch 1 taken 5016 times.
✓ Branch 2 taken 705 times.
5721 if (ff_fmt_is_regular_yuv(swfmt)) {
471
2/4
✓ Branch 1 taken 5016 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5016 times.
10032 if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
472 5016 (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, color_spaces)) < 0)
473 return ret;
474
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 4942 times.
5016 if (ff_fmt_is_forced_full_range(swfmt)) {
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_JPEG)) < 0)
476 return ret;
477 } else {
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4942 times.
4942 if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
479 return ret;
480
2/2
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 4250 times.
4942 if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
481 /* allow implicitly promoting unspecified to mpeg */
482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 692 times.
692 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
483 return ret;
484 }
485 }
486
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5016 times.
5016 if ((ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, color_ranges)) < 0)
487 return ret;
488 }
489
2/2
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 5408 times.
5721 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_ALPHA) {
490
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
313 if ((ret = ff_add_format(&alpha_modes, c->alpha_mode)) < 0)
491 return ret;
492
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 70 times.
313 if (c->alpha_mode == AVALPHA_MODE_UNSPECIFIED) {
493 /* allow implicitly promoting unspecified to straight */
494
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 243 times.
243 if ((ret = ff_add_format(&alpha_modes, AVALPHA_MODE_STRAIGHT)) < 0)
495 return ret;
496 }
497
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
313 if ((ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, alpha_modes)) < 0)
498 return ret;
499 }
500 5721 break;
501 }
502 1270 case AVMEDIA_TYPE_AUDIO:
503
2/4
✓ Branch 1 taken 1270 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1270 times.
✗ Branch 4 not taken.
2540 if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
504
1/2
✓ Branch 1 taken 1270 times.
✗ Branch 2 not taken.
2540 (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, formats)) < 0 ||
505
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1270 times.
2540 (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
506 1270 (ret = ff_set_common_samplerates2(ctx, cfg_in, cfg_out, samplerates)) < 0)
507 return ret;
508
509
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1270 times.
1270 if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
510 return ret;
511
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1270 times.
1270 if ((ret = ff_set_common_channel_layouts2(ctx, cfg_in, cfg_out, channel_layouts)) < 0)
512 return ret;
513 1270 break;
514 default:
515 return AVERROR(EINVAL);
516 }
517
518 6991 return 0;
519 }
520
521 6991 static int config_props(AVFilterLink *link)
522 {
523 6991 FilterLink *l = ff_filter_link(link);
524 6991 BufferSourceContext *c = link->src->priv;
525
526
2/3
✓ Branch 0 taken 5721 times.
✓ Branch 1 taken 1270 times.
✗ Branch 2 not taken.
6991 switch (link->type) {
527 5721 case AVMEDIA_TYPE_VIDEO:
528 5721 link->w = c->w;
529 5721 link->h = c->h;
530 5721 link->sample_aspect_ratio = c->pixel_aspect;
531
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5721 times.
5721 if (c->hw_frames_ctx) {
533 l->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
534 if (!l->hw_frames_ctx)
535 return AVERROR(ENOMEM);
536 }
537 5721 break;
538 1270 case AVMEDIA_TYPE_AUDIO:
539
3/4
✓ Branch 0 taken 1270 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1257 times.
1270 if (!c->ch_layout.nb_channels || c->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
540 13 int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
542 return ret;
543 }
544 1270 break;
545 default:
546 return AVERROR(EINVAL);
547 }
548
549
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 6991 times.
7260 for (int i = 0; i < c->nb_side_data; i++) {
550 int ret;
551
552 269 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
553 269 c->side_data[i], 0);
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (ret < 0) {
555 av_frame_side_data_free(&link->side_data, &link->nb_side_data);
556 return ret;
557 }
558 }
559
560 6991 link->time_base = c->time_base;
561 6991 l->frame_rate = c->frame_rate;
562 6991 return 0;
563 }
564
565 380750 static int activate(AVFilterContext *ctx)
566 {
567 380750 AVFilterLink *outlink = ctx->outputs[0];
568 380750 BufferSourceContext *c = ctx->priv;
569
570
4/4
✓ Branch 0 taken 378051 times.
✓ Branch 1 taken 2699 times.
✓ Branch 3 taken 573 times.
✓ Branch 4 taken 377478 times.
380750 if (!c->eof && ff_outlink_get_status(outlink)) {
571 573 c->eof = 1;
572 573 return 0;
573 }
574
575
2/2
✓ Branch 0 taken 2699 times.
✓ Branch 1 taken 377478 times.
380177 if (c->eof) {
576 2699 ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
577 2699 return 0;
578 }
579 377478 c->nb_failed_requests++;
580 377478 return FFERROR_BUFFERSRC_EMPTY;
581 }
582
583 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
584 {
585 .name = "default",
586 .type = AVMEDIA_TYPE_VIDEO,
587 .config_props = config_props,
588 },
589 };
590
591 const FFFilter ff_vsrc_buffer = {
592 .p.name = "buffer",
593 .p.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
594 .p.priv_class = &buffer_class,
595 .priv_size = sizeof(BufferSourceContext),
596 .activate = activate,
597 .init = init_video,
598 .uninit = uninit,
599
600 FILTER_OUTPUTS(avfilter_vsrc_buffer_outputs),
601 FILTER_QUERY_FUNC2(query_formats),
602 };
603
604 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
605 {
606 .name = "default",
607 .type = AVMEDIA_TYPE_AUDIO,
608 .config_props = config_props,
609 },
610 };
611
612 const FFFilter ff_asrc_abuffer = {
613 .p.name = "abuffer",
614 .p.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
615 .p.priv_class = &abuffer_class,
616 .priv_size = sizeof(BufferSourceContext),
617 .activate = activate,
618 .init = init_audio,
619 .uninit = uninit,
620
621 FILTER_OUTPUTS(avfilter_asrc_abuffer_outputs),
622 FILTER_QUERY_FUNC2(query_formats),
623 };
624