| 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 | 63802 | static Muxer *mux_from_of(OutputFile *of) | |
| 48 | { | ||
| 49 | 63802 | return (Muxer*)of; | |
| 50 | } | ||
| 51 | |||
| 52 | 612549 | static int64_t filesize(AVIOContext *pb) | |
| 53 | { | ||
| 54 | 612549 | int64_t ret = -1; | |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 600929 times.
✓ Branch 1 taken 11620 times.
|
612549 | if (pb) { |
| 57 | 600929 | ret = avio_size(pb); | |
| 58 |
2/2✓ Branch 0 taken 147641 times.
✓ Branch 1 taken 453288 times.
|
600929 | if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too |
| 59 | 147641 | ret = avio_tell(pb); | |
| 60 | } | ||
| 61 | |||
| 62 | 612549 | 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 | 603728 | static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt) | |
| 140 | { | ||
| 141 | 603728 | OutputStream *ost = &ms->ost; | |
| 142 | |||
| 143 | // rescale timestamps to the stream timebase | ||
| 144 |
4/4✓ Branch 0 taken 438749 times.
✓ Branch 1 taken 164979 times.
✓ Branch 2 taken 55132 times.
✓ Branch 3 taken 383617 times.
|
603728 | 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 | 55132 | int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size); | |
| 148 | |||
| 149 |
2/2✓ Branch 0 taken 16998 times.
✓ Branch 1 taken 38134 times.
|
55132 | if (!duration) |
| 150 | 16998 | duration = ost->st->codecpar->frame_size; | |
| 151 | |||
| 152 | 110264 | pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts, | |
| 153 | 55132 | (AVRational){1, ost->st->codecpar->sample_rate}, duration, | |
| 154 | 55132 | &ms->ts_rescale_delta_last, ost->st->time_base); | |
| 155 | 55132 | pkt->pts = pkt->dts; | |
| 156 | |||
| 157 | 55132 | pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base); | |
| 158 | } else | ||
| 159 | 548596 | av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base); | |
| 160 | 603728 | pkt->time_base = ost->st->time_base; | |
| 161 | |||
| 162 |
2/2✓ Branch 0 taken 275387 times.
✓ Branch 1 taken 328341 times.
|
603728 | if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) { |
| 163 |
2/2✓ Branch 0 taken 275383 times.
✓ Branch 1 taken 4 times.
|
275387 | if (pkt->dts != AV_NOPTS_VALUE && |
| 164 |
2/2✓ Branch 0 taken 274266 times.
✓ Branch 1 taken 1117 times.
|
275383 | pkt->pts != AV_NOPTS_VALUE && |
| 165 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 274264 times.
|
274266 | 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 99645 times.
✓ Branch 1 taken 175742 times.
✓ Branch 2 taken 1827 times.
✓ Branch 3 taken 97818 times.
✓ Branch 4 taken 1750 times.
✓ Branch 5 taken 77 times.
|
275387 | if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) && |
| 174 |
2/2✓ Branch 0 taken 275306 times.
✓ Branch 1 taken 4 times.
|
275310 | pkt->dts != AV_NOPTS_VALUE && |
| 175 |
2/2✓ Branch 0 taken 268789 times.
✓ Branch 1 taken 6517 times.
|
275306 | ms->last_mux_dts != AV_NOPTS_VALUE) { |
| 176 | 268789 | 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 268733 times.
|
268789 | 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 | 603728 | ms->last_mux_dts = pkt->dts; | |
| 198 | |||
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603728 times.
|
603728 | if (debug_ts) |
| 200 | ✗ | mux_log_debug_ts(ost, pkt); | |
| 201 | |||
| 202 | 603728 | return 0; | |
| 203 | } | ||
| 204 | |||
| 205 | 603728 | static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) | |
| 206 | { | ||
| 207 | 603728 | MuxStream *ms = ms_from_ost(ost); | |
| 208 | 603728 | AVFormatContext *s = mux->fc; | |
| 209 | int64_t fs; | ||
| 210 | uint64_t frame_num; | ||
| 211 | int ret; | ||
| 212 | |||
| 213 | 603728 | fs = filesize(s->pb); | |
| 214 | 603728 | atomic_store(&mux->last_filesize, fs); | |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603728 times.
|
603728 | if (fs >= mux->limit_filesize) { |
| 216 | ✗ | ret = AVERROR_EOF; | |
| 217 | ✗ | goto fail; | |
| 218 | } | ||
| 219 | |||
| 220 | 603728 | ret = mux_fixup_ts(mux, ms, pkt); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603728 times.
|
603728 | if (ret < 0) |
| 222 | ✗ | goto fail; | |
| 223 | |||
| 224 | 603728 | ms->data_size_mux += pkt->size; | |
| 225 | 603728 | frame_num = atomic_fetch_add(&ost->packets_written, 1); | |
| 226 | |||
| 227 | 603728 | pkt->stream_index = ost->index; | |
| 228 | |||
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603728 times.
|
603728 | if (ms->stats.io) |
| 230 | ✗ | enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num); | |
| 231 | |||
| 232 | 603728 | ret = av_interleaved_write_frame(s, pkt); | |
| 233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603728 times.
|
603728 | 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 | 603728 | return 0; | |
| 241 | ✗ | fail: | |
| 242 | ✗ | av_packet_unref(pkt); | |
| 243 | ✗ | return ret; | |
| 244 | } | ||
| 245 | |||
| 246 | 613087 | static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof) | |
| 247 | { | ||
| 248 | 613087 | OutputFile *of = &mux->of; | |
| 249 | |||
| 250 |
2/2✓ Branch 0 taken 981 times.
✓ Branch 1 taken 612106 times.
|
613087 | if (ms->sq_idx_mux >= 0) { |
| 251 | 981 | int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt)); | |
| 252 |
2/2✓ Branch 0 taken 951 times.
✓ Branch 1 taken 30 times.
|
981 | if (ret < 0) { |
| 253 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (ret == AVERROR_EOF) |
| 254 | 30 | *stream_eof = 1; | |
| 255 | |||
| 256 | 30 | 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 602838 times.
✓ Branch 1 taken 9268 times.
|
612106 | } else if (pkt) |
| 276 | 602838 | return write_packet(mux, &ms->ost, pkt); | |
| 277 | |||
| 278 | 9268 | return 0; | |
| 279 | } | ||
| 280 | |||
| 281 | static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt); | ||
| 282 | |||
| 283 | /* apply the output bitstream filters */ | ||
| 284 | 614140 | static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt, | |
| 285 | OutputStream *ost, AVPacket *pkt, int *stream_eof) | ||
| 286 | { | ||
| 287 | 614140 | MuxStream *ms = ms_from_ost(ost); | |
| 288 | const char *err_msg; | ||
| 289 | int ret; | ||
| 290 | |||
| 291 |
4/4✓ Branch 0 taken 604955 times.
✓ Branch 1 taken 9185 times.
✓ Branch 2 taken 76434 times.
✓ Branch 3 taken 528521 times.
|
614140 | if (pkt && !ost->enc) { |
| 292 | 76434 | ret = of_streamcopy(&mux->of, ost, pkt); | |
| 293 |
2/2✓ Branch 0 taken 273 times.
✓ Branch 1 taken 76161 times.
|
76434 | if (ret == AVERROR(EAGAIN)) |
| 294 | 273 | return 0; | |
| 295 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 76093 times.
|
76161 | 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 76093 times.
|
76093 | } 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 604614 times.
✓ Branch 1 taken 9253 times.
✓ Branch 2 taken 564227 times.
✓ Branch 3 taken 40387 times.
|
613867 | if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) { |
| 306 | 564227 | mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION; | |
| 307 | 564227 | mt->fix_sub_duration_pkt->pts = pkt->pts; | |
| 308 | 564227 | mt->fix_sub_duration_pkt->time_base = pkt->time_base; | |
| 309 | |||
| 310 | 564227 | ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx, | |
| 311 | 564227 | mt->fix_sub_duration_pkt); | |
| 312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 564227 times.
|
564227 | if (ret < 0) |
| 313 | ✗ | goto fail; | |
| 314 | } | ||
| 315 | |||
| 316 |
2/2✓ Branch 0 taken 11256 times.
✓ Branch 1 taken 602611 times.
|
613867 | 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 | 602611 | ret = sync_queue_process(mux, ms, pkt, stream_eof); | |
| 353 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 602568 times.
|
602611 | if (ret < 0) |
| 354 | 43 | goto mux_fail; | |
| 355 | } | ||
| 356 | |||
| 357 |
2/2✓ Branch 0 taken 253 times.
✓ Branch 1 taken 602501 times.
|
602754 | return *stream_eof ? AVERROR_EOF : 0; |
| 358 | |||
| 359 | 50 | mux_fail: | |
| 360 | 50 | err_msg = "submitting a packet to the muxer"; | |
| 361 | |||
| 362 | 50 | fail: | |
| 363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (ret != AVERROR_EOF) |
| 364 | ✗ | av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret)); | |
| 365 | 50 | return ret; | |
| 366 | } | ||
| 367 | |||
| 368 | 8821 | static void thread_set_name(Muxer *mux) | |
| 369 | { | ||
| 370 | char name[16]; | ||
| 371 | 8821 | snprintf(name, sizeof(name), "mux%d:%s", | |
| 372 | 8821 | mux->of.index, mux->fc->oformat->name); | |
| 373 | 8821 | ff_thread_setname(name); | |
| 374 | 8821 | } | |
| 375 | |||
| 376 | 8821 | static void mux_thread_uninit(MuxThreadContext *mt) | |
| 377 | { | ||
| 378 | 8821 | av_packet_free(&mt->pkt); | |
| 379 | 8821 | av_packet_free(&mt->fix_sub_duration_pkt); | |
| 380 | |||
| 381 | 8821 | memset(mt, 0, sizeof(*mt)); | |
| 382 | 8821 | } | |
| 383 | |||
| 384 | 8821 | static int mux_thread_init(MuxThreadContext *mt) | |
| 385 | { | ||
| 386 | 8821 | memset(mt, 0, sizeof(*mt)); | |
| 387 | |||
| 388 | 8821 | mt->pkt = av_packet_alloc(); | |
| 389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8821 times.
|
8821 | if (!mt->pkt) |
| 390 | ✗ | goto fail; | |
| 391 | |||
| 392 | 8821 | mt->fix_sub_duration_pkt = av_packet_alloc(); | |
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8821 times.
|
8821 | if (!mt->fix_sub_duration_pkt) |
| 394 | ✗ | goto fail; | |
| 395 | |||
| 396 | 8821 | return 0; | |
| 397 | |||
| 398 | ✗ | fail: | |
| 399 | ✗ | mux_thread_uninit(mt); | |
| 400 | ✗ | return AVERROR(ENOMEM); | |
| 401 | } | ||
| 402 | |||
| 403 | 8821 | int muxer_thread(void *arg) | |
| 404 | { | ||
| 405 | 8821 | Muxer *mux = arg; | |
| 406 | 8821 | OutputFile *of = &mux->of; | |
| 407 | |||
| 408 | MuxThreadContext mt; | ||
| 409 | |||
| 410 | 8821 | int ret = 0; | |
| 411 | |||
| 412 | 8821 | ret = mux_thread_init(&mt); | |
| 413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8821 times.
|
8821 | if (ret < 0) |
| 414 | ✗ | goto finish; | |
| 415 | |||
| 416 | 8821 | thread_set_name(mux); | |
| 417 | |||
| 418 | 614120 | while (1) { | |
| 419 | OutputStream *ost; | ||
| 420 | 622941 | int stream_idx, stream_eof = 0; | |
| 421 | |||
| 422 | 622941 | ret = sch_mux_receive(mux->sch, of->index, mt.pkt); | |
| 423 | 622941 | stream_idx = mt.pkt->stream_index; | |
| 424 |
2/2✓ Branch 0 taken 8801 times.
✓ Branch 1 taken 614140 times.
|
622941 | if (stream_idx < 0) { |
| 425 | 8801 | av_log(mux, AV_LOG_VERBOSE, "All streams finished\n"); | |
| 426 | 8801 | ret = 0; | |
| 427 | 8821 | break; | |
| 428 | } | ||
| 429 | |||
| 430 | 614140 | ost = of->streams[mux->sch_stream_idx[stream_idx]]; | |
| 431 | 614140 | mt.pkt->stream_index = ost->index; | |
| 432 | 614140 | mt.pkt->flags &= ~AV_PKT_FLAG_TRUSTED; | |
| 433 | |||
| 434 |
2/2✓ Branch 0 taken 604955 times.
✓ Branch 1 taken 9185 times.
|
614140 | ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof); |
| 435 | 614140 | av_packet_unref(mt.pkt); | |
| 436 |
2/2✓ Branch 0 taken 303 times.
✓ Branch 1 taken 613837 times.
|
614140 | if (ret == AVERROR_EOF) { |
| 437 |
2/2✓ Branch 0 taken 283 times.
✓ Branch 1 taken 20 times.
|
303 | if (stream_eof) { |
| 438 | 283 | 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 613837 times.
|
613837 | } else if (ret < 0) { |
| 445 | ✗ | av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n"); | |
| 446 | ✗ | break; | |
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | 8821 | finish: | |
| 451 | 8821 | mux_thread_uninit(&mt); | |
| 452 | |||
| 453 | 8821 | return ret; | |
| 454 | } | ||
| 455 | |||
| 456 | 76434 | static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt) | |
| 457 | { | ||
| 458 | 76434 | MuxStream *ms = ms_from_ost(ost); | |
| 459 |
1/2✓ Branch 0 taken 76434 times.
✗ Branch 1 not taken.
|
76434 | FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL; |
| 460 |
1/2✓ Branch 0 taken 76434 times.
✗ Branch 1 not taken.
|
76434 | int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE; |
| 461 |
2/2✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 75361 times.
|
76434 | 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 74768 times.
|
76434 | 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 1157 times.
✓ Branch 1 taken 75209 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 1067 times.
|
76366 | 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 1067 times.
✓ Branch 1 taken 75209 times.
|
76276 | if (!ms->streamcopy_started) { |
| 473 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1067 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1067 | 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 880 times.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 4 times.
|
1067 | if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time) |
| 480 | 183 | return AVERROR(EAGAIN); | |
| 481 | } | ||
| 482 | |||
| 483 | 76093 | ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base); | |
| 484 | |||
| 485 |
2/2✓ Branch 0 taken 67501 times.
✓ Branch 1 taken 8592 times.
|
76093 | if (pkt->pts != AV_NOPTS_VALUE) |
| 486 | 67501 | pkt->pts -= ts_offset; | |
| 487 | |||
| 488 |
2/2✓ Branch 0 taken 8376 times.
✓ Branch 1 taken 67717 times.
|
76093 | 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 55228 times.
✓ Branch 1 taken 12489 times.
|
67717 | } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 491 | 55228 | pkt->pts = pkt->dts - ts_offset; | |
| 492 | } | ||
| 493 | |||
| 494 | 76093 | pkt->dts -= ts_offset; | |
| 495 | |||
| 496 | 76093 | ms->streamcopy_started = 1; | |
| 497 | |||
| 498 | 76093 | 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 | 8821 | int mux_check_init(void *arg) | |
| 552 | { | ||
| 553 | 8821 | Muxer *mux = arg; | |
| 554 | 8821 | OutputFile *of = &mux->of; | |
| 555 | 8821 | AVFormatContext *fc = mux->fc; | |
| 556 | int ret; | ||
| 557 | |||
| 558 | 8821 | ret = avformat_write_header(fc, &mux->opts); | |
| 559 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8821 times.
|
8821 | 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 | 8821 | mux->header_written = 1; | |
| 566 | |||
| 567 | 8821 | av_dump_format(fc, of->index, fc->url, 1); | |
| 568 | 8821 | atomic_fetch_add(&nb_output_dumped, 1); | |
| 569 | |||
| 570 | 8821 | return 0; | |
| 571 | } | ||
| 572 | |||
| 573 | 9330 | static int bsf_init(MuxStream *ms) | |
| 574 | { | ||
| 575 | 9330 | OutputStream *ost = &ms->ost; | |
| 576 | 9330 | AVBSFContext *ctx = ms->bsf_ctx; | |
| 577 | int ret; | ||
| 578 | |||
| 579 |
2/2✓ Branch 0 taken 9137 times.
✓ Branch 1 taken 193 times.
|
9330 | if (!ctx) |
| 580 | 9137 | 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 | 9330 | int of_stream_init(OutputFile *of, OutputStream *ost, | |
| 608 | const AVCodecContext *enc_ctx) | ||
| 609 | { | ||
| 610 | 9330 | Muxer *mux = mux_from_of(of); | |
| 611 | 9330 | MuxStream *ms = ms_from_ost(ost); | |
| 612 | int ret; | ||
| 613 | |||
| 614 |
2/2✓ Branch 0 taken 8421 times.
✓ Branch 1 taken 909 times.
|
9330 | 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 8417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
8421 | if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) |
| 617 | 8417 | ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1}); | |
| 618 | |||
| 619 | 8421 | ost->st->avg_frame_rate = enc_ctx->framerate; | |
| 620 | 8421 | ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio; | |
| 621 | |||
| 622 | 8421 | ret = avcodec_parameters_from_context(ms->par_in, enc_ctx); | |
| 623 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8421 times.
|
8421 | 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 | 9330 | ret = bsf_init(ms); | |
| 634 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9330 times.
|
9330 | if (ret < 0) |
| 635 | ✗ | return ret; | |
| 636 | |||
| 637 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9325 times.
|
9330 | 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 6207 times.
✓ Branch 1 taken 3123 times.
|
9330 | if (ms->stream_duration) { |
| 672 | 6207 | ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb, | |
| 673 | 6207 | ost->st->time_base); | |
| 674 | } | ||
| 675 | |||
| 676 |
2/2✓ Branch 0 taken 9329 times.
✓ Branch 1 taken 1 times.
|
9330 | if (ms->sch_idx >= 0) |
| 677 | 9329 | return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx); | |
| 678 | |||
| 679 | 1 | return 0; | |
| 680 | } | ||
| 681 | |||
| 682 | 8821 | static int check_written(OutputFile *of) | |
| 683 | { | ||
| 684 | 8821 | int64_t total_packets_written = 0; | |
| 685 | 8821 | int pass1_used = 1; | |
| 686 | 8821 | int ret = 0; | |
| 687 | |||
| 688 |
2/2✓ Branch 0 taken 9330 times.
✓ Branch 1 taken 8821 times.
|
18151 | for (int i = 0; i < of->nb_streams; i++) { |
| 689 | 9330 | OutputStream *ost = of->streams[i]; | |
| 690 | 9330 | uint64_t packets_written = atomic_load(&ost->packets_written); | |
| 691 | |||
| 692 | 9330 | total_packets_written += packets_written; | |
| 693 | |||
| 694 |
2/2✓ Branch 0 taken 8421 times.
✓ Branch 1 taken 909 times.
|
9330 | if (ost->enc && |
| 695 |
2/2✓ Branch 0 taken 8413 times.
✓ Branch 1 taken 8 times.
|
8421 | (ost->enc->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) |
| 696 | != AV_CODEC_FLAG_PASS1) | ||
| 697 | 8413 | pass1_used = 0; | |
| 698 | |||
| 699 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 9265 times.
|
9330 | if (!packets_written && |
| 700 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | (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 26 times.
✓ Branch 1 taken 8795 times.
|
8821 | if (!total_packets_written) { |
| 707 | 26 | int level = AV_LOG_WARNING; | |
| 708 | |||
| 709 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | 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 23 times.
✓ Branch 1 taken 3 times.
|
26 | 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 | 8821 | return ret; | |
| 719 | } | ||
| 720 | |||
| 721 | 8821 | static void mux_final_stats(Muxer *mux) | |
| 722 | { | ||
| 723 | 8821 | OutputFile *of = &mux->of; | |
| 724 | 8821 | uint64_t total_packets = 0, total_size = 0; | |
| 725 | 8821 | uint64_t video_size = 0, audio_size = 0, subtitle_size = 0, | |
| 726 | 8821 | extra_size = 0, other_size = 0; | |
| 727 | |||
| 728 | 8821 | uint8_t overhead[16] = "unknown"; | |
| 729 | 8821 | int64_t file_size = of_filesize(of); | |
| 730 | |||
| 731 | 8821 | av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n", | |
| 732 | of->index, of->url); | ||
| 733 | |||
| 734 |
2/2✓ Branch 0 taken 9330 times.
✓ Branch 1 taken 8821 times.
|
18151 | for (int j = 0; j < of->nb_streams; j++) { |
| 735 | 9330 | OutputStream *ost = of->streams[j]; | |
| 736 | 9330 | MuxStream *ms = ms_from_ost(ost); | |
| 737 | 9330 | const AVCodecParameters *par = ost->st->codecpar; | |
| 738 | 9330 | const enum AVMediaType type = par->codec_type; | |
| 739 | 9330 | const uint64_t s = ms->data_size_mux; | |
| 740 | |||
| 741 |
4/4✓ Branch 0 taken 7388 times.
✓ Branch 1 taken 1848 times.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 13 times.
|
9330 | switch (type) { |
| 742 | 7388 | case AVMEDIA_TYPE_VIDEO: video_size += s; break; | |
| 743 | 1848 | case AVMEDIA_TYPE_AUDIO: audio_size += s; break; | |
| 744 | 81 | case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break; | |
| 745 | 13 | default: other_size += s; break; | |
| 746 | } | ||
| 747 | |||
| 748 | 9330 | extra_size += par->extradata_size; | |
| 749 | 9330 | total_size += s; | |
| 750 | 9330 | total_packets += atomic_load(&ost->packets_written); | |
| 751 | |||
| 752 | 9330 | 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 8421 times.
✓ Branch 1 taken 909 times.
|
9330 | if (ost->enc) { |
| 755 | 8421 | av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded", | |
| 756 | 8421 | ost->enc->frames_encoded); | |
| 757 |
2/2✓ Branch 0 taken 1449 times.
✓ Branch 1 taken 6972 times.
|
8421 | if (type == AVMEDIA_TYPE_AUDIO) |
| 758 | 1449 | av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->enc->samples_encoded); | |
| 759 | 8421 | av_log(of, AV_LOG_VERBOSE, "; "); | |
| 760 | } | ||
| 761 | |||
| 762 | 9330 | av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ", | |
| 763 | 9330 | atomic_load(&ost->packets_written), s); | |
| 764 | |||
| 765 | 9330 | av_log(of, AV_LOG_VERBOSE, "\n"); | |
| 766 | } | ||
| 767 | |||
| 768 | 8821 | 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 8794 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 8659 times.
✓ Branch 3 taken 135 times.
✓ Branch 4 taken 6421 times.
✓ Branch 5 taken 2238 times.
|
8821 | if (total_size && file_size > 0 && file_size >= total_size) { |
| 772 | 6421 | snprintf(overhead, sizeof(overhead), "%f%%", | |
| 773 | 6421 | 100.0 * (file_size - total_size) / total_size); | |
| 774 | } | ||
| 775 | |||
| 776 | 8821 | 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 | 8821 | } | |
| 786 | |||
| 787 | 8822 | int of_write_trailer(OutputFile *of) | |
| 788 | { | ||
| 789 | 8822 | Muxer *mux = mux_from_of(of); | |
| 790 | 8822 | AVFormatContext *fc = mux->fc; | |
| 791 | 8822 | int ret, mux_result = 0; | |
| 792 | |||
| 793 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8821 times.
|
8822 | 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 | 8821 | ret = av_write_trailer(fc); | |
| 801 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8821 times.
|
8821 | 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 | 8821 | mux->last_filesize = filesize(fc->pb); | |
| 807 | |||
| 808 |
2/2✓ Branch 0 taken 8685 times.
✓ Branch 1 taken 136 times.
|
8821 | if (!(fc->oformat->flags & AVFMT_NOFILE)) { |
| 809 | 8685 | ret = avio_closep(&fc->pb); | |
| 810 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8685 times.
|
8685 | 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 | 8821 | mux_final_stats(mux); | |
| 817 | |||
| 818 | // check whether anything was actually written | ||
| 819 | 8821 | ret = check_written(of); | |
| 820 | 8821 | mux_result = err_merge(mux_result, ret); | |
| 821 | |||
| 822 | 8821 | return mux_result; | |
| 823 | } | ||
| 824 | |||
| 825 | 28005 | static void enc_stats_uninit(EncStats *es) | |
| 826 | { | ||
| 827 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28005 times.
|
28005 | for (int i = 0; i < es->nb_components; i++) |
| 828 | ✗ | av_freep(&es->components[i].str); | |
| 829 | 28005 | av_freep(&es->components); | |
| 830 | |||
| 831 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28005 times.
|
28005 | if (es->lock_initialized) |
| 832 | ✗ | pthread_mutex_destroy(&es->lock); | |
| 833 | 28005 | es->lock_initialized = 0; | |
| 834 | 28005 | } | |
| 835 | |||
| 836 | 9335 | static void ost_free(OutputStream **post) | |
| 837 | { | ||
| 838 | 9335 | OutputStream *ost = *post; | |
| 839 | MuxStream *ms; | ||
| 840 | |||
| 841 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9335 times.
|
9335 | if (!ost) |
| 842 | ✗ | return; | |
| 843 | 9335 | ms = ms_from_ost(ost); | |
| 844 | |||
| 845 | 9335 | enc_free(&ost->enc); | |
| 846 | 9335 | fg_free(&ost->fg_simple); | |
| 847 | |||
| 848 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9327 times.
|
9335 | 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 | 9335 | avcodec_parameters_free(&ms->par_in); | |
| 857 | |||
| 858 | 9335 | av_bsf_free(&ms->bsf_ctx); | |
| 859 | 9335 | av_packet_free(&ms->bsf_pkt); | |
| 860 | |||
| 861 | 9335 | av_packet_free(&ms->pkt); | |
| 862 | |||
| 863 | 9335 | av_freep(&ost->kf.pts); | |
| 864 | 9335 | av_expr_free(ost->kf.pexpr); | |
| 865 | |||
| 866 | 9335 | av_freep(&ost->logfile_prefix); | |
| 867 | |||
| 868 | 9335 | av_freep(&ost->attachment_filename); | |
| 869 | |||
| 870 | 9335 | enc_stats_uninit(&ost->enc_stats_pre); | |
| 871 | 9335 | enc_stats_uninit(&ost->enc_stats_post); | |
| 872 | 9335 | enc_stats_uninit(&ms->stats); | |
| 873 | |||
| 874 | 9335 | av_freep(post); | |
| 875 | } | ||
| 876 | |||
| 877 | 8827 | static void fc_close(AVFormatContext **pfc) | |
| 878 | { | ||
| 879 | 8827 | AVFormatContext *fc = *pfc; | |
| 880 | |||
| 881 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8826 times.
|
8827 | if (!fc) |
| 882 | 1 | return; | |
| 883 | |||
| 884 |
2/2✓ Branch 0 taken 8686 times.
✓ Branch 1 taken 140 times.
|
8826 | if (!(fc->oformat->flags & AVFMT_NOFILE)) |
| 885 | 8686 | avio_closep(&fc->pb); | |
| 886 | 8826 | avformat_free_context(fc); | |
| 887 | |||
| 888 | 8826 | *pfc = NULL; | |
| 889 | } | ||
| 890 | |||
| 891 | 8827 | void of_free(OutputFile **pof) | |
| 892 | { | ||
| 893 | 8827 | OutputFile *of = *pof; | |
| 894 | Muxer *mux; | ||
| 895 | |||
| 896 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8827 times.
|
8827 | if (!of) |
| 897 | ✗ | return; | |
| 898 | 8827 | mux = mux_from_of(of); | |
| 899 | |||
| 900 | 8827 | sq_free(&mux->sq_mux); | |
| 901 | |||
| 902 |
2/2✓ Branch 0 taken 9335 times.
✓ Branch 1 taken 8827 times.
|
18162 | for (int i = 0; i < of->nb_streams; i++) |
| 903 | 9335 | ost_free(&of->streams[i]); | |
| 904 | 8827 | av_freep(&of->streams); | |
| 905 | |||
| 906 | 8827 | av_freep(&mux->sch_stream_idx); | |
| 907 | |||
| 908 | 8827 | av_dict_free(&mux->opts); | |
| 909 | 8827 | av_dict_free(&mux->enc_opts_used); | |
| 910 | |||
| 911 | 8827 | av_packet_free(&mux->sq_pkt); | |
| 912 | |||
| 913 | 8827 | fc_close(&mux->fc); | |
| 914 | |||
| 915 | 8827 | av_freep(pof); | |
| 916 | } | ||
| 917 | |||
| 918 | 36823 | int64_t of_filesize(OutputFile *of) | |
| 919 | { | ||
| 920 | 36823 | Muxer *mux = mux_from_of(of); | |
| 921 | 36823 | return atomic_load(&mux->last_filesize); | |
| 922 | } | ||
| 923 |