1 /*****************************************************************************
2 * ts.c: MPEG-II TS Muxer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VideoLAN (Centrale Réseaux) and its contributors
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * Wallace Wadge <wwadge #_at_# gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
33 #include <vlc/input.h>
42 #ifdef HAVE_DVBPSI_DR_H
43 # include <dvbpsi/dvbpsi.h>
44 # include <dvbpsi/demux.h>
45 # include <dvbpsi/descriptor.h>
46 # include <dvbpsi/pat.h>
47 # include <dvbpsi/pmt.h>
48 # include <dvbpsi/sdt.h>
49 # include <dvbpsi/dr.h>
50 # include <dvbpsi/psi.h>
54 # include "descriptor.h"
55 # include "tables/pat.h"
56 # include "tables/pmt.h"
57 # include "tables/sdt.h"
58 # include "descriptors/dr.h"
64 * - check PCR frequency requirement
66 * - check PCR/PCR "soft"
67 * - check if "registration" descriptor : "AC-3" should be a program
68 * descriptor or an es one. (xine want an es one)
70 * - remove creation of PAT/PMT without dvbpsi
73 * - subtitle support is far from perfect. I expect some subtitles drop
74 * if they arrive a bit late
75 * (We cannot rely on the fact that the fifo should be full)
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
83 #define VPID_TEXT N_("Video PID")
84 #define VPID_LONGTEXT N_("Assigns a fixed PID to the video stream. The PCR " \
85 "PID will automatically be the video.")
86 #define APID_TEXT N_("Audio PID")
87 #define APID_LONGTEXT N_("Assigns a fixed PID to the audio stream.")
88 #define SPUPID_TEXT N_("SPU PID")
89 #define SPUPID_LONGTEXT N_("Assigns a fixed PID to the SPU.")
90 #define PMTPID_TEXT N_("PMT PID")
91 #define PMTPID_LONGTEXT N_("Assigns a fixed PID to the PMT")
92 #define TSID_TEXT N_("TS ID")
93 #define TSID_LONGTEXT N_("Assigns a fixed Transport Stream ID.")
94 #define NETID_TEXT N_("NET ID")
95 #define NETID_LONGTEXT N_("Assigns a fixed Network ID (for SDT table)")
96 #define PMTPROG_TEXT N_("PMT Program numbers (requires --sout-ts-es-id-pid)")
97 #define PMTPROG_LONGTEXT N_("Assigns a program number to each PMT")
98 #define MUXPMT_TEXT N_("Mux PMT (requires --sout-ts-es-id-pid)")
99 #define MUXPMT_LONGTEXT N_("Defines the pids to add to each pmt." )
101 #define SDTDESC_TEXT N_("SDT Descriptors (requires --sout-ts-es-id-pid)")
102 #define SDTDESC_LONGTEXT N_("Defines the descriptors of each SDT" )
104 #define PID_TEXT N_("Set PID to id of ES")
105 #define PID_LONGTEXT N_("set PID to id of es")
107 #define SHAPING_TEXT N_("Shaping delay (ms)")
108 #define SHAPING_LONGTEXT N_("If enabled, the TS muxer will cut the " \
109 "stream in slices of the given duration, and ensure a constant bitrate " \
110 "between the two boundaries. This avoids having huge bitrate peaks for " \
111 "reference frames, in particular.")
112 #define KEYF_TEXT N_("Use keyframes")
113 #define KEYF_LONGTEXT N_("If enabled, and shaping is specified, " \
114 "the TS muxer will place the boundaries at the end of I pictures. In " \
115 "that case, the shaping duration given by the user is a worse case " \
116 "used when no reference frame is available. This enhances the efficiency " \
117 "of the shaping algorithm, since I frames are usually the biggest " \
118 "frames in the stream.")
120 #define PCR_TEXT N_("PCR delay (ms)")
121 #define PCR_LONGTEXT N_("This option allows you to set at which interval " \
122 "PCRs (Program Clock Reference) will be sent. " \
123 "This value should be below 100ms. (default is 70)")
125 #define BMIN_TEXT N_( "Minimum B (deprecated)")
126 #define BMIN_LONGTEXT N_( "This setting is deprecated and not used anymore" )
128 #define BMAX_TEXT N_( "Maximum B (deprecated)")
129 #define BMAX_LONGTEXT N_( "This setting is deprecated and not used anymore")
131 #define DTS_TEXT N_("DTS delay (ms)")
132 #define DTS_LONGTEXT N_("This option will delay the DTS (decoding time " \
133 "stamps) and PTS (presentation timestamps) of the data in the " \
134 "stream, compared to the PCRs. This allows for some buffering inside " \
135 "the client decoder.")
137 #define ACRYPT_TEXT N_("Crypt audio")
138 #define ACRYPT_LONGTEXT N_("Crypt audio using CSA")
139 #define VCRYPT_TEXT N_("Crypt video")
140 #define VCRYPT_LONGTEXT N_("Crypt video using CSA")
142 #define CK_TEXT N_("CSA Key")
143 #define CK_LONGTEXT N_("Defines the CSA encryption key. This must be a " \
144 "16 char string (8 hexadecimal bytes).")
146 #define CPKT_TEXT N_("Packet size in bytes to encrypt")
147 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to encrypt. " \
148 "The encryption routines subtract the TS-header from the value before " \
151 #define SOUT_CFG_PREFIX "sout-ts-"
153 # define MAX_PMT 64 /* Maximum number of programs. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
157 #define MAX_PMT_PID 64 /* Maximum pids in each pmt. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
160 set_description( _("TS muxer (libdvbpsi)") );
161 set_shortname( "MPEG-TS");
162 set_category( CAT_SOUT );
163 set_subcategory( SUBCAT_SOUT_MUX );
164 set_capability( "sout mux", 120 );
165 add_shortcut( "ts" );
167 add_integer( SOUT_CFG_PREFIX "pid-video", 0, NULL,VPID_TEXT, VPID_LONGTEXT,
169 add_integer( SOUT_CFG_PREFIX "pid-audio", 0, NULL, APID_TEXT,
170 APID_LONGTEXT, VLC_TRUE );
171 add_integer( SOUT_CFG_PREFIX "pid-spu", 0, NULL, SPUPID_TEXT,
172 SPUPID_LONGTEXT, VLC_TRUE );
173 add_integer( SOUT_CFG_PREFIX "pid-pmt", 0, NULL, PMTPID_TEXT,
174 PMTPID_LONGTEXT, VLC_TRUE );
175 add_integer( SOUT_CFG_PREFIX "tsid", 0, NULL, TSID_TEXT,
176 TSID_LONGTEXT, VLC_TRUE );
177 #ifdef HAVE_DVBPSI_SDT
178 add_integer( SOUT_CFG_PREFIX "netid", 0, NULL, NETID_TEXT,
179 NETID_LONGTEXT, VLC_TRUE );
181 add_string( SOUT_CFG_PREFIX "program-pmt", NULL, NULL, PMTPROG_TEXT,
182 PMTPROG_LONGTEXT, VLC_TRUE );
183 add_bool( SOUT_CFG_PREFIX "es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT,
185 add_string( SOUT_CFG_PREFIX "muxpmt", NULL, NULL, MUXPMT_TEXT, MUXPMT_LONGTEXT, VLC_TRUE );
186 #ifdef HAVE_DVBPSI_SDT
187 add_string( SOUT_CFG_PREFIX "sdtdesc", NULL, NULL, SDTDESC_TEXT, SDTDESC_LONGTEXT, VLC_TRUE );
190 add_integer( SOUT_CFG_PREFIX "shaping", 200, NULL,SHAPING_TEXT,
191 SHAPING_LONGTEXT, VLC_TRUE );
192 add_bool( SOUT_CFG_PREFIX "use-key-frames", VLC_FALSE, NULL, KEYF_TEXT,
193 KEYF_LONGTEXT, VLC_TRUE );
195 add_integer( SOUT_CFG_PREFIX "pcr", 70, NULL, PCR_TEXT, PCR_LONGTEXT,
197 add_integer( SOUT_CFG_PREFIX "bmin", 0, NULL, BMIN_TEXT, BMIN_LONGTEXT,
199 add_integer( SOUT_CFG_PREFIX "bmax", 0, NULL, BMAX_TEXT, BMAX_LONGTEXT,
201 add_integer( SOUT_CFG_PREFIX "dts-delay", 400, NULL, DTS_TEXT,
202 DTS_LONGTEXT, VLC_TRUE );
204 add_bool( SOUT_CFG_PREFIX "crypt-audio", VLC_TRUE, NULL, ACRYPT_TEXT,
205 ACRYPT_LONGTEXT, VLC_TRUE );
206 add_bool( SOUT_CFG_PREFIX "crypt-video", VLC_TRUE, NULL, VCRYPT_TEXT,
207 VCRYPT_LONGTEXT, VLC_TRUE );
209 add_string( SOUT_CFG_PREFIX "csa-ck", NULL, NULL, CK_TEXT, CK_LONGTEXT,
211 add_integer( SOUT_CFG_PREFIX "csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );
213 set_callbacks( Open, Close );
216 /*****************************************************************************
217 * Local data structures
218 *****************************************************************************/
219 static const char *ppsz_sout_options[] = {
220 "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid", "netid",
221 "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
222 "dts-delay", "csa-ck", "csa-pkt", "crypt-audio", "crypt-video",
223 "muxpmt", "sdtdesc", "program-pmt",
227 typedef struct pmt_map_t /* Holds the mapping between the pmt-pid/pmt table */
230 unsigned long i_prog;
233 typedef struct sdt_desc_t
236 char *psz_service_name; /* name of program */
244 } sout_buffer_chain_t;
246 static inline void BufferChainInit ( sout_buffer_chain_t *c )
250 c->pp_last = &c->p_first;
253 static inline void BufferChainAppend( sout_buffer_chain_t *c, block_t *b )
263 c->pp_last = &b->p_next;
266 static inline block_t *BufferChainGet( sout_buffer_chain_t *c )
268 block_t *b = c->p_first;
273 c->p_first = b->p_next;
275 if( c->p_first == NULL )
277 c->pp_last = &c->p_first;
285 static inline block_t *BufferChainPeek( sout_buffer_chain_t *c )
287 block_t *b = c->p_first;
292 static inline void BufferChainClean( sout_instance_t *p_sout,
293 sout_buffer_chain_t *c )
297 while( ( b = BufferChainGet( c ) ) )
301 BufferChainInit( c );
304 typedef struct ts_stream_t
307 vlc_fourcc_t i_codec;
311 int i_continuity_counter;
312 vlc_bool_t b_discontinuity;
314 /* to be used for carriege of DIV3 */
315 vlc_fourcc_t i_bih_codec;
316 int i_bih_width, i_bih_height;
318 /* Specific to mpeg4 in mpeg2ts */
321 int i_decoder_specific_info;
322 uint8_t *p_decoder_specific_info;
324 /* language is iso639-2T */
327 sout_buffer_chain_t chain_pes;
329 mtime_t i_pes_length;
331 vlc_bool_t b_key_frame;
335 struct sout_mux_sys_t
338 sout_input_t *p_pcr_input;
343 vlc_bool_t b_es_id_pid;
348 int i_pid_free; /* first usable pid */
354 int i_pat_version_number;
357 int i_pmt_version_number;
358 ts_stream_t pmt[MAX_PMT];
359 pmt_map_t pmtmap[MAX_PMT_PID];
360 int i_pmt_program_number[MAX_PMT];
361 sdt_desc_t sdt_descriptors[MAX_PMT];
365 int i_null_continuity_counter; /* Needed ? */
367 dvbpsi_pmt_t *dvbpmt;
369 /* for TS building */
370 int64_t i_bitrate_min;
371 int64_t i_bitrate_max;
373 int64_t i_shaping_delay;
378 vlc_bool_t b_use_key_frames;
380 mtime_t i_pcr; /* last PCR emited */
384 vlc_bool_t b_crypt_audio;
385 vlc_bool_t b_crypt_video;
388 /* Reserve a pid and return it */
389 static int AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
392 if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
394 i_pid = p_sys->i_pid_video;
395 p_sys->i_pid_video = 0;
397 else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
399 i_pid = p_sys->i_pid_audio;
400 p_sys->i_pid_audio = 0;
402 else if ( i_cat == SPU_ES && p_sys->i_pid_spu )
404 i_pid = p_sys->i_pid_spu;
405 p_sys->i_pid_spu = 0;
409 i_pid = ++p_sys->i_pid_free;
414 static int pmtcompare( const void *pa, const void *pb )
416 if ( ((pmt_map_t *)pa)->i_pid < ((pmt_map_t *)pb)->i_pid )
418 else if ( ((pmt_map_t *)pa)->i_pid > ((pmt_map_t *)pb)->i_pid )
424 static int intcompare( const void *pa, const void *pb )
426 if ( *(int *)pa < *(int *)pb )
428 else if ( *(int *)pa > *(int *)pb )
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control ( sout_mux_t *, int, va_list );
438 static int AddStream( sout_mux_t *, sout_input_t * );
439 static int DelStream( sout_mux_t *, sout_input_t * );
440 static int Mux ( sout_mux_t * );
442 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo );
443 static void TSSchedule ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
444 mtime_t i_pcr_length, mtime_t i_pcr_dts );
445 static void TSDate ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
446 mtime_t i_pcr_length, mtime_t i_pcr_dts );
447 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
448 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
450 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, vlc_bool_t b_pcr );
451 static void TSSetPCR( block_t *p_ts, mtime_t i_dts );
453 static void PEStoTS ( sout_instance_t *, sout_buffer_chain_t *, block_t *, ts_stream_t * );
455 /*****************************************************************************
457 *****************************************************************************/
458 static int Open( vlc_object_t *p_this )
460 sout_mux_t *p_mux =(sout_mux_t*)p_this;
461 sout_mux_sys_t *p_sys = NULL;
465 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
467 p_sys = malloc( sizeof( sout_mux_sys_t ) );
470 p_sys->i_pmtslots = p_sys->b_sdt = 0;
471 p_sys->i_num_pmt = 1;
472 p_sys->dvbpmt = NULL;
473 memset( &p_sys->pmtmap, 0, sizeof(p_sys->pmtmap) );
475 p_mux->pf_control = Control;
476 p_mux->pf_addstream = AddStream;
477 p_mux->pf_delstream = DelStream;
479 p_mux->p_sys = p_sys;
481 srand( (uint32_t)mdate() );
482 for ( i = 0; i < MAX_PMT; i++ )
483 p_sys->sdt_descriptors[i].psz_service_name
484 = p_sys->sdt_descriptors[i].psz_provider = NULL;
485 memset( p_sys->sdt_descriptors, 0, sizeof(sdt_desc_t) );
487 p_sys->i_audio_bound = 0;
488 p_sys->i_video_bound = 0;
490 var_Get( p_mux, SOUT_CFG_PREFIX "es-id-pid", &val );
491 p_sys->b_es_id_pid = val.b_bool;
493 var_Get( p_mux, SOUT_CFG_PREFIX "muxpmt", &val );
495 fetch string of pmts. Here's a sample: --sout-ts-muxpmt="0x451,0x200,0x28a,0x240,,0x450,0x201,0x28b,0x241,,0x452,0x202,0x28c,0x242"
496 This would mean 0x451, 0x200, 0x28a, 0x240 would fall under one pmt (program), 0x450,0x201,0x28b,0x241 would fall under another
498 if( val.psz_string != NULL && *val.psz_string )
501 char *psz = val.psz_string;
507 i_pid = strtoul( psz, &psz_next, 0 );
509 if ( strlen(psz_next) > 0 )
514 if ( p_sys->i_num_pmt > MAX_PMT )
517 "Number of PMTs greater than compiled maximum (%d)", MAX_PMT );
518 p_sys->i_num_pmt = MAX_PMT;
523 p_sys->pmtmap[p_sys->i_pmtslots].i_pid = i_pid;
524 p_sys->pmtmap[p_sys->i_pmtslots].i_prog = p_sys->i_num_pmt - 1;
526 if ( p_sys->i_pmtslots > MAX_PMT_PID )
529 "Number of pids in PMT greater than compiled maximum (%d)",
531 p_sys->i_pmtslots = MAX_PMT_PID;
535 /* Now sort according to pids for fast search later on */
536 qsort( (void *)p_sys->pmtmap, p_sys->i_pmtslots,
537 sizeof(pmt_map_t), &pmtcompare );
542 if( val.psz_string != NULL) free( val.psz_string );
544 p_sys->i_pat_version_number = rand() % 32;
545 p_sys->pat.i_pid = 0;
546 p_sys->pat.i_continuity_counter = 0;
547 p_sys->pat.b_discontinuity = VLC_FALSE;
549 var_Get( p_mux, SOUT_CFG_PREFIX "tsid", &val );
551 p_sys->i_tsid = val.i_int;
553 p_sys->i_tsid = rand() % 65536;
555 p_sys->i_netid = rand() % 65536;
556 #ifdef HAVE_DVBPSI_SDT
557 var_Get( p_mux, SOUT_CFG_PREFIX "netid", &val );
559 p_sys->i_netid = val.i_int;
562 p_sys->i_pmt_version_number = rand() % 32;
563 for( i = 0; i < p_sys->i_num_pmt; i++ )
565 p_sys->pmt[i].i_continuity_counter = 0;
566 p_sys->pmt[i].b_discontinuity = VLC_FALSE;
569 p_sys->sdt.i_pid = 0x11;
570 p_sys->sdt.i_continuity_counter = 0;
571 p_sys->sdt.b_discontinuity = VLC_FALSE;
573 #ifdef HAVE_DVBPSI_SDT
574 var_Get( p_mux, SOUT_CFG_PREFIX "sdtdesc", &val );
575 p_sys->b_sdt = val.psz_string && *val.psz_string ? VLC_TRUE : VLC_FALSE;
577 /* Syntax is provider_sdt1,service_name_sdt1,provider_sdt2,service_name_sdt2... */
581 char *psz = val.psz_string;
582 char *psz_sdttoken = psz;
585 while ( psz_sdttoken != NULL )
587 char *psz_end = strchr( psz_sdttoken, ',' );
588 if( psz_end != NULL )
594 p_sys->sdt_descriptors[i/2].psz_provider
595 = strdup(psz_sdttoken);
599 p_sys->sdt_descriptors[i/2].psz_service_name
600 = strdup(psz_sdttoken);
604 psz_sdttoken = psz_end;
607 if( val.psz_string != NULL ) free( val.psz_string );
609 p_sys->b_sdt = VLC_FALSE;
612 var_Get( p_mux, SOUT_CFG_PREFIX "program-pmt", &val );
613 if( val.psz_string && *val.psz_string )
616 char *psz = val.psz_string;
621 while ( psz != NULL )
623 i_pid = strtoul( psz, &psz_next, 0 );
624 if( strlen(psz_next) > 0 )
632 msg_Err( p_mux, "Number of PMTs > maximum (%d)",
637 p_sys->i_pmt_program_number[i] = i_pid;
644 /* Option not specified, use 1, 2, 3... */
645 for( i = 0; i < p_sys->i_num_pmt; i++ )
646 p_sys->i_pmt_program_number[i] = i + 1;
648 if( val.psz_string != NULL ) free( val.psz_string );
650 var_Get( p_mux, SOUT_CFG_PREFIX "pid-pmt", &val );
653 for( i = 0; i < p_sys->i_num_pmt; i++ )
654 p_sys->pmt[i].i_pid = val.i_int + i; /* Does this make any sense? */
658 for( i = 0; i < p_sys->i_num_pmt; i++ )
659 p_sys->pmt[i].i_pid = 0x42 + i;
662 p_sys->i_pid_free = p_sys->pmt[p_sys->i_num_pmt - 1].i_pid + 1;
664 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
665 p_sys->i_pid_video = val.i_int;
666 if ( p_sys->i_pid_video > p_sys->i_pid_free )
668 p_sys->i_pid_free = p_sys->i_pid_video + 1;
671 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
672 p_sys->i_pid_audio = val.i_int;
673 if ( p_sys->i_pid_audio > p_sys->i_pid_free )
675 p_sys->i_pid_free = p_sys->i_pid_audio + 1;
678 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
679 p_sys->i_pid_spu = val.i_int;
680 if ( p_sys->i_pid_spu > p_sys->i_pid_free )
682 p_sys->i_pid_free = p_sys->i_pid_spu + 1;
685 p_sys->i_pcr_pid = 0x1fff;
686 p_sys->p_pcr_input = NULL;
688 p_sys->i_mpeg4_streams = 0;
690 p_sys->i_null_continuity_counter = 0;
692 /* Allow to create constrained stream */
693 var_Get( p_mux, SOUT_CFG_PREFIX "bmin", &val );
694 p_sys->i_bitrate_min = val.i_int;
696 var_Get( p_mux, SOUT_CFG_PREFIX "bmax", &val );
697 p_sys->i_bitrate_max = val.i_int;
699 if( p_sys->i_bitrate_min > 0 && p_sys->i_bitrate_max > 0 &&
700 p_sys->i_bitrate_min > p_sys->i_bitrate_max )
702 msg_Err( p_mux, "incompatible minimum and maximum bitrate, "
703 "disabling bitrate control" );
704 p_sys->i_bitrate_min = 0;
705 p_sys->i_bitrate_max = 0;
707 if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
709 msg_Err( p_mux, "bmin and bmax no more supported "
710 "(if you need them report it)" );
713 var_Get( p_mux, SOUT_CFG_PREFIX "shaping", &val );
714 p_sys->i_shaping_delay = (int64_t)val.i_int * 1000;
715 if( p_sys->i_shaping_delay <= 0 )
718 "invalid shaping ("I64Fd"ms) resetting to 200ms",
719 p_sys->i_shaping_delay / 1000 );
720 p_sys->i_shaping_delay = 200000;
723 var_Get( p_mux, SOUT_CFG_PREFIX "pcr", &val );
724 p_sys->i_pcr_delay = (int64_t)val.i_int * 1000;
725 if( p_sys->i_pcr_delay <= 0 ||
726 p_sys->i_pcr_delay >= p_sys->i_shaping_delay )
729 "invalid pcr delay ("I64Fd"ms) resetting to 70ms",
730 p_sys->i_pcr_delay / 1000 );
731 p_sys->i_pcr_delay = 70000;
734 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
735 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
737 msg_Dbg( p_mux, "shaping="I64Fd" pcr="I64Fd" dts_delay="I64Fd,
738 p_sys->i_shaping_delay, p_sys->i_pcr_delay, p_sys->i_dts_delay );
740 var_Get( p_mux, SOUT_CFG_PREFIX "use-key-frames", &val );
741 p_sys->b_use_key_frames = val.b_bool;
743 /* for TS generation */
747 var_Get( p_mux, SOUT_CFG_PREFIX "csa-ck", &val );
748 if( val.psz_string && *val.psz_string )
750 char *psz = val.psz_string;
753 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
757 if( strlen( psz ) != 16 )
759 msg_Dbg( p_mux, "invalid csa ck (it must be 16 chars long)" );
763 uint64_t i_ck = strtoull( psz, NULL, 16 );
767 for( i = 0; i < 8; i++ )
769 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
771 #ifndef TS_NO_CSA_CK_MSG
772 msg_Dbg( p_mux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
773 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
775 p_sys->csa = csa_New();
780 csa_SetCW( p_sys->csa, ck, ck );
782 var_Get( p_mux, SOUT_CFG_PREFIX "csa-pkt", &pkt_val );
783 if( pkt_val.i_int < 12 || pkt_val.i_int > 188 )
785 msg_Err( p_mux, "wrong packet size %d specified.", pkt_val.i_int );
786 msg_Warn( p_mux, "using default packet size of 188 bytes" );
787 p_sys->i_csa_pkt_size = 188;
789 else p_sys->i_csa_pkt_size = pkt_val.i_int;
790 msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
794 if( val.psz_string ) free( val.psz_string );
796 var_Get( p_mux, SOUT_CFG_PREFIX "crypt-audio", &val );
797 p_sys->b_crypt_audio = val.b_bool;
799 var_Get( p_mux, SOUT_CFG_PREFIX "crypt-video", &val );
800 p_sys->b_crypt_video = val.b_bool;
805 /*****************************************************************************
807 *****************************************************************************/
808 static void Close( vlc_object_t * p_this )
810 sout_mux_t *p_mux = (sout_mux_t*)p_this;
811 sout_mux_sys_t *p_sys = p_mux->p_sys;
816 csa_Delete( p_sys->csa );
818 for( i = 0; i < MAX_PMT; i++ )
820 if( p_sys->sdt_descriptors[i].psz_service_name != NULL )
821 free( p_sys->sdt_descriptors[i].psz_service_name );
822 if( p_sys->sdt_descriptors[i].psz_provider != NULL )
823 free( p_sys->sdt_descriptors[i].psz_provider );
826 if( p_sys->dvbpmt != NULL ) /* safety */
827 free ( p_sys->dvbpmt );
832 /*****************************************************************************
834 *****************************************************************************/
835 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
842 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
843 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
847 case MUX_GET_ADD_STREAM_WAIT:
848 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
849 *pb_bool = VLC_FALSE;
853 ppsz = (char**)va_arg( args, char ** );
854 *ppsz = strdup( "video/mpeg" ); /* FIXME not sure */
862 /*****************************************************************************
863 * AddStream: called for each stream addition
864 *****************************************************************************/
865 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
867 sout_mux_sys_t *p_sys = p_mux->p_sys;
868 ts_stream_t *p_stream;
870 p_input->p_sys = p_stream = malloc( sizeof( ts_stream_t ) );
872 /* Init this new stream */
873 if ( p_sys->b_es_id_pid )
874 p_stream->i_pid = p_input->p_fmt->i_id & 0x1fff;
876 p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
877 p_stream->i_codec = p_input->p_fmt->i_codec;
878 p_stream->i_continuity_counter = 0;
879 p_stream->b_discontinuity = VLC_FALSE;
880 p_stream->i_decoder_specific_info = 0;
881 p_stream->p_decoder_specific_info = NULL;
883 msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d",
884 (char*)&p_input->p_fmt->i_codec, p_stream->i_pid );
886 /* All others fields depand on codec */
887 switch( p_input->p_fmt->i_cat )
890 switch( p_input->p_fmt->i_codec )
892 case VLC_FOURCC( 'm', 'p','g', 'v' ):
893 /* TODO: do we need to check MPEG-I/II ? */
894 p_stream->i_stream_type = 0x02;
895 p_stream->i_stream_id = 0xe0;
897 case VLC_FOURCC( 'm', 'p','4', 'v' ):
898 p_stream->i_stream_type = 0x10;
899 p_stream->i_stream_id = 0xe0;
900 p_stream->i_es_id = p_stream->i_pid;
902 case VLC_FOURCC( 'h', '2','6', '4' ):
903 p_stream->i_stream_type = 0x1b;
904 p_stream->i_stream_id = 0xe0;
906 /* XXX dirty dirty but somebody want that:
907 * using crapy MS-codec XXX */
908 /* I didn't want to do that :P */
909 case VLC_FOURCC( 'H', '2', '6', '3' ):
910 case VLC_FOURCC( 'I', '2', '6', '3' ):
911 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
912 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
913 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
914 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
915 case VLC_FOURCC( 'D', 'I', 'V', '2' ):
916 case VLC_FOURCC( 'D', 'I', 'V', '1' ):
917 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
918 p_stream->i_stream_type = 0xa0; /* private */
919 p_stream->i_stream_id = 0xa0; /* beurk */
920 p_stream->i_bih_codec = p_input->p_fmt->i_codec;
921 p_stream->i_bih_width = p_input->p_fmt->video.i_width;
922 p_stream->i_bih_height = p_input->p_fmt->video.i_height;
928 p_sys->i_video_bound++;
932 switch( p_input->p_fmt->i_codec )
934 case VLC_FOURCC( 'm', 'p','g', 'a' ):
935 p_stream->i_stream_type =
936 p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
937 p_stream->i_stream_id = 0xc0;
939 case VLC_FOURCC( 'a', '5','2', ' ' ):
940 p_stream->i_stream_type = 0x81;
941 p_stream->i_stream_id = 0xbd;
943 case VLC_FOURCC( 'l', 'p','c', 'm' ):
944 p_stream->i_stream_type = 0x83;
945 p_stream->i_stream_id = 0xbd;
947 case VLC_FOURCC( 'd', 't','s', ' ' ):
948 p_stream->i_stream_type = 0x06;
949 p_stream->i_stream_id = 0xbd;
951 case VLC_FOURCC( 'm', 'p','4', 'a' ):
952 p_stream->i_stream_type = 0x11;
953 p_stream->i_stream_id = 0xfa;
954 p_sys->i_mpeg4_streams++;
955 p_stream->i_es_id = p_stream->i_pid;
961 p_sys->i_audio_bound++;
965 switch( p_input->p_fmt->i_codec )
967 case VLC_FOURCC( 's', 'p','u', ' ' ):
968 p_stream->i_stream_type = 0x82;
969 p_stream->i_stream_id = 0xbd;
971 case VLC_FOURCC( 's', 'u','b', 't' ):
972 p_stream->i_stream_type = 0x12;
973 p_stream->i_stream_id = 0xfa;
974 p_sys->i_mpeg4_streams++;
975 p_stream->i_es_id = p_stream->i_pid;
977 case VLC_FOURCC('d','v','b','s'):
978 p_stream->i_stream_type = 0x06;
979 p_stream->i_es_id = p_input->p_fmt->subs.dvb.i_id;
980 p_stream->i_stream_id = 0xbd;
982 case VLC_FOURCC('t','e','l','x'):
983 p_stream->i_stream_type = 0x06;
984 p_stream->i_stream_id = 0xbd; /* FIXME */
999 p_stream->lang[2] = '\0';
1000 if( p_input->p_fmt->psz_language )
1002 char *psz = p_input->p_fmt->psz_language;
1003 const iso639_lang_t *pl = NULL;
1005 if( strlen( psz ) == 2 )
1007 pl = GetLang_1( psz );
1009 else if( strlen( psz ) == 3 )
1011 pl = GetLang_2B( psz );
1012 if( !strcmp( pl->psz_iso639_1, "??" ) )
1014 pl = GetLang_2T( psz );
1017 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1019 p_stream->lang[0] = pl->psz_iso639_2T[0];
1020 p_stream->lang[1] = pl->psz_iso639_2T[1];
1021 p_stream->lang[2] = pl->psz_iso639_2T[2];
1023 msg_Dbg( p_mux, " - lang=%c%c%c",
1024 p_stream->lang[0], p_stream->lang[1], p_stream->lang[2] );
1028 /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
1029 p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra;
1030 if( p_stream->i_decoder_specific_info > 0 )
1032 p_stream->p_decoder_specific_info =
1033 malloc( p_stream->i_decoder_specific_info );
1034 memcpy( p_stream->p_decoder_specific_info,
1035 p_input->p_fmt->p_extra,
1036 p_input->p_fmt->i_extra );
1039 /* Create decoder specific info for subt */
1040 if( p_stream->i_codec == VLC_FOURCC( 's', 'u','b', 't' ) )
1044 p_stream->i_decoder_specific_info = 55;
1045 p_stream->p_decoder_specific_info = p =
1046 malloc( p_stream->i_decoder_specific_info );
1048 p[0] = 0x10; /* textFormat, 0x10 for 3GPP TS 26.245 */
1049 p[1] = 0x00; /* flags: 1b: associated video info flag
1053 p[2] = 52; /* remaining size */
1057 p[0] = p[1] = p[2] = p[3] = 0; p+=4; /* display flags */
1058 *p++ = 0; /* horizontal justification (-1: left, 0 center, 1 right) */
1059 *p++ = 1; /* vertical justification (-1: top, 0 center, 1 bottom) */
1061 p[0] = p[1] = p[2] = 0x00; p+=3;/* background rgb */
1062 *p++ = 0xff; /* background a */
1064 p[0] = p[1] = 0; p += 2; /* text box top */
1065 p[0] = p[1] = 0; p += 2; /* text box left */
1066 p[0] = p[1] = 0; p += 2; /* text box bottom */
1067 p[0] = p[1] = 0; p += 2; /* text box right */
1069 p[0] = p[1] = 0; p += 2; /* start char */
1070 p[0] = p[1] = 0; p += 2; /* end char */
1071 p[0] = p[1] = 0; p += 2; /* default font id */
1073 *p++ = 0; /* font style flags */
1074 *p++ = 12; /* font size */
1076 p[0] = p[1] = p[2] = 0x00; p+=3;/* foreground rgb */
1077 *p++ = 0x00; /* foreground a */
1079 p[0] = p[1] = p[2] = 0; p[3] = 22; p += 4;
1080 memcpy( p, "ftab", 4 ); p += 4;
1081 *p++ = 0; *p++ = 1; /* entry count */
1082 p[0] = p[1] = 0; p += 2; /* font id */
1083 *p++ = 9; /* font name length */
1084 memcpy( p, "Helvetica", 9 ); /* font name */
1087 /* Init pes chain */
1088 BufferChainInit( &p_stream->chain_pes );
1089 p_stream->i_pes_dts = 0;
1090 p_stream->i_pes_length = 0;
1091 p_stream->i_pes_used = 0;
1092 p_stream->b_key_frame = 0;
1094 /* We only change PMT version (PAT isn't changed) */
1095 p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
1097 /* Update pcr_pid */
1098 if( p_input->p_fmt->i_cat != SPU_ES &&
1099 ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
1101 if( p_sys->p_pcr_input )
1103 /* There was already a PCR stream, so clean context */
1106 p_sys->i_pcr_pid = p_stream->i_pid;
1107 p_sys->p_pcr_input = p_input;
1109 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1115 /*****************************************************************************
1116 * DelStream: called before a stream deletion
1117 *****************************************************************************/
1118 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1120 sout_mux_sys_t *p_sys = p_mux->p_sys;
1121 ts_stream_t *p_stream;
1124 p_stream = (ts_stream_t*)p_input->p_sys;
1125 msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
1127 if( p_sys->i_pcr_pid == p_stream->i_pid )
1131 /* Find a new pcr stream (Prefer Video Stream) */
1132 p_sys->i_pcr_pid = 0x1fff;
1133 p_sys->p_pcr_input = NULL;
1134 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1136 if( p_mux->pp_inputs[i] == p_input )
1141 if( p_mux->pp_inputs[i]->p_fmt->i_cat == VIDEO_ES )
1144 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1145 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1148 else if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES &&
1149 p_sys->i_pcr_pid == 0x1fff )
1152 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1153 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1156 if( p_sys->p_pcr_input )
1158 /* Empty TS buffer */
1161 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1164 /* Empty all data in chain_pes */
1165 BufferChainClean( p_mux->p_sout, &p_stream->chain_pes );
1167 if( p_stream->p_decoder_specific_info )
1169 free( p_stream->p_decoder_specific_info );
1171 if( p_stream->i_stream_id == 0xfa ||
1172 p_stream->i_stream_id == 0xfb ||
1173 p_stream->i_stream_id == 0xfe )
1175 p_sys->i_mpeg4_streams--;
1178 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
1181 int i_pid_video = val.i_int;
1182 if ( i_pid_video == p_stream->i_pid )
1184 p_sys->i_pid_video = i_pid_video;
1185 msg_Dbg( p_mux, "freeing video PID %d", i_pid_video );
1188 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
1191 int i_pid_audio = val.i_int;
1192 if ( i_pid_audio == p_stream->i_pid )
1194 p_sys->i_pid_audio = i_pid_audio;
1195 msg_Dbg( p_mux, "freeing audio PID %d", i_pid_audio );
1198 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
1201 int i_pid_spu = val.i_int;
1202 if ( i_pid_spu == p_stream->i_pid )
1204 p_sys->i_pid_spu = i_pid_spu;
1205 msg_Dbg( p_mux, "freeing spu PID %d", i_pid_spu );
1210 /* We only change PMT version (PAT isn't changed) */
1211 p_sys->i_pmt_version_number++; p_sys->i_pmt_version_number %= 32;
1216 /*****************************************************************************
1217 * Mux: Call each time there is new data for at least one stream
1218 *****************************************************************************
1220 *****************************************************************************/
1221 static int Mux( sout_mux_t *p_mux )
1223 sout_mux_sys_t *p_sys = p_mux->p_sys;
1224 ts_stream_t *p_pcr_stream;
1226 if( p_sys->i_pcr_pid == 0x1fff )
1228 msg_Dbg( p_mux, "waiting for PCR streams" );
1232 p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
1236 sout_buffer_chain_t chain_ts;
1240 mtime_t i_pcr_length;
1241 mtime_t i_shaping_delay;
1244 if( p_pcr_stream->b_key_frame )
1246 i_shaping_delay = p_pcr_stream->i_pes_length;
1250 i_shaping_delay = p_sys->i_shaping_delay;
1253 /* 1: get enough PES packet for all input */
1256 vlc_bool_t b_ok = VLC_TRUE;
1259 /* Accumulate enough data in the pcr stream (>i_shaping_delay) */
1260 /* Accumulate enough data in all other stream ( >= length of pcr)*/
1261 for( i = -1; i < p_mux->i_nb_inputs; i++ )
1263 sout_input_t *p_input;
1264 ts_stream_t *p_stream;
1265 int64_t i_spu_delay = 0;
1268 p_input = p_sys->p_pcr_input;
1269 else if( p_mux->pp_inputs[i]->p_sys == p_pcr_stream )
1272 p_input = p_mux->pp_inputs[i];
1273 p_stream = (ts_stream_t*)p_input->p_sys;
1275 if( ( p_stream == p_pcr_stream &&
1276 p_stream->i_pes_length < i_shaping_delay ) ||
1277 p_stream->i_pes_dts + p_stream->i_pes_length <
1278 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1280 /* Need more data */
1281 if( p_input->p_fifo->i_depth <= 1 )
1283 if( p_input->p_fmt->i_cat == AUDIO_ES ||
1284 p_input->p_fmt->i_cat == VIDEO_ES )
1286 /* We need more data */
1289 else if( p_input->p_fifo->i_depth <= 0 )
1291 /* spu, only one packet is needed */
1296 /* Don't mux the SPU yet if it is too early */
1297 block_t *p_spu = block_FifoShow( p_input->p_fifo );
1300 p_spu->i_dts - p_pcr_stream->i_pes_dts;
1302 if( i_spu_delay > i_shaping_delay &&
1303 i_spu_delay < I64C(100000000) )
1306 if ( i_spu_delay >= I64C(100000000)
1307 || i_spu_delay < 10000 )
1309 BufferChainClean( p_mux->p_sout,
1310 &p_stream->chain_pes );
1311 p_stream->i_pes_dts = 0;
1312 p_stream->i_pes_used = 0;
1313 p_stream->i_pes_length = 0;
1320 if( p_stream == p_pcr_stream
1321 || p_input->p_fmt->i_codec !=
1322 VLC_FOURCC('m', 'p', 'g', 'a') )
1323 p_data = block_FifoGet( p_input->p_fifo );
1325 p_data = FixPES( p_mux, p_input->p_fifo );
1327 if( p_input->p_fifo->i_depth > 0 &&
1328 p_input->p_fmt->i_cat != SPU_ES )
1330 block_t *p_next = block_FifoShow( p_input->p_fifo );
1331 p_data->i_length = p_next->i_dts - p_data->i_dts;
1333 else if( p_input->p_fmt->i_codec !=
1334 VLC_FOURCC('s', 'u', 'b', 't' ) )
1335 p_data->i_length = 1000;
1337 if( ( p_pcr_stream->i_pes_dts > 0 &&
1338 p_data->i_dts - 10000000 > p_pcr_stream->i_pes_dts +
1339 p_pcr_stream->i_pes_length ) ||
1340 p_data->i_dts < p_stream->i_pes_dts ||
1341 ( p_stream->i_pes_dts > 0 &&
1342 p_input->p_fmt->i_cat != SPU_ES &&
1343 p_data->i_dts - 10000000 > p_stream->i_pes_dts +
1344 p_stream->i_pes_length ) )
1346 msg_Warn( p_mux, "packet with too strange dts "
1347 "(dts="I64Fd",old="I64Fd",pcr="I64Fd")",
1348 p_data->i_dts, p_stream->i_pes_dts,
1349 p_pcr_stream->i_pes_dts );
1350 block_Release( p_data );
1352 BufferChainClean( p_mux->p_sout,
1353 &p_stream->chain_pes );
1354 p_stream->i_pes_dts = 0;
1355 p_stream->i_pes_used = 0;
1356 p_stream->i_pes_length = 0;
1358 if( p_input->p_fmt->i_cat != SPU_ES )
1360 BufferChainClean( p_mux->p_sout,
1361 &p_pcr_stream->chain_pes );
1362 p_pcr_stream->i_pes_dts = 0;
1363 p_pcr_stream->i_pes_used = 0;
1364 p_pcr_stream->i_pes_length = 0;
1369 int i_header_size = 0;
1370 int b_data_alignment = 0;
1371 if( p_input->p_fmt->i_cat == SPU_ES )
1373 if( p_input->p_fmt->i_codec ==
1374 VLC_FOURCC('s','u','b','t') )
1376 /* Prepend header */
1377 p_data = block_Realloc( p_data, 2,
1379 p_data->p_buffer[0] =
1380 ( (p_data->i_buffer - 2) >> 8) & 0xff;
1381 p_data->p_buffer[1] =
1382 ( (p_data->i_buffer - 2) ) & 0xff;
1384 /* remove trailling \0 if any */
1385 if( p_data->i_buffer > 2 &&
1386 p_data->p_buffer[p_data->i_buffer -1] ==
1390 /* Append a empty sub (sub text only) */
1391 if( p_data->i_length > 0 &&
1392 !( p_data->i_buffer == 1 &&
1393 *p_data->p_buffer == ' ' ) )
1395 block_t *p_spu = block_New( p_mux, 3 );
1397 p_spu->i_dts = p_spu->i_pts =
1398 p_data->i_dts + p_data->i_length;
1399 p_spu->i_length = 1000;
1401 p_spu->p_buffer[0] = 0;
1402 p_spu->p_buffer[1] = 1;
1403 p_spu->p_buffer[2] = ' ';
1405 E_(EStoPES)( p_mux->p_sout, &p_spu, p_spu,
1407 p_stream->i_stream_id, 1,
1409 p_data->p_next = p_spu;
1412 else if( p_input->p_fmt->i_codec ==
1413 VLC_FOURCC('t','e','l','x') )
1416 i_header_size = 0x24;
1417 b_data_alignment = 1;
1420 else if( p_data->i_length < 0 ||
1421 p_data->i_length > 2000000 )
1423 /* FIXME choose a better value, but anyway we
1424 * should never have to do that */
1425 p_data->i_length = 1000;
1428 p_stream->i_pes_length += p_data->i_length;
1429 if( p_stream->i_pes_dts == 0 )
1431 p_stream->i_pes_dts = p_data->i_dts;
1434 /* Convert to pes */
1435 if( p_stream->i_stream_id == 0xa0 &&
1436 p_data->i_pts <= 0 )
1438 /* XXX yes I know, it's awfull, but it's needed,
1439 * so don't remove it ... */
1440 p_data->i_pts = p_data->i_dts;
1443 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
1444 p_input->p_fmt, p_stream->i_stream_id,
1445 1, b_data_alignment, i_header_size, 0 );
1447 BufferChainAppend( &p_stream->chain_pes, p_data );
1449 if( p_sys->b_use_key_frames && p_stream == p_pcr_stream
1450 && (p_data->i_flags & BLOCK_FLAG_TYPE_I)
1451 && !(p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME)
1452 && (p_stream->i_pes_length > 400000) )
1454 i_shaping_delay = p_stream->i_pes_length;
1455 p_stream->b_key_frame = 1;
1468 i_pcr_dts = p_pcr_stream->i_pes_dts;
1469 i_pcr_length = p_pcr_stream->i_pes_length;
1470 p_pcr_stream->b_key_frame = 0;
1472 /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
1473 /* 2: calculate non accurate total size of muxed ts */
1475 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1477 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1480 /* False for pcr stream but it will be enough to do PCR algo */
1481 for( p_pes = p_stream->chain_pes.p_first; p_pes != NULL;
1482 p_pes = p_pes->p_next )
1484 int i_size = p_pes->i_buffer;
1485 if( p_pes->i_dts + p_pes->i_length >
1486 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1488 mtime_t i_frag = p_pcr_stream->i_pes_dts +
1489 p_pcr_stream->i_pes_length - p_pes->i_dts;
1495 i_size = p_pes->i_buffer * i_frag / p_pes->i_length;
1497 i_packet_count += ( i_size + 183 ) / 184;
1500 /* add overhead for PCR (not really exact) */
1501 i_packet_count += (8 * i_pcr_length / p_sys->i_pcr_delay + 175) / 176;
1503 /* 3: mux PES into TS */
1504 BufferChainInit( &chain_ts );
1505 /* append PAT/PMT -> FIXME with big pcr delay it won't have enough pat/pmt */
1506 GetPAT( p_mux, &chain_ts );
1507 GetPMT( p_mux, &chain_ts );
1509 i_packet_count += chain_ts.i_depth;
1510 /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
1516 ts_stream_t *p_stream;
1517 sout_input_t *p_input;
1521 /* Select stream (lowest dts) */
1522 for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
1524 p_input = p_mux->pp_inputs[i];
1525 p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1527 if( p_stream->i_pes_dts == 0 )
1532 if( i_stream == -1 ||
1533 p_stream->i_pes_dts < i_dts )
1536 i_dts = p_stream->i_pes_dts;
1539 if( i_stream == -1 || i_dts > i_pcr_dts + i_pcr_length )
1543 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1545 /* do we need to issue pcr */
1547 if( p_stream == p_pcr_stream &&
1548 i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >=
1549 p_sys->i_pcr + p_sys->i_pcr_delay )
1552 p_sys->i_pcr = i_pcr_dts + i_packet_pos *
1553 i_pcr_length / i_packet_count;
1556 /* Build the TS packet */
1557 p_ts = TSNew( p_mux, p_stream, b_pcr );
1558 if( p_sys->csa != NULL &&
1559 (p_input->p_fmt->i_cat != AUDIO_ES || p_sys->b_crypt_audio) &&
1560 (p_input->p_fmt->i_cat != VIDEO_ES || p_sys->b_crypt_video) )
1562 p_ts->i_flags |= BLOCK_FLAG_SCRAMBLED;
1567 BufferChainAppend( &chain_ts, p_ts );
1570 /* 4: date and send */
1571 TSSchedule( p_mux, &chain_ts, i_pcr_length, i_pcr_dts );
1575 #define STD_PES_PAYLOAD 170
1576 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
1581 p_data = block_FifoShow( p_fifo );
1582 i_size = p_data->i_buffer;
1584 if( i_size == STD_PES_PAYLOAD )
1586 return block_FifoGet( p_fifo );
1588 else if( i_size > STD_PES_PAYLOAD )
1590 block_t *p_new = block_New( p_mux, STD_PES_PAYLOAD );
1591 p_mux->p_vlc->pf_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
1592 p_new->i_pts = p_data->i_pts;
1593 p_new->i_dts = p_data->i_dts;
1594 p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
1596 p_data->i_buffer -= STD_PES_PAYLOAD;
1597 p_data->p_buffer += STD_PES_PAYLOAD;
1598 p_data->i_pts += p_new->i_length;
1599 p_data->i_dts += p_new->i_length;
1600 p_data->i_length -= p_new->i_length;
1601 p_data->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1609 p_data = block_FifoGet( p_fifo );
1610 p_data = block_Realloc( p_data, 0, STD_PES_PAYLOAD );
1611 p_next = block_FifoShow( p_fifo );
1612 if ( p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME )
1614 p_data->i_flags &= ~BLOCK_FLAG_NO_KEYFRAME;
1615 p_data->i_pts = p_next->i_pts;
1616 p_data->i_dts = p_next->i_dts;
1618 i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
1620 p_mux->p_vlc->pf_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer,
1622 p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
1623 p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
1624 p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
1625 p_next->i_buffer -= i_copy;
1626 p_next->p_buffer += i_copy;
1627 p_next->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1629 if( !p_next->i_buffer )
1631 p_next = block_FifoGet( p_fifo );
1632 block_Release( p_next );
1638 static void TSSchedule( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1639 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1641 sout_mux_sys_t *p_sys = p_mux->p_sys;
1642 sout_buffer_chain_t new_chain;
1643 int i_packet_count = p_chain_ts->i_depth;
1646 BufferChainInit( &new_chain );
1648 if ( i_pcr_length <= 0 )
1650 i_pcr_length = i_packet_count;
1653 for( i = 0; i < i_packet_count; i++ )
1655 block_t *p_ts = BufferChainGet( p_chain_ts );
1656 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1658 BufferChainAppend( &new_chain, p_ts );
1661 p_ts->i_dts + p_sys->i_dts_delay * 2/3 < i_new_dts )
1663 mtime_t i_max_diff = i_new_dts - p_ts->i_dts;
1664 mtime_t i_cut_dts = p_ts->i_dts;
1666 p_ts = BufferChainPeek( p_chain_ts );
1668 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1669 while ( p_ts != NULL && i_new_dts - p_ts->i_dts >= i_max_diff )
1671 p_ts = BufferChainGet( p_chain_ts );
1672 i_max_diff = i_new_dts - p_ts->i_dts;
1673 i_cut_dts = p_ts->i_dts;
1674 BufferChainAppend( &new_chain, p_ts );
1676 p_ts = BufferChainPeek( p_chain_ts );
1678 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1680 msg_Dbg( p_mux, "adjusting rate at "I64Fd"/"I64Fd" (%d/%d)",
1681 i_cut_dts - i_pcr_dts, i_pcr_length, new_chain.i_depth,
1682 p_chain_ts->i_depth );
1683 if ( new_chain.i_depth )
1684 TSDate( p_mux, &new_chain,
1685 i_cut_dts - i_pcr_dts,
1687 if ( p_chain_ts->i_depth )
1689 p_chain_ts, i_pcr_dts + i_pcr_length - i_cut_dts,
1695 if ( new_chain.i_depth )
1696 TSDate( p_mux, &new_chain, i_pcr_length, i_pcr_dts );
1699 static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1700 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1702 sout_mux_sys_t *p_sys = p_mux->p_sys;
1703 int i_packet_count = p_chain_ts->i_depth;
1706 if ( i_pcr_length / 1000 > 0 )
1708 int i_bitrate = ((uint64_t)i_packet_count * 188 * 8000)
1709 / (uint64_t)(i_pcr_length / 1000);
1710 if ( p_sys->i_bitrate_max && p_sys->i_bitrate_max < i_bitrate )
1712 msg_Warn( p_mux, "max bitrate exceeded at "I64Fd
1713 " (%d bi/s for %d pkt in "I64Fd" us)",
1714 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1715 i_bitrate, i_packet_count, i_pcr_length);
1720 msg_Dbg( p_mux, "starting at "I64Fd
1721 " (%d bi/s for %d packets in "I64Fd" us)",
1722 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1723 i_bitrate, i_packet_count, i_pcr_length);
1729 /* This shouldn't happen, but happens in some rare heavy load
1730 * and packet losses conditions. */
1731 i_pcr_length = i_packet_count;
1734 /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
1735 for( i = 0; i < i_packet_count; i++ )
1737 block_t *p_ts = BufferChainGet( p_chain_ts );
1738 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1740 p_ts->i_dts = i_new_dts;
1741 p_ts->i_length = i_pcr_length / i_packet_count;
1743 if( p_ts->i_flags & BLOCK_FLAG_CLOCK )
1745 /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
1746 TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay );
1748 if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
1750 csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size, 0 );
1754 p_ts->i_dts += p_sys->i_shaping_delay * 3 / 2;
1756 sout_AccessOutWrite( p_mux->p_access, p_ts );
1760 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream,
1763 block_t *p_pes = p_stream->chain_pes.p_first;
1766 vlc_bool_t b_new_pes = VLC_FALSE;
1767 vlc_bool_t b_adaptation_field = VLC_FALSE;
1769 int i_payload_max = 184 - ( b_pcr ? 8 : 0 );
1772 if( p_stream->i_pes_used <= 0 )
1774 b_new_pes = VLC_TRUE;
1776 i_payload = __MIN( (int)p_pes->i_buffer - p_stream->i_pes_used,
1779 if( b_pcr || i_payload < i_payload_max )
1781 b_adaptation_field = VLC_TRUE;
1784 p_ts = block_New( p_mux, 188 );
1785 p_ts->i_dts = p_pes->i_dts;
1787 p_ts->p_buffer[0] = 0x47;
1788 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 ) |
1789 ( ( p_stream->i_pid >> 8 )&0x1f );
1790 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1791 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 ) |
1792 p_stream->i_continuity_counter;
1794 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1795 p_stream->b_discontinuity = (p_pes->i_flags & BLOCK_FLAG_DISCONTINUITY);
1797 if( b_adaptation_field )
1803 int i_stuffing = i_payload_max - i_payload;
1805 p_ts->i_flags |= BLOCK_FLAG_CLOCK;
1807 p_ts->p_buffer[4] = 7 + i_stuffing;
1808 p_ts->p_buffer[5] = 0x10; /* flags */
1809 if( p_stream->b_discontinuity )
1811 p_ts->p_buffer[5] |= 0x80; /* flag TS dicontinuity */
1812 p_stream->b_discontinuity = VLC_FALSE;
1814 p_ts->p_buffer[6] = ( 0 )&0xff;
1815 p_ts->p_buffer[7] = ( 0 )&0xff;
1816 p_ts->p_buffer[8] = ( 0 )&0xff;
1817 p_ts->p_buffer[9] = ( 0 )&0xff;
1818 p_ts->p_buffer[10]= ( ( 0 )&0x80 ) | 0x7e;
1819 p_ts->p_buffer[11]= 0;
1821 for( i = 12; i < 12 + i_stuffing; i++ )
1823 p_ts->p_buffer[i] = 0xff;
1828 int i_stuffing = i_payload_max - i_payload;
1830 p_ts->p_buffer[4] = i_stuffing - 1;
1831 if( i_stuffing > 1 )
1833 p_ts->p_buffer[5] = 0x00;
1834 for( i = 6; i < 6 + i_stuffing - 2; i++ )
1836 p_ts->p_buffer[i] = 0xff;
1843 memcpy( &p_ts->p_buffer[188 - i_payload],
1844 &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
1846 p_stream->i_pes_used += i_payload;
1847 p_stream->i_pes_dts = p_pes->i_dts + p_pes->i_length *
1848 p_stream->i_pes_used / p_pes->i_buffer;
1849 p_stream->i_pes_length -= p_pes->i_length * i_payload / p_pes->i_buffer;
1851 if( p_stream->i_pes_used >= (int)p_pes->i_buffer )
1853 p_pes = BufferChainGet( &p_stream->chain_pes );
1854 block_Release( p_pes );
1856 p_pes = p_stream->chain_pes.p_first;
1859 p_stream->i_pes_dts = p_pes->i_dts;
1860 p_stream->i_pes_length = 0;
1863 p_stream->i_pes_length += p_pes->i_length;
1865 p_pes = p_pes->p_next;
1870 p_stream->i_pes_dts = 0;
1871 p_stream->i_pes_length = 0;
1873 p_stream->i_pes_used = 0;
1879 static void TSSetPCR( block_t *p_ts, mtime_t i_dts )
1881 mtime_t i_pcr = 9 * i_dts / 100;
1883 p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
1884 p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
1885 p_ts->p_buffer[8] = ( i_pcr >> 9 )&0xff;
1886 p_ts->p_buffer[9] = ( i_pcr >> 1 )&0xff;
1887 p_ts->p_buffer[10]|= ( i_pcr << 7 )&0x80;
1891 static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c,
1892 mtime_t i_length, int i_bitrate_min,
1895 sout_mux_sys_t *p_sys = p_mux->p_sys;
1896 sout_buffer_chain_t s = *c;
1899 int i_packets_min = 0;
1900 int i_packets_max = 0;
1907 i_packets = c->i_depth;
1908 i_packets_min = ( (int64_t)i_bitrate_min * i_length / 8 / 1000000 + 187 ) / 188;
1909 i_packets_max = ( (int64_t)i_bitrate_max * i_length / 8 / 1000000 + 187 ) / 188;
1911 if( i_packets < i_packets_min && i_packets_min > 0 )
1914 int i_div = ( i_packets_min - i_packets ) / i_packets;
1915 int i_mod = ( i_packets_min - i_packets ) % i_packets;
1918 /* We need to pad with null packets (pid=0x1fff)
1919 * We try to melt null packets with true packets */
1921 "packets=%d but min=%d -> adding %d packets of padding",
1922 i_packets, i_packets_min, i_packets_min - i_packets );
1924 BufferChainInit( c );
1925 while( ( p_pk = BufferChainGet( &s ) ) )
1929 BufferChainAppend( c, p_pk );
1931 i_null = i_div + ( i_rest + i_mod ) / i_packets;
1933 for( i = 0; i < i_null; i++ )
1937 p_null = sout_BufferNew( p_mux->p_sout, 188 );
1938 p_null->p_buffer[0] = 0x47;
1939 p_null->p_buffer[1] = 0x1f;
1940 p_null->p_buffer[2] = 0xff;
1941 p_null->p_buffer[3] = 0x10 | p_sys->i_null_continuity_counter;
1942 memset( &p_null->p_buffer[4], 0, 184 );
1943 p_sys->i_null_continuity_counter =
1944 ( p_sys->i_null_continuity_counter + 1 ) % 16;
1946 BufferChainAppend( c, p_null );
1949 i_rest = ( i_rest + i_mod ) % i_packets;
1952 else if( i_packets > i_packets_max && i_packets_max > 0 )
1957 /* Arg, we need to drop packets, I don't do something clever (like
1958 * dropping complete pid, b frames, ... ), I just get the right amount
1959 * of packets and discard the others */
1961 "packets=%d but max=%d -> removing %d packets -> stream broken",
1962 i_packets, i_packets_max, i_packets - i_packets_max );
1964 BufferChainInit( c );
1965 for( i = 0; i < i_packets_max; i++ )
1967 BufferChainAppend( c, BufferChainGet( &s ) );
1970 while( ( p_pk = BufferChainGet( &s ) ) )
1972 sout_BufferDelete( p_mux->p_sout, p_pk );
1978 static void PEStoTS( sout_instance_t *p_sout,
1979 sout_buffer_chain_t *c, block_t *p_pes,
1980 ts_stream_t *p_stream )
1986 /* get PES total size */
1987 i_size = p_pes->i_buffer;
1988 p_data = p_pes->p_buffer;
1990 b_new_pes = VLC_TRUE;
1994 int b_adaptation_field;
1998 p_ts = block_New( p_sout, 188 );
2001 * 1b transport_error_indicator
2002 * 1b payload_unit_start
2003 * 1b transport_priority
2005 * 2b transport_scrambling_control
2006 * 2b if adaptation_field 0x03 else 0x01
2007 * 4b continuity_counter
2010 i_copy = __MIN( i_size, 184 );
2011 b_adaptation_field = i_size < 184 ? VLC_TRUE : VLC_FALSE;
2013 p_ts->p_buffer[0] = 0x47;
2014 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
2015 ( ( p_stream->i_pid >> 8 )&0x1f );
2016 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
2017 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
2018 p_stream->i_continuity_counter;
2020 b_new_pes = VLC_FALSE;
2021 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
2023 if( b_adaptation_field )
2025 int i_stuffing = 184 - i_copy;
2028 p_ts->p_buffer[4] = i_stuffing - 1;
2029 if( i_stuffing > 1 )
2031 p_ts->p_buffer[5] = 0x00;
2032 if( p_stream->b_discontinuity )
2034 p_ts->p_buffer[5] |= 0x80;
2035 p_stream->b_discontinuity = VLC_FALSE;
2037 for( i = 6; i < 6 + i_stuffing - 2; i++ )
2039 p_ts->p_buffer[i] = 0xff;
2044 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
2048 BufferChainAppend( c, p_ts );
2052 block_t *p_next = p_pes->p_next;
2054 p_pes->p_next = NULL;
2055 block_Release( p_pes );
2056 if( p_next == NULL )
2060 b_new_pes = VLC_TRUE;
2062 i_size = p_pes->i_buffer;
2063 p_data = p_pes->p_buffer;
2070 static block_t *WritePSISection( sout_instance_t *p_sout,
2071 dvbpsi_psi_section_t* p_section )
2073 block_t *p_psi, *p_first = NULL;
2079 i_size = (uint32_t)( p_section->p_payload_end - p_section->p_data )+
2080 ( p_section->b_syntax_indicator ? 4 : 0 );
2082 p_psi = block_New( p_sout, i_size + 1 );
2085 p_psi->i_length = 0;
2086 p_psi->i_buffer = i_size + 1;
2088 p_psi->p_buffer[0] = 0; /* pointer */
2089 memcpy( p_psi->p_buffer + 1,
2093 block_ChainAppend( &p_first, p_psi );
2095 p_section = p_section->p_next;
2101 static void GetPAT( sout_mux_t *p_mux,
2102 sout_buffer_chain_t *c )
2104 sout_mux_sys_t *p_sys = p_mux->p_sys;
2107 dvbpsi_psi_section_t *p_section;
2110 dvbpsi_InitPAT( &pat, p_sys->i_tsid, p_sys->i_pat_version_number,
2111 1 ); /* b_current_next */
2112 /* add all programs */
2113 for ( i = 0; i < p_sys->i_num_pmt; i++ )
2114 dvbpsi_PATAddProgram( &pat,
2115 p_sys->i_pmt_program_number[i],
2116 p_sys->pmt[i].i_pid );
2118 p_section = dvbpsi_GenPATSections( &pat,
2119 0 ); /* max program per section */
2121 p_pat = WritePSISection( p_mux->p_sout, p_section );
2123 PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
2125 dvbpsi_DeletePSISections( p_section );
2126 dvbpsi_EmptyPAT( &pat );
2129 static uint32_t GetDescriptorLength24b( int i_length )
2131 uint32_t i_l1, i_l2, i_l3;
2133 i_l1 = i_length&0x7f;
2134 i_l2 = ( i_length >> 7 )&0x7f;
2135 i_l3 = ( i_length >> 14 )&0x7f;
2137 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
2140 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c )
2142 sout_mux_sys_t *p_sys = p_mux->p_sys;
2143 block_t *p_pmt[MAX_PMT];
2147 dvbpsi_pmt_es_t *p_es;
2148 dvbpsi_psi_section_t *p_section[MAX_PMT], *p_section2;
2149 dvbpsi_sdt_service_t *p_service;
2155 int *p_usepid = NULL;
2157 if( p_sys->dvbpmt == NULL )
2158 p_sys->dvbpmt = malloc( p_sys->i_num_pmt * sizeof(dvbpsi_pmt_t) );
2159 #ifdef HAVE_DVBPSI_SDT
2161 dvbpsi_InitSDT( &sdt, p_sys->i_tsid, 1, 1, p_sys->i_netid );
2164 for( i = 0; i < p_sys->i_num_pmt; i++ )
2166 dvbpsi_InitPMT( &p_sys->dvbpmt[i],
2167 p_sys->i_pmt_program_number[i], /* program number */
2168 p_sys->i_pmt_version_number,
2169 1, /* b_current_next */
2172 #ifdef HAVE_DVBPSI_SDT
2175 p_service = dvbpsi_SDTAddService( &sdt,
2176 p_sys->i_pmt_program_number[i], /* service id */
2177 0, /* eit schedule */
2178 0, /* eit present */
2179 4, /* running status ("4=RUNNING") */
2182 #define psz_sdtprov p_sys->sdt_descriptors[i].psz_provider
2183 #define psz_sdtserv p_sys->sdt_descriptors[i].psz_service_name
2185 /* FIXME: Ineffecient malloc's & ugly code...... */
2186 if( psz_sdtprov != NULL && psz_sdtserv != NULL )
2188 psz_sdt_desc = malloc( 3 + strlen(psz_sdtprov)
2189 + strlen(psz_sdtserv) );
2190 psz_sdt_desc[0] = 0x01; /* digital television service */
2192 /* service provider name length */
2193 psz_sdt_desc[1] = (char)strlen(psz_sdtprov);
2194 memcpy( &psz_sdt_desc[2], psz_sdtprov, strlen(psz_sdtprov) );
2196 /* service name length */
2197 psz_sdt_desc[ 2 + strlen(psz_sdtprov) ]
2198 = (char)strlen(psz_sdtserv);
2199 memcpy( &psz_sdt_desc[3+strlen(psz_sdtprov)], psz_sdtserv,
2200 strlen(psz_sdtserv) );
2202 dvbpsi_SDTServiceAddDescriptor( p_service, 0x48,
2203 3 + strlen(psz_sdtprov) + strlen(psz_sdtserv),
2205 free( psz_sdt_desc );
2213 if( p_sys->i_mpeg4_streams > 0 )
2217 bits_buffer_t bits_fix_IOD;
2219 /* Make valgrind happy : it works at byte level not bit one so
2220 * bit_write confuse it (but DON'T CHANGE the way that bit_write is
2221 * working (needed when fixing some bits) */
2222 memset( iod, 0, 4096 );
2224 bits_initwrite( &bits, 4096, iod );
2225 /* IOD_label_scope */
2226 bits_write( &bits, 8, 0x11 );
2228 bits_write( &bits, 8, 0x01 );
2229 /* InitialObjectDescriptor */
2230 bits_align( &bits );
2231 bits_write( &bits, 8, 0x02 ); /* tag */
2232 bits_fix_IOD = bits; /* save states to fix length later */
2233 bits_write( &bits, 24,
2234 GetDescriptorLength24b( 0 ) ); /* variable length (fixed later) */
2235 bits_write( &bits, 10, 0x01 ); /* ObjectDescriptorID */
2236 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2237 bits_write( &bits, 1, 0x00 ); /* includeInlineProfileLevelFlag */
2238 bits_write( &bits, 4, 0x0f ); /* reserved */
2239 bits_write( &bits, 8, 0xff ); /* ODProfile (no ODcapability ) */
2240 bits_write( &bits, 8, 0xff ); /* sceneProfile */
2241 bits_write( &bits, 8, 0xfe ); /* audioProfile (unspecified) */
2242 bits_write( &bits, 8, 0xfe ); /* visualProfile( // ) */
2243 bits_write( &bits, 8, 0xff ); /* graphicProfile (no ) */
2244 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2246 ts_stream_t *p_stream;
2247 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
2249 if( p_stream->i_stream_id == 0xfa ||
2250 p_stream->i_stream_id == 0xfb ||
2251 p_stream->i_stream_id == 0xfe )
2253 bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
2255 bits_align( &bits );
2256 bits_write( &bits, 8, 0x03 ); /* ES_DescrTag */
2257 bits_fix_ESDescr = bits;
2258 bits_write( &bits, 24,
2259 GetDescriptorLength24b( 0 ) ); /* variable size */
2260 bits_write( &bits, 16, p_stream->i_es_id );
2261 bits_write( &bits, 1, 0x00 ); /* streamDependency */
2262 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2263 bits_write( &bits, 1, 0x00 ); /* OCRStreamFlag */
2264 bits_write( &bits, 5, 0x1f ); /* streamPriority */
2266 /* DecoderConfigDesciptor */
2267 bits_align( &bits );
2268 bits_write( &bits, 8, 0x04 ); /* DecoderConfigDescrTag */
2269 bits_fix_Decoder = bits;
2270 bits_write( &bits, 24, GetDescriptorLength24b( 0 ) );
2271 if( p_stream->i_stream_type == 0x10 )
2273 bits_write( &bits, 8, 0x20 ); /* Visual 14496-2 */
2274 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2276 else if( p_stream->i_stream_type == 0x1b )
2278 bits_write( &bits, 8, 0x21 ); /* Visual 14496-2 */
2279 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2281 else if( p_stream->i_stream_type == 0x11 || p_stream->i_stream_type == 0x0f )
2283 bits_write( &bits, 8, 0x40 ); /* Audio 14496-3 */
2284 bits_write( &bits, 6, 0x05 ); /* AudioStream */
2286 else if( p_stream->i_stream_type == 0x12 &&
2287 p_stream->i_codec == VLC_FOURCC('s','u','b','t') )
2289 bits_write( &bits, 8, 0x0B ); /* Text Stream */
2290 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2294 bits_write( &bits, 8, 0x00 );
2295 bits_write( &bits, 6, 0x00 );
2297 msg_Err( p_mux->p_sout,"Unsupported stream_type => "
2300 bits_write( &bits, 1, 0x00 ); /* UpStream */
2301 bits_write( &bits, 1, 0x01 ); /* reserved */
2302 bits_write( &bits, 24, 1024 * 1024 ); /* bufferSizeDB */
2303 bits_write( &bits, 32, 0x7fffffff ); /* maxBitrate */
2304 bits_write( &bits, 32, 0 ); /* avgBitrate */
2306 if( p_stream->i_decoder_specific_info > 0 )
2309 /* DecoderSpecificInfo */
2310 bits_align( &bits );
2311 bits_write( &bits, 8, 0x05 ); /* tag */
2312 bits_write( &bits, 24, GetDescriptorLength24b(
2313 p_stream->i_decoder_specific_info ) );
2314 for( i = 0; i < p_stream->i_decoder_specific_info; i++ )
2316 bits_write( &bits, 8,
2317 ((uint8_t*)p_stream->p_decoder_specific_info)[i] );
2320 /* fix Decoder length */
2321 bits_write( &bits_fix_Decoder, 24,
2322 GetDescriptorLength24b( bits.i_data -
2323 bits_fix_Decoder.i_data - 3 ) );
2325 /* SLConfigDescriptor : predefined (0x01) */
2326 bits_align( &bits );
2327 bits_write( &bits, 8, 0x06 ); /* tag */
2328 bits_write( &bits, 24, GetDescriptorLength24b( 8 ) );
2329 bits_write( &bits, 8, 0x01 );/* predefined */
2330 bits_write( &bits, 1, 0 ); /* durationFlag */
2331 bits_write( &bits, 32, 0 ); /* OCRResolution */
2332 bits_write( &bits, 8, 0 ); /* OCRLength */
2333 bits_write( &bits, 8, 0 ); /* InstantBitrateLength */
2334 bits_align( &bits );
2336 /* fix ESDescr length */
2337 bits_write( &bits_fix_ESDescr, 24,
2338 GetDescriptorLength24b( bits.i_data -
2339 bits_fix_ESDescr.i_data - 3 ) );
2342 bits_align( &bits );
2343 /* fix IOD length */
2344 bits_write( &bits_fix_IOD, 24,
2345 GetDescriptorLength24b( bits.i_data -
2346 bits_fix_IOD.i_data - 3 ) );
2348 #if 0//def HAVE_BSEARCH /* FIXME!!! This can't possibly work */
2349 i_pidinput = p_mux->pp_inputs[i]->p_fmt->i_id;
2350 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2351 sizeof(pmt_map_t), intcompare );
2352 p_usepid = bsearch( &p_usepid, p_sys->pmtmap, p_sys->i_num_pmt,
2353 sizeof(pmt_map_t), pmtcompare );
2354 if( p_usepid != NULL )
2355 dvbpsi_PMTAddDescriptor(
2356 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog], 0x1d,
2357 bits.i_data, bits.p_data );
2359 msg_Err( p_mux, "Received an unmapped PID" );
2361 dvbpsi_PMTAddDescriptor( &p_sys->dvbpmt[0], 0x1d, bits.i_data,
2366 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2368 ts_stream_t *p_stream;
2370 p_stream = (ts_stream_t *)p_mux->pp_inputs[i_stream]->p_sys;
2373 i_pidinput = p_mux->pp_inputs[i_stream]->p_fmt->i_id;
2374 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2375 sizeof(pmt_map_t), intcompare );
2377 if( p_usepid != NULL )
2378 p_es = dvbpsi_PMTAddES(
2379 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog],
2380 p_stream->i_stream_type, p_stream->i_pid );
2382 /* If there's an error somewhere, dump it to the first pmt */
2384 p_es = dvbpsi_PMTAddES( &p_sys->dvbpmt[0], p_stream->i_stream_type,
2387 if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
2392 es_id[0] = (p_stream->i_es_id >> 8)&0xff;
2393 es_id[1] = (p_stream->i_es_id)&0xff;
2394 dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
2396 else if( p_stream->i_stream_type == 0xa0 )
2399 int i_extra = __MIN( p_stream->i_decoder_specific_info, 502 );
2401 /* private DIV3 descripor */
2402 memcpy( &data[0], &p_stream->i_bih_codec, 4 );
2403 data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
2404 data[5] = ( p_stream->i_bih_width )&0xff;
2405 data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
2406 data[7] = ( p_stream->i_bih_height )&0xff;
2407 data[8] = ( i_extra >> 8 )&0xff;
2408 data[9] = ( i_extra )&0xff;
2411 memcpy( &data[10], p_stream->p_decoder_specific_info, i_extra );
2414 /* 0xa0 is private */
2415 dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra + 10, data );
2417 else if( p_stream->i_stream_type == 0x81 )
2419 uint8_t format[4] = { 0x41, 0x43, 0x2d, 0x33 };
2421 /* "registration" descriptor : "AC-3" */
2422 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
2424 else if( p_stream->i_codec == VLC_FOURCC('d','t','s',' ') )
2426 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2428 /* DTS format identifier, frame size 1024 - FIXME */
2429 uint8_t data[4] = { 0x44, 0x54, 0x53, 0x32 };
2430 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2432 else if( p_stream->i_codec == VLC_FOURCC('t','e','l','x') )
2434 dvbpsi_PMTESAddDescriptor( p_es, 0x56,
2435 p_stream->i_decoder_specific_info,
2436 p_stream->p_decoder_specific_info );
2438 #ifdef _DVBPSI_DR_59_H_
2439 else if( p_stream->i_codec == VLC_FOURCC('d','v','b','s') )
2442 dvbpsi_subtitling_dr_t descr;
2443 dvbpsi_subtitle_t sub;
2444 dvbpsi_descriptor_t *p_descr;
2446 memcpy( sub.i_iso6392_language_code, p_stream->lang, 3 );
2447 sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
2448 sub.i_composition_page_id = p_stream->i_es_id & 0xFF;
2449 sub.i_ancillary_page_id = p_stream->i_es_id >> 16;
2451 descr.i_subtitles_number = 1;
2452 descr.p_subtitle[0] = sub;
2454 p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
2455 /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
2456 dvbpsi_PMTESAddDescriptor( p_es, p_descr->i_tag,
2457 p_descr->i_length, p_descr->p_data );
2460 #endif /* _DVBPSI_DR_59_H_ */
2462 if( p_stream->lang[0] != 0 )
2466 /* I construct the content myself, way faster than looking at
2467 * over complicated/mind broken libdvbpsi way */
2468 data[0] = p_stream->lang[0];
2469 data[1] = p_stream->lang[1];
2470 data[2] = p_stream->lang[2];
2471 data[3] = 0x00; /* audio type: 0x00 undefined */
2473 dvbpsi_PMTESAddDescriptor( p_es, 0x0a, 4, data );
2477 for( i = 0; i < p_sys->i_num_pmt; i++ )
2479 p_section[i] = dvbpsi_GenPMTSections( &p_sys->dvbpmt[i] );
2480 p_pmt[i] = WritePSISection( p_mux->p_sout, p_section[i] );
2481 PEStoTS( p_mux->p_sout, c, p_pmt[i], &p_sys->pmt[i] );
2482 dvbpsi_DeletePSISections( p_section[i] );
2483 dvbpsi_EmptyPMT( &p_sys->dvbpmt[i] );
2486 #ifdef HAVE_DVBPSI_SDT
2489 p_section2 = dvbpsi_GenSDTSections( &sdt );
2490 p_sdt = WritePSISection( p_mux->p_sout, p_section2 );
2491 PEStoTS( p_mux->p_sout, c, p_sdt, &p_sys->sdt );
2492 dvbpsi_DeletePSISections( p_section2 );
2493 dvbpsi_EmptySDT( &sdt );