summaryrefslogtreecommitdiff
path: root/fftools/ffmpeg.c
diff options
context:
space:
mode:
authorJan Ekström <jeebjp@gmail.com>2020-10-15 21:21:05 +0300
committerJan Ekström <jeebjp@gmail.com>2020-10-29 16:59:48 +0200
commit453b2f3c154f6b83221940ad411599ded91f7413 (patch)
tree5b718860b89a427fa1ad5680bf9c273f577672d6 /fftools/ffmpeg.c
parent9b45c6d74b6bcc21c3246580db3392d15a6988b8 (diff)
ffmpeg: add a data size threshold for muxing queue size
This way the old max queue size limit based behavior for streams where each individual packet is large is kept, while for smaller streams more packets can be buffered (current default is at 50 megabytes per stream). For some explanation, by default ffmpeg copies packets from before the appointed seek point/start time and puts them into the local muxing queue. Before, it getting utilized was much less likely since as soon as the filter chain was initialized, the encoder (and thus output stream) was also initialized. Now, since we will be pushing the encoder initialization to when the first AVFrame is decoded and filtered - which only happens after the exact seek point is hit as packets are ignored until then - this queue will be seeing much more usage. In more layman's terms, this attempts to fix cases such as where: - seek point ends up being 5 seconds before requested time. - audio is set to copy, and thus immediately begins filling the muxing queue. - video is being encoded, and thus all received packets are skipped until the requested time is hit.
Diffstat (limited to 'fftools/ffmpeg.c')
-rw-r--r--fftools/ffmpeg.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index cb7644de6a..3af189e7f2 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -725,8 +725,13 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u
AVPacket tmp_pkt = {0};
/* the muxer is not initialized yet, buffer the packet */
if (!av_fifo_space(ost->muxing_queue)) {
- int new_size = FFMIN(2 * av_fifo_size(ost->muxing_queue),
- ost->max_muxing_queue_size);
+ unsigned int are_we_over_size =
+ (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
+ int new_size = are_we_over_size ?
+ FFMIN(2 * av_fifo_size(ost->muxing_queue),
+ ost->max_muxing_queue_size) :
+ 2 * av_fifo_size(ost->muxing_queue);
+
if (new_size <= av_fifo_size(ost->muxing_queue)) {
av_log(NULL, AV_LOG_ERROR,
"Too many packets buffered for output stream %d:%d.\n",
@@ -741,6 +746,7 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u
if (ret < 0)
exit_program(1);
av_packet_move_ref(&tmp_pkt, pkt);
+ ost->muxing_queue_data_size += tmp_pkt.size;
av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
return;
}
@@ -2991,6 +2997,7 @@ static int check_init_output_file(OutputFile *of, int file_index)
while (av_fifo_size(ost->muxing_queue)) {
AVPacket pkt;
av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
+ ost->muxing_queue_data_size -= pkt.size;
write_packet(of, &pkt, ost, 1);
}
}