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>
40 /*****************************************************************************
42 *****************************************************************************/
43 #define DTS_TEXT N_("DTS delay (ms)")
44 #define DTS_LONGTEXT N_("This option will delay the DTS (decoding time " \
45 "stamps) and PTS (presentation timestamps) of the data in the " \
46 "stream, compared to the SCRs. This allows for some buffering inside " \
47 "the client decoder.")
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 #define SOUT_CFG_PREFIX "sout-ps-"
55 set_description( _("PS muxer") );
56 set_capability( "sout mux", 50 );
58 add_shortcut( "mpeg1" );
59 add_shortcut( "dvd" );
60 set_callbacks( Open, Close );
62 add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
63 DTS_LONGTEXT, VLC_TRUE );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Control ( sout_mux_t *, int, va_list );
70 static int AddStream( sout_mux_t *, sout_input_t * );
71 static int DelStream( sout_mux_t *, sout_input_t * );
72 static int Mux ( sout_mux_t * );
74 /*****************************************************************************
76 *****************************************************************************/
77 static int MuxGetStream ( sout_mux_t *, int *, mtime_t * );
79 static void MuxWritePackHeader ( sout_mux_t *, block_t **, mtime_t );
80 static void MuxWriteSystemHeader( sout_mux_t *, block_t **, mtime_t );
82 static void StreamIdInit ( vlc_bool_t *id, int i_range );
83 static int StreamIdGet ( vlc_bool_t *id, int i_id_min, int i_id_max );
84 static void StreamIdRelease ( vlc_bool_t *id, int i_id_min, int i_id );
86 typedef struct ps_stream_s
94 /* Which id are unused */
95 vlc_bool_t stream_id_mpga[16]; /* 0xc0 -> 0xcf */
96 vlc_bool_t stream_id_mpgv[16]; /* 0xe0 -> 0xef */
97 vlc_bool_t stream_id_a52[8]; /* 0x80 -> 0x87 <- FIXME I'm not sure */
98 vlc_bool_t stream_id_spu[32]; /* 0x20 -> 0x3f */
99 vlc_bool_t stream_id_dts[8]; /* 0x88 -> 0x8f */
100 vlc_bool_t stream_id_lpcm[16]; /* 0xa0 -> 0xaf */
111 static const char *ppsz_sout_options[] = {
115 /*****************************************************************************
117 *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
120 sout_mux_t *p_mux = (sout_mux_t*)p_this;
121 sout_mux_sys_t *p_sys;
124 msg_Info( p_mux, "Open" );
125 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
127 p_mux->pf_control = Control;
128 p_mux->pf_addstream = AddStream;
129 p_mux->pf_delstream = DelStream;
131 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
133 /* Init free stream id */
134 StreamIdInit( p_sys->stream_id_a52, 8 );
135 StreamIdInit( p_sys->stream_id_dts, 8 );
136 StreamIdInit( p_sys->stream_id_mpga, 16 );
137 StreamIdInit( p_sys->stream_id_mpgv, 16 );
138 StreamIdInit( p_sys->stream_id_lpcm, 16 );
139 StreamIdInit( p_sys->stream_id_spu, 32 );
141 p_sys->i_audio_bound = 0;
142 p_sys->i_video_bound = 0;
143 p_sys->i_system_header = 0;
144 p_sys->i_pes_count = 0;
146 p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
148 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
149 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
154 /*****************************************************************************
156 *****************************************************************************/
157 static void Close( vlc_object_t * p_this )
159 sout_mux_t *p_mux = (sout_mux_t*)p_this;
160 sout_mux_sys_t *p_sys = p_mux->p_sys;
164 msg_Info( p_mux, "Close" );
166 p_end = block_New( p_mux, 4 );
167 p_end->p_buffer[0] = 0x00;
168 p_end->p_buffer[1] = 0x00;
169 p_end->p_buffer[2] = 0x01;
170 p_end->p_buffer[3] = 0xb9;
172 sout_AccessOutWrite( p_mux->p_access, p_end );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
187 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
188 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
192 case MUX_GET_ADD_STREAM_WAIT:
193 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
194 *pb_bool = VLC_FALSE;
198 ppsz = (char**)va_arg( args, char ** );
199 *ppsz = strdup( "video/mpeg" );
207 /*****************************************************************************
209 *****************************************************************************/
210 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
212 sout_mux_sys_t *p_sys = p_mux->p_sys;
213 ps_stream_t *p_stream;
215 msg_Dbg( p_mux, "adding input codec=%4.4s",
216 (char*)&p_input->p_fmt->i_codec );
218 p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
220 /* Init this new stream */
221 switch( p_input->p_fmt->i_codec )
223 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
224 p_stream->i_stream_id =
225 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
227 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
228 p_stream->i_stream_id =
229 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
231 case VLC_FOURCC( 'd', 't', 's', ' ' ):
232 p_stream->i_stream_id =
233 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
235 case VLC_FOURCC( 'a', '5', '2', ' ' ):
236 p_stream->i_stream_id =
237 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
239 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
240 p_stream->i_stream_id =
241 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
243 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
244 p_stream->i_stream_id =
245 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
251 if( p_stream->i_stream_id < 0 )
256 if( p_input->p_fmt->i_cat == AUDIO_ES )
258 p_sys->i_audio_bound++;
260 else if( p_input->p_fmt->i_cat == VIDEO_ES )
262 p_sys->i_video_bound++;
272 /*****************************************************************************
274 *****************************************************************************/
275 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
277 sout_mux_sys_t *p_sys = p_mux->p_sys;
278 ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
280 msg_Dbg( p_mux, "removing input" );
281 switch( p_input->p_fmt->i_codec )
283 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
284 StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
285 p_stream->i_stream_id );
287 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
288 StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
289 p_stream->i_stream_id&0xff );
291 case VLC_FOURCC( 'd', 't', 's', ' ' ):
292 StreamIdRelease( p_sys->stream_id_dts, 0x88,
293 p_stream->i_stream_id&0xff );
295 case VLC_FOURCC( 'a', '5', '2', ' ' ):
296 StreamIdRelease( p_sys->stream_id_a52, 0x80,
297 p_stream->i_stream_id&0xff );
299 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
300 StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
301 p_stream->i_stream_id );
303 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
304 StreamIdRelease( p_sys->stream_id_spu, 0x20,
305 p_stream->i_stream_id&0xff );
312 if( p_input->p_fmt->i_cat == AUDIO_ES )
314 p_sys->i_audio_bound--;
316 else if( p_input->p_fmt->i_cat == VIDEO_ES )
318 p_sys->i_video_bound--;
325 /*****************************************************************************
326 * Mux: Call each time there is new data for at least one stream
327 *****************************************************************************/
328 static int Mux( sout_mux_t *p_mux )
330 sout_mux_sys_t *p_sys = p_mux->p_sys;
334 sout_input_t *p_input;
335 ps_stream_t *p_stream;
337 block_t *p_ps, *p_data;
342 /* Choose which stream to mux */
343 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
348 p_input = p_mux->pp_inputs[i_stream];
349 p_stream = (ps_stream_t*)p_input->p_sys;
352 /* Write regulary PackHeader */
353 if( p_sys->i_pes_count % 30 == 0)
355 MuxWritePackHeader( p_mux, &p_ps, i_dts );
358 /* Write regulary SystemHeader */
359 if( p_sys->i_pes_count % 300 == 0 )
363 MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
365 /* For MPEG1 streaming, set HEADER flag */
366 for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
368 p_pk->i_flags |= BLOCK_FLAG_HEADER;
372 /* Get and mux a packet */
373 p_data = block_FifoGet( p_input->p_fifo );
374 E_( EStoPES )( p_mux->p_sout, &p_data, p_data, p_stream->i_stream_id,
375 p_mux->p_sys->b_mpeg2, 0, 0 );
377 block_ChainAppend( &p_ps, p_data );
379 sout_AccessOutWrite( p_mux->p_access, p_ps );
381 /* Increase counter */
382 p_sys->i_pes_count++;
388 /*****************************************************************************
390 *****************************************************************************/
391 static void StreamIdInit( vlc_bool_t *id, int i_range )
395 for( i = 0; i < i_range; i++ )
400 static int StreamIdGet( vlc_bool_t *id, int i_id_min, int i_id_max )
404 for( i = 0; i <= i_id_max - i_id_min; i++ )
415 static void StreamIdRelease( vlc_bool_t *id, int i_id_min, int i_id )
417 id[i_id - i_id_min] = VLC_TRUE;
420 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
423 sout_mux_sys_t *p_sys = p_mux->p_sys;
428 i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
430 p_hdr = block_New( p_mux, 18 );
431 p_hdr->i_pts = p_hdr->i_dts = i_dts;
432 bits_initwrite( &bits, 14, p_hdr->p_buffer );
433 bits_write( &bits, 32, 0x01ba );
437 bits_write( &bits, 2, 0x01 );
441 bits_write( &bits, 4, 0x02 );
444 bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
445 bits_write( &bits, 1, 1 );
446 bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
447 bits_write( &bits, 1, 1 );
448 bits_write( &bits, 15, i_scr&0x7fff );
449 bits_write( &bits, 1, 1 );
453 bits_write( &bits, 9, 0 ); // src extention
455 bits_write( &bits, 1, 1 );
457 bits_write( &bits, 22, 1000/8/50); // FIXME mux rate
458 bits_write( &bits, 1, 1 );
462 bits_write( &bits, 1, 1 );
463 bits_write( &bits, 5, 0x1f ); // FIXME reserved
464 bits_write( &bits, 3, 0 ); // stuffing bytes
467 p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
469 block_ChainAppend( p_buf, p_hdr );
472 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
475 sout_mux_sys_t *p_sys = p_mux->p_sys;
478 vlc_bool_t b_private;
479 int i_nb_private, i_nb_stream;
482 /* Count the number of private stream */
483 for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
485 ps_stream_t *p_stream;
487 p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
489 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
495 /* Private stream are declared only one time */
496 i_nb_stream = p_mux->i_nb_inputs -
497 ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
499 p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
500 p_hdr->i_dts = p_hdr->i_pts = i_dts;
502 bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
503 bits_write( &bits, 32, 0x01bb );
504 bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
505 bits_write( &bits, 1, 1 );
506 bits_write( &bits, 22, 0 ); // FIXME rate bound
507 bits_write( &bits, 1, 1 );
509 bits_write( &bits, 6, p_sys->i_audio_bound );
510 bits_write( &bits, 1, 0 ); // fixed flag
511 bits_write( &bits, 1, 0 ); // CSPS flag
512 bits_write( &bits, 1, 0 ); // system audio lock flag
513 bits_write( &bits, 1, 0 ); // system video lock flag
515 bits_write( &bits, 1, 1 ); // marker bit
517 bits_write( &bits, 5, p_sys->i_video_bound );
518 bits_write( &bits, 1, 1 ); // packet rate restriction flag (1 for mpeg1)
519 bits_write( &bits, 7, 0xff ); // reserved bits
521 /* stream_id table */
522 for( i = 0, b_private = VLC_FALSE; i < p_mux->i_nb_inputs; i++ )
524 sout_input_t *p_input;
525 ps_stream_t *p_stream;
527 p_input = p_mux->pp_inputs[i];
528 p_stream = (ps_stream_t *)p_input->p_sys;
530 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
536 b_private = VLC_TRUE;
537 /* Write stream id */
538 bits_write( &bits, 8, 0xbd );
542 /* Write stream id */
543 bits_write( &bits, 8, p_stream->i_stream_id&0xff );
545 bits_write( &bits, 2, 0x03 );
546 if( p_input->p_fmt->i_cat == AUDIO_ES )
548 bits_write( &bits, 1, 0 );
549 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 128 );
551 else if( p_input->p_fmt->i_cat == VIDEO_ES )
553 bits_write( &bits, 1, 1 );
554 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 1024);
559 bits_write( &bits, 1, 0 );
560 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 );
564 block_ChainAppend( p_buf, p_hdr );
568 * Find stream to be muxed.
570 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
576 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
578 sout_input_t *p_input = p_mux->pp_inputs[i];
581 if( p_input->p_fifo->i_depth <= 0 )
583 if( p_input->p_fmt->i_cat == AUDIO_ES ||
584 p_input->p_fmt->i_cat == VIDEO_ES )
586 /* We need that audio+video fifo contain at least 1 packet */
594 p_data = block_FifoShow( p_input->p_fifo );
595 if( i_stream == -1 || p_data->i_dts < i_dts )
598 i_dts = p_data->i_dts;
602 *pi_stream = i_stream;