[vlc-commits] transcode: audio: refactor encoder and filters config

Francois Cartegnie git at videolan.org
Mon Jul 9 16:15:43 CEST 2018


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Sun Jul  1 16:21:18 2018 +0200| [bd14a129249529fb14a08b6de19631bc86cf827a] | committer: Francois Cartegnie

transcode: audio: refactor encoder and filters config

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

 modules/stream_out/transcode/audio.c     |  86 +++++++++++------------
 modules/stream_out/transcode/transcode.c | 117 ++++++++++++++++---------------
 modules/stream_out/transcode/transcode.h |  19 ++---
 3 files changed, 113 insertions(+), 109 deletions(-)

diff --git a/modules/stream_out/transcode/audio.c b/modules/stream_out/transcode/audio.c
index 818a03d3af..fd872dc9ad 100644
--- a/modules/stream_out/transcode/audio.c
+++ b/modules/stream_out/transcode/audio.c
@@ -76,17 +76,17 @@ static int audio_update_format( decoder_t *p_dec )
 }
 
 static int transcode_audio_filters_init( sout_stream_t *p_stream,
+                                         const sout_filters_config_t *p_cfg,
                                          const audio_format_t *p_dec_out,
                                          const audio_format_t *p_enc_in,
                                          aout_filters_t **pp_chain )
 {
-    sout_stream_sys_t *p_sys = p_stream->p_sys;
     /* Load user specified audio filters */
     /* XXX: These variable names come kinda out of nowhere... */
     var_Create( p_stream, "audio-time-stretch", VLC_VAR_BOOL );
     var_Create( p_stream, "audio-filter", VLC_VAR_STRING );
-    if( p_sys->psz_af )
-        var_SetString( p_stream, "audio-filter", p_sys->psz_af );
+    if( p_cfg->psz_filters )
+        var_SetString( p_stream, "audio-filter", p_cfg->psz_filters );
     *pp_chain = aout_FiltersNew( p_stream, p_dec_out, p_enc_in, NULL, NULL );
     var_Destroy( p_stream, "audio-filter" );
     var_Destroy( p_stream, "audio-time-stretch" );
@@ -97,14 +97,15 @@ static int transcode_audio_encoder_open( sout_stream_t *p_stream, sout_stream_id
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
 
-    id->p_encoder->p_module = module_need( id->p_encoder, "encoder", p_sys->psz_aenc, true );
+    id->p_encoder->p_module = module_need( id->p_encoder, "encoder",
+                                           p_sys->aenc_cfg.psz_name, true );
     /* p_sys->i_acodec = 0 if there isn't acodec defined */
     if( !id->p_encoder->p_module )
     {
         msg_Err( p_stream, "cannot find audio encoder (module:%s fourcc:%4.4s). "
                            "Take a look few lines earlier to see possible reason.",
-                           p_sys->psz_aenc ? p_sys->psz_aenc : "any",
-                           (char *)&p_sys->i_acodec );
+                           p_sys->aenc_cfg.psz_name ? p_sys->aenc_cfg.psz_name : "any",
+                           (char *)&p_sys->aenc_cfg.i_codec );
         return VLC_EGENERIC;
     }
 
@@ -123,23 +124,24 @@ static int transcode_audio_encoder_open( sout_stream_t *p_stream, sout_stream_id
     return VLC_SUCCESS;
 }
 
-static int transcode_audio_encoder_configure( sout_stream_t *p_stream,
+static int transcode_audio_encoder_configure( vlc_object_t *p_obj,
+                                              const sout_encoder_config_t *p_cfg,
                                               const audio_format_t *p_dec_out,
                                               encoder_t *p_encoder )
 {
-    sout_stream_sys_t *p_sys = p_stream->p_sys;
+    VLC_UNUSED(p_obj);
     audio_format_t *p_enc_in = &p_encoder->fmt_in.audio;
     audio_format_t *p_enc_out = &p_encoder->fmt_out.audio;
 
     /* Complete destination format */
-    p_encoder->fmt_out.i_codec = p_sys->i_acodec;
-    p_encoder->fmt_out.audio.i_format = p_sys->i_acodec;
-    p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
-    p_enc_out->i_rate = p_sys->i_sample_rate ? p_sys->i_sample_rate
-                                             : p_dec_out->i_rate;
+    p_encoder->fmt_out.i_codec = p_cfg->i_codec;
+    p_encoder->fmt_out.audio.i_format = p_cfg->i_codec;
+    p_encoder->fmt_out.i_bitrate = p_cfg->audio.i_bitrate;
+    p_enc_out->i_rate = p_cfg->audio.i_sample_rate ? p_cfg->audio.i_sample_rate
+                                                   : p_dec_out->i_rate;
     p_enc_out->i_bitspersample = p_dec_out->i_bitspersample;
-    p_enc_out->i_channels = p_sys->i_channels ? p_sys->i_channels
-                                              : p_dec_out->i_channels;
+    p_enc_out->i_channels = p_cfg->audio.i_channels ? p_cfg->audio.i_channels
+                                                    : p_dec_out->i_channels;
     aout_FormatPrepare( p_enc_out );
     assert(p_enc_out->i_channels > 0);
     if( p_enc_out->i_channels >= ARRAY_SIZE(pi_channels_maps) )
@@ -155,7 +157,7 @@ static int transcode_audio_encoder_configure( sout_stream_t *p_stream,
     p_enc_in->i_physical_channels = p_enc_out->i_physical_channels;
     aout_FormatPrepare( p_enc_in );
 
-    p_encoder->p_cfg = p_sys->p_audio_cfg;
+    p_encoder->p_cfg = p_cfg->p_config_chain;
 
     /* Fix input format */
     p_enc_in->i_format = p_encoder->fmt_in.i_codec;
@@ -169,20 +171,18 @@ static int transcode_audio_encoder_configure( sout_stream_t *p_stream,
     return VLC_SUCCESS;
 }
 
-static int transcode_audio_encoder_test( sout_stream_t *p_stream,
-                                         const audio_format_t *p_dec_in,
+static int transcode_audio_encoder_test( vlc_object_t *p_obj,
                                          const audio_format_t *p_dec_out,
                                          vlc_fourcc_t i_codec_in,
-                                         config_chain_t *p_cfg,
+                                         const sout_encoder_config_t *p_cfg,
                                          const es_format_t *p_enc_fmtout,
                                          es_format_t *p_enc_wanted_in )
 {
-    sout_stream_sys_t *p_sys = p_stream->p_sys;
-    encoder_t *p_encoder = sout_EncoderCreate( p_stream );
+    encoder_t *p_encoder = sout_EncoderCreate( p_obj );
     if( !p_encoder )
         return VLC_EGENERIC;
 
-    p_encoder->p_cfg = p_cfg;
+    p_encoder->p_cfg = p_cfg->p_config_chain;
 
     es_format_Init( &p_encoder->fmt_in, AUDIO_ES, i_codec_in );
     p_encoder->fmt_in.audio = *p_dec_out;
@@ -191,34 +191,29 @@ static int transcode_audio_encoder_test( sout_stream_t *p_stream,
 
     audio_format_t *p_afmt_out = &p_encoder->fmt_out.audio;
 
+    if( transcode_audio_encoder_configure( p_obj, p_cfg, p_dec_out, p_encoder ) )
+    {
+        es_format_Clean( &p_encoder->fmt_in );
+        es_format_Clean( &p_encoder->fmt_out );
+        vlc_object_release( p_encoder );
+        return VLC_EGENERIC;
+    }
+
     p_encoder->fmt_in.audio.i_format = i_codec_in;
-    p_encoder->fmt_out.i_codec = p_afmt_out->i_format =  p_sys->i_acodec;
-    p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
 
-    p_afmt_out->i_rate = FIRSTVALID( p_sys->i_sample_rate, p_dec_out->i_rate, p_dec_in->i_rate );
-    p_afmt_out->i_channels = p_sys->i_channels ? p_sys->i_channels
-                                               : p_dec_out->i_channels;
     if( p_afmt_out->i_channels == 0 )
     {
         p_afmt_out->i_channels = 2;
         p_afmt_out->i_physical_channels = AOUT_CHANS_STEREO;
     }
-    else
-    {
-        if( p_afmt_out->i_physical_channels )
-            p_afmt_out->i_physical_channels = p_afmt_out->i_physical_channels;
-        else
-            p_afmt_out->i_physical_channels = p_dec_out->i_physical_channels;
-    }
-    aout_FormatPrepare( p_afmt_out );
 
-    module_t *p_module = module_need( p_encoder, "encoder", p_sys->psz_aenc, true );
+    module_t *p_module = module_need( p_encoder, "encoder", p_cfg->psz_name, true );
     if( !p_module )
     {
-        msg_Err( p_stream, "cannot find audio encoder (module:%s fourcc:%4.4s). "
+        msg_Err( p_obj, "cannot find audio encoder (module:%s fourcc:%4.4s). "
                            "Take a look few lines earlier to see possible reason.",
-                           p_sys->psz_aenc ? p_sys->psz_aenc : "any",
-                           (char *)&p_sys->i_acodec );
+                           p_cfg->psz_name ? p_cfg->psz_name : "any",
+                           (char *)&p_cfg->i_codec );
     }
     else
     {
@@ -321,10 +316,10 @@ static int transcode_audio_new( sout_stream_t *p_stream,
      * This should be enough to initialize the encoder for the first time (it
      * will be reloaded when all informations from the decoder are available).
      * */
-    if( transcode_audio_encoder_test( p_stream, &id->p_decoder->fmt_in.audio,
+    if( transcode_audio_encoder_test( VLC_OBJECT(p_stream),
                                                 &id->audio_dec_out,
                                                 id->p_decoder->fmt_out.i_codec,
-                                                p_sys->p_audio_cfg,
+                                                &p_sys->aenc_cfg,
                                                 &id->p_encoder->fmt_out,
                                                 &id->encoder_tested_fmt_in ) != VLC_SUCCESS )
     {
@@ -397,7 +392,8 @@ int transcode_audio_process( sout_stream_t *p_stream,
         {
             if( id->p_encoder->p_module == NULL )
             {
-                transcode_audio_encoder_configure( p_stream, &id->audio_dec_out, id->p_encoder );
+                transcode_audio_encoder_configure( VLC_OBJECT(p_stream), &p_sys->aenc_cfg,
+                                                   &id->audio_dec_out, id->p_encoder );
                 id->fmt_input_audio.i_rate = id->audio_dec_out.i_rate;
                 id->fmt_input_audio.i_physical_channels = id->audio_dec_out.i_physical_channels;
 
@@ -413,7 +409,9 @@ int transcode_audio_process( sout_stream_t *p_stream,
                 }
             }
 
-            if( transcode_audio_filters_init( p_stream, &id->audio_dec_out,
+            if( transcode_audio_filters_init( p_stream,
+                                              &p_sys->afilters_cfg,
+                                              &id->audio_dec_out,
                                               &id->p_encoder->fmt_in.audio,
                                               &id->p_af_chain ) )
             {
@@ -496,7 +494,7 @@ bool transcode_audio_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
 
     msg_Dbg( p_stream,
              "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
-             (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
+             (char*)&p_fmt->i_codec, (char*)&p_sys->aenc_cfg.i_codec );
 
     id->fifo.audio.first = NULL;
     id->fifo.audio.last = &id->fifo.audio.first;
diff --git a/modules/stream_out/transcode/transcode.c b/modules/stream_out/transcode/transcode.c
index 4cb2931e24..e2cf0cf17c 100644
--- a/modules/stream_out/transcode/transcode.c
+++ b/modules/stream_out/transcode/transcode.c
@@ -235,6 +235,56 @@ static void *Add( sout_stream_t *, const es_format_t * );
 static void  Del( sout_stream_t *, void * );
 static int   Send( sout_stream_t *, void *, block_t * );
 
+static void SetAudioEncoderConfig( sout_stream_t *p_stream, sout_encoder_config_t *p_cfg )
+{
+    char *psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "aenc" );
+    if( psz_string && *psz_string )
+    {
+        char *psz_next = config_ChainCreate( &p_cfg->psz_name,
+                                            &p_cfg->p_config_chain,
+                                            psz_string );
+        free( psz_next );
+    }
+    free( psz_string );
+
+    psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "acodec" );
+    p_cfg->i_codec = 0;
+    if( psz_string && *psz_string )
+    {
+        char fcc[5] = "    \0";
+        memcpy( fcc, psz_string, __MIN( strlen( psz_string ), 4 ) );
+        p_cfg->i_codec = vlc_fourcc_GetCodecFromString( AUDIO_ES, fcc );
+        msg_Dbg( p_stream, "Checking codec mapping for %s got %4.4s ",
+                            fcc, (char*)&p_cfg->i_codec);
+    }
+    free( psz_string );
+
+    p_cfg->audio.i_bitrate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ab" );
+    if( p_cfg->audio.i_bitrate < 4000 )
+        p_cfg->audio.i_bitrate *= 1000;
+
+    p_cfg->audio.i_sample_rate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "samplerate" );
+    p_cfg->audio.i_channels = var_GetInteger( p_stream, SOUT_CFG_PREFIX "channels" );
+
+    if( p_cfg->i_codec )
+    {
+        if( ( p_cfg->i_codec == VLC_CODEC_MP3 ||
+              p_cfg->i_codec == VLC_CODEC_MP2 ||
+              p_cfg->i_codec == VLC_CODEC_MPGA ) &&
+              p_cfg->audio.i_channels > 2 )
+        {
+            msg_Warn( p_stream, "%d channels invalid for mp2/mp3, forcing to 2",
+                      p_cfg->audio.i_channels );
+            p_cfg->audio.i_channels = 2;
+        }
+        msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
+                 (char *)&p_cfg->i_codec, p_cfg->audio.i_sample_rate,
+                 p_cfg->audio.i_channels, p_cfg->audio.i_bitrate / 1000 );
+    }
+
+    p_cfg->psz_lang = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "alang" );
+}
+
 static void SetVideoEncoderConfig( sout_stream_t *p_stream, sout_encoder_config_t *p_cfg )
 {
     char *psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "venc" );
@@ -304,59 +354,17 @@ static int Open( vlc_object_t *p_this )
                    p_stream->p_cfg );
 
     /* Audio transcoding parameters */
-    psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "aenc" );
-    p_sys->psz_aenc = NULL;
-    p_sys->p_audio_cfg = NULL;
-    if( psz_string && *psz_string )
-    {
-        char *psz_next;
-        psz_next = config_ChainCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
-                                       psz_string );
-        free( psz_next );
-    }
-    free( psz_string );
-
-    psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "acodec" );
-    p_sys->i_acodec = 0;
-    if( psz_string && *psz_string )
-    {
-        char fcc[5] = "    \0";
-        memcpy( fcc, psz_string, __MIN( strlen( psz_string ), 4 ) );
-        p_sys->i_acodec = vlc_fourcc_GetCodecFromString( AUDIO_ES, fcc );
-        msg_Dbg( p_stream, "Checking codec mapping for %s got %4.4s ", fcc, (char*)&p_sys->i_acodec);
-    }
-    free( psz_string );
+    sout_encoder_config_init( &p_sys->aenc_cfg );
+    SetAudioEncoderConfig( p_stream, &p_sys->aenc_cfg );
 
-    p_sys->psz_alang = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "alang" );
-
-    p_sys->i_abitrate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ab" );
-    if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
-
-    p_sys->i_sample_rate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "samplerate" );
-
-    p_sys->i_channels = var_GetInteger( p_stream, SOUT_CFG_PREFIX "channels" );
-
-    if( p_sys->i_acodec )
-    {
-        if( ( p_sys->i_acodec == VLC_CODEC_MP3 ||
-              p_sys->i_acodec == VLC_CODEC_MP2 ||
-              p_sys->i_acodec == VLC_CODEC_MPGA ) && p_sys->i_channels > 2 )
-        {
-            msg_Warn( p_stream, "%d channels invalid for mp2/mp3, forcing to 2",
-                      p_sys->i_channels );
-            p_sys->i_channels = 2;
-        }
-        msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
-                 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
-                 p_sys->i_channels, p_sys->i_abitrate / 1000 );
-    }
+    /* Audio Filter Parameters */
+    sout_filters_config_init( &p_sys->afilters_cfg );
 
     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "afilter" );
     if( psz_string && *psz_string )
-        p_sys->psz_af = strdup( psz_string );
+        p_sys->afilters_cfg.psz_filters = psz_string;
     else
-        p_sys->psz_af = NULL;
-    free( psz_string );
+        free( psz_string );
 
     /* Video transcoding parameters */
     sout_encoder_config_init( &p_sys->venc_cfg );
@@ -457,11 +465,8 @@ static void Close( vlc_object_t * p_this )
     sout_encoder_config_clean( &p_sys->venc_cfg );
     sout_filters_config_clean( &p_sys->vfilters_cfg );
 
-    free( p_sys->psz_af );
-
-    config_ChainDestroy( p_sys->p_audio_cfg );
-    free( p_sys->psz_aenc );
-    free( p_sys->psz_alang );
+    sout_encoder_config_clean( &p_sys->aenc_cfg );
+    sout_filters_config_clean( &p_sys->afilters_cfg );
 
     config_ChainDestroy( p_sys->p_spu_cfg );
     free( p_sys->psz_senc );
@@ -533,14 +538,14 @@ static void *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
 
-    if( p_sys->psz_alang )
-        id->p_encoder->fmt_out.psz_language = strdup( p_sys->psz_alang );
+    if( p_sys->aenc_cfg.psz_lang )
+        id->p_encoder->fmt_out.psz_language = strdup( p_sys->aenc_cfg.psz_lang );
     else if( p_fmt->psz_language )
         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
 
     bool success;
 
-    if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec )
+    if( p_fmt->i_cat == AUDIO_ES && p_sys->aenc_cfg.i_codec )
         success = transcode_audio_add(p_stream, p_fmt, id);
     else if( p_fmt->i_cat == VIDEO_ES && p_sys->venc_cfg.i_codec )
         success = transcode_video_add(p_stream, p_fmt, id);
diff --git a/modules/stream_out/transcode/transcode.h b/modules/stream_out/transcode/transcode.h
index 4c0c67e462..cc62dfaa8a 100644
--- a/modules/stream_out/transcode/transcode.h
+++ b/modules/stream_out/transcode/transcode.h
@@ -42,6 +42,7 @@ typedef struct
 {
     vlc_fourcc_t i_codec; /* (0 if not transcode) */
     char         *psz_name;
+    char         *psz_lang;
     config_chain_t *p_config_chain;
     union
     {
@@ -60,6 +61,12 @@ typedef struct
                 uint32_t     pool_size;
             } threads;
         } video;
+        struct
+        {
+            unsigned int    i_bitrate;
+            uint32_t        i_sample_rate;
+            uint32_t        i_channels;
+        } audio;
     };
 } sout_encoder_config_t;
 
@@ -73,6 +80,7 @@ static inline
 void sout_encoder_config_clean( sout_encoder_config_t *p_cfg )
 {
     free( p_cfg->psz_name );
+    free( p_cfg->psz_lang );
     config_ChainDestroy( p_cfg->p_config_chain );
 }
 
@@ -81,15 +89,8 @@ typedef struct
     sout_stream_id_sys_t *id_video;
 
     /* Audio */
-    vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
-    char            *psz_aenc;
-    char            *psz_alang;
-    config_chain_t  *p_audio_cfg;
-    uint32_t        i_sample_rate;
-    uint32_t        i_channels;
-    int             i_abitrate;
-
-    char            *psz_af;
+    sout_encoder_config_t aenc_cfg;
+    sout_filters_config_t afilters_cfg;
 
     /* Video */
     sout_encoder_config_t venc_cfg;



More information about the vlc-commits mailing list