[FFmpeg-devel] [PATCH] avcodec/libx264: Implement reference frame count limiting based on level

Michael Niedermayer michaelni at gmx.at
Sat Apr 12 04:36:16 CEST 2014


This makes libavcodec/libx264.c behave more similar to the x264 command line
util

Fixes Ticket3307

Implementation based on x264

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavcodec/libx264.c |   32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index d9130b2..04f09fb 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -82,6 +82,29 @@ typedef struct X264Context {
     char *x264_params;
 } X264Context;
 
+static const struct {
+    const char level[4];
+    int dpb;
+} level_dpb[] = {
+    { "1b" ,    396},
+    { "1.0",    396},
+    { "1.1",    900},
+    { "1.2",   2376},
+    { "1.3",   2376},
+    { "2.0",   2376},
+    { "2.1",   4752},
+    { "2.2",   8100},
+    { "3.0",   8100},
+    { "3.1",  18000},
+    { "3.2",  20480},
+    { "4.0",  32768},
+    { "4.1",  32768},
+    { "4.2",  34816},
+    { "5.0", 110400},
+    { "5.1", 184320},
+    { "5.2", 184320},
+};
+
 static void X264_log(void *p, int level, const char *fmt, va_list args)
 {
     static const int level_map[] = {
@@ -426,6 +449,15 @@ static av_cold int X264_init(AVCodecContext *avctx)
         x4->params.rc.f_qcompress       = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
     if (avctx->refs >= 0)
         x4->params.i_frame_reference    = avctx->refs;
+    else if (x4->level) {
+        int i;
+        int mbn = FF_CEIL_RSHIFT(avctx->width, 4) * FF_CEIL_RSHIFT(avctx->height, 4);
+
+        for (i = 0; i<FF_ARRAY_ELEMS(level_dpb); i++)
+            if (!strcmp(level_dpb[i].level, x4->level))
+                x4->params.i_frame_reference = av_clip(level_dpb[i].dpb / mbn, 1, x4->params.i_frame_reference);
+    }
+
     if (avctx->trellis >= 0)
         x4->params.analyse.i_trellis    = avctx->trellis;
     if (avctx->me_range >= 0)
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list