]> git.sesse.net Git - vlc/blobdiff - modules/codec/x264.c
OSX GUI: store the interface pointer to avoid multiple accesses.
[vlc] / modules / codec / x264.c
index ae3247e07ac8c2245cdadd608211098c949b382d..7c9f6230aaae006f7aa95e06532a48706a1fe13c 100644 (file)
  *****************************************************************************/
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
+static void x264_log( void *, int i_level, const char *psz, va_list );
 
 /* Frame-type options */
 
 #define KEYINT_TEXT N_("Maximum GOP size")
 #define KEYINT_LONGTEXT N_( "Sets maximum interval between IDR-frames." \
     "Larger values save bits, thus improving quality for a given bitrate at " \
-    "the cost of seeking precision." )
+    "the cost of seeking precision. Use -1 for infinite." )
 
 #define MIN_KEYINT_TEXT N_("Minimum GOP size")
 #define MIN_KEYINT_LONGTEXT N_( "Sets minimum interval between IDR-frames. " \
@@ -374,7 +375,6 @@ static void Close( vlc_object_t * );
     "the actual encoding quality." )
 
 #define QUIET_TEXT N_("Quiet mode")
-#define QUIET_LONGTEXT N_( "Quiet mode.")
 
 #define VERBOSE_TEXT N_("Statistics")
 #define VERBOSE_LONGTEXT N_( "Print stats for each frame.")
@@ -676,7 +676,7 @@ vlc_module_begin ()
               SSIM_LONGTEXT, true )
 
     add_bool( SOUT_CFG_PREFIX "quiet", false, QUIET_TEXT,
-              QUIET_LONGTEXT, true )
+              QUIET_TEXT, true )
 
     add_integer( SOUT_CFG_PREFIX "sps-id", 0, SPS_ID_TEXT,
                  SPS_ID_LONGTEXT, true )
@@ -728,6 +728,7 @@ struct encoder_sys_t
 
     char            *psz_stat_name;
     int             i_sei_size;
+    uint32_t         i_colorspace;
     uint8_t         *p_sei;
 };
 
@@ -761,13 +762,38 @@ static int  Open ( vlc_object_t *p_this )
 
     p_enc->fmt_out.i_cat = VIDEO_ES;
     p_enc->fmt_out.i_codec = VLC_CODEC_H264;
+    p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
+
     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
+    p_sys->i_colorspace = X264_CSP_I420;
+#if X264_BUILD >= 118
+    char *psz_profile = var_GetString( p_enc, SOUT_CFG_PREFIX "profile" );
+    if( psz_profile )
+    {
+        const int mask = x264_bit_depth > 8 ? X264_CSP_HIGH_DEPTH : 0;
+        if( !strcmp( psz_profile, "high10" ) )
+        {
+            p_enc->fmt_in.i_codec = mask ? VLC_CODEC_I420_10L : VLC_CODEC_I420;
+            p_sys->i_colorspace = X264_CSP_I420 | mask;
+        }
+        if( !strcmp( psz_profile, "high422" ) )
+        {
+            p_enc->fmt_in.i_codec = mask ? VLC_CODEC_I422_10L : VLC_CODEC_I422;
+            p_sys->i_colorspace = X264_CSP_I422 | mask;
+        }
+        if( !strcmp( psz_profile, "high444" ) )
+        {
+            p_enc->fmt_in.i_codec = mask ? VLC_CODEC_I444_10L : VLC_CODEC_I444;
+            p_sys->i_colorspace = X264_CSP_I444 | mask;
+        }
+    }
+    free( psz_profile );
+#endif //X264_BUILD
 
     p_enc->pf_encode_video = Encode;
     p_enc->pf_encode_audio = NULL;
-    p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
-    if( !p_sys )
-        return VLC_ENOMEM;
     p_sys->i_initial_delay = 0;
     p_sys->psz_stat_name = NULL;
     p_sys->i_sei_size = 0;
@@ -784,6 +810,7 @@ static int  Open ( vlc_object_t *p_this )
     x264_param_default_preset( &p_sys->param, psz_preset, psz_tune );
     free( psz_preset );
     free( psz_tune );
+    p_sys->param.i_csp = p_sys->i_colorspace;
     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
 
@@ -845,13 +872,11 @@ static int  Open ( vlc_object_t *p_this )
                             SOUT_CFG_PREFIX "vbv-bufsize" );
 
     /* max bitrate = average bitrate -> CBR */
-    i_val = var_GetInteger( p_enc, SOUT_CFG_PREFIX "vbv-maxrate" );
+    p_sys->param.rc.i_vbv_max_bitrate = var_GetInteger( p_enc, SOUT_CFG_PREFIX "vbv-maxrate" );
 
-    if( !i_val && p_sys->param.rc.i_rc_method == X264_RC_ABR )
-        p_sys->param.rc.i_vbv_max_bitrate = p_sys->param.rc.i_bitrate;
-    else if ( i_val )
-        p_sys->param.rc.i_vbv_max_bitrate = i_val;
 
+    if( !var_GetBool( p_enc, SOUT_CFG_PREFIX "mbtree" ) )
+       p_sys->param.rc.b_mb_tree = var_GetBool( p_enc, SOUT_CFG_PREFIX "mbtree" );
 
     if( !var_GetBool( p_enc, SOUT_CFG_PREFIX "cabac" ) )
         p_sys->param.b_cabac = var_GetBool( p_enc, SOUT_CFG_PREFIX "cabac" );
@@ -925,6 +950,9 @@ static int  Open ( vlc_object_t *p_this )
 
     i_val = var_GetInteger( p_enc, SOUT_CFG_PREFIX "keyint" );
     if( i_val > 0 && i_val != 250 ) p_sys->param.i_keyint_max = i_val;
+#if X264_BUILD >= 102
+    if( i_val == -1 ) p_sys->param.i_keyint_max = X264_KEYINT_MAX_INFINITE;
+#endif
 
     i_val = var_GetInteger( p_enc, SOUT_CFG_PREFIX "min-keyint" );
     if( i_val > 0 && i_val != 25 ) p_sys->param.i_keyint_min = i_val;
@@ -1160,42 +1188,11 @@ static int  Open ( vlc_object_t *p_this )
     if( i_val > 0 )
         p_sys->param.i_slice_max_mbs = i_val;
 
-
-    /* x264 vbv-bufsize = 0 (default). if not provided set period
-       in seconds for local maximum bitrate (cache/bufsize) based
-       on average bitrate when use has told bitrate.
-       vbv-buffer size is set to bitrate * secods between keyframes */
-    if( !p_sys->param.rc.i_vbv_buffer_size &&
-         p_sys->param.rc.i_rc_method == X264_RC_ABR &&
-         p_sys->param.i_fps_num )
-    {
-        p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate *
-            p_sys->param.i_fps_den;
-        p_sys->param.rc.i_vbv_buffer_size *= p_sys->param.i_keyint_max;
-        p_sys->param.rc.i_vbv_buffer_size /= p_sys->param.i_fps_num;
-    }
-
     /* Check if user has given some profile (baseline,main,high) to limit
      * settings, and apply those*/
     psz_val = var_GetString( p_enc, SOUT_CFG_PREFIX "profile" );
     if( psz_val )
-    {
-        if( !strcasecmp( psz_val, "baseline" ) )
-        {
-            msg_Dbg( p_enc, "Limiting to baseline profile");
-            p_sys->param.analyse.b_transform_8x8 = 0;
-            p_sys->param.b_cabac = 0;
-            p_sys->param.i_bframe = 0;
-            p_sys->param.analyse.i_weighted_pred = X264_WEIGHTP_NONE;
-            p_sys->param.i_bframe_pyramid = X264_B_PYRAMID_NONE;
-        }
-        else if (!strcasecmp( psz_val, "main" ) )
-        {
-            msg_Dbg( p_enc, "Limiting to main-profile");
-            p_sys->param.analyse.b_transform_8x8 = 0;
-        }
-        /* high profile don't restrict stuff*/
-    }
+        x264_param_apply_profile( &p_sys->param, psz_val );
     free( psz_val );
 
 
@@ -1239,9 +1236,8 @@ static int  Open ( vlc_object_t *p_this )
         p_sys->param.rc.b_stat_read = i_val & 2;
     }
 
