FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 192 255 75.3%
Functions: 13 14 92.9%
Branches: 133 218 61.0%

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