FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfilter.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 585 779 75.1%
Branches: 336 494 68.0%

Line Branch Exec Source
1 /*
2 * filter layer
3 * Copyright (c) 2007 Bobby Bingham
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/bprint.h"
25 #include "libavutil/buffer.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/frame.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/rational.h"
35 #include "libavutil/samplefmt.h"
36
37 #define FF_INTERNAL_FIELDS 1
38 #include "framequeue.h"
39
40 #include "audio.h"
41 #include "avfilter.h"
42 #include "filters.h"
43 #include "formats.h"
44 #include "framepool.h"
45 #include "internal.h"
46
47 1404555 static void tlog_ref(void *ctx, AVFrame *ref, int end)
48 {
49 #ifdef TRACE
50 ff_tlog(ctx,
51 "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
52 ref, ref->buf, ref->data[0],
53 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
54 ref->pts, ref->pkt_pos);
55
56 if (ref->width) {
57 ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
58 ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
59 ref->width, ref->height,
60 !ref->interlaced_frame ? 'P' : /* Progressive */
61 ref->top_field_first ? 'T' : 'B', /* Top / Bottom */
62 ref->key_frame,
63 av_get_picture_type_char(ref->pict_type));
64 }
65 if (ref->nb_samples) {
66 AVBPrint bprint;
67
68 av_bprint_init(&bprint, 1, AV_BPRINT_SIZE_UNLIMITED);
69 av_channel_layout_describe_bprint(&ref->ch_layout, &bprint);
70 ff_tlog(ctx, " cl:%s n:%d r:%d",
71 bprint.str,
72 ref->nb_samples,
73 ref->sample_rate);
74 av_bprint_finalize(&bprint, NULL);
75 }
76
77 ff_tlog(ctx, "]%s", end ? "\n" : "");
78 #endif
79 1404555 }
80
81 static void command_queue_pop(AVFilterContext *filter)
82 {
83 AVFilterCommand *c= filter->command_queue;
84 av_freep(&c->arg);
85 av_freep(&c->command);
86 filter->command_queue= c->next;
87 av_free(c);
88 }
89
90 /**
91 * Append a new pad.
92 *
93 * @param count Pointer to the number of pads in the list
94 * @param pads Pointer to the pointer to the beginning of the list of pads
95 * @param links Pointer to the pointer to the beginning of the list of links
96 * @param newpad The new pad to add. A copy is made when adding.
97 * @return >= 0 in case of success, a negative AVERROR code on error
98 */
99 181 static int append_pad(unsigned *count, AVFilterPad **pads,
100 AVFilterLink ***links, AVFilterPad *newpad)
101 {
102 AVFilterLink **newlinks;
103 AVFilterPad *newpads;
104 181 unsigned idx = *count;
105
106 181 newpads = av_realloc_array(*pads, idx + 1, sizeof(*newpads));
107 181 newlinks = av_realloc_array(*links, idx + 1, sizeof(*newlinks));
108
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 if (newpads)
109 181 *pads = newpads;
110
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 if (newlinks)
111 181 *links = newlinks;
112
2/4
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 181 times.
181 if (!newpads || !newlinks) {
113 if (newpad->flags & AVFILTERPAD_FLAG_FREE_NAME)
114 av_freep(&newpad->name);
115 return AVERROR(ENOMEM);
116 }
117
118 181 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
119 181 (*links)[idx] = NULL;
120
121 181 (*count)++;
122
123 181 return 0;
124 }
125
126 82 int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
127 {
128 82 return append_pad(&f->nb_inputs, &f->input_pads, &f->inputs, p);
129 }
130
131 70 int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
132 {
133 70 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
134 70 return ff_append_inpad(f, p);
135 }
136
137 99 int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
138 {
139 99 return append_pad(&f->nb_outputs, &f->output_pads, &f->outputs, p);
140 }
141
142 90 int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
143 {
144 90 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
145 90 return ff_append_outpad(f, p);
146 }
147
148 25272 int avfilter_link(AVFilterContext *src, unsigned srcpad,
149 AVFilterContext *dst, unsigned dstpad)
150 {
151 AVFilterLink *link;
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25272 times.
25272 av_assert0(src->graph);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25272 times.
25272 av_assert0(dst->graph);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25272 times.
25272 av_assert0(src->graph == dst->graph);
156
157
2/4
✓ Branch 0 taken 25272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25272 times.
✗ Branch 3 not taken.
25272 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
158
2/4
✓ Branch 0 taken 25272 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25272 times.
25272 src->outputs[srcpad] || dst->inputs[dstpad])
159 return AVERROR(EINVAL);
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25272 times.
25272 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
162 av_log(src, AV_LOG_ERROR,
163 "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
164 src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
165 dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
166 return AVERROR(EINVAL);
167 }
168
169 25272 link = av_mallocz(sizeof(*link));
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25272 times.
25272 if (!link)
171 return AVERROR(ENOMEM);
172
173 25272 src->outputs[srcpad] = dst->inputs[dstpad] = link;
174
175 25272 link->src = src;
176 25272 link->dst = dst;
177 25272 link->srcpad = &src->output_pads[srcpad];
178 25272 link->dstpad = &dst->input_pads[dstpad];
179 25272 link->type = src->output_pads[srcpad].type;
180 av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
181 25272 link->format = -1;
182 25272 ff_framequeue_init(&link->fifo, &src->graph->internal->frame_queues);
183
184 25272 return 0;
185 }
186
187 25404 void avfilter_link_free(AVFilterLink **link)
188 {
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25404 times.
25404 if (!*link)
190 return;
191
192 25404 ff_framequeue_free(&(*link)->fifo);
193 25404 ff_frame_pool_uninit((FFFramePool**)&(*link)->frame_pool);
194 25404 av_channel_layout_uninit(&(*link)->ch_layout);
195
196 25404 av_freep(link);
197 }
198
199 3743171 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
200 {
201 3743171 filter->ready = FFMAX(filter->ready, priority);
202 3743171 }
203
204 /**
205 * Clear frame_blocked_in on all outputs.
206 * This is necessary whenever something changes on input.
207 */
208 2429330 static void filter_unblock(AVFilterContext *filter)
209 {
210 unsigned i;
211
212
2/2
✓ Branch 0 taken 2015354 times.
✓ Branch 1 taken 2429330 times.
4444684 for (i = 0; i < filter->nb_outputs; i++)
213 2015354 filter->outputs[i]->frame_blocked_in = 0;
214 2429330 }
215
216
217 24889 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
218 {
219
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 24852 times.
24889 if (link->status_in == status)
220 37 return;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24852 times.
24852 av_assert0(!link->status_in);
222 24852 link->status_in = status;
223 24852 link->status_in_pts = pts;
224 24852 link->frame_wanted_out = 0;
225 24852 link->frame_blocked_in = 0;
226 24852 filter_unblock(link->dst);
227 24852 ff_filter_set_ready(link->dst, 200);
228 }
229
230 18468 void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts)
231 {
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18468 times.
18468 av_assert0(!link->frame_wanted_out);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18468 times.
18468 av_assert0(!link->status_out);
234 18468 link->status_out = status;
235
2/2
✓ Branch 0 taken 17926 times.
✓ Branch 1 taken 542 times.
18468 if (pts != AV_NOPTS_VALUE)
236 17926 ff_update_link_current_pts(link, pts);
237 18468 filter_unblock(link->dst);
238 18468 ff_filter_set_ready(link->src, 200);
239 18468 }
240
241 957 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
242 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
243 {
244 int ret;
245 957 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
246
247 957 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
248 "between the filter '%s' and the filter '%s'\n",
249 957 filt->name, link->src->name, link->dst->name);
250
251 957 link->dst->inputs[dstpad_idx] = NULL;
252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 957 times.
957 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
253 /* failed to link output filter to new filter */
254 link->dst->inputs[dstpad_idx] = link;
255 return ret;
256 }
257
258 /* re-hookup the link to the new destination filter we inserted */
259 957 link->dst = filt;
260 957 link->dstpad = &filt->input_pads[filt_srcpad_idx];
261 957 filt->inputs[filt_srcpad_idx] = link;
262
263 /* if any information on supported media formats already exists on the
264 * link, we need to preserve that */
265
1/2
✓ Branch 0 taken 957 times.
✗ Branch 1 not taken.
957 if (link->outcfg.formats)
266 957 ff_formats_changeref(&link->outcfg.formats,
267 957 &filt->outputs[filt_dstpad_idx]->outcfg.formats);
268
2/2
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 550 times.
957 if (link->outcfg.samplerates)
269 407 ff_formats_changeref(&link->outcfg.samplerates,
270 407 &filt->outputs[filt_dstpad_idx]->outcfg.samplerates);
271
2/2
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 550 times.
957 if (link->outcfg.channel_layouts)
272 407 ff_channel_layouts_changeref(&link->outcfg.channel_layouts,
273 407 &filt->outputs[filt_dstpad_idx]->outcfg.channel_layouts);
274
275 957 return 0;
276 }
277
278 31374 int avfilter_config_links(AVFilterContext *filter)
279 {
280 int (*config_link)(AVFilterLink *);
281 unsigned i;
282 int ret;
283
284
2/2
✓ Branch 0 taken 25144 times.
✓ Branch 1 taken 31374 times.
56518 for (i = 0; i < filter->nb_inputs; i ++) {
285 25144 AVFilterLink *link = filter->inputs[i];
286 AVFilterLink *inlink;
287
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25144 times.
25144 if (!link) continue;
289
2/4
✓ Branch 0 taken 25144 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25144 times.
25144 if (!link->src || !link->dst) {
290 av_log(filter, AV_LOG_ERROR,
291 "Not all input and output are properly linked (%d).\n", i);
292 return AVERROR(EINVAL);
293 }
294
295
2/2
✓ Branch 0 taken 18814 times.
✓ Branch 1 taken 6330 times.
25144 inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
296 25144 link->current_pts =
297 25144 link->current_pts_us = AV_NOPTS_VALUE;
298
299
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25113 times.
✗ Branch 3 not taken.
25144 switch (link->init_state) {
300 31 case AVLINK_INIT:
301 31 continue;
302 case AVLINK_STARTINIT:
303 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
304 return 0;
305 25113 case AVLINK_UNINIT:
306 25113 link->init_state = AVLINK_STARTINIT;
307
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25113 times.
25113 if ((ret = avfilter_config_links(link->src)) < 0)
309 return ret;
310
311
2/2
✓ Branch 0 taken 12943 times.
✓ Branch 1 taken 12170 times.
25113 if (!(config_link = link->srcpad->config_props)) {
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12943 times.
12943 if (link->src->nb_inputs != 1) {
313 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
314 "with more than one input "
315 "must set config_props() "
316 "callbacks on all outputs\n");
317 return AVERROR(EINVAL);
318 }
319
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12170 times.
12170 } else if ((ret = config_link(link)) < 0) {
320 av_log(link->src, AV_LOG_ERROR,
321 "Failed to configure output pad on %s\n",
322 link->src->name);
323 return ret;
324 }
325
326
2/3
✓ Branch 0 taken 20000 times.
✓ Branch 1 taken 5113 times.
✗ Branch 2 not taken.
25113 switch (link->type) {
327 20000 case AVMEDIA_TYPE_VIDEO:
328
3/4
✓ Branch 0 taken 14664 times.
✓ Branch 1 taken 5336 times.
✓ Branch 2 taken 14664 times.
✗ Branch 3 not taken.
20000 if (!link->time_base.num && !link->time_base.den)
329
1/2
✓ Branch 0 taken 14664 times.
✗ Branch 1 not taken.
14664 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
330
331
4/4
✓ Branch 0 taken 19046 times.
✓ Branch 1 taken 954 times.
✓ Branch 2 taken 10818 times.
✓ Branch 3 taken 8228 times.
20000 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
332
2/2
✓ Branch 0 taken 10805 times.
✓ Branch 1 taken 13 times.
10818 link->sample_aspect_ratio = inlink ?
333 inlink->sample_aspect_ratio : (AVRational){1,1};
334
335
2/2
✓ Branch 0 taken 14908 times.
✓ Branch 1 taken 5092 times.
20000 if (inlink) {
336
3/4
✓ Branch 0 taken 14719 times.
✓ Branch 1 taken 189 times.
✓ Branch 2 taken 14719 times.
✗ Branch 3 not taken.
14908 if (!link->frame_rate.num && !link->frame_rate.den)
337 14719 link->frame_rate = inlink->frame_rate;
338
2/2
✓ Branch 0 taken 10367 times.
✓ Branch 1 taken 4541 times.
14908 if (!link->w)
339 10367 link->w = inlink->w;
340
2/2
✓ Branch 0 taken 10367 times.
✓ Branch 1 taken 4541 times.
14908 if (!link->h)
341 10367 link->h = inlink->h;
342
2/4
✓ Branch 0 taken 5092 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5092 times.
5092 } else if (!link->w || !link->h) {
343 av_log(link->src, AV_LOG_ERROR,
344 "Video source filters must set their output link's "
345 "width and height\n");
346 return AVERROR(EINVAL);
347 }
348 20000 break;
349
350 5113 case AVMEDIA_TYPE_AUDIO:
351
2/2
✓ Branch 0 taken 3897 times.
✓ Branch 1 taken 1216 times.
5113 if (inlink) {
352
3/4
✓ Branch 0 taken 2644 times.
✓ Branch 1 taken 1253 times.
✓ Branch 2 taken 2644 times.
✗ Branch 3 not taken.
3897 if (!link->time_base.num && !link->time_base.den)
353 2644 link->time_base = inlink->time_base;
354 }
355
356
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5100 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
5113 if (!link->time_base.num && !link->time_base.den)
357 13 link->time_base = (AVRational) {1, link->sample_rate};
358 }
359
360
3/4
✓ Branch 0 taken 18805 times.
✓ Branch 1 taken 6308 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18805 times.
25113 if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
361 !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
362 av_assert0(!link->hw_frames_ctx &&
363 "should not be set by non-hwframe-aware filter");
364 link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
365 if (!link->hw_frames_ctx)
366 return AVERROR(ENOMEM);
367 }
368
369
2/2
✓ Branch 0 taken 2078 times.
✓ Branch 1 taken 23035 times.
25113 if ((config_link = link->dstpad->config_props))
370
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2078 times.
2078 if ((ret = config_link(link)) < 0) {
371 av_log(link->dst, AV_LOG_ERROR,
372 "Failed to configure input pad on %s\n",
373 link->dst->name);
374 return ret;
375 }
376
377 25113 link->init_state = AVLINK_INIT;
378 }
379 }
380
381 31374 return 0;
382 }
383
384 #ifdef TRACE
385 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
386 {
387 if (link->type == AVMEDIA_TYPE_VIDEO) {
388 ff_tlog(ctx,
389 "link[%p s:%dx%d fmt:%s %s->%s]%s",
390 link, link->w, link->h,
391 av_get_pix_fmt_name(link->format),
392 link->src ? link->src->filter->name : "",
393 link->dst ? link->dst->filter->name : "",
394 end ? "\n" : "");
395 } else {
396 char buf[128];
397 av_channel_layout_describe(&link->ch_layout, buf, sizeof(buf));
398
399 ff_tlog(ctx,
400 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
401 link, (int)link->sample_rate, buf,
402 av_get_sample_fmt_name(link->format),
403 link->src ? link->src->filter->name : "",
404 link->dst ? link->dst->filter->name : "",
405 end ? "\n" : "");
406 }
407 }
408 #endif
409
410 923736 int ff_request_frame(AVFilterLink *link)
411 {
412 FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
413
414 av_assert1(!link->dst->filter->activate);
415
2/2
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 922586 times.
923736 if (link->status_out)
416 1150 return link->status_out;
417
2/2
✓ Branch 0 taken 17954 times.
✓ Branch 1 taken 904632 times.
922586 if (link->status_in) {
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17954 times.
17954 if (ff_framequeue_queued_frames(&link->fifo)) {
419 av_assert1(!link->frame_wanted_out);
420 av_assert1(link->dst->ready >= 300);
421 return 0;
422 } else {
423 /* Acknowledge status change. Filters using ff_request_frame() will
424 handle the change automatically. Filters can also check the
425 status directly but none do yet. */
426 17954 ff_avfilter_link_set_out_status(link, link->status_in, link->status_in_pts);
427 17954 return link->status_out;
428 }
429 }
430 904632 link->frame_wanted_out = 1;
431 904632 ff_filter_set_ready(link->src, 100);
432 904632 return 0;
433 }
434
435 18457 static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
436 {
437 unsigned i;
438 18457 int64_t r = INT64_MAX;
439
440
2/2
✓ Branch 0 taken 18442 times.
✓ Branch 1 taken 18457 times.
36899 for (i = 0; i < ctx->nb_inputs; i++)
441
1/2
✓ Branch 0 taken 18442 times.
✗ Branch 1 not taken.
18442 if (ctx->inputs[i]->status_out == status)
442 18442 r = FFMIN(r, av_rescale_q(ctx->inputs[i]->current_pts, ctx->inputs[i]->time_base, link_time_base));
443
2/2
✓ Branch 0 taken 18442 times.
✓ Branch 1 taken 15 times.
18457 if (r < INT64_MAX)
444 18442 return r;
445 15 av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 for (i = 0; i < ctx->nb_inputs; i++)
447 r = FFMIN(r, av_rescale_q(ctx->inputs[i]->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (r < INT64_MAX)
449 return r;
450 15 return AV_NOPTS_VALUE;
451 }
452
453 1292267 static int ff_request_frame_to_filter(AVFilterLink *link)
454 {
455 1292267 int ret = -1;
456
457 FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
458 /* Assume the filter is blocked, let the method clear it if not */
459 1292267 link->frame_blocked_in = 1;
460
2/2
✓ Branch 0 taken 588613 times.
✓ Branch 1 taken 703654 times.
1292267 if (link->srcpad->request_frame)
461 588613 ret = link->srcpad->request_frame(link);
462
1/2
✓ Branch 0 taken 703654 times.
✗ Branch 1 not taken.
703654 else if (link->src->inputs[0])
463 703654 ret = ff_request_frame(link->src->inputs[0]);
464
2/2
✓ Branch 0 taken 382963 times.
✓ Branch 1 taken 909304 times.
1292267 if (ret < 0) {
465
3/4
✓ Branch 0 taken 18457 times.
✓ Branch 1 taken 364506 times.
✓ Branch 2 taken 18457 times.
✗ Branch 3 not taken.
382963 if (ret != AVERROR(EAGAIN) && ret != link->status_in)
466 18457 ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
467
2/2
✓ Branch 0 taken 18457 times.
✓ Branch 1 taken 364506 times.
382963 if (ret == AVERROR_EOF)
468 18457 ret = 0;
469 }
470 1292267 return ret;
471 }
472
473 static const char *const var_names[] = {
474 "t",
475 "n",
476 "pos",
477 "w",
478 "h",
479 NULL
480 };
481
482 enum {
483 VAR_T,
484 VAR_N,
485 VAR_POS,
486 VAR_W,
487 VAR_H,
488 VAR_VARS_NB
489 };
490
491 4 static int set_enable_expr(AVFilterContext *ctx, const char *expr)
492 {
493 int ret;
494 char *expr_dup;
495 4 AVExpr *old = ctx->enable;
496
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
498 av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
499 "with filter '%s'\n", ctx->filter->name);
500 return AVERROR_PATCHWELCOME;
501 }
502
503 4 expr_dup = av_strdup(expr);
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!expr_dup)
505 return AVERROR(ENOMEM);
506
507
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!ctx->var_values) {
508 4 ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ctx->var_values) {
510 av_free(expr_dup);
511 return AVERROR(ENOMEM);
512 }
513 }
514
515 4 ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
516 NULL, NULL, NULL, NULL, 0, ctx->priv);
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
518 av_log(ctx->priv, AV_LOG_ERROR,
519 "Error when evaluating the expression '%s' for enable\n",
520 expr_dup);
521 av_free(expr_dup);
522 return ret;
523 }
524
525 4 av_expr_free(old);
526 4 av_free(ctx->enable_str);
527 4 ctx->enable_str = expr_dup;
528 4 return 0;
529 }
530
531 1448938 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
532 {
533
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1448915 times.
1448938 if (pts == AV_NOPTS_VALUE)
534 23 return;
535 1448915 link->current_pts = pts;
536 1448915 link->current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
537 /* TODO use duration */
538
3/4
✓ Branch 0 taken 1448915 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 435270 times.
✓ Branch 3 taken 1013645 times.
1448915 if (link->graph && link->age_index >= 0)
539 435270 ff_avfilter_graph_update_heap(link->graph, link);
540 }
541
542 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
543 {
544 if(!strcmp(cmd, "ping")){
545 char local_res[256] = {0};
546
547 if (!res) {
548 res = local_res;
549 res_len = sizeof(local_res);
550 }
551 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
552 if (res == local_res)
553 av_log(filter, AV_LOG_INFO, "%s", res);
554 return 0;
555 }else if(!strcmp(cmd, "enable")) {
556 return set_enable_expr(filter, arg);
557 }else if(filter->filter->process_command) {
558 return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
559 }
560 return AVERROR(ENOSYS);
561 }
562
563 #if FF_API_PAD_COUNT
564 int avfilter_pad_count(const AVFilterPad *pads)
565 {
566 const AVFilter *filter;
567 void *opaque = NULL;
568
569 if (!pads)
570 return 0;
571
572 while (filter = av_filter_iterate(&opaque)) {
573 if (pads == filter->inputs)
574 return filter->nb_inputs;
575 if (pads == filter->outputs)
576 return filter->nb_outputs;
577 }
578
579 av_assert0(!"AVFilterPad list not from a filter");
580 return AVERROR_BUG;
581 }
582 #endif
583
584 unsigned avfilter_filter_pad_count(const AVFilter *filter, int is_output)
585 {
586 return is_output ? filter->nb_outputs : filter->nb_inputs;
587 }
588
589 464 static const char *default_filter_name(void *filter_ctx)
590 {
591 464 AVFilterContext *ctx = filter_ctx;
592
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
464 return ctx->name ? ctx->name : ctx->filter->name;
593 }
594
595 1412 static void *filter_child_next(void *obj, void *prev)
596 {
597 1412 AVFilterContext *ctx = obj;
598
4/8
✓ Branch 0 taken 1412 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1412 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1412 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1412 times.
✗ Branch 7 not taken.
1412 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
599 1412 return ctx->priv;
600 return NULL;
601 }
602
603 static const AVClass *filter_child_class_iterate(void **iter)
604 {
605 const AVFilter *f;
606
607 while ((f = av_filter_iterate(iter)))
608 if (f->priv_class)
609 return f->priv_class;
610
611 return NULL;
612 }
613
614 #define OFFSET(x) offsetof(AVFilterContext, x)
615 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
616 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
617 static const AVOption avfilter_options[] = {
618 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
619 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
620 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
621 { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
622 { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
623 { .i64 = 0 }, 0, INT_MAX, FLAGS },
624 { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
625 OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
626 { NULL },
627 };
628
629 static const AVClass avfilter_class = {
630 .class_name = "AVFilter",
631 .item_name = default_filter_name,
632 .version = LIBAVUTIL_VERSION_INT,
633 .category = AV_CLASS_CATEGORY_FILTER,
634 .child_next = filter_child_next,
635 .child_class_iterate = filter_child_class_iterate,
636 .option = avfilter_options,
637 };
638
639 682 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
640 int *ret, int nb_jobs)
641 {
642 int i;
643
644
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 682 times.
1364 for (i = 0; i < nb_jobs; i++) {
645 682 int r = func(ctx, arg, i, nb_jobs);
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 682 times.
682 if (ret)
647 ret[i] = r;
648 }
649 682 return 0;
650 }
651
652 31668 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
653 {
654 AVFilterContext *ret;
655 31668 int preinited = 0;
656
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (!filter)
658 return NULL;
659
660 31668 ret = av_mallocz(sizeof(AVFilterContext));
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (!ret)
662 return NULL;
663
664 31668 ret->av_class = &avfilter_class;
665 31668 ret->filter = filter;
666
1/2
✓ Branch 0 taken 31668 times.
✗ Branch 1 not taken.
31668 ret->name = inst_name ? av_strdup(inst_name) : NULL;
667
2/2
✓ Branch 0 taken 28487 times.
✓ Branch 1 taken 3181 times.
31668 if (filter->priv_size) {
668 28487 ret->priv = av_mallocz(filter->priv_size);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28487 times.
28487 if (!ret->priv)
670 goto err;
671 }
672
2/2
✓ Branch 0 taken 1301 times.
✓ Branch 1 taken 30367 times.
31668 if (filter->preinit) {
673
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1301 times.
1301 if (filter->preinit(ret) < 0)
674 goto err;
675 1301 preinited = 1;
676 }
677
678 31668 av_opt_set_defaults(ret);
679
2/2
✓ Branch 0 taken 28289 times.
✓ Branch 1 taken 3379 times.
31668 if (filter->priv_class) {
680 28289 *(const AVClass**)ret->priv = filter->priv_class;
681 28289 av_opt_set_defaults(ret->priv);
682 }
683
684 31668 ret->internal = av_mallocz(sizeof(*ret->internal));
685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (!ret->internal)
686 goto err;
687 31668 ret->internal->execute = default_execute;
688
689 31668 ret->nb_inputs = filter->nb_inputs;
690
2/2
✓ Branch 0 taken 25260 times.
✓ Branch 1 taken 6408 times.
31668 if (ret->nb_inputs ) {
691 25260 ret->input_pads = av_memdup(filter->inputs, ret->nb_inputs * sizeof(*filter->inputs));
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25260 times.
25260 if (!ret->input_pads)
693 goto err;
694 25260 ret->inputs = av_calloc(ret->nb_inputs, sizeof(*ret->inputs));
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25260 times.
25260 if (!ret->inputs)
696 goto err;
697 }
698
699 31668 ret->nb_outputs = filter->nb_outputs;
700
2/2
✓ Branch 0 taken 25345 times.
✓ Branch 1 taken 6323 times.
31668 if (ret->nb_outputs) {
701 25345 ret->output_pads = av_memdup(filter->outputs, ret->nb_outputs * sizeof(*filter->outputs));
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25345 times.
25345 if (!ret->output_pads)
703 goto err;
704 25345 ret->outputs = av_calloc(ret->nb_outputs, sizeof(*ret->outputs));
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25345 times.
25345 if (!ret->outputs)
706 goto err;
707 }
708
709 31668 return ret;
710
711 err:
712 if (preinited)
713 filter->uninit(ret);
714 av_freep(&ret->inputs);
715 av_freep(&ret->input_pads);
716 ret->nb_inputs = 0;
717 av_freep(&ret->outputs);
718 av_freep(&ret->output_pads);
719 ret->nb_outputs = 0;
720 av_freep(&ret->priv);
721 av_freep(&ret->internal);
722 av_free(ret);
723 return NULL;
724 }
725
726 50868 static void free_link(AVFilterLink *link)
727 {
728
2/2
✓ Branch 0 taken 25464 times.
✓ Branch 1 taken 25404 times.
50868 if (!link)
729 25464 return;
730
731
2/2
✓ Branch 0 taken 25272 times.
✓ Branch 1 taken 132 times.
25404 if (link->src)
732 25272 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
733
2/2
✓ Branch 0 taken 25272 times.
✓ Branch 1 taken 132 times.
25404 if (link->dst)
734 25272 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
735
736 25404 av_buffer_unref(&link->hw_frames_ctx);
737
738 25404 ff_formats_unref(&link->incfg.formats);
739 25404 ff_formats_unref(&link->outcfg.formats);
740 25404 ff_formats_unref(&link->incfg.samplerates);
741 25404 ff_formats_unref(&link->outcfg.samplerates);
742 25404 ff_channel_layouts_unref(&link->incfg.channel_layouts);
743 25404 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
744 25404 avfilter_link_free(&link);
745 }
746
747 31668 void avfilter_free(AVFilterContext *filter)
748 {
749 int i;
750
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (!filter)
752 return;
753
754
1/2
✓ Branch 0 taken 31668 times.
✗ Branch 1 not taken.
31668 if (filter->graph)
755 31668 ff_filter_graph_remove_filter(filter->graph, filter);
756
757
2/2
✓ Branch 0 taken 20207 times.
✓ Branch 1 taken 11461 times.
31668 if (filter->filter->uninit)
758 20207 filter->filter->uninit(filter);
759
760
2/2
✓ Branch 0 taken 25422 times.
✓ Branch 1 taken 31668 times.
57090 for (i = 0; i < filter->nb_inputs; i++) {
761 25422 free_link(filter->inputs[i]);
762
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 25352 times.
25422 if (filter->input_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
763 70 av_freep(&filter->input_pads[i].name);
764 }
765
2/2
✓ Branch 0 taken 25446 times.
✓ Branch 1 taken 31668 times.
57114 for (i = 0; i < filter->nb_outputs; i++) {
766 25446 free_link(filter->outputs[i]);
767
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 25352 times.
25446 if (filter->output_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
768 94 av_freep(&filter->output_pads[i].name);
769 }
770
771
2/2
✓ Branch 0 taken 28289 times.
✓ Branch 1 taken 3379 times.
31668 if (filter->filter->priv_class)
772 28289 av_opt_free(filter->priv);
773
774 31668 av_buffer_unref(&filter->hw_device_ctx);
775
776 31668 av_freep(&filter->name);
777 31668 av_freep(&filter->input_pads);
778 31668 av_freep(&filter->output_pads);
779 31668 av_freep(&filter->inputs);
780 31668 av_freep(&filter->outputs);
781 31668 av_freep(&filter->priv);
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 while(filter->command_queue){
783 command_queue_pop(filter);
784 }
785 31668 av_opt_free(filter);
786 31668 av_expr_free(filter->enable);
787 31668 filter->enable = NULL;
788 31668 av_freep(&filter->var_values);
789 31668 av_freep(&filter->internal);
790 31668 av_free(filter);
791 }
792
793 11568 int ff_filter_get_nb_threads(AVFilterContext *ctx)
794 {
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11568 times.
11568 if (ctx->nb_threads > 0)
796 return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
797 11568 return ctx->graph->nb_threads;
798 }
799
800 20301 static int process_options(AVFilterContext *ctx, AVDictionary **options,
801 const char *args)
802 {
803 20301 const AVOption *o = NULL;
804 int ret;
805 20301 char *av_uninit(parsed_key), *av_uninit(value);
806 const char *key;
807 20301 int offset= -1;
808
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20301 times.
20301 if (!args)
810 return 0;
811
812
2/2
✓ Branch 0 taken 54011 times.
✓ Branch 1 taken 20301 times.
74312 while (*args) {
813 54011 const char *shorthand = NULL;
814
815 54011 o = av_opt_next(ctx->priv, o);
816
2/2
✓ Branch 0 taken 53365 times.
✓ Branch 1 taken 646 times.
54011 if (o) {
817
4/4
✓ Branch 0 taken 50550 times.
✓ Branch 1 taken 2815 times.
✓ Branch 2 taken 2147 times.
✓ Branch 3 taken 48403 times.
53365 if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
818 4962 continue;
819 48403 offset = o->offset;
820 48403 shorthand = o->name;
821 }
822
823 49049 ret = av_opt_get_key_value(&args, "=", ":",
824 shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
825 &parsed_key, &value);
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49049 times.
49049 if (ret < 0) {
827 if (ret == AVERROR(EINVAL))
828 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
829 else
830 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
831 av_err2str(ret));
832 return ret;
833 }
834
2/2
✓ Branch 0 taken 29928 times.
✓ Branch 1 taken 19121 times.
49049 if (*args)
835 29928 args++;
836
2/2
✓ Branch 0 taken 38450 times.
✓ Branch 1 taken 10599 times.
49049 if (parsed_key) {
837 38450 key = parsed_key;
838
2/2
✓ Branch 1 taken 332774 times.
✓ Branch 2 taken 38450 times.
371224 while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
839 } else {
840 10599 key = shorthand;
841 }
842
843 49049 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
844
845
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 49045 times.
49049 if (av_opt_find(ctx, key, NULL, 0, 0)) {
846 4 ret = av_opt_set(ctx, key, value, 0);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
848 av_free(value);
849 av_free(parsed_key);
850 return ret;
851 }
852 } else {
853 49045 o = av_opt_find(ctx->priv, key, NULL, 0,
854 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49045 times.
49045 if (!o) {
856 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
857 av_free(value);
858 av_free(parsed_key);
859 return AVERROR_OPTION_NOT_FOUND;
860 }
861 49045 av_dict_set(options, key, value,
862
2/2
✓ Branch 0 taken 3366 times.
✓ Branch 1 taken 45679 times.
49045 (o->type == AV_OPT_TYPE_FLAGS &&
863
3/4
✓ Branch 0 taken 3366 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3119 times.
✓ Branch 3 taken 247 times.
3366 (value[0] == '-' || value[0] == '+')) ? AV_DICT_APPEND : 0);
864 }
865
866 49049 av_free(value);
867 49049 av_free(parsed_key);
868 }
869
870 20301 return 0;
871 }
872
873 int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
874 const char *arg, char *res, int res_len, int flags)
875 {
876 const AVOption *o;
877
878 if (!ctx->filter->priv_class)
879 return 0;
880 o = av_opt_find2(ctx->priv, cmd, NULL, AV_OPT_FLAG_RUNTIME_PARAM | AV_OPT_FLAG_FILTERING_PARAM, AV_OPT_SEARCH_CHILDREN, NULL);
881 if (!o)
882 return AVERROR(ENOSYS);
883 return av_opt_set(ctx->priv, cmd, arg, 0);
884 }
885
886 31668 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
887 {
888 31668 int ret = 0;
889
890 31668 ret = av_opt_set_dict(ctx, options);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (ret < 0) {
892 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
893 return ret;
894 }
895
896
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 31068 times.
31668 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
897
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 458 times.
600 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
898
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
142 ctx->graph->internal->thread_execute) {
899 142 ctx->thread_type = AVFILTER_THREAD_SLICE;
900 142 ctx->internal->execute = ctx->graph->internal->thread_execute;
901 } else {
902 31526 ctx->thread_type = 0;
903 }
904
905
2/2
✓ Branch 0 taken 28289 times.
✓ Branch 1 taken 3379 times.
31668 if (ctx->filter->priv_class) {
906 28289 ret = av_opt_set_dict2(ctx->priv, options, AV_OPT_SEARCH_CHILDREN);
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28289 times.
28289 if (ret < 0) {
908 av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
909 return ret;
910 }
911 }
912
913
2/2
✓ Branch 0 taken 21612 times.
✓ Branch 1 taken 10056 times.
31668 if (ctx->filter->init)
914 21612 ret = ctx->filter->init(ctx);
915
2/2
✓ Branch 0 taken 3731 times.
✓ Branch 1 taken 6325 times.
10056 else if (ctx->filter->init_dict)
916 3731 ret = ctx->filter->init_dict(ctx, options);
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (ret < 0)
918 return ret;
919
920
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31664 times.
31668 if (ctx->enable_str) {
921 4 ret = set_enable_expr(ctx, ctx->enable_str);
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
923 return ret;
924 }
925
926 31668 return 0;
927 }
928
929 31668 int avfilter_init_str(AVFilterContext *filter, const char *args)
930 {
931 31668 AVDictionary *options = NULL;
932 AVDictionaryEntry *e;
933 31668 int ret = 0;
934
935
4/4
✓ Branch 0 taken 21468 times.
✓ Branch 1 taken 10200 times.
✓ Branch 2 taken 20301 times.
✓ Branch 3 taken 1167 times.
31668 if (args && *args) {
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20301 times.
20301 if (!filter->filter->priv_class) {
937 av_log(filter, AV_LOG_ERROR, "This filter does not take any "
938 "options, but options were provided: %s.\n", args);
939 return AVERROR(EINVAL);
940 }
941
942 20301 ret = process_options(filter, &options, args);
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20301 times.
20301 if (ret < 0)
944 goto fail;
945 }
946
947 31668 ret = avfilter_init_dict(filter, &options);
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31668 times.
31668 if (ret < 0)
949 goto fail;
950
951
1/2
✓ Branch 1 taken 31668 times.
✗ Branch 2 not taken.
31668 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
952 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
953 ret = AVERROR_OPTION_NOT_FOUND;
954 goto fail;
955 }
956
957 31668 fail:
958 31668 av_dict_free(&options);
959
960 31668 return ret;
961 }
962
963 271 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
964 {
965 271 return pads[pad_idx].name;
966 }
967
968 25187 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
969 {
970 25187 return pads[pad_idx].type;
971 }
972
973 667015 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
974 {
975 667015 return ff_filter_frame(link->dst->outputs[0], frame);
976 }
977
978 981455 static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
979 {
980 int (*filter_frame)(AVFilterLink *, AVFrame *);
981 981455 AVFilterContext *dstctx = link->dst;
982 981455 AVFilterPad *dst = link->dstpad;
983 int ret;
984
985
2/2
✓ Branch 0 taken 667006 times.
✓ Branch 1 taken 314449 times.
981455 if (!(filter_frame = dst->filter_frame))
986 667006 filter_frame = default_filter_frame;
987
988
2/2
✓ Branch 0 taken 2243 times.
✓ Branch 1 taken 979212 times.
981455 if (dst->flags & AVFILTERPAD_FLAG_NEEDS_WRITABLE) {
989 2243 ret = ff_inlink_make_frame_writable(link, &frame);
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2243 times.
2243 if (ret < 0)
991 goto fail;
992 }
993
994 981455 ff_inlink_process_commands(link, frame);
995 981455 dstctx->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
996
997
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 981436 times.
981455 if (dstctx->is_disabled &&
998
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
999 9 filter_frame = default_filter_frame;
1000 981455 ret = filter_frame(link, frame);
1001 981455 link->frame_count_out++;
1002 981455 return ret;
1003
1004 fail:
1005 av_frame_free(&frame);
1006 return ret;
1007 }
1008
1009 1404555 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
1010 {
1011 int ret;
1012 1404555 FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); tlog_ref(NULL, frame, 1);
1013
1014 /* Consistency checks */
1015
2/2
✓ Branch 0 taken 323232 times.
✓ Branch 1 taken 1081323 times.
1404555 if (link->type == AVMEDIA_TYPE_VIDEO) {
1016
2/2
✓ Branch 0 taken 218698 times.
✓ Branch 1 taken 104534 times.
323232 if (strcmp(link->dst->filter->name, "buffersink") &&
1017
2/2
✓ Branch 0 taken 147714 times.
✓ Branch 1 taken 70984 times.
218698 strcmp(link->dst->filter->name, "format") &&
1018
2/2
✓ Branch 0 taken 147688 times.
✓ Branch 1 taken 26 times.
147714 strcmp(link->dst->filter->name, "idet") &&
1019
2/2
✓ Branch 0 taken 63532 times.
✓ Branch 1 taken 84156 times.
147688 strcmp(link->dst->filter->name, "null") &&
1020 63532 strcmp(link->dst->filter->name, "scale")) {
1021 av_assert1(frame->format == link->format);
1022 av_assert1(frame->width == link->w);
1023 av_assert1(frame->height == link->h);
1024 }
1025 } else {
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1081323 times.
1081323 if (frame->format != link->format) {
1027 av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
1028 goto error;
1029 }
1030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1081323 times.
1081323 if (av_channel_layout_compare(&frame->ch_layout, &link->ch_layout)) {
1031 av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
1032 goto error;
1033 }
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1081323 times.
1081323 if (frame->sample_rate != link->sample_rate) {
1035 av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
1036 goto error;
1037 }
1038 }
1039
1040 1404555 link->frame_blocked_in = link->frame_wanted_out = 0;
1041 1404555 link->frame_count_in++;
1042 1404555 link->sample_count_in += frame->nb_samples;
1043 1404555 filter_unblock(link->dst);
1044 1404555 ret = ff_framequeue_add(&link->fifo, frame);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1404555 times.
1404555 if (ret < 0) {
1046 av_frame_free(&frame);
1047 return ret;
1048 }
1049 1404555 ff_filter_set_ready(link->dst, 300);
1050 1404555 return 0;
1051
1052 error:
1053 av_frame_free(&frame);
1054 return AVERROR_PATCHWELCOME;
1055 }
1056
1057 2901910 static int samples_ready(AVFilterLink *link, unsigned min)
1058 {
1059
2/2
✓ Branch 1 taken 981755 times.
✓ Branch 2 taken 1920155 times.
3883665 return ff_framequeue_queued_frames(&link->fifo) &&
1060
2/2
✓ Branch 1 taken 301 times.
✓ Branch 2 taken 981454 times.
981755 (ff_framequeue_queued_samples(&link->fifo) >= min ||
1061
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 300 times.
301 link->status_in);
1062 }
1063
1064 72275 static int take_samples(AVFilterLink *link, unsigned min, unsigned max,
1065 AVFrame **rframe)
1066 {
1067 AVFrame *frame0, *frame, *buf;
1068 unsigned nb_samples, nb_frames, i, p;
1069 int ret;
1070
1071 /* Note: this function relies on no format changes and must only be
1072 called with enough samples. */
1073 av_assert1(samples_ready(link, link->min_samples));
1074 72275 frame0 = frame = ff_framequeue_peek(&link->fifo, 0);
1075
6/6
✓ Branch 0 taken 12616 times.
✓ Branch 1 taken 59659 times.
✓ Branch 2 taken 9406 times.
✓ Branch 3 taken 3210 times.
✓ Branch 4 taken 6311 times.
✓ Branch 5 taken 3095 times.
72275 if (!link->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
1076 6311 *rframe = ff_framequeue_take(&link->fifo);
1077 6311 return 0;
1078 }
1079 65964 nb_frames = 0;
1080 65964 nb_samples = 0;
1081 while (1) {
1082
2/2
✓ Branch 0 taken 59662 times.
✓ Branch 1 taken 43321 times.
102983 if (nb_samples + frame->nb_samples > max) {
1083
1/2
✓ Branch 0 taken 59662 times.
✗ Branch 1 not taken.
59662 if (nb_samples < min)
1084 59662 nb_samples = max;
1085 59662 break;
1086 }
1087 43321 nb_samples += frame->nb_samples;
1088 43321 nb_frames++;
1089
2/2
✓ Branch 1 taken 6302 times.
✓ Branch 2 taken 37019 times.
43321 if (nb_frames == ff_framequeue_queued_frames(&link->fifo))
1090 6302 break;
1091 37019 frame = ff_framequeue_peek(&link->fifo, nb_frames);
1092 }
1093
1094 65964 buf = ff_get_audio_buffer(link, nb_samples);
1095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65964 times.
65964 if (!buf)
1096 return AVERROR(ENOMEM);
1097 65964 ret = av_frame_copy_props(buf, frame0);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65964 times.
65964 if (ret < 0) {
1099 av_frame_free(&buf);
1100 return ret;
1101 }
1102
1103 65964 p = 0;
1104
2/2
✓ Branch 0 taken 43321 times.
✓ Branch 1 taken 65964 times.
109285 for (i = 0; i < nb_frames; i++) {
1105 43321 frame = ff_framequeue_take(&link->fifo);
1106 43321 av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
1107 43321 frame->nb_samples, link->ch_layout.nb_channels, link->format);
1108 43321 p += frame->nb_samples;
1109 43321 av_frame_free(&frame);
1110 }
1111
2/2
✓ Branch 0 taken 59662 times.
✓ Branch 1 taken 6302 times.
65964 if (p < nb_samples) {
1112 59662 unsigned n = nb_samples - p;
1113 59662 frame = ff_framequeue_peek(&link->fifo, 0);
1114 59662 av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
1115 59662 link->ch_layout.nb_channels, link->format);
1116 59662 ff_framequeue_skip_samples(&link->fifo, n, link->time_base);
1117 }
1118
1119 65964 *rframe = buf;
1120 65964 return 0;
1121 }
1122
1123 981455 static int ff_filter_frame_to_filter(AVFilterLink *link)
1124 {
1125 981455 AVFrame *frame = NULL;
1126 981455 AVFilterContext *dst = link->dst;
1127 int ret;
1128
1129 av_assert1(ff_framequeue_queued_frames(&link->fifo));
1130 1962910 ret = link->min_samples ?
1131
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 981419 times.
981455 ff_inlink_consume_samples(link, link->min_samples, link->max_samples, &frame) :
1132 981419 ff_inlink_consume_frame(link, &frame);
1133 av_assert1(ret);
1134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 981455 times.
981455 if (ret < 0) {
1135 av_assert1(!frame);
1136 return ret;
1137 }
1138 /* The filter will soon have received a new frame, that may allow it to
1139 produce one or more: unblock its outputs. */
1140 981455 filter_unblock(dst);
1141 /* AVFilterPad.filter_frame() expect frame_count_out to have the value
1142 before the frame; ff_filter_frame_framed() will re-increment it. */
1143 981455 link->frame_count_out--;
1144 981455 ret = ff_filter_frame_framed(link, frame);
1145
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 981455 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
981455 if (ret < 0 && ret != link->status_out) {
1146 ff_avfilter_link_set_out_status(link, ret, AV_NOPTS_VALUE);
1147 } else {
1148 /* Run once again, to see if several frames were available, or if
1149 the input status has also changed, or any other reason. */
1150 981455 ff_filter_set_ready(dst, 300);
1151 }
1152 981455 return ret;
1153 }
1154
1155 17956 static int forward_status_change(AVFilterContext *filter, AVFilterLink *in)
1156 {
1157 17956 unsigned out = 0, progress = 0;
1158 int ret;
1159
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17956 times.
17956 av_assert0(!in->status_out);
1161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17956 times.
17956 if (!filter->nb_outputs) {
1162 /* not necessary with the current API and sinks */
1163 return 0;
1164 }
1165
2/2
✓ Branch 0 taken 17957 times.
✓ Branch 1 taken 17954 times.
35911 while (!in->status_out) {
1166
2/2
✓ Branch 0 taken 17955 times.
✓ Branch 1 taken 2 times.
17957 if (!filter->outputs[out]->status_in) {
1167 17955 progress++;
1168 17955 ret = ff_request_frame_to_filter(filter->outputs[out]);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17955 times.
17955 if (ret < 0)
1170 return ret;
1171 }
1172
1/2
✓ Branch 0 taken 17957 times.
✗ Branch 1 not taken.
17957 if (++out == filter->nb_outputs) {
1173
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17955 times.
17957 if (!progress) {
1174 /* Every output already closed: input no longer interesting
1175 (example: overlay in shortest mode, other input closed). */
1176 2 ff_avfilter_link_set_out_status(in, in->status_in, in->status_in_pts);
1177 2 return 0;
1178 }
1179 17955 progress = 0;
1180 17955 out = 0;
1181 }
1182 }
1183 17954 ff_filter_set_ready(filter, 200);
1184 17954 return 0;
1185 }
1186
1187 3276412 static int ff_filter_activate_default(AVFilterContext *filter)
1188 {
1189 unsigned i;
1190
1191
2/2
✓ Branch 0 taken 2901910 times.
✓ Branch 1 taken 2294957 times.
5196867 for (i = 0; i < filter->nb_inputs; i++) {
1192
2/2
✓ Branch 1 taken 981455 times.
✓ Branch 2 taken 1920455 times.
2901910 if (samples_ready(filter->inputs[i], filter->inputs[i]->min_samples)) {
1193 981455 return ff_filter_frame_to_filter(filter->inputs[i]);
1194 }
1195 }
1196
2/2
✓ Branch 0 taken 1920450 times.
✓ Branch 1 taken 2277001 times.
4197451 for (i = 0; i < filter->nb_inputs; i++) {
1197
4/4
✓ Branch 0 taken 47464 times.
✓ Branch 1 taken 1872986 times.
✓ Branch 2 taken 17956 times.
✓ Branch 3 taken 29508 times.
1920450 if (filter->inputs[i]->status_in && !filter->inputs[i]->status_out) {
1198 av_assert1(!ff_framequeue_queued_frames(&filter->inputs[i]->fifo));
1199 17956 return forward_status_change(filter, filter->inputs[i]);
1200 }
1201 }
1202
2/2
✓ Branch 0 taken 2277006 times.
✓ Branch 1 taken 1002689 times.
3279695 for (i = 0; i < filter->nb_outputs; i++) {
1203
2/2
✓ Branch 0 taken 1274478 times.
✓ Branch 1 taken 1002528 times.
2277006 if (filter->outputs[i]->frame_wanted_out &&
1204
2/2
✓ Branch 0 taken 1274312 times.
✓ Branch 1 taken 166 times.
1274478 !filter->outputs[i]->frame_blocked_in) {
1205 1274312 return ff_request_frame_to_filter(filter->outputs[i]);
1206 }
1207 }
1208 1002689 return FFERROR_NOT_READY;
1209 }
1210
1211 /*
1212 Filter scheduling and activation
1213
1214 When a filter is activated, it must:
1215 - if possible, output a frame;
1216 - else, if relevant, forward the input status change;
1217 - else, check outputs for wanted frames and forward the requests.
1218
1219 The following AVFilterLink fields are used for activation:
1220
1221 - frame_wanted_out:
1222
1223 This field indicates if a frame is needed on this input of the
1224 destination filter. A positive value indicates that a frame is needed
1225 to process queued frames or internal data or to satisfy the
1226 application; a zero value indicates that a frame is not especially
1227 needed but could be processed anyway; a negative value indicates that a
1228 frame would just be queued.
1229
1230 It is set by filters using ff_request_frame() or ff_request_no_frame(),
1231 when requested by the application through a specific API or when it is
1232 set on one of the outputs.
1233
1234 It is cleared when a frame is sent from the source using
1235 ff_filter_frame().
1236
1237 It is also cleared when a status change is sent from the source using
1238 ff_avfilter_link_set_in_status().
1239
1240 - frame_blocked_in:
1241
1242 This field means that the source filter can not generate a frame as is.
1243 Its goal is to avoid repeatedly calling the request_frame() method on
1244 the same link.
1245
1246 It is set by the framework on all outputs of a filter before activating it.
1247
1248 It is automatically cleared by ff_filter_frame().
1249
1250 It is also automatically cleared by ff_avfilter_link_set_in_status().
1251
1252 It is also cleared on all outputs (using filter_unblock()) when
1253 something happens on an input: processing a frame or changing the
1254 status.
1255
1256 - fifo:
1257
1258 Contains the frames queued on a filter input. If it contains frames and
1259 frame_wanted_out is not set, then the filter can be activated. If that
1260 result in the filter not able to use these frames, the filter must set
1261 frame_wanted_out to ask for more frames.
1262
1263 - status_in and status_in_pts:
1264
1265 Status (EOF or error code) of the link and timestamp of the status
1266 change (in link time base, same as frames) as seen from the input of
1267 the link. The status change is considered happening after the frames
1268 queued in fifo.
1269
1270 It is set by the source filter using ff_avfilter_link_set_in_status().
1271
1272 - status_out:
1273
1274 Status of the link as seen from the output of the link. The status
1275 change is considered having already happened.
1276
1277 It is set by the destination filter using
1278 ff_avfilter_link_set_out_status().
1279
1280 Filters are activated according to the ready field, set using the
1281 ff_filter_set_ready(). Eventually, a priority queue will be used.
1282 ff_filter_set_ready() is called whenever anything could cause progress to
1283 be possible. Marking a filter ready when it is not is not a problem,
1284 except for the small overhead it causes.
1285
1286 Conditions that cause a filter to be marked ready are:
1287
1288 - frames added on an input link;
1289
1290 - changes in the input or output status of an input link;
1291
1292 - requests for a frame on an output link;
1293
1294 - after any actual processing using the legacy methods (filter_frame(),
1295 and request_frame() to acknowledge status changes), to run once more
1296 and check if enough input was present for several frames.
1297
1298 Examples of scenarios to consider:
1299
1300 - buffersrc: activate if frame_wanted_out to notify the application;
1301 activate when the application adds a frame to push it immediately.
1302
1303 - testsrc: activate only if frame_wanted_out to produce and push a frame.
1304
1305 - concat (not at stitch points): can process a frame on any output.
1306 Activate if frame_wanted_out on output to forward on the corresponding
1307 input. Activate when a frame is present on input to process it
1308 immediately.
1309
1310 - framesync: needs at least one frame on each input; extra frames on the
1311 wrong input will accumulate. When a frame is first added on one input,
1312 set frame_wanted_out<0 on it to avoid getting more (would trigger
1313 testsrc) and frame_wanted_out>0 on the other to allow processing it.
1314
1315 Activation of old filters:
1316
1317 In order to activate a filter implementing the legacy filter_frame() and
1318 request_frame() methods, perform the first possible of the following
1319 actions:
1320
1321 - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
1322 frame and call filter_frame().
1323
1324 Rationale: filter frames as soon as possible instead of leaving them
1325 queued; frame_wanted_out < 0 is not possible since the old API does not
1326 set it nor provides any similar feedback; frame_wanted_out > 0 happens
1327 when min_samples > 0 and there are not enough samples queued.
1328
1329 - If an input has status_in set but not status_out, try to call
1330 request_frame() on one of the outputs in the hope that it will trigger
1331 request_frame() on the input with status_in and acknowledge it. This is
1332 awkward and fragile, filters with several inputs or outputs should be
1333 updated to direct activation as soon as possible.
1334
1335 - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
1336 request_frame().
1337
1338 Rationale: checking frame_blocked_in is necessary to avoid requesting
1339 repeatedly on a blocked input if another is not blocked (example:
1340 [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
1341 */
1342
1343 3727340 int ff_filter_activate(AVFilterContext *filter)
1344 {
1345 int ret;
1346
1347 /* Generic timeline support is not yet implemented but should be easy */
1348 av_assert1(!(filter->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
1349 filter->filter->activate));
1350 3727340 filter->ready = 0;
1351
2/2
✓ Branch 0 taken 450928 times.
✓ Branch 1 taken 3276412 times.
3727340 ret = filter->filter->activate ? filter->filter->activate(filter) :
1352 3276412 ff_filter_activate_default(filter);
1353
2/2
✓ Branch 0 taken 1003595 times.
✓ Branch 1 taken 2723745 times.
3727340 if (ret == FFERROR_NOT_READY)
1354 1003595 ret = 0;
1355 3727340 return ret;
1356 }
1357
1358 2140703 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
1359 {
1360 2140703 *rpts = link->current_pts;
1361
2/2
✓ Branch 1 taken 205740 times.
✓ Branch 2 taken 1934963 times.
2140703 if (ff_framequeue_queued_frames(&link->fifo))
1362 205740 return *rstatus = 0;
1363
2/2
✓ Branch 0 taken 5440 times.
✓ Branch 1 taken 1929523 times.
1934963 if (link->status_out)
1364 5440 return *rstatus = link->status_out;
1365
2/2
✓ Branch 0 taken 1925668 times.
✓ Branch 1 taken 3855 times.
1929523 if (!link->status_in)
1366 1925668 return *rstatus = 0;
1367 3855 *rstatus = link->status_out = link->status_in;
1368 3855 ff_update_link_current_pts(link, link->status_in_pts);
1369 3855 *rpts = link->current_pts;
1370 3855 return 1;
1371 }
1372
1373 1798 size_t ff_inlink_queued_frames(AVFilterLink *link)
1374 {
1375 1798 return ff_framequeue_queued_frames(&link->fifo);
1376 }
1377
1378 3220139 int ff_inlink_check_available_frame(AVFilterLink *link)
1379 {
1380 3220139 return ff_framequeue_queued_frames(&link->fifo) > 0;
1381 }
1382
1383 2687 int ff_inlink_queued_samples(AVFilterLink *link)
1384 {
1385 2687 return ff_framequeue_queued_samples(&link->fifo);
1386 }
1387
1388 343167 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
1389 {
1390 343167 uint64_t samples = ff_framequeue_queued_samples(&link->fifo);
1391 av_assert1(min);
1392
6/6
✓ Branch 0 taken 271003 times.
✓ Branch 1 taken 72164 times.
✓ Branch 2 taken 450 times.
✓ Branch 3 taken 270553 times.
✓ Branch 4 taken 111 times.
✓ Branch 5 taken 339 times.
343167 return samples >= min || (link->status_in && samples);
1393 }
1394
1395 1427157 static void consume_update(AVFilterLink *link, const AVFrame *frame)
1396 {
1397 1427157 ff_update_link_current_pts(link, frame->pts);
1398 1427157 ff_inlink_process_commands(link, frame);
1399 1427157 link->dst->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
1400 1427157 link->frame_count_out++;
1401 1427157 link->sample_count_out += frame->nb_samples;
1402 1427157 }
1403
1404 3218171 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
1405 {
1406 AVFrame *frame;
1407
1408 3218171 *rframe = NULL;
1409
2/2
✓ Branch 1 taken 1863288 times.
✓ Branch 2 taken 1354883 times.
3218171 if (!ff_inlink_check_available_frame(link))
1410 1863288 return 0;
1411
1412
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1354882 times.
1354883 if (link->fifo.samples_skipped) {
1413 1 frame = ff_framequeue_peek(&link->fifo, 0);
1414 1 return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
1415 }
1416
1417 1354882 frame = ff_framequeue_take(&link->fifo);
1418 1354882 consume_update(link, frame);
1419 1354882 *rframe = frame;
1420 1354882 return 1;
1421 }
1422
1423 343167 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
1424 AVFrame **rframe)
1425 {
1426 AVFrame *frame;
1427 int ret;
1428
1429 av_assert1(min);
1430 343167 *rframe = NULL;
1431
2/2
✓ Branch 1 taken 270892 times.
✓ Branch 2 taken 72275 times.
343167 if (!ff_inlink_check_available_samples(link, min))
1432 270892 return 0;
1433
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 72163 times.
72275 if (link->status_in)
1434
2/2
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 1 times.
112 min = FFMIN(min, ff_framequeue_queued_samples(&link->fifo));
1435 72275 ret = take_samples(link, min, max, &frame);
1436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72275 times.
72275 if (ret < 0)
1437 return ret;
1438 72275 consume_update(link, frame);
1439 72275 *rframe = frame;
1440 72275 return 1;
1441 }
1442
1443 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
1444 {
1445 return ff_framequeue_peek(&link->fifo, idx);
1446 }
1447
1448 3456 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
1449 {
1450 3456 AVFrame *frame = *rframe;
1451 AVFrame *out;
1452 int ret;
1453
1454
2/2
✓ Branch 1 taken 3376 times.
✓ Branch 2 taken 80 times.
3456 if (av_frame_is_writable(frame))
1455 3376 return 0;
1456 80 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
1457
1458
1/3
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
80 switch (link->type) {
1459 80 case AVMEDIA_TYPE_VIDEO:
1460 80 out = ff_get_video_buffer(link, link->w, link->h);
1461 80 break;
1462 case AVMEDIA_TYPE_AUDIO:
1463 out = ff_get_audio_buffer(link, frame->nb_samples);
1464 break;
1465 default:
1466 return AVERROR(EINVAL);
1467 }
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (!out)
1469 return AVERROR(ENOMEM);
1470
1471 80 ret = av_frame_copy_props(out, frame);
1472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ret < 0) {
1473 av_frame_free(&out);
1474 return ret;
1475 }
1476
1477 80 ret = av_frame_copy(out, frame);
1478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ret < 0) {
1479 av_frame_free(&out);
1480 return ret;
1481 }
1482
1483 80 av_frame_free(&frame);
1484 80 *rframe = out;
1485 80 return 0;
1486 }
1487
1488 2408612 int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
1489 {
1490 2408612 AVFilterCommand *cmd = link->dst->command_queue;
1491
1492
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2408612 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2408612 while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
1493 av_log(link->dst, AV_LOG_DEBUG,
1494 "Processing command time:%f command:%s arg:%s\n",
1495 cmd->time, cmd->command, cmd->arg);
1496 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
1497 command_queue_pop(link->dst);
1498 cmd= link->dst->command_queue;
1499 }
1500 2408612 return 0;
1501 }
1502
1503 2408612 int ff_inlink_evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
1504 {
1505 2408612 AVFilterContext *dstctx = link->dst;
1506 2408612 int64_t pts = frame->pts;
1507 2408612 int64_t pos = frame->pkt_pos;
1508
1509
2/2
✓ Branch 0 taken 2408416 times.
✓ Branch 1 taken 196 times.
2408612 if (!dstctx->enable_str)
1510 2408416 return 1;
1511
1512 196 dstctx->var_values[VAR_N] = link->frame_count_out;
1513
1/2
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
196 dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
1514 196 dstctx->var_values[VAR_W] = link->w;
1515 196 dstctx->var_values[VAR_H] = link->h;
1516
1/2
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
196 dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
1517
1518 196 return fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) >= 0.5;
1519 }
1520
1521 389713 void ff_inlink_request_frame(AVFilterLink *link)
1522 {
1523 av_assert1(!link->status_in);
1524 av_assert1(!link->status_out);
1525 389713 link->frame_wanted_out = 1;
1526 389713 ff_filter_set_ready(link->src, 100);
1527 389713 }
1528
1529 121 void ff_inlink_set_status(AVFilterLink *link, int status)
1530 {
1531
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 9 times.
121 if (link->status_out)
1532 112 return;
1533 9 link->frame_wanted_out = 0;
1534 9 link->frame_blocked_in = 0;
1535 9 ff_avfilter_link_set_out_status(link, status, AV_NOPTS_VALUE);
1536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 while (ff_framequeue_queued_frames(&link->fifo)) {
1537 AVFrame *frame = ff_framequeue_take(&link->fifo);
1538 av_frame_free(&frame);
1539 }
1540
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (!link->status_in)
1541 3 link->status_in = status;
1542 }
1543
1544 29368 int ff_outlink_get_status(AVFilterLink *link)
1545 {
1546 29368 return link->status_in;
1547 }
1548
1549 1443 int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
1550 {
1551
1/2
✓ Branch 1 taken 958 times.
✗ Branch 2 not taken.
2401 return ff_outlink_frame_wanted(outlink) ||
1552
2/2
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 485 times.
2401 ff_inlink_check_available_frame(inlink) ||
1553
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 928 times.
958 inlink->status_out;
1554 }
1555
1556
1557 const AVClass *avfilter_get_class(void)
1558 {
1559 return &avfilter_class;
1560 }
1561
1562 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
1563 int default_pool_size)
1564 {
1565 AVHWFramesContext *frames;
1566
1567 // Must already be set by caller.
1568 av_assert0(link->hw_frames_ctx);
1569
1570 frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
1571
1572 if (frames->initial_pool_size == 0) {
1573 // Dynamic allocation is necessarily supported.
1574 } else if (avctx->extra_hw_frames >= 0) {
1575 frames->initial_pool_size += avctx->extra_hw_frames;
1576 } else {
1577 frames->initial_pool_size = default_pool_size;
1578 }
1579
1580 return 0;
1581 }
1582