FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/buffersrc.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 166 237 70.0%
Functions: 13 14 92.9%
Branches: 111 206 53.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 "buffersrc.h"
39 #include "filters.h"
40 #include "formats.h"
41 #include "internal.h"
42
43 typedef struct BufferSourceContext {
44 const AVClass *class;
45 AVRational time_base; ///< time_base to set in the output link
46 AVRational frame_rate; ///< frame_rate to set in the output link
47 unsigned nb_failed_requests;
48
49 /* video only */
50 int w, h, prev_w, prev_h;
51 enum AVPixelFormat pix_fmt, prev_pix_fmt;
52 enum AVColorSpace color_space, prev_color_space;
53 enum AVColorRange color_range, prev_color_range;
54 AVRational pixel_aspect;
55
56 AVBufferRef *hw_frames_ctx;
57
58 /* audio only */
59 int sample_rate;
60 enum AVSampleFormat sample_fmt;
61 int channels;
62 char *channel_layout_str;
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 5199 AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
104 {
105 5199 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (!par)
107 return NULL;
108
109 5199 par->format = -1;
110 5199 par->color_range = AVCOL_RANGE_UNSPECIFIED;
111 5199 par->color_space = AVCOL_SPC_UNSPECIFIED;
112
113 5199 return par;
114 }
115
116 5199 int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
117 {
118 5199 BufferSourceContext *s = ctx->priv;
119
120
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5199 if (param->time_base.num > 0 && param->time_base.den > 0)
121 s->time_base = param->time_base;
122
123
1/3
✓ Branch 0 taken 5199 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
5199 switch (ctx->filter->outputs[0].type) {
124 5199 case AVMEDIA_TYPE_VIDEO:
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (param->format != AV_PIX_FMT_NONE) {
126 s->pix_fmt = s->prev_pix_fmt = param->format;
127 }
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (param->width > 0)
129 s->w = s->prev_w = param->width;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (param->height > 0)
131 s->h = s->prev_h = param->height;
132
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5199 if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
133 s->pixel_aspect = param->sample_aspect_ratio;
134
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5199 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
135 s->frame_rate = param->frame_rate;
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (param->color_space != AVCOL_SPC_UNSPECIFIED)
143 s->color_space = s->prev_color_space = param->color_space;
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (param->color_range != AVCOL_RANGE_UNSPECIFIED)
145 s->color_range = s->prev_color_range = param->color_range;
146 5199 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 5199 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 210 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
173 {
174 210 return av_buffersrc_add_frame_flags(ctx, frame, 0);
175 }
176
177 358740 static int push_frame(AVFilterGraph *graph)
178 {
179 int ret;
180
181 while (1) {
182 2246916 ret = ff_filter_graph_run_once(graph);
183
2/2
✓ Branch 0 taken 358740 times.
✓ Branch 1 taken 1888176 times.
2246916 if (ret == AVERROR(EAGAIN))
184 358740 break;
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1888176 times.
1888176 if (ret < 0)
186 return ret;
187 }
188 358740 return 0;
189 }
190
191 355766 int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
192 {
193 355766 BufferSourceContext *s = ctx->priv;
194 AVFrame *copy;
195 int refcounted, ret;
196
197 355766 s->nb_failed_requests = 0;
198
199
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 355755 times.
355766 if (!frame)
200 11 return av_buffersrc_close(ctx, s->last_pts, flags);
201
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 355754 times.
355755 if (s->eof)
202 1 return AVERROR_EOF;
203
204 355754 s->last_pts = frame->pts + frame->duration;
205
206 355754 refcounted = !!frame->buf[0];
207
208
1/2
✓ Branch 0 taken 355754 times.
✗ Branch 1 not taken.
355754 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
209
210
2/3
✓ Branch 0 taken 107663 times.
✓ Branch 1 taken 248091 times.
✗ Branch 2 not taken.
355754 switch (ctx->outputs[0]->type) {
211 107663 case AVMEDIA_TYPE_VIDEO:
212
15/28
✓ Branch 0 taken 107663 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 107663 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 107663 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 107663 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 107663 times.
✓ Branch 10 taken 102464 times.
✓ Branch 11 taken 5199 times.
✓ Branch 12 taken 102464 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 102464 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 102464 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 102464 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 107663 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 31 taken 5199 times.
✓ Branch 32 taken 102464 times.
✓ Branch 33 taken 5199 times.
✗ Branch 34 not taken.
112862 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
213 frame->format, frame->colorspace,
214 frame->color_range, frame->pts);
215 107663 break;
216 248091 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 246121 times.
✓ Branch 2 taken 1970 times.
✗ Branch 3 not taken.
248091 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 248091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 248091 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 248091 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 248091 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
248091 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->ch_layout,
224 frame->format, frame->pts);
225 248091 break;
226 default:
227 return AVERROR(EINVAL);
228 }
229
230 }
231
232
3/4
✓ Branch 0 taken 355754 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 355483 times.
✓ Branch 3 taken 271 times.
355754 if (refcounted && !(flags & AV_BUFFERSRC_FLAG_KEEP_REF)) {
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 355483 times.
355483 if (!(copy = av_frame_alloc()))
234 return AVERROR(ENOMEM);
235 355483 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 7318 times.
✓ Branch 1 taken 348436 times.
355754 if (copy->interlaced_frame)
245 7318 copy->flags |= AV_FRAME_FLAG_INTERLACED;
246
2/2
✓ Branch 0 taken 5433 times.
✓ Branch 1 taken 350321 times.
355754 if (copy->top_field_first)
247 5433 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 294849 times.
✓ Branch 1 taken 60905 times.
355754 if (copy->key_frame)
254 294849 copy->flags |= AV_FRAME_FLAG_KEY;
255 FF_ENABLE_DEPRECATION_WARNINGS
256 #endif
257
258
2/2
✓ Branch 0 taken 101617 times.
✓ Branch 1 taken 254137 times.
355754 if (copy->colorspace == AVCOL_SPC_UNSPECIFIED)
259 101617 copy->colorspace = ctx->outputs[0]->colorspace;
260
2/2
✓ Branch 0 taken 323938 times.
✓ Branch 1 taken 31816 times.
355754 if (copy->color_range == AVCOL_RANGE_UNSPECIFIED)
261 323938 copy->color_range = ctx->outputs[0]->color_range;
262
263 355754 ret = ff_filter_frame(ctx->outputs[0], copy);
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355754 times.
355754 if (ret < 0)
265 return ret;
266
267
2/2
✓ Branch 0 taken 355555 times.
✓ Branch 1 taken 199 times.
355754 if ((flags & AV_BUFFERSRC_FLAG_PUSH)) {
268 355555 ret = push_frame(ctx->graph);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355555 times.
355555 if (ret < 0)
270 return ret;
271 }
272
273 355754 return 0;
274 }
275
276 3196 int av_buffersrc_close(AVFilterContext *ctx, int64_t pts, unsigned flags)
277 {
278 3196 BufferSourceContext *s = ctx->priv;
279
280 3196 s->eof = 1;
281 3196 ff_avfilter_link_set_in_status(ctx->outputs[0], AVERROR_EOF, pts);
282
2/2
✓ Branch 0 taken 3185 times.
✓ Branch 1 taken 11 times.
3196 return (flags & AV_BUFFERSRC_FLAG_PUSH) ? push_frame(ctx->graph) : 0;
283 }
284
285 5199 static av_cold int init_video(AVFilterContext *ctx)
286 {
287 5199 BufferSourceContext *c = ctx->priv;
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 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
2/4
✓ Branch 0 taken 5199 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5199 times.
5199 if (c->w <= 0 || c->h <= 0) {
294 av_log(ctx, AV_LOG_ERROR, "Invalid size %dx%d\n", c->w, c->h);
295 return AVERROR(EINVAL);
296 }
297
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5199 times.
5199 if (av_q2d(c->time_base) <= 0) {
298 av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
299 return AVERROR(EINVAL);
300 }
301
302 5199 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",
303 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
304 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
305 c->pixel_aspect.num, c->pixel_aspect.den,
306 av_color_space_name(c->color_space), av_color_range_name(c->color_range));
307
308 5199 return 0;
309 }
310
311 363158 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
312 {
313 363158 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
314 }
315
316 #define OFFSET(x) offsetof(BufferSourceContext, x)
317 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
318 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
319
320 static const AVOption buffer_options[] = {
321 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
322 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
323 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
324 { "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 },
325 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
326 { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
327 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
328 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
329 { "colorspace", "select colorspace", OFFSET(color_space), AV_OPT_TYPE_INT, {.i64=AVCOL_SPC_UNSPECIFIED}, 0, AVCOL_SPC_NB-1, V, .unit = "colorspace"},
330 { "gbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_RGB}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
331 { "bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
332 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_UNSPECIFIED}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
333 { "fcc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_FCC}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
334 { "bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT470BG}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
335 { "smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
336 { "smpte240m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE240M}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
337 { "ycgco", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
338 { "ycgco-re", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RE}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
339 { "ycgco-ro", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_YCGCO_RO}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
340 { "bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
341 { "bt2020c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
342 { "smpte2085", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE2085}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
343 { "chroma-derived-nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_NCL},INT_MIN, INT_MAX, V, .unit = "colorspace"},
344 { "chroma-derived-c", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_CHROMA_DERIVED_CL}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
345 { "ictcp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_ICTCP}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
346 { "ipt-c2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_IPT_C2}, INT_MIN, INT_MAX, V, .unit = "colorspace"},
347 { "range", "select color range", OFFSET(color_range), AV_OPT_TYPE_INT, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, AVCOL_RANGE_NB-1, V, .unit = "range"},
348 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
349 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_UNSPECIFIED}, 0, 0, V, .unit = "range"},
350 { "limited", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
351 { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
352 { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_MPEG}, 0, 0, V, .unit = "range"},
353 { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
354 { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
355 { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_RANGE_JPEG}, 0, 0, V, .unit = "range"},
356 { NULL },
357 };
358
359 AVFILTER_DEFINE_CLASS(buffer);
360
361 static const AVOption abuffer_options[] = {
362 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
363 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
364 { "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 },
365 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
366 { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
367 { NULL },
368 };
369
370 AVFILTER_DEFINE_CLASS(abuffer);
371
372 1241 static av_cold int init_audio(AVFilterContext *ctx)
373 {
374 1241 BufferSourceContext *s = ctx->priv;
375 char buf[128];
376 1241 int ret = 0;
377
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1241 times.
1241 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
379 av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n");
380 return AVERROR(EINVAL);
381 }
382
383
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1228 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
1241 if (s->channel_layout_str || s->ch_layout.nb_channels) {
384 int n;
385
386
1/2
✓ Branch 0 taken 1228 times.
✗ Branch 1 not taken.
1228 if (!s->ch_layout.nb_channels) {
387 1228 ret = av_channel_layout_from_string(&s->ch_layout, s->channel_layout_str);
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 if (ret < 0) {
389 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
390 s->channel_layout_str);
391 return AVERROR(EINVAL);
392 }
393 }
394
395 1228 n = s->ch_layout.nb_channels;
396 1228 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 if (s->channels) {
398 if (n != s->channels) {
399 av_log(ctx, AV_LOG_ERROR,
400 "Mismatching channel count %d and layout '%s' "
401 "(%d channels)\n",
402 s->channels, buf, n);
403 return AVERROR(EINVAL);
404 }
405 }
406 1228 s->channels = n;
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 } else if (!s->channels) {
408 av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
409 "channel layout specified\n");
410 return AVERROR(EINVAL);
411 } else {
412 13 s->ch_layout = FF_COUNT2LAYOUT(s->channels);
413 13 av_channel_layout_describe(&s->ch_layout, buf, sizeof(buf));
414 }
415
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1241 times.
1241 if (!s->time_base.num)
417 s->time_base = (AVRational){1, s->sample_rate};
418
419 1241 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 1241 return ret;
425 }
426
427 6440 static av_cold void uninit(AVFilterContext *ctx)
428 {
429 6440 BufferSourceContext *s = ctx->priv;
430 6440 av_buffer_unref(&s->hw_frames_ctx);
431 6440 av_channel_layout_uninit(&s->ch_layout);
432 6440 }
433
434 6440 static int query_formats(AVFilterContext *ctx)
435 {
436 6440 BufferSourceContext *c = ctx->priv;
437 6440 AVFilterChannelLayouts *channel_layouts = NULL;
438 6440 AVFilterFormats *formats = NULL;
439 6440 AVFilterFormats *samplerates = NULL;
440 6440 AVFilterFormats *color_spaces = NULL;
441 6440 AVFilterFormats *color_ranges = NULL;
442 int ret;
443
444
2/3
✓ Branch 0 taken 5199 times.
✓ Branch 1 taken 1241 times.
✗ Branch 2 not taken.
6440 switch (ctx->outputs[0]->type) {
445 5199 case AVMEDIA_TYPE_VIDEO: {
446 5199 enum AVPixelFormat swfmt = c->pix_fmt;
447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5199 times.
5199 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
448 if (!c->hw_frames_ctx) {
449 av_log(ctx, AV_LOG_ERROR, "Setting BufferSourceContext.pix_fmt "
450 "to a HW format requires hw_frames_ctx to be non-NULL!\n");
451 return AVERROR(EINVAL);
452 }
453 swfmt = ((AVHWFramesContext *) c->hw_frames_ctx->data)->sw_format;
454 }
455
2/4
✓ Branch 1 taken 5199 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5199 times.
10398 if ((ret = ff_add_format (&formats, c->pix_fmt)) < 0 ||
456 5199 (ret = ff_set_common_formats (ctx , formats )) < 0)
457 return ret;
458 /* force specific colorspace/range downstream only for ordinary YUV */
459
2/2
✓ Branch 1 taken 4432 times.
✓ Branch 2 taken 767 times.
5199 if (ff_fmt_is_regular_yuv(swfmt)) {
460
2/4
✓ Branch 1 taken 4432 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4432 times.
8864 if ((ret = ff_add_format(&color_spaces, c->color_space)) < 0 ||
461 4432 (ret = ff_set_common_color_spaces(ctx, color_spaces)) < 0)
462 return ret;
463
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4432 times.
4432 if ((ret = ff_add_format(&color_ranges, c->color_range)) < 0)
464 return ret;
465
2/2
✓ Branch 0 taken 1166 times.
✓ Branch 1 taken 3266 times.
4432 if (c->color_range == AVCOL_RANGE_UNSPECIFIED) {
466 /* allow implicitly promoting unspecified to mpeg */
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1166 times.
1166 if ((ret = ff_add_format(&color_ranges, AVCOL_RANGE_MPEG)) < 0)
468 return ret;
469 }
470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4432 times.
4432 if ((ret = ff_set_common_color_ranges(ctx, color_ranges)) < 0)
471 return ret;
472 }
473 5199 break;
474 }
475 1241 case AVMEDIA_TYPE_AUDIO:
476
2/4
✓ Branch 1 taken 1241 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1241 times.
✗ Branch 4 not taken.
2482 if ((ret = ff_add_format (&formats , c->sample_fmt )) < 0 ||
477
1/2
✓ Branch 1 taken 1241 times.
✗ Branch 2 not taken.
2482 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1241 times.
2482 (ret = ff_add_format (&samplerates, c->sample_rate)) < 0 ||
479 1241 (ret = ff_set_common_samplerates (ctx , samplerates )) < 0)
480 return ret;
481
482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1241 times.
1241 if ((ret = ff_add_channel_layout(&channel_layouts, &c->ch_layout)) < 0)
483 return ret;
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1241 times.
1241 if ((ret = ff_set_common_channel_layouts(ctx, channel_layouts)) < 0)
485 return ret;
486 1241 break;
487 default:
488 return AVERROR(EINVAL);
489 }
490
491 6440 return 0;
492 }
493
494 6440 static int config_props(AVFilterLink *link)
495 {
496 6440 BufferSourceContext *c = link->src->priv;
497
498
2/3
✓ Branch 0 taken 5199 times.
✓ Branch 1 taken 1241 times.
✗ Branch 2 not taken.
6440 switch (link->type) {
499 5199 case AVMEDIA_TYPE_VIDEO:
500 5199 link->w = c->w;
501 5199 link->h = c->h;
502 5199 link->sample_aspect_ratio = c->pixel_aspect;
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (c->hw_frames_ctx) {
505 link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
506 if (!link->hw_frames_ctx)
507 return AVERROR(ENOMEM);
508 }
509 5199 break;
510 1241 case AVMEDIA_TYPE_AUDIO:
511
3/4
✓ Branch 0 taken 1241 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1228 times.
1241 if (!c->ch_layout.nb_channels || c->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
512 13 int ret = av_channel_layout_copy(&c->ch_layout, &link->ch_layout);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
514 return ret;
515 }
516 1241 break;
517 default:
518 return AVERROR(EINVAL);
519 }
520
521 6440 link->time_base = c->time_base;
522 6440 link->frame_rate = c->frame_rate;
523 6440 return 0;
524 }
525
526 355605 static int activate(AVFilterContext *ctx)
527 {
528 355605 AVFilterLink *outlink = ctx->outputs[0];
529 355605 BufferSourceContext *c = ctx->priv;
530
531
4/4
✓ Branch 0 taken 352644 times.
✓ Branch 1 taken 2961 times.
✓ Branch 3 taken 546 times.
✓ Branch 4 taken 352098 times.
355605 if (!c->eof && ff_outlink_get_status(outlink)) {
532 546 c->eof = 1;
533 546 return 0;
534 }
535
536
2/2
✓ Branch 0 taken 2961 times.
✓ Branch 1 taken 352098 times.
355059 if (c->eof) {
537 2961 ff_outlink_set_status(outlink, AVERROR_EOF, c->last_pts);
538 2961 return 0;
539 }
540 352098 c->nb_failed_requests++;
541 352098 return FFERROR_NOT_READY;
542 }
543
544 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
545 {
546 .name = "default",
547 .type = AVMEDIA_TYPE_VIDEO,
548 .config_props = config_props,
549 },
550 };
551
552 const AVFilter ff_vsrc_buffer = {
553 .name = "buffer",
554 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
555 .priv_size = sizeof(BufferSourceContext),
556 .activate = activate,
557 .init = init_video,
558 .uninit = uninit,
559
560 .inputs = NULL,
561 FILTER_OUTPUTS(avfilter_vsrc_buffer_outputs),
562 FILTER_QUERY_FUNC(query_formats),
563 .priv_class = &buffer_class,
564 };
565
566 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
567 {
568 .name = "default",
569 .type = AVMEDIA_TYPE_AUDIO,
570 .config_props = config_props,
571 },
572 };
573
574 const AVFilter ff_asrc_abuffer = {
575 .name = "abuffer",
576 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
577 .priv_size = sizeof(BufferSourceContext),
578 .activate = activate,
579 .init = init_audio,
580 .uninit = uninit,
581
582 .inputs = NULL,
583 FILTER_OUTPUTS(avfilter_asrc_abuffer_outputs),
584 FILTER_QUERY_FUNC(query_formats),
585 .priv_class = &abuffer_class,
586 };
587