[vlc-devel] [PATCH] x264: write first SEI NAL into first picture

Rafaël Carré rafael.carre at gmail.com
Thu Nov 4 17:30:09 CET 2010


It contains the x264 version info and options used for encoding
If we let it in extradata it will not be present in the final file

libavcodec does that already, and this makes it easier to compare x264
options used by VLC and FFmpeg.

Comparing FFmpeg and VLC with the exact same set of options might help
finding why mpeg (at least mpeg2/mpeg4/flv) encoding is 5 times slower
with vlc than with ffmpeg, supposedly for the same set of options.

---
2nd version of this patch fixes a double free
---
 modules/codec/x264.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/modules/codec/x264.c b/modules/codec/x264.c
index c34b593..126d686 100644
--- a/modules/codec/x264.c
+++ b/modules/codec/x264.c
@@ -712,6 +712,8 @@ struct encoder_sys_t
     mtime_t         i_initial_delay;
 
     char            *psz_stat_name;
+    int             i_sei_size;
+    uint8_t         *p_sei;
 };
 
 #ifdef PTW32_STATIC_LIB
@@ -753,6 +755,8 @@ static int  Open ( vlc_object_t *p_this )
         return VLC_ENOMEM;
     p_sys->i_initial_delay = 0;
     p_sys->psz_stat_name = NULL;
+    p_sys->i_sei_size = 0;
+    p_sys->p_sei = 0;
 
     x264_param_default( &p_sys->param );
     char *psz_preset = var_GetString( p_enc, SOUT_CFG_PREFIX  "preset" );
@@ -1270,8 +1274,23 @@ static int  Open ( vlc_object_t *p_this )
     uint8_t *p_tmp = p_enc->fmt_out.p_extra;
     for( i = 0; i < i_nal; i++ )
     {
-        memcpy( p_tmp, nal[i].p_payload, nal[i].i_payload );
-        p_tmp += nal[i].i_payload;
+        if( nal[i].i_type != NAL_SEI )
+        {
+            memcpy( p_tmp, nal[i].p_payload, nal[i].i_payload );
+            p_tmp += nal[i].i_payload;
+        }
+        else
+        {
+            p_enc->fmt_out.i_extra -= nal[i].i_payload;
+            p_sys->i_sei_size = nal[i].i_payload;
+            p_sys->p_sei = malloc( p_sys->i_sei_size );
+            if( !p_sys->p_sei )
+            {
+                Close( VLC_OBJECT(p_enc) );
+                return VLC_ENOMEM;
+            }
+            memcpy( p_sys->p_sei, nal[i].p_payload, nal[i].i_payload );
+        }
     }
 
     return VLC_SUCCESS;
@@ -1324,6 +1343,9 @@ static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
     for( i = 0, i_out = 0; i < i_nal; i++ )
         i_out += nal[i].i_payload;
 
+    if( p_sys->i_sei_size )
+        i_out += p_sys->i_sei_size;
+
     p_block = block_New( p_enc, i_out );
     if( !p_block ) return NULL;
 
@@ -1333,6 +1355,14 @@ static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
         memcpy( p_block->p_buffer + i_out, nal[i].p_payload, nal[i].i_payload );
         i_out += nal[i].i_payload;
     }
+    if( p_sys->i_sei_size )
+    {
+        memcpy( p_block->p_buffer, p_sys->p_sei, p_sys->i_sei_size );
+        i_out += p_sys->i_sei_size;
+        p_sys->i_sei_size = 0;
+        free( p_sys->p_sei );
+        p_sys->p_sei = NULL;
+    }
 
     if( pic.b_keyframe )
         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
@@ -1364,6 +1394,7 @@ static void Close( vlc_object_t *p_this )
     encoder_sys_t *p_sys = p_enc->p_sys;
 
     free( p_sys->psz_stat_name );
+    free( p_sys->p_sei );
 
     msg_Dbg( p_enc, "framecount still in libx264 buffer: %d", x264_encoder_delayed_frames( p_sys->h ) );
 
-- 
1.7.3.2





More information about the vlc-devel mailing list