../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
1486 / 1985
74.9%
Branches:
890 / 1365
65.2%
Functions:
73 / 78
93.6%

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