FFmpeg coverage


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