FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 176 240 73.3%
Functions: 13 14 92.9%
Branches: 120 208 57.7%

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