/
ts.c
2798 lines (2403 loc) · 95.4 KB
/
ts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* ts.c: Transport Stream input module for VLC.
*****************************************************************************
* Copyright (C) 2004-2016 VLC authors and VideoLAN
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Jean-Paul Saman <jpsaman #_at_# m2x.nl>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h> /* DVB-specific things */
#include <vlc_demux.h>
#include <vlc_input.h>
#include "ts_pid.h"
#include "ts_streams.h"
#include "ts_streams_private.h"
#include "ts_pes.h"
#include "ts_psi.h"
#include "ts_si.h"
#include "ts_psip.h"
#include "ts_hotfixes.h"
#include "ts_sl.h"
#include "ts_metadata.h"
#include "sections.h"
#include "pes.h"
#include "timestamps.h"
#include "ts.h"
#include "../../codec/scte18.h"
#include "../opus.h"
#include "../../mux/mpeg/csa.h"
#ifdef HAVE_ARIBB24
#include <aribb24/aribb24.h>
#endif
#include <assert.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
/* TODO
* - Rename "extra pmt" to "user pmt"
* - Update extra pmt description
* pmt_pid[:pmt_number][=pid_description[,pid_description]]
* where pid_description could take 3 forms:
* 1. pid:pcr (to force the pcr pid)
* 2. pid:stream_type
* 3. pid:type=fourcc where type=(video|audio|spu)
* Ex: Program 1 on pid 8190 with hevc on pid 289, aac on pid 305
* and pcr on 305
* "8190:1=289:video=hevc,305:pcr,305:audio=mp4a"
*/
#define PMT_TEXT N_("Extra PMT")
#define PMT_LONGTEXT N_( \
"Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
#define PID_TEXT N_("Set id of ES to PID")
#define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
" handled by VLC to the same value as the PID in" \
" the TS stream, instead of 1, 2, 3, etc. Useful to" \
" do \'#duplicate{..., select=\"es=<pid>\"}\'.")
#define CSA_TEXT N_("CSA Key")
#define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
"16 char string (8 hexadecimal bytes).")
#define CSA2_TEXT N_("Second CSA Key")
#define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
"16 char string (8 hexadecimal bytes).")
#define CPKT_TEXT N_("Packet size in bytes to decrypt")
#define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
"The decryption routines subtract the TS-header from the value before " \
"decrypting." )
#define SPLIT_ES_TEXT N_("Separate sub-streams")
#define SPLIT_ES_LONGTEXT N_( \
"Separate teletex/dvbs pages into independent ES. " \
"It can be useful to turn off this option when using stream output." )
#define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
#define SEEK_PERCENT_LONGTEXT N_( \
"Seek and position based on a percent byte position, not a PCR generated " \
"time position. If seeking doesn't work property, turn on this option." )
#define CC_CHECK_TEXT "Check packets continuity counter"
#define CC_CHECK_LONGTEXT "Detect discontinuities and drop packet duplicates. " \
"(bluRay sources are known broken and have false positives). "
#define TS_PATFIX_TEXT "Try to generate PAT/PMT if missing"
#define TS_SKIP_GHOST_PROGRAM_TEXT "Only create ES on program sending data"
#define TS_OFFSETFIX_TEXT "Try to fix too early PCR (or late DTS)"
#define TS_GENERATED_PCR_OFFSET_TEXT "Offset in ms for generated PCR"
#define PCR_TEXT N_("Trust in-stream PCR")
#define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
static const char *const ts_standards_list[] =
{ "auto", "mpeg", "dvb", "arib", "atsc", "tdmb" };
static const char *const ts_standards_list_text[] =
{ N_("Auto"), "MPEG", "DVB", "ARIB", "ATSC", "T-DMB" };
#define STANDARD_TEXT N_("Digital TV Standard")
#define STANDARD_LONGTEXT N_( "Selects mode for digital TV standard. " \
"This feature affects EPG information and subtitles." )
vlc_module_begin ()
set_description( N_("MPEG Transport Stream demuxer") )
set_shortname ( "MPEG-TS" )
set_subcategory( SUBCAT_INPUT_DEMUX )
add_string( "ts-standard", "auto", STANDARD_TEXT, STANDARD_LONGTEXT )
change_string_list( ts_standards_list, ts_standards_list_text )
add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT )
add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT )
change_safe()
add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT )
change_safe()
add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT )
change_safe()
add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT )
change_safe()
add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT )
change_safe()
add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT )
add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT )
add_bool( "ts-cc-check", true, CC_CHECK_TEXT, CC_CHECK_LONGTEXT )
add_bool( "ts-pmtfix-waitdata", true, TS_SKIP_GHOST_PROGRAM_TEXT, NULL )
add_bool( "ts-patfix", true, TS_PATFIX_TEXT, NULL )
add_bool( "ts-pcr-offsetfix", true, TS_OFFSETFIX_TEXT, NULL )
add_integer_with_range( "ts-generated-pcr-offset", 120, 0, 500,
TS_GENERATED_PCR_OFFSET_TEXT, NULL )
set_capability( "demux", 10 )
set_callbacks( Open, Close )
add_shortcut( "ts" )
vlc_module_end ()
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Demux ( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args );
static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
/* Helpers */
static bool PIDReferencedByProgram( const ts_pmt_t *, uint16_t );
void UpdatePESFilters( demux_t *p_demux, bool b_all );
static inline void FlushESBuffer( ts_stream_t *p_pes );
static void UpdatePIDScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool );
static inline int PIDGet( block_t *p )
{
return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
}
static stime_t GetPCR( const block_t * );
static block_t * ProcessTSPacket( demux_t *p_demux, ts_pid_t *pid, block_t *p_pkt, int * );
static bool GatherSectionsData( demux_t *p_demux, ts_pid_t *, block_t *, size_t );
static bool GatherPESData( demux_t *p_demux, ts_pid_t *, block_t *, size_t );
static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, stime_t i_pcr );
static block_t* ReadTSPacket( demux_t *p_demux );
static int SeekToTime( demux_t *p_demux, const ts_pmt_t *, stime_t time );
static void ReadyQueuesPostSeek( demux_t *p_demux );
static void PCRHandle( demux_t *p_demux, ts_pid_t *, stime_t );
static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
#define TS_PACKET_SIZE_188 188
#define TS_PACKET_SIZE_192 192
#define TS_PACKET_SIZE_204 204
#define TS_PACKET_SIZE_MAX 204
#define TS_HEADER_SIZE 4
#define PROBE_CHUNK_COUNT 500
#define PROBE_MAX (PROBE_CHUNK_COUNT * 10)
static int DetectPacketSize( demux_t *p_demux, unsigned *pi_header_size, int i_offset )
{
const uint8_t *p_peek;
if( vlc_stream_Peek( p_demux->s,
&p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
return -1;
for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
{
if( p_peek[i_offset + i_sync] != 0x47 )
continue;
/* Check next 3 sync bytes */
int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
if( ( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
return -1;
if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
{
return TS_PACKET_SIZE_188;
}
else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
{
if( i_sync == 4 )
{
*pi_header_size = 4; /* BluRay TS packets have 4-byte header */
}
return TS_PACKET_SIZE_192;
}
else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
{
return TS_PACKET_SIZE_204;
}
}
if( p_demux->obj.force )
{
msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
return TS_PACKET_SIZE_188;
}
msg_Dbg( p_demux, "TS module discarded (lost sync)" );
return -1;
}
#define TOPFIELD_HEADER_SIZE 3712
static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, unsigned *pi_header_size, vdr_info_t *p_vdr )
{
const uint8_t *p_peek;
*pi_header_size = 0;
int i_packet_size = -1;
int i_offset = 0;
if( vlc_stream_Peek( p_demux->s,
&p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
return -1;
if( memcmp( p_peek, "TFrc", 4 ) == 0 && p_peek[6] == 0 &&
vlc_stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
== TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
{
const int i_service = GetWBE(&p_peek[18]);
i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
if( i_packet_size != -1 )
{
msg_Dbg( p_demux, "this is a topfield file" );
#if 0
/* I used the TF5000PVR 2004 Firmware .doc header documentation,
* /doc/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc on streams.vo
* but after the filename the offsets seem to be incorrect. - DJ */
int i_duration, i_name;
char *psz_name = xmalloc(25);
char *psz_event_name;
char *psz_event_text = xmalloc(130);
char *psz_ext_text = xmalloc(1025);
// 2 bytes version Uimsbf (4,5)
// 2 bytes reserved (6,7)
// 2 bytes duration in minutes Uimsbf (8,9(
i_duration = (int) (p_peek[8] << 8) | p_peek[9];
msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
// 2 bytes service number in channel list (10, 11)
// 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
// 4 bytes of reserved + tuner info (14,15,16,17)
// 2 bytes of Service ID Bslbf (18,19)
// 2 bytes of PMT PID Uimsbf (20,21)
// 2 bytes of PCR PID Uimsbf (22,23)
// 2 bytes of Video PID Uimsbf (24,25)
// 2 bytes of Audio PID Uimsbf (26,27)
// 24 bytes filename Bslbf
memcpy( psz_name, &p_peek[28], 24 );
psz_name[24] = '\0';
msg_Dbg( p_demux, "recordingname=%s", psz_name );
// 1 byte of sat index Uimsbf (52)
// 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
// 4 bytes of freq. Uimsbf (56,57,58,59)
// 2 bytes of symbol rate Uimsbf (60,61)
// 2 bytes of TS stream ID Uimsbf (62,63)
// 4 bytes reserved
// 2 bytes reserved
// 2 bytes duration Uimsbf (70,71)
//i_duration = (int) (p_peek[70] << 8) | p_peek[71];
//msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
// 4 bytes EventID Uimsbf (72-75)
// 8 bytes of Start and End time info (76-83)
// 1 byte reserved (84)
// 1 byte event name length Uimsbf (89)
i_name = (int)(p_peek[89]&~0x81);
msg_Dbg( p_demux, "event name length = %d", i_name);
psz_event_name = xmalloc( i_name+1 );
// 1 byte parental rating (90)
// 129 bytes of event text
memcpy( psz_event_name, &p_peek[91], i_name );
psz_event_name[i_name] = '\0';
memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
psz_event_text[129-i_name] = '\0';
msg_Dbg( p_demux, "event name=%s", psz_event_name );
msg_Dbg( p_demux, "event text=%s", psz_event_text );
// 12 bytes reserved (220)
// 6 bytes reserved
// 2 bytes Event Text Length Uimsbf
// 4 bytes EventID Uimsbf
// FIXME We just have 613 bytes. not enough for this entire text
// 1024 bytes Extended Event Text Bslbf
memcpy( psz_ext_text, p_peek+372, 1024 );
psz_ext_text[1024] = '\0';
msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
// 52 bytes reserved Bslbf
#endif
p_vdr->i_service = i_service;
return i_packet_size;
//return TS_PACKET_SIZE_188;
}
}
else if( !memcmp( p_peek, "\x47\xff\xffPRIV", 7 ) ) /* Denver */
{
/* Bogus TS packets with private payload interleaved in stream */
i_offset = TS_PACKET_SIZE_188 * 11;
}
return DetectPacketSize( p_demux, pi_header_size, i_offset );
}
/*****************************************************************************
* Open
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys;
int i_packet_size;
unsigned i_packet_header_size = 0;
ts_pid_t *patpid;
vdr_info_t vdr = {0};
/* Search first sync byte */
i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
if( i_packet_size < 0 )
return VLC_EGENERIC;
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
if( !p_sys )
return VLC_ENOMEM;
memset( p_sys, 0, sizeof( demux_sys_t ) );
vlc_mutex_init( &p_sys->csa_lock );
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
/* Init p_sys field */
p_sys->b_end_preparse = false;
ARRAY_INIT( p_sys->programs );
p_sys->b_default_selection = false;
p_sys->i_network_time = 0;
p_sys->i_network_time_update = 0;
p_sys->vdr = vdr;
p_sys->stream = p_demux->s;
p_sys->b_broken_charset = false;
ts_pid_list_Init( &p_sys->pids );
p_sys->i_packet_size = i_packet_size;
p_sys->i_packet_header_size = i_packet_header_size;
p_sys->i_ts_read = 50;
p_sys->csa = NULL;
p_sys->b_start_record = false;
p_sys->record_dir_path = NULL;
vlc_dictionary_init( &p_sys->attachments, 0 );
p_sys->patfix.i_first_dts = -1;
p_sys->patfix.i_timesourcepid = 0;
p_sys->patfix.b_pcrhasnopcrfield = false;
p_sys->patfix.status = var_CreateGetBool( p_demux, "ts-patfix" ) ? PAT_WAITING : PAT_FIXTRIED;
/* Init PAT handler */
patpid = GetPID(p_sys, 0);
if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
{
free( p_sys );
return VLC_ENOMEM;
}
if( !ts_psi_PAT_Attach( patpid, p_demux ) )
{
PIDRelease( p_demux, patpid );
free( p_sys );
return VLC_EGENERIC;
}
p_sys->b_access_control = ( VLC_SUCCESS == SetPIDFilter( p_sys, patpid, true ) );
p_sys->i_pmt_es = 0;
p_sys->seltype = PROGRAM_AUTO_DEFAULT;
/* Read config */
p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
p_sys->i_next_extraid = 1;
p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
p_sys->b_check_pcr_offset = p_sys->b_trust_pcr && var_CreateGetBool(p_demux, "ts-pcr-offsetfix" );
p_sys->i_generated_pcr_dpb_offset = VLC_TICK_FROM_MS(var_CreateGetInteger( p_demux, "ts-generated-pcr-offset" ));
/* We handle description of an extra PMT */
char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
p_sys->b_user_pmt = false;
if( psz_string && *psz_string )
UserPmt( p_demux, psz_string );
free( psz_string );
psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
if( psz_string && *psz_string )
{
int i_res;
char* psz_csa2 = NULL;
p_sys->csa = csa_New();
if( p_sys->csa )
{
psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
i_res = csa_SetCW( VLC_OBJECT(p_demux), p_sys->csa, psz_string, true );
}
else
i_res = VLC_ENOMEM;
if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
{
if( csa_SetCW( VLC_OBJECT(p_demux), p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
{
csa_SetCW( VLC_OBJECT(p_demux), p_sys->csa, psz_string, false );
}
}
else if ( i_res == VLC_SUCCESS )
{
csa_SetCW( VLC_OBJECT(p_demux), p_sys->csa, psz_string, false );
}
else
{
csa_Delete( p_sys->csa );
p_sys->csa = NULL;
}
if( p_sys->csa )
{
var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
if( i_pkt < 4 || i_pkt > 188 )
{
msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
msg_Warn( p_demux, "using default packet size of 188 bytes" );
p_sys->i_csa_pkt_size = 188;
}
else
p_sys->i_csa_pkt_size = i_pkt;
msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
}
free( psz_csa2 );
}
free( psz_string );
p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
p_sys->b_canseek = false;
p_sys->b_canfastseek = false;
p_sys->b_lowdelay = var_InheritBool( p_demux, "low-delay" );
p_sys->b_ignore_time_for_positions = var_InheritBool( p_demux, "ts-seek-percent" );
p_sys->b_cc_check = var_InheritBool( p_demux, "ts-cc-check" );
p_sys->standard = TS_STANDARD_AUTO;
char *psz_standard = var_InheritString( p_demux, "ts-standard" );
if( psz_standard )
{
for( unsigned i=0; i<ARRAY_SIZE(ts_standards_list); i++ )
{
if( !strcmp( psz_standard, ts_standards_list[i] ) )
{
TsChangeStandard( p_sys, TS_STANDARD_AUTO + i );
msg_Dbg( p_demux, "Standard set to %s", ts_standards_list_text[i] );
break;
}
}
free( psz_standard );
}
if( p_sys->standard == TS_STANDARD_AUTO &&
!strncasecmp( p_demux->psz_url, "atsc", 4 ) )
{
TsChangeStandard( p_sys, TS_STANDARD_ATSC );
}
vlc_stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
vlc_stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK,
&p_sys->b_canfastseek );
if( !p_sys->b_access_control && var_CreateGetBool( p_demux, "ts-pmtfix-waitdata" ) )
p_sys->es_creation = DELAY_ES;
else
p_sys->es_creation = CREATE_ES;
/* Preparse time */
if( p_demux->b_preparsing && p_sys->b_canseek )
{
while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
break;
}
return VLC_SUCCESS;
}
/*****************************************************************************
* Close
*****************************************************************************/
static void FreeDictAttachment( void *p_value, void *p_obj )
{
VLC_UNUSED(p_obj);
vlc_input_attachment_Release( (input_attachment_t *) p_value );
}
static void Close( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
PIDRelease( p_demux, GetPID(p_sys, 0) );
vlc_mutex_lock( &p_sys->csa_lock );
if( p_sys->csa )
{
var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
csa_Delete( p_sys->csa );
}
vlc_mutex_unlock( &p_sys->csa_lock );
ARRAY_RESET( p_sys->programs );
#ifdef HAVE_ARIBB24
if ( p_sys->arib.p_instance )
arib_instance_destroy( p_sys->arib.p_instance );
#endif
if ( p_sys->stream != p_demux->s ) /* B25 wrapper in use */
{
vlc_stream_Delete( p_sys->stream );
p_sys->stream = p_demux->s;
}
/* Release all non default pids */
ts_pid_list_Release( p_demux, &p_sys->pids );
/* Clear up attachments */
vlc_dictionary_clear( &p_sys->attachments, FreeDictAttachment, NULL );
free( p_sys->record_dir_path );
free( p_sys );
}
/*****************************************************************************
* ChangeKeyCallback: called when changing the odd encryption key on the fly.
*****************************************************************************/
static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
vlc_value_t oldval, vlc_value_t newval,
void *p_data )
{
VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
int i_tmp = (intptr_t)p_data;
vlc_mutex_lock( &p_sys->csa_lock );
if ( i_tmp )
i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
else
i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
vlc_mutex_unlock( &p_sys->csa_lock );
return i_tmp;
}
/*****************************************************************************
* Demux:
*****************************************************************************/
static int Demux( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
bool b_wait_es = p_sys->i_pmt_es <= 0;
/* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
if( p_sys->i_pmt_es == 0 && !SEEN(GetPID(p_sys, 0)) && p_sys->patfix.status == PAT_MISSING )
{
msg_Warn( p_demux, "Generating PAT as we still have not received one" );
MissingPATPMTFixup( p_demux );
GetPID(p_sys, 0)->u.p_pat->b_generated = true;
p_sys->patfix.status = PAT_FIXTRIED;
}
/* We read at most 100 TS packet or until a frame is completed */
for( unsigned i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
{
bool b_frame = false;
int i_header = 0;
block_t *p_pkt;
if( !(p_pkt = ReadTSPacket( p_demux )) )
{
return VLC_DEMUXER_EOF;
}
if( p_sys->b_start_record )
{
/* Enable recording once synchronized */
vlc_stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true,
p_sys->record_dir_path, "ts" );
p_sys->b_start_record = false;
}
/* Early reject truncated packets from hw devices */
if( unlikely(p_pkt->i_buffer < TS_PACKET_SIZE_188) )
{
block_Release( p_pkt );
continue;
}
/* Reject any fully uncorrected packet. Even PID can be incorrect */
if( p_pkt->p_buffer[1]&0x80 )
{
msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
PIDGet( p_pkt ) );
block_Release( p_pkt );
continue;
}
/* Parse the TS packet */
ts_pid_t *p_pid = GetPID( p_sys, PIDGet( p_pkt ) );
if( !SEEN(p_pid) )
{
if( p_pid->type == TYPE_FREE )
msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
p_pid->i_flags |= FLAG_SEEN;
if( p_pid->i_pid == 0x01 )
p_sys->b_valid_scrambling = true;
}
/* Drop duplicates and invalid (DOES NOT drop corrupted) */
p_pkt = ProcessTSPacket( p_demux, p_pid, p_pkt, &i_header );
if( !p_pkt )
continue;
if( !SCRAMBLED(*p_pid) != !(p_pkt->i_flags & BLOCK_FLAG_SCRAMBLED) )
{
UpdatePIDScrambledState( p_demux, p_pid, p_pkt->i_flags & BLOCK_FLAG_SCRAMBLED );
}
/* Adaptation field cannot be scrambled */
stime_t i_pcr = GetPCR( p_pkt );
if( i_pcr >= 0 )
PCRHandle( p_demux, p_pid, i_pcr );
/* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
if( !SEEN( GetPID( p_sys, 0 ) ) &&
(p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
(p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
{
ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
}
switch( p_pid->type )
{
case TYPE_PAT:
case TYPE_PMT:
/* PAT and PMT are not allowed to be scrambled */
ts_psi_Packet_Push( p_pid, p_pkt->p_buffer );
block_Release( p_pkt );
break;
case TYPE_STREAM:
p_sys->b_end_preparse = true;
if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
{
msg_Dbg( p_demux, "Creating delayed ES" );
AddAndCreateES( p_demux, p_pid, true );
UpdatePESFilters( p_demux, p_sys->seltype == PROGRAM_ALL );
}
/* Emulate HW filter */
if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
{
/* That packet is for an unselected ES, don't waste time/memory gathering its data */
block_Release( p_pkt );
continue;
}
if( p_pid->u.p_stream->transport == TS_TRANSPORT_PES )
{
b_frame = GatherPESData( p_demux, p_pid, p_pkt, i_header );
}
else if( p_pid->u.p_stream->transport == TS_TRANSPORT_SECTIONS )
{
b_frame = GatherSectionsData( p_demux, p_pid, p_pkt, i_header );
}
else // pid->u.p_pes->transport == TS_TRANSPORT_IGNORE
{
block_Release( p_pkt );
}
break;
case TYPE_SI:
if( (p_pkt->i_flags & (BLOCK_FLAG_SCRAMBLED|BLOCK_FLAG_CORRUPTED)) == 0 )
ts_si_Packet_Push( p_pid, p_pkt->p_buffer );
block_Release( p_pkt );
break;
case TYPE_PSIP:
if( (p_pkt->i_flags & (BLOCK_FLAG_SCRAMBLED|BLOCK_FLAG_CORRUPTED)) == 0 )
ts_psip_Packet_Push( p_pid, p_pkt->p_buffer );
block_Release( p_pkt );
break;
case TYPE_CAT:
default:
/* We have to handle PCR if present */
block_Release( p_pkt );
break;
}
if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
break;
}
demux_UpdateTitleFromStream( p_demux );
return VLC_DEMUXER_SUCCESS;
}
/*****************************************************************************
* Control:
*****************************************************************************/
static int EITCurrentEventTime( const ts_pmt_t *p_pmt, demux_sys_t *p_sys,
time_t *pi_time, time_t *pi_length )
{
if( p_sys->i_network_time == 0 || !p_pmt || p_pmt->eit.i_event_length == 0 )
return VLC_EGENERIC;
if( p_pmt->eit.i_event_start <= p_sys->i_network_time &&
p_sys->i_network_time < p_pmt->eit.i_event_start + p_pmt->eit.i_event_length )
{
if( pi_length )
*pi_length = p_pmt->eit.i_event_length;
if( pi_time )
{
*pi_time = p_sys->i_network_time - p_pmt->eit.i_event_start;
*pi_time += time(NULL) - p_sys->i_network_time_update;
}
return VLC_SUCCESS;
}
return VLC_EGENERIC;
}
static inline void HasSelectedES( es_out_t *out, const ts_es_t *p_es,
const ts_pmt_t *p_pmt, bool *pb_stream_selected )
{
for( ; p_es && !*pb_stream_selected; p_es = p_es->p_next )
{
if( p_es->id )
es_out_Control( out, ES_OUT_GET_ES_STATE,
p_es->id, pb_stream_selected );
HasSelectedES( out, p_es->p_extraes, p_pmt, pb_stream_selected );
}
}
void UpdatePESFilters( demux_t *p_demux, bool b_all )
{
demux_sys_t *p_sys = p_demux->p_sys;
ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
/* We need 3 pass to avoid loss on deselect/relesect with hw filters and
because pid could be shared and its state altered by another unselected pmt
First clear flag on every referenced pid
Then add flag if non on each selected pmt/pcr and active es
Then commit it at hardware level if any */
/* clear selection flag on every pmt referenced pid */
for( int i=0; i< p_pat->programs.i_size; i++ )
{
ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
p_pmt_pid->i_flags &= ~FLAG_FILTERED;
for( int j=0; j< p_pmt->e_streams.i_size; j++ )
p_pmt->e_streams.p_elems[j]->i_flags &= ~FLAG_FILTERED;
GetPID(p_sys, p_pmt->i_pid_pcr)->i_flags &= ~FLAG_FILTERED;
}
/* set selection flag on selected pmt referenced pid with active es */
for( int i=0; i< p_pat->programs.i_size; i++ )
{
ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
p_pmt->b_selected = true;
else
p_pmt->b_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
if( p_pmt->b_selected )
{
p_pmt_pid->i_flags |= FLAG_FILTERED;
for( int j=0; j<p_pmt->e_streams.i_size; j++ )
{
ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
ts_stream_t *p_pes = espid->u.p_stream;
bool b_stream_selected = true;
if( !p_pes->b_always_receive && !b_all )
HasSelectedES( p_demux->out, p_pes->p_es, p_pmt, &b_stream_selected );
if( b_stream_selected )
{
msg_Dbg( p_demux, "enabling pid %d from program %d", espid->i_pid, p_pmt->i_number );
espid->i_flags |= FLAG_FILTERED;
}
}
/* Select pcr last in case it is handled by unselected ES */
if( p_pmt->i_pid_pcr > 0 )
{
GetPID(p_sys, p_pmt->i_pid_pcr)->i_flags |= FLAG_FILTERED;
msg_Dbg( p_demux, "enabling pcr pid %d from program %d", p_pmt->i_pid_pcr, p_pmt->i_number );
}
}
}
/* Commit HW changes based on flags */
for( int i=0; i< p_pat->programs.i_size; i++ )
{
ts_pid_t *p_pmt_pid = p_pat->programs.p_elems[i];
ts_pmt_t *p_pmt = p_pmt_pid->u.p_pmt;
UpdateHWFilter( p_sys, p_pmt_pid );
for( int j=0; j< p_pmt->e_streams.i_size; j++ )
{
ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
UpdateHWFilter( p_sys, espid );
if( (espid->i_flags & FLAG_FILTERED) == 0 )
FlushESBuffer( espid->u.p_stream );
}
UpdateHWFilter( p_sys, GetPID(p_sys, p_pmt->i_pid_pcr) );
}
}
static int Control( demux_t *p_demux, int i_query, va_list args )
{
demux_sys_t *p_sys = p_demux->p_sys;
double f, *pf;
bool b_bool, *pb_bool;
int64_t i64;
int i_int;
const ts_pmt_t *p_pmt = NULL;
const ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
for( int i=0; i<p_pat->programs.i_size && !p_pmt; i++ )
{
if( p_pat->programs.p_elems[i]->u.p_pmt->b_selected )
p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
}
switch( i_query )
{
case DEMUX_CAN_SEEK:
*va_arg( args, bool * ) = p_sys->b_canseek;
return VLC_SUCCESS;
case DEMUX_GET_TITLE:
*va_arg( args, int * ) = p_sys->current_title;
return VLC_SUCCESS;
case DEMUX_GET_SEEKPOINT:
*va_arg( args, int * ) = p_sys->current_seekpoint;
return VLC_SUCCESS;
case DEMUX_GET_POSITION:
pf = va_arg( args, double * );
/* Access control test is because EPG for recordings is not relevant */
if( p_sys->b_access_control )
{
time_t i_time, i_length;
if( !EITCurrentEventTime( p_pmt, p_sys, &i_time, &i_length ) && i_length > 0 )
{
*pf = (double)i_time/(double)i_length;
return VLC_SUCCESS;
}
}
if( !p_sys->b_ignore_time_for_positions &&
p_pmt &&
p_pmt->pcr.i_first > -1 && SETANDVALID(p_pmt->i_last_dts) &&
p_pmt->pcr.i_current > -1 )
{
double i_length = TimeStampWrapAround( p_pmt->pcr.i_first,
p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
i_length += p_pmt->pcr.i_pcroffset;
double i_pos = TimeStampWrapAround( p_pmt->pcr.i_first,
p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
if( i_length > 0 )
{
*pf = i_pos / i_length;
return VLC_SUCCESS;
}
}
if( (i64 = stream_Size( p_sys->stream) ) > 0 )
{
uint64_t offset = vlc_stream_Tell( p_sys->stream );
*pf = (double)offset / (double)i64;
return VLC_SUCCESS;
}
break;
case DEMUX_SET_POSITION:
f = va_arg( args, double );
b_bool = (bool) va_arg( args, int ); /* precise */
if(!p_sys->b_canseek)
break;
if( p_sys->b_access_control &&
!p_sys->b_ignore_time_for_positions && b_bool && p_pmt )
{
time_t i_time, i_length;
if( !EITCurrentEventTime( p_pmt, p_sys, &i_time, &i_length ) &&
i_length > 0 && !SeekToTime( p_demux, p_pmt, (int64_t)(TO_SCALE( vlc_tick_from_sec( i_length * f ))) ) )
{
ReadyQueuesPostSeek( p_demux );
es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
(int64_t)(TO_SCALE( vlc_tick_from_sec( i_length * f ))) );
return VLC_SUCCESS;
}
}
if( !p_sys->b_ignore_time_for_positions && b_bool && p_pmt &&
p_pmt->pcr.i_first > -1 && SETANDVALID(p_pmt->i_last_dts) &&
p_pmt->pcr.i_current > -1 )
{
stime_t i_length = TimeStampWrapAround( p_pmt->pcr.i_first,
p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
i64 = p_pmt->pcr.i_first + (int64_t)(i_length * f);
if( i64 <= p_pmt->i_last_dts )
{
if( !SeekToTime( p_demux, p_pmt, i64 ) )
{