FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 186 249 74.7%
Functions: 13 14 92.9%
Branches: 127 212 59.9%

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 6864 AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
106 {
107 6864 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6864 times.
6864 if (!par)
109 return NULL;
110
111 6864 par->format = -1;
112 6864 par->color_range = AVCOL_RANGE_UNSPECIFIED;
113 6864 par->color_space = AVCOL_SPC_UNSPECIFIED;
114
115 6864 return par;
116 }
117
118 6864 int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
119 {
120 6864 BufferSourceContext *s = ctx->priv;
121
122
3/4
✓ Branch 0 taken 5602 times.
✓ Branch 1 taken 1262 times.
✓ Branch 2 taken 5602 times.
✗ Branch 3 not taken.
6864 if (param->time_base.num > 0 && param->time_base.den > 0)
123 5602 s->time_base = param->time_base;
124
125
2/3
✓ Branch 0 taken 5602 times.
✓ Branch 1 taken 1262 times.
✗ Branch 2 not taken.
6864 switch (ctx->filter->outputs[0].type) {
126 5602 case AVMEDIA_TYPE_VIDEO:
127
1/2
✓ Branch 0 taken 5602 times.
✗ Branch 1 not taken.
5602 if (param->format != AV_PIX_FMT_NONE) {
128 5602 s->pix_fmt = s->prev_pix_fmt = param->format;
129 }
130
1/2
✓ Branch 0 taken 5602 times.
✗ Branch 1 not taken.
5602 if (param->width > 0)
131 5602 s->w = s->prev_w = param->width;
132
1/2
✓ Branch 0 taken 5602 times.
✗ Branch 1 not taken.
5602 if (param->height > 0)
133 5602 s->h = s->prev_h = param->height;
134
3/4
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 4923 times.
✓ Branch 2 taken 679 times.
✗ Branch 3 not taken.
5602 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 5597 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5597 times.
✗ Branch 3 not taken.
5602 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
137 5597 s->frame_rate = param->frame_rate;
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 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 5233 times.
5602 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 4294 times.
✓ Branch 1 taken 1308 times.
5602 if (param->color_range != AVCOL_RANGE_UNSPECIFIED)
147 4294 s->color_range = s->prev_color_range = param->color_range;
148 5602 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 6636 times.
6864 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 6864 times.
7097 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 6864 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 259 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
186 {
187 259 return av_buffersrc_add_frame_flags(ctx, frame, 0);
188 }
189
190 366826 static int push_frame(AVFilterGraph *graph)
191 {
192 int ret;
193
194 while (1) {
195 2254564 ret = ff_filter_graph_run_once(graph);
196
2/2
✓ Branch 0 taken 366826 times.
✓ Branch 1 taken 1887738 times.
2254564 if (ret == AVERROR(EAGAIN))
197 366826 break;
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1887738 times.
1887738 if (ret < 0)
199 return ret;
200 }
201 366826 return 0;
202 }
203
204 363831 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
205 {
206 363831 BufferSourceContext *s = ctx->priv;
207 AVFrame *copy;
208 int refcounted, ret;
209
210 363831 s->nb_failed_requests = 0;
211
212
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 363819 times.
363831 if (!frame)
213 12 return av_buffersrc_close(ctx, s->last_pts, flags);
214
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 363818 times.
363819 if (s->eof)
215 1 return AVERROR_EOF;
216
217 363818 s->last_pts = frame->pts + frame->duration;
218
219 363818 refcounted = !!frame->buf[0];
220
221
1/2
✓ Branch 0 taken 363818 times.
✗ Branch 1 not taken.
363818 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
222
223
2/3
✓ Branch 0 taken 111997 times.
✓ Branch 1 taken 251821 times.
✗ Branch 2 not taken.
363818 switch (ctx->outputs[0]->type) {
224 111997 case AVMEDIA_TYPE_VIDEO:
225
15/28
✓ Branch 0 taken 111997 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111997 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 111997 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 111997 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 111997 times.
✓ Branch 10 taken 111997 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 111997 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 111997 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 106764 times.
✓ Branch 17 taken 5233 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 106764 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 111997 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 31 taken 5233 times.
✓ Branch 32 taken 106764 times.
✓ Branch 33 taken 5233 times.
✗ Branch 34 not taken.
117230 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
226 frame->format, frame->colorspace,
227 frame->color_range, frame->pts);
228 111997 break;
229 251821 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 249851 times.
✓ Branch 2 taken 1970 times.
✗ Branch 3 not taken.
251821 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 251821 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 251821 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 251821 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 251821 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
251821 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
237 frame->format, frame->pts);
238 251821 break;
239 default:
240 return AVERROR(EINVAL);
241 }
242
243 }
244
245
3/4
✓ Branch 0 taken 363818 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 363547 times.
✓ Branch 3 taken 271 times.
363818 if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 363547 times.
363547 if (!(copy = av_frame_alloc()))
247 return AVERROR(ENOMEM);
248 363547 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
2/2
✓ Branch 0 taken 105927 times.
✓ Branch 1 taken 257891 times.
363818 if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
256 105927 copy->colorspace = ctx->outputs[0]->colorspace;
257
2/2
✓ Branch 0 taken 307764 times.
✓ Branch 1 taken 56054 times.
363818 if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
258 307764 copy->color_range = ctx->outputs[0]->color_range;
259
260 363818 ret = ff_filter_frame(ctx->outputs[0], copy);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363818 times.
363818 if (ret < 0)
262 return ret;
263
264
2/2
✓ Branch 0 taken 363571 times.
✓ Branch 1 taken 247 times.
363818 if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
265 363571 ret = push_frame(ctx->graph);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363571 times.
363571 if (ret < 0)
267 return ret;
268 }
269
270 363818 return 0;
271 }
272
273 3267 int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
274 {
275 3267 BufferSourceContext *s = ctx->priv;
276
277 3267 s->eof = 1;
278 3267 ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
279
2/2
✓ Branch 0 taken 3255 times.
✓ Branch 1 taken 12 times.
3267 return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
280 }
281
282 5602 static av_cold int init_video(AVFilterContext *ctx)
283 {
284 5602 BufferSourceContext *c = ctx->priv;
285
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 if (c->pix_fmt == AV_PIX_FMT_NONE) {
287 av_log(ctx, AV_LOG_ERROR, "Unspecified pixel format\n");
288 return AVERROR(EINVAL);
289 }
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5602 times.
5602 if (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
291 if (!c->hw_frames_ctx) {
292 av_log(ctx, AV_LOG_ERROR, "Setting BufferSourceContext.pix_fmt "
293 "to a HW format requires hw_frames_ctx to be non-NULL!\n");
294 return AVERROR(EINVAL);
295 }
296 }
297
2/4
✓ Branch 0 taken 5602 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5602 times.
5602 if (c->w <= 0 || c->h <= 0) {
298 av_log(ctx, AV_LOG_ERROR, "Invalid size %dx%d\n", c->w, c->h);
299 return AVERROR(EINVAL);
300 }
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5602 times.
5602 if (av_q2d(c->time_base) <= 0) {
302 av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
303 return AVERROR(EINVAL);
304 }
305
306 5602 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",
307 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
308 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
309 c->pixel_aspect.num, c->pixel_aspect.den,
310 av_color_space_name(c->color_space), av_color_range_name(c->color_range));
311
312 5602 return 0;
313 }
314
315 372242 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
316 {
317 372242 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
318 }
319
320 #define OFFSET(x) offsetof(BufferSourceContext, x)
321 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
322 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
323
324 static const AVOption buffer_options[] = {
325 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
326 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
327 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
328 { "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 },
329 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
330 { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
331 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
332 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
333 { "colorspace", "select colorspace", OFFSET(color_space), AV_OPT_TYPE_INT, {.i64=AVCOL_SPC_UNSPECIFIED}, 0, AVCOL_SPC_NB-1, V, .unit = "colorspace"},
334 { "gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
335 { "bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
336 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
337 { "fcc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_FCC}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
338 { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
339 { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
340 { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
341 { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
342 { "ycgco-re", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RE}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
343 { "ycgco-ro", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
344 { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
345 { "bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
346 { "smpte2085", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE2085}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
347 { "chroma-derived-nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_NCL},INT_MIN, INT_MAX, V, .unit = "colorspace"},
348 { "chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
349 { "ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
350 { "ipt-c2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_IPT_C2}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
351 { "range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "range"},
352 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
353 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
354 { "limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
355 { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
356 { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
357 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
358 { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
359 { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
360 { NULL },
361 };
362
363 AVFILTER_DEFINE_CLASS(buffer);
364
365 static const AVOption abuffer_options[] = {
366 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
367 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
368 { "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 },
369 { "channel_layout", NULL, OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, .flags = A },
370 { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
371 { NULL },
372 };
373
374 AVFILTER_DEFINE_CLASS(abuffer);
375
376 1262 static av_cold int init_audio(AVFilterContext *ctx)
377 {
378 1262 BufferSourceContext *s = ctx->priv;
379 char buf[128];
380 1262 int ret = 0;
381
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1262 times.
1262 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
383 av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
384 return AVERROR(EINVAL);
385 }
386
387
2/2
✓ Branch 1 taken 1249 times.
✓ Branch 2 taken 13 times.
1262 if (av_channel_layout_check(&s->ch_layout)) {
388 int n;
389
390 1249 n = s->ch_layout.nb_channels;
391 1249 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1249 times.
1249 if (s->channels) {
393 if (n != s->channels) {
394 av_log(ctx, AV_LOG_ERROR,
395 "Mismatching channel count %d and layout '%s' "
396 "(%d channels)\n",
397 s->channels, buf, n);
398 return AVERROR(EINVAL);
399 }
400 }
401 1249 s->channels = n;
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 } else if (!s->channels) {
403 av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
404 "channel layout specified\n");
405 return AVERROR(EINVAL);
406 } else {
407 13 s->ch_layout = FF_COUNT2LAYOUT(s->channels);
408 13 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
409 }
410
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1262 times.
1262 if (s->sample_rate <= 0) {
412 av_log(ctx, AV_LOG_ERROR, "Sample rate not set\n");
413 return AVERROR(EINVAL);
414 }
415
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1262 times.
1262 if (!s->time_base.num)
417 s->time_base = (AVRational){1, s->sample_rate};
418
419 1262 av_log(ctx, AV_LOG_VERBOSE,
420 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
421 s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt),
422 s->sample_rate, buf);
423
424 1262 return ret;
425 }
426
427 6864 static av_cold void uninit(AVFilterContext *ctx)
428 {
429 6864 BufferSourceContext *s = ctx->priv;
430 6864 av_buffer_unref(&s->hw_frames_ctx);
431 6864 av_channel_layout_uninit(&s->ch_layout);
432 6864 av_frame_side_data_free(&s->side_data, &s->nb_side_data);
433 6864 }
434
435 6864 static int query_formats(const AVFilterContext *ctx,
436 AVFilterFormatsConfig **cfg_in,
437 AVFilterFormatsConfig **cfg_out)
438 {
439 6864 const BufferSourceContext *c = ctx->priv;
440 6864 AVFilterChannelLayouts *channel_layouts = NULL;
441 6864 AVFilterFormats *formats = NULL;
442 6864 AVFilterFormats *samplerates = NULL;
443 6864 AVFilterFormats *color_spaces = NULL;
444 6864 AVFilterFormats *color_ranges = NULL;
445 int ret;
446
447
2/3
✓ Branch 0 taken 5602 times.
✓ Branch 1 taken 1262 times.
✗ Branch 2 not taken.
6864 switch (ctx->outputs[0]->type) {
448 5602 case AVMEDIA_TYPE_VIDEO: {
449 5602 enum AVPixelFormat swfmt = c->pix_fmt;
450
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5602 times.
5602 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL)
451 swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
452
2/4
✓ Branch 1 taken 5602 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5602 times.
11204 if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
453 5602 (ret = ff_set_common_formats2(ctx, cfg_in, cfg_out, formats)) < 0)
454 return ret;
455 /* force specific colorspace/range downstream only for ordinary YUV */
456
2/2
✓ Branch 1 taken 4900 times.
✓ Branch 2 taken 702 times.
5602 if (ff_fmt_is_regular_yuv(swfmt)) {
457
2/4
✓ Branch 1 taken 4900 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4900 times.
9800 if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
458 4900 (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, color_spaces)) < 0)
459 return ret;
460
2/2
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 4831 times.
4900 if (ff_fmt_is_forced_full_range(swfmt)) {
461
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
69 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_JPEG)) < 0)
462 return ret;
463 } else {
464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4831 times.
4831 if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
465 return ret;
466
2/2
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 4145 times.
4831 if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
467 /* allow implicitly promoting unspecified to mpeg */
468
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 686 times.
686 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
469 return ret;
470 }
471 }
472
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4900 times.
4900 if ((ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, color_ranges)) < 0)
473 return ret;
474 }
475 5602 break;
476 }
477 1262 case AVMEDIA_TYPE_AUDIO:
478
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 ||
479
1/2
✓ Branch 1 taken 1262 times.
✗ Branch 2 not taken.
2524 (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, formats)) < 0 ||
480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1262 times.
2524 (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
481 1262 (ret = ff_set_common_samplerates2(ctx, cfg_in, cfg_out, samplerates)) < 0)
482 return ret;
483
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1262 times.
1262 if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
485 return ret;
486
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)
487 return ret;
488 1262 break;
489 default:
490 return AVERROR(EINVAL);
491 }
492
493 6864 return 0;
494 }
495
496 6864 static int config_props(AVFilterLink *link)
497 {
498 6864 FilterLink *l = ff_filter_link(link);
499 6864 BufferSourceContext *c = link->src->priv;
500
501
2/3
✓ Branch 0 taken 5602 times.
✓ Branch 1 taken 1262 times.
✗ Branch 2 not taken.
6864 switch (link->type) {
502 5602 case AVMEDIA_TYPE_VIDEO:
503 5602 link->w = c->w;
504 5602 link->h = c->h;
505 5602 link->sample_aspect_ratio = c->pixel_aspect;
506
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 if (c->hw_frames_ctx) {
508 l->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
509 if (!l->hw_frames_ctx)
510 return AVERROR(ENOMEM);
511 }
512 5602 break;
513 1262 case AVMEDIA_TYPE_AUDIO:
514
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) {
515 13 int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
517 return ret;
518 }
519 1262 break;
520 default:
521 return AVERROR(EINVAL);
522 }
523
524
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 6864 times.
7097 for (int i = 0; i < c->nb_side_data; i++) {
525 int ret;
526
527 233 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
528 233 c->side_data[i], 0);
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 233 times.
233 if (ret < 0) {
530 av_frame_side_data_free(&link->side_data, &link->nb_side_data);
531 return ret;
532 }
533 }
534
535 6864 link->time_base = c->time_base;
536 6864 l->frame_rate = c->frame_rate;
537 6864 return 0;
538 }
539
540 362959 static int activate(AVFilterContext *ctx)
541 {
542 362959 AVFilterLink *outlink = ctx->outputs[0];
543 362959 BufferSourceContext *c = ctx->priv;
544
545
4/4
✓ Branch 0 taken 360289 times.
✓ Branch 1 taken 2670 times.
✓ Branch 3 taken 574 times.
✓ Branch 4 taken 359715 times.
362959 if (!c->eof && ff_outlink_get_status(outlink)) {
546 574 c->eof = 1;
547 574 return 0;
548 }
549
550
2/2
✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 359715 times.
362385 if (c->eof) {
551 2670 ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
552 2670 return 0;
553 }
554 359715 c->nb_failed_requests++;
555 359715 return FFERROR_NOT_READY;
556 }
557
558 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
559 {
560 .name = "default",
561 .type = AVMEDIA_TYPE_VIDEO,
562 .config_props = config_props,
563 },
564 };
565
566 const FFFilter ff_vsrc_buffer = {
567 .p.name = "buffer",
568 .p.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
569 .p.priv_class = &buffer_class,
570 .priv_size = sizeof(BufferSourceContext),
571 .activate = activate,
572 .init = init_video,
573 .uninit = uninit,
574
575 FILTER_OUTPUTS(avfilter_vsrc_buffer_outputs),
576 FILTER_QUERY_FUNC2(query_formats),
577 };
578
579 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
580 {
581 .name = "default",
582 .type = AVMEDIA_TYPE_AUDIO,
583 .config_props = config_props,
584 },
585 };
586
587 const FFFilter ff_asrc_abuffer = {
588 .p.name = "abuffer",
589 .p.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
590 .p.priv_class = &abuffer_class,
591 .priv_size = sizeof(BufferSourceContext),
592 .activate = activate,
593 .init = init_audio,
594 .uninit = uninit,
595
596 FILTER_OUTPUTS(avfilter_asrc_abuffer_outputs),
597 FILTER_QUERY_FUNC2(query_formats),
598 };
599