[vlc-devel] commit: avcodec, switcher: use the global avcodec lock ( Rémi Denis-Courmont )

git version control git at videolan.org
Thu Mar 12 16:41:54 CET 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Mar 12 17:34:36 2009 +0200| [bf7164b79255132d97dffbc82fe3dc1e945e053e] | committer: Rémi Denis-Courmont 

avcodec, switcher: use the global avcodec lock

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=bf7164b79255132d97dffbc82fe3dc1e945e053e
---

 modules/codec/avcodec/audio.c   |   11 ++++++-----
 modules/codec/avcodec/avcodec.c |   11 +++++------
 modules/codec/avcodec/avcodec.h |    3 ---
 modules/codec/avcodec/encoder.c |   22 ++++++++++++----------
 modules/codec/avcodec/video.c   |   12 ++++++------
 modules/stream_out/switcher.c   |   13 +++++++++++++
 6 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/modules/codec/avcodec/audio.c b/modules/codec/avcodec/audio.c
index 97424a1..4f8a7e1 100644
--- a/modules/codec/avcodec/audio.c
+++ b/modules/codec/avcodec/audio.c
@@ -32,6 +32,7 @@
 #include <vlc_common.h>
 #include <vlc_aout.h>
 #include <vlc_codec.h>
+#include <vlc_avcodec.h>
 
 /* ffmpeg header */
 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
@@ -188,17 +189,17 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
     }
 
     /* ***** Open the codec ***** */
-    vlc_mutex_lock( &avcodec_lock );
-
-    if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
+    int ret;
+    vlc_avcodec_lock();
+    ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
+    vlc_avcodec_unlock();
+    if( ret < 0 )
     {
-        vlc_mutex_unlock( &avcodec_lock );
         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
         free( p_sys->p_context->extradata );
         free( p_sys );
         return VLC_EGENERIC;
     }
-    vlc_mutex_unlock( &avcodec_lock );
 
     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
 
diff --git a/modules/codec/avcodec/avcodec.c b/modules/codec/avcodec/avcodec.c
index 76cb524..1f3ca6f 100644
--- a/modules/codec/avcodec/avcodec.c
+++ b/modules/codec/avcodec/avcodec.c
@@ -32,6 +32,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_codec.h>
+#include <vlc_avcodec.h>
 
 /* ffmpeg header */
 #define HAVE_MMX 1
@@ -201,8 +202,6 @@ vlc_module_begin ()
 
 vlc_module_end ()
 
-vlc_mutex_t avcodec_lock = VLC_STATIC_MUTEX;
-
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
  *****************************************************************************/
@@ -311,9 +310,9 @@ static void CloseDecoder( vlc_object_t *p_this )
 
         if( !p_sys->b_delayed_open )
         {
-            vlc_mutex_lock( &avcodec_lock );
+            vlc_avcodec_lock();
             avcodec_close( p_sys->p_context );
-            vlc_mutex_unlock( &avcodec_lock );
+            vlc_avcodec_unlock();
         }
         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
         av_free( p_sys->p_context );
@@ -326,7 +325,7 @@ void InitLibavcodec( vlc_object_t *p_object )
 {
     static bool b_ffmpeginit = false;
 
-    vlc_mutex_lock( &avcodec_lock );
+    vlc_avcodec_lock();
 
     /* *** init ffmpeg library (libavcodec) *** */
     if( !b_ffmpeginit )
@@ -344,5 +343,5 @@ void InitLibavcodec( vlc_object_t *p_object )
         msg_Dbg( p_object, "libavcodec already initialized" );
     }
 
-    vlc_mutex_unlock( &avcodec_lock );
+    vlc_avcodec_unlock();
 }
diff --git a/modules/codec/avcodec/avcodec.h b/modules/codec/avcodec/avcodec.h
index 31d01c5..be0f1d1 100644
--- a/modules/codec/avcodec/avcodec.h
+++ b/modules/codec/avcodec/avcodec.h
@@ -58,9 +58,6 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
                   AVCodec *p_codec, int i_codec_id, const char *psz_namecodec );
 void EndAudioDec( decoder_t *p_dec );
 
-/* Avcodec global lock */
-extern vlc_mutex_t avcodec_lock;
-
 /*****************************************************************************
  * Module descriptor help strings
  *****************************************************************************/
diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c
index 573c97c..3b1a17f 100644
--- a/modules/codec/avcodec/encoder.c
+++ b/modules/codec/avcodec/encoder.c
@@ -38,6 +38,7 @@
 #include <vlc_sout.h>
 #include <vlc_codec.h>
 #include <vlc_dialog.h>
+#include <vlc_avcodec.h>
 
 /* ffmpeg header */
 #define HAVE_MMX 1
@@ -614,11 +615,12 @@ int OpenEncoder( vlc_object_t *p_this )
     p_context->extradata = NULL;
     p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
