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 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
33 #include <vlc/input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 #define DTS_TEXT N_("DTS delay (ms)")
46 #define DTS_LONGTEXT N_("This option will 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 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
54 #define SOUT_CFG_PREFIX "sout-ps-"
57 set_description( _("PS muxer") );
58 set_shortname( "MPEG-PS" );
59 set_category( CAT_SOUT );
60 set_subcategory( SUBCAT_SOUT_MUX );
61 set_capability( "sout mux", 50 );
63 add_shortcut( "mpeg1" );
64 add_shortcut( "dvd" );
65 set_callbacks( Open, Close );
67 add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
68 DTS_LONGTEXT, VLC_TRUE );
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Control ( sout_mux_t *, int, va_list );
75 static int AddStream( sout_mux_t *, sout_input_t * );
76 static int DelStream( sout_mux_t *, sout_input_t * );
77 static int Mux ( sout_mux_t * );
79 /*****************************************************************************
81 *****************************************************************************/
82 static int MuxGetStream ( sout_mux_t *, int *, mtime_t * );
84 static void MuxWritePackHeader ( sout_mux_t *, block_t **, mtime_t );
85 static void MuxWriteSystemHeader( sout_mux_t *, block_t **, mtime_t );
86 static void MuxWritePSM ( sout_mux_t *, block_t **, mtime_t );
88 static void StreamIdInit ( vlc_bool_t *id, int i_range );
89 static int StreamIdGet ( vlc_bool_t *id, int i_id_min, int i_id_max );
90 static void StreamIdRelease ( vlc_bool_t *id, int i_id_min, int i_id );
92 typedef struct ps_stream_s
97 /* Language is iso639-2T */
102 struct sout_mux_sys_t
104 /* Which id are unused */
105 vlc_bool_t stream_id_mpga[16]; /* 0xc0 -> 0xcf */
106 vlc_bool_t stream_id_mpgv[16]; /* 0xe0 -> 0xef */
107 vlc_bool_t stream_id_a52[8]; /* 0x80 -> 0x87 <- FIXME I'm not sure */
108 vlc_bool_t stream_id_spu[32]; /* 0x20 -> 0x3f */
109 vlc_bool_t stream_id_dts[8]; /* 0x88 -> 0x8f */
110 vlc_bool_t stream_id_lpcm[16]; /* 0xa0 -> 0xaf */
118 int64_t i_instant_bitrate;
119 int64_t i_instant_size;
120 int64_t i_instant_dts;
125 uint32_t crc32_table[256];
128 static const char *ppsz_sout_options[] = {
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
137 sout_mux_t *p_mux = (sout_mux_t*)p_this;
138 sout_mux_sys_t *p_sys;
141 msg_Info( p_mux, "Open" );
142 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
144 p_mux->pf_control = Control;
145 p_mux->pf_addstream = AddStream;
146 p_mux->pf_delstream = DelStream;
148 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
150 /* Init free stream id */
151 StreamIdInit( p_sys->stream_id_a52, 8 );
152 StreamIdInit( p_sys->stream_id_dts, 8 );
153 StreamIdInit( p_sys->stream_id_mpga, 16 );
154 StreamIdInit( p_sys->stream_id_mpgv, 16 );
155 StreamIdInit( p_sys->stream_id_lpcm, 16 );
156 StreamIdInit( p_sys->stream_id_spu, 32 );
158 p_sys->i_audio_bound = 0;
159 p_sys->i_video_bound = 0;
160 p_sys->i_system_header = 0;
161 p_sys->i_pes_count = 0;
163 p_sys->i_psm_version = 0;
165 p_sys->i_instant_bitrate = 0;
166 p_sys->i_instant_size = 0;
167 p_sys->i_instant_dts = 0;
169 p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
171 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
172 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
174 /* Initialise CRC32 table */
179 for(i = 0; i < 256; i++)
182 for( j = (i << 24) | 0x800000; j != 0x80000000; j <<= 1 )
183 k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
185 p_sys->crc32_table[i] = k;
192 /*****************************************************************************
194 *****************************************************************************/
195 static void Close( vlc_object_t * p_this )
197 sout_mux_t *p_mux = (sout_mux_t*)p_this;
198 sout_mux_sys_t *p_sys = p_mux->p_sys;
202 msg_Info( p_mux, "Close" );
204 p_end = block_New( p_mux, 4 );
205 p_end->p_buffer[0] = 0x00; p_end->p_buffer[1] = 0x00;
206 p_end->p_buffer[2] = 0x01; p_end->p_buffer[3] = 0xb9;
208 sout_AccessOutWrite( p_mux->p_access, p_end );
213 /*****************************************************************************
215 *****************************************************************************/
216 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
223 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
224 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
228 case MUX_GET_ADD_STREAM_WAIT:
229 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
230 *pb_bool = VLC_FALSE;
234 ppsz = (char**)va_arg( args, char ** );
235 *ppsz = strdup( "video/mpeg" );
243 /*****************************************************************************
245 *****************************************************************************/
246 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
248 sout_mux_sys_t *p_sys = p_mux->p_sys;
249 ps_stream_t *p_stream;
251 msg_Dbg( p_mux, "adding input codec=%4.4s",
252 (char*)&p_input->p_fmt->i_codec );
254 p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
255 p_stream->i_stream_type = 0x81;
257 /* Init this new stream */
258 switch( p_input->p_fmt->i_codec )
260 case VLC_FOURCC( 'm', 'p', '1', 'v' ):
261 p_stream->i_stream_id =
262 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
263 p_stream->i_stream_type = 0x01; /* ISO/IEC 11172 Video */
265 case VLC_FOURCC( 'm', 'p', '2', 'v' ):
266 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
267 p_stream->i_stream_id =
268 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
269 p_stream->i_stream_type = 0x02; /* ISO/IEC 13818 Video */
271 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
272 p_stream->i_stream_id =
273 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
274 p_stream->i_stream_type = 0x10;
276 case VLC_FOURCC( 'h', '2', '6', '4' ):
277 p_stream->i_stream_id =
278 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
279 p_stream->i_stream_type = 0x1b;
281 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
282 p_stream->i_stream_id =
283 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
285 case VLC_FOURCC( 'd', 't', 's', ' ' ):
286 p_stream->i_stream_id =
287 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
289 case VLC_FOURCC( 'a', '5', '2', ' ' ):
290 p_stream->i_stream_id =
291 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
293 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
294 p_stream->i_stream_id =
295 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
296 p_stream->i_stream_type = 0x03; /* ISO/IEC 11172 Audio */
298 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
299 p_stream->i_stream_id =
300 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
301 p_stream->i_stream_type = 0x0f;
303 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
304 p_stream->i_stream_id =
305 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
311 if( p_stream->i_stream_id < 0 ) goto error;
313 if( p_input->p_fmt->i_cat == AUDIO_ES )
315 p_sys->i_audio_bound++;
317 else if( p_input->p_fmt->i_cat == VIDEO_ES )
319 p_sys->i_video_bound++;
322 /* Try to set a sensible default value for the instant bitrate */
323 p_sys->i_instant_bitrate += p_input->p_fmt->i_bitrate + 1000/* overhead */;
325 p_sys->i_psm_version++;
327 p_stream->lang[0] = p_stream->lang[1] = p_stream->lang[2] = 0;
328 if( p_input->p_fmt->psz_language )
330 char *psz = p_input->p_fmt->psz_language;
331 const iso639_lang_t *pl = NULL;
333 if( strlen( psz ) == 2 )
335 pl = GetLang_1( psz );
337 else if( strlen( psz ) == 3 )
339 pl = GetLang_2B( psz );
340 if( !strcmp( pl->psz_iso639_1, "??" ) )
342 pl = GetLang_2T( psz );
345 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
347 p_stream->lang[0] = pl->psz_iso639_2T[0];
348 p_stream->lang[1] = pl->psz_iso639_2T[1];
349 p_stream->lang[2] = pl->psz_iso639_2T[2];
351 msg_Dbg( p_mux, " - lang=%c%c%c",
352 p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
363 /*****************************************************************************
365 *****************************************************************************/
366 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
368 sout_mux_sys_t *p_sys = p_mux->p_sys;
369 ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
371 msg_Dbg( p_mux, "removing input" );
372 switch( p_input->p_fmt->i_codec )
374 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
375 StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
376 p_stream->i_stream_id );
378 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
379 StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
380 p_stream->i_stream_id&0xff );
382 case VLC_FOURCC( 'd', 't', 's', ' ' ):
383 StreamIdRelease( p_sys->stream_id_dts, 0x88,
384 p_stream->i_stream_id&0xff );
386 case VLC_FOURCC( 'a', '5', '2', ' ' ):
387 StreamIdRelease( p_sys->stream_id_a52, 0x80,
388 p_stream->i_stream_id&0xff );
390 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
391 StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
392 p_stream->i_stream_id );
394 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
395 StreamIdRelease( p_sys->stream_id_spu, 0x20,
396 p_stream->i_stream_id&0xff );
403 if( p_input->p_fmt->i_cat == AUDIO_ES )
405 p_sys->i_audio_bound--;
407 else if( p_input->p_fmt->i_cat == VIDEO_ES )
409 p_sys->i_video_bound--;
412 /* Try to set a sensible default value for the instant bitrate */
413 p_sys->i_instant_bitrate -= (p_input->p_fmt->i_bitrate + 1000);
415 p_sys->i_psm_version++;
421 /*****************************************************************************
422 * Mux: Call each time there is new data for at least one stream
423 *****************************************************************************/
424 static int Mux( sout_mux_t *p_mux )
426 sout_mux_sys_t *p_sys = p_mux->p_sys;
430 sout_input_t *p_input;
431 ps_stream_t *p_stream;
433 block_t *p_ps, *p_data;
438 /* Choose which stream to mux */
439 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
444 p_input = p_mux->pp_inputs[i_stream];
445 p_stream = (ps_stream_t*)p_input->p_sys;
448 /* Write regulary PackHeader */
449 if( p_sys->i_pes_count % 30 == 0)
451 /* Update the instant bitrate every second or so */
452 if( p_sys->i_instant_size &&
453 i_dts - p_sys->i_instant_dts > 1000000 )
455 int64_t i_instant_bitrate = p_sys->i_instant_size * 8000000 /
456 ( i_dts - p_sys->i_instant_dts );
458 p_sys->i_instant_bitrate += i_instant_bitrate;
459 p_sys->i_instant_bitrate /= 2;
461 p_sys->i_instant_size = 0;
462 p_sys->i_instant_dts = i_dts;
464 else if( !p_sys->i_instant_size )
466 p_sys->i_instant_dts = i_dts;
469 MuxWritePackHeader( p_mux, &p_ps, i_dts );
472 /* Write regulary SystemHeader */
473 if( p_sys->i_pes_count % 300 == 0 )
477 MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
479 /* For MPEG1 streaming, set HEADER flag */
480 for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
482 p_pk->i_flags |= BLOCK_FLAG_HEADER;
486 /* Write regulary ProgramStreamMap */
487 if( p_sys->b_mpeg2 && p_sys->i_pes_count % 300 == 0 )
489 MuxWritePSM( p_mux, &p_ps, i_dts );
492 /* Get and mux a packet */
493 p_data = block_FifoGet( p_input->p_fifo );
494 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
495 p_input->p_fmt, p_stream->i_stream_id,
496 p_sys->b_mpeg2, 0, 0 );
498 block_ChainAppend( &p_ps, p_data );
500 /* Get size of output data so we can calculate the instant bitrate */
501 for( p_data = p_ps; p_data; p_data = p_data->p_next )
503 p_sys->i_instant_size += p_data->i_buffer;
506 sout_AccessOutWrite( p_mux->p_access, p_ps );
508 /* Increase counter */
509 p_sys->i_pes_count++;
515 /*****************************************************************************
517 *****************************************************************************/
518 static void StreamIdInit( vlc_bool_t *id, int i_range )
522 for( i = 0; i < i_range; i++ )
527 static int StreamIdGet( vlc_bool_t *id, int i_id_min, int i_id_max )
531 for( i = 0; i <= i_id_max - i_id_min; i++ )
542 static void StreamIdRelease( vlc_bool_t *id, int i_id_min, int i_id )
544 id[i_id - i_id_min] = VLC_TRUE;
547 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
550 sout_mux_sys_t *p_sys = p_mux->p_sys;
556 i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
558 p_hdr = block_New( p_mux, 18 );
559 p_hdr->i_pts = p_hdr->i_dts = i_dts;
560 bits_initwrite( &bits, 14, p_hdr->p_buffer );
561 bits_write( &bits, 32, 0x01ba );
563 /* The spec specifies that the mux rate must be rounded upwards */
564 i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
568 bits_write( &bits, 2, 0x01 );
572 bits_write( &bits, 4, 0x02 );
575 bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
576 bits_write( &bits, 1, 1 );
577 bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
578 bits_write( &bits, 1, 1 );
579 bits_write( &bits, 15, i_scr&0x7fff );
580 bits_write( &bits, 1, 1 );
584 bits_write( &bits, 9, 0 ); // src extention
586 bits_write( &bits, 1, 1 );
588 bits_write( &bits, 22, i_mux_rate); // FIXME mux rate
589 bits_write( &bits, 1, 1 );
593 bits_write( &bits, 1, 1 );
594 bits_write( &bits, 5, 0x1f ); // FIXME reserved
595 bits_write( &bits, 3, 0 ); // stuffing bytes
598 p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
600 block_ChainAppend( p_buf, p_hdr );
603 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
606 sout_mux_sys_t *p_sys = p_mux->p_sys;
609 vlc_bool_t b_private;
612 int i_nb_private, i_nb_stream;
615 /* Count the number of private stream */
616 for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
618 ps_stream_t *p_stream;
620 p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
622 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
628 /* Private stream are declared only one time */
629 i_nb_stream = p_mux->i_nb_inputs -
630 ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
632 p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
633 p_hdr->i_dts = p_hdr->i_pts = i_dts;
635 /* The spec specifies that the mux rate must be rounded upwards */
636 i_mux_rate = (p_sys->i_instant_bitrate + 8 * 50 - 1 ) / (8 * 50);
638 bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
639 bits_write( &bits, 32, 0x01bb );
640 bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
641 bits_write( &bits, 1, 1 );
642 bits_write( &bits, 22, i_mux_rate); // FIXME rate bound
643 bits_write( &bits, 1, 1 );
645 bits_write( &bits, 6, p_sys->i_audio_bound );
646 bits_write( &bits, 1, 0 ); // fixed flag
647 bits_write( &bits, 1, 0 ); // CSPS flag
648 bits_write( &bits, 1, 0 ); // system audio lock flag
649 bits_write( &bits, 1, 0 ); // system video lock flag
651 bits_write( &bits, 1, 1 ); // marker bit
653 bits_write( &bits, 5, p_sys->i_video_bound );
654 bits_write( &bits, 1, 1 ); // packet rate restriction flag (1 for mpeg1)
655 bits_write( &bits, 7, 0xff ); // reserved bits
657 /* stream_id table */
658 for( i = 0, b_private = VLC_FALSE; i < p_mux->i_nb_inputs; i++ )
660 sout_input_t *p_input;
661 ps_stream_t *p_stream;
663 p_input = p_mux->pp_inputs[i];
664 p_stream = (ps_stream_t *)p_input->p_sys;
666 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
672 b_private = VLC_TRUE;
673 /* Write stream id */
674 bits_write( &bits, 8, 0xbd );
678 /* Write stream id */
679 bits_write( &bits, 8, p_stream->i_stream_id&0xff );
681 bits_write( &bits, 2, 0x03 );
682 if( p_input->p_fmt->i_cat == AUDIO_ES )
684 bits_write( &bits, 1, 0 );
685 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 128 );
687 else if( p_input->p_fmt->i_cat == VIDEO_ES )
689 bits_write( &bits, 1, 1 );
690 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 1024);
695 bits_write( &bits, 1, 0 );
696 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 );
700 block_ChainAppend( p_buf, p_hdr );
703 static void MuxWritePSM( sout_mux_t *p_mux, block_t **p_buf, mtime_t i_dts )
705 sout_mux_sys_t *p_sys = p_mux->p_sys;
708 int i, i_psm_size = 16, i_es_map_size = 0;
710 for( i = 0; i < p_mux->i_nb_inputs; i++ )
712 sout_input_t *p_input = p_mux->pp_inputs[i];
713 ps_stream_t *p_stream = p_input->p_sys;
716 if( p_stream->lang[0] != 0 ) i_es_map_size += 6;
719 i_psm_size += i_es_map_size;
721 p_hdr = block_New( p_mux, i_psm_size );
722 p_hdr->i_dts = p_hdr->i_pts = i_dts;
724 bits_initwrite( &bits, i_psm_size, p_hdr->p_buffer );
725 bits_write( &bits, 32, 0x01bc );
726 bits_write( &bits, 16, i_psm_size - 3 );
727 bits_write( &bits, 1, 1 ); /* current_next_indicator */
728 bits_write( &bits, 2, 0xF ); /* reserved */
729 bits_write( &bits, 5, p_sys->i_psm_version );
730 bits_write( &bits, 7, 0xFF ); /* reserved */
731 bits_write( &bits, 1, 1 ); /* marker */
733 bits_write( &bits, 16, 0 ); /* program_stream_info_length */
736 bits_write( &bits, 16, i_es_map_size ); /* elementary_stream_map_length */
737 for( i = 0; i < p_mux->i_nb_inputs; i++ )
739 sout_input_t *p_input = p_mux->pp_inputs[i];
740 ps_stream_t *p_stream = p_input->p_sys;
742 bits_write( &bits, 8, p_stream->i_stream_type ); /* stream_type */
743 bits_write( &bits, 8, p_stream->i_stream_id ); /* elementary_stream_id */
745 /* ISO639 language descriptor */
746 if( p_stream->lang[0] != 0 )
748 bits_write( &bits, 16, 6 ); /* elementary_stream_info_length */
750 bits_write( &bits, 8, 0x0a ); /* descriptor_tag */
751 bits_write( &bits, 8, 4 ); /* descriptor_length */
753 bits_write( &bits, 8, p_stream->lang[0] );
754 bits_write( &bits, 8, p_stream->lang[1] );
755 bits_write( &bits, 8, p_stream->lang[2] );
756 bits_write( &bits, 8, 0 ); /* audio type: 0x00 undefined */
760 bits_write( &bits, 16, 0 ); /* elementary_stream_info_length */
766 uint32_t i_crc = 0xffffffff;
767 for( i = 0; i < p_hdr->i_buffer; i++ )
768 i_crc = (i_crc << 8) ^
769 p_sys->crc32_table[((i_crc >> 24) ^ p_hdr->p_buffer[i]) & 0xff];
771 bits_write( &bits, 32, i_crc );
774 block_ChainAppend( p_buf, p_hdr );
778 * Find stream to be muxed.
780 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
786 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
788 sout_input_t *p_input = p_mux->pp_inputs[i];
791 if( p_input->p_fifo->i_depth <= 0 )
793 if( p_input->p_fmt->i_cat == AUDIO_ES ||
794 p_input->p_fmt->i_cat == VIDEO_ES )
796 /* We need that audio+video fifo contain at least 1 packet */
804 p_data = block_FifoShow( p_input->p_fifo );
805 if( i_stream == -1 || p_data->i_dts < i_dts )
808 i_dts = p_data->i_dts;
812 *pi_stream = i_stream;