]> git.sesse.net Git - vlc/commitdiff
avcodec, switcher: use the global avcodec lock
authorRémi Denis-Courmont <remi@remlab.net>
Thu, 12 Mar 2009 15:34:36 +0000 (17:34 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Thu, 12 Mar 2009 15:41:49 +0000 (17:41 +0200)
modules/codec/avcodec/audio.c
modules/codec/avcodec/avcodec.c
modules/codec/avcodec/avcodec.h
modules/codec/avcodec/encoder.c
modules/codec/avcodec/video.c
modules/stream_out/switcher.c

index 97424a10bf45b88bd4ee4655670fb0c1824d8a31..4f8a7e1471ff55b538b5d923c6b910fdbfd9a250 100644 (file)
@@ -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 );
 
index 76cb524ce686644c6a492bbb62545375a583a5c5..1f3ca6f7c465c7610aa4acaf3fba11e59b91b5d5 100644 (file)
@@ -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();
 }
index 31d01c59de67818b209974353e6f04aca14c1692..be0f1d1912c9ed20b4cb542926828a58194d8beb 100644 (file)
@@ -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
  *****************************************************************************/
index 573c97ce0fd55e8c3682315725776e9ed3281b6d..3b1a17fe003b4f75416416eae20156b5f4f2ac30 100644 (file)
@@ -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 );
index e5527033c8a474c7892bdd36e88ae0d7ff908177..d28be2391a1ac16796031bc43be1545b29462ae4 100644 (file)
@@ -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;
index dba845fb71975f09c872b7513d6b84c4c19d5389..9faf03eff2a2f97518feb9d561f1148c43da6558 100644 (file)
@@ -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();