FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 361 489 73.8%
Functions: 22 24 91.7%
Branches: 190 274 69.3%

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/time.h"
34 #include "libavutil/timestamp.h"
35
36 #include "libavcodec/packet.h"
37
38 #include "libavformat/avformat.h"
39 #include "libavformat/avio.h"
40
41 typedef struct MuxThreadContext {
42 AVPacket *pkt;
43 AVPacket *fix_sub_duration_pkt;
44 } MuxThreadContext;
45
46 48589 static Muxer *mux_from_of(OutputFile *of)
47 {
48 48589 return (Muxer*)of;
49 }
50
51 502137 static int64_t filesize(AVIOContext *pb)
52 {
53 502137 int64_t ret = -1;
54
55
2/2
✓ Branch 0 taken 492864 times.
✓ Branch 1 taken 9273 times.
502137 if (pb) {
56 492864 ret = avio_size(pb);
57
2/2
✓ Branch 0 taken 104439 times.
✓ Branch 1 taken 388425 times.
492864 if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
58 104439 ret = avio_tell(pb);
59 }
60
61 502137 return ret;
62 }
63
64 static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
65 {
66 static const char *desc[] = {
67 [LATENCY_PROBE_DEMUX] = "demux",
68 [LATENCY_PROBE_DEC_PRE] = "decode",
69 [LATENCY_PROBE_DEC_POST] = "decode",
70 [LATENCY_PROBE_FILTER_PRE] = "filter",
71 [LATENCY_PROBE_FILTER_POST] = "filter",
72 [LATENCY_PROBE_ENC_PRE] = "encode",
73 [LATENCY_PROBE_ENC_POST] = "encode",
74 [LATENCY_PROBE_NB] = "mux",
75 };
76
77 char latency[512];
78
79 *latency = 0;
80 if (pkt->opaque_ref) {
81 const FrameData *fd = (FrameData*)pkt->opaque_ref->data;
82 int64_t now = av_gettime_relative();
83 int64_t total = INT64_MIN;
84
85 int next;
86
87 for (unsigned i = 0; i < FF_ARRAY_ELEMS(fd->wallclock); i = next) {
88 int64_t val = fd->wallclock[i];
89
90 next = i + 1;
91
92 if (val == INT64_MIN)
93 continue;
94
95 if (total == INT64_MIN) {
96 total = now - val;
97 snprintf(latency, sizeof(latency), "total:%gms", total / 1e3);
98 }
99
100 // find the next valid entry
101 for (; next <= FF_ARRAY_ELEMS(fd->wallclock); next++) {
102 int64_t val_next = (next == FF_ARRAY_ELEMS(fd->wallclock)) ?
103 now : fd->wallclock[next];
104 int64_t diff;
105
106 if (val_next == INT64_MIN)
107 continue;
108 diff = val_next - val;
109
110 // print those stages that take at least 5% of total
111 if (100. * diff > 5. * total) {
112 av_strlcat(latency, ", ", sizeof(latency));
113
114 if (!strcmp(desc[i], desc[next]))
115 av_strlcat(latency, desc[i], sizeof(latency));
116 else
117 av_strlcatf(latency, sizeof(latency), "%s-%s:",
118 desc[i], desc[next]);
119
120 av_strlcatf(latency, sizeof(latency), " %gms/%d%%",
121 diff / 1e3, (int)(100. * diff / total));
122 }
123
124 break;
125 }
126
127 }
128 }
129
130 av_log(ost, AV_LOG_INFO, "muxer <- pts:%s pts_time:%s dts:%s dts_time:%s "
131 "duration:%s duration_time:%s size:%d latency(%s)\n",
132 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
133 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
134 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
135 pkt->size, *latency ? latency : "N/A");
136 }
137
138 495384 static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
139 {
140 495384 OutputStream *ost = &ms->ost;
141
142 #if FFMPEG_OPT_VSYNC_DROP
143
3/4
✓ Branch 0 taken 124549 times.
✓ Branch 1 taken 370835 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 124549 times.
495384 if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
144 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
145 #endif
146
147 // rescale timestamps to the stream timebase
148
4/4
✓ Branch 0 taken 369000 times.
✓ Branch 1 taken 126384 times.
✓ Branch 2 taken 53000 times.
✓ Branch 3 taken 316000 times.
495384 if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
149 // use av_rescale_delta() for streamcopying audio, to preserve
150 // accuracy with coarse input timebases
151 53000 int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
152
153
2/2
✓ Branch 0 taken 16456 times.
✓ Branch 1 taken 36544 times.
53000 if (!duration)
154 16456 duration = ost->st->codecpar->frame_size;
155
156 106000 pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
157 53000 (AVRational){1, ost->st->codecpar->sample_rate}, duration,
158 53000 &ms->ts_rescale_delta_last, ost->st->time_base);
159 53000 pkt->pts = pkt->dts;
160
161 53000 pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
162 } else
163 442384 av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
164 495384 pkt->time_base = ost->st->time_base;
165
166
2/2
✓ Branch 0 taken 237002 times.
✓ Branch 1 taken 258382 times.
495384 if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
167
1/2
✓ Branch 0 taken 237002 times.
✗ Branch 1 not taken.
237002 if (pkt->dts != AV_NOPTS_VALUE &&
168
2/2
✓ Branch 0 taken 236014 times.
✓ Branch 1 taken 988 times.
237002 pkt->pts != AV_NOPTS_VALUE &&
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236014 times.
236014 pkt->dts > pkt->pts) {
170 av_log(ost, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64", replacing by guess\n",
171 pkt->dts, pkt->pts);
172 pkt->pts =
173 pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
174 - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
175 - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
176 }
177
6/6
✓ Branch 0 taken 90464 times.
✓ Branch 1 taken 146538 times.
✓ Branch 2 taken 1332 times.
✓ Branch 3 taken 89132 times.
✓ Branch 4 taken 1319 times.
✓ Branch 5 taken 13 times.
237002 if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
178
1/2
✓ Branch 0 taken 236989 times.
✗ Branch 1 not taken.
236989 pkt->dts != AV_NOPTS_VALUE &&
179
2/2
✓ Branch 0 taken 231403 times.
✓ Branch 1 taken 5586 times.
236989 ms->last_mux_dts != AV_NOPTS_VALUE) {
180 231403 int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
181
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 231375 times.
231403 if (pkt->dts < max) {
182
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
28 int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (exit_on_error)
184 loglevel = AV_LOG_ERROR;
185 28 av_log(ost, loglevel, "Non-monotonic DTS; "
186 "previous: %"PRId64", current: %"PRId64"; ",
187 ms->last_mux_dts, pkt->dts);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (exit_on_error) {
189 return AVERROR(EINVAL);
190 }
191
192 28 av_log(ost, loglevel, "changing to %"PRId64". This may result "
193 "in incorrect timestamps in the output file.\n",
194 max);
195
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (pkt->pts >= pkt->dts)
196 28 pkt->pts = FFMAX(pkt->pts, max);
197 28 pkt->dts = max;
198 }
199 }
200 }
201 495384 ms->last_mux_dts = pkt->dts;
202
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495384 times.
495384 if (debug_ts)
204 mux_log_debug_ts(ost, pkt);
205
206 495384 return 0;
207 }
208
209 495384 static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
210 {
211 495384 MuxStream *ms = ms_from_ost(ost);
212 495384 AVFormatContext *s = mux->fc;
213 int64_t fs;
214 uint64_t frame_num;
215 int ret;
216
217 495384 fs = filesize(s->pb);
218 495384 atomic_store(&mux->last_filesize, fs);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495384 times.
495384 if (fs >= mux->limit_filesize) {
220 ret = AVERROR_EOF;
221 goto fail;
222 }
223
224 495384 ret = mux_fixup_ts(mux, ms, pkt);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495384 times.
495384 if (ret < 0)
226 goto fail;
227
228 495384 ms->data_size_mux += pkt->size;
229 495384 frame_num = atomic_fetch_add(&ost->packets_written, 1);
230
231 495384 pkt->stream_index = ost->index;
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495384 times.
495384 if (ms->stats.io)
234 enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
235
236 495384 ret = av_interleaved_write_frame(s, pkt);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495384 times.
495384 if (ret < 0) {
238 av_log(ost, AV_LOG_ERROR,
239 "Error submitting a packet to the muxer: %s\n",
240 av_err2str(ret));
241 goto fail;
242 }
243
244 495384 return 0;
245 fail:
246 av_packet_unref(pkt);
247 return ret;
248 }
249
250 502517 static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof)
251 {
252 502517 OutputFile *of = &mux->of;
253
254
2/2
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 501698 times.
502517 if (ms->sq_idx_mux >= 0) {
255 819 int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt));
256
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 14 times.
819 if (ret < 0) {
257
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (ret == AVERROR_EOF)
258 14 *stream_eof = 1;
259
260 14 return ret;
261 }
262
263 745 while (1) {
264 1550 ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
265
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 745 times.
1550 if (ret < 0) {
266 /* n.b.: We forward EOF from the sync queue, terminating muxing.
267 * This assumes that if a muxing sync queue is present, then all
268 * the streams use it. That is true currently, but may change in
269 * the future, then this code needs to be revisited.
270 */
271
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 795 times.
805 return ret == AVERROR(EAGAIN) ? 0 : ret;
272 }
273
274 745 ret = write_packet(mux, of->streams[ret],
275 mux->sq_pkt);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 745 times.
745 if (ret < 0)
277 return ret;
278 }
279
2/2
✓ Branch 0 taken 494639 times.
✓ Branch 1 taken 7059 times.
501698 } else if (pkt)
280 494639 return write_packet(mux, &ms->ost, pkt);
281
282 7059 return 0;
283 }
284
285 static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
286
287 /* apply the output bitstream filters */
288 503337 static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
289 OutputStream *ost, AVPacket *pkt, int *stream_eof)
290 {
291 503337 MuxStream *ms = ms_from_ost(ost);
292 const char *err_msg;
293 503337 int ret = 0;
294
295
4/4
✓ Branch 0 taken 496309 times.
✓ Branch 1 taken 7028 times.
✓ Branch 2 taken 71396 times.
✓ Branch 3 taken 424913 times.
503337 if (pkt && !ost->enc) {
296 71396 ret = of_streamcopy(&mux->of, ost, pkt);
297
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 71149 times.
71396 if (ret == AVERROR(EAGAIN))
298 247 return 0;
299
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 71106 times.
71149 else if (ret == AVERROR_EOF) {
300 43 av_packet_unref(pkt);
301 43 pkt = NULL;
302 43 ret = 0;
303 43 *stream_eof = 1;
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71106 times.
71106 } else if (ret < 0)
305 goto fail;
306 }
307
308 // emit heartbeat for -fix_sub_duration;
309 // we are only interested in heartbeats on on random access points.
310
4/4
✓ Branch 0 taken 496019 times.
✓ Branch 1 taken 7071 times.
✓ Branch 2 taken 460183 times.
✓ Branch 3 taken 35836 times.
503090 if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
311 460183 mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
312 460183 mt->fix_sub_duration_pkt->pts = pkt->pts;
313 460183 mt->fix_sub_duration_pkt->time_base = pkt->time_base;
314
315 460183 ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
316 460183 mt->fix_sub_duration_pkt);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460183 times.
460183 if (ret < 0)
318 goto fail;
319 }
320
321
2/2
✓ Branch 0 taken 10296 times.
✓ Branch 1 taken 492794 times.
503090 if (ms->bsf_ctx) {
322 10296 int bsf_eof = 0;
323
324
2/2
✓ Branch 0 taken 10164 times.
✓ Branch 1 taken 132 times.
10296 if (pkt)
325 10164 av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
326
327 10296 ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10296 times.
10296 if (ret < 0) {
329 err_msg = "submitting a packet for bitstream filtering";
330 goto fail;
331 }
332
333
2/2
✓ Branch 0 taken 19889 times.
✓ Branch 1 taken 132 times.
20021 while (!bsf_eof) {
334 19889 ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
335
2/2
✓ Branch 0 taken 10164 times.
✓ Branch 1 taken 9725 times.
19889 if (ret == AVERROR(EAGAIN))
336 10164 return 0;
337
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 9593 times.
9725 else if (ret == AVERROR_EOF)
338 132 bsf_eof = 1;
339
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9591 times.
9593 else if (ret < 0) {
340 2 av_log(ost, AV_LOG_ERROR,
341 "Error applying bitstream filters to a packet: %s",
342 2 av_err2str(ret));
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (exit_on_error)
344 return ret;
345 2 continue;
346 }
347
348
2/2
✓ Branch 0 taken 9591 times.
✓ Branch 1 taken 132 times.
9723 if (!bsf_eof)
349 9591 ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
350
351
2/2
✓ Branch 0 taken 9591 times.
✓ Branch 1 taken 132 times.
9723 ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9723 times.
9723 if (ret < 0)
353 goto mux_fail;
354 }
355 132 *stream_eof = 1;
356 } else {
357 492794 ret = sync_queue_process(mux, ms, pkt, stream_eof);
358
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 492770 times.
492794 if (ret < 0)
359 24 goto mux_fail;
360 }
361
362
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 492728 times.
492902 return *stream_eof ? AVERROR_EOF : 0;
363
364 24 mux_fail:
365 24 err_msg = "submitting a packet to the muxer";
366
367 24 fail:
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret != AVERROR_EOF)
369 av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
370 24 return ret;
371 }
372
373 6753 static void thread_set_name(OutputFile *of)
374 {
375 char name[16];
376 6753 snprintf(name, sizeof(name), "mux%d:%s", of->index, of->format->name);
377 6753 ff_thread_setname(name);
378 6753 }
379
380 6753 static void mux_thread_uninit(MuxThreadContext *mt)
381 {
382 6753 av_packet_free(&mt->pkt);
383 6753 av_packet_free(&mt->fix_sub_duration_pkt);
384
385 6753 memset(mt, 0, sizeof(*mt));
386 6753 }
387
388 6753 static int mux_thread_init(MuxThreadContext *mt)
389 {
390 6753 memset(mt, 0, sizeof(*mt));
391
392 6753 mt->pkt = av_packet_alloc();
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!mt->pkt)
394 goto fail;
395
396 6753 mt->fix_sub_duration_pkt = av_packet_alloc();
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!mt->fix_sub_duration_pkt)
398 goto fail;
399
400 6753 return 0;
401
402 fail:
403 mux_thread_uninit(mt);
404 return AVERROR(ENOMEM);
405 }
406
407 6753 void *muxer_thread(void *arg)
408 {
409 6753 Muxer *mux = arg;
410 6753 OutputFile *of = &mux->of;
411
412 MuxThreadContext mt;
413
414 6753 int ret = 0;
415
416 6753 ret = mux_thread_init(&mt);
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (ret < 0)
418 goto finish;
419
420 6753 thread_set_name(of);
421
422 503327 while (1) {
423 OutputStream *ost;
424 510080 int stream_idx, stream_eof = 0;
425
426 510080 ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
427 510080 stream_idx = mt.pkt->stream_index;
428
2/2
✓ Branch 0 taken 6743 times.
✓ Branch 1 taken 503337 times.
510080 if (stream_idx < 0) {
429 6743 av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
430 6743 ret = 0;
431 6753 break;
432 }
433
434 503337 ost = of->streams[mux->sch_stream_idx[stream_idx]];
435 503337 mt.pkt->stream_index = ost->index;
436
437
2/2
✓ Branch 0 taken 496309 times.
✓ Branch 1 taken 7028 times.
503337 ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
438 503337 av_packet_unref(mt.pkt);
439
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 503139 times.
503337 if (ret == AVERROR_EOF) {
440
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 10 times.
198 if (stream_eof) {
441 188 sch_mux_receive_finish(mux->sch, of->index, stream_idx);
442 } else {
443 10 av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
444 10 ret = 0;
445 10 break;
446 }
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 503139 times.
503139 } else if (ret < 0) {
448 av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
449 break;
450 }
451 }
452
453 6753 finish:
454 6753 mux_thread_uninit(&mt);
455
456 6753 return (void*)(intptr_t)ret;
457 }
458
459 71396 static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
460 {
461 71396 MuxStream *ms = ms_from_ost(ost);
462
1/2
✓ Branch 0 taken 71396 times.
✗ Branch 1 not taken.
71396 FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
463
1/2
✓ Branch 0 taken 71396 times.
✗ Branch 1 not taken.
71396 int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
464
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 70323 times.
71396 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
465 int64_t ts_offset;
466
467
2/2
✓ Branch 0 taken 1219 times.
✓ Branch 1 taken 70177 times.
71396 if (of->recording_time != INT64_MAX &&
468
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1176 times.
1219 dts >= of->recording_time + start_time)
469 43 return AVERROR_EOF;
470
471
4/4
✓ Branch 0 taken 847 times.
✓ Branch 1 taken 70506 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 783 times.
71353 if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
472
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 !ms->copy_initial_nonkeyframes)
473 64 return AVERROR(EAGAIN);
474
475
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 70506 times.
71289 if (!ms->streamcopy_started) {
476
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
783 if (!ms->copy_prior_start &&
477 (pkt->pts == AV_NOPTS_VALUE ?
478 dts < ms->ts_copy_start :
479 pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
480 return AVERROR(EAGAIN);
481
482
4/4
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 596 times.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 4 times.
783 if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
483 183 return AVERROR(EAGAIN);
484 }
485
486 71106 ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
487
488
2/2
✓ Branch 0 taken 61852 times.
✓ Branch 1 taken 9254 times.
71106 if (pkt->pts != AV_NOPTS_VALUE)
489 61852 pkt->pts -= ts_offset;
490
491
2/2
✓ Branch 0 taken 9018 times.
✓ Branch 1 taken 62088 times.
71106 if (pkt->dts == AV_NOPTS_VALUE) {
492 9018 pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
493
2/2
✓ Branch 0 taken 51841 times.
✓ Branch 1 taken 10247 times.
62088 } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
494 51841 pkt->pts = pkt->dts - ts_offset;
495 }
496
497 71106 pkt->dts -= ts_offset;
498
499 71106 ms->streamcopy_started = 1;
500
501 71106 return 0;
502 }
503
504 int print_sdp(const char *filename);
505
506 int print_sdp(const char *filename)
507 {
508 char sdp[16384];
509 int j = 0, ret;
510 AVIOContext *sdp_pb;
511 AVFormatContext **avc;
512
513 avc = av_malloc_array(nb_output_files, sizeof(*avc));
514 if (!avc)
515 return AVERROR(ENOMEM);
516 for (int i = 0; i < nb_output_files; i++) {
517 if (!strcmp(output_files[i]->format->name, "rtp")) {
518 avc[j] = mux_from_of(output_files[i])->fc;
519 j++;
520 }
521 }
522
523 if (!j) {
524 av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
525 ret = AVERROR(EINVAL);
526 goto fail;
527 }
528
529 ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
530 if (ret < 0)
531 goto fail;
532
533 if (!filename) {
534 printf("SDP:\n%s\n", sdp);
535 fflush(stdout);
536 } else {
537 ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
538 if (ret < 0) {
539 av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
540 goto fail;
541 }
542
543 avio_print(sdp_pb, sdp);
544 avio_closep(&sdp_pb);
545 }
546
547 fail:
548 av_freep(&avc);
549 return ret;
550 }
551
552 6753 int mux_check_init(void *arg)
553 {
554 6753 Muxer *mux = arg;
555 6753 OutputFile *of = &mux->of;
556 6753 AVFormatContext *fc = mux->fc;
557 int ret;
558
559 6753 ret = avformat_write_header(fc, &mux->opts);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (ret < 0) {
561 av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
562 "parameters ?): %s\n", av_err2str(ret));
563 return ret;
564 }
565 //assert_avoptions(of->opts);
566 6753 mux->header_written = 1;
567
568 6753 av_dump_format(fc, of->index, fc->url, 1);
569 6753 atomic_fetch_add(&nb_output_dumped, 1);
570
571 6753 return 0;
572 }
573
574 7095 static int bsf_init(MuxStream *ms)
575 {
576 7095 OutputStream *ost = &ms->ost;
577 7095 AVBSFContext *ctx = ms->bsf_ctx;
578 int ret;
579
580
2/2
✓ Branch 0 taken 6963 times.
✓ Branch 1 taken 132 times.
7095 if (!ctx)
581 6963 return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
582
583 132 ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
585 return ret;
586
587 132 ctx->time_base_in = ost->st->time_base;
588
589 132 ret = av_bsf_init(ctx);
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0) {
591 av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
592 ctx->filter->name);
593 return ret;
594 }
595
596 132 ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
598 return ret;
599 132 ost->st->time_base = ctx->time_base_out;
600
601 132 ms->bsf_pkt = av_packet_alloc();
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (!ms->bsf_pkt)
603 return AVERROR(ENOMEM);
604
605 132 return 0;
606 }
607
608 7095 int of_stream_init(OutputFile *of, OutputStream *ost)
609 {
610 7095 Muxer *mux = mux_from_of(of);
611 7095 MuxStream *ms = ms_from_ost(ost);
612 int ret;
613
614 /* initialize bitstream filters for the output stream
615 * needs to be done here, because the codec id for streamcopy is not
616 * known until now */
617 7095 ret = bsf_init(ms);
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7095 times.
7095 if (ret < 0)
619 return ret;
620
621
2/2
✓ Branch 0 taken 5433 times.
✓ Branch 1 taken 1662 times.
7095 if (ms->stream_duration) {
622 5433 ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
623 5433 ost->st->time_base);
624 }
625
626
2/2
✓ Branch 0 taken 7094 times.
✓ Branch 1 taken 1 times.
7095 if (ms->sch_idx >= 0)
627 7094 return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
628
629 1 return 0;
630 }
631
632 6753 static int check_written(OutputFile *of)
633 {
634 6753 int64_t total_packets_written = 0;
635 6753 int pass1_used = 1;
636 6753 int ret = 0;
637
638
2/2
✓ Branch 0 taken 7095 times.
✓ Branch 1 taken 6753 times.
13848 for (int i = 0; i < of->nb_streams; i++) {
639 7095 OutputStream *ost = of->streams[i];
640 7095 uint64_t packets_written = atomic_load(&ost->packets_written);
641
642 7095 total_packets_written += packets_written;
643
644
2/2
✓ Branch 0 taken 6474 times.
✓ Branch 1 taken 621 times.
7095 if (ost->enc_ctx &&
645
2/2
✓ Branch 0 taken 6470 times.
✓ Branch 1 taken 4 times.
6474 (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
646 != AV_CODEC_FLAG_PASS1)
647 6470 pass1_used = 0;
648
649
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 7056 times.
7095 if (!packets_written &&
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
651 av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
652 ret = err_merge(ret, AVERROR(EINVAL));
653 }
654 }
655
656
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6741 times.
6753 if (!total_packets_written) {
657 12 int level = AV_LOG_WARNING;
658
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
660 ret = err_merge(ret, AVERROR(EINVAL));
661 level = AV_LOG_FATAL;
662 }
663
664
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 av_log(of, level, "Output file is empty, nothing was encoded%s\n",
665 pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
666 }
667
668 6753 return ret;
669 }
670
671 6753 static void mux_final_stats(Muxer *mux)
672 {
673 6753 OutputFile *of = &mux->of;
674 6753 uint64_t total_packets = 0, total_size = 0;
675 6753 uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
676 6753 extra_size = 0, other_size = 0;
677
678 6753 uint8_t overhead[16] = "unknown";
679 6753 int64_t file_size = of_filesize(of);
680
681 6753 av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
682 of->index, of->url);
683
684
2/2
✓ Branch 0 taken 7095 times.
✓ Branch 1 taken 6753 times.
13848 for (int j = 0; j < of->nb_streams; j++) {
685 7095 OutputStream *ost = of->streams[j];
686 7095 MuxStream *ms = ms_from_ost(ost);
687 7095 const AVCodecParameters *par = ost->st->codecpar;
688 7095 const enum AVMediaType type = par->codec_type;
689 7095 const uint64_t s = ms->data_size_mux;
690
691
4/4
✓ Branch 0 taken 5517 times.
✓ Branch 1 taken 1496 times.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 11 times.
7095 switch (type) {
692 5517 case AVMEDIA_TYPE_VIDEO: video_size += s; break;
693 1496 case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
694 71 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
695 11 default: other_size += s; break;
696 }
697
698 7095 extra_size += par->extradata_size;
699 7095 total_size += s;
700 7095 total_packets += atomic_load(&ost->packets_written);
701
702 7095 av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
703 of->index, j, av_get_media_type_string(type));
704
2/2
✓ Branch 0 taken 6474 times.
✓ Branch 1 taken 621 times.
7095 if (ost->enc) {
705 6474 av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
706 ost->frames_encoded);
707
2/2
✓ Branch 0 taken 1247 times.
✓ Branch 1 taken 5227 times.
6474 if (type == AVMEDIA_TYPE_AUDIO)
708 1247 av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
709 6474 av_log(of, AV_LOG_VERBOSE, "; ");
710 }
711
712 7095 av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
713 7095 atomic_load(&ost->packets_written), s);
714
715 7095 av_log(of, AV_LOG_VERBOSE, "\n");
716 }
717
718 6753 av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
719 total_packets, total_size);
720
721
6/6
✓ Branch 0 taken 6741 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 6651 times.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 4648 times.
✓ Branch 5 taken 2003 times.
6753 if (total_size && file_size > 0 && file_size >= total_size) {
722 4648 snprintf(overhead, sizeof(overhead), "%f%%",
723 4648 100.0 * (file_size - total_size) / total_size);
724 }
725
726 6753 av_log(of, AV_LOG_INFO,
727 "video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
728 "global headers:%1.0fKiB muxing overhead: %s\n",
729 video_size / 1024.0,
730 audio_size / 1024.0,
731 subtitle_size / 1024.0,
732 other_size / 1024.0,
733 extra_size / 1024.0,
734 overhead);
735 6753 }
736
737 6753 int of_write_trailer(OutputFile *of)
738 {
739 6753 Muxer *mux = mux_from_of(of);
740 6753 AVFormatContext *fc = mux->fc;
741 6753 int ret, mux_result = 0;
742
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!mux->header_written) {
744 av_log(mux, AV_LOG_ERROR,
745 "Nothing was written into output file, because "
746 "at least one of its streams received no packets.\n");
747 return AVERROR(EINVAL);
748 }
749
750 6753 ret = av_write_trailer(fc);
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (ret < 0) {
752 av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
753 mux_result = err_merge(mux_result, ret);
754 }
755
756 6753 mux->last_filesize = filesize(fc->pb);
757
758
2/2
✓ Branch 0 taken 6663 times.
✓ Branch 1 taken 90 times.
6753 if (!(of->format->flags & AVFMT_NOFILE)) {
759 6663 ret = avio_closep(&fc->pb);
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6663 times.
6663 if (ret < 0) {
761 av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
762 mux_result = err_merge(mux_result, ret);
763 }
764 }
765
766 6753 mux_final_stats(mux);
767
768 // check whether anything was actually written
769 6753 ret = check_written(of);
770 6753 mux_result = err_merge(mux_result, ret);
771
772 6753 return mux_result;
773 }
774
775 21285 static void enc_stats_uninit(EncStats *es)
776 {
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21285 times.
21285 for (int i = 0; i < es->nb_components; i++)
778 av_freep(&es->components[i].str);
779 21285 av_freep(&es->components);
780
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21285 times.
21285 if (es->lock_initialized)
782 pthread_mutex_destroy(&es->lock);
783 21285 es->lock_initialized = 0;
784 21285 }
785
786 7095 static void ost_free(OutputStream **post)
787 {
788 7095 OutputStream *ost = *post;
789 MuxStream *ms;
790
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7095 times.
7095 if (!ost)
792 return;
793 7095 ms = ms_from_ost(ost);
794
795 7095 enc_free(&ost->enc);
796
797
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7091 times.
7095 if (ost->logfile) {
798
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (fclose(ost->logfile))
799 av_log(ms, AV_LOG_ERROR,
800 "Error closing logfile, loss of information possible: %s\n",
801 av_err2str(AVERROR(errno)));
802 4 ost->logfile = NULL;
803 }
804
805 7095 avcodec_parameters_free(&ost->par_in);
806
807 7095 av_bsf_free(&ms->bsf_ctx);
808 7095 av_packet_free(&ms->bsf_pkt);
809
810 7095 av_packet_free(&ms->pkt);
811 7095 av_dict_free(&ost->encoder_opts);
812
813 7095 av_freep(&ost->kf.pts);
814 7095 av_expr_free(ost->kf.pexpr);
815
816 7095 av_freep(&ost->logfile_prefix);
817 7095 av_freep(&ost->apad);
818
819 7095 av_freep(&ost->attachment_filename);
820
821 #if FFMPEG_OPT_MAP_CHANNEL
822 7095 av_freep(&ost->audio_channels_map);
823 7095 ost->audio_channels_mapped = 0;
824 #endif
825
826 7095 av_dict_free(&ost->sws_dict);
827 7095 av_dict_free(&ost->swr_opts);
828
829
2/2
✓ Branch 0 taken 6474 times.
✓ Branch 1 taken 621 times.
7095 if (ost->enc_ctx)
830 6474 av_freep(&ost->enc_ctx->stats_in);
831 7095 avcodec_free_context(&ost->enc_ctx);
832
833 7095 enc_stats_uninit(&ost->enc_stats_pre);
834 7095 enc_stats_uninit(&ost->enc_stats_post);
835 7095 enc_stats_uninit(&ms->stats);
836
837 7095 av_freep(post);
838 }
839
840 6753 static void fc_close(AVFormatContext **pfc)
841 {
842 6753 AVFormatContext *fc = *pfc;
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!fc)
845 return;
846
847
2/2
✓ Branch 0 taken 6663 times.
✓ Branch 1 taken 90 times.
6753 if (!(fc->oformat->flags & AVFMT_NOFILE))
848 6663 avio_closep(&fc->pb);
849 6753 avformat_free_context(fc);
850
851 6753 *pfc = NULL;
852 }
853
854 6753 void of_free(OutputFile **pof)
855 {
856 6753 OutputFile *of = *pof;
857 Muxer *mux;
858
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!of)
860 return;
861 6753 mux = mux_from_of(of);
862
863 6753 sq_free(&mux->sq_mux);
864
865
2/2
✓ Branch 0 taken 7095 times.
✓ Branch 1 taken 6753 times.
13848 for (int i = 0; i < of->nb_streams; i++)
866 7095 ost_free(&of->streams[i]);
867 6753 av_freep(&of->streams);
868
869 6753 av_freep(&mux->sch_stream_idx);
870
871 6753 av_dict_free(&mux->opts);
872
873 6753 av_packet_free(&mux->sq_pkt);
874
875 6753 fc_close(&mux->fc);
876
877 6753 av_freep(pof);
878 }
879
880 27988 int64_t of_filesize(OutputFile *of)
881 {
882 27988 Muxer *mux = mux_from_of(of);
883 27988 return atomic_load(&mux->last_filesize);
884 }
885