54#include "libavutil/fifo.h"
55#include "libavutil/intreadwrite.h"
56#include "libavutil/log.h"
57#include "libavutil/mem.h"
58#include "libavutil/timestamp.h"
59#include "libavutil/thread.h"
61#include "libavcodec/packet.h"
63#include "libavformat/avformat.h"
64#include "libavformat/avio.h"
94 AVFormatContext *s = mux->
fc;
95 AVStream *st = ost->
st;
108 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
110 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
112 if (pkt->duration > 0)
113 av_log(ost, AV_LOG_WARNING,
"Overriding packet duration by frame rate, this should not happen\n");
114 pkt->duration = av_rescale_q(1, av_inv_q(ost->
frame_rate),
119 av_packet_rescale_ts(pkt, pkt->time_base, ost->
st->time_base);
120 pkt->time_base = ost->
st->time_base;
122 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
123 if (pkt->dts != AV_NOPTS_VALUE &&
124 pkt->pts != AV_NOPTS_VALUE &&
125 pkt->dts > pkt->pts) {
126 av_log(s, AV_LOG_WARNING,
"Invalid DTS: %"PRId64
" PTS: %"PRId64
" in output stream %d:%d, replacing by guess\n",
134 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
135 pkt->dts != AV_NOPTS_VALUE &&
137 int64_t max = ms->
last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
138 if (pkt->dts < max) {
139 int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
141 loglevel = AV_LOG_ERROR;
142 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
143 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
146 ret = AVERROR(EINVAL);
150 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
151 "in incorrect timestamps in the output file.\n",
153 if (pkt->pts >= pkt->dts)
154 pkt->pts = FFMAX(pkt->pts, max);
164 pkt->stream_index = ost->
index;
167 av_log(ost, AV_LOG_INFO,
"muxer <- type:%s "
168 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
169 av_get_media_type_string(st->codecpar->codec_type),
170 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->
st->time_base),
171 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->
st->time_base),
172 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->
st->time_base),
180 ret = av_interleaved_write_frame(s, pkt);
188 av_packet_unref(pkt);
199 if (ret == AVERROR_EOF)
208 return (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) ? 0 : ret;
224 snprintf(name,
sizeof(name),
"mux%d:%s", of->
index, of->
format->name);
225 ff_thread_setname(name);
232 AVPacket *pkt = NULL;
235 pkt = av_packet_alloc();
237 ret = AVERROR(ENOMEM);
245 int stream_idx, stream_eof = 0;
248 if (stream_idx < 0) {
249 av_log(mux, AV_LOG_VERBOSE,
"All streams finished\n");
256 av_packet_unref(pkt);
257 if (ret == AVERROR_EOF && stream_eof)
260 av_log(mux, AV_LOG_ERROR,
"Error muxing a packet\n");
266 av_packet_free(&pkt);
268 for (
unsigned int i = 0; i < mux->
fc->nb_streams; i++)
271 av_log(mux, AV_LOG_VERBOSE,
"Terminating muxer thread\n");
273 return (
void*)(intptr_t)ret;
291 av_packet_unref(pkt);
295 return ret == AVERROR_EOF ? 0 : ret;
301 AVPacket *tmp_pkt = NULL;
306 size_t pkt_size = pkt ? pkt->size : 0;
307 unsigned int are_we_over_size =
310 size_t new_size = FFMIN(2 * cur_size, limit);
312 if (new_size <= cur_size) {
313 av_log(ost, AV_LOG_ERROR,
314 "Too many packets buffered for output stream %d:%d.\n",
316 return AVERROR(ENOSPC);
318 ret = av_fifo_grow2(ms->
muxing_queue, new_size - cur_size);
324 ret = av_packet_make_refcounted(pkt);
328 tmp_pkt = av_packet_alloc();
330 return AVERROR(ENOMEM);
332 av_packet_move_ref(tmp_pkt, pkt);
351 av_packet_unref(pkt);
366 if (!eof && pkt->dts != AV_NOPTS_VALUE)
367 ost->
last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
373 ret = av_bsf_send_packet(ms->
bsf_ctx, eof ? NULL : pkt);
375 err_msg =
"submitting a packet for bitstream filtering";
380 ret = av_bsf_receive_packet(ms->
bsf_ctx, pkt);
381 if (ret == AVERROR(EAGAIN))
383 else if (ret == AVERROR_EOF)
386 err_msg =
"applying bitstream filters to a packet";
403 err_msg =
"submitting a packet to the muxer";
406 av_log(ost, AV_LOG_ERROR,
"Error %s\n", err_msg);
416 if (!mux || !mux->
tq)
419 for (
unsigned int i = 0; i < mux->
fc->nb_streams; i++)
422 pthread_join(mux->
thread, &ret);
426 return (
int)(intptr_t)ret;
431 av_packet_move_ref(dst, src);
436 AVFormatContext *fc = mux->
fc;
442 return AVERROR(ENOMEM);
447 return AVERROR(ENOMEM);
457 for (
int i = 0; i < fc->nb_streams; i++) {
470 av_packet_free(&pkt);
486 AVFormatContext **avc;
495 return AVERROR(ENOMEM);
504 av_log(NULL, AV_LOG_ERROR,
"No output streams in the SDP.\n");
505 ret = AVERROR(EINVAL);
509 ret = av_sdp_create(avc, j, sdp,
sizeof(sdp));
514 av_log(NULL, AV_LOG_ERROR,
"SDP:\n%s\n", sdp);
519 av_log(NULL, AV_LOG_ERROR,
"Failed to open sdp file '%s'\n",
sdp_filename);
523 avio_print(sdp_pb, sdp);
524 avio_closep(&sdp_pb);
539 AVFormatContext *fc = mux->
fc;
542 for (i = 0; i < fc->nb_streams; i++) {
548 ret = avformat_write_header(fc, &mux->
opts);
550 av_log(mux, AV_LOG_ERROR,
"Could not write header (incorrect codec "
551 "parameters ?): %s\n", av_err2str(ret));
557 av_dump_format(fc, of->
index, fc->url, 1);
563 av_log(NULL, AV_LOG_ERROR,
"Error writing the SDP.\n");
565 }
else if (ret == 1) {
586 AVBSFContext *ctx = ms->
bsf_ctx;
592 ret = avcodec_parameters_copy(ctx->par_in, ost->
st->codecpar);
596 ctx->time_base_in = ost->
st->time_base;
598 ret = av_bsf_init(ctx);
600 av_log(ms, AV_LOG_ERROR,
"Error initializing bitstream filter: %s\n",
605 ret = avcodec_parameters_copy(ost->
st->codecpar, ctx->par_out);
608 ost->
st->time_base = ctx->time_base_out;
637 AVFormatContext *fc = mux->
fc;
641 av_log(mux, AV_LOG_ERROR,
642 "Nothing was written into output file, because "
643 "at least one of its streams received no packets.\n");
644 return AVERROR(EINVAL);
651 ret = av_write_trailer(fc);
653 av_log(mux, AV_LOG_ERROR,
"Error writing trailer: %s\n", av_err2str(ret));
659 if (!(of->
format->flags & AVFMT_NOFILE)) {
660 ret = avio_closep(&fc->pb);
662 av_log(mux, AV_LOG_ERROR,
"Error closing file: %s\n", av_err2str(ret));
681 av_log(ms, AV_LOG_ERROR,
682 "Error closing logfile, loss of information possible: %s\n",
683 av_err2str(AVERROR(errno)));
690 av_packet_free(&pkt);
699 av_packet_free(&ost->
pkt);
702 av_freep(&ost->
kf.
pts);
707 av_freep(&ost->
apad);
709#if FFMPEG_OPT_MAP_CHANNEL
718 av_freep(&ost->
enc_ctx->stats_in);
719 avcodec_free_context(&ost->
enc_ctx);
738 AVFormatContext *fc = *pfc;
743 if (!(fc->oformat->flags & AVFMT_NOFILE))
744 avio_closep(&fc->pb);
745 avformat_free_context(fc);
768 av_dict_free(&mux->
opts);
770 av_packet_free(&mux->
sq_pkt);
EncStatsComponent * components
int max_muxing_queue_size
size_t muxing_queue_data_size
size_t muxing_queue_data_threshold
atomic_int_least64_t last_filesize
const AVOutputFormat * format
int audio_channels_mapped
enum VideoSyncMethod vsync_method
AVDictionary * encoder_opts
atomic_uint_least64_t packets_written