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/input.h>
42 /*****************************************************************************
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.")
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.")
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
58 #define SOUT_CFG_PREFIX "sout-ps-"
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 );
67 add_shortcut( "mpeg1" );
68 add_shortcut( "dvd" );
69 set_callbacks( Open, Close );
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 );
77 /*****************************************************************************
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 * );
85 /*****************************************************************************
87 *****************************************************************************/
88 static int MuxGetStream ( sout_mux_t *, int *, mtime_t * );
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 );
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 );
98 typedef struct ps_stream_s
102 int i_max_buff_size; /* used in system header */
104 /* Language is iso639-2T */
109 struct sout_mux_sys_t
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 */
124 int i_rate_bound; /* units of 50 bytes/second */
126 int64_t i_instant_bitrate;
127 int64_t i_instant_size;
128 int64_t i_instant_dts;
135 uint32_t crc32_table[256];
138 static const char *ppsz_sout_options[] = {
139 "dts-delay", "pes-max-size", NULL
142 /*****************************************************************************
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 sout_mux_t *p_mux = (sout_mux_t*)p_this;
148 sout_mux_sys_t *p_sys;
151 msg_Info( p_mux, "Open" );
152 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
154 p_mux->pf_control = Control;
155 p_mux->pf_addstream = AddStream;
156 p_mux->pf_delstream = DelStream;
158 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
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 );
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;
173 p_sys->i_psm_version = 0;
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" ));
181 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
182 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
184 var_Get( p_mux, SOUT_CFG_PREFIX "pes-max-size", &val );
185 p_sys->i_pes_max_size = (int64_t)val.i_int;
187 /* Initialise CRC32 table */
192 for( i = 0; i < 256; i++ )
195 for( j = (i << 24) | 0x800000; j != 0x80000000; j <<= 1 )
196 k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
198 p_sys->crc32_table[i] = k;
205 /*****************************************************************************
207 *****************************************************************************/
208 static void Close( vlc_object_t * p_this )
210 sout_mux_t *p_mux = (sout_mux_t*)p_this;
211 sout_mux_sys_t *p_sys = p_mux->p_sys;
215 msg_Info( p_mux, "Close" );
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;
221 sout_AccessOutWrite( p_mux->p_access, p_end );
226 /*****************************************************************************
228 *****************************************************************************/
229 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
236 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
237 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
241 case MUX_GET_ADD_STREAM_WAIT:
242 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
243 *pb_bool = VLC_FALSE;
247 ppsz = (char**)va_arg( args, char ** );
248 *ppsz = strdup( "video/mpeg" );
256 /*****************************************************************************
258 *****************************************************************************/
259 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
261 sout_mux_sys_t *p_sys = p_mux->p_sys;
262 ps_stream_t *p_stream;
265 msg_Dbg( p_mux, "adding input codec=%4.4s",
266 (char*)&p_input->p_fmt->i_codec );
268 p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
269 p_stream->i_stream_type = 0x81;
271 /* Init this new stream */
272 switch( p_input->p_fmt->i_codec )
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 */
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 */
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;
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;
295 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
296 p_stream->i_stream_id =
297 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
299 case VLC_FOURCC( 'd', 't', 's', ' ' ):
300 p_stream->i_stream_id =
301 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
303 case VLC_FOURCC( 'a', '5', '2', ' ' ):
304 p_stream->i_stream_id =
305 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
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 */
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;
317 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
318 p_stream->i_stream_id =
319 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
325 if( p_stream->i_stream_id < 0 ) goto error;
327 if( p_input->p_fmt->i_cat == AUDIO_ES )
329 p_sys->i_audio_bound++;
330 p_stream->i_max_buff_size = 4 * 1024;
332 else if( p_input->p_fmt->i_cat == VIDEO_ES )
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 */
339 { /* FIXME -- what's valid for not audio or video? */
340 p_stream->i_max_buff_size = 4 * 1024;
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 */;
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++;
351 p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
352 if( p_input->p_fmt->psz_language )
354 char *psz = p_input->p_fmt->psz_language;
355 const iso639_lang_t *pl = NULL;
357 if( strlen( psz ) == 2 )
359 pl = GetLang_1( psz );
361 else if( strlen( psz ) == 3 )
363 pl = GetLang_2B( psz );
364 if( !strcmp( pl->psz_iso639_1, "??" ) )
366 pl = GetLang_2T( psz );
369 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
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];
375 msg_Dbg( p_mux, " - lang=%c%c%c",
376 p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
386 /*****************************************************************************
388 *****************************************************************************/
389 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
391 sout_mux_sys_t *p_sys = p_mux->p_sys;
392 ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
394 msg_Dbg( p_mux, "removing input" );
395 switch( p_input->p_fmt->i_codec )
397 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
398 StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
399 p_stream->i_stream_id );
401 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
402 StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
403 p_stream->i_stream_id&0xff );
405 case VLC_FOURCC( 'd', 't', 's', ' ' ):
406 StreamIdRelease( p_sys->stream_id_dts, 0x88,
407 p_stream->i_stream_id&0xff );
409 case VLC_FOURCC( 'a', '5', '2', ' ' ):
410 StreamIdRelease( p_sys->stream_id_a52, 0x80,
411 p_stream->i_stream_id&0xff );
413 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
414 StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
415 p_stream->i_stream_id );
417 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
418 StreamIdRelease( p_sys->stream_id_spu, 0x20,
419 p_stream->i_stream_id&0xff );
426 if( p_input->p_fmt->i_cat == AUDIO_ES )
428 p_sys->i_audio_bound--;
430 else if( p_input->p_fmt->i_cat == VIDEO_ES )
432 p_sys->i_video_bound--;
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);
440 p_sys->i_psm_version++;
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 )
451 sout_mux_sys_t *p_sys = p_mux->p_sys;
455 sout_input_t *p_input;
456 ps_stream_t *p_stream;
458 block_t *p_ps, *p_data;
463 /* Choose which stream to mux */
464 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
469 p_input = p_mux->pp_inputs[i_stream];
470 p_stream = (ps_stream_t*)p_input->p_sys;
473 /* Write regulary PackHeader */
474 if( p_sys->i_pes_count % 30 == 0)
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 )
480 int64_t i_instant_bitrate = p_sys->i_instant_size * 8000000 /
481 ( i_dts - p_sys->i_instant_dts );
483 p_sys->i_instant_bitrate += i_instant_bitrate;
484 p_sys->i_instant_bitrate /= 2;
486 p_sys->i_instant_size = 0;
487 p_sys->i_instant_dts = i_dts;
489 else if( !p_sys->i_instant_size )
491 p_sys->i_instant_dts = i_dts;
494 MuxWritePackHeader( p_mux, &p_ps, i_dts );
497 /* Write regulary SystemHeader */
498 if( p_sys->i_pes_count % 300 == 0 )
502 MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
504 /* For MPEG1 streaming, set HEADER flag */
505 for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
507 p_pk->i_flags |= BLOCK_FLAG_HEADER;
511 /* Write regulary ProgramStreamMap */
512 if( p_sys->b_mpeg2 && p_sys->i_pes_count % 300 == 0 )
514 MuxWritePSM( p_mux, &p_ps, i_dts );
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 );
523 block_ChainAppend( &p_ps, p_data );
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 )
528 p_sys->i_instant_size += p_data->i_buffer;
531 sout_AccessOutWrite( p_mux->p_access, p_ps );
533 /* Increase counter */
534 p_sys->i_pes_count++;
540 /*****************************************************************************
542 *****************************************************************************/
543 static void StreamIdInit( vlc_bool_t *id, int i_range )
547 for( i = 0; i < i_range; i++ )
552 static int StreamIdGet( vlc_bool_t *id, int i_id_min, int i_id_max )
556 for( i = 0; i <= i_id_max - i_id_min; i++ )
567 static void StreamIdRelease( vlc_bool_t *id, int i_id_min, int i_id )
569 id[i_id - i_id_min] = VLC_TRUE;
572 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
575 sout_mux_sys_t *p_sys = p_mux->p_sys;
581 i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
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 );
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);
593 bits_write( &bits, 2, 0x01 );
597 bits_write( &bits, 4, 0x02 );
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
609 bits_write( &bits, 9, 0 ); // src extension
611 bits_write( &bits, 1, 1 ); // marker
613 bits_write( &bits, 22, i_mux_rate);
614 bits_write( &bits, 1, 1 ); // marker
618 bits_write( &bits, 1, 1 ); // marker
619 bits_write( &bits, 5, 0x1f ); // reserved
620 bits_write( &bits, 3, 0 ); // stuffing bytes
623 p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
625 block_ChainAppend( p_buf, p_hdr );
628 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
631 sout_mux_sys_t *p_sys = p_mux->p_sys;
634 vlc_bool_t b_private;
637 int i_nb_private, i_nb_stream;
640 /* Count the number of private stream */
641 for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
643 ps_stream_t *p_stream;
645 p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
647 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
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 );
657 p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
658 p_hdr->i_dts = p_hdr->i_pts = i_dts;
660 /* The spec specifies that the reported rate_bound must be upper limit */
661 i_rate_bound = (p_sys->i_rate_bound);
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
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
676 bits_write( &bits, 1, 1 ); // marker bit
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
682 /* stream_id table */
683 for( i = 0, b_private = VLC_FALSE; i < p_mux->i_nb_inputs; i++ )
685 sout_input_t *p_input;
686 ps_stream_t *p_stream;
688 p_input = p_mux->pp_inputs[i];
689 p_stream = (ps_stream_t *)p_input->p_sys;
691 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
697 b_private = VLC_TRUE;
698 /* Write stream id */
699 bits_write( &bits, 8, 0xbd );
703 /* Write stream id */
704 bits_write( &bits, 8, p_stream->i_stream_id&0xff );
707 bits_write( &bits, 2, 0x03 ); /* reserved */
708 if( p_input->p_fmt->i_cat == AUDIO_ES )
710 bits_write( &bits, 1, 0 );
711 bits_write( &bits, 13, p_stream->i_max_buff_size / 128 );
713 else if( p_input->p_fmt->i_cat == VIDEO_ES )
715 bits_write( &bits, 1, 1 );
716 bits_write( &bits, 13, p_stream->i_max_buff_size / 1024);
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 );
726 block_ChainAppend( p_buf, p_hdr );
729 static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
731 sout_mux_sys_t *p_sys = p_mux->p_sys;
734 int i, i_psm_size = 16, i_es_map_size = 0;
736 for( i = 0; i < p_mux->i_nb_inputs; i++ )
738 sout_input_t *p_input = p_mux->pp_inputs[i];
739 ps_stream_t *p_stream = p_input->p_sys;
742 if( p_stream->lang[0] != 0 ) i_es_map_size += 6;
745 i_psm_size += i_es_map_size;
747 p_hdr = block_New( p_mux, i_psm_size );
748 p_hdr->i_dts = p_hdr->i_pts = i_dts;
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 */
760 bits_write( &bits, 16, 0 ); /* program_stream_info_length */
763 bits_write( &bits, 16, i_es_map_size ); /* elementary_stream_map_length */
764 for( i = 0; i < p_mux->i_nb_inputs; i++ )
766 sout_input_t *p_input = p_mux->pp_inputs[i];
767 ps_stream_t *p_stream = p_input->p_sys;
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 */
772 /* ISO639 language descriptor */
773 if( p_stream->lang[0] != 0 )
775 bits_write( &bits, 16, 6 ); /* elementary_stream_info_length */
777 bits_write( &bits, 8, 0x0a ); /* descriptor_tag */
778 bits_write( &bits, 8, 4 ); /* descriptor_length */
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 */
787 bits_write( &bits, 16, 0 ); /* elementary_stream_info_length */
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];
798 bits_write( &bits, 32, i_crc );
801 block_ChainAppend( p_buf, p_hdr );
805 * Find stream to be muxed.
807 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
813 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
815 sout_input_t *p_input = p_mux->pp_inputs[i];
818 if( p_input->p_fifo->i_depth <= 0 )
820 if( p_input->p_fmt->i_cat == AUDIO_ES ||
821 p_input->p_fmt->i_cat == VIDEO_ES )
823 /* We need that audio+video fifo contain at least 1 packet */
831 p_data = block_FifoShow( p_input->p_fifo );
832 if( i_stream == -1 || p_data->i_dts < i_dts )
835 i_dts = p_data->i_dts;
839 *pi_stream = i_stream;