FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux.c
Date: 2026-09-30 05:00:11
Exec Total Coverage
Lines: 385 510 75.5%
Functions: 22 24 91.7%
Branches: 211 294 71.8%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdatomic.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "ffmpeg.h"
24 #include "ffmpeg_mux.h"
25 #include "ffmpeg_utils.h"
26 #include "sync_queue.h"
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/fifo.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/log.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/stereo3d.h"
34 #include "libavutil/time.h"
35 #include "libavutil/timestamp.h"
36
37 #include "libavcodec/packet.h"
38
39 #include "libavformat/avformat.h"
40 #include "libavformat/avio.h"
41
42 typedef struct MuxThreadContext {
43 AVPacket *pkt;
44 AVPacket *fix_sub_duration_pkt;
45 } MuxThreadContext;
46
47 63571 static Muxer *mux_from_of(OutputFile *of)
48 {
49 63571 return (Muxer*)of;
50 }
51
52 620211 static int64_t filesize(AVIOContext *pb)
53 {
54 620211 int64_t ret = -1;
55
56
2/2
✓ Branch 0 taken 605325 times.
✓ Branch 1 taken 14886 times.
620211 if (pb) {
57 605325 ret = avio_size(pb);
58
2/2
✓ Branch 0 taken 147993 times.
✓ Branch 1 taken 457332 times.
605325 if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
59 147993 ret = avio_tell(pb);
60 }
61
62 620211 return ret;
63 }
64
65 ✗ static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
66 {
67 static const char *desc[] = {
68 [LATENCY_PROBE_DEMUX] = "demux",
69 [LATENCY_PROBE_DEC_PRE] = "decode",
70 [LATENCY_PROBE_DEC_POST] = "decode",
71 [LATENCY_PROBE_FILTER_PRE] = "filter",
72 [LATENCY_PROBE_FILTER_POST] = "filter",
73 [LATENCY_PROBE_ENC_PRE] = "encode",
74 [LATENCY_PROBE_ENC_POST] = "encode",
75 [LATENCY_PROBE_NB] = "mux",
76 };
77
78 char latency[512];
79
80 ✗ *latency = 0;
81 ✗ if (pkt->opaque_ref) {
82 ✗ const FrameData *fd = (FrameData*)pkt->opaque_ref->data;
83 ✗ int64_t now = av_gettime_relative();
84 ✗ int64_t total = INT64_MIN;
85
86 int next;
87
88 ✗ for (unsigned i = 0; i < FF_ARRAY_ELEMS(fd->wallclock); i = next) {
89 ✗ int64_t val = fd->wallclock[i];
90
91 ✗ next = i + 1;
92
93 ✗ if (val == INT64_MIN)
94 ✗ continue;
95
96 ✗ if (total == INT64_MIN) {
97 ✗ total = now - val;
98 ✗ snprintf(latency, sizeof(latency), "total:%gms", total / 1e3);
99 }
100
101 // find the next valid entry
102 ✗ for (; next <= FF_ARRAY_ELEMS(fd->wallclock); next++) {
103 ✗ int64_t val_next = (next == FF_ARRAY_ELEMS(fd->wallclock)) ?
104 ✗ now : fd->wallclock[next];
105 int64_t diff;
106
107 ✗ if (val_next == INT64_MIN)
108 ✗ continue;
109 ✗ diff = val_next - val;
110
111 // print those stages that take at least 5% of total
112 ✗ if (100. * diff > 5. * total) {
113 ✗ av_strlcat(latency, ", ", sizeof(latency));
114
115 ✗ if (!strcmp(desc[i], desc[next]))
116 ✗ av_strlcat(latency, desc[i], sizeof(latency));
117 else
118 ✗ av_strlcatf(latency, sizeof(latency), "%s-%s:",
119 desc[i], desc[next]);
120
121 ✗ av_strlcatf(latency, sizeof(latency), " %gms/%d%%",
122 ✗ diff / 1e3, (int)(100. * diff / total));
123 }
124
125 ✗ break;
126 }
127
128 }
129 }
130
131 ✗ av_log(ost, AV_LOG_INFO, "muxer <- pts:%s pts_time:%s dts:%s dts_time:%s "
132 "duration:%s duration_time:%s size:%d latency(%s)\n",
133 ✗ av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
134 ✗ av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
135 ✗ av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
136 ✗ pkt->size, *latency ? latency : "N/A");
137 ✗ }
138
139 611364 static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
140 {
141 611364 OutputStream *ost = &ms->ost;
142
143 // rescale timestamps to the stream timebase
144
4/4
✓ Branch 0 taken 446273 times.
✓ Branch 1 taken 165091 times.
✓ Branch 2 taken 59191 times.
✓ Branch 3 taken 387082 times.
611364 if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
145 // use av_rescale_delta() for streamcopying audio, to preserve
146 // accuracy with coarse input timebases
147 59191 int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
148
149
2/2
✓ Branch 0 taken 17018 times.
✓ Branch 1 taken 42173 times.
59191 if (!duration)
150 17018 duration = ost->st->codecpar->frame_size;
151
152 118382 pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
153 59191 (AVRational){1, ost->st->codecpar->sample_rate}, duration,
154 59191 &ms->ts_rescale_delta_last, ost->st->time_base);
155 59191 pkt->pts = pkt->dts;
156
157 59191 pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
158 } else
159 552173 av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
160 611364 pkt->time_base = ost->st->time_base;
161
162
2/2
✓ Branch 0 taken 278781 times.
✓ Branch 1 taken 332583 times.
611364 if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
163
2/2
✓ Branch 0 taken 278777 times.
✓ Branch 1 taken 4 times.
278781 if (pkt->dts != AV_NOPTS_VALUE &&
164
2/2
✓ Branch 0 taken 277660 times.
✓ Branch 1 taken 1117 times.
278777 pkt->pts != AV_NOPTS_VALUE &&
165
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 277658 times.
277660 pkt->dts > pkt->pts) {
166 2 av_log(ost, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64", replacing by guess\n",
167 pkt->dts, pkt->pts);
168 2 pkt->pts =
169 2 pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
170 2 - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
171 2 - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
172 }
173
6/6
✓ Branch 0 taken 99705 times.
✓ Branch 1 taken 179076 times.
✓ Branch 2 taken 1836 times.
✓ Branch 3 taken 97869 times.
✓ Branch 4 taken 1759 times.
✓ Branch 5 taken 77 times.
278781 if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
174
2/2
✓ Branch 0 taken 278700 times.
✓ Branch 1 taken 4 times.
278704 pkt->dts != AV_NOPTS_VALUE &&
175
2/2
✓ Branch 0 taken 272069 times.
✓ Branch 1 taken 6631 times.
278700 ms->last_mux_dts != AV_NOPTS_VALUE) {
176 272069 int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
177
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 272013 times.
272069 if (pkt->dts < max) {
178
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 33 times.
56 int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (exit_on_error)
180 ✗ loglevel = AV_LOG_ERROR;
181 56 av_log(ost, loglevel, "Non-monotonic DTS; "
182 "previous: %"PRId64", current: %"PRId64"; ",
183 ms->last_mux_dts, pkt->dts);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (exit_on_error) {
185 ✗ return AVERROR(EINVAL);
186 }
187
188 56 av_log(ost, loglevel, "changing to %"PRId64". This may result "
189 "in incorrect timestamps in the output file.\n",
190 max);
191
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (pkt->pts >= pkt->dts)
192 56 pkt->pts = FFMAX(pkt->pts, max);
193 56 pkt->dts = max;
194 }
195 }
196 }
197 611364 ms->last_mux_dts = pkt->dts;
198
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611364 times.
611364 if (debug_ts)
200 ✗ mux_log_debug_ts(ost, pkt);
201
202 611364 return 0;
203 }
204
205 611364 static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
206 {
207 611364 MuxStream *ms = ms_from_ost(ost);
208 611364 AVFormatContext *s = mux->fc;
209 int64_t fs;
210 uint64_t frame_num;
211 int ret;
212
213 611364 fs = filesize(s->pb);
214 611364 atomic_store(&mux->last_filesize, fs);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611364 times.
611364 if (fs >= mux->limit_filesize) {
216 ✗ ret = AVERROR_EOF;
217 ✗ goto fail;
218 }
219
220 611364 ret = mux_fixup_ts(mux, ms, pkt);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611364 times.
611364 if (ret < 0)
222 ✗ goto fail;
223
224 611364 ms->data_size_mux += pkt->size;
225 611364 frame_num = atomic_fetch_add(&ost->packets_written, 1);
226
227 611364 pkt->stream_index = ost->index;
228
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611364 times.
611364 if (ms->stats.io)
230 ✗ enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
231
232 611364 ret = av_interleaved_write_frame(s, pkt);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611364 times.
611364 if (ret < 0) {
234 ✗ av_log(ost, AV_LOG_ERROR,
235 "Error submitting a packet to the muxer: %s\n",
236 ✗ av_err2str(ret));
237 ✗ goto fail;
238 }
239
240 611364 return 0;
241 ✗ fail:
242 ✗ av_packet_unref(pkt);
243 ✗ return ret;
244 }
245
246 620893 static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof)
247 {
248 620893 OutputFile *of = &mux->of;
249
250
2/2
✓ Branch 0 taken 1013 times.
✓ Branch 1 taken 619880 times.
620893 if (ms->sq_idx_mux >= 0) {
251 1013 int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt));
252
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 62 times.
1013 if (ret < 0) {
253
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (ret == AVERROR_EOF)
254 62 *stream_eof = 1;
255
256 62 return ret;
257 }
258
259 890 while (1) {
260 1841 ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
261
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 890 times.
1841 if (ret < 0) {
262 /* n.b.: We forward EOF from the sync queue, terminating muxing.
263 * This assumes that if a muxing sync queue is present, then all
264 * the streams use it. That is true currently, but may change in
265 * the future, then this code needs to be revisited.
266 */
267
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 931 times.
951 return ret == AVERROR(EAGAIN) ? 0 : ret;
268 }
269
270 890 ret = write_packet(mux, of->streams[ret],
271 mux->sq_pkt);
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890 times.
890 if (ret < 0)
273 ✗ return ret;
274 }
275
2/2
✓ Branch 0 taken 610474 times.
✓ Branch 1 taken 9406 times.
619880 } else if (pkt)
276 610474 return write_packet(mux, &ms->ost, pkt);
277
278 9406 return 0;
279 }
280
281 static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
282
283 /* apply the output bitstream filters */
284 621946 static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
285 OutputStream *ost, AVPacket *pkt, int *stream_eof)
286 {
287 621946 MuxStream *ms = ms_from_ost(ost);
288 const char *err_msg;
289 int ret;
290
291
4/4
✓ Branch 0 taken 612623 times.
✓ Branch 1 taken 9323 times.
✓ Branch 2 taken 80528 times.
✓ Branch 3 taken 532095 times.
621946 if (pkt && !ost->enc) {
292 80528 ret = of_streamcopy(&mux->of, ost, pkt);
293
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 80255 times.
80528 if (ret == AVERROR(EAGAIN))
294 273 return 0;
295
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 80187 times.
80255 else if (ret == AVERROR_EOF) {
296 68 av_packet_unref(pkt);
297 68 pkt = NULL;
298 68 *stream_eof = 1;
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80187 times.
80187 } else if (ret < 0)
300 ✗ goto fail;
301 }
302
303 // emit heartbeat for -fix_sub_duration;
304 // we are only interested in heartbeats on on random access points.
305
4/4
✓ Branch 0 taken 612282 times.
✓ Branch 1 taken 9391 times.
✓ Branch 2 taken 571834 times.
✓ Branch 3 taken 40448 times.
621673 if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
306 571834 mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
307 571834 mt->fix_sub_duration_pkt->pts = pkt->pts;
308 571834 mt->fix_sub_duration_pkt->time_base = pkt->time_base;
309
310 571834 ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
311 571834 mt->fix_sub_duration_pkt);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 571834 times.
571834 if (ret < 0)
313 ✗ goto fail;
314 }
315
316
2/2
✓ Branch 0 taken 11256 times.
✓ Branch 1 taken 610417 times.
621673 if (ms->bsf_ctx) {
317 11256 int bsf_eof = 0;
318
319
2/2
✓ Branch 0 taken 11098 times.
✓ Branch 1 taken 158 times.
11256 if (pkt)
320 11098 av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
321
322 11256 ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11256 times.
11256 if (ret < 0) {
324 ✗ err_msg = "submitting a packet for bitstream filtering";
325 ✗ goto fail;
326 }
327
328
2/2
✓ Branch 0 taken 21541 times.
✓ Branch 1 taken 186 times.
21727 while (!bsf_eof) {
329 21541 ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
330
2/2
✓ Branch 0 taken 11063 times.
✓ Branch 1 taken 10478 times.
21541 if (ret == AVERROR(EAGAIN))
331 11063 return 0;
332
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 10292 times.
10478 else if (ret == AVERROR_EOF)
333 186 bsf_eof = 1;
334
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10290 times.
10292 else if (ret < 0) {
335 2 av_log(ost, AV_LOG_ERROR,
336 "Error applying bitstream filters to a packet: %s",
337 2 av_err2str(ret));
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (exit_on_error)
339 ✗ return ret;
340 2 continue;
341 }
342
343
2/2
✓ Branch 0 taken 10290 times.
✓ Branch 1 taken 186 times.
10476 if (!bsf_eof)
344 10290 ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
345
346
2/2
✓ Branch 0 taken 10290 times.
✓ Branch 1 taken 186 times.
10476 ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
347
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10469 times.
10476 if (ret < 0)
348 7 goto mux_fail;
349 }
350 186 *stream_eof = 1;
351 } else {
352 610417 ret = sync_queue_process(mux, ms, pkt, stream_eof);
353
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 610342 times.
610417 if (ret < 0)
354 75 goto mux_fail;
355 }
356
357
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 610275 times.
610528 return *stream_eof ? AVERROR_EOF : 0;
358
359 82 mux_fail:
360 82 err_msg = "submitting a packet to the muxer";
361
362 82 fail:
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret != AVERROR_EOF)
364 ✗ av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
365 82 return ret;
366 }
367
368 8847 static void thread_set_name(Muxer *mux)
369 {
370 char name[16];
371 8847 snprintf(name, sizeof(name), "mux%d:%s",
372 8847 mux->of.index, mux->fc->oformat->name);
373 8847 ff_thread_setname(name);
374 8847 }
375
376 8847 static void mux_thread_uninit(MuxThreadContext *mt)
377 {
378 8847 av_packet_free(&mt->pkt);
379 8847 av_packet_free(&mt->fix_sub_duration_pkt);
380
381 8847 memset(mt, 0, sizeof(*mt));
382 8847 }
383
384 8847 static int mux_thread_init(MuxThreadContext *mt)
385 {
386 8847 memset(mt, 0, sizeof(*mt));
387
388 8847 mt->pkt = av_packet_alloc();
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8847 times.
8847 if (!mt->pkt)
390 ✗ goto fail;
391
392 8847 mt->fix_sub_duration_pkt = av_packet_alloc();
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8847 times.
8847 if (!mt->fix_sub_duration_pkt)
394 ✗ goto fail;
395
396 8847 return 0;
397
398 ✗ fail:
399 ✗ mux_thread_uninit(mt);
400 ✗ return AVERROR(ENOMEM);
401 }
402
403 8847 int muxer_thread(void *arg)
404 {
405 8847 Muxer *mux = arg;
406 8847 OutputFile *of = &mux->of;
407
408 MuxThreadContext mt;
409
410 8847 int ret = 0;
411
412 8847 ret = mux_thread_init(&mt);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8847 times.
8847 if (ret < 0)
414 ✗ goto finish;
415
416 8847 thread_set_name(mux);
417
418 621926 while (1) {
419 OutputStream *ost;
420 630773 int stream_idx, stream_eof = 0;
421
422 630773 ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
423 630773 stream_idx = mt.pkt->stream_index;
424
2/2
✓ Branch 0 taken 8827 times.
✓ Branch 1 taken 621946 times.
630773 if (stream_idx < 0) {
425 8827 av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
426 8827 ret = 0;
427 8847 break;
428 }
429
430 621946 ost = of->streams[mux->sch_stream_idx[stream_idx]];
431 621946 mt.pkt->stream_index = ost->index;
432 621946 mt.pkt->flags &= ~AV_PKT_FLAG_TRUSTED;
433
434
2/2
✓ Branch 0 taken 612623 times.
✓ Branch 1 taken 9323 times.
621946 ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
435 621946 av_packet_unref(mt.pkt);
436
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 621611 times.
621946 if (ret == AVERROR_EOF) {
437
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 20 times.
335 if (stream_eof) {
438 315 sch_mux_receive_finish(mux->sch, of->index, stream_idx);
439 } else {
440 20 av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
441 20 ret = 0;
442 20 break;
443 }
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621611 times.
621611 } else if (ret < 0) {
445 ✗ av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
446 ✗ break;
447 }
448 }
449
450 8847 finish:
451 8847 mux_thread_uninit(&mt);
452
453 8847 return ret;
454 }
455
456 80528 static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
457 {
458 80528 MuxStream *ms = ms_from_ost(ost);
459
1/2
✓ Branch 0 taken 80528 times.
✗ Branch 1 not taken.
80528 FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
460
1/2
✓ Branch 0 taken 80528 times.
✗ Branch 1 not taken.
80528 int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
461
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 79455 times.
80528 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
462 int64_t ts_offset;
463
464
2/2
✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 78862 times.
80528 if (of->recording_time != INT64_MAX &&
465
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1598 times.
1666 dts >= of->recording_time + start_time)
466 68 return AVERROR_EOF;
467
468
4/4
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 79246 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 1124 times.
80460 if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
469
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 !ms->copy_initial_nonkeyframes)
470 90 return AVERROR(EAGAIN);
471
472
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 79246 times.
80370 if (!ms->streamcopy_started) {
473
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1124 if (!ms->copy_prior_start &&
474 ✗ (pkt->pts == AV_NOPTS_VALUE ?
475 ✗ dts < ms->ts_copy_start :
476 ✗ pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
477 ✗ return AVERROR(EAGAIN);
478
479
4/4
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 937 times.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 4 times.
1124 if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
480 183 return AVERROR(EAGAIN);
481 }
482
483 80187 ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
484
485
2/2
✓ Branch 0 taken 71595 times.
✓ Branch 1 taken 8592 times.
80187 if (pkt->pts != AV_NOPTS_VALUE)
486 71595 pkt->pts -= ts_offset;
487
488
2/2
✓ Branch 0 taken 8376 times.
✓ Branch 1 taken 71811 times.
80187 if (pkt->dts == AV_NOPTS_VALUE) {
489 8376 pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
490
2/2
✓ Branch 0 taken 59319 times.
✓ Branch 1 taken 12492 times.
71811 } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
491 59319 pkt->pts = pkt->dts - ts_offset;
492 }
493
494 80187 pkt->dts -= ts_offset;
495
496 80187 ms->streamcopy_started = 1;
497
498 80187 return 0;
499 }
500
501 int print_sdp(const char *filename);
502
503 ✗ int print_sdp(const char *filename)
504 {
505 char sdp[16384];
506 ✗ int j = 0, ret;
507 AVIOContext *sdp_pb;
508 AVFormatContext **avc;
509
510 ✗ avc = av_malloc_array(nb_output_files, sizeof(*avc));
511 ✗ if (!avc)
512 ✗ return AVERROR(ENOMEM);
513 ✗ for (int i = 0; i < nb_output_files; i++) {
514 ✗ Muxer *mux = mux_from_of(output_files[i]);
515
516 ✗ if (!strcmp(mux->fc->oformat->name, "rtp")) {
517 ✗ avc[j] = mux->fc;
518 ✗ j++;
519 }
520 }
521
522 ✗ if (!j) {
523 ✗ av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
524 ✗ ret = AVERROR(EINVAL);
525 ✗ goto fail;
526 }
527
528 ✗ ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
529 ✗ if (ret < 0)
530 ✗ goto fail;
531
532 ✗ if (!filename) {
533 ✗ printf("SDP:\n%s\n", sdp);
534 ✗ fflush(stdout);
535 } else {
536 ✗ ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
537 ✗ if (ret < 0) {
538 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
539 ✗ goto fail;
540 }
541
542 ✗ avio_print(sdp_pb, sdp);
543 ✗ avio_closep(&sdp_pb);
544 }
545
546 ✗ fail:
547 ✗ av_freep(&avc);
548 ✗ return ret;
549 }
550
551 8847 int mux_check_init(void *arg)
552 {
553 8847 Muxer *mux = arg;
554 8847 OutputFile *of = &mux->of;
555 8847 AVFormatContext *fc = mux->fc;
556 int ret;
557
558 8847 ret = avformat_write_header(fc, &mux->opts);
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8847 times.
8847 if (ret < 0) {
560 ✗ av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
561 ✗ "parameters ?): %s\n", av_err2str(ret));
562 ✗ return ret;
563 }
564 //assert_avoptions(of->opts);
565 8847 mux->header_written = 1;
566
567 8847 av_dump_format(fc, of->index, fc->url, 1);
568 8847 atomic_fetch_add(&nb_output_dumped, 1);
569
570 8847 return 0;
571 }
572
573 9500 static int bsf_init(MuxStream *ms)
574 {
575 9500 OutputStream *ost = &ms->ost;
576 9500 AVBSFContext *ctx = ms->bsf_ctx;
577 int ret;
578
579
2/2
✓ Branch 0 taken 9307 times.
✓ Branch 1 taken 193 times.
9500 if (!ctx)
580 9307 return avcodec_parameters_copy(ost->st->codecpar, ms->par_in);
581
582 193 ret = avcodec_parameters_copy(ctx->par_in, ms->par_in);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (ret < 0)
584 ✗ return ret;
585
586 193 ctx->time_base_in = ost->st->time_base;
587
588 193 ret = av_bsf_init(ctx);
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (ret < 0) {
590 ✗ av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
591 ✗ ctx->filter->name);
592 ✗ return ret;
593 }
594
595 193 ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (ret < 0)
597 ✗ return ret;
598 193 ost->st->time_base = ctx->time_base_out;
599
600 193 ms->bsf_pkt = av_packet_alloc();
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (!ms->bsf_pkt)
602 ✗ return AVERROR(ENOMEM);
603
604 193 return 0;
605 }
606
607 9500 int of_stream_init(OutputFile *of, OutputStream *ost,
608 const AVCodecContext *enc_ctx)
609 {
610 9500 Muxer *mux = mux_from_of(of);
611 9500 MuxStream *ms = ms_from_ost(ost);
612 int ret;
613
614
2/2
✓ Branch 0 taken 8534 times.
✓ Branch 1 taken 966 times.
9500 if (enc_ctx) {
615 // use upstream time base unless it has been overridden previously
616
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8530 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
8534 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
617 8530 ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1});
618
619 8534 ost->st->avg_frame_rate = enc_ctx->framerate;
620 8534 ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio;
621
622 8534 ret = avcodec_parameters_from_context(ms->par_in, enc_ctx);
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8534 times.
8534 if (ret < 0) {
624 ✗ av_log(ost, AV_LOG_FATAL,
625 "Error initializing the output stream codec parameters.\n");
626 ✗ return ret;
627 }
628 }
629
630 /* initialize bitstream filters for the output stream
631 * needs to be done here, because the codec id for streamcopy is not
632 * known until now */
633 9500 ret = bsf_init(ms);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9500 times.
9500 if (ret < 0)
635 ✗ return ret;
636
637
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9495 times.
9500 if (ms->stereo3d_set) {
638 5 AVCodecParameters *par = ost->st->codecpar;
639 AVStereo3D *stereo;
640 size_t size;
641
642 5 stereo = av_stereo3d_alloc_size(&size);
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!stereo)
644 ✗ return AVERROR(ENOMEM);
645 5 stereo->type = ms->stereo3d_type;
646
647
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
648 AV_PKT_DATA_STEREO3D))
649 2 av_log(ost, AV_LOG_INFO, "Overriding existing stereoscopic 3D side data\n");
650
651
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!av_packet_side_data_add(&par->coded_side_data, &par->nb_coded_side_data,
652 AV_PKT_DATA_STEREO3D, stereo, size, 0)) {
653 ✗ av_freep(&stereo);
654 ✗ return AVERROR(ENOMEM);
655 }
656
657 /* Keep Matroska/WebM metadata consistent with the explicit layout,
658 * as their muxer consults the tag before the side data. */
659
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (!strcmp(mux->fc->oformat->name, "matroska") ||
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !strcmp(mux->fc->oformat->name, "webm")) {
661 4 const char *stereo_mode =
662
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 ms->stereo3d_type == AV_STEREO3D_2D ? "mono" :
663
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 ms->stereo3d_type == AV_STEREO3D_SIDEBYSIDE ? "left_right" :
664 "top_bottom";
665 4 ret = av_dict_set(&ost->st->metadata, "stereo_mode", stereo_mode, 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
667 ✗ return ret;
668 }
669 }
670
671
2/2
✓ Branch 0 taken 6266 times.
✓ Branch 1 taken 3234 times.
9500 if (ms->stream_duration) {
672 6266 ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
673 6266 ost->st->time_base);
674 }
675
676
2/2
✓ Branch 0 taken 9499 times.
✓ Branch 1 taken 1 times.
9500 if (ms->sch_idx >= 0)
677 9499 return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
678
679 1 return 0;
680 }
681
682 8847 static int check_written(OutputFile *of)
683 {
684 8847 int64_t total_packets_written = 0;
685 8847 int pass1_used = 1;
686 8847 int ret = 0;
687
688
2/2
✓ Branch 0 taken 9500 times.
✓ Branch 1 taken 8847 times.
18347 for (int i = 0; i < of->nb_streams; i++) {
689 9500 OutputStream *ost = of->streams[i];
690 9500 uint64_t packets_written = atomic_load(&ost->packets_written);
691
692 9500 total_packets_written += packets_written;
693
694
2/2
✓ Branch 0 taken 8534 times.
✓ Branch 1 taken 966 times.
9500 if (ost->enc &&
695
2/2
✓ Branch 0 taken 8526 times.
✓ Branch 1 taken 8 times.
8534 (ost->enc->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
696 != AV_CODEC_FLAG_PASS1)
697 8526 pass1_used = 0;
698
699
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 9403 times.
9500 if (!packets_written &&
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
701 ✗ av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
702 ✗ ret = err_merge(ret, AVERROR(EINVAL));
703 }
704 }
705
706
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8819 times.
8847 if (!total_packets_written) {
707 28 int level = AV_LOG_WARNING;
708
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
710 ✗ ret = err_merge(ret, AVERROR(EINVAL));
711 ✗ level = AV_LOG_FATAL;
712 }
713
714
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3 times.
28 av_log(of, level, "Output file is empty, nothing was encoded%s\n",
715 pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
716 }
717
718 8847 return ret;
719 }
720
721 8847 static void mux_final_stats(Muxer *mux)
722 {
723 8847 OutputFile *of = &mux->of;
724 8847 uint64_t total_packets = 0, total_size = 0;
725 8847 uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
726 8847 extra_size = 0, other_size = 0;
727
728 8847 uint8_t overhead[16] = "unknown";
729 8847 int64_t file_size = of_filesize(of);
730
731 8847 av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
732 of->index, of->url);
733
734
2/2
✓ Branch 0 taken 9500 times.
✓ Branch 1 taken 8847 times.
18347 for (int j = 0; j < of->nb_streams; j++) {
735 9500 OutputStream *ost = of->streams[j];
736 9500 MuxStream *ms = ms_from_ost(ost);
737 9500 const AVCodecParameters *par = ost->st->codecpar;
738 9500 const enum AVMediaType type = par->codec_type;
739 9500 const uint64_t s = ms->data_size_mux;
740
741
4/4
✓ Branch 0 taken 7392 times.
✓ Branch 1 taken 2011 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 13 times.
9500 switch (type) {
742 7392 case AVMEDIA_TYPE_VIDEO: video_size += s; break;
743 2011 case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
744 84 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
745 13 default: other_size += s; break;
746 }
747
748 9500 extra_size += par->extradata_size;
749 9500 total_size += s;
750 9500 total_packets += atomic_load(&ost->packets_written);
751
752 9500 av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
753 of->index, j, av_get_media_type_string(type));
754
2/2
✓ Branch 0 taken 8534 times.
✓ Branch 1 taken 966 times.
9500 if (ost->enc) {
755 8534 av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
756 8534 ost->enc->frames_encoded);
757
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 6978 times.
8534 if (type == AVMEDIA_TYPE_AUDIO)
758 1556 av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->enc->samples_encoded);
759 8534 av_log(of, AV_LOG_VERBOSE, "; ");
760 }
761
762 9500 av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
763 9500 atomic_load(&ost->packets_written), s);
764
765 9500 av_log(of, AV_LOG_VERBOSE, "\n");
766 }
767
768 8847 av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
769 total_packets, total_size);
770
771
6/6
✓ Branch 0 taken 8818 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 8679 times.
✓ Branch 3 taken 139 times.
✓ Branch 4 taken 6436 times.
✓ Branch 5 taken 2243 times.
8847 if (total_size && file_size > 0 && file_size >= total_size) {
772 6436 snprintf(overhead, sizeof(overhead), "%f%%",
773 6436 100.0 * (file_size - total_size) / total_size);
774 }
775
776 8847 av_log(of, AV_LOG_INFO,
777 "video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
778 "global headers:%1.0fKiB muxing overhead: %s\n",
779 video_size / 1024.0,
780 audio_size / 1024.0,
781 subtitle_size / 1024.0,
782 other_size / 1024.0,
783 extra_size / 1024.0,
784 overhead);
785 8847 }
786
787 8848 int of_write_trailer(OutputFile *of)
788 {
789 8848 Muxer *mux = mux_from_of(of);
790 8848 AVFormatContext *fc = mux->fc;
791 8848 int ret, mux_result = 0;
792
793
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8847 times.
8848 if (!mux->header_written) {
794 1 av_log(mux, AV_LOG_ERROR,
795 "Nothing was written into output file, because "
796 "at least one of its streams received no packets.\n");
797 1 return AVERROR(EINVAL);
798 }
799
800 8847 ret = av_write_trailer(fc);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8847 times.
8847 if (ret < 0) {
802 ✗ av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
803 ✗ mux_result = err_merge(mux_result, ret);
804 }
805
806 8847 mux->last_filesize = filesize(fc->pb);
807
808
2/2
✓ Branch 0 taken 8707 times.
✓ Branch 1 taken 140 times.
8847 if (!(fc->oformat->flags & AVFMT_NOFILE)) {
809 8707 ret = avio_closep(&fc->pb);
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8707 times.
8707 if (ret < 0) {
811 ✗ av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
812 ✗ mux_result = err_merge(mux_result, ret);
813 }
814 }
815
816 8847 mux_final_stats(mux);
817
818 // check whether anything was actually written
819 8847 ret = check_written(of);
820 8847 mux_result = err_merge(mux_result, ret);
821
822 8847 return mux_result;
823 }
824
825 28515 static void enc_stats_uninit(EncStats *es)
826 {
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28515 times.
28515 for (int i = 0; i < es->nb_components; i++)
828 ✗ av_freep(&es->components[i].str);
829 28515 av_freep(&es->components);
830
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28515 times.
28515 if (es->lock_initialized)
832 ✗ pthread_mutex_destroy(&es->lock);
833 28515 es->lock_initialized = 0;
834 28515 }
835
836 9505 static void ost_free(OutputStream **post)
837 {
838 9505 OutputStream *ost = *post;
839 MuxStream *ms;
840
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9505 times.
9505 if (!ost)
842 ✗ return;
843 9505 ms = ms_from_ost(ost);
844
845 9505 enc_free(&ost->enc);
846 9505 fg_free(&ost->fg_simple);
847
848
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9497 times.
9505 if (ost->logfile) {
849
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (fclose(ost->logfile))
850 ✗ av_log(ms, AV_LOG_ERROR,
851 "Error closing logfile, loss of information possible: %s\n",
852 ✗ av_err2str(AVERROR(errno)));
853 8 ost->logfile = NULL;
854 }
855
856 9505 avcodec_parameters_free(&ms->par_in);
857
858 9505 av_bsf_free(&ms->bsf_ctx);
859 9505 av_packet_free(&ms->bsf_pkt);
860
861 9505 av_packet_free(&ms->pkt);
862
863 9505 av_freep(&ost->kf.pts);
864 9505 av_expr_free(ost->kf.pexpr);
865
866 9505 av_freep(&ost->logfile_prefix);
867
868 9505 av_freep(&ost->attachment_filename);
869
870 9505 enc_stats_uninit(&ost->enc_stats_pre);
871 9505 enc_stats_uninit(&ost->enc_stats_post);
872 9505 enc_stats_uninit(&ms->stats);
873
874 9505 av_freep(post);
875 }
876
877 8853 static void fc_close(AVFormatContext **pfc)
878 {
879 8853 AVFormatContext *fc = *pfc;
880
881
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8852 times.
8853 if (!fc)
882 1 return;
883
884
2/2
✓ Branch 0 taken 8708 times.
✓ Branch 1 taken 144 times.
8852 if (!(fc->oformat->flags & AVFMT_NOFILE))
885 8708 avio_closep(&fc->pb);
886 8852 avformat_free_context(fc);
887
888 8852 *pfc = NULL;
889 }
890
891 8853 void of_free(OutputFile **pof)
892 {
893 8853 OutputFile *of = *pof;
894 Muxer *mux;
895
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8853 times.
8853 if (!of)
897 ✗ return;
898 8853 mux = mux_from_of(of);
899
900 8853 sq_free(&mux->sq_mux);
901
902
2/2
✓ Branch 0 taken 9505 times.
✓ Branch 1 taken 8853 times.
18358 for (int i = 0; i < of->nb_streams; i++)
903 9505 ost_free(&of->streams[i]);
904 8853 av_freep(&of->streams);
905
906 8853 av_freep(&mux->sch_stream_idx);
907
908 8853 av_dict_free(&mux->opts);
909 8853 av_dict_free(&mux->enc_opts_used);
910
911 8853 av_packet_free(&mux->sq_pkt);
912
913 8853 fc_close(&mux->fc);
914
915 8853 av_freep(pof);
916 }
917
918 36370 int64_t of_filesize(OutputFile *of)
919 {
920 36370 Muxer *mux = mux_from_of(of);
921 36370 return atomic_load(&mux->last_filesize);
922 }
923