]> git.sesse.net Git - vlc/blobdiff - modules/mux/mp4.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / mux / mp4.c
index 59a436716f6f3c96aad95e1716efffedd57a5d6e..a258adf4cd3bd326968e05ad3972a3c80f3966b4 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>
-#include <string.h>
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <vlc/sout.h>
+#include <vlc_sout.h>
+#include <vlc_block.h>
+#include <vlc_codecs.h>
 
 #ifdef HAVE_TIME_H
 #include <time.h>
@@ -114,18 +113,6 @@ typedef struct
     uint64_t i_stco_pos;
     vlc_bool_t b_stco64;
 
-    /* for h264 */
-    struct
-    {
-        int     i_profile;
-        int     i_level;
-
-        int     i_sps;
-        uint8_t *sps;
-        int     i_pps;
-        uint8_t *pps;
-    } avc;
-
     /* for spu */
     int64_t i_last_dts;
 
@@ -163,15 +150,15 @@ static void bo_add_16be ( bo_t *, uint16_t );
 static void bo_add_24be ( bo_t *, uint32_t );
 static void bo_add_32be ( bo_t *, uint32_t );
 static void bo_add_64be ( bo_t *, uint64_t );
-static void bo_add_fourcc(bo_t *, char * );
+static void bo_add_fourcc(bo_t *, const char * );
 static void bo_add_bo   ( bo_t *, bo_t * );
 static void bo_add_mem  ( bo_t *, int , uint8_t * );
 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
 
 static void bo_fix_32be ( bo_t *, int , uint32_t );
 
-static bo_t *box_new     ( char *fcc );
-static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
+static bo_t *box_new     ( const char *fcc );
+static bo_t *box_full_new( const char *fcc, uint8_t v, uint32_t f );
 static void  box_fix     ( bo_t *box );
 static void  box_free    ( bo_t *box );
 static void  box_gather  ( bo_t *box, bo_t *box2 );
@@ -183,7 +170,7 @@ static block_t *bo_to_sout( sout_instance_t *p_sout,  bo_t *box );
 static bo_t *GetMoovBox( sout_mux_t *p_mux );
 
 static block_t *ConvertSUBT( sout_mux_t *, mp4_stream_t *, block_t *);
-static void ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
+static block_t *ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
 
 /*****************************************************************************
  * Open:
@@ -195,7 +182,7 @@ static int Open( vlc_object_t *p_this )
     bo_t            *box;
 
     msg_Dbg( p_mux, "Mp4 muxer opend" );
-    sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
+    config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
 
     p_mux->pf_control   = Control;
     p_mux->pf_addstream = AddStream;
@@ -363,8 +350,6 @@ static void Close( vlc_object_t * p_this )
         mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
 
         es_format_Clean( &p_stream->fmt );
-        if( p_stream->avc.i_sps ) free( p_stream->avc.sps );
-        if( p_stream->avc.i_pps ) free( p_stream->avc.pps );
         free( p_stream->entry );
         free( p_stream );
     }
@@ -419,6 +404,8 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
         case VLC_FOURCC( 'h', '2', '6', '4' ):
         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
+        case VLC_FOURCC( 'Y', 'V', '1', '2' ):
+        case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
             break;
         case VLC_FOURCC( 's', 'u', 'b', 't' ):
             msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
@@ -439,12 +426,6 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
         calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
     p_stream->i_dts_start   = 0;
     p_stream->i_duration    = 0;
-    p_stream->avc.i_profile = 77;
-    p_stream->avc.i_level   = 51;
-    p_stream->avc.i_sps     = 0;
-    p_stream->avc.sps       = NULL;
-    p_stream->avc.i_pps     = 0;
-    p_stream->avc.pps       = NULL;
 
     p_input->p_sys          = p_stream;
 
@@ -525,15 +506,17 @@ static int Mux( sout_mux_t *p_mux )
         p_input  = p_mux->pp_inputs[i_stream];
         p_stream = (mp4_stream_t*)p_input->p_sys;
 
+again:
         p_data  = block_FifoGet( p_input->p_fifo );
         if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
         {
-            ConvertAVC1( p_mux, p_stream, p_data );
+            p_data = ConvertAVC1( p_mux, p_stream, p_data );
         }
         else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
         {
             p_data = ConvertSUBT( p_mux, p_stream, p_data );
         }
+        if( p_data == NULL ) goto again;
 
         if( p_stream->fmt.i_cat != SPU_ES )
         {
@@ -680,7 +663,7 @@ static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_blo
     return p_block;
 }
 
-static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
+static block_t *ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
 {
     uint8_t *last = p_block->p_buffer;  /* Assume it starts with 0x00000001 */
     uint8_t *dat  = &p_block->p_buffer[4];
