mux ts: Don't do extra copy on every aac packet
[vlc.git] / modules / mux / mpeg / ts.c
blobfd3e5d57bcf0ffe78ed654c3faf76377fe806d2d
1 /*****************************************************************************
2 * ts.c: MPEG-II TS Muxer
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VLC authors and VideoLAN
5 * $Id$
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <limits.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_sout.h>
40 #include <vlc_block.h>
41 #include <vlc_rand.h>
43 #include <vlc_iso_lang.h>
45 #include "bits.h"
46 #include "pes.h"
47 #include "csa.h"
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/demux.h>
51 # include <dvbpsi/descriptor.h>
52 # include <dvbpsi/pat.h>
53 # include <dvbpsi/pmt.h>
54 # include <dvbpsi/sdt.h>
55 # include <dvbpsi/dr.h>
56 # include <dvbpsi/psi.h>
58 #include "dvbpsi_compat.h"
61 * TODO:
62 * - check PCR frequency requirement
63 * - check PAT/PMT " "
64 * - check PCR/PCR "soft"
65 * - check if "registration" descriptor : "AC-3" should be a program
66 * descriptor or an es one. (xine want an es one)
68 * - remove creation of PAT/PMT without dvbpsi
69 * - ?
70 * FIXME:
71 * - subtitle support is far from perfect. I expect some subtitles drop
72 * if they arrive a bit late
73 * (We cannot rely on the fact that the fifo should be full)
76 /*****************************************************************************
77 * Callback prototypes
78 *****************************************************************************/
79 static int ChangeKeyCallback ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
80 static int ActiveKeyCallback ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
82 /*****************************************************************************
83 * Module descriptor
84 *****************************************************************************/
85 static int Open ( vlc_object_t * );
86 static void Close ( vlc_object_t * );
88 #define VPID_TEXT N_("Video PID")
89 #define VPID_LONGTEXT N_("Assign a fixed PID to the video stream. The PCR " \
90 "PID will automatically be the video.")
91 #define APID_TEXT N_("Audio PID")
92 #define APID_LONGTEXT N_("Assign a fixed PID to the audio stream.")
93 #define SPUPID_TEXT N_("SPU PID")
94 #define SPUPID_LONGTEXT N_("Assign a fixed PID to the SPU.")
95 #define PMTPID_TEXT N_("PMT PID")
96 #define PMTPID_LONGTEXT N_("Assign a fixed PID to the PMT")
97 #define TSID_TEXT N_("TS ID")
98 #define TSID_LONGTEXT N_("Assign a fixed Transport Stream ID.")
99 #define NETID_TEXT N_("NET ID")
100 #define NETID_LONGTEXT N_("Assign a fixed Network ID (for SDT table)")
102 #define PMTPROG_TEXT N_("PMT Program numbers")
103 #define PMTPROG_LONGTEXT N_("Assign a program number to each PMT. This " \
104 "requires \"Set PID to ID of ES\" to be enabled." )
106 #define MUXPMT_TEXT N_("Mux PMT (requires --sout-ts-es-id-pid)")
107 #define MUXPMT_LONGTEXT N_("Define the pids to add to each pmt. This " \
108 "requires \"Set PID to ID of ES\" to be enabled." )
110 #define SDTDESC_TEXT N_("SDT Descriptors (requires --sout-ts-es-id-pid)")
111 #define SDTDESC_LONGTEXT N_("Defines the descriptors of each SDT. This" \
112 "requires \"Set PID to ID of ES\" to be enabled." )
114 #define PID_TEXT N_("Set PID to ID of ES")
115 #define PID_LONGTEXT N_("Sets PID to the ID if the incoming ES. This is for " \
116 "use with --ts-es-id-pid, and allows having the same PIDs in the input " \
117 "and output streams.")
119 #define ALIGNMENT_TEXT N_("Data alignment")
120 #define ALIGNMENT_LONGTEXT N_("Enforces alignment of all access units on " \
121 "PES boundaries. Disabling this might save some bandwidth but introduce incompatibilities.")
123 #define SHAPING_TEXT N_("Shaping delay (ms)")
124 #define SHAPING_LONGTEXT N_("Cut the " \
125 "stream in slices of the given duration, and ensure a constant bitrate " \
126 "between the two boundaries. This avoids having huge bitrate peaks, " \
127 "especially for reference frames." )
129 #define KEYF_TEXT N_("Use keyframes")
130 #define KEYF_LONGTEXT N_("If enabled, and shaping is specified, " \
131 "the TS muxer will place the boundaries at the end of I pictures. In " \
132 "that case, the shaping duration given by the user is a worse case " \
133 "used when no reference frame is available. This enhances the efficiency " \
134 "of the shaping algorithm, since I frames are usually the biggest " \
135 "frames in the stream.")
137 #define PCR_TEXT N_("PCR interval (ms)")
138 #define PCR_LONGTEXT N_("Set at which interval " \
139 "PCRs (Program Clock Reference) will be sent (in milliseconds). " \
140 "This value should be below 100ms. (default is 70ms).")
142 #define BMIN_TEXT N_( "Minimum B (deprecated)")
143 #define BMIN_LONGTEXT N_( "This setting is deprecated and not used anymore" )
145 #define BMAX_TEXT N_( "Maximum B (deprecated)")
146 #define BMAX_LONGTEXT N_( "This setting is deprecated and not used anymore")
148 #define DTS_TEXT N_("DTS delay (ms)")
149 #define DTS_LONGTEXT N_("Delay the DTS (decoding time " \
150 "stamps) and PTS (presentation timestamps) of the data in the " \
151 "stream, compared to the PCRs. This allows for some buffering inside " \
152 "the client decoder.")
154 #define ACRYPT_TEXT N_("Crypt audio")
155 #define ACRYPT_LONGTEXT N_("Crypt audio using CSA")
156 #define VCRYPT_TEXT N_("Crypt video")
157 #define VCRYPT_LONGTEXT N_("Crypt video using CSA")
159 #define CK_TEXT N_("CSA Key")
160 #define CK_LONGTEXT N_("CSA encryption key. This must be a " \
161 "16 char string (8 hexadecimal bytes).")
163 #define CK2_TEXT N_("Second CSA Key")
164 #define CK2_LONGTEXT N_("The even CSA encryption key. This must be a " \
165 "16 char string (8 hexadecimal bytes).")
167 #define CU_TEXT N_("CSA Key in use")
168 #define CU_LONGTEXT N_("CSA encryption key used. It can be the odd/first/1 " \
169 "(default) or the even/second/2 one.")
171 #define CPKT_TEXT N_("Packet size in bytes to encrypt")
172 #define CPKT_LONGTEXT N_("Size of the TS packet to encrypt. " \
173 "The encryption routines subtract the TS-header from the value before " \
174 "encrypting." )
176 #define SOUT_CFG_PREFIX "sout-ts-"
177 #define MAX_PMT 64 /* Maximum number of programs. FIXME: I just chose an arbitrary number. Where is the maximum in the spec? */
178 #define MAX_PMT_PID 64 /* Maximum pids in each pmt. FIXME: I just chose an arbitrary number. Where is the maximum in the spec? */
180 vlc_module_begin ()
181 set_description( N_("TS muxer (libdvbpsi)") )
182 set_shortname( "MPEG-TS")
183 set_category( CAT_SOUT )
184 set_subcategory( SUBCAT_SOUT_MUX )
185 set_capability( "sout mux", 120 )
186 add_shortcut( "ts" )
188 add_integer(SOUT_CFG_PREFIX "pid-video", 0, VPID_TEXT, VPID_LONGTEXT, true)
189 add_integer(SOUT_CFG_PREFIX "pid-audio", 0, APID_TEXT, APID_LONGTEXT, true)
190 add_integer(SOUT_CFG_PREFIX "pid-spu", 0, SPUPID_TEXT, SPUPID_LONGTEXT, true)
191 add_integer(SOUT_CFG_PREFIX "pid-pmt", 0, PMTPID_TEXT, PMTPID_LONGTEXT, true)
192 add_integer(SOUT_CFG_PREFIX "tsid", 0, TSID_TEXT, TSID_LONGTEXT, true)
193 add_integer(SOUT_CFG_PREFIX "netid", 0, NETID_TEXT, NETID_LONGTEXT, true)
194 add_string(SOUT_CFG_PREFIX "program-pmt", NULL, PMTPROG_TEXT, PMTPROG_LONGTEXT, true)
195 add_bool(SOUT_CFG_PREFIX "es-id-pid", false, PID_TEXT, PID_LONGTEXT, true)
196 add_string(SOUT_CFG_PREFIX "muxpmt", NULL, MUXPMT_TEXT, MUXPMT_LONGTEXT, true)
197 add_string(SOUT_CFG_PREFIX "sdtdesc", NULL, SDTDESC_TEXT, SDTDESC_LONGTEXT, true)
198 add_bool(SOUT_CFG_PREFIX "alignment", true, ALIGNMENT_TEXT, ALIGNMENT_LONGTEXT, true)
200 add_integer(SOUT_CFG_PREFIX "shaping", 200, SHAPING_TEXT, SHAPING_LONGTEXT, true)
201 add_bool(SOUT_CFG_PREFIX "use-key-frames", false, KEYF_TEXT, KEYF_LONGTEXT, true)
203 add_integer( SOUT_CFG_PREFIX "pcr", 70, PCR_TEXT, PCR_LONGTEXT, true)
204 add_integer( SOUT_CFG_PREFIX "bmin", 0, BMIN_TEXT, BMIN_LONGTEXT, true)
205 add_integer( SOUT_CFG_PREFIX "bmax", 0, BMAX_TEXT, BMAX_LONGTEXT, true)
206 add_integer( SOUT_CFG_PREFIX "dts-delay", 400, DTS_TEXT, DTS_LONGTEXT, true)
208 add_bool( SOUT_CFG_PREFIX "crypt-audio", true, ACRYPT_TEXT, ACRYPT_LONGTEXT, true)
209 add_bool( SOUT_CFG_PREFIX "crypt-video", true, VCRYPT_TEXT, VCRYPT_LONGTEXT, true)
210 add_string( SOUT_CFG_PREFIX "csa-ck", NULL, CK_TEXT, CK_LONGTEXT, true)
211 add_string( SOUT_CFG_PREFIX "csa2-ck", NULL, CK2_TEXT, CK2_LONGTEXT, true)
212 add_string( SOUT_CFG_PREFIX "csa-use", "1", CU_TEXT, CU_LONGTEXT, true)
213 add_integer(SOUT_CFG_PREFIX "csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true)
215 set_callbacks( Open, Close )
216 vlc_module_end ()
218 /*****************************************************************************
219 * Local data structures
220 *****************************************************************************/
221 static const char *const ppsz_sout_options[] = {
222 "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid",
223 "netid", "sdtdesc",
224 "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
225 "dts-delay", "csa-ck", "csa2-ck", "csa-use", "csa-pkt", "crypt-audio", "crypt-video",
226 "muxpmt", "program-pmt", "alignment",
227 NULL
230 typedef struct pmt_map_t /* Holds the mapping between the pmt-pid/pmt table */
232 int i_pid;
233 unsigned i_prog;
234 } pmt_map_t;
236 typedef struct sdt_desc_t
238 char *psz_provider;
239 char *psz_service_name; /* name of program */
240 } sdt_desc_t;
242 typedef struct
244 int i_depth;
245 block_t *p_first;
246 block_t **pp_last;
247 } sout_buffer_chain_t;
249 static inline void BufferChainInit ( sout_buffer_chain_t *c )
251 c->i_depth = 0;
252 c->p_first = NULL;
253 c->pp_last = &c->p_first;
256 static inline void BufferChainAppend( sout_buffer_chain_t *c, block_t *b )
258 *c->pp_last = b;
259 c->i_depth++;
261 while( b->p_next )
263 b = b->p_next;
264 c->i_depth++;
266 c->pp_last = &b->p_next;
269 static inline block_t *BufferChainGet( sout_buffer_chain_t *c )
271 block_t *b = c->p_first;
273 if( b )
275 c->i_depth--;
276 c->p_first = b->p_next;
278 if( c->p_first == NULL )
280 c->pp_last = &c->p_first;
283 b->p_next = NULL;
285 return b;
288 static inline block_t *BufferChainPeek( sout_buffer_chain_t *c )
290 block_t *b = c->p_first;
292 return b;
295 static inline void BufferChainClean( sout_buffer_chain_t *c )
297 block_t *b;
299 while( ( b = BufferChainGet( c ) ) )
301 block_Release( b );
303 BufferChainInit( c );
306 typedef struct ts_stream_t
308 int i_pid;
309 vlc_fourcc_t i_codec;
311 int i_stream_type;
312 int i_stream_id;
313 int i_continuity_counter;
314 bool b_discontinuity;
316 /* to be used for carriege of DIV3 */
317 vlc_fourcc_t i_bih_codec;
318 int i_bih_width, i_bih_height;
320 /* Specific to mpeg4 in mpeg2ts */
321 int i_es_id;
323 int i_extra;
324 uint8_t *p_extra;
326 /* language is iso639-2T */
327 int i_langs;
328 uint8_t *lang;
330 sout_buffer_chain_t chain_pes;
331 mtime_t i_pes_dts;
332 mtime_t i_pes_length;
333 int i_pes_used;
334 bool b_key_frame;
336 } ts_stream_t;
338 struct sout_mux_sys_t
340 int i_pcr_pid;
341 sout_input_t *p_pcr_input;
343 vlc_mutex_t csa_lock;
345 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
346 dvbpsi_t *p_dvbpsi;
347 #endif
348 bool b_es_id_pid;
349 bool b_sdt;
350 int i_pid_video;
351 int i_pid_audio;
352 int i_pid_spu;
353 int i_pid_free; /* first usable pid */
355 int i_tsid;
356 int i_netid;
357 unsigned i_num_pmt;
358 int i_pmtslots;
359 int i_pat_version_number;
360 ts_stream_t pat;
362 int i_pmt_version_number;
363 ts_stream_t pmt[MAX_PMT];
364 pmt_map_t pmtmap[MAX_PMT_PID];
365 int i_pmt_program_number[MAX_PMT];
366 sdt_desc_t sdt_descriptors[MAX_PMT];
367 bool b_data_alignment;
369 int i_mpeg4_streams;
371 ts_stream_t sdt;
372 dvbpsi_pmt_t *dvbpmt;
374 /* for TS building */
375 int64_t i_bitrate_min;
376 int64_t i_bitrate_max;
378 int64_t i_shaping_delay;
379 int64_t i_pcr_delay;
381 int64_t i_dts_delay;
382 mtime_t first_dts;
384 bool b_use_key_frames;
386 mtime_t i_pcr; /* last PCR emited */
388 csa_t *csa;
389 int i_csa_pkt_size;
390 bool b_crypt_audio;
391 bool b_crypt_video;
394 /* Reserve a pid and return it */
395 static int AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
397 int i_pid;
398 if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
400 i_pid = p_sys->i_pid_video;
401 p_sys->i_pid_video = 0;
403 else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
405 i_pid = p_sys->i_pid_audio;
406 p_sys->i_pid_audio = 0;
408 else if ( i_cat == SPU_ES && p_sys->i_pid_spu )
410 i_pid = p_sys->i_pid_spu;
411 p_sys->i_pid_spu = 0;
413 else
415 i_pid = ++p_sys->i_pid_free;
417 return i_pid;
420 static int pmtcompare( const void *pa, const void *pb )
422 int id1 = ((pmt_map_t *)pa)->i_pid;
423 int id2 = ((pmt_map_t *)pb)->i_pid;
425 return id1 - id2;
428 static int intcompare( const void *pa, const void *pb )
430 return *(int*)pa - *(int*)pb;
433 /*****************************************************************************
434 * Local prototypes
435 *****************************************************************************/
436 static int Control ( sout_mux_t *, int, va_list );
437 static int AddStream( sout_mux_t *, sout_input_t * );
438 static int DelStream( sout_mux_t *, sout_input_t * );
439 static int Mux ( sout_mux_t * );
441 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo );
442 static block_t *Add_ADTS( block_t *, es_format_t * );
443 static void TSSchedule ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
444 mtime_t i_pcr_length, mtime_t i_pcr_dts );
445 static void TSDate ( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
446 mtime_t i_pcr_length, mtime_t i_pcr_dts );
447 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
448 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
450 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, bool b_pcr );
451 static void TSSetPCR( block_t *p_ts, mtime_t i_dts );
453 static csa_t *csaSetup( vlc_object_t *p_this )
455 sout_mux_t *p_mux = (sout_mux_t*)p_this;
456 sout_mux_sys_t *p_sys = p_mux->p_sys;
457 char *csack = var_CreateGetNonEmptyStringCommand( p_mux, SOUT_CFG_PREFIX "csa-ck" );
458 if( !csack )
459 return NULL;
461 csa_t *csa = csa_New();
463 if( csa_SetCW( p_this, csa, csack, true ) )
465 free(csack);
466 csa_Delete( csa );
467 return NULL;
470 vlc_mutex_init( &p_sys->csa_lock );
471 p_sys->b_crypt_audio = var_GetBool( p_mux, SOUT_CFG_PREFIX "crypt-audio" );
472 p_sys->b_crypt_video = var_GetBool( p_mux, SOUT_CFG_PREFIX "crypt-video" );
474 char *csa2ck = var_CreateGetNonEmptyStringCommand( p_mux, SOUT_CFG_PREFIX "csa2-ck");
475 if (!csa2ck || csa_SetCW( p_this, csa, csa2ck, false ) )
476 csa_SetCW( p_this, csa, csack, false );
477 free(csa2ck);
479 var_Create( p_mux, SOUT_CFG_PREFIX "csa-use", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
480 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
481 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, (void *)1 );
482 var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
484 vlc_value_t use_val;
485 var_Get( p_mux, SOUT_CFG_PREFIX "csa-use", &use_val );
486 if ( var_Set( p_mux, SOUT_CFG_PREFIX "csa-use", use_val ) )
487 var_SetString( p_mux, SOUT_CFG_PREFIX "csa-use", "odd" );
488 free( use_val.psz_string );
490 p_sys->i_csa_pkt_size = var_GetInteger( p_mux, SOUT_CFG_PREFIX "csa-pkt" );
491 if( p_sys->i_csa_pkt_size < 12 || p_sys->i_csa_pkt_size > 188 )
493 msg_Err( p_mux, "wrong packet size %d specified",
494 p_sys->i_csa_pkt_size );
495 p_sys->i_csa_pkt_size = 188;
498 msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
500 free(csack);
502 return csa;
505 /*****************************************************************************
506 * Open:
507 *****************************************************************************/
508 static int Open( vlc_object_t *p_this )
510 sout_mux_t *p_mux =(sout_mux_t*)p_this;
511 sout_mux_sys_t *p_sys = NULL;
513 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
515 p_sys = calloc( 1, sizeof( sout_mux_sys_t ) );
516 if( !p_sys )
517 return VLC_ENOMEM;
518 p_sys->i_num_pmt = 1;
521 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
522 p_sys->p_dvbpsi = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
523 if( !p_sys->p_dvbpsi )
525 free( p_sys );
526 return VLC_ENOMEM;
528 p_sys->p_dvbpsi->p_sys = (void *) p_mux;
529 #endif
531 p_sys->b_es_id_pid = var_GetBool( p_mux, SOUT_CFG_PREFIX "es-id-pid" );
534 fetch string of pmts. Here's a sample: --sout-ts-muxpmt="0x451,0x200,0x28a,0x240,,0x450,0x201,0x28b,0x241,,0x452,0x202,0x28c,0x242"
535 This would mean 0x451, 0x200, 0x28a, 0x240 would fall under one pmt (program), 0x450,0x201,0x28b,0x241 would fall under another
537 char *muxpmt = var_GetNonEmptyString(p_mux, SOUT_CFG_PREFIX "muxpmt");
538 for (char *psz = muxpmt; psz; )
540 char *psz_next;
541 uint16_t i_pid = strtoul( psz, &psz_next, 0 );
542 psz = *psz_next ? &psz_next[1] : NULL;
544 if ( i_pid == 0 )
546 if ( ++p_sys->i_num_pmt > MAX_PMT )
548 msg_Err( p_mux, "Number of PMTs > %d)", MAX_PMT );
549 p_sys->i_num_pmt = MAX_PMT;
552 else
554 p_sys->pmtmap[p_sys->i_pmtslots].i_pid = i_pid;
555 p_sys->pmtmap[p_sys->i_pmtslots].i_prog = p_sys->i_num_pmt - 1;
556 if ( ++p_sys->i_pmtslots >= MAX_PMT_PID )
558 msg_Err( p_mux, "Number of pids in PMT > %d", MAX_PMT_PID );
559 p_sys->i_pmtslots = MAX_PMT_PID - 1;
563 /* Now sort according to pids for fast search later on */
564 qsort( (void *)p_sys->pmtmap, p_sys->i_pmtslots,
565 sizeof(pmt_map_t), pmtcompare );
566 free(muxpmt);
568 unsigned short subi[3];
569 vlc_rand_bytes(subi, sizeof(subi));
570 p_sys->i_pat_version_number = nrand48(subi) & 0x1f;
572 vlc_value_t val;
573 var_Get( p_mux, SOUT_CFG_PREFIX "tsid", &val );
574 if ( val.i_int )
575 p_sys->i_tsid = val.i_int;
576 else
577 p_sys->i_tsid = nrand48(subi) & 0xffff;
579 p_sys->i_netid = nrand48(subi) & 0xffff;
581 var_Get( p_mux, SOUT_CFG_PREFIX "netid", &val );
582 if ( val.i_int )
583 p_sys->i_netid = val.i_int;
585 p_sys->i_pmt_version_number = nrand48(subi) & 0x1f;
586 p_sys->sdt.i_pid = 0x11;
588 char *sdtdesc = var_GetNonEmptyString( p_mux, SOUT_CFG_PREFIX "sdtdesc" );
590 /* Syntax is provider_sdt1,service_name_sdt1,provider_sdt2,service_name_sdt2... */
591 if( sdtdesc )
593 p_sys->b_sdt = true;
595 char *psz_sdttoken = sdtdesc;
597 for (int i = 0; i < MAX_PMT * 2 && psz_sdttoken; i++)
599 sdt_desc_t *sdt = &p_sys->sdt_descriptors[i/2];
600 char *psz_end = strchr( psz_sdttoken, ',' );
601 if ( psz_end )
602 *psz_end++ = '\0';
604 if (i % 2)
605 sdt->psz_service_name = strdup(psz_sdttoken);
606 else
607 sdt->psz_provider = strdup(psz_sdttoken);
609 psz_sdttoken = psz_end;
611 free(sdtdesc);
614 p_sys->b_data_alignment = var_GetBool( p_mux, SOUT_CFG_PREFIX "alignment" );
616 char *pgrpmt = var_GetNonEmptyString(p_mux, SOUT_CFG_PREFIX "program-pmt");
617 if( pgrpmt )
619 char *psz = pgrpmt;
620 char *psz_next = psz;
622 for (int i = 0; psz; )
624 uint16_t i_pid = strtoul( psz, &psz_next, 0 );
625 if( psz_next[0] != '\0' )
626 psz = &psz_next[1];
627 else
628 psz = NULL;
630 if( i_pid == 0 )
632 if( i >= MAX_PMT )
633 msg_Err( p_mux, "Number of PMTs > maximum (%d)", MAX_PMT );
635 else
637 p_sys->i_pmt_program_number[i] = i_pid;
638 i++;
641 free(pgrpmt);
643 else
645 /* Option not specified, use 1, 2, 3... */
646 for (unsigned i = 0; i < p_sys->i_num_pmt; i++ )
647 p_sys->i_pmt_program_number[i] = i + 1;
650 var_Get( p_mux, SOUT_CFG_PREFIX "pid-pmt", &val );
651 if( !val.i_int ) /* Does this make any sense? */
652 val.i_int = 0x42;
653 for (unsigned i = 0; i < p_sys->i_num_pmt; i++ )
654 p_sys->pmt[i].i_pid = val.i_int + i;
656 p_sys->i_pid_free = p_sys->pmt[p_sys->i_num_pmt - 1].i_pid + 1;
658 p_sys->i_pid_video = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-video" );
659 if ( p_sys->i_pid_video > p_sys->i_pid_free )
661 p_sys->i_pid_free = p_sys->i_pid_video + 1;
664 p_sys->i_pid_audio = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-audio" );
665 if ( p_sys->i_pid_audio > p_sys->i_pid_free )
667 p_sys->i_pid_free = p_sys->i_pid_audio + 1;
670 p_sys->i_pid_spu = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-spu" );
671 if ( p_sys->i_pid_spu > p_sys->i_pid_free )
673 p_sys->i_pid_free = p_sys->i_pid_spu + 1;
676 p_sys->i_pcr_pid = 0x1fff;
678 /* Allow to create constrained stream */
679 p_sys->i_bitrate_min = var_GetInteger( p_mux, SOUT_CFG_PREFIX "bmin" );
681 p_sys->i_bitrate_max = var_GetInteger( p_mux, SOUT_CFG_PREFIX "bmax" );
683 if( p_sys->i_bitrate_min > 0 && p_sys->i_bitrate_max > 0 &&
684 p_sys->i_bitrate_min > p_sys->i_bitrate_max )
686 msg_Err( p_mux, "incompatible minimum and maximum bitrate, "
687 "disabling bitrate control" );
688 p_sys->i_bitrate_min = 0;
689 p_sys->i_bitrate_max = 0;
691 if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
693 msg_Err( p_mux, "bmin and bmax no more supported "
694 "(if you need them report it)" );
697 var_Get( p_mux, SOUT_CFG_PREFIX "shaping", &val );
698 p_sys->i_shaping_delay = val.i_int * 1000;
699 if( p_sys->i_shaping_delay <= 0 )
701 msg_Err( p_mux,
702 "invalid shaping (%"PRId64"ms) resetting to 200ms",
703 p_sys->i_shaping_delay / 1000 );
704 p_sys->i_shaping_delay = 200000;
707 var_Get( p_mux, SOUT_CFG_PREFIX "pcr", &val );
708 p_sys->i_pcr_delay = val.i_int * 1000;
709 if( p_sys->i_pcr_delay <= 0 ||
710 p_sys->i_pcr_delay >= p_sys->i_shaping_delay )
712 msg_Err( p_mux,
713 "invalid pcr delay (%"PRId64"ms) resetting to 70ms",
714 p_sys->i_pcr_delay / 1000 );
715 p_sys->i_pcr_delay = 70000;
718 var_Get( p_mux, SOUT_CFG_PREFIX "dts-delay", &val );
719 p_sys->i_dts_delay = val.i_int * 1000;
721 msg_Dbg( p_mux, "shaping=%"PRId64" pcr=%"PRId64" dts_delay=%"PRId64,
722 p_sys->i_shaping_delay, p_sys->i_pcr_delay, p_sys->i_dts_delay );
724 p_sys->b_use_key_frames = var_GetBool( p_mux, SOUT_CFG_PREFIX "use-key-frames" );
726 p_mux->p_sys = p_sys;
728 p_sys->csa = csaSetup(p_this);
730 p_mux->pf_control = Control;
731 p_mux->pf_addstream = AddStream;
732 p_mux->pf_delstream = DelStream;
733 p_mux->pf_mux = Mux;
735 return VLC_SUCCESS;
738 /*****************************************************************************
739 * Close:
740 *****************************************************************************/
741 static void Close( vlc_object_t * p_this )
743 sout_mux_t *p_mux = (sout_mux_t*)p_this;
744 sout_mux_sys_t *p_sys = p_mux->p_sys;
746 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
747 if( p_sys->p_dvbpsi )
748 dvbpsi_delete( p_sys->p_dvbpsi );
749 #endif
751 if( p_sys->csa )
753 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, NULL );
754 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
755 var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
756 csa_Delete( p_sys->csa );
757 vlc_mutex_destroy( &p_sys->csa_lock );
760 for (int i = 0; i < MAX_PMT; i++ )
762 free( p_sys->sdt_descriptors[i].psz_service_name );
763 free( p_sys->sdt_descriptors[i].psz_provider );
766 free( p_sys->dvbpmt );
767 free( p_sys );
770 /*****************************************************************************
771 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
772 *****************************************************************************/
773 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
774 vlc_value_t oldval, vlc_value_t newval,
775 void *p_data )
777 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
778 sout_mux_t *p_mux = (sout_mux_t*)p_this;
779 sout_mux_sys_t *p_sys = p_mux->p_sys;
780 int ret;
782 vlc_mutex_lock(&p_sys->csa_lock);
783 ret = csa_SetCW(p_this, p_sys->csa, newval.psz_string, !!(intptr_t)p_data);
784 vlc_mutex_unlock(&p_sys->csa_lock);
786 return ret;
789 /*****************************************************************************
790 * ActiveKeyCallback: called when changing the active (in use) encryption key on the fly.
791 *****************************************************************************/
792 static int ActiveKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
793 vlc_value_t oldval, vlc_value_t newval,
794 void *p_data )
796 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
797 sout_mux_t *p_mux = (sout_mux_t*)p_this;
798 sout_mux_sys_t *p_sys = p_mux->p_sys;
799 int i_res, use_odd = -1;
801 if( !strcmp(newval.psz_string, "odd" ) ||
802 !strcmp(newval.psz_string, "first" ) ||
803 !strcmp(newval.psz_string, "1" ) )
805 use_odd = 1;
807 else if( !strcmp(newval.psz_string, "even" ) ||
808 !strcmp(newval.psz_string, "second" ) ||
809 !strcmp(newval.psz_string, "2" ) )
811 use_odd = 0;
814 if (use_odd < 0)
815 return VLC_EBADVAR;
817 vlc_mutex_lock( &p_sys->csa_lock );
818 i_res = csa_UseKey( p_this, p_sys->csa, use_odd );
819 vlc_mutex_unlock( &p_sys->csa_lock );
821 return i_res;
824 /*****************************************************************************
825 * Control:
826 *****************************************************************************/
827 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
829 VLC_UNUSED(p_mux);
830 bool *pb_bool;
831 char **ppsz;
833 switch( i_query )
835 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
836 pb_bool = (bool*)va_arg( args, bool * );
837 *pb_bool = true;
838 return VLC_SUCCESS;
840 case MUX_GET_ADD_STREAM_WAIT:
841 pb_bool = (bool*)va_arg( args, bool * );
842 *pb_bool = false;
843 return VLC_SUCCESS;
845 case MUX_GET_MIME:
846 ppsz = (char**)va_arg( args, char ** );
847 *ppsz = strdup( "video/mp2t" );
848 return VLC_SUCCESS;
850 default:
851 return VLC_EGENERIC;
855 /* returns a pointer to a valid string, with length 0 or 3 */
856 static const char *GetIso639_2LangCode(const char *lang)
858 const iso639_lang_t *pl;
860 if (strlen(lang) == 2)
862 pl = GetLang_1(lang);
864 else
866 pl = GetLang_2B(lang); /* try native code first */
867 if (!*pl->psz_iso639_2T)
868 pl = GetLang_2T(lang); /* else fallback to english code */
872 return pl->psz_iso639_2T; /* returns the english code */
875 /*****************************************************************************
876 * AddStream: called for each stream addition
877 *****************************************************************************/
878 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
880 sout_mux_sys_t *p_sys = p_mux->p_sys;
881 ts_stream_t *p_stream;
883 p_input->p_sys = p_stream = calloc( 1, sizeof( ts_stream_t ) );
884 if( !p_stream )
885 goto oom;
887 if ( p_sys->b_es_id_pid )
888 p_stream->i_pid = p_input->p_fmt->i_id & 0x1fff;
889 else
890 p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
892 p_stream->i_codec = p_input->p_fmt->i_codec;
894 p_stream->i_stream_type = -1;
895 switch( p_input->p_fmt->i_codec )
897 /* VIDEO */
899 case VLC_CODEC_MPGV:
900 case VLC_CODEC_MP2V:
901 case VLC_CODEC_MP1V:
902 /* TODO: do we need to check MPEG-I/II ? */
903 p_stream->i_stream_type = 0x02;
904 p_stream->i_stream_id = 0xe0;
905 break;
906 case VLC_CODEC_MP4V:
907 p_stream->i_stream_type = 0x10;
908 p_stream->i_stream_id = 0xe0;
909 p_stream->i_es_id = p_stream->i_pid;
910 break;
911 case VLC_CODEC_HEVC:
912 p_stream->i_stream_type = 0x24;
913 p_stream->i_stream_id = 0xe0;
914 break;
915 case VLC_CODEC_H264:
916 p_stream->i_stream_type = 0x1b;
917 p_stream->i_stream_id = 0xe0;
918 break;
919 /* XXX dirty dirty but somebody want crapy MS-codec XXX */
920 case VLC_CODEC_H263I:
921 case VLC_CODEC_H263:
922 case VLC_CODEC_WMV3:
923 case VLC_CODEC_WMV2:
924 case VLC_CODEC_WMV1:
925 case VLC_CODEC_DIV3:
926 case VLC_CODEC_DIV2:
927 case VLC_CODEC_DIV1:
928 case VLC_CODEC_MJPG:
929 p_stream->i_stream_type = 0xa0; /* private */
930 p_stream->i_stream_id = 0xa0; /* beurk */
931 p_stream->i_bih_codec = p_input->p_fmt->i_codec;
932 p_stream->i_bih_width = p_input->p_fmt->video.i_width;
933 p_stream->i_bih_height = p_input->p_fmt->video.i_height;
934 break;
935 case VLC_CODEC_DIRAC:
936 /* stream_id makes use of stream_id_extension */
937 p_stream->i_stream_id = (PES_EXTENDED_STREAM_ID << 8) | 0x60;
938 p_stream->i_stream_type = 0xd1;
939 break;
941 /* AUDIO */
943 case VLC_CODEC_MPGA:
944 case VLC_CODEC_MP3:
945 p_stream->i_stream_type =
946 p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
947 p_stream->i_stream_id = 0xc0;
948 break;
949 case VLC_CODEC_A52:
950 p_stream->i_stream_type = 0x81;
951 p_stream->i_stream_id = 0xbd;
952 break;
953 case VLC_CODEC_DVD_LPCM:
954 p_stream->i_stream_type = 0x83;
955 p_stream->i_stream_id = 0xbd;
956 break;
957 case VLC_CODEC_OPUS:
958 if (p_input->p_fmt->audio.i_channels > 8) {
959 msg_Err(p_mux, "Too many opus channels (%d > 8)",
960 p_input->p_fmt->audio.i_channels);
961 break;
963 case VLC_CODEC_EAC3:
964 case VLC_CODEC_DTS:
965 p_stream->i_stream_type = 0x06;
966 p_stream->i_stream_id = 0xbd;
967 break;
968 case VLC_CODEC_MP4A:
969 /* XXX: make that configurable in some way when LOAS
970 * is implemented for AAC in TS */
971 //p_stream->i_stream_type = 0x11; /* LOAS/LATM */
972 p_stream->i_stream_type = 0x0f; /* ADTS */
973 p_stream->i_stream_id = 0xc0;
974 p_stream->i_es_id = p_stream->i_pid;
975 break;
977 /* TEXT */
979 case VLC_CODEC_SPU:
980 p_stream->i_stream_type = 0x82;
981 p_stream->i_stream_id = 0xbd;
982 break;
983 case VLC_CODEC_SUBT:
984 p_stream->i_stream_type = 0x12;
985 p_stream->i_stream_id = 0xfa;
986 p_sys->i_mpeg4_streams++;
987 p_stream->i_es_id = p_stream->i_pid;
988 break;
989 case VLC_CODEC_DVBS:
990 p_stream->i_stream_type = 0x06;
991 p_stream->i_es_id = p_input->p_fmt->subs.dvb.i_id;
992 p_stream->i_stream_id = 0xbd;
993 break;
994 case VLC_CODEC_TELETEXT:
995 p_stream->i_stream_type = 0x06;
996 p_stream->i_stream_id = 0xbd; /* FIXME */
997 break;
1000 if (p_stream->i_stream_type == -1)
1002 msg_Warn( p_mux, "rejecting stream with unsupported codec %4.4s",
1003 (char*)&p_stream->i_codec );
1004 free( p_stream );
1005 return VLC_EGENERIC;
1008 p_stream->i_langs = 1 + p_input->p_fmt->i_extra_languages;
1009 p_stream->lang = calloc(1, p_stream->i_langs * 4);
1010 if( !p_stream->lang )
1011 goto oom;
1013 msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d",
1014 (char*)&p_stream->i_codec, p_stream->i_pid );
1016 for (int i = 0; i < p_stream->i_langs; i++) {
1017 char *lang = (i == 0)
1018 ? p_input->p_fmt->psz_language
1019 : p_input->p_fmt->p_extra_languages[i-1].psz_language;
1021 if (!lang)
1022 continue;
1024 const char *code = GetIso639_2LangCode(lang);
1025 if (*code)
1027 memcpy(&p_stream->lang[i*4], code, 3);
1028 p_stream->lang[i*4+3] = 0x00; /* audio type: 0x00 undefined */
1029 msg_Dbg( p_mux, " - lang=%3.3s", &p_stream->lang[i*4] );
1033 /* Create decoder specific info for subt */
1034 if( p_stream->i_codec == VLC_CODEC_SUBT )
1036 p_stream->i_extra = 55;
1037 p_stream->p_extra = malloc( p_stream->i_extra );
1038 if (!p_stream->p_extra)
1039 goto oom;
1041 uint8_t *p = p_stream->p_extra;
1042 p[0] = 0x10; /* textFormat, 0x10 for 3GPP TS 26.245 */
1043 p[1] = 0x00; /* flags: 1b: associated video info flag
1044 3b: reserved
1045 1b: duration flag
1046 3b: reserved */
1047 p[2] = 52; /* remaining size */
1049 p += 3;
1051 p[0] = p[1] = p[2] = p[3] = 0; p+=4; /* display flags */
1052 *p++ = 0; /* horizontal justification (-1: left, 0 center, 1 right) */
1053 *p++ = 1; /* vertical justification (-1: top, 0 center, 1 bottom) */
1055 p[0] = p[1] = p[2] = 0x00; p+=3;/* background rgb */
1056 *p++ = 0xff; /* background a */
1058 p[0] = p[1] = 0; p += 2; /* text box top */
1059 p[0] = p[1] = 0; p += 2; /* text box left */
1060 p[0] = p[1] = 0; p += 2; /* text box bottom */
1061 p[0] = p[1] = 0; p += 2; /* text box right */
1063 p[0] = p[1] = 0; p += 2; /* start char */
1064 p[0] = p[1] = 0; p += 2; /* end char */
1065 p[0] = p[1] = 0; p += 2; /* default font id */
1067 *p++ = 0; /* font style flags */
1068 *p++ = 12; /* font size */
1070 p[0] = p[1] = p[2] = 0x00; p+=3;/* foreground rgb */
1071 *p++ = 0x00; /* foreground a */
1073 p[0] = p[1] = p[2] = 0; p[3] = 22; p += 4;
1074 memcpy( p, "ftab", 4 ); p += 4;
1075 *p++ = 0; *p++ = 1; /* entry count */
1076 p[0] = p[1] = 0; p += 2; /* font id */
1077 *p++ = 9; /* font name length */
1078 memcpy( p, "Helvetica", 9 ); /* font name */
1080 else
1082 /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
1083 es_format_t *fmt = p_input->p_fmt;
1084 if( fmt->i_extra > 0 )
1086 p_stream->i_extra = fmt->i_extra;
1087 p_stream->p_extra = malloc( fmt->i_extra );
1088 if( !p_stream->p_extra )
1089 goto oom;
1091 memcpy( p_stream->p_extra, fmt->p_extra, fmt->i_extra );
1095 /* Init pes chain */
1096 BufferChainInit( &p_stream->chain_pes );
1098 /* We only change PMT version (PAT isn't changed) */
1099 p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
1101 /* Update pcr_pid */
1102 if( p_input->p_fmt->i_cat != SPU_ES &&
1103 ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
1105 if( p_sys->p_pcr_input )
1107 /* There was already a PCR stream, so clean context */
1108 /* FIXME */
1110 p_sys->i_pcr_pid = p_stream->i_pid;
1111 p_sys->p_pcr_input = p_input;
1113 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1116 return VLC_SUCCESS;
1118 oom:
1119 if(p_stream)
1121 free(p_stream->lang);
1122 free(p_stream);
1124 return VLC_ENOMEM;
1127 /*****************************************************************************
1128 * DelStream: called before a stream deletion
1129 *****************************************************************************/
1130 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1132 sout_mux_sys_t *p_sys = p_mux->p_sys;
1133 ts_stream_t *p_stream = (ts_stream_t*)p_input->p_sys;
1134 int pid;
1136 msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
1138 if( p_sys->i_pcr_pid == p_stream->i_pid )
1140 /* Find a new pcr stream (Prefer Video Stream) */
1141 p_sys->i_pcr_pid = 0x1fff;
1142 p_sys->p_pcr_input = NULL;
1143 for (int i = 0; i < p_mux->i_nb_inputs; i++ )
1145 if( p_mux->pp_inputs[i] == p_input )
1147 continue;
1150 if( p_mux->pp_inputs[i]->p_fmt->i_cat == VIDEO_ES )
1152 p_sys->i_pcr_pid =
1153 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1154 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1155 break;
1157 else if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES &&
1158 p_sys->i_pcr_pid == 0x1fff )
1160 p_sys->i_pcr_pid =
1161 ((ts_stream_t*)p_mux->pp_inputs[i]->p_sys)->i_pid;
1162 p_sys->p_pcr_input= p_mux->pp_inputs[i];
1165 if( p_sys->p_pcr_input )
1167 /* Empty TS buffer */
1168 /* FIXME */
1170 msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
1173 /* Empty all data in chain_pes */
1174 BufferChainClean( &p_stream->chain_pes );
1176 free(p_stream->lang);
1177 free( p_stream->p_extra );
1178 if( p_stream->i_stream_id == 0xfa ||
1179 p_stream->i_stream_id == 0xfb ||
1180 p_stream->i_stream_id == 0xfe )
1182 p_sys->i_mpeg4_streams--;
1185 pid = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-video" );
1186 if ( pid > 0 && pid == p_stream->i_pid )
1188 p_sys->i_pid_video = pid;
1189 msg_Dbg( p_mux, "freeing video PID %d", pid);
1191 pid = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-audio" );
1192 if ( pid > 0 && pid == p_stream->i_pid )
1194 p_sys->i_pid_audio = pid;
1195 msg_Dbg( p_mux, "freeing audio PID %d", pid);
1197 pid = var_GetInteger( p_mux, SOUT_CFG_PREFIX "pid-spu" );
1198 if ( pid > 0 && pid == p_stream->i_pid )
1200 p_sys->i_pid_spu = pid;
1201 msg_Dbg( p_mux, "freeing spu PID %d", pid);
1204 free( p_stream );
1206 /* We only change PMT version (PAT isn't changed) */
1207 p_sys->i_pmt_version_number++;
1208 p_sys->i_pmt_version_number %= 32;
1210 return VLC_SUCCESS;
1213 static void SetHeader( sout_buffer_chain_t *c,
1214 int depth )
1216 block_t *p_ts = BufferChainPeek( c );
1217 while( depth > 0 )
1219 p_ts = p_ts->p_next;
1220 depth--;
1222 p_ts->i_flags |= BLOCK_FLAG_HEADER;
1225 static block_t *Pack_Opus(block_t *p_data)
1227 lldiv_t d = lldiv(p_data->i_buffer, 255);
1228 p_data = block_Realloc(p_data, 2 + d.quot + 1, p_data->i_buffer);
1229 if (p_data) { /* no flags */
1230 p_data->p_buffer[0] = 0x7f;
1231 p_data->p_buffer[1] = 0xe0;
1232 memset(&p_data->p_buffer[2], 0xff, d.quot);
1233 p_data->p_buffer[2+d.quot] = d.rem;
1236 return p_data;
1239 /* returns true if needs more data */
1240 static bool MuxStreams(sout_mux_t *p_mux )
1242 sout_mux_sys_t *p_sys = p_mux->p_sys;
1243 ts_stream_t *p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
1245 sout_buffer_chain_t chain_ts;
1246 mtime_t i_shaping_delay = p_pcr_stream->b_key_frame
1247 ? p_pcr_stream->i_pes_length
1248 : p_sys->i_shaping_delay;
1250 bool b_ok = true;
1252 /* Accumulate enough data in the pcr stream (>i_shaping_delay) */
1253 /* Accumulate enough data in all other stream ( >= length of pcr)*/
1254 for (int i = -1; !b_ok || i < p_mux->i_nb_inputs; i++ )
1256 if (i == p_mux->i_nb_inputs)
1258 /* get enough PES packet for all input */
1259 b_ok = true;
1260 i = -1;
1262 sout_input_t *p_input;
1264 if( i == -1 )
1265 p_input = p_sys->p_pcr_input;
1266 else if( p_mux->pp_inputs[i]->p_sys == p_pcr_stream )
1267 continue;
1268 else
1269 p_input = p_mux->pp_inputs[i];
1270 ts_stream_t *p_stream = (ts_stream_t*)p_input->p_sys;
1272 if( ( p_stream != p_pcr_stream ||
1273 p_stream->i_pes_length >= i_shaping_delay ) &&
1274 p_stream->i_pes_dts + p_stream->i_pes_length >=
1275 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1276 continue;
1278 /* Need more data */
1279 if( block_FifoCount( p_input->p_fifo ) <= 1 )
1281 if( ( p_input->p_fmt->i_cat == AUDIO_ES ) ||
1282 ( p_input->p_fmt->i_cat == VIDEO_ES ) )
1284 /* We need more data */
1285 return true;
1287 else if( block_FifoCount( p_input->p_fifo ) <= 0 )
1289 /* spu, only one packet is needed */
1290 continue;
1292 else if( p_input->p_fmt->i_cat == SPU_ES )
1294 /* Don't mux the SPU yet if it is too early */
1295 block_t *p_spu = block_FifoShow( p_input->p_fifo );
1297 int64_t i_spu_delay = p_spu->i_dts - p_pcr_stream->i_pes_dts;
1298 if( ( i_spu_delay > i_shaping_delay ) &&
1299 ( i_spu_delay < 100 * CLOCK_FREQ ) )
1300 continue;
1302 if ( ( i_spu_delay >= 100 * CLOCK_FREQ ) ||
1303 ( i_spu_delay < CLOCK_FREQ / 100 ) )
1305 BufferChainClean( &p_stream->chain_pes );
1306 p_stream->i_pes_dts = 0;
1307 p_stream->i_pes_used = 0;
1308 p_stream->i_pes_length = 0;
1309 continue;
1313 b_ok = false;
1315 block_t *p_data;
1316 if( p_stream == p_pcr_stream || p_sys->b_data_alignment
1317 || ((p_input->p_fmt->i_codec != VLC_CODEC_MPGA ) &&
1318 (p_input->p_fmt->i_codec != VLC_CODEC_MP3) ) )
1320 p_data = block_FifoGet( p_input->p_fifo );
1321 if (p_data->i_pts <= VLC_TS_INVALID)
1322 p_data->i_pts = p_data->i_dts;
1324 if( p_input->p_fmt->i_codec == VLC_CODEC_MP4A )
1325 p_data = Add_ADTS( p_data, p_input->p_fmt );
1326 else if( p_input->p_fmt->i_codec == VLC_CODEC_OPUS )
1327 p_data = Pack_Opus( p_data );
1329 else
1330 p_data = FixPES( p_mux, p_input->p_fifo );
1332 if( block_FifoCount( p_input->p_fifo ) > 0 &&
1333 p_input->p_fmt->i_cat != SPU_ES )
1335 block_t *p_next = block_FifoShow( p_input->p_fifo );
1336 p_data->i_length = p_next->i_dts - p_data->i_dts;
1338 else if( p_input->p_fmt->i_codec !=
1339 VLC_CODEC_SUBT )
1340 p_data->i_length = 1000;
1342 if (p_sys->first_dts == 0)
1343 p_sys->first_dts = p_data->i_dts;
1345 if( ( p_pcr_stream->i_pes_dts > 0 &&
1346 p_data->i_dts - 10 * CLOCK_FREQ > p_pcr_stream->i_pes_dts +
1347 p_pcr_stream->i_pes_length ) ||
1348 p_data->i_dts < p_stream->i_pes_dts ||
1349 ( p_stream->i_pes_dts > 0 &&
1350 p_input->p_fmt->i_cat != SPU_ES &&
1351 p_data->i_dts - 10 * CLOCK_FREQ > p_stream->i_pes_dts +
1352 p_stream->i_pes_length ) )
1354 msg_Warn( p_mux, "packet with too strange dts "
1355 "(dts=%"PRId64",old=%"PRId64",pcr=%"PRId64")",
1356 p_data->i_dts, p_stream->i_pes_dts,
1357 p_pcr_stream->i_pes_dts );
1358 block_Release( p_data );
1360 BufferChainClean( &p_stream->chain_pes );
1361 p_stream->i_pes_dts = 0;
1362 p_stream->i_pes_used = 0;
1363 p_stream->i_pes_length = 0;
1365 if( p_input->p_fmt->i_cat != SPU_ES )
1367 BufferChainClean( &p_pcr_stream->chain_pes );
1368 p_pcr_stream->i_pes_dts = 0;
1369 p_pcr_stream->i_pes_used = 0;
1370 p_pcr_stream->i_pes_length = 0;
1373 continue;
1376 int i_header_size = 0;
1377 int i_max_pes_size = 0;
1378 int b_data_alignment = 0;
1379 if( p_input->p_fmt->i_cat == SPU_ES ) switch (p_input->p_fmt->i_codec)
1381 case VLC_CODEC_SUBT:
1382 /* Prepend header */
1383 p_data = block_Realloc( p_data, 2, p_data->i_buffer );
1384 p_data->p_buffer[0] = ( (p_data->i_buffer - 2) >> 8) & 0xff;
1385 p_data->p_buffer[1] = ( (p_data->i_buffer - 2) ) & 0xff;
1387 /* remove trailling \0 if any */
1388 if( p_data->i_buffer > 2 && !p_data->p_buffer[p_data->i_buffer-1] )
1389 p_data->i_buffer--;
1391 /* Append a empty sub (sub text only) */
1392 if( p_data->i_length > 0 &&
1393 ( p_data->i_buffer != 1 || *p_data->p_buffer != ' ' ) )
1395 block_t *p_spu = block_Alloc( 3 );
1397 p_spu->i_dts = p_data->i_dts + p_data->i_length;
1398 p_spu->i_pts = p_spu->i_dts;
1399 p_spu->i_length = 1000;
1401 p_spu->p_buffer[0] = 0;
1402 p_spu->p_buffer[1] = 1;
1403 p_spu->p_buffer[2] = ' ';
1405 EStoPES( &p_spu, p_input->p_fmt,
1406 p_stream->i_stream_id, 1, 0, 0, 0, p_sys->first_dts );
1407 p_data->p_next = p_spu;
1409 break;
1411 case VLC_CODEC_TELETEXT:
1412 /* EN 300 472 */
1413 i_header_size = 0x24;
1414 b_data_alignment = 1;
1415 break;
1417 case VLC_CODEC_DVBS:
1418 /* EN 300 743 */
1419 b_data_alignment = 1;
1420 break;
1422 else if( p_data->i_length < 0 || p_data->i_length > 2000000 )
1424 /* FIXME choose a better value, but anyway we
1425 * should never have to do that */
1426 p_data->i_length = 1000;
1429 p_stream->i_pes_length += p_data->i_length;
1430 if( p_stream->i_pes_dts == 0 )
1432 p_stream->i_pes_dts = p_data->i_dts;
1435 /* Convert to pes */
1436 if( p_stream->i_stream_id == 0xa0 && p_data->i_pts <= 0 )
1438 /* XXX yes I know, it's awful, but it's needed,
1439 * so don't remove it ... */
1440 p_data->i_pts = p_data->i_dts;
1443 if( p_input->p_fmt->i_codec == VLC_CODEC_DIRAC )
1445 b_data_alignment = 1;
1446 /* dirac pes packets should be unbounded in
1447 * length, specify a suitibly large max size */
1448 i_max_pes_size = INT_MAX;
1451 EStoPES ( &p_data, p_input->p_fmt, p_stream->i_stream_id,
1452 1, b_data_alignment, i_header_size,
1453 i_max_pes_size, p_sys->first_dts );
1455 BufferChainAppend( &p_stream->chain_pes, p_data );
1457 if( p_sys->b_use_key_frames && p_stream == p_pcr_stream
1458 && (p_data->i_flags & BLOCK_FLAG_TYPE_I)
1459 && !(p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME)
1460 && (p_stream->i_pes_length > 400000) )
1462 i_shaping_delay = p_stream->i_pes_length;
1463 p_stream->b_key_frame = 1;
1467 /* save */
1468 const mtime_t i_pcr_length = p_pcr_stream->i_pes_length;
1469 p_pcr_stream->b_key_frame = 0;
1471 /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
1472 /* 2: calculate non accurate total size of muxed ts */
1473 int i_packet_count = 0;
1474 for (int i = 0; i < p_mux->i_nb_inputs; i++ )
1476 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1478 /* False for pcr stream but it will be enough to do PCR algo */
1479 for (block_t *p_pes = p_stream->chain_pes.p_first; p_pes != NULL;
1480 p_pes = p_pes->p_next )
1482 int i_size = p_pes->i_buffer;
1483 if( p_pes->i_dts + p_pes->i_length >
1484 p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
1486 mtime_t i_frag = p_pcr_stream->i_pes_dts +
1487 p_pcr_stream->i_pes_length - p_pes->i_dts;
1488 if( i_frag < 0 )
1490 /* Next stream */
1491 break;
1493 i_size = p_pes->i_buffer * i_frag / p_pes->i_length;
1495 i_packet_count += ( i_size + 183 ) / 184;
1498 /* add overhead for PCR (not really exact) */
1499 i_packet_count += (8 * i_pcr_length / p_sys->i_pcr_delay + 175) / 176;
1501 /* 3: mux PES into TS */
1502 BufferChainInit( &chain_ts );
1503 /* append PAT/PMT -> FIXME with big pcr delay it won't have enough pat/pmt */
1504 bool pat_was_previous = true; //This is to prevent unnecessary double PAT/PMT insertions
1505 GetPAT( p_mux, &chain_ts );
1506 GetPMT( p_mux, &chain_ts );
1507 int i_packet_pos = 0;
1508 i_packet_count += chain_ts.i_depth;
1509 /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
1511 const mtime_t i_pcr_dts = p_pcr_stream->i_pes_dts;
1512 for (;;)
1514 int i_stream = -1;
1515 mtime_t i_dts = 0;
1516 ts_stream_t *p_stream;
1518 /* Select stream (lowest dts) */
1519 for (int i = 0; i < p_mux->i_nb_inputs; i++ )
1521 p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
1523 if( p_stream->i_pes_dts == 0 )
1525 continue;
1528 if( i_stream == -1 || p_stream->i_pes_dts < i_dts )
1530 i_stream = i;
1531 i_dts = p_stream->i_pes_dts;
1534 if( i_stream == -1 || i_dts > i_pcr_dts + i_pcr_length )
1536 break;
1538 p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
1539 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
1541 /* do we need to issue pcr */
1542 bool b_pcr = false;
1543 if( p_stream == p_pcr_stream &&
1544 i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >=
1545 p_sys->i_pcr + p_sys->i_pcr_delay )
1547 b_pcr = true;
1548 p_sys->i_pcr = i_pcr_dts + i_packet_pos *
1549 i_pcr_length / i_packet_count;
1552 /* Build the TS packet */
1553 block_t *p_ts = TSNew( p_mux, p_stream, b_pcr );
1554 if( p_sys->csa != NULL &&
1555 (p_input->p_fmt->i_cat != AUDIO_ES || p_sys->b_crypt_audio) &&
1556 (p_input->p_fmt->i_cat != VIDEO_ES || p_sys->b_crypt_video) )
1558 p_ts->i_flags |= BLOCK_FLAG_SCRAMBLED;
1560 i_packet_pos++;
1562 /* Write PAT/PMT before every keyframe if use-key-frames is enabled,
1563 * this helps to do segmenting with livehttp-output so it can cut segment
1564 * and start new one with pat,pmt,keyframe*/
1565 if( ( p_sys->b_use_key_frames ) && ( p_ts->i_flags & BLOCK_FLAG_TYPE_I ) )
1567 if( likely( !pat_was_previous ) )
1569 int startcount = chain_ts.i_depth;
1570 GetPAT( p_mux, &chain_ts );
1571 GetPMT( p_mux, &chain_ts );
1572 SetHeader( &chain_ts, startcount );
1573 i_packet_count += (chain_ts.i_depth - startcount );
1574 } else {
1575 SetHeader( &chain_ts, 0); //We just inserted pat/pmt,so just flag it instead of adding new one
1578 pat_was_previous = false;
1580 /* */
1581 BufferChainAppend( &chain_ts, p_ts );
1584 /* 4: date and send */
1585 TSSchedule( p_mux, &chain_ts, i_pcr_length, i_pcr_dts );
1586 return false;
1589 /*****************************************************************************
1590 * Mux: Call each time there is new data for at least one stream
1591 *****************************************************************************
1593 *****************************************************************************/
1594 static int Mux( sout_mux_t *p_mux )
1596 sout_mux_sys_t *p_sys = p_mux->p_sys;
1598 if( p_sys->i_pcr_pid == 0x1fff )
1600 for (int i = 0; i < p_mux->i_nb_inputs; i++ )
1602 block_FifoEmpty( p_mux->pp_inputs[i]->p_fifo );
1604 msg_Dbg( p_mux, "waiting for PCR streams" );
1605 return VLC_SUCCESS;
1608 while (!MuxStreams(p_mux))
1610 return VLC_SUCCESS;
1613 #define STD_PES_PAYLOAD 170
1614 static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
1616 VLC_UNUSED(p_mux);
1617 block_t *p_data;
1618 size_t i_size;
1620 p_data = block_FifoShow( p_fifo );
1621 i_size = p_data->i_buffer;
1623 if( i_size == STD_PES_PAYLOAD )
1625 return block_FifoGet( p_fifo );
1627 else if( i_size > STD_PES_PAYLOAD )
1629 block_t *p_new = block_Alloc( STD_PES_PAYLOAD );
1630 memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
1631 p_new->i_pts = p_data->i_pts;
1632 p_new->i_dts = p_data->i_dts;
1633 p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
1634 / p_data->i_buffer;
1635 p_data->i_buffer -= STD_PES_PAYLOAD;
1636 p_data->p_buffer += STD_PES_PAYLOAD;
1637 p_data->i_pts += p_new->i_length;
1638 p_data->i_dts += p_new->i_length;
1639 p_data->i_length -= p_new->i_length;
1640 p_data->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1641 return p_new;
1643 else
1645 block_t *p_next;
1646 int i_copy;
1648 p_data = block_FifoGet( p_fifo );
1649 p_data = block_Realloc( p_data, 0, STD_PES_PAYLOAD );
1650 p_next = block_FifoShow( p_fifo );
1651 if ( p_data->i_flags & BLOCK_FLAG_NO_KEYFRAME )
1653 p_data->i_flags &= ~BLOCK_FLAG_NO_KEYFRAME;
1654 p_data->i_pts = p_next->i_pts;
1655 p_data->i_dts = p_next->i_dts;
1657 i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
1659 memcpy( &p_data->p_buffer[i_size], p_next->p_buffer, i_copy );
1660 p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
1661 p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
1662 p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
1663 p_next->i_buffer -= i_copy;
1664 p_next->p_buffer += i_copy;
1665 p_next->i_flags |= BLOCK_FLAG_NO_KEYFRAME;
1667 if( !p_next->i_buffer )
1669 p_next = block_FifoGet( p_fifo );
1670 block_Release( p_next );
1672 return p_data;
1676 static block_t *Add_ADTS( block_t *p_data, es_format_t *p_fmt )
1678 #define ADTS_HEADER_SIZE 7 /* CRC needs 2 more bytes */
1680 uint8_t *p_extra = p_fmt->p_extra;
1682 if( !p_data || p_fmt->i_extra < 2 || !p_extra )
1683 return p_data; /* no data to construct the headers */
1685 size_t frame_length = p_data->i_buffer + ADTS_HEADER_SIZE;
1686 int i_index = ( (p_extra[0] << 1) | (p_extra[1] >> 7) ) & 0x0f;
1687 int i_profile = (p_extra[0] >> 3) - 1; /* i_profile < 4 */
1689 if( i_index == 0x0f && p_fmt->i_extra < 5 )
1690 return p_data; /* not enough data */
1692 int i_channels = (p_extra[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f;
1694 block_t *p_new_block = block_Realloc( p_data, ADTS_HEADER_SIZE,
1695 p_data->i_buffer );
1696 uint8_t *p_buffer = p_new_block->p_buffer;
1698 /* fixed header */
1699 p_buffer[0] = 0xff;
1700 p_buffer[1] = 0xf1; /* 0xf0 | 0x00 | 0x00 | 0x01 */
1701 p_buffer[2] = (i_profile << 6) | ((i_index & 0x0f) << 2) | ((i_channels >> 2) & 0x01) ;
1702 p_buffer[3] = (i_channels << 6) | ((frame_length >> 11) & 0x03);
1704 /* variable header (starts at last 2 bits of 4th byte) */
1706 int i_fullness = 0x7ff; /* 0x7ff means VBR */
1707 /* XXX: We should check if it's CBR or VBR, but no known implementation
1708 * do that, and it's a pain to calculate this field */
1710 p_buffer[4] = frame_length >> 3;
1711 p_buffer[5] = ((frame_length & 0x07) << 5) | ((i_fullness >> 6) & 0x1f);
1712 p_buffer[6] = ((i_fullness & 0x3f) << 2) /* | 0xfc */;
1714 return p_new_block;
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;
1724 BufferChainInit( &new_chain );
1726 if ( i_pcr_length <= 0 )
1728 i_pcr_length = i_packet_count;
1731 for (int i = 0; i < i_packet_count; i++ )
1733 block_t *p_ts = BufferChainGet( p_chain_ts );
1734 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1736 BufferChainAppend( &new_chain, p_ts );
1738 if (!p_ts->i_dts || p_ts->i_dts + p_sys->i_dts_delay * 2/3 >= i_new_dts)
1739 continue;
1741 mtime_t i_max_diff = i_new_dts - p_ts->i_dts;
1742 mtime_t i_cut_dts = p_ts->i_dts;
1744 p_ts = BufferChainPeek( p_chain_ts );
1745 i++;
1746 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1747 while ( p_ts != NULL && i_new_dts - p_ts->i_dts >= i_max_diff )
1749 p_ts = BufferChainGet( p_chain_ts );
1750 i_max_diff = i_new_dts - p_ts->i_dts;
1751 i_cut_dts = p_ts->i_dts;
1752 BufferChainAppend( &new_chain, p_ts );
1754 p_ts = BufferChainPeek( p_chain_ts );
1755 i++;
1756 i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1758 msg_Dbg( p_mux, "adjusting rate at %"PRId64"/%"PRId64" (%d/%d)",
1759 i_cut_dts - i_pcr_dts, i_pcr_length, new_chain.i_depth,
1760 p_chain_ts->i_depth );
1761 if ( new_chain.i_depth )
1762 TSDate( p_mux, &new_chain, i_cut_dts - i_pcr_dts, i_pcr_dts );
1763 if ( p_chain_ts->i_depth )
1764 TSSchedule( p_mux, p_chain_ts, i_pcr_dts + i_pcr_length - i_cut_dts,
1765 i_cut_dts );
1766 return;
1769 if ( new_chain.i_depth )
1770 TSDate( p_mux, &new_chain, i_pcr_length, i_pcr_dts );
1773 static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
1774 mtime_t i_pcr_length, mtime_t i_pcr_dts )
1776 sout_mux_sys_t *p_sys = p_mux->p_sys;
1777 int i_packet_count = p_chain_ts->i_depth;
1779 if ( i_pcr_length / 1000 > 0 )
1781 int i_bitrate = ((uint64_t)i_packet_count * 188 * 8000)
1782 / (uint64_t)(i_pcr_length / 1000);
1783 if ( p_sys->i_bitrate_max && p_sys->i_bitrate_max < i_bitrate )
1785 msg_Warn( p_mux, "max bitrate exceeded at %"PRId64
1786 " (%d bi/s for %d pkt in %"PRId64" us)",
1787 i_pcr_dts + p_sys->i_shaping_delay * 3 / 2 - mdate(),
1788 i_bitrate, i_packet_count, i_pcr_length);
1791 else
1793 /* This shouldn't happen, but happens in some rare heavy load
1794 * and packet losses conditions. */
1795 i_pcr_length = i_packet_count;
1798 /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
1799 for (int i = 0; i < i_packet_count; i++ )
1801 block_t *p_ts = BufferChainGet( p_chain_ts );
1802 mtime_t i_new_dts = i_pcr_dts + i_pcr_length * i / i_packet_count;
1804 p_ts->i_dts = i_new_dts;
1805 p_ts->i_length = i_pcr_length / i_packet_count;
1807 if( p_ts->i_flags & BLOCK_FLAG_CLOCK )
1809 /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
1810 TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay - p_sys->first_dts );
1812 if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
1814 vlc_mutex_lock( &p_sys->csa_lock );
1815 csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size );
1816 vlc_mutex_unlock( &p_sys->csa_lock );
1819 /* latency */
1820 p_ts->i_dts += p_sys->i_shaping_delay * 3 / 2;
1822 sout_AccessOutWrite( p_mux->p_access, p_ts );
1826 static block_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream,
1827 bool b_pcr )
1829 VLC_UNUSED(p_mux);
1830 block_t *p_pes = p_stream->chain_pes.p_first;
1832 bool b_new_pes = false;
1833 bool b_adaptation_field = false;
1835 int i_payload_max = 184 - ( b_pcr ? 8 : 0 );
1837 if( p_stream->i_pes_used <= 0 )
1839 b_new_pes = true;
1841 int i_payload = __MIN( (int)p_pes->i_buffer - p_stream->i_pes_used,
1842 i_payload_max );
1844 if( b_pcr || i_payload < i_payload_max )
1846 b_adaptation_field = true;
1849 block_t *p_ts = block_Alloc( 188 );
1851 if (b_new_pes && !(p_pes->i_flags & BLOCK_FLAG_NO_KEYFRAME) && p_pes->i_flags & BLOCK_FLAG_TYPE_I)
1853 p_ts->i_flags |= BLOCK_FLAG_TYPE_I;
1856 p_ts->i_dts = p_pes->i_dts;
1858 p_ts->p_buffer[0] = 0x47;
1859 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 ) |
1860 ( ( p_stream->i_pid >> 8 )&0x1f );
1861 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1862 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 ) |
1863 p_stream->i_continuity_counter;
1865 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1866 p_stream->b_discontinuity = p_pes->i_flags & BLOCK_FLAG_DISCONTINUITY;
1868 if( b_adaptation_field )
1870 int i_stuffing = i_payload_max - i_payload;
1871 if( b_pcr )
1873 p_ts->i_flags |= BLOCK_FLAG_CLOCK;
1875 p_ts->p_buffer[4] = 7 + i_stuffing;
1876 p_ts->p_buffer[5] = 1 << 4; /* PCR_flag */
1877 if( p_stream->b_discontinuity )
1879 p_ts->p_buffer[5] |= 0x80; /* flag TS dicontinuity */
1880 p_stream->b_discontinuity = false;
1882 memset(&p_ts->p_buffer[12], 0xff, i_stuffing);
1884 else
1886 p_ts->p_buffer[4] = --i_stuffing;
1887 if( i_stuffing-- )
1889 p_ts->p_buffer[5] = 0;
1890 memset(&p_ts->p_buffer[6], 0xff, i_stuffing);
1895 /* copy payload */
1896 memcpy( &p_ts->p_buffer[188 - i_payload],
1897 &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
1899 p_stream->i_pes_used += i_payload;
1900 p_stream->i_pes_dts = p_pes->i_dts + p_pes->i_length *
1901 p_stream->i_pes_used / p_pes->i_buffer;
1902 p_stream->i_pes_length -= p_pes->i_length * i_payload / p_pes->i_buffer;
1904 if( p_stream->i_pes_used >= (int)p_pes->i_buffer )
1906 block_Release(BufferChainGet( &p_stream->chain_pes ));
1908 p_pes = p_stream->chain_pes.p_first;
1909 p_stream->i_pes_length = 0;
1910 if( p_pes )
1912 p_stream->i_pes_dts = p_pes->i_dts;
1913 while( p_pes )
1915 p_stream->i_pes_length += p_pes->i_length;
1916 p_pes = p_pes->p_next;
1919 else
1921 p_stream->i_pes_dts = 0;
1923 p_stream->i_pes_used = 0;
1926 return p_ts;
1929 static void TSSetPCR( block_t *p_ts, mtime_t i_dts )
1931 mtime_t i_pcr = 9 * i_dts / 100;
1933 p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
1934 p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
1935 p_ts->p_buffer[8] = ( i_pcr >> 9 )&0xff;
1936 p_ts->p_buffer[9] = ( i_pcr >> 1 )&0xff;
1937 p_ts->p_buffer[10] = ( i_pcr << 7 )&0x80;
1938 p_ts->p_buffer[10] |= 0x7e;
1939 p_ts->p_buffer[11] = 0; /* we don't set PCR extension */
1942 static void PEStoTS( sout_buffer_chain_t *c, block_t *p_pes,
1943 ts_stream_t *p_stream )
1945 /* get PES total size */
1946 uint8_t *p_data = p_pes->p_buffer;
1947 int i_size = p_pes->i_buffer;
1949 bool b_new_pes = true;
1951 for (;;)
1953 /* write header
1954 * 8b 0x47 sync byte
1955 * 1b transport_error_indicator
1956 * 1b payload_unit_start
1957 * 1b transport_priority
1958 * 13b pid
1959 * 2b transport_scrambling_control
1960 * 2b if adaptation_field 0x03 else 0x01
1961 * 4b continuity_counter
1964 int i_copy = __MIN( i_size, 184 );
1965 bool b_adaptation_field = i_size < 184;
1966 block_t *p_ts = block_Alloc( 188 );
1968 p_ts->p_buffer[0] = 0x47;
1969 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
1970 ( ( p_stream->i_pid >> 8 )&0x1f );
1971 p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
1972 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
1973 p_stream->i_continuity_counter;
1975 b_new_pes = false;
1976 p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
1978 if( b_adaptation_field )
1980 int i_stuffing = 184 - i_copy;
1982 p_ts->p_buffer[4] = i_stuffing - 1;
1983 if( i_stuffing > 1 )
1985 p_ts->p_buffer[5] = 0x00;
1986 if( p_stream->b_discontinuity )
1988 p_ts->p_buffer[5] |= 0x80;
1989 p_stream->b_discontinuity = false;
1991 for (int i = 6; i < 6 + i_stuffing - 2; i++ )
1993 p_ts->p_buffer[i] = 0xff;
1997 /* copy payload */
1998 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
1999 p_data += i_copy;
2000 i_size -= i_copy;
2002 BufferChainAppend( c, p_ts );
2004 if( i_size <= 0 )
2006 block_t *p_next = p_pes->p_next;
2008 p_pes->p_next = NULL;
2009 block_Release( p_pes );
2010 if( p_next == NULL )
2011 return;
2013 b_new_pes = true;
2014 p_pes = p_next;
2015 i_size = p_pes->i_buffer;
2016 p_data = p_pes->p_buffer;
2021 static block_t *WritePSISection( dvbpsi_psi_section_t* p_section )
2023 block_t *p_psi, *p_first = NULL;
2025 while( p_section )
2027 int i_size = (uint32_t)(p_section->p_payload_end - p_section->p_data) +
2028 (p_section->b_syntax_indicator ? 4 : 0);
2030 p_psi = block_Alloc( i_size + 1 );
2031 if( !p_psi )
2032 goto error;
2033 p_psi->i_pts = 0;
2034 p_psi->i_dts = 0;
2035 p_psi->i_length = 0;
2036 p_psi->i_buffer = i_size + 1;
2038 p_psi->p_buffer[0] = 0; /* pointer */
2039 memcpy( p_psi->p_buffer + 1,
2040 p_section->p_data,
2041 i_size );
2043 block_ChainAppend( &p_first, p_psi );
2045 p_section = p_section->p_next;
2048 return( p_first );
2050 error:
2051 if( p_first )
2052 block_ChainRelease( p_first );
2053 return NULL;
2056 static void GetPAT( sout_mux_t *p_mux,
2057 sout_buffer_chain_t *c )
2059 sout_mux_sys_t *p_sys = p_mux->p_sys;
2060 block_t *p_pat;
2061 dvbpsi_pat_t pat;
2062 dvbpsi_psi_section_t *p_section;
2064 dvbpsi_InitPAT( &pat, p_sys->i_tsid, p_sys->i_pat_version_number,
2065 1 ); /* b_current_next */
2066 /* add all programs */
2067 for (unsigned i = 0; i < p_sys->i_num_pmt; i++ )
2068 dvbpsi_PATAddProgram( &pat, p_sys->i_pmt_program_number[i],
2069 p_sys->pmt[i].i_pid );
2071 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2072 p_section = dvbpsi_pat_sections_generate( p_sys->p_dvbpsi, &pat, 0 );
2073 #else
2074 p_section = dvbpsi_GenPATSections( &pat, 0 /* max program per section */ );
2075 #endif
2076 p_pat = WritePSISection( p_section );
2078 PEStoTS( c, p_pat, &p_sys->pat );
2080 dvbpsi_DeletePSISections( p_section );
2081 dvbpsi_EmptyPAT( &pat );
2084 static uint32_t GetDescriptorLength24b( int i_length )
2086 uint32_t i_l1, i_l2, i_l3;
2088 i_l1 = i_length&0x7f;
2089 i_l2 = ( i_length >> 7 )&0x7f;
2090 i_l3 = ( i_length >> 14 )&0x7f;
2092 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
2095 static void GetPMTmpeg4(sout_mux_t *p_mux)
2097 sout_mux_sys_t *p_sys = p_mux->p_sys;
2098 uint8_t iod[4096];
2099 bits_buffer_t bits, bits_fix_IOD;
2101 /* Make valgrind happy : it works at byte level not bit one so
2102 * bit_write confuse it (but DON'T CHANGE the way that bit_write is
2103 * working (needed when fixing some bits) */
2104 memset( iod, 0, 4096 );
2106 bits_initwrite( &bits, 4096, iod );
2107 /* IOD_label_scope */
2108 bits_write( &bits, 8, 0x11 );
2109 /* IOD_label */
2110 bits_write( &bits, 8, 0x01 );
2111 /* InitialObjectDescriptor */
2112 bits_align( &bits );
2113 bits_write( &bits, 8, 0x02 ); /* tag */
2114 bits_fix_IOD = bits; /* save states to fix length later */
2115 bits_write( &bits, 24,
2116 GetDescriptorLength24b( 0 ) ); /* variable length (fixed later) */
2117 bits_write( &bits, 10, 0x01 ); /* ObjectDescriptorID */
2118 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2119 bits_write( &bits, 1, 0x00 ); /* includeInlineProfileLevelFlag */
2120 bits_write( &bits, 4, 0x0f ); /* reserved */
2121 bits_write( &bits, 8, 0xff ); /* ODProfile (no ODcapability ) */
2122 bits_write( &bits, 8, 0xff ); /* sceneProfile */
2123 bits_write( &bits, 8, 0xfe ); /* audioProfile (unspecified) */
2124 bits_write( &bits, 8, 0xfe ); /* visualProfile( // ) */
2125 bits_write( &bits, 8, 0xff ); /* graphicProfile (no ) */
2126 for (int i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2128 ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
2130 if( p_stream->i_stream_id != 0xfa && p_stream->i_stream_id != 0xfb &&
2131 p_stream->i_stream_id != 0xfe )
2132 continue;
2134 bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
2135 /* ES descriptor */
2136 bits_align( &bits );
2137 bits_write( &bits, 8, 0x03 ); /* ES_DescrTag */
2138 bits_fix_ESDescr = bits;
2139 bits_write( &bits, 24,
2140 GetDescriptorLength24b( 0 ) ); /* variable size */
2141 bits_write( &bits, 16, p_stream->i_es_id );
2142 bits_write( &bits, 1, 0x00 ); /* streamDependency */
2143 bits_write( &bits, 1, 0x00 ); /* URL Flag */
2144 bits_write( &bits, 1, 0x00 ); /* OCRStreamFlag */
2145 bits_write( &bits, 5, 0x1f ); /* streamPriority */
2147 /* DecoderConfigDesciptor */
2148 bits_align( &bits );
2149 bits_write( &bits, 8, 0x04 ); /* DecoderConfigDescrTag */
2150 bits_fix_Decoder = bits;
2151 bits_write( &bits, 24, GetDescriptorLength24b( 0 ) );
2152 if( p_stream->i_stream_type == 0x10 )
2154 bits_write( &bits, 8, 0x20 ); /* Visual 14496-2 */
2155 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2157 else if( p_stream->i_stream_type == 0x1b )
2159 bits_write( &bits, 8, 0x21 ); /* Visual 14496-2 */
2160 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2162 else if( p_stream->i_stream_type == 0x11 ||
2163 p_stream->i_stream_type == 0x0f )
2165 bits_write( &bits, 8, 0x40 ); /* Audio 14496-3 */
2166 bits_write( &bits, 6, 0x05 ); /* AudioStream */
2168 else if( p_stream->i_stream_type == 0x12 &&
2169 p_stream->i_codec == VLC_CODEC_SUBT )
2171 bits_write( &bits, 8, 0x0B ); /* Text Stream */
2172 bits_write( &bits, 6, 0x04 ); /* VisualStream */
2174 else
2176 bits_write( &bits, 8, 0x00 );
2177 bits_write( &bits, 6, 0x00 );
2179 msg_Err( p_mux, "Unsupported stream_type => broken IOD" );
2181 bits_write( &bits, 1, 0x00 ); /* UpStream */
2182 bits_write( &bits, 1, 0x01 ); /* reserved */
2183 bits_write( &bits, 24, 1024 * 1024 ); /* bufferSizeDB */
2184 bits_write( &bits, 32, 0x7fffffff ); /* maxBitrate */
2185 bits_write( &bits, 32, 0 ); /* avgBitrate */
2187 if( p_stream->i_extra > 0 )
2189 /* DecoderSpecificInfo */
2190 bits_align( &bits );
2191 bits_write( &bits, 8, 0x05 ); /* tag */
2192 bits_write( &bits, 24, GetDescriptorLength24b(
2193 p_stream->i_extra ) );
2194 for (int i = 0; i < p_stream->i_extra; i++ )
2196 bits_write( &bits, 8,
2197 ((uint8_t*)p_stream->p_extra)[i] );
2200 /* fix Decoder length */
2201 bits_write( &bits_fix_Decoder, 24,
2202 GetDescriptorLength24b( bits.i_data -
2203 bits_fix_Decoder.i_data - 3 ) );
2205 /* SLConfigDescriptor : predefined (0x01) */
2206 bits_align( &bits );
2207 bits_write( &bits, 8, 0x06 ); /* tag */
2208 bits_write( &bits, 24, GetDescriptorLength24b( 8 ) );
2209 bits_write( &bits, 8, 0x01 );/* predefined */
2210 bits_write( &bits, 1, 0 ); /* durationFlag */
2211 bits_write( &bits, 32, 0 ); /* OCRResolution */
2212 bits_write( &bits, 8, 0 ); /* OCRLength */
2213 bits_write( &bits, 8, 0 ); /* InstantBitrateLength */
2214 bits_align( &bits );
2216 /* fix ESDescr length */
2217 bits_write( &bits_fix_ESDescr, 24,
2218 GetDescriptorLength24b( bits.i_data -
2219 bits_fix_ESDescr.i_data - 3 ) );
2221 bits_align( &bits );
2222 /* fix IOD length */
2223 bits_write( &bits_fix_IOD, 24,
2224 GetDescriptorLength24b(bits.i_data - bits_fix_IOD.i_data - 3 ));
2226 dvbpsi_PMTAddDescriptor(&p_sys->dvbpmt[0], 0x1d, bits.i_data, bits.p_data);
2229 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c )
2231 sout_mux_sys_t *p_sys = p_mux->p_sys;
2233 if( p_sys->dvbpmt == NULL )
2235 p_sys->dvbpmt = malloc( p_sys->i_num_pmt * sizeof(dvbpsi_pmt_t) );
2236 if( p_sys->dvbpmt == NULL )
2237 return;
2240 dvbpsi_sdt_t sdt;
2241 if( p_sys->b_sdt )
2242 dvbpsi_InitSDT( &sdt, p_sys->i_tsid, 1, 1, p_sys->i_netid );
2244 for (unsigned i = 0; i < p_sys->i_num_pmt; i++ )
2246 dvbpsi_InitPMT( &p_sys->dvbpmt[i],
2247 p_sys->i_pmt_program_number[i], /* program number */
2248 p_sys->i_pmt_version_number,
2249 1, /* b_current_next */
2250 p_sys->i_pcr_pid );
2252 if( !p_sys->b_sdt )
2253 continue;
2255 dvbpsi_sdt_service_t *p_service = dvbpsi_SDTAddService( &sdt,
2256 p_sys->i_pmt_program_number[i], /* service id */
2257 0, /* eit schedule */
2258 0, /* eit present */
2259 4, /* running status ("4=RUNNING") */
2260 0 ); /* free ca */
2262 const char *psz_sdtprov = p_sys->sdt_descriptors[i].psz_provider;
2263 const char *psz_sdtserv = p_sys->sdt_descriptors[i].psz_service_name;
2265 if( !psz_sdtprov || !psz_sdtserv )
2266 continue;
2267 size_t provlen = VLC_CLIP(strlen(psz_sdtprov), 0, 255);
2268 size_t servlen = VLC_CLIP(strlen(psz_sdtserv), 0, 255);
2270 uint8_t psz_sdt_desc[3 + provlen + servlen];
2272 psz_sdt_desc[0] = 0x01; /* digital television service */
2274 /* service provider name length */
2275 psz_sdt_desc[1] = (char)provlen;
2276 memcpy( &psz_sdt_desc[2], psz_sdtprov, provlen );
2278 /* service name length */
2279 psz_sdt_desc[ 2 + provlen ] = (char)servlen;
2280 memcpy( &psz_sdt_desc[3+provlen], psz_sdtserv, servlen );
2282 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2283 dvbpsi_sdt_service_descriptor_add( p_service, 0x48,
2284 (3 + provlen + servlen),
2285 psz_sdt_desc );
2286 #else
2287 dvbpsi_SDTServiceAddDescriptor( p_service, 0x48,
2288 3 + provlen + servlen, psz_sdt_desc );
2289 #endif
2292 if( p_sys->i_mpeg4_streams > 0 )
2293 GetPMTmpeg4(p_mux);
2295 for (int i_stream = 0; i_stream < p_mux->i_nb_inputs; i_stream++ )
2297 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
2298 ts_stream_t *p_stream = (ts_stream_t*)p_input->p_sys;
2300 int i_pidinput = p_input->p_fmt->i_id;
2301 pmt_map_t *p_usepid = bsearch( &i_pidinput, p_sys->pmtmap,
2302 p_sys->i_pmtslots, sizeof(pmt_map_t), intcompare );
2304 /* If there's an error somewhere, dump it to the first pmt */
2305 unsigned prog = p_usepid ? p_usepid->i_prog : 0;
2307 dvbpsi_pmt_es_t *p_es = dvbpsi_PMTAddES( &p_sys->dvbpmt[prog],
2308 p_stream->i_stream_type, p_stream->i_pid );
2310 if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
2312 uint8_t es_id[2];
2314 /* SL descriptor */
2315 es_id[0] = (p_stream->i_es_id >> 8)&0xff;
2316 es_id[1] = (p_stream->i_es_id)&0xff;
2317 dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
2319 else if( p_stream->i_stream_type == 0xa0 )
2321 uint8_t data[512];
2322 int i_extra = __MIN( p_stream->i_extra, 502 );
2324 /* private DIV3 descripor */
2325 memcpy( &data[0], &p_stream->i_bih_codec, 4 );
2326 data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
2327 data[5] = ( p_stream->i_bih_width )&0xff;
2328 data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
2329 data[7] = ( p_stream->i_bih_height )&0xff;
2330 data[8] = ( i_extra >> 8 )&0xff;
2331 data[9] = ( i_extra )&0xff;
2332 if( i_extra > 0 )
2334 memcpy( &data[10], p_stream->p_extra, i_extra );
2337 /* 0xa0 is private */
2338 dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra + 10, data );
2340 else if( p_stream->i_stream_type == 0x81 )
2342 uint8_t format[4] = { 'A', 'C', '-', '3'};
2344 /* "registration" descriptor : "AC-3" */
2345 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
2347 else if( p_stream->i_codec == VLC_CODEC_DIRAC )
2349 /* Dirac registration descriptor */
2351 uint8_t data[4] = { 'd', 'r', 'a', 'c' };
2352 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2354 else if( p_stream->i_codec == VLC_CODEC_DTS )
2356 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2358 /* DTS format identifier, frame size 1024 - FIXME */
2359 uint8_t data[4] = { 'D', 'T', 'S', '2' };
2360 dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, data );
2362 else if( p_stream->i_codec == VLC_CODEC_EAC3 )
2364 uint8_t data[1] = { 0x00 };
2365 dvbpsi_PMTESAddDescriptor( p_es, 0x7a, 1, data );
2367 else if( p_stream->i_codec == VLC_CODEC_OPUS )
2369 uint8_t data[2] = {
2370 0x80, /* tag extension */
2371 p_input->p_fmt->audio.i_channels
2373 dvbpsi_PMTESAddDescriptor( p_es, 0x7f, 2, data );
2375 else if( p_stream->i_codec == VLC_CODEC_TELETEXT )
2377 if( p_stream->i_extra )
2379 dvbpsi_PMTESAddDescriptor( p_es, 0x56,
2380 p_stream->i_extra,
2381 p_stream->p_extra );
2383 continue;
2385 else if( p_stream->i_codec == VLC_CODEC_DVBS )
2387 /* DVB subtitles */
2388 if( p_stream->i_extra )
2390 /* pass-through from the TS demux */
2391 dvbpsi_PMTESAddDescriptor( p_es, 0x59,
2392 p_stream->i_extra,
2393 p_stream->p_extra );
2395 else
2397 /* from the dvbsub transcoder */
2398 dvbpsi_subtitling_dr_t descr;
2399 dvbpsi_subtitle_t sub;
2400 dvbpsi_descriptor_t *p_descr;
2402 memcpy( sub.i_iso6392_language_code, p_stream->lang, 3 );
2403 sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
2404 sub.i_composition_page_id = p_stream->i_es_id & 0xFF;
2405 sub.i_ancillary_page_id = p_stream->i_es_id >> 16;
2407 descr.i_subtitles_number = 1;
2408 descr.p_subtitle[0] = sub;
2410 p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
2411 /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
2412 dvbpsi_PMTESAddDescriptor( p_es, p_descr->i_tag,
2413 p_descr->i_length, p_descr->p_data );
2415 continue;
2418 if( p_stream->i_langs )
2420 dvbpsi_PMTESAddDescriptor( p_es, 0x0a, 4*p_stream->i_langs,
2421 p_stream->lang);
2425 for (unsigned i = 0; i < p_sys->i_num_pmt; i++ )
2427 dvbpsi_psi_section_t *sect;
2428 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2429 sect = dvbpsi_pmt_sections_generate( p_sys->p_dvbpsi, &p_sys->dvbpmt[i] );
2430 #else
2431 sect = dvbpsi_GenPMTSections( &p_sys->dvbpmt[i] );
2432 #endif
2433 block_t *pmt = WritePSISection( sect );
2434 PEStoTS( c, pmt, &p_sys->pmt[i] );
2435 dvbpsi_DeletePSISections(sect);
2436 dvbpsi_EmptyPMT( &p_sys->dvbpmt[i] );
2439 if( p_sys->b_sdt )
2441 dvbpsi_psi_section_t *sect;
2442 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2443 sect = dvbpsi_sdt_sections_generate( p_sys->p_dvbpsi, &sdt );
2444 #else
2445 sect = dvbpsi_GenSDTSections( &sdt );
2446 #endif
2447 block_t *p_sdt = WritePSISection( sect );
2448 PEStoTS( c, p_sdt, &p_sys->sdt );
2449 dvbpsi_DeletePSISections( sect );
2450 dvbpsi_EmptySDT( &sdt );