demux: ts: just ignore cc if no payload
[vlc.git] / modules / demux / mpeg / ts.c
blobe8f7d3d266a983ea21ba80cce2ee5620430b0945
1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2016 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h> /* DVB-specific things */
36 #include <vlc_demux.h>
37 #include <vlc_input.h>
39 #include "ts_pid.h"
40 #include "ts_streams.h"
41 #include "ts_streams_private.h"
42 #include "ts_psi.h"
43 #include "ts_si.h"
44 #include "ts_psip.h"
46 #include "ts_hotfixes.h"
47 #include "ts_sl.h"
48 #include "ts_metadata.h"
49 #include "sections.h"
50 #include "pes.h"
51 #include "timestamps.h"
53 #include "ts.h"
55 #include "../../codec/scte18.h"
56 #include "../opus.h"
57 #include "../../mux/mpeg/csa.h"
59 #ifdef HAVE_ARIBB24
60 #include <aribb24/aribb24.h>
61 #endif
63 #include <assert.h>
65 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
71 /* TODO
72 * - Rename "extra pmt" to "user pmt"
73 * - Update extra pmt description
74 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
75 * where pid_description could take 3 forms:
76 * 1. pid:pcr (to force the pcr pid)
77 * 2. pid:stream_type
78 * 3. pid:type=fourcc where type=(video|audio|spu)
80 #define PMT_TEXT N_("Extra PMT")
81 #define PMT_LONGTEXT N_( \
82 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
84 #define PID_TEXT N_("Set id of ES to PID")
85 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
86 " handled by VLC to the same value as the PID in" \
87 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
88 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
90 #define CSA_TEXT N_("CSA Key")
91 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
92 "16 char string (8 hexadecimal bytes).")
94 #define CSA2_TEXT N_("Second CSA Key")
95 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
96 "16 char string (8 hexadecimal bytes).")
99 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
100 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
101 "The decryption routines subtract the TS-header from the value before " \
102 "decrypting. " )
104 #define SPLIT_ES_TEXT N_("Separate sub-streams")
105 #define SPLIT_ES_LONGTEXT N_( \
106 "Separate teletex/dvbs pages into independent ES. " \
107 "It can be useful to turn off this option when using stream output." )
109 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
110 #define SEEK_PERCENT_LONGTEXT N_( \
111 "Seek and position based on a percent byte position, not a PCR generated " \
112 "time position. If seeking doesn't work property, turn on this option." )
114 #define PCR_TEXT N_("Trust in-stream PCR")
115 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
117 static const char *const ts_standards_list[] =
118 { "auto", "mpeg", "dvb", "arib", "atsc", "tdmb" };
119 static const char *const ts_standards_list_text[] =
120 { N_("Auto"), "MPEG", "DVB", "ARIB", "ATSC", "T-DMB" };
122 #define STANDARD_TEXT N_("Digital TV Standard")
123 #define STANDARD_LONGTEXT N_( "Selects mode for digital TV standard." \
124 "This feature affects EPG information and subtitles." )
126 vlc_module_begin ()
127 set_description( N_("MPEG Transport Stream demuxer") )
128 set_shortname ( "MPEG-TS" )
129 set_category( CAT_INPUT )
130 set_subcategory( SUBCAT_INPUT_DEMUX )
132 add_string( "ts-standard", "auto", STANDARD_TEXT, STANDARD_LONGTEXT, true )
133 change_string_list( ts_standards_list, ts_standards_list_text )
135 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
136 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
137 change_safe()
138 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
139 change_safe()
140 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
141 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
142 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
143 change_safe()
144 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
145 change_safe()
146 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
147 change_safe()
149 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
150 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
152 add_obsolete_bool( "ts-silent" );
154 set_capability( "demux", 10 )
155 set_callbacks( Open, Close )
156 add_shortcut( "ts" )
157 vlc_module_end ()
159 /*****************************************************************************
160 * Local prototypes
161 *****************************************************************************/
162 static int Demux ( demux_t *p_demux );
163 static int Control( demux_t *p_demux, int i_query, va_list args );
165 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
167 /* Helpers */
168 static bool PIDReferencedByProgram( const ts_pmt_t *, uint16_t );
169 void UpdatePESFilters( demux_t *p_demux, bool b_all );
170 static inline void FlushESBuffer( ts_stream_t *p_pes );
171 static void UpdatePIDScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool );
172 static inline int PIDGet( block_t *p )
174 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
176 static mtime_t GetPCR( const block_t * );
178 static block_t * ProcessTSPacket( demux_t *p_demux, ts_pid_t *pid, block_t *p_pkt, int * );
179 static bool GatherPESData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk, size_t );
180 static bool GatherSectionsData( demux_t *p_demux, ts_pid_t *, block_t *, size_t );
181 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
183 static block_t* ReadTSPacket( demux_t *p_demux );
184 static int SeekToTime( demux_t *p_demux, const ts_pmt_t *, int64_t time );
185 static void ReadyQueuesPostSeek( demux_t *p_demux );
186 static void PCRHandle( demux_t *p_demux, ts_pid_t *, mtime_t );
187 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
189 #define TS_PACKET_SIZE_188 188
190 #define TS_PACKET_SIZE_192 192
191 #define TS_PACKET_SIZE_204 204
192 #define TS_PACKET_SIZE_MAX 204
193 #define TS_HEADER_SIZE 4
195 static int DetectPacketSize( demux_t *p_demux, unsigned *pi_header_size, int i_offset )
197 const uint8_t *p_peek;
199 if( vlc_stream_Peek( p_demux->s,
200 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
201 return -1;
203 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
205 if( p_peek[i_offset + i_sync] != 0x47 )
206 continue;
208 /* Check next 3 sync bytes */
209 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
210 if( ( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
212 msg_Dbg( p_demux, "cannot peek" );
213 return -1;
215 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
216 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
217 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
219 return TS_PACKET_SIZE_188;
221 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
222 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
223 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
225 if( i_sync == 4 )
227 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
229 return TS_PACKET_SIZE_192;
231 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
232 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
233 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
235 return TS_PACKET_SIZE_204;
239 if( p_demux->obj.force )
241 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
242 return TS_PACKET_SIZE_188;
244 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
245 return -1;
248 #define TOPFIELD_HEADER_SIZE 3712
250 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, unsigned *pi_header_size, vdr_info_t *p_vdr )
252 const uint8_t *p_peek;
253 *pi_header_size = 0;
254 int i_packet_size = -1;
256 if( vlc_stream_Peek( p_demux->s,
257 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
258 return -1;
260 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
261 p_peek[6] == 0 && ((GetDWBE(&p_peek[53]) & 0x7FFFFF00U) == 0x00) &&
262 vlc_stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
263 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
265 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
266 if( i_packet_size != -1 )
268 msg_Dbg( p_demux, "this is a topfield file" );
269 #if 0
270 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
271 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
272 * but after the filename the offsets seem to be incorrect. - DJ */
273 int i_duration, i_name;
274 char *psz_name = xmalloc(25);
275 char *psz_event_name;
276 char *psz_event_text = xmalloc(130);
277 char *psz_ext_text = xmalloc(1025);
279 // 2 bytes version Uimsbf (4,5)
280 // 2 bytes reserved (6,7)
281 // 2 bytes duration in minutes Uimsbf (8,9(
282 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
283 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
284 // 2 bytes service number in channel list (10, 11)
285 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
286 // 4 bytes of reserved + tuner info (14,15,16,17)
287 // 2 bytes of Service ID Bslbf (18,19)
288 // 2 bytes of PMT PID Uimsbf (20,21)
289 // 2 bytes of PCR PID Uimsbf (22,23)
290 // 2 bytes of Video PID Uimsbf (24,25)
291 // 2 bytes of Audio PID Uimsbf (26,27)
292 // 24 bytes filename Bslbf
293 memcpy( psz_name, &p_peek[28], 24 );
294 psz_name[24] = '\0';
295 msg_Dbg( p_demux, "recordingname=%s", psz_name );
296 // 1 byte of sat index Uimsbf (52)
297 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
298 // 4 bytes of freq. Uimsbf (56,57,58,59)
299 // 2 bytes of symbol rate Uimsbf (60,61)
300 // 2 bytes of TS stream ID Uimsbf (62,63)
301 // 4 bytes reserved
302 // 2 bytes reserved
303 // 2 bytes duration Uimsbf (70,71)
304 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
305 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
306 // 4 bytes EventID Uimsbf (72-75)
307 // 8 bytes of Start and End time info (76-83)
308 // 1 byte reserved (84)
309 // 1 byte event name length Uimsbf (89)
310 i_name = (int)(p_peek[89]&~0x81);
311 msg_Dbg( p_demux, "event name length = %d", i_name);
312 psz_event_name = xmalloc( i_name+1 );
313 // 1 byte parental rating (90)
314 // 129 bytes of event text
315 memcpy( psz_event_name, &p_peek[91], i_name );
316 psz_event_name[i_name] = '\0';
317 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
318 psz_event_text[129-i_name] = '\0';
319 msg_Dbg( p_demux, "event name=%s", psz_event_name );
320 msg_Dbg( p_demux, "event text=%s", psz_event_text );
321 // 12 bytes reserved (220)
322 // 6 bytes reserved
323 // 2 bytes Event Text Length Uimsbf
324 // 4 bytes EventID Uimsbf
325 // FIXME We just have 613 bytes. not enough for this entire text
326 // 1024 bytes Extended Event Text Bslbf
327 memcpy( psz_ext_text, p_peek+372, 1024 );
328 psz_ext_text[1024] = '\0';
329 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
330 // 52 bytes reserved Bslbf
331 #endif
332 p_vdr->i_service = GetWBE(&p_peek[18]);
334 return i_packet_size;
335 //return TS_PACKET_SIZE_188;
339 return DetectPacketSize( p_demux, pi_header_size, 0 );
342 /*****************************************************************************
343 * Open
344 *****************************************************************************/
345 static int Open( vlc_object_t *p_this )
347 demux_t *p_demux = (demux_t*)p_this;
348 demux_sys_t *p_sys;
350 int i_packet_size;
351 unsigned i_packet_header_size = 0;
353 ts_pid_t *patpid;
354 vdr_info_t vdr = {0};
356 /* Search first sync byte */
357 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
358 if( i_packet_size < 0 )
359 return VLC_EGENERIC;
361 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
362 if( !p_sys )
363 return VLC_ENOMEM;
364 memset( p_sys, 0, sizeof( demux_sys_t ) );
365 vlc_mutex_init( &p_sys->csa_lock );
367 p_demux->pf_demux = Demux;
368 p_demux->pf_control = Control;
370 /* Init p_sys field */
371 p_sys->b_end_preparse = false;
372 ARRAY_INIT( p_sys->programs );
373 p_sys->b_default_selection = false;
374 p_sys->i_network_time = 0;
375 p_sys->i_network_time_update = 0;
377 p_sys->vdr = vdr;
379 p_sys->arib.b25stream = NULL;
380 p_sys->stream = p_demux->s;
382 p_sys->b_broken_charset = false;
384 ts_pid_list_Init( &p_sys->pids );
386 p_sys->i_packet_size = i_packet_size;
387 p_sys->i_packet_header_size = i_packet_header_size;
388 p_sys->i_ts_read = 50;
389 p_sys->csa = NULL;
390 p_sys->b_start_record = false;
392 vlc_dictionary_init( &p_sys->attachments, 0 );
394 p_sys->patfix.i_first_dts = -1;
395 p_sys->patfix.i_timesourcepid = 0;
396 p_sys->patfix.status = PAT_WAITING;
398 /* Init PAT handler */
399 patpid = GetPID(p_sys, 0);
400 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
402 vlc_mutex_destroy( &p_sys->csa_lock );
403 free( p_sys );
404 return VLC_ENOMEM;
406 if( !ts_psi_PAT_Attach( patpid, p_demux ) )
408 PIDRelease( p_demux, patpid );
409 vlc_mutex_destroy( &p_sys->csa_lock );
410 free( p_sys );
411 return VLC_EGENERIC;
414 p_sys->b_access_control = true;
415 p_sys->b_access_control = ( VLC_SUCCESS == SetPIDFilter( p_sys, patpid, true ) );
417 p_sys->i_pmt_es = 0;
418 p_sys->b_es_all = false;
420 /* Read config */
421 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
422 p_sys->i_next_extraid = 1;
424 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
426 /* We handle description of an extra PMT */
427 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
428 p_sys->b_user_pmt = false;
429 if( psz_string && *psz_string )
430 UserPmt( p_demux, psz_string );
431 free( psz_string );
433 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
434 if( psz_string && *psz_string )
436 int i_res;
437 char* psz_csa2;
439 p_sys->csa = csa_New();
441 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
442 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
443 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
445 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
447 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
450 else if ( i_res == VLC_SUCCESS )
452 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
454 else
456 csa_Delete( p_sys->csa );
457 p_sys->csa = NULL;
460 if( p_sys->csa )
462 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
463 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
465 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
466 if( i_pkt < 4 || i_pkt > 188 )
468 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
469 msg_Warn( p_demux, "using default packet size of 188 bytes" );
470 p_sys->i_csa_pkt_size = 188;
472 else
473 p_sys->i_csa_pkt_size = i_pkt;
474 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
476 free( psz_csa2 );
478 free( psz_string );
480 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
482 p_sys->b_canseek = false;
483 p_sys->b_canfastseek = false;
484 p_sys->b_ignore_time_for_positions = var_InheritBool( p_demux, "ts-seek-percent" );
486 p_sys->standard = TS_STANDARD_AUTO;
487 char *psz_standard = var_InheritString( p_demux, "ts-standard" );
488 if( psz_standard )
490 for( unsigned i=0; i<ARRAY_SIZE(ts_standards_list); i++ )
492 if( !strcmp( psz_standard, ts_standards_list[i] ) )
494 TsChangeStandard( p_sys, TS_STANDARD_AUTO + i );
495 msg_Dbg( p_demux, "Standard set to %s", ts_standards_list_text[i] );
496 break;
499 free( psz_standard );
502 if( p_sys->standard == TS_STANDARD_AUTO &&
503 ( !strcmp( p_demux->psz_access, "atsc" ) ||
504 !strcmp( p_demux->psz_access, "usdigital" ) ) )
506 TsChangeStandard( p_sys, TS_STANDARD_ATSC );
509 vlc_stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
510 vlc_stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK,
511 &p_sys->b_canfastseek );
513 /* Preparse time */
514 if( p_sys->b_canseek )
516 p_sys->es_creation = NO_ES;
517 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
518 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
519 break;
520 p_sys->es_creation = DELAY_ES;
522 else
523 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
525 return VLC_SUCCESS;
528 /*****************************************************************************
529 * Close
530 *****************************************************************************/
531 static void FreeDictAttachment( void *p_value, void *p_obj )
533 VLC_UNUSED(p_obj);
534 vlc_input_attachment_Delete( (input_attachment_t *) p_value );
537 static void Close( vlc_object_t *p_this )
539 demux_t *p_demux = (demux_t*)p_this;
540 demux_sys_t *p_sys = p_demux->p_sys;
542 PIDRelease( p_demux, GetPID(p_sys, 0) );
544 vlc_mutex_lock( &p_sys->csa_lock );
545 if( p_sys->csa )
547 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
548 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
549 csa_Delete( p_sys->csa );
551 vlc_mutex_unlock( &p_sys->csa_lock );
553 ARRAY_RESET( p_sys->programs );
555 #ifdef HAVE_ARIBB24
556 if ( p_sys->arib.p_instance )
557 arib_instance_destroy( p_sys->arib.p_instance );
558 #endif
560 if ( p_sys->arib.b25stream )
562 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
563 vlc_stream_Delete( p_sys->arib.b25stream );
566 vlc_mutex_destroy( &p_sys->csa_lock );
568 /* Release all non default pids */
569 ts_pid_list_Release( p_demux, &p_sys->pids );
571 /* Clear up attachments */
572 vlc_dictionary_clear( &p_sys->attachments, FreeDictAttachment, NULL );
574 free( p_sys );
577 /*****************************************************************************
578 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
579 *****************************************************************************/
580 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
581 vlc_value_t oldval, vlc_value_t newval,
582 void *p_data )
584 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
585 demux_t *p_demux = (demux_t*)p_this;
586 demux_sys_t *p_sys = p_demux->p_sys;
587 int i_tmp = (intptr_t)p_data;
589 vlc_mutex_lock( &p_sys->csa_lock );
590 if ( i_tmp )
591 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
592 else
593 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
595 vlc_mutex_unlock( &p_sys->csa_lock );
596 return i_tmp;
599 /*****************************************************************************
600 * Demux:
601 *****************************************************************************/
602 static int Demux( demux_t *p_demux )
604 demux_sys_t *p_sys = p_demux->p_sys;
605 bool b_wait_es = p_sys->i_pmt_es <= 0;
607 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
608 if( p_sys->i_pmt_es == 0 && !SEEN(GetPID(p_sys, 0)) && p_sys->patfix.status == PAT_MISSING )
610 MissingPATPMTFixup( p_demux );
611 p_sys->patfix.status = PAT_FIXTRIED;
614 /* We read at most 100 TS packet or until a frame is completed */
615 for( unsigned i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
617 bool b_frame = false;
618 int i_header = 0;
619 block_t *p_pkt;
620 if( !(p_pkt = ReadTSPacket( p_demux )) )
622 return VLC_DEMUXER_EOF;
625 if( p_sys->b_start_record )
627 /* Enable recording once synchronized */
628 vlc_stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true,
629 "ts" );
630 p_sys->b_start_record = false;
633 /* Early reject truncated packets from hw devices */
634 if( unlikely(p_pkt->i_buffer < TS_PACKET_SIZE_188) )
636 block_Release( p_pkt );
637 continue;
640 /* Reject any fully uncorrected packet. Even PID can be incorrect */
641 if( p_pkt->p_buffer[1]&0x80 )
643 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
644 PIDGet( p_pkt ) );
645 block_Release( p_pkt );
646 continue;
649 /* Parse the TS packet */
650 ts_pid_t *p_pid = GetPID( p_sys, PIDGet( p_pkt ) );
651 if( !SEEN(p_pid) )
653 if( p_pid->type == TYPE_FREE )
654 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
655 p_pid->i_flags |= FLAG_SEEN;
656 if( p_pid->i_pid == 0x01 )
657 p_sys->b_valid_scrambling = true;
660 /* Drop duplicates and invalid (DOES NOT drop corrupted) */
661 p_pkt = ProcessTSPacket( p_demux, p_pid, p_pkt, &i_header );
662 if( !p_pkt )
663 continue;
665 if( !SCRAMBLED(*p_pid) != !(p_pkt->i_flags & BLOCK_FLAG_SCRAMBLED) )
667 UpdatePIDScrambledState( p_demux, p_pid, p_pkt->i_flags & BLOCK_FLAG_SCRAMBLED );
670 /* Adaptation field cannot be scrambled */
671 mtime_t i_pcr = GetPCR( p_pkt );
672 if( i_pcr > VLC_TS_INVALID )
673 PCRHandle( p_demux, p_pid, i_pcr );
675 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
676 if( !SEEN( GetPID( p_sys, 0 ) ) &&
677 (p_pid->probed.i_fourcc == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
678 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
679 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
681 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
682 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
685 switch( p_pid->type )
687 case TYPE_PAT:
688 case TYPE_PMT:
689 /* PAT and PMT are not allowed to be scrambled */
690 ts_psi_Packet_Push( p_pid, p_pkt->p_buffer );
691 block_Release( p_pkt );
692 break;
694 case TYPE_STREAM:
695 p_sys->b_end_preparse = true;
697 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
699 msg_Dbg( p_demux, "Creating delayed ES" );
700 AddAndCreateES( p_demux, p_pid, true );
701 UpdatePESFilters( p_demux, p_sys->b_es_all );
704 /* Emulate HW filter */
705 if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
707 /* That packet is for an unselected ES, don't waste time/memory gathering its data */
708 block_Release( p_pkt );
709 continue;
712 if( p_pid->u.p_stream->transport == TS_TRANSPORT_PES )
714 b_frame = GatherPESData( p_demux, p_pid, p_pkt, i_header );
716 else if( p_pid->u.p_stream->transport == TS_TRANSPORT_SECTIONS )
718 b_frame = GatherSectionsData( p_demux, p_pid, p_pkt, i_header );
720 else // pid->u.p_pes->transport == TS_TRANSPORT_IGNORE
722 block_Release( p_pkt );
725 break;
727 case TYPE_SI:
728 if( (p_pkt->i_flags & (BLOCK_FLAG_SCRAMBLED|BLOCK_FLAG_CORRUPTED)) == 0 )
729 ts_si_Packet_Push( p_pid, p_pkt->p_buffer );
730 block_Release( p_pkt );
731 break;
733 case TYPE_PSIP:
734 if( (p_pkt->i_flags & (BLOCK_FLAG_SCRAMBLED|BLOCK_FLAG_CORRUPTED)) == 0 )
735 ts_psip_Packet_Push( p_pid, p_pkt->p_buffer );
736 block_Release( p_pkt );
737 break;
739 case TYPE_CAT:
740 default:
741 /* We have to handle PCR if present */
742 block_Release( p_pkt );
743 break;
746 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
747 break;
750 demux_UpdateTitleFromStream( p_demux );
751 return VLC_DEMUXER_SUCCESS;
754 /*****************************************************************************
755 * Control:
756 *****************************************************************************/
757 static int EITCurrentEventTime( const ts_pmt_t *p_pmt, demux_sys_t *p_sys,
758 time_t *pi_time, time_t *pi_length )
760 if( p_sys->i_network_time == 0 || !p_pmt || p_pmt->eit.i_event_length == 0 )
761 return VLC_EGENERIC;
763 if( p_pmt->eit.i_event_start <= p_sys->i_network_time &&
764 p_sys->i_network_time < p_pmt->eit.i_event_start + p_pmt->eit.i_event_length )
766 if( pi_length )
767 *pi_length = p_pmt->eit.i_event_length;
768 if( pi_time )
770 *pi_time = p_sys->i_network_time - p_pmt->eit.i_event_start;
771 *pi_time += time(NULL) - p_sys->i_network_time_update;
773 return VLC_SUCCESS;
776 return VLC_EGENERIC;
779 static inline void HasSelectedES( es_out_t *out, const ts_es_t *p_es,
780 const ts_pmt_t *p_pmt, bool *pb_stream_selected )
782 for( ; p_es && !*pb_stream_selected; p_es = p_es->p_next )
784 if( p_es->id )
785 es_out_Control( out, ES_OUT_GET_ES_STATE,
786 p_es->id, pb_stream_selected );
787 HasSelectedES( out, p_es->p_extraes, p_pmt, pb_stream_selected );
791 void UpdatePESFilters( demux_t *p_demux, bool b_all )
793 demux_sys_t *p_sys = p_demux->p_sys;
794 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
796 /* We need 3 pass to avoid loss on deselect/relesect with hw filters and
797 because pid could be shared and its state altered by another unselected pmt
798 First clear flag on every referenced pid
799 Then add flag if non on each selected pmt/pcr and active es
800 Then commit it at hardware level if any */
802 /* clear selection flag on every pmt referenced pid */
803 for( int i=0; i< p_pat->programs.i_size; i++ )
805 ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
806 ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
808 p_pmt_pid->i_flags &= ~FLAG_FILTERED;
809 for( int j=0; j< p_pmt->e_streams.i_size; j++ )
810 p_pmt->e_streams.p_elems[j]->i_flags &= ~FLAG_FILTERED;
811 GetPID(p_sys, p_pmt->i_pid_pcr)->i_flags &= ~FLAG_FILTERED;
814 /* set selection flag on selected pmt referenced pid with active es */
815 for( int i=0; i< p_pat->programs.i_size; i++ )
817 ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
818 ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
820 if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
821 p_pmt->b_selected = true;
822 else
823 p_pmt->b_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
825 if( p_pmt->b_selected )
827 p_pmt_pid->i_flags |= FLAG_FILTERED;
829 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
831 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
832 ts_stream_t *p_pes = espid->u.p_stream;
834 bool b_stream_selected = true;
835 if( !p_pes->b_always_receive && !b_all )
836 HasSelectedES( p_demux->out, p_pes->p_es, p_pmt, &b_stream_selected );
838 if( b_stream_selected )
840 msg_Dbg( p_demux, "enabling pid %d from program %d", espid->i_pid, p_pmt->i_number );
841 espid->i_flags |= FLAG_FILTERED;
845 /* Select pcr last in case it is handled by unselected ES */
846 if( p_pmt->i_pid_pcr > 0 )
848 GetPID(p_sys, p_pmt->i_pid_pcr)->i_flags |= FLAG_FILTERED;
849 msg_Dbg( p_demux, "enabling pcr pid %d from program %d", p_pmt->i_pid_pcr, p_pmt->i_number );
854 /* Commit HW changes based on flags */
855 for( int i=0; i< p_pat->programs.i_size; i++ )
857 ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
858 ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
860 UpdateHWFilter( p_sys, p_pmt_pid );
861 for( int j=0; j< p_pmt->e_streams.i_size; j++ )
863 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
864 UpdateHWFilter( p_sys, espid );
865 if( (espid->i_flags & FLAG_FILTERED) == 0 )
866 FlushESBuffer( espid->u.p_stream );
868 UpdateHWFilter( p_sys, GetPID(p_sys, p_pmt->i_pid_pcr) );
872 static int Control( demux_t *p_demux, int i_query, va_list args )
874 demux_sys_t *p_sys = p_demux->p_sys;
875 double f, *pf;
876 bool b_bool, *pb_bool;
877 int64_t i64;
878 int64_t *pi64;
879 int i_int;
880 const ts_pmt_t *p_pmt = NULL;
881 const ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
883 for( int i=0; i<p_pat->programs.i_size && !p_pmt; i++ )
885 if( p_pat->programs.p_elems[i]->u.p_pmt->b_selected )
886 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
889 switch( i_query )
891 case DEMUX_CAN_SEEK:
892 *va_arg( args, bool * ) = p_sys->b_canseek;
893 return VLC_SUCCESS;
895 case DEMUX_GET_POSITION:
896 pf = va_arg( args, double * );
898 /* Access control test is because EPG for recordings is not relevant */
899 if( p_sys->b_access_control )
901 time_t i_time, i_length;
902 if( !EITCurrentEventTime( p_pmt, p_sys, &i_time, &i_length ) && i_length > 0 )
904 *pf = (double)i_time/(double)i_length;
905 return VLC_SUCCESS;
909 if( !p_sys->b_ignore_time_for_positions &&
910 p_pmt &&
911 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
912 p_pmt->pcr.i_current > -1 )
914 double i_length = TimeStampWrapAround( p_pmt->pcr.i_first,
915 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
916 i_length += p_pmt->pcr.i_pcroffset;
917 double i_pos = TimeStampWrapAround( p_pmt->pcr.i_first,
918 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
919 if( i_length > 0 )
921 *pf = i_pos / i_length;
922 return VLC_SUCCESS;
926 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
928 uint64_t offset = vlc_stream_Tell( p_sys->stream );
929 *pf = (double)offset / (double)i64;
930 return VLC_SUCCESS;
932 break;
934 case DEMUX_SET_POSITION:
935 f = va_arg( args, double );
936 b_bool = (bool) va_arg( args, int ); /* precise */
938 if(!p_sys->b_canseek)
939 break;
941 if( p_sys->b_access_control &&
942 !p_sys->b_ignore_time_for_positions && b_bool && p_pmt )
944 time_t i_time, i_length;
945 if( !EITCurrentEventTime( p_pmt, p_sys, &i_time, &i_length ) &&
946 i_length > 0 && !SeekToTime( p_demux, p_pmt, (int64_t)(TO_SCALE(i_length * CLOCK_FREQ) * f) ) )
948 ReadyQueuesPostSeek( p_demux );
949 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
950 (int64_t)(TO_SCALE(i_length * CLOCK_FREQ) * f) );
951 return VLC_SUCCESS;
955 if( !p_sys->b_ignore_time_for_positions && b_bool && p_pmt &&
956 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
957 p_pmt->pcr.i_current > -1 )
959 int64_t i_length = TimeStampWrapAround( p_pmt->pcr.i_first,
960 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
961 i64 = p_pmt->pcr.i_first + (int64_t)(i_length * f);
962 if( i64 <= p_pmt->i_last_dts )
964 if( !SeekToTime( p_demux, p_pmt, i64 ) )
966 ReadyQueuesPostSeek( p_demux );
967 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, FROM_SCALE(i64) );
968 return VLC_SUCCESS;
973 i64 = stream_Size( p_sys->stream );
974 if( i64 > 0 &&
975 vlc_stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
977 ReadyQueuesPostSeek( p_demux );
978 return VLC_SUCCESS;
980 break;
982 case DEMUX_SET_TIME:
983 i64 = va_arg( args, int64_t );
985 if( p_sys->b_canseek && p_pmt && p_pmt->pcr.i_first > -1 &&
986 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
988 ReadyQueuesPostSeek( p_demux );
989 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
990 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
991 return VLC_SUCCESS;
993 break;
995 case DEMUX_GET_TIME:
996 pi64 = va_arg( args, int64_t * );
998 if( p_sys->b_access_control )
1000 time_t i_event_start;
1001 if( !EITCurrentEventTime( p_pmt, p_sys, &i_event_start, NULL ) )
1003 *pi64 = i_event_start * CLOCK_FREQ;
1004 return VLC_SUCCESS;
1008 if( p_pmt && p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1010 int64_t i_pcr = TimeStampWrapAround( p_pmt->pcr.i_first, p_pmt->pcr.i_current );
1011 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1012 return VLC_SUCCESS;
1014 break;
1016 case DEMUX_GET_LENGTH:
1017 pi64 = va_arg( args, int64_t * );
1019 if( p_sys->b_access_control )
1021 time_t i_event_duration;
1022 if( !EITCurrentEventTime( p_pmt, p_sys, NULL, &i_event_duration ) )
1024 *pi64 = i_event_duration * CLOCK_FREQ;
1025 return VLC_SUCCESS;
1029 if( !p_sys->b_ignore_time_for_positions &&
1030 p_pmt &&
1031 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1032 p_pmt->i_last_dts > 0 )
1034 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1035 TO_SCALE(p_pmt->pcr.i_first_dts);
1036 int64_t i_last = TimeStampWrapAround( p_pmt->pcr.i_first, p_pmt->i_last_dts );
1037 i_last += p_pmt->pcr.i_pcroffset;
1038 *pi64 = FROM_SCALE(i_last - i_start);
1039 return VLC_SUCCESS;
1041 break;
1043 case DEMUX_SET_GROUP:
1045 vlc_list_t *p_list;
1047 i_int = va_arg( args, int );
1048 p_list = va_arg( args, vlc_list_t * );
1049 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, (void *)p_list );
1051 if( i_int != 0 ) /* If not default program */
1053 /* Deselect/filter current ones */
1054 ARRAY_RESET( p_sys->programs );
1056 if( i_int != -1 )
1058 p_sys->b_es_all = false;
1059 ARRAY_APPEND( p_sys->programs, i_int );
1060 UpdatePESFilters( p_demux, false );
1062 else if( likely( p_list != NULL ) )
1064 p_sys->b_es_all = false;
1065 for( int i = 0; i < p_list->i_count; i++ )
1066 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1067 UpdatePESFilters( p_demux, false );
1069 else // All ES Mode
1071 p_sys->b_es_all = true;
1072 p_pat = GetPID(p_sys, 0)->u.p_pat;
1073 for( int i = 0; i < p_pat->programs.i_size; i++ )
1074 ARRAY_APPEND( p_sys->programs, p_pat->programs.p_elems[i]->i_pid );
1075 UpdatePESFilters( p_demux, true );
1078 p_sys->b_default_selection = false;
1081 return VLC_SUCCESS;
1084 case DEMUX_SET_ES:
1086 i_int = va_arg( args, int );
1087 msg_Dbg( p_demux, "DEMUX_SET_ES %d", i_int );
1089 if( !p_sys->b_es_all ) /* Won't change anything */
1090 UpdatePESFilters( p_demux, false );
1092 return VLC_SUCCESS;
1095 case DEMUX_GET_TITLE_INFO:
1097 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1098 int *c = va_arg( args, int * );
1100 *va_arg( args, int* ) = 0; /* Title offset */
1101 *va_arg( args, int* ) = 0; /* Chapter offset */
1102 return vlc_stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v,
1103 c );
1106 case DEMUX_SET_TITLE:
1107 return vlc_stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1109 case DEMUX_SET_SEEKPOINT:
1110 return vlc_stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT,
1111 args );
1113 case DEMUX_GET_META:
1114 return vlc_stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1116 case DEMUX_CAN_RECORD:
1117 pb_bool = va_arg( args, bool * );
1118 *pb_bool = true;
1119 return VLC_SUCCESS;
1121 case DEMUX_SET_RECORD_STATE:
1122 b_bool = va_arg( args, int );
1124 if( !b_bool )
1125 vlc_stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE,
1126 false );
1127 p_sys->b_start_record = b_bool;
1128 return VLC_SUCCESS;
1130 case DEMUX_GET_SIGNAL:
1131 return vlc_stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1133 case DEMUX_GET_ATTACHMENTS:
1135 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t *** );
1136 int *pi_int = va_arg( args, int * );
1138 *pi_int = vlc_dictionary_keys_count( &p_sys->attachments );
1139 if( *pi_int <= 0 )
1140 return VLC_EGENERIC;
1142 *ppp_attach = malloc( sizeof(input_attachment_t*) * *pi_int );
1143 if( !*ppp_attach )
1144 return VLC_EGENERIC;
1146 *pi_int = 0;
1147 for( int i = 0; i < p_sys->attachments.i_size; i++ )
1149 for( vlc_dictionary_entry_t *p_entry = p_sys->attachments.p_entries[i];
1150 p_entry; p_entry = p_entry->p_next )
1152 msg_Err(p_demux, "GET ATTACHMENT %s", p_entry->psz_key);
1153 (*ppp_attach)[*pi_int] = vlc_input_attachment_Duplicate(
1154 (input_attachment_t *) p_entry->p_value );
1155 if( (*ppp_attach)[*pi_int] )
1156 (*pi_int)++;
1160 return VLC_SUCCESS;
1163 default:
1164 break;
1167 return VLC_EGENERIC;
1170 /*****************************************************************************
1172 *****************************************************************************/
1173 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1175 if (*len < 2)
1176 return -1;
1178 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1180 *len -= 2;
1181 *buf += 2;
1183 if (ret & (3<<13))
1184 ret = -1;
1186 return ret;
1189 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1191 block_t *p_chain = NULL;
1192 block_t **pp_chain_last = &p_chain;
1194 uint8_t *buf = block->p_buffer;
1195 size_t len = block->i_buffer;
1197 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1198 int16_t start_trim = 0, end_trim = 0;
1199 int start_trim_flag = (buf[1] >> 4) & 1;
1200 int end_trim_flag = (buf[1] >> 3) & 1;
1201 int control_extension_flag = (buf[1] >> 2) & 1;
1203 len -= 2;
1204 buf += 2;
1206 unsigned au_size = 0;
1207 while (len--) {
1208 int c = *buf++;
1209 au_size += c;
1210 if (c != 0xff)
1211 break;
1214 if (start_trim_flag) {
1215 start_trim = read_opus_flag(&buf, &len);
1216 if (start_trim < 0) {
1217 msg_Err(demux, "Invalid start trimming flag");
1220 if (end_trim_flag) {
1221 end_trim = read_opus_flag(&buf, &len);
1222 if (end_trim < 0) {
1223 msg_Err(demux, "Invalid end trimming flag");
1226 if (control_extension_flag && len) {
1227 unsigned l = *buf++; len--;
1228 if (l > len) {
1229 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
1230 break;
1232 buf += l;
1233 len -= l;
1236 if (!au_size || au_size > len) {
1237 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
1238 break;
1241 block_t *au = block_Alloc(au_size);
1242 if (!au)
1243 break;
1244 memcpy(au->p_buffer, buf, au_size);
1245 block_CopyProperties(au, block);
1246 block_ChainLastAppend( &pp_chain_last, au );
1248 au->i_nb_samples = opus_frame_duration(buf, au_size);
1249 if (end_trim && (uint16_t) end_trim <= au->i_nb_samples)
1250 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
1251 else
1252 au->i_length = 0;
1254 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
1255 au->i_nb_samples -= start_trim;
1256 if (au->i_nb_samples == 0)
1257 au->i_flags |= BLOCK_FLAG_PREROLL;
1260 buf += au_size;
1261 len -= au_size;
1264 block_Release(block);
1265 return p_chain;
1268 static block_t *J2K_Parse( demux_t *p_demux, block_t *p_block, bool b_interlaced )
1270 const uint8_t *p_buf = p_block->p_buffer;
1272 if( p_block->i_buffer < ((b_interlaced) ? 48 : 38) )
1273 goto invalid;
1275 if( memcmp( p_buf, "elsmfrat", 8 ) )
1276 goto invalid;
1278 uint16_t i_den = GetWBE( &p_buf[8] );
1279 uint16_t i_num = GetWBE( &p_buf[10] );
1280 if( i_den == 0 )
1281 goto invalid;
1282 p_block->i_length = CLOCK_FREQ * i_den / i_num;
1284 p_block->p_buffer += (b_interlaced) ? 48 : 38;
1285 p_block->i_buffer -= (b_interlaced) ? 48 : 38;
1287 return p_block;
1289 invalid:
1290 msg_Warn( p_demux, "invalid J2K header, dropping codestream" );
1291 block_Release( p_block );
1292 return NULL;
1295 static block_t * ConvertPESBlock( demux_t *p_demux, ts_es_t *p_es,
1296 size_t i_pes_size, uint8_t i_stream_id,
1297 block_t *p_block )
1299 if(!p_block)
1300 return NULL;
1302 if( p_es->fmt.i_codec == VLC_CODEC_SUBT )
1304 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1306 p_block->i_buffer = i_pes_size;
1308 /* Append a \0 */
1309 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1310 if( p_block )
1311 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1313 else if( p_es->fmt.i_codec == VLC_CODEC_TELETEXT )
1315 if( p_block->i_pts <= VLC_TS_INVALID )
1317 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
1318 * In this case use the last PCR + 40ms */
1319 mtime_t i_pcr = p_es->p_program->pcr.i_current;
1320 if( i_pcr > VLC_TS_INVALID )
1321 p_block->i_pts = FROM_SCALE(i_pcr) + 40000;
1324 else if( p_es->fmt.i_codec == VLC_CODEC_ARIB_A ||
1325 p_es->fmt.i_codec == VLC_CODEC_ARIB_C )
1327 if( p_block->i_pts <= VLC_TS_INVALID )
1329 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1331 p_block->i_buffer = i_pes_size;
1333 /* Append a \0 */
1334 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1335 if( p_block )
1336 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1339 else if( p_es->fmt.i_codec == VLC_CODEC_OPUS)
1341 p_block = Opus_Parse(p_demux, p_block);
1343 else if( p_es->fmt.i_codec == VLC_CODEC_JPEG2000 )
1345 if( unlikely(i_stream_id != 0xBD) )
1347 block_Release( p_block );
1348 p_block = NULL;
1350 else
1352 p_block = J2K_Parse( p_demux, p_block, p_es->b_interlaced );
1356 return p_block;
1359 /****************************************************************************
1360 * fanouts current block to all subdecoders / shared pid es
1361 ****************************************************************************/
1362 static void SendDataChain( demux_t *p_demux, ts_es_t *p_es, block_t *p_chain )
1364 while( p_chain )
1366 block_t *p_block = p_chain;
1367 p_chain = p_block->p_next;
1368 p_block->p_next = NULL;
1370 ts_es_t *p_es_send = p_es;
1371 if( p_es_send->i_next_block_flags )
1373 p_block->i_flags |= p_es_send->i_next_block_flags;
1374 p_es_send->i_next_block_flags = 0;
1377 while( p_es_send )
1379 if( p_es_send->p_program->b_selected )
1381 /* Send a copy to each extra es */
1382 ts_es_t *p_extra_es = p_es_send->p_extraes;
1383 while( p_extra_es )
1385 if( p_extra_es->id )
1387 block_t *p_dup = block_Duplicate( p_block );
1388 if( p_dup )
1389 es_out_Send( p_demux->out, p_extra_es->id, p_dup );
1391 p_extra_es = p_extra_es->p_next;
1394 if( p_es_send->p_next )
1396 if( p_es_send->id )
1398 block_t *p_dup = block_Duplicate( p_block );
1399 if( p_dup )
1400 es_out_Send( p_demux->out, p_es_send->id, p_dup );
1403 else
1405 if( p_es_send->id )
1407 es_out_Send( p_demux->out, p_es_send->id, p_block );
1408 p_block = NULL;
1412 p_es_send = p_es_send->p_next;
1415 if( p_block )
1416 block_Release( p_block );
1420 /****************************************************************************
1421 * gathering stuff
1422 ****************************************************************************/
1423 static void ParsePESDataChain( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
1425 uint8_t header[34];
1426 unsigned i_pes_size = 0;
1427 unsigned i_skip = 0;
1428 mtime_t i_dts = -1;
1429 mtime_t i_pts = -1;
1430 mtime_t i_length = 0;
1431 uint8_t i_stream_id;
1432 bool b_pes_scrambling = false;
1433 const es_mpeg4_descriptor_t *p_mpeg4desc = NULL;
1435 assert(pid->type == TYPE_STREAM);
1437 const int i_max = block_ChainExtract( p_pes, header, 34 );
1438 if ( i_max < 4 )
1440 block_ChainRelease( p_pes );
1441 return;
1444 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1446 if ( !(p_pes->i_flags & BLOCK_FLAG_SCRAMBLED) )
1447 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1448 header[0], header[1],header[2],header[3], pid->i_pid );
1449 block_ChainRelease( p_pes );
1450 return;
1452 else
1454 /* Incorrect transport scrambling flag set by german boxes */
1455 p_pes->i_flags &= ~BLOCK_FLAG_SCRAMBLED;
1458 ts_es_t *p_es = pid->u.p_stream->p_es;
1460 if( ParsePESHeader( VLC_OBJECT(p_demux), (uint8_t*)&header, i_max, &i_skip,
1461 &i_dts, &i_pts, &i_stream_id, &b_pes_scrambling ) == VLC_EGENERIC )
1463 block_ChainRelease( p_pes );
1464 return;
1466 else
1468 if( i_pts != -1 && p_es->p_program )
1469 i_pts = TimeStampWrapAround( p_es->p_program->pcr.i_first, i_pts );
1470 if( i_dts != -1 && p_es->p_program )
1471 i_dts = TimeStampWrapAround( p_es->p_program->pcr.i_first, i_dts );
1472 if( b_pes_scrambling )
1473 p_pes->i_flags |= BLOCK_FLAG_SCRAMBLED;
1476 if( p_es->i_sl_es_id )
1477 p_mpeg4desc = GetMPEG4DescByEsId( p_es->p_program, p_es->i_sl_es_id );
1479 if( p_es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1480 p_es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1482 i_skip += 4;
1484 else if( p_es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1485 p_es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1486 p_es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1488 i_skip += 1;
1490 else if( p_es->fmt.i_codec == VLC_CODEC_SUBT && p_mpeg4desc )
1492 const decoder_config_descriptor_t *dcd = &p_mpeg4desc->dec_descr;
1494 if( dcd->i_extra > 2 &&
1495 dcd->p_extra[0] == 0x10 &&
1496 ( dcd->p_extra[1]&0x10 ) )
1498 /* display length */
1499 if( p_pes->i_buffer + 2 <= i_skip )
1500 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1502 i_skip += 2;
1504 if( p_pes->i_buffer + 2 <= i_skip )
1505 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1506 /* */
1507 i_skip += 2;
1510 /* skip header */
1511 while( p_pes && i_skip > 0 )
1513 if( p_pes->i_buffer <= i_skip )
1515 block_t *p_next = p_pes->p_next;
1517 i_skip -= p_pes->i_buffer;
1518 block_Release( p_pes );
1519 p_pes = p_next;
1521 else
1523 p_pes->i_buffer -= i_skip;
1524 p_pes->p_buffer += i_skip;
1525 break;
1529 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1530 if( i_pts >= 0 && i_dts < 0 )
1531 i_dts = i_pts;
1533 if( p_pes )
1535 ts_pmt_t *p_pmt = p_es->p_program;
1536 if( unlikely(!p_pmt) )
1538 block_ChainRelease( p_pes );
1539 return;
1542 if( i_dts >= 0 )
1543 p_pes->i_dts = FROM_SCALE(i_dts);
1545 if( i_pts >= 0 )
1546 p_pes->i_pts = FROM_SCALE(i_pts);
1548 p_pes->i_length = FROM_SCALE_NZ(i_length);
1550 /* Can become a chain on next call due to prepcr */
1551 block_t *p_chain = block_ChainGather( p_pes );
1552 while ( p_chain ) {
1553 block_t *p_block = p_chain;
1554 p_chain = p_chain->p_next;
1555 p_block->p_next = NULL;
1557 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
1558 PCRFixHandle( p_demux, p_pmt, p_block );
1560 if( p_es->id && (p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable) )
1562 if( pid->u.p_stream->prepcr.p_head )
1564 /* Rebuild current output chain, appending any prepcr outqueue */
1565 block_ChainLastAppend( &pid->u.p_stream->prepcr.pp_last, p_block );
1566 if( p_chain )
1567 block_ChainLastAppend( &pid->u.p_stream->prepcr.pp_last, p_chain );
1568 p_chain = pid->u.p_stream->prepcr.p_head;
1569 pid->u.p_stream->prepcr.p_head = NULL;
1570 pid->u.p_stream->prepcr.pp_last = &pid->u.p_stream->prepcr.p_head;
1571 /* Then now output all data */
1572 continue;
1575 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
1576 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
1578 ProgramSetPCR( p_demux, p_pmt, TO_SCALE(p_block->i_dts) - 120000 );
1581 /* Compute PCR/DTS offset if any */
1582 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
1583 p_pmt->pcr.i_current > VLC_TS_INVALID &&
1584 (p_es->fmt.i_cat == VIDEO_ES || p_es->fmt.i_cat == AUDIO_ES) )
1586 int64_t i_dts27 = TO_SCALE(p_block->i_dts);
1587 i_dts27 = TimeStampWrapAround( p_pmt->pcr.i_first, i_dts27 );
1588 int64_t i_pcr = TimeStampWrapAround( p_pmt->pcr.i_first, p_pmt->pcr.i_current );
1589 if( i_dts27 < i_pcr )
1591 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
1592 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
1593 "us later than pcr, applying delay",
1594 pid->i_pid, FROM_SCALE_NZ(p_pmt->pcr.i_pcroffset) );
1596 else p_pmt->pcr.i_pcroffset = 0;
1599 if( p_pmt->pcr.i_pcroffset != -1 )
1601 if( p_block->i_dts > VLC_TS_INVALID )
1602 p_block->i_dts += FROM_SCALE_NZ(p_pmt->pcr.i_pcroffset);
1603 if( p_block->i_pts > VLC_TS_INVALID )
1604 p_block->i_pts += FROM_SCALE_NZ(p_pmt->pcr.i_pcroffset);
1607 /*** From here, block can become a chain again though conversion below ***/
1609 if( pid->u.p_stream->p_proc )
1611 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
1612 ts_stream_processor_Reset( pid->u.p_stream->p_proc );
1613 p_block = ts_stream_processor_Push( pid->u.p_stream->p_proc, i_stream_id, p_block );
1615 else
1616 /* Some codecs might need xform or AU splitting */
1618 p_block = ConvertPESBlock( p_demux, p_es, i_pes_size, i_stream_id, p_block );
1621 SendDataChain( p_demux, p_es, p_block );
1623 else
1625 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
1626 PCRFixHandle( p_demux, p_pmt, p_block );
1628 block_ChainLastAppend( &pid->u.p_stream->prepcr.pp_last, p_block );
1630 /* PCR Seen and no es->id, cleanup current and prepcr blocks */
1631 if( p_pmt->pcr.i_current > -1)
1633 block_ChainRelease( pid->u.p_stream->prepcr.p_head );
1634 pid->u.p_stream->prepcr.p_head = NULL;
1635 pid->u.p_stream->prepcr.pp_last = &pid->u.p_stream->prepcr.p_head;
1640 else
1642 msg_Warn( p_demux, "empty pes" );
1646 static bool PushPESBlock( demux_t *p_demux, ts_pid_t *pid, block_t *p_pkt, bool b_unit_start )
1648 bool b_ret = false;
1649 ts_stream_t *p_pes = pid->u.p_stream;
1651 if ( b_unit_start && p_pes->gather.p_data )
1653 block_t *p_datachain = p_pes->gather.p_data;
1654 /* Flush the pes from pid */
1655 p_pes->gather.p_data = NULL;
1656 p_pes->gather.i_data_size = 0;
1657 p_pes->gather.i_gathered = 0;
1658 p_pes->gather.pp_last = &p_pes->gather.p_data;
1659 ParsePESDataChain( p_demux, pid, p_datachain );
1660 b_ret = true;
1663 if( p_pkt == NULL )
1664 return b_ret;
1666 if( !b_unit_start && p_pes->gather.p_data == NULL )
1668 /* msg_Dbg( p_demux, "broken packet" ); */
1669 block_Release( p_pkt );
1670 return b_ret;
1673 block_ChainLastAppend( &p_pes->gather.pp_last, p_pkt );
1674 p_pes->gather.i_gathered += p_pkt->i_buffer;
1676 if( p_pes->gather.i_data_size > 0 &&
1677 p_pes->gather.i_gathered >= p_pes->gather.i_data_size )
1679 /* re-enter in Flush above */
1680 assert(p_pes->gather.p_data);
1681 return PushPESBlock( p_demux, pid, NULL, true );
1684 return b_ret;
1687 static block_t* ReadTSPacket( demux_t *p_demux )
1689 demux_sys_t *p_sys = p_demux->p_sys;
1691 block_t *p_pkt;
1693 /* Get a new TS packet */
1694 if( !( p_pkt = vlc_stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
1696 int64_t size = stream_Size( p_sys->stream );
1697 if( size >= 0 && (uint64_t)size == vlc_stream_Tell( p_sys->stream ) )
1698 msg_Dbg( p_demux, "EOF at %"PRIu64, vlc_stream_Tell( p_sys->stream ) );
1699 else
1700 msg_Dbg( p_demux, "Can't read TS packet at %"PRIu64, vlc_stream_Tell(p_sys->stream) );
1701 return NULL;
1704 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
1706 block_Release( p_pkt );
1707 return NULL;
1710 /* Skip header (BluRay streams).
1711 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
1712 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
1714 p_pkt->p_buffer += p_sys->i_packet_header_size;
1715 p_pkt->i_buffer -= p_sys->i_packet_header_size;
1717 /* Check sync byte and re-sync if needed */
1718 if( p_pkt->p_buffer[0] != 0x47 )
1720 msg_Warn( p_demux, "lost synchro" );
1721 block_Release( p_pkt );
1722 for( ;; )
1724 const uint8_t *p_peek;
1725 int i_peek = 0;
1726 unsigned i_skip = 0;
1728 i_peek = vlc_stream_Peek( p_sys->stream, &p_peek,
1729 p_sys->i_packet_size * 10 );
1730 if( i_peek < 0 || (unsigned)i_peek < p_sys->i_packet_size + 1 )
1732 msg_Dbg( p_demux, "eof ?" );
1733 return NULL;
1736 while( i_skip < i_peek - p_sys->i_packet_size )
1738 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
1739 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
1741 break;
1743 i_skip++;
1745 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1746 if (vlc_stream_Read( p_sys->stream, NULL, i_skip ) != i_skip)
1747 return NULL;
1749 if( i_skip < i_peek - p_sys->i_packet_size )
1751 break;
1754 if( !( p_pkt = vlc_stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
1756 msg_Dbg( p_demux, "eof ?" );
1757 return NULL;
1760 return p_pkt;
1763 static mtime_t GetPCR( const block_t *p_pkt )
1765 const uint8_t *p = p_pkt->p_buffer;
1767 mtime_t i_pcr = -1;
1769 if( likely(p_pkt->i_buffer > 11) &&
1770 ( p[3]&0x20 ) && /* adaptation */
1771 ( p[5]&0x10 ) &&
1772 ( p[4] >= 7 ) )
1774 /* PCR is 33 bits */
1775 i_pcr = ( (mtime_t)p[6] << 25 ) |
1776 ( (mtime_t)p[7] << 17 ) |
1777 ( (mtime_t)p[8] << 9 ) |
1778 ( (mtime_t)p[9] << 1 ) |
1779 ( (mtime_t)p[10] >> 7 );
1781 return i_pcr;
1784 static inline void UpdateESScrambledState( es_out_t *out, const ts_es_t *p_es, bool b_scrambled )
1786 for( ; p_es; p_es = p_es->p_next )
1788 if( p_es->id )
1789 es_out_Control( out, ES_OUT_SET_ES_SCRAMBLED_STATE,
1790 p_es->id, b_scrambled );
1791 UpdateESScrambledState( out, p_es->p_extraes, b_scrambled );
1795 static void UpdatePIDScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool b_scrambled )
1797 if( !SCRAMBLED(*p_pid) == !b_scrambled )
1798 return;
1800 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
1801 p_pid->i_pid, !!SCRAMBLED(*p_pid), b_scrambled );
1803 if( b_scrambled )
1804 p_pid->i_flags |= FLAG_SCRAMBLED;
1805 else
1806 p_pid->i_flags &= ~FLAG_SCRAMBLED;
1808 if( p_pid->type == TYPE_STREAM )
1809 UpdateESScrambledState( p_demux->out, p_pid->u.p_stream->p_es, b_scrambled );
1812 static inline void FlushESBuffer( ts_stream_t *p_pes )
1814 if( p_pes->gather.p_data )
1816 p_pes->gather.i_gathered = p_pes->gather.i_data_size = 0;
1817 block_ChainRelease( p_pes->gather.p_data );
1818 p_pes->gather.p_data = NULL;
1819 p_pes->gather.pp_last = &p_pes->gather.p_data;
1820 p_pes->gather.i_saved = 0;
1822 if( p_pes->p_proc )
1823 ts_stream_processor_Reset( p_pes->p_proc );
1826 static void ReadyQueuesPostSeek( demux_t *p_demux )
1828 demux_sys_t *p_sys = p_demux->p_sys;
1830 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
1831 for( int i=0; i< p_pat->programs.i_size; i++ )
1833 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1834 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
1836 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
1837 ts_stream_t *p_pes = pid->u.p_stream;
1839 if( pid->type != TYPE_STREAM )
1840 continue;
1842 for( ts_es_t *p_es = p_pes->p_es; p_es; p_es = p_es->p_next )
1843 p_es->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1845 pid->i_cc = 0xff;
1847 if( pid->u.p_stream->prepcr.p_head )
1849 block_ChainRelease( pid->u.p_stream->prepcr.p_head );
1850 pid->u.p_stream->prepcr.p_head = NULL;
1851 pid->u.p_stream->prepcr.pp_last = &pid->u.p_stream->prepcr.p_head;
1854 ts_sections_processor_Reset( pid->u.p_stream->p_sections_proc );
1855 ts_stream_processor_Reset( pid->u.p_stream->p_proc );
1857 FlushESBuffer( pid->u.p_stream );
1859 p_pmt->pcr.i_current = -1;
1863 static int SeekToTime( demux_t *p_demux, const ts_pmt_t *p_pmt, int64_t i_scaledtime )
1865 demux_sys_t *p_sys = p_demux->p_sys;
1867 /* Deal with common but worst binary search case */
1868 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
1869 return vlc_stream_Seek( p_sys->stream, 0 );
1871 const int64_t i_stream_size = stream_Size( p_sys->stream );
1872 if( !p_sys->b_canfastseek || i_stream_size < p_sys->i_packet_size )
1873 return VLC_EGENERIC;
1875 const uint64_t i_initial_pos = vlc_stream_Tell( p_sys->stream );
1877 /* Find the time position by using binary search algorithm. */
1878 uint64_t i_head_pos = 0;
1879 uint64_t i_tail_pos = (uint64_t) i_stream_size - p_sys->i_packet_size;
1880 if( i_head_pos >= i_tail_pos )
1881 return VLC_EGENERIC;
1883 bool b_found = false;
1884 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
1886 /* Round i_pos to a multiple of p_sys->i_packet_size */
1887 uint64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
1888 uint64_t i_div = i_splitpos % p_sys->i_packet_size;
1889 i_splitpos -= i_div;
1891 if ( vlc_stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
1892 break;
1894 uint64_t i_pos = i_splitpos;
1895 while( i_pos < i_tail_pos )
1897 int64_t i_pcr = -1;
1898 block_t *p_pkt = ReadTSPacket( p_demux );
1899 if( !p_pkt )
1901 i_head_pos = i_tail_pos;
1902 break;
1904 else
1905 i_pos = vlc_stream_Tell( p_sys->stream );
1907 int i_pid = PIDGet( p_pkt );
1908 ts_pid_t *p_pid = GetPID(p_sys, i_pid);
1909 if( i_pid != 0x1FFF && p_pid->type == TYPE_STREAM &&
1910 ts_stream_Find_es( p_pid->u.p_stream, p_pmt ) &&
1911 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1912 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
1915 unsigned i_skip = 4;
1916 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
1918 if( p_pkt->i_buffer >= 4 + 2 + 5 )
1920 i_pcr = GetPCR( p_pkt );
1921 i_skip += 1 + p_pkt->p_buffer[4];
1924 else
1926 mtime_t i_dts = -1;
1927 mtime_t i_pts = -1;
1928 uint8_t i_stream_id;
1929 if ( VLC_SUCCESS == ParsePESHeader( VLC_OBJECT(p_demux), &p_pkt->p_buffer[i_skip],
1930 p_pkt->i_buffer - i_skip, &i_skip,
1931 &i_dts, &i_pts, &i_stream_id, NULL ) )
1933 if( i_dts > -1 )
1934 i_pcr = i_dts;
1938 block_Release( p_pkt );
1940 if( i_pcr != -1 )
1942 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt->pcr.i_first, i_pcr );
1943 if ( i_diff < 0 )
1944 i_tail_pos = (i_splitpos >= p_sys->i_packet_size) ? i_splitpos - p_sys->i_packet_size : 0;
1945 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
1946 b_found = true;
1947 else
1948 i_head_pos = i_pos;
1949 break;
1953 if ( !b_found && i_pos + p_sys->i_packet_size > i_tail_pos )
1954 i_tail_pos = (i_splitpos >= p_sys->i_packet_size) ? i_splitpos - p_sys->i_packet_size : 0;
1957 if( !b_found )
1959 msg_Dbg( p_demux, "Seek():cannot find a time position." );
1960 vlc_stream_Seek( p_sys->stream, i_initial_pos );
1961 return VLC_EGENERIC;
1963 return VLC_SUCCESS;
1966 #define PROBE_CHUNK_COUNT 250
1968 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
1970 demux_sys_t *p_sys = p_demux->p_sys;
1971 int i_count = 0;
1972 block_t *p_pkt = NULL;
1974 for( ;; )
1976 *pi_pcr = -1;
1978 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
1980 break;
1983 if( p_pkt->i_size < TS_PACKET_SIZE_188 &&
1984 ( p_pkt->p_buffer[1]&0x80 ) /* transport error */ )
1986 block_Release( p_pkt );
1987 continue;
1990 const int i_pid = PIDGet( p_pkt );
1991 ts_pid_t *p_pid = GetPID(p_sys, i_pid);
1993 p_pid->i_flags |= FLAG_SEEN;
1995 if( i_pid != 0x1FFF && (p_pkt->p_buffer[1] & 0x80) == 0 ) /* not corrupt */
1997 bool b_pcrresult = true;
1998 bool b_adaptfield = p_pkt->p_buffer[3] & 0x20;
2000 if( b_adaptfield && p_pkt->i_buffer >= 4 + 2 + 5 )
2001 *pi_pcr = GetPCR( p_pkt );
2003 if( *pi_pcr == -1 &&
2004 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* payload start */
2005 (p_pkt->p_buffer[3] & 0xD0) == 0x10 && /* Has payload but is not encrypted */
2006 p_pid->type == TYPE_STREAM &&
2007 p_pid->u.p_stream->p_es->fmt.i_cat != UNKNOWN_ES
2010 b_pcrresult = false;
2011 mtime_t i_dts = -1;
2012 mtime_t i_pts = -1;
2013 uint8_t i_stream_id;
2014 unsigned i_skip = 4;
2015 if ( b_adaptfield ) // adaptation field
2016 i_skip += 1 + p_pkt->p_buffer[4];
2018 if ( VLC_SUCCESS == ParsePESHeader( VLC_OBJECT(p_demux), &p_pkt->p_buffer[i_skip],
2019 p_pkt->i_buffer - i_skip, &i_skip,
2020 &i_dts, &i_pts, &i_stream_id, NULL ) )
2022 if( i_dts != -1 )
2023 *pi_pcr = i_dts;
2024 else if( i_pts != -1 )
2025 *pi_pcr = i_pts;
2029 if( *pi_pcr != -1 )
2031 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
2032 for( int i=0; i<p_pat->programs.i_size; i++ )
2034 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2035 if( p_pmt->i_pid_pcr == p_pid->i_pid ||
2036 ( p_pmt->i_pid_pcr == 0x1FFF &&
2037 PIDReferencedByProgram( p_pmt, p_pid->i_pid ) )
2040 if( b_end )
2042 p_pmt->i_last_dts = *pi_pcr;
2043 p_pmt->i_last_dts_byte = vlc_stream_Tell( p_sys->stream );
2045 /* Start, only keep first */
2046 else if( b_pcrresult && p_pmt->pcr.i_first == -1 )
2048 p_pmt->pcr.i_first = *pi_pcr;
2050 else if( p_pmt->pcr.i_first_dts < VLC_TS_0 )
2052 p_pmt->pcr.i_first_dts = FROM_SCALE(*pi_pcr);
2055 if( i_program == 0 || i_program == p_pmt->i_number )
2056 *pb_found = true;
2062 block_Release( p_pkt );
2065 return i_count;
2068 int ProbeStart( demux_t *p_demux, int i_program )
2070 demux_sys_t *p_sys = p_demux->p_sys;
2071 const uint64_t i_initial_pos = vlc_stream_Tell( p_sys->stream );
2072 int64_t i_stream_size = stream_Size( p_sys->stream );
2074 int i_probe_count = 0;
2075 int64_t i_pos;
2076 mtime_t i_pcr = -1;
2077 bool b_found = false;
2081 i_pos = p_sys->i_packet_size * i_probe_count;
2082 i_pos = __MIN( i_pos, i_stream_size );
2084 if( vlc_stream_Seek( p_sys->stream, i_pos ) )
2085 return VLC_EGENERIC;
2087 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2089 /* Go ahead one more chunk if end of file contained only stuffing packets */
2090 i_probe_count += PROBE_CHUNK_COUNT;
2091 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2093 if( vlc_stream_Seek( p_sys->stream, i_initial_pos ) )
2094 return VLC_EGENERIC;
2096 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2099 int ProbeEnd( demux_t *p_demux, int i_program )
2101 demux_sys_t *p_sys = p_demux->p_sys;
2102 const uint64_t i_initial_pos = vlc_stream_Tell( p_sys->stream );
2103 int64_t i_stream_size = stream_Size( p_sys->stream );
2105 int i_probe_count = PROBE_CHUNK_COUNT;
2106 int64_t i_pos;
2107 mtime_t i_pcr = -1;
2108 bool b_found = false;
2112 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2113 i_pos = __MAX( i_pos, 0 );
2115 if( vlc_stream_Seek( p_sys->stream, i_pos ) )
2116 return VLC_EGENERIC;
2118 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2120 /* Go ahead one more chunk if end of file contained only stuffing packets */
2121 i_probe_count += PROBE_CHUNK_COUNT;
2122 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2124 if( vlc_stream_Seek( p_sys->stream, i_initial_pos ) )
2125 return VLC_EGENERIC;
2127 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2130 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
2132 demux_sys_t *p_sys = p_demux->p_sys;
2134 /* Check if we have enqueued blocks waiting the/before the
2135 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
2136 if( p_pmt->pcr.i_current == -1 && p_pmt->pcr.b_fix_done )
2138 mtime_t i_mindts = -1;
2140 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
2141 for( int i=0; i< p_pat->programs.i_size; i++ )
2143 ts_pmt_t *p_opmt = p_pat->programs.p_elems[i]->u.p_pmt;
2144 for( int j=0; j<p_opmt->e_streams.i_size; j++ )
2146 ts_pid_t *p_pid = p_opmt->e_streams.p_elems[j];
2147 block_t *p_block = p_pid->u.p_stream->prepcr.p_head;
2148 while( p_block && p_block->i_dts == VLC_TS_INVALID )
2149 p_block = p_block->p_next;
2151 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
2152 i_mindts = p_block->i_dts;
2156 if( i_mindts > VLC_TS_INVALID )
2158 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
2159 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
2160 i_pcr = TO_SCALE(i_mindts);
2164 p_pmt->pcr.i_current = i_pcr;
2165 if( p_pmt->pcr.i_first == -1 )
2167 p_pmt->pcr.i_first = i_pcr; // now seen
2170 if ( p_sys->i_pmt_es )
2172 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, p_pmt->i_number, FROM_SCALE(i_pcr) );
2173 /* growing files/named fifo handling */
2174 if( p_sys->b_access_control == false &&
2175 vlc_stream_Tell( p_sys->stream ) > p_pmt->i_last_dts_byte )
2177 p_pmt->i_last_dts = i_pcr;
2178 p_pmt->i_last_dts_byte = vlc_stream_Tell( p_sys->stream );
2183 static int IsVideoEnd( ts_pid_t *p_pid )
2185 /* jump to near end of PES packet */
2186 block_t *p = p_pid->u.p_stream->gather.p_data;
2187 if( !p || !p->p_next )
2188 return 0;
2189 while( p->p_next->p_next )
2190 p = p->p_next;
2191 if( p->p_next->i_buffer > 4)
2192 p = p->p_next;
2194 /* extract last bytes */
2195 uint8_t tail[ 188 ];
2196 const int i_tail = block_ChainExtract( p, tail, sizeof( tail ) );
2197 if( i_tail < 4 )
2198 return 0;
2200 /* check for start code at end */
2201 return ( tail[ i_tail - 4 ] == 0 && tail[ i_tail - 3 ] == 0 && tail[ i_tail - 2 ] == 1 &&
2202 ( tail[ i_tail - 1 ] == 0xb7 || tail[ i_tail - 1 ] == 0x0a ) );
2205 static void PCRCheckDTS( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr)
2207 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
2209 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
2211 if( p_pid->type != TYPE_STREAM || SCRAMBLED(*p_pid) )
2212 continue;
2214 ts_stream_t *p_pes = p_pid->u.p_stream;
2215 ts_es_t *p_es = p_pes->p_es;
2217 if( p_pes->gather.p_data == NULL )
2218 continue;
2219 if( p_pes->gather.i_data_size != 0 )
2220 continue;
2222 /* check only MPEG2, H.264 and VC-1 */
2223 if( p_es->fmt.i_codec != VLC_CODEC_MPGV &&
2224 p_es->fmt.i_codec != VLC_CODEC_H264 &&
2225 p_es->fmt.i_codec != VLC_CODEC_VC1 )
2226 continue;
2228 uint8_t header[34];
2229 const int i_max = block_ChainExtract( p_pes->gather.p_data, header, 34 );
2231 if( i_max < 6 || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2232 continue;
2234 unsigned i_skip = 0;
2235 mtime_t i_dts = -1;
2236 mtime_t i_pts = -1;
2237 uint8_t i_stream_id;
2239 if( ParsePESHeader( VLC_OBJECT(p_demux), (uint8_t*)&header, i_max, &i_skip,
2240 &i_dts, &i_pts, &i_stream_id, NULL ) == VLC_EGENERIC )
2241 continue;
2243 if (p_pmt->pcr.i_pcroffset > 0) {
2244 if( i_dts > VLC_TS_INVALID )
2245 i_dts += p_pmt->pcr.i_pcroffset;
2246 if( i_pts > VLC_TS_INVALID )
2247 i_pts += p_pmt->pcr.i_pcroffset;
2250 if( i_dts > VLC_TS_INVALID )
2251 i_dts = TimeStampWrapAround( i_pcr, i_dts );
2252 if( i_pts > VLC_TS_INVALID )
2253 i_pts = TimeStampWrapAround( i_pcr, i_pts );
2255 if(( i_dts > VLC_TS_INVALID && i_dts <= i_pcr ) ||
2256 ( i_pts > VLC_TS_INVALID && i_pts <= i_pcr ))
2258 if( IsVideoEnd( p_pid ) )
2260 msg_Warn( p_demux, "send queued data for pid %d: TS %"PRId64" <= PCR %"PRId64"\n",
2261 p_pid->i_pid, i_dts > VLC_TS_INVALID ? i_dts : i_pts, i_pcr);
2262 PushPESBlock( p_demux, p_pid, NULL, true ); /* Flush */
2268 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, mtime_t i_pcr )
2270 demux_sys_t *p_sys = p_demux->p_sys;
2272 pid->probed.i_pcr_count++;
2274 if( p_sys->i_pmt_es <= 0 )
2275 return;
2277 if(unlikely(GetPID(p_sys, 0)->type != TYPE_PAT))
2278 return;
2280 /* Search program and set the PCR */
2281 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
2282 for( int i = 0; i < p_pat->programs.i_size; i++ )
2284 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2285 if( p_pmt->pcr.b_disable )
2286 continue;
2287 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt->pcr.i_first, i_pcr );
2289 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2291 if( PIDReferencedByProgram( p_pmt, pid->i_pid ) ) /* PCR shall be on pid itself */
2293 /* ? update PCR for the whole group program ? */
2294 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
2297 else /* set PCR provided by current pid to program(s) referencing it */
2299 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
2300 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
2302 /* We've found a target group for update */
2303 PCRCheckDTS( p_demux, p_pmt, i_pcr );
2304 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
2311 int FindPCRCandidate( ts_pmt_t *p_pmt )
2313 ts_pid_t *p_cand = NULL;
2314 int i_previous = p_pmt->i_pid_pcr;
2316 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
2318 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
2319 if( SEEN(p_pid) && p_pid->i_pid != i_previous )
2321 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
2323 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
2325 p_cand = p_pid;
2326 continue;
2330 if( p_pid->u.p_stream->p_es->fmt.i_cat == AUDIO_ES )
2332 if( !p_cand )
2333 p_cand = p_pid;
2335 else if ( p_pid->u.p_stream->p_es->fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
2337 if( !p_cand || p_cand->u.p_stream->p_es->fmt.i_cat == AUDIO_ES )
2338 p_cand = p_pid;
2343 if( p_cand )
2344 return p_cand->i_pid;
2345 else
2346 return 0x1FFF;
2349 /* Tries to reselect a new PCR when none has been received */
2350 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
2352 if ( p_pmt->pcr.b_disable || p_pmt->pcr.b_fix_done )
2354 return;
2356 /* Record the first data packet timestamp in case there wont be any PCR */
2357 else if( !p_pmt->pcr.i_first_dts )
2359 p_pmt->pcr.i_first_dts = p_block->i_dts;
2361 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "PCR repeat rate shall not exceed 100ms" */
2363 if( p_pmt->pcr.i_current < 0 &&
2364 GetPID( p_demux->p_sys, p_pmt->i_pid_pcr )->probed.i_pcr_count == 0 )
2366 int i_cand = FindPCRCandidate( p_pmt );
2367 p_pmt->i_pid_pcr = i_cand;
2368 if ( GetPID( p_demux->p_sys, p_pmt->i_pid_pcr )->probed.i_pcr_count == 0 )
2369 p_pmt->pcr.b_disable = true;
2370 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
2371 p_pmt->i_number, i_cand );
2372 UpdatePESFilters( p_demux, p_demux->p_sys->b_es_all );
2374 p_pmt->pcr.b_fix_done = true;
2378 static block_t * ProcessTSPacket( demux_t *p_demux, ts_pid_t *pid, block_t *p_pkt, int *pi_skip )
2380 const uint8_t *p = p_pkt->p_buffer;
2381 const bool b_adaptation = p[3]&0x20;
2382 const bool b_payload = p[3]&0x10;
2383 const bool b_scrambled = p[3]&0xc0;
2384 const int i_cc = p[3]&0x0f; /* continuity counter */
2385 bool b_discontinuity = false; /* discontinuity */
2387 /* transport_scrambling_control is ignored */
2388 *pi_skip = 4;
2390 #if 0
2391 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2392 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2393 b_payload, i_cc );
2394 #endif
2396 /* Drop null packets */
2397 if( unlikely(pid->i_pid == 0x1FFF) )
2399 block_Release( p_pkt );
2400 return NULL;
2403 /* For now, ignore additional error correction
2404 * TODO: handle Reed-Solomon 204,188 error correction */
2405 p_pkt->i_buffer = TS_PACKET_SIZE_188;
2407 if( b_scrambled )
2409 if( p_demux->p_sys->csa )
2411 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2412 csa_Decrypt( p_demux->p_sys->csa, p_pkt->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2413 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2415 else
2416 p_pkt->i_flags |= BLOCK_FLAG_SCRAMBLED;
2419 /* We don't have any adaptation_field, so payload starts
2420 * immediately after the 4 byte TS header */
2421 if( b_adaptation )
2423 /* p[4] is adaptation_field_length minus one */
2424 *pi_skip += 1 + p[4];
2425 if( p[4] + 5 > 188 /* adaptation field only == 188 */ )
2427 /* Broken is broken */
2428 block_Release( p_pkt );
2429 return NULL;
2431 else if( p[4] > 0 )
2433 /* discontinuity indicator found in stream */
2434 b_discontinuity = (p[5]&0x80) ? true : false;
2435 if( b_discontinuity )
2437 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2438 pid->i_pid );
2439 p_pkt->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2441 #if 0
2442 if( p[5]&0x40 )
2443 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2444 #endif
2448 /* Test continuity counter */
2449 /* continuous when (one of this):
2450 * diff == 1
2451 * diff == 0 and payload == 0
2452 * diff == 0 and duplicate packet (playload != 0) <- should we
2453 * test the content ?
2455 if( b_payload )
2457 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2458 if( i_diff == 1 )
2460 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2461 pid->i_dup = 0;
2463 else
2465 if( pid->i_cc == 0xff )
2467 msg_Dbg( p_demux, "first packet for pid=%d cc=0x%x",
2468 pid->i_pid, i_cc );
2469 pid->i_cc = i_cc;
2471 else if( i_diff == 0 && pid->i_dup == 0 )
2473 /* Discard duplicated payload 2.4.3.3 */
2474 pid->i_dup++;
2475 block_Release( p_pkt );
2476 return NULL;
2478 else if( i_diff != 0 && !b_discontinuity )
2480 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2481 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2483 pid->i_cc = i_cc;
2484 pid->i_dup = 0;
2485 p_pkt->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2487 else pid->i_cc = i_cc;
2490 else /* Ignore all 00 or 10 as in 2.4.3.3 CC counter must not be
2491 incremented in those cases, but there is humax inserting
2492 empty/10 packets always set with cc = 0 between 2 payload pkts
2493 see stream_main_pcr_1280x720p50_5mbps.ts */
2495 if( b_discontinuity )
2496 pid->i_cc = i_cc;
2499 if( unlikely(!(b_payload || b_adaptation)) ) /* Invalid, ignore */
2501 block_Release( p_pkt );
2502 return NULL;
2505 return p_pkt;
2508 /* Avoids largest memcpy */
2509 static bool block_Split( block_t **pp_block, block_t **pp_remain, size_t i_offset )
2511 block_t *p_block = *pp_block;
2512 block_t *p_split = NULL;
2513 *pp_remain = NULL;
2515 size_t i_tocopy = p_block->i_buffer - i_offset;
2516 if( i_tocopy > i_offset ) /* make new block for head */
2518 if( i_offset > 0 )
2520 p_split = block_Alloc( i_offset );
2521 if( p_split == NULL )
2522 return false;
2523 memcpy( p_split->p_buffer, p_block->p_buffer, i_offset );
2524 p_block->p_buffer += i_offset;
2525 p_block->i_buffer -= i_offset;
2527 *pp_remain = p_block;
2528 *pp_block = p_split;
2530 else /* other gets the tail of our split */
2532 if( i_tocopy > 0 )
2534 p_split = block_Alloc( i_tocopy );
2535 if( p_split == NULL )
2536 return false;
2537 memcpy( p_split->p_buffer, &p_block->p_buffer[i_offset], i_tocopy );
2538 p_block->i_buffer -= i_tocopy;
2540 *pp_remain = p_split;
2542 return true;
2545 static uint8_t *FindNextPESHeader( uint8_t *p_buf, size_t i_buffer )
2547 const uint8_t *p_end = &p_buf[i_buffer];
2548 unsigned i_bitflow = 0;
2549 for( ; p_buf != p_end; p_buf++ )
2551 i_bitflow <<= 1;
2552 if( !*p_buf )
2554 i_bitflow |= 1;
2556 else if( *p_buf == 0x01 && (i_bitflow & 0x06) == 0x06 ) /* >= two zero prefixed 1 */
2558 return p_buf - 2;
2561 return NULL;
2564 static const uint8_t pes_sync[] = { 0, 0, 1 };
2566 static bool MayHaveStartCodeOnEnd( const uint8_t *p_buf, size_t i_buf )
2568 assert(i_buf > 2);
2569 return !( *(--p_buf) > 1 || *(--p_buf) > 0 || *(--p_buf) > 0 );
2572 static bool GatherPESData( demux_t *p_demux, ts_pid_t *pid, block_t *p_pkt, size_t i_skip )
2574 const bool b_unit_start = p_pkt->p_buffer[1]&0x40;
2575 bool b_ret = false;
2576 ts_stream_t *p_pes = pid->u.p_stream;
2578 /* We have to gather it */
2579 p_pkt->p_buffer += i_skip;
2580 p_pkt->i_buffer -= i_skip;
2582 bool b_single_payload = b_unit_start; /* Single payload in case of unit start */
2583 bool b_aligned_ts_payload = true;
2585 if( unlikely(p_pes->b_broken_PUSI_conformance) )
2587 /* Stream does not conform to payload_unit_start flag
2588 * applied to PES packets (AdTech private_stream_1) */
2589 b_aligned_ts_payload = false;
2590 b_single_payload = false;
2594 /* We'll cannot parse any pes data */
2595 if( (p_pkt->i_flags & BLOCK_FLAG_SCRAMBLED) && p_demux->p_sys->b_valid_scrambling )
2597 block_Release( p_pkt );
2598 return PushPESBlock( p_demux, pid, NULL, true );
2601 /* Data discontinuity, we need to drop or output currently
2602 * gathered data as it can't match the target size or can
2603 * have dropped next sync code */
2604 if( p_pkt->i_flags & BLOCK_FLAG_DISCONTINUITY )
2606 p_pes->gather.i_saved = 0;
2607 /* Flush/output current */
2608 b_ret |= PushPESBlock( p_demux, pid, NULL, true );
2609 /* Propagate to output block to notify packetizers/decoders */
2610 if( p_pes->p_es )
2611 p_pes->p_es->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
2614 if ( unlikely(p_pes->gather.i_saved > 0) )
2616 /* Saved from previous packet end */
2617 assert(p_pes->gather.i_saved < 6);
2618 if( !b_aligned_ts_payload )
2620 p_pkt = block_Realloc( p_pkt, p_pes->gather.i_saved, p_pkt->i_buffer );
2621 if( p_pkt )
2622 memcpy( p_pkt->p_buffer, p_pes->gather.saved, p_pes->gather.i_saved );
2624 p_pes->gather.i_saved = 0;
2627 for( bool b_first_sync_done = false; p_pkt; )
2629 assert( p_pes->gather.i_saved == 0 );
2631 if( p_pes->gather.p_data == NULL && !b_first_sync_done && p_pkt->i_buffer >= 6 )
2633 if( likely(b_aligned_ts_payload) )
2635 if( memcmp( p_pkt->p_buffer, pes_sync, 3 ) )
2637 block_Release( p_pkt );
2638 return b_ret;
2641 else
2643 /* Need to find sync code */
2644 uint8_t *p_buf = FindNextPESHeader( p_pkt->p_buffer, p_pkt->i_buffer - 3 );
2645 if( p_buf == NULL )
2647 /* no first sync code */
2648 if( MayHaveStartCodeOnEnd( p_pkt->p_buffer, p_pkt->i_buffer ) )
2650 /* Drop everything except last bytes for next packet */
2651 p_pkt->p_buffer += p_pkt->i_buffer - 3;
2652 p_pes->gather.i_saved = p_pkt->i_buffer = 3;
2653 memcpy(p_pes->gather.saved, p_pkt->p_buffer, p_pkt->i_buffer);
2655 block_Release( p_pkt );
2656 return b_ret;
2658 p_pkt->i_buffer -= p_buf - p_pkt->p_buffer;
2659 p_pkt->p_buffer = p_buf;
2661 /* now points to PES header */
2662 p_pes->gather.i_data_size = GetWBE(&p_pkt->p_buffer[4]);
2663 if( p_pes->gather.i_data_size > 0 )
2664 p_pes->gather.i_data_size += 6;
2665 b_first_sync_done = true; /* Because if size is 0, we woud not look for second sync */
2667 else
2669 assert( p_pes->gather.i_data_size > p_pes->gather.i_gathered ||
2670 p_pes->gather.i_data_size == 0 );
2672 /* If we started reading a fixed size */
2673 if( p_pes->gather.i_data_size > p_pes->gather.i_gathered )
2675 const size_t i_remain = p_pes->gather.i_data_size - p_pes->gather.i_gathered;
2676 /* Append whole block */
2677 if( likely(p_pkt->i_buffer <= i_remain || b_single_payload) )
2679 b_ret |= PushPESBlock( p_demux, pid, p_pkt, p_pes->gather.p_data == NULL );
2680 p_pkt = NULL;
2682 else /* p_pkt->i_buffer > i_remain */
2684 block_t *p_split;
2685 if( !block_Split( &p_pkt, &p_split, i_remain ) )
2687 block_Release( p_pkt );
2688 return false;
2690 b_ret |= PushPESBlock( p_demux, pid, p_pkt, p_pes->gather.p_data == NULL );
2691 p_pkt = p_split;
2692 b_first_sync_done = false;
2695 else /* if( p_pes->gather.i_data_size == 0 ) // see next packet */
2697 /* Append or finish current/start new PES depending on unit_start */
2698 b_ret |= PushPESBlock( p_demux, pid, p_pkt, b_unit_start );
2699 p_pkt = NULL;
2703 if( unlikely(p_pkt && p_pkt->i_buffer < 6) )
2705 /* save and prepend to next packet */
2706 assert(!b_single_payload);
2707 assert(p_pes->gather.i_saved == 0);
2708 p_pes->gather.i_saved = p_pkt->i_buffer;
2709 memcpy(p_pes->gather.saved, p_pkt->p_buffer, p_pkt->i_buffer);
2710 block_Release( p_pkt );
2711 p_pkt = NULL;
2715 return b_ret;
2718 static bool GatherSectionsData( demux_t *p_demux, ts_pid_t *p_pid, block_t *p_pkt, size_t i_skip )
2720 VLC_UNUSED(i_skip); VLC_UNUSED(p_demux);
2721 bool b_ret = false;
2723 if( p_pkt->i_flags & BLOCK_FLAG_DISCONTINUITY )
2725 ts_sections_processor_Reset( p_pid->u.p_stream->p_sections_proc );
2728 if( (p_pkt->i_flags & (BLOCK_FLAG_SCRAMBLED | BLOCK_FLAG_CORRUPTED)) == 0 )
2730 ts_sections_processor_Push( p_pid->u.p_stream->p_sections_proc, p_pkt->p_buffer );
2731 b_ret = true;
2734 block_Release( p_pkt );
2736 return b_ret;
2739 void TsChangeStandard( demux_sys_t *p_sys, ts_standards_e v )
2741 if( p_sys->standard != TS_STANDARD_AUTO &&
2742 p_sys->standard != v )
2743 return; /* TODO */
2744 p_sys->standard = v;
2747 bool ProgramIsSelected( demux_sys_t *p_sys, uint16_t i_pgrm )
2749 for(int i=0; i<p_sys->programs.i_size; i++)
2750 if( p_sys->programs.p_elems[i] == i_pgrm )
2751 return true;
2753 return false;
2756 static bool PIDReferencedByProgram( const ts_pmt_t *p_pmt, uint16_t i_pid )
2758 for(int i=0; i<p_pmt->e_streams.i_size; i++)
2759 if( p_pmt->e_streams.p_elems[i]->i_pid == i_pid )
2760 return true;
2762 return false;
2765 static void DoCreateES( demux_t *p_demux, ts_es_t *p_es, const ts_es_t *p_parent_es )
2767 demux_sys_t *p_sys = p_demux->p_sys;
2769 for( ; p_es ; p_es = p_es->p_next )
2771 if( !p_es->id )
2773 if( !p_es->fmt.i_group )
2774 p_es->fmt.i_group = p_es->p_program->i_number;
2775 p_es->id = es_out_Add( p_demux->out, &p_es->fmt );
2776 if( p_parent_es ) /* Set Extra ES group and original ID */
2778 if ( p_sys->b_es_id_pid ) /* pid is 13 bits */
2779 p_es->fmt.i_id = (p_sys->i_next_extraid++ << 13) | p_parent_es->fmt.i_id;
2780 p_es->fmt.i_group = p_parent_es->fmt.i_group;
2782 p_sys->i_pmt_es++;
2784 DoCreateES( p_demux, p_es->p_extraes, p_es );
2788 void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool b_create_delayed )
2790 demux_sys_t *p_sys = p_demux->p_sys;
2792 if( b_create_delayed )
2793 p_sys->es_creation = CREATE_ES;
2795 if( pid && p_sys->es_creation == CREATE_ES )
2797 DoCreateES( p_demux, pid->u.p_stream->p_es, NULL );
2799 /* Update the default program == first created ES group */
2800 if( p_sys->b_default_selection && p_sys->programs.i_size > 0)
2802 p_sys->b_default_selection = false;
2803 const int i_first_program = pid->u.p_stream->p_es->p_program->i_number;
2804 if( p_sys->programs.p_elems[0] != i_first_program )
2805 p_sys->programs.p_elems[0] = i_first_program;
2806 msg_Dbg( p_demux, "Default program is %d", i_first_program );
2810 if( b_create_delayed )
2812 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
2813 for( int i=0; i< p_pat->programs.i_size; i++ )
2815 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2816 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2817 DoCreateES( p_demux, p_pmt->e_streams.p_elems[j]->u.p_stream->p_es, NULL );