FFmpeg
ffmpeg_mux.c
Go to the documentation of this file.
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 <stdio.h>
20 #include <string.h>
21 
22 #include "ffmpeg.h"
23 
24 #include "libavutil/fifo.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/timestamp.h"
29 
30 #include "libavcodec/packet.h"
31 
32 #include "libavformat/avformat.h"
33 #include "libavformat/avio.h"
34 
36 {
37  int i;
38  for (i = 0; i < nb_output_streams; i++) {
39  OutputStream *ost2 = output_streams[i];
40  ost2->finished |= ost == ost2 ? this_stream : others;
41  }
42 }
43 
45  int unqueue)
46 {
47  AVFormatContext *s = of->ctx;
48  AVStream *st = ost->st;
49  int ret;
50 
51  /*
52  * Audio encoders may split the packets -- #frames in != #packets out.
53  * But there is no reordering, so we can limit the number of output packets
54  * by simply dropping them here.
55  * Counting encoded video frames needs to be done separately because of
56  * reordering, see do_video_out().
57  * Do not count the packet when unqueued because it has been counted when queued.
58  */
59  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) {
60  if (ost->frame_number >= ost->max_frames) {
62  return;
63  }
64  ost->frame_number++;
65  }
66 
67  if (!of->header_written) {
68  AVPacket *tmp_pkt;
69  /* the muxer is not initialized yet, buffer the packet */
70  if (!av_fifo_can_write(ost->muxing_queue)) {
71  size_t cur_size = av_fifo_can_read(ost->muxing_queue);
72  unsigned int are_we_over_size =
73  (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
74  size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX;
75  size_t new_size = FFMIN(2 * cur_size, limit);
76 
77  if (new_size <= cur_size) {
79  "Too many packets buffered for output stream %d:%d.\n",
80  ost->file_index, ost->st->index);
81  exit_program(1);
82  }
83  ret = av_fifo_grow2(ost->muxing_queue, new_size - cur_size);
84  if (ret < 0)
85  exit_program(1);
86  }
88  if (ret < 0)
89  exit_program(1);
90  tmp_pkt = av_packet_alloc();
91  if (!tmp_pkt)
92  exit_program(1);
93  av_packet_move_ref(tmp_pkt, pkt);
94  ost->muxing_queue_data_size += tmp_pkt->size;
95  av_fifo_write(ost->muxing_queue, &tmp_pkt, 1);
96  return;
97  }
98 
99  if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP) ||
101  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
102 
103  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
104  if (ost->frame_rate.num && ost->is_cfr) {
105  if (pkt->duration > 0)
106  av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
107  pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
108  ost->mux_timebase);
109  }
110  }
111 
112  av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
113 
114  if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
115  if (pkt->dts != AV_NOPTS_VALUE &&
116  pkt->pts != AV_NOPTS_VALUE &&
117  pkt->dts > pkt->pts) {
118  av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
119  pkt->dts, pkt->pts,
120  ost->file_index, ost->st->index);
121  pkt->pts =
122  pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1
123  - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1)
124  - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1);
125  }
127  pkt->dts != AV_NOPTS_VALUE &&
128  ost->last_mux_dts != AV_NOPTS_VALUE) {
129  int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
130  if (pkt->dts < max) {
131  int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
132  if (exit_on_error)
133  loglevel = AV_LOG_ERROR;
134  av_log(s, loglevel, "Non-monotonous DTS in output stream "
135  "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
136  ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
137  if (exit_on_error) {
138  av_log(NULL, AV_LOG_FATAL, "aborting.\n");
139  exit_program(1);
140  }
141  av_log(s, loglevel, "changing to %"PRId64". This may result "
142  "in incorrect timestamps in the output file.\n",
143  max);
144  if (pkt->pts >= pkt->dts)
145  pkt->pts = FFMAX(pkt->pts, max);
146  pkt->dts = max;
147  }
148  }
149  }
150  ost->last_mux_dts = pkt->dts;
151 
152  ost->data_size += pkt->size;
153  ost->packets_written++;
154 
156 
157  if (debug_ts) {
158  av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
159  "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
160  av_get_media_type_string(ost->enc_ctx->codec_type),
164  pkt->size
165  );
166  }
167 
169  if (ret < 0) {
170  print_error("av_interleaved_write_frame()", ret);
171  main_return_code = 1;
173  }
174 }
175 
176 static int print_sdp(void)
177 {
178  char sdp[16384];
179  int i;
180  int j, ret;
181  AVIOContext *sdp_pb;
182  AVFormatContext **avc;
183 
184  for (i = 0; i < nb_output_files; i++) {
185  if (!output_files[i]->header_written)
186  return 0;
187  }
188 
189  avc = av_malloc_array(nb_output_files, sizeof(*avc));
190  if (!avc)
191  exit_program(1);
192  for (i = 0, j = 0; i < nb_output_files; i++) {
193  if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) {
194  avc[j] = output_files[i]->ctx;
195  j++;
196  }
197  }
198 
199  if (!j) {
200  av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
201  ret = AVERROR(EINVAL);
202  goto fail;
203  }
204 
205  ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
206  if (ret < 0)
207  goto fail;
208 
209  if (!sdp_filename) {
210  printf("SDP:\n%s\n", sdp);
211  fflush(stdout);
212  } else {
214  if (ret < 0) {
215  av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
216  goto fail;
217  }
218 
219  avio_print(sdp_pb, sdp);
220  avio_closep(&sdp_pb);
222  }
223 
224 fail:
225  av_freep(&avc);
226  return ret;
227 }
228 
229 /* open the muxer when all the streams are initialized */
231 {
232  int ret, i;
233 
234  for (i = 0; i < of->ctx->nb_streams; i++) {
236  if (!ost->initialized)
237  return 0;
238  }
239 
240  ret = avformat_write_header(of->ctx, &of->opts);
241  if (ret < 0) {
243  "Could not write header for output file #%d "
244  "(incorrect codec parameters ?): %s\n",
245  of->index, av_err2str(ret));
246  return ret;
247  }
248  //assert_avoptions(of->opts);
249  of->header_written = 1;
250 
251  av_dump_format(of->ctx, of->index, of->ctx->url, 1);
253 
254  if (sdp_filename || want_sdp) {
255  ret = print_sdp();
256  if (ret < 0) {
257  av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n");
258  return ret;
259  }
260  }
261 
262  /* flush the muxing queues */
263  for (i = 0; i < of->ctx->nb_streams; i++) {
265  AVPacket *pkt;
266 
267  /* try to improve muxing time_base (only possible if nothing has been written yet) */
268  if (!av_fifo_can_read(ost->muxing_queue))
269  ost->mux_timebase = ost->st->time_base;
270 
271  while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0) {
272  ost->muxing_queue_data_size -= pkt->size;
273  of_write_packet(of, pkt, ost, 1);
275  }
276  }
277 
278  return 0;
279 }
280 
282 {
283  int ret;
284 
285  if (!of->header_written) {
287  "Nothing was written into output file %d (%s), because "
288  "at least one of its streams received no packets.\n",
289  of->index, of->ctx->url);
290  return AVERROR(EINVAL);
291  }
292 
293  ret = av_write_trailer(of->ctx);
294  if (ret < 0) {
295  av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", of->ctx->url, av_err2str(ret));
296  return ret;
297  }
298 
299  return 0;
300 }
301 
302 void of_close(OutputFile **pof)
303 {
304  OutputFile *of = *pof;
306 
307  if (!of)
308  return;
309 
310  s = of->ctx;
311  if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
312  avio_closep(&s->pb);
314  av_dict_free(&of->opts);
315 
316  av_freep(pof);
317 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
audio_sync_method
int audio_sync_method
Definition: ffmpeg_opt.c:160
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:481
print_sdp
static int print_sdp(void)
Definition: ffmpeg_mux.c:176
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
nb_output_dumped
unsigned nb_output_dumped
Definition: ffmpeg.c:138
ffmpeg.h
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
max
#define max(a, b)
Definition: cuda_runtime.h:33
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ost
static AVStream * ost
Definition: vaapi_transcode.c:45
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:73
want_sdp
int want_sdp
Definition: ffmpeg.c:140
exit_program
void exit_program(int ret)
Wraps exit with a program-specific cleanup routine.
Definition: cmdutils.c:93
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:170
print_error
void print_error(const char *filename, int err)
Print an error message to stderr, indicating filename and a human readable description of the error c...
Definition: cmdutils.c:793
fifo.h
avio_open2
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1249
fail
#define fail()
Definition: checkasm.h:131
of_close
void of_close(OutputFile **pof)
Definition: ffmpeg_mux.c:302
OutputFile::opts
AVDictionary * opts
Definition: ffmpeg.h:590
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dump_format
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate,...
Definition: dump.c:630
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
ENCODER_FINISHED
@ ENCODER_FINISHED
Definition: ffmpeg.h:451
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:629
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
MUXER_FINISHED
@ MUXER_FINISHED
Definition: ffmpeg.h:452
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
OutputFile::header_written
int header_written
Definition: ffmpeg.h:598
av_fifo_can_write
size_t av_fifo_can_write(const AVFifo *f)
Definition: fifo.c:94
of_write_packet
void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg_mux.c:44
avformat_write_header
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:449
output_streams
OutputStream ** output_streams
Definition: ffmpeg.c:152
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:978
NULL
#define NULL
Definition: coverity.c:32
avio_print
#define avio_print(s,...)
Write strings (const char *) to the context.
Definition: avio.h:548
main_return_code
int main_return_code
Definition: ffmpeg.c:346
av_packet_move_ref
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:479
OutputFile::index
int index
Definition: ffmpeg.h:585
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1269
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
of_write_trailer
int of_write_trailer(OutputFile *of)
Definition: ffmpeg_mux.c:281
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
AVPacket::size
int size
Definition: packet.h:375
output_files
OutputFile ** output_files
Definition: ffmpeg.c:154
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1296
avio.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
printf
printf("static const uint8_t my_array[100] = {\n")
OSTFinished
OSTFinished
Definition: ffmpeg.h:450
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
av_sdp_create
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition: sdp.c:910
av_packet_make_refcounted
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:485
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:62
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
av_packet_rescale_ts
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another.
Definition: avpacket.c:526
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
of_check_init
int of_check_init(OutputFile *of)
Definition: ffmpeg_mux.c:230
avio_closep
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1288
av_write_trailer
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1250
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
packet.h
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:171
OutputFile::ost_index
int ost_index
Definition: ffmpeg.h:591
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:512
nb_output_files
int nb_output_files
Definition: ffmpeg.c:155
limit
static double limit(double x)
Definition: vf_pseudocolor.c:128
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1232
VSYNC_DROP
@ VSYNC_DROP
Definition: ffmpeg.h:56
sdp_filename
char * sdp_filename
Definition: ffmpeg_opt.c:153
avformat.h
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
av_get_media_type_string
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:28
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:956
OutputStream::finished
OSTFinished finished
Definition: ffmpeg.h:530
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:95
AVPacket::stream_index
int stream_index
Definition: packet.h:376
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
OutputFile::ctx
AVFormatContext * ctx
Definition: ffmpeg.h:589
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_interleaved_write_frame
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:1235
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
timestamp.h
OutputStream
Definition: muxing.c:54
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
close_all_output_streams
static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
Definition: ffmpeg_mux.c:35
nb_output_streams
int nb_output_streams
Definition: ffmpeg.c:153
OutputFile
Definition: ffmpeg.h:584
av_fifo_grow2
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition: fifo.c:99