X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fcodec%2Fshine%2Fshine_mod.c;h=37e2e983384e092b0268ae009ccec3e9decaee30;hb=99b4edcb0aa0a0ce36d10ad2a48375995a2c32ca;hp=94de78ef7732300be190b7bbe1985be8b953b999;hpb=67242bac7ca5c120d63cea2f730a15330fe682af;p=vlc diff --git a/modules/codec/shine/shine_mod.c b/modules/codec/shine/shine_mod.c index 94de78ef77..37e2e98338 100644 --- a/modules/codec/shine/shine_mod.c +++ b/modules/codec/shine/shine_mod.c @@ -40,8 +40,8 @@ /* shine.c uses a lot of static variables, so we include the C file to keep * the scope. - * Note that it makes this decoder non reentrant, this is why we set - * b_reentrant to VLC_FALSE in the module initialisation */ + * Note that it makes this decoder non reentrant, this is why we have the + * struct entrant below */ #include "shine.c" struct encoder_sys_t @@ -68,13 +68,19 @@ vlc_module_begin(); set_callbacks( OpenEncoder, CloseEncoder ); vlc_module_end(); +static struct +{ + bool busy; + vlc_mutex_t lock; +} entrant = { false, VLC_STATIC_MUTEX, }; + static int OpenEncoder( vlc_object_t *p_this ) { encoder_t *p_enc = (encoder_t*)p_this; encoder_sys_t *p_sys; /* FIXME: what about layers 1 and 2 ? shine is an 'MP3' encoder */ - if( p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','3',' ') || + if( p_enc->fmt_out.i_codec != VLC_CODEC_MP3 || p_enc->fmt_out.audio.i_channels > 2 ) return VLC_EGENERIC; @@ -96,23 +102,39 @@ static int OpenEncoder( vlc_object_t *p_this ) p_enc->fmt_out.i_bitrate, p_enc->fmt_out.audio.i_rate, p_enc->fmt_out.audio.i_channels ); - p_sys = p_enc->p_sys = malloc( sizeof( encoder_sys_t ) ); + vlc_mutex_lock( &entrant.lock ); + if( entrant.busy ) + { + msg_Err( p_enc, "encoder already in progress" ); + vlc_mutex_unlock( &entrant.lock ); + return VLC_EGENERIC; + } + entrant.busy = true; + vlc_mutex_unlock( &entrant.lock ); + + p_enc->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) ); if( !p_sys ) - return VLC_ENOMEM; - memset( p_sys, 0, sizeof( encoder_sys_t ) ); + goto enomem; if( !( p_sys->p_fifo = block_FifoNew() ) ) { free( p_sys ); - return VLC_ENOMEM; + goto enomem; } init_mp3_encoder_engine( p_enc->fmt_out.audio.i_rate, p_enc->fmt_out.audio.i_channels, p_enc->fmt_out.i_bitrate / 1000 ); p_enc->pf_encode_audio = EncodeFrame; + p_enc->fmt_out.i_cat = AUDIO_ES; return VLC_SUCCESS; + +enomem: + vlc_mutex_lock( &entrant.lock ); + entrant.busy = false; + vlc_mutex_unlock( &entrant.lock ); + return VLC_ENOMEM; } /* We split/pack PCM blocks to a fixed size: pcm_chunk_size bytes */ @@ -124,7 +146,7 @@ static block_t *GetPCM( encoder_t *p_enc, aout_buffer_t *p_block ) if( !p_block ) goto buffered; /* just return a block if we can */ /* Put the PCM samples sent by VLC in the Fifo */ - while( p_sys->i_buffer + p_block->i_nb_bytes >= pcm_chunk_size ) + while( p_sys->i_buffer + p_block->i_buffer >= pcm_chunk_size ) { unsigned int i_buffer = 0; p_pcm_block = block_New( p_enc, pcm_chunk_size ); @@ -144,20 +166,20 @@ static block_t *GetPCM( encoder_t *p_enc, aout_buffer_t *p_block ) p_block->p_buffer, pcm_chunk_size - i_buffer ); p_block->p_buffer += pcm_chunk_size - i_buffer; - p_block->i_nb_bytes -= pcm_chunk_size - i_buffer; + p_block->i_buffer -= pcm_chunk_size - i_buffer; block_FifoPut( p_sys->p_fifo, p_pcm_block ); } /* We hadn't enough data to make a block, put it in standby */ - if( p_block->i_nb_bytes ) + if( p_block->i_buffer ) { uint8_t *p_tmp; if( p_sys->i_buffer > 0 ) - p_tmp = realloc( p_sys->p_buffer, p_block->i_nb_bytes + p_sys->i_buffer ); + p_tmp = realloc( p_sys->p_buffer, p_block->i_buffer + p_sys->i_buffer ); else - p_tmp = malloc( p_block->i_nb_bytes ); + p_tmp = malloc( p_block->i_buffer ); if( !p_tmp ) { @@ -168,10 +190,10 @@ static block_t *GetPCM( encoder_t *p_enc, aout_buffer_t *p_block ) } p_sys->p_buffer = p_tmp; vlc_memcpy( p_sys->p_buffer + p_sys->i_buffer, - p_block->p_buffer, p_block->i_nb_bytes ); + p_block->p_buffer, p_block->i_buffer ); - p_sys->i_buffer += p_block->i_nb_bytes; - p_block->i_nb_bytes = 0; + p_sys->i_buffer += p_block->i_buffer; + p_block->i_buffer = 0; } buffered: @@ -181,13 +203,14 @@ buffered: static block_t *EncodeFrame( encoder_t *p_enc, aout_buffer_t *p_block ) { - encoder_sys_t *p_sys = (encoder_sys_t *)p_enc->p_sys; block_t *p_pcm_block; block_t *p_chain = NULL; - unsigned int i_samples = p_block->i_nb_bytes >> 2 /* s16l stereo */; - mtime_t start_date = p_block->start_date; + unsigned int i_samples = p_block->i_buffer >> 2 /* s16l stereo */; + mtime_t start_date = p_block->i_pts; start_date -= (mtime_t)i_samples * (mtime_t)1000000 / (mtime_t)p_enc->fmt_out.audio.i_rate; + VLC_UNUSED(p_enc); + do { p_pcm_block = GetPCM( p_enc, p_block ); if( !p_pcm_block ) @@ -215,7 +238,7 @@ static block_t *EncodeFrame( encoder_t *p_enc, aout_buffer_t *p_block ) start_date += p_mp3_block->i_length; p_mp3_block->i_dts = p_mp3_block->i_pts = start_date; - p_mp3_block->i_samples = SAMP_PER_FRAME1; + p_mp3_block->i_nb_samples = SAMP_PER_FRAME1; block_ChainAppend( &p_chain, p_mp3_block ); @@ -228,6 +251,10 @@ static void CloseEncoder( vlc_object_t *p_this ) { encoder_sys_t *p_sys = ((encoder_t*)p_this)->p_sys; + vlc_mutex_lock( &entrant.lock ); + entrant.busy = false; + vlc_mutex_unlock( &entrant.lock ); + /* TODO: we should send the last PCM block padded with 0 * But we don't know if other blocks will come before it's too late */ if( p_sys->i_buffer )