[vlc-devel] [PATCH 04/26] sout:encoder: use a union for the encoder callback

Steve Lhomme robux4 at ycbcr.xyz
Mon Jan 18 08:32:43 UTC 2021


There is no encoder that can encode video or audio or subtitles at the same
time and it's not going to happen. Merging video and SPU should be blended
first.
---
 include/vlc_codec.h             |  8 +++++---
 modules/codec/avcodec/avcodec.c |  2 +-
 modules/codec/avcodec/avcodec.h |  3 +++
 modules/codec/avcodec/encoder.c | 24 +++++++++++++++++++-----
 modules/codec/x264.c            |  1 -
 modules/codec/x265.c            |  1 -
 6 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/include/vlc_codec.h b/include/vlc_codec.h
index 7ab479ae32d..7b5f4042d6e 100644
--- a/include/vlc_codec.h
+++ b/include/vlc_codec.h
@@ -253,9 +253,11 @@ struct encoder_t
     /* Properties of the output of the encoder */
     es_format_t         fmt_out;
 
-    block_t *           ( * pf_encode_video )( encoder_t *, picture_t * );
-    block_t *           ( * pf_encode_audio )( encoder_t *, block_t * );
-    block_t *           ( * pf_encode_sub )( encoder_t *, subpicture_t * );
+    union {
+        block_t *           ( * pf_encode_video )( encoder_t *, picture_t * );
+        block_t *           ( * pf_encode_audio )( encoder_t *, block_t * );
+        block_t *           ( * pf_encode_sub )( encoder_t *, subpicture_t * );
+    };
 
     /* Common encoder options */
     int i_threads;               /* Number of threads to use during encoding */
diff --git a/modules/codec/avcodec/avcodec.c b/modules/codec/avcodec/avcodec.c
index 5c4bf688b99..a0e0f52f1f1 100644
--- a/modules/codec/avcodec/avcodec.c
+++ b/modules/codec/avcodec/avcodec.c
@@ -149,7 +149,7 @@ vlc_module_begin ()
     add_submodule()
         set_capability( "audio encoder", 100 )
         add_shortcut( "ffmpeg" )
-        set_callbacks( InitVideoEnc, EndVideoEnc )
+        set_callbacks( InitAudioEnc, EndVideoEnc )
 
     /* removed in 2.1.0 */
     add_obsolete_string( "sout-ffmpeg-codec" )
diff --git a/modules/codec/avcodec/avcodec.h b/modules/codec/avcodec/avcodec.h
index 184ceb11d77..3aeace5d639 100644
--- a/modules/codec/avcodec/avcodec.h
+++ b/modules/codec/avcodec/avcodec.h
@@ -41,6 +41,9 @@ void EndVideoDec( vlc_object_t * );
 int InitAudioDec( vlc_object_t * );
 void EndAudioDec( vlc_object_t * );
 
+/* Audio encoder module */
+int  InitAudioEnc ( vlc_object_t * );
+
 /* Subtitle Decoder */
 int InitSubtitleDec( vlc_object_t * );
 void EndSubtitleDec( vlc_object_t * );
diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c
index 6636023b998..2034e1df0dd 100644
--- a/modules/codec/avcodec/encoder.c
+++ b/modules/codec/avcodec/encoder.c
@@ -288,7 +288,7 @@ static void add_av_option_float( encoder_t *p_enc, AVDictionary** pp_dict, const
         msg_Warn( p_enc, "Failed to set encoder option %s", psz_name );
 }
 
-int InitVideoEnc( vlc_object_t *p_this )
+static int InitEncoder( vlc_object_t *p_this )
 {
     encoder_t *p_enc = (encoder_t *)p_this;
     encoder_sys_t *p_sys;
@@ -1046,10 +1046,6 @@ errmsg:
     }
     msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
 
-    p_enc->pf_encode_video = EncodeVideo;
-    p_enc->pf_encode_audio = EncodeAudio;
-
-
     return VLC_SUCCESS;
 error:
     free( p_enc->fmt_out.p_extra );
@@ -1060,6 +1056,24 @@ error:
     return VLC_ENOMEM;
 }
 
+int InitVideoEnc( vlc_object_t *p_this )
+{
+    encoder_t *p_enc = (encoder_t *)p_this;
+    int res = InitEncoder( p_this );
+    if (res == VLC_SUCCESS)
+        p_enc->pf_encode_video = EncodeVideo;
+    return res;
+}
+
+int InitAudioEnc( vlc_object_t *p_this )
+{
+    encoder_t *p_enc = (encoder_t *)p_this;
+    int res = InitEncoder( p_this );
+    if (res == VLC_SUCCESS)
+        p_enc->pf_encode_audio = EncodeAudio;
+    return res;
+}
+
 typedef struct
 {
     block_t self;
diff --git a/modules/codec/x264.c b/modules/codec/x264.c
index c18ecb31354..a509ffa672a 100644
--- a/modules/codec/x264.c
+++ b/modules/codec/x264.c
@@ -865,7 +865,6 @@ static int  Open ( vlc_object_t *p_this )
     }
 
     p_enc->pf_encode_video = Encode;
-    p_enc->pf_encode_audio = NULL;
     p_sys->psz_stat_name = NULL;
     p_sys->i_sei_size = 0;
     p_sys->p_sei = NULL;
diff --git a/modules/codec/x265.c b/modules/codec/x265.c
index 7a39797fb80..83854e0e6e3 100644
--- a/modules/codec/x265.c
+++ b/modules/codec/x265.c
@@ -228,7 +228,6 @@ static int  Open (vlc_object_t *p_this)
     p_sys->initial_date = VLC_TICK_INVALID;
 
     p_enc->pf_encode_video = Encode;
-    p_enc->pf_encode_audio = NULL;
 
     return VLC_SUCCESS;
 }
-- 
2.29.2



More information about the vlc-devel mailing list