]> git.sesse.net Git - vlc/blobdiff - modules/mux/mpeg/ps.c
* fix the getlength. It needs to be after the SETUP, so that liveMedia can correct...
[vlc] / modules / mux / mpeg / ps.c
index 336d4a6723987313676a42e29d4f8fadb5648e94..300f52cbe8ff5bac43e1e2512f8cbc01e67764c6 100644 (file)
@@ -2,7 +2,7 @@
  * ps.c: MPEG PS (ISO/IEC 13818-1) / MPEG SYSTEM (ISO/IEC 1172-1)
  *       multiplexer module for vlc
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
+ * Copyright (C) 2001, 2002 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -21,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 #define DTS_TEXT N_("DTS delay (ms)")
-#define DTS_LONGTEXT N_("This option will delay the DTS (decoding time " \
+#define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
   "stamps) and PTS (presentation timestamps) of the data in the " \
   "stream, compared to the SCRs. This allows for some buffering inside " \
   "the client decoder.")
 
+#define PES_SIZE_TEXT N_("PES maximum size")
+#define PES_SIZE_LONGTEXT N_("Set the maximum allowed PES "\
+  "size when producing the MPEG PS streams.")
+
 static int     Open   ( vlc_object_t * );
 static void    Close  ( vlc_object_t * );
 
@@ -66,6 +70,8 @@ vlc_module_begin();
 
     add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
                  DTS_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "pes-max-size", PES_PAYLOAD_SIZE_MAX, NULL,
+                 PES_SIZE_TEXT, PES_SIZE_LONGTEXT, VLC_TRUE );
 vlc_module_end();
 
 /*****************************************************************************
@@ -93,6 +99,7 @@ typedef struct ps_stream_s
 {
     int i_stream_id;
     int i_stream_type;
+    int i_max_buff_size; /* used in system header */
 
     /* Language is iso639-2T */
     uint8_t lang[3];
@@ -114,19 +121,22 @@ struct sout_mux_sys_t
     int i_pes_count;
     int i_system_header;
     int i_dts_delay;
-
+    int i_rate_bound; /* units of 50 bytes/second */
+    
     int64_t i_instant_bitrate;
     int64_t i_instant_size;
     int64_t i_instant_dts;
 
     vlc_bool_t b_mpeg2;
 
+    int i_pes_max_size;
+
     int i_psm_version;
     uint32_t crc32_table[256];
 };
 
 static const char *ppsz_sout_options[] = {
-    "dts-delay", NULL
+    "dts-delay", "pes-max-size", NULL
 };
 
 /*****************************************************************************
@@ -165,12 +175,15 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_instant_bitrate  = 0;
     p_sys->i_instant_size     = 0;
     p_sys->i_instant_dts      = 0;
-
+    p_sys->i_rate_bound      = 0;
     p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
 
     var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
     p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
 
+    var_Get( p_mux, SOUT_CFG_PREFIX "pes-max-size", &val );
+    p_sys->i_pes_max_size = (int64_t)val.i_int;
+
     /* Initialise CRC32 table */
     if( p_sys->b_mpeg2 )
     {
@@ -247,6 +260,7 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
 {
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
     ps_stream_t *p_stream;
+    
 
     msg_Dbg( p_mux, "adding input codec=%4.4s",
              (char*)&p_input->p_fmt->i_codec );
@@ -313,15 +327,25 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     if( p_input->p_fmt->i_cat == AUDIO_ES )
     {
         p_sys->i_audio_bound++;
+        p_stream->i_max_buff_size = 4 * 1024;
     }
     else if( p_input->p_fmt->i_cat == VIDEO_ES )
     {
         p_sys->i_video_bound++;
+        p_stream->i_max_buff_size = 400 * 1024; /* FIXME -- VCD uses 46, SVCD
+                        uses 230, ffmpeg has 230 with a note that it is small */
+    }
+    else
+    {   /* FIXME -- what's valid for not audio or video? */
+        p_stream->i_max_buff_size = 4 * 1024;
     }
 
     /* Try to set a sensible default value for the instant bitrate */
     p_sys->i_instant_bitrate += p_input->p_fmt->i_bitrate + 1000/* overhead */;
 
+    /* FIXME -- spec requires  an upper limit rate boundary in the system header;
+       our codecs are VBR; using 2x nominal rate, convert to 50 bytes/sec */ 
+    p_sys->i_rate_bound += p_input->p_fmt->i_bitrate * 2 / (8 * 50);
     p_sys->i_psm_version++;
 
     p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
@@ -352,7 +376,6 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
                      p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
         }
     }
-
     return VLC_SUCCESS;
 
 error:
