FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux_init.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 831 1205 69.0%
Functions: 32 36 88.9%
Branches: 748 1745 42.9%

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