Commit bf7164b7 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

avcodec, switcher: use the global avcodec lock

parent 6f037fa4
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_aout.h> #include <vlc_aout.h>
#include <vlc_codec.h> #include <vlc_codec.h>
#include <vlc_avcodec.h>
/* ffmpeg header */ /* ffmpeg header */
#ifdef HAVE_LIBAVCODEC_AVCODEC_H #ifdef HAVE_LIBAVCODEC_AVCODEC_H
...@@ -188,17 +189,17 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context, ...@@ -188,17 +189,17 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
} }
/* ***** Open the codec ***** */ /* ***** Open the codec ***** */
vlc_mutex_lock( &avcodec_lock ); int ret;
vlc_avcodec_lock();
if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 ) 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 ); msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
free( p_sys->p_context->extradata ); free( p_sys->p_context->extradata );
free( p_sys ); free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
vlc_mutex_unlock( &avcodec_lock );
msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec ); msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_plugin.h> #include <vlc_plugin.h>
#include <vlc_codec.h> #include <vlc_codec.h>
#include <vlc_avcodec.h>
/* ffmpeg header */ /* ffmpeg header */
#define HAVE_MMX 1 #define HAVE_MMX 1
...@@ -201,8 +202,6 @@ vlc_module_begin () ...@@ -201,8 +202,6 @@ vlc_module_begin ()
vlc_module_end () vlc_module_end ()
vlc_mutex_t avcodec_lock = VLC_STATIC_MUTEX;
/***************************************************************************** /*****************************************************************************
* OpenDecoder: probe the decoder and return score * OpenDecoder: probe the decoder and return score
*****************************************************************************/ *****************************************************************************/
...@@ -311,9 +310,9 @@ static void CloseDecoder( vlc_object_t *p_this ) ...@@ -311,9 +310,9 @@ static void CloseDecoder( vlc_object_t *p_this )
if( !p_sys->b_delayed_open ) if( !p_sys->b_delayed_open )
{ {
vlc_mutex_lock( &avcodec_lock ); vlc_avcodec_lock();
avcodec_close( p_sys->p_context ); 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 ); msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
av_free( p_sys->p_context ); av_free( p_sys->p_context );
...@@ -326,7 +325,7 @@ void InitLibavcodec( vlc_object_t *p_object ) ...@@ -326,7 +325,7 @@ void InitLibavcodec( vlc_object_t *p_object )
{ {
static bool b_ffmpeginit = false; static bool b_ffmpeginit = false;
vlc_mutex_lock( &avcodec_lock ); vlc_avcodec_lock();
/* *** init ffmpeg library (libavcodec) *** */ /* *** init ffmpeg library (libavcodec) *** */
if( !b_ffmpeginit ) if( !b_ffmpeginit )
...@@ -344,5 +343,5 @@ void InitLibavcodec( vlc_object_t *p_object ) ...@@ -344,5 +343,5 @@ void InitLibavcodec( vlc_object_t *p_object )
msg_Dbg( p_object, "libavcodec already initialized" ); msg_Dbg( p_object, "libavcodec already initialized" );
} }
vlc_mutex_unlock( &avcodec_lock ); vlc_avcodec_unlock();
} }
...@@ -58,9 +58,6 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context, ...@@ -58,9 +58,6 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
AVCodec *p_codec, int i_codec_id, const char *psz_namecodec ); AVCodec *p_codec, int i_codec_id, const char *psz_namecodec );
void EndAudioDec( decoder_t *p_dec ); void EndAudioDec( decoder_t *p_dec );
/* Avcodec global lock */
extern vlc_mutex_t avcodec_lock;
/***************************************************************************** /*****************************************************************************
* Module descriptor help strings * Module descriptor help strings
*****************************************************************************/ *****************************************************************************/
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <vlc_sout.h> #include <vlc_sout.h>
#include <vlc_codec.h> #include <vlc_codec.h>
#include <vlc_dialog.h> #include <vlc_dialog.h>
#include <vlc_avcodec.h>
/* ffmpeg header */ /* ffmpeg header */
#define HAVE_MMX 1 #define HAVE_MMX 1
...@@ -614,11 +615,12 @@ int OpenEncoder( vlc_object_t *p_this ) ...@@ -614,11 +615,12 @@ int OpenEncoder( vlc_object_t *p_this )
p_context->extradata = NULL; p_context->extradata = NULL;
p_context->flags |= CODEC_FLAG_GLOBAL_HEADER; p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
vlc_mutex_lock( &avcodec_lock ); int ret;
vlc_avcodec_lock();
if( avcodec_open( p_context, p_codec ) ) 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 && if( p_enc->fmt_in.i_cat == AUDIO_ES &&
(p_context->channels > 2 || i_codec_id == CODEC_ID_MP2 (p_context->channels > 2 || i_codec_id == CODEC_ID_MP2
|| i_codec_id == CODEC_ID_MP3) ) || i_codec_id == CODEC_ID_MP3) )
...@@ -668,10 +670,11 @@ int OpenEncoder( vlc_object_t *p_this ) ...@@ -668,10 +670,11 @@ int OpenEncoder( vlc_object_t *p_this )
} }
p_context->codec = NULL; p_context->codec = NULL;
vlc_mutex_lock( &avcodec_lock ); vlc_avcodec_lock();
if( avcodec_open( p_context, p_codec ) ) ret = avcodec_open( p_context, p_codec );
vlc_avcodec_unlock();
if( ret )
{ {
vlc_mutex_unlock( &avcodec_lock );
msg_Err( p_enc, "cannot open encoder" ); msg_Err( p_enc, "cannot open encoder" );
dialog_Fatal( p_enc, dialog_Fatal( p_enc,
_("Streaming / Transcoding failed"), _("Streaming / Transcoding failed"),
...@@ -689,7 +692,6 @@ int OpenEncoder( vlc_object_t *p_this ) ...@@ -689,7 +692,6 @@ int OpenEncoder( vlc_object_t *p_this )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
} }
vlc_mutex_unlock( &avcodec_lock );
p_enc->fmt_out.i_extra = p_context->extradata_size; p_enc->fmt_out.i_extra = p_context->extradata_size;
if( p_enc->fmt_out.i_extra ) if( p_enc->fmt_out.i_extra )
...@@ -1122,9 +1124,9 @@ void CloseEncoder( vlc_object_t *p_this ) ...@@ -1122,9 +1124,9 @@ void CloseEncoder( vlc_object_t *p_this )
free( pp_contexts ); free( pp_contexts );
} }
vlc_mutex_lock( &avcodec_lock ); vlc_avcodec_lock();
avcodec_close( p_sys->p_context ); avcodec_close( p_sys->p_context );
vlc_mutex_unlock( &avcodec_lock ); vlc_avcodec_unlock();
av_free( p_sys->p_context ); av_free( p_sys->p_context );
free( p_sys->p_buffer ); free( p_sys->p_buffer );
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <vlc_codec.h> #include <vlc_codec.h>
#include <vlc_vout.h> #include <vlc_vout.h>
#include <vlc_codecs.h> /* BITMAPINFOHEADER */ #include <vlc_codecs.h> /* BITMAPINFOHEADER */
#include <vlc_avcodec.h>
/* ffmpeg header */ /* ffmpeg header */
#ifdef HAVE_LIBAVCODEC_AVCODEC_H #ifdef HAVE_LIBAVCODEC_AVCODEC_H
...@@ -809,13 +810,12 @@ static int ffmpeg_OpenCodec( decoder_t *p_dec ) ...@@ -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; p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
#endif #endif
vlc_mutex_lock( &avcodec_lock ); int ret;
if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 ) vlc_avcodec_lock();
{ ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
vlc_mutex_unlock( &avcodec_lock ); vlc_avcodec_unlock();
if( ret < 0 )
return VLC_EGENERIC; return VLC_EGENERIC;
}
vlc_mutex_unlock( &avcodec_lock );
msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec ); msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
p_sys->b_delayed_open = false; p_sys->b_delayed_open = false;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <vlc_plugin.h> #include <vlc_plugin.h>
#include <vlc_sout.h> #include <vlc_sout.h>
#include <vlc_vout.h> #include <vlc_vout.h>
#include <vlc_avcodec.h>
#include <vlc_block.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 ) ...@@ -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->channels = p_fmt->audio.i_channels;
id->ff_enc_c->bit_rate = p_fmt->i_bitrate; id->ff_enc_c->bit_rate = p_fmt->i_bitrate;
vlc_avcodec_lock();
if( avcodec_open( id->ff_enc_c, id->ff_enc ) ) if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
{ {
avcodec_unlock();
msg_Err( p_stream, "cannot open encoder" ); msg_Err( p_stream, "cannot open encoder" );
av_free( id->ff_enc_c ); av_free( id->ff_enc_c );
free( id ); free( id );
return NULL; return NULL;
} }
avcodec_unlock();
id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 ); 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, 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 ) ...@@ -428,7 +432,9 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
return id; return id;
error: error:
vlc_avcodec_lock();
avcodec_close( id->ff_enc_c ); avcodec_close( id->ff_enc_c );
vlc_avcodec_unlock();
free( id->p_samples ); free( id->p_samples );
free( id->p_buffer_out ); free( id->p_buffer_out );
av_free( id->ff_enc_c ); av_free( id->ff_enc_c );
...@@ -458,7 +464,9 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id ) ...@@ -458,7 +464,9 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
if ( id->ff_enc ) if ( id->ff_enc )
{ {
vlc_avcodec_lock();
avcodec_close( id->ff_enc_c ); avcodec_close( id->ff_enc_c );
vlc_avcodec_unlock();
av_free( id->ff_enc_c ); av_free( id->ff_enc_c );
av_free( id->p_frame ); av_free( id->p_frame );
free( id->p_buffer_out ); free( id->p_buffer_out );
...@@ -708,7 +716,9 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id ) ...@@ -708,7 +716,9 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
if ( id->ff_enc ) if ( id->ff_enc )
{ {
vlc_avcodec_lock();
avcodec_close( id->ff_enc_c ); avcodec_close( id->ff_enc_c );
vlc_avcodec_unlock();
av_free( id->ff_enc_c ); av_free( id->ff_enc_c );
av_free( id->p_frame ); av_free( id->p_frame );
free( id->p_buffer_out ); free( id->p_buffer_out );
...@@ -785,11 +795,14 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id ) ...@@ -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->mb_decision = FF_MB_DECISION_SIMPLE;
id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P; id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
avcodec_lock();
if( avcodec_open( id->ff_enc_c, id->ff_enc ) ) if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
{ {
avcodec_unlock();
msg_Err( p_stream, "cannot open encoder" ); msg_Err( p_stream, "cannot open encoder" );
return 0; return 0;
} }
avcodec_unlock();
id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 ); id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
id->p_frame = avcodec_alloc_frame(); id->p_frame = avcodec_alloc_frame();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment