]> git.sesse.net Git - vlc/blobdiff - modules/codec/x264.c
new (failing) test for libvlc
[vlc] / modules / codec / x264.c
index 5fd15961a234a9298cbbb18341258003ea20c3c0..4333c40ad96af562f2f8e961e0e792f9da697303 100644 (file)
@@ -33,6 +33,9 @@
 #include <vlc_sout.h>
 #include <vlc_codec.h>
 
+#ifdef PTW32_STATIC_LIB
+#include <pthread.h>
+#endif
 #include <x264.h>
 
 #define SOUT_CFG_PREFIX "sout-x264-"
@@ -216,11 +219,20 @@ static void Close( vlc_object_t * );
 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
 
 #define ME_TEXT N_("Integer pixel motion estimation method")
+#if X264_BUILD >= 58 /* r728 */
+#define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
+    " - dia: diamond search, radius 1 (fast)\n" \
+    " - hex: hexagonal search, radius 2\n" \
+    " - umh: uneven multi-hexagon search (better but slower)\n" \
+    " - esa: exhaustive search (extremely slow, primarily for testing)\n" \
+    " - tesa: hadamard exhaustive search (extremely slow, primarily for testing)\n" )
+#else
 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
     " - dia: diamond search, radius 1 (fast)\n" \
     " - hex: hexagonal search, radius 2\n" \
     " - umh: uneven multi-hexagon search (better but slower)\n" \
     " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
+#endif
 
 #if X264_BUILD >= 24
 #define MERANGE_TEXT N_("Maximum motion vector search range")
@@ -345,13 +357,20 @@ static void Close( vlc_object_t * );
 #define AUD_TEXT N_("Access unit delimiters")
 #define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")
 
-#if X264_BUILD >= 24
+#if X264_BUILD >= 24 && X264_BUILD < 58
 static const char *enc_me_list[] =
   { "dia", "hex", "umh", "esa" };
 static const char *enc_me_list_text[] =
   { N_("dia"), N_("hex"), N_("umh"), N_("esa") };
 #endif
 
+#if X264_BUILD >= 58 /* r728 */
+static const char *enc_me_list[] =
+  { "dia", "hex", "umh", "esa", "tesa" };
+static const char *enc_me_list_text[] =
+  { N_("dia"), N_("hex"), N_("umh"), N_("esa"), N_("tesa") };
+#endif
+
 static const char *enc_analyse_list[] =
   { "none", "fast", "normal", "slow", "all" };
 static const char *enc_analyse_list_text[] =
@@ -515,7 +534,7 @@ vlc_module_begin();
         change_string_list( direct_pred_list, direct_pred_list_text, 0 );
 
 #if X264_BUILD >= 52 /* r573 */
-    add_integer( SOUT_CFG_PREFIX "direct-8x8", 0, NULL, DIRECT_PRED_SIZE_TEXT,
+    add_integer( SOUT_CFG_PREFIX "direct-8x8", -1, NULL, DIRECT_PRED_SIZE_TEXT,
                  DIRECT_PRED_SIZE_LONGTEXT, VLC_FALSE );
         change_integer_range( -1, 1 );
 #endif
@@ -696,6 +715,9 @@ static int  Open ( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
+    /* X264_POINTVER or X264_VERSION are not available */
+    msg_Dbg ( p_enc, "version x264 0.%d.X", X264_BUILD );
+
 #if X264_BUILD < 37
     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
         p_enc->fmt_in.video.i_height % 16 != 0 )
@@ -749,9 +771,17 @@ static int  Open ( vlc_object_t *p_this )
     var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
     if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
     var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
-    if( val.i_int >= 0 && val.i_int <= 51 ) i_qmin = val.i_int;
+    if( val.i_int >= 0 && val.i_int <= 51 )
+    {
+        i_qmin = val.i_int;
+        p_sys->param.rc.i_qp_min = i_qmin;
+    }
     var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
-    if( val.i_int >= 0 && val.i_int <= 51 ) i_qmax = val.i_int;
+    if( val.i_int >= 0 && val.i_int <= 51 )
+    {
+        i_qmax = val.i_int;
+        p_sys->param.rc.i_qp_max = i_qmax;
+    }
 
     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
     if( val.i_int >= 0 && val.i_int <= 51 )
@@ -759,6 +789,7 @@ static int  Open ( vlc_object_t *p_this )
         if( i_qmin > val.i_int ) i_qmin = val.i_int;
         if( i_qmax < val.i_int ) i_qmax = val.i_int;
 
+        p_sys->param.rc.i_rc_method = X264_RC_CQP;
 #if X264_BUILD >= 0x000a
         p_sys->param.rc.i_qp_constant = val.i_int;
         p_sys->param.rc.i_qp_min = i_qmin;
@@ -917,7 +948,13 @@ static int  Open ( vlc_object_t *p_this )
     {
         p_sys->param.analyse.i_me_method = X264_ME_ESA;
     }
-    if( val.psz_string ) free( val.psz_string );
+    #if X264_BUILD >= 58 /* r728 */
+        else if( !strcmp( val.psz_string, "tesa" ) )
+        {
+            p_sys->param.analyse.i_me_method = X264_ME_TESA;
+        }
+    #endif
+    free( val.psz_string );
 
     var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
     if( val.i_int >= 0 && val.i_int <= 64 )
@@ -951,7 +988,7 @@ static int  Open ( vlc_object_t *p_this )
         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
     }
 #endif
