]> git.sesse.net Git - vlc/blobdiff - modules/mux/ogg.c
* all: do not export input_NullPacket
[vlc] / modules / mux / ogg.c
index e5aac177def01e76e67941c1636b9defcf811626..28d648b2edbebec641603eebe5a86edf523b1624 100644 (file)
@@ -2,7 +2,7 @@
  * ogg.c: ogg muxer module for vlc
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: ogg.c,v 1.14 2003/09/29 22:37:36 gbazin Exp $
+ * $Id: ogg.c,v 1.23 2003/11/24 00:01:42 gbazin Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Gildas Bazin <gbazin@netcourrier.com>
  *****************************************************************************/
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+
+#ifdef HAVE_TIME_H
+#   include <time.h>
+#endif
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
@@ -110,13 +113,17 @@ typedef struct
 
     int32_t i_buffer_size;
     int16_t i_bits_per_sample;
-    int16_t i_padding_0;            // hum hum
+
+    int16_t i_padding_0; /* Because the original is using MSVC packing style */
+
     union
     {
         oggds_header_video_t video;
         oggds_header_audio_t audio;
     } header;
 
+    int32_t i_padding_1; /* Because the original is using MSVC packing style */
+
 } oggds_header_t;
 
 /* Helper writer functions */
@@ -207,6 +214,7 @@ typedef struct
     mtime_t i_length;
     int     i_packet_no;
     int     i_serial_no;
+    int     i_keyframe_granule_shift; /* Theora only */
     ogg_stream_state os;
 
     oggds_header_t oggds_header;
@@ -221,6 +229,7 @@ struct sout_mux_sys_t
     int     i_streams;
 
     mtime_t i_start_dts;
+    int     i_next_serial_no;
 
     /* number of logical streams pending to be added */
     int i_add_streams;
@@ -257,6 +266,12 @@ static int Open( vlc_object_t *p_this )
     p_mux->pf_mux       = Mux;
     p_mux->i_preheader  = 1;
 
+    /* First serial number is random.
+     * (Done like this because on win32 you need to seed the random number
+     *  generator once per thread). */
+    srand( (unsigned int)time( NULL ) );
+    p_sys->i_next_serial_no = rand();
+
     return VLC_SUCCESS;
 }
 
@@ -324,21 +339,23 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
 
     p_stream->i_cat       = p_input->p_fmt->i_cat;
-    p_stream->i_fourcc    = p_input->p_fmt->i_fourcc;
-    p_stream->i_serial_no = rand();
+    p_stream->i_fourcc    = p_input->p_fmt->i_codec;
+    p_stream->i_serial_no = p_sys->i_next_serial_no++;
     p_stream->i_packet_no = 0;
 
     p_stream->i_sout_headers = 0;
 
+    memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
     p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
     switch( p_input->p_fmt->i_cat )
     {
     case VIDEO_ES:
         switch( p_stream->i_fourcc )
         {
+        case VLC_FOURCC( 'm', 'p','g', 'v' ):
         case VLC_FOURCC( 'm', 'p','4', 'v' ):
         case VLC_FOURCC( 'D', 'I','V', '3' ):
-            memcpy( p_stream->oggds_header.stream_type, "video    ", 8 );
+            memcpy( p_stream->oggds_header.stream_type, "video", 5 );
             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
             {
                 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
@@ -347,18 +364,22 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             {
                 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
             }
+            else
+            {
+                memcpy(p_stream->oggds_header.sub_type,&p_stream->i_fourcc,4);
+            }
             SetDWLE( &p_stream->oggds_header.i_size,
                      sizeof( oggds_header_t ) - 1);
             SetQWLE( &p_stream->oggds_header.i_time_unit,
                      I64C(10000000)/(int64_t)25 );  // FIXME (25fps)
             SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
-            SetDWLE( &p_stream->oggds_header.i_default_len, 0 );      /* ??? */
+            SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
             SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
             SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
             SetDWLE( &p_stream->oggds_header.header.video.i_width,
-                     p_input->p_fmt->i_width );
+                     p_input->p_fmt->video.i_width );
             SetDWLE( &p_stream->oggds_header.header.video.i_height,
-                     p_input->p_fmt->i_height );
+                     p_input->p_fmt->video.i_height );
             msg_Dbg( p_mux, "mp4v/div3 stream" );
             break;
 
@@ -377,7 +398,7 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
         {
         case VLC_FOURCC( 'm', 'p','g', 'a' ):
         case VLC_FOURCC( 'a', '5','2', ' ' ):
-            memcpy( p_stream->oggds_header.stream_type, "audio    ", 8 );
+            memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
             {
                 memcpy( p_stream->oggds_header.sub_type, "55  ", 4 );
@@ -389,15 +410,15 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             SetDWLE( &p_stream->oggds_header.i_size,
                      sizeof( oggds_header_t ) - 1);
             SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
-            SetDWLE( &p_stream->oggds_header.i_default_len, 0 /* not used */ );
+            SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
             SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
             SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
-                     p_input->p_fmt->i_sample_rate );
+                     p_input->p_fmt->audio.i_rate );
             SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
             SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
-                     p_input->p_fmt->i_channels );
+                     p_input->p_fmt->audio.i_channels );
             SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
-                     p_input->p_fmt->i_block_align );
+                     p_input->p_fmt->audio.i_blockalign );
             SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
             msg_Dbg( p_mux, "mpga/a52 stream" );
             break;
