]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/ps.c
41d040078f573f0a8825e4fba5d68cd78d980e83
[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 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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_block.h>
39
40 #include "bits.h"
41 #include "pes.h"
42
43 #include <vlc_iso_lang.h>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define DTS_TEXT N_("DTS delay (ms)")
49 #define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
50   "stamps) and PTS (presentation timestamps) of the data in the " \
51   "stream, compared to the SCRs. This allows for some buffering inside " \
52   "the client decoder.")
53
54 #define PES_SIZE_TEXT N_("PES maximum size")
55 #define PES_SIZE_LONGTEXT N_("Set the maximum allowed PES "\
56   "size when producing the MPEG PS streams.")
57
58 static int     Open   ( vlc_object_t * );
59 static void    Close  ( vlc_object_t * );
60
61 #define SOUT_CFG_PREFIX "sout-ps-"
62
63 vlc_module_begin ()
64     set_description( N_("PS muxer") )
65     set_shortname( "MPEG-PS" )
66     set_category( CAT_SOUT )
67     set_subcategory( SUBCAT_SOUT_MUX )
68     set_capability( "sout mux", 50 )
69     add_shortcut( "ps", "mpeg1", "dvd" )
70     set_callbacks( Open, Close )
71
72     add_integer( SOUT_CFG_PREFIX "dts-delay", 200, DTS_TEXT,
73                  DTS_LONGTEXT, true )
74     add_integer( SOUT_CFG_PREFIX "pes-max-size", PES_PAYLOAD_SIZE_MAX,
75                  PES_SIZE_TEXT, PES_SIZE_LONGTEXT, true )
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 void DelStream( sout_mux_t *, sout_input_t * );
84 static int Mux      ( sout_mux_t * );
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
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        ( bool *id, int i_range );
95 static int  StreamIdGet         ( bool *id, int i_id_min, int i_id_max );
96 static void StreamIdRelease     ( bool *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     bool  stream_id_mpga[16]; /* 0xc0 -> 0xcf */
113     bool  stream_id_mpgv[16]; /* 0xe0 -> 0xef */
114     bool  stream_id_a52[8];   /* 0x80 -> 0x87 <- FIXME I'm not sure */
115     bool  stream_id_spu[32];  /* 0x20 -> 0x3f */
116     bool  stream_id_dts[8];   /* 0x88 -> 0x8f */
117     bool  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     bool 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 *const 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_Alloc( 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_UNUSED(p_mux);
232     bool *pb_bool;
233     char **ppsz;
234
235     switch( i_query )
236     {
237         case MUX_CAN_ADD_STREAM_WHILE_MUXING:
238             pb_bool = (bool*)va_arg( args, bool * );
239             *pb_bool = true;
240             return VLC_SUCCESS;
241
242         case MUX_GET_ADD_STREAM_WAIT:
243             pb_bool = (bool*)va_arg( args, bool * );
244             *pb_bool = 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_CODEC_MPGV:
276         case VLC_CODEC_MP2V:
277         case VLC_CODEC_MP1V:
278             p_stream->i_stream_id =
279                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
280             p_stream->i_stream_type = 0x02; /* ISO/IEC 13818 Video */
281             break;
282         case VLC_CODEC_MP4V:
283             p_stream->i_stream_id =
284                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
285             p_stream->i_stream_type = 0x10;
286             break;
287         case VLC_CODEC_H264:
288             p_stream->i_stream_id =
289                 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
290             p_stream->i_stream_type = 0x1b;
291             break;
292         case VLC_CODEC_DVD_LPCM:
293             p_stream->i_stream_id =
294                 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
295             break;
296         case VLC_CODEC_DTS:
297             p_stream->i_stream_id =
298                 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
299             break;
300         case VLC_CODEC_A52:
301             p_stream->i_stream_id =
302                 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
303             break;
304         case VLC_CODEC_MPGA:
305             p_stream->i_stream_id =
306                 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
307             p_stream->i_stream_type = 0x03; /* ISO/IEC 11172 Audio */
308             break;
309         case VLC_CODEC_MP4A:
310             p_stream->i_stream_id =
311                 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
312             p_stream->i_stream_type = 0x0f;
313             break;
314         case VLC_CODEC_SPU:
315             p_stream->i_stream_id =
316                 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
317             break;
318         default:
319             goto error;
320     }
321
322     if( p_stream->i_stream_id < 0 ) goto error;
323
324     if( p_input->p_fmt->i_cat == AUDIO_ES )
325     {
326         p_sys->i_audio_bound++;
327         p_stream->i_max_buff_size = 4 * 1024;
328     }
329     else if( p_input->p_fmt->i_cat == VIDEO_ES )
330     {
331         p_sys->i_video_bound++;
332         p_stream->i_max_buff_size = 400 * 1024; /* FIXME -- VCD uses 46, SVCD
333                         uses 230, ffmpeg has 230 with a note that it is small */
334     }
335     else
336     {   /* FIXME -- what's valid for not audio or video? */
337         p_stream->i_max_buff_size = 4 * 1024;
338     }
339
340     /* Try to set a sensible default value for the instant bitrate */
341     p_sys->i_instant_bitrate += p_input->p_fmt->i_bitrate + 1000/* overhead */;
342
343     /* FIXME -- spec requires  an upper limit rate boundary in the system header;
344        our codecs are VBR; using 2x nominal rate, convert to 50 bytes/sec */
345     p_sys->i_rate_bound += p_input->p_fmt->i_bitrate * 2 / (8 * 50);
346     p_sys->i_psm_version++;
347
348     p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
349     if( p_input->p_fmt->psz_language )
350     {
351         char *psz = p_input->p_fmt->psz_language;
352         const iso639_lang_t *pl = NULL;
353
354         if( strlen( psz ) == 2 )
355         {
356             pl = GetLang_1( psz );
357         }
358         else if( strlen( psz ) == 3 )
359         {
360             pl = GetLang_2B( psz );
361             if( !strcmp( pl->psz_iso639_1, "??" ) )
362             {
363                 pl = GetLang_2T( psz );
364             }
365         }
366         if( pl && strcmp( pl->psz_iso639_1, "??" ) )
367         {
368             p_stream->lang[0] = pl->psz_iso639_2T[0];
369             p_stream->lang[1] = pl->psz_iso639_2T[1];
370             p_stream->lang[2] = pl->psz_iso639_2T[2];
371
372             msg_Dbg( p_mux, "    - lang=%c%c%c",
373                      p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
374         }
375     }
376     return VLC_SUCCESS;
377
378 error:
379     free( p_stream );
380     return VLC_EGENERIC;
381 }
382
383 /*****************************************************************************
384  * DelStream:
385  *****************************************************************************/
386 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
387 {
388     sout_mux_sys_t *p_sys = p_mux->p_sys;
389     ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
390
391     msg_Dbg( p_mux, "removing input" );
392     switch( p_input->p_fmt->i_codec )
393     {
394         case VLC_CODEC_MPGV:
395             StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
396                              p_stream->i_stream_id );
397             break;
398         case VLC_CODEC_DVD_LPCM:
399             StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
400                              p_stream->i_stream_id&0xff );
401             break;
402         case VLC_CODEC_DTS:
403             StreamIdRelease( p_sys->stream_id_dts, 0x88,
404                              p_stream->i_stream_id&0xff );
405             break;
406         case VLC_CODEC_A52:
407             StreamIdRelease( p_sys->stream_id_a52, 0x80,
408                              p_stream->i_stream_id&0xff );
409             break;
410         case VLC_CODEC_MPGA:
411             StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
412                              p_stream->i_stream_id  );
413             break;
414         case VLC_CODEC_SPU:
415             StreamIdRelease( p_sys->stream_id_spu, 0x20,
416                              p_stream->i_stream_id&0xff );
417             break;
418         default:
419             /* Never reached */
420             break;
421     }
422
423     if( p_input->p_fmt->i_cat == AUDIO_ES )
424     {
425         p_sys->i_audio_bound--;
426     }
427     else if( p_input->p_fmt->i_cat == VIDEO_ES )
428     {
429         p_sys->i_video_bound--;
430     }
431
432     /* Try to set a sensible default value for the instant bitrate */
433     p_sys->i_instant_bitrate -= (p_input->p_fmt->i_bitrate + 1000);
434     /* rate_bound is in units of 50 bytes/second */
435     p_sys->i_rate_bound -= (p_input->p_fmt->i_bitrate * 2)/(8 * 50);
436
437     p_sys->i_psm_version++;
438
439     free( p_stream );
440 }
441
442 /*****************************************************************************
443  * Mux: Call each time there is new data for at least one stream
444  *****************************************************************************/
445 static int Mux( sout_mux_t *p_mux )
446 {
447     sout_mux_sys_t *p_sys = p_mux->p_sys;
448
449     for( ;; )
450     {
451         sout_input_t *p_input;
452         ps_stream_t *p_stream;
453
454         block_t *p_ps, *p_data;
455
456         mtime_t        i_dts;
457
458         /* Choose which stream to mux */
459         int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
460         if( i_stream < 0 )
461         {
462             return VLC_SUCCESS;
463         }
464
465         p_input  = p_mux->pp_inputs[i_stream];
466         p_stream = (ps_stream_t*)p_input->p_sys;
467         p_ps     = NULL;
468
469         /* Write regulary PackHeader */
470         if( p_sys->i_pes_count % 30 == 0)
471         {
472             /* Update the instant bitrate every second or so */
473             if( p_sys->i_instant_size &&
474                 i_dts - p_sys->i_instant_dts > 1000000 )
475             {
476                 int64_t i_instant_bitrate = p_sys->i_instant_size * 8000000 /
477                     ( i_dts - p_sys->i_instant_dts );
478
479                 p_sys->i_instant_bitrate += i_instant_bitrate;
480                 p_sys->i_instant_bitrate /= 2;
481
482                 p_sys->i_instant_size = 0;
483                 p_sys->i_instant_dts = i_dts;
484             }
485             else if( !p_sys->i_instant_size )
486             {
487                 p_sys->i_instant_dts = i_dts;
488             }
489
490             MuxWritePackHeader( p_mux, &p_ps, i_dts );
491         }
492
493         /* Write regulary SystemHeader */
494         if( p_sys->i_pes_count % 300 == 0 )
495         {
496             block_t *p_pk;
497
498             MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
499
500             /* For MPEG1 streaming, set HEADER flag */
501             for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
502             {
503                 p_pk->i_flags |= BLOCK_FLAG_HEADER;
504             }
505         }
506
507         /* Write regulary ProgramStreamMap */
508         if( p_sys->b_mpeg2 && p_sys->i_pes_count % 300 == 0 )
509         {
510             MuxWritePSM( p_mux, &p_ps, i_dts );
511         }
512
513         /* Get and mux a packet */
514         p_data = block_FifoGet( p_input->p_fifo );
515         EStoPES ( &p_data, p_input->p_fmt, p_stream->i_stream_id,
516                        p_sys->b_mpeg2, 0, 0, p_sys->i_pes_max_size, 0 );
517
518         block_ChainAppend( &p_ps, p_data );
519
520         /* Get size of output data so we can calculate the instant bitrate */
521         for( p_data = p_ps; p_data; p_data = p_data->p_next )
522         {
523             p_sys->i_instant_size += p_data->i_buffer;
524         }
525
526         sout_AccessOutWrite( p_mux->p_access, p_ps );
527
528         /* Increase counter */
529         p_sys->i_pes_count++;
530     }
531
532     return VLC_SUCCESS;
533 }
534
535 /*****************************************************************************
536  *
537  *****************************************************************************/
538 static void StreamIdInit( bool *id, int i_range )
539 {
540     int i;
541
542     for( i = 0; i < i_range; i++ )
543     {
544         id[i] = true;
545     }
546 }
547 static int StreamIdGet( bool *id, int i_id_min, int i_id_max )
548 {
549     int i;
550
551     for( i = 0; i <= i_id_max - i_id_min; i++ )
552     {
553         if( id[i] )
554         {
555             id[i] = false;
556
557             return i_id_min + i;
558         }
559     }
560     return -1;
561 }
562 static void StreamIdRelease( bool *id, int i_id_min, int i_id )
563 {
564     id[i_id - i_id_min] = true;
565 }
566
567 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
568                                 mtime_t i_dts )
569 {
570     sout_mux_sys_t *p_sys = p_mux->p_sys;
571     bits_buffer_t bits;
572     block_t *p_hdr;
573     mtime_t i_scr;
574     int i_mux_rate;
575
576     i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
577
578     p_hdr = block_Alloc( 18 );
579     p_hdr->i_pts = p_hdr->i_dts = i_dts;
580     bits_initwrite( &bits, 14, p_hdr->p_buffer );
581     bits_write( &bits, 32, 0x01ba );
582
583     /* The spec specifies that the mux rate must be rounded upwards */
584     i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
585
586     if( p_sys->b_mpeg2 )
587     {
588         bits_write( &bits, 2, 0x01 );
589     }
590     else
591     {
592         bits_write( &bits, 4, 0x02 );
593     }
594
595     bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
596     bits_write( &bits, 1,  1 ); // marker
597     bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
598     bits_write( &bits, 1,  1 ); // marker
599     bits_write( &bits, 15, i_scr&0x7fff );
600     bits_write( &bits, 1,  1 ); // marker
601
602     if( p_sys->b_mpeg2 )
603     {
604         bits_write( &bits, 9,  0 ); // src extension
605     }
606     bits_write( &bits, 1,  1 );     // marker
607
608     bits_write( &bits, 22, i_mux_rate);
609     bits_write( &bits, 1,  1 );     // marker
610
611     if( p_sys->b_mpeg2 )
612     {
613         bits_write( &bits, 1,  1 );     // marker
614         bits_write( &bits, 5,  0x1f );  // reserved
615         bits_write( &bits, 3,  0 );     // stuffing bytes
616     }
617
618     p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
619
620     block_ChainAppend( p_buf, p_hdr );
621 }
622
623 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
624                                   mtime_t i_dts )
625 {
626     sout_mux_sys_t  *p_sys = p_mux->p_sys;
627     block_t   *p_hdr;
628     bits_buffer_t   bits;
629     bool      b_private;
630     int i_rate_bound;
631
632     int             i_nb_private, i_nb_stream;
633     int i;
634
635     /* Count the number of private stream */
636     for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
637     {
638         ps_stream_t *p_stream;
639
640         p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
641
642         if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
643         {
644             i_nb_private++;
645         }
646     }
647
648     /* Private stream are declared only one time */
649     i_nb_stream = p_mux->i_nb_inputs -
650         ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
651
652     p_hdr = block_Alloc(  12 + i_nb_stream * 3 );
653     p_hdr->i_dts = p_hdr->i_pts = i_dts;
654
655     /* The spec specifies that the reported rate_bound must be upper limit */
656     i_rate_bound = (p_sys->i_rate_bound);
657
658     bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
659     bits_write( &bits, 32, 0x01bb );
660     bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
661     bits_write( &bits, 1,  1 ); // marker bit
662     bits_write( &bits, 22, i_rate_bound);
663     bits_write( &bits, 1,  1 ); // marker bit
664
665     bits_write( &bits, 6,  p_sys->i_audio_bound );
666     bits_write( &bits, 1,  0 ); // fixed flag
667     bits_write( &bits, 1,  0 ); // CSPS flag
668     bits_write( &bits, 1,  0 ); // system audio lock flag
669     bits_write( &bits, 1,  0 ); // system video lock flag
670
671     bits_write( &bits, 1,  1 ); // marker bit
672
673     bits_write( &bits, 5,  p_sys->i_video_bound );
674     bits_write( &bits, 1,  1 ); // packet rate restriction flag (1 for mpeg1)
675     bits_write( &bits, 7,  0xff ); // reserved bits
676
677     /* stream_id table */
678     for( i = 0, b_private = false; i < p_mux->i_nb_inputs; i++ )
679     {
680         sout_input_t *p_input;
681         ps_stream_t *p_stream;
682
683         p_input = p_mux->pp_inputs[i];
684         p_stream = (ps_stream_t *)p_input->p_sys;
685
686         if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
687         {
688             if( b_private )
689             {
690                 continue;
691             }
692             b_private = true;
693             /* Write stream id */
694             bits_write( &bits, 8, 0xbd );
695         }
696         else
697         {
698             /* Write stream id */
699             bits_write( &bits, 8, p_stream->i_stream_id&0xff );
700         }
701
702         bits_write( &bits, 2, 0x03 ); /* reserved */
703         if( p_input->p_fmt->i_cat == AUDIO_ES )
704         {
705             bits_write( &bits, 1, 0 );
706             bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
707         }
708         else if( p_input->p_fmt->i_cat == VIDEO_ES )
709         {
710             bits_write( &bits, 1, 1 );
711             bits_write( &bits, 13, p_stream->i_max_buff_size / 1024);
712         }
713         else
714         {
715             /* FIXME -- the scale of 0 means do a /128 */
716             bits_write( &bits, 1, 0 );
717             bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
718         }
719     }
720
721     block_ChainAppend( p_buf, p_hdr );
722 }
723
724 static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
725 {
726     sout_mux_sys_t *p_sys = p_mux->p_sys;
727     block_t *p_hdr;
728     bits_buffer_t bits;
729     int i, i_psm_size = 16, i_es_map_size = 0;
730
731     for( i = 0; i < p_mux->i_nb_inputs; i++ )
732     {
733         sout_input_t *p_input = p_mux->pp_inputs[i];
734         ps_stream_t *p_stream = p_input->p_sys;
735
736         i_es_map_size += 4;
737         if( p_stream->lang[0] != 0 ) i_es_map_size += 6;
738     }
739
740     i_psm_size += i_es_map_size;
741
742     p_hdr = block_Alloc( i_psm_size );
743     p_hdr->i_dts = p_hdr->i_pts = i_dts;
744
745     memset( p_hdr->p_buffer, 0, p_hdr->i_buffer );
746     bits_initwrite( &bits, i_psm_size, p_hdr->p_buffer );
747     bits_write( &bits, 32, 0x01bc );
748     bits_write( &bits, 16, i_psm_size - 6 );
749     bits_write( &bits, 1, 1 ); /* current_next_indicator */
750     bits_write( &bits, 2, 0xF ); /* reserved */
751     bits_write( &bits, 5, p_sys->i_psm_version );
752     bits_write( &bits, 7, 0xFF ); /* reserved */
753     bits_write( &bits, 1, 1 ); /* marker */
754
755     bits_write( &bits, 16, 0 ); /* program_stream_info_length */
756     /* empty */
757
758     bits_write( &bits, 16, i_es_map_size ); /* elementary_stream_map_length */
759     for( i = 0; i < p_mux->i_nb_inputs; i++ )
760     {
761         sout_input_t *p_input = p_mux->pp_inputs[i];
762         ps_stream_t *p_stream = p_input->p_sys;
763
764         bits_write( &bits, 8, p_stream->i_stream_type ); /* stream_type */
765         bits_write( &bits, 8, p_stream->i_stream_id ); /* elementary_stream_id */
766
767         /* ISO639 language descriptor */
768         if( p_stream->lang[0] != 0 )
769         {
770             bits_write( &bits, 16, 6 ); /* elementary_stream_info_length */
771
772             bits_write( &bits, 8, 0x0a ); /* descriptor_tag */
773             bits_write( &bits, 8, 4 ); /* descriptor_length */
774
775             bits_write( &bits, 8, p_stream->lang[0] );
776             bits_write( &bits, 8, p_stream->lang[1] );
777             bits_write( &bits, 8, p_stream->lang[2] );
778             bits_write( &bits, 8, 0 ); /* audio type: 0x00 undefined */
779         }
780         else
781         {
782             bits_write( &bits, 16, 0 ); /* elementary_stream_info_length */
783         }
784     }
785
786     /* CRC32 */
787     {
788         uint32_t i_crc = 0xffffffff;
789         for( i = 0; (size_t)i < p_hdr->i_buffer; i++ )
790         i_crc = (i_crc << 8) ^
791             p_sys->crc32_table[((i_crc >> 24) ^ p_hdr->p_buffer[i]) & 0xff];
792
793         bits_write( &bits, 32, i_crc );
794     }
795
796     block_ChainAppend( p_buf, p_hdr );
797 }