FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux_init.c
Date: 2023-03-22 23:59:29
Exec Total Coverage
Lines: 869 1350 64.4%
Functions: 37 44 84.1%
Branches: 771 1979 39.0%

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