-    vlc_mutex_lock( &avcodec_lock );
-
-    if( avcodec_open( p_context, p_codec ) )
+    int ret;
+    vlc_avcodec_lock();
+    ret = avcodec_open( p_context, p_codec );
+    vlc_avcodec_unlock();
+    if( ret )
     {
-        vlc_mutex_unlock( &avcodec_lock );
         if( p_enc->fmt_in.i_cat == AUDIO_ES &&
              (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
                || i_codec_id == CODEC_ID_MP3) )
@@ -668,10 +670,11 @@ int OpenEncoder( vlc_object_t *p_this )
             }
 
             p_context->codec = NULL;
-            vlc_mutex_lock( &avcodec_lock );
-            if( avcodec_open( p_context, p_codec ) )
+            vlc_avcodec_lock();
+            ret = avcodec_open( p_context, p_codec );
+            vlc_avcodec_unlock();
+            if( ret )
             {
-                vlc_mutex_unlock( &avcodec_lock );
                 msg_Err( p_enc, "cannot open encoder" );
                 dialog_Fatal( p_enc,
                                 _("Streaming / Transcoding failed"),
@@ -689,7 +692,6 @@ int OpenEncoder( vlc_object_t *p_this )
             return VLC_EGENERIC;
         }
     }
-    vlc_mutex_unlock( &avcodec_lock );
 
     p_enc->fmt_out.i_extra = p_context->extradata_size;
     if( p_enc->fmt_out.i_extra )
@@ -1122,9 +1124,9 @@ void CloseEncoder( vlc_object_t *p_this )
         free( pp_contexts );
     }
 
-    vlc_mutex_lock( &avcodec_lock );
+    vlc_avcodec_lock();
     avcodec_close( p_sys->p_context );
-    vlc_mutex_unlock( &avcodec_lock );
+    vlc_avcodec_unlock();
     av_free( p_sys->p_context );
 
     free( p_sys->p_buffer );
diff --git a/modules/codec/avcodec/video.c b/modules/codec/avcodec/video.c
index e552703..d28be23 100644
--- a/modules/codec/avcodec/video.c
+++ b/modules/codec/avcodec/video.c
@@ -33,6 +33,7 @@
 #include <vlc_codec.h>
 #include <vlc_vout.h>
 #include <vlc_codecs.h>                               /* BITMAPINFOHEADER */
+#include <vlc_avcodec.h>
 
 /* ffmpeg header */
 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
@@ -809,13 +810,12 @@ static int ffmpeg_OpenCodec( decoder_t *p_dec )
     p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
 #endif
 
-    vlc_mutex_lock( &avcodec_lock );
-    if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
-    {
-        vlc_mutex_unlock( &avcodec_lock );
+    int ret;
+    vlc_avcodec_lock();
+    ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
+    vlc_avcodec_unlock();
+    if( ret < 0 )
         return VLC_EGENERIC;
-    }
-    vlc_mutex_unlock( &avcodec_lock );
     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
 
     p_sys->b_delayed_open = false;
diff --git a/modules/stream_out/switcher.c b/modules/stream_out/switcher.c
index dba845f..9faf03e 100644
--- a/modules/stream_out/switcher.c
+++ b/modules/stream_out/switcher.c
@@ -34,6 +34,7 @@
 #include <vlc_plugin.h>
 #include <vlc_sout.h>
 #include <vlc_vout.h>
+#include <vlc_avcodec.h>
 
 #include <vlc_block.h>
 
@@ -384,13 +385,16 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         id->ff_enc_c->channels    = p_fmt->audio.i_channels;
         id->ff_enc_c->bit_rate    = p_fmt->i_bitrate;
 
+        vlc_avcodec_lock();
         if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
         {
+            avcodec_unlock();
             msg_Err( p_stream, "cannot open encoder" );
             av_free( id->ff_enc_c );
             free( id );
             return NULL;
         }
+        avcodec_unlock();
 
         id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
         id->p_samples = calloc( id->ff_enc_c->frame_size * p_fmt->audio.i_channels,
@@ -428,7 +432,9 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         return id;
 
 error:
+    vlc_avcodec_lock();
     avcodec_close( id->ff_enc_c );
+    vlc_avcodec_unlock();
     free( id->p_samples );
     free( id->p_buffer_out );
     av_free( id->ff_enc_c );
@@ -458,7 +464,9 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
 
     if ( id->ff_enc )
     {
+        vlc_avcodec_lock();
         avcodec_close( id->ff_enc_c );
+        vlc_avcodec_unlock();
         av_free( id->ff_enc_c );
         av_free( id->p_frame );
         free( id->p_buffer_out );
@@ -708,7 +716,9 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
 
     if ( id->ff_enc )
     {
+        vlc_avcodec_lock();
         avcodec_close( id->ff_enc_c );
+        vlc_avcodec_unlock();
         av_free( id->ff_enc_c );
         av_free( id->p_frame );
         free( id->p_buffer_out );
@@ -785,11 +795,14 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
         id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
         id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
 
+        avcodec_lock();
         if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
         {
+            avcodec_unlock();
             msg_Err( p_stream, "cannot open encoder" );
             return 0;
         }
+        avcodec_unlock();
 
         id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
         id->p_frame = avcodec_alloc_frame();




More information about the vlc-devel mailing list