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_name( "MPEG-PS" );
57 set_category( CAT_SOUT );
58 set_subcategory( SUBCAT_SOUT_MUX );
59 set_capability( "sout mux", 50 );
61 add_shortcut( "mpeg1" );
62 add_shortcut( "dvd" );
63 set_callbacks( Open, Close );
65 add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
66 DTS_LONGTEXT, VLC_TRUE );
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Control ( sout_mux_t *, int, va_list );
73 static int AddStream( sout_mux_t *, sout_input_t * );
74 static int DelStream( sout_mux_t *, sout_input_t * );
75 static int Mux ( sout_mux_t * );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int MuxGetStream ( sout_mux_t *, int *, mtime_t * );
82 static void MuxWritePackHeader ( sout_mux_t *, block_t **, mtime_t );
83 static void MuxWriteSystemHeader( sout_mux_t *, block_t **, mtime_t );
85 static void StreamIdInit ( vlc_bool_t *id, int i_range );
86 static int StreamIdGet ( vlc_bool_t *id, int i_id_min, int i_id_max );
87 static void StreamIdRelease ( vlc_bool_t *id, int i_id_min, int i_id );
89 typedef struct ps_stream_s
97 /* Which id are unused */
98 vlc_bool_t stream_id_mpga[16]; /* 0xc0 -> 0xcf */
99 vlc_bool_t stream_id_mpgv[16]; /* 0xe0 -> 0xef */
100 vlc_bool_t stream_id_a52[8]; /* 0x80 -> 0x87 <- FIXME I'm not sure */
101 vlc_bool_t stream_id_spu[32]; /* 0x20 -> 0x3f */
102 vlc_bool_t stream_id_dts[8]; /* 0x88 -> 0x8f */
103 vlc_bool_t stream_id_lpcm[16]; /* 0xa0 -> 0xaf */
114 static const char *ppsz_sout_options[] = {
118 /*****************************************************************************
120 *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
123 sout_mux_t *p_mux = (sout_mux_t*)p_this;
124 sout_mux_sys_t *p_sys;
127 msg_Info( p_mux, "Open" );
128 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
130 p_mux->pf_control = Control;
131 p_mux->pf_addstream = AddStream;
132 p_mux->pf_delstream = DelStream;
134 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
136 /* Init free stream id */
137 StreamIdInit( p_sys->stream_id_a52, 8 );
138 StreamIdInit( p_sys->stream_id_dts, 8 );
139 StreamIdInit( p_sys->stream_id_mpga, 16 );
140 StreamIdInit( p_sys->stream_id_mpgv, 16 );
141 StreamIdInit( p_sys->stream_id_lpcm, 16 );
142 StreamIdInit( p_sys->stream_id_spu, 32 );
144 p_sys->i_audio_bound = 0;
145 p_sys->i_video_bound = 0;
146 p_sys->i_system_header = 0;
147 p_sys->i_pes_count = 0;
149 p_sys->b_mpeg2 = !(p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mpeg1" ));
151 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
152 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
157 /*****************************************************************************
159 *****************************************************************************/
160 static void Close( vlc_object_t * p_this )
162 sout_mux_t *p_mux = (sout_mux_t*)p_this;
163 sout_mux_sys_t *p_sys = p_mux->p_sys;
167 msg_Info( p_mux, "Close" );
169 p_end = block_New( p_mux, 4 );
170 p_end->p_buffer[0] = 0x00;
171 p_end->p_buffer[1] = 0x00;
172 p_end->p_buffer[2] = 0x01;
173 p_end->p_buffer[3] = 0xb9;
175 sout_AccessOutWrite( p_mux->p_access, p_end );
180 /*****************************************************************************
182 *****************************************************************************/
183 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
190 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
191 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
195 case MUX_GET_ADD_STREAM_WAIT:
196 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
197 *pb_bool = VLC_FALSE;
201 ppsz = (char**)va_arg( args, char ** );
202 *ppsz = strdup( "video/mpeg" );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
215 sout_mux_sys_t *p_sys = p_mux->p_sys;
216 ps_stream_t *p_stream;
218 msg_Dbg( p_mux, "adding input codec=%4.4s",
219 (char*)&p_input->p_fmt->i_codec );
221 p_input->p_sys = p_stream = malloc( sizeof( ps_stream_t ) );
223 /* Init this new stream */
224 switch( p_input->p_fmt->i_codec )
226 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
227 p_stream->i_stream_id =
228 StreamIdGet( p_sys->stream_id_mpgv, 0xe0, 0xef );
230 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
231 p_stream->i_stream_id =
232 0xbd00 | StreamIdGet( p_sys->stream_id_lpcm, 0xa0, 0xaf );
234 case VLC_FOURCC( 'd', 't', 's', ' ' ):
235 p_stream->i_stream_id =
236 0xbd00 | StreamIdGet( p_sys->stream_id_dts, 0x88, 0x8f );
238 case VLC_FOURCC( 'a', '5', '2', ' ' ):
239 p_stream->i_stream_id =
240 0xbd00 | StreamIdGet( p_sys->stream_id_a52, 0x80, 0x87 );
242 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
243 p_stream->i_stream_id =
244 StreamIdGet( p_sys->stream_id_mpga, 0xc0, 0xcf );
246 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
247 p_stream->i_stream_id =
248 0xbd00 | StreamIdGet( p_sys->stream_id_spu, 0x20, 0x3f );
254 if( p_stream->i_stream_id < 0 )
259 if( p_input->p_fmt->i_cat == AUDIO_ES )
261 p_sys->i_audio_bound++;
263 else if( p_input->p_fmt->i_cat == VIDEO_ES )
265 p_sys->i_video_bound++;
275 /*****************************************************************************
277 *****************************************************************************/
278 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
280 sout_mux_sys_t *p_sys = p_mux->p_sys;
281 ps_stream_t *p_stream =(ps_stream_t*)p_input->p_sys;
283 msg_Dbg( p_mux, "removing input" );
284 switch( p_input->p_fmt->i_codec )
286 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
287 StreamIdRelease( p_sys->stream_id_mpgv, 0xe0,
288 p_stream->i_stream_id );
290 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
291 StreamIdRelease( p_sys->stream_id_lpcm, 0xa0,
292 p_stream->i_stream_id&0xff );
294 case VLC_FOURCC( 'd', 't', 's', ' ' ):
295 StreamIdRelease( p_sys->stream_id_dts, 0x88,
296 p_stream->i_stream_id&0xff );
298 case VLC_FOURCC( 'a', '5', '2', ' ' ):
299 StreamIdRelease( p_sys->stream_id_a52, 0x80,
300 p_stream->i_stream_id&0xff );
302 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
303 StreamIdRelease( p_sys->stream_id_mpga, 0xc0,
304 p_stream->i_stream_id );
306 case VLC_FOURCC( 's', 'p', 'u', ' ' ):
307 StreamIdRelease( p_sys->stream_id_spu, 0x20,
308 p_stream->i_stream_id&0xff );
315 if( p_input->p_fmt->i_cat == AUDIO_ES )
317 p_sys->i_audio_bound--;
319 else if( p_input->p_fmt->i_cat == VIDEO_ES )
321 p_sys->i_video_bound--;
328 /*****************************************************************************
329 * Mux: Call each time there is new data for at least one stream
330 *****************************************************************************/
331 static int Mux( sout_mux_t *p_mux )
333 sout_mux_sys_t *p_sys = p_mux->p_sys;
337 sout_input_t *p_input;
338 ps_stream_t *p_stream;
340 block_t *p_ps, *p_data;
345 /* Choose which stream to mux */
346 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
351 p_input = p_mux->pp_inputs[i_stream];
352 p_stream = (ps_stream_t*)p_input->p_sys;
355 /* Write regulary PackHeader */
356 if( p_sys->i_pes_count % 30 == 0)
358 MuxWritePackHeader( p_mux, &p_ps, i_dts );
361 /* Write regulary SystemHeader */
362 if( p_sys->i_pes_count % 300 == 0 )
366 MuxWriteSystemHeader( p_mux, &p_ps, i_dts );
368 /* For MPEG1 streaming, set HEADER flag */
369 for( p_pk = p_ps; p_pk != NULL; p_pk = p_pk->p_next )
371 p_pk->i_flags |= BLOCK_FLAG_HEADER;
375 /* Get and mux a packet */
376 p_data = block_FifoGet( p_input->p_fifo );
377 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
378 p_input->p_fmt, p_stream->i_stream_id,
379 p_mux->p_sys->b_mpeg2, 0, 0 );
381 block_ChainAppend( &p_ps, p_data );
383 sout_AccessOutWrite( p_mux->p_access, p_ps );
385 /* Increase counter */
386 p_sys->i_pes_count++;
392 /*****************************************************************************
394 *****************************************************************************/
395 static void StreamIdInit( vlc_bool_t *id, int i_range )
399 for( i = 0; i < i_range; i++ )
404 static int StreamIdGet( vlc_bool_t *id, int i_id_min, int i_id_max )
408 for( i = 0; i <= i_id_max - i_id_min; i++ )
419 static void StreamIdRelease( vlc_bool_t *id, int i_id_min, int i_id )
421 id[i_id - i_id_min] = VLC_TRUE;
424 static void MuxWritePackHeader( sout_mux_t *p_mux, block_t **p_buf,
427 sout_mux_sys_t *p_sys = p_mux->p_sys;
432 i_scr = (i_dts - p_sys->i_dts_delay) * 9 / 100;
434 p_hdr = block_New( p_mux, 18 );
435 p_hdr->i_pts = p_hdr->i_dts = i_dts;
436 bits_initwrite( &bits, 14, p_hdr->p_buffer );
437 bits_write( &bits, 32, 0x01ba );
441 bits_write( &bits, 2, 0x01 );
445 bits_write( &bits, 4, 0x02 );
448 bits_write( &bits, 3, ( i_scr >> 30 )&0x07 );
449 bits_write( &bits, 1, 1 );
450 bits_write( &bits, 15, ( i_scr >> 15 )&0x7fff );
451 bits_write( &bits, 1, 1 );
452 bits_write( &bits, 15, i_scr&0x7fff );
453 bits_write( &bits, 1, 1 );
457 bits_write( &bits, 9, 0 ); // src extention
459 bits_write( &bits, 1, 1 );
461 bits_write( &bits, 22, 1000/8/50); // FIXME mux rate
462 bits_write( &bits, 1, 1 );
466 bits_write( &bits, 1, 1 );
467 bits_write( &bits, 5, 0x1f ); // FIXME reserved
468 bits_write( &bits, 3, 0 ); // stuffing bytes
471 p_hdr->i_buffer = p_sys->b_mpeg2 ? 14: 12;
473 block_ChainAppend( p_buf, p_hdr );
476 static void MuxWriteSystemHeader( sout_mux_t *p_mux, block_t **p_buf,
479 sout_mux_sys_t *p_sys = p_mux->p_sys;
482 vlc_bool_t b_private;
483 int i_nb_private, i_nb_stream;
486 /* Count the number of private stream */
487 for( i = 0, i_nb_private = 0; i < p_mux->i_nb_inputs; i++ )
489 ps_stream_t *p_stream;
491 p_stream = (ps_stream_t*)p_mux->pp_inputs[i]->p_sys;
493 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
499 /* Private stream are declared only one time */
500 i_nb_stream = p_mux->i_nb_inputs -
501 ( i_nb_private > 0 ? i_nb_private - 1 : 0 );
503 p_hdr = block_New( p_mux, 12 + i_nb_stream * 3 );
504 p_hdr->i_dts = p_hdr->i_pts = i_dts;
506 bits_initwrite( &bits, 12 + i_nb_stream * 3, p_hdr->p_buffer );
507 bits_write( &bits, 32, 0x01bb );
508 bits_write( &bits, 16, 12 - 6 + i_nb_stream * 3 );
509 bits_write( &bits, 1, 1 );
510 bits_write( &bits, 22, 0 ); // FIXME rate bound
511 bits_write( &bits, 1, 1 );
513 bits_write( &bits, 6, p_sys->i_audio_bound );
514 bits_write( &bits, 1, 0 ); // fixed flag
515 bits_write( &bits, 1, 0 ); // CSPS flag
516 bits_write( &bits, 1, 0 ); // system audio lock flag
517 bits_write( &bits, 1, 0 ); // system video lock flag
519 bits_write( &bits, 1, 1 ); // marker bit
521 bits_write( &bits, 5, p_sys->i_video_bound );
522 bits_write( &bits, 1, 1 ); // packet rate restriction flag (1 for mpeg1)
523 bits_write( &bits, 7, 0xff ); // reserved bits
525 /* stream_id table */
526 for( i = 0, b_private = VLC_FALSE; i < p_mux->i_nb_inputs; i++ )
528 sout_input_t *p_input;
529 ps_stream_t *p_stream;
531 p_input = p_mux->pp_inputs[i];
532 p_stream = (ps_stream_t *)p_input->p_sys;
534 if( ( p_stream->i_stream_id&0xff00 ) == 0xbd00 )
540 b_private = VLC_TRUE;
541 /* Write stream id */
542 bits_write( &bits, 8, 0xbd );
546 /* Write stream id */
547 bits_write( &bits, 8, p_stream->i_stream_id&0xff );
549 bits_write( &bits, 2, 0x03 );
550 if( p_input->p_fmt->i_cat == AUDIO_ES )
552 bits_write( &bits, 1, 0 );
553 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 128 );
555 else if( p_input->p_fmt->i_cat == VIDEO_ES )
557 bits_write( &bits, 1, 1 );
558 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 / 1024);
563 bits_write( &bits, 1, 0 );
564 bits_write( &bits, 13, /* stream->max_buffer_size */ 0 );
568 block_ChainAppend( p_buf, p_hdr );
572 * Find stream to be muxed.
574 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
580 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
582 sout_input_t *p_input = p_mux->pp_inputs[i];
585 if( p_input->p_fifo->i_depth <= 0 )
587 if( p_input->p_fmt->i_cat == AUDIO_ES ||
588 p_input->p_fmt->i_cat == VIDEO_ES )
590 /* We need that audio+video fifo contain at least 1 packet */
598 p_data = block_FifoShow( p_input->p_fifo );
599 if( i_stream == -1 || p_data->i_dts < i_dts )
602 i_dts = p_data->i_dts;
606 *pi_stream = i_stream;