38#include "libavutil/fifo.h"
39#include "libavutil/intreadwrite.h"
40#include "libavutil/log.h"
41#include "libavutil/mem.h"
42#include "libavutil/timestamp.h"
44#include "libavcodec/packet.h"
46#include "libavformat/avformat.h"
47#include "libavformat/avio.h"
54 ost2->
finished |= ost == ost2 ? this_stream : others;
61 AVFormatContext *s = of->
ctx;
62 AVStream *st = ost->
st;
73 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->
encoding_needed) && !unqueue) {
86 unsigned int are_we_over_size =
89 size_t new_size = FFMIN(2 * cur_size, limit);
91 if (new_size <= cur_size) {
92 av_log(NULL, AV_LOG_ERROR,
93 "Too many packets buffered for output stream %d:%d.\n",
97 ret = av_fifo_grow2(ost->
muxing_queue, new_size - cur_size);
101 ret = av_packet_make_refcounted(pkt);
104 tmp_pkt = av_packet_alloc();
107 av_packet_move_ref(tmp_pkt, pkt);
115 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
117 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
119 if (pkt->duration > 0)
120 av_log(NULL, AV_LOG_WARNING,
"Overriding packet duration by frame rate, this should not happen\n");
121 pkt->duration = av_rescale_q(1, av_inv_q(ost->
frame_rate),
128 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
129 if (pkt->dts != AV_NOPTS_VALUE &&
130 pkt->pts != AV_NOPTS_VALUE &&
131 pkt->dts > pkt->pts) {
132 av_log(s, AV_LOG_WARNING,
"Invalid DTS: %"PRId64
" PTS: %"PRId64
" in output stream %d:%d, replacing by guess\n",
140 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
141 pkt->dts != AV_NOPTS_VALUE &&
143 int64_t max = ost->
last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
144 if (pkt->dts < max) {
145 int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
147 loglevel = AV_LOG_ERROR;
148 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
149 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
152 av_log(NULL, AV_LOG_FATAL,
"aborting.\n");
155 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
156 "in incorrect timestamps in the output file.\n",
158 if (pkt->pts >= pkt->dts)
159 pkt->pts = FFMAX(pkt->pts, max);
169 pkt->stream_index = ost->
index;
172 av_log(NULL, AV_LOG_INFO,
"muxer <- type:%s "
173 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
174 av_get_media_type_string(ost->
enc_ctx->codec_type),
175 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->
st->time_base),
176 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->
st->time_base),
177 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->
st->time_base),
182 ret = av_interleaved_write_frame(s, pkt);
196 AVFormatContext **avc;
207 if (!strcmp(
output_files[i]->ctx->oformat->name,
"rtp")) {
214 av_log(NULL, AV_LOG_ERROR,
"No output streams in the SDP.\n");
215 ret = AVERROR(EINVAL);
219 ret = av_sdp_create(avc, j, sdp,
sizeof(sdp));
224 printf(
"SDP:\n%s\n", sdp);
229 av_log(NULL, AV_LOG_ERROR,
"Failed to open sdp file '%s'\n",
sdp_filename);
233 avio_print(sdp_pb, sdp);
234 avio_closep(&sdp_pb);
248 for (i = 0; i < of->
ctx->nb_streams; i++) {
254 ret = avformat_write_header(of->
ctx, &of->
opts);
256 av_log(NULL, AV_LOG_ERROR,
257 "Could not write header for output file #%d "
258 "(incorrect codec parameters ?): %s\n",
259 of->
index, av_err2str(ret));
265 av_dump_format(of->
ctx, of->
index, of->
ctx->url, 1);
271 av_log(NULL, AV_LOG_ERROR,
"Error writing the SDP.\n");
277 for (i = 0; i < of->
ctx->nb_streams; i++) {
288 av_packet_free(&pkt);
300 av_log(NULL, AV_LOG_ERROR,
301 "Nothing was written into output file %d (%s), because "
302 "at least one of its streams received no packets.\n",
304 return AVERROR(EINVAL);
307 ret = av_write_trailer(of->
ctx);
309 av_log(NULL, AV_LOG_ERROR,
"Error writing trailer of %s: %s\n", of->
ctx->url, av_err2str(ret));
325 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
327 avformat_free_context(s);
328 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