FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_mux.c
Date: 2022-10-02 18:56:10
Exec Total Coverage
Lines: 245 361 67.9%
Branches: 139 220 63.2%

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 "objpool.h"
25 #include "sync_queue.h"
26 #include "thread_queue.h"
27
28 #include "libavutil/fifo.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/log.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/timestamp.h"
33 #include "libavutil/thread.h"
34
35 #include "libavcodec/packet.h"
36
37 #include "libavformat/avformat.h"
38 #include "libavformat/avio.h"
39
40 typedef struct MuxStream {
41 /* the packets are buffered here until the muxer is ready to be initialized */
42 AVFifo *muxing_queue;
43
44 /*
45 * The size of the AVPackets' buffers in queue.
46 * Updated when a packet is either pushed or pulled from the queue.
47 */
48 size_t muxing_queue_data_size;
49
50 /* dts of the last packet sent to the muxer, in the stream timebase
51 * used for making up missing dts values */
52 int64_t last_mux_dts;
53 } MuxStream;
54
55 struct Muxer {
56 AVFormatContext *fc;
57
58 pthread_t thread;
59 ThreadQueue *tq;
60
61 MuxStream *streams;
62
63 AVDictionary *opts;
64
65 int thread_queue_size;
66
67 /* filesize limit expressed in bytes */
68 int64_t limit_filesize;
69 atomic_int_least64_t last_filesize;
70 int header_written;
71
72 AVPacket *sq_pkt;
73 };
74
75 static int want_sdp = 1;
76
77 484708 static int64_t filesize(AVIOContext *pb)
78 {
79 484708 int64_t ret = -1;
80
81
2/2
✓ Branch 0 taken 475807 times.
✓ Branch 1 taken 8901 times.
484708 if (pb) {
82 475807 ret = avio_size(pb);
83
2/2
✓ Branch 0 taken 100965 times.
✓ Branch 1 taken 374842 times.
475807 if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
84 100965 ret = avio_tell(pb);
85 }
86
87 484708 return ret;
88 }
89
90 478286 static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
91 {
92 478286 MuxStream *ms = &of->mux->streams[ost->index];
93 478286 AVFormatContext *s = of->mux->fc;
94 478286 AVStream *st = ost->st;
95 int64_t fs;
96 int ret;
97
98 478286 fs = filesize(s->pb);
99 478286 atomic_store(&of->mux->last_filesize, fs);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 478286 times.
478286 if (fs >= of->mux->limit_filesize) {
101 ret = AVERROR_EOF;
102 goto fail;
103 }
104
105
3/4
✓ Branch 0 taken 115223 times.
✓ Branch 1 taken 363063 times.
✓ Branch 2 taken 115223 times.
✗ Branch 3 not taken.
478286 if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP) ||
106
3/4
✓ Branch 0 taken 361364 times.
✓ Branch 1 taken 116922 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 361364 times.
478286 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
107 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
108
109
2/2
✓ Branch 0 taken 115223 times.
✓ Branch 1 taken 363063 times.
478286 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
110
4/4
✓ Branch 0 taken 102526 times.
✓ Branch 1 taken 12697 times.
✓ Branch 2 taken 10483 times.
✓ Branch 3 taken 92043 times.
115223 if (ost->frame_rate.num && ost->is_cfr) {
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10483 times.
10483 if (pkt->duration > 0)
112 av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
113 10483 pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
114 ost->mux_timebase);
115 }
116 }
117
118 478286 av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
119
120
2/2
✓ Branch 0 taken 228330 times.
✓ Branch 1 taken 249956 times.
478286 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
121
1/2
✓ Branch 0 taken 228330 times.
✗ Branch 1 not taken.
228330 if (pkt->dts != AV_NOPTS_VALUE &&
122
2/2
✓ Branch 0 taken 226165 times.
✓ Branch 1 taken 2165 times.
228330 pkt->pts != AV_NOPTS_VALUE &&
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 226165 times.
226165 pkt->dts > pkt->pts) {
124 av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
125 pkt->dts, pkt->pts,
126 ost->file_index, ost->st->index);
127 pkt->pts =
128 pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
129 - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
130 - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
131 }
132
6/6
✓ Branch 0 taken 85783 times.
✓ Branch 1 taken 142547 times.
✓ Branch 2 taken 1204 times.
✓ Branch 3 taken 84579 times.
✓ Branch 4 taken 1189 times.
✓ Branch 5 taken 15 times.
228330 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
133
1/2
✓ Branch 0 taken 228315 times.
✗ Branch 1 not taken.
228315 pkt->dts != AV_NOPTS_VALUE &&
134
2/2
✓ Branch 0 taken 223031 times.
✓ Branch 1 taken 5284 times.
228315 ms->last_mux_dts != AV_NOPTS_VALUE) {
135 223031 int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
136
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 223001 times.
223031 if (pkt->dts < max) {
137
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
30 int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (exit_on_error)
139 loglevel = AV_LOG_ERROR;
140 30 av_log(s, loglevel, "Non-monotonous DTS in output stream "
141 "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
142 30 ost->file_index, ost->st->index, ms->last_mux_dts, pkt->dts);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (exit_on_error) {
144 ret = AVERROR(EINVAL);
145 goto fail;
146 }
147
148 30 av_log(s, loglevel, "changing to %"PRId64". This may result "
149 "in incorrect timestamps in the output file.\n",
150 max);
151
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (pkt->pts >= pkt->dts)
152 30 pkt->pts = FFMAX(pkt->pts, max);
153 30 pkt->dts = max;
154 }
155 }
156 }
157 478286 ms->last_mux_dts = pkt->dts;
158
159 478286 ost->data_size_mux += pkt->size;
160 478286 atomic_fetch_add(&ost->packets_written, 1);
161
162 478286 pkt->stream_index = ost->index;
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 478286 times.
478286 if (debug_ts) {
165 av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
166 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
167 av_get_media_type_string(st->codecpar->codec_type),
168 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
169 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
170 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
171 pkt->size
172 );
173 }
174
175 478286 ret = av_interleaved_write_frame(s, pkt);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 478286 times.
478286 if (ret < 0) {
177 print_error("av_interleaved_write_frame()", ret);
178 goto fail;
179 }
180
181 478286 return 0;
182 fail:
183 av_packet_unref(pkt);
184 return ret;
185 }
186
187 485020 static int sync_queue_process(OutputFile *of, OutputStream *ost, AVPacket *pkt)
188 {
189
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 484214 times.
485020 if (ost->sq_idx_mux >= 0) {
190 806 int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(pkt));
191
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 773 times.
806 if (ret < 0)
192 33 return ret;
193
194 719 while (1) {
195 1492 ret = sq_receive(of->sq_mux, -1, SQPKT(of->mux->sq_pkt));
196
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 719 times.
1492 if (ret < 0)
197
3/4
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 764 times.
773 return (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) ? 0 : ret;
198
199 719 ret = write_packet(of, output_streams[of->ost_index + ret],
200 719 of->mux->sq_pkt);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 719 times.
719 if (ret < 0)
202 return ret;
203 }
204
2/2
✓ Branch 0 taken 477567 times.
✓ Branch 1 taken 6647 times.
484214 } else if (pkt)
205 477567 return write_packet(of, ost, pkt);
206
207 6647 return 0;
208 }
209
210 6422 static void *muxer_thread(void *arg)
211 {
212 6422 OutputFile *of = arg;
213 6422 Muxer *mux = of->mux;
214 6422 AVPacket *pkt = NULL;
215 6422 int ret = 0;
216
217 6422 pkt = av_packet_alloc();
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!pkt) {
219 ret = AVERROR(ENOMEM);
220 goto finish;
221 }
222
223 485020 while (1) {
224 OutputStream *ost;
225 int stream_idx;
226
227 491442 ret = tq_receive(mux->tq, &stream_idx, pkt);
228
2/2
✓ Branch 0 taken 6422 times.
✓ Branch 1 taken 485020 times.
491442 if (stream_idx < 0) {
229 6422 av_log(NULL, AV_LOG_VERBOSE,
230 "All streams finished for output file #%d\n", of->index);
231 6422 ret = 0;
232 6422 break;
233 }
234
235 485020 ost = output_streams[of->ost_index + stream_idx];
236
2/2
✓ Branch 0 taken 478367 times.
✓ Branch 1 taken 6653 times.
485020 ret = sync_queue_process(of, ost, ret < 0 ? NULL : pkt);
237 485020 av_packet_unref(pkt);
238
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 484987 times.
485020 if (ret == AVERROR_EOF)
239 33 tq_receive_finish(mux->tq, stream_idx);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484987 times.
484987 else if (ret < 0) {
241 av_log(NULL, AV_LOG_ERROR,
242 "Error muxing a packet for output file #%d\n", of->index);
243 break;
244 }
245 }
246
247 6422 finish:
248 6422 av_packet_free(&pkt);
249
250
2/2
✓ Branch 0 taken 6667 times.
✓ Branch 1 taken 6422 times.
13089 for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
251 6667 tq_receive_finish(mux->tq, i);
252
253 6422 av_log(NULL, AV_LOG_VERBOSE, "Terminating muxer thread %d\n", of->index);
254
255 6422 return (void*)(intptr_t)ret;
256 }
257
258 488041 static int submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
259 {
260 488041 Muxer *mux = of->mux;
261 488041 int ret = 0;
262
263
3/4
✓ Branch 0 taken 478379 times.
✓ Branch 1 taken 9662 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 478379 times.
488041 if (!pkt || ost->finished & MUXER_FINISHED)
264 9662 goto finish;
265
266 478379 ret = tq_send(mux->tq, ost->index, pkt);
267
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 478367 times.
478379 if (ret < 0)
268 12 goto finish;
269
270 478367 return 0;
271
272 9674 finish:
273
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9662 times.
9674 if (pkt)
274 12 av_packet_unref(pkt);
275
276 9674 ost->finished |= MUXER_FINISHED;
277 9674 tq_send_finish(mux->tq, ost->index);
278
2/2
✓ Branch 0 taken 9662 times.
✓ Branch 1 taken 12 times.
9674 return ret == AVERROR_EOF ? 0 : ret;
279 }
280
281 332 static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
282 {
283 332 MuxStream *ms = &of->mux->streams[ost->index];
284 332 AVPacket *tmp_pkt = NULL;
285 int ret;
286
287
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 314 times.
332 if (!av_fifo_can_write(ms->muxing_queue)) {
288 18 size_t cur_size = av_fifo_can_read(ms->muxing_queue);
289
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 size_t pkt_size = pkt ? pkt->size : 0;
290 18 unsigned int are_we_over_size =
291 18 (ms->muxing_queue_data_size + pkt_size) > ost->muxing_queue_data_threshold;
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX;
293 18 size_t new_size = FFMIN(2 * cur_size, limit);
294
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (new_size <= cur_size) {
296 av_log(NULL, AV_LOG_ERROR,
297 "Too many packets buffered for output stream %d:%d.\n",
298 ost->file_index, ost->st->index);
299 return AVERROR(ENOSPC);
300 }
301 18 ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size);
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
303 return ret;
304 }
305
306
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
332 if (pkt) {
307 332 ret = av_packet_make_refcounted(pkt);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (ret < 0)
309 return ret;
310
311 332 tmp_pkt = av_packet_alloc();
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (!tmp_pkt)
313 return AVERROR(ENOMEM);
314
315 332 av_packet_move_ref(tmp_pkt, pkt);
316 332 ms->muxing_queue_data_size += tmp_pkt->size;
317 }
318 332 av_fifo_write(ms->muxing_queue, &tmp_pkt, 1);
319
320 332 return 0;
321 }
322
323 488041 int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
324 {
325 int ret;
326
327
2/2
✓ Branch 0 taken 487709 times.
✓ Branch 1 taken 332 times.
488041 if (of->mux->tq) {
328 487709 return submit_packet(of, ost, pkt);
329 } else {
330 /* the muxer is not initialized yet, buffer the packet */
331 332 ret = queue_packet(of, ost, pkt);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (ret < 0) {
333 av_packet_unref(pkt);
334 return ret;
335 }
336 }
337
338 332 return 0;
339 }
340
341 12844 static int thread_stop(OutputFile *of)
342 {
343 12844 Muxer *mux = of->mux;
344 void *ret;
345
346
3/4
✓ Branch 0 taken 12844 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6422 times.
✓ Branch 3 taken 6422 times.
12844 if (!mux || !mux->tq)
347 6422 return 0;
348
349
2/2
✓ Branch 0 taken 6667 times.
✓ Branch 1 taken 6422 times.
13089 for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
350 6667 tq_send_finish(mux->tq, i);
351
352 6422 pthread_join(mux->thread, &ret);
353
354 6422 tq_free(&mux->tq);
355
356 6422 return (int)(intptr_t)ret;
357 }
358
359 956734 static void pkt_move(void *dst, void *src)
360 {
361 956734 av_packet_move_ref(dst, src);
362 956734 }
363
364 6422 static int thread_start(OutputFile *of)
365 {
366 6422 Muxer *mux = of->mux;
367 6422 AVFormatContext *fc = mux->fc;
368 ObjPool *op;
369 int ret;
370
371 6422 op = objpool_alloc_packets();
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!op)
373 return AVERROR(ENOMEM);
374
375 6422 mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!mux->tq) {
377 objpool_free(&op);
378 return AVERROR(ENOMEM);
379 }
380
381 6422 ret = pthread_create(&mux->thread, NULL, muxer_thread, (void*)of);
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret) {
383 tq_free(&mux->tq);
384 return AVERROR(ret);
385 }
386
387 /* flush the muxing queues */
388
2/2
✓ Branch 0 taken 6667 times.
✓ Branch 1 taken 6422 times.
13089 for (int i = 0; i < fc->nb_streams; i++) {
389 6667 MuxStream *ms = &of->mux->streams[i];
390 6667 OutputStream *ost = output_streams[of->ost_index + i];
391 AVPacket *pkt;
392
393 /* try to improve muxing time_base (only possible if nothing has been written yet) */
394
2/2
✓ Branch 1 taken 6575 times.
✓ Branch 2 taken 92 times.
6667 if (!av_fifo_can_read(ms->muxing_queue))
395 6575 ost->mux_timebase = ost->st->time_base;
396
397
2/2
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 6667 times.
6999 while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
398 332 ret = submit_packet(of, ost, pkt);
399
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
332 if (pkt) {
400 332 ms->muxing_queue_data_size -= pkt->size;
401 332 av_packet_free(&pkt);
402 }
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 if (ret < 0)
404 return ret;
405 }
406 }
407
408 6422 return 0;
409 }
410
411 static int print_sdp(void)
412 {
413 char sdp[16384];
414 int i;
415 int j, ret;
416 AVIOContext *sdp_pb;
417 AVFormatContext **avc;
418
419 for (i = 0; i < nb_output_files; i++) {
420 if (!output_files[i]->mux->header_written)
421 return 0;
422 }
423
424 avc = av_malloc_array(nb_output_files, sizeof(*avc));
425 if (!avc)
426 return AVERROR(ENOMEM);
427 for (i = 0, j = 0; i < nb_output_files; i++) {
428 if (!strcmp(output_files[i]->format->name, "rtp")) {
429 avc[j] = output_files[i]->mux->fc;
430 j++;
431 }
432 }
433
434 if (!j) {
435 av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
436 ret = AVERROR(EINVAL);
437 goto fail;
438 }
439
440 ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
441 if (ret < 0)
442 goto fail;
443
444 if (!sdp_filename) {
445 printf("SDP:\n%s\n", sdp);
446 fflush(stdout);
447 } else {
448 ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL);
449 if (ret < 0) {
450 av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
451 goto fail;
452 }
453
454 avio_print(sdp_pb, sdp);
455 avio_closep(&sdp_pb);
456 av_freep(&sdp_filename);
457 }
458
459 // SDP successfully written, allow muxer threads to start
460 ret = 1;
461
462 fail:
463 av_freep(&avc);
464 return ret;
465 }
466
467 /* open the muxer when all the streams are initialized */
468 6667 int of_check_init(OutputFile *of)
469 {
470 6667 AVFormatContext *fc = of->mux->fc;
471 int ret, i;
472
473
2/2
✓ Branch 0 taken 7204 times.
✓ Branch 1 taken 6422 times.
13626 for (i = 0; i < fc->nb_streams; i++) {
474 7204 OutputStream *ost = output_streams[of->ost_index + i];
475
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 6959 times.
7204 if (!ost->initialized)
476 245 return 0;
477 }
478
479 6422 ret = avformat_write_header(fc, &of->mux->opts);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret < 0) {
481 av_log(NULL, AV_LOG_ERROR,
482 "Could not write header for output file #%d "
483 "(incorrect codec parameters ?): %s\n",
484 of->index, av_err2str(ret));
485 return ret;
486 }
487 //assert_avoptions(of->opts);
488 6422 of->mux->header_written = 1;
489
490 6422 av_dump_format(fc, of->index, fc->url, 1);
491 6422 nb_output_dumped++;
492
493
2/4
✓ Branch 0 taken 6422 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6422 times.
6422 if (sdp_filename || want_sdp) {
494 ret = print_sdp();
495 if (ret < 0) {
496 av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n");
497 return ret;
498 } else if (ret == 1) {
499 /* SDP is written only after all the muxers are ready, so now we
500 * start ALL the threads */
501 for (i = 0; i < nb_output_files; i++) {
502 ret = thread_start(output_files[i]);
503 if (ret < 0)
504 return ret;
505 }
506 }
507 } else {
508 6422 ret = thread_start(of);
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret < 0)
510 return ret;
511 }
512
513 6422 return 0;
514 }
515
516 6422 int of_write_trailer(OutputFile *of)
517 {
518 6422 AVFormatContext *fc = of->mux->fc;
519 int ret;
520
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!of->mux->tq) {
522 av_log(NULL, AV_LOG_ERROR,
523 "Nothing was written into output file %d (%s), because "
524 "at least one of its streams received no packets.\n",
525 of->index, fc->url);
526 return AVERROR(EINVAL);
527 }
528
529 6422 ret = thread_stop(of);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret < 0)
531 main_return_code = ret;
532
533 6422 ret = av_write_trailer(fc);
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret < 0) {
535 av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", fc->url, av_err2str(ret));
536 return ret;
537 }
538
539 6422 of->mux->last_filesize = filesize(fc->pb);
540
541
2/2
✓ Branch 0 taken 6336 times.
✓ Branch 1 taken 86 times.
6422 if (!(of->format->flags & AVFMT_NOFILE)) {
542 6336 ret = avio_closep(&fc->pb);
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6336 times.
6336 if (ret < 0) {
544 av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n",
545 fc->url, av_err2str(ret));
546 return ret;
547 }
548 }
549
550 6422 return 0;
551 }
552
553 6422 static void fc_close(AVFormatContext **pfc)
554 {
555 6422 AVFormatContext *fc = *pfc;
556
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!fc)
558 return;
559
560
2/2
✓ Branch 0 taken 6336 times.
✓ Branch 1 taken 86 times.
6422 if (!(fc->oformat->flags & AVFMT_NOFILE))
561 6336 avio_closep(&fc->pb);
562 6422 avformat_free_context(fc);
563
564 6422 *pfc = NULL;
565 }
566
567 6422 static void mux_free(Muxer **pmux)
568 {
569 6422 Muxer *mux = *pmux;
570
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!mux)
572 return;
573
574
2/2
✓ Branch 0 taken 6667 times.
✓ Branch 1 taken 6422 times.
13089 for (int i = 0; i < mux->fc->nb_streams; i++) {
575 6667 MuxStream *ms = &mux->streams[i];
576 AVPacket *pkt;
577
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6667 times.
6667 if (!ms->muxing_queue)
579 continue;
580
581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6667 times.
6667 while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0)
582 av_packet_free(&pkt);
583 6667 av_fifo_freep2(&ms->muxing_queue);
584 }
585 6422 av_freep(&mux->streams);
586 6422 av_dict_free(&mux->opts);
587
588 6422 av_packet_free(&mux->sq_pkt);
589
590 6422 fc_close(&mux->fc);
591
592 6422 av_freep(pmux);
593 }
594
595 6422 void of_close(OutputFile **pof)
596 {
597 6422 OutputFile *of = *pof;
598
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!of)
600 return;
601
602 6422 thread_stop(of);
603
604 6422 sq_free(&of->sq_encode);
605 6422 sq_free(&of->sq_mux);
606
607 6422 mux_free(&of->mux);
608
609 6422 av_freep(pof);
610 }
611
612 6422 int of_muxer_init(OutputFile *of, AVFormatContext *fc,
613 AVDictionary *opts, int64_t limit_filesize,
614 int thread_queue_size)
615 {
616 6422 Muxer *mux = av_mallocz(sizeof(*mux));
617 6422 int ret = 0;
618
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!mux) {
620 fc_close(&fc);
621 return AVERROR(ENOMEM);
622 }
623
624 6422 mux->streams = av_calloc(fc->nb_streams, sizeof(*mux->streams));
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (!mux->streams) {
626 fc_close(&fc);
627 av_freep(&mux);
628 return AVERROR(ENOMEM);
629 }
630
631 6422 of->mux = mux;
632 6422 mux->fc = fc;
633
634
2/2
✓ Branch 0 taken 6667 times.
✓ Branch 1 taken 6422 times.
13089 for (int i = 0; i < fc->nb_streams; i++) {
635 6667 MuxStream *ms = &mux->streams[i];
636 6667 ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6667 times.
6667 if (!ms->muxing_queue) {
638 ret = AVERROR(ENOMEM);
639 goto fail;
640 }
641 6667 ms->last_mux_dts = AV_NOPTS_VALUE;
642 }
643
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 mux->thread_queue_size = thread_queue_size > 0 ? thread_queue_size : 8;
645 6422 mux->limit_filesize = limit_filesize;
646 6422 mux->opts = opts;
647
648
1/2
✓ Branch 0 taken 6422 times.
✗ Branch 1 not taken.
6422 if (strcmp(of->format->name, "rtp"))
649 6422 want_sdp = 0;
650
651
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6414 times.
6422 if (of->sq_mux) {
652 8 mux->sq_pkt = av_packet_alloc();
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!mux->sq_pkt) {
654 ret = AVERROR(ENOMEM);
655 goto fail;
656 }
657 }
658
659 /* write the header for files with no streams */
660
3/4
✓ Branch 0 taken 6420 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
6422 if (of->format->flags & AVFMT_NOSTREAMS && fc->nb_streams == 0) {
661 ret = of_check_init(of);
662 if (ret < 0)
663 goto fail;
664 }
665
666 6422 fail:
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6422 times.
6422 if (ret < 0)
668 mux_free(&of->mux);
669
670 6422 return ret;
671 }
672
673 470756 int64_t of_filesize(OutputFile *of)
674 {
675 470756 return atomic_load(&of->mux->last_filesize);
676 }
677
678 AVChapter * const *
679 of_get_chapters(OutputFile *of, unsigned int *nb_chapters)
680 {
681 *nb_chapters = of->mux->fc->nb_chapters;
682 return of->mux->fc->chapters;
683 }
684