OpenCL support improvement/refactoring
[x262.git] / encoder / encoder.c
index d091ff5..32e23e6 100644 (file)
 #include "common/visualize.h"
 #endif
 
-#if HAVE_OPENCL
-#include "common/opencl.h"
-#endif
-
 //#define DEBUG_MB_TYPE
 
 #define bs_write_ue bs_write_ue_big
@@ -1391,6 +1387,18 @@ x264_t *x264_encoder_open( x264_param_t *param )
         x264_threadpool_init( &h->lookaheadpool, h->param.i_lookahead_threads, (void*)x264_lookahead_thread_init, h ) )
         goto fail;
 
+#if HAVE_OPENCL
+    if( h->param.b_opencl )
+    {
+        h->opencl.ocl = x264_opencl_load_library();
+        if( !h->opencl.ocl )
+        {
+            x264_log( h, X264_LOG_WARNING, "failed to load OpenCL\n" );
+            h->param.b_opencl = 0;
+        }
+    }
+#endif
+
     h->thread[0] = h;
     for( int i = 1; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ )
         CHECKED_MALLOC( h->thread[i], sizeof(x264_t) );
@@ -1432,7 +1440,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
     }
 
 #if HAVE_OPENCL
-    if( h->param.b_opencl && x264_opencl_init( h ) < 0 )
+    if( h->param.b_opencl && x264_opencl_lookahead_init( h ) < 0 )
         h->param.b_opencl = 0;
 #endif
 
@@ -3649,12 +3657,13 @@ void    x264_encoder_close  ( x264_t *h )
                    || h->stat.i_mb_count[SLICE_TYPE_P][I_PCM]
                    || h->stat.i_mb_count[SLICE_TYPE_B][I_PCM];
 
+    x264_lookahead_delete( h );
+
 #if HAVE_OPENCL
-    x264_opencl_free( h );
+    x264_opencl_lookahead_delete( h );
+    x264_opencl_function_t *ocl = h->opencl.ocl;
 #endif
 
-    x264_lookahead_delete( h );
-
     if( h->param.b_sliced_threads )
         x264_threadpool_wait_all( h );
     if( h->param.i_threads > 1 )
@@ -4004,6 +4013,9 @@ void    x264_encoder_close  ( x264_t *h )
         x264_pthread_cond_destroy( &h->thread[i]->cv );
         x264_free( h->thread[i] );
     }
+#if HAVE_OPENCL
+    x264_opencl_close_library( ocl );
+#endif
 }
 
 int x264_encoder_delayed_frames( x264_t *h )