39#include "libavutil/fifo.h"
40#include "libavutil/intreadwrite.h"
41#include "libavutil/log.h"
42#include "libavutil/mem.h"
43#include "libavutil/timestamp.h"
45#include "libavcodec/packet.h"
47#include "libavformat/avformat.h"
48#include "libavformat/avio.h"
55 ost2->
finished |= ost == ost2 ? this_stream : others;
62 AVFormatContext *s = of->
ctx;
63 AVStream *st = ost->
st;
74 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->
encoding_needed) && !unqueue) {
87 unsigned int are_we_over_size =
90 size_t new_size = FFMIN(2 * cur_size, limit);
92 if (new_size <= cur_size) {
93 av_log(NULL, AV_LOG_ERROR,
94 "Too many packets buffered for output stream %d:%d.\n",
98 ret = av_fifo_grow2(ost->
muxing_queue, new_size - cur_size);
102 ret = av_packet_make_refcounted(pkt);
105 tmp_pkt = av_packet_alloc();
108 av_packet_move_ref(tmp_pkt, pkt);
116 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
118 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
120 if (pkt->duration > 0)
121 av_log(NULL, AV_LOG_WARNING,
"Overriding packet duration by frame rate, this should not happen\n");
122 pkt->duration = av_rescale_q(1, av_inv_q(ost->
frame_rate),
129 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
130 if (pkt->dts != AV_NOPTS_VALUE &&
131 pkt->pts != AV_NOPTS_VALUE &&
132 pkt->dts > pkt->pts) {
133 av_log(s, AV_LOG_WARNING,
"Invalid DTS: %"PRId64
" PTS: %"PRId64
" in output stream %d:%d, replacing by guess\n",
141 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
142 pkt->dts != AV_NOPTS_VALUE &&
144 int64_t max = ost->
last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
145 if (pkt->dts < max) {
146 int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
148 loglevel = AV_LOG_ERROR;
149 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
150 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
153 av_log(NULL, AV_LOG_FATAL,
"aborting.\n");
156 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
157 "in incorrect timestamps in the output file.\n",
159 if (pkt->pts >= pkt->dts)
160 pkt->pts = FFMAX(pkt->pts, max);
170 pkt->stream_index = ost->
index;
173 av_log(NULL, AV_LOG_INFO,
"muxer <- type:%s "
174 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
175 av_get_media_type_string(ost->
enc_ctx->codec_type),
176 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->
st->time_base),
177 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->
st->time_base),
178 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->
st->time_base),
183 ret = av_interleaved_write_frame(s, pkt);
197 AVFormatContext **avc;
208 if (!strcmp(
output_files[i]->ctx->oformat->name,
"rtp")) {
215 av_log(NULL, AV_LOG_ERROR,
"No output streams in the SDP.\n");
216 ret = AVERROR(EINVAL);
220 ret = av_sdp_create(avc, j, sdp,
sizeof(sdp));
225 av_log(NULL, AV_LOG_ERROR,
"SDP:\n%s\n", sdp);
230 av_log(NULL, AV_LOG_ERROR,
"Failed to open sdp file '%s'\n",
sdp_filename);
234 avio_print(sdp_pb, sdp);
235 avio_closep(&sdp_pb);
249 for (i = 0; i < of->
ctx->nb_streams; i++) {
255 ret = avformat_write_header(of->
ctx, &of->
opts);
257 av_log(NULL, AV_LOG_ERROR,
258 "Could not write header for output file #%d "
259 "(incorrect codec parameters ?): %s\n",
260 of->
index, av_err2str(ret));
266 av_dump_format(of->
ctx, of->
index, of->
ctx->url, 1);
272 av_log(NULL, AV_LOG_ERROR,
"Error writing the SDP.\n");
278 for (i = 0; i < of->
ctx->nb_streams; i++) {
289 av_packet_free(&pkt);
301 av_log(NULL, AV_LOG_ERROR,
302 "Nothing was written into output file %d (%s), because "
303 "at least one of its streams received no packets.\n",
305 return AVERROR(EINVAL);
308 ret = av_write_trailer(of->
ctx);
310 av_log(NULL, AV_LOG_ERROR,
"Error writing trailer of %s: %s\n", of->
ctx->url, av_err2str(ret));
326 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
328 avformat_free_context(s);
329 av_dict_free(&of->
opts);
int max_muxing_queue_size
size_t muxing_queue_data_threshold
enum VideoSyncMethod vsync_method
size_t muxing_queue_data_size