FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_filter.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 466 616 75.6%
Branches: 297 474 62.7%

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
25 #include "libavfilter/avfilter.h"
26 #include "libavfilter/buffersink.h"
27 #include "libavfilter/buffersrc.h"
28
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/display.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/pixfmt.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/samplefmt.h"
39
40 // FIXME: YUV420P etc. are actually supported with full color range,
41 // yet the latter information isn't available here.
42 512 static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
43 {
44 static const enum AVPixelFormat mjpeg_formats[] =
45 { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
46 AV_PIX_FMT_NONE };
47
48
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 486 times.
512 if (!strcmp(codec->name, "mjpeg")) {
49 26 return mjpeg_formats;
50 } else {
51 486 return default_formats;
52 }
53 }
54
55 3504 static enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx,
56 const AVCodec *codec, enum AVPixelFormat target)
57 {
58
3/4
✓ Branch 0 taken 3504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 273 times.
✓ Branch 3 taken 3231 times.
3504 if (codec && codec->pix_fmts) {
59 273 const enum AVPixelFormat *p = codec->pix_fmts;
60 273 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
61 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
62
3/4
✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 259 times.
273 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
63 273 enum AVPixelFormat best= AV_PIX_FMT_NONE;
64
65
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 45 times.
273 if (enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
66 228 p = get_compliance_normal_pix_fmts(codec, p);
67 }
68
2/2
✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 9 times.
1371 for (; *p != AV_PIX_FMT_NONE; p++) {
69 1362 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
70
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 1098 times.
1362 if (*p == target)
71 264 break;
72 }
73
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 264 times.
273 if (*p == AV_PIX_FMT_NONE) {
74
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (target != AV_PIX_FMT_NONE)
75 18 av_log(NULL, AV_LOG_WARNING,
76 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
77 av_get_pix_fmt_name(target),
78 9 codec->name,
79 av_get_pix_fmt_name(best));
80 9 return best;
81 }
82 }
83 3495 return target;
84 }
85
86 /* May return NULL (no pixel format found), a static string or a string
87 * backed by the bprint. Nothing has been written to the AVBPrint in case
88 * NULL is returned. The AVBPrint provided should be clean. */
89 4836 static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
90 {
91 4836 OutputStream *ost = ofilter->ost;
92 4836 const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
93
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4772 times.
4836 if (strict_dict)
94 // used by choose_pixel_fmt() and below
95 64 av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
96
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4836 times.
4836 if (ost->keep_pix_fmt) {
98 avfilter_graph_set_auto_convert(ofilter->graph->graph,
99 AVFILTER_AUTO_CONVERT_NONE);
100 if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
101 return NULL;
102 return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt);
103 }
104
2/2
✓ Branch 0 taken 3504 times.
✓ Branch 1 taken 1332 times.
4836 if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
105 3504 return av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc_ctx, ost->enc, ost->enc_ctx->pix_fmt));
106
3/4
✓ Branch 0 taken 1332 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 303 times.
✓ Branch 3 taken 1029 times.
1332 } else if (ost->enc && ost->enc->pix_fmts) {
107 const enum AVPixelFormat *p;
108
109 303 p = ost->enc->pix_fmts;
110
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 19 times.
303 if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
111 284 p = get_compliance_normal_pix_fmts(ost->enc, p);
112 }
113
114
2/2
✓ Branch 0 taken 1321 times.
✓ Branch 1 taken 303 times.
1624 for (; *p != AV_PIX_FMT_NONE; p++) {
115 1321 const char *name = av_get_pix_fmt_name(*p);
116
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 1018 times.
1321 av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
117 }
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 303 times.
303 if (!av_bprint_is_complete(bprint))
119 exit_program(1);
120 303 return bprint->str;
121 } else
122 1029 return NULL;
123 }
124
125 /* Define a function for appending a list of allowed formats
126 * to an AVBPrint. If nonempty, the list will have a header. */
127 #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
128 static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
129 { \
130 if (ofilter->var == none && !ofilter->supported_list) \
131 return; \
132 av_bprintf(bprint, #name "="); \
133 if (ofilter->var != none) { \
134 av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
135 } else { \
136 const type *p; \
137 \
138 for (p = ofilter->supported_list; *p != none; p++) { \
139 av_bprintf(bprint, printf_format "|", get_name(*p)); \
140 } \
141 if (bprint->len > 0) \
142 bprint->str[--bprint->len] = '\0'; \
143 } \
144 av_bprint_chars(bprint, ':', 1); \
145 }
146
147 //DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
148 // GET_PIX_FMT_NAME)
149
150
8/10
✓ Branch 0 taken 1139 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1139 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1139 times.
✓ Branch 11 taken 1177 times.
✓ Branch 12 taken 1139 times.
✓ Branch 13 taken 1139 times.
✗ Branch 14 not taken.
2317 DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
151 AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
152
153
9/10
✓ Branch 0 taken 1114 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 1076 times.
✓ Branch 3 taken 38 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 38 times.
✓ Branch 9 taken 279 times.
✓ Branch 10 taken 38 times.
✓ Branch 11 taken 38 times.
✗ Branch 12 not taken.
1419 DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
154 "%d", )
155
156
9/10
✓ Branch 0 taken 1127 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1082 times.
✓ Branch 3 taken 45 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 45 times.
✓ Branch 9 taken 292 times.
✓ Branch 10 taken 45 times.
✓ Branch 11 taken 45 times.
✗ Branch 12 not taken.
1432 DEF_CHOOSE_FORMAT(channel_layouts, uint64_t, channel_layout, channel_layouts, 0,
157 "0x%"PRIx64, )
158
159 5850 int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
160 {
161 5850 FilterGraph *fg = av_mallocz(sizeof(*fg));
162 OutputFilter *ofilter;
163 InputFilter *ifilter;
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5850 times.
5850 if (!fg)
166 exit_program(1);
167 5850 fg->index = nb_filtergraphs;
168
169 5850 ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
170 5850 ofilter->ost = ost;
171 5850 ofilter->graph = fg;
172 5850 ofilter->format = -1;
173
174 5850 ost->filter = ofilter;
175
176 5850 ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
177 5850 ifilter->ist = ist;
178 5850 ifilter->graph = fg;
179 5850 ifilter->format = -1;
180
181 5850 ifilter->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5850 times.
5850 if (!ifilter->frame_queue)
183 exit_program(1);
184
185 5850 GROW_ARRAY(ist->filters, ist->nb_filters);
186 5850 ist->filters[ist->nb_filters - 1] = ifilter;
187
188 5850 GROW_ARRAY(filtergraphs, nb_filtergraphs);
189 5850 filtergraphs[nb_filtergraphs - 1] = fg;
190
191 5850 return 0;
192 }
193
194 155 static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
195 {
196 155 AVFilterContext *ctx = inout->filter_ctx;
197
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 87 times.
155 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
198
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 87 times.
155 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
199 char *res;
200
201
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 102 times.
155 if (nb_pads > 1)
202 53 res = av_strdup(ctx->filter->name);
203 else
204 102 res = av_asprintf("%s:%s", ctx->filter->name,
205 avfilter_pad_get_name(pads, inout->pad_idx));
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (!res)
207 exit_program(1);
208 155 return res;
209 }
210
211 68 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
212 {
213 68 InputStream *ist = NULL;
214 68 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
215 InputFilter *ifilter;
216 int i;
217
218 // TODO: support other filter types
219
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
68 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
220 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
221 "currently.\n");
222 exit_program(1);
223 }
224
225
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 50 times.
68 if (in->name) {
226 AVFormatContext *s;
227 18 AVStream *st = NULL;
228 char *p;
229 18 int file_idx = strtol(in->name, &p, 0);
230
231
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (file_idx < 0 || file_idx >= nb_input_files) {
232 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
233 file_idx, fg->graph_desc);
234 exit_program(1);
235 }
236 18 s = input_files[file_idx]->ctx;
237
238
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 for (i = 0; i < s->nb_streams; i++) {
239 23 enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
240
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
23 if (stream_type != type &&
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
242 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
243 3 continue;
244
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 2 times.
20 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
245 18 st = s->streams[i];
246 18 break;
247 }
248 }
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!st) {
250 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
251 "matches no streams.\n", p, fg->graph_desc);
252 exit_program(1);
253 }
254 18 ist = input_streams[input_files[file_idx]->ist_index + st->index];
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ist->user_set_discard == AVDISCARD_ALL) {
256 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
257 "matches a disabled input stream.\n", p, fg->graph_desc);
258 exit_program(1);
259 }
260 } else {
261 /* find the first unused stream of corresponding type */
262
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 for (i = 0; i < nb_input_streams; i++) {
263 69 ist = input_streams[i];
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (ist->user_set_discard == AVDISCARD_ALL)
265 continue;
266
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 18 times.
69 if (ist->dec_ctx->codec_type == type && ist->discard)
267 50 break;
268 }
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (i == nb_input_streams) {
270 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
271 "unlabeled input pad %d on filter %s\n", in->pad_idx,
272 in->filter_ctx->name);
273 exit_program(1);
274 }
275 }
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 av_assert0(ist);
277
278 68 ist->discard = 0;
279 68 ist->decoding_needed |= DECODING_FOR_FILTER;
280 68 ist->st->discard = AVDISCARD_NONE;
281
282 68 ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
283 68 ifilter->ist = ist;
284 68 ifilter->graph = fg;
285 68 ifilter->format = -1;
286 68 ifilter->type = ist->st->codecpar->codec_type;
287 68 ifilter->name = describe_filter_link(fg, in, 1);
288
289 68 ifilter->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!ifilter->frame_queue)
291 exit_program(1);
292
293 68 GROW_ARRAY(ist->filters, ist->nb_filters);
294 68 ist->filters[ist->nb_filters - 1] = ifilter;
295 68 }
296
297 84 int init_complex_filtergraph(FilterGraph *fg)
298 {
299 AVFilterInOut *inputs, *outputs, *cur;
300 AVFilterGraph *graph;
301 84 int ret = 0;
302
303 /* this graph is only used for determining the kinds of inputs
304 * and outputs we have, and is discarded on exit from this function */
305 84 graph = avfilter_graph_alloc();
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (!graph)
307 return AVERROR(ENOMEM);
308 84 graph->nb_threads = 1;
309
310 84 ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (ret < 0)
312 goto fail;
313
314
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 84 times.
152 for (cur = inputs; cur; cur = cur->next)
315 68 init_input_filter(fg, cur);
316
317
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 84 times.
171 for (cur = outputs; cur;) {
318 87 OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
319
320 87 ofilter->graph = fg;
321 87 ofilter->out_tmp = cur;
322 87 ofilter->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
323 cur->pad_idx);
324 87 ofilter->name = describe_filter_link(fg, cur, 0);
325 87 cur = cur->next;
326 87 ofilter->out_tmp->next = NULL;
327 }
328
329 84 fail:
330 84 avfilter_inout_free(&inputs);
331 84 avfilter_graph_free(&graph);
332 84 return ret;
333 }
334
335 11931 static int insert_trim(int64_t start_time, int64_t duration,
336 AVFilterContext **last_filter, int *pad_idx,
337 const char *filter_name)
338 {
339 11931 AVFilterGraph *graph = (*last_filter)->graph;
340 AVFilterContext *ctx;
341 const AVFilter *trim;
342 11931 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
343
2/2
✓ Branch 0 taken 9645 times.
✓ Branch 1 taken 2286 times.
11931 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
344 11931 int ret = 0;
345
346
4/4
✓ Branch 0 taken 11770 times.
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 11753 times.
✓ Branch 3 taken 17 times.
11931 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
347 11753 return 0;
348
349 178 trim = avfilter_get_by_name(name);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (!trim) {
351 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
352 "recording time.\n", name);
353 return AVERROR_FILTER_NOT_FOUND;
354 }
355
356 178 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (!ctx)
358 return AVERROR(ENOMEM);
359
360
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 17 times.
178 if (duration != INT64_MAX) {
361 161 ret = av_opt_set_int(ctx, "durationi", duration,
362 AV_OPT_SEARCH_CHILDREN);
363 }
364
3/4
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 161 times.
178 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
365 17 ret = av_opt_set_int(ctx, "starti", start_time,
366 AV_OPT_SEARCH_CHILDREN);
367 }
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (ret < 0) {
369 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
370 return ret;
371 }
372
373 178 ret = avfilter_init_str(ctx, NULL);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (ret < 0)
375 return ret;
376
377 178 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (ret < 0)
379 return ret;
380
381 178 *last_filter = ctx;
382 178 *pad_idx = 0;
383 178 return 0;
384 }
385
386 static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
387 const char *filter_name, const char *args)
388 {
389 AVFilterGraph *graph = (*last_filter)->graph;
390 AVFilterContext *ctx;
391 int ret;
392
393 ret = avfilter_graph_create_filter(&ctx,
394 avfilter_get_by_name(filter_name),
395 filter_name, args, NULL, graph);
396 if (ret < 0)
397 return ret;
398
399 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
400 if (ret < 0)
401 return ret;
402
403 *last_filter = ctx;
404 *pad_idx = 0;
405 return 0;
406 }
407
408 4836 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
409 {
410 4836 OutputStream *ost = ofilter->ost;
411 4836 OutputFile *of = output_files[ost->file_index];
412 4836 AVFilterContext *last_filter = out->filter_ctx;
413 AVBPrint bprint;
414 4836 int pad_idx = out->pad_idx;
415 int ret;
416 const char *pix_fmts;
417 char name[255];
418
419 4836 snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
420 4836 ret = avfilter_graph_create_filter(&ofilter->filter,
421 avfilter_get_by_name("buffersink"),
422 name, NULL, NULL, fg->graph);
423
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4836 times.
4836 if (ret < 0)
425 return ret;
426
427
4/6
✓ Branch 0 taken 4189 times.
✓ Branch 1 taken 647 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4189 times.
✓ Branch 4 taken 647 times.
✗ Branch 5 not taken.
4836 if ((ofilter->width || ofilter->height) && ofilter->ost->autoscale) {
428 char args[255];
429 AVFilterContext *filter;
430 647 const AVDictionaryEntry *e = NULL;
431
432 647 snprintf(args, sizeof(args), "%d:%d",
433 ofilter->width, ofilter->height);
434
435
2/2
✓ Branch 1 taken 636 times.
✓ Branch 2 taken 647 times.
1283 while ((e = av_dict_get(ost->sws_dict, "", e,
436 AV_DICT_IGNORE_SUFFIX))) {
437 636 av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
438 }
439
440 647 snprintf(name, sizeof(name), "scaler_out_%d_%d",
441 ost->file_index, ost->index);
442
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 647 times.
647 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
443 name, args, NULL, fg->graph)) < 0)
444 return ret;
445
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 647 times.
647 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
446 return ret;
447
448 647 last_filter = filter;
449 647 pad_idx = 0;
450 }
451
452 4836 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
453
2/2
✓ Branch 1 taken 3807 times.
✓ Branch 2 taken 1029 times.
4836 if ((pix_fmts = choose_pix_fmts(ofilter, &bprint))) {
454 AVFilterContext *filter;
455
456 3807 ret = avfilter_graph_create_filter(&filter,
457 avfilter_get_by_name("format"),
458 "format", pix_fmts, NULL, fg->graph);
459 3807 av_bprint_finalize(&bprint, NULL);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3807 times.
3807 if (ret < 0)
461 return ret;
462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3807 times.
3807 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
463 return ret;
464
465 3807 last_filter = filter;
466 3807 pad_idx = 0;
467 }
468
469 if (ost->frame_rate.num && 0) {
470 AVFilterContext *fps;
471 char args[255];
472
473 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
474 ost->frame_rate.den);
475 snprintf(name, sizeof(name), "fps_out_%d_%d",
476 ost->file_index, ost->index);
477 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
478 name, args, NULL, fg->graph);
479 if (ret < 0)
480 return ret;
481
482 ret = avfilter_link(last_filter, pad_idx, fps, 0);
483 if (ret < 0)
484 return ret;
485 last_filter = fps;
486 pad_idx = 0;
487 }
488
489 4836 snprintf(name, sizeof(name), "trim_out_%d_%d",
490 ost->file_index, ost->index);
491 4836 ret = insert_trim(of->start_time, of->recording_time,
492 &last_filter, &pad_idx, name);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4836 times.
4836 if (ret < 0)
494 return ret;
495
496
497
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4836 times.
4836 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
498 return ret;
499
500 4836 return 0;
501 }
502
503 1140 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
504 {
505 1140 OutputStream *ost = ofilter->ost;
506 1140 OutputFile *of = output_files[ost->file_index];
507 1140 AVCodecContext *codec = ost->enc_ctx;
508 1140 AVFilterContext *last_filter = out->filter_ctx;
509 1140 int pad_idx = out->pad_idx;
510 AVBPrint args;
511 char name[255];
512 int ret;
513
514 1140 snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
515 1140 ret = avfilter_graph_create_filter(&ofilter->filter,
516 avfilter_get_by_name("abuffersink"),
517 name, NULL, NULL, fg->graph);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140 times.
1140 if (ret < 0)
519 return ret;
520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1140 times.
1140 if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
521 return ret;
522
523 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
524 AVFilterContext *filt_ctx; \
525 \
526 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
527 "similarly to -af " filter_name "=%s.\n", arg); \
528 \
529 ret = avfilter_graph_create_filter(&filt_ctx, \
530 avfilter_get_by_name(filter_name), \
531 filter_name, arg, NULL, fg->graph); \
532 if (ret < 0) \
533 goto fail; \
534 \
535 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
536 if (ret < 0) \
537 goto fail; \
538 \
539 last_filter = filt_ctx; \
540 pad_idx = 0; \
541 } while (0)
542 1140 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
543
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1134 times.
1140 if (ost->audio_channels_mapped) {
544 int i;
545 6 av_bprintf(&args, "0x%"PRIx64,
546 av_get_default_channel_layout(ost->audio_channels_mapped));
547
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 for (i = 0; i < ost->audio_channels_mapped; i++)
548
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 if (ost->audio_channels_map[i] != -1)
549 9 av_bprintf(&args, "|c%d=c%d", i, ost->audio_channels_map[i]);
550
551
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
6 AUTO_INSERT_FILTER("-map_channel", "pan", args.str);
552 6 av_bprint_clear(&args);
553 }
554
555
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1127 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1 times.
1140 if (codec->channels && !codec->channel_layout)
556 12 codec->channel_layout = av_get_default_channel_layout(codec->channels);
557
558 1140 choose_sample_fmts(ofilter, &args);
559 1140 choose_sample_rates(ofilter, &args);
560 1140 choose_channel_layouts(ofilter, &args);
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1140 times.
1140 if (!av_bprint_is_complete(&args)) {
562 ret = AVERROR(ENOMEM);
563 goto fail;
564 }
565
1/2
✓ Branch 0 taken 1140 times.
✗ Branch 1 not taken.
1140 if (args.len) {
566 AVFilterContext *format;
567
568 1140 snprintf(name, sizeof(name), "format_out_%d_%d",
569 ost->file_index, ost->index);
570 1140 ret = avfilter_graph_create_filter(&format,
571 avfilter_get_by_name("aformat"),
572 1140 name, args.str, NULL, fg->graph);
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140 times.
1140 if (ret < 0)
574 goto fail;
575
576 1140 ret = avfilter_link(last_filter, pad_idx, format, 0);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140 times.
1140 if (ret < 0)
578 goto fail;
579
580 1140 last_filter = format;
581 1140 pad_idx = 0;
582 }
583
584
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1140 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1140 if (ost->apad && of->shortest) {
585 int i;
586
587 for (i=0; i<of->ctx->nb_streams; i++)
588 if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
589 break;
590
591 if (i<of->ctx->nb_streams) {
592 AUTO_INSERT_FILTER("-apad", "apad", ost->apad);
593 }
594 }
595
596 1140 snprintf(name, sizeof(name), "trim for output stream %d:%d",
597 ost->file_index, ost->index);
598 1140 ret = insert_trim(of->start_time, of->recording_time,
599 &last_filter, &pad_idx, name);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140 times.
1140 if (ret < 0)
601 goto fail;
602
603
1/2
✓ Branch 1 taken 1140 times.
✗ Branch 2 not taken.
1140 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
604 goto fail;
605 1140 fail:
606 1140 av_bprint_finalize(&args, NULL);
607
608 1140 return ret;
609 }
610
611 5976 static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter,
612 AVFilterInOut *out)
613 {
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5976 times.
5976 if (!ofilter->ost) {
615 av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
616 exit_program(1);
617 }
618
619
2/3
✓ Branch 1 taken 4836 times.
✓ Branch 2 taken 1140 times.
✗ Branch 3 not taken.
5976 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
620 4836 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
621 1140 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
622 default: av_assert0(0); return 0;
623 }
624 }
625
626 6161 void check_filter_outputs(void)
627 {
628 int i;
629
2/2
✓ Branch 0 taken 5934 times.
✓ Branch 1 taken 6161 times.
12095 for (i = 0; i < nb_filtergraphs; i++) {
630 int n;
631
2/2
✓ Branch 0 taken 5937 times.
✓ Branch 1 taken 5934 times.
11871 for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
632 5937 OutputFilter *output = filtergraphs[i]->outputs[n];
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5937 times.
5937 if (!output->ost) {
634 av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
635 exit_program(1);
636 }
637 }
638 }
639 6161 }
640
641 4 static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
642 {
643 4 AVFormatContext *avf = input_files[ist->file_index]->ctx;
644 int i, w, h;
645
646 /* Compute the size of the canvas for the subtitles stream.
647 If the subtitles codecpar has set a size, use it. Otherwise use the
648 maximum dimensions of the video streams in the same file. */
649 4 w = ifilter->width;
650 4 h = ifilter->height;
651
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (!(w && h)) {
652
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < avf->nb_streams; i++) {
653
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
654 1 w = FFMAX(w, avf->streams[i]->codecpar->width);
655 1 h = FFMAX(h, avf->streams[i]->codecpar->height);
656 }
657 }
658
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 if (!(w && h)) {
659 3 w = FFMAX(w, 720);
660 3 h = FFMAX(h, 576);
661 }
662 4 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
663 }
664 4 ist->sub2video.w = ifilter->width = w;
665 4 ist->sub2video.h = ifilter->height = h;
666
667
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 ifilter->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
668
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 ifilter->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
669
670 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
671 palettes for all rectangles are identical or compatible */
672 4 ifilter->format = AV_PIX_FMT_RGB32;
673
674 4 ist->sub2video.frame = av_frame_alloc();
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ist->sub2video.frame)
676 return AVERROR(ENOMEM);
677 4 ist->sub2video.last_pts = INT64_MIN;
678 4 ist->sub2video.end_pts = INT64_MIN;
679
680 /* sub2video structure has been (re-)initialized.
681 Mark it as such so that the system will be
682 initialized with the first received heartbeat. */
683 4 ist->sub2video.initialize = 1;
684
685 4 return 0;
686 }
687
688 4809 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
689 AVFilterInOut *in)
690 {
691 AVFilterContext *last_filter;
692 4809 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
693 const AVPixFmtDescriptor *desc;
694 4809 InputStream *ist = ifilter->ist;
695 4809 InputFile *f = input_files[ist->file_index];
696
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4785 times.
4809 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
697 4785 ist->st->time_base;
698 4809 AVRational fr = ist->framerate;
699 AVRational sar;
700 AVBPrint args;
701 char name[255];
702 4809 int ret, pad_idx = 0;
703 4809 int64_t tsoffset = 0;
704 4809 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
705
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (!par)
707 return AVERROR(ENOMEM);
708 4809 memset(par, 0, sizeof(*par));
709 4809 par->format = AV_PIX_FMT_NONE;
710
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
712 av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
713 ret = AVERROR(EINVAL);
714 goto fail;
715 }
716
717
2/2
✓ Branch 0 taken 4785 times.
✓ Branch 1 taken 24 times.
4809 if (!fr.num)
718 4785 fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
719
720
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4805 times.
4809 if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
721 4 ret = sub2video_prepare(ist, ifilter);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
723 goto fail;
724 }
725
726 4809 sar = ifilter->sample_aspect_ratio;
727
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4805 times.
4809 if(!sar.den)
728 4 sar = (AVRational){0,1};
729 4809 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
730 4809 av_bprintf(&args,
731 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
732 "pixel_aspect=%d/%d",
733 ifilter->width, ifilter->height, ifilter->format,
734 tb.num, tb.den, sar.num, sar.den);
735
3/4
✓ Branch 0 taken 4805 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4805 times.
✗ Branch 3 not taken.
4809 if (fr.num && fr.den)
736 4805 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
737 4809 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
738 4809 ist->file_index, ist->st->index);
739
740
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
742 4809 args.str, NULL, fg->graph)) < 0)
743 goto fail;
744 4809 par->hw_frames_ctx = ifilter->hw_frames_ctx;
745 4809 ret = av_buffersrc_parameters_set(ifilter->filter, par);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (ret < 0)
747 goto fail;
748 4809 av_freep(&par);
749 4809 last_filter = ifilter->filter;
750
751 4809 desc = av_pix_fmt_desc_get(ifilter->format);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 av_assert0(desc);
753
754 // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
755
2/4
✓ Branch 0 taken 4809 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4809 times.
✗ Branch 3 not taken.
4809 if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
756 4809 int32_t *displaymatrix = ifilter->displaymatrix;
757 double theta;
758
759
1/2
✓ Branch 0 taken 4809 times.
✗ Branch 1 not taken.
4809 if (!displaymatrix)
760 4809 displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
761 4809 theta = get_rotation(displaymatrix);
762
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (fabs(theta - 90) < 1.0) {
764 ret = insert_filter(&last_filter, &pad_idx, "transpose",
765 displaymatrix[3] > 0 ? "cclock_flip" : "clock");
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 } else if (fabs(theta - 180) < 1.0) {
767 if (displaymatrix[0] < 0) {
768 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
769 if (ret < 0)
770 return ret;
771 }
772 if (displaymatrix[4] < 0) {
773 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
774 }
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 } else if (fabs(theta - 270) < 1.0) {
776 ret = insert_filter(&last_filter, &pad_idx, "transpose",
777 displaymatrix[3] < 0 ? "clock_flip" : "cclock");
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 } else if (fabs(theta) > 1.0) {
779 char rotate_buf[64];
780 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
781 ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
782
1/2
✓ Branch 0 taken 4809 times.
✗ Branch 1 not taken.
4809 } else if (fabs(theta) < 1.0) {
783
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4809 if (displaymatrix && displaymatrix[4] < 0) {
784 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
785 }
786 }
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (ret < 0)
788 return ret;
789 }
790
791 4809 snprintf(name, sizeof(name), "trim_in_%d_%d",
792 4809 ist->file_index, ist->st->index);
793
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4807 times.
4809 if (copy_ts) {
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
795
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
796 1 tsoffset += f->ctx->start_time;
797 }
798
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4808 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4809 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
799 AV_NOPTS_VALUE : tsoffset, f->recording_time,
800 &last_filter, &pad_idx, name);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4809 times.
4809 if (ret < 0)
802 return ret;
803
804
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4809 times.
4809 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
805 return ret;
806 4809 return 0;
807 fail:
808 av_freep(&par);
809
810 return ret;
811 }
812
813 1146 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
814 AVFilterInOut *in)
815 {
816 AVFilterContext *last_filter;
817 1146 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
818 1146 InputStream *ist = ifilter->ist;
819 1146 InputFile *f = input_files[ist->file_index];
820 AVBPrint args;
821 char name[255];
822 1146 int ret, pad_idx = 0;
823 1146 int64_t tsoffset = 0;
824
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
826 av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
827 return AVERROR(EINVAL);
828 }
829
830 1146 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
831 1146 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
832 1, ifilter->sample_rate,
833 ifilter->sample_rate,
834 1146 av_get_sample_fmt_name(ifilter->format));
835
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 6 times.
1146 if (ifilter->channel_layout)
836 1140 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
837 ifilter->channel_layout);
838 else
839 6 av_bprintf(&args, ":channels=%d", ifilter->channels);
840 1146 snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
841 1146 ist->file_index, ist->st->index);
842
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
844 1146 name, args.str, NULL,
845 fg->graph)) < 0)
846 return ret;
847 1146 last_filter = ifilter->filter;
848
849 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
850 AVFilterContext *filt_ctx; \
851 \
852 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
853 "similarly to -af " filter_name "=%s.\n", arg); \
854 \
855 snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
856 fg->index, filter_name, ist->file_index, ist->st->index); \
857 ret = avfilter_graph_create_filter(&filt_ctx, \
858 avfilter_get_by_name(filter_name), \
859 name, arg, NULL, fg->graph); \
860 if (ret < 0) \
861 return ret; \
862 \
863 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
864 if (ret < 0) \
865 return ret; \
866 \
867 last_filter = filt_ctx; \
868 } while (0)
869
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if (audio_sync_method > 0) {
871 char args[256] = {0};
872
873 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
874 if (audio_drift_threshold != 0.1)
875 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
876 if (!fg->reconfiguration)
877 av_strlcatf(args, sizeof(args), ":first_pts=0");
878 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
879 }
880
881 // if (ost->audio_channels_mapped) {
882 // int i;
883 // AVBPrint pan_buf;
884 // av_bprint_init(&pan_buf, 256, 8192);
885 // av_bprintf(&pan_buf, "0x%"PRIx64,
886 // av_get_default_channel_layout(ost->audio_channels_mapped));
887 // for (i = 0; i < ost->audio_channels_mapped; i++)
888 // if (ost->audio_channels_map[i] != -1)
889 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
890 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
891 // av_bprint_finalize(&pan_buf, NULL);
892 // }
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if (audio_volume != 256) {
895 char args[256];
896
897 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
898 "audio filter instead.\n");
899
900 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
901 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
902 }
903
904 1146 snprintf(name, sizeof(name), "trim for input stream %d:%d",
905 1146 ist->file_index, ist->st->index);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if (copy_ts) {
907 tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
908 if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
909 tsoffset += f->ctx->start_time;
910 }
911
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1131 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
1146 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
912 AV_NOPTS_VALUE : tsoffset, f->recording_time,
913 &last_filter, &pad_idx, name);
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146 times.
1146 if (ret < 0)
915 return ret;
916
917
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1146 times.
1146 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
918 return ret;
919
920 1146 return 0;
921 }
922
923 5955 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
924 AVFilterInOut *in)
925 {
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5955 times.
5955 if (!ifilter->ist->dec) {
927 av_log(NULL, AV_LOG_ERROR,
928 "No decoder for stream #%d:%d, filtering impossible\n",
929 ifilter->ist->file_index, ifilter->ist->st->index);
930 return AVERROR_DECODER_NOT_FOUND;
931 }
932
2/3
✓ Branch 1 taken 4809 times.
✓ Branch 2 taken 1146 times.
✗ Branch 3 not taken.
5955 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
933 4809 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
934 1146 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
935 default: av_assert0(0); return 0;
936 }
937 }
938
939 5977 static void cleanup_filtergraph(FilterGraph *fg)
940 {
941 int i;
942
2/2
✓ Branch 0 taken 5980 times.
✓ Branch 1 taken 5977 times.
11957 for (i = 0; i < fg->nb_outputs; i++)
943 5980 fg->outputs[i]->filter = (AVFilterContext *)NULL;
944
2/2
✓ Branch 0 taken 5955 times.
✓ Branch 1 taken 5977 times.
11932 for (i = 0; i < fg->nb_inputs; i++)
945 5955 fg->inputs[i]->filter = (AVFilterContext *)NULL;
946 5977 avfilter_graph_free(&fg->graph);
947 5977 }
948
949 5969 static int filter_is_buffersrc(const AVFilterContext *f)
950 {
951
2/2
✓ Branch 0 taken 2753 times.
✓ Branch 1 taken 3216 times.
8722 return f->nb_inputs == 0 &&
952
1/2
✓ Branch 0 taken 2753 times.
✗ Branch 1 not taken.
2753 (!strcmp(f->filter->name, "buffersrc") ||
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2753 times.
2753 !strcmp(f->filter->name, "abuffersrc"));
954 }
955
956 5969 static int graph_is_meta(AVFilterGraph *graph)
957 {
958
1/2
✓ Branch 0 taken 9216 times.
✗ Branch 1 not taken.
9216 for (unsigned i = 0; i < graph->nb_filters; i++) {
959 9216 const AVFilterContext *f = graph->filters[i];
960
961 /* in addition to filters flagged as meta, also
962 * disregard sinks and buffersources (but not other sources,
963 * since they introduce data we are not aware of)
964 */
965
3/4
✓ Branch 0 taken 5969 times.
✓ Branch 1 taken 3247 times.
✓ Branch 2 taken 5969 times.
✗ Branch 3 not taken.
15185 if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
966
1/2
✓ Branch 0 taken 5969 times.
✗ Branch 1 not taken.
5969 f->nb_outputs == 0 ||
967 5969 filter_is_buffersrc(f)))
968 5969 return 0;
969 }
970 return 1;
971 }
972
973 5973 int configure_filtergraph(FilterGraph *fg)
974 {
975 AVFilterInOut *inputs, *outputs, *cur;
976 5973 int ret, i, simple = filtergraph_is_simple(fg);
977
2/2
✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 86 times.
5973 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
978 fg->graph_desc;
979
980 5973 cleanup_filtergraph(fg);
981
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5973 times.
5973 if (!(fg->graph = avfilter_graph_alloc()))
982 return AVERROR(ENOMEM);
983
984
2/2
✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 86 times.
5973 if (simple) {
985 5887 OutputStream *ost = fg->outputs[0]->ost;
986 char args[512];
987 5887 const AVDictionaryEntry *e = NULL;
988
989
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 5578 times.
5887 if (filter_nbthreads) {
990 309 ret = av_opt_set(fg->graph, "threads", filter_nbthreads, 0);
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 309 times.
309 if (ret < 0)
992 goto fail;
993 } else {
994 5578 e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
995
2/2
✓ Branch 0 taken 3279 times.
✓ Branch 1 taken 2299 times.
5578 if (e)
996 3279 av_opt_set(fg->graph, "threads", e->value, 0);
997 }
998
999 5887 args[0] = 0;
1000 5887 e = NULL;
1001
2/2
✓ Branch 1 taken 3593 times.
✓ Branch 2 taken 5887 times.
9480 while ((e = av_dict_get(ost->sws_dict, "", e,
1002 AV_DICT_IGNORE_SUFFIX))) {
1003 3593 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1004 }
1005
2/2
✓ Branch 0 taken 3593 times.
✓ Branch 1 taken 2294 times.
5887 if (strlen(args)) {
1006 3593 args[strlen(args)-1] = 0;
1007 3593 fg->graph->scale_sws_opts = av_strdup(args);
1008 }
1009
1010 5887 args[0] = 0;
1011 5887 e = NULL;
1012
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 5887 times.
5941 while ((e = av_dict_get(ost->swr_opts, "", e,
1013 AV_DICT_IGNORE_SUFFIX))) {
1014 54 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1015 }
1016
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 5833 times.
5887 if (strlen(args))
1017 54 args[strlen(args)-1] = 0;
1018 5887 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
1019 } else {
1020 86 fg->graph->nb_threads = filter_complex_nbthreads;
1021 }
1022
1023
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5973 times.
5973 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
1024 goto fail;
1025
1026 5973 ret = hw_device_setup_for_filter(fg);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
5973 if (ret < 0)
1028 goto fail;
1029
1030
6/10
✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 5887 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5887 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5887 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 5887 times.
5973 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1031 const char *num_inputs;
1032 const char *num_outputs;
1033 if (!outputs) {
1034 num_outputs = "0";
1035 } else if (outputs->next) {
1036 num_outputs = ">1";
1037 } else {
1038 num_outputs = "1";
1039 }
1040 if (!inputs) {
1041 num_inputs = "0";
1042 } else if (inputs->next) {
1043 num_inputs = ">1";
1044 } else {
1045 num_inputs = "1";
1046 }
1047 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
1048 "to have exactly 1 input and 1 output."
1049 " However, it had %s input(s) and %s output(s)."
1050 " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1051 graph_desc, num_inputs, num_outputs);
1052 ret = AVERROR(EINVAL);
1053 goto fail;
1054 }
1055
1056
2/2
✓ Branch 0 taken 5955 times.
✓ Branch 1 taken 5973 times.
11928 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1057
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5955 times.
5955 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
1058 avfilter_inout_free(&inputs);
1059 avfilter_inout_free(&outputs);
1060 goto fail;
1061 }
1062 5973 avfilter_inout_free(&inputs);
1063
1064
2/2
✓ Branch 0 taken 5976 times.
✓ Branch 1 taken 5973 times.
11949 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1065 5976 configure_output_filter(fg, fg->outputs[i], cur);
1066 5973 avfilter_inout_free(&outputs);
1067
1068
2/2
✓ Branch 0 taken 4321 times.
✓ Branch 1 taken 1652 times.
5973 if (!auto_conversion_filters)
1069 4321 avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
1070
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5969 times.
5973 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
1071 4 goto fail;
1072
1073 5969 fg->is_meta = graph_is_meta(fg->graph);
1074
1075 /* limit the lists of allowed formats to the ones selected, to
1076 * make sure they stay the same if the filtergraph is reconfigured later */
1077
2/2
✓ Branch 0 taken 5972 times.
✓ Branch 1 taken 5969 times.
11941 for (i = 0; i < fg->nb_outputs; i++) {
1078 5972 OutputFilter *ofilter = fg->outputs[i];
1079 5972 AVFilterContext *sink = ofilter->filter;
1080
1081 5972 ofilter->format = av_buffersink_get_format(sink);
1082
1083 5972 ofilter->width = av_buffersink_get_w(sink);
1084 5972 ofilter->height = av_buffersink_get_h(sink);
1085
1086 5972 ofilter->sample_rate = av_buffersink_get_sample_rate(sink);
1087 5972 ofilter->channel_layout = av_buffersink_get_channel_layout(sink);
1088 }
1089
1090 5969 fg->reconfiguration = 1;
1091
1092
2/2
✓ Branch 0 taken 5972 times.
✓ Branch 1 taken 5969 times.
11941 for (i = 0; i < fg->nb_outputs; i++) {
1093 5972 OutputStream *ost = fg->outputs[i]->ost;
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5972 times.
5972 if (!ost->enc) {
1095 /* identical to the same check in ffmpeg.c, needed because
1096 complex filter graphs are initialized earlier */
1097 av_log(NULL, AV_LOG_ERROR, "Encoder (codec %s) not found for output stream #%d:%d\n",
1098 avcodec_get_name(ost->st->codecpar->codec_id), ost->file_index, ost->index);
1099 ret = AVERROR(EINVAL);
1100 goto fail;
1101 }
1102
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 4832 times.
5972 if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
1103
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 1034 times.
1140 !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1104 106 av_buffersink_set_frame_size(ost->filter->filter,
1105 106 ost->enc_ctx->frame_size);
1106 }
1107
1108
2/2
✓ Branch 0 taken 5955 times.
✓ Branch 1 taken 5969 times.
11924 for (i = 0; i < fg->nb_inputs; i++) {
1109
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 5955 times.
5978 while (av_fifo_size(fg->inputs[i]->frame_queue)) {
1110 AVFrame *tmp;
1111 23 av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
1112 23 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
1113 23 av_frame_free(&tmp);
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
1115 goto fail;
1116 }
1117 }
1118
1119 /* send the EOFs for the finished inputs */
1120
2/2
✓ Branch 0 taken 5955 times.
✓ Branch 1 taken 5969 times.
11924 for (i = 0; i < fg->nb_inputs; i++) {
1121
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5954 times.
5955 if (fg->inputs[i]->eof) {
1122 1 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
1123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1124 goto fail;
1125 }
1126 }
1127
1128 /* process queued up subtitle packets */
1129
2/2
✓ Branch 0 taken 5955 times.
✓ Branch 1 taken 5969 times.
11924 for (i = 0; i < fg->nb_inputs; i++) {
1130 5955 InputStream *ist = fg->inputs[i]->ist;
1131
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5955 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5955 if (ist->sub2video.sub_queue && ist->sub2video.frame) {
1132 while (av_fifo_size(ist->sub2video.sub_queue)) {
1133 AVSubtitle tmp;
1134 av_fifo_generic_read(ist->sub2video.sub_queue, &tmp, sizeof(tmp), NULL);
1135 sub2video_update(ist, INT64_MIN, &tmp);
1136 avsubtitle_free(&tmp);
1137 }
1138 }
1139 }
1140
1141 5969 return 0;
1142
1143 4 fail:
1144 4 cleanup_filtergraph(fg);
1145 4 return ret;
1146 }
1147
1148 5950 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
1149 {
1150 AVFrameSideData *sd;
1151
1152 5950 av_buffer_unref(&ifilter->hw_frames_ctx);
1153
1154 5950 ifilter->format = frame->format;
1155
1156 5950 ifilter->width = frame->width;
1157 5950 ifilter->height = frame->height;
1158 5950 ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
1159
1160 5950 ifilter->sample_rate = frame->sample_rate;
1161 5950 ifilter->channels = frame->channels;
1162 5950 ifilter->channel_layout = frame->channel_layout;
1163
1164 5950 av_freep(&ifilter->displaymatrix);
1165 5950 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5950 times.
5950 if (sd)
1167 ifilter->displaymatrix = av_memdup(sd->data, sizeof(int32_t) * 9);
1168
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5950 times.
5950 if (frame->hw_frames_ctx) {
1170 ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1171 if (!ifilter->hw_frames_ctx)
1172 return AVERROR(ENOMEM);
1173 }
1174
1175 5950 return 0;
1176 }
1177
1178 17828 int filtergraph_is_simple(FilterGraph *fg)
1179 {
1180 17828 return !fg->graph_desc;
1181 }
1182