-    if( val.psz_string ) free( val.psz_string );
+    free( val.psz_string );
 
     var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
     p_sys->param.analyse.b_psnr = val.b_bool;
@@ -1093,7 +1130,7 @@ static int  Open ( vlc_object_t *p_this )
         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
 #endif
     }
-    if( val.psz_string ) free( val.psz_string );
+    free( val.psz_string );
 
 #if X264_BUILD >= 30
     var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
@@ -1137,12 +1174,13 @@ static int  Open ( vlc_object_t *p_this )
         p_sys->param.cpu &= ~X264_CPU_SSE2;
     }
 
+    /* BUILD 29 adds support for multi-threaded encoding while BUILD 49 (r543)
+       also adds support for threads = 0 for automatically selecting an optimal
+       value (cores * 1.5) based on detected CPUs. Default behavior for x264 is
+       threads = 1, however VLC usage differs and uses threads = 0 (auto) by
+       default unless ofcourse transcode threads is explicitly specified.. */
 #if X264_BUILD >= 29
-    /* As of r543 x264 will autodetect the number of cpus and will set
-       the number of threads accordingly unless ofcourse the number of
-       threads is explicitly specified... */
-    if( p_enc->i_threads >= 1 )
-        p_sys->param.i_threads = p_enc->i_threads;
+    p_sys->param.i_threads = p_enc->i_threads;
 #endif
 
     var_Get( p_enc, SOUT_CFG_PREFIX "stats", &val );
@@ -1160,6 +1198,36 @@ static int  Open ( vlc_object_t *p_this )
         p_sys->param.rc.b_stat_read = val.i_int & 2;
     }
 
+    /* We need to initialize pthreadw32 before we open the encoder,
+       but only oncce for the whole application. Since pthreadw32
+       doesn't keep a refcount, do it ourselves. */
+#ifdef PTW32_STATIC_LIB
+    vlc_value_t lock, count;
+
+    var_Create( p_enc->p_libvlc, "pthread_win32_mutex", VLC_VAR_MUTEX );
+    var_Get( p_enc->p_libvlc, "pthread_win32_mutex", &lock );
+    vlc_mutex_lock( lock.p_address );
+
+    var_Create( p_enc->p_libvlc, "pthread_win32_count", VLC_VAR_INTEGER );
+    var_Get( p_enc->p_libvlc, "pthread_win32_count", &count );
+
+    if( count.i_int == 0 )
+    {   
+        msg_Dbg( p_enc, "initializing pthread-win32" );
+        if( !pthread_win32_process_attach_np() || !pthread_win32_thread_attach_np() )   
+        {   
+            msg_Warn( p_enc, "pthread Win32 Initialization failed" );
+            vlc_mutex_unlock( lock.p_address );
+            return VLC_EGENERIC;
+        }
+    }
+
+    count.i_int++;
+    var_Set( p_enc->p_libvlc, "pthread_win32_count", count );
+    vlc_mutex_unlock( lock.p_address );
+
+#endif
+
     /* Open the encoder */
     p_sys->h = x264_encoder_open( &p_sys->param );
 
@@ -1181,8 +1249,8 @@ static int  Open ( vlc_object_t *p_this )
 
         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
 
-        memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
-                p_sys->p_buffer, i_size );
+        memcpy( (uint8_t*)p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
+            p_sys->p_buffer, i_size );
 
         p_enc->fmt_out.i_extra += i_size;
     }
@@ -1283,11 +1351,32 @@ static void Close( vlc_object_t *p_this )
 {
     encoder_t     *p_enc = (encoder_t *)p_this;
     encoder_sys_t *p_sys = p_enc->p_sys;
-    if( p_sys->psz_stat_name )
-        free( p_sys->psz_stat_name );
+
+    free( p_sys->psz_stat_name );
 
     x264_encoder_close( p_sys->h );
+
+#ifdef PTW32_STATIC_LIB
+    vlc_value_t lock, count;
+
+    var_Create( p_enc->p_libvlc, "pthread_win32_mutex", VLC_VAR_MUTEX );
+    var_Get( p_enc->p_libvlc, "pthread_win32_mutex", &lock );
+    vlc_mutex_lock( lock.p_address );
+
+    var_Create( p_enc->p_libvlc, "pthread_win32_count", VLC_VAR_INTEGER );
+    var_Get( p_enc->p_libvlc, "pthread_win32_count", &count );
+    count.i_int--;
+    var_Set( p_enc->p_libvlc, "pthread_win32_count", count );
+
+    if( count.i_int == 0 )
+    {
+        pthread_win32_thread_detach_np();
+        pthread_win32_process_detach_np();
+        msg_Dbg( p_enc, "pthread-win32 deinitialized" );
+    }
+    vlc_mutex_unlock( lock.p_address );
+#endif
+
     free( p_sys->p_buffer );
     free( p_sys );
 }