@@ -714,29 +697,17 @@ static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
         last[2] = ( i_size >>  8 )&0xff;
         last[3] = ( i_size       )&0xff;
 
-        if( (last[4]&0x1f) == 7 && tk->avc.i_sps <= 0 )  /* SPS */
+        /* Skip blocks with SPS/PPS */
+        if( (last[4]&0x1f) == 7 || (last[4]&0x1f) == 8 )
         {
-            tk->avc.i_sps = i_size;
-            tk->avc.sps = malloc( i_size );
-            memcpy( tk->avc.sps, &last[4], i_size );
-
-            tk->avc.i_profile = tk->avc.sps[1];
-            tk->avc.i_level   = tk->avc.sps[3];
+            ; // FIXME Find a way to skip dat without frelling everything
         }
-        else if( (last[4]&0x1f) == 8 && tk->avc.i_pps <= 0 )   /* PPS */
-        {
-            tk->avc.i_pps = i_size;
-            tk->avc.pps = malloc( i_size );
-            memcpy( tk->avc.pps, &last[4], i_size );
-        }
-
         last = dat;
-
         dat += 4;
     }
+    return p_block;
 }
 
-
 static int GetDescrLength( int i_size )
 {
     if( i_size < 0x00000080 )
@@ -924,28 +895,75 @@ static bo_t *GetD263Tag( mp4_stream_t *p_stream )
 
 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
 {
-    bo_t *avcC;
+    bo_t    *avcC = NULL;
+    uint8_t *p_sps = NULL;
+    uint8_t *p_pps = NULL;
+    int     i_sps_size = 0;
+    int     i_pps_size = 0;
+
+    if( p_stream->fmt.i_extra > 0 )
+    {
+        /* FIXME: take into account multiple sps/pps */
+        uint8_t *p_buffer = p_stream->fmt.p_extra;
+        int     i_buffer = p_stream->fmt.i_extra;
 
+        while( i_buffer > 4 &&
+            p_buffer[0] == 0 && p_buffer[1] == 0 &&
+            p_buffer[2] == 0 && p_buffer[3] == 1 )
+        {
+            const int i_nal_type = p_buffer[4]&0x1f;
+            int i_offset    = 1;
+            int i_size      = 0;
+            int i_startcode = 0;
+            //msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
+            for( i_offset = 1; i_offset+3 < i_buffer ; i_offset++)
+            {
+                if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 &&
+                    p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
+                {
+                    /* we found another startcode */
+                    i_startcode = i_offset;
+                    break;
+                }
+            }
+            i_size = i_startcode ? i_startcode : i_buffer;
+            if( i_nal_type == 7 )
+            {
+                p_sps = &p_buffer[4];
+                i_sps_size = i_size - 4;
+            }
+            if( i_nal_type == 8 )
+            {
+                p_pps = &p_buffer[4];
+                i_pps_size = i_size - 4;
+            }
+            i_buffer -= i_size;
+            p_buffer += i_size;
+        }
+    }
     /* FIXME use better value */
     avcC = box_new( "avcC" );
     bo_add_8( avcC, 1 );      /* configuration version */
-    bo_add_8( avcC, p_stream->avc.i_profile );
-    bo_add_8( avcC, p_stream->avc.i_profile );     /* profile compatible ??? */
-    bo_add_8( avcC, p_stream->avc.i_level );       /* level, 5.1 */
+    bo_add_8( avcC, i_sps_size ? p_sps[1] : 77 );
+    bo_add_8( avcC, i_sps_size ? p_sps[2] : 64 );
+    bo_add_8( avcC, i_sps_size ? p_sps[3] : 30 );       /* level, 5.1 */
     bo_add_8( avcC, 0xff );   /* 0b11111100 | lengthsize = 0x11 */
 
-    bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) );   /* 0b11100000 | sps_count */
-    if( p_stream->avc.i_sps > 0 )
+    bo_add_8( avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0) );   /* 0b11100000 | sps_count */
+    if( i_sps_size > 0 )
     {
-        bo_add_16be( avcC, p_stream->avc.i_sps );
-        bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
+        bo_add_16be( avcC, i_sps_size );
+        bo_add_mem( avcC, i_sps_size, p_sps );
     }
 
-    bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) );   /* pps_count */
-    if( p_stream->avc.i_pps > 0 )
+    bo_add_8( avcC, (i_pps_size > 0 ? 1 : 0) );   /* pps_count */
+    if( i_pps_size > 0 )
     {
-        bo_add_16be( avcC, p_stream->avc.i_pps );
-        bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
+        bo_add_16be( avcC, i_pps_size );
+        bo_add_mem( avcC, i_pps_size, p_pps );
     }
     box_fix( avcC );
 
