OSDN Git Service

fix a threading issue with avcodec_open/close
authorjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 12 Dec 2008 18:54:36 +0000 (18:54 +0000)
committerjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 12 Dec 2008 18:54:36 +0000 (18:54 +0000)
these functions can not be called from 2 threads simultaneosly.
made a wrapper function that holds a lock while making the call

git-svn-id: svn://localhost/HandBrake/trunk@2023 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/decavcodec.c
libhb/decomb.c
libhb/deinterlace.c
libhb/encavcodec.c
libhb/hb.c
libhb/stream.c
libhb/sync.c

index d443b4a..12c9802 100644 (file)
@@ -197,7 +197,7 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
     pv->parser = av_parser_init( codec_id );
 
     pv->context = avcodec_alloc_context();
-    avcodec_open( pv->context, codec );
+    hb_avcodec_open( pv->context, codec );
 
     return 0;
 }
@@ -229,7 +229,7 @@ static void decavcodecClose( hb_work_object_t * w )
         }
         if ( pv->context && pv->context->codec )
         {
-            avcodec_close( pv->context );
+            hb_avcodec_close( pv->context );
         }
         if ( pv->list )
         {
@@ -798,9 +798,9 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         // There's a mis-feature in ffmpeg that causes the context to be 
         // incorrectly initialized the 1st time avcodec_open is called.
         // If you close it and open a 2nd time, it finishes the job.
-        avcodec_open( pv->context, codec );
-        avcodec_close( pv->context );
-        avcodec_open( pv->context, codec );
+        hb_avcodec_open( pv->context, codec );
+        hb_avcodec_close( pv->context );
+        hb_avcodec_open( pv->context, codec );
     }
 
     if( in->start >= 0 )
@@ -902,7 +902,7 @@ static void init_ffmpeg_context( hb_work_object_t *w )
     if ( ! pv->context->codec )
     {
         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
-        avcodec_open( pv->context, codec );
+        hb_avcodec_open( pv->context, codec );
     }
     // set up our best guess at the frame duration.
     // the frame rate in the codec is usually bogus but it's sometimes
index 2194f56..4fb5730 100644 (file)
@@ -1309,7 +1309,7 @@ hb_filter_private_t * hb_decomb_init( int pix_fmt,
                     avctx_enc->flags |= CODEC_FLAG_QPEL;
             }
 
-            avcodec_open(avctx_enc, enc);
+            hb_avcodec_open(avctx_enc, enc);
         }
 
         pv->mcdeint_frame       = avcodec_alloc_frame();
@@ -1413,7 +1413,7 @@ void hb_decomb_close( hb_filter_private_t * pv )
     {
         if( pv->mcdeint_avctx_enc )
         {
-            avcodec_close( pv->mcdeint_avctx_enc );
+            hb_avcodec_close( pv->mcdeint_avctx_enc );
             av_freep( &pv->mcdeint_avctx_enc );
         }
         if( pv->mcdeint_outbuf )
index b6b10fe..b7d672a 100644 (file)
@@ -653,7 +653,7 @@ hb_filter_private_t * hb_deinterlace_init( int pix_fmt,
                     avctx_enc->flags |= CODEC_FLAG_QPEL;
             }
 
-            avcodec_open(avctx_enc, enc);
+            hb_avcodec_open(avctx_enc, enc);
         }
 
         pv->mcdeint_frame       = avcodec_alloc_frame();
@@ -726,7 +726,7 @@ void hb_deinterlace_close( hb_filter_private_t * pv )
     {
         if( pv->mcdeint_avctx_enc )
         {
-            avcodec_close( pv->mcdeint_avctx_enc );
+            hb_avcodec_close( pv->mcdeint_avctx_enc );
             av_freep( &pv->mcdeint_avctx_enc );
         }
         if( pv->mcdeint_outbuf )
index d22d30d..5cf8c50 100644 (file)
@@ -159,7 +159,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
         }
     }
 
-    if( avcodec_open( context, codec ) )
+    if( hb_avcodec_open( context, codec ) )
     {
         hb_log( "hb_work_encavcodec_init: avcodec_open failed" );
     }
@@ -194,7 +194,7 @@ void encavcodecClose( hb_work_object_t * w )
     {
         hb_deep_log( 2, "encavcodec: closing libavcodec" );
         avcodec_flush_buffers( pv->context );
-        avcodec_close( pv->context );
+        hb_avcodec_close( pv->context );
     }
     if( pv->file )
     {
index 31b45c2..72eb2b0 100644 (file)
@@ -44,10 +44,35 @@ struct hb_handle_s
 
 };
 
+hb_lock_t *hb_avcodec_lock;
 hb_work_object_t * hb_objects = NULL;
 
 static void thread_func( void * );
 
+void hb_avcodec_init()
+{
+    hb_avcodec_lock  = hb_lock_init();
+    av_register_all();
+}
+
+int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
+{
+    int ret;
+    hb_lock( hb_avcodec_lock );
+    ret = avcodec_open(avctx, codec);
+    hb_unlock( hb_avcodec_lock );
+    return ret;
+}
+
+int hb_avcodec_close(AVCodecContext *avctx)
+{
+    int ret;
+    hb_lock( hb_avcodec_lock );
+    ret = avcodec_close(avctx);
+    hb_unlock( hb_avcodec_lock );
+    return ret;
+}
+
 /**
  * Registers work objects, by adding the work object to a liked list.
  * @param w Handle to hb_work_object_t to register.
@@ -121,7 +146,7 @@ hb_handle_t * hb_init_real( int verbose, int update_check )
     h->pause_lock = hb_lock_init();
 
     /* libavcodec */
-    av_register_all();
+    hb_avcodec_init();
 
     /* Start library thread */
     hb_log( "hb_init: starting libhb thread" );
index abe827e..2fd308c 100755 (executable)
@@ -2460,7 +2460,7 @@ static void ffmpeg_add_codec( hb_stream_t *stream, int stream_index )
     context->error_recognition = 1;
     context->error_concealment = FF_EC_GUESS_MVS|FF_EC_DEBLOCK;
     AVCodec *codec = avcodec_find_decoder( context->codec_id );
-    avcodec_open( context, codec );
+    hb_avcodec_open( context, codec );
 }
 
 // The ffmpeg stream reader / parser shares a lot of state with the 
index e810959..411c656 100644 (file)
@@ -238,7 +238,7 @@ static void InitAudio( hb_work_object_t * w, int i )
         c->sample_rate = sync->audio->config.in.samplerate;
         c->channels    = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( sync->audio->config.in.channel_layout );
 
-        if( avcodec_open( c, codec ) < 0 )
+        if( hb_avcodec_open( c, codec ) < 0 )
         {
             hb_log( "sync: avcodec_open failed" );
             return;
@@ -257,7 +257,7 @@ static void InitAudio( hb_work_object_t * w, int i )
         }
 
         free( zeros );
-        avcodec_close( c );
+        hb_avcodec_close( c );
         av_free( c );
     }
     else