[FFmpeg-devel] [PATCH 21/25] avcodec/v4l2m2m: clean up buffer options and pick sane defaults

Aman Gupta ffmpeg at tmm1.net
Tue Sep 3 04:02:26 EEST 2019


From: Aman Gupta <aman at tmm1.net>

Previously the default values for output/capture buffers were
quite high, causing a lot of memory usage on devices with limited
resources. On a default RPI3 installation, it is easy to run out of
kernel CMA memory without additional kernel boot flags.

Similar patches are being used in the LibreELEC ffmpeg fork for
AMLogic support.

I also reduced the minimum value for the buffering options so that users
can further reduce the number of buffers if required on their specific
platform.

Prior to this change, setting up a decode+scale+encode pipeline on
the RPI would fail with ENOMEM due to a total of 76 buffers being
pre-allocated.

Signed-off-by: Aman Gupta <aman at tmm1.net>
---
 libavcodec/v4l2_m2m.h          | 6 ++++--
 libavcodec/v4l2_m2m_dec.c      | 4 +---
 libavcodec/v4l2_m2m_enc.c      | 4 +---
 libavfilter/vf_scale_v4l2m2m.c | 6 +-----
 4 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h
index b94d724a93..f60773d227 100644
--- a/libavcodec/v4l2_m2m.h
+++ b/libavcodec/v4l2_m2m.h
@@ -37,9 +37,11 @@
         const __typeof__(((type *)0)->member ) *__mptr = (ptr); \
         (type *)((char *)__mptr - offsetof(type,member) );})
 
-#define V4L_M2M_DEFAULT_OPTS \
+#define V4L_M2M_DEFAULT_OPTS(output_bufs, capture_bufs) \
     { "num_output_buffers", "Number of buffers in the output context",\
-        OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 6, INT_MAX, FLAGS }
+        OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = output_bufs },  1, INT_MAX, FLAGS }, \
+    { "num_capture_buffers", "Number of buffers in the capture context", \
+        OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = capture_bufs }, 1, INT_MAX, FLAGS }
 
 typedef struct V4L2m2mContext {
     char devname[PATH_MAX];
diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
index 71b2211594..36c2363eee 100644
--- a/libavcodec/v4l2_m2m_dec.c
+++ b/libavcodec/v4l2_m2m_dec.c
@@ -281,9 +281,7 @@ static void v4l2_flush(AVCodecContext *avctx)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 
 static const AVOption options[] = {
-    V4L_M2M_DEFAULT_OPTS,
-    { "num_capture_buffers", "Number of buffers in the capture context",
-        OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
+    V4L_M2M_DEFAULT_OPTS(8, 6),
     { NULL},
 };
 
diff --git a/libavcodec/v4l2_m2m_enc.c b/libavcodec/v4l2_m2m_enc.c
index 4ccfe77322..ca433ac1ea 100644
--- a/libavcodec/v4l2_m2m_enc.c
+++ b/libavcodec/v4l2_m2m_enc.c
@@ -342,9 +342,7 @@ static av_cold int v4l2_encode_close(AVCodecContext *avctx)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 
 static const AVOption options[] = {
-    V4L_M2M_DEFAULT_OPTS,
-    { "num_capture_buffers", "Number of buffers in the capture context",
-        OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 4 }, 4, INT_MAX, FLAGS },
+    V4L_M2M_DEFAULT_OPTS(6, 8),
     { NULL },
 };
 
diff --git a/libavfilter/vf_scale_v4l2m2m.c b/libavfilter/vf_scale_v4l2m2m.c
index a5ffa9953e..03d9f6406c 100644
--- a/libavfilter/vf_scale_v4l2m2m.c
+++ b/libavfilter/vf_scale_v4l2m2m.c
@@ -301,11 +301,7 @@ static const AVOption scale_v4l2m2m_options[] = {
 
 #undef OFFSET
 #define OFFSET(x) offsetof(V4L2m2mPriv, x)
-
-    V4L_M2M_DEFAULT_OPTS,
-    { "num_capture_buffers", "Number of buffers in the capture context",
-        OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 4 }, 4, INT_MAX, FLAGS },
-
+    V4L_M2M_DEFAULT_OPTS(6, 6),
     { NULL },
 };
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list