FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_filter.c
Date: 2026-08-26 16:21:34
Exec Total Coverage
Lines: 1449 1951 74.3%
Functions: 72 77 93.5%
Branches: 858 1333 64.4%

Line Branch Exec Source
1 /*
2 * ffmpeg filter configuration
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 #include <stdint.h>
22
23 #include "ffmpeg.h"
24 #include "graph/graphprint.h"
25
26 #include "libavfilter/avfilter.h"
27 #include "libavfilter/buffersink.h"
28 #include "libavfilter/buffersrc.h"
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/downmix_info.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/pixfmt.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/time.h"
42 #include "libavutil/timestamp.h"
43
44 typedef struct FilterGraphPriv {
45 FilterGraph fg;
46
47 // name used for logging
48 char log_name[32];
49
50 int is_simple;
51 // true when the filtergraph contains only meta filters
52 // that do not modify the frame data
53 int is_meta;
54 int disable_conversions;
55
56 unsigned nb_outputs_done;
57
58 int nb_threads;
59
60 // frame for temporarily holding output from the filtergraph
61 AVFrame *frame;
62 // frame for sending output to the encoder
63 AVFrame *frame_enc;
64
65 Scheduler *sch;
66 unsigned sch_idx;
67 } FilterGraphPriv;
68
69 2305386 static FilterGraphPriv *fgp_from_fg(FilterGraph *fg)
70 {
71 2305386 return (FilterGraphPriv*)fg;
72 }
73
74 39077 static const FilterGraphPriv *cfgp_from_cfg(const FilterGraph *fg)
75 {
76 39077 return (const FilterGraphPriv*)fg;
77 }
78
79 // data that is local to the filter thread and not visible outside of it
80 typedef struct FilterGraphThread {
81 AVFilterGraph *graph;
82
83 AVFrame *frame;
84
85 // Temporary buffer for output frames, since on filtergraph reset
86 // we cannot send them to encoders immediately.
87 // The output index is stored in frame opaque.
88 AVFifo *frame_queue_out;
89
90 // index of the next input to request from the scheduler
91 unsigned next_in;
92 // set to 1 after at least one frame passed through this output
93 int got_frame;
94
95 // EOF status of each input/output, as received by the thread
96 uint8_t *eof_in;
97 uint8_t *eof_out;
98 } FilterGraphThread;
99
100 typedef struct InputFilterPriv {
101 InputFilter ifilter;
102
103 InputFilterOptions opts;
104
105 // used to hold submitted input
106 AVFrame *frame;
107
108 // For inputs bound to a filtergraph output
109 OutputFilter *ofilter_src;
110
111 // source data type: AVMEDIA_TYPE_SUBTITLE for sub2video,
112 // same as type otherwise
113 enum AVMediaType type_src;
114
115 int eof;
116 int bound;
117 int drop_warned;
118 uint64_t nb_dropped;
119
120 // parameters configured for this input
121 int format;
122
123 int width, height;
124 AVRational sample_aspect_ratio;
125 enum AVColorSpace color_space;
126 enum AVColorRange color_range;
127 enum AVAlphaMode alpha_mode;
128
129 int sample_rate;
130 AVChannelLayout ch_layout;
131
132 AVRational time_base;
133
134 AVFrameSideData **side_data;
135 int nb_side_data;
136
137 AVFifo *frame_queue;
138
139 AVBufferRef *hw_frames_ctx;
140
141 int displaymatrix_present;
142 int displaymatrix_applied;
143 int32_t displaymatrix[9];
144
145 int downmixinfo_present;
146 AVDownmixInfo downmixinfo;
147
148 AVBufferRef *downmixmatrix;
149 size_t downmixmatrix_size;
150 int downmixmatrix_present;
151
152 struct {
153 AVFrame *frame;
154
155 int64_t last_pts;
156 int64_t end_pts;
157
158 /// marks if sub2video_update should force an initialization
159 unsigned int initialize;
160 } sub2video;
161 } InputFilterPriv;
162
163 1346781 static InputFilterPriv *ifp_from_ifilter(InputFilter *ifilter)
164 {
165 1346781 return (InputFilterPriv*)ifilter;
166 }
167
168 typedef struct FPSConvContext {
169 AVFrame *last_frame;
170 /* number of frames emitted by the video-encoding sync code */
171 int64_t frame_number;
172 /* history of nb_frames_prev, i.e. the number of times the
173 * previous frame was duplicated by vsync code in recent
174 * do_video_out() calls */
175 int64_t frames_prev_hist[3];
176
177 uint64_t dup_warning;
178
179 int last_dropped;
180 int dropped_keyframe;
181
182 enum VideoSyncMethod vsync_method;
183
184 AVRational framerate;
185 AVRational framerate_max;
186 const AVRational *framerate_supported;
187 int framerate_clip;
188 } FPSConvContext;
189
190 typedef struct ReinitOpts {
191 int64_t pts;
192 AVDictionary *dict;
193 } ReinitOpts;
194
195 typedef struct OutputFilterPriv {
196 OutputFilter ofilter;
197
198 void *log_parent;
199 char log_name[32];
200
201 int needed;
202
203 /* desired output stream properties */
204 int format;
205 int width, height;
206 int sample_rate;
207 AVChannelLayout ch_layout;
208 enum AVColorSpace color_space;
209 enum AVColorRange color_range;
210 enum AVAlphaMode alpha_mode;
211
212 unsigned crop_top;
213 unsigned crop_bottom;
214 unsigned crop_left;
215 unsigned crop_right;
216
217 AVFrameSideData **side_data;
218 int nb_side_data;
219
220 // time base in which the output is sent to our downstream
221 // does not need to match the filtersink's timebase
222 AVRational tb_out;
223 // at least one frame with the above timebase was sent
224 // to our downstream, so it cannot change anymore
225 int tb_out_locked;
226
227 AVRational sample_aspect_ratio;
228
229 AVDictionary *sws_opts;
230 AVDictionary *swr_opts;
231
232 // those are only set if no format is specified and the encoder gives us multiple options
233 // They point directly to the relevant lists of the encoder.
234 union {
235 const enum AVPixelFormat *pix_fmts;
236 const enum AVSampleFormat *sample_fmts;
237 };
238 const AVChannelLayout *ch_layouts;
239 const int *sample_rates;
240 const enum AVColorSpace *color_spaces;
241 const enum AVColorRange *color_ranges;
242 const enum AVAlphaMode *alpha_modes;
243
244 int32_t displaymatrix[9];
245
246 AVRational enc_timebase;
247 int64_t trim_start_us;
248 int64_t trim_duration_us;
249 // offset for output timestamps, in AV_TIME_BASE_Q
250 int64_t ts_offset;
251 int64_t next_pts;
252 FPSConvContext fps;
253
254 AVFifo *reinit_opts_fifo;
255 ReinitOpts reinit_opts;
256
257 unsigned flags;
258 } OutputFilterPriv;
259
260 968677 static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter)
261 {
262 968677 return (OutputFilterPriv*)ofilter;
263 }
264
265 typedef struct FilterCommand {
266 char *target;
267 char *command;
268 char *arg;
269
270 double time;
271 int all_filters;
272 } FilterCommand;
273
274 static void filter_command_free(void *opaque, uint8_t *data)
275 {
276 FilterCommand *fc = (FilterCommand*)data;
277
278 av_freep(&fc->target);
279 av_freep(&fc->command);
280 av_freep(&fc->arg);
281
282 av_free(data);
283 }
284
285 180 static int sub2video_get_blank_frame(InputFilterPriv *ifp)
286 {
287 180 AVFrame *frame = ifp->sub2video.frame;
288 int ret;
289
290 180 av_frame_unref(frame);
291
292 180 frame->width = ifp->width;
293 180 frame->height = ifp->height;
294 180 frame->format = ifp->format;
295 180 frame->colorspace = ifp->color_space;
296 180 frame->color_range = ifp->color_range;
297 180 frame->alpha_mode = ifp->alpha_mode;
298
299 180 ret = av_frame_get_buffer(frame, 0);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
180 if (ret < 0)
301 return ret;
302
303 180 memset(frame->data[0], 0, frame->height * frame->linesize[0]);
304
305 180 return 0;
306 }
307
308 89 static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
309 AVSubtitleRect *r)
310 {
311 uint32_t *pal, *dst2;
312 uint8_t *src, *src2;
313 int x, y;
314
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (r->type != SUBTITLE_BITMAP) {
316 av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
317 return;
318 }
319
4/8
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 89 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 89 times.
89 if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
320 av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
321 r->x, r->y, r->w, r->h, w, h
322 );
323 return;
324 }
325
326 89 dst += r->y * dst_linesize + r->x * 4;
327 89 src = r->data[0];
328 89 pal = (uint32_t *)r->data[1];
329
2/2
✓ Branch 0 taken 3291 times.
✓ Branch 1 taken 89 times.
3380 for (y = 0; y < r->h; y++) {
330 3291 dst2 = (uint32_t *)dst;
331 3291 src2 = src;
332
2/2
✓ Branch 0 taken 1036075 times.
✓ Branch 1 taken 3291 times.
1039366 for (x = 0; x < r->w; x++)
333 1036075 *(dst2++) = pal[*(src2++)];
334 3291 dst += dst_linesize;
335 3291 src += r->linesize[0];
336 }
337 }
338
339 271 static void sub2video_push_ref(InputFilterPriv *ifp, int64_t pts)
340 {
341 271 AVFrame *frame = ifp->sub2video.frame;
342 int ret;
343
344 av_assert1(frame->data[0]);
345 271 ifp->sub2video.last_pts = frame->pts = pts;
346 271 ret = av_buffersrc_add_frame_flags(ifp->ifilter.filter, frame,
347 AV_BUFFERSRC_FLAG_KEEP_REF |
348 AV_BUFFERSRC_FLAG_PUSH);
349
2/4
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 271 times.
271 if (ret != AVERROR_EOF && ret < 0)
350 av_log(ifp->ifilter.graph, AV_LOG_WARNING,
351 "Error while add the frame to buffer source(%s).\n",
352 av_err2str(ret));
353 271 }
354
355 180 static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts,
356 const AVSubtitle *sub)
357 {
358 180 AVFrame *frame = ifp->sub2video.frame;
359 int8_t *dst;
360 int dst_linesize;
361 int num_rects;
362 int64_t pts, end_pts;
363
364
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 92 times.
180 if (sub) {
365 88 pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
366 88 AV_TIME_BASE_Q, ifp->time_base);
367 88 end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
368 88 AV_TIME_BASE_Q, ifp->time_base);
369 88 num_rects = sub->num_rects;
370 } else {
371 /* If we are initializing the system, utilize current heartbeat
372 PTS as the start time, and show until the following subpicture
373 is received. Otherwise, utilize the previous subpicture's end time
374 as the fall-back value. */
375 184 pts = ifp->sub2video.initialize ?
376
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4 times.
92 heartbeat_pts : ifp->sub2video.end_pts;
377 92 end_pts = INT64_MAX;
378 92 num_rects = 0;
379 }
380
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 if (sub2video_get_blank_frame(ifp) < 0) {
381 av_log(ifp->ifilter.graph, AV_LOG_ERROR,
382 "Impossible to get a blank canvas.\n");
383 return;
384 }
385 180 dst = frame->data [0];
386 180 dst_linesize = frame->linesize[0];
387
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 180 times.
269 for (int i = 0; i < num_rects; i++)
388 89 sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
389 180 sub2video_push_ref(ifp, pts);
390 180 ifp->sub2video.end_pts = end_pts;
391 180 ifp->sub2video.initialize = 0;
392 }
393
394 /* Define a function for appending a list of allowed formats
395 * to an AVBPrint. If nonempty, the list will have a header. */
396 #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
397 static void choose_ ## name (OutputFilterPriv *ofp, AVBPrint *bprint) \
398 { \
399 if (ofp->var == none && !ofp->supported_list) \
400 return; \
401 av_bprintf(bprint, #name "="); \
402 if (ofp->var != none) { \
403 av_bprintf(bprint, printf_format, get_name(ofp->var)); \
404 } else { \
405 const type *p; \
406 \
407 for (p = ofp->supported_list; *p != none; p++) { \
408 av_bprintf(bprint, printf_format "|", get_name(*p)); \
409 } \
410 if (bprint->len > 0) \
411 bprint->str[--bprint->len] = '\0'; \
412 } \
413 av_bprint_chars(bprint, ':', 1); \
414 }
415
416
9/10
✓ Branch 0 taken 1547 times.
✓ Branch 1 taken 5414 times.
✓ Branch 2 taken 1178 times.
✓ Branch 3 taken 369 times.
✓ Branch 5 taken 5414 times.
✓ Branch 6 taken 369 times.
✓ Branch 11 taken 2382 times.
✓ Branch 12 taken 369 times.
✓ Branch 13 taken 369 times.
✗ Branch 14 not taken.
9343 DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, pix_fmts,
417 AV_PIX_FMT_NONE, "%s", av_get_pix_fmt_name)
418
419
8/10
✓ Branch 0 taken 1436 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1436 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1436 times.
✓ Branch 11 taken 1550 times.
✓ Branch 12 taken 1436 times.
✓ Branch 13 taken 1436 times.
✗ Branch 14 not taken.
2988 DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, sample_fmts,
420 AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
421
422
9/10
✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 1334 times.
✓ Branch 3 taken 87 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 87 times.
✓ Branch 9 taken 554 times.
✓ Branch 10 taken 87 times.
✓ Branch 11 taken 87 times.
✗ Branch 12 not taken.
1992 DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
423 "%d", )
424
425
4/10
✓ Branch 0 taken 6941 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6941 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
6961 DEF_CHOOSE_FORMAT(color_spaces, enum AVColorSpace, color_space, color_spaces,
426 AVCOL_SPC_UNSPECIFIED, "%s", av_color_space_name)
427
428
9/10
✓ Branch 0 taken 5279 times.
✓ Branch 1 taken 1682 times.
✓ Branch 2 taken 4756 times.
✓ Branch 3 taken 523 times.
✓ Branch 5 taken 1682 times.
✓ Branch 6 taken 523 times.
✓ Branch 11 taken 560 times.
✓ Branch 12 taken 523 times.
✓ Branch 13 taken 523 times.
✗ Branch 14 not taken.
7521 DEF_CHOOSE_FORMAT(color_ranges, enum AVColorRange, color_range, color_ranges,
429 AVCOL_RANGE_UNSPECIFIED, "%s", av_color_range_name)
430
431
9/10
✓ Branch 0 taken 6953 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6923 times.
✓ Branch 3 taken 30 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 30 times.
✓ Branch 11 taken 30 times.
✓ Branch 12 taken 30 times.
✓ Branch 13 taken 30 times.
✗ Branch 14 not taken.
6991 DEF_CHOOSE_FORMAT(alpha_modes, enum AVAlphaMode, alpha_mode, alpha_modes,
432 AVALPHA_MODE_UNSPECIFIED, "%s", av_alpha_mode_name)
433
434 1438 static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint)
435 {
436
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1426 times.
1438 if (av_channel_layout_check(&ofp->ch_layout)) {
437 12 av_bprintf(bprint, "channel_layouts=");
438 12 av_channel_layout_describe_bprint(&ofp->ch_layout, bprint);
439
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 1325 times.
1426 } else if (ofp->ch_layouts) {
440 const AVChannelLayout *p;
441
442 101 av_bprintf(bprint, "channel_layouts=");
443
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 101 times.
605 for (p = ofp->ch_layouts; p->nb_channels; p++) {
444 504 av_channel_layout_describe_bprint(p, bprint);
445 504 av_bprintf(bprint, "|");
446 }
447
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (bprint->len > 0)
448 101 bprint->str[--bprint->len] = '\0';
449 } else
450 1325 return;
451 113 av_bprint_chars(bprint, ':', 1);
452 }
453
454 static int read_binary(void *logctx, const char *path,
455 uint8_t **data, int *len)
456 {
457 AVIOContext *io = NULL;
458 int64_t fsize;
459 int ret;
460
461 *data = NULL;
462 *len = 0;
463
464 ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL);
465 if (ret < 0) {
466 av_log(logctx, AV_LOG_ERROR, "Cannot open file '%s': %s\n",
467 path, av_err2str(ret));
468 return ret;
469 }
470
471 fsize = avio_size(io);
472 if (fsize < 0 || fsize > INT_MAX) {
473 av_log(logctx, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path);
474 ret = AVERROR(EIO);
475 goto fail;
476 }
477
478 *data = av_malloc(fsize);
479 if (!*data) {
480 ret = AVERROR(ENOMEM);
481 goto fail;
482 }
483
484 ret = avio_read(io, *data, fsize);
485 if (ret != fsize) {
486 av_log(logctx, AV_LOG_ERROR, "Error reading file %s\n", path);
487 ret = ret < 0 ? ret : AVERROR(EIO);
488 goto fail;
489 }
490
491 *len = fsize;
492
493 ret = 0;
494 fail:
495 avio_close(io);
496 if (ret < 0) {
497 av_freep(data);
498 *len = 0;
499 }
500 return ret;
501 }
502
503 33015 static int filter_opt_apply(void *logctx, AVFilterContext *f,
504 const char *key, const char *val)
505 {
506 33015 const AVOption *o = NULL;
507 int ret;
508
509 33015 ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN);
510
1/2
✓ Branch 0 taken 33015 times.
✗ Branch 1 not taken.
33015 if (ret >= 0)
511 33015 return 0;
512
513 if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/')
514 o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN);
515 if (!o)
516 goto err_apply;
517
518 // key is a valid option name prefixed with '/'
519 // interpret value as a path from which to load the actual option value
520 key++;
521
522 if (o->type == AV_OPT_TYPE_BINARY) {
523 uint8_t *data;
524 int len;
525
526 ret = read_binary(logctx, val, &data, &len);
527 if (ret < 0)
528 goto err_load;
529
530 ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN);
531 av_freep(&data);
532 } else {
533 char *data = read_file_to_string(val);
534 if (!data) {
535 ret = AVERROR(EIO);
536 goto err_load;
537 }
538
539 ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN);
540 av_freep(&data);
541 }
542 if (ret < 0)
543 goto err_apply;
544
545 return 0;
546
547 err_apply:
548 av_log(logctx, AV_LOG_ERROR,
549 "Error applying option '%s' to filter '%s': %s\n",
550 key, f->filter->name, av_err2str(ret));
551 return ret;
552 err_load:
553 av_log(logctx, AV_LOG_ERROR,
554 "Error loading value for option '%s' from file '%s'\n",
555 key, val);
556 return ret;
557 }
558
559 16486 static int graph_opts_apply(void *logctx, AVFilterGraphSegment *seg)
560 {
561
2/2
✓ Branch 0 taken 16918 times.
✓ Branch 1 taken 16486 times.
33404 for (size_t i = 0; i < seg->nb_chains; i++) {
562 16918 AVFilterChain *ch = seg->chains[i];
563
564
2/2
✓ Branch 0 taken 37361 times.
✓ Branch 1 taken 16918 times.
54279 for (size_t j = 0; j < ch->nb_filters; j++) {
565 37361 AVFilterParams *p = ch->filters[j];
566 37361 const AVDictionaryEntry *e = NULL;
567
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37361 times.
37361 av_assert0(p->filter);
569
570
2/2
✓ Branch 1 taken 33015 times.
✓ Branch 2 taken 37361 times.
70376 while ((e = av_dict_iterate(p->opts, e))) {
571 33015 int ret = filter_opt_apply(logctx, p->filter, e->key, e->value);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33015 times.
33015 if (ret < 0)
573 return ret;
574 }
575
576 37361 av_dict_free(&p->opts);
577 }
578 }
579
580 16486 return 0;
581 }
582
583 16486 static int graph_parse(void *logctx,
584 AVFilterGraph *graph, const char *desc,
585 AVFilterInOut **inputs, AVFilterInOut **outputs,
586 AVBufferRef *hw_device)
587 {
588 AVFilterGraphSegment *seg;
589 int ret;
590
591 16486 *inputs = NULL;
592 16486 *outputs = NULL;
593
594 16486 ret = avfilter_graph_segment_parse(graph, desc, 0, &seg);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16486 times.
16486 if (ret < 0)
596 return ret;
597
598 16486 ret = avfilter_graph_segment_create_filters(seg, 0);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16486 times.
16486 if (ret < 0)
600 goto fail;
601
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16486 times.
16486 if (hw_device) {
603 for (int i = 0; i < graph->nb_filters; i++) {
604 AVFilterContext *f = graph->filters[i];
605
606 if (!(f->filter->flags & AVFILTER_FLAG_HWDEVICE))
607 continue;
608 f->hw_device_ctx = av_buffer_ref(hw_device);
609 if (!f->hw_device_ctx) {
610 ret = AVERROR(ENOMEM);
611 goto fail;
612 }
613 }
614 }
615
616 16486 ret = graph_opts_apply(logctx, seg);
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16486 times.
16486 if (ret < 0)
618 goto fail;
619
620 16486 ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
621
622 16486 fail:
623 16486 avfilter_graph_segment_free(&seg);
624 16486 return ret;
625 }
626
627 // Filters can be configured only if the formats of all inputs are known.
628 15430 static int ifilter_has_all_input_formats(FilterGraph *fg)
629 {
630
2/2
✓ Branch 0 taken 14644 times.
✓ Branch 1 taken 8265 times.
22909 for (int i = 0; i < fg->nb_inputs; i++) {
631 14644 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
632
2/2
✓ Branch 0 taken 7165 times.
✓ Branch 1 taken 7479 times.
14644 if (ifp->format < 0)
633 7165 return 0;
634 }
635 8265 return 1;
636 }
637
638 static int filter_thread(void *arg);
639
640 15489 static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
641 {
642 15489 AVFilterContext *ctx = inout->filter_ctx;
643
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 8356 times.
15489 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
644
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 8356 times.
15489 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
645
646
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 15320 times.
15489 if (nb_pads > 1)
647 169 return av_strdup(ctx->filter->name);
648 15320 return av_asprintf("%s:%s", ctx->filter->name,
649 avfilter_pad_get_name(pads, inout->pad_idx));
650 }
651
652 3 static const char *ofilter_item_name(void *obj)
653 {
654 3 OutputFilterPriv *ofp = obj;
655 3 return ofp->log_name;
656 }
657
658 static const AVOption ofilter_options[] = {
659 {"video_size", "set video size", offsetof(OutputFilterPriv, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, INT_MAX },
660 {"pixel_format", "set pixel format", offsetof(OutputFilterPriv, format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX },
661 {"colorspace", "color space", offsetof(OutputFilterPriv, color_space), AV_OPT_TYPE_INT, {.i64 = AVCOL_SPC_UNSPECIFIED }, 0, INT_MAX },
662 {"color_range", "color range", offsetof(OutputFilterPriv, color_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, INT_MAX },
663 {"alpha_mode", "color range", offsetof(OutputFilterPriv, alpha_mode), AV_OPT_TYPE_INT, {.i64 = AVALPHA_MODE_UNSPECIFIED }, 0, INT_MAX },
664 {"ar", "set audio sampling rate (in Hz)", offsetof(OutputFilterPriv, sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX },
665 {"ch_layout", NULL, offsetof(OutputFilterPriv, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL }, 0, 0 },
666 { NULL },
667 };
668
669 static const AVClass ofilter_class = {
670 .class_name = "OutputFilter",
671 .version = LIBAVUTIL_VERSION_INT,
672 .item_name = ofilter_item_name,
673 .option = ofilter_options,
674 .parent_log_context_offset = offsetof(OutputFilterPriv, log_parent),
675 .category = AV_CLASS_CATEGORY_FILTER,
676 };
677
678 8356 static OutputFilter *ofilter_alloc(FilterGraph *fg, enum AVMediaType type)
679 {
680 OutputFilterPriv *ofp;
681 OutputFilter *ofilter;
682
683 8356 ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8356 times.
8356 if (!ofp)
685 return NULL;
686
687 8356 ofilter = &ofp->ofilter;
688 8356 ofilter->class = &ofilter_class;
689 8356 ofp->log_parent = fg;
690 8356 ofilter->graph = fg;
691 8356 ofilter->type = type;
692 8356 av_opt_set_defaults(ofp);
693 8356 ofp->reinit_opts = (ReinitOpts){ .pts = AV_NOPTS_VALUE };
694 8356 ofilter->index = fg->nb_outputs - 1;
695
696 16712 snprintf(ofp->log_name, sizeof(ofp->log_name), "%co%d",
697 8356 av_get_media_type_string(type)[0], ofilter->index);
698
699 8356 return ofilter;
700 }
701
702 7123 static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist,
703 const ViewSpecifier *vs)
704 {
705 7123 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
706 7123 FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
707 SchedulerNode src;
708 int ret;
709
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7123 times.
7123 av_assert0(!ifp->bound);
711 7123 ifp->bound = 1;
712
713
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7119 times.
7123 if (ifilter->type != ist->par->codec_type &&
714
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 !(ifilter->type == AVMEDIA_TYPE_VIDEO && ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
715 av_log(fgp, AV_LOG_ERROR, "Tried to connect %s stream to %s filtergraph input\n",
716 av_get_media_type_string(ist->par->codec_type), av_get_media_type_string(ifilter->type));
717 return AVERROR(EINVAL);
718 }
719
720 7123 ifp->type_src = ist->st->codecpar->codec_type;
721
722 7123 ifp->opts.fallback = av_frame_alloc();
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7123 times.
7123 if (!ifp->opts.fallback)
724 return AVERROR(ENOMEM);
725
726 7123 ret = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph),
727 vs, &ifp->opts, &src);
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7123 times.
7123 if (ret < 0)
729 return ret;
730
731 7123 ifilter->input_name = av_strdup(ifp->opts.name);
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7123 times.
7123 if (!ifilter->input_name)
733 return AVERROR(EINVAL);
734
735 7123 ret = sch_connect(fgp->sch,
736 7123 src, SCH_FILTER_IN(fgp->sch_idx, ifilter->index));
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7123 times.
7123 if (ret < 0)
738 return ret;
739
740
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7119 times.
7123 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
741 4 ifp->sub2video.frame = av_frame_alloc();
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ifp->sub2video.frame)
743 return AVERROR(ENOMEM);
744
745 4 ifp->width = ifp->opts.sub2video_width;
746 4 ifp->height = ifp->opts.sub2video_height;
747
748 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
749 palettes for all rectangles are identical or compatible */
750 4 ifp->format = AV_PIX_FMT_RGB32;
751
752 4 ifp->time_base = AV_TIME_BASE_Q;
753
754 4 av_log(fgp, AV_LOG_VERBOSE, "sub2video: using %dx%d canvas\n",
755 ifp->width, ifp->height);
756 }
757
758 7123 return 0;
759 }
760
761 1 static int ifilter_bind_dec(InputFilterPriv *ifp, Decoder *dec,
762 const ViewSpecifier *vs)
763 {
764 1 FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
765 SchedulerNode src;
766 int ret;
767
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(!ifp->bound);
769 1 ifp->bound = 1;
770
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ifp->ifilter.type != dec->type) {
772 av_log(fgp, AV_LOG_ERROR, "Tried to connect %s decoder to %s filtergraph input\n",
773 av_get_media_type_string(dec->type), av_get_media_type_string(ifp->ifilter.type));
774 return AVERROR(EINVAL);
775 }
776
777 1 ifp->type_src = ifp->ifilter.type;
778
779 1 ret = dec_filter_add(dec, &ifp->ifilter, &ifp->opts, vs, &src);
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
781 return ret;
782
783 1 ifp->ifilter.input_name = av_strdup(ifp->opts.name);
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ifp->ifilter.input_name)
785 return AVERROR(EINVAL);
786
787 1 ret = sch_connect(fgp->sch, src, SCH_FILTER_IN(fgp->sch_idx, ifp->ifilter.index));
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
789 return ret;
790
791 1 return 0;
792 }
793
794 11 static int set_channel_layout(OutputFilterPriv *f, const AVChannelLayout *layouts_allowed,
795 const AVChannelLayout *layout_requested)
796 {
797 int i, err;
798
799
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (layout_requested->order != AV_CHANNEL_ORDER_UNSPEC) {
800 /* Pass the layout through for all orders but UNSPEC */
801 1 err = av_channel_layout_copy(&f->ch_layout, layout_requested);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
803 return err;
804 1 return 0;
805 }
806
807 /* Requested layout is of order UNSPEC */
808
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!layouts_allowed) {
809 /* Use the default native layout for the requested amount of channels when the
810 encoder doesn't have a list of supported layouts */
811 10 av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels);
812 10 return 0;
813 }
814 /* Encoder has a list of supported layouts. Pick the first layout in it with the
815 same amount of channels as the requested layout */
816 for (i = 0; layouts_allowed[i].nb_channels; i++) {
817 if (layouts_allowed[i].nb_channels == layout_requested->nb_channels)
818 break;
819 }
820 if (layouts_allowed[i].nb_channels) {
821 /* Use it if one is found */
822 err = av_channel_layout_copy(&f->ch_layout, &layouts_allowed[i]);
823 if (err < 0)
824 return err;
825 return 0;
826 }
827 /* If no layout for the amount of channels requested was found, use the default
828 native layout for it. */
829 av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels);
830
831 return 0;
832 }
833
834 static int parse_reinit_opts(AVFifo **pout, const char *opts, void *logctx)
835 {
836 AVFifo *out;
837 int ret = AVERROR_BUG;
838 char *ptr, *str, *substr = NULL;
839 const char *token;
840
841 str = av_strdup(opts);
842 if (!str)
843 return AVERROR(ENOMEM);
844
845 out = av_fifo_alloc2(1, sizeof(ReinitOpts), AV_FIFO_FLAG_AUTO_GROW);
846 if (!out) {
847 ret = AVERROR(ENOMEM);
848 goto end;
849 }
850
851 token = av_strtok(str, ",", &ptr);
852 while (token) {
853 ReinitOpts o = { 0 };
854 const char *subtoken;
855 char *subptr, *endptr;
856
857 substr = av_strdup(token);
858 if (!substr) {
859 ret = AVERROR(ENOMEM);
860 goto end;
861 }
862 subtoken = av_strtok(substr, "|", &subptr);
863 if (subtoken && subptr) {
864 if (!av_strstart(subtoken, "pts=", &subtoken)) {
865 av_log(logctx, AV_LOG_ERROR, "Invalid reinit identifier\n");
866 ret = AVERROR(ENOMEM);
867 goto end;
868 }
869 o.pts = strtoll(subtoken, &endptr, 0);
870 if (*endptr || o.pts < 0) {
871 ret = AVERROR(EINVAL);
872 goto end;
873 }
874 ret = av_dict_parse_string(&o.dict, subptr, "=", ":", 0);
875 if (ret < 0) {
876 av_log(logctx, AV_LOG_ERROR, "Error parsing encoder options\n");
877 goto end;
878 }
879 ret = av_fifo_write(out, &o, 1);
880 if (ret < 0) {
881 av_dict_free(&o.dict);
882 goto end;
883 }
884 } else {
885 ret = AVERROR(EINVAL);
886 goto end;
887 }
888 av_freep(&substr);
889 if (ptr)
890 ptr += strspn(ptr, " \n\t\r");
891 token = av_strtok(NULL, ",", &ptr);
892 }
893
894 ret = 0;
895 end:
896 *pout = out;
897 av_free(substr);
898 av_free(str);
899
900 return ret;
901 }
902
903 8352 int ofilter_bind_enc(OutputFilter *ofilter, unsigned sched_idx_enc,
904 const OutputFilterOptions *opts)
905 {
906 8352 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
907 8352 FilterGraph *fg = ofilter->graph;
908 8352 FilterGraphPriv *fgp = fgp_from_fg(fg);
909 int ret;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 av_assert0(!ofilter->bound);
912
2/4
✓ Branch 0 taken 8352 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8352 times.
8352 av_assert0(!opts->enc ||
913 ofilter->type == opts->enc->type);
914
915 8352 ofp->needed = ofilter->bound = 1;
916 8352 av_freep(&ofilter->linklabel);
917
918 8352 ofp->flags |= opts->flags;
919 8352 ofp->ts_offset = opts->ts_offset;
920 8352 ofp->enc_timebase = opts->output_tb;
921
922 8352 ofp->trim_start_us = opts->trim_start_us;
923 8352 ofp->trim_duration_us = opts->trim_duration_us;
924
925 8352 ofilter->output_name = av_strdup(opts->name);
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 if (!ofilter->output_name)
927 return AVERROR(EINVAL);
928
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 if (opts->reinit_opts) {
930 ret = parse_reinit_opts(&ofp->reinit_opts_fifo, opts->reinit_opts, ofilter);
931 if (ret < 0)
932 return ret;
933 }
934
935 8352 ret = av_dict_copy(&ofp->sws_opts, opts->sws_opts, 0);
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 if (ret < 0)
937 return ret;
938
939 8352 ret = av_dict_copy(&ofp->swr_opts, opts->swr_opts, 0);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 if (ret < 0)
941 return ret;
942
943
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 8287 times.
8352 if (opts->flags & OFILTER_FLAG_AUDIO_24BIT)
944 65 av_dict_set(&ofp->swr_opts, "output_sample_bits", "24", 0);
945
946
2/2
✓ Branch 0 taken 6947 times.
✓ Branch 1 taken 1405 times.
8352 if (fgp->is_simple) {
947 // for simple filtergraph there is just one output,
948 // so use only graph-level information for logging
949 6947 ofp->log_parent = NULL;
950 6947 av_strlcpy(ofp->log_name, fgp->log_name, sizeof(ofp->log_name));
951 } else
952 1405 av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofilter->output_name);
953
954
2/3
✓ Branch 0 taken 6914 times.
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
8352 switch (ofilter->type) {
955 6914 case AVMEDIA_TYPE_VIDEO:
956 6914 ofp->width = opts->width;
957 6914 ofp->height = opts->height;
958
2/2
✓ Branch 0 taken 5368 times.
✓ Branch 1 taken 1546 times.
6914 if (opts->format != AV_PIX_FMT_NONE) {
959 5368 ofp->format = opts->format;
960 } else
961 1546 ofp->pix_fmts = opts->pix_fmts;
962
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6914 times.
6914 if (opts->color_space != AVCOL_SPC_UNSPECIFIED)
964 ofp->color_space = opts->color_space;
965 else
966 6914 ofp->color_spaces = opts->color_spaces;
967
968
2/2
✓ Branch 0 taken 1651 times.
✓ Branch 1 taken 5263 times.
6914 if (opts->color_range != AVCOL_RANGE_UNSPECIFIED)
969 1651 ofp->color_range = opts->color_range;
970 else
971 5263 ofp->color_ranges = opts->color_ranges;
972
973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6914 times.
6914 if (opts->alpha_mode != AVALPHA_MODE_UNSPECIFIED)
974 ofp->alpha_mode = opts->alpha_mode;
975 else
976 6914 ofp->alpha_modes = opts->alpha_modes;
977
978 6914 fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT);
979
980 6914 ofp->fps.last_frame = av_frame_alloc();
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6914 times.
6914 if (!ofp->fps.last_frame)
982 return AVERROR(ENOMEM);
983
984 6914 ofp->fps.vsync_method = opts->vsync_method;
985 6914 ofp->fps.framerate = opts->frame_rate;
986 6914 ofp->fps.framerate_max = opts->max_frame_rate;
987 6914 ofp->fps.framerate_supported = opts->frame_rates;
988
989 // reduce frame rate for mpeg4 to be within the spec limits
990
3/4
✓ Branch 0 taken 6914 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 6849 times.
6914 if (opts->enc && opts->enc->id == AV_CODEC_ID_MPEG4)
991 65 ofp->fps.framerate_clip = 65535;
992
993 6914 ofp->fps.dup_warning = 1000;
994
995 6914 break;
996 1438 case AVMEDIA_TYPE_AUDIO:
997
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1437 times.
1438 if (opts->format != AV_SAMPLE_FMT_NONE) {
998 1 ofp->format = opts->format;
999 } else {
1000 1437 ofp->sample_fmts = opts->sample_fmts;
1001 }
1002
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1422 times.
1438 if (opts->sample_rate) {
1003 16 ofp->sample_rate = opts->sample_rate;
1004 } else
1005 1422 ofp->sample_rates = opts->sample_rates;
1006
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1427 times.
1438 if (opts->ch_layout.nb_channels) {
1007 11 int ret = set_channel_layout(ofp, opts->ch_layouts, &opts->ch_layout);
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
1009 return ret;
1010 } else {
1011 1427 ofp->ch_layouts = opts->ch_layouts;
1012 }
1013 1438 break;
1014 }
1015
1016 8352 ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fgp->sch_idx, ofilter->index),
1017 8352 SCH_ENC(sched_idx_enc));
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8352 times.
8352 if (ret < 0)
1019 return ret;
1020
1021 8352 return 0;
1022 }
1023
1024 1 static int ofilter_bind_ifilter(OutputFilter *ofilter, InputFilterPriv *ifp,
1025 const OutputFilterOptions *opts)
1026 {
1027 1 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1028
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(!ofilter->bound);
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(ofilter->type == ifp->ifilter.type);
1031
1032 1 ofp->needed = ofilter->bound = 1;
1033 1 av_freep(&ofilter->linklabel);
1034
1035 1 ofilter->output_name = av_strdup(opts->name);
1036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ofilter->output_name)
1037 return AVERROR(EINVAL);
1038
1039 1 ifp->ofilter_src = ofilter;
1040
1041 1 av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofilter->output_name);
1042
1043 1 return 0;
1044 }
1045
1046 1 static int ifilter_bind_fg(InputFilterPriv *ifp, FilterGraph *fg_src, int out_idx)
1047 {
1048 1 FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
1049 1 OutputFilter *ofilter_src = fg_src->outputs[out_idx];
1050 OutputFilterOptions opts;
1051 char name[32];
1052 int ret;
1053
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(!ifp->bound);
1055 1 ifp->bound = 1;
1056
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ifp->ifilter.type != ofilter_src->type) {
1058 av_log(fgp, AV_LOG_ERROR, "Tried to connect %s output to %s input\n",
1059 av_get_media_type_string(ofilter_src->type),
1060 av_get_media_type_string(ifp->ifilter.type));
1061 return AVERROR(EINVAL);
1062 }
1063
1064 1 ifp->type_src = ifp->ifilter.type;
1065
1066 1 memset(&opts, 0, sizeof(opts));
1067
1068 1 snprintf(name, sizeof(name), "fg:%d:%d", fgp->fg.index, ifp->ifilter.index);
1069 1 opts.name = name;
1070
1071 1 ret = ofilter_bind_ifilter(ofilter_src, ifp, &opts);
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1073 return ret;
1074
1075 1 ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fg_src->index, out_idx),
1076 1 SCH_FILTER_IN(fgp->sch_idx, ifp->ifilter.index));
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1078 return ret;
1079
1080 1 return 0;
1081 }
1082
1083 7133 static InputFilter *ifilter_alloc(FilterGraph *fg)
1084 {
1085 InputFilterPriv *ifp;
1086 InputFilter *ifilter;
1087
1088 7133 ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs);
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7133 times.
7133 if (!ifp)
1090 return NULL;
1091
1092 7133 ifilter = &ifp->ifilter;
1093 7133 ifilter->graph = fg;
1094
1095 7133 ifp->frame = av_frame_alloc();
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7133 times.
7133 if (!ifp->frame)
1097 return NULL;
1098
1099 7133 ifilter->index = fg->nb_inputs - 1;
1100 7133 ifp->format = -1;
1101 7133 ifp->color_space = AVCOL_SPC_UNSPECIFIED;
1102 7133 ifp->color_range = AVCOL_RANGE_UNSPECIFIED;
1103 7133 ifp->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
1104
1105 7133 ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7133 times.
7133 if (!ifp->frame_queue)
1107 return NULL;
1108
1109 7133 return ifilter;
1110 }
1111
1112 10512 void fg_free(FilterGraph **pfg)
1113 {
1114 10512 FilterGraph *fg = *pfg;
1115 FilterGraphPriv *fgp;
1116
1117
2/2
✓ Branch 0 taken 2290 times.
✓ Branch 1 taken 8222 times.
10512 if (!fg)
1118 2290 return;
1119 8222 fgp = fgp_from_fg(fg);
1120
1121
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 8222 times.
15355 for (int j = 0; j < fg->nb_inputs; j++) {
1122 7133 InputFilter *ifilter = fg->inputs[j];
1123 7133 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
1124
1125
1/2
✓ Branch 0 taken 7133 times.
✗ Branch 1 not taken.
7133 if (ifp->frame_queue) {
1126 AVFrame *frame;
1127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7133 times.
7133 while (av_fifo_read(ifp->frame_queue, &frame, 1) >= 0)
1128 av_frame_free(&frame);
1129 7133 av_fifo_freep2(&ifp->frame_queue);
1130 }
1131 7133 av_frame_free(&ifp->sub2video.frame);
1132
1133 7133 av_frame_free(&ifp->frame);
1134 7133 av_frame_free(&ifp->opts.fallback);
1135
1136 7133 av_buffer_unref(&ifp->hw_frames_ctx);
1137 7133 av_channel_layout_uninit(&ifp->ch_layout);
1138 7133 av_freep(&ifilter->linklabel);
1139 7133 av_freep(&ifp->opts.name);
1140 7133 av_buffer_unref(&ifp->downmixmatrix);
1141 7133 av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
1142 7133 av_freep(&ifilter->name);
1143 7133 av_freep(&ifilter->input_name);
1144 7133 av_freep(&fg->inputs[j]);
1145 }
1146 8222 av_freep(&fg->inputs);
1147
2/2
✓ Branch 0 taken 8356 times.
✓ Branch 1 taken 8222 times.
16578 for (int j = 0; j < fg->nb_outputs; j++) {
1148 8356 OutputFilter *ofilter = fg->outputs[j];
1149 8356 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1150
1151 8356 av_frame_free(&ofp->fps.last_frame);
1152 8356 av_dict_free(&ofp->sws_opts);
1153 8356 av_dict_free(&ofp->swr_opts);
1154
1155 8356 av_freep(&ofilter->linklabel);
1156 8356 av_freep(&ofilter->name);
1157 8356 av_freep(&ofilter->output_name);
1158 8356 av_freep(&ofilter->apad);
1159 8356 av_dict_free(&ofp->reinit_opts.dict);
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8356 times.
8356 if (ofp->reinit_opts_fifo) {
1161 while (av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1) >= 0)
1162 av_dict_free(&ofp->reinit_opts.dict);
1163 av_fifo_freep2(&ofp->reinit_opts_fifo);
1164 }
1165 8356 av_channel_layout_uninit(&ofp->ch_layout);
1166 8356 av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data);
1167 8356 av_freep(&fg->outputs[j]);
1168 }
1169 8222 av_freep(&fg->outputs);
1170 8222 av_freep(&fg->graph_desc);
1171
1172 8222 av_frame_free(&fgp->frame);
1173 8222 av_frame_free(&fgp->frame_enc);
1174
1175 8222 av_freep(pfg);
1176 }
1177
1178 62 static const char *fg_item_name(void *obj)
1179 {
1180 62 const FilterGraphPriv *fgp = obj;
1181
1182 62 return fgp->log_name;
1183 }
1184
1185 static const AVClass fg_class = {
1186 .class_name = "FilterGraph",
1187 .version = LIBAVUTIL_VERSION_INT,
1188 .item_name = fg_item_name,
1189 .category = AV_CLASS_CATEGORY_FILTER,
1190 };
1191
1192 8222 int fg_create(FilterGraph **pfg, char **graph_desc, Scheduler *sch,
1193 const OutputFilterOptions *opts)
1194 {
1195 FilterGraphPriv *fgp;
1196 FilterGraph *fg;
1197
1198 AVFilterInOut *inputs, *outputs;
1199 AVFilterGraph *graph;
1200 8222 int ret = 0;
1201
1202 8222 fgp = av_mallocz(sizeof(*fgp));
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8222 times.
8222 if (!fgp) {
1204 av_freep(graph_desc);
1205 return AVERROR(ENOMEM);
1206 }
1207 8222 fg = &fgp->fg;
1208
1209
2/2
✓ Branch 0 taken 6948 times.
✓ Branch 1 taken 1274 times.
8222 if (pfg) {
1210 6948 *pfg = fg;
1211 6948 fg->index = -1;
1212 } else {
1213 1274 ret = av_dynarray_add_nofree(&filtergraphs, &nb_filtergraphs, fgp);
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1274 times.
1274 if (ret < 0) {
1215 av_freep(graph_desc);
1216 av_freep(&fgp);
1217 return ret;
1218 }
1219
1220 1274 fg->index = nb_filtergraphs - 1;
1221 }
1222
1223 8222 fg->class = &fg_class;
1224 8222 fg->graph_desc = *graph_desc;
1225 8222 fgp->disable_conversions = !auto_conversion_filters;
1226 8222 fgp->nb_threads = -1;
1227 8222 fgp->sch = sch;
1228
1229 8222 *graph_desc = NULL;
1230
1231 8222 snprintf(fgp->log_name, sizeof(fgp->log_name), "fc#%d", fg->index);
1232
1233 8222 fgp->frame = av_frame_alloc();
1234 8222 fgp->frame_enc = av_frame_alloc();
1235
2/4
✓ Branch 0 taken 8222 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8222 times.
8222 if (!fgp->frame || !fgp->frame_enc)
1236 return AVERROR(ENOMEM);
1237
1238 /* this graph is only used for determining the kinds of inputs
1239 * and outputs we have, and is discarded on exit from this function */
1240 8222 graph = avfilter_graph_alloc();
1241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8222 times.
8222 if (!graph)
1242 return AVERROR(ENOMEM);;
1243 8222 graph->nb_threads = 1;
1244
1245 8222 ret = graph_parse(fg, graph, fg->graph_desc, &inputs, &outputs,
1246 hw_device_for_filter());
1247
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8221 times.
8222 if (ret < 0)
1248 1 goto fail;
1249
1250
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 8221 times.
15354 for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
1251 7133 InputFilter *const ifilter = ifilter_alloc(fg);
1252
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7133 times.
7133 if (!ifilter) {
1254 ret = AVERROR(ENOMEM);
1255 goto fail;
1256 }
1257
1258 7133 ifilter->linklabel = cur->name;
1259 7133 cur->name = NULL;
1260
1261 7133 ifilter->type = avfilter_pad_get_type(cur->filter_ctx->input_pads,
1262 cur->pad_idx);
1263
1264
3/4
✓ Branch 0 taken 1375 times.
✓ Branch 1 taken 5758 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1375 times.
7133 if (ifilter->type != AVMEDIA_TYPE_VIDEO && ifilter->type != AVMEDIA_TYPE_AUDIO) {
1265 av_log(fg, AV_LOG_FATAL, "Only video and audio filters supported "
1266 "currently.\n");
1267 ret = AVERROR(ENOSYS);
1268 goto fail;
1269 }
1270
1271 7133 ifilter->name = describe_filter_link(fg, cur, 1);
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7133 times.
7133 if (!ifilter->name) {
1273 ret = AVERROR(ENOMEM);
1274 goto fail;
1275 }
1276 }
1277
1278
2/2
✓ Branch 0 taken 8356 times.
✓ Branch 1 taken 8221 times.
16577 for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
1279 8356 const enum AVMediaType type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
1280 cur->pad_idx);
1281 8356 OutputFilter *const ofilter = ofilter_alloc(fg, type);
1282 OutputFilterPriv *ofp;
1283
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8356 times.
8356 if (!ofilter) {
1285 ret = AVERROR(ENOMEM);
1286 goto fail;
1287 }
1288 8356 ofp = ofp_from_ofilter(ofilter);
1289
1290 8356 ofilter->linklabel = cur->name;
1291 8356 cur->name = NULL;
1292
1293 8356 ofilter->name = describe_filter_link(fg, cur, 0);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8356 times.
8356 if (!ofilter->name) {
1295 ret = AVERROR(ENOMEM);
1296 goto fail;
1297 }
1298
1299 // opts should only be needed in this function to fill fields from filtergraphs
1300 // whose output is meant to be treated as if it was stream, e.g. merged HEIF
1301 // tile groups.
1302
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8350 times.
8356 if (opts) {
1303 6 ofp->flags = opts->flags;
1304 6 ofp->side_data = opts->side_data;
1305 6 ofp->nb_side_data = opts->nb_side_data;
1306
1307 6 ofp->crop_top = opts->crop_top;
1308 6 ofp->crop_bottom = opts->crop_bottom;
1309 6 ofp->crop_left = opts->crop_left;
1310 6 ofp->crop_right = opts->crop_right;
1311
1312 6 const AVFrameSideData *sd = av_frame_side_data_get(ofp->side_data, ofp->nb_side_data,
1313 AV_FRAME_DATA_DISPLAYMATRIX);
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (sd)
1315 memcpy(ofp->displaymatrix, sd->data, sizeof(ofp->displaymatrix));
1316 }
1317 }
1318
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8221 times.
8221 if (!fg->nb_outputs) {
1320 av_log(fg, AV_LOG_FATAL, "A filtergraph has zero outputs, this is not supported\n");
1321 ret = AVERROR(ENOSYS);
1322 goto fail;
1323 }
1324
1325 8221 ret = sch_add_filtergraph(sch, fg->nb_inputs, fg->nb_outputs,
1326 filter_thread, fgp);
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8221 times.
8221 if (ret < 0)
1328 goto fail;
1329 8221 fgp->sch_idx = ret;
1330
1331 8222 fail:
1332 8222 avfilter_inout_free(&inputs);
1333 8222 avfilter_inout_free(&outputs);
1334 8222 avfilter_graph_free(&graph);
1335
1336
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8221 times.
8222 if (ret < 0)
1337 1 return ret;
1338
1339 8221 return 0;
1340 }
1341
1342 6948 int fg_create_simple(FilterGraph **pfg,
1343 InputStream *ist,
1344 char **graph_desc,
1345 Scheduler *sch, unsigned sched_idx_enc,
1346 const OutputFilterOptions *opts)
1347 {
1348 6948 const enum AVMediaType type = ist->par->codec_type;
1349 FilterGraph *fg;
1350 FilterGraphPriv *fgp;
1351 int ret;
1352
1353 6948 ret = fg_create(pfg, graph_desc, sch, NULL);
1354
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6947 times.
6948 if (ret < 0)
1355 1 return ret;
1356 6947 fg = *pfg;
1357 6947 fgp = fgp_from_fg(fg);
1358
1359 6947 fgp->is_simple = 1;
1360
1361 6947 snprintf(fgp->log_name, sizeof(fgp->log_name), "%cf%s",
1362 6947 av_get_media_type_string(type)[0], opts->name);
1363
1364
2/4
✓ Branch 0 taken 6947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6947 times.
6947 if (fg->nb_inputs != 1 || fg->nb_outputs != 1) {
1365 av_log(fg, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
1366 "to have exactly 1 input and 1 output. "
1367 "However, it had %d input(s) and %d output(s). Please adjust, "
1368 "or use a complex filtergraph (-filter_complex) instead.\n",
1369 *graph_desc, fg->nb_inputs, fg->nb_outputs);
1370 return AVERROR(EINVAL);
1371 }
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6947 times.
6947 if (fg->outputs[0]->type != type) {
1373 av_log(fg, AV_LOG_ERROR, "Filtergraph has a %s output, cannot connect "
1374 "it to %s output stream\n",
1375 av_get_media_type_string(fg->outputs[0]->type),
1376 av_get_media_type_string(type));
1377 return AVERROR(EINVAL);
1378 }
1379
1380 6947 ret = ifilter_bind_ist(fg->inputs[0], ist, opts->vs);
1381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6947 times.
6947 if (ret < 0)
1382 return ret;
1383
1384 6947 ret = ofilter_bind_enc(fg->outputs[0], sched_idx_enc, opts);
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6947 times.
6947 if (ret < 0)
1386 return ret;
1387
1388
2/2
✓ Branch 0 taken 4316 times.
✓ Branch 1 taken 2631 times.
6947 if (opts->nb_threads >= 0)
1389 4316 fgp->nb_threads = opts->nb_threads;
1390
1391 6947 return 0;
1392 }
1393
1394 363 static int fg_complex_bind_input(FilterGraph *fg, InputFilter *ifilter, int commit)
1395 {
1396 363 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
1397 363 InputStream *ist = NULL;
1398 363 enum AVMediaType type = ifilter->type;
1399 363 ViewSpecifier vs = { .type = VIEW_SPECIFIER_TYPE_NONE };
1400 const char *spec;
1401 char *p;
1402 int i, ret;
1403
1404
4/4
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 248 times.
363 if (ifilter->linklabel && !strncmp(ifilter->linklabel, "dec:", 4)) {
1405 // bind to a standalone decoder
1406 int dec_idx;
1407
1408 1 dec_idx = strtol(ifilter->linklabel + 4, &p, 0);
1409
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (dec_idx < 0 || dec_idx >= nb_decoders) {
1410 av_log(fg, AV_LOG_ERROR, "Invalid decoder index %d in filtergraph description %s\n",
1411 dec_idx, fg->graph_desc);
1412 return AVERROR(EINVAL);
1413 }
1414
1415
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (type == AVMEDIA_TYPE_VIDEO) {
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 spec = *p == ':' ? p + 1 : p;
1417 1 ret = view_specifier_parse(&spec, &vs);
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1419 return ret;
1420 }
1421
1422 1 ret = ifilter_bind_dec(ifp, decoders[dec_idx], &vs);
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1424 av_log(fg, AV_LOG_ERROR, "Error binding a decoder to filtergraph input %s\n",
1425 ifilter->name);
1426 1 return ret;
1427
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 114 times.
362 } else if (ifilter->linklabel) {
1428 StreamSpecifier ss;
1429 AVFormatContext *s;
1430 248 AVStream *st = NULL;
1431 int file_idx;
1432
1433 // try finding an unbound filtergraph output with this label
1434
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 246 times.
504 for (int i = 0; i < nb_filtergraphs; i++) {
1435 258 FilterGraph *fg_src = filtergraphs[i];
1436
1437
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 10 times.
258 if (fg == fg_src)
1438 248 continue;
1439
1440
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8 times.
18 for (int j = 0; j < fg_src->nb_outputs; j++) {
1441 10 OutputFilter *ofilter = fg_src->outputs[j];
1442
1443
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
10 if (!ofilter->bound && ofilter->linklabel &&
1444
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !strcmp(ofilter->linklabel, ifilter->linklabel)) {
1445
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (commit) {
1446 1 av_log(fg, AV_LOG_VERBOSE,
1447 "Binding input with label '%s' to filtergraph output %d:%d\n",
1448 ifilter->linklabel, i, j);
1449
1450 1 ret = ifilter_bind_fg(ifp, fg_src, j);
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1452 av_log(fg, AV_LOG_ERROR, "Error binding filtergraph input %s\n",
1453 ifilter->linklabel);
1454 2 return ret;
1455 }
1456 } else
1457 1 ofp_from_ofilter(ofilter)->needed = 1;
1458 2 return 0;
1459 }
1460 }
1461 }
1462
1463 // bind to an explicitly specified demuxer stream
1464 246 file_idx = strtol(ifilter->linklabel, &p, 0);
1465
2/4
✓ Branch 0 taken 246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 246 times.
246 if (file_idx < 0 || file_idx >= nb_input_files) {
1466 av_log(fg, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
1467 file_idx, fg->graph_desc);
1468 return AVERROR(EINVAL);
1469 }
1470 246 s = input_files[file_idx]->ctx;
1471
1472
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 8 times.
246 ret = stream_specifier_parse(&ss, *p == ':' ? p + 1 : p, 1, fg);
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (ret < 0) {
1474 av_log(fg, AV_LOG_ERROR, "Invalid stream specifier: %s\n", p);
1475 return ret;
1476 }
1477
1478
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 170 times.
246 if (type == AVMEDIA_TYPE_VIDEO) {
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 spec = ss.remainder ? ss.remainder : "";
1480 76 ret = view_specifier_parse(&spec, &vs);
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (ret < 0) {
1482 stream_specifier_uninit(&ss);
1483 return ret;
1484 }
1485 }
1486
1487
1/2
✓ Branch 0 taken 299 times.
✗ Branch 1 not taken.
299 for (i = 0; i < s->nb_streams; i++) {
1488 299 enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
1489
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 283 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 6 times.
299 if (stream_type != type &&
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
1491 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
1492 6 continue;
1493
2/2
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 47 times.
293 if (stream_specifier_match(&ss, s, s->streams[i], fg)) {
1494 246 st = s->streams[i];
1495 246 break;
1496 }
1497 }
1498 246 stream_specifier_uninit(&ss);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (!st) {
1500 av_log(fg, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
1501 "matches no streams.\n", p, fg->graph_desc);
1502 return AVERROR(EINVAL);
1503 }
1504 246 ist = input_files[file_idx]->streams[st->index];
1505
1506
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 127 times.
246 if (commit)
1507 119 av_log(fg, AV_LOG_VERBOSE,
1508 "Binding input with label '%s' to input stream %d:%d\n",
1509 119 ifilter->linklabel, ist->file->index, ist->index);
1510 } else {
1511 // try finding an unbound filtergraph output
1512
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 114 times.
228 for (int i = 0; i < nb_filtergraphs; i++) {
1513 114 FilterGraph *fg_src = filtergraphs[i];
1514
1515
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 if (fg == fg_src)
1516 114 continue;
1517
1518 for (int j = 0; j < fg_src->nb_outputs; j++) {
1519 OutputFilter *ofilter = fg_src->outputs[j];
1520
1521 if (!ofilter->bound) {
1522 if (commit) {
1523 av_log(fg, AV_LOG_VERBOSE,
1524 "Binding unlabeled filtergraph input to filtergraph output %d:%d\n", i, j);
1525
1526 ret = ifilter_bind_fg(ifp, fg_src, j);
1527 if (ret < 0) {
1528 av_log(fg, AV_LOG_ERROR, "Error binding filtergraph input %d:%d\n", i, j);
1529 return ret;
1530 }
1531 } else
1532 ofp_from_ofilter(ofilter)->needed = 1;
1533 return 0;
1534 }
1535 }
1536 }
1537
1538 114 ist = ist_find_unused(type);
1539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (!ist) {
1540 av_log(fg, AV_LOG_FATAL,
1541 "Cannot find an unused %s input stream to feed the "
1542 "unlabeled input pad %s.\n",
1543 av_get_media_type_string(type), ifilter->name);
1544 return AVERROR(EINVAL);
1545 }
1546
1547
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 57 times.
114 if (commit)
1548 57 av_log(fg, AV_LOG_VERBOSE,
1549 "Binding unlabeled input %d to input stream %d:%d\n",
1550 57 ifilter->index, ist->file->index, ist->index);
1551 }
1552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 av_assert0(ist);
1553
1554
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 184 times.
360 if (commit) {
1555 176 ret = ifilter_bind_ist(ifilter, ist, &vs);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 if (ret < 0) {
1557 av_log(fg, AV_LOG_ERROR,
1558 "Error binding an input stream to complex filtergraph input %s.\n",
1559 ifilter->name);
1560 return ret;
1561 }
1562 }
1563
1564 360 return 0;
1565 }
1566
1567 2545 static int bind_inputs(FilterGraph *fg, int commit)
1568 {
1569 // bind filtergraph inputs to input streams or other filtergraphs
1570
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 2545 times.
2909 for (int i = 0; i < fg->nb_inputs; i++) {
1571 364 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
1572 int ret;
1573
1574
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 363 times.
364 if (ifp->bound)
1575 1 continue;
1576
1577 363 ret = fg_complex_bind_input(fg, &ifp->ifilter, commit);
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (ret < 0)
1579 return ret;
1580 }
1581
1582 2545 return 0;
1583 }
1584
1585 8722 int fg_finalise_bindings(void)
1586 {
1587 int ret;
1588
1589
2/2
✓ Branch 0 taken 1274 times.
✓ Branch 1 taken 8722 times.
9996 for (int i = 0; i < nb_filtergraphs; i++) {
1590 1274 ret = bind_inputs(filtergraphs[i], 0);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1274 times.
1274 if (ret < 0)
1592 return ret;
1593 }
1594
1595 // check that all outputs were bound
1596
2/2
✓ Branch 0 taken 1274 times.
✓ Branch 1 taken 8722 times.
9996 for (int i = nb_filtergraphs - 1; i >= 0; i--) {
1597 1274 FilterGraph *fg = filtergraphs[i];
1598 1274 FilterGraphPriv *fgp = fgp_from_fg(filtergraphs[i]);
1599
1600
2/2
✓ Branch 0 taken 1409 times.
✓ Branch 1 taken 1271 times.
2680 for (int j = 0; j < fg->nb_outputs; j++) {
1601 1409 OutputFilter *output = fg->outputs[j];
1602
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1406 times.
1409 if (!ofp_from_ofilter(output)->needed) {
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!fg->is_internal) {
1604 av_log(fg, AV_LOG_FATAL,
1605 "Filter '%s' has output %d (%s) unconnected\n",
1606 output->name, j,
1607 output->linklabel ? (const char *)output->linklabel : "unlabeled");
1608 return AVERROR(EINVAL);
1609 }
1610
1611 3 av_log(fg, AV_LOG_DEBUG,
1612 "Internal filter '%s' has output %d (%s) unconnected. Removing graph\n",
1613 output->name, j,
1614
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 output->linklabel ? (const char *)output->linklabel : "unlabeled");
1615 3 sch_remove_filtergraph(fgp->sch, fgp->sch_idx);
1616 3 fg_free(&filtergraphs[i]);
1617 3 nb_filtergraphs--;
1618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (nb_filtergraphs > 0)
1619 memmove(&filtergraphs[i],
1620 &filtergraphs[i + 1],
1621 (nb_filtergraphs - i) * sizeof(*filtergraphs));
1622 3 break;
1623 }
1624 }
1625 }
1626
1627
2/2
✓ Branch 0 taken 1271 times.
✓ Branch 1 taken 8722 times.
9993 for (int i = 0; i < nb_filtergraphs; i++) {
1628 1271 ret = bind_inputs(filtergraphs[i], 1);
1629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271 times.
1271 if (ret < 0)
1630 return ret;
1631 }
1632
1633 8722 return 0;
1634 }
1635
1636 15570 static int insert_trim(void *logctx, int64_t start_time, int64_t duration,
1637 AVFilterContext **last_filter, int *pad_idx,
1638 const char *filter_name)
1639 {
1640 15570 AVFilterGraph *graph = (*last_filter)->graph;
1641 AVFilterContext *ctx;
1642 const AVFilter *trim;
1643 15570 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
1644
2/2
✓ Branch 0 taken 12757 times.
✓ Branch 1 taken 2813 times.
15570 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
1645 15570 int ret = 0;
1646
1647
4/4
✓ Branch 0 taken 14192 times.
✓ Branch 1 taken 1378 times.
✓ Branch 2 taken 14168 times.
✓ Branch 3 taken 24 times.
15570 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
1648 14168 return 0;
1649
1650 1402 trim = avfilter_get_by_name(name);
1651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (!trim) {
1652 av_log(logctx, AV_LOG_ERROR, "%s filter not present, cannot limit "
1653 "recording time.\n", name);
1654 return AVERROR_FILTER_NOT_FOUND;
1655 }
1656
1657 1402 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
1658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (!ctx)
1659 return AVERROR(ENOMEM);
1660
1661
2/2
✓ Branch 0 taken 1378 times.
✓ Branch 1 taken 24 times.
1402 if (duration != INT64_MAX) {
1662 1378 ret = av_opt_set_int(ctx, "durationi", duration,
1663 AV_OPT_SEARCH_CHILDREN);
1664 }
1665
3/4
✓ Branch 0 taken 1402 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 1375 times.
1402 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
1666 27 ret = av_opt_set_int(ctx, "starti", start_time,
1667 AV_OPT_SEARCH_CHILDREN);
1668 }
1669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (ret < 0) {
1670 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
1671 return ret;
1672 }
1673
1674 1402 ret = avfilter_init_str(ctx, NULL);
1675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (ret < 0)
1676 return ret;
1677
1678 1402 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
1679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (ret < 0)
1680 return ret;
1681
1682 1402 *last_filter = ctx;
1683 1402 *pad_idx = 0;
1684 1402 return 0;
1685 }
1686
1687 24 static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
1688 const char *filter_name, const char *args)
1689 {
1690 24 AVFilterGraph *graph = (*last_filter)->graph;
1691 24 const AVFilter *filter = avfilter_get_by_name(filter_name);
1692 AVFilterContext *ctx;
1693 int ret;
1694
1695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!filter)
1696 return AVERROR_BUG;
1697
1698 24 ret = avfilter_graph_create_filter(&ctx,
1699 filter,
1700 filter_name, args, NULL, graph);
1701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
1702 return ret;
1703
1704 24 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
1705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
1706 return ret;
1707
1708 24 *last_filter = ctx;
1709 24 *pad_idx = 0;
1710 24 return 0;
1711 }
1712
1713 6961 static int configure_output_video_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1714 OutputFilter *ofilter, AVFilterInOut *out)
1715 {
1716 6961 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1717 6961 AVFilterContext *last_filter = out->filter_ctx;
1718 AVBPrint bprint;
1719 6961 int pad_idx = out->pad_idx;
1720 int ret;
1721 char name[255];
1722
1723 6961 snprintf(name, sizeof(name), "out_%s", ofilter->output_name);
1724 6961 ret = avfilter_graph_create_filter(&ofilter->filter,
1725 avfilter_get_by_name("buffersink"),
1726 name, NULL, NULL, graph);
1727
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6961 times.
6961 if (ret < 0)
1729 return ret;
1730
1731
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6958 times.
6961 if (ofp->flags & OFILTER_FLAG_CROP) {
1732 char crop_buf[64];
1733 3 snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u",
1734 ofp->crop_left, ofp->crop_right,
1735 ofp->crop_top, ofp->crop_bottom,
1736 ofp->crop_left, ofp->crop_top);
1737 3 ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf);
1738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1739 return ret;
1740 }
1741
1742
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6958 times.
6961 if (ofp->flags & OFILTER_FLAG_AUTOROTATE) {
1743 3 int32_t *displaymatrix = ofp->displaymatrix;
1744 double theta;
1745
1746 3 theta = get_rotation(displaymatrix);
1747
1748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (fabs(theta - 90) < 1.0) {
1749 ret = insert_filter(&last_filter, &pad_idx, "transpose",
1750 displaymatrix[3] > 0 ? "cclock_flip" : "clock");
1751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (fabs(theta - 180) < 1.0) {
1752 if (displaymatrix[0] < 0) {
1753 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
1754 if (ret < 0)
1755 return ret;
1756 }
1757 if (displaymatrix[4] < 0) {
1758 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
1759 }
1760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (fabs(theta - 270) < 1.0) {
1761 ret = insert_filter(&last_filter, &pad_idx, "transpose",
1762 displaymatrix[3] < 0 ? "clock_flip" : "cclock");
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (fabs(theta) > 1.0) {
1764 char rotate_buf[64];
1765 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
1766 ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
1767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (fabs(theta) < 1.0) {
1768 if (displaymatrix && displaymatrix[4] < 0) {
1769 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
1770 }
1771 }
1772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1773 return ret;
1774
1775 3 av_frame_side_data_remove(&ofp->side_data, &ofp->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX);
1776 }
1777
1778
4/6
✓ Branch 0 taken 5565 times.
✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5565 times.
✓ Branch 4 taken 1396 times.
✗ Branch 5 not taken.
6961 if ((ofp->width || ofp->height) && (ofp->flags & OFILTER_FLAG_AUTOSCALE) &&
1779 // skip add scale for hardware format
1780
2/2
✓ Branch 0 taken 1371 times.
✓ Branch 1 taken 25 times.
1396 !(ofp->format != AV_PIX_FMT_NONE &&
1781
1/2
✓ Branch 1 taken 1371 times.
✗ Branch 2 not taken.
1371 av_pix_fmt_desc_get(ofp->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
1782 char args[255];
1783 AVFilterContext *filter;
1784 1396 const AVDictionaryEntry *e = NULL;
1785
1786 1396 snprintf(args, sizeof(args), "%d:%d",
1787 ofp->width, ofp->height);
1788
1789
2/2
✓ Branch 1 taken 1382 times.
✓ Branch 2 taken 1396 times.
2778 while ((e = av_dict_iterate(ofp->sws_opts, e))) {
1790 1382 av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
1791 }
1792
1793 1396 snprintf(name, sizeof(name), "scaler_out_%s", ofilter->output_name);
1794
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1396 times.
1396 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
1795 name, args, NULL, graph)) < 0)
1796 return ret;
1797
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1396 times.
1396 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
1798 return ret;
1799
1800 1396 last_filter = filter;
1801 1396 pad_idx = 0;
1802 }
1803
1804
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6961 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6961 av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) ||
1805 ofp->format != AV_PIX_FMT_NONE || !ofp->pix_fmts);
1806 6961 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1807 6961 choose_pix_fmts(ofp, &bprint);
1808 6961 choose_color_spaces(ofp, &bprint);
1809 6961 choose_color_ranges(ofp, &bprint);
1810 6961 choose_alpha_modes(ofp, &bprint);
1811
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6961 times.
6961 if (!av_bprint_is_complete(&bprint))
1812 return AVERROR(ENOMEM);
1813
1814
2/2
✓ Branch 0 taken 5783 times.
✓ Branch 1 taken 1178 times.
6961 if (bprint.len) {
1815 AVFilterContext *filter;
1816
1817 5783 ret = avfilter_graph_create_filter(&filter,
1818 avfilter_get_by_name("format"),
1819 5783 "format", bprint.str, NULL, graph);
1820 5783 av_bprint_finalize(&bprint, NULL);
1821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5783 times.
5783 if (ret < 0)
1822 return ret;
1823
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5783 times.
5783 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
1824 return ret;
1825
1826 5783 last_filter = filter;
1827 5783 pad_idx = 0;
1828 }
1829
1830 6961 snprintf(name, sizeof(name), "trim_out_%s", ofilter->output_name);
1831 6961 ret = insert_trim(fgp, ofp->trim_start_us, ofp->trim_duration_us,
1832 &last_filter, &pad_idx, name);
1833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6961 times.
6961 if (ret < 0)
1834 return ret;
1835
1836
1837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6961 times.
6961 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
1838 return ret;
1839
1840 6961 return 0;
1841 }
1842
1843 1438 static int configure_output_audio_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1844 OutputFilter *ofilter, AVFilterInOut *out)
1845 {
1846 1438 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1847 1438 AVFilterContext *last_filter = out->filter_ctx;
1848 1438 int pad_idx = out->pad_idx;
1849 AVBPrint args;
1850 char name[255];
1851 int ret;
1852
1853 1438 snprintf(name, sizeof(name), "out_%s", ofilter->output_name);
1854 1438 ret = avfilter_graph_create_filter(&ofilter->filter,
1855 avfilter_get_by_name("abuffersink"),
1856 name, NULL, NULL, graph);
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ret < 0)
1858 return ret;
1859
1860 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
1861 AVFilterContext *filt_ctx; \
1862 \
1863 av_log(ofilter, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
1864 "similarly to -af " filter_name "=%s.\n", arg); \
1865 \
1866 ret = avfilter_graph_create_filter(&filt_ctx, \
1867 avfilter_get_by_name(filter_name), \
1868 filter_name, arg, NULL, graph); \
1869 if (ret < 0) \
1870 goto fail; \
1871 \
1872 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
1873 if (ret < 0) \
1874 goto fail; \
1875 \
1876 last_filter = filt_ctx; \
1877 pad_idx = 0; \
1878 } while (0)
1879 1438 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
1880
1881 1438 choose_sample_fmts(ofp, &args);
1882 1438 choose_sample_rates(ofp, &args);
1883 1438 choose_channel_layouts(ofp, &args);
1884
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1438 times.
1438 if (!av_bprint_is_complete(&args)) {
1885 ret = AVERROR(ENOMEM);
1886 goto fail;
1887 }
1888
1/2
✓ Branch 0 taken 1438 times.
✗ Branch 1 not taken.
1438 if (args.len) {
1889 AVFilterContext *format;
1890
1891 1438 snprintf(name, sizeof(name), "format_out_%s", ofilter->output_name);
1892 1438 ret = avfilter_graph_create_filter(&format,
1893 avfilter_get_by_name("aformat"),
1894 1438 name, args.str, NULL, graph);
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ret < 0)
1896 goto fail;
1897
1898 1438 ret = avfilter_link(last_filter, pad_idx, format, 0);
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ret < 0)
1900 goto fail;
1901
1902 1438 last_filter = format;
1903 1438 pad_idx = 0;
1904 }
1905
1906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ofilter->apad)
1907 AUTO_INSERT_FILTER("-apad", "apad", ofilter->apad);
1908
1909 1438 snprintf(name, sizeof(name), "trim for output %s", ofilter->output_name);
1910 1438 ret = insert_trim(fgp, ofp->trim_start_us, ofp->trim_duration_us,
1911 &last_filter, &pad_idx, name);
1912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ret < 0)
1913 goto fail;
1914
1915
1/2
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
1438 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
1916 goto fail;
1917 1438 fail:
1918 1438 av_bprint_finalize(&args, NULL);
1919
1920 1438 return ret;
1921 }
1922
1923 8399 static int configure_output_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1924 OutputFilter *ofilter, AVFilterInOut *out)
1925 {
1926
2/3
✓ Branch 0 taken 6961 times.
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
8399 switch (ofilter->type) {
1927 6961 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fgp, graph, ofilter, out);
1928 1438 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fgp, graph, ofilter, out);
1929 default: av_assert0(0); return 0;
1930 }
1931 }
1932
1933 4 static void sub2video_prepare(InputFilterPriv *ifp)
1934 {
1935 4 ifp->sub2video.last_pts = INT64_MIN;
1936 4 ifp->sub2video.end_pts = INT64_MIN;
1937
1938 /* sub2video structure has been (re-)initialized.
1939 Mark it as such so that the system will be
1940 initialized with the first received heartbeat. */
1941 4 ifp->sub2video.initialize = 1;
1942 4 }
1943
1944 5796 static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
1945 InputFilter *ifilter, AVFilterInOut *in)
1946 {
1947 5796 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
1948
1949 AVFilterContext *last_filter;
1950 5796 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
1951 const AVPixFmtDescriptor *desc;
1952 char name[255];
1953 5796 int ret, pad_idx = 0;
1954 5796 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
1955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 if (!par)
1956 return AVERROR(ENOMEM);
1957
1958
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5792 times.
5796 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
1959 4 sub2video_prepare(ifp);
1960
1961 5796 snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index,
1962 ifp->opts.name);
1963
1964 5796 ifilter->filter = avfilter_graph_alloc_filter(graph, buffer_filt, name);
1965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 if (!ifilter->filter) {
1966 ret = AVERROR(ENOMEM);
1967 goto fail;
1968 }
1969
1970 5796 par->format = ifp->format;
1971 5796 par->time_base = ifp->time_base;
1972 5796 par->frame_rate = ifp->opts.framerate;
1973 5796 par->width = ifp->width;
1974 5796 par->height = ifp->height;
1975 5796 par->sample_aspect_ratio = ifp->sample_aspect_ratio.den > 0 ?
1976
2/2
✓ Branch 0 taken 5792 times.
✓ Branch 1 taken 4 times.
5796 ifp->sample_aspect_ratio : (AVRational){ 0, 1 };
1977 5796 par->color_space = ifp->color_space;
1978 5796 par->color_range = ifp->color_range;
1979 5796 par->alpha_mode = ifp->alpha_mode;
1980 5796 par->hw_frames_ctx = ifp->hw_frames_ctx;
1981 5796 par->side_data = ifp->side_data;
1982 5796 par->nb_side_data = ifp->nb_side_data;
1983
1984 5796 ret = av_buffersrc_parameters_set(ifilter->filter, par);
1985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 if (ret < 0)
1986 goto fail;
1987 5796 av_freep(&par);
1988
1989 5796 ret = avfilter_init_dict(ifilter->filter, NULL);
1990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 if (ret < 0)
1991 goto fail;
1992
1993 5796 last_filter = ifilter->filter;
1994
1995 5796 desc = av_pix_fmt_desc_get(ifp->format);
1996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 av_assert0(desc);
1997
1998
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5790 times.
5796 if ((ifp->opts.flags & IFILTER_FLAG_CROP)) {
1999 char crop_buf[64];
2000 6 snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u",
2001 ifp->opts.crop_left, ifp->opts.crop_right,
2002 ifp->opts.crop_top, ifp->opts.crop_bottom,
2003 ifp->opts.crop_left, ifp->opts.crop_top);
2004 6 ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf);
2005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
2006 return ret;
2007 }
2008
2009 // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
2010 5796 ifp->displaymatrix_applied = 0;
2011
2/2
✓ Branch 0 taken 5791 times.
✓ Branch 1 taken 5 times.
5796 if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
2012
1/2
✓ Branch 0 taken 5791 times.
✗ Branch 1 not taken.
5791 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
2013 5791 int32_t *displaymatrix = ifp->displaymatrix;
2014 double theta;
2015
2016 5791 theta = get_rotation(displaymatrix);
2017
2018
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5776 times.
5791 if (fabs(theta - 90) < 1.0) {
2019 15 ret = insert_filter(&last_filter, &pad_idx, "transpose",
2020
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
15 displaymatrix[3] > 0 ? "cclock_flip" : "clock");
2021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5776 times.
5776 } else if (fabs(theta - 180) < 1.0) {
2022 if (displaymatrix[0] < 0) {
2023 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
2024 if (ret < 0)
2025 return ret;
2026 }
2027 if (displaymatrix[4] < 0) {
2028 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
2029 }
2030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5776 times.
5776 } else if (fabs(theta - 270) < 1.0) {
2031 ret = insert_filter(&last_filter, &pad_idx, "transpose",
2032 displaymatrix[3] < 0 ? "clock_flip" : "cclock");
2033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5776 times.
5776 } else if (fabs(theta) > 1.0) {
2034 char rotate_buf[64];
2035 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
2036 ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
2037
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5767 times.
5776 } else if (fabs(theta) < 1.0) {
2038
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (displaymatrix && displaymatrix[4] < 0) {
2039 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
2040 }
2041 }
2042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5791 times.
5791 if (ret < 0)
2043 return ret;
2044
2045 5791 ifp->displaymatrix_applied = 1;
2046 }
2047
2048 5796 snprintf(name, sizeof(name), "trim_in_%s", ifp->opts.name);
2049 5796 ret = insert_trim(fg, ifp->opts.trim_start_us, ifp->opts.trim_end_us,
2050 &last_filter, &pad_idx, name);
2051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5796 times.
5796 if (ret < 0)
2052 return ret;
2053
2054
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5796 times.
5796 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
2055 return ret;
2056 5796 return 0;
2057 fail:
2058 av_freep(&par);
2059
2060 return ret;
2061 }
2062
2063 1375 static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
2064 InputFilter *ifilter, AVFilterInOut *in)
2065 {
2066 1375 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2067 AVFilterContext *last_filter;
2068 AVBufferSrcParameters *par;
2069 1375 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
2070 AVBPrint args;
2071 char name[255];
2072 1375 int ret, pad_idx = 0;
2073
2074 1375 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
2075 1375 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
2076 ifp->time_base.num, ifp->time_base.den,
2077 ifp->sample_rate,
2078 1375 av_get_sample_fmt_name(ifp->format));
2079
1/2
✓ Branch 1 taken 1375 times.
✗ Branch 2 not taken.
1375 if (av_channel_layout_check(&ifp->ch_layout) &&
2080
2/2
✓ Branch 0 taken 1355 times.
✓ Branch 1 taken 20 times.
1375 ifp->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
2081 1355 av_bprintf(&args, ":channel_layout=");
2082 1355 av_channel_layout_describe_bprint(&ifp->ch_layout, &args);
2083 } else
2084 20 av_bprintf(&args, ":channels=%d", ifp->ch_layout.nb_channels);
2085 1375 snprintf(name, sizeof(name), "graph_%d_in_%s", fg->index, ifp->opts.name);
2086
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1375 times.
1375 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
2088 1375 name, args.str, NULL,
2089 graph)) < 0)
2090 return ret;
2091 1375 par = av_buffersrc_parameters_alloc();
2092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1375 times.
1375 if (!par)
2093 return AVERROR(ENOMEM);
2094 1375 par->side_data = ifp->side_data;
2095 1375 par->nb_side_data = ifp->nb_side_data;
2096 1375 ret = av_buffersrc_parameters_set(ifilter->filter, par);
2097 1375 av_free(par);
2098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1375 times.
1375 if (ret < 0)
2099 return ret;
2100 1375 last_filter = ifilter->filter;
2101
2102 1375 snprintf(name, sizeof(name), "trim for input stream %s", ifp->opts.name);
2103 1375 ret = insert_trim(fg, ifp->opts.trim_start_us, ifp->opts.trim_end_us,
2104 &last_filter, &pad_idx, name);
2105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1375 times.
1375 if (ret < 0)
2106 return ret;
2107
2108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1375 times.
1375 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
2109 return ret;
2110
2111 1375 return 0;
2112 }
2113
2114 7171 static int configure_input_filter(FilterGraph *fg, AVFilterGraph *graph,
2115 InputFilter *ifilter, AVFilterInOut *in)
2116 {
2117
2/3
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 1375 times.
✗ Branch 2 not taken.
7171 switch (ifilter->type) {
2118 5796 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, graph, ifilter, in);
2119 1375 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, graph, ifilter, in);
2120 default: av_assert0(0); return 0;
2121 }
2122 }
2123
2124 8265 static void cleanup_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
2125 {
2126
2/2
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 8265 times.
16665 for (int i = 0; i < fg->nb_outputs; i++)
2127 8400 fg->outputs[i]->filter = NULL;
2128
2/2
✓ Branch 0 taken 7172 times.
✓ Branch 1 taken 8265 times.
15437 for (int i = 0; i < fg->nb_inputs; i++)
2129 7172 fg->inputs[i]->filter = NULL;
2130 8265 avfilter_graph_free(&fgt->graph);
2131 8265 }
2132
2133 9518 static int filter_is_buffersrc(const AVFilterContext *f)
2134 {
2135
2/2
✓ Branch 0 taken 4285 times.
✓ Branch 1 taken 5233 times.
13803 return f->nb_inputs == 0 &&
2136
2/2
✓ Branch 0 taken 1894 times.
✓ Branch 1 taken 2391 times.
4285 (!strcmp(f->filter->name, "buffer") ||
2137
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 1199 times.
1894 !strcmp(f->filter->name, "abuffer"));
2138 }
2139
2140 8263 static int graph_is_meta(AVFilterGraph *graph)
2141 {
2142
2/2
✓ Branch 0 taken 18209 times.
✓ Branch 1 taken 1831 times.
20040 for (unsigned i = 0; i < graph->nb_filters; i++) {
2143 18209 const AVFilterContext *f = graph->filters[i];
2144
2145 /* in addition to filters flagged as meta, also
2146 * disregard sinks and buffersources (but not other sources,
2147 * since they introduce data we are not aware of)
2148 */
2149
4/4
✓ Branch 0 taken 12604 times.
✓ Branch 1 taken 5605 times.
✓ Branch 2 taken 6432 times.
✓ Branch 3 taken 3086 times.
27727 if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
2150
2/2
✓ Branch 0 taken 9518 times.
✓ Branch 1 taken 3086 times.
12604 f->nb_outputs == 0 ||
2151 9518 filter_is_buffersrc(f)))
2152 6432 return 0;
2153 }
2154 1831 return 1;
2155 }
2156
2157 static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer);
2158
2159 8264 static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
2160 {
2161 8264 FilterGraphPriv *fgp = fgp_from_fg(fg);
2162 AVBufferRef *hw_device;
2163 AVFilterInOut *inputs, *outputs, *cur;
2164 8264 int ret = AVERROR_BUG, i, simple = filtergraph_is_simple(fg);
2165 8264 int have_input_eof = 0;
2166 8264 const char *graph_desc = fg->graph_desc;
2167
2168 8264 cleanup_filtergraph(fg, fgt);
2169 8264 fgt->graph = avfilter_graph_alloc();
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8264 times.
8264 if (!fgt->graph)
2171 return AVERROR(ENOMEM);
2172
2173
2/2
✓ Branch 0 taken 6993 times.
✓ Branch 1 taken 1271 times.
8264 if (simple) {
2174 6993 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
2175
2176
2/2
✓ Branch 0 taken 391 times.
✓ Branch 1 taken 6602 times.
6993 if (filter_nbthreads) {
2177 391 ret = av_opt_set(fgt->graph, "threads", filter_nbthreads, 0);
2178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391 times.
391 if (ret < 0)
2179 goto fail;
2180
2/2
✓ Branch 0 taken 4099 times.
✓ Branch 1 taken 2503 times.
6602 } else if (fgp->nb_threads >= 0) {
2181 4099 ret = av_opt_set_int(fgt->graph, "threads", fgp->nb_threads, 0);
2182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4099 times.
4099 if (ret < 0)
2183 return ret;
2184 }
2185
2186
2/2
✓ Branch 1 taken 4465 times.
✓ Branch 2 taken 2528 times.
6993 if (av_dict_count(ofp->sws_opts)) {
2187 4465 ret = av_dict_get_string(ofp->sws_opts,
2188 4465 &fgt->graph->scale_sws_opts,
2189 '=', ':');
2190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4465 times.
4465 if (ret < 0)
2191 goto fail;
2192 }
2193
2194
2/2
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 6928 times.
6993 if (av_dict_count(ofp->swr_opts)) {
2195 char *args;
2196 65 ret = av_dict_get_string(ofp->swr_opts, &args, '=', ':');
2197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (ret < 0)
2198 goto fail;
2199 65 av_opt_set(fgt->graph, "aresample_swr_opts", args, 0);
2200 65 av_free(args);
2201 }
2202 } else {
2203 1271 fgt->graph->nb_threads = filter_complex_nbthreads;
2204 }
2205
2206
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8263 times.
8264 if (filter_buffered_frames) {
2207 1 ret = av_opt_set_int(fgt->graph, "max_buffered_frames", filter_buffered_frames, 0);
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
2209 return ret;
2210 }
2211
2212 8264 hw_device = hw_device_for_filter();
2213
2214 8264 ret = graph_parse(fg, fgt->graph, graph_desc, &inputs, &outputs, hw_device);
2215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8264 times.
8264 if (ret < 0)
2216 goto fail;
2217
2218
2/2
✓ Branch 0 taken 7171 times.
✓ Branch 1 taken 8264 times.
15435 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
2219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7171 times.
7171 if ((ret = configure_input_filter(fg, fgt->graph, fg->inputs[i], cur)) < 0) {
2220 avfilter_inout_free(&inputs);
2221 avfilter_inout_free(&outputs);
2222 goto fail;
2223 }
2224 8264 avfilter_inout_free(&inputs);
2225
2226
2/2
✓ Branch 0 taken 8399 times.
✓ Branch 1 taken 8264 times.
16663 for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
2227 8399 ret = configure_output_filter(fgp, fgt->graph, fg->outputs[i], cur);
2228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8399 times.
8399 if (ret < 0) {
2229 avfilter_inout_free(&outputs);
2230 goto fail;
2231 }
2232 }
2233 8264 avfilter_inout_free(&outputs);
2234
2235
2/2
✓ Branch 0 taken 6344 times.
✓ Branch 1 taken 1920 times.
8264 if (fgp->disable_conversions)
2236 6344 avfilter_graph_set_auto_convert(fgt->graph, AVFILTER_AUTO_CONVERT_NONE);
2237
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8263 times.
8264 if ((ret = avfilter_graph_config(fgt->graph, NULL)) < 0)
2238 1 goto fail;
2239
2240 8263 fgp->is_meta = graph_is_meta(fgt->graph);
2241
2242 /* limit the lists of allowed formats to the ones selected, to
2243 * make sure they stay the same if the filtergraph is reconfigured later */
2244
2/2
✓ Branch 0 taken 8398 times.
✓ Branch 1 taken 8263 times.
16661 for (int i = 0; i < fg->nb_outputs; i++) {
2245 const AVFrameSideData *const *sd;
2246 int nb_sd;
2247 8398 OutputFilter *ofilter = fg->outputs[i];
2248 8398 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
2249 8398 AVFilterContext *sink = ofilter->filter;
2250
2251 8398 ofp->format = av_buffersink_get_format(sink);
2252
2253 8398 ofp->width = av_buffersink_get_w(sink);
2254 8398 ofp->height = av_buffersink_get_h(sink);
2255 8398 ofp->color_space = av_buffersink_get_colorspace(sink);
2256 8398 ofp->color_range = av_buffersink_get_color_range(sink);
2257 8398 ofp->alpha_mode = av_buffersink_get_alpha_mode(sink);
2258
2259 // If the timing parameters are not locked yet, get the tentative values
2260 // here but don't lock them. They will only be used if no output frames
2261 // are ever produced.
2262
2/2
✓ Branch 0 taken 8351 times.
✓ Branch 1 taken 47 times.
8398 if (!ofp->tb_out_locked) {
2263 8351 AVRational fr = av_buffersink_get_frame_rate(sink);
2264
3/4
✓ Branch 0 taken 8330 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 8330 times.
✗ Branch 3 not taken.
8351 if (ofp->fps.framerate.num <= 0 && ofp->fps.framerate.den <= 0 &&
2265
4/4
✓ Branch 0 taken 6891 times.
✓ Branch 1 taken 1439 times.
✓ Branch 2 taken 6877 times.
✓ Branch 3 taken 14 times.
8330 fr.num > 0 && fr.den > 0)
2266 6877 ofp->fps.framerate = fr;
2267 8351 ofp->tb_out = av_buffersink_get_time_base(sink);
2268 }
2269 8398 ofp->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
2270
2271 8398 ofp->sample_rate = av_buffersink_get_sample_rate(sink);
2272 8398 av_channel_layout_uninit(&ofp->ch_layout);
2273 8398 ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout);
2274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8398 times.
8398 if (ret < 0)
2275 goto fail;
2276 8398 sd = av_buffersink_get_side_data(sink, &nb_sd);
2277
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 8135 times.
8398 if (nb_sd)
2278
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 263 times.
546 for (int j = 0; j < nb_sd; j++) {
2279 283 ret = av_frame_side_data_clone(&ofp->side_data, &ofp->nb_side_data,
2280 283 sd[j], AV_FRAME_SIDE_DATA_FLAG_REPLACE);
2281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (ret < 0) {
2282 av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data);
2283 goto fail;
2284 }
2285 }
2286 }
2287
2288
2/2
✓ Branch 0 taken 7170 times.
✓ Branch 1 taken 8263 times.
15433 for (int i = 0; i < fg->nb_inputs; i++) {
2289 7170 InputFilter *ifilter = fg->inputs[i];
2290 7170 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
2291 AVFrame *tmp;
2292
2/2
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 7170 times.
7356 while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) {
2293
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 147 times.
186 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
2294 39 sub2video_frame(&ifp->ifilter, tmp, !fgt->graph);
2295 } else {
2296
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 101 times.
147 if (ifp->type_src == AVMEDIA_TYPE_VIDEO) {
2297
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ifp->displaymatrix_applied)
2298 46 av_frame_remove_side_data(tmp, AV_FRAME_DATA_DISPLAYMATRIX);
2299 }
2300 147 ret = av_buffersrc_add_frame(ifilter->filter, tmp);
2301 }
2302 186 av_frame_free(&tmp);
2303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (ret < 0)
2304 goto fail;
2305 }
2306 }
2307
2308 /* send the EOFs for the finished inputs */
2309
2/2
✓ Branch 0 taken 7170 times.
✓ Branch 1 taken 8263 times.
15433 for (int i = 0; i < fg->nb_inputs; i++) {
2310 7170 InputFilter *ifilter = fg->inputs[i];
2311
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7153 times.
7170 if (fgt->eof_in[i]) {
2312 17 ret = av_buffersrc_add_frame(ifilter->filter, NULL);
2313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
2314 goto fail;
2315 17 have_input_eof = 1;
2316 }
2317 }
2318
2319
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8252 times.
8263 if (have_input_eof) {
2320 // make sure the EOF propagates to the end of the graph
2321 11 ret = avfilter_graph_request_oldest(fgt->graph);
2322
4/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
11 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2323 goto fail;
2324 }
2325
2326 8263 return 0;
2327 1 fail:
2328 1 cleanup_filtergraph(fg, fgt);
2329 1 return ret;
2330 }
2331
2332 7165 static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
2333 {
2334 7165 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2335 AVFrameSideData *sd;
2336 int ret;
2337
2338 7165 ret = av_buffer_replace(&ifp->hw_frames_ctx, frame->hw_frames_ctx);
2339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7165 times.
7165 if (ret < 0)
2340 return ret;
2341
2342
2/2
✓ Branch 0 taken 1372 times.
✓ Branch 1 taken 5793 times.
12926 ifp->time_base = (ifilter->type == AVMEDIA_TYPE_AUDIO) ? (AVRational){ 1, frame->sample_rate } :
2343
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5761 times.
5793 (ifp->opts.flags & IFILTER_FLAG_CFR) ? av_inv_q(ifp->opts.framerate) :
2344 frame->time_base;
2345
2346 7165 ifp->format = frame->format;
2347
2348 7165 ifp->width = frame->width;
2349 7165 ifp->height = frame->height;
2350 7165 ifp->sample_aspect_ratio = frame->sample_aspect_ratio;
2351 7165 ifp->color_space = frame->colorspace;
2352 7165 ifp->color_range = frame->color_range;
2353 7165 ifp->alpha_mode = frame->alpha_mode;
2354
2355 7165 ifp->sample_rate = frame->sample_rate;
2356 7165 ret = av_channel_layout_copy(&ifp->ch_layout, &frame->ch_layout);
2357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7165 times.
7165 if (ret < 0)
2358 return ret;
2359
2360 7165 av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
2361
2/2
✓ Branch 0 taken 631 times.
✓ Branch 1 taken 7165 times.
7796 for (int i = 0; i < frame->nb_side_data; i++) {
2362 631 const AVSideDataDescriptor *desc = av_frame_side_data_desc(frame->side_data[i]->type);
2363
2364
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 297 times.
631 if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL))
2365 334 continue;
2366
2367 297 ret = av_frame_side_data_clone(&ifp->side_data,
2368 &ifp->nb_side_data,
2369 297 frame->side_data[i], 0);
2370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (ret < 0)
2371 return ret;
2372 }
2373
2374 7165 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
2375
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 7138 times.
7165 if (sd) {
2376 27 memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
2377
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 if (ifp->opts.flags & IFILTER_FLAG_AUTOROTATE)
2378 24 av_frame_side_data_remove(&ifp->side_data, &ifp->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX);
2379 }
2380 7165 ifp->displaymatrix_present = !!sd;
2381
2382 /* Copy downmix related side data to InputFilterPriv so it may be propagated
2383 * to the filter chain even though it's not "global", as filters like aresample
2384 * require this information during init and not when remixing a frame */
2385 7165 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO);
2386
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7157 times.
7165 if (sd) {
2387 8 ret = av_frame_side_data_clone(&ifp->side_data,
2388 &ifp->nb_side_data, sd, 0);
2389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
2390 return ret;
2391 8 memcpy(&ifp->downmixinfo, sd->data, sizeof(ifp->downmixinfo));
2392 }
2393 7165 ifp->downmixinfo_present = !!sd;
2394 7165 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_MATRIX);
2395
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7161 times.
7165 if (sd) {
2396 4 ret = av_frame_side_data_clone(&ifp->side_data,
2397 &ifp->nb_side_data, sd, 0);
2398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
2399 return ret;
2400 4 ret = av_buffer_replace(&ifp->downmixmatrix, sd->buf);
2401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
2402 return ret;
2403 4 ifp->downmixmatrix_size = sd->size;
2404 }
2405 7165 ifp->downmixmatrix_present = !!sd;
2406
2407 7165 return 0;
2408 }
2409
2410 1 static int ifilter_parameters_from_ofilter(InputFilter *ifilter, OutputFilter *ofilter)
2411 {
2412 1 const OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
2413 1 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2414
2415
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!ifp->opts.framerate.num) {
2416 1 ifp->opts.framerate = ofp->fps.framerate;
2417
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (ifp->opts.framerate.num > 0 && ifp->opts.framerate.den > 0)
2418 1 ifp->opts.flags |= IFILTER_FLAG_CFR;
2419 }
2420
2421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (int i = 0; i < ofp->nb_side_data; i++) {
2422 int ret = av_frame_side_data_clone(&ifp->side_data, &ifp->nb_side_data,
2423 ofp->side_data[i], AV_FRAME_SIDE_DATA_FLAG_REPLACE);
2424 if (ret < 0)
2425 return ret;
2426 }
2427
2428 1 return 0;
2429 }
2430
2431 39077 int filtergraph_is_simple(const FilterGraph *fg)
2432 {
2433 39077 const FilterGraphPriv *fgp = cfgp_from_cfg(fg);
2434 39077 return fgp->is_simple;
2435 }
2436
2437 static void send_command(FilterGraph *fg, AVFilterGraph *graph,
2438 double time, const char *target,
2439 const char *command, const char *arg, int all_filters)
2440 {
2441 int ret;
2442
2443 if (!graph)
2444 return;
2445
2446 if (time < 0) {
2447 char response[4096];
2448 ret = avfilter_graph_send_command(graph, target, command, arg,
2449 response, sizeof(response),
2450 all_filters ? 0 : AVFILTER_CMD_FLAG_ONE);
2451 fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s",
2452 fg->index, ret, response);
2453 } else if (!all_filters) {
2454 fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
2455 } else {
2456 ret = avfilter_graph_queue_command(graph, target, command, arg, 0, time);
2457 if (ret < 0)
2458 fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
2459 }
2460 }
2461
2462 420808 static int choose_input(const FilterGraph *fg, const FilterGraphThread *fgt)
2463 {
2464 420808 int nb_requests, nb_requests_max = -1;
2465 420808 int best_input = -1;
2466
2467
2/2
✓ Branch 0 taken 435751 times.
✓ Branch 1 taken 420808 times.
856559 for (int i = 0; i < fg->nb_inputs; i++) {
2468 435751 InputFilter *ifilter = fg->inputs[i];
2469
2470
2/2
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 434505 times.
435751 if (fgt->eof_in[i])
2471 1246 continue;
2472
2473 434505 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
2474
2/2
✓ Branch 0 taken 423355 times.
✓ Branch 1 taken 11150 times.
434505 if (nb_requests > nb_requests_max) {
2475 423355 nb_requests_max = nb_requests;
2476 423355 best_input = i;
2477 }
2478 }
2479
2480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 420808 times.
420808 av_assert0(best_input >= 0);
2481
2482 420808 return best_input;
2483 }
2484
2485 8348 static int choose_out_timebase(OutputFilterPriv *ofp, AVFrame *frame)
2486 {
2487 8348 OutputFilter *ofilter = &ofp->ofilter;
2488 8348 FPSConvContext *fps = &ofp->fps;
2489 8348 AVRational tb = (AVRational){ 0, 0 };
2490 AVRational fr;
2491 const FrameData *fd;
2492
2493 8348 fd = frame_data_c(frame);
2494
2495 // apply -enc_time_base
2496
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8347 times.
8348 if (ofp->enc_timebase.num == ENC_TIME_BASE_DEMUX &&
2497
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 (fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0)) {
2498 av_log(ofp, AV_LOG_ERROR,
2499 "Demuxing timebase not available - cannot use it for encoding\n");
2500 return AVERROR(EINVAL);
2501 }
2502
2503
2/4
✓ Branch 0 taken 8347 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8348 switch (ofp->enc_timebase.num) {
2504 8347 case 0: break;
2505 1 case ENC_TIME_BASE_DEMUX: tb = fd->dec.tb; break;
2506 case ENC_TIME_BASE_FILTER: tb = frame->time_base; break;
2507 default: tb = ofp->enc_timebase; break;
2508 }
2509
2510
2/2
✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 6914 times.
8348 if (ofilter->type == AVMEDIA_TYPE_AUDIO) {
2511
1/2
✓ Branch 0 taken 1434 times.
✗ Branch 1 not taken.
1434 tb = tb.num ? tb : (AVRational){ 1, frame->sample_rate };
2512 1434 goto finish;
2513 }
2514
2515 6914 fr = fps->framerate;
2516
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6898 times.
6914 if (!fr.num) {
2517 16 AVRational fr_sink = av_buffersink_get_frame_rate(ofilter->filter);
2518
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
16 if (fr_sink.num > 0 && fr_sink.den > 0)
2519 fr = fr_sink;
2520 }
2521
2522
4/4
✓ Branch 0 taken 5758 times.
✓ Branch 1 taken 1156 times.
✓ Branch 2 taken 365 times.
✓ Branch 3 taken 5393 times.
6914 if (fps->vsync_method == VSYNC_CFR || fps->vsync_method == VSYNC_VSCFR) {
2523
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1521 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1521 if (!fr.num && !fps->framerate_max.num) {
2524 fr = (AVRational){25, 1};
2525 av_log(ofp, AV_LOG_WARNING,
2526 "No information "
2527 "about the input framerate is available. Falling "
2528 "back to a default value of 25fps. Use the -r option "
2529 "if you want a different framerate.\n");
2530 }
2531
2532
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1521 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1521 if (fps->framerate_max.num &&
2533 (av_q2d(fr) > av_q2d(fps->framerate_max) ||
2534 !fr.den))
2535 fr = fps->framerate_max;
2536 }
2537
2538
2/2
✓ Branch 0 taken 6898 times.
✓ Branch 1 taken 16 times.
6914 if (fr.num > 0) {
2539
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 6822 times.
6898 if (fps->framerate_supported) {
2540 76 int idx = av_find_nearest_q_idx(fr, fps->framerate_supported);
2541 76 fr = fps->framerate_supported[idx];
2542 }
2543
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 6833 times.
6898 if (fps->framerate_clip) {
2544 65 av_reduce(&fr.num, &fr.den,
2545 65 fr.num, fr.den, fps->framerate_clip);
2546 }
2547 }
2548
2549
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6913 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
6914 if (!(tb.num > 0 && tb.den > 0))
2550 6913 tb = av_inv_q(fr);
2551
3/4
✓ Branch 0 taken 6898 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6898 times.
6914 if (!(tb.num > 0 && tb.den > 0))
2552 16 tb = frame->time_base;
2553
2554 6914 fps->framerate = fr;
2555 8348 finish:
2556 8348 ofp->tb_out = tb;
2557 8348 ofp->tb_out_locked = 1;
2558
2559 8348 return 0;
2560 }
2561
2562 143852 static double adjust_frame_pts_to_encoder_tb(void *logctx, AVFrame *frame,
2563 AVRational tb_dst, int64_t start_time)
2564 {
2565 143852 double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
2566
2567 143852 AVRational tb = tb_dst;
2568 143852 AVRational filter_tb = frame->time_base;
2569 143852 const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
2570
2571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143852 times.
143852 if (frame->pts == AV_NOPTS_VALUE)
2572 goto early_exit;
2573
2574 143852 tb.den <<= extra_bits;
2575 143852 float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
2576 143852 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
2577 143852 float_pts /= 1 << extra_bits;
2578 // when float_pts is not exactly an integer,
2579 // avoid exact midpoints to reduce the chance of rounding differences, this
2580 // can be removed in case the fps code is changed to work with integers
2581
2/2
✓ Branch 0 taken 6023 times.
✓ Branch 1 taken 137829 times.
143852 if (float_pts != llrint(float_pts))
2582
1/2
✓ Branch 0 taken 6023 times.
✗ Branch 1 not taken.
6023 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
2583
2584 143852 frame->pts = av_rescale_q(frame->pts, filter_tb, tb_dst) -
2585 143852 av_rescale_q(start_time, AV_TIME_BASE_Q, tb_dst);
2586 143852 frame->time_base = tb_dst;
2587
2588 143852 early_exit:
2589
2590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143852 times.
143852 if (debug_ts) {
2591 av_log(logctx, AV_LOG_INFO,
2592 "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
2593 frame ? av_ts2str(frame->pts) : "NULL",
2594 av_ts2timestr(frame->pts, &tb_dst),
2595 float_pts, tb_dst.num, tb_dst.den);
2596 }
2597
2598 143852 return float_pts;
2599 }
2600
2601 3867 static int64_t median3(int64_t a, int64_t b, int64_t c)
2602 {
2603 int64_t max2, min2, m;
2604
2605
1/2
✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
3867 if (a >= b) {
2606 3867 max2 = a;
2607 3867 min2 = b;
2608 } else {
2609 max2 = b;
2610 min2 = a;
2611 }
2612 3867 m = (c >= max2) ? max2 : c;
2613
2614 3867 return (m >= min2) ? m : min2;
2615 }
2616
2617
2618 /* Convert frame timestamps to the encoder timebase and decide how many times
2619 * should this (and possibly previous) frame be repeated in order to conform to
2620 * desired target framerate (if any).
2621 */
2622 147719 static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame,
2623 int64_t *nb_frames, int64_t *nb_frames_prev)
2624 {
2625 147719 OutputFilter *ofilter = &ofp->ofilter;
2626 147719 FPSConvContext *fps = &ofp->fps;
2627 double delta0, delta, sync_ipts, duration;
2628
2629
2/2
✓ Branch 0 taken 3867 times.
✓ Branch 1 taken 143852 times.
147719 if (!frame) {
2630 3867 *nb_frames_prev = *nb_frames = median3(fps->frames_prev_hist[0],
2631 fps->frames_prev_hist[1],
2632 fps->frames_prev_hist[2]);
2633
2634
4/4
✓ Branch 0 taken 3866 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3864 times.
3867 if (!*nb_frames && fps->last_dropped) {
2635 2 atomic_fetch_add(&ofilter->nb_frames_drop, 1);
2636 2 fps->last_dropped++;
2637 }
2638
2639 3867 goto finish;
2640 }
2641
2642 143852 duration = frame->duration * av_q2d(frame->time_base) / av_q2d(ofp->tb_out);
2643
2644 143852 sync_ipts = adjust_frame_pts_to_encoder_tb(ofilter->graph, frame,
2645 ofp->tb_out, ofp->ts_offset);
2646 /* delta0 is the "drift" between the input frame and
2647 * where it would fall in the output. */
2648 143852 delta0 = sync_ipts - ofp->next_pts;
2649 143852 delta = delta0 + duration;
2650
2651 // tracks the number of times the PREVIOUS frame should be duplicated,
2652 // mostly for variable framerate (VFR)
2653 143852 *nb_frames_prev = 0;
2654 /* by default, we output a single frame */
2655 143852 *nb_frames = 1;
2656
2657
4/4
✓ Branch 0 taken 5034 times.
✓ Branch 1 taken 138818 times.
✓ Branch 2 taken 3847 times.
✓ Branch 3 taken 1187 times.
143852 if (delta0 < 0 &&
2658 3847 delta > 0 &&
2659
1/2
✓ Branch 0 taken 3847 times.
✗ Branch 1 not taken.
3847 fps->vsync_method != VSYNC_PASSTHROUGH) {
2660
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 3802 times.
3847 if (delta0 < -0.6) {
2661 45 av_log(ofp, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
2662 } else
2663 3802 av_log(ofp, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
2664 3847 sync_ipts = ofp->next_pts;
2665 3847 duration += delta0;
2666 3847 delta0 = 0;
2667 }
2668
2669
4/5
✓ Branch 0 taken 7838 times.
✓ Branch 1 taken 29014 times.
✓ Branch 2 taken 84099 times.
✓ Branch 3 taken 22901 times.
✗ Branch 4 not taken.
143852 switch (fps->vsync_method) {
2670 7838 case VSYNC_VSCFR:
2671
3/4
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 7473 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 365 times.
7838 if (fps->frame_number == 0 && delta0 >= 0.5) {
2672 av_log(ofp, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
2673 delta = duration;
2674 delta0 = 0;
2675 ofp->next_pts = llrint(sync_ipts);
2676 }
2677 av_fallthrough;
2678 case VSYNC_CFR:
2679 // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
2680
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 36852 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
36852 if (frame_drop_threshold && delta < frame_drop_threshold && fps->frame_number) {
2681 *nb_frames = 0;
2682
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 36477 times.
36852 } else if (delta < -1.1)
2683 375 *nb_frames = 0;
2684
2/2
✓ Branch 0 taken 677 times.
✓ Branch 1 taken 35800 times.
36477 else if (delta > 1.1) {
2685 677 *nb_frames = llrintf(delta);
2686
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 675 times.
677 if (delta0 > 1.1)
2687 2 *nb_frames_prev = llrintf(delta0 - 0.6);
2688 }
2689 36852 frame->duration = 1;
2690 36852 break;
2691 84099 case VSYNC_VFR:
2692
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 84000 times.
84099 if (delta <= -0.6)
2693 99 *nb_frames = 0;
2694
2/2
✓ Branch 0 taken 66647 times.
✓ Branch 1 taken 17353 times.
84000 else if (delta > 0.6)
2695 66647 ofp->next_pts = llrint(sync_ipts);
2696 84099 frame->duration = llrint(duration);
2697 84099 break;
2698 22901 case VSYNC_PASSTHROUGH:
2699 22901 ofp->next_pts = llrint(sync_ipts);
2700 22901 frame->duration = llrint(duration);
2701 22901 break;
2702 default:
2703 av_assert0(0);
2704 }
2705
2706 147719 finish:
2707 147719 memmove(fps->frames_prev_hist + 1,
2708 147719 fps->frames_prev_hist,
2709 sizeof(fps->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(fps->frames_prev_hist) - 1));
2710 147719 fps->frames_prev_hist[0] = *nb_frames_prev;
2711
2712
4/4
✓ Branch 0 taken 147716 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 474 times.
✓ Branch 3 taken 147242 times.
147719 if (*nb_frames_prev == 0 && fps->last_dropped) {
2713 474 atomic_fetch_add(&ofilter->nb_frames_drop, 1);
2714 474 av_log(ofp, AV_LOG_VERBOSE,
2715 "*** dropping frame %"PRId64" at ts %"PRId64"\n",
2716 474 fps->frame_number, fps->last_frame->pts);
2717 }
2718
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 147716 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 269 times.
✓ Branch 5 taken 147450 times.
147719 if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) {
2719 uint64_t nb_frames_dup;
2720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (*nb_frames > dts_error_threshold * 30) {
2721 av_log(ofp, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1);
2722 atomic_fetch_add(&ofilter->nb_frames_drop, 1);
2723 *nb_frames = 0;
2724 return;
2725 }
2726
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 266 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
269 nb_frames_dup = atomic_fetch_add(&ofilter->nb_frames_dup,
2727 *nb_frames - (*nb_frames_prev && fps->last_dropped) - (*nb_frames > *nb_frames_prev));
2728 269 av_log(ofp, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1);
2729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (nb_frames_dup > fps->dup_warning) {
2730 av_log(ofp, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning);
2731 fps->dup_warning *= 10;
2732 }
2733 }
2734
2735
4/4
✓ Branch 0 taken 4341 times.
✓ Branch 1 taken 143378 times.
✓ Branch 2 taken 474 times.
✓ Branch 3 taken 3867 times.
147719 fps->last_dropped = *nb_frames == *nb_frames_prev && frame;
2736
4/4
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 147245 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 467 times.
147719 fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY);
2737 }
2738
2739 1778 static void close_input(InputFilterPriv *ifp)
2740 {
2741 1778 FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
2742
2743
2/2
✓ Branch 0 taken 409 times.
✓ Branch 1 taken 1369 times.
1778 if (!ifp->eof) {
2744 409 sch_filter_receive_finish(fgp->sch, fgp->sch_idx, ifp->ifilter.index);
2745 409 ifp->eof = 1;
2746 }
2747 1778 }
2748
2749 5262 static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt)
2750 {
2751 5262 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2752 int ret;
2753
2754 // we are finished and no frames were ever seen at this output,
2755 // at least initialize the encoder with a dummy frame
2756
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5259 times.
5262 if (!fgt->got_frame) {
2757 3 AVFrame *frame = fgt->frame;
2758 FrameData *fd;
2759
2760 3 frame->time_base = ofp->tb_out;
2761 3 frame->format = ofp->format;
2762
2763 3 frame->width = ofp->width;
2764 3 frame->height = ofp->height;
2765 3 frame->sample_aspect_ratio = ofp->sample_aspect_ratio;
2766
2767 3 frame->sample_rate = ofp->sample_rate;
2768
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ofp->ch_layout.nb_channels) {
2769 3 ret = av_channel_layout_copy(&frame->ch_layout, &ofp->ch_layout);
2770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
2771 return ret;
2772 }
2773
2774 3 fd = frame_data(frame);
2775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!fd)
2776 return AVERROR(ENOMEM);
2777
2778 3 av_frame_side_data_free(&fd->side_data, &fd->nb_side_data);
2779 3 ret = clone_side_data(&fd->side_data, &fd->nb_side_data,
2780 3 ofp->side_data, ofp->nb_side_data, 0);
2781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
2782 return ret;
2783
2784 3 fd->frame_rate_filter = ofp->fps.framerate;
2785
2786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(!frame->buf[0]);
2787
2788 3 av_log(ofp, AV_LOG_WARNING,
2789 "No filtered frames for output stream, trying to "
2790 "initialize anyway.\n");
2791
2792 3 ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, frame);
2793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
2794 av_frame_unref(frame);
2795 return ret;
2796 }
2797 }
2798
2799 5262 fgt->eof_out[ofp->ofilter.index] = 1;
2800
2801 5262 ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, NULL);
2802
2/2
✓ Branch 0 taken 5101 times.
✓ Branch 1 taken 161 times.
5262 return (ret == AVERROR_EOF) ? 0 : ret;
2803 }
2804
2805 454993 static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2806 AVFrame *frame)
2807 {
2808 454993 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2809 454993 AVFrame *frame_prev = ofp->fps.last_frame;
2810 454993 enum AVMediaType type = ofp->ofilter.type;
2811
2812 454993 int64_t nb_frames = !!frame, nb_frames_prev = 0;
2813
2814
5/6
✓ Branch 0 taken 147719 times.
✓ Branch 1 taken 307274 times.
✓ Branch 2 taken 3867 times.
✓ Branch 3 taken 143852 times.
✓ Branch 4 taken 3867 times.
✗ Branch 5 not taken.
454993 if (type == AVMEDIA_TYPE_VIDEO && (frame || fgt->got_frame))
2815 147719 video_sync_process(ofp, frame, &nb_frames, &nb_frames_prev);
2816
2817
2/2
✓ Branch 0 taken 449689 times.
✓ Branch 1 taken 451904 times.
901593 for (int64_t i = 0; i < nb_frames; i++) {
2818 AVFrame *frame_out;
2819 int ret;
2820
2821
2/2
✓ Branch 0 taken 143810 times.
✓ Branch 1 taken 305879 times.
449689 if (type == AVMEDIA_TYPE_VIDEO) {
2822
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 AVFrame *frame_in = (i < nb_frames_prev && frame_prev->buf[0]) ?
2823
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 143741 times.
143879 frame_prev : frame;
2824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143810 times.
143810 if (!frame_in)
2825 break;
2826
2827 143810 frame_out = fgp->frame_enc;
2828 143810 ret = av_frame_ref(frame_out, frame_in);
2829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143810 times.
143810 if (ret < 0)
2830 return ret;
2831
2832 143810 frame_out->pts = ofp->next_pts;
2833
2834
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 143803 times.
143810 if (ofp->fps.dropped_keyframe) {
2835 7 frame_out->flags |= AV_FRAME_FLAG_KEY;
2836 7 ofp->fps.dropped_keyframe = 0;
2837 }
2838 } else {
2839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305879 times.
305879 frame->pts = (frame->pts == AV_NOPTS_VALUE) ? ofp->next_pts :
2840 305879 av_rescale_q(frame->pts, frame->time_base, ofp->tb_out) -
2841 305879 av_rescale_q(ofp->ts_offset, AV_TIME_BASE_Q, ofp->tb_out);
2842
2843 305879 frame->time_base = ofp->tb_out;
2844 305879 frame->duration = av_rescale_q(frame->nb_samples,
2845 305879 (AVRational){ 1, frame->sample_rate },
2846 ofp->tb_out);
2847
2848 305879 ofp->next_pts = frame->pts + frame->duration;
2849
2850 305879 frame_out = frame;
2851 }
2852
2853 // send the frame to consumers
2854 449689 ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, frame_out);
2855
2/2
✓ Branch 0 taken 3089 times.
✓ Branch 1 taken 446600 times.
449689 if (ret < 0) {
2856 3089 av_frame_unref(frame_out);
2857
2858
1/2
✓ Branch 0 taken 3089 times.
✗ Branch 1 not taken.
3089 if (!fgt->eof_out[ofp->ofilter.index]) {
2859 3089 fgt->eof_out[ofp->ofilter.index] = 1;
2860 3089 fgp->nb_outputs_done++;
2861 }
2862
2863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3089 times.
3089 return ret == AVERROR_EOF ? 0 : ret;
2864 }
2865
2866
2/2
✓ Branch 0 taken 140763 times.
✓ Branch 1 taken 305837 times.
446600 if (type == AVMEDIA_TYPE_VIDEO) {
2867 140763 ofp->fps.frame_number++;
2868 140763 ofp->next_pts++;
2869
2870
3/4
✓ Branch 0 taken 140331 times.
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 140331 times.
✗ Branch 3 not taken.
140763 if (i == nb_frames_prev && frame)
2871 140331 frame->flags &= ~AV_FRAME_FLAG_KEY;
2872 }
2873
2874 446600 fgt->got_frame = 1;
2875 }
2876
2877
4/4
✓ Branch 0 taken 446642 times.
✓ Branch 1 taken 5262 times.
✓ Branch 2 taken 140804 times.
✓ Branch 3 taken 305838 times.
451904 if (frame && frame_prev) {
2878 140804 av_frame_unref(frame_prev);
2879 140804 av_frame_move_ref(frame_prev, frame);
2880 }
2881
2882
2/2
✓ Branch 0 taken 5262 times.
✓ Branch 1 taken 446642 times.
451904 if (!frame)
2883 5262 return close_output(ofp, fgt);
2884
2885 446642 return 0;
2886 }
2887
2888 918641 static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2889 AVFrame *frame)
2890 {
2891 918641 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2892 918641 AVFilterContext *filter = ofp->ofilter.filter;
2893 FrameData *fd;
2894 int ret;
2895
2896 918641 ret = av_buffersink_get_frame_flags(filter, frame,
2897 AV_BUFFERSINK_FLAG_NO_REQUEST);
2898
4/4
✓ Branch 0 taken 4296 times.
✓ Branch 1 taken 914345 times.
✓ Branch 2 taken 3654 times.
✓ Branch 3 taken 642 times.
918641 if (ret == AVERROR_EOF && !fgt->eof_out[ofp->ofilter.index]) {
2899 3654 ret = fg_output_frame(ofp, fgt, NULL);
2900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3654 times.
3654 return (ret < 0) ? ret : 1;
2901
4/4
✓ Branch 0 taken 450375 times.
✓ Branch 1 taken 464612 times.
✓ Branch 2 taken 642 times.
✓ Branch 3 taken 449733 times.
914987 } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2902 465254 return 1;
2903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449733 times.
449733 } else if (ret < 0) {
2904 av_log(ofp, AV_LOG_WARNING,
2905 "Error in retrieving a frame from the filtergraph: %s\n",
2906 av_err2str(ret));
2907 return ret;
2908 }
2909
2910
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 449731 times.
449733 if (fgt->eof_out[ofp->ofilter.index]) {
2911 2 av_frame_unref(frame);
2912 2 return 0;
2913 }
2914
2915 449731 frame->time_base = av_buffersink_get_time_base(filter);
2916
2917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449731 times.
449731 if (debug_ts)
2918 av_log(ofp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
2919 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &frame->time_base),
2920 frame->time_base.num, frame->time_base.den);
2921
2922 // Choose the output timebase the first time we get a frame.
2923
2/2
✓ Branch 0 taken 8348 times.
✓ Branch 1 taken 441383 times.
449731 if (!ofp->tb_out_locked) {
2924 8348 ret = choose_out_timebase(ofp, frame);
2925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8348 times.
8348 if (ret < 0) {
2926 av_log(ofp, AV_LOG_ERROR, "Could not choose an output time base\n");
2927 av_frame_unref(frame);
2928 return ret;
2929 }
2930 }
2931
2932 449731 fd = frame_data(frame);
2933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449731 times.
449731 if (!fd) {
2934 av_frame_unref(frame);
2935 return AVERROR(ENOMEM);
2936 }
2937
2938 449731 av_frame_side_data_free(&fd->side_data, &fd->nb_side_data);
2939
2/2
✓ Branch 0 taken 8214 times.
✓ Branch 1 taken 441517 times.
449731 if (!fgt->got_frame) {
2940 8214 ret = clone_side_data(&fd->side_data, &fd->nb_side_data,
2941 8214 ofp->side_data, ofp->nb_side_data, 0);
2942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8214 times.
8214 if (ret < 0) {
2943 av_frame_unref(frame);
2944 return ret;
2945 }
2946 }
2947
2948 449731 fd->wallclock[LATENCY_PROBE_FILTER_POST] = av_gettime_relative();
2949
2950 // only use bits_per_raw_sample passed through from the decoder
2951 // if the filtergraph did not touch the frame data
2952
2/2
✓ Branch 0 taken 334971 times.
✓ Branch 1 taken 114760 times.
449731 if (!fgp->is_meta)
2953 334971 fd->bits_per_raw_sample = 0;
2954
2955
2/2
✓ Branch 0 taken 143852 times.
✓ Branch 1 taken 305879 times.
449731 if (ofp->ofilter.type == AVMEDIA_TYPE_VIDEO) {
2956
2/2
✓ Branch 0 taken 2421 times.
✓ Branch 1 taken 141431 times.
143852 if (!frame->duration) {
2957 2421 AVRational fr = av_buffersink_get_frame_rate(filter);
2958
4/4
✓ Branch 0 taken 2328 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 1787 times.
✓ Branch 3 taken 541 times.
2421 if (fr.num > 0 && fr.den > 0)
2959 1787 frame->duration = av_rescale_q(1, av_inv_q(fr), frame->time_base);
2960 }
2961
2962 143852 fd->frame_rate_filter = ofp->fps.framerate;
2963 }
2964
2965 449731 ret = fg_output_frame(ofp, fgt, frame);
2966 449731 av_frame_unref(frame);
2967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449731 times.
449731 if (ret < 0)
2968 return ret;
2969
2970 449731 return 0;
2971 }
2972
2973 /* retrieve all frames available at filtergraph outputs
2974 * and send them to consumers */
2975 460761 static int read_frames(FilterGraph *fg, FilterGraphThread *fgt,
2976 AVFrame *frame)
2977 {
2978 460761 FilterGraphPriv *fgp = fgp_from_fg(fg);
2979
2980 // graph not configured, just select the input to request
2981
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 460561 times.
460761 if (!fgt->graph) {
2982
1/2
✓ Branch 0 taken 516 times.
✗ Branch 1 not taken.
516 for (int i = 0; i < fg->nb_inputs; i++) {
2983 516 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
2984
3/4
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
516 if (ifp->format < 0 && !fgt->eof_in[i]) {
2985 200 fgt->next_in = i;
2986 200 return 0;
2987 }
2988 }
2989
2990 // This state - graph is not configured, but all inputs are either
2991 // initialized or EOF - should be unreachable because sending EOF to a
2992 // filter without even a fallback format should fail
2993 av_assert0(0);
2994 return AVERROR_BUG;
2995 }
2996
2997
2/2
✓ Branch 0 taken 457477 times.
✓ Branch 1 taken 3084 times.
460561 if (fgp->nb_outputs_done < fg->nb_outputs) {
2998 int ret;
2999
3000 /* Reap all buffers present in the buffer sinks */
3001
2/2
✓ Branch 0 taken 468908 times.
✓ Branch 1 taken 457477 times.
926385 for (int i = 0; i < fg->nb_outputs; i++) {
3002 468908 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3003
3004 468908 ret = 0;
3005
2/2
✓ Branch 0 taken 918641 times.
✓ Branch 1 taken 468908 times.
1387549 while (!ret) {
3006 918641 ret = fg_output_step(ofp, fgt, frame);
3007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 918641 times.
918641 if (ret < 0)
3008 return ret;
3009 }
3010 }
3011
3012
3013 457477 ret = avfilter_graph_request_oldest(fgt->graph);
3014
2/2
✓ Branch 0 taken 420808 times.
✓ Branch 1 taken 36669 times.
457477 if (ret == AVERROR(EAGAIN)) {
3015 420808 fgt->next_in = choose_input(fg, fgt);
3016 420808 return 0;
3017
2/2
✓ Branch 0 taken 5121 times.
✓ Branch 1 taken 31548 times.
36669 } else if (ret < 0) {
3018
1/2
✓ Branch 0 taken 5121 times.
✗ Branch 1 not taken.
5121 if (ret == AVERROR_EOF)
3019 5121 av_log(fg, AV_LOG_VERBOSE, "Filtergraph returned EOF, finishing\n");
3020 else
3021 av_log(fg, AV_LOG_ERROR,
3022 "Error requesting a frame from the filtergraph: %s\n",
3023 av_err2str(ret));
3024 5121 return ret;
3025 }
3026 31548 fgt->next_in = fg->nb_inputs;
3027
3028 // return so that scheduler can rate-control us
3029 31548 return 0;
3030 }
3031
3032 3084 return AVERROR_EOF;
3033 }
3034
3035 948 static void sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb)
3036 {
3037 948 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3038 int64_t pts2;
3039
3040 /* subtitles seem to be usually muxed ahead of other streams;
3041 if not, subtracting a larger time here is necessary */
3042 948 pts2 = av_rescale_q(pts, tb, ifp->time_base) - 1;
3043
3044 /* do not send the heartbeat frame if the subtitle is already ahead */
3045
2/2
✓ Branch 0 taken 769 times.
✓ Branch 1 taken 179 times.
948 if (pts2 <= ifp->sub2video.last_pts)
3046 769 return;
3047
3048
3/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 91 times.
179 if (pts2 >= ifp->sub2video.end_pts || ifp->sub2video.initialize)
3049 /* if we have hit the end of the current displayed subpicture,
3050 or if we need to initialize the system, update the
3051 overlaid subpicture and its start/end times */
3052 88 sub2video_update(ifp, pts2 + 1, NULL);
3053 else
3054 91 sub2video_push_ref(ifp, pts2);
3055 }
3056
3057 1079 static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer)
3058 {
3059 1079 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3060 int ret;
3061
3062
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1040 times.
1079 if (buffer) {
3063 AVFrame *tmp;
3064
3065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (!frame)
3066 return 0;
3067
3068 39 tmp = av_frame_alloc();
3069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (!tmp)
3070 return AVERROR(ENOMEM);
3071
3072 39 av_frame_move_ref(tmp, frame);
3073
3074 39 ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
3075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0) {
3076 av_frame_free(&tmp);
3077 return ret;
3078 }
3079
3080 39 return 0;
3081 }
3082
3083 // heartbeat frame
3084
4/4
✓ Branch 0 taken 1036 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 88 times.
1040 if (frame && !frame->buf[0]) {
3085 948 sub2video_heartbeat(ifilter, frame->pts, frame->time_base);
3086 948 return 0;
3087 }
3088
3089
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 88 times.
92 if (!frame) {
3090
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ifp->sub2video.end_pts < INT64_MAX)
3091 4 sub2video_update(ifp, INT64_MAX, NULL);
3092
3093 4 return av_buffersrc_add_frame(ifilter->filter, NULL);
3094 }
3095
3096
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 ifp->width = frame->width ? frame->width : ifp->width;
3097
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 ifp->height = frame->height ? frame->height : ifp->height;
3098
3099 88 sub2video_update(ifp, INT64_MIN, (const AVSubtitle*)frame->buf[0]->data);
3100
3101 88 return 0;
3102 }
3103
3104 3446 static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
3105 int64_t pts, AVRational tb)
3106 {
3107 3446 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3108 int ret;
3109
3110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3446 times.
3446 if (fgt->eof_in[ifilter->index])
3111 return 0;
3112
3113 3446 fgt->eof_in[ifilter->index] = 1;
3114
3115
2/2
✓ Branch 0 taken 3429 times.
✓ Branch 1 taken 17 times.
3446 if (ifilter->filter) {
3116 3429 pts = av_rescale_q_rnd(pts, tb, ifp->time_base,
3117 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
3118
3119 3429 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
3120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3429 times.
3429 if (ret < 0)
3121 return ret;
3122 } else {
3123
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 14 times.
17 if (ifp->format < 0) {
3124 // the filtergraph was never configured, use the fallback parameters
3125 3 ifp->format = ifp->opts.fallback->format;
3126 3 ifp->sample_rate = ifp->opts.fallback->sample_rate;
3127 3 ifp->width = ifp->opts.fallback->width;
3128 3 ifp->height = ifp->opts.fallback->height;
3129 3 ifp->sample_aspect_ratio = ifp->opts.fallback->sample_aspect_ratio;
3130 3 ifp->color_space = ifp->opts.fallback->colorspace;
3131 3 ifp->color_range = ifp->opts.fallback->color_range;
3132 3 ifp->alpha_mode = ifp->opts.fallback->alpha_mode;
3133 3 ifp->time_base = ifp->opts.fallback->time_base;
3134
3135 3 ret = av_channel_layout_copy(&ifp->ch_layout,
3136 3 &ifp->opts.fallback->ch_layout);
3137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
3138 return ret;
3139
3140 3 av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
3141 3 ret = clone_side_data(&ifp->side_data, &ifp->nb_side_data,
3142 3 ifp->opts.fallback->side_data,
3143 3 ifp->opts.fallback->nb_side_data, 0);
3144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
3145 return ret;
3146
3147
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (ifilter_has_all_input_formats(ifilter->graph)) {
3148 3 ret = configure_filtergraph(ifilter->graph, fgt);
3149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
3150 av_log(ifilter->graph, AV_LOG_ERROR, "Error initializing filters!\n");
3151 return ret;
3152 }
3153 }
3154 }
3155
3156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ifp->format < 0) {
3157 av_log(ifilter->graph, AV_LOG_ERROR,
3158 "Cannot determine format of input %s after EOF\n",
3159 ifp->opts.name);
3160 return AVERROR_INVALIDDATA;
3161 }
3162 }
3163
3164 3446 return 0;
3165 }
3166
3167 enum ReinitReason {
3168 VIDEO_CHANGED = (1 << 0),
3169 AUDIO_CHANGED = (1 << 1),
3170 MATRIX_CHANGED = (1 << 2),
3171 DOWNMIX_CHANGED = (1 << 3),
3172 HWACCEL_CHANGED = (1 << 4)
3173 };
3174
3175 185 static const char *unknown_if_null(const char *str)
3176 {
3177
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 return str ? str : "unknown";
3178 }
3179
3180 423767 static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
3181 InputFilter *ifilter, AVFrame *frame, int force_reinit)
3182 {
3183 423767 FilterGraphPriv *fgp = fgp_from_fg(fg);
3184 423767 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3185 FrameData *fd;
3186 AVFrameSideData *sd;
3187 423767 int need_reinit = 0, ret;
3188
3189 /* determine if the parameters for this input changed */
3190
2/3
✓ Branch 0 taken 305723 times.
✓ Branch 1 taken 118044 times.
✗ Branch 2 not taken.
423767 switch (ifilter->type) {
3191 305723 case AVMEDIA_TYPE_AUDIO:
3192
2/2
✓ Branch 0 taken 304352 times.
✓ Branch 1 taken 1371 times.
305723 if (ifp->format != frame->format ||
3193
3/4
✓ Branch 0 taken 304352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 304351 times.
608704 ifp->sample_rate != frame->sample_rate ||
3194 304352 av_channel_layout_compare(&ifp->ch_layout, &frame->ch_layout))
3195 1372 need_reinit |= AUDIO_CHANGED;
3196 305723 break;
3197 118044 case AVMEDIA_TYPE_VIDEO:
3198
2/2
✓ Branch 0 taken 112207 times.
✓ Branch 1 taken 5837 times.
118044 if (ifp->format != frame->format ||
3199
2/2
✓ Branch 0 taken 112179 times.
✓ Branch 1 taken 28 times.
112207 ifp->width != frame->width ||
3200
2/2
✓ Branch 0 taken 112172 times.
✓ Branch 1 taken 7 times.
112179 ifp->height != frame->height ||
3201
2/2
✓ Branch 0 taken 112155 times.
✓ Branch 1 taken 17 times.
112172 ifp->color_space != frame->colorspace ||
3202
1/2
✓ Branch 0 taken 112155 times.
✗ Branch 1 not taken.
112155 ifp->color_range != frame->color_range ||
3203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112155 times.
112155 ifp->alpha_mode != frame->alpha_mode)
3204 5889 need_reinit |= VIDEO_CHANGED;
3205 118044 break;
3206 }
3207
3208
2/2
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 423546 times.
423767 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
3209
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 27 times.
221 if (!ifp->displaymatrix_present ||
3210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 memcmp(sd->data, ifp->displaymatrix, sizeof(ifp->displaymatrix)))
3211 27 need_reinit |= MATRIX_CHANGED;
3212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423546 times.
423546 } else if (ifp->displaymatrix_present)
3213 need_reinit |= MATRIX_CHANGED;
3214
3215
2/2
✓ Branch 1 taken 720 times.
✓ Branch 2 taken 423047 times.
423767 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO)) {
3216
2/2
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 8 times.
720 if (!ifp->downmixinfo_present ||
3217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 712 times.
712 memcmp(sd->data, &ifp->downmixinfo, sizeof(ifp->downmixinfo)))
3218 8 need_reinit |= DOWNMIX_CHANGED;
3219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423047 times.
423047 } else if (ifp->downmixinfo_present)
3220 need_reinit |= DOWNMIX_CHANGED;
3221
3222
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 423745 times.
423767 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_MATRIX)) {
3223
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
22 if (!ifp->downmixmatrix_present ||
3224
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 sd->size != ifp->downmixmatrix_size || memcmp(sd->data, ifp->downmixmatrix->data, sd->size))
3225 4 need_reinit |= DOWNMIX_CHANGED;
3226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423745 times.
423745 } else if (ifp->downmixmatrix_present)
3227 need_reinit |= DOWNMIX_CHANGED;
3228
3229
5/6
✓ Branch 0 taken 7261 times.
✓ Branch 1 taken 416506 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 7117 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 144 times.
423767 if (need_reinit && fgt->graph && (ifp->opts.flags & IFILTER_FLAG_DROPCHANGED)) {
3230 ifp->nb_dropped++;
3231 av_log_once(fg, AV_LOG_WARNING, AV_LOG_DEBUG, &ifp->drop_warned, "Avoiding reinit; dropping frame pts: %s bound for %s\n", av_ts2str(frame->pts), ifilter->name);
3232 av_frame_unref(frame);
3233 return 0;
3234 }
3235
3236
4/4
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 423580 times.
✓ Branch 2 taken 182 times.
✓ Branch 3 taken 5 times.
423767 if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
3237 182 need_reinit = 0;
3238
3239
1/2
✓ Branch 0 taken 423767 times.
✗ Branch 1 not taken.
423767 if (!!ifp->hw_frames_ctx != !!frame->hw_frames_ctx ||
3240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 423767 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
423767 (ifp->hw_frames_ctx && ifp->hw_frames_ctx->data != frame->hw_frames_ctx->data))
3241 need_reinit |= HWACCEL_CHANGED;
3242
3243
2/2
✓ Branch 0 taken 7165 times.
✓ Branch 1 taken 416602 times.
423767 if (need_reinit) {
3244 7165 ret = ifilter_parameters_from_frame(ifilter, frame);
3245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7165 times.
7165 if (ret < 0)
3246 return ret;
3247
3248 /* Inputs bound to a filtergraph output will have some fields unset.
3249 * Handle them here */
3250
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7164 times.
7165 if (ifp->ofilter_src) {
3251 1 ret = ifilter_parameters_from_ofilter(ifilter, ifp->ofilter_src);
3252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3253 return ret;
3254 }
3255 }
3256
3257 /* (re)init the graph if possible, otherwise buffer the frame and return */
3258
5/6
✓ Branch 0 taken 416602 times.
✓ Branch 1 taken 7165 times.
✓ Branch 2 taken 416602 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 416557 times.
423767 if (need_reinit || force_reinit || !fgt->graph) {
3259 7210 AVFrame *tmp = av_frame_alloc();
3260
3261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7210 times.
7210 if (!tmp)
3262 149 return AVERROR(ENOMEM);
3263
3264
2/2
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 7063 times.
7210 if (!ifilter_has_all_input_formats(fg)) {
3265 147 av_frame_move_ref(tmp, frame);
3266
3267 147 ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
3268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (ret < 0)
3269 av_frame_free(&tmp);
3270
3271 147 return ret;
3272 }
3273
3274
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7015 times.
7063 ret = fgt->graph ? read_frames(fg, fgt, tmp) : 0;
3275 7063 av_frame_free(&tmp);
3276
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7062 times.
7063 if (ret < 0)
3277 1 return ret;
3278
3279
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 7015 times.
7062 if (fgt->graph) {
3280 AVBPrint reason;
3281 47 av_bprint_init(&reason, 0, AV_BPRINT_SIZE_AUTOMATIC);
3282
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
47 if (need_reinit & AUDIO_CHANGED) {
3283 1 const char *sample_format_name = av_get_sample_fmt_name(frame->format);
3284 1 av_bprintf(&reason, "audio parameters changed to %d Hz, ", frame->sample_rate);
3285 1 av_channel_layout_describe_bprint(&frame->ch_layout, &reason);
3286 1 av_bprintf(&reason, ", %s, ", unknown_if_null(sample_format_name));
3287 }
3288
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1 times.
47 if (need_reinit & VIDEO_CHANGED) {
3289 46 const char *pixel_format_name = av_get_pix_fmt_name(frame->format);
3290 46 const char *color_space_name = av_color_space_name(frame->colorspace);
3291 46 const char *color_range_name = av_color_range_name(frame->color_range);
3292 46 const char *alpha_mode = av_alpha_mode_name(frame->alpha_mode);
3293 46 av_bprintf(&reason, "video parameters changed to %s(%s, %s), %dx%d, %s alpha, ",
3294 unknown_if_null(pixel_format_name), unknown_if_null(color_range_name),
3295 unknown_if_null(color_space_name), frame->width, frame->height,
3296 unknown_if_null(alpha_mode));
3297 }
3298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (need_reinit & MATRIX_CHANGED)
3299 av_bprintf(&reason, "display matrix changed, ");
3300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (need_reinit & DOWNMIX_CHANGED)
3301 av_bprintf(&reason, "downmix medatata changed, ");
3302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (need_reinit & HWACCEL_CHANGED)
3303 av_bprintf(&reason, "hwaccel changed, ");
3304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (force_reinit)
3305 av_bprintf(&reason, "reinitialization arguments were provided, ");
3306
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if (reason.len > 1)
3307 47 reason.str[reason.len - 2] = '\0'; // remove last comma
3308
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 av_log(fg, AV_LOG_INFO, "Reconfiguring filter graph%s%s\n", reason.len ? " because " : "", reason.str);
3309 } else {
3310 /* Choke all input to avoid buffering excessive frames while the
3311 * initial filter graph is being configured, and before we have a
3312 * preferred input */
3313 7015 sch_filter_choke_inputs(fgp->sch, fgp->sch_idx);
3314 }
3315
3316 7062 ret = configure_filtergraph(fg, fgt);
3317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7061 times.
7062 if (ret < 0) {
3318 1 av_log(fg, AV_LOG_ERROR, "Error reinitializing filters!\n");
3319 1 return ret;
3320 }
3321 }
3322
3323 423618 frame->pts = av_rescale_q(frame->pts, frame->time_base, ifp->time_base);
3324 423618 frame->duration = av_rescale_q(frame->duration, frame->time_base, ifp->time_base);
3325 423618 frame->time_base = ifp->time_base;
3326
3327
2/2
✓ Branch 0 taken 117942 times.
✓ Branch 1 taken 305676 times.
423618 if (ifp->displaymatrix_applied)
3328 117942 av_frame_remove_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
3329
3330 423618 fd = frame_data(frame);
3331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423618 times.
423618 if (!fd)
3332 return AVERROR(ENOMEM);
3333 423618 fd->wallclock[LATENCY_PROBE_FILTER_PRE] = av_gettime_relative();
3334
3335 423618 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame,
3336 AV_BUFFERSRC_FLAG_PUSH);
3337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423618 times.
423618 if (ret < 0) {
3338 av_frame_unref(frame);
3339 if (ret != AVERROR_EOF)
3340 av_log(fg, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
3341 return ret;
3342 }
3343
3344 423618 return 0;
3345 }
3346
3347 8217 static void fg_thread_set_name(const FilterGraph *fg)
3348 {
3349 char name[16];
3350
2/2
✓ Branch 1 taken 6946 times.
✓ Branch 2 taken 1271 times.
8217 if (filtergraph_is_simple(fg)) {
3351 6946 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
3352 13892 snprintf(name, sizeof(name), "%cf%s",
3353 6946 av_get_media_type_string(ofp->ofilter.type)[0],
3354 ofp->ofilter.output_name);
3355 } else {
3356 1271 snprintf(name, sizeof(name), "fc%d", fg->index);
3357 }
3358
3359 8217 ff_thread_setname(name);
3360 8217 }
3361
3362 8217 static void fg_thread_uninit(FilterGraphThread *fgt)
3363 {
3364
1/2
✓ Branch 0 taken 8217 times.
✗ Branch 1 not taken.
8217 if (fgt->frame_queue_out) {
3365 AVFrame *frame;
3366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8217 times.
8217 while (av_fifo_read(fgt->frame_queue_out, &frame, 1) >= 0)
3367 av_frame_free(&frame);
3368 8217 av_fifo_freep2(&fgt->frame_queue_out);
3369 }
3370
3371 8217 av_frame_free(&fgt->frame);
3372 8217 av_freep(&fgt->eof_in);
3373 8217 av_freep(&fgt->eof_out);
3374
3375 8217 avfilter_graph_free(&fgt->graph);
3376
3377 8217 memset(fgt, 0, sizeof(*fgt));
3378 8217 }
3379
3380 8217 static int fg_thread_init(FilterGraphThread *fgt, const FilterGraph *fg)
3381 {
3382 8217 memset(fgt, 0, sizeof(*fgt));
3383
3384 8217 fgt->frame = av_frame_alloc();
3385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (!fgt->frame)
3386 goto fail;
3387
3388 8217 fgt->eof_in = av_calloc(fg->nb_inputs, sizeof(*fgt->eof_in));
3389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (!fgt->eof_in)
3390 goto fail;
3391
3392 8217 fgt->eof_out = av_calloc(fg->nb_outputs, sizeof(*fgt->eof_out));
3393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (!fgt->eof_out)
3394 goto fail;
3395
3396 8217 fgt->frame_queue_out = av_fifo_alloc2(1, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
3397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (!fgt->frame_queue_out)
3398 goto fail;
3399
3400 8217 return 0;
3401
3402 fail:
3403 fg_thread_uninit(fgt);
3404 return AVERROR(ENOMEM);
3405 }
3406
3407 423767 static int check_reinit(FilterGraph *fg, FilterGraphThread *fgt)
3408 {
3409 423767 int ret, reinit = 0;
3410
3411
2/2
✓ Branch 0 taken 434206 times.
✓ Branch 1 taken 423767 times.
857973 for (unsigned i = 0; i < fg->nb_outputs; i++) {
3412 434206 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3413
3414
2/6
✓ Branch 0 taken 434206 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 434206 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
434206 if (ofp->reinit_opts.pts == AV_NOPTS_VALUE && ofp->reinit_opts_fifo &&
3415 av_fifo_can_read(ofp->reinit_opts_fifo)) {
3416 av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1);
3417 }
3418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 434206 times.
434206 if (ofp->reinit_opts.pts != AV_NOPTS_VALUE &&
3419 ofp->reinit_opts.pts <= av_rescale_q(fgt->frame->pts, fgt->frame->time_base, AV_TIME_BASE_Q)) {
3420 FrameData *fd = frame_data(fgt->frame);
3421 if (!fd)
3422 return AVERROR(ENOMEM);
3423
3424 av_dict_free(&fd->reinit_opts);
3425 ret = av_dict_copy(&fd->reinit_opts, ofp->reinit_opts.dict, 0);
3426 if (ret < 0)
3427 return ret;
3428
3429 av_opt_set_dict(ofp, &ofp->reinit_opts.dict);
3430 av_dict_free(&ofp->reinit_opts.dict);
3431
3432 if (av_fifo_can_read(ofp->reinit_opts_fifo))
3433 av_fifo_read(ofp->reinit_opts_fifo, &ofp->reinit_opts, 1);
3434 else
3435 ofp->reinit_opts = (ReinitOpts){ .pts = AV_NOPTS_VALUE };
3436 reinit = 1;
3437 }
3438 }
3439
3440 423767 return reinit;
3441 }
3442
3443 8217 static int filter_thread(void *arg)
3444 {
3445 8217 FilterGraphPriv *fgp = arg;
3446 8217 FilterGraph *fg = &fgp->fg;
3447
3448 FilterGraphThread fgt;
3449 8217 int ret = 0, input_status = 0;
3450
3451 8217 ret = fg_thread_init(&fgt, fg);
3452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (ret < 0)
3453 goto finish;
3454
3455 8217 fg_thread_set_name(fg);
3456
3457 // if we have all input parameters the graph can now be configured
3458
2/2
✓ Branch 1 taken 7018 times.
✓ Branch 2 taken 1199 times.
8217 if (ifilter_has_all_input_formats(fg)) {
3459 1199 ret = configure_filtergraph(fg, &fgt);
3460
1/2
✓ Branch 0 taken 1199 times.
✗ Branch 1 not taken.
1199 if (ret < 0) {
3461 av_log(fg, AV_LOG_ERROR, "Error configuring filter graph: %s\n",
3462 av_err2str(ret));
3463 goto finish;
3464 }
3465 }
3466
3467 452510 while (1) {
3468 InputFilter *ifilter;
3469 460727 InputFilterPriv *ifp = NULL;
3470 enum FrameOpaque o;
3471 460727 unsigned input_idx = fgt.next_in;
3472
3473 460727 input_status = sch_filter_receive(fgp->sch, fgp->sch_idx,
3474 460727 &input_idx, fgt.frame);
3475
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 460715 times.
460727 if (input_status == AVERROR_EOF) {
3476 12 av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n");
3477 12 break;
3478
2/2
✓ Branch 0 taken 32462 times.
✓ Branch 1 taken 428253 times.
460715 } else if (input_status == AVERROR(EAGAIN)) {
3479 // should only happen when we didn't request any input
3480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32462 times.
32462 av_assert0(input_idx == fg->nb_inputs);
3481 32462 goto read_frames;
3482 }
3483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428253 times.
428253 av_assert0(input_status >= 0);
3484
3485 428253 o = (intptr_t)fgt.frame->opaque;
3486
3487 // message on the control stream
3488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428253 times.
428253 if (input_idx == fg->nb_inputs) {
3489 FilterCommand *fc;
3490
3491 av_assert0(o == FRAME_OPAQUE_SEND_COMMAND && fgt.frame->buf[0]);
3492
3493 fc = (FilterCommand*)fgt.frame->buf[0]->data;
3494 send_command(fg, fgt.graph, fc->time, fc->target, fc->command, fc->arg,
3495 fc->all_filters);
3496 av_frame_unref(fgt.frame);
3497 1 continue;
3498 }
3499
3500 // we received an input frame or EOF
3501 428253 ifilter = fg->inputs[input_idx];
3502 428253 ifp = ifp_from_ifilter(ifilter);
3503
3504
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 427213 times.
428253 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
3505
3/4
✓ Branch 0 taken 1040 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 92 times.
1040 int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT;
3506
2/2
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 4 times.
1040 ret = sub2video_frame(ifilter, (fgt.frame->buf[0] || hb_frame) ? fgt.frame : NULL,
3507
2/2
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 88 times.
1040 !fgt.graph);
3508
2/2
✓ Branch 0 taken 423767 times.
✓ Branch 1 taken 3446 times.
427213 } else if (fgt.frame->buf[0]) {
3509 423767 ret = check_reinit(fg, &fgt);
3510
1/2
✓ Branch 0 taken 423767 times.
✗ Branch 1 not taken.
423767 if (ret >= 0)
3511 423767 ret = send_frame(fg, &fgt, ifilter, fgt.frame, ret);
3512 } else {
3513 av_assert1(o == FRAME_OPAQUE_EOF);
3514 3446 ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base);
3515 }
3516 428253 av_frame_unref(fgt.frame);
3517
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 428252 times.
428253 if (ret == AVERROR_EOF) {
3518 1 av_log(fg, AV_LOG_VERBOSE, "Input %u no longer accepts new data\n",
3519 input_idx);
3520 1 close_input(ifp);
3521 1 continue;
3522 }
3523
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 428251 times.
428252 if (ret < 0)
3524 1 goto finish;
3525
3526 428251 read_frames:
3527 // retrieve all newly available frames
3528 460713 ret = read_frames(fg, &fgt, fgt.frame);
3529
2/2
✓ Branch 0 taken 8204 times.
✓ Branch 1 taken 452509 times.
460713 if (ret == AVERROR_EOF) {
3530 8204 av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n");
3531
3/4
✓ Branch 0 taken 6698 times.
✓ Branch 1 taken 1506 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6698 times.
8204 if (ifp && ifp->opts.flags & IFILTER_FLAG_DROPCHANGED)
3532 av_log(fg, AV_LOG_INFO, "Total changed input frames dropped : %"PRId64"\n", ifp->nb_dropped);
3533 8204 break;
3534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 452509 times.
452509 } else if (ret < 0) {
3535 av_log(fg, AV_LOG_ERROR, "Error sending frames to consumers: %s\n",
3536 av_err2str(ret));
3537 goto finish;
3538 }
3539
3540 // ensure all inputs no longer accepting data are closed
3541
4/4
✓ Branch 0 taken 889947 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 437638 times.
✓ Branch 3 taken 452309 times.
890147 for (int i = 0; fgt.graph && i < fg->nb_inputs; i++) {
3542 437638 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
3543
2/2
✓ Branch 1 taken 1777 times.
✓ Branch 2 taken 435861 times.
437638 if (av_buffersrc_get_status(ifp->ifilter.filter))
3544 1777 close_input(ifp);
3545 }
3546 }
3547
3548
2/2
✓ Branch 0 taken 8351 times.
✓ Branch 1 taken 8216 times.
16567 for (unsigned i = 0; i < fg->nb_outputs; i++) {
3549 8351 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3550
3551
3/4
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 6743 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1608 times.
8351 if (fgt.eof_out[i] || !fgt.graph)
3552 6743 continue;
3553
3554 1608 ret = fg_output_frame(ofp, &fgt, NULL);
3555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1608 times.
1608 if (ret < 0)
3556 goto finish;
3557 }
3558
3559 8216 finish:
3560
3561
2/4
✓ Branch 0 taken 8217 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8217 times.
8217 if (print_graphs || print_graphs_file)
3562 print_filtergraph(fg, fgt.graph);
3563
3564 // EOF is normal termination
3565
2/2
✓ Branch 0 taken 6670 times.
✓ Branch 1 taken 1547 times.
8217 if (ret == AVERROR_EOF)
3566 6670 ret = 0;
3567
3568 8217 fg_thread_uninit(&fgt);
3569
3570 8217 return ret;
3571 }
3572
3573 void fg_send_command(FilterGraph *fg, double time, const char *target,
3574 const char *command, const char *arg, int all_filters)
3575 {
3576 FilterGraphPriv *fgp = fgp_from_fg(fg);
3577 AVBufferRef *buf;
3578 FilterCommand *fc;
3579
3580 fc = av_mallocz(sizeof(*fc));
3581 if (!fc)
3582 return;
3583
3584 buf = av_buffer_create((uint8_t*)fc, sizeof(*fc), filter_command_free, NULL, 0);
3585 if (!buf) {
3586 av_freep(&fc);
3587 return;
3588 }
3589
3590 fc->target = av_strdup(target);
3591 fc->command = av_strdup(command);
3592 fc->arg = av_strdup(arg);
3593 if (!fc->target || !fc->command || !fc->arg) {
3594 av_buffer_unref(&buf);
3595 return;
3596 }
3597
3598 fc->time = time;
3599 fc->all_filters = all_filters;
3600
3601 fgp->frame->buf[0] = buf;
3602 fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND;
3603
3604 sch_filter_command(fgp->sch, fgp->sch_idx, fgp->frame);
3605 }
3606