FFmpeg coverage


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