]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/ps.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / mux / mpeg / ps.c
1 /*****************************************************************************
2  * ps.c: MPEG PS (ISO/IEC 13818-1) / MPEG SYSTEM (ISO/IEC 1172-1)
3  *       multiplexer module for vlc
4  *****************************************************************************
5  * Copyright (C) 2001, 2002 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9  *          Eric Petit <titer@videolan.org>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_codecs.h>
39 #include <vlc_block.h>
40
41 #include "bits.h"
42 #include "pes.h"
43
44 #include <vlc_iso_lang.h>
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 #define DTS_TEXT N_("DTS delay (ms)")
50 #define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
51   "stamps) and PTS (presentation timestamps) of the data in the " \
52   "stream, compared to the SCRs. This allows for some buffering inside " \
53   "the client decoder.")
54
55 #define PES_SIZE_TEXT N_("PES maximum size")
56 #define PES_SIZE_LONGTEXT N_("Set the maximum allowed PES "\
57   "size when producing the MPEG PS streams.")
58
59 static int     Open   ( vlc_object_t * );
60 static void    Close  ( vlc_object_t * );
61
62 #define SOUT_CFG_PREFIX "sout-ps-"
63
64 vlc_module_begin ()
65     set_description( N_("PS muxer") )
66     set_shortname( "MPEG-PS" )
67     set_category( CAT_SOUT )
68     set_subcategory( SUBCAT_SOUT_MUX )
69     set_capability( "sout mux", 50 )
70     add_shortcut( "ps" )
71     add_shortcut( "mpeg1" )
72     add_shortcut( "dvd" )
73     set_callbacks( Open, Close )
74
75     add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
76                  DTS_LONGTEXT, true )
77     add_integer( SOUT_CFG_PREFIX "pes-max-size", PES_PAYLOAD_SIZE_MAX, NULL,
78                  PES_SIZE_TEXT, PES_SIZE_LONGTEXT, true )
79 vlc_module_end ()
80
81 /*****************************************************************************
82  * Exported prototypes
83  *****************************************************************************/
84 static int Control  ( sout_mux_t *, int, va_list );
85 static int AddStream( sout_mux_t *, sout_input_t * );
86 static int DelStream( sout_mux_t *, sout_input_t * );
87 static int Mux      ( sout_mux_t * );
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static int  MuxGetStream        ( sout_mux_t *, int *, mtime_t * );
93
94 static void MuxWritePackHeader  ( sout_mux_t *, block_t **, mtime_t );
95 static void MuxWriteSystemHeader( sout_mux_t *, block_t **, mtime_t );
96 static void MuxWritePSM         ( sout_mux_t *, block_t **, mtime_t );
97
98 static void StreamIdInit        ( bool *id, int i_range );
99 static int  StreamIdGet         ( bool *id, int i_id_min, int i_id_max );
100 static void StreamIdRelease     ( bool *id, int i_id_min, int i_id );
101
102 typedef struct ps_stream_s
103 {
104     int i_stream_id;
105     int i_stream_type;
106     int i_max_buff_size; /* used in system header */
107
108     /* Language is iso639-2T */
109     uint8_t lang[3];
110
111 } ps_stream_t;
112
113 struct sout_mux_sys_t
114 {
115     /* Which id are unused */
116     bool  stream_id_mpga[16]; /* 0xc0 -> 0xcf */
117     bool  stream_id_mpgv[16]; /* 0xe0 -> 0xef */
118     bool  stream_id_a52[8];   /* 0x80 -> 0x87 <- FIXME I'm not sure */
119     bool  stream_id_spu[32];  /* 0x20 -> 0x3f */
120     bool  stream_id_dts[8];   /* 0x88 -> 0x8f */
121     bool  stream_id_lpcm[16]; /* 0xa0 -> 0xaf */
122
123     int i_audio_bound;
124     int i_video_bound;
125     int i_pes_count;
126     int i_system_header;
127     int i_dts_delay;
128     int i_rate_bound; /* units of 50 bytes/second */
129  
130     int64_t i_instant_bitrate;
131     int64_t i_instant_size;
132     int64_t i_instant_dts;
133
134     bool b_mpeg2;
135
136     int i_pes_max_size;
137
138     int i_psm_version;
139     uint32_t crc32_table[256];
140 };
141
142 static const char *const ppsz_sout_options[] = {
143     "dts-delay", "pes-max-size", NULL
144 };
145
146 /*****************************************************************************
147  * Open:
148  *****************************************************************************/
149 static int Open( vlc_object_t *p_this )
150 {
151     sout_mux_t *p_mux = (sout_mux_t*)p_this;
152     sout_mux_sys_t *p_sys;
153     vlc_value_t val;
154
155     msg_Info( p_mux, "Open" );
156     config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
157
158     p_mux->pf_control   = Control;
159     p_mux->pf_addstream = AddStream;
160     p_mux->pf_delstream = DelStream;
161     p_mux->pf_mux       = Mux;
162     p_mux->p_sys        = p_sys = malloc( sizeof( sout_mux_sys_t ) );
163
164     /* Init free stream id */
165     StreamIdInit( p_sys->stream_id_a52,  8  );
166     StreamIdInit( p_sys->stream_id_dts,  8  );
167     StreamIdInit( p_sys->stream_id_mpga, 16 );
168     StreamIdInit( p_sys->stream_id_mpgv, 16 );
169     StreamIdInit( p_sys->stream_id_lpcm, 16 );
170     StreamIdInit( p_sys->stream_id_spu,  32 );
171
172     p_sys->i_audio_bound   = 0;
173     p_sys->i_video_bound   = 0;
174     p_sys->i_system_header = 0;
175     p_sys->i_pes_count     = 0;
176
177     p_sys->i_psm_version   = 0;
178
179     p_sys->i_instant_bitrate  = 0;
180     p_sys->i_instant_size     = 0;
181     p_sys->i_instant_dts      = 0;
182     p_sys->i_rate_bound      = 0;
183     p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
184
185     var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
186     p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
187
188     var_Get( p_mux, SOUT_CFG_PREFIX "pes-max-size", &val );
189     p_sys->i_pes_max_size = (int64_t)val.i_int;
190
191     /* Initialise CRC32 table */
192     if( p_sys->b_mpeg2 )
193     {
194         uint32_t i, j, k;
195
196         for( i = 0; i < 256; i++ )
197         {
198             k = 0;
199             for( j = (i << 24) | 0x800000; j != 0x80000000; j <<= 1 )
200                 k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
201
202             p_sys->crc32_table[i] = k;
203         }
204     }
205
206     return VLC_SUCCESS;
207 }
208
209 /*****************************************************************************
210  * Close:
211  *****************************************************************************/
212 static void Close( vlc_object_t * p_this )
213 {
214     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
215     sout_mux_sys_t  *p_sys = p_mux->p_sys;
216
217     block_t   *p_end;
218
219     msg_Info( p_mux, "Close" );
220
221     p_end = block_New( p_mux, 4 );
222     p_end->p_buffer[0] = 0x00; p_end->p_buffer[1] = 0x00;
223     p_end->p_buffer[2] = 0x01; p_end->p_buffer[3] = 0xb9;
224
225     sout_AccessOutWrite( p_mux->p_access, p_end );
226
227     free( p_sys );
228 }
229
230 /*****************************************************************************
231  * Control:
232  *****************************************************************************/
233 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
234 {
235     VLC_UNUSED(p_mux);
236     bool *pb_bool;
237     char **ppsz;
238
239     switch( i_query )
240     {
241         case MUX_CAN_ADD_STREAM_WHILE_MUXING:
242             pb_bool = (bool*)va_arg( args, bool * );
243             *pb_bool = true;
244             return VLC_SUCCESS;
245
246         case MUX_GET_ADD_STREAM_WAIT:
247             pb_bool = (bool*)va_arg( args, bool * );
248             *pb_bool = false;
249             return VLC_SUCCESS;
250
251         case MUX_GET_MIME:
252             ppsz = (char**)va_arg( args, char ** );
253             *ppsz = strdup( "video/mpeg" );
254             return VLC_SUCCESS;
255
256         default:
257             return VLC_EGENERIC;
258     }
259 }
260
261 /*****************************************************************************
262  * AddStream:
263  *****************************************************************************/
264 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
265 {
266     sout_mux_sys_t  *p_sys = p_mux->p_sys;
267     ps_stream_t *p_stream;
268  
269
270     msg_Dbg( p_mux, "adding input codec=%4.4s",
271              (char*)&p_input->p_fmt->i_codec );
272
273     p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
274     p_stream->i_stream_type = 0x81;
275
276     /* Init this new stream */
277     switch( p_input->p_fmt->i_codec )
278     {
279         case VLC_FOURCC( 'm', 'p', '1', 'v' ):
280             p_stream->i_stream_id =
281                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
282             p_stream->i_stream_type = 0x01; /* ISO/IEC 11172 Video */
283             break;
284         case VLC_FOURCC( 'm', 'p', '2', 'v' ):
285         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
286             p_stream->i_stream_id =
287                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
288             p_stream->i_stream_type = 0x02; /* ISO/IEC 13818 Video */
289             break;
290         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
291             p_stream->i_stream_id =
292                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
293             p_stream->i_stream_type = 0x10;
294             break;
295         case VLC_FOURCC( 'h', '2', '6', '4' ):
296             p_stream->i_stream_id =
297                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
298             p_stream->i_stream_type = 0x1b;
299             break;
300         case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
301             p_stream->i_stream_id =
302                 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
303             break;
304         case VLC_FOURCC( 'd', 't', 's', ' ' ):
305             p_stream->i_stream_id =
306                 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
307             break;
308         case VLC_FOURCC( 'a', '5', '2', ' ' ):
309             p_stream->i_stream_id =
310                 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
311             break;
312         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
313             p_stream->i_stream_id =
314                 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
315             p_stream->i_stream_type = 0x03; /* ISO/IEC 11172 Audio */
316             break;
317         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
318             p_stream->i_stream_id =
319                 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
320             p_stream->i_stream_type = 0x0f;
321             break;
322         case VLC_FOURCC( 's', 'p', 'u', ' ' ):
323             p_stream->i_stream_id =
324                 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
325             break;
326         default:
327             goto error;
328     }
329
330     if( p_stream->i_stream_id < 0 ) goto error;
331
332     if( p_input->p_fmt->i_cat == AUDIO_ES )
333     {
334         p_sys->i_audio_bound++;
335         p_stream->i_max_buff_size = 4 * 1024;
336     }
337     else if( p_input->p_fmt->i_cat == VIDEO_ES )
338     {
339         p_sys->i_video_bound++;
340         p_stream->i_max_buff_size = 400 * 1024; /* FIXME -- VCD uses 46, SVCD
341                         uses 230, ffmpeg has 230 with a note that it is small */
342     }
343     else
344     {   /* FIXME -- what's valid for not audio or video? */
345         p_stream->i_max_buff_size = 4 * 1024;
346     }
347
348     /* Try to set a sensible default value for the instant bitrate */
349     p_sys->i_instant_bitrate += p_input->p_fmt->i_bitrate + 1000/* overhead */;
350
351     /* FIXME -- spec requires  an upper limit rate boundary in the system header;
352        our codecs are VBR; using 2x nominal rate, convert to 50 bytes/sec */
353     p_sys->i_rate_bound += p_input->p_fmt->i_bitrate * 2 / (8 * 50);
354     p_sys->i_psm_version++;
355
356     p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
357     if( p_input->p_fmt->psz_language )
358     {
359         char *psz = p_input->p_fmt->psz_language;
360         const iso639_lang_t *pl = NULL;
361
362         if( strlen( psz ) == 2 )
363         {
364             pl = GetLang_1( psz );
365         }
366         else if( strlen( psz ) == 3 )
367         {
368             pl = GetLang_2B( psz );
369             if( !strcmp( pl->psz_iso639_1, "??" ) )
370             {
371                 pl = GetLang_2T( psz );
372             }
373         }
374         if( pl && strcmp( pl->psz_iso639_1, "??" ) )
375         {
376             p_stream->lang[0] = pl->psz_iso639_2T[0];
377             p_stream->lang[1] = pl->psz_iso639_2T[1];
378             p_stream->lang[2] = pl->psz_iso639_2T[2];
379
380             msg_Dbg( p_mux, "    - lang=%c%c%c",
381                      p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
382         }
383     }
384     return VLC_SUCCESS;
385
386 error:
387     free( p_stream );
388     return VLC_EGENERIC;
389 }
390
391 /*****************************************************************************
392  * DelStream:
393  *****************************************************************************/
394 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
395 {
396     sout_mux_sys_t *p_sys = p_mux->p_sys;
397     ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
398
399     msg_Dbg( p_mux, "removing input" );
400     switch( p_input->p_fmt->i_codec )
401     {
402         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
403             StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
404                              p_stream->i_stream_id );
405             break;
406         case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
407             StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
408                              p_stream->i_stream_id&0xff );
409             break;
410         case VLC_FOURCC( 'd', 't', 's', ' ' ):
411             StreamIdRelease( p_sys->stream_id_dts, 0x88,
412                              p_stream->i_stream_id&0xff );
413             break;
414         case VLC_FOURCC( 'a', '5', '2', ' ' ):
415             StreamIdRelease( p_sys->stream_id_a52, 0x80,
416                              p_stream->i_stream_id&0xff );
417             break;
418         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
419             StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
420                              p_stream->i_stream_id  );
421             break;
422         case VLC_FOURCC( 's', 'p', 'u', ' ' ):
423             StreamIdRelease( p_sys->stream_id_spu, 0x20,
424                              p_stream->i_stream_id&0xff );
425             break;
426         default:
427             /* Never reached */
428             break;
429     }
430
431     if( p_input->p_fmt->i_cat == AUDIO_ES )
432     {
433         p_sys->i_audio_bound--;
434     }
435     else if( p_input->p_fmt->i_cat == VIDEO_ES )
436     {
437         p_sys->i_video_bound--;
438     }
439
440     /* Try to set a sensible default value for the instant bitrate */
441     p_sys->i_instant_bitrate -= (p_input->p_fmt->i_bitrate + 1000);
442     /* rate_bound is in units of 50 bytes/second */
443     p_sys->i_rate_bound -= (p_input->p_fmt->i_bitrate * 2)/(8 * 50);
444
445     p_sys->i_psm_version++;
446
447     free( p_stream );
448     return VLC_SUCCESS;
449 }
450
451 /*****************************************************************************
452  * Mux: Call each time there is new data for at least one stream
453  *****************************************************************************/
454 static int Mux( sout_mux_t *p_mux )
455 {
456     sout_mux_sys_t *p_sys = p_mux->p_sys;
457
458     for( ;; )
459     {
460         sout_input_t *p_input;
461         ps_stream_t *p_stream;
462
463         block_t *p_ps, *p_data;
464
465         mtime_t        i_dts;
466         int            i_stream;
467
468         /* Choose which stream to mux */
469         if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
470         {
471             return VLC_SUCCESS;
472         }
473
474         p_input  = p_mux->pp_inputs[i_stream];
475         p_stream = (ps_stream_t*)p_input->p_sys;
476         p_ps     = NULL;
477
478         /* Write regulary PackHeader */
479         if( p_sys->i_pes_count % 30 == 0)
480         {
481             /* Update the instant bitrate every second or so */
482             if( p_sys->i_instant_size &&
483                 i_dts - p_sys->i_instant_dts > 1000000 )
484             {
485                 int64_t i_instant_bitrate = p_sys->i_instant_size * 8000000 /
486                     ( i_dts - p_sys->i_instant_dts );
487
488                 p_sys->i_instant_bitrate += i_instant_bitrate;
489                 p_sys->i_instant_bitrate /= 2;
490
491                 p_sys->i_instant_size = 0;
492                 p_sys->i_instant_dts = i_dts;
493             }
494             else if( !p_sys->i_instant_size )
495             {
496                 p_sys->i_instant_dts = i_dts;
497             }
498
499             MuxWritePackHeader( p_mux, &p_ps, i_dts );
500         }
501
502         /* Write regulary SystemHeader */
503         if( p_sys->i_pes_count % 300 == 0 )
504         {
505             block_t *p_pk;
506
507             MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
508
509             /* For MPEG1 streaming, set HEADER flag */
510             for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
511             {
512                 p_pk->i_flags |= BLOCK_FLAG_HEADER;
513             }
514         }
515
516         /* Write regulary ProgramStreamMap */
517         if( p_sys->b_mpeg2 && p_sys->i_pes_count % 300 == 0 )
518         {
519             MuxWritePSM( p_mux, &p_ps, i_dts );
520         }
521
522         /* Get and mux a packet */
523         p_data = block_FifoGet( p_input->p_fifo );
524          EStoPES ( p_mux->p_sout, &p_data, p_data,
525                        p_input->p_fmt, p_stream->i_stream_id,
526                        p_sys->b_mpeg2, 0, 0, p_sys->i_pes_max_size );
527
528         block_ChainAppend( &p_ps, p_data );
529
530         /* Get size of output data so we can calculate the instant bitrate */
531         for( p_data = p_ps; p_data; p_data = p_data->p_next )
532         {
533             p_sys->i_instant_size += p_data->i_buffer;
534         }
535
536         sout_AccessOutWrite( p_mux->p_access, p_ps );
537
538         /* Increase counter */
539         p_sys->i_pes_count++;
540     }
541
542     return VLC_SUCCESS;
543 }
544
545 /*****************************************************************************
546  *
547  *****************************************************************************/
548 static void StreamIdInit( bool *id, int i_range )
549 {
550     int i;
551
552     for( i = 0; i < i_range; i++ )
553     {
554         id[i] = true;
555     }
556 }
557 static int StreamIdGet( bool *id, int i_id_min, int i_id_max )
558 {
559     int i;
560
561     for( i = 0; i <= i_id_max - i_id_min; i++ )
562     {
563         if( id[i] )
564         {
565             id[i] = false;
566
567             return i_id_min + i;
568         }
569     }
570     return -1;
571 }
572 static void StreamIdRelease( bool *id, int i_id_min, int i_id )
573 {
574     id[i_id - i_id_min] = true;
575 }
576
577 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
578                                 mtime_t i_dts )
579 {
580     sout_mux_sys_t *p_sys = p_mux->p_sys;
581     bits_buffer_t bits;
582     block_t *p_hdr;
583     mtime_t i_scr;
584     int i_mux_rate;
585
586     i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
587
588     p_hdr = block_New( p_mux, 18 );
589     p_hdr->i_pts = p_hdr->i_dts = i_dts;
590     bits_initwrite( &bits, 14, p_hdr->p_buffer );
591     bits_write( &bits, 32, 0x01ba );
592
593     /* The spec specifies that the mux rate must be rounded upwards */
594     i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
595
596     if( p_sys->b_mpeg2 )
597     {
598         bits_write( &bits, 2, 0x01 );
599     }
600     else
601     {
602         bits_write( &bits, 4, 0x02 );
603     }
604
605     bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
606     bits_write( &bits, 1,  1 ); // marker
607     bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
608     bits_write( &bits, 1,  1 ); // marker
609     bits_write( &bits, 15, i_scr&0x7fff );
610     bits_write( &bits, 1,  1 ); // marker
611
612     if( p_sys->b_mpeg2 )
613     {
614         bits_write( &bits, 9,  0 ); // src extension
615     }
616     bits_write( &bits, 1,  1 );     // marker
617
618     bits_write( &bits, 22, i_mux_rate);
619     bits_write( &bits, 1,  1 );     // marker
620
621     if( p_sys->b_mpeg2 )
622     {
623         bits_write( &bits, 1,  1 );     // marker
624         bits_write( &bits, 5,  0x1f );  // reserved
625         bits_write( &bits, 3,  0 );     // stuffing bytes
626     }
627
628     p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
629
630     block_ChainAppend( p_buf, p_hdr );
631 }
632
633 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
634                                   mtime_t i_dts )
635 {
636     sout_mux_sys_t  *p_sys = p_mux->p_sys;
637     block_t   *p_hdr;
638     bits_buffer_t   bits;
639     bool      b_private;
640     int i_rate_bound;
641
642     int             i_nb_private, i_nb_stream;
643     int i;
644
645     /* Count the number of private stream */
646     for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
647     {
648         ps_stream_t *p_stream;
649
650         p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
651
652         if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
653         {
654             i_nb_private++;
655         }
656     }
657
658     /* Private stream are declared only one time */
659     i_nb_stream = p_mux->i_nb_inputs -
660         ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
661
662     p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
663     p_hdr->i_dts = p_hdr->i_pts = i_dts;
664
665     /* The spec specifies that the reported rate_bound must be upper limit */
666     i_rate_bound = (p_sys->i_rate_bound);
667
668     bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
669     bits_write( &bits, 32, 0x01bb );
670     bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
671     bits_write( &bits, 1,  1 ); // marker bit
672     bits_write( &bits, 22, i_rate_bound);
673     bits_write( &bits, 1,  1 ); // marker bit
674
675     bits_write( &bits, 6,  p_sys->i_audio_bound );
676     bits_write( &bits, 1,  0 ); // fixed flag
677     bits_write( &bits, 1,  0 ); // CSPS flag
678     bits_write( &bits, 1,  0 ); // system audio lock flag
679     bits_write( &bits, 1,  0 ); // system video lock flag
680
681     bits_write( &bits, 1,  1 ); // marker bit
682
683     bits_write( &bits, 5,  p_sys->i_video_bound );
684     bits_write( &bits, 1,  1 ); // packet rate restriction flag (1 for mpeg1)
685     bits_write( &bits, 7,  0xff ); // reserved bits
686
687     /* stream_id table */
688     for( i = 0, b_private = false; i < p_mux->i_nb_inputs; i++ )
689     {
690         sout_input_t *p_input;
691         ps_stream_t *p_stream;
692
693         p_input = p_mux->pp_inputs[i];
694         p_stream = (ps_stream_t *)p_input->p_sys;
695
696         if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
697         {
698             if( b_private )
699             {
700                 continue;
701             }
702             b_private = true;
703             /* Write stream id */
704             bits_write( &bits, 8, 0xbd );
705         }
706         else
707         {
708             /* Write stream id */
709             bits_write( &bits, 8, p_stream->i_stream_id&0xff );
710         }
711
712         bits_write( &bits, 2, 0x03 ); /* reserved */
713         if( p_input->p_fmt->i_cat == AUDIO_ES )
714         {
715             bits_write( &bits, 1, 0 );
716             bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
717         }
718         else if( p_input->p_fmt->i_cat == VIDEO_ES )
719         {
720             bits_write( &bits, 1, 1 );
721             bits_write( &bits, 13, p_stream->i_max_buff_size / 1024);
722         }
723         else
724         {
725             /* FIXME -- the scale of 0 means do a /128 */
726             bits_write( &bits, 1, 0 );
727             bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
728         }
729     }
730
731     block_ChainAppend( p_buf, p_hdr );
732 }
733
734 static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
735 {
736     sout_mux_sys_t *p_sys = p_mux->p_sys;
737     block_t *p_hdr;
738     bits_buffer_t bits;
739     int i, i_psm_size = 16, i_es_map_size = 0;
740
741     for( i = 0; i < p_mux->i_nb_inputs; i++ )
742     {
743         sout_input_t *p_input = p_mux->pp_inputs[i];
744         ps_stream_t *p_stream = p_input->p_sys;
745
746         i_es_map_size += 4;
747         if( p_stream->lang[0] != 0 ) i_es_map_size += 6;
748     }
749
750     i_psm_size += i_es_map_size;
751
752     p_hdr = block_New( p_mux, i_psm_size );
753     p_hdr->i_dts = p_hdr->i_pts = i_dts;
754
755     memset( p_hdr->p_buffer, 0, p_hdr->i_buffer );
756     bits_initwrite( &bits, i_psm_size, p_hdr->p_buffer );
757     bits_write( &bits, 32, 0x01bc );
758     bits_write( &bits, 16, i_psm_size - 6 );
759     bits_write( &bits, 1, 1 ); /* current_next_indicator */
760     bits_write( &bits, 2, 0xF ); /* reserved */
761     bits_write( &bits, 5, p_sys->i_psm_version );
762     bits_write( &bits, 7, 0xFF ); /* reserved */
763     bits_write( &bits, 1, 1 ); /* marker */
764
765     bits_write( &bits, 16, 0 ); /* program_stream_info_length */
766     /* empty */
767
768     bits_write( &bits, 16, i_es_map_size ); /* elementary_stream_map_length */
769     for( i = 0; i < p_mux->i_nb_inputs; i++ )
770     {
771         sout_input_t *p_input = p_mux->pp_inputs[i];
772         ps_stream_t *p_stream = p_input->p_sys;
773
774         bits_write( &bits, 8, p_stream->i_stream_type ); /* stream_type */
775         bits_write( &bits, 8, p_stream->i_stream_id ); /* elementary_stream_id */
776
777         /* ISO639 language descriptor */
778         if( p_stream->lang[0] != 0 )
779         {
780             bits_write( &bits, 16, 6 ); /* elementary_stream_info_length */
781
782             bits_write( &bits, 8, 0x0a ); /* descriptor_tag */
783             bits_write( &bits, 8, 4 ); /* descriptor_length */
784
785             bits_write( &bits, 8, p_stream->lang[0] );
786             bits_write( &bits, 8, p_stream->lang[1] );
787             bits_write( &bits, 8, p_stream->lang[2] );
788             bits_write( &bits, 8, 0 ); /* audio type: 0x00 undefined */
789         }
790         else
791         {
792             bits_write( &bits, 16, 0 ); /* elementary_stream_info_length */
793         }
794     }
795
796     /* CRC32 */
797     {
798         uint32_t i_crc = 0xffffffff;
799         for( i = 0; (size_t)i < p_hdr->i_buffer; i++ )
800         i_crc = (i_crc << 8) ^
801             p_sys->crc32_table[((i_crc >> 24) ^ p_hdr->p_buffer[i]) & 0xff];
802
803         bits_write( &bits, 32, i_crc );
804     }
805
806     block_ChainAppend( p_buf, p_hdr );
807 }
808
809 /*
810  * Find stream to be muxed.
811  */
812 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
813 {
814     mtime_t i_dts;
815     int     i_stream, i;
816
817     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
818     {
819         sout_input_t *p_input = p_mux->pp_inputs[i];
820         block_t *p_data;
821
822         if( block_FifoCount( p_input->p_fifo ) <= 0 )
823         {
824             if( p_input->p_fmt->i_cat == AUDIO_ES ||
825                 p_input->p_fmt->i_cat == VIDEO_ES )
826             {
827                 /* We need that audio+video fifo contain at least 1 packet */
828                 return VLC_EGENERIC;
829             }
830
831             /* SPU */
832             continue;
833         }
834
835         p_data = block_FifoShow( p_input->p_fifo );
836         if( i_stream == -1 || p_data->i_dts < i_dts )
837         {
838             i_stream = i;
839             i_dts    = p_data->i_dts;
840         }
841     }
842
843     *pi_stream = i_stream;
844     *pi_dts = i_dts;
845
846     return VLC_SUCCESS;
847 }