]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libx264.c
libx264: add 'crf' private option.
[ffmpeg] / libavcodec / libx264.c
index bcf8b1f688370b6b77164c441b27c6c566003dbf..bd7955a2a37ed4a3ee0c50f4ebdf44ab018b2475 100644 (file)
@@ -21,7 +21,9 @@
 
 #include "libavutil/opt.h"
 #include "avcodec.h"
+#include "internal.h"
 #include <x264.h>
+#include <float.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,6 +41,7 @@ typedef struct X264Context {
     char *tune;
     char *profile;
     int fastfirstpass;
+    float crf;
 } X264Context;
 
 static void X264_log(void *p, int level, const char *fmt, va_list args)
@@ -263,27 +266,33 @@ static av_cold int X264_init(AVCodecContext *avctx)
     x4->params.i_log_level          = X264_LOG_DEBUG;
 
     x4->params.b_intra_refresh      = avctx->flags2 & CODEC_FLAG2_INTRA_REFRESH;
-    x4->params.rc.i_bitrate         = avctx->bit_rate       / 1000;
+    if (avctx->bit_rate) {
+        x4->params.rc.i_bitrate   = avctx->bit_rate / 1000;
+        x4->params.rc.i_rc_method = X264_RC_ABR;
+    }
     x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
     x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
     x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
     if (avctx->flags & CODEC_FLAG_PASS2) {
         x4->params.rc.b_stat_read = 1;
     } else {
+#if FF_API_X264_GLOBAL_OPTS
         if (avctx->crf) {
             x4->params.rc.i_rc_method   = X264_RC_CRF;
             x4->params.rc.f_rf_constant = avctx->crf;
             x4->params.rc.f_rf_constant_max = avctx->crf_max;
-        } else if (avctx->cqp > -1) {
+        } else
+#endif
+               if (avctx->cqp > -1) {
             x4->params.rc.i_rc_method   = X264_RC_CQP;
             x4->params.rc.i_qp_constant = avctx->cqp;
         }
-    }
 
-    // if neither crf nor cqp modes are selected we have to enable the RC
-    // we do it this way because we cannot check if the bitrate has been set
-    if (!(avctx->crf || (avctx->cqp > -1)))
-        x4->params.rc.i_rc_method = X264_RC_ABR;
+        if (x4->crf >= 0) {
+            x4->params.rc.i_rc_method   = X264_RC_CRF;
+            x4->params.rc.f_rf_constant = x4->crf;
+        }
+    }
 
     if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
         (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
@@ -362,10 +371,11 @@ static av_cold int X264_init(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(X264Context, x)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
-    { "preset",        "Set the encoding preset",                    OFFSET(preset),        FF_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
-    { "tune",          "Tune the encoding params",                   OFFSET(tune),          FF_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
-    { "profile",       "Set profile restrictions",                   OFFSET(profile),       FF_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
-    { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), FF_OPT_TYPE_INT,    { 1 }, 0, 1, VE},
+    { "preset",        "Set the encoding preset (cf. x264 --fullhelp)",   OFFSET(preset),        FF_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
+    { "tune",          "Tune the encoding params (cf. x264 --fullhelp)",  OFFSET(tune),          FF_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
+    { "profile",       "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile),       FF_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
+    { "fastfirstpass", "Use fast settings when encoding first pass",      OFFSET(fastfirstpass), FF_OPT_TYPE_INT,    { 1 }, 0, 1, VE},
+    { "crf",           "Select the quality for constant quality mode",    OFFSET(crf),           FF_OPT_TYPE_FLOAT,  {-1 }, -1, FLT_MAX, VE },
     { NULL },
 };
 
@@ -376,6 +386,11 @@ static const AVClass class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
+static const AVCodecDefault x264_defaults[] = {
+    { "b",                "0" },
+    { NULL },
+};
+
 AVCodec ff_libx264_encoder = {
     .name           = "libx264",
     .type           = AVMEDIA_TYPE_VIDEO,
@@ -388,4 +403,5 @@ AVCodec ff_libx264_encoder = {
     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_YUVJ420P, PIX_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
     .priv_class     = &class,
+    .defaults       = x264_defaults,
 };