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
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Eric Petit <titer@videolan.org>
10 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
33 #include <vlc_codecs.h>
34 #include <vlc_block.h>
41 /*****************************************************************************
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.")
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.")
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
57 #define SOUT_CFG_PREFIX "sout-ps-"
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 );
66 add_shortcut( "mpeg1" );
67 add_shortcut( "dvd" );
68 set_callbacks( Open, Close );
70 add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
71 DTS_LONGTEXT, VLC_TRUE );
72 add_integer( SOUT_CFG_PREFIX "pes-max-size", PES_PAYLOAD_SIZE_MAX, NULL,
73 PES_SIZE_TEXT, PES_SIZE_LONGTEXT, VLC_TRUE );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Control ( sout_mux_t *, int, va_list );
80 static int AddStream( sout_mux_t *, sout_input_t * );
81 static int DelStream( sout_mux_t *, sout_input_t * );
82 static int Mux ( sout_mux_t * );
84 /*****************************************************************************
86 *****************************************************************************/
87 static int MuxGetStream ( sout_mux_t *, int *, mtime_t * );
89 static void MuxWritePackHeader ( sout_mux_t *, block_t **, mtime_t );
90 static void MuxWriteSystemHeader( sout_mux_t *, block_t **, mtime_t );
91 static void MuxWritePSM ( sout_mux_t *, block_t **, mtime_t );
93 static void StreamIdInit ( vlc_bool_t *id, int i_range );
94 static int StreamIdGet ( vlc_bool_t *id, int i_id_min, int i_id_max );
95 static void StreamIdRelease ( vlc_bool_t *id, int i_id_min, int i_id );
97 typedef struct ps_stream_s
101 int i_max_buff_size; /* used in system header */
103 /* Language is iso639-2T */
108 struct sout_mux_sys_t
110 /* Which id are unused */
111 vlc_bool_t stream_id_mpga[16]; /* 0xc0 -> 0xcf */
112 vlc_bool_t stream_id_mpgv[16]; /* 0xe0 -> 0xef */
113 vlc_bool_t stream_id_a52[8]; /* 0x80 -> 0x87 <- FIXME I'm not sure */
114 vlc_bool_t stream_id_spu[32]; /* 0x20 -> 0x3f */
115 vlc_bool_t stream_id_dts[8]; /* 0x88 -> 0x8f */
116 vlc_bool_t stream_id_lpcm[16]; /* 0xa0 -> 0xaf */
123 int i_rate_bound; /* units of 50 bytes/second */
125 int64_t i_instant_bitrate;
126 int64_t i_instant_size;
127 int64_t i_instant_dts;
134 uint32_t crc32_table[256];
137 static const char *ppsz_sout_options[] = {
138 "dts-delay", "pes-max-size", NULL
141 /*****************************************************************************
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 sout_mux_t *p_mux = (sout_mux_t*)p_this;
147 sout_mux_sys_t *p_sys;
150 msg_Info( p_mux, "Open" );
151 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
153 p_mux->pf_control = Control;
154 p_mux->pf_addstream = AddStream;
155 p_mux->pf_delstream = DelStream;
157 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
159 /* Init free stream id */
160 StreamIdInit( p_sys->stream_id_a52, 8 );
161 StreamIdInit( p_sys->stream_id_dts, 8 );
162 StreamIdInit( p_sys->stream_id_mpga, 16 );
163 StreamIdInit( p_sys->stream_id_mpgv, 16 );
164 StreamIdInit( p_sys->stream_id_lpcm, 16 );
165 StreamIdInit( p_sys->stream_id_spu, 32 );
167 p_sys->i_audio_bound = 0;
168 p_sys->i_video_bound = 0;
169 p_sys->i_system_header = 0;
170 p_sys->i_pes_count = 0;
172 p_sys->i_psm_version = 0;
174 p_sys->i_instant_bitrate = 0;
175 p_sys->i_instant_size = 0;
176 p_sys->i_instant_dts = 0;
177 p_sys->i_rate_bound = 0;
178 p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
180 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
181 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
183 var_Get( p_mux, SOUT_CFG_PREFIX "pes-max-size", &val );
184 p_sys->i_pes_max_size = (int64_t)val.i_int;
186 /* Initialise CRC32 table */
191 for( i = 0; i < 256; i++ )
194 for( j = (i << 24) | 0x800000; j != 0x80000000; j <<= 1 )
195 k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
197 p_sys->crc32_table[i] = k;
204 /*****************************************************************************
206 *****************************************************************************/
207 static void Close( vlc_object_t * p_this )
209 sout_mux_t *p_mux = (sout_mux_t*)p_this;
210 sout_mux_sys_t *p_sys = p_mux->p_sys;
214 msg_Info( p_mux, "Close" );
216 p_end = block_New( p_mux, 4 );
217 p_end->p_buffer[0] = 0x00; p_end->p_buffer[1] = 0x00;
218 p_end->p_buffer[2] = 0x01; p_end->p_buffer[3] = 0xb9;
220 sout_AccessOutWrite( p_mux->p_access, p_end );
225 /*****************************************************************************
227 *****************************************************************************/
228 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
235 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
236 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
240 case MUX_GET_ADD_STREAM_WAIT:
241 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
242 *pb_bool = VLC_FALSE;
246 ppsz = (char**)va_arg( args, char ** );
247 *ppsz = strdup( "video/mpeg" );
255 /*****************************************************************************
257 *****************************************************************************/
258 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
260 sout_mux_sys_t *p_sys = p_mux->p_sys;
261 ps_stream_t *p_stream;
264 msg_Dbg( p_mux, "adding input codec=%4.4s",
265 (char*)&p_input->p_fmt->i_codec );
267 p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
268 p_stream->i_stream_type = 0x81;
270 /* Init this new stream */
271 switch( p_input->p_fmt->i_codec )
273 case VLC_FOURCC( 'm', 'p', '1', 'v' ):
274 p_stream->i_stream_id =
275 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
276 p_stream->i_stream_type = 0x01; /* ISO/IEC 11172 Video */
278 case VLC_FOURCC( 'm', 'p', '2', 'v' ):
279 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
280 p_stream->i_stream_id =
281 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
282 p_stream->i_stream_type = 0x02; /* ISO/IEC 13818 Video */
284 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
285 p_stream->i_stream_id =
286 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
287 p_stream->i_stream_type = 0x10;
289 case VLC_FOURCC( 'h', '2', '6', '4' ):
290 p_stream->i_stream_id =
291 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
292 p_stream->i_stream_type = 0x1b;
294 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
295 p_stream->i_stream_id =
296 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
298 case VLC_FOURCC( 'd', 't', 's', ' ' ):
299 p_stream->i_stream_id =
300 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
302 case VLC_FOURCC( 'a', '5', '2', ' ' ):
303 p_stream->i_stream_id =
304 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
306 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
307 p_stream->i_stream_id =
308 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
309 p_stream->i_stream_type = 0x03; /* ISO/IEC 11172 Audio */
311 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
312 p_stream->i_stream_id =
313 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
314 p_stream->i_stream_type = 0x0f;
316 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
317 p_stream->i_stream_id =
318 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
324 if( p_stream->i_stream_id < 0 ) goto error;
326 if( p_input->p_fmt->i_cat == AUDIO_ES )
328 p_sys->i_audio_bound++;
329 p_stream->i_max_buff_size = 4 * 1024;
331 else if( p_input->p_fmt->i_cat == VIDEO_ES )
333 p_sys->i_video_bound++;
334 p_stream->i_max_buff_size = 400 * 1024; /* FIXME -- VCD uses 46, SVCD
335 uses 230, ffmpeg has 230 with a note that it is small */
338 { /* FIXME -- what's valid for not audio or video? */
339 p_stream->i_max_buff_size = 4 * 1024;
342 /* Try to set a sensible default value for the instant bitrate */
343 p_sys->i_instant_bitrate += p_input->p_fmt->i_bitrate + 1000/* overhead */;
345 /* FIXME -- spec requires an upper limit rate boundary in the system header;
346 our codecs are VBR; using 2x nominal rate, convert to 50 bytes/sec */
347 p_sys->i_rate_bound += p_input->p_fmt->i_bitrate * 2 / (8 * 50);
348 p_sys->i_psm_version++;
350 p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
351 if( p_input->p_fmt->psz_language )
353 char *psz = p_input->p_fmt->psz_language;
354 const iso639_lang_t *pl = NULL;
356 if( strlen( psz ) == 2 )
358 pl = GetLang_1( psz );
360 else if( strlen( psz ) == 3 )
362 pl = GetLang_2B( psz );
363 if( !strcmp( pl->psz_iso639_1, "??" ) )
365 pl = GetLang_2T( psz );
368 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
370 p_stream->lang[0] = pl->psz_iso639_2T[0];
371 p_stream->lang[1] = pl->psz_iso639_2T[1];
372 p_stream->lang[2] = pl->psz_iso639_2T[2];
374 msg_Dbg( p_mux, " - lang=%c%c%c",
375 p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
385 /*****************************************************************************
387 *****************************************************************************/
388 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
390 sout_mux_sys_t *p_sys = p_mux->p_sys;
391 ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
393 msg_Dbg( p_mux, "removing input" );
394 switch( p_input->p_fmt->i_codec )
396 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
397 StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
398 p_stream->i_stream_id );
400 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
401 StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
402 p_stream->i_stream_id&0xff );
404 case VLC_FOURCC( 'd', 't', 's', ' ' ):
405 StreamIdRelease( p_sys->stream_id_dts, 0x88,
406 p_stream->i_stream_id&0xff );
408 case VLC_FOURCC( 'a', '5', '2', ' ' ):
409 StreamIdRelease( p_sys->stream_id_a52, 0x80,
410 p_stream->i_stream_id&0xff );
412 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
413 StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
414 p_stream->i_stream_id );
416 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
417 StreamIdRelease( p_sys->stream_id_spu, 0x20,
418 p_stream->i_stream_id&0xff );
425 if( p_input->p_fmt->i_cat == AUDIO_ES )
427 p_sys->i_audio_bound--;
429 else if( p_input->p_fmt->i_cat == VIDEO_ES )
431 p_sys->i_video_bound--;
434 /* Try to set a sensible default value for the instant bitrate */
435 p_sys->i_instant_bitrate -= (p_input->p_fmt->i_bitrate + 1000);
436 /* rate_bound is in units of 50 bytes/second */
437 p_sys->i_rate_bound -= (p_input->p_fmt->i_bitrate * 2)/(8 * 50);
439 p_sys->i_psm_version++;
445 /*****************************************************************************
446 * Mux: Call each time there is new data for at least one stream
447 *****************************************************************************/
448 static int Mux( sout_mux_t *p_mux )
450 sout_mux_sys_t *p_sys = p_mux->p_sys;
454 sout_input_t *p_input;
455 ps_stream_t *p_stream;
457 block_t *p_ps, *p_data;
462 /* Choose which stream to mux */
463 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
468 p_input = p_mux->pp_inputs[i_stream];
469 p_stream = (ps_stream_t*)p_input->p_sys;
472 /* Write regulary PackHeader */
473 if( p_sys->i_pes_count % 30 == 0)
475 /* Update the instant bitrate every second or so */
476 if( p_sys->i_instant_size &&
477 i_dts - p_sys->i_instant_dts > 1000000 )
479 int64_t i_instant_bitrate = p_sys->i_instant_size * 8000000 /
480 ( i_dts - p_sys->i_instant_dts );
482 p_sys->i_instant_bitrate += i_instant_bitrate;
483 p_sys->i_instant_bitrate /= 2;
485 p_sys->i_instant_size = 0;
486 p_sys->i_instant_dts = i_dts;
488 else if( !p_sys->i_instant_size )
490 p_sys->i_instant_dts = i_dts;
493 MuxWritePackHeader( p_mux, &p_ps, i_dts );
496 /* Write regulary SystemHeader */
497 if( p_sys->i_pes_count % 300 == 0 )
501 MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
503 /* For MPEG1 streaming, set HEADER flag */
504 for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
506 p_pk->i_flags |= BLOCK_FLAG_HEADER;
510 /* Write regulary ProgramStreamMap */
511 if( p_sys->b_mpeg2 && p_sys->i_pes_count % 300 == 0 )
513 MuxWritePSM( p_mux, &p_ps, i_dts );
516 /* Get and mux a packet */
517 p_data = block_FifoGet( p_input->p_fifo );
518 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
519 p_input->p_fmt, p_stream->i_stream_id,
520 p_sys->b_mpeg2, 0, 0, p_sys->i_pes_max_size );
522 block_ChainAppend( &p_ps, p_data );
524 /* Get size of output data so we can calculate the instant bitrate */
525 for( p_data = p_ps; p_data; p_data = p_data->p_next )
527 p_sys->i_instant_size += p_data->i_buffer;
530 sout_AccessOutWrite( p_mux->p_access, p_ps );
532 /* Increase counter */
533 p_sys->i_pes_count++;
539 /*****************************************************************************
541 *****************************************************************************/
542 static void StreamIdInit( vlc_bool_t *id, int i_range )
546 for( i = 0; i < i_range; i++ )
551 static int StreamIdGet( vlc_bool_t *id, int i_id_min, int i_id_max )
555 for( i = 0; i <= i_id_max - i_id_min; i++ )
566 static void StreamIdRelease( vlc_bool_t *id, int i_id_min, int i_id )
568 id[i_id - i_id_min] = VLC_TRUE;
571 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
574 sout_mux_sys_t *p_sys = p_mux->p_sys;
580 i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
582 p_hdr = block_New( p_mux, 18 );
583 p_hdr->i_pts = p_hdr->i_dts = i_dts;
584 bits_initwrite( &bits, 14, p_hdr->p_buffer );
585 bits_write( &bits, 32, 0x01ba );
587 /* The spec specifies that the mux rate must be rounded upwards */
588 i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
592 bits_write( &bits, 2, 0x01 );
596 bits_write( &bits, 4, 0x02 );
599 bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
600 bits_write( &bits, 1, 1 ); // marker
601 bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
602 bits_write( &bits, 1, 1 ); // marker
603 bits_write( &bits, 15, i_scr&0x7fff );
604 bits_write( &bits, 1, 1 ); // marker
608 bits_write( &bits, 9, 0 ); // src extension
610 bits_write( &bits, 1, 1 ); // marker
612 bits_write( &bits, 22, i_mux_rate);
613 bits_write( &bits, 1, 1 ); // marker
617 bits_write( &bits, 1, 1 ); // marker
618 bits_write( &bits, 5, 0x1f ); // reserved
619 bits_write( &bits, 3, 0 ); // stuffing bytes
622 p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
624 block_ChainAppend( p_buf, p_hdr );
627 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
630 sout_mux_sys_t *p_sys = p_mux->p_sys;
633 vlc_bool_t b_private;
636 int i_nb_private, i_nb_stream;
639 /* Count the number of private stream */
640 for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
642 ps_stream_t *p_stream;
644 p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
646 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
652 /* Private stream are declared only one time */
653 i_nb_stream = p_mux->i_nb_inputs -
654 ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
656 p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
657 p_hdr->i_dts = p_hdr->i_pts = i_dts;
659 /* The spec specifies that the reported rate_bound must be upper limit */
660 i_rate_bound = (p_sys->i_rate_bound);
662 bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
663 bits_write( &bits, 32, 0x01bb );
664 bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
665 bits_write( &bits, 1, 1 ); // marker bit
666 bits_write( &bits, 22, i_rate_bound);
667 bits_write( &bits, 1, 1 ); // marker bit
669 bits_write( &bits, 6, p_sys->i_audio_bound );
670 bits_write( &bits, 1, 0 ); // fixed flag
671 bits_write( &bits, 1, 0 ); // CSPS flag
672 bits_write( &bits, 1, 0 ); // system audio lock flag
673 bits_write( &bits, 1, 0 ); // system video lock flag
675 bits_write( &bits, 1, 1 ); // marker bit
677 bits_write( &bits, 5, p_sys->i_video_bound );
678 bits_write( &bits, 1, 1 ); // packet rate restriction flag (1 for mpeg1)
679 bits_write( &bits, 7, 0xff ); // reserved bits
681 /* stream_id table */
682 for( i = 0, b_private = VLC_FALSE; i < p_mux->i_nb_inputs; i++ )
684 sout_input_t *p_input;
685 ps_stream_t *p_stream;
687 p_input = p_mux->pp_inputs[i];
688 p_stream = (ps_stream_t *)p_input->p_sys;
690 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
696 b_private = VLC_TRUE;
697 /* Write stream id */
698 bits_write( &bits, 8, 0xbd );
702 /* Write stream id */
703 bits_write( &bits, 8, p_stream->i_stream_id&0xff );
706 bits_write( &bits, 2, 0x03 ); /* reserved */
707 if( p_input->p_fmt->i_cat == AUDIO_ES )
709 bits_write( &bits, 1, 0 );
710 bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
712 else if( p_input->p_fmt->i_cat == VIDEO_ES )
714 bits_write( &bits, 1, 1 );
715 bits_write( &bits, 13, p_stream->i_max_buff_size / 1024);
719 /* FIXME -- the scale of 0 means do a /128 */
720 bits_write( &bits, 1, 0 );
721 bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
725 block_ChainAppend( p_buf, p_hdr );
728 static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
730 sout_mux_sys_t *p_sys = p_mux->p_sys;
733 int i, i_psm_size = 16, i_es_map_size = 0;
735 for( i = 0; i < p_mux->i_nb_inputs; i++ )
737 sout_input_t *p_input = p_mux->pp_inputs[i];
738 ps_stream_t *p_stream = p_input->p_sys;
741 if( p_stream->lang[0] != 0 ) i_es_map_size += 6;
744 i_psm_size += i_es_map_size;
746 p_hdr = block_New( p_mux, i_psm_size );
747 p_hdr->i_dts = p_hdr->i_pts = i_dts;
749 memset( p_hdr->p_buffer, 0, p_hdr->i_buffer );
750 bits_initwrite( &bits, i_psm_size, p_hdr->p_buffer );
751 bits_write( &bits, 32, 0x01bc );
752 bits_write( &bits, 16, i_psm_size - 6 );
753 bits_write( &bits, 1, 1 ); /* current_next_indicator */
754 bits_write( &bits, 2, 0xF ); /* reserved */
755 bits_write( &bits, 5, p_sys->i_psm_version );
756 bits_write( &bits, 7, 0xFF ); /* reserved */
757 bits_write( &bits, 1, 1 ); /* marker */
759 bits_write( &bits, 16, 0 ); /* program_stream_info_length */
762 bits_write( &bits, 16, i_es_map_size ); /* elementary_stream_map_length */
763 for( i = 0; i < p_mux->i_nb_inputs; i++ )
765 sout_input_t *p_input = p_mux->pp_inputs[i];
766 ps_stream_t *p_stream = p_input->p_sys;
768 bits_write( &bits, 8, p_stream->i_stream_type ); /* stream_type */
769 bits_write( &bits, 8, p_stream->i_stream_id ); /* elementary_stream_id */
771 /* ISO639 language descriptor */
772 if( p_stream->lang[0] != 0 )
774 bits_write( &bits, 16, 6 ); /* elementary_stream_info_length */
776 bits_write( &bits, 8, 0x0a ); /* descriptor_tag */
777 bits_write( &bits, 8, 4 ); /* descriptor_length */
779 bits_write( &bits, 8, p_stream->lang[0] );
780 bits_write( &bits, 8, p_stream->lang[1] );
781 bits_write( &bits, 8, p_stream->lang[2] );
782 bits_write( &bits, 8, 0 ); /* audio type: 0x00 undefined */
786 bits_write( &bits, 16, 0 ); /* elementary_stream_info_length */
792 uint32_t i_crc = 0xffffffff;
793 for( i = 0; i < p_hdr->i_buffer; i++ )
794 i_crc = (i_crc << 8) ^
795 p_sys->crc32_table[((i_crc >> 24) ^ p_hdr->p_buffer[i]) & 0xff];
797 bits_write( &bits, 32, i_crc );
800 block_ChainAppend( p_buf, p_hdr );
804 * Find stream to be muxed.
806 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
812 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
814 sout_input_t *p_input = p_mux->pp_inputs[i];
817 if( p_input->p_fifo->i_depth <= 0 )
819 if( p_input->p_fmt->i_cat == AUDIO_ES ||
820 p_input->p_fmt->i_cat == VIDEO_ES )
822 /* We need that audio+video fifo contain at least 1 packet */
830 p_data = block_FifoShow( p_input->p_fifo );
831 if( i_stream == -1 || p_data->i_dts < i_dts )
834 i_dts = p_data->i_dts;
838 *pi_stream = i_stream;