@@ -411,6 +434,8 @@ static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
 
     /* Try to set a sensible default value for the instant bitrate */
     p_sys->i_instant_bitrate -= (p_input->p_fmt->i_bitrate + 1000);
+    /* rate_bound is in units of 50 bytes/second */
+    p_sys->i_rate_bound -= (p_input->p_fmt->i_bitrate * 2)/(8 * 50);
 
     p_sys->i_psm_version++;
 
@@ -493,7 +518,7 @@ static int Mux( sout_mux_t *p_mux )
         p_data = block_FifoGet( p_input->p_fifo );
         E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
                        p_input->p_fmt, p_stream->i_stream_id,
-                       p_sys->b_mpeg2, 0, 0 );
+                       p_sys->b_mpeg2, 0, 0, p_sys->i_pes_max_size );
 
         block_ChainAppend( &p_ps, p_data );
 
@@ -573,25 +598,25 @@ static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
     }
 
     bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 1,  1 ); // marker
     bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 1,  1 ); // marker
     bits_write( &bits, 15, i_scr&0x7fff );
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 1,  1 ); // marker
 
     if( p_sys->b_mpeg2 )
     {
-        bits_write( &bits, 9,  0 ); // src extention
+        bits_write( &bits, 9,  0 ); // src extension
     }
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 1,  1 );     // marker
 
-    bits_write( &bits, 22,  i_mux_rate);  // FIXME mux rate
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 22, i_mux_rate);
+    bits_write( &bits, 1,  1 );     // marker
 
     if( p_sys->b_mpeg2 )
     {
-        bits_write( &bits, 1,  1 );
-        bits_write( &bits, 5,  0x1f );  // FIXME reserved
+        bits_write( &bits, 1,  1 );     // marker
+        bits_write( &bits, 5,  0x1f );  // reserved
         bits_write( &bits, 3,  0 );     // stuffing bytes
     }
 
@@ -607,7 +632,7 @@ static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
     block_t   *p_hdr;
     bits_buffer_t   bits;
     vlc_bool_t      b_private;
-    int i_mux_rate;
+    int i_rate_bound;
 
     int             i_nb_private, i_nb_stream;
     int i;
@@ -632,15 +657,15 @@ static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
     p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
     p_hdr->i_dts = p_hdr->i_pts = i_dts;
 
-    /* The spec specifies that the mux rate must be rounded upwards */
-    i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
+    /* The spec specifies that the reported rate_bound must be upper limit */
+    i_rate_bound = (p_sys->i_rate_bound);
 
     bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
     bits_write( &bits, 32, 0x01bb );
     bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
-    bits_write( &bits, 1,  1 );
-    bits_write( &bits, 22, i_mux_rate); // FIXME rate bound
-    bits_write( &bits, 1,  1 );
+    bits_write( &bits, 1,  1 ); // marker bit
+    bits_write( &bits, 22, i_rate_bound); 
+    bits_write( &bits, 1,  1 ); // marker bit
 
     bits_write( &bits, 6,  p_sys->i_audio_bound );
     bits_write( &bits, 1,  0 ); // fixed flag
@@ -678,22 +703,23 @@ static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
             /* Write stream id */
             bits_write( &bits, 8, p_stream->i_stream_id&0xff );
         }
-        bits_write( &bits, 2, 0x03 );
+
+        bits_write( &bits, 2, 0x03 ); /* reserved */
         if( p_input->p_fmt->i_cat == AUDIO_ES )
         {
             bits_write( &bits, 1, 0 );
-            bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 128 );
+            bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
         }
         else if( p_input->p_fmt->i_cat == VIDEO_ES )
         {
             bits_write( &bits, 1, 1 );
-            bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 1024);
+            bits_write( &bits, 13, p_stream->i_max_buff_size / 1024);
         }
         else
         {
-            /* FIXME */
+            /* FIXME -- the scale of 0 means do a /128 */
             bits_write( &bits, 1, 0 );
-            bits_write( &bits, 13, /* stream->max_buffer_size */ 0 );
+            bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
         }
     }
 
@@ -724,7 +750,7 @@ static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
     memset( p_hdr->p_buffer, 0, p_hdr->i_buffer );
     bits_initwrite( &bits, i_psm_size, p_hdr->p_buffer );
     bits_write( &bits, 32, 0x01bc );
-    bits_write( &bits, 16, i_psm_size - 3 );
+    bits_write( &bits, 16, i_psm_size - 6 );
     bits_write( &bits, 1, 1 ); /* current_next_indicator */
     bits_write( &bits, 2, 0xF ); /* reserved */
     bits_write( &bits, 5, p_sys->i_psm_version );