FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_filter.c
Date: 2026-09-11 23:04:01
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 2307813 static FilterGraphPriv *fgp_from_fg(FilterGraph *fg)
70 {
71 2307813 return (FilterGraphPriv*)fg;
72 }
73
74 39210 static const FilterGraphPriv *cfgp_from_cfg(const FilterGraph *fg)
75 {
76 39210 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 1348303 static InputFilterPriv *ifp_from_ifilter(InputFilter *ifilter)
164 {
165 1348303 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 969790 static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter)
261 {
262 969790 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 1562 times.
✓ Branch 1 taken 5414 times.
✓ Branch 2 taken 1186 times.
✓ Branch 3 taken 376 times.
✓ Branch 5 taken 5414 times.
✓ Branch 6 taken 376 times.
✓ Branch 11 taken 2405 times.
✓ Branch 12 taken 376 times.
✓ Branch 13 taken 376 times.
✗ Branch 14 not taken.
9381 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 1448 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1448 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1448 times.
✓ Branch 11 taken 1562 times.
✓ Branch 12 taken 1448 times.
✓ Branch 13 taken 1448 times.
✗ Branch 14 not taken.
3012 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 1433 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 1343 times.
✓ Branch 3 taken 90 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 90 times.
✓ Branch 9 taken 563 times.
✓ Branch 10 taken 90 times.
✓ Branch 11 taken 90 times.
✗ Branch 12 not taken.
2013 DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
423 "%d", )
424
425
4/10
✓ Branch 0 taken 6956 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6956 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.
6976 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 5294 times.
✓ Branch 1 taken 1682 times.
✓ Branch 2 taken 4764 times.
✓ Branch 3 taken 530 times.
✓ Branch 5 taken 1682 times.
✓ Branch 6 taken 530 times.
✓ Branch 11 taken 567 times.
✓ Branch 12 taken 530 times.
✓ Branch 13 taken 530 times.
✗ Branch 14 not taken.
7543 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 6968 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6938 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.
7006 DEF_CHOOSE_FORMAT(alpha_modes, enum AVAlphaMode, alpha_mode, alpha_modes,
432 AVALPHA_MODE_UNSPECIFIED, "%s", av_alpha_mode_name)
433
434 1450 static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint)
435 {
436
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1438 times.
1450 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 104 times.
✓ Branch 1 taken 1334 times.
1438 } else if (ofp->ch_layouts) {
440 const AVChannelLayout *p;
441
442 104 av_bprintf(bprint, "channel_layouts=");
443
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 104 times.
656 for (p = ofp->ch_layouts; p->nb_channels; p++) {
444 552 av_channel_layout_describe_bprint(p, bprint);
445 552 av_bprintf(bprint, "|");
446 }
447
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 if (bprint->len > 0)
448 104 bprint->str[--bprint->len] = '\0';
449 } else
450 1334 return;
451 116 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 33027 static int filter_opt_apply(void *logctx, AVFilterContext *f,
504 const char *key, const char *val)
505 {
506 33027 const AVOption *o = NULL;
507 int ret;
508
509 33027 ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN);
510
1/2
✓ Branch 0 taken 33027 times.
✗ Branch 1 not taken.
33027 if (ret >= 0)
511 33027 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 16540 static int graph_opts_apply(void *logctx, AVFilterGraphSegment *seg)
560 {
561
2/2
✓ Branch 0 taken 16972 times.
✓ Branch 1 taken 16540 times.
33512 for (size_t i = 0; i < seg->nb_chains; i++) {
562 16972 AVFilterChain *ch = seg->chains[i];
563
564
2/2
✓ Branch 0 taken 37417 times.
✓ Branch 1 taken 16972 times.
54389 for (size_t j = 0; j < ch->nb_filters; j++) {
565 37417 AVFilterParams *p = ch->filters[j];
566 37417 const AVDictionaryEntry *e = NULL;
567
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37417 times.
37417 av_assert0(p->filter);
569
570
2/2
✓ Branch 1 taken 33027 times.
✓ Branch 2 taken 37417 times.
70444 while ((e = av_dict_iterate(p->opts, e))) {
571 33027 int ret = filter_opt_apply(logctx, p->filter, e->key, e->value);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33027 times.
33027 if (ret < 0)
573 return ret;
574 }
575
576 37417 av_dict_free(&p->opts);
577 }
578 }
579
580 16540 return 0;
581 }
582
583 16540 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 16540 *inputs = NULL;
592 16540 *outputs = NULL;
593
594 16540 ret = avfilter_graph_segment_parse(graph, desc, 0, &seg);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16540 times.
16540 if (ret < 0)
596 return ret;
597
598 16540 ret = avfilter_graph_segment_create_filters(seg, 0);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16540 times.
16540 if (ret < 0)
600 goto fail;
601
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16540 times.
16540 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 16540 ret = graph_opts_apply(logctx, seg);
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16540 times.
16540 if (ret < 0)
618 goto fail;
619
620 16540 ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
621
622 16540 fail:
623 16540 avfilter_graph_segment_free(&seg);
624 16540 return ret;
625 }
626
627 // Filters can be configured only if the formats of all inputs are known.
628 15481 static int ifilter_has_all_input_formats(FilterGraph *fg)
629 {
630
2/2
✓ Branch 0 taken 14701 times.
✓ Branch 1 taken 8292 times.
22993 for (int i = 0; i < fg->nb_inputs; i++) {
631 14701 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
632
2/2
✓ Branch 0 taken 7189 times.
✓ Branch 1 taken 7512 times.
14701 if (ifp->format < 0)
633 7189 return 0;
634 }
635 8292 return 1;
636 }
637
638 static int filter_thread(void *arg);
639
640 15542 static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
641 {
642 15542 AVFilterContext *ctx = inout->filter_ctx;
643
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 8383 times.
15542 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
644
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 8383 times.
15542 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
645
646
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 15373 times.
15542 if (nb_pads > 1)
647 169 return av_strdup(ctx->filter->name);
648 15373 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 8383 static OutputFilter *ofilter_alloc(FilterGraph *fg, enum AVMediaType type)
679 {
680 OutputFilterPriv *ofp;
681 OutputFilter *ofilter;
682
683 8383 ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8383 times.
8383 if (!ofp)
685 return NULL;
686
687 8383 ofilter = &ofp->ofilter;
688 8383 ofilter->class = &ofilter_class;
689 8383 ofp->log_parent = fg;
690 8383 ofilter->graph = fg;
691 8383 ofilter->type = type;
692 8383 av_opt_set_defaults(ofp);
693 8383 ofp->reinit_opts = (ReinitOpts){ .pts = AV_NOPTS_VALUE };
694 8383 ofilter->index = fg->nb_outputs - 1;
695
696 16766 snprintf(ofp->log_name, sizeof(ofp->log_name), "%co%d",
697 8383 av_get_media_type_string(type)[0], ofilter->index);
698
699 8383 return ofilter;
700 }
701
702 7149 static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist,
703 const ViewSpecifier *vs)
704 {
705 7149 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
706 7149 FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
707 SchedulerNode src;
708 int ret;
709
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
7149 av_assert0(!ifp->bound);
711 7149 ifp->bound = 1;
712
713
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7145 times.
7149 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 7149 ifp->type_src = ist->st->codecpar->codec_type;
721
722 7149 ifp->opts.fallback = av_frame_alloc();
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
7149 if (!ifp->opts.fallback)
724 return AVERROR(ENOMEM);
725
726 7149 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 7149 times.
7149 if (ret < 0)
729 return ret;
730
731 7149 ifilter->input_name = av_strdup(ifp->opts.name);
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
7149 if (!ifilter->input_name)
733 return AVERROR(EINVAL);
734
735 7149 ret = sch_connect(fgp->sch,
736 7149 src, SCH_FILTER_IN(fgp->sch_idx, ifilter->index));
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
7149 if (ret < 0)
738 return ret;
739
740
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7145 times.
7149 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 7149 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 8379 int ofilter_bind_enc(OutputFilter *ofilter, unsigned sched_idx_enc,
904 const OutputFilterOptions *opts)
905 {
906 8379 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
907 8379 FilterGraph *fg = ofilter->graph;
908 8379 FilterGraphPriv *fgp = fgp_from_fg(fg);
909 int ret;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 av_assert0(!ofilter->bound);
912
2/4
✓ Branch 0 taken 8379 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8379 times.
8379 av_assert0(!opts->enc ||
913 ofilter->type == opts->enc->type);
914
915 8379 ofp->needed = ofilter->bound = 1;
916 8379 av_freep(&ofilter->linklabel);
917
918 8379 ofp->flags |= opts->flags;
919 8379 ofp->ts_offset = opts->ts_offset;
920 8379 ofp->enc_timebase = opts->output_tb;
921
922 8379 ofp->trim_start_us = opts->trim_start_us;
923 8379 ofp->trim_duration_us = opts->trim_duration_us;
924
925 8379 ofilter->output_name = av_strdup(opts->name);
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 if (!ofilter->output_name)
927 return AVERROR(EINVAL);
928
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 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 8379 ret = av_dict_copy(&ofp->sws_opts, opts->sws_opts, 0);
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 if (ret < 0)
937 return ret;
938
939 8379 ret = av_dict_copy(&ofp->swr_opts, opts->swr_opts, 0);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 if (ret < 0)
941 return ret;
942
943
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 8314 times.
8379 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 6973 times.
✓ Branch 1 taken 1406 times.
8379 if (fgp->is_simple) {
947 // for simple filtergraph there is just one output,
948 // so use only graph-level information for logging
949 6973 ofp->log_parent = NULL;
950 6973 av_strlcpy(ofp->log_name, fgp->log_name, sizeof(ofp->log_name));
951 } else
952 1406 av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofilter->output_name);
953
954
2/3
✓ Branch 0 taken 6929 times.
✓ Branch 1 taken 1450 times.
✗ Branch 2 not taken.
8379 switch (ofilter->type) {
955 6929 case AVMEDIA_TYPE_VIDEO:
956 6929 ofp->width = opts->width;
957 6929 ofp->height = opts->height;
958
2/2
✓ Branch 0 taken 5368 times.
✓ Branch 1 taken 1561 times.
6929 if (opts->format != AV_PIX_FMT_NONE) {
959 5368 ofp->format = opts->format;
960 } else
961 1561 ofp->pix_fmts = opts->pix_fmts;
962
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6929 times.
6929 if (opts->color_space != AVCOL_SPC_UNSPECIFIED)
964 ofp->color_space = opts->color_space;
965 else
966 6929 ofp->color_spaces = opts->color_spaces;
967
968
2/2
✓ Branch 0 taken 1651 times.
✓ Branch 1 taken 5278 times.
6929 if (opts->color_range != AVCOL_RANGE_UNSPECIFIED)
969 1651 ofp->color_range = opts->color_range;
970 else
971 5278 ofp->color_ranges = opts->color_ranges;
972
973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6929 times.
6929 if (opts->alpha_mode != AVALPHA_MODE_UNSPECIFIED)
974 ofp->alpha_mode = opts->alpha_mode;
975 else
976 6929 ofp->alpha_modes = opts->alpha_modes;
977
978 6929 fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT);
979
980 6929 ofp->fps.last_frame = av_frame_alloc();
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6929 times.
6929 if (!ofp->fps.last_frame)
982 return AVERROR(ENOMEM);
983
984 6929 ofp->fps.vsync_method = opts->vsync_method;
985 6929 ofp->fps.framerate = opts->frame_rate;
986 6929 ofp->fps.framerate_max = opts->max_frame_rate;
987 6929 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 6929 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 6858 times.
6929 if (opts->enc && opts->enc->id == AV_CODEC_ID_MPEG4)
991 71 ofp->fps.framerate_clip = 65535;
992
993 6929 ofp->fps.dup_warning = 1000;
994
995 6929 break;
996 1450 case AVMEDIA_TYPE_AUDIO:
997
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1449 times.
1450 if (opts->format != AV_SAMPLE_FMT_NONE) {
998 1 ofp->format = opts->format;
999 } else {
1000 1449 ofp->sample_fmts = opts->sample_fmts;
1001 }
1002
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1434 times.
1450 if (opts->sample_rate) {
1003 16 ofp->sample_rate = opts->sample_rate;
1004 } else
1005 1434 ofp->sample_rates = opts->sample_rates;
1006
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1439 times.
1450 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 1439 ofp->ch_layouts = opts->ch_layouts;
1012 }
1013 1450 break;
1014 }
1015
1016 8379 ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fgp->sch_idx, ofilter->index),
1017 8379 SCH_ENC(sched_idx_enc));
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
8379 if (ret < 0)
1019 return ret;
1020
1021 8379 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 7159 static InputFilter *ifilter_alloc(FilterGraph *fg)
1084 {
1085 InputFilterPriv *ifp;
1086 InputFilter *ifilter;
1087
1088 7159 ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs);
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!ifp)
1090 return NULL;
1091
1092 7159 ifilter = &ifp->ifilter;
1093 7159 ifilter->graph = fg;
1094
1095 7159 ifp->frame = av_frame_alloc();
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!ifp->frame)
1097 return NULL;
1098
1099 7159 ifilter->index = fg->nb_inputs - 1;
1100 7159 ifp->format = -1;
1101 7159 ifp->color_space = AVCOL_SPC_UNSPECIFIED;
1102 7159 ifp->color_range = AVCOL_RANGE_UNSPECIFIED;
1103 7159 ifp->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
1104
1105 7159 ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!ifp->frame_queue)
1107 return NULL;
1108
1109 7159 return ifilter;
1110 }
1111
1112 10605 void fg_free(FilterGraph **pfg)
1113 {
1114 10605 FilterGraph *fg = *pfg;
1115 FilterGraphPriv *fgp;
1116
1117
2/2
✓ Branch 0 taken 2356 times.
✓ Branch 1 taken 8249 times.
10605 if (!fg)
1118 2356 return;
1119 8249 fgp = fgp_from_fg(fg);
1120
1121
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 8249 times.
15408 for (int j = 0; j < fg->nb_inputs; j++) {
1122 7159 InputFilter *ifilter = fg->inputs[j];
1123 7159 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
1124
1125
1/2
✓ Branch 0 taken 7159 times.
✗ Branch 1 not taken.
7159 if (ifp->frame_queue) {
1126 AVFrame *frame;
1127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7159 times.
7159 while (av_fifo_read(ifp->frame_queue, &frame, 1) >= 0)
1128 av_frame_free(&frame);
1129 7159 av_fifo_freep2(&ifp->frame_queue);
1130 }
1131 7159 av_frame_free(&ifp->sub2video.frame);
1132
1133 7159 av_frame_free(&ifp->frame);
1134 7159 av_frame_free(&ifp->opts.fallback);
1135
1136 7159 av_buffer_unref(&ifp->hw_frames_ctx);
1137 7159 av_channel_layout_uninit(&ifp->ch_layout);
1138 7159 av_freep(&ifilter->linklabel);
1139 7159 av_freep(&ifp->opts.name);
1140 7159 av_buffer_unref(&ifp->downmixmatrix);
1141 7159 av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
1142 7159 av_freep(&ifilter->name);
1143 7159 av_freep(&ifilter->input_name);
1144 7159 av_freep(&fg->inputs[j]);
1145 }
1146 8249 av_freep(&fg->inputs);
1147
2/2
✓ Branch 0 taken 8383 times.
✓ Branch 1 taken 8249 times.
16632 for (int j = 0; j < fg->nb_outputs; j++) {
1148 8383 OutputFilter *ofilter = fg->outputs[j];
1149 8383 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1150
1151 8383 av_frame_free(&ofp->fps.last_frame);
1152 8383 av_dict_free(&ofp->sws_opts);
1153 8383 av_dict_free(&ofp->swr_opts);
1154
1155 8383 av_freep(&ofilter->linklabel);
1156 8383 av_freep(&ofilter->name);
1157 8383 av_freep(&ofilter->output_name);
1158 8383 av_freep(&ofilter->apad);
1159 8383 av_dict_free(&ofp->reinit_opts.dict);
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8383 times.
8383 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 8383 av_channel_layout_uninit(&ofp->ch_layout);
1166 8383 av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data);
1167 8383 av_freep(&fg->outputs[j]);
1168 }
1169 8249 av_freep(&fg->outputs);
1170 8249 av_freep(&fg->graph_desc);
1171
1172 8249 av_frame_free(&fgp->frame);
1173 8249 av_frame_free(&fgp->frame_enc);
1174
1175 8249 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 8249 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 8249 int ret = 0;
1201
1202 8249 fgp = av_mallocz(sizeof(*fgp));
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
8249 if (!fgp) {
1204 av_freep(graph_desc);
1205 return AVERROR(ENOMEM);
1206 }
1207 8249 fg = &fgp->fg;
1208
1209
2/2
✓ Branch 0 taken 6974 times.
✓ Branch 1 taken 1275 times.
8249 if (pfg) {
1210 6974 *pfg = fg;
1211 6974 fg->index = -1;
1212 } else {
1213 1275 ret = av_dynarray_add_nofree(&filtergraphs, &nb_filtergraphs, fgp);
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1275 times.
1275 if (ret < 0) {
1215 av_freep(graph_desc);
1216 av_freep(&fgp);
1217 return ret;
1218 }
1219
1220 1275 fg->index = nb_filtergraphs - 1;
1221 }
1222
1223 8249 fg->class = &fg_class;
1224 8249 fg->graph_desc = *graph_desc;
1225 8249 fgp->disable_conversions = !auto_conversion_filters;
1226 8249 fgp->nb_threads = -1;
1227 8249 fgp->sch = sch;
1228
1229 8249 *graph_desc = NULL;
1230
1231 8249 snprintf(fgp->log_name, sizeof(fgp->log_name), "fc#%d", fg->index);
1232
1233 8249 fgp->frame = av_frame_alloc();
1234 8249 fgp->frame_enc = av_frame_alloc();
1235
2/4
✓ Branch 0 taken 8249 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8249 times.
8249 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 8249 graph = avfilter_graph_alloc();
1241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
8249 if (!graph)
1242 return AVERROR(ENOMEM);;
1243 8249 graph->nb_threads = 1;
1244
1245 8249 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 8248 times.
8249 if (ret < 0)
1248 1 goto fail;
1249
1250
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 8248 times.
15407 for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
1251 7159 InputFilter *const ifilter = ifilter_alloc(fg);
1252
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!ifilter) {
1254 ret = AVERROR(ENOMEM);
1255 goto fail;
1256 }
1257
1258 7159 ifilter->linklabel = cur->name;
1259 7159 cur->name = NULL;
1260
1261 7159 ifilter->type = avfilter_pad_get_type(cur->filter_ctx->input_pads,
1262 cur->pad_idx);
1263
1264
3/4
✓ Branch 0 taken 1387 times.
✓ Branch 1 taken 5772 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1387 times.
7159 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 7159 ifilter->name = describe_filter_link(fg, cur, 1);
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!ifilter->name) {
1273 ret = AVERROR(ENOMEM);
1274 goto fail;
1275 }
1276 }
1277
1278
2/2
✓ Branch 0 taken 8383 times.
✓ Branch 1 taken 8248 times.
16631 for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
1279 8383 const enum AVMediaType type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
1280 cur->pad_idx);
1281 8383 OutputFilter *const ofilter = ofilter_alloc(fg, type);
1282 OutputFilterPriv *ofp;
1283
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8383 times.
8383 if (!ofilter) {
1285 ret = AVERROR(ENOMEM);
1286 goto fail;
1287 }
1288 8383 ofp = ofp_from_ofilter(ofilter);
1289
1290 8383 ofilter->linklabel = cur->name;
1291 8383 cur->name = NULL;
1292
1293 8383 ofilter->name = describe_filter_link(fg, cur, 0);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8383 times.
8383 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 8377 times.
8383 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 8248 times.
8248 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 8248 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 8248 times.
8248 if (ret < 0)
1328 goto fail;
1329 8248 fgp->sch_idx = ret;
1330
1331 8249 fail:
1332 8249 avfilter_inout_free(&inputs);
1333 8249 avfilter_inout_free(&outputs);
1334 8249 avfilter_graph_free(&graph);
1335
1336
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8248 times.
8249 if (ret < 0)
1337 1 return ret;
1338
1339 8248 return 0;
1340 }
1341
1342 6974 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 6974 const enum AVMediaType type = ist->par->codec_type;
1349 FilterGraph *fg;
1350 FilterGraphPriv *fgp;
1351 int ret;
1352
1353 6974 ret = fg_create(pfg, graph_desc, sch, NULL);
1354
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6973 times.
6974 if (ret < 0)
1355 1 return ret;
1356 6973 fg = *pfg;
1357 6973 fgp = fgp_from_fg(fg);
1358
1359 6973 fgp->is_simple = 1;
1360
1361 6973 snprintf(fgp->log_name, sizeof(fgp->log_name), "%cf%s",
1362 6973 av_get_media_type_string(type)[0], opts->name);
1363
1364
2/4
✓ Branch 0 taken 6973 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6973 times.
6973 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 6973 times.
6973 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 6973 ret = ifilter_bind_ist(fg->inputs[0], ist, opts->vs);
1381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6973 times.
6973 if (ret < 0)
1382 return ret;
1383
1384 6973 ret = ofilter_bind_enc(fg->outputs[0], sched_idx_enc, opts);
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6973 times.
6973 if (ret < 0)
1386 return ret;
1387
1388
2/2
✓ Branch 0 taken 4320 times.
✓ Branch 1 taken 2653 times.
6973 if (opts->nb_threads >= 0)
1389 4320 fgp->nb_threads = opts->nb_threads;
1390
1391 6973 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 2547 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 2547 times.
2911 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 2547 return 0;
1583 }
1584
1585 8813 int fg_finalise_bindings(void)
1586 {
1587 int ret;
1588
1589
2/2
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 8813 times.
10088 for (int i = 0; i < nb_filtergraphs; i++) {
1590 1275 ret = bind_inputs(filtergraphs[i], 0);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1275 times.
1275 if (ret < 0)
1592 return ret;
1593 }
1594
1595 // check that all outputs were bound
1596
2/2
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 8813 times.
10088 for (int i = nb_filtergraphs - 1; i >= 0; i--) {
1597 1275 FilterGraph *fg = filtergraphs[i];
1598 1275 FilterGraphPriv *fgp = fgp_from_fg(filtergraphs[i]);
1599
1600
2/2
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1272 times.
2682 for (int j = 0; j < fg->nb_outputs; j++) {
1601 1410 OutputFilter *output = fg->outputs[j];
1602
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1407 times.
1410 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 1272 times.
✓ Branch 1 taken 8813 times.
10085 for (int i = 0; i < nb_filtergraphs; i++) {
1628 1272 ret = bind_inputs(filtergraphs[i], 1);
1629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1272 times.
1272 if (ret < 0)
1630 return ret;
1631 }
1632
1633 8813 return 0;
1634 }
1635
1636 15623 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 15623 AVFilterGraph *graph = (*last_filter)->graph;
1641 AVFilterContext *ctx;
1642 const AVFilter *trim;
1643 15623 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
1644
2/2
✓ Branch 0 taken 12786 times.
✓ Branch 1 taken 2837 times.
15623 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
1645 15623 int ret = 0;
1646
1647
4/4
✓ Branch 0 taken 14245 times.
✓ Branch 1 taken 1378 times.
✓ Branch 2 taken 14221 times.
✓ Branch 3 taken 24 times.
15623 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
1648 14221 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 6976 static int configure_output_video_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1714 OutputFilter *ofilter, AVFilterInOut *out)
1715 {
1716 6976 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1717 6976 AVFilterContext *last_filter = out->filter_ctx;
1718 AVBPrint bprint;
1719 6976 int pad_idx = out->pad_idx;
1720 int ret;
1721 char name[255];
1722
1723 6976 snprintf(name, sizeof(name), "out_%s", ofilter->output_name);
1724 6976 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 6976 times.
6976 if (ret < 0)
1729 return ret;
1730
1731
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6973 times.
6976 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 6973 times.
6976 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 5580 times.
✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5580 times.
✓ Branch 4 taken 1396 times.
✗ Branch 5 not taken.
6976 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 6976 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6976 av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) ||
1805 ofp->format != AV_PIX_FMT_NONE || !ofp->pix_fmts);
1806 6976 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1807 6976 choose_pix_fmts(ofp, &bprint);
1808 6976 choose_color_spaces(ofp, &bprint);
1809 6976 choose_color_ranges(ofp, &bprint);
1810 6976 choose_alpha_modes(ofp, &bprint);
1811
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6976 times.
6976 if (!av_bprint_is_complete(&bprint))
1812 return AVERROR(ENOMEM);
1813
1814
2/2
✓ Branch 0 taken 5790 times.
✓ Branch 1 taken 1186 times.
6976 if (bprint.len) {
1815 AVFilterContext *filter;
1816
1817 5790 ret = avfilter_graph_create_filter(&filter,
1818 avfilter_get_by_name("format"),
1819 5790 "format", bprint.str, NULL, graph);
1820 5790 av_bprint_finalize(&bprint, NULL);
1821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5790 times.
5790 if (ret < 0)
1822 return ret;
1823
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5790 times.
5790 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
1824 return ret;
1825
1826 5790 last_filter = filter;
1827 5790 pad_idx = 0;
1828 }
1829
1830 6976 snprintf(name, sizeof(name), "trim_out_%s", ofilter->output_name);
1831 6976 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 6976 times.
6976 if (ret < 0)
1834 return ret;
1835
1836
1837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6976 times.
6976 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
1838 return ret;
1839
1840 6976 return 0;
1841 }
1842
1843 1450 static int configure_output_audio_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1844 OutputFilter *ofilter, AVFilterInOut *out)
1845 {
1846 1450 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
1847 1450 AVFilterContext *last_filter = out->filter_ctx;
1848 1450 int pad_idx = out->pad_idx;
1849 AVBPrint args;
1850 char name[255];
1851 int ret;
1852
1853 1450 snprintf(name, sizeof(name), "out_%s", ofilter->output_name);
1854 1450 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 1450 times.
1450 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 1450 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
1880
1881 1450 choose_sample_fmts(ofp, &args);
1882 1450 choose_sample_rates(ofp, &args);
1883 1450 choose_channel_layouts(ofp, &args);
1884
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1450 times.
1450 if (!av_bprint_is_complete(&args)) {
1885 ret = AVERROR(ENOMEM);
1886 goto fail;
1887 }
1888
1/2
✓ Branch 0 taken 1450 times.
✗ Branch 1 not taken.
1450 if (args.len) {
1889 AVFilterContext *format;
1890
1891 1450 snprintf(name, sizeof(name), "format_out_%s", ofilter->output_name);
1892 1450 ret = avfilter_graph_create_filter(&format,
1893 avfilter_get_by_name("aformat"),
1894 1450 name, args.str, NULL, graph);
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1450 times.
1450 if (ret < 0)
1896 goto fail;
1897
1898 1450 ret = avfilter_link(last_filter, pad_idx, format, 0);
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1450 times.
1450 if (ret < 0)
1900 goto fail;
1901
1902 1450 last_filter = format;
1903 1450 pad_idx = 0;
1904 }
1905
1906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1450 times.
1450 if (ofilter->apad)
1907 AUTO_INSERT_FILTER("-apad", "apad", ofilter->apad);
1908
1909 1450 snprintf(name, sizeof(name), "trim for output %s", ofilter->output_name);
1910 1450 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 1450 times.
1450 if (ret < 0)
1913 goto fail;
1914
1915
1/2
✓ Branch 1 taken 1450 times.
✗ Branch 2 not taken.
1450 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
1916 goto fail;
1917 1450 fail:
1918 1450 av_bprint_finalize(&args, NULL);
1919
1920 1450 return ret;
1921 }
1922
1923 8426 static int configure_output_filter(FilterGraphPriv *fgp, AVFilterGraph *graph,
1924 OutputFilter *ofilter, AVFilterInOut *out)
1925 {
1926
2/3
✓ Branch 0 taken 6976 times.
✓ Branch 1 taken 1450 times.
✗ Branch 2 not taken.
8426 switch (ofilter->type) {
1927 6976 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fgp, graph, ofilter, out);
1928 1450 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 5810 static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
1945 InputFilter *ifilter, AVFilterInOut *in)
1946 {
1947 5810 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
1948
1949 AVFilterContext *last_filter;
1950 5810 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
1951 const AVPixFmtDescriptor *desc;
1952 char name[255];
1953 5810 int ret, pad_idx = 0;
1954 5810 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
1955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5810 times.
5810 if (!par)
1956 return AVERROR(ENOMEM);
1957
1958
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5806 times.
5810 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
1959 4 sub2video_prepare(ifp);
1960
1961 5810 snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index,
1962 ifp->opts.name);
1963
1964 5810 ifilter->filter = avfilter_graph_alloc_filter(graph, buffer_filt, name);
1965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5810 times.
5810 if (!ifilter->filter) {
1966 ret = AVERROR(ENOMEM);
1967 goto fail;
1968 }
1969
1970 5810 par->format = ifp->format;
1971 5810 par->time_base = ifp->time_base;
1972 5810 par->frame_rate = ifp->opts.framerate;
1973 5810 par->width = ifp->width;
1974 5810 par->height = ifp->height;
1975 5810 par->sample_aspect_ratio = ifp->sample_aspect_ratio.den > 0 ?
1976
2/2
✓ Branch 0 taken 5806 times.
✓ Branch 1 taken 4 times.
5810 ifp->sample_aspect_ratio : (AVRational){ 0, 1 };
1977 5810 par->color_space = ifp->color_space;
1978 5810 par->color_range = ifp->color_range;
1979 5810 par->alpha_mode = ifp->alpha_mode;
1980 5810 par->hw_frames_ctx = ifp->hw_frames_ctx;
1981 5810 par->side_data = ifp->side_data;
1982 5810 par->nb_side_data = ifp->nb_side_data;
1983
1984 5810 ret = av_buffersrc_parameters_set(ifilter->filter, par);
1985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5810 times.
5810 if (ret < 0)
1986 goto fail;
1987 5810 av_freep(&par);
1988
1989 5810 ret = avfilter_init_dict(ifilter->filter, NULL);
1990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5810 times.
5810 if (ret < 0)
1991 goto fail;
1992
1993 5810 last_filter = ifilter->filter;
1994
1995 5810 desc = av_pix_fmt_desc_get(ifp->format);
1996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5810 times.
5810 av_assert0(desc);
1997
1998
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5804 times.
5810 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 5810 ifp->displaymatrix_applied = 0;
2011
2/2
✓ Branch 0 taken 5805 times.
✓ Branch 1 taken 5 times.
5810 if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
2012
1/2
✓ Branch 0 taken 5805 times.
✗ Branch 1 not taken.
5805 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
2013 5805 int32_t *displaymatrix = ifp->displaymatrix;
2014 double theta;
2015
2016 5805 theta = get_rotation(displaymatrix);
2017
2018
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5790 times.
5805 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 5790 times.
5790 } 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 5790 times.
5790 } 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 5790 times.
5790 } 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 5781 times.
5790 } 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 5805 times.
5805 if (ret < 0)
2043 return ret;
2044
2045 5805 ifp->displaymatrix_applied = 1;
2046 }
2047
2048 5810 snprintf(name, sizeof(name), "trim_in_%s", ifp->opts.name);
2049 5810 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 5810 times.
5810 if (ret < 0)
2052 return ret;
2053
2054
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5810 times.
5810 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
2055 return ret;
2056 5810 return 0;
2057 fail:
2058 av_freep(&par);
2059
2060 return ret;
2061 }
2062
2063 1387 static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
2064 InputFilter *ifilter, AVFilterInOut *in)
2065 {
2066 1387 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2067 AVFilterContext *last_filter;
2068 AVBufferSrcParameters *par;
2069 1387 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
2070 AVBPrint args;
2071 char name[255];
2072 1387 int ret, pad_idx = 0;
2073
2074 1387 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
2075 1387 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 1387 av_get_sample_fmt_name(ifp->format));
2079
1/2
✓ Branch 1 taken 1387 times.
✗ Branch 2 not taken.
1387 if (av_channel_layout_check(&ifp->ch_layout) &&
2080
2/2
✓ Branch 0 taken 1367 times.
✓ Branch 1 taken 20 times.
1387 ifp->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
2081 1367 av_bprintf(&args, ":channel_layout=");
2082 1367 av_channel_layout_describe_bprint(&ifp->ch_layout, &args);
2083 } else
2084 20 av_bprintf(&args, ":channels=%d", ifp->ch_layout.nb_channels);
2085 1387 snprintf(name, sizeof(name), "graph_%d_in_%s", fg->index, ifp->opts.name);
2086
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1387 times.
1387 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
2088 1387 name, args.str, NULL,
2089 graph)) < 0)
2090 return ret;
2091 1387 par = av_buffersrc_parameters_alloc();
2092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1387 times.
1387 if (!par)
2093 return AVERROR(ENOMEM);
2094 1387 par->side_data = ifp->side_data;
2095 1387 par->nb_side_data = ifp->nb_side_data;
2096 1387 ret = av_buffersrc_parameters_set(ifilter->filter, par);
2097 1387 av_free(par);
2098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1387 times.
1387 if (ret < 0)
2099 return ret;
2100 1387 last_filter = ifilter->filter;
2101
2102 1387 snprintf(name, sizeof(name), "trim for input stream %s", ifp->opts.name);
2103 1387 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 1387 times.
1387 if (ret < 0)
2106 return ret;
2107
2108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1387 times.
1387 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
2109 return ret;
2110
2111 1387 return 0;
2112 }
2113
2114 7197 static int configure_input_filter(FilterGraph *fg, AVFilterGraph *graph,
2115 InputFilter *ifilter, AVFilterInOut *in)
2116 {
2117
2/3
✓ Branch 0 taken 5810 times.
✓ Branch 1 taken 1387 times.
✗ Branch 2 not taken.
7197 switch (ifilter->type) {
2118 5810 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, graph, ifilter, in);
2119 1387 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, graph, ifilter, in);
2120 default: av_assert0(0); return 0;
2121 }
2122 }
2123
2124 8292 static void cleanup_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
2125 {
2126
2/2
✓ Branch 0 taken 8427 times.
✓ Branch 1 taken 8292 times.
16719 for (int i = 0; i < fg->nb_outputs; i++)
2127 8427 fg->outputs[i]->filter = NULL;
2128
2/2
✓ Branch 0 taken 7198 times.
✓ Branch 1 taken 8292 times.
15490 for (int i = 0; i < fg->nb_inputs; i++)
2129 7198 fg->inputs[i]->filter = NULL;
2130 8292 avfilter_graph_free(&fgt->graph);
2131 8292 }
2132
2133 9553 static int filter_is_buffersrc(const AVFilterContext *f)
2134 {
2135
2/2
✓ Branch 0 taken 4309 times.
✓ Branch 1 taken 5244 times.
13862 return f->nb_inputs == 0 &&
2136
2/2
✓ Branch 0 taken 1905 times.
✓ Branch 1 taken 2404 times.
4309 (!strcmp(f->filter->name, "buffer") ||
2137
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 1200 times.
1905 !strcmp(f->filter->name, "abuffer"));
2138 }
2139
2140 8290 static int graph_is_meta(AVFilterGraph *graph)
2141 {
2142
2/2
✓ Branch 0 taken 18305 times.
✓ Branch 1 taken 1846 times.
20151 for (unsigned i = 0; i < graph->nb_filters; i++) {
2143 18305 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 12662 times.
✓ Branch 1 taken 5643 times.
✓ Branch 2 taken 6444 times.
✓ Branch 3 taken 3109 times.
27858 if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
2150
2/2
✓ Branch 0 taken 9553 times.
✓ Branch 1 taken 3109 times.
12662 f->nb_outputs == 0 ||
2151 9553 filter_is_buffersrc(f)))
2152 6444 return 0;
2153 }
2154 1846 return 1;
2155 }
2156
2157 static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer);
2158
2159 8291 static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
2160 {
2161 8291 FilterGraphPriv *fgp = fgp_from_fg(fg);
2162 AVBufferRef *hw_device;
2163 AVFilterInOut *inputs, *outputs, *cur;
2164 8291 int ret = AVERROR_BUG, i, simple = filtergraph_is_simple(fg);
2165 8291 int have_input_eof = 0;
2166 8291 const char *graph_desc = fg->graph_desc;
2167
2168 8291 cleanup_filtergraph(fg, fgt);
2169 8291 fgt->graph = avfilter_graph_alloc();
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8291 times.
8291 if (!fgt->graph)
2171 return AVERROR(ENOMEM);
2172
2173
2/2
✓ Branch 0 taken 7019 times.
✓ Branch 1 taken 1272 times.
8291 if (simple) {
2174 7019 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
2175
2176
2/2
✓ Branch 0 taken 391 times.
✓ Branch 1 taken 6628 times.
7019 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 4103 times.
✓ Branch 1 taken 2525 times.
6628 } else if (fgp->nb_threads >= 0) {
2181 4103 ret = av_opt_set_int(fgt->graph, "threads", fgp->nb_threads, 0);
2182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4103 times.
4103 if (ret < 0)
2183 return ret;
2184 }
2185
2186
2/2
✓ Branch 1 taken 4469 times.
✓ Branch 2 taken 2550 times.
7019 if (av_dict_count(ofp->sws_opts)) {
2187 4469 ret = av_dict_get_string(ofp->sws_opts,
2188 4469 &fgt->graph->scale_sws_opts,
2189 '=', ':');
2190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4469 times.
4469 if (ret < 0)
2191 goto fail;
2192 }
2193
2194
2/2
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 6954 times.
7019 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 1272 fgt->graph->nb_threads = filter_complex_nbthreads;
2204 }
2205
2206
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8290 times.
8291 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 8291 hw_device = hw_device_for_filter();
2213
2214 8291 ret = graph_parse(fg, fgt->graph, graph_desc, &inputs, &outputs, hw_device);
2215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8291 times.
8291 if (ret < 0)
2216 goto fail;
2217
2218
2/2
✓ Branch 0 taken 7197 times.
✓ Branch 1 taken 8291 times.
15488 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
2219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7197 times.
7197 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 8291 avfilter_inout_free(&inputs);
2225
2226
2/2
✓ Branch 0 taken 8426 times.
✓ Branch 1 taken 8291 times.
16717 for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
2227 8426 ret = configure_output_filter(fgp, fgt->graph, fg->outputs[i], cur);
2228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8426 times.
8426 if (ret < 0) {
2229 avfilter_inout_free(&outputs);
2230 goto fail;
2231 }
2232 }
2233 8291 avfilter_inout_free(&outputs);
2234
2235
2/2
✓ Branch 0 taken 6360 times.
✓ Branch 1 taken 1931 times.
8291 if (fgp->disable_conversions)
2236 6360 avfilter_graph_set_auto_convert(fgt->graph, AVFILTER_AUTO_CONVERT_NONE);
2237
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8290 times.
8291 if ((ret = avfilter_graph_config(fgt->graph, NULL)) < 0)
2238 1 goto fail;
2239
2240 8290 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 8425 times.
✓ Branch 1 taken 8290 times.
16715 for (int i = 0; i < fg->nb_outputs; i++) {
2245 const AVFrameSideData *const *sd;
2246 int nb_sd;
2247 8425 OutputFilter *ofilter = fg->outputs[i];
2248 8425 OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
2249 8425 AVFilterContext *sink = ofilter->filter;
2250
2251 8425 ofp->format = av_buffersink_get_format(sink);
2252
2253 8425 ofp->width = av_buffersink_get_w(sink);
2254 8425 ofp->height = av_buffersink_get_h(sink);
2255 8425 ofp->color_space = av_buffersink_get_colorspace(sink);
2256 8425 ofp->color_range = av_buffersink_get_color_range(sink);
2257 8425 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 8378 times.
✓ Branch 1 taken 47 times.
8425 if (!ofp->tb_out_locked) {
2263 8378 AVRational fr = av_buffersink_get_frame_rate(sink);
2264
3/4
✓ Branch 0 taken 8357 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 8357 times.
✗ Branch 3 not taken.
8378 if (ofp->fps.framerate.num <= 0 && ofp->fps.framerate.den <= 0 &&
2265
4/4
✓ Branch 0 taken 6906 times.
✓ Branch 1 taken 1451 times.
✓ Branch 2 taken 6892 times.
✓ Branch 3 taken 14 times.
8357 fr.num > 0 && fr.den > 0)
2266 6892 ofp->fps.framerate = fr;
2267 8378 ofp->tb_out = av_buffersink_get_time_base(sink);
2268 }
2269 8425 ofp->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
2270
2271 8425 ofp->sample_rate = av_buffersink_get_sample_rate(sink);
2272 8425 av_channel_layout_uninit(&ofp->ch_layout);
2273 8425 ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout);
2274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (ret < 0)
2275 goto fail;
2276 8425 sd = av_buffersink_get_side_data(sink, &nb_sd);
2277
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 8157 times.
8425 if (nb_sd)
2278
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 268 times.
557 for (int j = 0; j < nb_sd; j++) {
2279 289 ret = av_frame_side_data_clone(&ofp->side_data, &ofp->nb_side_data,
2280 289 sd[j], AV_FRAME_SIDE_DATA_FLAG_REPLACE);
2281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 289 times.
289 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 7196 times.
✓ Branch 1 taken 8290 times.
15486 for (int i = 0; i < fg->nb_inputs; i++) {
2289 7196 InputFilter *ifilter = fg->inputs[i];
2290 7196 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
2291 AVFrame *tmp;
2292
2/2
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 7196 times.
7379 while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) {
2293
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 145 times.
183 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
2294 38 sub2video_frame(&ifp->ifilter, tmp, !fgt->graph);
2295 } else {
2296
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 100 times.
145 if (ifp->type_src == AVMEDIA_TYPE_VIDEO) {
2297
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (ifp->displaymatrix_applied)
2298 45 av_frame_remove_side_data(tmp, AV_FRAME_DATA_DISPLAYMATRIX);
2299 }
2300 145 ret = av_buffersrc_add_frame(ifilter->filter, tmp);
2301 }
2302 183 av_frame_free(&tmp);
2303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (ret < 0)
2304 goto fail;
2305 }
2306 }
2307
2308 /* send the EOFs for the finished inputs */
2309
2/2
✓ Branch 0 taken 7196 times.
✓ Branch 1 taken 8290 times.
15486 for (int i = 0; i < fg->nb_inputs; i++) {
2310 7196 InputFilter *ifilter = fg->inputs[i];
2311
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7180 times.
7196 if (fgt->eof_in[i]) {
2312 16 ret = av_buffersrc_add_frame(ifilter->filter, NULL);
2313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
2314 goto fail;
2315 16 have_input_eof = 1;
2316 }
2317 }
2318
2319
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8280 times.
8290 if (have_input_eof) {
2320 // make sure the EOF propagates to the end of the graph
2321 10 ret = avfilter_graph_request_oldest(fgt->graph);
2322
4/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
10 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2323 goto fail;
2324 }
2325
2326 8290 return 0;
2327 1 fail:
2328 1 cleanup_filtergraph(fg, fgt);
2329 1 return ret;
2330 }
2331
2332 7191 static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
2333 {
2334 7191 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2335 AVFrameSideData *sd;
2336 int ret;
2337
2338 7191 ret = av_buffer_replace(&ifp->hw_frames_ctx, frame->hw_frames_ctx);
2339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7191 times.
7191 if (ret < 0)
2340 return ret;
2341
2342
2/2
✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 5807 times.
12966 ifp->time_base = (ifilter->type == AVMEDIA_TYPE_AUDIO) ? (AVRational){ 1, frame->sample_rate } :
2343
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5775 times.
5807 (ifp->opts.flags & IFILTER_FLAG_CFR) ? av_inv_q(ifp->opts.framerate) :
2344 frame->time_base;
2345
2346 7191 ifp->format = frame->format;
2347
2348 7191 ifp->width = frame->width;
2349 7191 ifp->height = frame->height;
2350 7191 ifp->sample_aspect_ratio = frame->sample_aspect_ratio;
2351 7191 ifp->color_space = frame->colorspace;
2352 7191 ifp->color_range = frame->color_range;
2353 7191 ifp->alpha_mode = frame->alpha_mode;
2354
2355 7191 ifp->sample_rate = frame->sample_rate;
2356 7191 ret = av_channel_layout_copy(&ifp->ch_layout, &frame->ch_layout);
2357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7191 times.
7191 if (ret < 0)
2358 return ret;
2359
2360 7191 av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
2361
2/2
✓ Branch 0 taken 637 times.
✓ Branch 1 taken 7191 times.
7828 for (int i = 0; i < frame->nb_side_data; i++) {
2362 637 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 303 times.
637 if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL))
2365 334 continue;
2366
2367 303 ret = av_frame_side_data_clone(&ifp->side_data,
2368 &ifp->nb_side_data,
2369 303 frame->side_data[i], 0);
2370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret < 0)
2371 return ret;
2372 }
2373
2374 7191 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
2375
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 7164 times.
7191 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 7191 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 7191 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO);
2386
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7183 times.
7191 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 7191 ifp->downmixinfo_present = !!sd;
2394 7191 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_MATRIX);
2395
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7187 times.
7191 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 7191 ifp->downmixmatrix_present = !!sd;
2406
2407 7191 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 39210 int filtergraph_is_simple(const FilterGraph *fg)
2432 {
2433 39210 const FilterGraphPriv *fgp = cfgp_from_cfg(fg);
2434 39210 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 421250 static int choose_input(const FilterGraph *fg, const FilterGraphThread *fgt)
2463 {
2464 421250 int nb_requests, nb_requests_max = -1;
2465 421250 int best_input = -1;
2466
2467
2/2
✓ Branch 0 taken 436192 times.
✓ Branch 1 taken 421250 times.
857442 for (int i = 0; i < fg->nb_inputs; i++) {
2468 436192 InputFilter *ifilter = fg->inputs[i];
2469
2470
2/2
✓ Branch 0 taken 1243 times.
✓ Branch 1 taken 434949 times.
436192 if (fgt->eof_in[i])
2471 1243 continue;
2472
2473 434949 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
2474
2/2
✓ Branch 0 taken 423791 times.
✓ Branch 1 taken 11158 times.
434949 if (nb_requests > nb_requests_max) {
2475 423791 nb_requests_max = nb_requests;
2476 423791 best_input = i;
2477 }
2478 }
2479
2480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421250 times.
421250 av_assert0(best_input >= 0);
2481
2482 421250 return best_input;
2483 }
2484
2485 8375 static int choose_out_timebase(OutputFilterPriv *ofp, AVFrame *frame)
2486 {
2487 8375 OutputFilter *ofilter = &ofp->ofilter;
2488 8375 FPSConvContext *fps = &ofp->fps;
2489 8375 AVRational tb = (AVRational){ 0, 0 };
2490 AVRational fr;
2491 const FrameData *fd;
2492
2493 8375 fd = frame_data_c(frame);
2494
2495 // apply -enc_time_base
2496
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8374 times.
8375 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 8374 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8375 switch (ofp->enc_timebase.num) {
2504 8374 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 1446 times.
✓ Branch 1 taken 6929 times.
8375 if (ofilter->type == AVMEDIA_TYPE_AUDIO) {
2511
1/2
✓ Branch 0 taken 1446 times.
✗ Branch 1 not taken.
1446 tb = tb.num ? tb : (AVRational){ 1, frame->sample_rate };
2512 1446 goto finish;
2513 }
2514
2515 6929 fr = fps->framerate;
2516
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6913 times.
6929 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 5773 times.
✓ Branch 1 taken 1156 times.
✓ Branch 2 taken 366 times.
✓ Branch 3 taken 5407 times.
6929 if (fps->vsync_method == VSYNC_CFR || fps->vsync_method == VSYNC_VSCFR) {
2523
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1522 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1522 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 1522 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1522 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 6913 times.
✓ Branch 1 taken 16 times.
6929 if (fr.num > 0) {
2539
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 6837 times.
6913 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 71 times.
✓ Branch 1 taken 6842 times.
6913 if (fps->framerate_clip) {
2544 71 av_reduce(&fr.num, &fr.den,
2545 71 fr.num, fr.den, fps->framerate_clip);
2546 }
2547 }
2548
2549
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6928 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
6929 if (!(tb.num > 0 && tb.den > 0))
2550 6928 tb = av_inv_q(fr);
2551
3/4
✓ Branch 0 taken 6913 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6913 times.
6929 if (!(tb.num > 0 && tb.den > 0))
2552 16 tb = frame->time_base;
2553
2554 6929 fps->framerate = fr;
2555 8375 finish:
2556 8375 ofp->tb_out = tb;
2557 8375 ofp->tb_out_locked = 1;
2558
2559 8375 return 0;
2560 }
2561
2562 143934 static double adjust_frame_pts_to_encoder_tb(void *logctx, AVFrame *frame,
2563 AVRational tb_dst, int64_t start_time)
2564 {
2565 143934 double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
2566
2567 143934 AVRational tb = tb_dst;
2568 143934 AVRational filter_tb = frame->time_base;
2569 143934 const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
2570
2571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143934 times.
143934 if (frame->pts == AV_NOPTS_VALUE)
2572 goto early_exit;
2573
2574 143934 tb.den <<= extra_bits;
2575 143934 float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
2576 143934 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
2577 143934 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 137911 times.
143934 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 143934 frame->pts = av_rescale_q(frame->pts, filter_tb, tb_dst) -
2585 143934 av_rescale_q(start_time, AV_TIME_BASE_Q, tb_dst);
2586 143934 frame->time_base = tb_dst;
2587
2588 143934 early_exit:
2589
2590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143934 times.
143934 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 143934 return float_pts;
2599 }
2600
2601 3876 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 3876 times.
✗ Branch 1 not taken.
3876 if (a >= b) {
2606 3876 max2 = a;
2607 3876 min2 = b;
2608 } else {
2609 max2 = b;
2610 min2 = a;
2611 }
2612 3876 m = (c >= max2) ? max2 : c;
2613
2614 3876 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 147810 static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame,
2623 int64_t *nb_frames, int64_t *nb_frames_prev)
2624 {
2625 147810 OutputFilter *ofilter = &ofp->ofilter;
2626 147810 FPSConvContext *fps = &ofp->fps;
2627 double delta0, delta, sync_ipts, duration;
2628
2629
2/2
✓ Branch 0 taken 3876 times.
✓ Branch 1 taken 143934 times.
147810 if (!frame) {
2630 3876 *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 3875 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3873 times.
3876 if (!*nb_frames && fps->last_dropped) {
2635 2 atomic_fetch_add(&ofilter->nb_frames_drop, 1);
2636 2 fps->last_dropped++;
2637 }
2638
2639 3876 goto finish;
2640 }
2641
2642 143934 duration = frame->duration * av_q2d(frame->time_base) / av_q2d(ofp->tb_out);
2643
2644 143934 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 143934 delta0 = sync_ipts - ofp->next_pts;
2649 143934 delta = delta0 + duration;
2650
2651 // tracks the number of times the PREVIOUS frame should be duplicated,
2652 // mostly for variable framerate (VFR)
2653 143934 *nb_frames_prev = 0;
2654 /* by default, we output a single frame */
2655 143934 *nb_frames = 1;
2656
2657
4/4
✓ Branch 0 taken 5034 times.
✓ Branch 1 taken 138900 times.
✓ Branch 2 taken 3847 times.
✓ Branch 3 taken 1187 times.
143934 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 84181 times.
✓ Branch 3 taken 22901 times.
✗ Branch 4 not taken.
143934 switch (fps->vsync_method) {
2670 7838 case VSYNC_VSCFR:
2671
3/4
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 7472 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 366 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 84181 case VSYNC_VFR:
2692
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 84082 times.
84181 if (delta <= -0.6)
2693 99 *nb_frames = 0;
2694
2/2
✓ Branch 0 taken 66729 times.
✓ Branch 1 taken 17353 times.
84082 else if (delta > 0.6)
2695 66729 ofp->next_pts = llrint(sync_ipts);
2696 84181 frame->duration = llrint(duration);
2697 84181 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 147810 finish:
2707 147810 memmove(fps->frames_prev_hist + 1,
2708 147810 fps->frames_prev_hist,
2709 sizeof(fps->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(fps->frames_prev_hist) - 1));
2710 147810 fps->frames_prev_hist[0] = *nb_frames_prev;
2711
2712
4/4
✓ Branch 0 taken 147807 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 474 times.
✓ Branch 3 taken 147333 times.
147810 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 147807 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 269 times.
✓ Branch 5 taken 147541 times.
147810 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 4350 times.
✓ Branch 1 taken 143460 times.
✓ Branch 2 taken 474 times.
✓ Branch 3 taken 3876 times.
147810 fps->last_dropped = *nb_frames == *nb_frames_prev && frame;
2736
4/4
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 147336 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 467 times.
147810 fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY);
2737 }
2738
2739 1775 static void close_input(InputFilterPriv *ifp)
2740 {
2741 1775 FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
2742
2743
2/2
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 1368 times.
1775 if (!ifp->eof) {
2744 407 sch_filter_receive_finish(fgp->sch, fgp->sch_idx, ifp->ifilter.index);
2745 407 ifp->eof = 1;
2746 }
2747 1775 }
2748
2749 5284 static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt)
2750 {
2751 5284 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 5281 times.
5284 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 5284 fgt->eof_out[ofp->ofilter.index] = 1;
2800
2801 5284 ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, NULL);
2802
2/2
✓ Branch 0 taken 5120 times.
✓ Branch 1 taken 164 times.
5284 return (ret == AVERROR_EOF) ? 0 : ret;
2803 }
2804
2805 455457 static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2806 AVFrame *frame)
2807 {
2808 455457 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2809 455457 AVFrame *frame_prev = ofp->fps.last_frame;
2810 455457 enum AVMediaType type = ofp->ofilter.type;
2811
2812 455457 int64_t nb_frames = !!frame, nb_frames_prev = 0;
2813
2814
5/6
✓ Branch 0 taken 147810 times.
✓ Branch 1 taken 307647 times.
✓ Branch 2 taken 3876 times.
✓ Branch 3 taken 143934 times.
✓ Branch 4 taken 3876 times.
✗ Branch 5 not taken.
455457 if (type == AVMEDIA_TYPE_VIDEO && (frame || fgt->got_frame))
2815 147810 video_sync_process(ofp, frame, &nb_frames, &nb_frames_prev);
2816
2817
2/2
✓ Branch 0 taken 450131 times.
✓ Branch 1 taken 452363 times.
902494 for (int64_t i = 0; i < nb_frames; i++) {
2818 AVFrame *frame_out;
2819 int ret;
2820
2821
2/2
✓ Branch 0 taken 143892 times.
✓ Branch 1 taken 306239 times.
450131 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 143823 times.
143961 frame_prev : frame;
2824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143892 times.
143892 if (!frame_in)
2825 break;
2826
2827 143892 frame_out = fgp->frame_enc;
2828 143892 ret = av_frame_ref(frame_out, frame_in);
2829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143892 times.
143892 if (ret < 0)
2830 return ret;
2831
2832 143892 frame_out->pts = ofp->next_pts;
2833
2834
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 143885 times.
143892 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 306239 times.
306239 frame->pts = (frame->pts == AV_NOPTS_VALUE) ? ofp->next_pts :
2840 306239 av_rescale_q(frame->pts, frame->time_base, ofp->tb_out) -
2841 306239 av_rescale_q(ofp->ts_offset, AV_TIME_BASE_Q, ofp->tb_out);
2842
2843 306239 frame->time_base = ofp->tb_out;
2844 306239 frame->duration = av_rescale_q(frame->nb_samples,
2845 306239 (AVRational){ 1, frame->sample_rate },
2846 ofp->tb_out);
2847
2848 306239 ofp->next_pts = frame->pts + frame->duration;
2849
2850 306239 frame_out = frame;
2851 }
2852
2853 // send the frame to consumers
2854 450131 ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->ofilter.index, frame_out);
2855
2/2
✓ Branch 0 taken 3094 times.
✓ Branch 1 taken 447037 times.
450131 if (ret < 0) {
2856 3094 av_frame_unref(frame_out);
2857
2858
1/2
✓ Branch 0 taken 3094 times.
✗ Branch 1 not taken.
3094 if (!fgt->eof_out[ofp->ofilter.index]) {
2859 3094 fgt->eof_out[ofp->ofilter.index] = 1;
2860 3094 fgp->nb_outputs_done++;
2861 }
2862
2863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3094 times.
3094 return ret == AVERROR_EOF ? 0 : ret;
2864 }
2865
2866
2/2
✓ Branch 0 taken 140839 times.
✓ Branch 1 taken 306198 times.
447037 if (type == AVMEDIA_TYPE_VIDEO) {
2867 140839 ofp->fps.frame_number++;
2868 140839 ofp->next_pts++;
2869
2870
3/4
✓ Branch 0 taken 140407 times.
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 140407 times.
✗ Branch 3 not taken.
140839 if (i == nb_frames_prev && frame)
2871 140407 frame->flags &= ~AV_FRAME_FLAG_KEY;
2872 }
2873
2874 447037 fgt->got_frame = 1;
2875 }
2876
2877
4/4
✓ Branch 0 taken 447079 times.
✓ Branch 1 taken 5284 times.
✓ Branch 2 taken 140880 times.
✓ Branch 3 taken 306199 times.
452363 if (frame && frame_prev) {
2878 140880 av_frame_unref(frame_prev);
2879 140880 av_frame_move_ref(frame_prev, frame);
2880 }
2881
2882
2/2
✓ Branch 0 taken 5284 times.
✓ Branch 1 taken 447079 times.
452363 if (!frame)
2883 5284 return close_output(ofp, fgt);
2884
2885 447079 return 0;
2886 }
2887
2888 919551 static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2889 AVFrame *frame)
2890 {
2891 919551 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2892 919551 AVFilterContext *filter = ofp->ofilter.filter;
2893 FrameData *fd;
2894 int ret;
2895
2896 919551 ret = av_buffersink_get_frame_flags(filter, frame,
2897 AV_BUFFERSINK_FLAG_NO_REQUEST);
2898
4/4
✓ Branch 0 taken 4311 times.
✓ Branch 1 taken 915240 times.
✓ Branch 2 taken 3676 times.
✓ Branch 3 taken 635 times.
919551 if (ret == AVERROR_EOF && !fgt->eof_out[ofp->ofilter.index]) {
2899 3676 ret = fg_output_frame(ofp, fgt, NULL);
2900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3676 times.
3676 return (ret < 0) ? ret : 1;
2901
4/4
✓ Branch 0 taken 450810 times.
✓ Branch 1 taken 465065 times.
✓ Branch 2 taken 635 times.
✓ Branch 3 taken 450175 times.
915875 } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2902 465700 return 1;
2903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450175 times.
450175 } 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 450173 times.
450175 if (fgt->eof_out[ofp->ofilter.index]) {
2911 2 av_frame_unref(frame);
2912 2 return 0;
2913 }
2914
2915 450173 frame->time_base = av_buffersink_get_time_base(filter);
2916
2917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450173 times.
450173 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 8375 times.
✓ Branch 1 taken 441798 times.
450173 if (!ofp->tb_out_locked) {
2924 8375 ret = choose_out_timebase(ofp, frame);
2925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8375 times.
8375 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 450173 fd = frame_data(frame);
2933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450173 times.
450173 if (!fd) {
2934 av_frame_unref(frame);
2935 return AVERROR(ENOMEM);
2936 }
2937
2938 450173 av_frame_side_data_free(&fd->side_data, &fd->nb_side_data);
2939
2/2
✓ Branch 0 taken 8241 times.
✓ Branch 1 taken 441932 times.
450173 if (!fgt->got_frame) {
2940 8241 ret = clone_side_data(&fd->side_data, &fd->nb_side_data,
2941 8241 ofp->side_data, ofp->nb_side_data, 0);
2942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8241 times.
8241 if (ret < 0) {
2943 av_frame_unref(frame);
2944 return ret;
2945 }
2946 }
2947
2948 450173 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 335249 times.
✓ Branch 1 taken 114924 times.
450173 if (!fgp->is_meta)
2953 335249 fd->bits_per_raw_sample = 0;
2954
2955
2/2
✓ Branch 0 taken 143934 times.
✓ Branch 1 taken 306239 times.
450173 if (ofp->ofilter.type == AVMEDIA_TYPE_VIDEO) {
2956
2/2
✓ Branch 0 taken 2421 times.
✓ Branch 1 taken 141513 times.
143934 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 143934 fd->frame_rate_filter = ofp->fps.framerate;
2963 }
2964
2965 450173 ret = fg_output_frame(ofp, fgt, frame);
2966 450173 av_frame_unref(frame);
2967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450173 times.
450173 if (ret < 0)
2968 return ret;
2969
2970 450173 return 0;
2971 }
2972
2973 /* retrieve all frames available at filtergraph outputs
2974 * and send them to consumers */
2975 461231 static int read_frames(FilterGraph *fg, FilterGraphThread *fgt,
2976 AVFrame *frame)
2977 {
2978 461231 FilterGraphPriv *fgp = fgp_from_fg(fg);
2979
2980 // graph not configured, just select the input to request
2981
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 461035 times.
461231 if (!fgt->graph) {
2982
1/2
✓ Branch 0 taken 522 times.
✗ Branch 1 not taken.
522 for (int i = 0; i < fg->nb_inputs; i++) {
2983 522 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
2984
3/4
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 326 times.
✓ Branch 2 taken 196 times.
✗ Branch 3 not taken.
522 if (ifp->format < 0 && !fgt->eof_in[i]) {
2985 196 fgt->next_in = i;
2986 196 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 457945 times.
✓ Branch 1 taken 3090 times.
461035 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 469376 times.
✓ Branch 1 taken 457945 times.
927321 for (int i = 0; i < fg->nb_outputs; i++) {
3002 469376 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3003
3004 469376 ret = 0;
3005
2/2
✓ Branch 0 taken 919551 times.
✓ Branch 1 taken 469376 times.
1388927 while (!ret) {
3006 919551 ret = fg_output_step(ofp, fgt, frame);
3007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 919551 times.
919551 if (ret < 0)
3008 return ret;
3009 }
3010 }
3011
3012
3013 457945 ret = avfilter_graph_request_oldest(fgt->graph);
3014
2/2
✓ Branch 0 taken 421250 times.
✓ Branch 1 taken 36695 times.
457945 if (ret == AVERROR(EAGAIN)) {
3015 421250 fgt->next_in = choose_input(fg, fgt);
3016 421250 return 0;
3017
2/2
✓ Branch 0 taken 5143 times.
✓ Branch 1 taken 31552 times.
36695 } else if (ret < 0) {
3018
1/2
✓ Branch 0 taken 5143 times.
✗ Branch 1 not taken.
5143 if (ret == AVERROR_EOF)
3019 5143 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 5143 return ret;
3025 }
3026 31552 fgt->next_in = fg->nb_inputs;
3027
3028 // return so that scheduler can rate-control us
3029 31552 return 0;
3030 }
3031
3032 3090 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 1078 static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer)
3058 {
3059 1078 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3060 int ret;
3061
3062
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1040 times.
1078 if (buffer) {
3063 AVFrame *tmp;
3064
3065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!frame)
3066 return 0;
3067
3068 38 tmp = av_frame_alloc();
3069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!tmp)
3070 return AVERROR(ENOMEM);
3071
3072 38 av_frame_move_ref(tmp, frame);
3073
3074 38 ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
3075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0) {
3076 av_frame_free(&tmp);
3077 return ret;
3078 }
3079
3080 38 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 3468 static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
3105 int64_t pts, AVRational tb)
3106 {
3107 3468 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3108 int ret;
3109
3110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3468 times.
3468 if (fgt->eof_in[ifilter->index])
3111 return 0;
3112
3113 3468 fgt->eof_in[ifilter->index] = 1;
3114
3115
2/2
✓ Branch 0 taken 3452 times.
✓ Branch 1 taken 16 times.
3468 if (ifilter->filter) {
3116 3452 pts = av_rescale_q_rnd(pts, tb, ifp->time_base,
3117 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
3118
3119 3452 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
3120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (ret < 0)
3121 return ret;
3122 } else {
3123
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13 times.
16 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 16 times.
16 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 3468 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 424197 static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
3181 InputFilter *ifilter, AVFrame *frame, int force_reinit)
3182 {
3183 424197 FilterGraphPriv *fgp = fgp_from_fg(fg);
3184 424197 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
3185 FrameData *fd;
3186 AVFrameSideData *sd;
3187 424197 int need_reinit = 0, ret;
3188
3189 /* determine if the parameters for this input changed */
3190
2/3
✓ Branch 0 taken 306076 times.
✓ Branch 1 taken 118121 times.
✗ Branch 2 not taken.
424197 switch (ifilter->type) {
3191 306076 case AVMEDIA_TYPE_AUDIO:
3192
2/2
✓ Branch 0 taken 304693 times.
✓ Branch 1 taken 1383 times.
306076 if (ifp->format != frame->format ||
3193
3/4
✓ Branch 0 taken 304693 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 304692 times.
609386 ifp->sample_rate != frame->sample_rate ||
3194 304693 av_channel_layout_compare(&ifp->ch_layout, &frame->ch_layout))
3195 1384 need_reinit |= AUDIO_CHANGED;
3196 306076 break;
3197 118121 case AVMEDIA_TYPE_VIDEO:
3198
2/2
✓ Branch 0 taken 112270 times.
✓ Branch 1 taken 5851 times.
118121 if (ifp->format != frame->format ||
3199
2/2
✓ Branch 0 taken 112242 times.
✓ Branch 1 taken 28 times.
112270 ifp->width != frame->width ||
3200
2/2
✓ Branch 0 taken 112235 times.
✓ Branch 1 taken 7 times.
112242 ifp->height != frame->height ||
3201
2/2
✓ Branch 0 taken 112218 times.
✓ Branch 1 taken 17 times.
112235 ifp->color_space != frame->colorspace ||
3202
1/2
✓ Branch 0 taken 112218 times.
✗ Branch 1 not taken.
112218 ifp->color_range != frame->color_range ||
3203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112218 times.
112218 ifp->alpha_mode != frame->alpha_mode)
3204 5903 need_reinit |= VIDEO_CHANGED;
3205 118121 break;
3206 }
3207
3208
2/2
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 423976 times.
424197 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 423976 times.
423976 } else if (ifp->displaymatrix_present)
3213 need_reinit |= MATRIX_CHANGED;
3214
3215
2/2
✓ Branch 1 taken 720 times.
✓ Branch 2 taken 423477 times.
424197 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 423477 times.
423477 } else if (ifp->downmixinfo_present)
3220 need_reinit |= DOWNMIX_CHANGED;
3221
3222
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 424175 times.
424197 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 424175 times.
424175 } else if (ifp->downmixmatrix_present)
3227 need_reinit |= DOWNMIX_CHANGED;
3228
3229
5/6
✓ Branch 0 taken 7287 times.
✓ Branch 1 taken 416910 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 7143 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 144 times.
424197 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 424010 times.
✓ Branch 2 taken 182 times.
✓ Branch 3 taken 5 times.
424197 if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
3237 182 need_reinit = 0;
3238
3239
1/2
✓ Branch 0 taken 424197 times.
✗ Branch 1 not taken.
424197 if (!!ifp->hw_frames_ctx != !!frame->hw_frames_ctx ||
3240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 424197 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
424197 (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 7191 times.
✓ Branch 1 taken 417006 times.
424197 if (need_reinit) {
3244 7191 ret = ifilter_parameters_from_frame(ifilter, frame);
3245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7191 times.
7191 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 7190 times.
7191 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 417006 times.
✓ Branch 1 taken 7191 times.
✓ Branch 2 taken 417006 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 416963 times.
424197 if (need_reinit || force_reinit || !fgt->graph) {
3259 7234 AVFrame *tmp = av_frame_alloc();
3260
3261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7234 times.
7234 if (!tmp)
3262 147 return AVERROR(ENOMEM);
3263
3264
2/2
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 7089 times.
7234 if (!ifilter_has_all_input_formats(fg)) {
3265 145 av_frame_move_ref(tmp, frame);
3266
3267 145 ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
3268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (ret < 0)
3269 av_frame_free(&tmp);
3270
3271 145 return ret;
3272 }
3273
3274
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7041 times.
7089 ret = fgt->graph ? read_frames(fg, fgt, tmp) : 0;
3275 7089 av_frame_free(&tmp);
3276
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7088 times.
7089 if (ret < 0)
3277 1 return ret;
3278
3279
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 7041 times.
7088 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 7041 sch_filter_choke_inputs(fgp->sch, fgp->sch_idx);
3314 }
3315
3316 7088 ret = configure_filtergraph(fg, fgt);
3317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7087 times.
7088 if (ret < 0) {
3318 1 av_log(fg, AV_LOG_ERROR, "Error reinitializing filters!\n");
3319 1 return ret;
3320 }
3321 }
3322
3323 424050 frame->pts = av_rescale_q(frame->pts, frame->time_base, ifp->time_base);
3324 424050 frame->duration = av_rescale_q(frame->duration, frame->time_base, ifp->time_base);
3325 424050 frame->time_base = ifp->time_base;
3326
3327
2/2
✓ Branch 0 taken 118020 times.
✓ Branch 1 taken 306030 times.
424050 if (ifp->displaymatrix_applied)
3328 118020 av_frame_remove_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
3329
3330 424050 fd = frame_data(frame);
3331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424050 times.
424050 if (!fd)
3332 return AVERROR(ENOMEM);
3333 424050 fd->wallclock[LATENCY_PROBE_FILTER_PRE] = av_gettime_relative();
3334
3335 424050 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame,
3336 AV_BUFFERSRC_FLAG_PUSH);
3337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424050 times.
424050 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 424050 return 0;
3345 }
3346
3347 8244 static void fg_thread_set_name(const FilterGraph *fg)
3348 {
3349 char name[16];
3350
2/2
✓ Branch 1 taken 6972 times.
✓ Branch 2 taken 1272 times.
8244 if (filtergraph_is_simple(fg)) {
3351 6972 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
3352 13944 snprintf(name, sizeof(name), "%cf%s",
3353 6972 av_get_media_type_string(ofp->ofilter.type)[0],
3354 ofp->ofilter.output_name);
3355 } else {
3356 1272 snprintf(name, sizeof(name), "fc%d", fg->index);
3357 }
3358
3359 8244 ff_thread_setname(name);
3360 8244 }
3361
3362 8244 static void fg_thread_uninit(FilterGraphThread *fgt)
3363 {
3364
1/2
✓ Branch 0 taken 8244 times.
✗ Branch 1 not taken.
8244 if (fgt->frame_queue_out) {
3365 AVFrame *frame;
3366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8244 times.
8244 while (av_fifo_read(fgt->frame_queue_out, &frame, 1) >= 0)
3367 av_frame_free(&frame);
3368 8244 av_fifo_freep2(&fgt->frame_queue_out);
3369 }
3370
3371 8244 av_frame_free(&fgt->frame);
3372 8244 av_freep(&fgt->eof_in);
3373 8244 av_freep(&fgt->eof_out);
3374
3375 8244 avfilter_graph_free(&fgt->graph);
3376
3377 8244 memset(fgt, 0, sizeof(*fgt));
3378 8244 }
3379
3380 8244 static int fg_thread_init(FilterGraphThread *fgt, const FilterGraph *fg)
3381 {
3382 8244 memset(fgt, 0, sizeof(*fgt));
3383
3384 8244 fgt->frame = av_frame_alloc();
3385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8244 times.
8244 if (!fgt->frame)
3386 goto fail;
3387
3388 8244 fgt->eof_in = av_calloc(fg->nb_inputs, sizeof(*fgt->eof_in));
3389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8244 times.
8244 if (!fgt->eof_in)
3390 goto fail;
3391
3392 8244 fgt->eof_out = av_calloc(fg->nb_outputs, sizeof(*fgt->eof_out));
3393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8244 times.
8244 if (!fgt->eof_out)
3394 goto fail;
3395
3396 8244 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 8244 times.
8244 if (!fgt->frame_queue_out)
3398 goto fail;
3399
3400 8244 return 0;
3401
3402 fail:
3403 fg_thread_uninit(fgt);
3404 return AVERROR(ENOMEM);
3405 }
3406
3407 424197 static int check_reinit(FilterGraph *fg, FilterGraphThread *fgt)
3408 {
3409 424197 int ret, reinit = 0;
3410
3411
2/2
✓ Branch 0 taken 434636 times.
✓ Branch 1 taken 424197 times.
858833 for (unsigned i = 0; i < fg->nb_outputs; i++) {
3412 434636 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3413
3414
2/6
✓ Branch 0 taken 434636 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 434636 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
434636 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 434636 times.
434636 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 424197 return reinit;
3441 }
3442
3443 8244 static int filter_thread(void *arg)
3444 {
3445 8244 FilterGraphPriv *fgp = arg;
3446 8244 FilterGraph *fg = &fgp->fg;
3447
3448 FilterGraphThread fgt;
3449 8244 int ret = 0, input_status = 0;
3450
3451 8244 ret = fg_thread_init(&fgt, fg);
3452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8244 times.
8244 if (ret < 0)
3453 goto finish;
3454
3455 8244 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 7044 times.
✓ Branch 2 taken 1200 times.
8244 if (ifilter_has_all_input_formats(fg)) {
3459 1200 ret = configure_filtergraph(fg, &fgt);
3460
1/2
✓ Branch 0 taken 1200 times.
✗ Branch 1 not taken.
1200 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 452952 while (1) {
3468 InputFilter *ifilter;
3469 461196 InputFilterPriv *ifp = NULL;
3470 enum FrameOpaque o;
3471 461196 unsigned input_idx = fgt.next_in;
3472
3473 461196 input_status = sch_filter_receive(fgp->sch, fgp->sch_idx,
3474 461196 &input_idx, fgt.frame);
3475
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 461185 times.
461196 if (input_status == AVERROR_EOF) {
3476 11 av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n");
3477 11 break;
3478
2/2
✓ Branch 0 taken 32480 times.
✓ Branch 1 taken 428705 times.
461185 } 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 32480 times.
32480 av_assert0(input_idx == fg->nb_inputs);
3481 32480 goto read_frames;
3482 }
3483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428705 times.
428705 av_assert0(input_status >= 0);
3484
3485 428705 o = (intptr_t)fgt.frame->opaque;
3486
3487 // message on the control stream
3488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428705 times.
428705 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 428705 ifilter = fg->inputs[input_idx];
3502 428705 ifp = ifp_from_ifilter(ifilter);
3503
3504
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 427665 times.
428705 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 424197 times.
✓ Branch 1 taken 3468 times.
427665 } else if (fgt.frame->buf[0]) {
3509 424197 ret = check_reinit(fg, &fgt);
3510
1/2
✓ Branch 0 taken 424197 times.
✗ Branch 1 not taken.
424197 if (ret >= 0)
3511 424197 ret = send_frame(fg, &fgt, ifilter, fgt.frame, ret);
3512 } else {
3513 av_assert1(o == FRAME_OPAQUE_EOF);
3514 3468 ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base);
3515 }
3516 428705 av_frame_unref(fgt.frame);
3517
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 428704 times.
428705 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 428703 times.
428704 if (ret < 0)
3524 1 goto finish;
3525
3526 428703 read_frames:
3527 // retrieve all newly available frames
3528 461183 ret = read_frames(fg, &fgt, fgt.frame);
3529
2/2
✓ Branch 0 taken 8232 times.
✓ Branch 1 taken 452951 times.
461183 if (ret == AVERROR_EOF) {
3530 8232 av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n");
3531
3/4
✓ Branch 0 taken 6724 times.
✓ Branch 1 taken 1508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6724 times.
8232 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 8232 break;
3534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 452951 times.
452951 } 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 890819 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 438064 times.
✓ Branch 3 taken 452755 times.
891015 for (int i = 0; fgt.graph && i < fg->nb_inputs; i++) {
3542 438064 InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
3543
2/2
✓ Branch 1 taken 1774 times.
✓ Branch 2 taken 436290 times.
438064 if (av_buffersrc_get_status(ifp->ifilter.filter))
3544 1774 close_input(ifp);
3545 }
3546 }
3547
3548
2/2
✓ Branch 0 taken 8378 times.
✓ Branch 1 taken 8243 times.
16621 for (unsigned i = 0; i < fg->nb_outputs; i++) {
3549 8378 OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
3550
3551
3/4
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 6770 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1608 times.
8378 if (fgt.eof_out[i] || !fgt.graph)
3552 6770 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 8243 finish:
3560
3561
2/4
✓ Branch 0 taken 8244 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8244 times.
8244 if (print_graphs || print_graphs_file)
3562 print_filtergraph(fg, fgt.graph);
3563
3564 // EOF is normal termination
3565
2/2
✓ Branch 0 taken 6698 times.
✓ Branch 1 taken 1546 times.
8244 if (ret == AVERROR_EOF)
3566 6698 ret = 0;
3567
3568 8244 fg_thread_uninit(&fgt);
3569
3570 8244 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