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