[FFmpeg-devel] [PATCH 3/3] ffmpeg: try to guess a good value for the thread message queue.

Nicolas George george at nsup.org
Mon Feb 16 22:22:17 CET 2015


The current value, 8, is too low to be really useful.
The heuristic tries to allocate enough room for 10 seconds.

Signed-off-by: Nicolas George <george at nsup.org>
---
 ffmpeg.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)


On my hardware, recording from v4l2 and ALSA produces ALSA underruns if the
message queue is below ~256-512.


diff --git a/ffmpeg.c b/ffmpeg.c
index a52de20..990ce9e 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3412,9 +3412,29 @@ static void free_input_threads(void)
     }
 }
 
+static int eval_thread_queue_size(InputFile *f)
+{
+    int i, s;
+    int64_t sec = 10, fps = 0;
+
+    for (i = 0; i < f->ctx->nb_streams; i++) {
+        AVStream *st = f->ctx->streams[i];
+        AVCodecContext *c = st->codec;
+        if (c->sample_rate && c->frame_size) {
+            fps += sec * (c->sample_rate / c->frame_size + 1);
+        } else if (st->avg_frame_rate.num && st->avg_frame_rate.den) {
+            fps += 1 + av_rescale(sec, st->avg_frame_rate.num, st->avg_frame_rate.den);
+        } else {
+            fps += 25 * sec;
+        }
+    }
+    s = FFMAX(3, FFMIN(20, av_log2(fps) + 1));
+    return 1 << s;
+}
+
 static int init_input_threads(void)
 {
-    int i, ret;
+    int i, ret, qs;
 
     if (nb_input_files == 1)
         return 0;
@@ -3425,8 +3445,10 @@ static int init_input_threads(void)
         if (f->ctx->pb ? !f->ctx->pb->seekable :
             strcmp(f->ctx->iformat->name, "lavfi"))
             f->non_blocking = 1;
+        qs = eval_thread_queue_size(f);
+        av_log(f->ctx, AV_LOG_VERBOSE, "Using input thread with queue size %d\n", qs);
         ret = av_thread_message_queue_alloc(&f->in_thread_queue,
-                                            8, sizeof(AVPacket));
+                                            qs, sizeof(AVPacket));
         if (ret < 0)
             return ret;
 
-- 
2.1.4



More information about the ffmpeg-devel mailing list