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_common.h>
38 #include <vlc_plugin.h>
40 #include <vlc_block.h>
43 #include <vlc_iso_lang.h>
49 #ifdef HAVE_DVBPSI_DR_H
50 # include <dvbpsi/dvbpsi.h>
51 # include <dvbpsi/demux.h>
52 # include <dvbpsi/descriptor.h>
53 # include <dvbpsi/pat.h>
54 # include <dvbpsi/pmt.h>
55 # include <dvbpsi/sdt.h>
56 # include <dvbpsi/dr.h>
57 # include <dvbpsi/psi.h>
61 # include "descriptor.h"
62 # include "tables/pat.h"
63 # include "tables/pmt.h"
64 # include "tables/sdt.h"
65 # include "descriptors/dr.h"
71 * - check PCR frequency requirement
73 * - check PCR/PCR "soft"
74 * - check if "registration" descriptor : "AC-3" should be a program
75 * descriptor or an es one. (xine want an es one)
77 * - remove creation of PAT/PMT without dvbpsi
80 * - subtitle support is far from perfect. I expect some subtitles drop
81 * if they arrive a bit late
82 * (We cannot rely on the fact that the fifo should be full)
85 /*****************************************************************************
87 *****************************************************************************/
88 static int ChangeKeyCallback ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
89 static int ActiveKeyCallback ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
91 /*****************************************************************************
93 *****************************************************************************/
94 static int Open ( vlc_object_t * );
95 static void Close ( vlc_object_t * );
97 #define VPID_TEXT N_("Video PID")
98 #define VPID_LONGTEXT N_("Assign a fixed PID to the video stream. The PCR " \
99 "PID will automatically be the video.")
100 #define APID_TEXT N_("Audio PID")
101 #define APID_LONGTEXT N_("Assign a fixed PID to the audio stream.")
102 #define SPUPID_TEXT N_("SPU PID")
103 #define SPUPID_LONGTEXT N_("Assign a fixed PID to the SPU.")
104 #define PMTPID_TEXT N_("PMT PID")
105 #define PMTPID_LONGTEXT N_("Assign a fixed PID to the PMT")
106 #define TSID_TEXT N_("TS ID")
107 #define TSID_LONGTEXT N_("Assign a fixed Transport Stream ID.")
108 #define NETID_TEXT N_("NET ID")
109 #define NETID_LONGTEXT N_("Assign a fixed Network ID (for SDT table)")
111 #define PMTPROG_TEXT N_("PMT Program numbers")
112 #define PMTPROG_LONGTEXT N_("Assign a program number to each PMT. This " \
113 "requires \"Set PID to ID of ES\" to be enabled." )
115 #define MUXPMT_TEXT N_("Mux PMT (requires --sout-ts-es-id-pid)")
116 #define MUXPMT_LONGTEXT N_("Define the pids to add to each pmt. This " \
117 "requires \"Set PID to ID of ES\" to be enabled." )
119 #define SDTDESC_TEXT N_("SDT Descriptors (requires --sout-ts-es-id-pid)")
120 #define SDTDESC_LONGTEXT N_("Defines the descriptors of each SDT. This" \
121 "requires \"Set PID to ID of ES\" to be enabled." )
123 #define PID_TEXT N_("Set PID to ID of ES")
124 #define PID_LONGTEXT N_("Sets PID to the ID if the incoming ES. This is for " \
125 "use with --ts-es-id-pid, and allows to have the same PIDs in the input " \
126 "and output streams.")
128 #define ALIGNMENT_TEXT N_("Data alignment")
129 #define ALIGNMENT_LONGTEXT N_("Enforces alignment of all access units on " \
130 "PES boundaries. Disabling this might save some bandwidth but introduce incompatibilities.")
132 #define SHAPING_TEXT N_("Shaping delay (ms)")
133 #define SHAPING_LONGTEXT N_("Cut the " \
134 "stream in slices of the given duration, and ensure a constant bitrate " \
135 "between the two boundaries. This avoids having huge bitrate peaks, " \
136 "especially for reference frames." )
138 #define KEYF_TEXT N_("Use keyframes")
139 #define KEYF_LONGTEXT N_("If enabled, and shaping is specified, " \
140 "the TS muxer will place the boundaries at the end of I pictures. In " \
141 "that case, the shaping duration given by the user is a worse case " \
142 "used when no reference frame is available. This enhances the efficiency " \
143 "of the shaping algorithm, since I frames are usually the biggest " \
144 "frames in the stream.")
146 #define PCR_TEXT N_("PCR interval (ms)")
147 #define PCR_LONGTEXT N_("Set at which interval " \
148 "PCRs (Program Clock Reference) will be sent (in milliseconds). " \
149 "This value should be below 100ms. (default is 70ms).")
151 #define BMIN_TEXT N_( "Minimum B (deprecated)")
152 #define BMIN_LONGTEXT N_( "This setting is deprecated and not used anymore" )
154 #define BMAX_TEXT N_( "Maximum B (deprecated)")
155 #define BMAX_LONGTEXT N_( "This setting is deprecated and not used anymore")
157 #define DTS_TEXT N_("DTS delay (ms)")
158 #define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
159 "stamps) and PTS (presentation timestamps) of the data in the " \
160 "stream, compared to the PCRs. This allows for some buffering inside " \
161 "the client decoder.")
163 #define ACRYPT_TEXT N_("Crypt audio")
164 #define ACRYPT_LONGTEXT N_("Crypt audio using CSA")
165 #define VCRYPT_TEXT N_("Crypt video")
166 #define VCRYPT_LONGTEXT N_("Crypt video using CSA")
168 #define CK_TEXT N_("CSA Key")
169 #define CK_LONGTEXT N_("CSA encryption key. This must be a " \
170 "16 char string (8 hexadecimal bytes).")
172 #define CK2_TEXT N_("Second CSA Key")
173 #define CK2_LONGTEXT N_("The even CSA encryption key. This must be a " \
174 "16 char string (8 hexadecimal bytes).")
176 #define CU_TEXT N_("CSA Key in use")
177 #define CU_LONGTEXT N_("CSA encryption key used. It can be the odd/first/1 " \
178 "(default) or the even/second/2 one.")
180 #define CPKT_TEXT N_("Packet size in bytes to encrypt")
181 #define CPKT_LONGTEXT N_("Size of the TS packet to encrypt. " \
182 "The encryption routines subtract the TS-header from the value before " \
185 #define SOUT_CFG_PREFIX "sout-ts-"
186 #define MAX_PMT 64 /* Maximum number of programs. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
187 #define MAX_PMT_PID 64 /* Maximum pids in each pmt. FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
190 set_description( N_("TS muxer (libdvbpsi)") )
191 set_shortname( "MPEG-TS")
192 set_category( CAT_SOUT )
193 set_subcategory( SUBCAT_SOUT_MUX )
194 set_capability( "sout mux", 120 )
197 add_integer( SOUT_CFG_PREFIX "pid-video", 0, NULL,VPID_TEXT, VPID_LONGTEXT,
199 add_integer( SOUT_CFG_PREFIX "pid-audio", 0, NULL, APID_TEXT,
200 APID_LONGTEXT, true )
201 add_integer( SOUT_CFG_PREFIX "pid-spu", 0, NULL, SPUPID_TEXT,
202 SPUPID_LONGTEXT, true )
203 add_integer( SOUT_CFG_PREFIX "pid-pmt", 0, NULL, PMTPID_TEXT,
204 PMTPID_LONGTEXT, true )
205 add_integer( SOUT_CFG_PREFIX "tsid", 0, NULL, TSID_TEXT,
206 TSID_LONGTEXT, true )
207 #ifdef HAVE_DVBPSI_SDT
208 add_integer( SOUT_CFG_PREFIX "netid", 0, NULL, NETID_TEXT,
209 NETID_LONGTEXT, true )
211 add_string( SOUT_CFG_PREFIX "program-pmt", NULL, NULL, PMTPROG_TEXT,
212 PMTPROG_LONGTEXT, true )
213 add_bool( SOUT_CFG_PREFIX "es-id-pid", false, NULL, PID_TEXT, PID_LONGTEXT,
215 add_string( SOUT_CFG_PREFIX "muxpmt", NULL, NULL, MUXPMT_TEXT, MUXPMT_LONGTEXT, true )
216 #ifdef HAVE_DVBPSI_SDT
217 add_string( SOUT_CFG_PREFIX "sdtdesc", NULL, NULL, SDTDESC_TEXT, SDTDESC_LONGTEXT, true )
219 add_bool( SOUT_CFG_PREFIX "alignment", true, NULL, ALIGNMENT_TEXT,
220 ALIGNMENT_LONGTEXT, true )
222 add_integer( SOUT_CFG_PREFIX "shaping", 200, NULL, SHAPING_TEXT,
223 SHAPING_LONGTEXT, true )
224 add_bool( SOUT_CFG_PREFIX "use-key-frames", false, NULL, KEYF_TEXT,
225 KEYF_LONGTEXT, true )
227 add_integer( SOUT_CFG_PREFIX "pcr", 70, NULL, PCR_TEXT, PCR_LONGTEXT,
229 add_integer( SOUT_CFG_PREFIX "bmin", 0, NULL, BMIN_TEXT, BMIN_LONGTEXT,
231 add_integer( SOUT_CFG_PREFIX "bmax", 0, NULL, BMAX_TEXT, BMAX_LONGTEXT,
233 add_integer( SOUT_CFG_PREFIX "dts-delay", 400, NULL, DTS_TEXT,
236 add_bool( SOUT_CFG_PREFIX "crypt-audio", true, NULL, ACRYPT_TEXT,
237 ACRYPT_LONGTEXT, true )
238 add_bool( SOUT_CFG_PREFIX "crypt-video", true, NULL, VCRYPT_TEXT,
239 VCRYPT_LONGTEXT, true )
241 add_string( SOUT_CFG_PREFIX "csa-ck", NULL, NULL, CK_TEXT, CK_LONGTEXT,
243 add_string( SOUT_CFG_PREFIX "csa2-ck", NULL, NULL, CK2_TEXT, CK2_LONGTEXT,
245 add_string( SOUT_CFG_PREFIX "csa-use", "1", NULL, CU_TEXT, CU_LONGTEXT,
247 add_integer( SOUT_CFG_PREFIX "csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, true )
249 set_callbacks( Open, Close )
252 /*****************************************************************************
253 * Local data structures
254 *****************************************************************************/
255 static const char *const ppsz_sout_options[] = {
256 "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid", "netid",
257 "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
258 "dts-delay", "csa-ck", "csa2-ck", "csa-use", "csa-pkt", "crypt-audio", "crypt-video",
259 "muxpmt", "sdtdesc", "program-pmt", "alignment",
263 typedef struct pmt_map_t /* Holds the mapping between the pmt-pid/pmt table */
266 unsigned long i_prog;
269 typedef struct sdt_desc_t
272 char *psz_service_name; /* name of program */
280 } sout_buffer_chain_t;
282 static inline void BufferChainInit ( sout_buffer_chain_t *c )
286 c->pp_last = &c->p_first;
289 static inline void BufferChainAppend( sout_buffer_chain_t *c, block_t *b )
299 c->pp_last = &b->p_next;
302 static inline block_t *BufferChainGet( sout_buffer_chain_t *c )
304 block_t *b = c->p_first;
309 c->p_first = b->p_next;
311 if( c->p_first == NULL )
313 c->pp_last = &c->p_first;
321 static inline block_t *BufferChainPeek( sout_buffer_chain_t *c )
323 block_t *b = c->p_first;
328 static inline void BufferChainClean( sout_buffer_chain_t *c )
332 while( ( b = BufferChainGet( c ) ) )
336 BufferChainInit( c );
339 typedef struct ts_stream_t
342 vlc_fourcc_t i_codec;
346 int i_continuity_counter;
347 bool b_discontinuity;
349 /* to be used for carriege of DIV3 */
350 vlc_fourcc_t i_bih_codec;
351 int i_bih_width, i_bih_height;
353 /* Specific to mpeg4 in mpeg2ts */
356 int i_decoder_specific_info;
357 uint8_t *p_decoder_specific_info;
359 /* language is iso639-2T */
363 sout_buffer_chain_t chain_pes;
365 mtime_t i_pes_length;
371 struct sout_mux_sys_t
374 sout_input_t *p_pcr_input;
376 vlc_mutex_t csa_lock;
386 int i_pid_free; /* first usable pid */
392 int i_pat_version_number;
395 int i_pmt_version_number;
396 ts_stream_t pmt[MAX_PMT];
397 pmt_map_t pmtmap[MAX_PMT_PID];
398 int i_pmt_program_number[MAX_PMT];
399 sdt_desc_t sdt_descriptors[MAX_PMT];
400 bool b_data_alignment;
404 int i_null_continuity_counter; /* Needed ? */
406 dvbpsi_pmt_t *dvbpmt;
408 /* for TS building */
409 int64_t i_bitrate_min;
410 int64_t i_bitrate_max;
412 int64_t i_shaping_delay;
417 bool b_use_key_frames;
419 mtime_t i_pcr; /* last PCR emited */
427 /* Reserve a pid and return it */
428 static int AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
431 if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
433 i_pid = p_sys->i_pid_video;
434 p_sys->i_pid_video = 0;
436 else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
438 i_pid = p_sys->i_pid_audio;
439 p_sys->i_pid_audio = 0;
441 else if ( i_cat == SPU_ES && p_sys->i_pid_spu )
443 i_pid = p_sys->i_pid_spu;
444 p_sys->i_pid_spu = 0;
448 i_pid = ++p_sys->i_pid_free;
453 static int pmtcompare( const void *pa, const void *pb )
455 if ( ((pmt_map_t *)pa)->i_pid < ((pmt_map_t *)pb)->i_pid )
457 else if ( ((pmt_map_t *)pa)->i_pid > ((pmt_map_t *)pb)->i_pid )
463 static int intcompare( const void *pa, const void *pb )
465 if ( *(int *)pa < *(int *)pb )
467 else if ( *(int *)pa > *(int *)pb )
473 /*****************************************************************************
475 *****************************************************************************/
476 static int Control ( sout_mux_t *, int, va_list );
477 static int AddStream( sout_mux_t *, sout_input_t * );
478 static int DelStream( sout_mux_t *, sout_input_t * );
479 static int Mux ( sout_mux_t * );
481 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo );
482 static block_t *Add_ADTS( block_t *, es_format_t * );
483 static void TSSchedule ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
484 mtime_t i_pcr_length, mtime_t i_pcr_dts );
485 static void TSDate ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
486 mtime_t i_pcr_length, mtime_t i_pcr_dts );
487 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
488 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
490 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, bool b_pcr );
491 static void TSSetPCR( block_t *p_ts, mtime_t i_dts );
493 static void PEStoTS ( sout_instance_t *, sout_buffer_chain_t *, block_t *, ts_stream_t * );
495 /*****************************************************************************
497 *****************************************************************************/
498 static int Open( vlc_object_t *p_this )
500 sout_mux_t *p_mux =(sout_mux_t*)p_this;
501 sout_mux_sys_t *p_sys = NULL;
505 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
507 p_sys = malloc( sizeof( sout_mux_sys_t ) );
510 p_sys->i_pmtslots = p_sys->b_sdt = 0;
511 p_sys->i_num_pmt = 1;
512 p_sys->dvbpmt = NULL;
513 memset( &p_sys->pmtmap, 0, sizeof(p_sys->pmtmap) );
515 vlc_mutex_init( &p_sys->csa_lock );
517 p_mux->pf_control = Control;
518 p_mux->pf_addstream = AddStream;
519 p_mux->pf_delstream = DelStream;
521 p_mux->p_sys = p_sys;
523 for ( i = 0; i < MAX_PMT; i++ )
524 p_sys->sdt_descriptors[i].psz_service_name
525 = p_sys->sdt_descriptors[i].psz_provider = NULL;
526 memset( p_sys->sdt_descriptors, 0, sizeof(sdt_desc_t) );
528 p_sys->i_audio_bound = 0;
529 p_sys->i_video_bound = 0;
531 p_sys->b_es_id_pid = var_GetBool( p_mux, SOUT_CFG_PREFIX "es-id-pid" );
533 var_Get( p_mux, SOUT_CFG_PREFIX "muxpmt", &val );
535 fetch string of pmts. Here's a sample: --sout-ts-muxpmt="0x451,0x200,0x28a,0x240,,0x450,0x201,0x28b,0x241,,0x452,0x202,0x28c,0x242"
536 This would mean 0x451, 0x200, 0x28a, 0x240 would fall under one pmt (program), 0x450,0x201,0x28b,0x241 would fall under another
538 if( val.psz_string != NULL && *val.psz_string )
541 char *psz = val.psz_string;
547 i_pid = strtoul( psz, &psz_next, 0 );
549 if ( strlen(psz_next) > 0 )
554 if ( p_sys->i_num_pmt > MAX_PMT )
557 "Number of PMTs greater than compiled maximum (%d)", MAX_PMT );
558 p_sys->i_num_pmt = MAX_PMT;
563 p_sys->pmtmap[p_sys->i_pmtslots].i_pid = i_pid;
564 p_sys->pmtmap[p_sys->i_pmtslots].i_prog = p_sys->i_num_pmt - 1;
566 if ( p_sys->i_pmtslots > MAX_PMT_PID )
569 "Number of pids in PMT greater than compiled maximum (%d)",
571 p_sys->i_pmtslots = MAX_PMT_PID;
575 /* Now sort according to pids for fast search later on */
576 qsort( (void *)p_sys->pmtmap, p_sys->i_pmtslots,
577 sizeof(pmt_map_t), &pmtcompare );
582 free( val.psz_string );
584 unsigned short subi[3];
585 vlc_rand_bytes(subi, sizeof(subi));
586 p_sys->i_pat_version_number = nrand48(subi) & 0x1f;
587 p_sys->pat.i_pid = 0;
588 p_sys->pat.i_continuity_counter = 0;
589 p_sys->pat.b_discontinuity = false;
591 var_Get( p_mux, SOUT_CFG_PREFIX "tsid", &val );
593 p_sys->i_tsid = val.i_int;
595 p_sys->i_tsid = nrand48(subi) & 0xffff;
597 p_sys->i_netid = nrand48(subi) & 0xffff;
598 #ifdef HAVE_DVBPSI_SDT
599 var_Get( p_mux, SOUT_CFG_PREFIX "netid", &val );
601 p_sys->i_netid = val.i_int;
604 p_sys->i_pmt_version_number = nrand48(subi) & 0x1f;
605 for( i = 0; i < p_sys->i_num_pmt; i++ )
607 p_sys->pmt[i].i_continuity_counter = 0;
608 p_sys->pmt[i].b_discontinuity = false;
611 p_sys->sdt.i_pid = 0x11;
612 p_sys->sdt.i_continuity_counter = 0;
613 p_sys->sdt.b_discontinuity = false;
615 #ifdef HAVE_DVBPSI_SDT
616 var_Get( p_mux, SOUT_CFG_PREFIX "sdtdesc", &val );
617 p_sys->b_sdt = val.psz_string && *val.psz_string ? true : false;
619 /* Syntax is provider_sdt1,service_name_sdt1,provider_sdt2,service_name_sdt2... */
623 char *psz = val.psz_string;
624 char *psz_sdttoken = psz;
627 while ( psz_sdttoken != NULL )
629 char *psz_end = strchr( psz_sdttoken, ',' );
630 if( psz_end != NULL )
636 p_sys->sdt_descriptors[i/2].psz_provider
637 = strdup(psz_sdttoken);
641 p_sys->sdt_descriptors[i/2].psz_service_name
642 = strdup(psz_sdttoken);
646 psz_sdttoken = psz_end;
649 free( val.psz_string );
651 p_sys->b_sdt = false;
654 p_sys->b_data_alignment = var_GetBool( p_mux, SOUT_CFG_PREFIX "alignment" );
656 var_Get( p_mux, SOUT_CFG_PREFIX "program-pmt", &val );
657 if( val.psz_string && *val.psz_string )
660 char *psz = val.psz_string;
665 while ( psz != NULL )
667 i_pid = strtoul( psz, &psz_next, 0 );
668 if( strlen(psz_next) > 0 )
676 msg_Err( p_mux, "Number of PMTs > maximum (%d)",
681 p_sys->i_pmt_program_number[i] = i_pid;
688 /* Option not specified, use 1, 2, 3... */
689 for( i = 0; i < p_sys->i_num_pmt; i++ )
690 p_sys->i_pmt_program_number[i] = i + 1;
692 free( val.psz_string );
694 var_Get( p_mux, SOUT_CFG_PREFIX "pid-pmt", &val );
697 for( i = 0; i < p_sys->i_num_pmt; i++ )
698 p_sys->pmt[i].i_pid = val.i_int + i; /* Does this make any sense? */
702 for( i = 0; i < p_sys->i_num_pmt; i++ )
703 p_sys->pmt[i].i_pid = 0x42 + i;
706 p_sys->i_pid_free = p_sys->pmt[p_sys->i_num_pmt - 1].i_pid + 1;
708 p_sys->i_pid_video = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-video" );
709 if ( p_sys->i_pid_video > p_sys->i_pid_free )
711 p_sys->i_pid_free = p_sys->i_pid_video + 1;
714 p_sys->i_pid_audio = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-audio" );
715 if ( p_sys->i_pid_audio > p_sys->i_pid_free )
717 p_sys->i_pid_free = p_sys->i_pid_audio + 1;
720 p_sys->i_pid_spu = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-spu" );
721 if ( p_sys->i_pid_spu > p_sys->i_pid_free )
723 p_sys->i_pid_free = p_sys->i_pid_spu + 1;
726 p_sys->i_pcr_pid = 0x1fff;
727 p_sys->p_pcr_input = NULL;
729 p_sys->i_mpeg4_streams = 0;
731 p_sys->i_null_continuity_counter = 0;
733 /* Allow to create constrained stream */
734 p_sys->i_bitrate_min = var_GetInteger( p_mux, SOUT_CFG_PREFIX "bmin" );
736 p_sys->i_bitrate_max = var_GetInteger( p_mux, SOUT_CFG_PREFIX "bmax" );
738 if( p_sys->i_bitrate_min > 0 && p_sys->i_bitrate_max > 0 &&
739 p_sys->i_bitrate_min > p_sys->i_bitrate_max )
741 msg_Err( p_mux, "incompatible minimum and maximum bitrate, "
742 "disabling bitrate control" );
743 p_sys->i_bitrate_min = 0;
744 p_sys->i_bitrate_max = 0;
746 if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
748 msg_Err( p_mux, "bmin and bmax no more supported "
749 "(if you need them report it)" );
752 var_Get( p_mux, SOUT_CFG_PREFIX "shaping", &val );
753 p_sys->i_shaping_delay = (int64_t)val.i_int * 1000;
754 if( p_sys->i_shaping_delay <= 0 )
757 "invalid shaping (%"PRId64"ms) resetting to 200ms",
758 p_sys->i_shaping_delay / 1000 );
759 p_sys->i_shaping_delay = 200000;
762 var_Get( p_mux, SOUT_CFG_PREFIX "pcr", &val );
763 p_sys->i_pcr_delay = (int64_t)val.i_int * 1000;
764 if( p_sys->i_pcr_delay <= 0 ||
765 p_sys->i_pcr_delay >= p_sys->i_shaping_delay )
768 "invalid pcr delay (%"PRId64"ms) resetting to 70ms",
769 p_sys->i_pcr_delay / 1000 );
770 p_sys->i_pcr_delay = 70000;
773 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
774 p_sys->i_dts_delay = (int64_t)val.i_int * 1000;
776 msg_Dbg( p_mux, "shaping=%"PRId64" pcr=%"PRId64" dts_delay=%"PRId64,
777 p_sys->i_shaping_delay, p_sys->i_pcr_delay, p_sys->i_dts_delay );
779 p_sys->b_use_key_frames = var_GetBool( p_mux, SOUT_CFG_PREFIX "use-key-frames" );
781 /* for TS generation */
785 var_Create( p_mux, SOUT_CFG_PREFIX "csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
786 var_Get( p_mux, SOUT_CFG_PREFIX "csa-ck", &val );
787 if( val.psz_string && *val.psz_string )
792 p_sys->csa = csa_New();
794 var_Create( p_mux, SOUT_CFG_PREFIX "csa2-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
795 var_Get( p_mux, SOUT_CFG_PREFIX "csa2-ck", &csa2 );
796 i_res = csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, true );
797 if( i_res == VLC_SUCCESS && csa2.psz_string && *csa2.psz_string )
799 if( csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, csa2.psz_string, false ) != VLC_SUCCESS )
801 csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, false );
804 else if( i_res == VLC_SUCCESS )
806 csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, false );
810 csa_Delete( p_sys->csa );
816 vlc_value_t use_val, pkt_val;
818 var_Create( p_mux, SOUT_CFG_PREFIX "csa-use", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
819 var_Get( p_mux, SOUT_CFG_PREFIX "csa-use", &use_val );
820 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
821 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, (void *)1 );
822 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
824 if ( var_Set( p_mux, SOUT_CFG_PREFIX "csa-use", use_val ) != VLC_SUCCESS )
826 var_SetString( p_mux, SOUT_CFG_PREFIX "csa-use", "odd" );
828 free( use_val.psz_string );
830 var_Get( p_mux, SOUT_CFG_PREFIX "csa-pkt", &pkt_val );
831 if( pkt_val.i_int < 12 || pkt_val.i_int > 188 )
833 msg_Err( p_mux, "wrong packet size %"PRId64" specified.",
835 msg_Warn( p_mux, "using default packet size of 188 bytes" );
836 p_sys->i_csa_pkt_size = 188;
838 else p_sys->i_csa_pkt_size = pkt_val.i_int;
839 msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
841 free( csa2.psz_string );
843 free( val.psz_string );
845 p_sys->b_crypt_audio = var_GetBool( p_mux, SOUT_CFG_PREFIX "crypt-audio" );
847 p_sys->b_crypt_video = var_GetBool( p_mux, SOUT_CFG_PREFIX "crypt-video" );
852 /*****************************************************************************
854 *****************************************************************************/
855 static void Close( vlc_object_t * p_this )
857 sout_mux_t *p_mux = (sout_mux_t*)p_this;
858 sout_mux_sys_t *p_sys = p_mux->p_sys;
863 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, NULL );
864 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
865 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
866 csa_Delete( p_sys->csa );
869 for( i = 0; i < MAX_PMT; i++ )
871 free( p_sys->sdt_descriptors[i].psz_service_name );
872 free( p_sys->sdt_descriptors[i].psz_provider );
875 vlc_mutex_destroy( &p_sys->csa_lock );
876 free( p_sys->dvbpmt );
880 /*****************************************************************************
881 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
882 *****************************************************************************/
883 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
884 vlc_value_t oldval, vlc_value_t newval,
887 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
888 sout_mux_t *p_mux = (sout_mux_t*)p_this;
889 sout_mux_sys_t *p_sys = p_mux->p_sys;
892 vlc_mutex_lock( &p_sys->csa_lock );
893 ret = csa_SetCW( p_this, p_sys->csa, newval.psz_string,
894 !!(intptr_t)p_data );
895 vlc_mutex_unlock( &p_sys->csa_lock );
900 /*****************************************************************************
901 * ActiveKeyCallback: called when changing the active (in use) encryption key on the fly.
902 *****************************************************************************/
903 static int ActiveKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
904 vlc_value_t oldval, vlc_value_t newval,
907 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
908 sout_mux_t *p_mux = (sout_mux_t*)p_this;
909 sout_mux_sys_t *p_sys = p_mux->p_sys;
910 int i_res = VLC_EBADVAR;
912 vlc_mutex_lock( &p_sys->csa_lock );
913 if( !strcmp(newval.psz_string, "odd" ) || !strcmp(newval.psz_string, "first" ) || !strcmp(newval.psz_string, "1" ) )
915 i_res = csa_UseKey( (vlc_object_t*)p_mux, p_sys->csa, 1 );
917 else if( !strcmp(newval.psz_string, "even" ) || !strcmp(newval.psz_string, "second" ) || !strcmp(newval.psz_string, "2" ) )
919 i_res = csa_UseKey( (vlc_object_t*)p_mux, p_sys->csa, 0 );
921 vlc_mutex_unlock( &p_sys->csa_lock );
926 /*****************************************************************************
928 *****************************************************************************/
929 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
937 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
938 pb_bool = (bool*)va_arg( args, bool * );
942 case MUX_GET_ADD_STREAM_WAIT:
943 pb_bool = (bool*)va_arg( args, bool * );
948 ppsz = (char**)va_arg( args, char ** );
949 *ppsz = strdup( "video/mpeg" ); /* FIXME not sure */
957 /*****************************************************************************
958 * AddStream: called for each stream addition
959 *****************************************************************************/
960 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
962 sout_mux_sys_t *p_sys = p_mux->p_sys;
963 ts_stream_t *p_stream;
966 p_input->p_sys = p_stream = malloc( sizeof( ts_stream_t ) );
967 if( !p_input->p_sys )
970 /* Init this new stream */
971 if ( p_sys->b_es_id_pid )
972 p_stream->i_pid = p_input->p_fmt->i_id & 0x1fff;
974 p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
975 p_stream->i_codec = p_input->p_fmt->i_codec;
976 p_stream->i_continuity_counter = 0;
977 p_stream->b_discontinuity = false;
978 p_stream->i_decoder_specific_info = 0;
979 p_stream->p_decoder_specific_info = NULL;
981 msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d",
982 (char*)&p_input->p_fmt->i_codec, p_stream->i_pid );
984 /* All others fields depand on codec */
985 switch( p_input->p_fmt->i_cat )
988 switch( p_input->p_fmt->i_codec )
991 /* TODO: do we need to check MPEG-I/II ? */
992 p_stream->i_stream_type = 0x02;
993 p_stream->i_stream_id = 0xe0;
996 p_stream->i_stream_type = 0x10;
997 p_stream->i_stream_id = 0xe0;
998 p_stream->i_es_id = p_stream->i_pid;
1000 case VLC_CODEC_H264:
1001 p_stream->i_stream_type = 0x1b;
1002 p_stream->i_stream_id = 0xe0;
1004 /* XXX dirty dirty but somebody want that:
1005 * using crapy MS-codec XXX */
1006 /* I didn't want to do that :P */
1007 case VLC_CODEC_H263I:
1008 case VLC_CODEC_H263:
1009 case VLC_CODEC_WMV3:
1010 case VLC_CODEC_WMV2:
1011 case VLC_CODEC_WMV1:
1012 case VLC_CODEC_DIV3:
1013 case VLC_CODEC_DIV2:
1014 case VLC_CODEC_DIV1:
1015 case VLC_CODEC_MJPG:
1016 p_stream->i_stream_type = 0xa0; /* private */
1017 p_stream->i_stream_id = 0xa0; /* beurk */
1018 p_stream->i_bih_codec = p_input->p_fmt->i_codec;
1019 p_stream->i_bih_width = p_input->p_fmt->video.i_width;
1020 p_stream->i_bih_height = p_input->p_fmt->video.i_height;
1022 case VLC_CODEC_DIRAC:
1023 /* stream_id makes use of stream_id_extension */
1024 p_stream->i_stream_id = (PES_EXTENDED_STREAM_ID << 8) | 0x60;
1025 p_stream->i_stream_type = 0xd1;
1029 return VLC_EGENERIC;
1031 p_sys->i_video_bound++;
1035 switch( p_input->p_fmt->i_codec )
1037 case VLC_CODEC_MPGA:
1038 p_stream->i_stream_type =
1039 p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
1040 p_stream->i_stream_id = 0xc0;
1043 p_stream->i_stream_type = 0x81;
1044 p_stream->i_stream_id = 0xbd;
1046 case VLC_CODEC_EAC3:
1047 p_stream->i_stream_type = 0x06;
1048 p_stream->i_stream_id = 0xbd;
1050 case VLC_CODEC_DVD_LPCM:
1051 p_stream->i_stream_type = 0x83;
1052 p_stream->i_stream_id = 0xbd;
1055 p_stream->i_stream_type = 0x06;
1056 p_stream->i_stream_id = 0xbd;
1058 case VLC_CODEC_MP4A:
1059 /* XXX: make that configurable in some way when LOAS
1060 * is implemented for AAC in TS */
1061 //p_stream->i_stream_type = 0x11; /* LOAS/LATM */
1062 p_stream->i_stream_type = 0x0f; /* ADTS */
1063 p_stream->i_stream_id = 0xc0;
1064 p_sys->i_mpeg4_streams++;
1065 p_stream->i_es_id = p_stream->i_pid;
1069 return VLC_EGENERIC;
1071 p_sys->i_audio_bound++;
1075 switch( p_input->p_fmt->i_codec )
1078 p_stream->i_stream_type = 0x82;
1079 p_stream->i_stream_id = 0xbd;
1081 case VLC_CODEC_SUBT:
1082 p_stream->i_stream_type = 0x12;
1083 p_stream->i_stream_id = 0xfa;
1084 p_sys->i_mpeg4_streams++;
1085 p_stream->i_es_id = p_stream->i_pid;
1087 case VLC_CODEC_DVBS:
1088 p_stream->i_stream_type = 0x06;
1089 p_stream->i_es_id = p_input->p_fmt->subs.dvb.i_id;
1090 p_stream->i_stream_id = 0xbd;
1092 case VLC_CODEC_TELETEXT:
1093 p_stream->i_stream_type = 0x06;
1094 p_stream->i_stream_id = 0xbd; /* FIXME */
1098 return VLC_EGENERIC;
1104 return VLC_EGENERIC;
1107 p_stream->i_langs = 1+p_input->p_fmt->i_extra_languages;
1108 p_stream->lang = malloc(p_stream->i_langs*3);
1109 if( !p_stream->lang )
1111 p_stream->i_langs = 0;
1118 p_stream->lang[2] = '\0';
1119 if( p_input->p_fmt->psz_language )
1121 char *psz = p_input->p_fmt->psz_language;
1122 const iso639_lang_t *pl = NULL;
1124 if( strlen( psz ) == 2 )
1126 pl = GetLang_1( psz );
1128 else if( strlen( psz ) == 3 )
1130 pl = GetLang_2B( psz );
1131 if( !strcmp( pl->psz_iso639_1, "??" ) )
1133 pl = GetLang_2T( psz );
1136 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1138 p_stream->lang[0] = pl->psz_iso639_2T[0];
1139 p_stream->lang[1] = pl->psz_iso639_2T[1];
1140 p_stream->lang[2] = pl->psz_iso639_2T[2];
1142 msg_Dbg( p_mux, " - lang=%c%c%c",
1143 p_stream->lang[0], p_stream->lang[1],
1144 p_stream->lang[2] );
1147 while( i < p_stream->i_langs ) {
1148 if( p_input->p_fmt->p_extra_languages[i-1].psz_language )
1150 char *psz = p_input->p_fmt->p_extra_languages[i-1].psz_language;
1151 const iso639_lang_t *pl = NULL;
1153 if( strlen( psz ) == 2 )
1155 pl = GetLang_1( psz );
1157 else if( strlen( psz ) == 3 )
1159 pl = GetLang_2B( psz );
1160 if( !strcmp( pl->psz_iso639_1, "??" ) )
1162 pl = GetLang_2T( psz );
1165 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1167 p_stream->lang[i*3+0] = pl->psz_iso639_2T[0];
1168 p_stream->lang[i*3+1] = pl->psz_iso639_2T[1];
1169 p_stream->lang[i*3+2] = pl->psz_iso639_2T[2];
1171 msg_Dbg( p_mux, " - lang=%c%c%c",
1172 p_stream->lang[i*3+0], p_stream->lang[i*3+1],
1173 p_stream->lang[i*3+2] );
1179 /* Create decoder specific info for subt */
1180 if( p_stream->i_codec == VLC_CODEC_SUBT )
1184 p_stream->i_decoder_specific_info = 55;
1185 p_stream->p_decoder_specific_info = p =
1186 malloc( p_stream->i_decoder_specific_info );
1189 p[0] = 0x10; /* textFormat, 0x10 for 3GPP TS 26.245 */
1190 p[1] = 0x00; /* flags: 1b: associated video info flag
1194 p[2] = 52; /* remaining size */
1198 p[0] = p[1] = p[2] = p[3] = 0; p+=4; /* display flags */
1199 *p++ = 0; /* horizontal justification (-1: left, 0 center, 1 right) */
1200 *p++ = 1; /* vertical justification (-1: top, 0 center, 1 bottom) */
1202 p[0] = p[1] = p[2] = 0x00; p+=3;/* background rgb */
1203 *p++ = 0xff; /* background a */
1205 p[0] = p[1] = 0; p += 2; /* text box top */
1206 p[0] = p[1] = 0; p += 2; /* text box left */
1207 p[0] = p[1] = 0; p += 2; /* text box bottom */
1208 p[0] = p[1] = 0; p += 2; /* text box right */
1210 p[0] = p[1] = 0; p += 2; /* start char */
1211 p[0] = p[1] = 0; p += 2; /* end char */
1212 p[0] = p[1] = 0; p += 2; /* default font id */
1214 *p++ = 0; /* font style flags */
1215 *p++ = 12; /* font size */
1217 p[0] = p[1] = p[2] = 0x00; p+=3;/* foreground rgb */
1218 *p++ = 0x00; /* foreground a */
1220 p[0] = p[1] = p[2] = 0; p[3] = 22; p += 4;
1221 memcpy( p, "ftab", 4 ); p += 4;
1222 *p++ = 0; *p++ = 1; /* entry count */
1223 p[0] = p[1] = 0; p += 2; /* font id */
1224 *p++ = 9; /* font name length */
1225 memcpy( p, "Helvetica", 9 ); /* font name */
1227 else p_stream->i_decoder_specific_info = 0;
1231 /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
1232 p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra;
1233 if( p_stream->i_decoder_specific_info > 0 )
1235 p_stream->p_decoder_specific_info =
1236 malloc( p_stream->i_decoder_specific_info );
1237 if( p_stream->p_decoder_specific_info )
1239 memcpy( p_stream->p_decoder_specific_info,
1240 p_input->p_fmt->p_extra,
1241 p_input->p_fmt->i_extra );
1243 else p_stream->i_decoder_specific_info = 0;
1247 /* Init pes chain */
1248 BufferChainInit( &p_stream->chain_pes );
1249 p_stream->i_pes_dts = 0;
1250 p_stream->i_pes_length = 0;
1251 p_stream->i_pes_used = 0;
1252 p_stream->b_key_frame = 0;
1254 /* We only change PMT version (PAT isn't changed) */
1255 p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
1257 /* Update pcr_pid */
1258 if( p_input->p_fmt->i_cat != SPU_ES &&
1259 ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
1261 if( p_sys->p_pcr_input )
1263 /* There was already a PCR stream, so clean context */
1266 p_sys->i_pcr_pid = p_stream->i_pid;
1267 p_sys->p_pcr_input = p_input;
1269 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1275 /*****************************************************************************
1276 * DelStream: called before a stream deletion
1277 *****************************************************************************/
1278 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1280 sout_mux_sys_t *p_sys = p_mux->p_sys;
1281 ts_stream_t *p_stream;
1284 p_stream = (ts_stream_t*)p_input->p_sys;
1285 msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
1287 if( p_sys->i_pcr_pid == p_stream->i_pid )
1291 /* Find a new pcr stream (Prefer Video Stream) */
1292 p_sys->i_pcr_pid = 0x1fff;
1293 p_sys->p_pcr_input = NULL;
1294 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1296 if( p_mux->pp_inputs[i] == p_input )
1301 if( p_mux->pp_inputs[i]->p_fmt->i_cat == VIDEO_ES )
1304 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1305 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1308 else if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES &&
1309 p_sys->i_pcr_pid == 0x1fff )
1312 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1313 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1316 if( p_sys->p_pcr_input )
1318 /* Empty TS buffer */
1321 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1324 /* Empty all data in chain_pes */
1325 BufferChainClean( &p_stream->chain_pes );
1327 free(p_stream->lang);
1328 free( p_stream->p_decoder_specific_info );
1329 if( p_stream->i_stream_id == 0xfa ||
1330 p_stream->i_stream_id == 0xfb ||
1331 p_stream->i_stream_id == 0xfe )
1333 p_sys->i_mpeg4_streams--;
1336 var_Get( p_mux, SOUT_CFG_PREFIX "pid-video", &val );
1339 int i_pid_video = val.i_int;
1340 if ( i_pid_video == p_stream->i_pid )
1342 p_sys->i_pid_video = i_pid_video;
1343 msg_Dbg( p_mux, "freeing video PID %d", i_pid_video );
1346 var_Get( p_mux, SOUT_CFG_PREFIX "pid-audio", &val );
1349 int i_pid_audio = val.i_int;
1350 if ( i_pid_audio == p_stream->i_pid )
1352 p_sys->i_pid_audio = i_pid_audio;
1353 msg_Dbg( p_mux, "freeing audio PID %d", i_pid_audio );
1356 var_Get( p_mux, SOUT_CFG_PREFIX "pid-spu", &val );
1359 int i_pid_spu = val.i_int;
1360 if ( i_pid_spu == p_stream->i_pid )
1362 p_sys->i_pid_spu = i_pid_spu;
1363 msg_Dbg( p_mux, "freeing spu PID %d", i_pid_spu );
1368 /* We only change PMT version (PAT isn't changed) */
1369 p_sys->i_pmt_version_number++; p_sys->i_pmt_version_number %= 32;
1374 /*****************************************************************************
1375 * Mux: Call each time there is new data for at least one stream
1376 *****************************************************************************
1378 *****************************************************************************/
1379 static int Mux( sout_mux_t *p_mux )
1381 sout_mux_sys_t *p_sys = p_mux->p_sys;
1382 ts_stream_t *p_pcr_stream;
1384 if( p_sys->i_pcr_pid == 0x1fff )
1387 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1389 block_FifoEmpty( p_mux->pp_inputs[i]->p_fifo );
1391 msg_Dbg( p_mux, "waiting for PCR streams" );
1394 p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
1398 sout_buffer_chain_t chain_ts;
1402 mtime_t i_pcr_length;
1403 mtime_t i_shaping_delay;
1406 if( p_pcr_stream->b_key_frame )
1408 i_shaping_delay = p_pcr_stream->i_pes_length;
1412 i_shaping_delay = p_sys->i_shaping_delay;
1415 /* 1: get enough PES packet for all input */
1421 /* Accumulate enough data in the pcr stream (>i_shaping_delay) */
1422 /* Accumulate enough data in all other stream ( >= length of pcr)*/
1423 for( i = -1; i < p_mux->i_nb_inputs; i++ )
1425 sout_input_t *p_input;
1426 ts_stream_t *p_stream;
1427 int64_t i_spu_delay = 0;
1430 p_input = p_sys->p_pcr_input;
1431 else if( p_mux->pp_inputs[i]->p_sys == p_pcr_stream )
1434 p_input = p_mux->pp_inputs[i];
1435 p_stream = (ts_stream_t*)p_input->p_sys;
1437 if( ( ( p_stream == p_pcr_stream ) &&
1438 ( p_stream->i_pes_length < i_shaping_delay ) ) ||
1439 ( p_stream->i_pes_dts + p_stream->i_pes_length <
1440 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length ) )
1442 /* Need more data */
1443 if( block_FifoCount( p_input->p_fifo ) <= 1 )
1445 if( ( p_input->p_fmt->i_cat == AUDIO_ES ) ||
1446 ( p_input->p_fmt->i_cat == VIDEO_ES ) )
1448 /* We need more data */
1451 else if( block_FifoCount( p_input->p_fifo ) <= 0 )
1453 /* spu, only one packet is needed */
1456 else if( p_input->p_fmt->i_cat == SPU_ES )
1458 /* Don't mux the SPU yet if it is too early */
1459 block_t *p_spu = block_FifoShow( p_input->p_fifo );
1462 p_spu->i_dts - p_pcr_stream->i_pes_dts;
1464 if( ( i_spu_delay > i_shaping_delay ) &&
1465 ( i_spu_delay < INT64_C(100000000) ) )
1468 if ( ( i_spu_delay >= INT64_C(100000000) ) ||
1469 ( i_spu_delay < INT64_C(10000) ) )
1471 BufferChainClean( &p_stream->chain_pes );
1472 p_stream->i_pes_dts = 0;
1473 p_stream->i_pes_used = 0;
1474 p_stream->i_pes_length = 0;
1481 if( p_stream == p_pcr_stream || p_sys->b_data_alignment
1482 || p_input->p_fmt->i_codec !=
1485 p_data = block_FifoGet( p_input->p_fifo );
1487 if( p_input->p_fmt->i_codec ==
1489 p_data = Add_ADTS( p_data, p_input->p_fmt );
1492 p_data = FixPES( p_mux, p_input->p_fifo );
1494 if( block_FifoCount( p_input->p_fifo ) > 0 &&
1495 p_input->p_fmt->i_cat != SPU_ES )
1497 block_t *p_next = block_FifoShow( p_input->p_fifo );
1498 p_data->i_length = p_next->i_dts - p_data->i_dts;
1500 else if( p_input->p_fmt->i_codec !=
1502 p_data->i_length = 1000;
1504 if( ( p_pcr_stream->i_pes_dts > 0 &&
1505 p_data->i_dts - 10000000 > p_pcr_stream->i_pes_dts +
1506 p_pcr_stream->i_pes_length ) ||
1507 p_data->i_dts < p_stream->i_pes_dts ||
1508 ( p_stream->i_pes_dts > 0 &&
1509 p_input->p_fmt->i_cat != SPU_ES &&
1510 p_data->i_dts - 10000000 > p_stream->i_pes_dts +
1511 p_stream->i_pes_length ) )
1513 msg_Warn( p_mux, "packet with too strange dts "
1514 "(dts=%"PRId64",old=%"PRId64",pcr=%"PRId64")",
1515 p_data->i_dts, p_stream->i_pes_dts,
1516 p_pcr_stream->i_pes_dts );
1517 block_Release( p_data );
1519 BufferChainClean( &p_stream->chain_pes );
1520 p_stream->i_pes_dts = 0;
1521 p_stream->i_pes_used = 0;
1522 p_stream->i_pes_length = 0;
1524 if( p_input->p_fmt->i_cat != SPU_ES )
1526 BufferChainClean( &p_pcr_stream->chain_pes );
1527 p_pcr_stream->i_pes_dts = 0;
1528 p_pcr_stream->i_pes_used = 0;
1529 p_pcr_stream->i_pes_length = 0;
1534 int i_header_size = 0;
1535 int i_max_pes_size = 0;
1536 int b_data_alignment = 0;
1537 if( p_input->p_fmt->i_cat == SPU_ES )
1539 if( p_input->p_fmt->i_codec ==
1542 /* Prepend header */
1543 p_data = block_Realloc( p_data, 2,
1545 p_data->p_buffer[0] =
1546 ( (p_data->i_buffer - 2) >> 8) & 0xff;
1547 p_data->p_buffer[1] =
1548 ( (p_data->i_buffer - 2) ) & 0xff;
1550 /* remove trailling \0 if any */
1551 if( p_data->i_buffer > 2 &&
1552 p_data->p_buffer[p_data->i_buffer -1] ==
1556 /* Append a empty sub (sub text only) */
1557 if( p_data->i_length > 0 &&
1558 !( p_data->i_buffer == 1 &&
1559 *p_data->p_buffer == ' ' ) )
1561 block_t *p_spu = block_New( p_mux, 3 );
1563 p_spu->i_dts = p_spu->i_pts =
1564 p_data->i_dts + p_data->i_length;
1565 p_spu->i_length = 1000;
1567 p_spu->p_buffer[0] = 0;
1568 p_spu->p_buffer[1] = 1;
1569 p_spu->p_buffer[2] = ' ';
1571 EStoPES( p_mux->p_sout, &p_spu, p_spu,
1573 p_stream->i_stream_id, 1,
1575 p_data->p_next = p_spu;
1578 else if( p_input->p_fmt->i_codec ==
1579 VLC_CODEC_TELETEXT )
1582 i_header_size = 0x24;
1583 b_data_alignment = 1;
1585 else if( p_input->p_fmt->i_codec ==
1589 b_data_alignment = 1;
1592 else if( p_data->i_length < 0 ||
1593 p_data->i_length > 2000000 )
1595 /* FIXME choose a better value, but anyway we
1596 * should never have to do that */
1597 p_data->i_length = 1000;
1600 p_stream->i_pes_length += p_data->i_length;
1601 if( p_stream->i_pes_dts == 0 )
1603 p_stream->i_pes_dts = p_data->i_dts;
1606 /* Convert to pes */
1607 if( (p_stream->i_stream_id == 0xa0 ||
1608 p_stream->i_stream_type == 0x1b ) && /*Workaroud for bug #3306 */
1609 p_data->i_pts <= 0 )
1611 /* XXX yes I know, it's awful, but it's needed,
1612 * so don't remove it ... */
1613 p_data->i_pts = p_data->i_dts;
1616 if( p_input->p_fmt->i_codec ==
1619 b_data_alignment = 1;
1620 /* dirac pes packets should be unbounded in
1621 * length, specify a suitibly large max size */
1622 i_max_pes_size = INT_MAX;
1625 EStoPES ( p_mux->p_sout, &p_data, p_data,
1626 p_input->p_fmt, p_stream->i_stream_id,
1627 1, b_data_alignment, i_header_size,
1630 BufferChainAppend( &p_stream->chain_pes, p_data );
1632 if( p_sys->b_use_key_frames && p_stream == p_pcr_stream
1633 && (p_data->i_flags & BLOCK_FLAG_TYPE_I)
1634 && !(p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME)
1635 && (p_stream->i_pes_length > 400000) )
1637 i_shaping_delay = p_stream->i_pes_length;
1638 p_stream->b_key_frame = 1;
1651 i_pcr_dts = p_pcr_stream->i_pes_dts;
1652 i_pcr_length = p_pcr_stream->i_pes_length;
1653 p_pcr_stream->b_key_frame = 0;
1655 /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
1656 /* 2: calculate non accurate total size of muxed ts */
1658 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1660 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1663 /* False for pcr stream but it will be enough to do PCR algo */
1664 for( p_pes = p_stream->chain_pes.p_first; p_pes != NULL;
1665 p_pes = p_pes->p_next )
1667 int i_size = p_pes->i_buffer;
1668 if( p_pes->i_dts + p_pes->i_length >
1669 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1671 mtime_t i_frag = p_pcr_stream->i_pes_dts +
1672 p_pcr_stream->i_pes_length - p_pes->i_dts;
1678 i_size = p_pes->i_buffer * i_frag / p_pes->i_length;
1680 i_packet_count += ( i_size + 183 ) / 184;
1683 /* add overhead for PCR (not really exact) */
1684 i_packet_count += (8 * i_pcr_length / p_sys->i_pcr_delay + 175) / 176;
1686 /* 3: mux PES into TS */
1687 BufferChainInit( &chain_ts );
1688 /* append PAT/PMT -> FIXME with big pcr delay it won't have enough pat/pmt */
1689 GetPAT( p_mux, &chain_ts );
1690 GetPMT( p_mux, &chain_ts );
1692 i_packet_count += chain_ts.i_depth;
1693 /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
1699 ts_stream_t *p_stream;
1700 sout_input_t *p_input;
1704 /* Select stream (lowest dts) */
1705 for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
1707 p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1709 if( p_stream->i_pes_dts == 0 )
1714 if( i_stream == -1 ||
1715 p_stream->i_pes_dts < i_dts )
1718 i_dts = p_stream->i_pes_dts;
1721 if( i_stream == -1 || i_dts > i_pcr_dts + i_pcr_length )
1725 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1726 p_input = p_mux->pp_inputs[i_stream];
1728 /* do we need to issue pcr */
1730 if( p_stream == p_pcr_stream &&
1731 i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >=
1732 p_sys->i_pcr + p_sys->i_pcr_delay )
1735 p_sys->i_pcr = i_pcr_dts + i_packet_pos *
1736 i_pcr_length / i_packet_count;
1739 /* Build the TS packet */
1740 p_ts = TSNew( p_mux, p_stream, b_pcr );
1741 if( p_sys->csa != NULL &&
1742 (p_input->p_fmt->i_cat != AUDIO_ES || p_sys->b_crypt_audio) &&
1743 (p_input->p_fmt->i_cat != VIDEO_ES || p_sys->b_crypt_video) )
1745 p_ts->i_flags |= BLOCK_FLAG_SCRAMBLED;
1750 BufferChainAppend( &chain_ts, p_ts );
1753 /* 4: date and send */
1754 TSSchedule( p_mux, &chain_ts, i_pcr_length, i_pcr_dts );
1758 #define STD_PES_PAYLOAD 170
1759 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
1765 p_data = block_FifoShow( p_fifo );
1766 i_size = p_data->i_buffer;
1768 if( i_size == STD_PES_PAYLOAD )
1770 return block_FifoGet( p_fifo );
1772 else if( i_size > STD_PES_PAYLOAD )
1774 block_t *p_new = block_New( p_mux, STD_PES_PAYLOAD );
1775 vlc_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
1776 p_new->i_pts = p_data->i_pts;
1777 p_new->i_dts = p_data->i_dts;
1778 p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
1780 p_data->i_buffer -= STD_PES_PAYLOAD;
1781 p_data->p_buffer += STD_PES_PAYLOAD;
1782 p_data->i_pts += p_new->i_length;
1783 p_data->i_dts += p_new->i_length;
1784 p_data->i_length -= p_new->i_length;
1785 p_data->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1793 p_data = block_FifoGet( p_fifo );
1794 p_data = block_Realloc( p_data, 0, STD_PES_PAYLOAD );
1795 p_next = block_FifoShow( p_fifo );
1796 if ( p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME )
1798 p_data->i_flags &= ~BLOCK_FLAG_NO_KEYFRAME;
1799 p_data->i_pts = p_next->i_pts;
1800 p_data->i_dts = p_next->i_dts;
1802 i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
1804 vlc_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer, i_copy );
1805 p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
1806 p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
1807 p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
1808 p_next->i_buffer -= i_copy;
1809 p_next->p_buffer += i_copy;
1810 p_next->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1812 if( !p_next->i_buffer )
1814 p_next = block_FifoGet( p_fifo );
1815 block_Release( p_next );
1821 static block_t *Add_ADTS( block_t *p_data, es_format_t *p_fmt )
1823 uint8_t *p_extra = p_fmt->p_extra;
1825 if( !p_data || p_fmt->i_extra < 2 || !p_extra )
1826 return p_data; /* no data to construct the headers */
1828 int i_index = ( (p_extra[0] << 1) | (p_extra[1] >> 7) ) & 0x0f;
1829 int i_profile = (p_extra[0] >> 3) - 1; /* i_profile < 4 */
1831 if( i_index == 0x0f && p_fmt->i_extra < 5 )
1832 return p_data; /* not enough data */
1834 int i_channels = (p_extra[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f;
1836 #define ADTS_HEADER_SIZE 7 /* CRC needs 2 more bytes */
1839 /* keep a copy in case block_Realloc() fails */
1840 block_t *p_bak_block = block_Duplicate( p_data );
1841 if( !p_bak_block ) /* OOM, block_Realloc() is likely to lose our block */
1842 return p_data; /* the frame isn't correct but that's the best we have */
1844 block_t *p_new_block = block_Realloc( p_data, ADTS_HEADER_SIZE,
1847 return p_bak_block; /* OOM, send the (incorrect) original frame */
1849 block_Release( p_bak_block ); /* we don't need the copy anymore */
1852 uint8_t *p_buffer = p_new_block->p_buffer;
1856 p_buffer[1] = 0xf1; /* 0xf0 | 0x00 | 0x00 | 0x01 */
1857 p_buffer[2] = (i_profile << 6) | ((i_index & 0x0f) << 2) | ((i_channels >> 2) & 0x01) ;
1858 p_buffer[3] = (i_channels << 6) | ((p_data->i_buffer >> 11) & 0x03);
1860 /* variable header (starts at last 2 bits of 4th byte) */
1862 int i_fullness = 0x7ff; /* 0x7ff means VBR */
1863 /* XXX: We should check if it's CBR or VBR, but no known implementation
1864 * do that, and it's a pain to calculate this field */
1866 p_buffer[4] = p_data->i_buffer >> 3;
1867 p_buffer[5] = ((p_data->i_buffer & 0x07) << 5) | ((i_fullness >> 6) & 0x1f);
1868 p_buffer[6] = ((i_fullness & 0x3f) << 2) /* | 0xfc */;
1873 static void TSSchedule( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1874 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1876 sout_mux_sys_t *p_sys = p_mux->p_sys;
1877 sout_buffer_chain_t new_chain;
1878 int i_packet_count = p_chain_ts->i_depth;
1881 BufferChainInit( &new_chain );
1883 if ( i_pcr_length <= 0 )
1885 i_pcr_length = i_packet_count;
1888 for( i = 0; i < i_packet_count; i++ )
1890 block_t *p_ts = BufferChainGet( p_chain_ts );
1891 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1893 BufferChainAppend( &new_chain, p_ts );
1896 p_ts->i_dts + p_sys->i_dts_delay * 2/3 < i_new_dts )
1898 mtime_t i_max_diff = i_new_dts - p_ts->i_dts;
1899 mtime_t i_cut_dts = p_ts->i_dts;
1901 p_ts = BufferChainPeek( p_chain_ts );
1903 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1904 while ( p_ts != NULL && i_new_dts - p_ts->i_dts >= i_max_diff )
1906 p_ts = BufferChainGet( p_chain_ts );
1907 i_max_diff = i_new_dts - p_ts->i_dts;
1908 i_cut_dts = p_ts->i_dts;
1909 BufferChainAppend( &new_chain, p_ts );
1911 p_ts = BufferChainPeek( p_chain_ts );
1913 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1915 msg_Dbg( p_mux, "adjusting rate at %"PRId64"/%"PRId64" (%d/%d)",
1916 i_cut_dts - i_pcr_dts, i_pcr_length, new_chain.i_depth,
1917 p_chain_ts->i_depth );
1918 if ( new_chain.i_depth )
1919 TSDate( p_mux, &new_chain,
1920 i_cut_dts - i_pcr_dts,
1922 if ( p_chain_ts->i_depth )
1924 p_chain_ts, i_pcr_dts + i_pcr_length - i_cut_dts,
1930 if ( new_chain.i_depth )
1931 TSDate( p_mux, &new_chain, i_pcr_length, i_pcr_dts );
1934 static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1935 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1937 sout_mux_sys_t *p_sys = p_mux->p_sys;
1938 int i_packet_count = p_chain_ts->i_depth;
1941 if ( i_pcr_length / 1000 > 0 )
1943 int i_bitrate = ((uint64_t)i_packet_count * 188 * 8000)
1944 / (uint64_t)(i_pcr_length / 1000);
1945 if ( p_sys->i_bitrate_max && p_sys->i_bitrate_max < i_bitrate )
1947 msg_Warn( p_mux, "max bitrate exceeded at %"PRId64
1948 " (%d bi/s for %d pkt in %"PRId64" us)",
1949 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1950 i_bitrate, i_packet_count, i_pcr_length);
1955 msg_Dbg( p_mux, "starting at %"PRId64
1956 " (%d bi/s for %d packets in %"PRId64" us)",
1957 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1958 i_bitrate, i_packet_count, i_pcr_length);
1964 /* This shouldn't happen, but happens in some rare heavy load
1965 * and packet losses conditions. */
1966 i_pcr_length = i_packet_count;
1969 /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
1970 for( i = 0; i < i_packet_count; i++ )
1972 block_t *p_ts = BufferChainGet( p_chain_ts );
1973 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1975 p_ts->i_dts = i_new_dts;
1976 p_ts->i_length = i_pcr_length / i_packet_count;
1978 if( p_ts->i_flags & BLOCK_FLAG_CLOCK )
1980 /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
1981 TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay );
1983 if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
1985 vlc_mutex_lock( &p_sys->csa_lock );
1986 csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size );
1987 vlc_mutex_unlock( &p_sys->csa_lock );
1991 p_ts->i_dts += p_sys->i_shaping_delay * 3 / 2;
1993 sout_AccessOutWrite( p_mux->p_access, p_ts );
1997 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream,
2001 block_t *p_pes = p_stream->chain_pes.p_first;
2004 bool b_new_pes = false;
2005 bool b_adaptation_field = false;
2007 int i_payload_max = 184 - ( b_pcr ? 8 : 0 );
2010 if( p_stream->i_pes_used <= 0 )
2014 i_payload = __MIN( (int)p_pes->i_buffer - p_stream->i_pes_used,
2017 if( b_pcr || i_payload < i_payload_max )
2019 b_adaptation_field = true;
2022 p_ts = block_New( p_mux, 188 );
2024 if (b_new_pes && !(p_pes->i_flags & BLOCK_FLAG_NO_KEYFRAME) && p_pes->i_flags & BLOCK_FLAG_TYPE_I)
2026 p_ts->i_flags |= BLOCK_FLAG_TYPE_I;
2029 p_ts->i_dts = p_pes->i_dts;
2031 p_ts->p_buffer[0] = 0x47;
2032 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 ) |
2033 ( ( p_stream->i_pid >> 8 )&0x1f );
2034 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
2035 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 ) |
2036 p_stream->i_continuity_counter;
2038 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
2039 p_stream->b_discontinuity = (p_pes->i_flags & BLOCK_FLAG_DISCONTINUITY);
2041 if( b_adaptation_field )
2047 int i_stuffing = i_payload_max - i_payload;
2049 p_ts->i_flags |= BLOCK_FLAG_CLOCK;
2051 p_ts->p_buffer[4] = 7 + i_stuffing;
2052 p_ts->p_buffer[5] = 0x10; /* flags */
2053 if( p_stream->b_discontinuity )
2055 p_ts->p_buffer[5] |= 0x80; /* flag TS dicontinuity */
2056 p_stream->b_discontinuity = false;
2058 p_ts->p_buffer[6] = ( 0 )&0xff;
2059 p_ts->p_buffer[7] = ( 0 )&0xff;
2060 p_ts->p_buffer[8] = ( 0 )&0xff;
2061 p_ts->p_buffer[9] = ( 0 )&0xff;
2062 p_ts->p_buffer[10]= ( ( 0 )&0x80 ) | 0x7e;
2063 p_ts->p_buffer[11]= 0;
2065 for( i = 12; i < 12 + i_stuffing; i++ )
2067 p_ts->p_buffer[i] = 0xff;
2072 int i_stuffing = i_payload_max - i_payload;
2074 p_ts->p_buffer[4] = i_stuffing - 1;
2075 if( i_stuffing > 1 )
2077 p_ts->p_buffer[5] = 0x00;
2078 for( i = 6; i < 6 + i_stuffing - 2; i++ )
2080 p_ts->p_buffer[i] = 0xff;
2087 memcpy( &p_ts->p_buffer[188 - i_payload],
2088 &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
2090 p_stream->i_pes_used += i_payload;
2091 p_stream->i_pes_dts = p_pes->i_dts + p_pes->i_length *
2092 p_stream->i_pes_used / p_pes->i_buffer;
2093 p_stream->i_pes_length -= p_pes->i_length * i_payload / p_pes->i_buffer;
2095 if( p_stream->i_pes_used >= (int)p_pes->i_buffer )
2097 p_pes = BufferChainGet( &p_stream->chain_pes );
2098 block_Release( p_pes );
2100 p_pes = p_stream->chain_pes.p_first;
2103 p_stream->i_pes_dts = p_pes->i_dts;
2104 p_stream->i_pes_length = 0;
2107 p_stream->i_pes_length += p_pes->i_length;
2109 p_pes = p_pes->p_next;
2114 p_stream->i_pes_dts = 0;
2115 p_stream->i_pes_length = 0;
2117 p_stream->i_pes_used = 0;
2123 static void TSSetPCR( block_t *p_ts, mtime_t i_dts )
2125 mtime_t i_pcr = 9 * i_dts / 100;
2127 p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
2128 p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
2129 p_ts->p_buffer[8] = ( i_pcr >> 9 )&0xff;
2130 p_ts->p_buffer[9] = ( i_pcr >> 1 )&0xff;
2131 p_ts->p_buffer[10]|= ( i_pcr << 7 )&0x80;
2135 static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c,
2136 mtime_t i_length, int i_bitrate_min,
2139 sout_mux_sys_t *p_sys = p_mux->p_sys;
2140 sout_buffer_chain_t s = *c;
2143 int i_packets_min = 0;
2144 int i_packets_max = 0;
2151 i_packets = c->i_depth;
2152 i_packets_min = ( (int64_t)i_bitrate_min * i_length / 8 / 1000000 + 187 ) / 188;
2153 i_packets_max = ( (int64_t)i_bitrate_max * i_length / 8 / 1000000 + 187 ) / 188;
2155 if( i_packets < i_packets_min && i_packets_min > 0 )
2158 int i_div = ( i_packets_min - i_packets ) / i_packets;
2159 int i_mod = ( i_packets_min - i_packets ) % i_packets;
2162 /* We need to pad with null packets (pid=0x1fff)
2163 * We try to melt null packets with true packets */
2165 "packets=%d but min=%d -> adding %d packets of padding",
2166 i_packets, i_packets_min, i_packets_min - i_packets );
2168 BufferChainInit( c );
2169 while( ( p_pk = BufferChainGet( &s ) ) )
2173 BufferChainAppend( c, p_pk );
2175 i_null = i_div + ( i_rest + i_mod ) / i_packets;
2177 for( i = 0; i < i_null; i++ )
2181 p_null = sout_BufferNew( p_mux->p_sout, 188 );
2182 p_null->p_buffer[0] = 0x47;
2183 p_null->p_buffer[1] = 0x1f;
2184 p_null->p_buffer[2] = 0xff;
2185 p_null->p_buffer[3] = 0x10 | p_sys->i_null_continuity_counter;
2186 memset( &p_null->p_buffer[4], 0, 184 );
2187 p_sys->i_null_continuity_counter =
2188 ( p_sys->i_null_continuity_counter + 1 ) % 16;
2190 BufferChainAppend( c, p_null );
2193 i_rest = ( i_rest + i_mod ) % i_packets;
2196 else if( i_packets > i_packets_max && i_packets_max > 0 )
2201 /* Arg, we need to drop packets, I don't do something clever (like
2202 * dropping complete pid, b frames, ... ), I just get the right amount
2203 * of packets and discard the others */
2205 "packets=%d but max=%d -> removing %d packets -> stream broken",
2206 i_packets, i_packets_max, i_packets - i_packets_max );
2208 BufferChainInit( c );
2209 for( i = 0; i < i_packets_max; i++ )
2211 BufferChainAppend( c, BufferChainGet( &s ) );
2214 while( ( p_pk = BufferChainGet( &s ) ) )
2216 sout_BufferDelete( p_mux->p_sout, p_pk );
2222 static void PEStoTS( sout_instance_t *p_sout,
2223 sout_buffer_chain_t *c, block_t *p_pes,
2224 ts_stream_t *p_stream )
2231 /* get PES total size */
2232 i_size = p_pes->i_buffer;
2233 p_data = p_pes->p_buffer;
2239 int b_adaptation_field;
2243 p_ts = block_New( p_sout, 188 );
2246 * 1b transport_error_indicator
2247 * 1b payload_unit_start
2248 * 1b transport_priority
2250 * 2b transport_scrambling_control
2251 * 2b if adaptation_field 0x03 else 0x01
2252 * 4b continuity_counter
2255 i_copy = __MIN( i_size, 184 );
2256 b_adaptation_field = i_size < 184 ? true : false;
2258 p_ts->p_buffer[0] = 0x47;
2259 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
2260 ( ( p_stream->i_pid >> 8 )&0x1f );
2261 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
2262 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
2263 p_stream->i_continuity_counter;
2266 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
2268 if( b_adaptation_field )
2270 int i_stuffing = 184 - i_copy;
2273 p_ts->p_buffer[4] = i_stuffing - 1;
2274 if( i_stuffing > 1 )
2276 p_ts->p_buffer[5] = 0x00;
2277 if( p_stream->b_discontinuity )
2279 p_ts->p_buffer[5] |= 0x80;
2280 p_stream->b_discontinuity = false;
2282 for( i = 6; i < 6 + i_stuffing - 2; i++ )
2284 p_ts->p_buffer[i] = 0xff;
2289 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
2293 BufferChainAppend( c, p_ts );
2297 block_t *p_next = p_pes->p_next;
2299 p_pes->p_next = NULL;
2300 block_Release( p_pes );
2301 if( p_next == NULL )
2307 i_size = p_pes->i_buffer;
2308 p_data = p_pes->p_buffer;
2315 static block_t *WritePSISection( sout_instance_t *p_sout,
2316 dvbpsi_psi_section_t* p_section )
2319 block_t *p_psi, *p_first = NULL;
2325 i_size = (uint32_t)( p_section->p_payload_end - p_section->p_data )+
2326 ( p_section->b_syntax_indicator ? 4 : 0 );
2328 p_psi = block_New( p_sout, i_size + 1 );
2331 p_psi->i_length = 0;
2332 p_psi->i_buffer = i_size + 1;
2334 p_psi->p_buffer[0] = 0; /* pointer */
2335 memcpy( p_psi->p_buffer + 1,
2339 block_ChainAppend( &p_first, p_psi );
2341 p_section = p_section->p_next;
2347 static void GetPAT( sout_mux_t *p_mux,
2348 sout_buffer_chain_t *c )
2350 sout_mux_sys_t *p_sys = p_mux->p_sys;
2353 dvbpsi_psi_section_t *p_section;
2356 dvbpsi_InitPAT( &pat, p_sys->i_tsid, p_sys->i_pat_version_number,
2357 1 ); /* b_current_next */
2358 /* add all programs */
2359 for ( i = 0; i < p_sys->i_num_pmt; i++ )
2360 dvbpsi_PATAddProgram( &pat,
2361 p_sys->i_pmt_program_number[i],
2362 p_sys->pmt[i].i_pid );
2364 p_section = dvbpsi_GenPATSections( &pat,
2365 0 ); /* max program per section */
2367 p_pat = WritePSISection( p_mux->p_sout, p_section );
2369 PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
2371 dvbpsi_DeletePSISections( p_section );
2372 dvbpsi_EmptyPAT( &pat );
2375 static uint32_t GetDescriptorLength24b( int i_length )
2377 uint32_t i_l1, i_l2, i_l3;
2379 i_l1 = i_length&0x7f;
2380 i_l2 = ( i_length >> 7 )&0x7f;
2381 i_l3 = ( i_length >> 14 )&0x7f;
2383 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
2386 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c )
2388 sout_mux_sys_t *p_sys = p_mux->p_sys;
2389 block_t *p_pmt[MAX_PMT];
2391 dvbpsi_pmt_es_t *p_es;
2392 dvbpsi_psi_section_t *p_section[MAX_PMT];
2397 int *p_usepid = NULL;
2399 #ifdef HAVE_DVBPSI_SDT
2403 dvbpsi_psi_section_t* p_section2;
2404 dvbpsi_sdt_service_t *p_service;
2406 uint8_t *psz_sdt_desc;
2409 if( p_sys->dvbpmt == NULL )
2411 p_sys->dvbpmt = malloc( p_sys->i_num_pmt * sizeof(dvbpsi_pmt_t) );
2412 if( !p_sys->dvbpmt )
2417 #ifdef HAVE_DVBPSI_SDT
2419 dvbpsi_InitSDT( &sdt, p_sys->i_tsid, 1, 1, p_sys->i_netid );
2422 for( i = 0; i < p_sys->i_num_pmt; i++ )
2424 dvbpsi_InitPMT( &p_sys->dvbpmt[i],
2425 p_sys->i_pmt_program_number[i], /* program number */
2426 p_sys->i_pmt_version_number,
2427 1, /* b_current_next */
2430 #ifdef HAVE_DVBPSI_SDT
2433 p_service = dvbpsi_SDTAddService( &sdt,
2434 p_sys->i_pmt_program_number[i], /* service id */
2435 0, /* eit schedule */
2436 0, /* eit present */
2437 4, /* running status ("4=RUNNING") */
2440 #define psz_sdtprov p_sys->sdt_descriptors[i].psz_provider
2441 #define psz_sdtserv p_sys->sdt_descriptors[i].psz_service_name
2443 /* FIXME: Ineffecient malloc's & ugly code...... */
2444 if( ( psz_sdtprov != NULL ) && ( psz_sdtserv != NULL ) )
2446 psz_sdt_desc = malloc( 3 + strlen(psz_sdtprov)
2447 + strlen(psz_sdtserv) );
2450 psz_sdt_desc[0] = 0x01; /* digital television service */
2452 /* service provider name length */
2453 psz_sdt_desc[1] = (char)strlen(psz_sdtprov);
2454 memcpy( &psz_sdt_desc[2], psz_sdtprov, strlen(psz_sdtprov) );
2456 /* service name length */
2457 psz_sdt_desc[ 2 + strlen(psz_sdtprov) ]
2458 = (char)strlen(psz_sdtserv);
2459 memcpy( &psz_sdt_desc[3+strlen(psz_sdtprov)], psz_sdtserv,
2460 strlen(psz_sdtserv) );
2462 dvbpsi_SDTServiceAddDescriptor( p_service, 0x48,
2463 3 + strlen(psz_sdtprov) + strlen(psz_sdtserv),
2464 (uint8_t *)psz_sdt_desc );
2465 free( psz_sdt_desc );
2466 psz_sdt_desc = NULL;
2475 if( p_sys->i_mpeg4_streams > 0 )
2479 bits_buffer_t bits_fix_IOD;
2481 /* Make valgrind happy : it works at byte level not bit one so
2482 * bit_write confuse it (but DON'T CHANGE the way that bit_write is
2483 * working (needed when fixing some bits) */
2484 memset( iod, 0, 4096 );
2486 bits_initwrite( &bits, 4096, iod );
2487 /* IOD_label_scope */
2488 bits_write( &bits, 8, 0x11 );
2490 bits_write( &bits, 8, 0x01 );
2491 /* InitialObjectDescriptor */
2492 bits_align( &bits );
2493 bits_write( &bits, 8, 0x02 ); /* tag */
2494 bits_fix_IOD = bits; /* save states to fix length later */
2495 bits_write( &bits, 24,
2496 GetDescriptorLength24b( 0 ) ); /* variable length (fixed later) */
2497 bits_write( &bits, 10, 0x01 ); /* ObjectDescriptorID */
2498 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2499 bits_write( &bits, 1, 0x00 ); /* includeInlineProfileLevelFlag */
2500 bits_write( &bits, 4, 0x0f ); /* reserved */
2501 bits_write( &bits, 8, 0xff ); /* ODProfile (no ODcapability ) */
2502 bits_write( &bits, 8, 0xff ); /* sceneProfile */
2503 bits_write( &bits, 8, 0xfe ); /* audioProfile (unspecified) */
2504 bits_write( &bits, 8, 0xfe ); /* visualProfile( // ) */
2505 bits_write( &bits, 8, 0xff ); /* graphicProfile (no ) */
2506 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2508 ts_stream_t *p_stream;
2509 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
2511 if( p_stream->i_stream_id == 0xfa ||
2512 p_stream->i_stream_id == 0xfb ||
2513 p_stream->i_stream_id == 0xfe )
2515 bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
2517 bits_align( &bits );
2518 bits_write( &bits, 8, 0x03 ); /* ES_DescrTag */
2519 bits_fix_ESDescr = bits;
2520 bits_write( &bits, 24,
2521 GetDescriptorLength24b( 0 ) ); /* variable size */
2522 bits_write( &bits, 16, p_stream->i_es_id );
2523 bits_write( &bits, 1, 0x00 ); /* streamDependency */
2524 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2525 bits_write( &bits, 1, 0x00 ); /* OCRStreamFlag */
2526 bits_write( &bits, 5, 0x1f ); /* streamPriority */
2528 /* DecoderConfigDesciptor */
2529 bits_align( &bits );
2530 bits_write( &bits, 8, 0x04 ); /* DecoderConfigDescrTag */
2531 bits_fix_Decoder = bits;
2532 bits_write( &bits, 24, GetDescriptorLength24b( 0 ) );
2533 if( p_stream->i_stream_type == 0x10 )
2535 bits_write( &bits, 8, 0x20 ); /* Visual 14496-2 */
2536 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2538 else if( p_stream->i_stream_type == 0x1b )
2540 bits_write( &bits, 8, 0x21 ); /* Visual 14496-2 */
2541 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2543 else if( p_stream->i_stream_type == 0x11 ||
2544 p_stream->i_stream_type == 0x0f )
2546 bits_write( &bits, 8, 0x40 ); /* Audio 14496-3 */
2547 bits_write( &bits, 6, 0x05 ); /* AudioStream */
2549 else if( p_stream->i_stream_type == 0x12 &&
2550 p_stream->i_codec == VLC_CODEC_SUBT )
2552 bits_write( &bits, 8, 0x0B ); /* Text Stream */
2553 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2557 bits_write( &bits, 8, 0x00 );
2558 bits_write( &bits, 6, 0x00 );
2560 msg_Err( p_mux->p_sout,"Unsupported stream_type => "
2563 bits_write( &bits, 1, 0x00 ); /* UpStream */
2564 bits_write( &bits, 1, 0x01 ); /* reserved */
2565 bits_write( &bits, 24, 1024 * 1024 ); /* bufferSizeDB */
2566 bits_write( &bits, 32, 0x7fffffff ); /* maxBitrate */
2567 bits_write( &bits, 32, 0 ); /* avgBitrate */
2569 if( p_stream->i_decoder_specific_info > 0 )
2572 /* DecoderSpecificInfo */
2573 bits_align( &bits );
2574 bits_write( &bits, 8, 0x05 ); /* tag */
2575 bits_write( &bits, 24, GetDescriptorLength24b(
2576 p_stream->i_decoder_specific_info ) );
2577 for( i = 0; i < p_stream->i_decoder_specific_info; i++ )
2579 bits_write( &bits, 8,
2580 ((uint8_t*)p_stream->p_decoder_specific_info)[i] );
2583 /* fix Decoder length */
2584 bits_write( &bits_fix_Decoder, 24,
2585 GetDescriptorLength24b( bits.i_data -
2586 bits_fix_Decoder.i_data - 3 ) );
2588 /* SLConfigDescriptor : predefined (0x01) */
2589 bits_align( &bits );
2590 bits_write( &bits, 8, 0x06 ); /* tag */
2591 bits_write( &bits, 24, GetDescriptorLength24b( 8 ) );
2592 bits_write( &bits, 8, 0x01 );/* predefined */
2593 bits_write( &bits, 1, 0 ); /* durationFlag */
2594 bits_write( &bits, 32, 0 ); /* OCRResolution */
2595 bits_write( &bits, 8, 0 ); /* OCRLength */
2596 bits_write( &bits, 8, 0 ); /* InstantBitrateLength */
2597 bits_align( &bits );
2599 /* fix ESDescr length */
2600 bits_write( &bits_fix_ESDescr, 24,
2601 GetDescriptorLength24b( bits.i_data -
2602 bits_fix_ESDescr.i_data - 3 ) );
2605 bits_align( &bits );
2606 /* fix IOD length */
2607 bits_write( &bits_fix_IOD, 24,
2608 GetDescriptorLength24b( bits.i_data -
2609 bits_fix_IOD.i_data - 3 ) );
2611 #if 0 /* FIXME!!! This can't possibly work */
2612 i_pidinput = p_mux->pp_inputs[i]->p_fmt->i_id;
2613 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2614 sizeof(pmt_map_t), intcompare );
2615 p_usepid = bsearch( &p_usepid, p_sys->pmtmap, p_sys->i_num_pmt,
2616 sizeof(pmt_map_t), pmtcompare );
2617 if( p_usepid != NULL )
2618 dvbpsi_PMTAddDescriptor(
2619 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog], 0x1d,
2620 bits.i_data, bits.p_data );
2622 msg_Err( p_mux, "Received an unmapped PID" );
2624 dvbpsi_PMTAddDescriptor( &p_sys->dvbpmt[0], 0x1d, bits.i_data,
2629 for( i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2631 ts_stream_t *p_stream;
2633 p_stream = (ts_stream_t *)p_mux->pp_inputs[i_stream]->p_sys;
2635 i_pidinput = p_mux->pp_inputs[i_stream]->p_fmt->i_id;
2636 p_usepid = bsearch( &i_pidinput, p_sys->pmtmap, p_sys->i_pmtslots,
2637 sizeof(pmt_map_t), intcompare );
2639 if( p_usepid != NULL )
2640 p_es = dvbpsi_PMTAddES(
2641 &p_sys->dvbpmt[((pmt_map_t *)p_usepid)->i_prog],
2642 p_stream->i_stream_type, p_stream->i_pid );
2644 /* If there's an error somewhere, dump it to the first pmt */
2645 p_es = dvbpsi_PMTAddES( &p_sys->dvbpmt[0], p_stream->i_stream_type,
2648 if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
2653 es_id[0] = (p_stream->i_es_id >> 8)&0xff;
2654 es_id[1] = (p_stream->i_es_id)&0xff;
2655 dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
2657 else if( p_stream->i_stream_type == 0xa0 )
2660 int i_extra = __MIN( p_stream->i_decoder_specific_info, 502 );
2662 /* private DIV3 descripor */
2663 memcpy( &data[0], &p_stream->i_bih_codec, 4 );
2664 data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
2665 data[5] = ( p_stream->i_bih_width )&0xff;
2666 data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
2667 data[7] = ( p_stream->i_bih_height )&0xff;
2668 data[8] = ( i_extra >> 8 )&0xff;
2669 data[9] = ( i_extra )&0xff;
2672 memcpy( &data[10], p_stream->p_decoder_specific_info, i_extra );
2675 /* 0xa0 is private */
2676 dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra + 10, data );
2678 else if( p_stream->i_stream_type == 0x81 )
2680 uint8_t format[4] = { 0x41, 0x43, 0x2d, 0x33 };
2682 /* "registration" descriptor : "AC-3" */
2683 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
2685 else if( p_stream->i_codec == VLC_CODEC_DIRAC )
2687 /* Dirac registration descriptor */
2689 uint8_t data[4] = { 'd', 'r', 'a', 'c' };
2690 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2692 else if( p_stream->i_codec == VLC_CODEC_DTS )
2694 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2696 /* DTS format identifier, frame size 1024 - FIXME */
2697 uint8_t data[4] = { 0x44, 0x54, 0x53, 0x32 };
2698 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2700 else if( p_stream->i_codec == VLC_CODEC_EAC3 )
2702 uint8_t data[1] = { 0x00 };
2703 dvbpsi_PMTESAddDescriptor( p_es, 0x7a, 1, data );
2705 else if( p_stream->i_codec == VLC_CODEC_TELETEXT )
2707 if( p_stream->i_decoder_specific_info )
2709 dvbpsi_PMTESAddDescriptor( p_es, 0x56,
2710 p_stream->i_decoder_specific_info,
2711 p_stream->p_decoder_specific_info );
2715 else if( p_stream->i_codec == VLC_CODEC_DVBS )
2718 if( p_stream->i_decoder_specific_info )
2720 /* pass-through from the TS demux */
2721 dvbpsi_PMTESAddDescriptor( p_es, 0x59,
2722 p_stream->i_decoder_specific_info,
2723 p_stream->p_decoder_specific_info );
2725 #ifdef _DVBPSI_DR_59_H_
2728 /* from the dvbsub transcoder */
2729 dvbpsi_subtitling_dr_t descr;
2730 dvbpsi_subtitle_t sub;
2731 dvbpsi_descriptor_t *p_descr;
2733 memcpy( sub.i_iso6392_language_code, p_stream->lang, 3 );
2734 sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
2735 sub.i_composition_page_id = p_stream->i_es_id & 0xFF;
2736 sub.i_ancillary_page_id = p_stream->i_es_id >> 16;
2738 descr.i_subtitles_number = 1;
2739 descr.p_subtitle[0] = sub;
2741 p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
2742 /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
2743 dvbpsi_PMTESAddDescriptor( p_es, p_descr->i_tag,
2744 p_descr->i_length, p_descr->p_data );
2746 #endif /* _DVBPSI_DR_59_H_ */
2750 if( p_stream->lang[0] != 0 )
2752 uint8_t data[4*p_stream->i_langs];
2754 /* I construct the content myself, way faster than looking at
2755 * over complicated/mind broken libdvbpsi way */
2756 for(i = 0; i < p_stream->i_langs; i++ )
2758 data[i*4+0] = p_stream->lang[i*3+0];
2759 data[i*4+1] = p_stream->lang[i*3+1];
2760 data[i*4+2] = p_stream->lang[i*3+2];
2761 data[i*4+3] = 0x00; /* audio type: 0x00 undefined */
2763 dvbpsi_PMTESAddDescriptor( p_es, 0x0a, 4*p_stream->i_langs, data );
2767 for( i = 0; i < p_sys->i_num_pmt; i++ )
2769 p_section[i] = dvbpsi_GenPMTSections( &p_sys->dvbpmt[i] );
2770 p_pmt[i] = WritePSISection( p_mux->p_sout, p_section[i] );
2771 PEStoTS( p_mux->p_sout, c, p_pmt[i], &p_sys->pmt[i] );
2772 dvbpsi_DeletePSISections( p_section[i] );
2773 dvbpsi_EmptyPMT( &p_sys->dvbpmt[i] );
2776 #ifdef HAVE_DVBPSI_SDT
2779 p_section2 = dvbpsi_GenSDTSections( &sdt );
2780 p_sdt = WritePSISection( p_mux->p_sout, p_section2 );
2781 PEStoTS( p_mux->p_sout, c, p_sdt, &p_sys->sdt );
2782 dvbpsi_DeletePSISections( p_section2 );
2783 dvbpsi_EmptySDT( &sdt );