]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/audio.c
AVCodec : Set audio related parameters in ffmpeg_OpenCodec and use it for audio decod...
[vlc] / modules / codec / avcodec / audio.c
index 15b1e11439794e5650648ae0800384921e720413..483c3067c84515774f7747c3b45705de54cfad2c 100644 (file)
@@ -82,39 +82,8 @@ struct decoder_sys_t
 
 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust );
 
-/*****************************************************************************
- * InitAudioDec: initialize audio decoder
- *****************************************************************************
- * The ffmpeg codec will be opened, some memory allocated.
- *****************************************************************************/
-int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
-                      AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
+static void InitDecoderConfig( decoder_t *p_dec, AVCodecContext *p_context )
 {
-    decoder_sys_t *p_sys;
-
-    /* Allocate the memory needed to store the decoder's structure */
-    if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
-    {
-        return VLC_ENOMEM;
-    }
-
-    p_codec->type = CODEC_TYPE_AUDIO;
-    p_context->codec_type = CODEC_TYPE_AUDIO;
-    p_context->codec_id = i_codec_id;
-    p_sys->p_context = p_context;
-    p_sys->p_codec = p_codec;
-    p_sys->i_codec_id = i_codec_id;
-    p_sys->psz_namecodec = psz_namecodec;
-    p_sys->b_delayed_open = false;
-
-    /* ***** Fill p_context with init values ***** */
-    p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
-    p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
-
-    p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
-    p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
-    p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
-
     if( p_dec->fmt_in.i_extra > 0 )
     {
         const uint8_t * const p_src = p_dec->fmt_in.p_extra;
@@ -147,13 +116,13 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
 
         if( i_size > 0 )
         {
-            p_sys->p_context->extradata =
+            p_context->extradata =
                 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
-            if( p_sys->p_context->extradata )
+            if( p_context->extradata )
             {
-                uint8_t *p_dst = p_sys->p_context->extradata;
+                uint8_t *p_dst = p_context->extradata;
 
-                p_sys->p_context->extradata_size = i_size;
+                p_context->extradata_size = i_size;
 
                 memcpy( &p_dst[0],            &p_src[i_offset], i_size );
                 memset( &p_dst[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
@@ -162,16 +131,41 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
     }
     else
     {
-        p_sys->p_context->extradata_size = 0;
-        p_sys->p_context->extradata = NULL;
+        p_context->extradata_size = 0;
+        p_context->extradata = NULL;
+    }
+}
+
+/*****************************************************************************
+ * InitAudioDec: initialize audio decoder
+ *****************************************************************************
+ * The ffmpeg codec will be opened, some memory allocated.
+ *****************************************************************************/
+int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
+                      AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
+{
+    decoder_sys_t *p_sys;
+
+    /* Allocate the memory needed to store the decoder's structure */
+    if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
+    {
+        return VLC_ENOMEM;
     }
 
+    p_codec->type = CODEC_TYPE_AUDIO;
+    p_context->codec_type = CODEC_TYPE_AUDIO;
+    p_context->codec_id = i_codec_id;
+    p_sys->p_context = p_context;
+    p_sys->p_codec = p_codec;
+    p_sys->i_codec_id = i_codec_id;
+    p_sys->psz_namecodec = psz_namecodec;
+    p_sys->b_delayed_open = false;
+
+    // Initialize decoder extradata
+    InitDecoderConfig( p_dec, p_context);
+
     /* ***** Open the codec ***** */
-    int ret;
-    vlc_avcodec_lock();
-    ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
-    vlc_avcodec_unlock();
-    if( ret < 0 )
+    if( ffmpeg_OpenCodec( p_dec ) < 0 )
     {
         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
         free( p_sys->p_context->extradata );
@@ -179,8 +173,6 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
         return VLC_EGENERIC;
     }
 
-    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
-
     switch( i_codec_id )
     {
     case CODEC_ID_WAVPACK:
@@ -215,7 +207,7 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
 
     /* */
     p_dec->fmt_out.i_cat = AUDIO_ES;
-    /* Try to set as much informations as possible but do not trust it */
+    /* Try to set as much information as possible but do not trust it */
     SetupOutputFormat( p_dec, false );
 
     date_Set( &p_sys->end_date, 0 );
@@ -404,6 +396,35 @@ void EndAudioDec( decoder_t *p_dec )
  *
  *****************************************************************************/
 
+void GetVlcAudioFormat( vlc_fourcc_t *pi_codec, unsigned *pi_bits, int i_sample_fmt )
+{
+    switch( i_sample_fmt )
+    {
+    case SAMPLE_FMT_U8:
+        *pi_codec = VLC_CODEC_U8;
+        *pi_bits = 8;
+        break;
+    case SAMPLE_FMT_S32:
+        *pi_codec = VLC_CODEC_S32N;
+        *pi_bits = 32;
+        break;
+    case SAMPLE_FMT_FLT:
+        *pi_codec = VLC_CODEC_FL32;
+        *pi_bits = 32;
+        break;
+    case SAMPLE_FMT_DBL:
+        *pi_codec = VLC_CODEC_FL64;
+        *pi_bits = 64;
+        break;
+
+    case SAMPLE_FMT_S16:
+    default:
+        *pi_codec = VLC_CODEC_S16N;
+        *pi_bits = 16;
+        break;
+    }
+}
+
 static const uint64_t pi_channels_map[][2] =
 {
     { CH_FRONT_LEFT,        AOUT_CHAN_LEFT },
@@ -432,32 +453,10 @@ static void SetupOutputFormat( decoder_t *p_dec, bool b_trust )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    switch( p_sys->p_context->sample_fmt )
-    {
-    case SAMPLE_FMT_U8:
-        p_dec->fmt_out.i_codec = VLC_CODEC_U8;
-        p_dec->fmt_out.audio.i_bitspersample = 8;
-        break;
-    case SAMPLE_FMT_S32:
-        p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
-        p_dec->fmt_out.audio.i_bitspersample = 32;
-        break;
-    case SAMPLE_FMT_FLT:
-        p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
-        p_dec->fmt_out.audio.i_bitspersample = 32;
-        break;
-    case SAMPLE_FMT_DBL:
-        p_dec->fmt_out.i_codec = VLC_CODEC_FL64;
-        p_dec->fmt_out.audio.i_bitspersample = 64;
-        break;
-
-    case SAMPLE_FMT_S16:
-    default:
-        p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
-        p_dec->fmt_out.audio.i_bitspersample = 16;
-        break;
-    }
-    p_dec->fmt_out.audio.i_rate     = p_sys->p_context->sample_rate;
+    GetVlcAudioFormat( &p_dec->fmt_out.i_codec,
+                       &p_dec->fmt_out.audio.i_bitspersample,
+                       p_sys->p_context->sample_fmt );
+    p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
 
     /* */
     if( p_sys->i_previous_channels == p_sys->p_context->channels &&