1 /*****************************************************************************
2 * ts.c: MPEG-II TS Muxer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
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 *****************************************************************************/
37 #include <vlc_codecs.h>
38 #include <vlc_block.h>
46 #ifdef HAVE_DVBPSI_DR_H
47 # include <dvbpsi/dvbpsi.h>
48 # include <dvbpsi/demux.h>
49 # include <dvbpsi/descriptor.h>
50 # include <dvbpsi/pat.h>
51 # include <dvbpsi/pmt.h>
52 # include <dvbpsi/sdt.h>
53 # include <dvbpsi/dr.h>
54 # include <dvbpsi/psi.h>
58 # include "descriptor.h"
59 # include "tables/pat.h"
60 # include "tables/pmt.h"
61 # include "tables/sdt.h"
62 # include "descriptors/dr.h"
68 * - check PCR frequency requirement
70 * - check PCR/PCR "soft"
71 * - check if "registration" descriptor : "AC-3" should be a program
72 * descriptor or an es one. (xine want an es one)
74 * - remove creation of PAT/PMT without dvbpsi
77 * - subtitle support is far from perfect. I expect some subtitles drop
78 * if they arrive a bit late
79 * (We cannot rely on the fact that the fifo should be full)
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close ( vlc_object_t * );
87 #define VPID_TEXT N_("Video PID")
88 #define VPID_LONGTEXT N_("Assign a fixed PID to the video stream. The PCR " \
89 "PID will automatically be the video.")
90 #define APID_TEXT N_("Audio PID")
91 #define APID_LONGTEXT N_("Assign a fixed PID to the audio stream.")
92 #define SPUPID_TEXT N_("SPU PID")
93 #define SPUPID_LONGTEXT N_("Assign a fixed PID to the SPU.")
94 #define PMTPID_TEXT N_("PMT PID")
95 #define PMTPID_LONGTEXT N_("Assign a fixed PID to the PMT")
96 #define TSID_TEXT N_("TS ID")
97 #define TSID_LONGTEXT N_("Assign a fixed Transport Stream ID.")
98 #define NETID_TEXT N_("NET ID")
99 #define NETID_LONGTEXT N_("Assign a fixed Network ID (for SDT table)")
101 #define PMTPROG_TEXT N_("PMT Program numbers")
102 #define PMTPROG_LONGTEXT N_("Assign a program number to each PMT. This " \
103 "requires \"Set PID to ID of ES\" to be enabled." )
105 #define MUXPMT_TEXT N_("Mux PMT (requires --sout-ts-es-id-pid)")
106 #define MUXPMT_LONGTEXT N_("Define the pids to add to each pmt. This " \
107 "requires \"Set PID to ID of ES\" to be enabled." )
109 #define SDTDESC_TEXT N_("SDT Descriptors (requires --sout-ts-es-id-pid)")
110 #define SDTDESC_LONGTEXT N_("Defines the descriptors of each SDT. This" \
111 "requires \"Set PID to ID of ES\" to be enabled." )
113 #define PID_TEXT N_("Set PID to ID of ES")
114 #define PID_LONGTEXT N_("Sets PID to the ID if the incoming ES. This is for " \
115 "use with --ts-es-id-pid, and allows to have the same PIDs in the input " \
116 "and output streams.")
118 #define ALIGNMENT_TEXT N_("Data alignment")
119 #define ALIGNMENT_LONGTEXT N_("Enforces alignment of all access units on " \
120 "PES boundaries. Disabling this might save some bandwidth but introduce incompatibilities.")
122 #define SHAPING_TEXT N_("Shaping delay (ms)")
123 #define SHAPING_LONGTEXT N_("Cut the " \
124 "stream in slices of the given duration, and ensure a constant bitrate " \
125 "between the two boundaries. This avoids having huge bitrate peaks, " \
126 "especially for reference frames." )
128 #define KEYF_TEXT N_("Use keyframes")
129 #define KEYF_LONGTEXT N_("If enabled, and shaping is specified, " \
130 "the TS muxer will place the boundaries at the end of I pictures. In " \
131 "that case, the shaping duration given by the user is a worse case " \
132 "used when no reference frame is available. This enhances the efficiency " \
133 "of the shaping algorithm, since I frames are usually the biggest " \
134 "frames in the stream.")
136 #define PCR_TEXT N_("PCR delay (ms)")
137 #define PCR_LONGTEXT N_("Set at which interval " \
138 "PCRs (Program Clock Reference) will be sent (in milliseconds). " \
139 "This value should be below 100ms. (default is 70ms).")
141 #define BMIN_TEXT N_( "Minimum B (deprecated)")
142 #define BMIN_LONGTEXT N_( "This setting is deprecated and not used anymore" )
144 #define BMAX_TEXT N_( "Maximum B (deprecated)")
145 #define BMAX_LONGTEXT N_( "This setting is deprecated and not used anymore")
147 #define DTS_TEXT N_("DTS delay (ms)")
148 #define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
149 "stamps) and PTS (presentation timestamps) of the data in the " \
150 "stream, compared to the PCRs. This allows for some buffering inside " \
151 "the client decoder.")
153 #define ACRYPT_TEXT N_("Crypt audio")
154 #define ACRYPT_LONGTEXT N_("Crypt audio using CSA")
155 #define VCRYPT_TEXT N_("Crypt video")
156 #define VCRYPT_LONGTEXT N_("Crypt video using CSA")
158 #define CK_TEXT N_("CSA Key")
159 #define CK_LONGTEXT N_("CSA encryption key. This must be a " \
160 "16 char string (8 hexadecimal bytes).")
162 #define CPKT_TEXT N_("Packet size in bytes to encrypt")
163 #define CPKT_LONGTEXT N_("Size of the TS packet to encrypt. " \
164 "The encryption routines subtract the TS-header from the value before " \
167 #define SOUT_CFG_PREFIX "sout-ts-"
169 # define MAX_PMT 64 /* Maximum number of programs. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
173 #define MAX_PMT_PID 64 /* Maximum pids in each pmt. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
176 set_description( _("TS muxer (libdvbpsi)") );
177 set_shortname( "MPEG-TS");
178 set_category( CAT_SOUT );
179 set_subcategory( SUBCAT_SOUT_MUX );
180 set_capability( "sout mux", 120 );
181 add_shortcut( "ts" );
183 add_integer( SOUT_CFG_PREFIX "pid-video", 0, NULL,VPID_TEXT, VPID_LONGTEXT,
185 add_integer( SOUT_CFG_PREFIX "pid-audio", 0, NULL, APID_TEXT,
186 APID_LONGTEXT, VLC_TRUE );
187 add_integer( SOUT_CFG_PREFIX "pid-spu", 0, NULL, SPUPID_TEXT,
188 SPUPID_LONGTEXT, VLC_TRUE );
189 add_integer( SOUT_CFG_PREFIX "pid-pmt", 0, NULL, PMTPID_TEXT,
190 PMTPID_LONGTEXT, VLC_TRUE );
191 add_integer( SOUT_CFG_PREFIX "tsid", 0, NULL, TSID_TEXT,
192 TSID_LONGTEXT, VLC_TRUE );
193 #ifdef HAVE_DVBPSI_SDT
194 add_integer( SOUT_CFG_PREFIX "netid", 0, NULL, NETID_TEXT,
195 NETID_LONGTEXT, VLC_TRUE );
197 add_string( SOUT_CFG_PREFIX "program-pmt", NULL, NULL, PMTPROG_TEXT,
198 PMTPROG_LONGTEXT, VLC_TRUE );
199 add_bool( SOUT_CFG_PREFIX "es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT,
201 add_string( SOUT_CFG_PREFIX "muxpmt", NULL, NULL, MUXPMT_TEXT, MUXPMT_LONGTEXT, VLC_TRUE );
202 #ifdef HAVE_DVBPSI_SDT
203 add_string( SOUT_CFG_PREFIX "sdtdesc", NULL, NULL, SDTDESC_TEXT, SDTDESC_LONGTEXT, VLC_TRUE );
205 add_bool( SOUT_CFG_PREFIX "alignment", VLC_TRUE, NULL, ALIGNMENT_TEXT,
206 ALIGNMENT_LONGTEXT, VLC_TRUE );
208 add_integer( SOUT_CFG_PREFIX "shaping", 200, NULL,SHAPING_TEXT,
209 SHAPING_LONGTEXT, VLC_TRUE );
210 add_bool( SOUT_CFG_PREFIX "use-key-frames", VLC_FALSE, NULL, KEYF_TEXT,
211 KEYF_LONGTEXT, VLC_TRUE );
213 add_integer( SOUT_CFG_PREFIX "pcr", 70, NULL, PCR_TEXT, PCR_LONGTEXT,
215 add_integer( SOUT_CFG_PREFIX "bmin", 0, NULL, BMIN_TEXT, BMIN_LONGTEXT,
217 add_integer( SOUT_CFG_PREFIX "bmax", 0, NULL, BMAX_TEXT, BMAX_LONGTEXT,
219 add_integer( SOUT_CFG_PREFIX "dts-delay", 400, NULL, DTS_TEXT,
220 DTS_LONGTEXT, VLC_TRUE );
222 add_bool( SOUT_CFG_PREFIX "crypt-audio", VLC_TRUE, NULL, ACRYPT_TEXT,
223 ACRYPT_LONGTEXT, VLC_TRUE );
224 add_bool( SOUT_CFG_PREFIX "crypt-video", VLC_TRUE, NULL, VCRYPT_TEXT,
225 VCRYPT_LONGTEXT, VLC_TRUE );
227 add_string( SOUT_CFG_PREFIX "csa-ck", NULL, NULL, CK_TEXT, CK_LONGTEXT,
229 add_integer( SOUT_CFG_PREFIX "csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );
231 set_callbacks( Open, Close );
234 /*****************************************************************************
235 * Local data structures
236 *****************************************************************************/
237 static const char *ppsz_sout_options[] = {
238 "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid", "netid",
239 "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
240 "dts-delay", "csa-ck", "csa-pkt", "crypt-audio", "crypt-video",
241 "muxpmt", "sdtdesc", "program-pmt", "alignment",
245 typedef struct pmt_map_t /* Holds the mapping between the pmt-pid/pmt table */
248 unsigned long i_prog;
251 typedef struct sdt_desc_t
254 char *psz_service_name; /* name of program */
262 } sout_buffer_chain_t;
264 static inline void BufferChainInit ( sout_buffer_chain_t *c )
268 c->pp_last = &c->p_first;
271 static inline void BufferChainAppend( sout_buffer_chain_t *c, block_t *b )
281 c->pp_last = &b->p_next;
284 static inline block_t *BufferChainGet( sout_buffer_chain_t *c )
286 block_t *b = c->p_first;
291 c->p_first = b->p_next;
293 if( c->p_first == NULL )
295 c->pp_last = &c->p_first;
303 static inline block_t *BufferChainPeek( sout_buffer_chain_t *c )
305 block_t *b = c->p_first;
310 static inline void BufferChainClean( sout_buffer_chain_t *c )
314 while( ( b = BufferChainGet( c ) ) )
318 BufferChainInit( c );
321 typedef struct ts_stream_t
324 vlc_fourcc_t i_codec;
328 int i_continuity_counter;
329 vlc_bool_t b_discontinuity;
331 /* to be used for carriege of DIV3 */
332 vlc_fourcc_t i_bih_codec;
333 int i_bih_width, i_bih_height;
335 /* Specific to mpeg4 in mpeg2ts */
338 int i_decoder_specific_info;
339 uint8_t *p_decoder_specific_info;
341 /* language is iso639-2T */
345 sout_buffer_chain_t chain_pes;
347 mtime_t i_pes_length;
349 vlc_bool_t b_key_frame;
353 struct sout_mux_sys_t
356 sout_input_t *p_pcr_input;
361 vlc_bool_t b_es_id_pid;
366 int i_pid_free; /* first usable pid */
372 int i_pat_version_number;
375 int i_pmt_version_number;
376 ts_stream_t pmt[MAX_PMT];
377 pmt_map_t pmtmap[MAX_PMT_PID];
378 int i_pmt_program_number[MAX_PMT];
379 sdt_desc_t sdt_descriptors[MAX_PMT];
380 vlc_bool_t b_data_alignment;
384 int i_null_continuity_counter; /* Needed ? */
386 dvbpsi_pmt_t *dvbpmt;
388 /* for TS building */
389 int64_t i_bitrate_min;
390 int64_t i_bitrate_max;
392 int64_t i_shaping_delay;
397 vlc_bool_t b_use_key_frames;
399 mtime_t i_pcr; /* last PCR emited */
403 vlc_bool_t b_crypt_audio;
404 vlc_bool_t b_crypt_video;
407 /* Reserve a pid and return it */
408 static int AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
411 if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
413 i_pid = p_sys->i_pid_video;
414 p_sys->i_pid_video = 0;
416 else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
418 i_pid = p_sys->i_pid_audio;
419 p_sys->i_pid_audio = 0;
421 else if ( i_cat == SPU_ES && p_sys->i_pid_spu )
423 i_pid = p_sys->i_pid_spu;
424 p_sys->i_pid_spu = 0;
428 i_pid = ++p_sys->i_pid_free;
433 static int pmtcompare( const void *pa, const void *pb )
435 if ( ((pmt_map_t *)pa)->i_pid < ((pmt_map_t *)pb)->i_pid )
437 else if ( ((pmt_map_t *)pa)->i_pid > ((pmt_map_t *)pb)->i_pid )
443 static int intcompare( const void *pa, const void *pb )
445 if ( *(int *)pa < *(int *)pb )
447 else if ( *(int *)pa > *(int *)pb )
453 /*****************************************************************************
455 *****************************************************************************/
456 static int Control ( sout_mux_t *, int, va_list );
457 static int AddStream( sout_mux_t *, sout_input_t * );
458 static int DelStream( sout_mux_t *, sout_input_t * );
459 static int Mux ( sout_mux_t * );
461 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo );
462 static void TSSchedule ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
463 mtime_t i_pcr_length, mtime_t i_pcr_dts );
464 static void TSDate ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
465 mtime_t i_pcr_length, mtime_t i_pcr_dts );
466 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
467 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
469 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, vlc_bool_t b_pcr );
470 static void TSSetPCR( block_t *p_ts, mtime_t i_dts );
472 static void PEStoTS ( sout_instance_t *, sout_buffer_chain_t *, block_t *, ts_stream_t * );
474 /*****************************************************************************
476 *****************************************************************************/
477 static int Open( vlc_object_t *p_this )
479 sout_mux_t *p_mux =(sout_mux_t*)p_this;
480 sout_mux_sys_t *p_sys = NULL;
484 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
486 p_sys = malloc( sizeof( sout_mux_sys_t ) );
489 p_sys->i_pmtslots = p_sys->b_sdt = 0;
490 p_sys->i_num_pmt = 1;
491 p_sys->dvbpmt = NULL;
492 memset( &p_sys->pmtmap, 0, sizeof(p_sys->pmtmap) );
494 p_mux->pf_control = Control;
495 p_mux->pf_addstream = AddStream;
496 p_mux->pf_delstream = DelStream;
498 p_mux->p_sys = p_sys;
500 srand( (uint32_t)mdate() );
501 for ( i = 0; i < MAX_PMT; i++ )
502 p_sys->sdt_descriptors[i].psz_service_name
503 = p_sys->sdt_descriptors[i].psz_provider = NULL;
504 memset( p_sys->sdt_descriptors, 0, sizeof(sdt_desc_t) );
506 p_sys->i_audio_bound = 0;
507 p_sys->i_video_bound = 0;
509 var_Get( p_mux, SOUT_CFG_PREFIX "es-id-pid", &val );
510 p_sys->b_es_id_pid = val.b_bool;
512 var_Get( p_mux, SOUT_CFG_PREFIX "muxpmt", &val );
514 fetch string of pmts. Here's a sample: --sout-ts-muxpmt="0x451,0x200,0x28a,0x240,,0x450,0x201,0x28b,0x241,,0x452,0x202,0x28c,0x242"
515 This would mean 0x451, 0x200, 0x28a, 0x240 would fall under one pmt (program), 0x450,0x201,0x28b,0x241 would fall under another
517 if( val.psz_string != NULL && *val.psz_string )
520 char *psz = val.psz_string;
526 i_pid = strtoul( psz, &psz_next, 0 );
528 if ( strlen(psz_next) > 0 )
533 if ( p_sys->i_num_pmt > MAX_PMT )
536 "Number of PMTs greater than compiled maximum (%d)", MAX_PMT );
537 p_sys->i_num_pmt = MAX_PMT;
542 p_sys->pmtmap[p_sys->i_pmtslots].i_pid = i_pid;
543 p_sys->pmtmap[p_sys->i_pmtslots].i_prog = p_sys->i_num_pmt - 1;
545 if ( p_sys->i_pmtslots > MAX_PMT_PID )
548 "Number of pids in PMT greater than compiled maximum (%d)",
550 p_sys->i_pmtslots = MAX_PMT_PID;
554 /* Now sort according to pids for fast search later on */
555 qsort( (void *)p_sys->pmtmap, p_sys->i_pmtslots,
556 sizeof(pmt_map_t), &pmtcompare );
561 free( val.psz_string );
563 p_sys->i_pat_version_number = rand() % 32;
564 p_sys->pat.i_pid = 0;
565 p_sys->pat.i_continuity_counter = 0;
566 p_sys->pat.b_discontinuity = VLC_FALSE;
568 var_Get( p_mux, SOUT_CFG_PREFIX "tsid", &val );
570 p_sys->i_tsid = val.i_int;
572 p_sys->i_tsid = rand() % 65536;
574 p_sys->i_netid = rand() % 65536;
575 #ifdef HAVE_DVBPSI_SDT
576 var_Get( p_mux, SOUT_CFG_PREFIX "netid", &val );
578 p_sys->i_netid = val.i_int;
581 p_sys->i_pmt_version_number = rand() % 32;
582 for( i = 0; i < p_sys->i_num_pmt; i++ )
584 p_sys->pmt[i].i_continuity_counter = 0;
585 p_sys->pmt[i].b_discontinuity = VLC_FALSE;
588 p_sys->sdt.i_pid = 0x11;
589 p_sys->sdt.i_continuity_counter = 0;
590 p_sys->sdt.b_discontinuity = VLC_FALSE;
592 #ifdef HAVE_DVBPSI_SDT
593 var_Get( p_mux, SOUT_CFG_PREFIX "sdtdesc", &val );
594 p_sys->b_sdt = val.psz_string && *val.psz_string ? VLC_TRUE : VLC_FALSE;
596 /* Syntax is provider_sdt1,service_name_sdt1,provider_sdt2,service_name_sdt2... */
600 char *psz = val.psz_string;
601 char *psz_sdttoken = psz;
604 while ( psz_sdttoken != NULL )
606 char *psz_end = strchr( psz_sdttoken, ',' );
607 if( psz_end != NULL )
613 p_sys->sdt_descriptors[i/2].psz_provider
614 = strdup(psz_sdttoken);
618 p_sys->sdt_descriptors[i/2].psz_service_name
619 = strdup(psz_sdttoken);
623 psz_sdttoken = psz_end;
626 free( val.psz_string );
628 p_sys->b_sdt = VLC_FALSE;
631 var_Get( p_mux, SOUT_CFG_PREFIX "alignment", &val );
632 p_sys->b_data_alignment = val.b_bool;
634 var_Get( p_mux, SOUT_CFG_PREFIX "program-pmt", &val );
635 if( val.psz_string && *val.psz_string )
638 char *psz = val.psz_string;
643 while ( psz != NULL )
645 i_pid = strtoul( psz, &psz_next, 0 );
646 if( strlen(psz_next) > 0 )
654 msg_Err( p_mux, "Number of PMTs > maximum (%d)",
659 p_sys->i_pmt_program_number[i] = i_pid;
666 /* Option not specified, use 1, 2, 3... */
667 for( i = 0; i < p_sys->i_num_pmt; i++ )
668 p_sys->i_pmt_program_number[i] = i + 1;
670 free( val.psz_string );
672 var_Get( p_mux, SOUT_CFG_PREFIX "pid-pmt", &val );
675 for( i = 0; i < p_sys->i_num_pmt; i++ )
676 p_sys->pmt[i].i_pid = val.i_int + i; /* Does this make any sense? */
680 for( i = 0; i < p_sys->i_num_pmt; i++ )
681 p_sys->pmt[i].i_pid = 0x42 + i;
684 p_sys->i_pid_free = p_sys->pmt[p_sys->i_num_pmt - 1].i_pid + 1;
686 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
687 p_sys->i_pid_video = val.i_int;
688 if ( p_sys->i_pid_video > p_sys->i_pid_free )
690 p_sys->i_pid_free = p_sys->i_pid_video + 1;
693 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
694 p_sys->i_pid_audio = val.i_int;
695 if ( p_sys->i_pid_audio > p_sys->i_pid_free )
697 p_sys->i_pid_free = p_sys->i_pid_audio + 1;
700 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
701 p_sys->i_pid_spu = val.i_int;
702 if ( p_sys->i_pid_spu > p_sys->i_pid_free )
704 p_sys->i_pid_free = p_sys->i_pid_spu + 1;
707 p_sys->i_pcr_pid = 0x1fff;
708 p_sys->p_pcr_input = NULL;
710 p_sys->i_mpeg4_streams = 0;
712 p_sys->i_null_continuity_counter = 0;
714 /* Allow to create constrained stream */
715 var_Get( p_mux, SOUT_CFG_PREFIX "bmin", &val );
716 p_sys->i_bitrate_min = val.i_int;
718 var_Get( p_mux, SOUT_CFG_PREFIX "bmax", &val );
719 p_sys->i_bitrate_max = val.i_int;
721 if( p_sys->i_bitrate_min > 0 && p_sys->i_bitrate_max > 0 &&
722 p_sys->i_bitrate_min > p_sys->i_bitrate_max )
724 msg_Err( p_mux, "incompatible minimum and maximum bitrate, "
725 "disabling bitrate control" );
726 p_sys->i_bitrate_min = 0;
727 p_sys->i_bitrate_max = 0;
729 if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
731 msg_Err( p_mux, "bmin and bmax no more supported "
732 "(if you need them report it)" );
735 var_Get( p_mux, SOUT_CFG_PREFIX "shaping", &val );
736 p_sys->i_shaping_delay = (int64_t)val.i_int * 1000;
737 if( p_sys->i_shaping_delay <= 0 )
740 "invalid shaping ("I64Fd"ms) resetting to 200ms",
741 p_sys->i_shaping_delay / 1000 );
742 p_sys->i_shaping_delay = 200000;
745 var_Get( p_mux, SOUT_CFG_PREFIX "pcr", &val );
746 p_sys->i_pcr_delay = (int64_t)val.i_int * 1000;
747 if( p_sys->i_pcr_delay <= 0 ||
748 p_sys->i_pcr_delay >= p_sys->i_shaping_delay )
751 "invalid pcr delay ("I64Fd"ms) resetting to 70ms",
752 p_sys->i_pcr_delay / 1000 );
753 p_sys->i_pcr_delay = 70000;
756 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
757 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
759 msg_Dbg( p_mux, "shaping="I64Fd" pcr="I64Fd" dts_delay="I64Fd,
760 p_sys->i_shaping_delay, p_sys->i_pcr_delay, p_sys->i_dts_delay );
762 var_Get( p_mux, SOUT_CFG_PREFIX "use-key-frames", &val );
763 p_sys->b_use_key_frames = val.b_bool;
765 /* for TS generation */
769 var_Get( p_mux, SOUT_CFG_PREFIX "csa-ck", &val );
770 if( val.psz_string && *val.psz_string )
772 char *psz = val.psz_string;
775 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
779 if( strlen( psz ) != 16 )
781 msg_Dbg( p_mux, "invalid csa ck (it must be 16 chars long)" );
785 uint64_t i_ck = strtoull( psz, NULL, 16 );
789 for( i = 0; i < 8; i++ )
791 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
793 #ifndef TS_NO_CSA_CK_MSG
794 msg_Dbg( p_mux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
795 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
797 p_sys->csa = csa_New();
802 csa_SetCW( p_sys->csa, ck, ck );
804 var_Get( p_mux, SOUT_CFG_PREFIX "csa-pkt", &pkt_val );
805 if( pkt_val.i_int < 12 || pkt_val.i_int > 188 )
807 msg_Err( p_mux, "wrong packet size %d specified.", pkt_val.i_int );
808 msg_Warn( p_mux, "using default packet size of 188 bytes" );
809 p_sys->i_csa_pkt_size = 188;
811 else p_sys->i_csa_pkt_size = pkt_val.i_int;
812 msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
816 free( val.psz_string );
818 var_Get( p_mux, SOUT_CFG_PREFIX "crypt-audio", &val );
819 p_sys->b_crypt_audio = val.b_bool;
821 var_Get( p_mux, SOUT_CFG_PREFIX "crypt-video", &val );
822 p_sys->b_crypt_video = val.b_bool;
827 /*****************************************************************************
829 *****************************************************************************/
830 static void Close( vlc_object_t * p_this )
832 sout_mux_t *p_mux = (sout_mux_t*)p_this;
833 sout_mux_sys_t *p_sys = p_mux->p_sys;
838 csa_Delete( p_sys->csa );
840 for( i = 0; i < MAX_PMT; i++ )
842 free( p_sys->sdt_descriptors[i].psz_service_name );
843 free( p_sys->sdt_descriptors[i].psz_provider );
846 free( p_sys->dvbpmt );
850 /*****************************************************************************
852 *****************************************************************************/
853 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
861 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
862 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
866 case MUX_GET_ADD_STREAM_WAIT:
867 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
868 *pb_bool = VLC_FALSE;
872 ppsz = (char**)va_arg( args, char ** );
873 *ppsz = strdup( "video/mpeg" ); /* FIXME not sure */
881 /*****************************************************************************
882 * AddStream: called for each stream addition
883 *****************************************************************************/
884 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
886 sout_mux_sys_t *p_sys = p_mux->p_sys;
887 ts_stream_t *p_stream;
890 p_input->p_sys = p_stream = malloc( sizeof( ts_stream_t ) );
891 if( !p_input->p_sys )
894 /* Init this new stream */
895 if ( p_sys->b_es_id_pid )
896 p_stream->i_pid = p_input->p_fmt->i_id & 0x1fff;
898 p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
899 p_stream->i_codec = p_input->p_fmt->i_codec;
900 p_stream->i_continuity_counter = 0;
901 p_stream->b_discontinuity = VLC_FALSE;
902 p_stream->i_decoder_specific_info = 0;
903 p_stream->p_decoder_specific_info = NULL;
905 msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d",
906 (char*)&p_input->p_fmt->i_codec, p_stream->i_pid );
908 /* All others fields depand on codec */
909 switch( p_input->p_fmt->i_cat )
912 switch( p_input->p_fmt->i_codec )
914 case VLC_FOURCC( 'm', 'p','g', 'v' ):
915 /* TODO: do we need to check MPEG-I/II ? */
916 p_stream->i_stream_type = 0x02;
917 p_stream->i_stream_id = 0xe0;
919 case VLC_FOURCC( 'm', 'p','4', 'v' ):
920 p_stream->i_stream_type = 0x10;
921 p_stream->i_stream_id = 0xe0;
922 p_stream->i_es_id = p_stream->i_pid;
924 case VLC_FOURCC( 'h', '2','6', '4' ):
925 p_stream->i_stream_type = 0x1b;
926 p_stream->i_stream_id = 0xe0;
928 /* XXX dirty dirty but somebody want that:
929 * using crapy MS-codec XXX */
930 /* I didn't want to do that :P */
931 case VLC_FOURCC( 'H', '2', '6', '3' ):
932 case VLC_FOURCC( 'I', '2', '6', '3' ):
933 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
934 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
935 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
936 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
937 case VLC_FOURCC( 'D', 'I', 'V', '2' ):
938 case VLC_FOURCC( 'D', 'I', 'V', '1' ):
939 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
940 p_stream->i_stream_type = 0xa0; /* private */
941 p_stream->i_stream_id = 0xa0; /* beurk */
942 p_stream->i_bih_codec = p_input->p_fmt->i_codec;
943 p_stream->i_bih_width = p_input->p_fmt->video.i_width;
944 p_stream->i_bih_height = p_input->p_fmt->video.i_height;
950 p_sys->i_video_bound++;
954 switch( p_input->p_fmt->i_codec )
956 case VLC_FOURCC( 'm', 'p','g', 'a' ):
957 case VLC_FOURCC( 'm', 'p', '3', ' ' ):
958 p_stream->i_stream_type =
959 p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
960 p_stream->i_stream_id = 0xc0;
962 case VLC_FOURCC( 'a', '5','2', ' ' ):
963 p_stream->i_stream_type = 0x81;
964 p_stream->i_stream_id = 0xbd;
966 case VLC_FOURCC( 'l', 'p','c', 'm' ):
967 p_stream->i_stream_type = 0x83;
968 p_stream->i_stream_id = 0xbd;
970 case VLC_FOURCC( 'd', 't','s', ' ' ):
971 p_stream->i_stream_type = 0x06;
972 p_stream->i_stream_id = 0xbd;
974 case VLC_FOURCC( 'm', 'p','4', 'a' ):
975 p_stream->i_stream_type = 0x11;
976 p_stream->i_stream_id = 0xfa;
977 p_sys->i_mpeg4_streams++;
978 p_stream->i_es_id = p_stream->i_pid;
984 p_sys->i_audio_bound++;
988 switch( p_input->p_fmt->i_codec )
990 case VLC_FOURCC( 's', 'p','u', ' ' ):
991 p_stream->i_stream_type = 0x82;
992 p_stream->i_stream_id = 0xbd;
994 case VLC_FOURCC( 's', 'u','b', 't' ):
995 p_stream->i_stream_type = 0x12;
996 p_stream->i_stream_id = 0xfa;
997 p_sys->i_mpeg4_streams++;
998 p_stream->i_es_id = p_stream->i_pid;
1000 case VLC_FOURCC('d','v','b','s'):
1001 p_stream->i_stream_type = 0x06;
1002 p_stream->i_es_id = p_input->p_fmt->subs.dvb.i_id;
1003 p_stream->i_stream_id = 0xbd;
1005 case VLC_FOURCC('t','e','l','x'):
1006 p_stream->i_stream_type = 0x06;
1007 p_stream->i_stream_id = 0xbd; /* FIXME */
1011 return VLC_EGENERIC;
1017 return VLC_EGENERIC;
1020 p_stream->i_langs = 1+p_input->p_fmt->i_extra_languages;
1021 p_stream->lang = malloc(p_stream->i_langs*3);
1022 if( !p_stream->lang )
1024 p_stream->i_langs = 0;
1031 p_stream->lang[2] = '\0';
1032 if( p_input->p_fmt->psz_language )
1034 char *psz = p_input->p_fmt->psz_language;
1035 const iso639_lang_t *pl = NULL;
1037 if( strlen( psz ) == 2 )
1039 pl = GetLang_1( psz );
1041 else if( strlen( psz ) == 3 )
1043 pl = GetLang_2B( psz );
1044 if( !strcmp( pl->psz_iso639_1, "??" ) )
1046 pl = GetLang_2T( psz );
1049 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1051 p_stream->lang[0] = pl->psz_iso639_2T[0];
1052 p_stream->lang[1] = pl->psz_iso639_2T[1];
1053 p_stream->lang[2] = pl->psz_iso639_2T[2];
1055 msg_Dbg( p_mux, " - lang=%c%c%c",
1056 p_stream->lang[0], p_stream->lang[1],
1057 p_stream->lang[2] );
1060 while( i < p_stream->i_langs ) {
1061 if( p_input->p_fmt->p_extra_languages[i-1].psz_language )
1063 char *psz = p_input->p_fmt->p_extra_languages[i-1].psz_language;
1064 const iso639_lang_t *pl = NULL;
1066 if( strlen( psz ) == 2 )
1068 pl = GetLang_1( psz );
1070 else if( strlen( psz ) == 3 )
1072 pl = GetLang_2B( psz );
1073 if( !strcmp( pl->psz_iso639_1, "??" ) )
1075 pl = GetLang_2T( psz );
1078 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1080 p_stream->lang[i*3+0] = pl->psz_iso639_2T[0];
1081 p_stream->lang[i*3+1] = pl->psz_iso639_2T[1];
1082 p_stream->lang[i*3+2] = pl->psz_iso639_2T[2];
1084 msg_Dbg( p_mux, " - lang=%c%c%c",
1085 p_stream->lang[i*3+0], p_stream->lang[i*3+1],
1086 p_stream->lang[i*3+2] );
1092 /* Create decoder specific info for subt */
1093 if( p_stream->i_codec == VLC_FOURCC( 's', 'u','b', 't' ) )
1097 p_stream->i_decoder_specific_info = 55;
1098 p_stream->p_decoder_specific_info = p =
1099 malloc( p_stream->i_decoder_specific_info );
1102 p[0] = 0x10; /* textFormat, 0x10 for 3GPP TS 26.245 */
1103 p[1] = 0x00; /* flags: 1b: associated video info flag
1107 p[2] = 52; /* remaining size */
1111 p[0] = p[1] = p[2] = p[3] = 0; p+=4; /* display flags */
1112 *p++ = 0; /* horizontal justification (-1: left, 0 center, 1 right) */
1113 *p++ = 1; /* vertical justification (-1: top, 0 center, 1 bottom) */
1115 p[0] = p[1] = p[2] = 0x00; p+=3;/* background rgb */
1116 *p++ = 0xff; /* background a */
1118 p[0] = p[1] = 0; p += 2; /* text box top */
1119 p[0] = p[1] = 0; p += 2; /* text box left */
1120 p[0] = p[1] = 0; p += 2; /* text box bottom */
1121 p[0] = p[1] = 0; p += 2; /* text box right */
1123 p[0] = p[1] = 0; p += 2; /* start char */
1124 p[0] = p[1] = 0; p += 2; /* end char */
1125 p[0] = p[1] = 0; p += 2; /* default font id */
1127 *p++ = 0; /* font style flags */
1128 *p++ = 12; /* font size */
1130 p[0] = p[1] = p[2] = 0x00; p+=3;/* foreground rgb */
1131 *p++ = 0x00; /* foreground a */
1133 p[0] = p[1] = p[2] = 0; p[3] = 22; p += 4;
1134 memcpy( p, "ftab", 4 ); p += 4;
1135 *p++ = 0; *p++ = 1; /* entry count */
1136 p[0] = p[1] = 0; p += 2; /* font id */
1137 *p++ = 9; /* font name length */
1138 memcpy( p, "Helvetica", 9 ); /* font name */
1140 else p_stream->i_decoder_specific_info = 0;
1144 /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
1145 p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra;
1146 if( p_stream->i_decoder_specific_info > 0 )
1148 p_stream->p_decoder_specific_info =
1149 malloc( p_stream->i_decoder_specific_info );
1150 if( p_stream->p_decoder_specific_info )
1152 memcpy( p_stream->p_decoder_specific_info,
1153 p_input->p_fmt->p_extra,
1154 p_input->p_fmt->i_extra );
1156 else p_stream->i_decoder_specific_info = 0;
1160 /* Init pes chain */
1161 BufferChainInit( &p_stream->chain_pes );
1162 p_stream->i_pes_dts = 0;
1163 p_stream->i_pes_length = 0;
1164 p_stream->i_pes_used = 0;
1165 p_stream->b_key_frame = 0;
1167 /* We only change PMT version (PAT isn't changed) */
1168 p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
1170 /* Update pcr_pid */
1171 if( p_input->p_fmt->i_cat != SPU_ES &&
1172 ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
1174 if( p_sys->p_pcr_input )
1176 /* There was already a PCR stream, so clean context */
1179 p_sys->i_pcr_pid = p_stream->i_pid;
1180 p_sys->p_pcr_input = p_input;
1182 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1188 /*****************************************************************************
1189 * DelStream: called before a stream deletion
1190 *****************************************************************************/
1191 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1193 sout_mux_sys_t *p_sys = p_mux->p_sys;
1194 ts_stream_t *p_stream;
1197 p_stream = (ts_stream_t*)p_input->p_sys;
1198 msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
1200 if( p_sys->i_pcr_pid == p_stream->i_pid )
1204 /* Find a new pcr stream (Prefer Video Stream) */
1205 p_sys->i_pcr_pid = 0x1fff;
1206 p_sys->p_pcr_input = NULL;
1207 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1209 if( p_mux->pp_inputs[i] == p_input )
1214 if( p_mux->pp_inputs[i]->p_fmt->i_cat == VIDEO_ES )
1217 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1218 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1221 else if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES &&
1222 p_sys->i_pcr_pid == 0x1fff )
1225 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1226 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1229 if( p_sys->p_pcr_input )
1231 /* Empty TS buffer */
1234 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1237 /* Empty all data in chain_pes */
1238 BufferChainClean( &p_stream->chain_pes );
1240 free(p_stream->lang);
1241 free( p_stream->p_decoder_specific_info );
1242 if( p_stream->i_stream_id == 0xfa ||
1243 p_stream->i_stream_id == 0xfb ||
1244 p_stream->i_stream_id == 0xfe )
1246 p_sys->i_mpeg4_streams--;
1249 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
1252 int i_pid_video = val.i_int;
1253 if ( i_pid_video == p_stream->i_pid )
1255 p_sys->i_pid_video = i_pid_video;
1256 msg_Dbg( p_mux, "freeing video PID %d", i_pid_video );
1259 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
1262 int i_pid_audio = val.i_int;
1263 if ( i_pid_audio == p_stream->i_pid )
1265 p_sys->i_pid_audio = i_pid_audio;
1266 msg_Dbg( p_mux, "freeing audio PID %d", i_pid_audio );
1269 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
1272 int i_pid_spu = val.i_int;
1273 if ( i_pid_spu == p_stream->i_pid )
1275 p_sys->i_pid_spu = i_pid_spu;
1276 msg_Dbg( p_mux, "freeing spu PID %d", i_pid_spu );
1281 /* We only change PMT version (PAT isn't changed) */
1282 p_sys->i_pmt_version_number++; p_sys->i_pmt_version_number %= 32;
1287 /*****************************************************************************
1288 * Mux: Call each time there is new data for at least one stream
1289 *****************************************************************************
1291 *****************************************************************************/
1292 static int Mux( sout_mux_t *p_mux )
1294 sout_mux_sys_t *p_sys = p_mux->p_sys;
1295 ts_stream_t *p_pcr_stream;
1297 if( p_sys->i_pcr_pid == 0x1fff )
1300 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1302 block_FifoEmpty( p_mux->pp_inputs[i]->p_fifo );
1304 msg_Dbg( p_mux, "waiting for PCR streams" );
1308 p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
1312 sout_buffer_chain_t chain_ts;
1316 mtime_t i_pcr_length;
1317 mtime_t i_shaping_delay;
1320 if( p_pcr_stream->b_key_frame )
1322 i_shaping_delay = p_pcr_stream->i_pes_length;
1326 i_shaping_delay = p_sys->i_shaping_delay;
1329 /* 1: get enough PES packet for all input */
1332 vlc_bool_t b_ok = VLC_TRUE;
1335 /* Accumulate enough data in the pcr stream (>i_shaping_delay) */
1336 /* Accumulate enough data in all other stream ( >= length of pcr)*/
1337 for( i = -1; i < p_mux->i_nb_inputs; i++ )
1339 sout_input_t *p_input;
1340 ts_stream_t *p_stream;
1341 int64_t i_spu_delay = 0;
1344 p_input = p_sys->p_pcr_input;
1345 else if( p_mux->pp_inputs[i]->p_sys == p_pcr_stream )
1348 p_input = p_mux->pp_inputs[i];
1349 p_stream = (ts_stream_t*)p_input->p_sys;
1351 if( ( ( p_stream == p_pcr_stream ) &&
1352 ( p_stream->i_pes_length < i_shaping_delay ) ) ||
1353 ( p_stream->i_pes_dts + p_stream->i_pes_length <
1354 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length ) )
1356 /* Need more data */
1357 if( block_FifoCount( p_input->p_fifo ) <= 1 )
1359 if( ( p_input->p_fmt->i_cat == AUDIO_ES ) ||
1360 ( p_input->p_fmt->i_cat == VIDEO_ES ) )
1362 /* We need more data */
1365 else if( block_FifoCount( p_input->p_fifo ) <= 0 )
1367 /* spu, only one packet is needed */
1370 else if( p_input->p_fmt->i_cat == SPU_ES )
1372 /* Don't mux the SPU yet if it is too early */
1373 block_t *p_spu = block_FifoShow( p_input->p_fifo );
1376 p_spu->i_dts - p_pcr_stream->i_pes_dts;
1378 if( ( i_spu_delay > i_shaping_delay ) &&
1379 ( i_spu_delay < I64C(100000000) ) )
1382 if ( ( i_spu_delay >= I64C(100000000) ) ||
1383 ( i_spu_delay < I64C(10000) ) )
1385 BufferChainClean( &p_stream->chain_pes );
1386 p_stream->i_pes_dts = 0;
1387 p_stream->i_pes_used = 0;
1388 p_stream->i_pes_length = 0;
1395 if( p_stream == p_pcr_stream || p_sys->b_data_alignment
1396 || p_input->p_fmt->i_codec !=
1397 VLC_FOURCC('m', 'p', 'g', 'a') )
1398 p_data = block_FifoGet( p_input->p_fifo );
1400 p_data = FixPES( p_mux, p_input->p_fifo );
1402 if( block_FifoCount( p_input->p_fifo ) > 0 &&
1403 p_input->p_fmt->i_cat != SPU_ES )
1405 block_t *p_next = block_FifoShow( p_input->p_fifo );
1406 p_data->i_length = p_next->i_dts - p_data->i_dts;
1408 else if( p_input->p_fmt->i_codec !=
1409 VLC_FOURCC('s', 'u', 'b', 't' ) )
1410 p_data->i_length = 1000;
1412 if( ( p_pcr_stream->i_pes_dts > 0 &&
1413 p_data->i_dts - 10000000 > p_pcr_stream->i_pes_dts +
1414 p_pcr_stream->i_pes_length ) ||
1415 p_data->i_dts < p_stream->i_pes_dts ||
1416 ( p_stream->i_pes_dts > 0 &&
1417 p_input->p_fmt->i_cat != SPU_ES &&
1418 p_data->i_dts - 10000000 > p_stream->i_pes_dts +
1419 p_stream->i_pes_length ) )
1421 msg_Warn( p_mux, "packet with too strange dts "
1422 "(dts="I64Fd",old="I64Fd",pcr="I64Fd")",
1423 p_data->i_dts, p_stream->i_pes_dts,
1424 p_pcr_stream->i_pes_dts );
1425 block_Release( p_data );
1427 BufferChainClean( &p_stream->chain_pes );
1428 p_stream->i_pes_dts = 0;
1429 p_stream->i_pes_used = 0;
1430 p_stream->i_pes_length = 0;
1432 if( p_input->p_fmt->i_cat != SPU_ES )
1434 BufferChainClean( &p_pcr_stream->chain_pes );
1435 p_pcr_stream->i_pes_dts = 0;
1436 p_pcr_stream->i_pes_used = 0;
1437 p_pcr_stream->i_pes_length = 0;
1442 int i_header_size = 0;
1443 int b_data_alignment = 0;
1444 if( p_input->p_fmt->i_cat == SPU_ES )
1446 if( p_input->p_fmt->i_codec ==
1447 VLC_FOURCC('s','u','b','t') )
1449 /* Prepend header */
1450 p_data = block_Realloc( p_data, 2,
1452 p_data->p_buffer[0] =
1453 ( (p_data->i_buffer - 2) >> 8) & 0xff;
1454 p_data->p_buffer[1] =
1455 ( (p_data->i_buffer - 2) ) & 0xff;
1457 /* remove trailling \0 if any */
1458 if( p_data->i_buffer > 2 &&
1459 p_data->p_buffer[p_data->i_buffer -1] ==
1463 /* Append a empty sub (sub text only) */
1464 if( p_data->i_length > 0 &&
1465 !( p_data->i_buffer == 1 &&
1466 *p_data->p_buffer == ' ' ) )
1468 block_t *p_spu = block_New( p_mux, 3 );
1470 p_spu->i_dts = p_spu->i_pts =
1471 p_data->i_dts + p_data->i_length;
1472 p_spu->i_length = 1000;
1474 p_spu->p_buffer[0] = 0;
1475 p_spu->p_buffer[1] = 1;
1476 p_spu->p_buffer[2] = ' ';
1478 E_(EStoPES)( p_mux->p_sout, &p_spu, p_spu,
1480 p_stream->i_stream_id, 1,
1482 p_data->p_next = p_spu;
1485 else if( p_input->p_fmt->i_codec ==
1486 VLC_FOURCC('t','e','l','x') )
1489 i_header_size = 0x24;
1490 b_data_alignment = 1;
1492 else if( p_input->p_fmt->i_codec ==
1493 VLC_FOURCC('d','v','b','s') )
1496 b_data_alignment = 1;
1499 else if( p_data->i_length < 0 ||
1500 p_data->i_length > 2000000 )
1502 /* FIXME choose a better value, but anyway we
1503 * should never have to do that */
1504 p_data->i_length = 1000;
1507 p_stream->i_pes_length += p_data->i_length;
1508 if( p_stream->i_pes_dts == 0 )
1510 p_stream->i_pes_dts = p_data->i_dts;
1513 /* Convert to pes */
1514 if( p_stream->i_stream_id == 0xa0 &&
1515 p_data->i_pts <= 0 )
1517 /* XXX yes I know, it's awful, but it's needed,
1518 * so don't remove it ... */
1519 p_data->i_pts = p_data->i_dts;
1522 E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
1523 p_input->p_fmt, p_stream->i_stream_id,
1524 1, b_data_alignment, i_header_size, 0 );
1526 BufferChainAppend( &p_stream->chain_pes, p_data );
1528 if( p_sys->b_use_key_frames && p_stream == p_pcr_stream
1529 && (p_data->i_flags & BLOCK_FLAG_TYPE_I)
1530 && !(p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME)
1531 && (p_stream->i_pes_length > 400000) )
1533 i_shaping_delay = p_stream->i_pes_length;
1534 p_stream->b_key_frame = 1;
1547 i_pcr_dts = p_pcr_stream->i_pes_dts;
1548 i_pcr_length = p_pcr_stream->i_pes_length;
1549 p_pcr_stream->b_key_frame = 0;
1551 /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
1552 /* 2: calculate non accurate total size of muxed ts */
1554 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1556 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1559 /* False for pcr stream but it will be enough to do PCR algo */
1560 for( p_pes = p_stream->chain_pes.p_first; p_pes != NULL;
1561 p_pes = p_pes->p_next )
1563 int i_size = p_pes->i_buffer;
1564 if( p_pes->i_dts + p_pes->i_length >
1565 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1567 mtime_t i_frag = p_pcr_stream->i_pes_dts +
1568 p_pcr_stream->i_pes_length - p_pes->i_dts;
1574 i_size = p_pes->i_buffer * i_frag / p_pes->i_length;
1576 i_packet_count += ( i_size + 183 ) / 184;
1579 /* add overhead for PCR (not really exact) */
1580 i_packet_count += (8 * i_pcr_length / p_sys->i_pcr_delay + 175) / 176;
1582 /* 3: mux PES into TS */
1583 BufferChainInit( &chain_ts );
1584 /* append PAT/PMT -> FIXME with big pcr delay it won't have enough pat/pmt */
1585 GetPAT( p_mux, &chain_ts );
1586 GetPMT( p_mux, &chain_ts );
1588 i_packet_count += chain_ts.i_depth;
1589 /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
1595 ts_stream_t *p_stream;
1596 sout_input_t *p_input;
1600 /* Select stream (lowest dts) */
1601 for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
1603 p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1605 if( p_stream->i_pes_dts == 0 )
1610 if( i_stream == -1 ||
1611 p_stream->i_pes_dts < i_dts )
1614 i_dts = p_stream->i_pes_dts;
1617 if( i_stream == -1 || i_dts > i_pcr_dts + i_pcr_length )
1621 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1622 p_input = p_mux->pp_inputs[i_stream];
1624 /* do we need to issue pcr */
1626 if( p_stream == p_pcr_stream &&
1627 i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >=
1628 p_sys->i_pcr + p_sys->i_pcr_delay )
1631 p_sys->i_pcr = i_pcr_dts + i_packet_pos *
1632 i_pcr_length / i_packet_count;
1635 /* Build the TS packet */
1636 p_ts = TSNew( p_mux, p_stream, b_pcr );
1637 if( p_sys->csa != NULL &&
1638 (p_input->p_fmt->i_cat != AUDIO_ES || p_sys->b_crypt_audio) &&
1639 (p_input->p_fmt->i_cat != VIDEO_ES || p_sys->b_crypt_video) )
1641 p_ts->i_flags |= BLOCK_FLAG_SCRAMBLED;
1646 BufferChainAppend( &chain_ts, p_ts );
1649 /* 4: date and send */
1650 TSSchedule( p_mux, &chain_ts, i_pcr_length, i_pcr_dts );
1654 #define STD_PES_PAYLOAD 170
1655 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
1660 p_data = block_FifoShow( p_fifo );
1661 i_size = p_data->i_buffer;
1663 if( i_size == STD_PES_PAYLOAD )
1665 return block_FifoGet( p_fifo );
1667 else if( i_size > STD_PES_PAYLOAD )
1669 block_t *p_new = block_New( p_mux, STD_PES_PAYLOAD );
1670 p_mux->p_libvlc->pf_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
1671 p_new->i_pts = p_data->i_pts;
1672 p_new->i_dts = p_data->i_dts;
1673 p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
1675 p_data->i_buffer -= STD_PES_PAYLOAD;
1676 p_data->p_buffer += STD_PES_PAYLOAD;
1677 p_data->i_pts += p_new->i_length;
1678 p_data->i_dts += p_new->i_length;
1679 p_data->i_length -= p_new->i_length;
1680 p_data->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1688 p_data = block_FifoGet( p_fifo );
1689 p_data = block_Realloc( p_data, 0, STD_PES_PAYLOAD );
1690 p_next = block_FifoShow( p_fifo );
1691 if ( p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME )
1693 p_data->i_flags &= ~BLOCK_FLAG_NO_KEYFRAME;
1694 p_data->i_pts = p_next->i_pts;
1695 p_data->i_dts = p_next->i_dts;
1697 i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
1699 p_mux->p_libvlc->pf_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer,
1701 p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
1702 p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
1703 p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
1704 p_next->i_buffer -= i_copy;
1705 p_next->p_buffer += i_copy;
1706 p_next->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1708 if( !p_next->i_buffer )
1710 p_next = block_FifoGet( p_fifo );
1711 block_Release( p_next );
1717 static void TSSchedule( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1718 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1720 sout_mux_sys_t *p_sys = p_mux->p_sys;
1721 sout_buffer_chain_t new_chain;
1722 int i_packet_count = p_chain_ts->i_depth;
1725 BufferChainInit( &new_chain );
1727 if ( i_pcr_length <= 0 )
1729 i_pcr_length = i_packet_count;
1732 for( i = 0; i < i_packet_count; i++ )
1734 block_t *p_ts = BufferChainGet( p_chain_ts );
1735 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1737 BufferChainAppend( &new_chain, p_ts );
1740 p_ts->i_dts + p_sys->i_dts_delay * 2/3 < i_new_dts )
1742 mtime_t i_max_diff = i_new_dts - p_ts->i_dts;
1743 mtime_t i_cut_dts = p_ts->i_dts;
1745 p_ts = BufferChainPeek( p_chain_ts );
1747 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1748 while ( p_ts != NULL && i_new_dts - p_ts->i_dts >= i_max_diff )
1750 p_ts = BufferChainGet( p_chain_ts );
1751 i_max_diff = i_new_dts - p_ts->i_dts;
1752 i_cut_dts = p_ts->i_dts;
1753 BufferChainAppend( &new_chain, p_ts );
1755 p_ts = BufferChainPeek( p_chain_ts );
1757 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1759 msg_Dbg( p_mux, "adjusting rate at "I64Fd"/"I64Fd" (%d/%d)",
1760 i_cut_dts - i_pcr_dts, i_pcr_length, new_chain.i_depth,
1761 p_chain_ts->i_depth );
1762 if ( new_chain.i_depth )
1763 TSDate( p_mux, &new_chain,
1764 i_cut_dts - i_pcr_dts,
1766 if ( p_chain_ts->i_depth )
1768 p_chain_ts, i_pcr_dts + i_pcr_length - i_cut_dts,
1774 if ( new_chain.i_depth )
1775 TSDate( p_mux, &new_chain, i_pcr_length, i_pcr_dts );
1778 static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1779 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1781 sout_mux_sys_t *p_sys = p_mux->p_sys;
1782 int i_packet_count = p_chain_ts->i_depth;
1785 if ( i_pcr_length / 1000 > 0 )
1787 int i_bitrate = ((uint64_t)i_packet_count * 188 * 8000)
1788 / (uint64_t)(i_pcr_length / 1000);
1789 if ( p_sys->i_bitrate_max && p_sys->i_bitrate_max < i_bitrate )
1791 msg_Warn( p_mux, "max bitrate exceeded at "I64Fd
1792 " (%d bi/s for %d pkt in "I64Fd" us)",
1793 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1794 i_bitrate, i_packet_count, i_pcr_length);
1799 msg_Dbg( p_mux, "starting at "I64Fd
1800 " (%d bi/s for %d packets in "I64Fd" us)",
1801 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1802 i_bitrate, i_packet_count, i_pcr_length);
1808 /* This shouldn't happen, but happens in some rare heavy load
1809 * and packet losses conditions. */
1810 i_pcr_length = i_packet_count;
1813 /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
1814 for( i = 0; i < i_packet_count; i++ )
1816 block_t *p_ts = BufferChainGet( p_chain_ts );
1817 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1819 p_ts->i_dts = i_new_dts;
1820 p_ts->i_length = i_pcr_length / i_packet_count;
1822 if( p_ts->i_flags & BLOCK_FLAG_CLOCK )
1824 /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
1825 TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay );
1827 if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
1829 csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size, 0 );
1833 p_ts->i_dts += p_sys->i_shaping_delay * 3 / 2;
1835 sout_AccessOutWrite( p_mux->p_access, p_ts );
1839 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream,
1842 block_t *p_pes = p_stream->chain_pes.p_first;
1845 vlc_bool_t b_new_pes = VLC_FALSE;
1846 vlc_bool_t b_adaptation_field = VLC_FALSE;
1848 int i_payload_max = 184 - ( b_pcr ? 8 : 0 );
1851 if( p_stream->i_pes_used <= 0 )
1853 b_new_pes = VLC_TRUE;
1855 i_payload = __MIN( (int)p_pes->i_buffer - p_stream->i_pes_used,
1858 if( b_pcr || i_payload < i_payload_max )
1860 b_adaptation_field = VLC_TRUE;
1863 p_ts = block_New( p_mux, 188 );
1864 p_ts->i_dts = p_pes->i_dts;
1866 p_ts->p_buffer[0] = 0x47;
1867 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 ) |
1868 ( ( p_stream->i_pid >> 8 )&0x1f );
1869 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1870 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 ) |
1871 p_stream->i_continuity_counter;
1873 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1874 p_stream->b_discontinuity = (p_pes->i_flags & BLOCK_FLAG_DISCONTINUITY);
1876 if( b_adaptation_field )
1882 int i_stuffing = i_payload_max - i_payload;
1884 p_ts->i_flags |= BLOCK_FLAG_CLOCK;
1886 p_ts->p_buffer[4] = 7 + i_stuffing;
1887 p_ts->p_buffer[5] = 0x10; /* flags */
1888 if( p_stream->b_discontinuity )
1890 p_ts->p_buffer[5] |= 0x80; /* flag TS dicontinuity */
1891 p_stream->b_discontinuity = VLC_FALSE;
1893 p_ts->p_buffer[6] = ( 0 )&0xff;
1894 p_ts->p_buffer[7] = ( 0 )&0xff;
1895 p_ts->p_buffer[8] = ( 0 )&0xff;
1896 p_ts->p_buffer[9] = ( 0 )&0xff;
1897 p_ts->p_buffer[10]= ( ( 0 )&0x80 ) | 0x7e;
1898 p_ts->p_buffer[11]= 0;
1900 for( i = 12; i < 12 + i_stuffing; i++ )
1902 p_ts->p_buffer[i] = 0xff;
1907 int i_stuffing = i_payload_max - i_payload;
1909 p_ts->p_buffer[4] = i_stuffing - 1;
1910 if( i_stuffing > 1 )
1912 p_ts->p_buffer[5] = 0x00;
1913 for( i = 6; i < 6 + i_stuffing - 2; i++ )
1915 p_ts->p_buffer[i] = 0xff;
1922 memcpy( &p_ts->p_buffer[188 - i_payload],
1923 &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
1925 p_stream->i_pes_used += i_payload;
1926 p_stream->i_pes_dts = p_pes->i_dts + p_pes->i_length *
1927 p_stream->i_pes_used / p_pes->i_buffer;
1928 p_stream->i_pes_length -= p_pes->i_length * i_payload / p_pes->i_buffer;
1930 if( p_stream->i_pes_used >= (int)p_pes->i_buffer )
1932 p_pes = BufferChainGet( &p_stream->chain_pes );
1933 block_Release( p_pes );
1935 p_pes = p_stream->chain_pes.p_first;
1938 p_stream->i_pes_dts = p_pes->i_dts;
1939 p_stream->i_pes_length = 0;
1942 p_stream->i_pes_length += p_pes->i_length;
1944 p_pes = p_pes->p_next;
1949 p_stream->i_pes_dts = 0;
1950 p_stream->i_pes_length = 0;
1952 p_stream->i_pes_used = 0;
1958 static void TSSetPCR( block_t *p_ts, mtime_t i_dts )
1960 mtime_t i_pcr = 9 * i_dts / 100;
1962 p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
1963 p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
1964 p_ts->p_buffer[8] = ( i_pcr >> 9 )&0xff;
1965 p_ts->p_buffer[9] = ( i_pcr >> 1 )&0xff;
1966 p_ts->p_buffer[10]|= ( i_pcr << 7 )&0x80;
1970 static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c,
1971 mtime_t i_length, int i_bitrate_min,
1974 sout_mux_sys_t *p_sys = p_mux->p_sys;
1975 sout_buffer_chain_t s = *c;
1978 int i_packets_min = 0;
1979 int i_packets_max = 0;
1986 i_packets = c->i_depth;
1987 i_packets_min = ( (int64_t)i_bitrate_min * i_length / 8 / 1000000 + 187 ) / 188;
1988 i_packets_max = ( (int64_t)i_bitrate_max * i_length / 8 / 1000000 + 187 ) / 188;
1990 if( i_packets < i_packets_min && i_packets_min > 0 )
1993 int i_div = ( i_packets_min - i_packets ) / i_packets;
1994 int i_mod = ( i_packets_min - i_packets ) % i_packets;
1997 /* We need to pad with null packets (pid=0x1fff)
1998 * We try to melt null packets with true packets */
2000 "packets=%d but min=%d -> adding %d packets of padding",
2001 i_packets, i_packets_min, i_packets_min - i_packets );
2003 BufferChainInit( c );
2004 while( ( p_pk = BufferChainGet( &s ) ) )
2008 BufferChainAppend( c, p_pk );
2010 i_null = i_div + ( i_rest + i_mod ) / i_packets;
2012 for( i = 0; i < i_null; i++ )
2016 p_null = sout_BufferNew( p_mux->p_sout, 188 );
2017 p_null->p_buffer[0] = 0x47;
2018 p_null->p_buffer[1] = 0x1f;
2019 p_null->p_buffer[2] = 0xff;
2020 p_null->p_buffer[3] = 0x10 | p_sys->i_null_continuity_counter;
2021 memset( &p_null->p_buffer[4], 0, 184 );
2022 p_sys->i_null_continuity_counter =
2023 ( p_sys->i_null_continuity_counter + 1 ) % 16;
2025 BufferChainAppend( c, p_null );
2028 i_rest = ( i_rest + i_mod ) % i_packets;
2031 else if( i_packets > i_packets_max && i_packets_max > 0 )
2036 /* Arg, we need to drop packets, I don't do something clever (like
2037 * dropping complete pid, b frames, ... ), I just get the right amount
2038 * of packets and discard the others */
2040 "packets=%d but max=%d -> removing %d packets -> stream broken",
2041 i_packets, i_packets_max, i_packets - i_packets_max );
2043 BufferChainInit( c );
2044 for( i = 0; i < i_packets_max; i++ )
2046 BufferChainAppend( c, BufferChainGet( &s ) );
2049 while( ( p_pk = BufferChainGet( &s ) ) )
2051 sout_BufferDelete( p_mux->p_sout, p_pk );
2057 static void PEStoTS( sout_instance_t *p_sout,
2058 sout_buffer_chain_t *c, block_t *p_pes,
2059 ts_stream_t *p_stream )
2065 /* get PES total size */
2066 i_size = p_pes->i_buffer;
2067 p_data = p_pes->p_buffer;
2069 b_new_pes = VLC_TRUE;
2073 int b_adaptation_field;
2077 p_ts = block_New( p_sout, 188 );
2080 * 1b transport_error_indicator
2081 * 1b payload_unit_start
2082 * 1b transport_priority
2084 * 2b transport_scrambling_control
2085 * 2b if adaptation_field 0x03 else 0x01
2086 * 4b continuity_counter
2089 i_copy = __MIN( i_size, 184 );
2090 b_adaptation_field = i_size < 184 ? VLC_TRUE : VLC_FALSE;
2092 p_ts->p_buffer[0] = 0x47;
2093 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
2094 ( ( p_stream->i_pid >> 8 )&0x1f );
2095 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
2096 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
2097 p_stream->i_continuity_counter;
2099 b_new_pes = VLC_FALSE;
2100 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
2102 if( b_adaptation_field )
2104 int i_stuffing = 184 - i_copy;
2107 p_ts->p_buffer[4] = i_stuffing - 1;
2108 if( i_stuffing > 1 )
2110 p_ts->p_buffer[5] = 0x00;
2111 if( p_stream->b_discontinuity )
2113 p_ts->p_buffer[5] |= 0x80;
2114 p_stream->b_discontinuity = VLC_FALSE;
2116 for( i = 6; i < 6 + i_stuffing - 2; i++ )
2118 p_ts->p_buffer[i] = 0xff;
2123 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
2127 BufferChainAppend( c, p_ts );
2131 block_t *p_next = p_pes->p_next;
2133 p_pes->p_next = NULL;
2134 block_Release( p_pes );
2135 if( p_next == NULL )
2139 b_new_pes = VLC_TRUE;
2141 i_size = p_pes->i_buffer;
2142 p_data = p_pes->p_buffer;
2149 static block_t *WritePSISection( sout_instance_t *p_sout,
2150 dvbpsi_psi_section_t* p_section )
2152 block_t *p_psi, *p_first = NULL;
2158 i_size = (uint32_t)( p_section->p_payload_end - p_section->p_data )+
2159 ( p_section->b_syntax_indicator ? 4 : 0 );
2161 p_psi = block_New( p_sout, i_size + 1 );
2164 p_psi->i_length = 0;
2165 p_psi->i_buffer = i_size + 1;
2167 p_psi->p_buffer[0] = 0; /* pointer */
2168 memcpy( p_psi->p_buffer + 1,
2172 block_ChainAppend( &p_first, p_psi );
2174 p_section = p_section->p_next;
2180 static void GetPAT( sout_mux_t *p_mux,
2181 sout_buffer_chain_t *c )
2183 sout_mux_sys_t *p_sys = p_mux->p_sys;
2186 dvbpsi_psi_section_t *p_section;
2189 dvbpsi_InitPAT( &pat, p_sys->i_tsid, p_sys->i_pat_version_number,
2190 1 ); /* b_current_next */
2191 /* add all programs */
2192 for ( i = 0; i < p_sys->i_num_pmt; i++ )
2193 dvbpsi_PATAddProgram( &pat,
2194 p_sys->i_pmt_program_number[i],
2195 p_sys->pmt[i].i_pid );
2197 p_section = dvbpsi_GenPATSections( &pat,
2198 0 ); /* max program per section */
2200 p_pat = WritePSISection( p_mux->p_sout, p_section );
2202 PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
2204 dvbpsi_DeletePSISections( p_section );
2205 dvbpsi_EmptyPAT( &pat );
2208 static uint32_t GetDescriptorLength24b( int i_length )
2210 uint32_t i_l1, i_l2, i_l3;
2212 i_l1 = i_length&0x7f;
2213 i_l2 = ( i_length >> 7 )&0x7f;
2214 i_l3 = ( i_length >> 14 )&0x7f;
2216 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
2219 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c )
2221 sout_mux_sys_t *p_sys = p_mux->p_sys;
2222 block_t *p_pmt[MAX_PMT];
2224 dvbpsi_pmt_es_t *p_es;
2225 dvbpsi_psi_section_t *p_section[MAX_PMT];
2230 int *p_usepid = NULL;
2232 #ifdef HAVE_DVBPSI_SDT
2236 dvbpsi_psi_section_t* p_section2;
2237 dvbpsi_sdt_service_t *p_service;
2239 uint8_t *psz_sdt_desc;
2242 if( p_sys->dvbpmt == NULL )
2244 p_sys->dvbpmt = malloc( p_sys->i_num_pmt * sizeof(dvbpsi_pmt_t) );
2245 if( !p_sys->dvbpmt )
2250 #ifdef HAVE_DVBPSI_SDT
2252 dvbpsi_InitSDT( &sdt, p_sys->i_tsid, 1, 1, p_sys->i_netid );
2255 for( i = 0; i < p_sys->i_num_pmt; i++ )
2257 dvbpsi_InitPMT( &p_sys->dvbpmt[i],
2258 p_sys->i_pmt_program_number[i], /* program number */
2259 p_sys->i_pmt_version_number,
2260 1, /* b_current_next */
2263 #ifdef HAVE_DVBPSI_SDT
2266 p_service = dvbpsi_SDTAddService( &sdt,
2267 p_sys->i_pmt_program_number[i], /* service id */
2268 0, /* eit schedule */
2269 0, /* eit present */
2270 4, /* running status ("4=RUNNING") */
2273 #define psz_sdtprov p_sys->sdt_descriptors[i].psz_provider
2274 #define psz_sdtserv p_sys->sdt_descriptors[i].psz_service_name
2276 /* FIXME: Ineffecient malloc's & ugly code...... */
2277 if( ( psz_sdtprov != NULL ) && ( psz_sdtserv != NULL ) )
2279 psz_sdt_desc = malloc( 3 + strlen(psz_sdtprov)
2280 + strlen(psz_sdtserv) );
2283 psz_sdt_desc[0] = 0x01; /* digital television service */
2285 /* service provider name length */
2286 psz_sdt_desc[1] = (char)strlen(psz_sdtprov);
2287 memcpy( &psz_sdt_desc[2], psz_sdtprov, strlen(psz_sdtprov) );
2289 /* service name length */
2290 psz_sdt_desc[ 2 + strlen(psz_sdtprov) ]
2291 = (char)strlen(psz_sdtserv);
2292 memcpy( &psz_sdt_desc[3+strlen(psz_sdtprov)], psz_sdtserv,
2293 strlen(psz_sdtserv) );
2295 dvbpsi_SDTServiceAddDescriptor( p_service, 0x48,
2296 3 + strlen(psz_sdtprov) + strlen(psz_sdtserv),
2297 (uint8_t *)psz_sdt_desc );
2298 free( psz_sdt_desc );
2299 psz_sdt_desc = NULL;
2308 if( p_sys->i_mpeg4_streams > 0 )
2312 bits_buffer_t bits_fix_IOD;
2314 /* Make valgrind happy : it works at byte level not bit one so
2315 * bit_write confuse it (but DON'T CHANGE the way that bit_write is
2316 * working (needed when fixing some bits) */
2317 memset( iod, 0, 4096 );
2319 bits_initwrite( &bits, 4096, iod );
2320 /* IOD_label_scope */
2321 bits_write( &bits, 8, 0x11 );
2323 bits_write( &bits, 8, 0x01 );
2324 /* InitialObjectDescriptor */
2325 bits_align( &bits );
2326 bits_write( &bits, 8, 0x02 ); /* tag */
2327 bits_fix_IOD = bits; /* save states to fix length later */
2328 bits_write( &bits, 24,
2329 GetDescriptorLength24b( 0 ) ); /* variable length (fixed later) */
2330 bits_write( &bits, 10, 0x01 ); /* ObjectDescriptorID */
2331 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2332 bits_write( &bits, 1, 0x00 ); /* includeInlineProfileLevelFlag */
2333 bits_write( &bits, 4, 0x0f ); /* reserved */
2334 bits_write( &bits, 8, 0xff ); /* ODProfile (no ODcapability ) */
2335 bits_write( &bits, 8, 0xff ); /* sceneProfile */
2336 bits_write( &bits, 8, 0xfe ); /* audioProfile (unspecified) */
2337 bits_write( &bits, 8, 0xfe ); /* visualProfile( // ) */
2338 bits_write( &bits, 8, 0xff ); /* graphicProfile (no ) */
2339 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2341 ts_stream_t *p_stream;
2342 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
2344 if( p_stream->i_stream_id == 0xfa ||
2345 p_stream->i_stream_id == 0xfb ||
2346 p_stream->i_stream_id == 0xfe )
2348 bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
2350 bits_align( &bits );
2351 bits_write( &bits, 8, 0x03 ); /* ES_DescrTag */
2352 bits_fix_ESDescr = bits;
2353 bits_write( &bits, 24,
2354 GetDescriptorLength24b( 0 ) ); /* variable size */
2355 bits_write( &bits, 16, p_stream->i_es_id );
2356 bits_write( &bits, 1, 0x00 ); /* streamDependency */
2357 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2358 bits_write( &bits, 1, 0x00 ); /* OCRStreamFlag */
2359 bits_write( &bits, 5, 0x1f ); /* streamPriority */
2361 /* DecoderConfigDesciptor */
2362 bits_align( &bits );
2363 bits_write( &bits, 8, 0x04 ); /* DecoderConfigDescrTag */
2364 bits_fix_Decoder = bits;
2365 bits_write( &bits, 24, GetDescriptorLength24b( 0 ) );
2366 if( p_stream->i_stream_type == 0x10 )
2368 bits_write( &bits, 8, 0x20 ); /* Visual 14496-2 */
2369 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2371 else if( p_stream->i_stream_type == 0x1b )
2373 bits_write( &bits, 8, 0x21 ); /* Visual 14496-2 */
2374 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2376 else if( p_stream->i_stream_type == 0x11 || p_stream->i_stream_type == 0x0f )
2378 bits_write( &bits, 8, 0x40 ); /* Audio 14496-3 */
2379 bits_write( &bits, 6, 0x05 ); /* AudioStream */
2381 else if( p_stream->i_stream_type == 0x12 &&
2382 p_stream->i_codec == VLC_FOURCC('s','u','b','t') )
2384 bits_write( &bits, 8, 0x0B ); /* Text Stream */
2385 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2389 bits_write( &bits, 8, 0x00 );
2390 bits_write( &bits, 6, 0x00 );
2392 msg_Err( p_mux->p_sout,"Unsupported stream_type => "
2395 bits_write( &bits, 1, 0x00 ); /* UpStream */
2396 bits_write( &bits, 1, 0x01 ); /* reserved */
2397 bits_write( &bits, 24, 1024 * 1024 ); /* bufferSizeDB */
2398 bits_write( &bits, 32, 0x7fffffff ); /* maxBitrate */
2399 bits_write( &bits, 32, 0 ); /* avgBitrate */
2401 if( p_stream->i_decoder_specific_info > 0 )
2404 /* DecoderSpecificInfo */
2405 bits_align( &bits );
2406 bits_write( &bits, 8, 0x05 ); /* tag */
2407 bits_write( &bits, 24, GetDescriptorLength24b(
2408 p_stream->i_decoder_specific_info ) );
2409 for( i = 0; i < p_stream->i_decoder_specific_info; i++ )
2411 bits_write( &bits, 8,
2412 ((uint8_t*)p_stream->p_decoder_specific_info)[i] );
2415 /* fix Decoder length */
2416 bits_write( &bits_fix_Decoder, 24,
2417 GetDescriptorLength24b( bits.i_data -
2418 bits_fix_Decoder.i_data - 3 ) );
2420 /* SLConfigDescriptor : predefined (0x01) */
2421 bits_align( &bits );
2422 bits_write( &bits, 8, 0x06 ); /* tag */
2423 bits_write( &bits, 24, GetDescriptorLength24b( 8 ) );
2424 bits_write( &bits, 8, 0x01 );/* predefined */
2425 bits_write( &bits, 1, 0 ); /* durationFlag */
2426 bits_write( &bits, 32, 0 ); /* OCRResolution */
2427 bits_write( &bits, 8, 0 ); /* OCRLength */
2428 bits_write( &bits, 8, 0 ); /* InstantBitrateLength */
2429 bits_align( &bits );
2431 /* fix ESDescr length */
2432 bits_write( &bits_fix_ESDescr, 24,
2433 GetDescriptorLength24b( bits.i_data -
2434 bits_fix_ESDescr.i_data - 3 ) );
2437 bits_align( &bits );
2438 /* fix IOD length */
2439 bits_write( &bits_fix_IOD, 24,
2440 GetDescriptorLength24b( bits.i_data -
2441 bits_fix_IOD.i_data - 3 ) );
2443 #if 0//def HAVE_BSEARCH /* FIXME!!! This can't possibly work */
2444 i_pidinput = p_mux->pp_inputs[i]->p_fmt->i_id;
2445 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2446 sizeof(pmt_map_t), intcompare );
2447 p_usepid = bsearch( &p_usepid, p_sys->pmtmap, p_sys->i_num_pmt,
2448 sizeof(pmt_map_t), pmtcompare );
2449 if( p_usepid != NULL )
2450 dvbpsi_PMTAddDescriptor(
2451 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog], 0x1d,
2452 bits.i_data, bits.p_data );
2454 msg_Err( p_mux, "Received an unmapped PID" );
2456 dvbpsi_PMTAddDescriptor( &p_sys->dvbpmt[0], 0x1d, bits.i_data,
2461 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2463 ts_stream_t *p_stream;
2465 p_stream = (ts_stream_t *)p_mux->pp_inputs[i_stream]->p_sys;
2468 i_pidinput = p_mux->pp_inputs[i_stream]->p_fmt->i_id;
2469 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2470 sizeof(pmt_map_t), intcompare );
2472 if( p_usepid != NULL )
2473 p_es = dvbpsi_PMTAddES(
2474 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog],
2475 p_stream->i_stream_type, p_stream->i_pid );
2477 /* If there's an error somewhere, dump it to the first pmt */
2479 p_es = dvbpsi_PMTAddES( &p_sys->dvbpmt[0], p_stream->i_stream_type,
2482 if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
2487 es_id[0] = (p_stream->i_es_id >> 8)&0xff;
2488 es_id[1] = (p_stream->i_es_id)&0xff;
2489 dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
2491 else if( p_stream->i_stream_type == 0xa0 )
2494 int i_extra = __MIN( p_stream->i_decoder_specific_info, 502 );
2496 /* private DIV3 descripor */
2497 memcpy( &data[0], &p_stream->i_bih_codec, 4 );
2498 data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
2499 data[5] = ( p_stream->i_bih_width )&0xff;
2500 data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
2501 data[7] = ( p_stream->i_bih_height )&0xff;
2502 data[8] = ( i_extra >> 8 )&0xff;
2503 data[9] = ( i_extra )&0xff;
2506 memcpy( &data[10], p_stream->p_decoder_specific_info, i_extra );
2509 /* 0xa0 is private */
2510 dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra + 10, data );
2512 else if( p_stream->i_stream_type == 0x81 )
2514 uint8_t format[4] = { 0x41, 0x43, 0x2d, 0x33 };
2516 /* "registration" descriptor : "AC-3" */
2517 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
2519 else if( p_stream->i_codec == VLC_FOURCC('d','t','s',' ') )
2521 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2523 /* DTS format identifier, frame size 1024 - FIXME */
2524 uint8_t data[4] = { 0x44, 0x54, 0x53, 0x32 };
2525 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2527 else if( p_stream->i_codec == VLC_FOURCC('t','e','l','x') )
2529 if( p_stream->i_decoder_specific_info )
2531 dvbpsi_PMTESAddDescriptor( p_es, 0x56,
2532 p_stream->i_decoder_specific_info,
2533 p_stream->p_decoder_specific_info );
2537 else if( p_stream->i_codec == VLC_FOURCC('d','v','b','s') )
2540 if( p_stream->i_decoder_specific_info )
2542 /* pass-through from the TS demux */
2543 dvbpsi_PMTESAddDescriptor( p_es, 0x59,
2544 p_stream->i_decoder_specific_info,
2545 p_stream->p_decoder_specific_info );
2547 #ifdef _DVBPSI_DR_59_H_
2550 /* from the dvbsub transcoder */
2551 dvbpsi_subtitling_dr_t descr;
2552 dvbpsi_subtitle_t sub;
2553 dvbpsi_descriptor_t *p_descr;
2555 memcpy( sub.i_iso6392_language_code, p_stream->lang, 3 );
2556 sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
2557 sub.i_composition_page_id = p_stream->i_es_id & 0xFF;
2558 sub.i_ancillary_page_id = p_stream->i_es_id >> 16;
2560 descr.i_subtitles_number = 1;
2561 descr.p_subtitle[0] = sub;
2563 p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
2564 /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
2565 dvbpsi_PMTESAddDescriptor( p_es, p_descr->i_tag,
2566 p_descr->i_length, p_descr->p_data );
2568 #endif /* _DVBPSI_DR_59_H_ */
2572 if( p_stream->lang[0] != 0 )
2574 uint8_t data[4*p_stream->i_langs];
2576 /* I construct the content myself, way faster than looking at
2577 * over complicated/mind broken libdvbpsi way */
2578 for(i = 0; i < p_stream->i_langs; i++ )
2580 data[i*4+0] = p_stream->lang[i*3+0];
2581 data[i*4+1] = p_stream->lang[i*3+1];
2582 data[i*4+2] = p_stream->lang[i*3+2];
2583 data[i*4+3] = 0x00; /* audio type: 0x00 undefined */
2585 dvbpsi_PMTESAddDescriptor( p_es, 0x0a, 4*p_stream->i_langs, data );
2589 for( i = 0; i < p_sys->i_num_pmt; i++ )
2591 p_section[i] = dvbpsi_GenPMTSections( &p_sys->dvbpmt[i] );
2592 p_pmt[i] = WritePSISection( p_mux->p_sout, p_section[i] );
2593 PEStoTS( p_mux->p_sout, c, p_pmt[i], &p_sys->pmt[i] );
2594 dvbpsi_DeletePSISections( p_section[i] );
2595 dvbpsi_EmptyPMT( &p_sys->dvbpmt[i] );
2598 #ifdef HAVE_DVBPSI_SDT
2601 p_section2 = dvbpsi_GenSDTSections( &sdt );
2602 p_sdt = WritePSISection( p_mux->p_sout, p_section2 );
2603 PEStoTS( p_mux->p_sout, c, p_sdt, &p_sys->sdt );
2604 dvbpsi_DeletePSISections( p_section2 );
2605 dvbpsi_EmptySDT( &sdt );