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