-    if( !var_GetBool( p_enc, SOUT_CFG_PREFIX "mbtree" ) )
-       p_sys->param.rc.b_mb_tree = var_GetBool( p_enc, SOUT_CFG_PREFIX "mbtree" );
-
+    p_sys->param.pf_log = x264_log;
+    p_sys->param.p_log_private = p_enc;
     /* We need to initialize pthreadw32 before we open the encoder,
        but only once for the whole application. Since pthreadw32
        doesn't keep a refcount, do it ourselves. */
@@ -1322,6 +1318,33 @@ static int  Open ( vlc_object_t *p_this )
     return VLC_SUCCESS;
 }
 
+/****************************************************************************
+ * Logging
+ ****************************************************************************/
+static void x264_log( void *data, int i_level, const char *psz, va_list args)
+{
+    encoder_t *p_enc = (encoder_t *)data;
+
+    switch( i_level )
+    {
+        case X264_LOG_ERROR:
+            i_level = VLC_MSG_ERR;
+            break;
+        case X264_LOG_WARNING:
+            i_level = VLC_MSG_WARN;
+            break;
+        case X264_LOG_INFO:
+            i_level = VLC_MSG_INFO;
+            break;
+        case X264_LOG_DEBUG:
+            i_level = VLC_MSG_DBG;
+        default:
+            i_level = VLC_MSG_DBG;
+    }
+
+    msg_GenericVa( p_enc, i_level, MODULE_STRING, psz, args );
+};
+
 /****************************************************************************
  * Encode:
  ****************************************************************************/
@@ -1341,13 +1364,7 @@ static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
 #endif
     if( likely(p_pict) ) {
        pic.i_pts = p_pict->date;
-       /* scale pts starting from 0 as libx264 seems to return dts values
-          assume that
-        */
-       if( unlikely( p_sys->i_initial_delay == 0 ) )
-           p_sys->i_initial_delay = p_pict->date;
-       pic.i_pts -= p_sys->i_initial_delay;
-       pic.img.i_csp = X264_CSP_I420;
+       pic.img.i_csp = p_sys->i_colorspace;
        pic.img.i_plane = p_pict->i_planes;
        for( i = 0; i < p_pict->i_planes; i++ )
        {
@@ -1366,28 +1383,24 @@ static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
 
 
     /* Get size of block we need */
-    i_out = p_sys->i_sei_size;
     for( i = 0; i < i_nal; i++ )
         i_out += nal[i].i_payload;
 
-    p_block = block_New( p_enc, i_out );
+    p_block = block_New( p_enc, i_out + p_sys->i_sei_size );
     if( !p_block ) return NULL;
 
-    /* copy encoded data directly to block */
-    for( i = 0, i_out = 0; i < i_nal; i++ )
+    unsigned int i_offset = 0;
+    if( unlikely( p_sys->i_sei_size ) )
     {
-        if( p_sys->i_sei_size && nal[i].i_type == NAL_SLICE )
-        {
-            /* insert x264 headers SEI nal before first SLICE nal */
-            memcpy( p_block->p_buffer, p_sys->p_sei, p_sys->i_sei_size );
-            i_out += p_sys->i_sei_size;
-            p_sys->i_sei_size = 0;
-            free( p_sys->p_sei );
-            p_sys->p_sei = NULL;
-        }
-        memcpy( p_block->p_buffer + i_out, nal[i].p_payload, nal[i].i_payload );
-        i_out += nal[i].i_payload;
+       /* insert x264 headers SEI nal into the first picture block at the start */
+       memcpy( p_block->p_buffer, p_sys->p_sei, p_sys->i_sei_size );
+       i_offset = p_sys->i_sei_size;
+       p_sys->i_sei_size = 0;
+       free( p_sys->p_sei );
+       p_sys->p_sei = NULL;
     }
+    /* copy encoded data directly to block */
+    memcpy( p_block->p_buffer + i_offset, nal[0].p_payload, i_out );
 
     if( pic.b_keyframe )
         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
@@ -1404,8 +1417,8 @@ static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
             p_enc->fmt_in.video.i_frame_rate;
 
     /* scale pts-values back*/
-    p_block->i_pts = pic.i_pts + p_sys->i_initial_delay;
-    p_block->i_dts = pic.i_dts + p_sys->i_initial_delay;
+    p_block->i_pts = pic.i_pts;
+    p_block->i_dts = pic.i_dts;
 
     return p_block;
 }