@@ -406,6 +427,14 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             msg_Dbg( p_mux, "vorbis stream" );
             break;
 
+        case VLC_FOURCC( 's', 'p', 'x', ' ' ):
+            msg_Dbg( p_mux, "speex stream" );
+            break;
+
+        case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
+            msg_Dbg( p_mux, "flac stream" );
+            break;
+
         default:
             FREE( p_input->p_sys );
             return( VLC_EGENERIC );
@@ -416,7 +445,7 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
         switch( p_stream->i_fourcc )
         {
         case VLC_FOURCC( 's', 'u','b', 't' ):
-            memcpy( p_stream->oggds_header.stream_type, "text     ", 8 );
+            memcpy( p_stream->oggds_header.stream_type, "text", 4 );
             msg_Dbg( p_mux, "subtitles stream" );
             break;
 
@@ -579,12 +608,13 @@ static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
         p_stream->i_packet_no = 0;
 
         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
+            p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
         {
             /* Special case, headers are already there in the
              * incoming stream or we backed them up earlier */
 
-            /* first packet in order: vorbis/theora info */
+            /* first packet in order: vorbis/speex/theora info */
             if( !p_stream->i_sout_headers )
             {
                 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
@@ -601,6 +631,33 @@ static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
             }
             p_og = sout_BufferDuplicate( p_mux->p_sout,
                                          p_stream->pp_sout_headers[0] );
+
+            /* Get keyframe_granule_shift for theora granulepos calculation */
+            if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
+            {
+                int i_keyframe_frequency_force = 1 << (op.packet[36] >> 3);
+
+                /* granule_shift = i_log( frequency_force -1 ) */
+                p_stream->i_keyframe_granule_shift = 0;
+                i_keyframe_frequency_force--;
+                while( i_keyframe_frequency_force )
+                {
+                    p_stream->i_keyframe_granule_shift++;
+                    i_keyframe_frequency_force >>= 1;
+                }
+            }
+        }
+        else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
+        {
+            /* flac stream marker (yeah, only that in the 1st packet) */
+            op.packet = "fLaC";
+            op.bytes  = 4;
+            op.b_o_s  = 1;
+            op.e_o_s  = 0;
+            op.granulepos = 0;
+            op.packetno = p_stream->i_packet_no++;
+            ogg_stream_packetin( &p_stream->os, &op );
+            p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
         }
         else
         {
@@ -624,6 +681,7 @@ static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
 
         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
+            p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
         {
             /* Special case, headers are already there in the incoming stream.
@@ -652,7 +710,7 @@ static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
                 sout_BufferChain( &p_hdr, p_og );
             }
         }
-        else
+        else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
         {
             uint8_t com[128];
             int     i_com;
@@ -670,6 +728,31 @@ static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
             sout_BufferChain( &p_hdr, p_og );
         }
+
+        /* Special case for mp4v and flac */
+        if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
+              p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
+            p_mux->pp_inputs[i]->p_fmt->i_extra )
+        {
+            /* Send a packet with the VOL data for mp4v
+             * or STREAMINFO for flac */
+            msg_Dbg( p_mux, "writing extra data" );
+            op.bytes  = p_mux->pp_inputs[i]->p_fmt->i_extra;
+            op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
+            if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
+            {
+                /* Skip the flac stream marker */
+                ((uint8_t *)op.bytes) -= 4;
+                ((uint8_t *)op.packet) += 4;
+            }
+            op.b_o_s  = 0;
+            op.e_o_s  = 0;
+            op.granulepos = 0;
+            op.packetno = p_stream->i_packet_no++;
+            ogg_stream_packetin( &p_stream->os, &op );
+            p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
+            sout_BufferChain( &p_hdr, p_og );
+        }
     }
 
     /* set HEADER flag */
@@ -829,6 +912,7 @@ static int Mux( sout_mux_t *p_mux )
         p_data   = sout_FifoGet( p_input->p_fifo );
 
         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
+            p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
             p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
         {
             sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
@@ -843,12 +927,13 @@ static int Mux( sout_mux_t *p_mux )
 
         if( p_stream->i_cat == AUDIO_ES )
         {
-            if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
+            if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
+                p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
             {
                 /* number of sample from begining + current packet */
                 op.granulepos =
                     ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
-                    p_input->p_fmt->i_sample_rate / I64C(1000000);
+                    p_input->p_fmt->audio.i_rate / I64C(1000000);
             }
             else
             {
@@ -861,7 +946,9 @@ static int Mux( sout_mux_t *p_mux )
         {
             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
             {
-                op.granulepos = op.packetno; /* FIXME */
+                /* FIXME, we assume only keyframes and 25fps */
+                op.granulepos = ( ( i_dts - p_sys->i_start_dts ) * I64C(25)
+                    / I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
             }
             else
                 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
@@ -875,9 +962,11 @@ static int Mux( sout_mux_t *p_mux )
 
         ogg_stream_packetin( &p_stream->os, &op );
 
-        if( p_stream->i_cat == SPU_ES )
+        if( p_stream->i_cat == SPU_ES ||
+            p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
         {
-            /* Subtitles need to be flushed to be sent on time */
+            /* Subtitles or Speex packets are quite small so they 
+             * need to be flushed to be sent on time */
             sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
                                                      p_data->i_dts ) );
         }