FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux_init.c
Date: 2026-04-21 18:58:04
Exec Total Coverage
Lines: 1353 2039 66.4%
Functions: 43 50 86.0%
Branches: 908 1568 57.9%

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 6677 static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
54 const char *opt_name, int flag)
55 {
56 6677 const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0);
57
58
2/2
✓ Branch 0 taken 5671 times.
✓ Branch 1 taken 1006 times.
6677 if (e) {
59 5671 const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0);
60 5671 int val = 0;
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5671 times.
5671 if (!o)
62 return 0;
63 5671 av_opt_eval_flags(ctx, o, e->value, &val);
64 5671 return !!(val & flag);
65 }
66 1006 return 0;
67 }
68
69 9044 static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
70 MuxStream *ms, const AVCodec **enc)
71 {
72 9044 OutputStream *ost = &ms->ost;
73 9044 enum AVMediaType type = ost->type;
74 9044 const char *codec_name = NULL;
75
76 9044 *enc = NULL;
77
78 9044 opt_match_per_stream_str(ost, &o->codec_names, s, ost->st, &codec_name);
79
80
4/4
✓ Branch 0 taken 1783 times.
✓ Branch 1 taken 7261 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 1690 times.
9044 if (type != AVMEDIA_TYPE_VIDEO &&
81
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 80 times.
93 type != AVMEDIA_TYPE_AUDIO &&
82 type != AVMEDIA_TYPE_SUBTITLE) {
83
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
13 if (codec_name && strcmp(codec_name, "copy")) {
84 const char *type_str = av_get_media_type_string(type);
85 av_log(ost, AV_LOG_FATAL,
86 "Encoder '%s' specified, but only '-codec copy' supported "
87 "for %s streams\n", codec_name, type_str);
88 return AVERROR(ENOSYS);
89 }
90 13 return 0;
91 }
92
93
2/2
✓ Branch 0 taken 4198 times.
✓ Branch 1 taken 4833 times.
9031 if (!codec_name) {
94 4198 ms->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type);
95 4198 *enc = avcodec_find_encoder(ms->par_in->codec_id);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4198 times.
4198 if (!*enc) {
97 av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
98 "Default encoder for format %s (codec %s) is "
99 "probably disabled. Please choose an encoder manually.\n",
100 s->oformat->name, avcodec_get_name(ms->par_in->codec_id));
101 return AVERROR_ENCODER_NOT_FOUND;
102 }
103
2/2
✓ Branch 0 taken 4092 times.
✓ Branch 1 taken 741 times.
4833 } else if (strcmp(codec_name, "copy")) {
104 4092 int ret = find_codec(ost, codec_name, ost->type, 1, enc);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4092 times.
4092 if (ret < 0)
106 return ret;
107 4092 ms->par_in->codec_id = (*enc)->id;
108 }
109
110 9031 return 0;
111 }
112
113 static char *get_line(AVIOContext *s, AVBPrint *bprint)
114 {
115 char c;
116
117 while ((c = avio_r8(s)) && c != '\n')
118 av_bprint_chars(bprint, c, 1);
119
120 if (!av_bprint_is_complete(bprint))
121 return NULL;
122
123 return bprint->str;
124 }
125
126 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
127 {
128 int i, ret = -1;
129 char filename[1000];
130 char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR");
131 char *env_home = getenv_utf8("HOME");
132 const char *base[3] = { env_avconv_datadir,
133 env_home,
134 AVCONV_DATADIR,
135 };
136
137 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
138 if (!base[i])
139 continue;
140 if (codec_name) {
141 snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
142 i != 1 ? "" : "/.avconv", codec_name, preset_name);
143 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
144 }
145 if (ret < 0) {
146 snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
147 i != 1 ? "" : "/.avconv", preset_name);
148 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
149 }
150 }
151 freeenv_utf8(env_home);
152 freeenv_utf8(env_avconv_datadir);
153 return ret;
154 }
155
156 typedef struct EncStatsFile {
157 char *path;
158 AVIOContext *io;
159 } EncStatsFile;
160
161 static EncStatsFile *enc_stats_files;
162 static int nb_enc_stats_files;
163
164 static int enc_stats_get_file(AVIOContext **io, const char *path)
165 {
166 EncStatsFile *esf;
167 int ret;
168
169 for (int i = 0; i < nb_enc_stats_files; i++)
170 if (!strcmp(path, enc_stats_files[i].path)) {
171 *io = enc_stats_files[i].io;
172 return 0;
173 }
174
175 ret = GROW_ARRAY(enc_stats_files, nb_enc_stats_files);
176 if (ret < 0)
177 return ret;
178
179 esf = &enc_stats_files[nb_enc_stats_files - 1];
180
181 ret = avio_open2(&esf->io, path, AVIO_FLAG_WRITE, &int_cb, NULL);
182 if (ret < 0) {
183 av_log(NULL, AV_LOG_ERROR, "Error opening stats file '%s': %s\n",
184 path, av_err2str(ret));
185 return ret;
186 }
187
188 esf->path = av_strdup(path);
189 if (!esf->path)
190 return AVERROR(ENOMEM);
191
192 *io = esf->io;
193
194 return 0;
195 }
196
197 8538 void of_enc_stats_close(void)
198 {
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8538 times.
8538 for (int i = 0; i < nb_enc_stats_files; i++) {
200 av_freep(&enc_stats_files[i].path);
201 avio_closep(&enc_stats_files[i].io);
202 }
203 8538 av_freep(&enc_stats_files);
204 8538 nb_enc_stats_files = 0;
205 8538 }
206
207 static int unescape(char **pdst, size_t *dst_len,
208 const char **pstr, char delim)
209 {
210 const char *str = *pstr;
211 char *dst;
212 size_t len, idx;
213
214 *pdst = NULL;
215
216 len = strlen(str);
217 if (!len)
218 return 0;
219
220 dst = av_malloc(len + 1);
221 if (!dst)
222 return AVERROR(ENOMEM);
223
224 for (idx = 0; *str; idx++, str++) {
225 if (str[0] == '\\' && str[1])
226 str++;
227 else if (*str == delim)
228 break;
229
230 dst[idx] = *str;
231 }
232 if (!idx) {
233 av_freep(&dst);
234 return 0;
235 }
236
237 dst[idx] = 0;
238
239 *pdst = dst;
240 *dst_len = idx;
241 *pstr = str;
242
243 return 0;
244 }
245
246 static int enc_stats_init(OutputStream *ost, EncStats *es, int pre,
247 const char *path, const char *fmt_spec)
248 {
249 static const struct {
250 enum EncStatsType type;
251 const char *str;
252 unsigned pre_only:1;
253 unsigned post_only:1;
254 unsigned need_input_data:1;
255 } fmt_specs[] = {
256 { ENC_STATS_FILE_IDX, "fidx" },
257 { ENC_STATS_STREAM_IDX, "sidx" },
258 { ENC_STATS_FRAME_NUM, "n" },
259 { ENC_STATS_FRAME_NUM_IN, "ni", 0, 0, 1 },
260 { ENC_STATS_TIMEBASE, "tb" },
261 { ENC_STATS_TIMEBASE_IN, "tbi", 0, 0, 1 },
262 { ENC_STATS_PTS, "pts" },
263 { ENC_STATS_PTS_TIME, "t" },
264 { ENC_STATS_PTS_IN, "ptsi", 0, 0, 1 },
265 { ENC_STATS_PTS_TIME_IN, "ti", 0, 0, 1 },
266 { ENC_STATS_DTS, "dts", 0, 1 },
267 { ENC_STATS_DTS_TIME, "dt", 0, 1 },
268 { ENC_STATS_SAMPLE_NUM, "sn", 1 },
269 { ENC_STATS_NB_SAMPLES, "samp", 1 },
270 { ENC_STATS_PKT_SIZE, "size", 0, 1 },
271 { ENC_STATS_BITRATE, "br", 0, 1 },
272 { ENC_STATS_AVG_BITRATE, "abr", 0, 1 },
273 { ENC_STATS_KEYFRAME, "key", 0, 1 },
274 };
275 const char *next = fmt_spec;
276
277 int ret;
278
279 while (*next) {
280 EncStatsComponent *c;
281 char *val;
282 size_t val_len;
283
284 // get the sequence up until next opening brace
285 ret = unescape(&val, &val_len, &next, '{');
286 if (ret < 0)
287 return ret;
288
289 if (val) {
290 ret = GROW_ARRAY(es->components, es->nb_components);
291 if (ret < 0) {
292 av_freep(&val);
293 return ret;
294 }
295
296 c = &es->components[es->nb_components - 1];
297 c->type = ENC_STATS_LITERAL;
298 c->str = val;
299 c->str_len = val_len;
300 }
301
302 if (!*next)
303 break;
304 next++;
305
306 // get the part inside braces
307 ret = unescape(&val, &val_len, &next, '}');
308 if (ret < 0)
309 return ret;
310
311 if (!val) {
312 av_log(NULL, AV_LOG_ERROR,
313 "Empty formatting directive in: %s\n", fmt_spec);
314 return AVERROR(EINVAL);
315 }
316
317 if (!*next) {
318 av_log(NULL, AV_LOG_ERROR,
319 "Missing closing brace in: %s\n", fmt_spec);
320 ret = AVERROR(EINVAL);
321 goto fail;
322 }
323 next++;
324
325 ret = GROW_ARRAY(es->components, es->nb_components);
326 if (ret < 0)
327 goto fail;
328
329 c = &es->components[es->nb_components - 1];
330
331 for (size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) {
332 if (!strcmp(val, fmt_specs[i].str)) {
333 if ((pre && fmt_specs[i].post_only) || (!pre && fmt_specs[i].pre_only)) {
334 av_log(NULL, AV_LOG_ERROR,
335 "Format directive '%s' may only be used %s-encoding\n",
336 val, pre ? "post" : "pre");
337 ret = AVERROR(EINVAL);
338 goto fail;
339 }
340
341 c->type = fmt_specs[i].type;
342
343 if (fmt_specs[i].need_input_data && !ost->ist) {
344 av_log(ost, AV_LOG_WARNING,
345 "Format directive '%s' is unavailable, because "
346 "this output stream has no associated input stream\n",
347 val);
348 }
349
350 break;
351 }
352 }
353
354 if (!c->type) {
355 av_log(NULL, AV_LOG_ERROR, "Invalid format directive: %s\n", val);
356 ret = AVERROR(EINVAL);
357 goto fail;
358 }
359
360 fail:
361 av_freep(&val);
362 if (ret < 0)
363 return ret;
364 }
365
366 ret = pthread_mutex_init(&es->lock, NULL);
367 if (ret)
368 return AVERROR(ret);
369 es->lock_initialized = 1;
370
371 ret = enc_stats_get_file(&es->io, path);
372 if (ret < 0)
373 return ret;
374
375 return 0;
376 }
377
378 2433 static const char *output_stream_item_name(void *obj)
379 {
380 2433 const MuxStream *ms = obj;
381
382 2433 return ms->log_name;
383 }
384
385 static const AVClass output_stream_class = {
386 .class_name = "OutputStream",
387 .version = LIBAVUTIL_VERSION_INT,
388 .item_name = output_stream_item_name,
389 .category = AV_CLASS_CATEGORY_MUXER,
390 };
391
392 9044 static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
393 {
394 9044 const char *type_str = av_get_media_type_string(type);
395 MuxStream *ms;
396
397 9044 ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (!ms)
399 return NULL;
400
401 9044 ms->ost.file = &mux->of;
402 9044 ms->ost.index = mux->of.nb_streams - 1;
403 9044 ms->ost.type = type;
404
405 9044 ms->ost.class = &output_stream_class;
406
407 9044 ms->sch_idx = -1;
408 9044 ms->sch_idx_enc = -1;
409
410
1/2
✓ Branch 0 taken 9044 times.
✗ Branch 1 not taken.
9044 snprintf(ms->log_name, sizeof(ms->log_name), "%cost#%d:%d",
411 9044 type_str ? *type_str : '?', mux->of.index, ms->ost.index);
412
413 9044 return ms;
414 }
415
416 8248 static int ost_get_filters(const OptionsContext *o, AVFormatContext *oc,
417 OutputStream *ost, char **dst)
418 {
419 8248 const char *filters = NULL;
420 #if FFMPEG_OPT_FILTER_SCRIPT
421 8248 const char *filters_script = NULL;
422
423 8248 opt_match_per_stream_str(ost, &o->filter_scripts, oc, ost->st, &filters_script);
424 #endif
425 8248 opt_match_per_stream_str(ost, &o->filters, oc, ost->st, &filters);
426
427
2/2
✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 6854 times.
8248 if (!ost->ist) {
428 1394 if (
429 #if FFMPEG_OPT_FILTER_SCRIPT
430
2/4
✓ Branch 0 taken 1394 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1394 times.
1394 filters_script ||
431 #endif
432 filters) {
433 av_log(ost, AV_LOG_ERROR,
434 "%s '%s' was specified for a stream fed from a complex "
435 "filtergraph. Simple and complex filtering cannot be used "
436 "together for the same stream.\n",
437 #if FFMPEG_OPT_FILTER_SCRIPT
438 filters ? "Filtergraph" : "Filtergraph script",
439 filters ? filters : filters_script
440 #else
441 "Filtergraph", filters
442 #endif
443 );
444 return AVERROR(EINVAL);
445 }
446 1394 return 0;
447 }
448
449 #if FFMPEG_OPT_FILTER_SCRIPT
450
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6854 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6854 if (filters_script && filters) {
451 av_log(ost, AV_LOG_ERROR, "Both -filter and -filter_script set\n");
452 return AVERROR(EINVAL);
453 }
454
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6854 times.
6854 if (filters_script)
456 *dst = read_file_to_string(filters_script);
457 else
458 #endif
459
2/2
✓ Branch 0 taken 3926 times.
✓ Branch 1 taken 2928 times.
6854 if (filters)
460 3926 *dst = av_strdup(filters);
461 else
462
2/2
✓ Branch 0 taken 2319 times.
✓ Branch 1 taken 609 times.
2928 *dst = av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull");
463
1/2
✓ Branch 0 taken 6854 times.
✗ Branch 1 not taken.
6854 return *dst ? 0 : AVERROR(ENOMEM);
464 }
465
466 static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str)
467 {
468 const char *p = str;
469 for (int i = 0;; i++) {
470 dest[i] = atoi(p);
471 if (i == 63)
472 break;
473 p = strchr(p, ',');
474 if (!p) {
475 av_log(logctx, AV_LOG_FATAL,
476 "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
477 return AVERROR(EINVAL);
478 }
479 p++;
480 }
481
482 return 0;
483 }
484
485 650 static int pixfmt_in_list(const enum AVPixelFormat *formats, enum AVPixelFormat format)
486 {
487
1/2
✓ Branch 0 taken 3710 times.
✗ Branch 1 not taken.
3710 for (; *formats != AV_PIX_FMT_NONE; formats++)
488
2/2
✓ Branch 0 taken 650 times.
✓ Branch 1 taken 3060 times.
3710 if (*formats == format)
489 650 return 1;
490 return 0;
491 }
492
493 static enum AVPixelFormat
494 choose_pixel_fmt(const AVCodecContext *avctx, enum AVPixelFormat target)
495 {
496 const enum AVPixelFormat *p;
497 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
498 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
499 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
500 enum AVPixelFormat best= AV_PIX_FMT_NONE;
501 int ret;
502
503 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
504 0, (const void **) &p, NULL);
505 if (ret < 0)
506 return AV_PIX_FMT_NONE;
507
508 for (; *p != AV_PIX_FMT_NONE; p++) {
509 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
510 if (*p == target)
511 break;
512 }
513 if (*p == AV_PIX_FMT_NONE) {
514 if (target != AV_PIX_FMT_NONE)
515 av_log(NULL, AV_LOG_WARNING,
516 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
517 av_get_pix_fmt_name(target),
518 avctx->codec->name,
519 av_get_pix_fmt_name(best));
520 return best;
521 }
522 return target;
523 }
524
525 5363 static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name)
526 {
527 const enum AVPixelFormat *fmts;
528 enum AVPixelFormat fmt;
529 int ret;
530
531 5363 fmt = av_get_pix_fmt(name);
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5363 times.
5363 if (fmt == AV_PIX_FMT_NONE) {
533 av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", name);
534 return AV_PIX_FMT_NONE;
535 }
536
537 5363 ret = avcodec_get_supported_config(ost->enc->enc_ctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
538 0, (const void **) &fmts, NULL);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5363 times.
5363 if (ret < 0)
540 return AV_PIX_FMT_NONE;
541
542 /* when the user specified-format is an alias for an endianness-specific
543 * one (e.g. rgb48 -> rgb48be/le), it gets translated into the native
544 * endianness by av_get_pix_fmt();
545 * the following code handles the case when the native endianness is not
546 * supported by the encoder, but the other one is */
547
3/4
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 5038 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 325 times.
5363 if (fmts && !pixfmt_in_list(fmts, fmt)) {
548 const char *name_canonical = av_get_pix_fmt_name(fmt);
549 int len = strlen(name_canonical);
550
551 if (strcmp(name, name_canonical) &&
552 (!strcmp(name_canonical + len - 2, "le") ||
553 !strcmp(name_canonical + len - 2, "be"))) {
554 char name_other[64];
555 enum AVPixelFormat fmt_other;
556
557 snprintf(name_other, sizeof(name_other), "%s%ce",
558 name, name_canonical[len - 2] == 'l' ? 'b' : 'l');
559 fmt_other = av_get_pix_fmt(name_other);
560 if (fmt_other != AV_PIX_FMT_NONE && pixfmt_in_list(fmts, fmt_other)) {
561 av_log(ost, AV_LOG_VERBOSE, "Mapping pixel format %s->%s\n",
562 name, name_other);
563 fmt = fmt_other;
564 }
565 }
566 }
567
568
3/4
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 5038 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 325 times.
5363 if (fmts && !pixfmt_in_list(fmts, fmt))
569 fmt = choose_pixel_fmt(ost->enc->enc_ctx, fmt);
570
571 5363 return fmt;
572 }
573
574 7261 static int new_stream_video(Muxer *mux, const OptionsContext *o,
575 OutputStream *ost, int *keep_pix_fmt,
576 enum VideoSyncMethod *vsync_method)
577 {
578 7261 MuxStream *ms = ms_from_ost(ost);
579 7261 AVFormatContext *oc = mux->fc;
580 AVStream *st;
581 7261 const char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
582 7261 int ret = 0;
583
584 7261 st = ost->st;
585
586 7261 opt_match_per_stream_str(ost, &o->frame_rates, oc, st, &frame_rate);
587
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7239 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
7261 if (frame_rate && av_parse_video_rate(&ms->frame_rate, frame_rate) < 0) {
588 av_log(ost, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
589 return AVERROR(EINVAL);
590 }
591
592 7261 opt_match_per_stream_str(ost, &o->max_frame_rates, oc, st, &max_frame_rate);
593
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7261 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7261 if (max_frame_rate && av_parse_video_rate(&ms->max_frame_rate, max_frame_rate) < 0) {
594 av_log(ost, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
595 return AVERROR(EINVAL);
596 }
597
598
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7239 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
7261 if (frame_rate && max_frame_rate) {
599 av_log(ost, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
600 return AVERROR(EINVAL);
601 }
602
603 7261 opt_match_per_stream_str(ost, &o->frame_aspect_ratios, oc, st, &frame_aspect_ratio);
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7261 times.
7261 if (frame_aspect_ratio) {
605 AVRational q;
606 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
607 q.num <= 0 || q.den <= 0) {
608 av_log(ost, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
609 return AVERROR(EINVAL);
610 }
611 ost->frame_aspect_ratio = q;
612 }
613
614
2/2
✓ Branch 0 taken 6877 times.
✓ Branch 1 taken 384 times.
7261 if (ost->enc) {
615 6877 AVCodecContext *video_enc = ost->enc->enc_ctx;
616 6877 const char *p = NULL, *fps_mode = NULL;
617 6877 const char *frame_size = NULL;
618 6877 const char *frame_pix_fmt = NULL;
619 6877 const char *intra_matrix = NULL, *inter_matrix = NULL;
620 6877 const char *chroma_intra_matrix = NULL;
621 6877 int do_pass = 0;
622 int i;
623
624 6877 opt_match_per_stream_str(ost, &o->frame_sizes, oc, st, &frame_size);
625
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 5527 times.
6877 if (frame_size) {
626 1350 ret = av_parse_video_size(&video_enc->width, &video_enc->height, frame_size);
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1350 times.
1350 if (ret < 0) {
628 av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
629 return AVERROR(EINVAL);
630 }
631 }
632
633 6877 opt_match_per_stream_str(ost, &o->frame_pix_fmts, oc, st, &frame_pix_fmt);
634
3/4
✓ Branch 0 taken 5363 times.
✓ Branch 1 taken 1514 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5363 times.
6877 if (frame_pix_fmt && *frame_pix_fmt == '+') {
635 *keep_pix_fmt = 1;
636 if (!*++frame_pix_fmt)
637 frame_pix_fmt = NULL;
638 }
639
2/2
✓ Branch 0 taken 5363 times.
✓ Branch 1 taken 1514 times.
6877 if (frame_pix_fmt) {
640 5363 video_enc->pix_fmt = pix_fmt_parse(ost, frame_pix_fmt);
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5363 times.
5363 if (video_enc->pix_fmt == AV_PIX_FMT_NONE)
642 return AVERROR(EINVAL);
643 }
644
645 6877 opt_match_per_stream_str(ost, &o->intra_matrices, oc, st, &intra_matrix);
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (intra_matrix) {
647 if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64)))
648 return AVERROR(ENOMEM);
649
650 ret = parse_matrix_coeffs(ost, video_enc->intra_matrix, intra_matrix);
651 if (ret < 0)
652 return ret;
653 }
654 6877 opt_match_per_stream_str(ost, &o->chroma_intra_matrices, oc, st, &chroma_intra_matrix);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (chroma_intra_matrix) {
656 if (!(video_enc->chroma_intra_matrix = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64)))
657 return AVERROR(ENOMEM);
658 ret = parse_matrix_coeffs(ost, video_enc->chroma_intra_matrix, chroma_intra_matrix);
659 if (ret < 0)
660 return ret;
661 }
662 6877 opt_match_per_stream_str(ost, &o->inter_matrices, oc, st, &inter_matrix);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (inter_matrix) {
664 if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64)))
665 return AVERROR(ENOMEM);
666 ret = parse_matrix_coeffs(ost, video_enc->inter_matrix, inter_matrix);
667 if (ret < 0)
668 return ret;
669 }
670
671 6877 opt_match_per_stream_str(ost, &o->rc_overrides, oc, st, &p);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 for (i = 0; p; i++) {
673 int start, end, q;
674 int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
675 if (e != 3) {
676 av_log(ost, AV_LOG_FATAL, "error parsing rc_override\n");
677 return AVERROR(EINVAL);
678 }
679 video_enc->rc_override =
680 av_realloc_array(video_enc->rc_override,
681 i + 1, sizeof(RcOverride));
682 if (!video_enc->rc_override) {
683 av_log(ost, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
684 return AVERROR(ENOMEM);
685 }
686 video_enc->rc_override[i].start_frame = start;
687 video_enc->rc_override[i].end_frame = end;
688 if (q > 0) {
689 video_enc->rc_override[i].qscale = q;
690 video_enc->rc_override[i].quality_factor = 1.0;
691 }
692 else {
693 video_enc->rc_override[i].qscale = 0;
694 video_enc->rc_override[i].quality_factor = -q/100.0;
695 }
696 p = strchr(p, '/');
697 if (p) p++;
698 }
699 6877 video_enc->rc_override_count = i;
700
701 /* two pass mode */
702 6877 opt_match_per_stream_int(ost, &o->pass, oc, st, &do_pass);
703
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6861 times.
6877 if (do_pass) {
704
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (do_pass & 1)
705 8 video_enc->flags |= AV_CODEC_FLAG_PASS1;
706
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (do_pass & 2)
707 8 video_enc->flags |= AV_CODEC_FLAG_PASS2;
708 }
709
710 6877 opt_match_per_stream_str(ost, &o->passlogfiles, oc, st, &ost->logfile_prefix);
711
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6861 times.
6877 if (ost->logfile_prefix &&
712
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
713 return AVERROR(ENOMEM);
714
715
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6861 times.
6877 if (do_pass) {
716 16 int ost_idx = -1;
717 char logfilename[1024];
718 FILE *f;
719
720 /* compute this stream's global index */
721
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 for (int idx = 0; idx <= ost->file->index; idx++)
722 16 ost_idx += output_files[idx]->nb_streams;
723
724 16 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
725
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 ost->logfile_prefix ? ost->logfile_prefix :
726 DEFAULT_PASS_LOGFILENAME_PREFIX,
727 ost_idx);
728
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (!strcmp(video_enc->codec->name, "libx264") || !strcmp(video_enc->codec->name, "libvvenc")) {
729 if (av_opt_is_set_to_default_by_name(video_enc, "stats",
730 AV_OPT_SEARCH_CHILDREN) > 0)
731 av_opt_set(video_enc, "stats", logfilename,
732 AV_OPT_SEARCH_CHILDREN);
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (!strcmp(video_enc->codec->name, "libx265")) {
734 if (av_opt_is_set_to_default_by_name(video_enc, "x265-stats",
735 AV_OPT_SEARCH_CHILDREN) > 0)
736 av_opt_set(video_enc, "x265-stats", logfilename,
737 AV_OPT_SEARCH_CHILDREN);
738 } else {
739
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
740 8 char *logbuffer = read_file_to_string(logfilename);
741
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!logbuffer) {
743 av_log(ost, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
744 logfilename);
745 return AVERROR(EIO);
746 }
747 8 video_enc->stats_in = logbuffer;
748 }
749
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
750 8 f = fopen_utf8(logfilename, "wb");
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!f) {
752 av_log(ost, AV_LOG_FATAL,
753 "Cannot write log file '%s' for pass-1 encoding: %s\n",
754 logfilename, strerror(errno));
755 return AVERROR(errno);
756 }
757 8 ost->logfile = f;
758 }
759 }
760 }
761
762 6877 opt_match_per_stream_int(ost, &o->force_fps, oc, st, &ms->force_fps);
763
764 #if FFMPEG_OPT_TOP
765 6877 ost->top_field_first = -1;
766 6877 opt_match_per_stream_int(ost, &o->top_field_first, oc, st, &ost->top_field_first);
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (ost->top_field_first >= 0)
768 av_log(ost, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
769 #endif
770
771 #if FFMPEG_OPT_VSYNC
772 6877 *vsync_method = video_sync_method;
773 #else
774 *vsync_method = VSYNC_AUTO;
775 #endif
776 6877 opt_match_per_stream_str(ost, &o->fps_mode, oc, st, &fps_mode);
777
2/2
✓ Branch 0 taken 533 times.
✓ Branch 1 taken 6344 times.
6877 if (fps_mode) {
778 533 ret = parse_and_set_vsync(fps_mode, vsync_method, ost->file->index, ost->index, 0);
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533 times.
533 if (ret < 0)
780 return ret;
781 }
782
783
3/4
✓ Branch 0 taken 6856 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6856 times.
6877 if ((ms->frame_rate.num || ms->max_frame_rate.num) &&
784
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 !(*vsync_method == VSYNC_AUTO ||
785
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 *vsync_method == VSYNC_CFR || *vsync_method == VSYNC_VSCFR)) {
786 av_log(ost, AV_LOG_FATAL, "One of -r/-fpsmax was specified "
787 "together a non-CFR -vsync/-fps_mode. This is contradictory.\n");
788 return AVERROR(EINVAL);
789 }
790
791
2/2
✓ Branch 0 taken 6344 times.
✓ Branch 1 taken 533 times.
6877 if (*vsync_method == VSYNC_AUTO) {
792
3/4
✓ Branch 0 taken 6324 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6324 times.
6344 if (ms->frame_rate.num || ms->max_frame_rate.num) {
793 20 *vsync_method = VSYNC_CFR;
794
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 6024 times.
6324 } else if (!strcmp(oc->oformat->name, "avi")) {
795 300 *vsync_method = VSYNC_VFR;
796 } else {
797 6024 *vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
798 4534 ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
799
4/4
✓ Branch 0 taken 4534 times.
✓ Branch 1 taken 1490 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 4517 times.
6024 VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR;
800 }
801
802
4/4
✓ Branch 0 taken 5119 times.
✓ Branch 1 taken 1225 times.
✓ Branch 2 taken 405 times.
✓ Branch 3 taken 4714 times.
6344 if (ost->ist && *vsync_method == VSYNC_CFR) {
803 405 const InputFile *ifile = ost->ist->file;
804
805
3/4
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 360 times.
✗ Branch 3 not taken.
405 if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0)
806 360 *vsync_method = VSYNC_VSCFR;
807 }
808
809
3/4
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 5194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1150 times.
6344 if (*vsync_method == VSYNC_CFR && copy_ts) {
810 *vsync_method = VSYNC_VSCFR;
811 }
812 }
813 #if FFMPEG_OPT_VSYNC_DROP
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (*vsync_method == VSYNC_DROP)
815 ms->ts_drop = 1;
816 #endif
817 }
818
819 7261 return 0;
820 }
821
822 1690 static int new_stream_audio(Muxer *mux, const OptionsContext *o,
823 OutputStream *ost)
824 {
825 1690 MuxStream *ms = ms_from_ost(ost);
826 1690 AVFormatContext *oc = mux->fc;
827 1690 AVStream *st = ost->st;
828
829
2/2
✓ Branch 0 taken 1371 times.
✓ Branch 1 taken 319 times.
1690 if (ost->enc) {
830 1371 AVCodecContext *audio_enc = ost->enc->enc_ctx;
831 1371 int channels = 0;
832 1371 const char *layout = NULL;
833 1371 const char *sample_fmt = NULL;
834
835 1371 opt_match_per_stream_int(ost, &o->audio_channels, oc, st, &channels);
836
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1361 times.
1371 if (channels) {
837 10 audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
838 10 audio_enc->ch_layout.nb_channels = channels;
839 }
840
841 1371 opt_match_per_stream_str(ost, &o->audio_ch_layouts, oc, st, &layout);
842
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1370 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1371 if (layout && av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
843 av_log(ost, AV_LOG_FATAL, "Unknown channel layout: %s\n", layout);
844 return AVERROR(EINVAL);
845 }
846
847 1371 opt_match_per_stream_str(ost, &o->sample_fmts, oc, st, &sample_fmt);
848
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1370 times.
1371 if (sample_fmt &&
849
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
850 av_log(ost, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
851 return AVERROR(EINVAL);
852 }
853
854 1371 opt_match_per_stream_int(ost, &o->audio_sample_rate, oc, st, &audio_enc->sample_rate);
855 1371 opt_match_per_stream_str(ost, &o->apad, oc, st, &ms->apad);
856 }
857
858 1690 return 0;
859 }
860
861 80 static int new_stream_subtitle(Muxer *mux, const OptionsContext *o,
862 OutputStream *ost)
863 {
864 AVStream *st;
865
866 80 st = ost->st;
867
868
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 38 times.
80 if (ost->enc) {
869 42 AVCodecContext *subtitle_enc = ost->enc->enc_ctx;
870
871 AVCodecDescriptor const *input_descriptor =
872 42 avcodec_descriptor_get(ost->ist->par->codec_id);
873 AVCodecDescriptor const *output_descriptor =
874 42 avcodec_descriptor_get(subtitle_enc->codec_id);
875 42 int input_props = 0, output_props = 0;
876
877 42 const char *frame_size = NULL;
878
879 42 opt_match_per_stream_str(ost, &o->frame_sizes, mux->fc, st, &frame_size);
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (frame_size) {
881 int ret = av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size);
882 if (ret < 0) {
883 av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
884 return ret;
885 }
886 }
887
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (input_descriptor)
888 42 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
889
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (output_descriptor)
890 42 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
891
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
42 if (input_props && output_props && input_props != output_props) {
892 av_log(ost, AV_LOG_ERROR,
893 "Subtitle encoding currently only possible from text to text "
894 "or bitmap to bitmap\n");
895 return AVERROR(EINVAL);
896 }
897 }
898
899 80 return 0;
900 }
901
902 static int
903 8248 ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter,
904 const OptionsContext *o,
905 AVRational enc_tb, enum VideoSyncMethod vsync_method,
906 int keep_pix_fmt, int autoscale, int threads_manual,
907 const ViewSpecifier *vs,
908 SchedulerNode *src)
909 {
910 8248 OutputStream *ost = &ms->ost;
911 8248 AVCodecContext *enc_ctx = ost->enc->enc_ctx;
912 char name[16];
913 8248 char *filters = NULL;
914 int ret;
915
916 32992 OutputFilterOptions opts = {
917 8248 .enc = enc_ctx->codec,
918 .name = name,
919 8248 .format = (ost->type == AVMEDIA_TYPE_VIDEO) ?
920 8248 enc_ctx->pix_fmt : enc_ctx->sample_fmt,
921 8248 .width = enc_ctx->width,
922 8248 .height = enc_ctx->height,
923 8248 .color_space = enc_ctx->colorspace,
924 8248 .color_range = enc_ctx->color_range,
925 8248 .alpha_mode = enc_ctx->alpha_mode,
926 .vsync_method = vsync_method,
927 .frame_rate = ms->frame_rate,
928 .max_frame_rate = ms->max_frame_rate,
929 8248 .sample_rate = enc_ctx->sample_rate,
930 .ch_layout = enc_ctx->ch_layout,
931 8248 .sws_opts = o->g->sws_dict,
932 8248 .swr_opts = o->g->swr_opts,
933 .output_tb = enc_tb,
934 8248 .trim_start_us = mux->of.start_time,
935 8248 .trim_duration_us = mux->of.recording_time,
936 8248 .ts_offset = mux->of.start_time == AV_NOPTS_VALUE ?
937
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8247 times.
8248 0 : mux->of.start_time,
938 .vs = vs,
939 .nb_threads = -1,
940
941 16496 .flags = OFILTER_FLAG_DISABLE_CONVERT * !!keep_pix_fmt |
942
3/4
✓ Branch 0 taken 8248 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6877 times.
✓ Branch 3 taken 1371 times.
16496 OFILTER_FLAG_AUTOSCALE * !!autoscale |
943
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 8188 times.
8248 OFILTER_FLAG_AUDIO_24BIT * !!(av_get_exact_bits_per_sample(enc_ctx->codec_id) == 24),
944 };
945
946 8248 snprintf(name, sizeof(name), "#%d:%d", mux->of.index, ost->index);
947
948
2/2
✓ Branch 0 taken 6877 times.
✓ Branch 1 taken 1371 times.
8248 if (ost->type == AVMEDIA_TYPE_VIDEO) {
949
1/2
✓ Branch 0 taken 6877 times.
✗ Branch 1 not taken.
6877 if (!keep_pix_fmt) {
950 6877 ret = avcodec_get_supported_config(enc_ctx, NULL,
951 AV_CODEC_CONFIG_PIX_FORMAT, 0,
952 (const void **) &opts.pix_fmts, NULL);
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (ret < 0)
954 return ret;
955 }
956
2/2
✓ Branch 0 taken 6876 times.
✓ Branch 1 taken 1 times.
6877 if (!ms->force_fps) {
957 6876 ret = avcodec_get_supported_config(enc_ctx, NULL,
958 AV_CODEC_CONFIG_FRAME_RATE, 0,
959 (const void **) &opts.frame_rates, NULL);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6876 times.
6876 if (ret < 0)
961 return ret;
962 }
963 6877 ret = avcodec_get_supported_config(enc_ctx, NULL,
964 AV_CODEC_CONFIG_COLOR_SPACE, 0,
965 (const void **) &opts.color_spaces, NULL);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (ret < 0)
967 return ret;
968 6877 ret = avcodec_get_supported_config(enc_ctx, NULL,
969 AV_CODEC_CONFIG_COLOR_RANGE, 0,
970 (const void **) &opts.color_ranges, NULL);
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (ret < 0)
972 return ret;
973 6877 ret = avcodec_get_supported_config(enc_ctx, NULL,
974 AV_CODEC_CONFIG_ALPHA_MODE, 0,
975 (const void **) &opts.alpha_modes, NULL);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6877 times.
6877 if (ret < 0)
977 return ret;
978 } else {
979 1371 ret = avcodec_get_supported_config(enc_ctx, NULL,
980 AV_CODEC_CONFIG_SAMPLE_FORMAT, 0,
981 (const void **) &opts.sample_fmts, NULL);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1371 times.
1371 if (ret < 0)
983 return ret;
984 1371 ret = avcodec_get_supported_config(enc_ctx, NULL,
985 AV_CODEC_CONFIG_SAMPLE_RATE, 0,
986 (const void **) &opts.sample_rates, NULL);
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1371 times.
1371 if (ret < 0)
988 return ret;
989 1371 ret = avcodec_get_supported_config(enc_ctx, NULL,
990 AV_CODEC_CONFIG_CHANNEL_LAYOUT, 0,
991 (const void **) &opts.ch_layouts, NULL);
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1371 times.
1371 if (ret < 0)
993 return ret;
994 }
995
996
2/2
✓ Branch 0 taken 5484 times.
✓ Branch 1 taken 2764 times.
8248 if (threads_manual) {
997 5484 ret = av_opt_get_int(enc_ctx, "threads", 0, &opts.nb_threads);
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5484 times.
5484 if (ret < 0)
999 return ret;
1000 }
1001
1002 8248 ret = ost_get_filters(o, mux->fc, ost, &filters);
1003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
8248 if (ret < 0)
1004 return ret;
1005
1006
2/2
✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 6854 times.
8248 if (ofilter) {
1007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
1394 av_assert0(!filters);
1008 1394 ost->filter = ofilter;
1009 1394 ret = ofilter_bind_enc(ofilter, ms->sch_idx_enc, &opts);
1010 } else {
1011 6854 ret = fg_create_simple(&ost->fg_simple, ost->ist, &filters,
1012 6854 mux->sch, ms->sch_idx_enc, &opts);
1013
1/2
✓ Branch 0 taken 6854 times.
✗ Branch 1 not taken.
6854 if (ret >= 0)
1014 6854 ost->filter = ost->fg_simple->outputs[0];
1015
1016 }
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
8248 if (ret < 0)
1018 return ret;
1019
1020 8248 *src = SCH_ENC(ms->sch_idx_enc);
1021
1022 8248 return 0;
1023 }
1024
1025 753 static int streamcopy_init(const OptionsContext *o, const Muxer *mux,
1026 OutputStream *ost, AVDictionary **encoder_opts)
1027 {
1028 753 MuxStream *ms = ms_from_ost(ost);
1029
1030 753 const InputStream *ist = ost->ist;
1031 753 const InputFile *ifile = ist->file;
1032
1033 753 AVCodecParameters *par = ms->par_in;
1034 753 uint32_t codec_tag = par->codec_tag;
1035
1036 753 AVCodecContext *codec_ctx = NULL;
1037
1038 753 AVRational fr = ms->frame_rate;
1039
1040 753 int ret = 0;
1041
1042 753 const char *filters = NULL;
1043 #if FFMPEG_OPT_FILTER_SCRIPT
1044 753 const char *filters_script = NULL;
1045
1046 753 opt_match_per_stream_str(ost, &o->filter_scripts, mux->fc, ost->st, &filters_script);
1047 #endif
1048 753 opt_match_per_stream_str(ost, &o->filters, mux->fc, ost->st, &filters);
1049
1050 753 if (
1051 #if FFMPEG_OPT_FILTER_SCRIPT
1052
2/4
✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 753 times.
753 filters_script ||
1053 #endif
1054 filters) {
1055 av_log(ost, AV_LOG_ERROR,
1056 "%s '%s' was specified, but codec copy was selected. "
1057 "Filtering and streamcopy cannot be used together.\n",
1058 #if FFMPEG_OPT_FILTER_SCRIPT
1059 filters ? "Filtergraph" : "Filtergraph script",
1060 filters ? filters : filters_script
1061 #else
1062 "Filtergraph", filters
1063 #endif
1064 );
1065 return AVERROR(EINVAL);
1066 }
1067
1068 753 codec_ctx = avcodec_alloc_context3(NULL);
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (!codec_ctx)
1070 return AVERROR(ENOMEM);
1071
1072 753 ret = avcodec_parameters_to_context(codec_ctx, ist->par);
1073
1/2
✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
753 if (ret >= 0)
1074 753 ret = av_opt_set_dict(codec_ctx, encoder_opts);
1075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (ret < 0) {
1076 av_log(ost, AV_LOG_FATAL,
1077 "Error setting up codec context options.\n");
1078 goto fail;
1079 }
1080
1081 753 ret = avcodec_parameters_from_context(par, codec_ctx);
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (ret < 0) {
1083 av_log(ost, AV_LOG_FATAL,
1084 "Error getting reference codec parameters.\n");
1085 goto fail;
1086 }
1087
1088
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 7 times.
753 if (!codec_tag) {
1089 746 const struct AVCodecTag * const *ct = mux->fc->oformat->codec_tag;
1090 unsigned int codec_tag_tmp;
1091
6/6
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 601 times.
✓ Branch 3 taken 110 times.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 96 times.
856 if (!ct || av_codec_get_id (ct, par->codec_tag) == par->codec_id ||
1092 110 !av_codec_get_tag2(ct, par->codec_id, &codec_tag_tmp))
1093 650 codec_tag = par->codec_tag;
1094 }
1095
1096 753 par->codec_tag = codec_tag;
1097
1098
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 1 times.
753 if (!fr.num)
1099 752 fr = ist->framerate;
1100
1101
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 751 times.
753 if (fr.num)
1102 2 ost->st->avg_frame_rate = fr;
1103 else
1104 751 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
1105
1106 // copy timebase while removing common factors
1107
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
753 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
1108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 751 times.
753 if (fr.num)
1109 2 ost->st->time_base = av_inv_q(fr);
1110 else
1111 751 ost->st->time_base = av_add_q(ist->st->time_base, (AVRational){0, 1});
1112 }
1113
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (!ms->copy_prior_start) {
1115 ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
1116 0 : mux->of.start_time;
1117 if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
1118 ms->ts_copy_start = FFMAX(ms->ts_copy_start,
1119 ifile->start_time + ifile->ts_offset);
1120 }
1121 }
1122
1123
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 753 times.
836 for (int i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
1124 83 const AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
1125 AVPacketSideData *sd_dst;
1126
1127 83 sd_dst = av_packet_side_data_new(&ost->st->codecpar->coded_side_data,
1128 83 &ost->st->codecpar->nb_coded_side_data,
1129 83 sd_src->type, sd_src->size, 0);
1130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!sd_dst) {
1131 ret = AVERROR(ENOMEM);
1132 goto fail;
1133 }
1134 83 memcpy(sd_dst->data, sd_src->data, sd_src->size);
1135 }
1136
1137
3/3
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 50 times.
753 switch (par->codec_type) {
1138 319 case AVMEDIA_TYPE_AUDIO:
1139
4/6
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 314 times.
319 if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) &&
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 par->codec_id == AV_CODEC_ID_MP3)
1141 par->block_align = 0;
1142
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 303 times.
319 if (par->codec_id == AV_CODEC_ID_AC3)
1143 16 par->block_align = 0;
1144 319 break;
1145 384 case AVMEDIA_TYPE_VIDEO: {
1146 AVRational sar;
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384 times.
384 if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
1148 sar =
1149 av_mul_q(ost->frame_aspect_ratio,
1150 (AVRational){ par->height, par->width });
1151 av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio "
1152 "with stream copy may produce invalid files\n");
1153 }
1154
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 288 times.
384 else if (ist->st->sample_aspect_ratio.num)
1155 96 sar = ist->st->sample_aspect_ratio;
1156 else
1157 288 sar = par->sample_aspect_ratio;
1158 384 ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar;
1159 384 ost->st->r_frame_rate = ist->st->r_frame_rate;
1160 384 break;
1161 }
1162 }
1163
1164 753 fail:
1165 753 avcodec_free_context(&codec_ctx);
1166 753 return ret;
1167 }
1168
1169 8290 static int set_encoder_id(OutputStream *ost, const AVCodec *codec)
1170 {
1171 8290 const char *cname = codec->name;
1172 uint8_t *encoder_string;
1173 int encoder_string_len;
1174
1175 8290 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
1176 8290 encoder_string = av_mallocz(encoder_string_len);
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (!encoder_string)
1178 return AVERROR(ENOMEM);
1179
1180
4/4
✓ Branch 0 taken 869 times.
✓ Branch 1 taken 7421 times.
✓ Branch 2 taken 862 times.
✓ Branch 3 taken 7 times.
8290 if (!ost->file->bitexact && !ost->bitexact)
1181 862 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
1182 else
1183 7428 av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
1184 8290 av_strlcat(encoder_string, cname, encoder_string_len);
1185 8290 av_dict_set(&ost->st->metadata, "encoder", encoder_string,
1186 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
1187
1188 8290 return 0;
1189 }
1190
1191 9044 static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
1192 InputStream *ist, OutputFilter *ofilter, const ViewSpecifier *vs,
1193 OutputStream **post)
1194 {
1195 9044 AVFormatContext *oc = mux->fc;
1196 MuxStream *ms;
1197 OutputStream *ost;
1198 const AVCodec *enc;
1199 AVStream *st;
1200 9044 SchedulerNode src = { .type = SCH_NODE_TYPE_NONE };
1201 9044 AVDictionary *encoder_opts = NULL;
1202 9044 int ret = 0, keep_pix_fmt = 0, autoscale = 1;
1203 9044 int threads_manual = 0;
1204 9044 AVRational enc_tb = { 0, 0 };
1205 9044 enum VideoSyncMethod vsync_method = VSYNC_AUTO;
1206 9044 const char *bsfs = NULL, *time_base = NULL, *codec_tag = NULL;
1207 char *next;
1208 9044 double qscale = -1;
1209
1210 9044 st = avformat_new_stream(oc, NULL);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (!st)
1212 return AVERROR(ENOMEM);
1213
1214 9044 ms = mux_stream_alloc(mux, type);
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (!ms)
1216 return AVERROR(ENOMEM);
1217
1218 // only streams with sources (i.e. not attachments)
1219 // are handled by the scheduler
1220
4/4
✓ Branch 0 taken 1395 times.
✓ Branch 1 taken 7649 times.
✓ Branch 2 taken 1394 times.
✓ Branch 3 taken 1 times.
9044 if (ist || ofilter) {
1221 9043 ret = GROW_ARRAY(mux->sch_stream_idx, mux->nb_sch_stream_idx);
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9043 times.
9043 if (ret < 0)
1223 return ret;
1224
1225 9043 ret = sch_add_mux_stream(mux->sch, mux->sch_idx);
1226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9043 times.
9043 if (ret < 0)
1227 return ret;
1228
1229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9043 times.
9043 av_assert0(ret == mux->nb_sch_stream_idx - 1);
1230 9043 mux->sch_stream_idx[ret] = ms->ost.index;
1231 9043 ms->sch_idx = ret;
1232 }
1233
1234 9044 ost = &ms->ost;
1235
1236
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 8977 times.
9044 if (o->streamid) {
1237 AVDictionaryEntry *e;
1238 char idx[16], *p;
1239 67 snprintf(idx, sizeof(idx), "%d", ost->index);
1240
1241 67 e = av_dict_get(o->streamid, idx, NULL, 0);
1242
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 if (e) {
1243 67 st->id = strtol(e->value, &p, 0);
1244
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
67 if (!e->value[0] || *p) {
1245 av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value);
1246 return AVERROR(EINVAL);
1247 }
1248 }
1249 }
1250
1251 9044 ms->par_in = avcodec_parameters_alloc();
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (!ms->par_in)
1253 return AVERROR(ENOMEM);
1254
1255 9044 ms->last_mux_dts = AV_NOPTS_VALUE;
1256
1257 9044 ost->st = st;
1258 9044 ost->ist = ist;
1259 9044 ost->kf.ref_pts = AV_NOPTS_VALUE;
1260 9044 ms->par_in->codec_type = type;
1261 9044 st->codecpar->codec_type = type;
1262
1263 9044 ret = choose_encoder(o, oc, ms, &enc);
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (ret < 0) {
1265 av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
1266 return ret;
1267 }
1268
1269
2/2
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
9044 if (enc) {
1270 8290 ret = sch_add_enc(mux->sch, encoder_thread, ost,
1271
2/2
✓ Branch 0 taken 8248 times.
✓ Branch 1 taken 42 times.
8290 ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open);
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0)
1273 return ret;
1274 8290 ms->sch_idx_enc = ret;
1275
1276 8290 ret = enc_alloc(&ost->enc, enc, mux->sch, ms->sch_idx_enc, ost);
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0)
1278 return ret;
1279
1280 8290 av_strlcat(ms->log_name, "/", sizeof(ms->log_name));
1281 8290 av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
1282 } else {
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 754 times.
754 if (ofilter) {
1284 av_log(ost, AV_LOG_ERROR,
1285 "Streamcopy requested for output stream fed "
1286 "from a complex filtergraph. Filtering and streamcopy "
1287 "cannot be used together.\n");
1288 return AVERROR(EINVAL);
1289 }
1290
1291 754 av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name));
1292 }
1293
1294 9044 av_log(ost, AV_LOG_VERBOSE, "Created %s stream from ",
1295 av_get_media_type_string(type));
1296
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 1395 times.
9044 if (ist)
1297 7649 av_log(ost, AV_LOG_VERBOSE, "input stream %d:%d",
1298 7649 ist->file->index, ist->index);
1299
2/2
✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 1 times.
1395 else if (ofilter)
1300 1394 av_log(ost, AV_LOG_VERBOSE, "complex filtergraph %d:[%s]\n",
1301 1394 ofilter->graph->index, ofilter->name);
1302
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (type == AVMEDIA_TYPE_ATTACHMENT)
1303 1 av_log(ost, AV_LOG_VERBOSE, "attached file");
1304 else av_assert0(0);
1305 9044 av_log(ost, AV_LOG_VERBOSE, "\n");
1306
1307 9044 ms->pkt = av_packet_alloc();
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (!ms->pkt)
1309 return AVERROR(ENOMEM);
1310
1311
2/2
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
9044 if (ost->enc) {
1312 8290 AVIOContext *s = NULL;
1313 8290 char *buf = NULL, *arg = NULL;
1314 8290 const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
1315 8290 const char *enc_time_base = NULL, *preset = NULL;
1316
1317 8290 ret = filter_codec_opts(o->g->codec_opts, enc->id,
1318 oc, st, enc, &encoder_opts,
1319 &mux->enc_opts_used);
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0)
1321 goto fail;
1322
1323 8290 opt_match_per_stream_str(ost, &o->presets, oc, st, &preset);
1324 8290 opt_match_per_stream_int(ost, &o->autoscale, oc, st, &autoscale);
1325
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8290 if (preset && (!(ret = get_preset_file_2(preset, enc->name, &s)))) {
1326 AVBPrint bprint;
1327 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1328 do {
1329 av_bprint_clear(&bprint);
1330 buf = get_line(s, &bprint);
1331 if (!buf) {
1332 ret = AVERROR(ENOMEM);
1333 break;
1334 }
1335
1336 if (!buf[0] || buf[0] == '#')
1337 continue;
1338 if (!(arg = strchr(buf, '='))) {
1339 av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
1340 ret = AVERROR(EINVAL);
1341 break;
1342 }
1343 *arg++ = 0;
1344 av_dict_set(&encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
1345 } while (!s->eof_reached);
1346 av_bprint_finalize(&bprint, NULL);
1347 avio_closep(&s);
1348 }
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret) {
1350 av_log(ost, AV_LOG_FATAL,
1351 "Preset %s specified, but could not be opened.\n", preset);
1352 goto fail;
1353 }
1354
1355 8290 opt_match_per_stream_str(ost, &o->enc_stats_pre, oc, st, &enc_stats_pre);
1356
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8290 if (enc_stats_pre &&
1357 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1358 const char *format = "{fidx} {sidx} {n} {t}";
1359
1360 opt_match_per_stream_str(ost, &o->enc_stats_pre_fmt, oc, st, &format);
1361
1362 ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format);
1363 if (ret < 0)
1364 goto fail;
1365 }
1366
1367 8290 opt_match_per_stream_str(ost, &o->enc_stats_post, oc, st, &enc_stats_post);
1368
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8290 if (enc_stats_post &&
1369 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1370 const char *format = "{fidx} {sidx} {n} {t}";
1371
1372 opt_match_per_stream_str(ost, &o->enc_stats_post_fmt, oc, st, &format);
1373
1374 ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format);
1375 if (ret < 0)
1376 goto fail;
1377 }
1378
1379 8290 opt_match_per_stream_str(ost, &o->mux_stats, oc, st, &mux_stats);
1380
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8290 if (mux_stats &&
1381 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1382 const char *format = "{fidx} {sidx} {n} {t}";
1383
1384 opt_match_per_stream_str(ost, &o->mux_stats_fmt, oc, st, &format);
1385
1386 ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format);
1387 if (ret < 0)
1388 goto fail;
1389 }
1390
1391 8290 opt_match_per_stream_str(ost, &o->enc_time_bases, oc, st, &enc_time_base);
1392
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8289 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
8290 if (enc_time_base && type == AVMEDIA_TYPE_SUBTITLE)
1393 av_log(ost, AV_LOG_WARNING,
1394 "-enc_time_base not supported for subtitles, ignoring\n");
1395
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8289 times.
8290 else if (enc_time_base) {
1396 AVRational q;
1397
1398
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!strcmp(enc_time_base, "demux")) {
1399 1 q = (AVRational){ ENC_TIME_BASE_DEMUX, 0 };
1400 } else if (!strcmp(enc_time_base, "filter")) {
1401 q = (AVRational){ ENC_TIME_BASE_FILTER, 0 };
1402 } else {
1403 ret = av_parse_ratio(&q, enc_time_base, INT_MAX, 0, NULL);
1404 if (ret < 0 || q.den <= 0
1405 #if !FFMPEG_OPT_ENC_TIME_BASE_NUM
1406 || q.num < 0
1407 #endif
1408 ) {
1409 av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", enc_time_base);
1410 ret = ret < 0 ? ret : AVERROR(EINVAL);
1411 goto fail;
1412 }
1413 #if FFMPEG_OPT_ENC_TIME_BASE_NUM
1414 if (q.num < 0)
1415 av_log(ost, AV_LOG_WARNING, "-enc_time_base -1 is deprecated,"
1416 " use -enc_time_base demux\n");
1417 #endif
1418 }
1419
1420 1 enc_tb = q;
1421 }
1422
1423 8290 threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0);
1424
1425 8290 ret = av_opt_set_dict2(ost->enc->enc_ctx, &encoder_opts, AV_OPT_SEARCH_CHILDREN);
1426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0) {
1427 av_log(ost, AV_LOG_ERROR, "Error applying encoder options: %s\n",
1428 av_err2str(ret));
1429 goto fail;
1430 }
1431
1432 8290 ret = check_avoptions(encoder_opts);
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0)
1434 goto fail;
1435
1436 // default to automatic thread count
1437
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 5484 times.
8290 if (!threads_manual)
1438 2806 ost->enc->enc_ctx->thread_count = 0;
1439 } else {
1440 754 ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st,
1441 NULL, &encoder_opts,
1442 &mux->enc_opts_used);
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 754 times.
754 if (ret < 0)
1444 goto fail;
1445 }
1446
1447
1448
2/2
✓ Branch 0 taken 2000 times.
✓ Branch 1 taken 7044 times.
9044 if (o->bitexact) {
1449 2000 ost->bitexact = 1;
1450
2/2
✓ Branch 0 taken 6411 times.
✓ Branch 1 taken 633 times.
7044 } else if (ost->enc) {
1451 6411 ost->bitexact = !!(ost->enc->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT);
1452 }
1453
1454
2/2
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
9044 if (enc) {
1455 8290 ret = set_encoder_id(ost, enc);
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8290 times.
8290 if (ret < 0)
1457 return ret;
1458 }
1459
1460 9044 opt_match_per_stream_str(ost, &o->time_bases, oc, st, &time_base);
1461
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9040 times.
9044 if (time_base) {
1462 AVRational q;
1463
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1464
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 q.num <= 0 || q.den <= 0) {
1465 av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
1466 ret = AVERROR(EINVAL);
1467 goto fail;
1468 }
1469 4 st->time_base = q;
1470 }
1471
1472 9044 ms->max_frames = INT64_MAX;
1473 9044 opt_match_per_stream_int64(ost, &o->max_frames, oc, st, &ms->max_frames);
1474
2/2
✓ Branch 0 taken 5440 times.
✓ Branch 1 taken 9038 times.
14478 for (int i = 0; i < o->max_frames.nb_opt; i++) {
1475 5440 char *p = o->max_frames.opt[i].specifier;
1476
4/4
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 5271 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 163 times.
5440 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1477 6 av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1478 6 break;
1479 }
1480 }
1481
1482 9044 ms->copy_prior_start = -1;
1483 9044 opt_match_per_stream_int(ost, &o->copy_prior_start, oc, st, &ms->copy_prior_start);
1484 9044 opt_match_per_stream_str(ost, &o->bitstream_filters, oc, st, &bsfs);
1485
3/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 8899 times.
✓ Branch 2 taken 145 times.
✗ Branch 3 not taken.
9044 if (bsfs && *bsfs) {
1486 145 ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx);
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (ret < 0) {
1488 av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1489 goto fail;
1490 }
1491 }
1492
1493 9044 opt_match_per_stream_str(ost, &o->codec_tags, oc, st, &codec_tag);
1494
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9035 times.
9044 if (codec_tag) {
1495 9 uint32_t tag = strtol(codec_tag, &next, 0);
1496
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (*next) {
1497 9 uint8_t buf[4] = { 0 };
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1499 9 tag = AV_RL32(buf);
1500 }
1501 9 ost->st->codecpar->codec_tag = tag;
1502 9 ms->par_in->codec_tag = tag;
1503
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (ost->enc)
1504 2 ost->enc->enc_ctx->codec_tag = tag;
1505 }
1506
1507 9044 opt_match_per_stream_dbl(ost, &o->qscale, oc, st, &qscale);
1508
4/4
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
✓ Branch 2 taken 272 times.
✓ Branch 3 taken 8018 times.
9044 if (ost->enc && qscale >= 0) {
1509 272 ost->enc->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1510 272 ost->enc->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1511 }
1512
1513
2/2
✓ Branch 0 taken 9043 times.
✓ Branch 1 taken 1 times.
9044 if (ms->sch_idx >= 0) {
1514 9043 int max_muxing_queue_size = 128;
1515 9043 int muxing_queue_data_threshold = 50 * 1024 * 1024;
1516
1517 9043 opt_match_per_stream_int(ost, &o->max_muxing_queue_size, oc, st,
1518 &max_muxing_queue_size);
1519 9043 opt_match_per_stream_int(ost, &o->muxing_queue_data_threshold,
1520 oc, st, &muxing_queue_data_threshold);
1521
1522 9043 sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx,
1523 max_muxing_queue_size, muxing_queue_data_threshold);
1524 }
1525
1526 9044 opt_match_per_stream_int(ost, &o->bits_per_raw_sample, oc, st,
1527 &ost->bits_per_raw_sample);
1528
1529 9044 opt_match_per_stream_int(ost, &o->fix_sub_duration_heartbeat,
1530 9044 oc, st, &ost->fix_sub_duration_heartbeat);
1531
1532
4/4
✓ Branch 0 taken 3240 times.
✓ Branch 1 taken 5804 times.
✓ Branch 2 taken 3104 times.
✓ Branch 3 taken 136 times.
9044 if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc)
1533 3104 ost->enc->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1534
1535 9044 opt_match_per_stream_int(ost, &o->copy_initial_nonkeyframes,
1536 oc, st, &ms->copy_initial_nonkeyframes);
1537
4/4
✓ Branch 0 taken 7261 times.
✓ Branch 1 taken 1690 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 13 times.
9044 switch (type) {
1538 7261 case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost, &keep_pix_fmt, &vsync_method); break;
1539 1690 case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break;
1540 80 case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break;
1541 }
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9044 times.
9044 if (ret < 0)
1543 goto fail;
1544
1545
4/4
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
✓ Branch 2 taken 1413 times.
✓ Branch 3 taken 6877 times.
9044 if (ost->enc &&
1546
2/2
✓ Branch 0 taken 1371 times.
✓ Branch 1 taken 42 times.
1413 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
1547 8248 ret = ost_bind_filter(mux, ms, ofilter, o, enc_tb, vsync_method,
1548 keep_pix_fmt, autoscale, threads_manual, vs, &src);
1549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
8248 if (ret < 0)
1550 goto fail;
1551
2/2
✓ Branch 0 taken 795 times.
✓ Branch 1 taken 1 times.
796 } else if (ost->ist) {
1552 795 ret = ist_use(ost->ist, !!ost->enc, NULL, &src);
1553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 795 times.
795 if (ret < 0) {
1554 av_log(ost, AV_LOG_ERROR,
1555 "Error binding an input stream\n");
1556 goto fail;
1557 }
1558 795 ms->sch_idx_src = src.idx;
1559
1560 // src refers to a decoder for transcoding, demux stream otherwise
1561
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 753 times.
795 if (ost->enc) {
1562 42 ret = sch_connect(mux->sch,
1563 42 src, SCH_ENC(ms->sch_idx_enc));
1564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (ret < 0)
1565 goto fail;
1566 42 src = SCH_ENC(ms->sch_idx_enc);
1567 }
1568 }
1569
1570
2/2
✓ Branch 0 taken 9043 times.
✓ Branch 1 taken 1 times.
9044 if (src.type != SCH_NODE_TYPE_NONE) {
1571 9043 ret = sch_connect(mux->sch,
1572 9043 src, SCH_MSTREAM(mux->sch_idx, ms->sch_idx));
1573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9043 times.
9043 if (ret < 0)
1574 goto fail;
1575 } else {
1576 // only attachment streams don't have a source
1577
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 av_assert0(type == AVMEDIA_TYPE_ATTACHMENT && ms->sch_idx < 0);
1578 }
1579
1580
4/4
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 1395 times.
✓ Branch 2 taken 753 times.
✓ Branch 3 taken 6896 times.
9044 if (ost->ist && !ost->enc) {
1581 753 ret = streamcopy_init(o, mux, ost, &encoder_opts);
1582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (ret < 0)
1583 goto fail;
1584 }
1585
1586 // copy estimated duration as a hint to the muxer
1587
4/4
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 1395 times.
✓ Branch 2 taken 5994 times.
✓ Branch 3 taken 1655 times.
9044 if (ost->ist && ost->ist->st->duration > 0) {
1588 5994 ms->stream_duration = ist->st->duration;
1589 5994 ms->stream_duration_tb = ist->st->time_base;
1590 }
1591
1592
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9043 times.
9044 if (post)
1593 1 *post = ost;
1594
1595 9044 ret = 0;
1596
1597 9044 fail:
1598 9044 av_dict_free(&encoder_opts);
1599
1600 9044 return ret;
1601 }
1602
1603 6955 static int map_auto_video(Muxer *mux, const OptionsContext *o)
1604 {
1605 6955 AVFormatContext *oc = mux->fc;
1606 6955 InputStreamGroup *best_istg = NULL;
1607 6955 InputStream *best_ist = NULL;
1608 6955 int64_t best_score = 0;
1609 int qcr;
1610
1611 /* video: highest resolution */
1612
2/2
✓ Branch 1 taken 747 times.
✓ Branch 2 taken 6208 times.
6955 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_VIDEO) == AV_CODEC_ID_NONE)
1613 747 return 0;
1614
1615 6208 qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
1616
2/2
✓ Branch 0 taken 6249 times.
✓ Branch 1 taken 6208 times.
12457 for (int j = 0; j < nb_input_files; j++) {
1617 6249 InputFile *ifile = input_files[j];
1618 6249 InputStreamGroup *file_best_istg = NULL;
1619 6249 InputStream *file_best_ist = NULL;
1620 6249 int64_t file_best_score = 0;
1621
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6249 times.
6250 for (int i = 0; i < ifile->nb_stream_groups; i++) {
1622 1 InputStreamGroup *istg = ifile->stream_groups[i];
1623 1 int64_t score = 0;
1624
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!istg->fg)
1626 continue;
1627
1628
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (int j = 0; j < istg->stg->nb_streams; j++) {
1629 1 AVStream *st = istg->stg->streams[j];
1630
1631
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) {
1632 1 score = 100000000;
1633 1 break;
1634 }
1635 }
1636
1637
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 switch (istg->stg->type) {
1638 1 case AV_STREAM_GROUP_PARAMS_TILE_GRID: {
1639 1 const AVStreamGroupTileGrid *tg = istg->stg->params.tile_grid;
1640 2 score += tg->width * (int64_t)tg->height
1641
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 + 5000000*!!(istg->stg->disposition & AV_DISPOSITION_DEFAULT);
1642 1 break;
1643 }
1644 default:
1645 continue;
1646 }
1647
1648
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (score > file_best_score) {
1649 1 file_best_score = score;
1650 1 file_best_istg = istg;
1651 }
1652 }
1653
2/2
✓ Branch 0 taken 6535 times.
✓ Branch 1 taken 6249 times.
12784 for (int i = 0; i < ifile->nb_streams; i++) {
1654 6535 InputStream *ist = ifile->streams[i];
1655 6535 const AVCodecDescriptor *desc = avcodec_descriptor_get(ist->st->codecpar->codec_id);
1656 int64_t score;
1657
1658
2/2
✓ Branch 0 taken 6534 times.
✓ Branch 1 taken 1 times.
6535 if (ist->user_set_discard == AVDISCARD_ALL ||
1659
3/4
✓ Branch 0 taken 5873 times.
✓ Branch 1 taken 661 times.
✓ Branch 2 taken 5873 times.
✗ Branch 3 not taken.
6534 ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5873 times.
5873 (desc && (desc->props & AV_CODEC_PROP_ENHANCEMENT)))
1661 662 continue;
1662
1663 11746 score = ist->st->codecpar->width * (int64_t)ist->st->codecpar->height
1664
2/2
✓ Branch 0 taken 5871 times.
✓ Branch 1 taken 2 times.
5873 + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1665
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 5342 times.
5873 + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1666
3/4
✓ Branch 0 taken 5873 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 5864 times.
5873 if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1667 9 score = 1;
1668
1669
2/2
✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 5 times.
5873 if (score > file_best_score) {
1670
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5868 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5868 if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1671 continue;
1672 5868 file_best_score = score;
1673 5868 file_best_ist = ist;
1674 5868 file_best_istg = NULL;
1675 }
1676 }
1677
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6248 times.
6249 if (file_best_istg) {
1678
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 file_best_score -= 5000000*!!(file_best_istg->stg->disposition & AV_DISPOSITION_DEFAULT);
1679
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (file_best_score > best_score) {
1680 1 best_score = file_best_score;
1681 1 best_istg = file_best_istg;
1682 1 best_ist = NULL;
1683 }
1684 }
1685
2/2
✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 381 times.
6249 if (file_best_ist) {
1686
1/2
✓ Branch 0 taken 5868 times.
✗ Branch 1 not taken.
5868 if((qcr == MKTAG('A', 'P', 'I', 'C')) ||
1687
2/2
✓ Branch 0 taken 5860 times.
✓ Branch 1 taken 8 times.
5868 !(file_best_ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1688
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 5329 times.
5860 file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1689
1/2
✓ Branch 0 taken 5868 times.
✗ Branch 1 not taken.
5868 if (file_best_score > best_score) {
1690 5868 best_score = file_best_score;
1691 5868 best_ist = file_best_ist;
1692 5868 best_istg = NULL;
1693 }
1694 }
1695 }
1696
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6207 times.
6208 if (best_istg) {
1697 1 FilterGraph *fg = best_istg->fg;
1698 1 OutputFilter *ofilter = fg->outputs[0];
1699
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(fg->nb_outputs == 1);
1701 1 av_log(mux, AV_LOG_VERBOSE, "Creating output stream from stream group derived complex filtergraph %d.\n", fg->index);
1702
1703 1 return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, NULL, ofilter, NULL, NULL);
1704 }
1705
2/2
✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 339 times.
6207 if (best_ist)
1706 5868 return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL, NULL, NULL);
1707
1708 339 return 0;
1709 }
1710
1711 8169 static int map_auto_audio(Muxer *mux, const OptionsContext *o)
1712 {
1713 8169 AVFormatContext *oc = mux->fc;
1714 8169 InputStream *best_ist = NULL;
1715 8169 int best_score = 0;
1716
1717 /* audio: most channels */
1718
2/2
✓ Branch 1 taken 1992 times.
✓ Branch 2 taken 6177 times.
8169 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_AUDIO) == AV_CODEC_ID_NONE)
1719 1992 return 0;
1720
1721
2/2
✓ Branch 0 taken 6152 times.
✓ Branch 1 taken 6177 times.
12329 for (int j = 0; j < nb_input_files; j++) {
1722 6152 InputFile *ifile = input_files[j];
1723 6152 InputStream *file_best_ist = NULL;
1724 6152 int file_best_score = 0;
1725
2/2
✓ Branch 0 taken 6362 times.
✓ Branch 1 taken 6152 times.
12514 for (int i = 0; i < ifile->nb_streams; i++) {
1726 6362 InputStream *ist = ifile->streams[i];
1727 int score;
1728
1729
2/2
✓ Branch 0 taken 6361 times.
✓ Branch 1 taken 1 times.
6362 if (ist->user_set_discard == AVDISCARD_ALL ||
1730
2/2
✓ Branch 0 taken 5026 times.
✓ Branch 1 taken 1335 times.
6361 ist->st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
1731 5027 continue;
1732
1733 2670 score = ist->st->codecpar->ch_layout.nb_channels
1734
2/2
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 56 times.
1335 + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1735
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1160 times.
1335 + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1736
2/2
✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 10 times.
1335 if (score > file_best_score) {
1737 1325 file_best_score = score;
1738 1325 file_best_ist = ist;
1739 }
1740 }
1741
2/2
✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 4827 times.
6152 if (file_best_ist) {
1742
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1150 times.
1325 file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1743
1/2
✓ Branch 0 taken 1325 times.
✗ Branch 1 not taken.
1325 if (file_best_score > best_score) {
1744 1325 best_score = file_best_score;
1745 1325 best_ist = file_best_ist;
1746 }
1747 }
1748 }
1749
2/2
✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 4852 times.
6177 if (best_ist)
1750 1325 return ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL, NULL, NULL);
1751
1752 4852 return 0;
1753 }
1754
1755 8336 static int map_auto_subtitle(Muxer *mux, const OptionsContext *o)
1756 {
1757 8336 AVFormatContext *oc = mux->fc;
1758 8336 const char *subtitle_codec_name = NULL;
1759
1760 /* subtitles: pick first */
1761 8336 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1762
4/4
✓ Branch 1 taken 8273 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 8266 times.
✓ Branch 4 taken 7 times.
8336 if (!avcodec_find_encoder(oc->oformat->subtitle_codec) && !subtitle_codec_name)
1763 8266 return 0;
1764
1765
2/2
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 20 times.
104 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
1766
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 34 times.
84 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1767 AVCodecDescriptor const *input_descriptor =
1768 50 avcodec_descriptor_get(ist->st->codecpar->codec_id);
1769 50 AVCodecDescriptor const *output_descriptor = NULL;
1770 AVCodec const *output_codec =
1771 50 avcodec_find_encoder(oc->oformat->subtitle_codec);
1772 50 int input_props = 0, output_props = 0;
1773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ist->user_set_discard == AVDISCARD_ALL)
1774 continue;
1775
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7 times.
50 if (output_codec)
1776 43 output_descriptor = avcodec_descriptor_get(output_codec->id);
1777
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (input_descriptor)
1778 50 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1779
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7 times.
50 if (output_descriptor)
1780 43 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1781
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 16 times.
50 if (subtitle_codec_name ||
1782
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34 input_props & output_props ||
1783 // Map dvb teletext which has neither property to any output subtitle encoder
1784 input_descriptor && output_descriptor &&
1785 (!input_descriptor->props ||
1786 !output_descriptor->props)) {
1787 50 return ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL, NULL, NULL);
1788 }
1789 }
1790
1791 20 return 0;
1792 }
1793
1794 8337 static int map_auto_data(Muxer *mux, const OptionsContext *o)
1795 {
1796 8337 AVFormatContext *oc = mux->fc;
1797 /* Data only if codec id match */
1798 8337 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_DATA);
1799
1800
1/2
✓ Branch 0 taken 8337 times.
✗ Branch 1 not taken.
8337 if (codec_id == AV_CODEC_ID_NONE)
1801 8337 return 0;
1802
1803 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
1804 if (ist->user_set_discard == AVDISCARD_ALL)
1805 continue;
1806 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1807 ist->st->codecpar->codec_id == codec_id) {
1808 int ret = ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL, NULL, NULL);
1809 if (ret < 0)
1810 return ret;
1811 }
1812 }
1813
1814 return 0;
1815 }
1816
1817 488 static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map)
1818 {
1819 InputStream *ist;
1820 int ret;
1821
1822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488 times.
488 if (map->disabled)
1823 return 0;
1824
1825
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 406 times.
488 if (map->linklabel) {
1826 FilterGraph *fg;
1827 82 OutputFilter *ofilter = NULL;
1828 int j, k;
1829
1830
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 for (j = 0; j < nb_filtergraphs; j++) {
1831 82 fg = filtergraphs[j];
1832
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 for (k = 0; k < fg->nb_outputs; k++) {
1833 279 const char *linklabel = fg->outputs[k]->linklabel;
1834
4/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 163 times.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 34 times.
279 if (linklabel && !strcmp(linklabel, map->linklabel)) {
1835 82 ofilter = fg->outputs[k];
1836 82 goto loop_end;
1837 }
1838 }
1839 }
1840 loop_end:
1841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!ofilter) {
1842 av_log(mux, AV_LOG_FATAL, "Output with label '%s' does not exist "
1843 "in any defined filter graph, or was already used elsewhere.\n", map->linklabel);
1844 return AVERROR(EINVAL);
1845 }
1846
1847 82 av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly "
1848 82 "mapped complex filtergraph %d, output [%s]\n", fg->index, map->linklabel);
1849
1850 82 ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL);
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret < 0)
1852 return ret;
1853 } else {
1854 812 const ViewSpecifier *vs = map->vs.type == VIEW_SPECIFIER_TYPE_NONE ?
1855
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 392 times.
406 NULL : &map->vs;
1856
1857 406 ist = input_files[map->file_index]->streams[map->stream_index];
1858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if (ist->user_set_discard == AVDISCARD_ALL) {
1859 av_log(mux, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n",
1860 map->file_index, map->stream_index);
1861 return AVERROR(EINVAL);
1862 }
1863
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
406 if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1864 return 0;
1865
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
406 if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1866 return 0;
1867
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 405 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
406 if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1868 return 0;
1869
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
406 if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1870 return 0;
1871
1872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
1873 !copy_unknown_streams) {
1874 av_log(mux, ignore_unknown_streams ? AV_LOG_WARNING : AV_LOG_FATAL,
1875 "Cannot map stream #%d:%d - unsupported type.\n",
1876 map->file_index, map->stream_index);
1877 if (!ignore_unknown_streams) {
1878 av_log(mux, AV_LOG_FATAL,
1879 "If you want unsupported types ignored instead "
1880 "of failing, please use the -ignore_unknown option\n"
1881 "If you want them copied, please use -copy_unknown\n");
1882 return AVERROR(EINVAL);
1883 }
1884 return 0;
1885 }
1886
1887
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 392 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
406 if (vs && ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
1888 av_log(mux, AV_LOG_ERROR,
1889 "View specifier given for mapping a %s input stream\n",
1890 av_get_media_type_string(ist->st->codecpar->codec_type));
1891 return AVERROR(EINVAL);
1892 }
1893
1894 406 ret = ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL, vs, NULL);
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if (ret < 0)
1896 return ret;
1897 }
1898
1899 488 return 0;
1900 }
1901
1902 8539 static int of_add_attachments(Muxer *mux, const OptionsContext *o)
1903 {
1904 MuxStream *ms;
1905 OutputStream *ost;
1906 int err;
1907
1908
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8539 times.
8540 for (int i = 0; i < o->nb_attachments; i++) {
1909 AVIOContext *pb;
1910 uint8_t *attachment;
1911 char *attachment_filename;
1912 const char *p;
1913 int64_t len;
1914
1915
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) {
1916 av_log(mux, AV_LOG_FATAL, "Could not open attachment file %s.\n",
1917 o->attachments[i]);
1918 return err;
1919 }
1920
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((len = avio_size(pb)) <= 0) {
1921 av_log(mux, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
1922 o->attachments[i]);
1923 err = len ? len : AVERROR_INVALIDDATA;
1924 goto read_fail;
1925 }
1926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
1927 av_log(mux, AV_LOG_FATAL, "Attachment %s too large.\n",
1928 o->attachments[i]);
1929 err = AVERROR(ERANGE);
1930 goto read_fail;
1931 }
1932
1933 1 attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
1934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!attachment) {
1935 err = AVERROR(ENOMEM);
1936 goto read_fail;
1937 }
1938
1939 1 err = avio_read(pb, attachment, len);
1940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1941 av_log(mux, AV_LOG_FATAL, "Error reading attachment file %s: %s\n",
1942 o->attachments[i], av_err2str(err));
1943
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (err != len) {
1944 av_log(mux, AV_LOG_FATAL, "Could not read all %"PRId64" bytes for "
1945 "attachment file %s\n", len, o->attachments[i]);
1946 err = AVERROR(EIO);
1947 }
1948
1949 1 read_fail:
1950 1 avio_closep(&pb);
1951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0)
1952 return err;
1953
1954 1 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1955
1956 1 av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n",
1957 1 o->attachments[i]);
1958
1959 1 attachment_filename = av_strdup(o->attachments[i]);
1960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!attachment_filename) {
1961 av_free(attachment);
1962 return AVERROR(ENOMEM);
1963 }
1964
1965 1 err = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL, NULL, &ost);
1966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
1967 av_free(attachment_filename);
1968 av_freep(&attachment);
1969 return err;
1970 }
1971
1972 1 ms = ms_from_ost(ost);
1973
1974 1 ost->attachment_filename = attachment_filename;
1975 1 ms->par_in->extradata = attachment;
1976 1 ms->par_in->extradata_size = len;
1977
1978 1 p = strrchr(o->attachments[i], '/');
1979
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);
1980 }
1981
1982 8539 return 0;
1983 }
1984
1985 8539 static int create_streams(Muxer *mux, const OptionsContext *o)
1986 {
1987 static int (* const map_func[])(Muxer *mux, const OptionsContext *o) = {
1988 [AVMEDIA_TYPE_VIDEO] = map_auto_video,
1989 [AVMEDIA_TYPE_AUDIO] = map_auto_audio,
1990 [AVMEDIA_TYPE_SUBTITLE] = map_auto_subtitle,
1991 [AVMEDIA_TYPE_DATA] = map_auto_data,
1992 };
1993
1994 8539 AVFormatContext *oc = mux->fc;
1995
1996 8539 int auto_disable =
1997 8539 o->video_disable * (1 << AVMEDIA_TYPE_VIDEO) |
1998 8539 o->audio_disable * (1 << AVMEDIA_TYPE_AUDIO) |
1999 8539 o->subtitle_disable * (1 << AVMEDIA_TYPE_SUBTITLE) |
2000 8539 o->data_disable * (1 << AVMEDIA_TYPE_DATA);
2001
2002 int ret;
2003
2004 /* create streams for all unlabeled output pads */
2005
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 8539 times.
9805 for (int i = 0; i < nb_filtergraphs; i++) {
2006 1266 FilterGraph *fg = filtergraphs[i];
2007
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 1266 times.
2671 for (int j = 0; j < fg->nb_outputs; j++) {
2008 1405 OutputFilter *ofilter = fg->outputs[j];
2009
2010
4/4
✓ Branch 0 taken 1314 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1311 times.
1405 if (ofilter->linklabel || ofilter->bound)
2011 94 continue;
2012
2013 1311 auto_disable |= 1 << ofilter->type;
2014
2015 1311 av_log(mux, AV_LOG_VERBOSE, "Creating output stream from unlabeled "
2016 "output of complex filtergraph %d.", fg->index);
2017
1/2
✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
1311 if (!o->nb_stream_maps)
2018 1311 av_log(mux, AV_LOG_VERBOSE, " This overrides automatic %s mapping.",
2019 av_get_media_type_string(ofilter->type));
2020 1311 av_log(mux, AV_LOG_VERBOSE, "\n");
2021
2022 1311 ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL);
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1311 times.
1311 if (ret < 0)
2024 return ret;
2025 }
2026 }
2027
2028
2/2
✓ Branch 0 taken 8337 times.
✓ Branch 1 taken 202 times.
8539 if (!o->nb_stream_maps) {
2029 8337 av_log(mux, AV_LOG_VERBOSE, "No explicit maps, mapping streams automatically...\n");
2030
2031 /* pick the "best" stream of each type */
2032
2/2
✓ Branch 0 taken 33348 times.
✓ Branch 1 taken 8337 times.
41685 for (int i = 0; i < FF_ARRAY_ELEMS(map_func); i++) {
2033
3/4
✓ Branch 0 taken 33348 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1551 times.
✓ Branch 3 taken 31797 times.
33348 if (!map_func[i] || auto_disable & (1 << i))
2034 1551 continue;
2035 31797 ret = map_func[i](mux, o);
2036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31797 times.
31797 if (ret < 0)
2037 return ret;
2038 }
2039 } else {
2040 202 av_log(mux, AV_LOG_VERBOSE, "Adding streams from explicit maps...\n");
2041
2042
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 202 times.
690 for (int i = 0; i < o->nb_stream_maps; i++) {
2043 488 ret = map_manual(mux, o, &o->stream_maps[i]);
2044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488 times.
488 if (ret < 0)
2045 return ret;
2046 }
2047 }
2048
2049 8539 ret = of_add_attachments(mux, o);
2050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (ret < 0)
2051 return ret;
2052
2053 // setup fix_sub_duration_heartbeat mappings
2054
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (unsigned i = 0; i < oc->nb_streams; i++) {
2055 9044 MuxStream *src = ms_from_ost(mux->of.streams[i]);
2056
2057
2/2
✓ Branch 0 taken 9043 times.
✓ Branch 1 taken 1 times.
9044 if (!src->ost.fix_sub_duration_heartbeat)
2058 9043 continue;
2059
2060
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (unsigned j = 0; j < oc->nb_streams; j++) {
2061 2 MuxStream *dst = ms_from_ost(mux->of.streams[j]);
2062
2063
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 ||
2064
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)
2065 1 continue;
2066
2067 1 ret = sch_mux_sub_heartbeat_add(mux->sch, mux->sch_idx, src->sch_idx,
2068 1 dst->sch_idx_src);
2069
2070 }
2071 }
2072
2073 // handle -apad
2074
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8535 times.
8539 if (o->shortest) {
2075 4 int have_video = 0;
2076
2077
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (unsigned i = 0; i < mux->of.nb_streams; i++)
2078
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (mux->of.streams[i]->type == AVMEDIA_TYPE_VIDEO) {
2079 4 have_video = 1;
2080 4 break;
2081 }
2082
2083
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 4 times.
12 for (unsigned i = 0; have_video && i < mux->of.nb_streams; i++) {
2084 8 MuxStream *ms = ms_from_ost(mux->of.streams[i]);
2085 8 OutputFilter *ofilter = ms->ost.filter;
2086
2087
3/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8 if (ms->ost.type != AVMEDIA_TYPE_AUDIO || !ms->apad || !ofilter)
2088 8 continue;
2089
2090 ofilter->apad = av_strdup(ms->apad);
2091 if (!ofilter->apad)
2092 return AVERROR(ENOMEM);
2093 }
2094 }
2095
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (unsigned i = 0; i < mux->of.nb_streams; i++) {
2096 9044 MuxStream *ms = ms_from_ost(mux->of.streams[i]);
2097 9044 ms->apad = NULL;
2098 }
2099
2100
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8539 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2101 av_dump_format(oc, nb_output_files - 1, oc->url, 1);
2102 av_log(mux, AV_LOG_ERROR, "Output file does not contain any stream\n");
2103 return AVERROR(EINVAL);
2104 }
2105
2106 8539 return 0;
2107 }
2108
2109 8539 static int setup_sync_queues(Muxer *mux, AVFormatContext *oc,
2110 int64_t buf_size_us, int shortest)
2111 {
2112 8539 OutputFile *of = &mux->of;
2113 8539 int nb_av_enc = 0, nb_audio_fs = 0, nb_interleaved = 0;
2114 8539 int limit_frames = 0, limit_frames_av_enc = 0;
2115
2116 #define IS_AV_ENC(ost, type) \
2117 (ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
2118 #define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT)
2119
2120
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (int i = 0; i < oc->nb_streams; i++) {
2121 9044 OutputStream *ost = of->streams[i];
2122 9044 MuxStream *ms = ms_from_ost(ost);
2123 9044 enum AVMediaType type = ost->type;
2124
2125 9044 ms->sq_idx_mux = -1;
2126
2127 9044 nb_interleaved += IS_INTERLEAVED(type);
2128
6/6
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
✓ Branch 2 taken 1413 times.
✓ Branch 3 taken 6877 times.
✓ Branch 4 taken 1371 times.
✓ Branch 5 taken 42 times.
9044 nb_av_enc += IS_AV_ENC(ost, type);
2129
4/4
✓ Branch 0 taken 8290 times.
✓ Branch 1 taken 754 times.
✓ Branch 2 taken 1371 times.
✓ Branch 3 taken 6919 times.
10415 nb_audio_fs += (ost->enc && type == AVMEDIA_TYPE_AUDIO &&
2130
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1179 times.
1371 !(ost->enc->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
2131
2132 9044 limit_frames |= ms->max_frames < INT64_MAX;
2133
7/8
✓ Branch 0 taken 3102 times.
✓ Branch 1 taken 5942 times.
✓ Branch 2 taken 3064 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 3035 times.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
9044 limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type);
2134 }
2135
2136
7/8
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 8282 times.
✓ Branch 2 taken 253 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 8535 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5317 times.
✓ Branch 7 taken 139 times.
13995 if (!((nb_interleaved > 1 && shortest) ||
2137
2/2
✓ Branch 0 taken 5456 times.
✓ Branch 1 taken 3079 times.
8535 (nb_interleaved > 0 && limit_frames) ||
2138 nb_audio_fs))
2139 5317 return 0;
2140
2141 /* we use a sync queue before encoding when:
2142 * - 'shortest' is in effect and we have two or more encoded audio/video
2143 * streams
2144 * - at least one encoded audio/video stream is frame-limited, since
2145 * that has similar semantics to 'shortest'
2146 * - at least one audio encoder requires constant frame sizes
2147 *
2148 * Note that encoding sync queues are handled in the scheduler, because
2149 * different encoders run in different threads and need external
2150 * synchronization, while muxer sync queues can be handled inside the muxer
2151 */
2152
8/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3218 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 157 times.
✓ Branch 5 taken 3064 times.
✓ Branch 6 taken 141 times.
✓ Branch 7 taken 16 times.
3222 if ((shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) {
2153 int sq_idx, ret;
2154
2155 3206 sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux);
2156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3206 times.
3206 if (sq_idx < 0)
2157 return sq_idx;
2158
2159
2/2
✓ Branch 0 taken 3278 times.
✓ Branch 1 taken 3206 times.
6484 for (int i = 0; i < oc->nb_streams; i++) {
2160 3278 OutputStream *ost = of->streams[i];
2161 3278 MuxStream *ms = ms_from_ost(ost);
2162 3278 enum AVMediaType type = ost->type;
2163
2164
5/6
✓ Branch 0 taken 3271 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 219 times.
✓ Branch 3 taken 3052 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 219 times.
3278 if (!IS_AV_ENC(ost, type))
2165 7 continue;
2166
2167 3271 ret = sch_sq_add_enc(mux->sch, sq_idx, ms->sch_idx_enc,
2168
2/2
✓ Branch 0 taken 3064 times.
✓ Branch 1 taken 203 times.
3267 shortest || ms->max_frames < INT64_MAX,
2169
2/2
✓ Branch 0 taken 3267 times.
✓ Branch 1 taken 4 times.
3271 ms->max_frames);
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3271 times.
3271 if (ret < 0)
2171 return ret;
2172 }
2173 }
2174
2175 /* if there are any additional interleaved streams, then ALL the streams
2176 * are also synchronized before sending them to the muxer */
2177
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 3202 times.
3222 if (nb_interleaved > nb_av_enc) {
2178 20 mux->sq_mux = sq_alloc(SYNC_QUEUE_PACKETS, buf_size_us, mux);
2179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!mux->sq_mux)
2180 return AVERROR(ENOMEM);
2181
2182 20 mux->sq_pkt = av_packet_alloc();
2183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!mux->sq_pkt)
2184 return AVERROR(ENOMEM);
2185
2186
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 20 times.
74 for (int i = 0; i < oc->nb_streams; i++) {
2187 54 OutputStream *ost = of->streams[i];
2188 54 MuxStream *ms = ms_from_ost(ost);
2189 54 enum AVMediaType type = ost->type;
2190
2191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (!IS_INTERLEAVED(type))
2192 continue;
2193
2194
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
102 ms->sq_idx_mux = sq_add_stream(mux->sq_mux,
2195
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 10 times.
48 shortest || ms->max_frames < INT64_MAX);
2196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ms->sq_idx_mux < 0)
2197 return ms->sq_idx_mux;
2198
2199
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 16 times.
54 if (ms->max_frames != INT64_MAX)
2200 38 sq_limit_frames(mux->sq_mux, ms->sq_idx_mux, ms->max_frames);
2201 }
2202 }
2203
2204 #undef IS_AV_ENC
2205 #undef IS_INTERLEAVED
2206
2207 3222 return 0;
2208 }
2209
2210 14 static int of_parse_iamf_audio_element_layers(Muxer *mux, AVStreamGroup *stg, char *ptr)
2211 {
2212 14 AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element;
2213 14 AVDictionary *dict = NULL;
2214 const char *token;
2215 14 int ret = 0;
2216
2217 14 audio_element->demixing_info =
2218 14 av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_DEMIXING, 1, NULL);
2219 14 audio_element->recon_gain_info =
2220 14 av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN, 1, NULL);
2221
2222
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (!audio_element->demixing_info ||
2223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 !audio_element->recon_gain_info)
2224 return AVERROR(ENOMEM);
2225
2226 /* process manually set layers and parameters */
2227 14 token = av_strtok(NULL, ",", &ptr);
2228
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 14 times.
64 while (token) {
2229 const AVDictionaryEntry *e;
2230 50 int demixing = 0, recon_gain = 0;
2231 50 int layer = 0;
2232
2233
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 2 times.
50 if (ptr)
2234 48 ptr += strspn(ptr, " \n\t\r");
2235
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 20 times.
50 if (av_strstart(token, "layer=", &token))
2236 30 layer = 1;
2237
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
20 else if (av_strstart(token, "demixing=", &token))
2238 10 demixing = 1;
2239
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 else if (av_strstart(token, "recon_gain=", &token))
2240 10 recon_gain = 1;
2241
2242 50 av_dict_free(&dict);
2243 50 ret = av_dict_parse_string(&dict, token, "=", ":", 0);
2244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0) {
2245 av_log(mux, AV_LOG_ERROR, "Error parsing audio element specification %s\n", token);
2246 goto fail;
2247 }
2248
2249
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20 times.
50 if (layer) {
2250 30 AVIAMFLayer *audio_layer = av_iamf_audio_element_add_layer(audio_element);
2251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!audio_layer) {
2252 av_log(mux, AV_LOG_ERROR, "Error adding layer to stream group %d\n", stg->index);
2253 ret = AVERROR(ENOMEM);
2254 goto fail;
2255 }
2256 30 av_opt_set_dict(audio_layer, &dict);
2257
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20 } else if (demixing || recon_gain) {
2258 20 AVIAMFParamDefinition *param = demixing ? audio_element->demixing_info
2259
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 : audio_element->recon_gain_info;
2260 20 void *subblock = av_iamf_param_definition_get_subblock(param, 0);
2261
2262 20 av_opt_set_dict(param, &dict);
2263 20 av_opt_set_dict(subblock, &dict);
2264 }
2265
2266 // make sure that no entries are left in the dict
2267 50 e = NULL;
2268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (e = av_dict_iterate(dict, e)) {
2269 av_log(mux, AV_LOG_FATAL, "Unknown layer key %s.\n", e->key);
2270 ret = AVERROR(EINVAL);
2271 goto fail;
2272 }
2273 50 token = av_strtok(NULL, ",", &ptr);
2274 }
2275
2276 14 fail:
2277 14 av_dict_free(&dict);
2278
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (!ret && !audio_element->nb_layers) {
2279 av_log(mux, AV_LOG_ERROR, "No layer in audio element specification\n");
2280 ret = AVERROR(EINVAL);
2281 }
2282
2283 14 return ret;
2284 }
2285
2286 13 static int of_parse_iamf_submixes(Muxer *mux, AVStreamGroup *stg, char *ptr)
2287 {
2288 13 AVFormatContext *oc = mux->fc;
2289 13 AVIAMFMixPresentation *mix = stg->params.iamf_mix_presentation;
2290 13 AVDictionary *dict = NULL;
2291 const char *token;
2292 13 char *submix_str = NULL;
2293 13 int ret = 0;
2294
2295 /* process manually set submixes */
2296 13 token = av_strtok(NULL, ",", &ptr);
2297
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 13 times.
28 while (token) {
2298 15 AVIAMFSubmix *submix = NULL;
2299 const char *subtoken;
2300 15 char *subptr = NULL;
2301
2302
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 if (ptr)
2303 13 ptr += strspn(ptr, " \n\t\r");
2304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!av_strstart(token, "submix=", &token)) {
2305 av_log(mux, AV_LOG_ERROR, "No submix in mix presentation specification \"%s\"\n", token);
2306 goto fail;
2307 }
2308
2309 15 submix_str = av_strdup(token);
2310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!submix_str)
2311 goto fail;
2312
2313 15 submix = av_iamf_mix_presentation_add_submix(mix);
2314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!submix) {
2315 av_log(mux, AV_LOG_ERROR, "Error adding submix to stream group %d\n", stg->index);
2316 ret = AVERROR(ENOMEM);
2317 goto fail;
2318 }
2319 15 submix->output_mix_config =
2320 15 av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, 0, NULL);
2321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!submix->output_mix_config) {
2322 ret = AVERROR(ENOMEM);
2323 goto fail;
2324 }
2325
2326 15 subptr = NULL;
2327 15 subtoken = av_strtok(submix_str, "|", &subptr);
2328
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 15 times.
78 while (subtoken) {
2329 const AVDictionaryEntry *e;
2330 63 int element = 0, layout = 0;
2331
2332
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 15 times.
63 if (subptr)
2333 48 subptr += strspn(subptr, " \n\t\r");
2334
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 47 times.
63 if (av_strstart(subtoken, "element=", &subtoken))
2335 16 element = 1;
2336
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 15 times.
47 else if (av_strstart(subtoken, "layout=", &subtoken))
2337 32 layout = 1;
2338
2339 63 av_dict_free(&dict);
2340 63 ret = av_dict_parse_string(&dict, subtoken, "=", ":", 0);
2341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (ret < 0) {
2342 av_log(mux, AV_LOG_ERROR, "Error parsing submix specification \"%s\"\n", subtoken);
2343 goto fail;
2344 }
2345
2346
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 47 times.
63 if (element) {
2347 AVIAMFSubmixElement *submix_element;
2348 16 char *endptr = NULL;
2349 16 int64_t idx = -1;
2350
2351
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (e = av_dict_get(dict, "stg", NULL, 0))
2352 16 idx = strtoll(e->value, &endptr, 0);
2353
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
16 if (!endptr || *endptr || idx < 0 || idx >= oc->nb_stream_groups - 1 ||
2354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 oc->stream_groups[idx]->type != AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT) {
2355 av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in "
2356 "submix element specification \"%s\"\n", subtoken);
2357 ret = AVERROR(EINVAL);
2358 goto fail;
2359 }
2360 16 submix_element = av_iamf_submix_add_element(submix);
2361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!submix_element) {
2362 av_log(mux, AV_LOG_ERROR, "Error adding element to submix\n");
2363 ret = AVERROR(ENOMEM);
2364 goto fail;
2365 }
2366
2367 16 submix_element->audio_element_id = oc->stream_groups[idx]->id;
2368
2369 16 submix_element->element_mix_config =
2370 16 av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, 0, NULL);
2371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!submix_element->element_mix_config)
2372 ret = AVERROR(ENOMEM);
2373 16 av_dict_set(&dict, "stg", NULL, 0);
2374 16 av_opt_set_dict2(submix_element, &dict, AV_OPT_SEARCH_CHILDREN);
2375
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 15 times.
47 } else if (layout) {
2376 32 AVIAMFSubmixLayout *submix_layout = av_iamf_submix_add_layout(submix);
2377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!submix_layout) {
2378 av_log(mux, AV_LOG_ERROR, "Error adding layout to submix\n");
2379 ret = AVERROR(ENOMEM);
2380 goto fail;
2381 }
2382 32 av_opt_set_dict(submix_layout, &dict);
2383 } else
2384 15 av_opt_set_dict2(submix, &dict, AV_OPT_SEARCH_CHILDREN);
2385
2386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (ret < 0) {
2387 goto fail;
2388 }
2389
2390 // make sure that no entries are left in the dict
2391 63 e = NULL;
2392
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
63 while (e = av_dict_iterate(dict, e)) {
2393 av_log(mux, AV_LOG_FATAL, "Unknown submix key %s.\n", e->key);
2394 ret = AVERROR(EINVAL);
2395 goto fail;
2396 }
2397 63 subtoken = av_strtok(NULL, "|", &subptr);
2398 }
2399 15 av_freep(&submix_str);
2400
2401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!submix->nb_elements) {
2402 av_log(mux, AV_LOG_ERROR, "No audio elements in submix specification \"%s\"\n", token);
2403 ret = AVERROR(EINVAL);
2404 }
2405 15 token = av_strtok(NULL, ",", &ptr);
2406 }
2407
2408 13 fail:
2409 13 av_dict_free(&dict);
2410 13 av_free(submix_str);
2411
2412 13 return ret;
2413 }
2414
2415 18 static int of_serialize_options(Muxer *mux, void *obj, AVBPrint *bp)
2416 {
2417 char *ptr;
2418 int ret;
2419
2420 18 ret = av_opt_serialize(obj, 0, AV_OPT_SERIALIZE_SKIP_DEFAULTS | AV_OPT_SERIALIZE_SEARCH_CHILDREN,
2421 &ptr, '=', ':');
2422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0) {
2423 av_log(mux, AV_LOG_ERROR, "Failed to serialize group\n");
2424 return ret;
2425 }
2426
2427 18 av_bprintf(bp, "%s", ptr);
2428 18 ret = strlen(ptr);
2429 18 av_free(ptr);
2430
2431 18 return ret;
2432 }
2433
2434 #define SERIALIZE(parent, child) do { \
2435 ret = of_serialize_options(mux, parent->child, bp); \
2436 if (ret < 0) \
2437 return ret; \
2438 } while (0)
2439
2440 #define SERIALIZE_LOOP_SUBBLOCK(obj) do { \
2441 for (int k = 0; k < obj->nb_subblocks; k++) { \
2442 ret = of_serialize_options(mux, \
2443 av_iamf_param_definition_get_subblock(obj, k), bp); \
2444 if (ret < 0) \
2445 return ret; \
2446 } \
2447 } while (0)
2448
2449 #define SERIALIZE_LOOP(parent, child, suffix, separator) do { \
2450 for (int j = 0; j < parent->nb_## child ## suffix; j++) { \
2451 av_bprintf(bp, separator#child "="); \
2452 SERIALIZE(parent, child ## suffix[j]); \
2453 } \
2454 } while (0)
2455
2456 2 static int64_t get_stream_group_index_from_id(Muxer *mux, int64_t id)
2457 {
2458 2 AVFormatContext *oc = mux->fc;
2459
2460
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for (unsigned i = 0; i < oc->nb_stream_groups; i++)
2461
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (oc->stream_groups[i]->id == id)
2462 2 return oc->stream_groups[i]->index;
2463
2464 return AVERROR(EINVAL);
2465 }
2466
2467 4 static int of_map_group(Muxer *mux, AVDictionary **dict, AVBPrint *bp, const char *map)
2468 {
2469 AVStreamGroup *stg;
2470 int ret, file_idx, stream_idx;
2471 char *ptr;
2472
2473 4 file_idx = strtol(map, &ptr, 0);
2474
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (file_idx >= nb_input_files || file_idx < 0 || map == ptr) {
2475 av_log(mux, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx);
2476 return AVERROR(EINVAL);
2477 }
2478
2479
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 stream_idx = strtol(*ptr == '=' ? ptr + 1 : ptr, &ptr, 0);
2480
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (*ptr || stream_idx >= input_files[file_idx]->ctx->nb_stream_groups || stream_idx < 0) {
2481 av_log(mux, AV_LOG_ERROR, "Invalid input stream group index: %d.\n", stream_idx);
2482 return AVERROR(EINVAL);
2483 }
2484
2485 4 stg = input_files[file_idx]->ctx->stream_groups[stream_idx];
2486 4 ret = of_serialize_options(mux, stg, bp);
2487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
2488 return ret;
2489
2490 4 ret = av_dict_parse_string(dict, bp->str, "=", ":", 0);
2491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
2492 av_log(mux, AV_LOG_ERROR, "Error parsing mapped group specification %s\n", ptr);
2493 4 av_dict_set_int(dict, "type", stg->type, 0);
2494
2495 4 av_bprint_clear(bp);
2496
2/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 switch(stg->type) {
2497 2 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: {
2498 2 AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element;
2499
2500
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (audio_element->demixing_info) {
2501 1 AVIAMFParamDefinition *demixing_info = audio_element->demixing_info;
2502 1 av_bprintf(bp, ",demixing=");
2503
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 SERIALIZE(audio_element, demixing_info);
2504
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (ret && demixing_info->nb_subblocks)
2505 1 av_bprintf(bp, ":");
2506
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 SERIALIZE_LOOP_SUBBLOCK(demixing_info);
2507 }
2508
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (audio_element->recon_gain_info) {
2509 1 AVIAMFParamDefinition *recon_gain_info = audio_element->recon_gain_info;
2510 1 av_bprintf(bp, ",recon_gain=");
2511
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 SERIALIZE(audio_element, recon_gain_info);
2512
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (ret && recon_gain_info->nb_subblocks)
2513 1 av_bprintf(bp, ":");
2514
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 SERIALIZE_LOOP_SUBBLOCK(recon_gain_info);
2515 }
2516
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
5 SERIALIZE_LOOP(audio_element, layer, s, ",");
2517 2 break;
2518 }
2519 2 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: {
2520 2 AVIAMFMixPresentation *mix = stg->params.iamf_mix_presentation;
2521
2522
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < mix->nb_submixes; i++) {
2523 2 AVIAMFSubmix *submix = mix->submixes[i];
2524 2 AVIAMFParamDefinition *output_mix_config = submix->output_mix_config;
2525
2526 2 av_bprintf(bp, ",submix=");
2527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 SERIALIZE(mix, submixes[i]);
2528
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (ret && output_mix_config->nb_subblocks)
2529 av_bprintf(bp, ":");
2530
1/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 SERIALIZE_LOOP_SUBBLOCK(output_mix_config);
2531
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int j = 0; j < submix->nb_elements; j++) {
2532 2 AVIAMFSubmixElement *element = submix->elements[j];
2533 2 AVIAMFParamDefinition *element_mix_config = element->element_mix_config;
2534 2 int64_t id = get_stream_group_index_from_id(mux, element->audio_element_id);
2535
2536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (id < 0) {
2537 av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in"
2538 "submix element");
2539 return id;
2540 }
2541
2542 2 av_bprintf(bp, "|element=");
2543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 SERIALIZE(submix, elements[j]);
2544
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (ret && element_mix_config->nb_subblocks)
2545 av_bprintf(bp, ":");
2546
1/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 SERIALIZE_LOOP_SUBBLOCK(element_mix_config);
2547
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ret)
2548 2 av_bprintf(bp, ":");
2549 2 av_bprintf(bp, "stg=%"PRId64, id);
2550 }
2551
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
5 SERIALIZE_LOOP(submix, layout, s, "|");
2552 }
2553 2 break;
2554 }
2555 case AV_STREAM_GROUP_PARAMS_LCEVC:
2556 break;
2557 default:
2558 av_log(mux, AV_LOG_ERROR, "Unsupported mapped group type %d.\n", stg->type);
2559 ret = AVERROR(EINVAL);
2560 break;
2561 }
2562 4 return 0;
2563 }
2564
2565 27 static int of_parse_group_token(Muxer *mux, const char *token, char *ptr)
2566 {
2567 27 AVFormatContext *oc = mux->fc;
2568 AVStreamGroup *stg;
2569 27 AVDictionary *dict = NULL, *tmp = NULL;
2570 27 char *mapped_string = NULL;
2571 const AVDictionaryEntry *e;
2572 27 const AVOption opts[] = {
2573 { "type", "Set group type", offsetof(AVStreamGroup, type), AV_OPT_TYPE_INT,
2574 { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "type" },
2575 { "iamf_audio_element", NULL, 0, AV_OPT_TYPE_CONST,
2576 { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT }, .unit = "type" },
2577 { "iamf_mix_presentation", NULL, 0, AV_OPT_TYPE_CONST,
2578 { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION }, .unit = "type" },
2579 { "lcevc", NULL, 0, AV_OPT_TYPE_CONST,
2580 { .i64 = AV_STREAM_GROUP_PARAMS_LCEVC }, .unit = "type" },
2581 { NULL },
2582 };
2583 27 const AVClass class = {
2584 .class_name = "StreamGroupType",
2585 .item_name = av_default_item_name,
2586 .option = opts,
2587 .version = LIBAVUTIL_VERSION_INT,
2588 };
2589 27 const AVClass *pclass = &class;
2590 int type, ret;
2591
2592 27 ret = av_dict_parse_string(&dict, token, "=", ":", AV_DICT_MULTIKEY);
2593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0) {
2594 av_log(mux, AV_LOG_ERROR, "Error parsing group specification %s\n", token);
2595 return ret;
2596 }
2597
2598 27 av_dict_copy(&tmp, dict, 0);
2599 27 e = av_dict_get(dict, "map", NULL, 0);
2600
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 23 times.
27 if (e) {
2601 AVBPrint bp;
2602
2603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ptr) {
2604 av_log(mux, AV_LOG_ERROR, "Unexpected extra parameters when mapping a"
2605 " stream group\n");
2606 ret = AVERROR(EINVAL);
2607 goto end;
2608 }
2609
2610 4 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
2611 4 ret = of_map_group(mux, &tmp, &bp, e->value);
2612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
2613 av_bprint_finalize(&bp, NULL);
2614 goto end;
2615 }
2616
2617 4 av_bprint_finalize(&bp, &mapped_string);
2618 4 ptr = mapped_string;
2619 }
2620
2621 // "type" is not a user settable AVOption in AVStreamGroup, so handle it here
2622 27 e = av_dict_get(tmp, "type", NULL, 0);
2623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!e) {
2624 av_log(mux, AV_LOG_ERROR, "No type specified for Stream Group in \"%s\"\n", token);
2625 ret = AVERROR(EINVAL);
2626 goto end;
2627 }
2628
2629 27 ret = av_opt_eval_int(&pclass, opts, e->value, &type);
2630
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
27 if (!ret && type == AV_STREAM_GROUP_PARAMS_NONE)
2631 ret = AVERROR(EINVAL);
2632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0) {
2633 av_log(mux, AV_LOG_ERROR, "Invalid group type \"%s\"\n", e->value);
2634 goto end;
2635 }
2636
2637 27 stg = avformat_stream_group_create(oc, type, &tmp);
2638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!stg) {
2639 ret = AVERROR(ENOMEM);
2640 goto end;
2641 }
2642
2643 27 e = NULL;
2644
2/2
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 27 times.
92 while (e = av_dict_get(dict, "st", e, 0)) {
2645 char *endptr;
2646 65 int64_t idx = strtoll(e->value, &endptr, 0);
2647
3/6
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 65 times.
65 if (*endptr || idx < 0 || idx >= oc->nb_streams) {
2648 av_log(mux, AV_LOG_ERROR, "Invalid stream index %"PRId64"\n", idx);
2649 ret = AVERROR(EINVAL);
2650 goto end;
2651 }
2652 65 ret = avformat_stream_group_add_stream(stg, oc->streams[idx]);
2653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (ret < 0)
2654 goto end;
2655 }
2656
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 27 times.
40 while (e = av_dict_get(dict, "stg", e, 0)) {
2657 char *endptr;
2658 13 int64_t idx = strtoll(e->value, &endptr, 0);
2659
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
13 if (*endptr || idx < 0 || idx >= oc->nb_stream_groups - 1) {
2660 av_log(mux, AV_LOG_ERROR, "Invalid stream group index %"PRId64"\n", idx);
2661 ret = AVERROR(EINVAL);
2662 goto end;
2663 }
2664
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 13 times.
77 for (unsigned i = 0; i < oc->stream_groups[idx]->nb_streams; i++) {
2665 64 ret = avformat_stream_group_add_stream(stg, oc->stream_groups[idx]->streams[i]);
2666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (ret < 0)
2667 goto end;
2668 }
2669 }
2670
2671
2/3
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
27 switch(type) {
2672 14 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
2673 14 ret = of_parse_iamf_audio_element_layers(mux, stg, ptr);
2674 14 break;
2675 13 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
2676 13 ret = of_parse_iamf_submixes(mux, stg, ptr);
2677 13 break;
2678 default:
2679 break;
2680 }
2681
2682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
2683 goto end;
2684
2685 // make sure that nothing but "st" and "stg" entries are left in the dict
2686 27 e = NULL;
2687 27 av_dict_set(&tmp, "map", NULL, 0);
2688 27 av_dict_set(&tmp, "type", NULL, 0);
2689
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 27 times.
54 while (e = av_dict_iterate(tmp, e)) {
2690
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
27 if (!strcmp(e->key, "st") || !strcmp(e->key, "stg"))
2691 27 continue;
2692
2693 av_log(mux, AV_LOG_FATAL, "Unknown group key %s.\n", e->key);
2694 ret = AVERROR(EINVAL);
2695 goto end;
2696 }
2697
2698 27 ret = 0;
2699 27 end:
2700 27 av_free(mapped_string);
2701 27 av_dict_free(&dict);
2702 27 av_dict_free(&tmp);
2703
2704 27 return ret;
2705 }
2706
2707 8539 static int of_add_groups(Muxer *mux, const OptionsContext *o)
2708 {
2709 /* process manually set groups */
2710
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 8539 times.
8566 for (int i = 0; i < o->stream_groups.nb_opt; i++) {
2711 const char *token;
2712 27 char *str, *ptr = NULL;
2713 27 int ret = 0;
2714
2715 27 str = av_strdup(o->stream_groups.opt[i].u.str);
2716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!str)
2717 return ret;
2718
2719 27 token = av_strtok(str, ",", &ptr);
2720
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (token) {
2721
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
27 if (ptr)
2722 23 ptr += strspn(ptr, " \n\t\r");
2723 27 ret = of_parse_group_token(mux, token, ptr);
2724 }
2725
2726 27 av_free(str);
2727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
2728 return ret;
2729 }
2730
2731 8539 return 0;
2732 }
2733
2734 8539 static int of_add_programs(Muxer *mux, const OptionsContext *o)
2735 {
2736 8539 AVFormatContext *oc = mux->fc;
2737 /* process manually set programs */
2738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 for (int i = 0; i < o->program.nb_opt; i++) {
2739 AVDictionary *dict = NULL;
2740 const AVDictionaryEntry *e;
2741 AVProgram *program;
2742 int ret, progid = i + 1;
2743
2744 ret = av_dict_parse_string(&dict, o->program.opt[i].u.str, "=", ":",
2745 AV_DICT_MULTIKEY);
2746 if (ret < 0) {
2747 av_log(mux, AV_LOG_ERROR, "Error parsing program specification %s\n",
2748 o->program.opt[i].u.str);
2749 return ret;
2750 }
2751
2752 e = av_dict_get(dict, "program_num", NULL, 0);
2753 if (e) {
2754 progid = strtol(e->value, NULL, 0);
2755 av_dict_set(&dict, e->key, NULL, 0);
2756 }
2757
2758 program = av_new_program(oc, progid);
2759 if (!program) {
2760 ret = AVERROR(ENOMEM);
2761 goto fail;
2762 }
2763
2764 e = av_dict_get(dict, "title", NULL, 0);
2765 if (e) {
2766 av_dict_set(&program->metadata, e->key, e->value, 0);
2767 av_dict_set(&dict, e->key, NULL, 0);
2768 }
2769
2770 e = NULL;
2771 while (e = av_dict_get(dict, "st", e, 0)) {
2772 int st_num = strtol(e->value, NULL, 0);
2773 av_program_add_stream_index(oc, progid, st_num);
2774 }
2775
2776 // make sure that nothing but "st" entries are left in the dict
2777 e = NULL;
2778 while (e = av_dict_iterate(dict, e)) {
2779 if (!strcmp(e->key, "st"))
2780 continue;
2781
2782 av_log(mux, AV_LOG_FATAL, "Unknown program key %s.\n", e->key);
2783 ret = AVERROR(EINVAL);
2784 goto fail;
2785 }
2786
2787 fail:
2788 av_dict_free(&dict);
2789 if (ret < 0)
2790 return ret;
2791 }
2792
2793 8539 return 0;
2794 }
2795
2796 /**
2797 * Parse a metadata specifier passed as 'arg' parameter.
2798 * @param arg metadata string to parse
2799 * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
2800 * @param index for type c/p, chapter/program index is written here
2801 * @param stream_spec for type s, the stream specifier is written here
2802 */
2803 242 static int parse_meta_type(void *logctx, const char *arg,
2804 char *type, int *index, const char **stream_spec)
2805 {
2806
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 171 times.
242 if (*arg) {
2807 71 *type = *arg;
2808
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
71 switch (*arg) {
2809 21 case 'g':
2810 21 break;
2811 46 case 's':
2812
2/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
46 if (*(++arg) && *arg != ':') {
2813 av_log(logctx, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
2814 return AVERROR(EINVAL);
2815 }
2816
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 *stream_spec = *arg == ':' ? arg + 1 : "";
2817 46 break;
2818 4 case 'c':
2819 case 'p':
2820
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (*(++arg) == ':')
2821 3 *index = strtol(++arg, NULL, 0);
2822 4 break;
2823 default:
2824 av_log(logctx, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
2825 return AVERROR(EINVAL);
2826 }
2827 } else
2828 171 *type = 'g';
2829
2830 242 return 0;
2831 }
2832
2833 8539 static int of_add_metadata(OutputFile *of, AVFormatContext *oc,
2834 const OptionsContext *o)
2835 {
2836
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 8539 times.
8767 for (int i = 0; i < o->metadata.nb_opt; i++) {
2837 AVDictionary **m;
2838 char type, *val;
2839 const char *stream_spec;
2840 228 int index = 0, ret = 0;
2841
2842 228 val = strchr(o->metadata.opt[i].u.str, '=');
2843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 if (!val) {
2844 av_log(of, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
2845 o->metadata.opt[i].u.str);
2846 return AVERROR(EINVAL);
2847 }
2848 228 *val++ = 0;
2849
2850 228 ret = parse_meta_type(of, o->metadata.opt[i].specifier, &type, &index, &stream_spec);
2851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 if (ret < 0)
2852 return ret;
2853
2854
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 187 times.
228 if (type == 's') {
2855
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 41 times.
260 for (int j = 0; j < oc->nb_streams; j++) {
2856
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 178 times.
219 if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
2857
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 av_dict_set(&oc->streams[j]->metadata, o->metadata.opt[i].u.str, *val ? val : NULL, 0);
2858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 } else if (ret < 0)
2859 return ret;
2860 }
2861 } else {
2862
2/4
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
187 switch (type) {
2863 183 case 'g':
2864 183 m = &oc->metadata;
2865 183 break;
2866 4 case 'c':
2867
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (index < 0 || index >= oc->nb_chapters) {
2868 av_log(of, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
2869 return AVERROR(EINVAL);
2870 }
2871 4 m = &oc->chapters[index]->metadata;
2872 4 break;
2873 case 'p':
2874 if (index < 0 || index >= oc->nb_programs) {
2875 av_log(of, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index);
2876 return AVERROR(EINVAL);
2877 }
2878 m = &oc->programs[index]->metadata;
2879 break;
2880 default:
2881 av_log(of, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata.opt[i].specifier);
2882 return AVERROR(EINVAL);
2883 }
2884
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 1 times.
187 av_dict_set(m, o->metadata.opt[i].u.str, *val ? val : NULL, 0);
2885 }
2886 }
2887
2888 8539 return 0;
2889 }
2890
2891 19 static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os,
2892 int copy_metadata)
2893 {
2894 19 AVFormatContext *is = ifile->ctx;
2895 AVChapter **tmp;
2896
2897 19 tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters));
2898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!tmp)
2899 return AVERROR(ENOMEM);
2900 19 os->chapters = tmp;
2901
2902
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 16 times.
53 for (int i = 0; i < is->nb_chapters; i++) {
2903 37 AVChapter *in_ch = is->chapters[i], *out_ch;
2904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
2905 37 int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset,
2906 37 AV_TIME_BASE_Q, in_ch->time_base);
2907
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 32 times.
37 int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
2908 5 av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
2909
2910
2911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (in_ch->end < ts_off)
2912 continue;
2913
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
37 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2914 3 break;
2915
2916 34 out_ch = av_mallocz(sizeof(AVChapter));
2917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!out_ch)
2918 return AVERROR(ENOMEM);
2919
2920 34 out_ch->id = in_ch->id;
2921 34 out_ch->time_base = in_ch->time_base;
2922 34 out_ch->start = FFMAX(0, in_ch->start - ts_off);
2923 34 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2924
2925
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (copy_metadata)
2926 34 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2927
2928 34 os->chapters[os->nb_chapters++] = out_ch;
2929 }
2930 19 return 0;
2931 }
2932
2933 7 static int copy_metadata(Muxer *mux, AVFormatContext *ic,
2934 const char *outspec, const char *inspec,
2935 int *metadata_global_manual, int *metadata_streams_manual,
2936 int *metadata_chapters_manual)
2937 {
2938 7 AVFormatContext *oc = mux->fc;
2939 7 AVDictionary **meta_in = NULL;
2940 7 AVDictionary **meta_out = NULL;
2941 7 int i, ret = 0;
2942 char type_in, type_out;
2943 7 const char *istream_spec = NULL, *ostream_spec = NULL;
2944 7 int idx_in = 0, idx_out = 0;
2945
2946 7 ret = parse_meta_type(mux, inspec, &type_in, &idx_in, &istream_spec);
2947
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2948 7 ret = parse_meta_type(mux, outspec, &type_out, &idx_out, &ostream_spec);
2949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
2950 return ret;
2951
2952
4/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
7 if (type_in == 'g' || type_out == 'g' || (!*outspec && !ic))
2953 5 *metadata_global_manual = 1;
2954
7/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
7 if (type_in == 's' || type_out == 's' || (!*outspec && !ic))
2955 3 *metadata_streams_manual = 1;
2956
5/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
7 if (type_in == 'c' || type_out == 'c' || (!*outspec && !ic))
2957 *metadata_chapters_manual = 1;
2958
2959 /* ic is NULL when just disabling automatic mappings */
2960
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (!ic)
2961 2 return 0;
2962
2963 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
2964 if ((index) < 0 || (index) >= (nb_elems)) {\
2965 av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
2966 (desc), (index));\
2967 return AVERROR(EINVAL);\
2968 }
2969
2970 #define SET_DICT(type, meta, context, index)\
2971 switch (type) {\
2972 case 'g':\
2973 meta = &context->metadata;\
2974 break;\
2975 case 'c':\
2976 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
2977 meta = &context->chapters[index]->metadata;\
2978 break;\
2979 case 'p':\
2980 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
2981 meta = &context->programs[index]->metadata;\
2982 break;\
2983 case 's':\
2984 break; /* handled separately below */ \
2985 default: av_assert0(0);\
2986 }\
2987
2988
2/13
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
5 SET_DICT(type_in, meta_in, ic, idx_in);
2989
2/13
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
5 SET_DICT(type_out, meta_out, oc, idx_out);
2990
2991 /* for input streams choose first matching stream */
2992
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (type_in == 's') {
2993
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (i = 0; i < ic->nb_streams; i++) {
2994
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
2995 2 meta_in = &ic->streams[i]->metadata;
2996 2 break;
2997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (ret < 0)
2998 return ret;
2999 }
3000
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!meta_in) {
3001 av_log(mux, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
3002 return AVERROR(EINVAL);
3003 }
3004 }
3005
3006
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (type_out == 's') {
3007
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 for (i = 0; i < oc->nb_streams; i++) {
3008
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
8 if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
3009 3 meta_out = &oc->streams[i]->metadata;
3010 3 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
3011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } else if (ret < 0)
3012 return ret;
3013 }
3014 } else
3015 2 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
3016
3017 5 return 0;
3018 }
3019
3020 8539 static int copy_meta(Muxer *mux, const OptionsContext *o)
3021 {
3022 8539 OutputFile *of = &mux->of;
3023 8539 AVFormatContext *oc = mux->fc;
3024 8539 int chapters_input_file = o->chapters_input_file;
3025 8539 int metadata_global_manual = 0;
3026 8539 int metadata_streams_manual = 0;
3027 8539 int metadata_chapters_manual = 0;
3028 int ret;
3029
3030 /* copy metadata */
3031
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8539 times.
8546 for (int i = 0; i < o->metadata_map.nb_opt; i++) {
3032 char *p;
3033 7 int in_file_index = strtol(o->metadata_map.opt[i].u.str, &p, 0);
3034
3035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (in_file_index >= nb_input_files) {
3036 av_log(mux, AV_LOG_FATAL, "Invalid input file index %d while "
3037 "processing metadata maps\n", in_file_index);
3038 return AVERROR(EINVAL);
3039 }
3040 10 ret = copy_metadata(mux,
3041 5 in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL,
3042
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
10 o->metadata_map.opt[i].specifier, *p ? p + 1 : p,
3043 &metadata_global_manual, &metadata_streams_manual,
3044 &metadata_chapters_manual);
3045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
3046 return ret;
3047 }
3048
3049 /* copy chapters */
3050
1/2
✓ Branch 0 taken 8539 times.
✗ Branch 1 not taken.
8539 if (chapters_input_file >= nb_input_files) {
3051
1/2
✓ Branch 0 taken 8539 times.
✗ Branch 1 not taken.
8539 if (chapters_input_file == INT_MAX) {
3052 /* copy chapters from the first input file that has them*/
3053 8539 chapters_input_file = -1;
3054
2/2
✓ Branch 0 taken 7449 times.
✓ Branch 1 taken 8520 times.
15969 for (int i = 0; i < nb_input_files; i++)
3055
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7430 times.
7449 if (input_files[i]->ctx->nb_chapters) {
3056 19 chapters_input_file = i;
3057 19 break;
3058 }
3059 } else {
3060 av_log(mux, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
3061 chapters_input_file);
3062 return AVERROR(EINVAL);
3063 }
3064 }
3065
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8520 times.
8539 if (chapters_input_file >= 0)
3066 19 copy_chapters(input_files[chapters_input_file], of, oc,
3067 !metadata_chapters_manual);
3068
3069 /* copy global metadata by default */
3070
4/4
✓ Branch 0 taken 8535 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7348 times.
✓ Branch 3 taken 1187 times.
8539 if (!metadata_global_manual && nb_input_files){
3071 7348 av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata,
3072 AV_DICT_DONT_OVERWRITE);
3073
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 7143 times.
7348 if (of->recording_time != INT64_MAX)
3074 205 av_dict_set(&oc->metadata, "duration", NULL, 0);
3075 7348 av_dict_set(&oc->metadata, "creation_time", NULL, 0);
3076 7348 av_dict_set(&oc->metadata, "company_name", NULL, 0);
3077 7348 av_dict_set(&oc->metadata, "product_name", NULL, 0);
3078 7348 av_dict_set(&oc->metadata, "product_version", NULL, 0);
3079 }
3080
2/2
✓ Branch 0 taken 8537 times.
✓ Branch 1 taken 2 times.
8539 if (!metadata_streams_manual)
3081
2/2
✓ Branch 0 taken 9039 times.
✓ Branch 1 taken 8537 times.
17576 for (int i = 0; i < of->nb_streams; i++) {
3082 9039 OutputStream *ost = of->streams[i];
3083
3084
2/2
✓ Branch 0 taken 1395 times.
✓ Branch 1 taken 7644 times.
9039 if (!ost->ist) /* this is true e.g. for attached files */
3085 1395 continue;
3086 7644 av_dict_copy(&ost->st->metadata, ost->ist->st->metadata, AV_DICT_DONT_OVERWRITE);
3087 }
3088
3089 8539 return 0;
3090 }
3091
3092 8539 static int set_dispositions(Muxer *mux, const OptionsContext *o)
3093 {
3094 8539 OutputFile *of = &mux->of;
3095 8539 AVFormatContext *ctx = mux->fc;
3096
3097 // indexed by type+1, because AVMEDIA_TYPE_UNKNOWN=-1
3098 8539 int nb_streams[AVMEDIA_TYPE_NB + 1] = { 0 };
3099 8539 int have_default[AVMEDIA_TYPE_NB + 1] = { 0 };
3100 8539 int have_manual = 0;
3101 8539 int ret = 0;
3102
3103 const char **dispositions;
3104
3105 8539 dispositions = av_calloc(ctx->nb_streams, sizeof(*dispositions));
3106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (!dispositions)
3107 return AVERROR(ENOMEM);
3108
3109 // first, copy the input dispositions
3110
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (int i = 0; i < ctx->nb_streams; i++) {
3111 9044 OutputStream *ost = of->streams[i];
3112
3113 9044 nb_streams[ost->type + 1]++;
3114
3115 9044 opt_match_per_stream_str(ost, &o->disposition, ctx, ost->st, &dispositions[i]);
3116
3117 9044 have_manual |= !!dispositions[i];
3118
3119
2/2
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 1395 times.
9044 if (ost->ist) {
3120 7649 ost->st->disposition = ost->ist->st->disposition;
3121
3122
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 6831 times.
7649 if (ost->st->disposition & AV_DISPOSITION_DEFAULT)
3123 818 have_default[ost->type + 1] = 1;
3124 }
3125 }
3126
3127
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8531 times.
8539 if (have_manual) {
3128 // process manually set dispositions - they override the above copy
3129
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
31 for (int i = 0; i < ctx->nb_streams; i++) {
3130 23 OutputStream *ost = of->streams[i];
3131 23 const char *disp = dispositions[i];
3132
3133
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
23 if (!disp)
3134 7 continue;
3135
3136 16 ret = av_opt_set(ost->st, "disposition", disp, 0);
3137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
3138 goto finish;
3139 }
3140 } else {
3141 // For each media type with more than one stream, find a suitable stream to
3142 // mark as default, unless one is already marked default.
3143 // "Suitable" means the first of that type, skipping attached pictures.
3144
2/2
✓ Branch 0 taken 9021 times.
✓ Branch 1 taken 8531 times.
17552 for (int i = 0; i < ctx->nb_streams; i++) {
3145 9021 OutputStream *ost = of->streams[i];
3146 9021 enum AVMediaType type = ost->type;
3147
3148
4/4
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 8616 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 325 times.
9021 if (nb_streams[type + 1] < 2 || have_default[type + 1] ||
3149
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 48 times.
80 ost->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3150 8973 continue;
3151
3152 48 ost->st->disposition |= AV_DISPOSITION_DEFAULT;
3153 48 have_default[type + 1] = 1;
3154 }
3155 }
3156
3157 8531 finish:
3158 8539 av_freep(&dispositions);
3159
3160 8539 return ret;
3161 }
3162
3163 static const char *const forced_keyframes_const_names[] = {
3164 "n",
3165 "n_forced",
3166 "prev_forced_n",
3167 "prev_forced_t",
3168 "t",
3169 NULL
3170 };
3171
3172 1 static int compare_int64(const void *a, const void *b)
3173 {
3174 1 return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
3175 }
3176
3177 1 static int parse_forced_key_frames(void *log, KeyframeForceCtx *kf,
3178 const Muxer *mux, const char *spec)
3179 {
3180 const char *p;
3181 1 int n = 1, i, ret, size, index = 0;
3182 int64_t t, *pts;
3183
3184
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 for (p = spec; *p; p++)
3185
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (*p == ',')
3186 1 n++;
3187 1 size = n;
3188 1 pts = av_malloc_array(size, sizeof(*pts));
3189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pts)
3190 return AVERROR(ENOMEM);
3191
3192 1 p = spec;
3193
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < n; i++) {
3194 2 char *next = strchr(p, ',');
3195
3196
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (next)
3197 1 *next++ = 0;
3198
3199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (strstr(p, "chapters") == p) {
3200 AVChapter * const *ch = mux->fc->chapters;
3201 unsigned int nb_ch = mux->fc->nb_chapters;
3202 int j;
3203
3204 if (nb_ch > INT_MAX - size) {
3205 ret = AVERROR(ERANGE);
3206 goto fail;
3207 }
3208 size += nb_ch - 1;
3209 pts = av_realloc_f(pts, size, sizeof(*pts));
3210 if (!pts)
3211 return AVERROR(ENOMEM);
3212
3213 if (p[8]) {
3214 ret = av_parse_time(&t, p + 8, 1);
3215 if (ret < 0) {
3216 av_log(log, AV_LOG_ERROR,
3217 "Invalid chapter time offset: %s\n", p + 8);
3218 goto fail;
3219 }
3220 } else
3221 t = 0;
3222
3223 for (j = 0; j < nb_ch; j++) {
3224 const AVChapter *c = ch[j];
3225 av_assert1(index < size);
3226 pts[index++] = av_rescale_q(c->start, c->time_base,
3227 AV_TIME_BASE_Q) + t;
3228 }
3229
3230 } else {
3231 av_assert1(index < size);
3232 2 ret = av_parse_time(&t, p, 1);
3233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
3234 av_log(log, AV_LOG_ERROR, "Invalid keyframe time: %s\n", p);
3235 goto fail;
3236 }
3237
3238 2 pts[index++] = t;
3239 }
3240
3241 2 p = next;
3242 }
3243
3244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(index == size);
3245 1 qsort(pts, size, sizeof(*pts), compare_int64);
3246 1 kf->nb_pts = size;
3247 1 kf->pts = pts;
3248
3249 1 return 0;
3250 fail:
3251 av_freep(&pts);
3252 return ret;
3253 }
3254
3255 8539 static int process_forced_keyframes(Muxer *mux, const OptionsContext *o)
3256 {
3257
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (int i = 0; i < mux->of.nb_streams; i++) {
3258 9044 OutputStream *ost = mux->of.streams[i];
3259 9044 const char *forced_keyframes = NULL;
3260
3261 9044 opt_match_per_stream_str(ost, &o->forced_key_frames,
3262 mux->fc, ost->st, &forced_keyframes);
3263
3264
2/2
✓ Branch 0 taken 7261 times.
✓ Branch 1 taken 1783 times.
9044 if (!(ost->type == AVMEDIA_TYPE_VIDEO &&
3265
4/4
✓ Branch 0 taken 6877 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 6872 times.
✓ Branch 3 taken 5 times.
7261 ost->enc && forced_keyframes))
3266 9039 continue;
3267
3268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!strncmp(forced_keyframes, "expr:", 5)) {
3269 int ret = av_expr_parse(&ost->kf.pexpr, forced_keyframes + 5,
3270 forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL);
3271 if (ret < 0) {
3272 av_log(ost, AV_LOG_ERROR,
3273 "Invalid force_key_frames expression '%s'\n", forced_keyframes + 5);
3274 return ret;
3275 }
3276 ost->kf.expr_const_values[FKF_N] = 0;
3277 ost->kf.expr_const_values[FKF_N_FORCED] = 0;
3278 ost->kf.expr_const_values[FKF_PREV_FORCED_N] = NAN;
3279 ost->kf.expr_const_values[FKF_PREV_FORCED_T] = NAN;
3280
3281 // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
3282 // parse it only for static kf timings
3283
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 } else if (!strcmp(forced_keyframes, "source")) {
3284 3 ost->kf.type = KF_FORCE_SOURCE;
3285 #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
3286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (!strcmp(forced_keyframes, "source_no_drop")) {
3287 av_log(ost, AV_LOG_WARNING, "The 'source_no_drop' value for "
3288 "-force_key_frames is deprecated, use just 'source'\n");
3289 ost->kf.type = KF_FORCE_SOURCE;
3290 #endif
3291
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (!strcmp(forced_keyframes, "scd_metadata")) {
3292 1 ost->kf.type = KF_FORCE_SCD_METADATA;
3293 } else {
3294 1 int ret = parse_forced_key_frames(ost, &ost->kf, mux, forced_keyframes);
3295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3296 return ret;
3297 }
3298 }
3299
3300 8539 return 0;
3301 }
3302
3303 9008 static const char *output_file_item_name(void *obj)
3304 {
3305 9008 const Muxer *mux = obj;
3306
3307 9008 return mux->log_name;
3308 }
3309
3310 static const AVClass output_file_class = {
3311 .class_name = "OutputFile",
3312 .version = LIBAVUTIL_VERSION_INT,
3313 .item_name = output_file_item_name,
3314 .category = AV_CLASS_CATEGORY_MUXER,
3315 };
3316
3317 8539 static Muxer *mux_alloc(void)
3318 {
3319 8539 Muxer *mux = allocate_array_elem(&output_files, sizeof(*mux), &nb_output_files);
3320
3321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (!mux)
3322 return NULL;
3323
3324 8539 mux->of.class = &output_file_class;
3325 8539 mux->of.index = nb_output_files - 1;
3326
3327 8539 snprintf(mux->log_name, sizeof(mux->log_name), "out#%d", mux->of.index);
3328
3329 8539 return mux;
3330 }
3331
3332 8539 int of_open(const OptionsContext *o, const char *filename, Scheduler *sch)
3333 {
3334 Muxer *mux;
3335 AVFormatContext *oc;
3336 int err;
3337 OutputFile *of;
3338
3339 8539 int64_t recording_time = o->recording_time;
3340 8539 int64_t stop_time = o->stop_time;
3341
3342 8539 mux = mux_alloc();
3343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (!mux)
3344 return AVERROR(ENOMEM);
3345
3346 8539 of = &mux->of;
3347
3348
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8539 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
3349 stop_time = INT64_MAX;
3350 av_log(mux, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
3351 }
3352
3353
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8539 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
3354 int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time;
3355 if (stop_time <= start_time) {
3356 av_log(mux, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
3357 return AVERROR(EINVAL);
3358 } else {
3359 recording_time = stop_time - start_time;
3360 }
3361 }
3362
3363 8539 of->recording_time = recording_time;
3364 8539 of->start_time = o->start_time;
3365
3366 8539 mux->limit_filesize = o->limit_filesize;
3367 8539 av_dict_copy(&mux->opts, o->g->format_opts, 0);
3368
3369
2/2
✓ Branch 0 taken 2734 times.
✓ Branch 1 taken 5805 times.
8539 if (!strcmp(filename, "-"))
3370 2734 filename = "pipe:";
3371
3372 8539 err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
3373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (!oc) {
3374 av_log(mux, AV_LOG_FATAL, "Error initializing the muxer for %s: %s\n",
3375 filename, av_err2str(err));
3376 return err;
3377 }
3378 8539 mux->fc = oc;
3379
3380 8539 av_strlcat(mux->log_name, "/", sizeof(mux->log_name));
3381 8539 av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name));
3382
3383
3384
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 7223 times.
8539 if (recording_time != INT64_MAX)
3385 1316 oc->duration = recording_time;
3386
3387 8539 oc->interrupt_callback = int_cb;
3388
3389
2/2
✓ Branch 0 taken 1862 times.
✓ Branch 1 taken 6677 times.
8539 if (o->bitexact) {
3390 1862 oc->flags |= AVFMT_FLAG_BITEXACT;
3391 1862 of->bitexact = 1;
3392 } else {
3393 6677 of->bitexact = check_opt_bitexact(oc, mux->opts, "fflags",
3394 AVFMT_FLAG_BITEXACT);
3395 }
3396
3397 8539 err = sch_add_mux(sch, muxer_thread, mux_check_init, mux,
3398 8539 !strcmp(oc->oformat->name, "rtp"), o->thread_queue_size);
3399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3400 return err;
3401 8539 mux->sch = sch;
3402 8539 mux->sch_idx = err;
3403
3404 /* create all output streams for this file */
3405 8539 err = create_streams(mux, o);
3406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3407 return err;
3408
3409 /* check if all codec options have been used */
3410 8539 err = check_avoptions_used(o->g->codec_opts, mux->enc_opts_used, mux, 0);
3411 8539 av_dict_free(&mux->enc_opts_used);
3412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3413 return err;
3414
3415 /* check filename in case of an image number is expected */
3416
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8539 if (oc->oformat->flags & AVFMT_NEEDNUMBER && !av_filename_number_test(oc->url)) {
3417 av_log(mux, AV_LOG_FATAL,
3418 "Output filename '%s' does not contain a numeric pattern like "
3419 "'%%d', which is required by output format '%s'.\n",
3420 oc->url, oc->oformat->name);
3421 return AVERROR(EINVAL);
3422 }
3423
3424
2/2
✓ Branch 0 taken 8433 times.
✓ Branch 1 taken 106 times.
8539 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
3425 /* test if it already exists to avoid losing precious files */
3426 8433 err = assert_file_overwrite(filename);
3427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8433 times.
8433 if (err < 0)
3428 return err;
3429
3430 /* open the file */
3431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8433 times.
8433 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
3432 8433 &oc->interrupt_callback,
3433 &mux->opts)) < 0) {
3434 av_log(mux, AV_LOG_FATAL, "Error opening output %s: %s\n",
3435 filename, av_err2str(err));
3436 return err;
3437 }
3438
4/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 45 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 51 times.
106 } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename)) {
3439 10 err = assert_file_overwrite(filename);
3440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (err < 0)
3441 return err;
3442 }
3443
3444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (o->mux_preload) {
3445 av_dict_set_int(&mux->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
3446 }
3447 8539 oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
3448
3449 /* copy metadata and chapters from input files */
3450 8539 err = copy_meta(mux, o);
3451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3452 return err;
3453
3454 8539 err = of_add_groups(mux, o);
3455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3456 return err;
3457
3458 8539 err = of_add_programs(mux, o);
3459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3460 return err;
3461
3462 8539 err = of_add_metadata(of, oc, o);
3463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0)
3464 return err;
3465
3466 8539 err = set_dispositions(mux, o);
3467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0) {
3468 av_log(mux, AV_LOG_FATAL, "Error setting output stream dispositions\n");
3469 return err;
3470 }
3471
3472 // parse forced keyframe specifications;
3473 // must be done after chapters are created
3474 8539 err = process_forced_keyframes(mux, o);
3475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0) {
3476 av_log(mux, AV_LOG_FATAL, "Error processing forced keyframes\n");
3477 return err;
3478 }
3479
3480 8539 err = setup_sync_queues(mux, oc, o->shortest_buf_duration * AV_TIME_BASE,
3481 8539 o->shortest);
3482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8539 times.
8539 if (err < 0) {
3483 av_log(mux, AV_LOG_FATAL, "Error setting up output sync queues\n");
3484 return err;
3485 }
3486
3487 8539 of->url = filename;
3488
3489 /* initialize streamcopy streams. */
3490
2/2
✓ Branch 0 taken 9044 times.
✓ Branch 1 taken 8539 times.
17583 for (int i = 0; i < of->nb_streams; i++) {
3491 9044 OutputStream *ost = of->streams[i];
3492
3493
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 8290 times.
9044 if (!ost->enc) {
3494 754 err = of_stream_init(of, ost, NULL);
3495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 754 times.
754 if (err < 0)
3496 return err;
3497 }
3498 }
3499
3500 8539 return 0;
3501 }
3502