@@ -1031,38 +1049,27 @@ static bo_t *GetUdtaTag( sout_mux_t *p_mux )
     /* Misc atoms */
     if( p_meta )
     {
-        int i;
-        for( i = 0; i < p_meta->i_meta; i++ )
-        {
-            bo_t *box = NULL;
-
-            if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
-                box = box_new( "\251nam" );
-            else if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
-                box = box_new( "\251aut" );
-            else if( !strcmp( p_meta->name[i], VLC_META_ARTIST ) )
-                box = box_new( "\251ART" );
-            else if( !strcmp( p_meta->name[i], VLC_META_GENRE ) )
-                box = box_new( "\251gen" );
-            else if( !strcmp( p_meta->name[i], VLC_META_COPYRIGHT ) )
-                box = box_new( "\251cpy" );
-            else if( !strcmp( p_meta->name[i], VLC_META_DESCRIPTION ) )
-                box = box_new( "\251des" );
-            else if( !strcmp( p_meta->name[i], VLC_META_DATE ) )
-                box = box_new( "\251day" );
-            else if( !strcmp( p_meta->name[i], VLC_META_URL ) )
-                box = box_new( "\251url" );
-
-            if( box )
-            {
-                bo_add_16be( box, strlen( p_meta->value[i] ) );
-                bo_add_16be( box, 0 );
-                bo_add_mem( box, strlen( p_meta->value[i] ),
-                            (uint8_t*)(p_meta->value[i]) );
-                box_fix( box );
-                box_gather( udta, box );
-            }
-        }
+#define ADD_META_BOX( type, box_string ) { \
+        bo_t *box = NULL;  \
+        if( vlc_meta_Get( p_meta, vlc_meta_##type ) ) box = box_new( "\251" box_string ); \
+        if( box ) \
+        { \
+            bo_add_16be( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) )); \
+            bo_add_16be( box, 0 ); \
+            bo_add_mem( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) ), \
+                        (uint8_t*)(vlc_meta_Get( p_meta, vlc_meta_##type ) ) ); \
+            box_fix( box ); \
+            box_gather( udta, box ); \
+        } }
+
+        ADD_META_BOX( Title, "nam" );
+        ADD_META_BOX( Artist, "ART" );
+        ADD_META_BOX( Genre, "gen" );
+        ADD_META_BOX( Copyright, "cpy" );
+        ADD_META_BOX( Description, "des" );
+        ADD_META_BOX( Date, "day" );
+        ADD_META_BOX( URL, "url" );
+#undef ADD_META_BOX
     }
 
     box_fix( udta );
@@ -1207,6 +1214,14 @@ static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
         memcpy( fcc, "avc1", 4 );
         break;
 
+    case VLC_FOURCC('Y','V','1','2'):
+        memcpy( fcc, "yv12", 4 );
+        break;
+
+    case VLC_FOURCC('Y','U','Y','2'):
+        memcpy( fcc, "yuy2", 4 );
+        break;
+
     default:
         memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
         break;
@@ -1511,7 +1526,7 @@ static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
     return stbl;
 }
 
-static int64_t get_timestamp();
+static int64_t get_timestamp(void);
 
 static uint32_t mvhd_matrix[9] =
     { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
@@ -1986,7 +2001,7 @@ static void bo_add_64be( bo_t *p_bo, uint64_t i )
     bo_add_32be( p_bo, i &0xffffffff );
 }
 
-static void bo_add_fourcc( bo_t *p_bo, char *fcc )
+static void bo_add_fourcc( bo_t *p_bo, const char *fcc )
 {
     bo_add_8( p_bo, fcc[0] );
     bo_add_8( p_bo, fcc[1] );
@@ -2012,9 +2027,9 @@ static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
     i_length = i_size;
     vals[3] = (unsigned char)(i_length & 0x7f);
     i_length >>= 7;
-    vals[2] = (unsigned char)((i_length & 0x7f) | 0x80); 
+    vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
     i_length >>= 7;
-    vals[1] = (unsigned char)((i_length & 0x7f) | 0x80); 
+    vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
     i_length >>= 7;
     vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
 
@@ -2054,7 +2069,7 @@ static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
     }
 }
 
-static bo_t * box_new( char *fcc )
+static bo_t * box_new( const char *fcc )
 {
     bo_t *box;
 
@@ -2069,7 +2084,7 @@ static bo_t * box_new( char *fcc )
     return box;
 }
 
-static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
+static bo_t * box_full_new( const char *fcc, uint8_t v, uint32_t f )
 {
     bo_t *box;
 
@@ -2135,7 +2150,7 @@ static void box_send( sout_mux_t *p_mux,  bo_t *box )
     sout_AccessOutWrite( p_mux->p_access, p_buf );
 }
 
-static int64_t get_timestamp()
+static int64_t get_timestamp(void)
 {
     int64_t i_timestamp = 0;