1 /*****************************************************************************
2 * ts.c: MPEG-II TS Muxer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
40 #ifdef HAVE_DVBPSI_DR_H
41 # include <dvbpsi/dvbpsi.h>
42 # include <dvbpsi/descriptor.h>
43 # include <dvbpsi/pat.h>
44 # include <dvbpsi/pmt.h>
45 # include <dvbpsi/dr.h>
46 # include <dvbpsi/psi.h>
49 # include "descriptor.h"
50 # include "tables/pat.h"
51 # include "tables/pmt.h"
52 # include "descriptors/dr.h"
58 * - check PCR frequency requirement
60 * - check PCR/PCR "soft"
61 * - check if "registration" descriptor : "AC-3" should be a program
62 * descriptor or an es one. (xine want an es one)
64 * - remove creation of PAT/PMT without dvbpsi
67 * - subtitle support is far from perfect. I expect some subtitles drop
68 * if they arrive a bit late
69 * (We cannot rely on the fact that the fifo should be full)
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close ( vlc_object_t * );
77 #define VPID_TEXT N_("Video PID")
78 #define VPID_LONGTEXT N_("Assigns a fixed PID to the video stream. The PCR " \
79 "PID will automatically be the video.")
80 #define APID_TEXT N_("Audio PID")
81 #define APID_LONGTEXT N_("Assigns a fixed PID to the audio stream.")
82 #define SPUPID_TEXT N_("SPU PID")
83 #define SPUPID_LONGTEXT N_("Assigns a fixed PID to the SPU.")
84 #define PMTPID_TEXT N_("PMT PID")
85 #define PMTPID_LONGTEXT N_("Assigns a fixed PID to the PMT")
86 #define TSID_TEXT N_("TS ID")
87 #define TSID_LONGTEXT N_("Assigns a fixed Transport Stream ID.")
88 #define PMTPROG_TEXT N_("PMT Program number")
89 #define PMTPROG_LONGTEXT N_("Assigns a program number to the PMT.")
91 #define PID_TEXT N_("Set PID to id of ES")
92 #define PID_LONGTEXT N_("set PID to id of es")
94 #define SHAPING_TEXT N_("Shaping delay (ms)")
95 #define SHAPING_LONGTEXT N_("If enabled, the TS muxer will cut the " \
96 "stream in slices of the given duration, and ensure a constant bitrate " \
97 "between the two boundaries. This avoids having huge bitrate peaks for " \
98 "reference frames, in particular.")
99 #define KEYF_TEXT N_("Use keyframes")
100 #define KEYF_LONGTEXT N_("If enabled, and shaping is specified, " \
101 "the TS muxer will place the boundaries at the end of I pictures. In " \
102 "that case, the shaping duration given by the user is a worse case " \
103 "used when no reference frame is available. This enhances the efficiency " \
104 "of the shaping algorithm, since I frames are usually the biggest " \
105 "frames in the stream.")
107 #define PCR_TEXT N_("PCR delay (ms)")
108 #define PCR_LONGTEXT N_("This option allows you to set at which interval " \
109 "PCRs (Program Clock Reference) will be sent. " \
110 "This value should be below 100ms. (default is 30)")
112 #define BMIN_TEXT N_( "Minimum B (deprecated)")
113 #define BMIN_LONGTEXT N_( "This setting is deprecated and not used anymore" )
115 #define BMAX_TEXT N_( "Maximum B (deprecated)")
116 #define BMAX_LONGTEXT N_( "This setting is deprecated and not used anymore")
118 #define DTS_TEXT N_("DTS delay (ms)")
119 #define DTS_LONGTEXT N_("This option will delay the DTS (decoding time " \
120 "stamps) and PTS (presentation timestamps) of the data in the " \
121 "stream, compared to the PCRs. This allows for some buffering inside " \
122 "the client decoder.")
124 #define ACRYPT_TEXT N_("Crypt audio")
125 #define ACRYPT_LONGTEXT N_("Crypt audio using CSA")
127 #define CK_TEXT N_("CSA Key")
128 #define CK_LONGTEXT N_("Defines the CSA encryption key. This must be a " \
129 "16 char string (8 hexadecimal bytes).")
131 #define SOUT_CFG_PREFIX "sout-ts-"
134 set_description( _("TS muxer (libdvbpsi)") );
135 set_shortname( "MPEG-TS");
136 set_category( CAT_SOUT );
137 set_subcategory( SUBCAT_SOUT_MUX );
138 set_capability( "sout mux", 120 );
139 add_shortcut( "ts" );
141 add_integer( SOUT_CFG_PREFIX "pid-video", 0, NULL,VPID_TEXT, VPID_LONGTEXT,
143 add_integer( SOUT_CFG_PREFIX "pid-audio", 0, NULL, APID_TEXT,
144 APID_LONGTEXT, VLC_TRUE );
145 add_integer( SOUT_CFG_PREFIX "pid-spu", 0, NULL, SPUPID_TEXT,
146 SPUPID_LONGTEXT, VLC_TRUE );
147 add_integer( SOUT_CFG_PREFIX "pid-pmt", 0, NULL, PMTPID_TEXT,
148 PMTPID_LONGTEXT, VLC_TRUE );
149 add_integer( SOUT_CFG_PREFIX "tsid", 0, NULL, TSID_TEXT,
150 TSID_LONGTEXT, VLC_TRUE );
151 add_integer( SOUT_CFG_PREFIX "program-pmt", 1, NULL, PMTPROG_TEXT,
152 PMTPROG_LONGTEXT, VLC_TRUE );
153 add_bool( SOUT_CFG_PREFIX "es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT,
156 add_integer( SOUT_CFG_PREFIX "shaping", 200, NULL,SHAPING_TEXT,
157 SHAPING_LONGTEXT, VLC_TRUE );
158 add_bool( SOUT_CFG_PREFIX "use-key-frames", VLC_FALSE, NULL, KEYF_TEXT,
159 KEYF_LONGTEXT, VLC_TRUE );
161 add_integer( SOUT_CFG_PREFIX "pcr", 30, NULL, PCR_TEXT, PCR_LONGTEXT,
163 add_integer( SOUT_CFG_PREFIX "bmin", 0, NULL, BMIN_TEXT, BMIN_LONGTEXT,
165 add_integer( SOUT_CFG_PREFIX "bmax", 0, NULL, BMAX_TEXT, BMAX_LONGTEXT,
167 add_integer( SOUT_CFG_PREFIX "dts-delay", 200, NULL, DTS_TEXT,
168 DTS_LONGTEXT, VLC_TRUE );
170 add_bool( SOUT_CFG_PREFIX "crypt-audio", VLC_TRUE, NULL, ACRYPT_TEXT,
171 ACRYPT_LONGTEXT, VLC_TRUE );
173 add_string( SOUT_CFG_PREFIX "csa-ck", NULL, NULL, CK_TEXT, CK_LONGTEXT,
176 set_callbacks( Open, Close );
179 /*****************************************************************************
180 * Local data structures
181 *****************************************************************************/
182 static const char *ppsz_sout_options[] = {
183 "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid", "program-pmt",
184 "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
185 "dts-delay", "csa-ck", "crypt-audio",
194 } sout_buffer_chain_t;
196 static inline void BufferChainInit ( sout_buffer_chain_t *c )
200 c->pp_last = &c->p_first;
202 static inline void BufferChainAppend( sout_buffer_chain_t *c, block_t *b )
212 c->pp_last = &b->p_next;
214 static inline block_t *BufferChainGet( sout_buffer_chain_t *c )
216 block_t *b = c->p_first;
221 c->p_first = b->p_next;
223 if( c->p_first == NULL )
225 c->pp_last = &c->p_first;
232 static inline block_t *BufferChainPeek( sout_buffer_chain_t *c )
234 block_t *b = c->p_first;
238 static inline void BufferChainClean( sout_instance_t *p_sout,
239 sout_buffer_chain_t *c )
243 while( ( b = BufferChainGet( c ) ) )
247 BufferChainInit( c );
250 typedef struct ts_stream_t
253 vlc_fourcc_t i_codec;
257 int i_continuity_counter;
259 /* to be used for carriege of DIV3 */
260 vlc_fourcc_t i_bih_codec;
261 int i_bih_width, i_bih_height;
263 /* Specific to mpeg4 in mpeg2ts */
266 int i_decoder_specific_info;
267 uint8_t *p_decoder_specific_info;
269 /* language is iso639-2T */
272 sout_buffer_chain_t chain_pes;
274 mtime_t i_pes_length;
276 vlc_bool_t b_key_frame;
280 struct sout_mux_sys_t
283 sout_input_t *p_pcr_input;
288 vlc_bool_t b_es_id_pid;
292 int i_pid_free; // first usable pid
295 int i_pat_version_number;
298 int i_pmt_version_number;
299 ts_stream_t pmt; // Up to now only one program
300 int i_pmt_program_number;
304 int i_null_continuity_counter; /* Needed ? */
306 /* for TS building */
307 int64_t i_bitrate_min;
308 int64_t i_bitrate_max;
310 int64_t i_shaping_delay;
315 vlc_bool_t b_use_key_frames;
317 mtime_t i_pcr; /* last PCR emited */
320 vlc_bool_t b_crypt_audio;
324 /* Reserve a pid and return it */
325 static int AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
328 if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
330 i_pid = p_sys->i_pid_video;
331 p_sys->i_pid_video = 0;
333 else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
335 i_pid = p_sys->i_pid_audio;
336 p_sys->i_pid_audio = 0;
338 else if ( i_cat == SPU_ES && p_sys->i_pid_spu )
340 i_pid = p_sys->i_pid_spu;
341 p_sys->i_pid_spu = 0;
345 i_pid = ++p_sys->i_pid_free;
350 /*****************************************************************************
352 *****************************************************************************/
353 static int Control ( sout_mux_t *, int, va_list );
354 static int AddStream( sout_mux_t *, sout_input_t * );
355 static int DelStream( sout_mux_t *, sout_input_t * );
356 static int Mux ( sout_mux_t * );
358 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo );
359 static void TSSchedule ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
360 mtime_t i_pcr_length, mtime_t i_pcr_dts );
361 static void TSDate ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
362 mtime_t i_pcr_length, mtime_t i_pcr_dts );
363 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
364 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
366 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, vlc_bool_t b_pcr );
367 static void TSSetPCR( block_t *p_ts, mtime_t i_dts );
369 static void PEStoTS ( sout_instance_t *, sout_buffer_chain_t *, block_t *, ts_stream_t * );
371 /*****************************************************************************
373 *****************************************************************************/
374 static int Open( vlc_object_t *p_this )
376 sout_mux_t *p_mux =(sout_mux_t*)p_this;
377 sout_mux_sys_t *p_sys;
380 msg_Dbg( p_mux, "Open" );
381 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
383 p_sys = malloc( sizeof( sout_mux_sys_t ) );
385 p_mux->pf_control = Control;
386 p_mux->pf_addstream = AddStream;
387 p_mux->pf_delstream = DelStream;
389 p_mux->p_sys = p_sys;
391 srand( (uint32_t)mdate() );
393 p_sys->i_audio_bound = 0;
394 p_sys->i_video_bound = 0;
396 p_sys->i_pat_version_number = rand() % 32;
397 p_sys->pat.i_pid = 0;
398 p_sys->pat.i_continuity_counter = 0;
400 var_Get( p_mux, SOUT_CFG_PREFIX "tsid", &val );
402 p_sys->i_tsid = val.i_int;
404 p_sys->i_tsid = rand() % 65536;
405 p_sys->i_pmt_version_number = rand() % 32;
406 p_sys->pmt.i_continuity_counter = 0;
408 var_Get( p_mux, SOUT_CFG_PREFIX "program-pmt", &val );
411 p_sys->i_pmt_program_number = val.i_int;
415 p_sys->i_pmt_program_number = 1;
418 var_Get( p_mux, SOUT_CFG_PREFIX "pid-pmt", &val );
421 p_sys->pmt.i_pid = val.i_int;
425 p_sys->pmt.i_pid = 0x42;
428 p_sys->i_pid_free = p_sys->pmt.i_pid + 1;
430 var_Get( p_mux, SOUT_CFG_PREFIX "es-id-pid", &val );
431 p_sys->b_es_id_pid = val.b_bool;
433 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
434 p_sys->i_pid_video = val.i_int;
435 if ( p_sys->i_pid_video > p_sys->i_pid_free )
437 p_sys->i_pid_free = p_sys->i_pid_video + 1;
440 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
441 p_sys->i_pid_audio = val.i_int;
442 if ( p_sys->i_pid_audio > p_sys->i_pid_free )
444 p_sys->i_pid_free = p_sys->i_pid_audio + 1;
447 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
448 p_sys->i_pid_spu = val.i_int;
449 if ( p_sys->i_pid_spu > p_sys->i_pid_free )
451 p_sys->i_pid_free = p_sys->i_pid_spu + 1;
454 p_sys->i_pcr_pid = 0x1fff;
455 p_sys->p_pcr_input = NULL;
457 p_sys->i_mpeg4_streams = 0;
459 p_sys->i_null_continuity_counter = 0;
461 /* Allow to create constrained stream */
462 var_Get( p_mux, SOUT_CFG_PREFIX "bmin", &val );
463 p_sys->i_bitrate_min = val.i_int;
465 var_Get( p_mux, SOUT_CFG_PREFIX "bmax", &val );
466 p_sys->i_bitrate_max = val.i_int;
468 if( p_sys->i_bitrate_min > 0 && p_sys->i_bitrate_max > 0 &&
469 p_sys->i_bitrate_min > p_sys->i_bitrate_max )
471 msg_Err( p_mux, "incompatible minimum and maximum bitrate, "
472 "disabling bitrate control" );
473 p_sys->i_bitrate_min = 0;
474 p_sys->i_bitrate_max = 0;
476 if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
478 msg_Err( p_mux, "bmin and bmax no more supported "
479 "(if you need them report it)" );
482 var_Get( p_mux, SOUT_CFG_PREFIX "shaping", &val );
483 p_sys->i_shaping_delay = (int64_t)val.i_int * 1000;
484 if( p_sys->i_shaping_delay <= 0 )
487 "invalid shaping ("I64Fd"ms) resetting to 200ms",
488 p_sys->i_shaping_delay / 1000 );
489 p_sys->i_shaping_delay = 200000;
492 var_Get( p_mux, SOUT_CFG_PREFIX "pcr", &val );
493 p_sys->i_pcr_delay = (int64_t)val.i_int * 1000;
494 if( p_sys->i_pcr_delay <= 0 ||
495 p_sys->i_pcr_delay >= p_sys->i_shaping_delay )
498 "invalid pcr delay ("I64Fd"ms) resetting to 30ms",
499 p_sys->i_pcr_delay / 1000 );
500 p_sys->i_pcr_delay = 30000;
503 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
504 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
506 msg_Dbg( p_mux, "shaping="I64Fd" pcr="I64Fd" dts_delay="I64Fd,
507 p_sys->i_shaping_delay, p_sys->i_pcr_delay, p_sys->i_dts_delay );
509 var_Get( p_mux, SOUT_CFG_PREFIX "use-key-frames", &val );
510 p_sys->b_use_key_frames = val.b_bool;
512 /* for TS generation */
516 var_Get( p_mux, SOUT_CFG_PREFIX "csa-ck", &val );
517 if( val.psz_string && *val.psz_string )
519 char *psz = val.psz_string;
522 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
526 if( strlen( psz ) != 16 )
528 msg_Dbg( p_mux, "invalid csa ck (it must be 16 chars long)" );
532 uint64_t i_ck = strtoull( psz, NULL, 16 );
536 for( i = 0; i < 8; i++ )
538 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
541 msg_Dbg( p_mux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
542 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
544 p_sys->csa = csa_New();
545 csa_SetCW( p_sys->csa, ck, ck );
548 if( val.psz_string ) free( val.psz_string );
550 var_Get( p_mux, SOUT_CFG_PREFIX "crypt-audio", &val );
551 p_sys->b_crypt_audio = val.b_bool;
556 /*****************************************************************************
558 *****************************************************************************/
559 static void Close( vlc_object_t * p_this )
561 sout_mux_t *p_mux = (sout_mux_t*)p_this;
562 sout_mux_sys_t *p_sys = p_mux->p_sys;
564 msg_Dbg( p_mux, "Close" );
567 csa_Delete( p_sys->csa );
573 /*****************************************************************************
575 *****************************************************************************/
576 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
583 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
584 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
588 case MUX_GET_ADD_STREAM_WAIT:
589 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
590 *pb_bool = VLC_FALSE;
594 ppsz = (char**)va_arg( args, char ** );
595 *ppsz = strdup( "video/mpeg" ); /* FIXME not sure */
603 /*****************************************************************************
604 * AddStream: called for each stream addition
605 *****************************************************************************/
606 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
608 sout_mux_sys_t *p_sys = p_mux->p_sys;
609 ts_stream_t *p_stream;
611 p_input->p_sys = p_stream = malloc( sizeof( ts_stream_t ) );
613 /* Init this new stream */
614 if ( p_sys->b_es_id_pid )
615 p_stream->i_pid = p_input->p_fmt->i_id & 0x1fff;
617 p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
618 p_stream->i_codec = p_input->p_fmt->i_codec;
619 p_stream->i_continuity_counter = 0;
620 p_stream->i_decoder_specific_info = 0;
621 p_stream->p_decoder_specific_info = NULL;
623 msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d",
624 (char*)&p_input->p_fmt->i_codec, p_stream->i_pid );
626 /* All others fields depand on codec */
627 switch( p_input->p_fmt->i_cat )
630 switch( p_input->p_fmt->i_codec )
632 case VLC_FOURCC( 'm', 'p','g', 'v' ):
633 /* TODO: do we need to check MPEG-I/II ? */
634 p_stream->i_stream_type = 0x02;
635 p_stream->i_stream_id = 0xe0;
637 case VLC_FOURCC( 'm', 'p','4', 'v' ):
638 p_stream->i_stream_type = 0x10;
639 p_stream->i_stream_id = 0xfa;
640 p_sys->i_mpeg4_streams++;
641 p_stream->i_es_id = p_stream->i_pid;
643 case VLC_FOURCC( 'h', '2','6', '4' ):
644 p_stream->i_stream_type = 0x1b;
645 p_stream->i_stream_id = 0xe0;
647 /* XXX dirty dirty but somebody want that:
648 * using crapy MS-codec XXX */
649 /* I didn't want to do that :P */
650 case VLC_FOURCC( 'H', '2', '6', '3' ):
651 case VLC_FOURCC( 'I', '2', '6', '3' ):
652 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
653 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
654 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
655 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
656 case VLC_FOURCC( 'D', 'I', 'V', '2' ):
657 case VLC_FOURCC( 'D', 'I', 'V', '1' ):
658 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
659 p_stream->i_stream_type = 0xa0; // private
660 p_stream->i_stream_id = 0xa0; // beurk
661 p_stream->i_bih_codec = p_input->p_fmt->i_codec;
662 p_stream->i_bih_width = p_input->p_fmt->video.i_width;
663 p_stream->i_bih_height = p_input->p_fmt->video.i_height;
669 p_sys->i_video_bound++;
673 switch( p_input->p_fmt->i_codec )
675 case VLC_FOURCC( 'm', 'p','g', 'a' ):
676 p_stream->i_stream_type =
677 p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
678 p_stream->i_stream_id = 0xc0;
680 case VLC_FOURCC( 'a', '5','2', ' ' ):
681 p_stream->i_stream_type = 0x81;
682 p_stream->i_stream_id = 0xbd;
684 case VLC_FOURCC( 'l', 'p','c', 'm' ):
685 p_stream->i_stream_type = 0x83;
686 p_stream->i_stream_id = 0xbd;
688 case VLC_FOURCC( 'd', 't','s', ' ' ):
689 p_stream->i_stream_type = 0x06;
690 p_stream->i_stream_id = 0xbd;
693 case VLC_FOURCC( 'm', 'p','4', 'a' ):
694 p_stream->i_stream_type = 0x11;
695 p_stream->i_stream_id = 0xfa;
696 p_sys->i_mpeg4_streams++;
697 p_stream->i_es_id = p_stream->i_pid;
703 p_sys->i_audio_bound++;
707 switch( p_input->p_fmt->i_codec )
709 case VLC_FOURCC( 's', 'p','u', ' ' ):
710 p_stream->i_stream_type = 0x82;
711 p_stream->i_stream_id = 0xbd;
713 case VLC_FOURCC( 's', 'u','b', 't' ):
714 p_stream->i_stream_type = 0x12;
715 p_stream->i_stream_id = 0xfa;
716 p_sys->i_mpeg4_streams++;
717 p_stream->i_es_id = p_stream->i_pid;
719 case VLC_FOURCC('d','v','b','s'):
720 p_stream->i_stream_type = 0x06;
721 p_stream->i_es_id = p_input->p_fmt->subs.dvb.i_id;
722 p_stream->i_stream_id = 0xbd;
724 case VLC_FOURCC('t','e','l','x'):
725 p_stream->i_stream_type = 0x06;
726 p_stream->i_stream_id = 0xbd; /* FIXME */
741 p_stream->lang[2] = '\0';
742 if( p_input->p_fmt->psz_language )
744 char *psz = p_input->p_fmt->psz_language;
745 const iso639_lang_t *pl = NULL;
747 if( strlen( psz ) == 2 )
749 pl = GetLang_1( psz );
751 else if( strlen( psz ) == 3 )
753 pl = GetLang_2B( psz );
754 if( !strcmp( pl->psz_iso639_1, "??" ) )
756 pl = GetLang_2T( psz );
759 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
761 p_stream->lang[0] = pl->psz_iso639_2T[0];
762 p_stream->lang[1] = pl->psz_iso639_2T[1];
763 p_stream->lang[2] = pl->psz_iso639_2T[2];
765 msg_Dbg( p_mux, " - lang=%c%c%c",
766 p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
771 /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
772 p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra;
773 if( p_stream->i_decoder_specific_info > 0 )
775 p_stream->p_decoder_specific_info =
776 malloc( p_stream->i_decoder_specific_info );
777 memcpy( p_stream->p_decoder_specific_info,
778 p_input->p_fmt->p_extra,
779 p_input->p_fmt->i_extra );
782 /* Create decoder specific info for subt */
783 if( p_stream->i_codec == VLC_FOURCC( 's', 'u','b', 't' ) )
787 p_stream->i_decoder_specific_info = 55;
788 p_stream->p_decoder_specific_info = p =
789 malloc( p_stream->i_decoder_specific_info );
791 p[0] = 0x10; /* textFormat, 0x10 for 3GPP TS 26.245 */
792 p[1] = 0x00; /* flags: 1b: associated video info flag
796 p[2] = 52; /* remaining size */
800 p[0] = p[1] = p[2] = p[3] = 0; p+=4; /* display flags */
801 *p++ = 0; /* horizontal justification (-1: left, 0 center, 1 right) */
802 *p++ = 1; /* vertical justification (-1: top, 0 center, 1 bottom) */
804 p[0] = p[1] = p[2] = 0x00; p+=3;/* background rgb */
805 *p++ = 0xff; /* background a */
807 p[0] = p[1] = 0; p += 2; /* text box top */
808 p[0] = p[1] = 0; p += 2; /* text box left */
809 p[0] = p[1] = 0; p += 2; /* text box bottom */
810 p[0] = p[1] = 0; p += 2; /* text box right */
812 p[0] = p[1] = 0; p += 2; /* start char */
813 p[0] = p[1] = 0; p += 2; /* end char */
814 p[0] = p[1] = 0; p += 2; /* default font id */
816 *p++ = 0; /* font style flags */
817 *p++ = 12; /* font size */
819 p[0] = p[1] = p[2] = 0x00; p+=3;/* foreground rgb */
820 *p++ = 0x00; /* foreground a */
822 p[0] = p[1] = p[2] = 0; p[3] = 22; p += 4;
823 memcpy( p, "ftab", 4 ); p += 4;
824 *p++ = 0; *p++ = 1; /* entry count */
825 p[0] = p[1] = 0; p += 2; /* font id */
826 *p++ = 9; /* font name length */
827 memcpy( p, "Helvetica", 9 ); /* font name */
831 BufferChainInit( &p_stream->chain_pes );
832 p_stream->i_pes_dts = 0;
833 p_stream->i_pes_length = 0;
834 p_stream->i_pes_used = 0;
835 p_stream->b_key_frame = 0;
837 /* We only change PMT version (PAT isn't changed) */
838 p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
841 if( p_input->p_fmt->i_cat != SPU_ES &&
842 ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
844 if( p_sys->p_pcr_input )
846 /* There was already a PCR stream, so clean context */
849 p_sys->i_pcr_pid = p_stream->i_pid;
850 p_sys->p_pcr_input = p_input;
852 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
858 /*****************************************************************************
859 * DelStream: called before a stream deletion
860 *****************************************************************************/
861 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
863 sout_mux_sys_t *p_sys = p_mux->p_sys;
864 ts_stream_t *p_stream;
867 p_stream = (ts_stream_t*)p_input->p_sys;
868 msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
870 if( p_sys->i_pcr_pid == p_stream->i_pid )
874 /* Find a new pcr stream (Prefer Video Stream) */
875 p_sys->i_pcr_pid = 0x1fff;
876 p_sys->p_pcr_input = NULL;
877 for( i = 0; i < p_mux->i_nb_inputs; i++ )
879 if( p_mux->pp_inputs[i] == p_input )
884 if( p_mux->pp_inputs[i]->p_fmt->i_cat == VIDEO_ES )
887 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
888 p_sys->p_pcr_input= p_mux->pp_inputs[i];
891 else if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES &&
892 p_sys->i_pcr_pid == 0x1fff )
895 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
896 p_sys->p_pcr_input= p_mux->pp_inputs[i];
899 if( p_sys->p_pcr_input )
901 /* Empty TS buffer */
904 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
907 /* Empty all data in chain_pes */
908 BufferChainClean( p_mux->p_sout, &p_stream->chain_pes );
910 if( p_stream->p_decoder_specific_info )
912 free( p_stream->p_decoder_specific_info );
914 if( p_stream->i_stream_id == 0xfa ||
915 p_stream->i_stream_id == 0xfb ||
916 p_stream->i_stream_id == 0xfe )
918 p_sys->i_mpeg4_streams--;
921 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
924 int i_pid_video = val.i_int;
925 if ( i_pid_video == p_stream->i_pid )
927 p_sys->i_pid_video = i_pid_video;
928 msg_Dbg( p_mux, "freeing video PID %d", i_pid_video );
931 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
934 int i_pid_audio = val.i_int;
935 if ( i_pid_audio == p_stream->i_pid )
937 p_sys->i_pid_audio = i_pid_audio;
938 msg_Dbg( p_mux, "freeing audio PID %d", i_pid_audio );
941 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
944 int i_pid_spu = val.i_int;
945 if ( i_pid_spu == p_stream->i_pid )
947 p_sys->i_pid_spu = i_pid_spu;
948 msg_Dbg( p_mux, "freeing spu PID %d", i_pid_spu );
953 /* We only change PMT version (PAT isn't changed) */
954 p_sys->i_pmt_version_number++; p_sys->i_pmt_version_number %= 32;
959 /*****************************************************************************
960 * Mux: Call each time there is new data for at least one stream
961 *****************************************************************************
963 *****************************************************************************/
964 static int Mux( sout_mux_t *p_mux )
966 sout_mux_sys_t *p_sys = p_mux->p_sys;
967 ts_stream_t *p_pcr_stream;
969 if( p_sys->i_pcr_pid == 0x1fff )
971 msg_Dbg( p_mux, "waiting for PCR streams" );
975 p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
979 sout_buffer_chain_t chain_ts;
983 mtime_t i_pcr_length;
984 mtime_t i_shaping_delay;
987 if( p_pcr_stream->b_key_frame )
989 i_shaping_delay = p_pcr_stream->i_pes_length;
993 i_shaping_delay = p_sys->i_shaping_delay;
996 /* 1: get enough PES packet for all input */
999 vlc_bool_t b_ok = VLC_TRUE;
1002 /* Accumulate enough data in the pcr stream (>i_shaping_delay) */
1003 /* Accumulate enough data in all other stream ( >= length of pcr)*/
1004 for( i = -1; i < p_mux->i_nb_inputs; i++ )
1006 sout_input_t *p_input;
1007 ts_stream_t *p_stream;
1008 int64_t i_spu_delay = 0;
1011 p_input = p_sys->p_pcr_input;
1012 else if( p_mux->pp_inputs[i]->p_sys == p_pcr_stream )
1015 p_input = p_mux->pp_inputs[i];
1016 p_stream = (ts_stream_t*)p_input->p_sys;
1018 if( ( p_stream == p_pcr_stream &&
1019 p_stream->i_pes_length < i_shaping_delay ) ||
1020 p_stream->i_pes_dts + p_stream->i_pes_length <
1021 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1023 /* Need more data */
1024 if( p_input->p_fifo->i_depth <= 1 )
1026 if( p_input->p_fmt->i_cat == AUDIO_ES ||
1027 p_input->p_fmt->i_cat == VIDEO_ES )
1029 /* We need more data */
1032 else if( p_input->p_fifo->i_depth <= 0 )
1034 /* spu, only one packet is needed */
1039 /* Don't mux the SPU yet if it is too early */
1040 block_t *p_spu = block_FifoShow( p_input->p_fifo );
1043 p_spu->i_dts - p_pcr_stream->i_pes_dts;
1045 if( i_spu_delay > i_shaping_delay &&
1046 i_spu_delay < I64C(100000000) )
1049 if ( i_spu_delay >= I64C(100000000)
1050 || i_spu_delay < 10000 )
1052 BufferChainClean( p_mux->p_sout,
1053 &p_stream->chain_pes );
1054 p_stream->i_pes_dts = 0;
1055 p_stream->i_pes_used = 0;
1056 p_stream->i_pes_length = 0;
1063 if( p_stream == p_pcr_stream
1064 || p_input->p_fmt->i_codec !=
1065 VLC_FOURCC('m', 'p', 'g', 'a') )
1066 p_data = block_FifoGet( p_input->p_fifo );
1068 p_data = FixPES( p_mux, p_input->p_fifo );
1070 if( p_input->p_fifo->i_depth > 0 &&
1071 p_input->p_fmt->i_cat != SPU_ES )
1073 block_t *p_next = block_FifoShow( p_input->p_fifo );
1074 p_data->i_length = p_next->i_dts - p_data->i_dts;
1076 else if( p_input->p_fmt->i_codec !=
1077 VLC_FOURCC('s', 'u', 'b', 't' ) )
1078 p_data->i_length = 1000;
1080 if( ( p_pcr_stream->i_pes_dts > 0 &&
1081 p_data->i_dts - 10000000 > p_pcr_stream->i_pes_dts +
1082 p_pcr_stream->i_pes_length ) ||
1083 p_data->i_dts < p_stream->i_pes_dts ||
1084 ( p_stream->i_pes_dts > 0 &&
1085 p_input->p_fmt->i_cat != SPU_ES &&
1086 p_data->i_dts - 10000000 > p_stream->i_pes_dts +
1087 p_stream->i_pes_length ) )
1089 msg_Warn( p_mux, "packet with too strange dts "
1090 "(dts="I64Fd",old="I64Fd",pcr="I64Fd")",
1091 p_data->i_dts, p_stream->i_pes_dts,
1092 p_pcr_stream->i_pes_dts );
1093 block_Release( p_data );
1095 BufferChainClean( p_mux->p_sout,
1096 &p_stream->chain_pes );
1097 p_stream->i_pes_dts = 0;
1098 p_stream->i_pes_used = 0;
1099 p_stream->i_pes_length = 0;
1101 if( p_input->p_fmt->i_cat != SPU_ES )
1103 BufferChainClean( p_mux->p_sout,
1104 &p_pcr_stream->chain_pes );
1105 p_pcr_stream->i_pes_dts = 0;
1106 p_pcr_stream->i_pes_used = 0;
1107 p_pcr_stream->i_pes_length = 0;
1112 int i_header_size = 0;
1113 int b_data_alignment = 0;
1114 if( p_input->p_fmt->i_cat == SPU_ES )
1116 if( p_input->p_fmt->i_codec ==
1117 VLC_FOURCC('s','u','b','t') )
1119 /* Prepend header */
1120 p_data = block_Realloc( p_data, 2,
1122 p_data->p_buffer[0] =
1123 ( (p_data->i_buffer - 2) >> 8) & 0xff;
1124 p_data->p_buffer[1] =
1125 ( (p_data->i_buffer - 2) ) & 0xff;
1127 /* remove trailling \0 if any */
1128 if( p_data->i_buffer > 2 &&
1129 p_data->p_buffer[p_data->i_buffer -1] ==
1133 /* Append a empty sub (sub text only) */
1134 if( p_data->i_length > 0 &&
1135 !( p_data->i_buffer == 1 &&
1136 *p_data->p_buffer == ' ' ) )
1138 block_t *p_spu = block_New( p_mux, 3 );
1140 p_spu->i_dts = p_spu->i_pts =
1141 p_data->i_dts + p_data->i_length;
1142 p_spu->i_length = 1000;
1144 p_spu->p_buffer[0] = 0;
1145 p_spu->p_buffer[1] = 1;
1146 p_spu->p_buffer[2] = ' ';
1148 E_(EStoPES)( p_mux->p_sout, &p_spu, p_spu,
1150 p_stream->i_stream_id, 1,
1152 p_data->p_next = p_spu;
1155 else if( p_input->p_fmt->i_codec ==
1156 VLC_FOURCC('t','e','l','x') )
1159 i_header_size = 0x24;
1160 b_data_alignment = 1;
1163 else if( p_data->i_length < 0 ||
1164 p_data->i_length > 2000000 )
1166 /* FIXME choose a better value, but anyway we
1167 * should never have to do that */
1168 p_data->i_length = 1000;
1171 p_stream->i_pes_length += p_data->i_length;
1172 if( p_stream->i_pes_dts == 0 )
1174 p_stream->i_pes_dts = p_data->i_dts;
1177 /* Convert to pes */
1178 if( p_stream->i_stream_id == 0xa0 &&
1179 p_data->i_pts <= 0 )
1181 /* XXX yes I know, it's awfull, but it's needed,
1182 * so don't remove it ... */
1183 p_data->i_pts = p_data->i_dts;
1185 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
1186 p_input->p_fmt, p_stream->i_stream_id,
1187 1, b_data_alignment, i_header_size, 0 );
1189 BufferChainAppend( &p_stream->chain_pes, p_data );
1191 if( p_sys->b_use_key_frames && p_stream == p_pcr_stream
1192 && (p_data->i_flags & BLOCK_FLAG_TYPE_I)
1193 && !(p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME)
1194 && (p_stream->i_pes_length > 400000) )
1196 i_shaping_delay = p_stream->i_pes_length;
1197 p_stream->b_key_frame = 1;
1210 i_pcr_dts = p_pcr_stream->i_pes_dts;
1211 i_pcr_length = p_pcr_stream->i_pes_length;
1212 p_pcr_stream->b_key_frame = 0;
1214 /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
1215 /* 2: calculate non accurate total size of muxed ts */
1217 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1219 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1222 /* False for pcr stream but it will be enough to do PCR algo */
1223 for( p_pes = p_stream->chain_pes.p_first; p_pes != NULL;
1224 p_pes = p_pes->p_next )
1226 int i_size = p_pes->i_buffer;
1227 if( p_pes->i_dts + p_pes->i_length >
1228 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1230 mtime_t i_frag = p_pcr_stream->i_pes_dts +
1231 p_pcr_stream->i_pes_length - p_pes->i_dts;
1237 i_size = p_pes->i_buffer * i_frag / p_pes->i_length;
1239 i_packet_count += ( i_size + 183 ) / 184;
1242 /* add overhead for PCR (not really exact) */
1243 i_packet_count += (8 * i_pcr_length / p_sys->i_pcr_delay + 175) / 176;
1245 /* 3: mux PES into TS */
1246 BufferChainInit( &chain_ts );
1247 /* append PAT/PMT -> FIXME with big pcr delay it won't have enough pat/pmt */
1248 GetPAT( p_mux, &chain_ts );
1249 GetPMT( p_mux, &chain_ts );
1251 i_packet_count += chain_ts.i_depth;
1252 /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
1258 ts_stream_t *p_stream;
1259 sout_input_t *p_input;
1263 /* Select stream (lowest dts) */
1264 for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
1266 p_input = p_mux->pp_inputs[i];
1267 p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1269 if( p_stream->i_pes_dts == 0 )
1274 if( i_stream == -1 ||
1275 p_stream->i_pes_dts < i_dts )
1278 i_dts = p_stream->i_pes_dts;
1281 if( i_stream == -1 || i_dts > i_pcr_dts + i_pcr_length )
1285 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1287 /* do we need to issue pcr */
1289 if( p_stream == p_pcr_stream &&
1290 i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >=
1291 p_sys->i_pcr + p_sys->i_pcr_delay )
1294 p_sys->i_pcr = i_pcr_dts + i_packet_pos *
1295 i_pcr_length / i_packet_count;
1298 /* Build the TS packet */
1299 p_ts = TSNew( p_mux, p_stream, b_pcr );
1300 if( p_sys->csa != NULL &&
1301 (p_input->p_fmt->i_cat != AUDIO_ES || p_sys->b_crypt_audio) )
1303 p_ts->i_flags |= BLOCK_FLAG_SCRAMBLED;
1308 BufferChainAppend( &chain_ts, p_ts );
1311 /* 4: date and send */
1312 TSSchedule( p_mux, &chain_ts, i_pcr_length, i_pcr_dts );
1316 #define STD_PES_PAYLOAD 170
1317 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
1322 p_data = block_FifoShow( p_fifo );
1323 i_size = p_data->i_buffer;
1325 if( i_size == STD_PES_PAYLOAD )
1327 return block_FifoGet( p_fifo );
1329 else if( i_size > STD_PES_PAYLOAD )
1331 block_t *p_new = block_New( p_mux, STD_PES_PAYLOAD );
1332 p_mux->p_vlc->pf_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
1333 p_new->i_pts = p_data->i_pts;
1334 p_new->i_dts = p_data->i_dts;
1335 p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
1337 p_data->i_buffer -= STD_PES_PAYLOAD;
1338 p_data->p_buffer += STD_PES_PAYLOAD;
1339 p_data->i_pts += p_new->i_length;
1340 p_data->i_dts += p_new->i_length;
1341 p_data->i_length -= p_new->i_length;
1342 p_data->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1350 p_data = block_FifoGet( p_fifo );
1351 p_data = block_Realloc( p_data, 0, STD_PES_PAYLOAD );
1352 p_next = block_FifoShow( p_fifo );
1353 if ( p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME )
1355 p_data->i_flags &= ~BLOCK_FLAG_NO_KEYFRAME;
1356 p_data->i_pts = p_next->i_pts;
1357 p_data->i_dts = p_next->i_dts;
1359 i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
1361 p_mux->p_vlc->pf_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer,
1363 p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
1364 p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
1365 p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
1366 p_next->i_buffer -= i_copy;
1367 p_next->p_buffer += i_copy;
1368 p_next->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1370 if( !p_next->i_buffer )
1372 p_next = block_FifoGet( p_fifo );
1373 block_Release( p_next );
1379 static void TSSchedule( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1380 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1382 sout_mux_sys_t *p_sys = p_mux->p_sys;
1383 sout_buffer_chain_t new_chain;
1384 int i_packet_count = p_chain_ts->i_depth;
1387 BufferChainInit( &new_chain );
1389 if ( i_pcr_length <= 0 )
1391 i_pcr_length = i_packet_count;
1394 for( i = 0; i < i_packet_count; i++ )
1396 block_t *p_ts = BufferChainGet( p_chain_ts );
1397 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1399 BufferChainAppend( &new_chain, p_ts );
1402 p_ts->i_dts + p_sys->i_dts_delay * 2/3 < i_new_dts )
1404 mtime_t i_max_diff = i_new_dts - p_ts->i_dts;
1405 mtime_t i_cut_dts = p_ts->i_dts;
1407 p_ts = BufferChainPeek( p_chain_ts );
1409 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1410 while ( p_ts != NULL && i_new_dts - p_ts->i_dts >= i_max_diff )
1412 p_ts = BufferChainGet( p_chain_ts );
1413 i_max_diff = i_new_dts - p_ts->i_dts;
1414 i_cut_dts = p_ts->i_dts;
1415 BufferChainAppend( &new_chain, p_ts );
1417 p_ts = BufferChainPeek( p_chain_ts );
1419 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1421 msg_Dbg( p_mux, "adjusting rate at "I64Fd"/"I64Fd" (%d/%d)",
1422 i_cut_dts - i_pcr_dts, i_pcr_length, new_chain.i_depth,
1423 p_chain_ts->i_depth );
1424 if ( new_chain.i_depth )
1425 TSDate( p_mux, &new_chain,
1426 i_cut_dts - i_pcr_dts,
1428 if ( p_chain_ts->i_depth )
1430 p_chain_ts, i_pcr_dts + i_pcr_length - i_cut_dts,
1436 if ( new_chain.i_depth )
1437 TSDate( p_mux, &new_chain, i_pcr_length, i_pcr_dts );
1440 static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1441 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1443 sout_mux_sys_t *p_sys = p_mux->p_sys;
1444 int i_packet_count = p_chain_ts->i_depth;
1447 if ( i_pcr_length / 1000 > 0 )
1449 int i_bitrate = ((uint64_t)i_packet_count * 188 * 8000)
1450 / (uint64_t)(i_pcr_length / 1000);
1451 if ( p_sys->i_bitrate_max && p_sys->i_bitrate_max < i_bitrate )
1453 msg_Warn( p_mux, "max bitrate exceeded at "I64Fd
1454 " (%d bi/s for %d pkt in "I64Fd" us)",
1455 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1456 i_bitrate, i_packet_count, i_pcr_length);
1461 msg_Dbg( p_mux, "starting at "I64Fd
1462 " (%d bi/s for %d packets in "I64Fd" us)",
1463 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1464 i_bitrate, i_packet_count, i_pcr_length);
1470 /* This shouldn't happen, but happens in some rare heavy load
1471 * and packet losses conditions. */
1472 i_pcr_length = i_packet_count;
1475 /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
1476 for( i = 0; i < i_packet_count; i++ )
1478 block_t *p_ts = BufferChainGet( p_chain_ts );
1479 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1481 p_ts->i_dts = i_new_dts;
1482 p_ts->i_length = i_pcr_length / i_packet_count;
1484 if( p_ts->i_flags & BLOCK_FLAG_CLOCK )
1486 /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
1487 TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay );
1489 if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
1491 csa_Encrypt( p_sys->csa, p_ts->p_buffer, 0 );
1495 p_ts->i_dts += p_sys->i_shaping_delay * 3 / 2;
1497 sout_AccessOutWrite( p_mux->p_access, p_ts );
1501 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream,
1504 block_t *p_pes = p_stream->chain_pes.p_first;
1507 vlc_bool_t b_new_pes = VLC_FALSE;
1508 vlc_bool_t b_adaptation_field = VLC_FALSE;
1510 int i_payload_max = 184 - ( b_pcr ? 8 : 0 );
1513 if( p_stream->i_pes_used <= 0 )
1515 b_new_pes = VLC_TRUE;
1517 i_payload = __MIN( (int)p_pes->i_buffer - p_stream->i_pes_used,
1520 if( b_pcr || i_payload < i_payload_max )
1522 b_adaptation_field = VLC_TRUE;
1525 p_ts = block_New( p_mux, 188 );
1526 p_ts->i_dts = p_pes->i_dts;
1528 p_ts->p_buffer[0] = 0x47;
1529 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 ) |
1530 ( ( p_stream->i_pid >> 8 )&0x1f );
1531 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1532 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 ) |
1533 p_stream->i_continuity_counter;
1535 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1537 if( b_adaptation_field )
1543 int i_stuffing = i_payload_max - i_payload;
1545 p_ts->i_flags |= BLOCK_FLAG_CLOCK;
1547 p_ts->p_buffer[4] = 7 + i_stuffing;
1548 p_ts->p_buffer[5] = 0x10; /* flags */
1549 p_ts->p_buffer[6] = ( 0 )&0xff;
1550 p_ts->p_buffer[7] = ( 0 )&0xff;
1551 p_ts->p_buffer[8] = ( 0 )&0xff;
1552 p_ts->p_buffer[9] = ( 0 )&0xff;
1553 p_ts->p_buffer[10]= ( ( 0 )&0x80 ) | 0x7e;
1554 p_ts->p_buffer[11]= 0;
1556 for( i = 12; i < 12 + i_stuffing; i++ )
1558 p_ts->p_buffer[i] = 0xff;
1563 int i_stuffing = i_payload_max - i_payload;
1565 p_ts->p_buffer[4] = i_stuffing - 1;
1566 if( i_stuffing > 1 )
1568 p_ts->p_buffer[5] = 0x00;
1569 for( i = 6; i < 6 + i_stuffing - 2; i++ )
1571 p_ts->p_buffer[i] = 0xff;
1578 memcpy( &p_ts->p_buffer[188 - i_payload],
1579 &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
1581 p_stream->i_pes_used += i_payload;
1582 p_stream->i_pes_dts = p_pes->i_dts + p_pes->i_length *
1583 p_stream->i_pes_used / p_pes->i_buffer;
1584 p_stream->i_pes_length -= p_pes->i_length * i_payload / p_pes->i_buffer;
1586 if( p_stream->i_pes_used >= (int)p_pes->i_buffer )
1588 p_pes = BufferChainGet( &p_stream->chain_pes );
1589 block_Release( p_pes );
1591 p_pes = p_stream->chain_pes.p_first;
1594 p_stream->i_pes_dts = p_pes->i_dts;
1595 p_stream->i_pes_length = 0;
1598 p_stream->i_pes_length += p_pes->i_length;
1600 p_pes = p_pes->p_next;
1605 p_stream->i_pes_dts = 0;
1606 p_stream->i_pes_length = 0;
1608 p_stream->i_pes_used = 0;
1615 static void TSSetPCR( block_t *p_ts, mtime_t i_dts )
1617 mtime_t i_pcr = 9 * i_dts / 100;
1619 p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
1620 p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
1621 p_ts->p_buffer[8] = ( i_pcr >> 9 )&0xff;
1622 p_ts->p_buffer[9] = ( i_pcr >> 1 )&0xff;
1623 p_ts->p_buffer[10]|= ( i_pcr << 7 )&0x80;
1627 static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c,
1628 mtime_t i_length, int i_bitrate_min,
1631 sout_mux_sys_t *p_sys = p_mux->p_sys;
1632 sout_buffer_chain_t s = *c;
1635 int i_packets_min = 0;
1636 int i_packets_max = 0;
1643 i_packets = c->i_depth;
1644 i_packets_min = ( (int64_t)i_bitrate_min * i_length / 8 / 1000000 + 187 ) / 188;
1645 i_packets_max = ( (int64_t)i_bitrate_max * i_length / 8 / 1000000 + 187 ) / 188;
1647 if( i_packets < i_packets_min && i_packets_min > 0 )
1650 int i_div = ( i_packets_min - i_packets ) / i_packets;
1651 int i_mod = ( i_packets_min - i_packets ) % i_packets;
1654 /* We need to pad with null packets (pid=0x1fff)
1655 * We try to melt null packets with true packets */
1657 "packets=%d but min=%d -> adding %d packets of padding",
1658 i_packets, i_packets_min, i_packets_min - i_packets );
1660 BufferChainInit( c );
1661 while( ( p_pk = BufferChainGet( &s ) ) )
1665 BufferChainAppend( c, p_pk );
1667 i_null = i_div + ( i_rest + i_mod ) / i_packets;
1669 for( i = 0; i < i_null; i++ )
1673 p_null = sout_BufferNew( p_mux->p_sout, 188 );
1674 p_null->p_buffer[0] = 0x47;
1675 p_null->p_buffer[1] = 0x1f;
1676 p_null->p_buffer[2] = 0xff;
1677 p_null->p_buffer[3] = 0x10 | p_sys->i_null_continuity_counter;
1678 memset( &p_null->p_buffer[4], 0, 184 );
1679 p_sys->i_null_continuity_counter =
1680 ( p_sys->i_null_continuity_counter + 1 ) % 16;
1682 BufferChainAppend( c, p_null );
1685 i_rest = ( i_rest + i_mod ) % i_packets;
1688 else if( i_packets > i_packets_max && i_packets_max > 0 )
1693 /* Arg, we need to drop packets, I don't do something clever (like
1694 * dropping complete pid, b frames, ... ), I just get the right amount
1695 * of packets and discard the others */
1697 "packets=%d but max=%d -> removing %d packets -> stream broken",
1698 i_packets, i_packets_max, i_packets - i_packets_max );
1700 BufferChainInit( c );
1701 for( i = 0; i < i_packets_max; i++ )
1703 BufferChainAppend( c, BufferChainGet( &s ) );
1706 while( ( p_pk = BufferChainGet( &s ) ) )
1708 sout_BufferDelete( p_mux->p_sout, p_pk );
1714 static void PEStoTS( sout_instance_t *p_sout,
1715 sout_buffer_chain_t *c, block_t *p_pes,
1716 ts_stream_t *p_stream )
1722 /* get PES total size */
1723 i_size = p_pes->i_buffer;
1724 p_data = p_pes->p_buffer;
1726 b_new_pes = VLC_TRUE;
1730 int b_adaptation_field;
1734 p_ts = block_New( p_sout, 188 );
1737 * 1b transport_error_indicator
1738 * 1b payload_unit_start
1739 * 1b transport_priority
1741 * 2b transport_scrambling_control
1742 * 2b if adaptation_field 0x03 else 0x01
1743 * 4b continuity_counter
1746 i_copy = __MIN( i_size, 184 );
1747 b_adaptation_field = i_size < 184 ? VLC_TRUE : VLC_FALSE;
1749 p_ts->p_buffer[0] = 0x47;
1750 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
1751 ( ( p_stream->i_pid >> 8 )&0x1f );
1752 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1753 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
1754 p_stream->i_continuity_counter;
1756 b_new_pes = VLC_FALSE;
1757 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1759 if( b_adaptation_field )
1761 int i_stuffing = 184 - i_copy;
1764 p_ts->p_buffer[4] = i_stuffing - 1;
1765 if( i_stuffing > 1 )
1767 p_ts->p_buffer[5] = 0x00;
1768 for( i = 6; i < 6 + i_stuffing - 2; i++ )
1770 p_ts->p_buffer[i] = 0xff;
1775 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
1779 BufferChainAppend( c, p_ts );
1783 block_t *p_next = p_pes->p_next;
1785 p_pes->p_next = NULL;
1786 block_Release( p_pes );
1787 if( p_next == NULL )
1791 b_new_pes = VLC_TRUE;
1793 i_size = p_pes->i_buffer;
1794 p_data = p_pes->p_buffer;
1801 static block_t *WritePSISection( sout_instance_t *p_sout,
1802 dvbpsi_psi_section_t* p_section )
1804 block_t *p_psi, *p_first = NULL;
1811 i_size = (uint32_t)( p_section->p_payload_end - p_section->p_data )+
1812 ( p_section->b_syntax_indicator ? 4 : 0 );
1814 p_psi = block_New( p_sout, i_size + 1 );
1817 p_psi->i_length = 0;
1818 p_psi->i_buffer = i_size + 1;
1820 p_psi->p_buffer[0] = 0; // pointer
1821 memcpy( p_psi->p_buffer + 1,
1825 block_ChainAppend( &p_first, p_psi );
1827 p_section = p_section->p_next;
1833 static void GetPAT( sout_mux_t *p_mux,
1834 sout_buffer_chain_t *c )
1836 sout_mux_sys_t *p_sys = p_mux->p_sys;
1839 dvbpsi_psi_section_t *p_section;
1841 dvbpsi_InitPAT( &pat, p_sys->i_tsid, p_sys->i_pat_version_number,
1842 1 ); // b_current_next
1843 /* add all program (only one) */
1844 dvbpsi_PATAddProgram( &pat,
1845 p_sys->i_pmt_program_number, // i_number
1846 p_sys->pmt.i_pid ); // i_pid
1848 p_section = dvbpsi_GenPATSections( &pat,
1849 0 ); // max program per section
1851 p_pat = WritePSISection( p_mux->p_sout, p_section );
1853 PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
1855 dvbpsi_DeletePSISections( p_section );
1856 dvbpsi_EmptyPAT( &pat );
1859 static uint32_t GetDescriptorLength24b( int i_length )
1861 uint32_t i_l1, i_l2, i_l3;
1863 i_l1 = i_length&0x7f;
1864 i_l2 = ( i_length >> 7 )&0x7f;
1865 i_l3 = ( i_length >> 14 )&0x7f;
1867 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
1870 static void GetPMT( sout_mux_t *p_mux,
1871 sout_buffer_chain_t *c )
1873 sout_mux_sys_t *p_sys = p_mux->p_sys;
1877 dvbpsi_pmt_es_t *p_es;
1878 dvbpsi_psi_section_t *p_section;
1882 dvbpsi_InitPMT( &pmt,
1883 p_sys->i_pmt_program_number, // program number
1884 p_sys->i_pmt_version_number,
1885 1, // b_current_next
1888 if( p_sys->i_mpeg4_streams > 0 )
1892 bits_buffer_t bits_fix_IOD;
1894 /* Make valgrind happy : it works at byte level not bit one so
1895 * bit_write confuse it (but DON'T CHANGE the way that bit_write is
1896 * working (needed when fixing some bits) */
1897 memset( iod, 0, 4096 );
1899 bits_initwrite( &bits, 4096, iod );
1901 bits_write( &bits, 8, 0x11 );
1903 bits_write( &bits, 8, 0x01 );
1904 // InitialObjectDescriptor
1905 bits_align( &bits );
1906 bits_write( &bits, 8, 0x02 ); // tag
1907 bits_fix_IOD = bits; // save states to fix length later
1908 bits_write( &bits, 24,
1909 GetDescriptorLength24b( 0 ) ); // variable length (fixed later)
1910 bits_write( &bits, 10, 0x01 ); // ObjectDescriptorID
1911 bits_write( &bits, 1, 0x00 ); // URL Flag
1912 bits_write( &bits, 1, 0x00 ); // includeInlineProfileLevelFlag
1913 bits_write( &bits, 4, 0x0f ); // reserved
1914 bits_write( &bits, 8, 0xff ); // ODProfile (no ODcapability )
1915 bits_write( &bits, 8, 0xff ); // sceneProfile
1916 bits_write( &bits, 8, 0xfe ); // audioProfile (unspecified)
1917 bits_write( &bits, 8, 0xfe ); // visualProfile( // )
1918 bits_write( &bits, 8, 0xff ); // graphicProfile (no )
1919 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
1921 ts_stream_t *p_stream;
1922 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1924 if( p_stream->i_stream_id == 0xfa ||
1925 p_stream->i_stream_id == 0xfb ||
1926 p_stream->i_stream_id == 0xfe )
1928 bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
1930 bits_align( &bits );
1931 bits_write( &bits, 8, 0x03 ); // ES_DescrTag
1932 bits_fix_ESDescr = bits;
1933 bits_write( &bits, 24,
1934 GetDescriptorLength24b( 0 ) ); // variable size
1935 bits_write( &bits, 16, p_stream->i_es_id );
1936 bits_write( &bits, 1, 0x00 ); // streamDependency
1937 bits_write( &bits, 1, 0x00 ); // URL Flag
1938 bits_write( &bits, 1, 0x00 ); // OCRStreamFlag
1939 bits_write( &bits, 5, 0x1f ); // streamPriority
1941 // DecoderConfigDesciptor
1942 bits_align( &bits );
1943 bits_write( &bits, 8, 0x04 ); // DecoderConfigDescrTag
1944 bits_fix_Decoder = bits;
1945 bits_write( &bits, 24, GetDescriptorLength24b( 0 ) );
1946 if( p_stream->i_stream_type == 0x10 )
1948 bits_write( &bits, 8, 0x20 ); // Visual 14496-2
1949 bits_write( &bits, 6, 0x04 ); // VisualStream
1951 else if( p_stream->i_stream_type == 0x1b )
1953 bits_write( &bits, 8, 0x21 ); // Visual 14496-2
1954 bits_write( &bits, 6, 0x04 ); // VisualStream
1956 else if( p_stream->i_stream_type == 0x11 || p_stream->i_stream_type == 0x0f )
1958 bits_write( &bits, 8, 0x40 ); // Audio 14496-3
1959 bits_write( &bits, 6, 0x05 ); // AudioStream
1961 else if( p_stream->i_stream_type == 0x12 &&
1962 p_stream->i_codec == VLC_FOURCC('s','u','b','t') )
1964 bits_write( &bits, 8, 0x0B ); // Text Stream
1965 bits_write( &bits, 6, 0x04 ); // VisualStream
1969 bits_write( &bits, 8, 0x00 );
1970 bits_write( &bits, 6, 0x00 );
1972 msg_Err( p_mux->p_sout,"Unsupported stream_type => "
1975 bits_write( &bits, 1, 0x00 ); // UpStream
1976 bits_write( &bits, 1, 0x01 ); // reserved
1977 bits_write( &bits, 24, 1024 * 1024 ); // bufferSizeDB
1978 bits_write( &bits, 32, 0x7fffffff ); // maxBitrate
1979 bits_write( &bits, 32, 0 ); // avgBitrate
1981 if( p_stream->i_decoder_specific_info > 0 )
1984 // DecoderSpecificInfo
1985 bits_align( &bits );
1986 bits_write( &bits, 8, 0x05 ); // tag
1987 bits_write( &bits, 24, GetDescriptorLength24b(
1988 p_stream->i_decoder_specific_info ) );
1989 for( i = 0; i < p_stream->i_decoder_specific_info; i++ )
1991 bits_write( &bits, 8,
1992 ((uint8_t*)p_stream->p_decoder_specific_info)[i] );
1995 /* fix Decoder length */
1996 bits_write( &bits_fix_Decoder, 24,
1997 GetDescriptorLength24b( bits.i_data -
1998 bits_fix_Decoder.i_data - 3 ) );
2000 /* SLConfigDescriptor : predifined (0x01) */
2001 bits_align( &bits );
2002 bits_write( &bits, 8, 0x06 ); // tag
2003 bits_write( &bits, 24, GetDescriptorLength24b( 8 ) );
2004 bits_write( &bits, 8, 0x01 ); // predefined
2005 bits_write( &bits, 1, 0 ); // durationFlag
2006 bits_write( &bits, 32, 0 ); // OCRResolution
2007 bits_write( &bits, 8, 0 ); // OCRLength
2008 bits_write( &bits, 8, 0 ); // InstantBitrateLength
2009 bits_align( &bits );
2011 /* fix ESDescr length */
2012 bits_write( &bits_fix_ESDescr, 24,
2013 GetDescriptorLength24b( bits.i_data -
2014 bits_fix_ESDescr.i_data - 3 ) );
2017 bits_align( &bits );
2018 /* fix IOD length */
2019 bits_write( &bits_fix_IOD, 24,
2020 GetDescriptorLength24b( bits.i_data -
2021 bits_fix_IOD.i_data - 3 ) );
2022 dvbpsi_PMTAddDescriptor( &pmt, 0x1d, bits.i_data, bits.p_data );
2025 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2027 ts_stream_t *p_stream;
2029 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
2031 p_es = dvbpsi_PMTAddES( &pmt, p_stream->i_stream_type,
2033 if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
2038 es_id[0] = (p_stream->i_es_id >> 8)&0xff;
2039 es_id[1] = (p_stream->i_es_id)&0xff;
2040 dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
2042 else if( p_stream->i_stream_type == 0xa0 )
2045 int i_extra = __MIN( p_stream->i_decoder_specific_info, 502 );
2047 /* private DIV3 descripor */
2048 memcpy( &data[0], &p_stream->i_bih_codec, 4 );
2049 data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
2050 data[5] = ( p_stream->i_bih_width )&0xff;
2051 data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
2052 data[7] = ( p_stream->i_bih_height )&0xff;
2053 data[8] = ( i_extra >> 8 )&0xff;
2054 data[9] = ( i_extra )&0xff;
2057 memcpy( &data[10], p_stream->p_decoder_specific_info, i_extra );
2060 /* 0xa0 is private */
2061 dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra + 10, data );
2063 else if( p_stream->i_stream_type == 0x81 )
2065 uint8_t format[4] = { 0x41, 0x43, 0x2d, 0x33 };
2067 /* "registration" descriptor : "AC-3" */
2068 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
2070 else if( p_stream->i_codec == VLC_FOURCC('d','t','s',' ') )
2072 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2074 /* DTS format identifier, frame size 1024 - FIXME */
2075 uint8_t data[4] = { 0x44, 0x54, 0x53, 0x32 };
2076 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2078 else if( p_stream->i_codec == VLC_FOURCC('t','e','l','x') )
2080 dvbpsi_PMTESAddDescriptor( p_es, 0x56,
2081 p_stream->i_decoder_specific_info,
2082 p_stream->p_decoder_specific_info );
2084 #ifdef _DVBPSI_DR_59_H_
2085 else if( p_stream->i_codec == VLC_FOURCC('d','v','b','s') )
2088 dvbpsi_subtitling_dr_t descr;
2089 dvbpsi_subtitle_t sub;
2090 dvbpsi_descriptor_t *p_descr;
2092 memcpy( sub.i_iso6392_language_code, p_stream->lang, 3 );
2093 sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
2094 sub.i_composition_page_id = p_stream->i_es_id & 0xFF;
2095 sub.i_ancillary_page_id = p_stream->i_es_id >> 16;
2097 descr.i_subtitles_number = 1;
2098 descr.p_subtitle[0] = sub;
2100 p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
2101 /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
2102 dvbpsi_PMTESAddDescriptor( p_es, p_descr->i_tag,
2103 p_descr->i_length, p_descr->p_data );
2106 #endif /* _DVBPSI_DR_59_H_ */
2108 if( p_stream->lang[0] != 0 )
2112 /* I construct the content myself, way faster than looking at
2113 * over complicated/mind broken libdvbpsi way */
2114 data[0] = p_stream->lang[0];
2115 data[1] = p_stream->lang[1];
2116 data[2] = p_stream->lang[2];
2117 data[3] = 0x00; /* audio type: 0x00 undefined */
2119 dvbpsi_PMTESAddDescriptor( p_es, 0x0a, 4, data );
2123 p_section = dvbpsi_GenPMTSections( &pmt );
2125 p_pmt = WritePSISection( p_mux->p_sout, p_section );
2127 PEStoTS( p_mux->p_sout, c, p_pmt, &p_sys->pmt );
2129 dvbpsi_DeletePSISections( p_section );
2130 dvbpsi_EmptyPMT( &pmt );