FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux_init.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 1192 1841 64.7%
Functions: 39 46 84.8%
Branches: 985 2314 42.6%

Line Branch Exec Source
1 /*
2 * Muxer/output file setup.
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 <string.h>
22
23 #include "cmdutils.h"
24 #include "ffmpeg.h"
25 #include "ffmpeg_mux.h"
26 #include "ffmpeg_sched.h"
27 #include "fopen_utf8.h"
28
29 #include "libavformat/avformat.h"
30 #include "libavformat/avio.h"
31
32 #include "libavcodec/avcodec.h"
33
34 #include "libavfilter/avfilter.h"
35
36 #include "libavutil/avassert.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/avutil.h"
39 #include "libavutil/bprint.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/display.h"
42 #include "libavutil/getenv_utf8.h"
43 #include "libavutil/iamf.h"
44 #include "libavutil/intreadwrite.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/pixdesc.h"
50
51 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
52
53 9660 static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
54 const char *opt_name, int flag)
55 {
56 9660 const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0);
57
58
2/2
✓ Branch 0 taken 7811 times.
✓ Branch 1 taken 1849 times.
9660 if (e) {
59 7811 const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0);
60 7811 int val = 0;
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7811 times.
7811 if (!o)
62 return 0;
63 7811 av_opt_eval_flags(ctx, o, e->value, &val);
64 7811 return !!(val & flag);
65 }
66 1849 return 0;
67 }
68
69 7051 static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
70 OutputStream *ost, const AVCodec **enc)
71 {
72 7051 enum AVMediaType type = ost->type;
73 7051 char *codec_name = NULL;
74
75 7051 *enc = NULL;
76
77
16/20
✓ Branch 1 taken 4231 times.
✓ Branch 2 taken 193 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 193 times.
✓ Branch 5 taken 4424 times.
✓ Branch 6 taken 7051 times.
✓ Branch 7 taken 62 times.
✓ Branch 8 taken 6989 times.
✓ Branch 9 taken 62 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 62 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 61 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 62 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 310 times.
✓ Branch 19 taken 62 times.
✓ Branch 20 taken 61 times.
✓ Branch 21 taken 1 times.
11785 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
78
79
4/4
✓ Branch 0 taken 1542 times.
✓ Branch 1 taken 5509 times.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 1460 times.
7051 if (type != AVMEDIA_TYPE_VIDEO &&
80
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 71 times.
82 type != AVMEDIA_TYPE_AUDIO &&
81 type != AVMEDIA_TYPE_SUBTITLE) {
82
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
11 if (codec_name && strcmp(codec_name, "copy")) {
83 const char *type_str = av_get_media_type_string(type);
84 av_log(ost, AV_LOG_FATAL,
85 "Encoder '%s' specified, but only '-codec copy' supported "
86 "for %s streams\n", codec_name, type_str);
87 return AVERROR(ENOSYS);
88 }
89 11 return 0;
90 }
91
92
2/2
✓ Branch 0 taken 2875 times.
✓ Branch 1 taken 4165 times.
7040 if (!codec_name) {
93 2875 ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type);
94 2875 *enc = avcodec_find_encoder(ost->par_in->codec_id);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2875 times.
2875 if (!*enc) {
96 av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
97 "Default encoder for format %s (codec %s) is "
98 "probably disabled. Please choose an encoder manually.\n",
99 s->oformat->name, avcodec_get_name(ost->par_in->codec_id));
100 return AVERROR_ENCODER_NOT_FOUND;
101 }
102
2/2
✓ Branch 0 taken 3580 times.
✓ Branch 1 taken 585 times.
4165 } else if (strcmp(codec_name, "copy")) {
103 3580 int ret = find_codec(ost, codec_name, ost->type, 1, enc);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3580 times.
3580 if (ret < 0)
105 return ret;
106 3580 ost->par_in->codec_id = (*enc)->id;
107 }
108
109 7040 return 0;
110 }
111
112 static char *get_line(AVIOContext *s, AVBPrint *bprint)
113 {
114 char c;
115
116 while ((c = avio_r8(s)) && c != '\n')
117 av_bprint_chars(bprint, c, 1);
118
119 if (!av_bprint_is_complete(bprint))
120 return NULL;
121
122 return bprint->str;
123 }
124
125 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
126 {
127 int i, ret = -1;
128 char filename[1000];
129 char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR");
130 char *env_home = getenv_utf8("HOME");
131 const char *base[3] = { env_avconv_datadir,
132 env_home,
133 AVCONV_DATADIR,
134 };
135
136 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
137 if (!base[i])
138 continue;
139 if (codec_name) {
140 snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
141 i != 1 ? "" : "/.avconv", codec_name, preset_name);
142 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
143 }
144 if (ret < 0) {
145 snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
146 i != 1 ? "" : "/.avconv", preset_name);
147 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
148 }
149 }
150 freeenv_utf8(env_home);
151 freeenv_utf8(env_avconv_datadir);
152 return ret;
153 }
154
155 typedef struct EncStatsFile {
156 char *path;
157 AVIOContext *io;
158 } EncStatsFile;
159
160 static EncStatsFile *enc_stats_files;
161 static int nb_enc_stats_files;
162
163 static int enc_stats_get_file(AVIOContext **io, const char *path)
164 {
165 EncStatsFile *esf;
166 int ret;
167
168 for (int i = 0; i < nb_enc_stats_files; i++)
169 if (!strcmp(path, enc_stats_files[i].path)) {
170 *io = enc_stats_files[i].io;
171 return 0;
172 }
173
174 ret = GROW_ARRAY(enc_stats_files, nb_enc_stats_files);
175 if (ret < 0)
176 return ret;
177
178 esf = &enc_stats_files[nb_enc_stats_files - 1];
179
180 ret = avio_open2(&esf->io, path, AVIO_FLAG_WRITE, &int_cb, NULL);
181 if (ret < 0) {
182 av_log(NULL, AV_LOG_ERROR, "Error opening stats file '%s': %s\n",
183 path, av_err2str(ret));
184 return ret;
185 }
186
187 esf->path = av_strdup(path);
188 if (!esf->path)
189 return AVERROR(ENOMEM);
190
191 *io = esf->io;
192
193 return 0;
194 }
195
196 6741 void of_enc_stats_close(void)
197 {
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6741 times.
6741 for (int i = 0; i < nb_enc_stats_files; i++) {
199 av_freep(&enc_stats_files[i].path);
200 avio_closep(&enc_stats_files[i].io);
201 }
202 6741 av_freep(&enc_stats_files);
203 6741 nb_enc_stats_files = 0;
204 6741 }
205
206 static int unescape(char **pdst, size_t *dst_len,
207 const char **pstr, char delim)
208 {
209 const char *str = *pstr;
210 char *dst;
211 size_t len, idx;
212
213 *pdst = NULL;
214
215 len = strlen(str);
216 if (!len)
217 return 0;
218
219 dst = av_malloc(len + 1);
220 if (!dst)
221 return AVERROR(ENOMEM);
222
223 for (idx = 0; *str; idx++, str++) {
224 if (str[0] == '\\' && str[1])
225 str++;
226 else if (*str == delim)
227 break;
228
229 dst[idx] = *str;
230 }
231 if (!idx) {
232 av_freep(&dst);
233 return 0;
234 }
235
236 dst[idx] = 0;
237
238 *pdst = dst;
239 *dst_len = idx;
240 *pstr = str;
241
242 return 0;
243 }
244
245 static int enc_stats_init(OutputStream *ost, EncStats *es, int pre,
246 const char *path, const char *fmt_spec)
247 {
248 static const struct {
249 enum EncStatsType type;
250 const char *str;
251 unsigned pre_only:1;
252 unsigned post_only:1;
253 unsigned need_input_data:1;
254 } fmt_specs[] = {
255 { ENC_STATS_FILE_IDX, "fidx" },
256 { ENC_STATS_STREAM_IDX, "sidx" },
257 { ENC_STATS_FRAME_NUM, "n" },
258 { ENC_STATS_FRAME_NUM_IN, "ni", 0, 0, 1 },
259 { ENC_STATS_TIMEBASE, "tb" },
260 { ENC_STATS_TIMEBASE_IN, "tbi", 0, 0, 1 },
261 { ENC_STATS_PTS, "pts" },
262 { ENC_STATS_PTS_TIME, "t" },
263 { ENC_STATS_PTS_IN, "ptsi", 0, 0, 1 },
264 { ENC_STATS_PTS_TIME_IN, "ti", 0, 0, 1 },
265 { ENC_STATS_DTS, "dts", 0, 1 },
266 { ENC_STATS_DTS_TIME, "dt", 0, 1 },
267 { ENC_STATS_SAMPLE_NUM, "sn", 1 },
268 { ENC_STATS_NB_SAMPLES, "samp", 1 },
269 { ENC_STATS_PKT_SIZE, "size", 0, 1 },
270 { ENC_STATS_BITRATE, "br", 0, 1 },
271 { ENC_STATS_AVG_BITRATE, "abr", 0, 1 },
272 { ENC_STATS_KEYFRAME, "key", 0, 1 },
273 };
274 const char *next = fmt_spec;
275
276 int ret;
277
278 while (*next) {
279 EncStatsComponent *c;
280 char *val;
281 size_t val_len;
282
283 // get the sequence up until next opening brace
284 ret = unescape(&val, &val_len, &next, '{');
285 if (ret < 0)
286 return ret;
287
288 if (val) {
289 ret = GROW_ARRAY(es->components, es->nb_components);
290 if (ret < 0) {
291 av_freep(&val);
292 return ret;
293 }
294
295 c = &es->components[es->nb_components - 1];
296 c->type = ENC_STATS_LITERAL;
297 c->str = val;
298 c->str_len = val_len;
299 }
300
301 if (!*next)
302 break;
303 next++;
304
305 // get the part inside braces
306 ret = unescape(&val, &val_len, &next, '}');
307 if (ret < 0)
308 return ret;
309
310 if (!val) {
311 av_log(NULL, AV_LOG_ERROR,
312 "Empty formatting directive in: %s\n", fmt_spec);
313 return AVERROR(EINVAL);
314 }
315
316 if (!*next) {
317 av_log(NULL, AV_LOG_ERROR,
318 "Missing closing brace in: %s\n", fmt_spec);
319 ret = AVERROR(EINVAL);
320 goto fail;
321 }
322 next++;
323
324 ret = GROW_ARRAY(es->components, es->nb_components);
325 if (ret < 0)
326 goto fail;
327
328 c = &es->components[es->nb_components - 1];
329
330 for (size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) {
331 if (!strcmp(val, fmt_specs[i].str)) {
332 if ((pre && fmt_specs[i].post_only) || (!pre && fmt_specs[i].pre_only)) {
333 av_log(NULL, AV_LOG_ERROR,
334 "Format directive '%s' may only be used %s-encoding\n",
335 val, pre ? "post" : "pre");
336 ret = AVERROR(EINVAL);
337 goto fail;
338 }
339
340 c->type = fmt_specs[i].type;
341
342 if (fmt_specs[i].need_input_data && !ost->ist) {
343 av_log(ost, AV_LOG_WARNING,
344 "Format directive '%s' is unavailable, because "
345 "this output stream has no associated input stream\n",
346 val);
347 }
348
349 break;
350 }
351 }
352
353 if (!c->type) {
354 av_log(NULL, AV_LOG_ERROR, "Invalid format directive: %s\n", val);
355 ret = AVERROR(EINVAL);
356 goto fail;
357 }
358
359 fail:
360 av_freep(&val);
361 if (ret < 0)
362 return ret;
363 }
364
365 ret = pthread_mutex_init(&es->lock, NULL);
366 if (ret)
367 return AVERROR(ret);
368 es->lock_initialized = 1;
369
370 ret = enc_stats_get_file(&es->io, path);
371 if (ret < 0)
372 return ret;
373
374 return 0;
375 }
376
377 31 static const char *output_stream_item_name(void *obj)
378 {
379 31 const MuxStream *ms = obj;
380
381 31 return ms->log_name;
382 }
383
384 static const AVClass output_stream_class = {
385 .class_name = "OutputStream",
386 .version = LIBAVUTIL_VERSION_INT,
387 .item_name = output_stream_item_name,
388 .category = AV_CLASS_CATEGORY_MUXER,
389 };
390
391 7051 static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
392 {
393 7051 const char *type_str = av_get_media_type_string(type);
394 MuxStream *ms;
395
396 7051 ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (!ms)
398 return NULL;
399
400 7051 ms->ost.file = &mux->of;
401 7051 ms->ost.index = mux->of.nb_streams - 1;
402 7051 ms->ost.type = type;
403
404 7051 ms->ost.class = &output_stream_class;
405
406 7051 ms->sch_idx = -1;
407 7051 ms->sch_idx_enc = -1;
408
409
1/2
✓ Branch 0 taken 7051 times.
✗ Branch 1 not taken.
7051 snprintf(ms->log_name, sizeof(ms->log_name), "%cost#%d:%d",
410 7051 type_str ? *type_str : '?', mux->of.index, ms->ost.index);
411
412 7051 return ms;
413 }
414
415 6969 static int ost_get_filters(const OptionsContext *o, AVFormatContext *oc,
416 OutputStream *ost, char **dst)
417 {
418 6969 const char *filters = NULL;
419 #if FFMPEG_OPT_FILTER_SCRIPT
420 6969 const char *filters_script = NULL;
421
422
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6969 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6969 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6969 MATCH_PER_STREAM_OPT(filter_scripts, str, filters_script, oc, ost->st);
423 #endif
424
6/20
✓ Branch 1 taken 3404 times.
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91 times.
✓ Branch 5 taken 3495 times.
✓ Branch 6 taken 6969 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6969 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
10464 MATCH_PER_STREAM_OPT(filters, str, filters, oc, ost->st);
425
426
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 6417 times.
6969 if (!ost->enc) {
427
1/2
✓ Branch 0 taken 552 times.
✗ Branch 1 not taken.
552 if (
428 #if FFMPEG_OPT_FILTER_SCRIPT
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 filters_script ||
430 #endif
431 filters) {
432 av_log(ost, AV_LOG_ERROR,
433 "%s '%s' was specified, but codec copy was selected. "
434 "Filtering and streamcopy cannot be used together.\n",
435 #if FFMPEG_OPT_FILTER_SCRIPT
436 filters ? "Filtergraph" : "Filtergraph script",
437 filters ? filters : filters_script
438 #else
439 "Filtergraph", filters
440 #endif
441 );
442 return AVERROR(ENOSYS);
443 }
444 552 return 0;
445 }
446
447
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6268 times.
6417 if (!ost->ist) {
448
1/2
✓ Branch 0 taken 149 times.
✗ Branch 1 not taken.
149 if (
449 #if FFMPEG_OPT_FILTER_SCRIPT
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
149 filters_script ||
451 #endif
452 filters) {
453 av_log(ost, AV_LOG_ERROR,
454 "%s '%s' was specified for a stream fed from a complex "
455 "filtergraph. Simple and complex filtering cannot be used "
456 "together for the same stream.\n",
457 #if FFMPEG_OPT_FILTER_SCRIPT
458 filters ? "Filtergraph" : "Filtergraph script",
459 filters ? filters : filters_script
460 #else
461 "Filtergraph", filters
462 #endif
463 );
464 return AVERROR(EINVAL);
465 }
466 149 return 0;
467 }
468
469 #if FFMPEG_OPT_FILTER_SCRIPT
470
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6268 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6268 if (filters_script && filters) {
471 av_log(ost, AV_LOG_ERROR, "Both -filter and -filter_script set\n");
472 return AVERROR(EINVAL);
473 }
474
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6268 times.
6268 if (filters_script)
476 *dst = file_read(filters_script);
477 else
478 #endif
479
2/2
✓ Branch 0 taken 3404 times.
✓ Branch 1 taken 2864 times.
6268 if (filters)
480 3404 *dst = av_strdup(filters);
481 else
482
2/2
✓ Branch 0 taken 2279 times.
✓ Branch 1 taken 585 times.
2864 *dst = av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull");
483
1/2
✓ Branch 0 taken 6268 times.
✗ Branch 1 not taken.
6268 return *dst ? 0 : AVERROR(ENOMEM);
484 }
485
486 static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str)
487 {
488 int i;
489 const char *p = str;
490 for (i = 0;; i++) {
491 dest[i] = atoi(p);
492 if (i == 63)
493 break;
494 p = strchr(p, ',');
495 if (!p) {
496 av_log(logctx, AV_LOG_FATAL,
497 "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
498 return AVERROR(EINVAL);
499 }
500 p++;
501 }
502
503 return 0;
504 }
505
506 622 static int fmt_in_list(const int *formats, int format)
507 {
508
1/2
✓ Branch 0 taken 3374 times.
✗ Branch 1 not taken.
3374 for (; *formats != -1; formats++)
509
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 2752 times.
3374 if (*formats == format)
510 622 return 1;
511 return 0;
512 }
513
514 static enum AVPixelFormat
515 choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target)
516 {
517 const enum AVPixelFormat *p = codec->pix_fmts;
518 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
519 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
520 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
521 enum AVPixelFormat best= AV_PIX_FMT_NONE;
522
523 for (; *p != AV_PIX_FMT_NONE; p++) {
524 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
525 if (*p == target)
526 break;
527 }
528 if (*p == AV_PIX_FMT_NONE) {
529 if (target != AV_PIX_FMT_NONE)
530 av_log(NULL, AV_LOG_WARNING,
531 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
532 av_get_pix_fmt_name(target),
533 codec->name,
534 av_get_pix_fmt_name(best));
535 return best;
536 }
537 return target;
538 }
539
540 3733 static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name)
541 {
542 3733 const enum AVPixelFormat *fmts = ost->enc_ctx->codec->pix_fmts;
543 enum AVPixelFormat fmt;
544
545 3733 fmt = av_get_pix_fmt(name);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3733 times.
3733 if (fmt == AV_PIX_FMT_NONE) {
547 av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", name);
548 return AV_PIX_FMT_NONE;
549 }
550
551 /* when the user specified-format is an alias for an endianness-specific
552 * one (e.g. rgb48 -> rgb48be/le), it gets translated into the native
553 * endianness by av_get_pix_fmt();
554 * the following code handles the case when the native endianness is not
555 * supported by the encoder, but the other one is */
556
3/4
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 3422 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
3733 if (fmts && !fmt_in_list(fmts, fmt)) {
557 const char *name_canonical = av_get_pix_fmt_name(fmt);
558 int len = strlen(name_canonical);
559
560 if (strcmp(name, name_canonical) &&
561 (!strcmp(name_canonical + len - 2, "le") ||
562 !strcmp(name_canonical + len - 2, "be"))) {
563 char name_other[64];
564 enum AVPixelFormat fmt_other;
565
566 snprintf(name_other, sizeof(name_other), "%s%ce",
567 name, name_canonical[len - 2] == 'l' ? 'b' : 'l');
568 fmt_other = av_get_pix_fmt(name_other);
569 if (fmt_other != AV_PIX_FMT_NONE && fmt_in_list(fmts, fmt_other)) {
570 av_log(ost, AV_LOG_VERBOSE, "Mapping pixel format %s->%s\n",
571 name, name_other);
572 fmt = fmt_other;
573 }
574 }
575 }
576
577
3/4
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 3422 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
3733 if (fmts && !fmt_in_list(fmts, fmt))
578 fmt = choose_pixel_fmt(ost->enc_ctx->codec, fmt);
579
580 3733 return fmt;
581 }
582
583 5509 static int new_stream_video(Muxer *mux, const OptionsContext *o,
584 OutputStream *ost)
585 {
586 5509 AVFormatContext *oc = mux->fc;
587 AVStream *st;
588 5509 char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
589 5509 int ret = 0;
590
591 5509 st = ost->st;
592
593
4/20
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 5509 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5509 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5531 MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
594
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 5487 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
5509 if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
595 av_log(ost, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
596 return AVERROR(EINVAL);
597 }
598
599
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5509 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5509 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5509 MATCH_PER_STREAM_OPT(max_frame_rates, str, max_frame_rate, oc, st);
600
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5509 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
5509 if (max_frame_rate && av_parse_video_rate(&ost->max_frame_rate, max_frame_rate) < 0) {
601 av_log(ost, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
602 return AVERROR(EINVAL);
603 }
604
605
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 5487 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
5509 if (frame_rate && max_frame_rate) {
606 av_log(ost, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
607 return AVERROR(EINVAL);
608 }
609
610
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5509 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5509 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5509 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5509 times.
5509 if (frame_aspect_ratio) {
612 AVRational q;
613 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
614 q.num <= 0 || q.den <= 0) {
615 av_log(ost, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
616 return AVERROR(EINVAL);
617 }
618 ost->frame_aspect_ratio = q;
619 }
620
621
2/2
✓ Branch 0 taken 5188 times.
✓ Branch 1 taken 321 times.
5509 if (ost->enc_ctx) {
622 5188 AVCodecContext *video_enc = ost->enc_ctx;
623 5188 const char *p = NULL, *fps_mode = NULL;
624 5188 char *frame_size = NULL;
625 5188 char *frame_pix_fmt = NULL;
626 5188 char *intra_matrix = NULL, *inter_matrix = NULL;
627 5188 char *chroma_intra_matrix = NULL;
628 5188 int do_pass = 0;
629 int i;
630
631
4/20
✓ Branch 1 taken 231 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 231 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5419 MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
632
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 4957 times.
5188 if (frame_size) {
633 231 ret = av_parse_video_size(&video_enc->width, &video_enc->height, frame_size);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 231 times.
231 if (ret < 0) {
635 av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
636 return AVERROR(EINVAL);
637 }
638 }
639
640
4/20
✓ Branch 1 taken 3733 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3733 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
8921 MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
641
3/4
✓ Branch 0 taken 3733 times.
✓ Branch 1 taken 1455 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3733 times.
5188 if (frame_pix_fmt && *frame_pix_fmt == '+') {
642 ost->keep_pix_fmt = 1;
643 if (!*++frame_pix_fmt)
644 frame_pix_fmt = NULL;
645 }
646
2/2
✓ Branch 0 taken 3733 times.
✓ Branch 1 taken 1455 times.
5188 if (frame_pix_fmt) {
647 3733 video_enc->pix_fmt = pix_fmt_parse(ost, frame_pix_fmt);
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3733 times.
3733 if (video_enc->pix_fmt == AV_PIX_FMT_NONE)
649 return AVERROR(EINVAL);
650 }
651
652
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5188 MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 if (intra_matrix) {
654 if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64)))
655 return AVERROR(ENOMEM);
656
657 ret = parse_matrix_coeffs(ost, video_enc->intra_matrix, intra_matrix);
658 if (ret < 0)
659 return ret;
660 }
661
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5188 MATCH_PER_STREAM_OPT(chroma_intra_matrices, str, chroma_intra_matrix, oc, st);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 if (chroma_intra_matrix) {
663 uint16_t *p = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64);
664 if (!p)
665 return AVERROR(ENOMEM);
666 video_enc->chroma_intra_matrix = p;
667 ret = parse_matrix_coeffs(ost, p, chroma_intra_matrix);
668 if (ret < 0)
669 return ret;
670 }
671
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5188 MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 if (inter_matrix) {
673 if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64)))
674 return AVERROR(ENOMEM);
675 ret = parse_matrix_coeffs(ost, video_enc->inter_matrix, inter_matrix);
676 if (ret < 0)
677 return ret;
678 }
679
680
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5188 MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 for (i = 0; p; i++) {
682 int start, end, q;
683 int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
684 if (e != 3) {
685 av_log(ost, AV_LOG_FATAL, "error parsing rc_override\n");
686 return AVERROR(EINVAL);
687 }
688 video_enc->rc_override =
689 av_realloc_array(video_enc->rc_override,
690 i + 1, sizeof(RcOverride));
691 if (!video_enc->rc_override) {
692 av_log(ost, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
693 return AVERROR(ENOMEM);
694 }
695 video_enc->rc_override[i].start_frame = start;
696 video_enc->rc_override[i].end_frame = end;
697 if (q > 0) {
698 video_enc->rc_override[i].qscale = q;
699 video_enc->rc_override[i].quality_factor = 1.0;
700 }
701 else {
702 video_enc->rc_override[i].qscale = 0;
703 video_enc->rc_override[i].quality_factor = -q/100.0;
704 }
705 p = strchr(p, '/');
706 if (p) p++;
707 }
708 5188 video_enc->rc_override_count = i;
709
710 #if FFMPEG_OPT_PSNR
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 if (do_psnr) {
712 av_log(ost, AV_LOG_WARNING, "The -psnr option is deprecated, use -flags +psnr\n");
713 video_enc->flags|= AV_CODEC_FLAG_PSNR;
714 }
715 #endif
716
717 /* two pass mode */
718
4/20
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5196 MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
719
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5180 times.
5188 if (do_pass) {
720
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (do_pass & 1) {
721 4 video_enc->flags |= AV_CODEC_FLAG_PASS1;
722 4 av_dict_set(&ost->encoder_opts, "flags", "+pass1", AV_DICT_APPEND);
723 }
724
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (do_pass & 2) {
725 4 video_enc->flags |= AV_CODEC_FLAG_PASS2;
726 4 av_dict_set(&ost->encoder_opts, "flags", "+pass2", AV_DICT_APPEND);
727 }
728 }
729
730
4/20
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5196 MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
731
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5180 times.
5188 if (ost->logfile_prefix &&
732
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
733 return AVERROR(ENOMEM);
734
735
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5180 times.
5188 if (do_pass) {
736 8 int ost_idx = -1;
737 char logfilename[1024];
738 FILE *f;
739
740 /* compute this stream's global index */
741
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 for (int i = 0; i <= ost->file->index; i++)
742 8 ost_idx += output_files[i]->nb_streams;
743
744 8 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
745
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 ost->logfile_prefix ? ost->logfile_prefix :
746 DEFAULT_PASS_LOGFILENAME_PREFIX,
747 ost_idx);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!strcmp(ost->enc_ctx->codec->name, "libx264")) {
749 av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
750 } else {
751
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
752 4 char *logbuffer = file_read(logfilename);
753
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!logbuffer) {
755 av_log(ost, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
756 logfilename);
757 return AVERROR(EIO);
758 }
759 4 video_enc->stats_in = logbuffer;
760 }
761
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
762 4 f = fopen_utf8(logfilename, "wb");
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!f) {
764 av_log(ost, AV_LOG_FATAL,
765 "Cannot write log file '%s' for pass-1 encoding: %s\n",
766 logfilename, strerror(errno));
767 return AVERROR(errno);
768 }
769 4 ost->logfile = f;
770 }
771 }
772 }
773
774
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5189 MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
775
776 #if FFMPEG_OPT_TOP
777 5188 ost->top_field_first = -1;
778
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5188 MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5188 times.
5188 if (ost->top_field_first >= 0)
780 av_log(ost, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
781 #endif
782
783 #if FFMPEG_OPT_VSYNC
784 5188 ost->vsync_method = video_sync_method;
785 #else
786 ost->vsync_method = VSYNC_AUTO;
787 #endif
788
4/20
✓ Branch 1 taken 514 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 514 times.
✓ Branch 6 taken 5188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5188 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5702 MATCH_PER_STREAM_OPT(fps_mode, str, fps_mode, oc, st);
789
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 4674 times.
5188 if (fps_mode) {
790 514 ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file->index, ost->index, 0);
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514 times.
514 if (ret < 0)
792 return ret;
793 }
794
795
3/4
✓ Branch 0 taken 5167 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5167 times.
5188 if ((ost->frame_rate.num || ost->max_frame_rate.num) &&
796
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 !(ost->vsync_method == VSYNC_AUTO ||
797
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR)) {
798 av_log(ost, AV_LOG_FATAL, "One of -r/-fpsmax was specified "
799 "together a non-CFR -vsync/-fps_mode. This is contradictory.\n");
800 return AVERROR(EINVAL);
801 }
802
803
2/2
✓ Branch 0 taken 4674 times.
✓ Branch 1 taken 514 times.
5188 if (ost->vsync_method == VSYNC_AUTO) {
804
3/4
✓ Branch 0 taken 4654 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4654 times.
4674 if (ost->frame_rate.num || ost->max_frame_rate.num) {
805 20 ost->vsync_method = VSYNC_CFR;
806
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 4370 times.
4654 } else if (!strcmp(oc->oformat->name, "avi")) {
807 284 ost->vsync_method = VSYNC_VFR;
808 } else {
809 4370 ost->vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
810 3902 ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
811
4/4
✓ Branch 0 taken 3902 times.
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3890 times.
4370 VSYNC_PASSTHROUGH : VSYNC_VFR) :
812 VSYNC_CFR;
813 }
814
815
4/4
✓ Branch 0 taken 4587 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 488 times.
✓ Branch 3 taken 4099 times.
4674 if (ost->ist && ost->vsync_method == VSYNC_CFR) {
816 488 const InputFile *ifile = ost->ist->file;
817
818
3/4
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 442 times.
✗ Branch 3 not taken.
488 if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0)
819 442 ost->vsync_method = VSYNC_VSCFR;
820 }
821
822
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 4628 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
4674 if (ost->vsync_method == VSYNC_CFR && copy_ts) {
823 ost->vsync_method = VSYNC_VSCFR;
824 }
825 }
826
4/4
✓ Branch 0 taken 5136 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 442 times.
✓ Branch 3 taken 4694 times.
5188 ost->is_cfr = (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR);
827 }
828
829 5509 return 0;
830 }
831
832 1460 static int new_stream_audio(Muxer *mux, const OptionsContext *o,
833 OutputStream *ost)
834 {
835 1460 AVFormatContext *oc = mux->fc;
836 AVStream *st;
837 1460 int ret = 0;
838
839 1460 st = ost->st;
840
841
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 231 times.
1460 if (ost->enc_ctx) {
842 1229 AVCodecContext *audio_enc = ost->enc_ctx;
843 1229 int channels = 0;
844 1229 char *layout = NULL;
845 1229 char *sample_fmt = NULL;
846
847
4/20
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 1229 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1240 MATCH_PER_STREAM_OPT(audio_channels, i, channels, oc, st);
848
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1218 times.
1229 if (channels) {
849 11 audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
850 11 audio_enc->ch_layout.nb_channels = channels;
851 }
852
853
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1229 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1230 MATCH_PER_STREAM_OPT(audio_ch_layouts, str, layout, oc, st);
854
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1228 times.
1229 if (layout) {
855
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
856 #if FF_API_OLD_CHANNEL_LAYOUT
857 uint64_t mask;
858 AV_NOWARN_DEPRECATED({
859 mask = av_get_channel_layout(layout);
860 })
861 if (!mask) {
862 #endif
863 av_log(ost, AV_LOG_FATAL, "Unknown channel layout: %s\n", layout);
864 return AVERROR(EINVAL);
865 #if FF_API_OLD_CHANNEL_LAYOUT
866 }
867 av_log(ost, AV_LOG_WARNING, "Channel layout '%s' uses a deprecated syntax.\n",
868 layout);
869 av_channel_layout_from_mask(&audio_enc->ch_layout, mask);
870 #endif
871 }
872 }
873
874
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1229 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1229 MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1229 times.
1229 if (sample_fmt &&
876 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
877 av_log(ost, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
878 return AVERROR(EINVAL);
879 }
880
881
4/20
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 1229 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1243 MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
882
883
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1229 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1229 MATCH_PER_STREAM_OPT(apad, str, ost->apad, oc, st);
884 1229 ost->apad = av_strdup(ost->apad);
885
886 #if FFMPEG_OPT_MAP_CHANNEL
887 /* check for channel mapping for this audio stream */
888
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1229 times.
1239 for (int n = 0; n < o->nb_audio_channel_maps; n++) {
889 10 AudioChannelMap *map = &o->audio_channel_maps[n];
890
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10 if ((map->ofile_idx == -1 || ost->file->index == map->ofile_idx) &&
891
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10 (map->ostream_idx == -1 || ost->st->index == map->ostream_idx)) {
892 InputStream *ist;
893
894
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (map->channel_idx == -1) {
895 1 ist = NULL;
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!ost->ist) {
897 av_log(ost, AV_LOG_FATAL, "Cannot determine input stream for channel mapping %d.%d\n",
898 ost->file->index, ost->st->index);
899 continue;
900 } else {
901 9 ist = ost->ist;
902 }
903
904
4/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
10 if (!ist || (ist->file->index == map->file_idx && ist->index == map->stream_idx)) {
905 10 ret = av_reallocp_array(&ost->audio_channels_map,
906 10 ost->audio_channels_mapped + 1,
907 sizeof(*ost->audio_channels_map));
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
909 return ret;
910
911 10 ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
912 }
913 }
914 }
915 #endif
916 }
917
918 1460 return 0;
919 }
920
921 71 static int new_stream_subtitle(Muxer *mux, const OptionsContext *o,
922 OutputStream *ost)
923 {
924 AVStream *st;
925
926 71 st = ost->st;
927
928
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 33 times.
71 if (ost->enc_ctx) {
929 38 AVCodecContext *subtitle_enc = ost->enc_ctx;
930
931 AVCodecDescriptor const *input_descriptor =
932 38 avcodec_descriptor_get(ost->ist->par->codec_id);
933 AVCodecDescriptor const *output_descriptor =
934 38 avcodec_descriptor_get(subtitle_enc->codec_id);
935 38 int input_props = 0, output_props = 0;
936
937 38 char *frame_size = NULL;
938
939
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
38 MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, mux->fc, st);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (frame_size) {
941 int ret = av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size);
942 if (ret < 0) {
943 av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
944 return ret;
945 }
946 }
947
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (input_descriptor)
948 38 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
949
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (output_descriptor)
950 38 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
951
3/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 38 times.
38 if (input_props && output_props && input_props != output_props) {
952 av_log(ost, AV_LOG_ERROR,
953 "Subtitle encoding currently only possible from text to text "
954 "or bitmap to bitmap\n");
955 return AVERROR(EINVAL);
956 }
957 }
958
959 71 return 0;
960 }
961
962 595 static int streamcopy_init(const Muxer *mux, OutputStream *ost)
963 {
964 595 MuxStream *ms = ms_from_ost(ost);
965
966 595 const InputStream *ist = ost->ist;
967 595 const InputFile *ifile = ist->file;
968
969 595 AVCodecParameters *par = ost->par_in;
970 595 uint32_t codec_tag = par->codec_tag;
971
972 595 AVCodecContext *codec_ctx = NULL;
973 595 AVDictionary *codec_opts = NULL;
974
975 595 AVRational fr = ost->frame_rate;
976
977 595 int ret = 0;
978
979 595 codec_ctx = avcodec_alloc_context3(NULL);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (!codec_ctx)
981 return AVERROR(ENOMEM);
982
983 595 ret = avcodec_parameters_to_context(codec_ctx, ist->par);
984
1/2
✓ Branch 0 taken 595 times.
✗ Branch 1 not taken.
595 if (ret >= 0)
985 595 ret = av_opt_set_dict(codec_ctx, &ost->encoder_opts);
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ret < 0) {
987 av_log(ost, AV_LOG_FATAL,
988 "Error setting up codec context options.\n");
989 goto fail;
990 }
991
992 595 ret = avcodec_parameters_from_context(par, codec_ctx);
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ret < 0) {
994 av_log(ost, AV_LOG_FATAL,
995 "Error getting reference codec parameters.\n");
996 goto fail;
997 }
998
999
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 6 times.
595 if (!codec_tag) {
1000 589 const struct AVCodecTag * const *ct = mux->fc->oformat->codec_tag;
1001 unsigned int codec_tag_tmp;
1002
6/6
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 479 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 67 times.
667 if (!ct || av_codec_get_id (ct, par->codec_tag) == par->codec_id ||
1003 78 !av_codec_get_tag2(ct, par->codec_id, &codec_tag_tmp))
1004 522 codec_tag = par->codec_tag;
1005 }
1006
1007 595 par->codec_tag = codec_tag;
1008
1009
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 1 times.
595 if (!fr.num)
1010 594 fr = ist->framerate;
1011
1012
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 593 times.
595 if (fr.num)
1013 2 ost->st->avg_frame_rate = fr;
1014 else
1015 593 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
1016
1017 595 ret = avformat_transfer_internal_stream_timing_info(mux->fc->oformat,
1018 595 ost->st, ist->st, copy_tb);
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ret < 0)
1020 goto fail;
1021
1022 // copy timebase while removing common factors
1023
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
595 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
1024
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 593 times.
594 if (fr.num)
1025 1 ost->st->time_base = av_inv_q(fr);
1026 else
1027 593 ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
1028 }
1029
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (!ms->copy_prior_start) {
1031 ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
1032 0 : mux->of.start_time;
1033 if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
1034 ms->ts_copy_start = FFMAX(ms->ts_copy_start,
1035 ifile->start_time + ifile->ts_offset);
1036 }
1037 }
1038
1039
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 595 times.
659 for (int i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
1040 64 const AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
1041 AVPacketSideData *sd_dst;
1042
1043 64 sd_dst = av_packet_side_data_new(&ost->st->codecpar->coded_side_data,
1044 64 &ost->st->codecpar->nb_coded_side_data,
1045 64 sd_src->type, sd_src->size, 0);
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!sd_dst) {
1047 ret = AVERROR(ENOMEM);
1048 goto fail;
1049 }
1050 64 memcpy(sd_dst->data, sd_src->data, sd_src->size);
1051 }
1052
1053 #if FFMPEG_ROTATION_METADATA
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ost->rotate_overridden) {
1055 AVPacketSideData *sd = av_packet_side_data_new(&ost->st->codecpar->coded_side_data,
1056 &ost->st->codecpar->nb_coded_side_data,
1057 AV_PKT_DATA_DISPLAYMATRIX,
1058 sizeof(int32_t) * 9, 0);
1059 if (sd)
1060 av_display_rotation_set((int32_t *)sd->data, -ost->rotate_override_value);
1061 }
1062 #endif
1063
1064
3/3
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 321 times.
✓ Branch 2 taken 43 times.
595 switch (par->codec_type) {
1065 231 case AVMEDIA_TYPE_AUDIO:
1066
4/6
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 226 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 226 times.
231 if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) &&
1067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 par->codec_id == AV_CODEC_ID_MP3)
1068 par->block_align = 0;
1069
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 218 times.
231 if (par->codec_id == AV_CODEC_ID_AC3)
1070 13 par->block_align = 0;
1071 231 break;
1072 321 case AVMEDIA_TYPE_VIDEO: {
1073 AVRational sar;
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
321 if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
1075 sar =
1076 av_mul_q(ost->frame_aspect_ratio,
1077 (AVRational){ par->height, par->width });
1078 av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio "
1079 "with stream copy may produce invalid files\n");
1080 }
1081
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 232 times.
321 else if (ist->st->sample_aspect_ratio.num)
1082 89 sar = ist->st->sample_aspect_ratio;
1083 else
1084 232 sar = par->sample_aspect_ratio;
1085 321 ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar;
1086 321 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
1087 321 ost->st->r_frame_rate = ist->st->r_frame_rate;
1088 321 break;
1089 }
1090 }
1091
1092 595 fail:
1093 595 avcodec_free_context(&codec_ctx);
1094 595 av_dict_free(&codec_opts);
1095 595 return ret;
1096 }
1097
1098 7051 static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
1099 InputStream *ist, OutputFilter *ofilter,
1100 OutputStream **post)
1101 {
1102 7051 AVFormatContext *oc = mux->fc;
1103 MuxStream *ms;
1104 OutputStream *ost;
1105 const AVCodec *enc;
1106 AVStream *st;
1107 7051 int ret = 0;
1108 7051 const char *bsfs = NULL, *time_base = NULL;
1109 7051 char *filters = NULL, *next, *codec_tag = NULL;
1110 7051 double qscale = -1;
1111 int i;
1112
1113 7051 st = avformat_new_stream(oc, NULL);
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (!st)
1115 return AVERROR(ENOMEM);
1116
1117 7051 ms = mux_stream_alloc(mux, type);
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (!ms)
1119 return AVERROR(ENOMEM);
1120
1121 // only streams with sources (i.e. not attachments)
1122 // are handled by the scheduler
1123
4/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 6901 times.
✓ Branch 2 taken 149 times.
✓ Branch 3 taken 1 times.
7051 if (ist || ofilter) {
1124 7050 ret = GROW_ARRAY(mux->sch_stream_idx, mux->nb_sch_stream_idx);
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7050 times.
7050 if (ret < 0)
1126 return ret;
1127
1128 7050 ret = sch_add_mux_stream(mux->sch, mux->sch_idx);
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7050 times.
7050 if (ret < 0)
1130 return ret;
1131
1132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7050 times.
7050 av_assert0(ret == mux->nb_sch_stream_idx - 1);
1133 7050 mux->sch_stream_idx[ret] = ms->ost.index;
1134 7050 ms->sch_idx = ret;
1135 }
1136
1137 7051 ost = &ms->ost;
1138
1139
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7033 times.
7051 if (o->streamid) {
1140 AVDictionaryEntry *e;
1141 char idx[16], *p;
1142 18 snprintf(idx, sizeof(idx), "%d", ost->index);
1143
1144 18 e = av_dict_get(o->streamid, idx, NULL, 0);
1145
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (e) {
1146 18 st->id = strtol(e->value, &p, 0);
1147
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (!e->value[0] || *p) {
1148 av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value);
1149 return AVERROR(EINVAL);
1150 }
1151 }
1152 }
1153
1154 7051 ost->par_in = avcodec_parameters_alloc();
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (!ost->par_in)
1156 return AVERROR(ENOMEM);
1157
1158 7051 ms->last_mux_dts = AV_NOPTS_VALUE;
1159
1160 7051 ost->st = st;
1161 7051 ost->ist = ist;
1162 7051 ost->kf.ref_pts = AV_NOPTS_VALUE;
1163 7051 ost->par_in->codec_type = type;
1164 7051 st->codecpar->codec_type = type;
1165
1166 7051 ret = choose_encoder(o, oc, ost, &enc);
1167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (ret < 0) {
1168 av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
1169 return ret;
1170 }
1171
1172
2/2
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
7051 if (enc) {
1173 6455 ost->enc_ctx = avcodec_alloc_context3(enc);
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (!ost->enc_ctx)
1175 return AVERROR(ENOMEM);
1176
1177 6455 ret = sch_add_enc(mux->sch, encoder_thread, ost,
1178
2/2
✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 38 times.
6455 ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
1180 return ret;
1181 6455 ms->sch_idx_enc = ret;
1182
1183 6455 ret = enc_alloc(&ost->enc, enc, mux->sch, ms->sch_idx_enc);
1184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
1185 return ret;
1186
1187 6455 av_strlcat(ms->log_name, "/", sizeof(ms->log_name));
1188 6455 av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
1189 } else {
1190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
596 if (ofilter) {
1191 av_log(ost, AV_LOG_ERROR,
1192 "Streamcopy requested for output stream fed "
1193 "from a complex filtergraph. Filtering and streamcopy "
1194 "cannot be used together.\n");
1195 return AVERROR(EINVAL);
1196 }
1197
1198 596 av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name));
1199 }
1200
1201 7051 av_log(ost, AV_LOG_VERBOSE, "Created %s stream from ",
1202 av_get_media_type_string(type));
1203
2/2
✓ Branch 0 taken 6901 times.
✓ Branch 1 taken 150 times.
7051 if (ist)
1204 6901 av_log(ost, AV_LOG_VERBOSE, "input stream %d:%d",
1205 6901 ist->file->index, ist->index);
1206
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 1 times.
150 else if (ofilter)
1207 149 av_log(ost, AV_LOG_VERBOSE, "complex filtergraph %d:[%s]\n",
1208 149 ofilter->graph->index, ofilter->name);
1209
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (type == AVMEDIA_TYPE_ATTACHMENT)
1210 1 av_log(ost, AV_LOG_VERBOSE, "attached file");
1211 else av_assert0(0);
1212 7051 av_log(ost, AV_LOG_VERBOSE, "\n");
1213
1214 7051 ms->pkt = av_packet_alloc();
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (!ms->pkt)
1216 return AVERROR(ENOMEM);
1217
1218
2/2
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
7051 if (ost->enc_ctx) {
1219 6455 AVCodecContext *enc = ost->enc_ctx;
1220 6455 AVIOContext *s = NULL;
1221 6455 char *buf = NULL, *arg = NULL, *preset = NULL;
1222 6455 const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
1223 6455 const char *enc_time_base = NULL;
1224
1225 6455 ret = filter_codec_opts(o->g->codec_opts, enc->codec_id,
1226 6455 oc, st, enc->codec, &ost->encoder_opts);
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
1228 return ret;
1229
1230
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6455 MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
1231 6455 ost->autoscale = 1;
1232
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6455 MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st);
1233
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6455 if (preset && (!(ret = get_preset_file_2(preset, enc->codec->name, &s)))) {
1234 AVBPrint bprint;
1235 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1236 do {
1237 av_bprint_clear(&bprint);
1238 buf = get_line(s, &bprint);
1239 if (!buf) {
1240 ret = AVERROR(ENOMEM);
1241 break;
1242 }
1243
1244 if (!buf[0] || buf[0] == '#')
1245 continue;
1246 if (!(arg = strchr(buf, '='))) {
1247 av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
1248 ret = AVERROR(EINVAL);
1249 break;
1250 }
1251 *arg++ = 0;
1252 av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
1253 } while (!s->eof_reached);
1254 av_bprint_finalize(&bprint, NULL);
1255 avio_closep(&s);
1256 }
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret) {
1258 av_log(ost, AV_LOG_FATAL,
1259 "Preset %s specified, but could not be opened.\n", preset);
1260 return ret;
1261 }
1262
1263
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6455 MATCH_PER_STREAM_OPT(enc_stats_pre, str, enc_stats_pre, oc, st);
1264
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6455 if (enc_stats_pre &&
1265 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1266 const char *format = "{fidx} {sidx} {n} {t}";
1267
1268 MATCH_PER_STREAM_OPT(enc_stats_pre_fmt, str, format, oc, st);
1269
1270 ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format);
1271 if (ret < 0)
1272 return ret;
1273 }
1274
1275
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6455 MATCH_PER_STREAM_OPT(enc_stats_post, str, enc_stats_post, oc, st);
1276
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6455 if (enc_stats_post &&
1277 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1278 const char *format = "{fidx} {sidx} {n} {t}";
1279
1280 MATCH_PER_STREAM_OPT(enc_stats_post_fmt, str, format, oc, st);
1281
1282 ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format);
1283 if (ret < 0)
1284 return ret;
1285 }
1286
1287
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6455 MATCH_PER_STREAM_OPT(mux_stats, str, mux_stats, oc, st);
1288
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6455 if (mux_stats &&
1289 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1290 const char *format = "{fidx} {sidx} {n} {t}";
1291
1292 MATCH_PER_STREAM_OPT(mux_stats_fmt, str, format, oc, st);
1293
1294 ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format);
1295 if (ret < 0)
1296 return ret;
1297 }
1298
1299
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 6455 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6455 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
6456 MATCH_PER_STREAM_OPT(enc_time_bases, str, enc_time_base, oc, st);
1300
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6454 times.
6455 if (enc_time_base) {
1301 AVRational q;
1302
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!strcmp(enc_time_base, "demux")) {
1303 1 q = (AVRational){ ENC_TIME_BASE_DEMUX, 0 };
1304 } else if (!strcmp(enc_time_base, "filter")) {
1305 q = (AVRational){ ENC_TIME_BASE_FILTER, 0 };
1306 } else {
1307 ret = av_parse_ratio(&q, enc_time_base, INT_MAX, 0, NULL);
1308 if (ret < 0 || q.den <= 0
1309 #if !FFMPEG_OPT_ENC_TIME_BASE_NUM
1310 || q.num < 0
1311 #endif
1312 ) {
1313 av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", enc_time_base);
1314 return ret < 0 ? ret : AVERROR(EINVAL);
1315 }
1316 #if FFMPEG_OPT_ENC_TIME_BASE_NUM
1317 if (q.num < 0)
1318 av_log(ost, AV_LOG_WARNING, "-enc_time_base -1 is deprecated,"
1319 " use -enc_timebase demux\n");
1320 #endif
1321 }
1322
1323 1 ost->enc_timebase = q;
1324 }
1325 } else {
1326 596 ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st,
1327 NULL, &ost->encoder_opts);
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
596 if (ret < 0)
1329 return ret;
1330 }
1331
1332
1333
2/2
✓ Branch 0 taken 1853 times.
✓ Branch 1 taken 5198 times.
7051 if (o->bitexact) {
1334 1853 ost->bitexact = 1;
1335
2/2
✓ Branch 0 taken 4709 times.
✓ Branch 1 taken 489 times.
5198 } else if (ost->enc_ctx) {
1336 4709 ost->bitexact = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags",
1337 AV_CODEC_FLAG_BITEXACT);
1338 }
1339
1340
4/20
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7054 MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
1341
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7048 times.
7051 if (time_base) {
1342 AVRational q;
1343
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1344
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 q.num <= 0 || q.den <= 0) {
1345 av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
1346 return AVERROR(EINVAL);
1347 }
1348 3 st->time_base = q;
1349 }
1350
1351 7051 ms->max_frames = INT64_MAX;
1352
14/20
✓ Branch 1 taken 4812 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4816 times.
✓ Branch 6 taken 7051 times.
✓ Branch 7 taken 2141 times.
✓ Branch 8 taken 4910 times.
✓ Branch 9 taken 2141 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 2141 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 2141 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2141 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 6423 times.
✓ Branch 19 taken 2141 times.
✓ Branch 20 taken 2141 times.
✗ Branch 21 not taken.
18290 MATCH_PER_STREAM_OPT(max_frames, i64, ms->max_frames, oc, st);
1353
2/2
✓ Branch 0 taken 4816 times.
✓ Branch 1 taken 7045 times.
11861 for (i = 0; i < o->max_frames.nb_opt; i++) {
1354 4816 char *p = o->max_frames.opt[i].specifier;
1355
4/4
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 4647 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 163 times.
4816 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1356 6 av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1357 6 break;
1358 }
1359 }
1360
1361 7051 ms->copy_prior_start = -1;
1362
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7051 MATCH_PER_STREAM_OPT(copy_prior_start, i, ms->copy_prior_start, oc ,st);
1363
1364
6/20
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 142 times.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7193 MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
1365
3/4
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 6919 times.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
7051 if (bsfs && *bsfs) {
1366 132 ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx);
1367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0) {
1368 av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1369 return ret;
1370 }
1371 }
1372
1373
4/20
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7058 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
1374
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7044 times.
7051 if (codec_tag) {
1375 7 uint32_t tag = strtol(codec_tag, &next, 0);
1376
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (*next) {
1377 7 uint8_t buf[4] = { 0 };
1378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1379 7 tag = AV_RL32(buf);
1380 }
1381 7 ost->st->codecpar->codec_tag = tag;
1382 7 ost->par_in->codec_tag = tag;
1383
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (ost->enc_ctx)
1384 1 ost->enc_ctx->codec_tag = tag;
1385 }
1386
1387
14/20
✓ Branch 1 taken 266 times.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✓ Branch 5 taken 321 times.
✓ Branch 6 taken 7051 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 7050 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
7373 MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
1388
4/4
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 265 times.
✓ Branch 3 taken 6190 times.
7051 if (ost->enc_ctx && qscale >= 0) {
1389 265 ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1390 265 ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1391 }
1392
1393
2/2
✓ Branch 0 taken 7050 times.
✓ Branch 1 taken 1 times.
7051 if (ms->sch_idx >= 0) {
1394 7050 int max_muxing_queue_size = 128;
1395 7050 int muxing_queue_data_threshold = 50 * 1024 * 1024;
1396
1397
4/20
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 7050 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7050 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7052 MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, max_muxing_queue_size, oc, st);
1398
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7050 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7050 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7050 MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, muxing_queue_data_threshold, oc, st);
1399
1400 7050 sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx,
1401 max_muxing_queue_size, muxing_queue_data_threshold);
1402 }
1403
1404
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7052 MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample,
1405 oc, st);
1406
1407
6/20
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7053 MATCH_PER_STREAM_OPT(fix_sub_duration_heartbeat, i, ost->fix_sub_duration_heartbeat,
1408 oc, st);
1409
1410
4/4
✓ Branch 0 taken 2726 times.
✓ Branch 1 taken 4325 times.
✓ Branch 2 taken 2632 times.
✓ Branch 3 taken 94 times.
7051 if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx)
1411 2632 ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1412
1413 7051 av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
1414
1415 7051 av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0);
1416
4/4
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
✓ Branch 3 taken 59 times.
✓ Branch 4 taken 6396 times.
7051 if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24)
1417 59 av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
1418
1419
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7051 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7051 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7051 MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
1420 ms->copy_initial_nonkeyframes, oc, st);
1421
1422
4/4
✓ Branch 0 taken 5509 times.
✓ Branch 1 taken 1460 times.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 11 times.
7051 switch (type) {
1423 5509 case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost); break;
1424 1460 case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break;
1425 71 case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break;
1426 }
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7051 times.
7051 if (ret < 0)
1428 return ret;
1429
1430
4/4
✓ Branch 0 taken 1542 times.
✓ Branch 1 taken 5509 times.
✓ Branch 2 taken 1460 times.
✓ Branch 3 taken 82 times.
7051 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) {
1431 6969 ret = ost_get_filters(o, oc, ost, &filters);
1432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6969 times.
6969 if (ret < 0)
1433 return ret;
1434 }
1435
1436
4/4
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 1267 times.
✓ Branch 3 taken 5188 times.
7051 if (ost->enc &&
1437
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 38 times.
7684 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1438
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6268 times.
6417 if (ofilter) {
1439 149 ost->filter = ofilter;
1440 149 ret = ofilter_bind_ost(ofilter, ost, ms->sch_idx_enc);
1441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
149 if (ret < 0)
1442 return ret;
1443 } else {
1444 6268 ret = init_simple_filtergraph(ost->ist, ost, filters,
1445 6268 mux->sch, ms->sch_idx_enc);
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6268 times.
6268 if (ret < 0) {
1447 av_log(ost, AV_LOG_ERROR,
1448 "Error initializing a simple filtergraph\n");
1449 return ret;
1450 }
1451 }
1452
1453 6417 ret = sch_connect(mux->sch, SCH_ENC(ms->sch_idx_enc),
1454 6417 SCH_MSTREAM(mux->sch_idx, ms->sch_idx));
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6417 times.
6417 if (ret < 0)
1456 return ret;
1457
2/2
✓ Branch 0 taken 633 times.
✓ Branch 1 taken 1 times.
634 } else if (ost->ist) {
1458 633 int sched_idx = ist_output_add(ost->ist, ost);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
633 if (sched_idx < 0) {
1460 av_log(ost, AV_LOG_ERROR,
1461 "Error binding an input stream\n");
1462 return sched_idx;
1463 }
1464 633 ms->sch_idx_src = sched_idx;
1465
1466
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 595 times.
633 if (ost->enc) {
1467 38 ret = sch_connect(mux->sch, SCH_DEC(sched_idx),
1468 38 SCH_ENC(ms->sch_idx_enc));
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0)
1470 return ret;
1471
1472 38 ret = sch_connect(mux->sch, SCH_ENC(ms->sch_idx_enc),
1473 38 SCH_MSTREAM(mux->sch_idx, ms->sch_idx));
1474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0)
1475 return ret;
1476 } else {
1477 595 ret = sch_connect(mux->sch, SCH_DSTREAM(ost->ist->file->index, sched_idx),
1478 595 SCH_MSTREAM(ost->file->index, ms->sch_idx));
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ret < 0)
1480 return ret;
1481 }
1482 }
1483
1484
4/4
✓ Branch 0 taken 6901 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 595 times.
✓ Branch 3 taken 6306 times.
7051 if (ost->ist && !ost->enc) {
1485 595 ret = streamcopy_init(mux, ost);
1486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 595 times.
595 if (ret < 0)
1487 return ret;
1488 }
1489
1490 // copy estimated duration as a hint to the muxer
1491
4/4
✓ Branch 0 taken 6901 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 5414 times.
✓ Branch 3 taken 1487 times.
7051 if (ost->ist && ost->ist->st->duration > 0) {
1492 5414 ms->stream_duration = ist->st->duration;
1493 5414 ms->stream_duration_tb = ist->st->time_base;
1494 }
1495
1496
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7050 times.
7051 if (post)
1497 1 *post = ost;
1498
1499 7051 return 0;
1500 }
1501
1502 6362 static int map_auto_video(Muxer *mux, const OptionsContext *o)
1503 {
1504 6362 AVFormatContext *oc = mux->fc;
1505 6362 InputStream *best_ist = NULL;
1506 6362 int best_score = 0;
1507 int qcr;
1508
1509 /* video: highest resolution */
1510
2/2
✓ Branch 1 taken 737 times.
✓ Branch 2 taken 5625 times.
6362 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_VIDEO) == AV_CODEC_ID_NONE)
1511 737 return 0;
1512
1513 5625 qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
1514
2/2
✓ Branch 0 taken 5665 times.
✓ Branch 1 taken 5625 times.
11290 for (int j = 0; j < nb_input_files; j++) {
1515 5665 InputFile *ifile = input_files[j];
1516 5665 InputStream *file_best_ist = NULL;
1517 5665 int file_best_score = 0;
1518
2/2
✓ Branch 0 taken 5941 times.
✓ Branch 1 taken 5665 times.
11606 for (int i = 0; i < ifile->nb_streams; i++) {
1519 5941 InputStream *ist = ifile->streams[i];
1520 int score;
1521
1522
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 1 times.
5941 if (ist->user_set_discard == AVDISCARD_ALL ||
1523
2/2
✓ Branch 0 taken 624 times.
✓ Branch 1 taken 5316 times.
5940 ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1524 625 continue;
1525
1526 10632 score = ist->st->codecpar->width * ist->st->codecpar->height
1527
2/2
✓ Branch 0 taken 5314 times.
✓ Branch 1 taken 2 times.
5316 + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1528
2/2
✓ Branch 0 taken 505 times.
✓ Branch 1 taken 4811 times.
5316 + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1529
3/4
✓ Branch 0 taken 5316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 5307 times.
5316 if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1530 9 score = 1;
1531
1532
2/2
✓ Branch 0 taken 5315 times.
✓ Branch 1 taken 1 times.
5316 if (score > file_best_score) {
1533
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5315 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5315 if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1534 continue;
1535 5315 file_best_score = score;
1536 5315 file_best_ist = ist;
1537 }
1538 }
1539
2/2
✓ Branch 0 taken 5315 times.
✓ Branch 1 taken 350 times.
5665 if (file_best_ist) {
1540
1/2
✓ Branch 0 taken 5315 times.
✗ Branch 1 not taken.
5315 if((qcr == MKTAG('A', 'P', 'I', 'C')) ||
1541
2/2
✓ Branch 0 taken 5307 times.
✓ Branch 1 taken 8 times.
5315 !(file_best_ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1542
2/2
✓ Branch 0 taken 505 times.
✓ Branch 1 taken 4802 times.
5307 file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1543
1/2
✓ Branch 0 taken 5315 times.
✗ Branch 1 not taken.
5315 if (file_best_score > best_score) {
1544 5315 best_score = file_best_score;
1545 5315 best_ist = file_best_ist;
1546 }
1547 }
1548 }
1549
2/2
✓ Branch 0 taken 5315 times.
✓ Branch 1 taken 310 times.
5625 if (best_ist)
1550 5315 return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL, NULL);
1551
1552 310 return 0;
1553 }
1554
1555 6451 static int map_auto_audio(Muxer *mux, const OptionsContext *o)
1556 {
1557 6451 AVFormatContext *oc = mux->fc;
1558 6451 InputStream *best_ist = NULL;
1559 6451 int best_score = 0;
1560
1561 /* audio: most channels */
1562
2/2
✓ Branch 1 taken 842 times.
✓ Branch 2 taken 5609 times.
6451 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_AUDIO) == AV_CODEC_ID_NONE)
1563 842 return 0;
1564
1565
2/2
✓ Branch 0 taken 5608 times.
✓ Branch 1 taken 5609 times.
11217 for (int j = 0; j < nb_input_files; j++) {
1566 5608 InputFile *ifile = input_files[j];
1567 5608 InputStream *file_best_ist = NULL;
1568 5608 int file_best_score = 0;
1569
2/2
✓ Branch 0 taken 5810 times.
✓ Branch 1 taken 5608 times.
11418 for (int i = 0; i < ifile->nb_streams; i++) {
1570 5810 InputStream *ist = ifile->streams[i];
1571 int score;
1572
1573
2/2
✓ Branch 0 taken 5809 times.
✓ Branch 1 taken 1 times.
5810 if (ist->user_set_discard == AVDISCARD_ALL ||
1574
2/2
✓ Branch 0 taken 4518 times.
✓ Branch 1 taken 1291 times.
5809 ist->st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
1575 4519 continue;
1576
1577 2582 score = ist->st->codecpar->ch_layout.nb_channels
1578
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 1 times.
1291 + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1579
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1120 times.
1291 + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1580
2/2
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 10 times.
1291 if (score > file_best_score) {
1581 1281 file_best_score = score;
1582 1281 file_best_ist = ist;
1583 }
1584 }
1585
2/2
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 4327 times.
5608 if (file_best_ist) {
1586
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1110 times.
1281 file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1587
1/2
✓ Branch 0 taken 1281 times.
✗ Branch 1 not taken.
1281 if (file_best_score > best_score) {
1588 1281 best_score = file_best_score;
1589 1281 best_ist = file_best_ist;
1590 }
1591 }
1592 }
1593
2/2
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 4328 times.
5609 if (best_ist)
1594 1281 return ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL, NULL);
1595
1596 4328 return 0;
1597 }
1598
1599 6608 static int map_auto_subtitle(Muxer *mux, const OptionsContext *o)
1600 {
1601 6608 AVFormatContext *oc = mux->fc;
1602 6608 const char *subtitle_codec_name = NULL;
1603
1604 /* subtitles: pick first */
1605 6608 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1606
4/4
✓ Branch 1 taken 6549 times.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 6542 times.
✓ Branch 4 taken 7 times.
6608 if (!avcodec_find_encoder(oc->oformat->subtitle_codec) && !subtitle_codec_name)
1607 6542 return 0;
1608
1609
2/2
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 18 times.
98 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
1610
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 32 times.
80 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1611 AVCodecDescriptor const *input_descriptor =
1612 48 avcodec_descriptor_get(ist->st->codecpar->codec_id);
1613 48 AVCodecDescriptor const *output_descriptor = NULL;
1614 AVCodec const *output_codec =
1615 48 avcodec_find_encoder(oc->oformat->subtitle_codec);
1616 48 int input_props = 0, output_props = 0;
1617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (ist->user_set_discard == AVDISCARD_ALL)
1618 continue;
1619
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 7 times.
48 if (output_codec)
1620 41 output_descriptor = avcodec_descriptor_get(output_codec->id);
1621
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (input_descriptor)
1622 48 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1623
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 7 times.
48 if (output_descriptor)
1624 41 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1625
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 if (subtitle_codec_name ||
1626
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
32 input_props & output_props ||
1627 // Map dvb teletext which has neither property to any output subtitle encoder
1628 input_descriptor && output_descriptor &&
1629 (!input_descriptor->props ||
1630 !output_descriptor->props)) {
1631 48 return ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL, NULL);
1632 }
1633 }
1634
1635 18 return 0;
1636 }
1637
1638 6609 static int map_auto_data(Muxer *mux, const OptionsContext *o)
1639 {
1640 6609 AVFormatContext *oc = mux->fc;
1641 /* Data only if codec id match */
1642 6609 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_DATA);
1643
1644
1/2
✓ Branch 0 taken 6609 times.
✗ Branch 1 not taken.
6609 if (codec_id == AV_CODEC_ID_NONE)
1645 6609 return 0;
1646
1647 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
1648 if (ist->user_set_discard == AVDISCARD_ALL)
1649 continue;
1650 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1651 ist->st->codecpar->codec_id == codec_id) {
1652 int ret = ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL, NULL);
1653 if (ret < 0)
1654 return ret;
1655 }
1656 }
1657
1658 return 0;
1659 }
1660
1661 293 static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map)
1662 {
1663 InputStream *ist;
1664 int ret;
1665
1666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
293 if (map->disabled)
1667 return 0;
1668
1669
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 257 times.
293 if (map->linklabel) {
1670 FilterGraph *fg;
1671 36 OutputFilter *ofilter = NULL;
1672 int j, k;
1673
1674
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 for (j = 0; j < nb_filtergraphs; j++) {
1675 36 fg = filtergraphs[j];
1676
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 for (k = 0; k < fg->nb_outputs; k++) {
1677 116 const char *linklabel = fg->outputs[k]->linklabel;
1678
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
116 if (linklabel && !strcmp(linklabel, map->linklabel)) {
1679 36 ofilter = fg->outputs[k];
1680 36 goto loop_end;
1681 }
1682 }
1683 }
1684 loop_end:
1685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!ofilter) {
1686 av_log(mux, AV_LOG_FATAL, "Output with label '%s' does not exist "
1687 "in any defined filter graph, or was already used elsewhere.\n", map->linklabel);
1688 return AVERROR(EINVAL);
1689 }
1690
1691 36 av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly "
1692 36 "mapped complex filtergraph %d, output [%s]\n", fg->index, map->linklabel);
1693
1694 36 ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL);
1695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (ret < 0)
1696 return ret;
1697 } else {
1698 257 ist = input_files[map->file_index]->streams[map->stream_index];
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
257 if (ist->user_set_discard == AVDISCARD_ALL) {
1700 av_log(mux, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n",
1701 map->file_index, map->stream_index);
1702 return AVERROR(EINVAL);
1703 }
1704
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
257 if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1705 return 0;
1706
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
257 if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1707 return 0;
1708
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
257 if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1709 return 0;
1710
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
257 if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1711 return 0;
1712
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
257 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
1714 !copy_unknown_streams) {
1715 av_log(mux, ignore_unknown_streams ? AV_LOG_WARNING : AV_LOG_FATAL,
1716 "Cannot map stream #%d:%d - unsupported type.\n",
1717 map->file_index, map->stream_index);
1718 if (!ignore_unknown_streams) {
1719 av_log(mux, AV_LOG_FATAL,
1720 "If you want unsupported types ignored instead "
1721 "of failing, please use the -ignore_unknown option\n"
1722 "If you want them copied, please use -copy_unknown\n");
1723 return AVERROR(EINVAL);
1724 }
1725 return 0;
1726 }
1727
1728 257 ret = ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL, NULL);
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257 times.
257 if (ret < 0)
1730 return ret;
1731 }
1732
1733 293 return 0;
1734 }
1735
1736 6742 static int of_add_attachments(Muxer *mux, const OptionsContext *o)
1737 {
1738 OutputStream *ost;
1739 int err;
1740
1741
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6742 times.
6743 for (int i = 0; i < o->nb_attachments; i++) {
1742 AVIOContext *pb;
1743 uint8_t *attachment;
1744 const char *p;
1745 int64_t len;
1746
1747
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
1748 av_log(mux, AV_LOG_FATAL, "Could not open attachment file %s.\n",
1749 o->attachments[i]);
1750 return err;
1751 }
1752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((len = avio_size(pb)) <= 0) {
1753 av_log(mux, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
1754 o->attachments[i]);
1755 err = len ? len : AVERROR_INVALIDDATA;
1756 goto read_fail;
1757 }
1758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
1759 av_log(mux, AV_LOG_FATAL, "Attachment %s too large.\n",
1760 o->attachments[i]);
1761 err = AVERROR(ERANGE);
1762 goto read_fail;
1763 }
1764
1765 1 attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
1766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!attachment) {
1767 err = AVERROR(ENOMEM);
1768 goto read_fail;
1769 }
1770
1771 1 err = avio_read(pb, attachment, len);
1772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1773 av_log(mux, AV_LOG_FATAL, "Error reading attachment file %s: %s\n",
1774 o->attachments[i], av_err2str(err));
1775
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (err != len) {
1776 av_log(mux, AV_LOG_FATAL, "Could not read all %"PRId64" bytes for "
1777 "attachment file %s\n", len, o->attachments[i]);
1778 err = AVERROR(EIO);
1779 }
1780
1781 1 read_fail:
1782 1 avio_closep(&pb);
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1784 return err;
1785
1786 1 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1787
1788 1 av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n",
1789 1 o->attachments[i]);
1790
1791 1 err = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL, &ost);
1792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
1793 av_freep(&attachment);
1794 return err;
1795 }
1796
1797 1 ost->attachment_filename = o->attachments[i];
1798 1 ost->par_in->extradata = attachment;
1799 1 ost->par_in->extradata_size = len;
1800
1801 1 p = strrchr(o->attachments[i], '/');
1802
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
1803 }
1804
1805 6742 return 0;
1806 }
1807
1808 6742 static int create_streams(Muxer *mux, const OptionsContext *o)
1809 {
1810 static int (* const map_func[])(Muxer *mux, const OptionsContext *o) = {
1811 [AVMEDIA_TYPE_VIDEO] = map_auto_video,
1812 [AVMEDIA_TYPE_AUDIO] = map_auto_audio,
1813 [AVMEDIA_TYPE_SUBTITLE] = map_auto_subtitle,
1814 [AVMEDIA_TYPE_DATA] = map_auto_data,
1815 };
1816
1817 6742 AVFormatContext *oc = mux->fc;
1818
1819 6742 int auto_disable =
1820 6742 o->video_disable * (1 << AVMEDIA_TYPE_VIDEO) |
1821 6742 o->audio_disable * (1 << AVMEDIA_TYPE_AUDIO) |
1822 6742 o->subtitle_disable * (1 << AVMEDIA_TYPE_SUBTITLE) |
1823 6742 o->data_disable * (1 << AVMEDIA_TYPE_DATA);
1824
1825 int ret;
1826
1827 /* create streams for all unlabeled output pads */
1828
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 6742 times.
6857 for (int i = 0; i < nb_filtergraphs; i++) {
1829 115 FilterGraph *fg = filtergraphs[i];
1830
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 115 times.
266 for (int j = 0; j < fg->nb_outputs; j++) {
1831 151 OutputFilter *ofilter = fg->outputs[j];
1832
1833
4/4
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 113 times.
151 if (ofilter->linklabel || ofilter->ost)
1834 38 continue;
1835
1836 113 auto_disable |= 1 << ofilter->type;
1837
1838 113 av_log(mux, AV_LOG_VERBOSE, "Creating output stream from unlabeled "
1839 "output of complex filtergraph %d.", fg->index);
1840
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 if (!o->nb_stream_maps)
1841 113 av_log(mux, AV_LOG_VERBOSE, " This overrides automatic %s mapping.",
1842 av_get_media_type_string(ofilter->type));
1843 113 av_log(mux, AV_LOG_VERBOSE, "\n");
1844
1845 113 ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL);
1846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (ret < 0)
1847 return ret;
1848 }
1849 }
1850
1851
2/2
✓ Branch 0 taken 6609 times.
✓ Branch 1 taken 133 times.
6742 if (!o->nb_stream_maps) {
1852 6609 av_log(mux, AV_LOG_VERBOSE, "No explicit maps, mapping streams automatically...\n");
1853
1854 /* pick the "best" stream of each type */
1855
2/2
✓ Branch 0 taken 26436 times.
✓ Branch 1 taken 6609 times.
33045 for (int i = 0; i < FF_ARRAY_ELEMS(map_func); i++) {
1856
3/4
✓ Branch 0 taken 26436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 406 times.
✓ Branch 3 taken 26030 times.
26436 if (!map_func[i] || auto_disable & (1 << i))
1857 406 continue;
1858 26030 ret = map_func[i](mux, o);
1859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26030 times.
26030 if (ret < 0)
1860 return ret;
1861 }
1862 } else {
1863 133 av_log(mux, AV_LOG_VERBOSE, "Adding streams from explicit maps...\n");
1864
1865
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 133 times.
426 for (int i = 0; i < o->nb_stream_maps; i++) {
1866 293 ret = map_manual(mux, o, &o->stream_maps[i]);
1867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
293 if (ret < 0)
1868 return ret;
1869 }
1870 }
1871
1872 6742 ret = of_add_attachments(mux, o);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6742 times.
6742 if (ret < 0)
1874 return ret;
1875
1876 // setup fix_sub_duration_heartbeat mappings
1877
2/2
✓ Branch 0 taken 7051 times.
✓ Branch 1 taken 6742 times.
13793 for (unsigned i = 0; i < oc->nb_streams; i++) {
1878 7051 MuxStream *src = ms_from_ost(mux->of.streams[i]);
1879
1880
2/2
✓ Branch 0 taken 7050 times.
✓ Branch 1 taken 1 times.
7051 if (!src->ost.fix_sub_duration_heartbeat)
1881 7050 continue;
1882
1883
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (unsigned j = 0; j < oc->nb_streams; j++) {
1884 2 MuxStream *dst = ms_from_ost(mux->of.streams[j]);
1885
1886
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 if (src == dst || dst->ost.type != AVMEDIA_TYPE_SUBTITLE ||
1887
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 !dst->ost.enc || !dst->ost.ist || !dst->ost.ist->fix_sub_duration)
1888 1 continue;
1889
1890 1 ret = sch_mux_sub_heartbeat_add(mux->sch, mux->sch_idx, src->sch_idx,
1891 1 dst->sch_idx_src);
1892
1893 }
1894 }
1895
1896
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6742 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6742 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
1897 av_dump_format(oc, nb_output_files - 1, oc->url, 1);
1898 av_log(mux, AV_LOG_ERROR, "Output file does not contain any stream\n");
1899 return AVERROR(EINVAL);
1900 }
1901
1902 6742 return 0;
1903 }
1904
1905 6742 static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_us)
1906 {
1907 6742 OutputFile *of = &mux->of;
1908 6742 int nb_av_enc = 0, nb_audio_fs = 0, nb_interleaved = 0;
1909 6742 int limit_frames = 0, limit_frames_av_enc = 0;
1910
1911 #define IS_AV_ENC(ost, type) \
1912 (ost->enc_ctx && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
1913 #define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT)
1914
1915
2/2
✓ Branch 0 taken 7051 times.
✓ Branch 1 taken 6742 times.
13793 for (int i = 0; i < oc->nb_streams; i++) {
1916 7051 OutputStream *ost = of->streams[i];
1917 7051 MuxStream *ms = ms_from_ost(ost);
1918 7051 enum AVMediaType type = ost->type;
1919
1920 7051 ms->sq_idx_mux = -1;
1921
1922 7051 nb_interleaved += IS_INTERLEAVED(type);
1923
6/6
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 1267 times.
✓ Branch 3 taken 5188 times.
✓ Branch 4 taken 1229 times.
✓ Branch 5 taken 38 times.
7051 nb_av_enc += IS_AV_ENC(ost, type);
1924
4/4
✓ Branch 0 taken 6455 times.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 1229 times.
✓ Branch 3 taken 5226 times.
8280 nb_audio_fs += (ost->enc_ctx && type == AVMEDIA_TYPE_AUDIO &&
1925
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 1091 times.
1229 !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
1926
1927 7051 limit_frames |= ms->max_frames < INT64_MAX;
1928
7/8
✓ Branch 0 taken 2671 times.
✓ Branch 1 taken 4380 times.
✓ Branch 2 taken 2652 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 2623 times.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
7051 limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type);
1929 }
1930
1931
7/8
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 6528 times.
✓ Branch 2 taken 210 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 6738 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3960 times.
✓ Branch 7 taken 118 times.
10820 if (!((nb_interleaved > 1 && of->shortest) ||
1932
2/2
✓ Branch 0 taken 4078 times.
✓ Branch 1 taken 2660 times.
6738 (nb_interleaved > 0 && limit_frames) ||
1933 nb_audio_fs))
1934 3960 return 0;
1935
1936 /* we use a sync queue before encoding when:
1937 * - 'shortest' is in effect and we have two or more encoded audio/video
1938 * streams