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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
|
/*
* (C) 2003-2006 Gabest
* (C) 2006-2013 see Authors.txt
*
* This file is part of MPC-HC.
*
* MPC-HC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* MPC-HC 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include <MMReg.h>
#include "MpegSplitterFile.h"
#ifdef STANDALONE_FILTER
#include <InitGuid.h>
#endif
#include "moreuuids.h"
#define MEGABYTE 1024*1024
#define PTS_MAX_BEFORE_WRAP (((1i64 << 33) - 1) * 10000 / 90)
CMpegSplitterFile::CMpegSplitterFile(IAsyncReader* pAsyncReader, HRESULT& hr, bool bIsHdmv, CHdmvClipInfo& ClipInfo, int guid_flag, bool ForcedSub, int AC3CoreOnly, bool AlternativeDuration)
: CBaseSplitterFileEx(pAsyncReader, hr, DEFAULT_CACHE_LENGTH, false, true)
, m_type(mpeg_us)
, m_rate(0)
, m_rtMin(0), m_rtMax(0)
, m_posMin(0), m_posMax(0)
, m_rtPrec(_I64_MIN)
, m_bPTSWrap(false)
, m_bIsHdmv(bIsHdmv)
, m_ClipInfo(ClipInfo)
, m_nVC1_GuidFlag(guid_flag)
, m_ForcedSub(ForcedSub)
, m_AC3CoreOnly(AC3CoreOnly)
, m_AlternativeDuration(AlternativeDuration)
, m_init(false)
{
if (SUCCEEDED(hr)) {
hr = Init(pAsyncReader);
}
}
HRESULT CMpegSplitterFile::Init(IAsyncReader* pAsyncReader)
{
HRESULT hr;
// get the type first
m_type = mpeg_us;
Seek(0);
if (m_type == mpeg_us) {
if (BitRead(32, true) == 'TFrc') {
Seek(0x67c);
}
int cnt = 0, limit = 4;
for (tshdr h; cnt < limit && Read(h); cnt++) {
Seek(h.next);
}
if (cnt >= limit) {
m_type = mpeg_ts;
}
}
Seek(0);
if (m_type == mpeg_us) {
if (BitRead(32, true) == 'TFrc') {
Seek(0xE80);
}
int cnt = 0, limit = 4;
for (tshdr h; cnt < limit && Read(h); cnt++) {
Seek(h.next);
}
if (cnt >= limit) {
m_type = mpeg_ts;
}
}
Seek(0);
if (m_type == mpeg_us) {
int cnt = 0, limit = 4;
for (pvahdr h; cnt < limit && Read(h); cnt++) {
Seek(GetPos() + h.length);
}
if (cnt >= limit) {
m_type = mpeg_pva;
}
}
Seek(0);
if (m_type == mpeg_us) {
BYTE b;
for (int i = 0; (i < 4 || GetPos() < MAX_PROBE_SIZE) && m_type == mpeg_us && NextMpegStartCode(b); i++) {
if (b == 0xba) {
pshdr h;
if (Read(h)) {
m_type = mpeg_ps;
m_rate = int(h.bitrate / 8);
break;
}
} else if ((b & 0xe0) == 0xc0 // audio, 110xxxxx, mpeg1/2/3
|| (b & 0xf0) == 0xe0 // video, 1110xxxx, mpeg1/2
// || (b&0xbd) == 0xbd) // private stream 1, 0xbd, ac3/dts/lpcm/subpic
|| b == 0xbd) { // private stream 1, 0xbd, ac3/dts/lpcm/subpic
peshdr h;
if (Read(h, b) && BitRead(24, true) == 0x000001) {
m_type = mpeg_es;
}
}
}
}
Seek(0);
if (m_type == mpeg_us) {
return E_FAIL;
}
// min/max pts & bitrate
m_rtMin = m_posMin = _I64_MAX;
m_rtMax = m_posMax = 0;
m_rtPrec = _I64_MIN;
m_bPTSWrap = false;
m_init = true;
if (IsRandomAccess() || IsStreaming()) {
if (IsStreaming()) {
for (int i = 0; i < 20 || i < 50 && S_OK != HasMoreData(MEGABYTE, 100); i++) {
;
}
}
SearchPrograms(0, min(GetLength(), MEGABYTE * 5)); // max 5Mb for search a valid Program Map Table
__int64 pfp = 0;
const int k = 5;
for (int i = 0; i <= k; i++) {
__int64 fp = i * GetLength() / k;
fp = min(GetLength() - MEGABYTE / 8, fp);
fp = max(pfp, fp);
__int64 nfp = fp + (pfp == 0 ? 10 * MEGABYTE : MEGABYTE / 8);
if (FAILED(hr = SearchStreams(fp, nfp, pAsyncReader))) {
return hr;
}
pfp = nfp;
}
} else {
if (FAILED(hr = SearchStreams(0, MEGABYTE / 8, pAsyncReader))) {
return hr;
}
}
if (m_type == mpeg_ts) {
if (IsRandomAccess() || IsStreaming()) {
if (IsStreaming()) {
for (int i = 0; i < 20 || i < 50 && S_OK != HasMoreData(1024 * 100, 100); i++) {
;
}
}
__int64 pfp = 0;
const int k = 5;
for (int i = 0; i <= k; i++) {
__int64 fp = i * GetLength() / k;
fp = min(GetLength() - MEGABYTE / 8, fp);
fp = max(pfp, fp);
__int64 nfp = fp + (pfp == 0 ? 10 * MEGABYTE : MEGABYTE / 8);
if (FAILED(hr = SearchStreams(fp, nfp, pAsyncReader, TRUE))) {
return hr;
}
pfp = nfp;
}
} else {
if (FAILED(hr = SearchStreams(0, MEGABYTE / 8, pAsyncReader, TRUE))) {
return hr;
}
}
}
if (m_posMax - m_posMin <= 0 || (m_rtMax - m_rtMin <= 0 && !m_bPTSWrap)) {
return E_FAIL;
}
m_init = false;
int indicated_rate = m_rate;
REFERENCE_TIME dur = !m_bPTSWrap ? (m_rtMax - m_rtMin) : (PTS_MAX_BEFORE_WRAP - m_rtMin + m_rtMax);
int detected_rate = int(10000000i64 * (m_posMax - m_posMin) / dur);
m_rate = detected_rate ? detected_rate : m_rate;
#if (0)
// normally "detected" should always be less than "indicated", but sometimes it can be a few percent higher (+10% is allowed here)
// (update: also allowing +/-50k/s)
if (indicated_rate == 0 || ((float)detected_rate / indicated_rate) < 1.1 || abs(detected_rate - indicated_rate) < 50 * 1024) {
m_rate = detected_rate;
} else {
; // TODO: in this case disable seeking, or try doing something less drastical...
}
#endif
// Add fake subtitle stream...
if (m_streams[video].GetCount() && m_streams[subpic].GetCount()) {
if (m_type == mpeg_ts && m_bIsHdmv) {
AddHdmvPGStream(NO_SUBTITLE_PID, "---");
} else {
stream s;
s.pid = NO_SUBTITLE_PID;
s.mt.majortype = m_streams[subpic].GetHead().mt.majortype;
s.mt.subtype = m_streams[subpic].GetHead().mt.subtype;
s.mt.formattype = m_streams[subpic].GetHead().mt.formattype;
m_streams[subpic].Insert(s, this);
}
}
Seek(0);
return S_OK;
}
void CMpegSplitterFile::OnComplete(IAsyncReader* pAsyncReader)
{
__int64 pos = GetPos();
if (SUCCEEDED(SearchStreams(GetLength() - 500 * 1024, GetLength(), pAsyncReader, TRUE))) {
int indicated_rate = m_rate;
REFERENCE_TIME dur = !m_bPTSWrap ? (m_rtMax - m_rtMin) : (PTS_MAX_BEFORE_WRAP - m_rtMin + m_rtMax);
int detected_rate = int(10000000i64 * (m_posMax - m_posMin) / dur);
m_rate = detected_rate ? detected_rate : m_rate;
#if (0)
// normally "detected" should always be less than "indicated", but sometimes it can be a few percent higher (+10% is allowed here)
// (update: also allowing +/-50k/s)
if (indicated_rate == 0 || ((float)detected_rate / indicated_rate) < 1.1 || abs(detected_rate - indicated_rate) < 50 * 1024) {
m_rate = detected_rate;
} else {
; // TODO: in this case disable seeking, or try doing something less drastical...
}
#endif
}
Seek(pos);
}
REFERENCE_TIME CMpegSplitterFile::NextPTS(DWORD TrackNum)
{
REFERENCE_TIME rt = -1;
__int64 rtpos = -1;
BYTE b;
while (GetRemaining()) {
if (m_type == mpeg_ps || m_type == mpeg_es) {
if (!NextMpegStartCode(b)) { // continue;
ASSERT(0);
break;
}
rtpos = GetPos() - 4;
if ((b >= 0xbd && b < 0xf0) || (b == 0xfd)) {
peshdr h;
if (!Read(h, b) || !h.len) {
continue;
}
__int64 pos = GetPos();
if (h.fpts && AddStream(0, b, h.id_ext, h.len) == TrackNum) {
//ASSERT(h.pts >= m_rtMin && h.pts <= m_rtMax);
rt = h.pts;
break;
}
Seek(pos + h.len);
}
} else if (m_type == mpeg_ts) {
tshdr h;
if (!Read(h)) {
continue;
}
rtpos = GetPos() - 4;
if (h.payload && h.payloadstart && ISVALIDPID(h.pid)) {
peshdr h2;
if (NextMpegStartCode(b, 4) && Read(h2, b)) { // pes packet
if (h2.fpts && AddStream(h.pid, b, 0, DWORD(h.bytes - (GetPos() - rtpos)) == TrackNum)) {
//ASSERT(h2.pts >= m_rtMin && h2.pts <= m_rtMax);
rt = h2.pts;
break;
}
}
}
Seek(h.next);
} else if (m_type == mpeg_pva) {
pvahdr h;
if (!Read(h)) {
continue;
}
if (h.fpts) {
rt = h.pts;
break;
}
}
}
if (rtpos >= 0) {
Seek(rtpos);
}
if (rt >= 0) {
rt -= m_rtMin;
}
return rt;
}
void CMpegSplitterFile::SearchPrograms(__int64 start, __int64 stop)
{
if (m_type != mpeg_ts) {
return;
}
Seek(start);
stop = min(stop, GetLength());
while (GetPos() < stop) {
tshdr h;
if (!Read(h)) {
continue;
}
UpdatePrograms(h);
Seek(h.next);
}
}
HRESULT CMpegSplitterFile::SearchStreams(__int64 start, __int64 stop, IAsyncReader* pAsyncReader, BOOL CalcDuration)
{
Seek(start);
stop = min(stop, GetLength());
while (GetPos() < stop) {
BYTE b;
if (m_type == mpeg_ps || m_type == mpeg_es) {
if (!NextMpegStartCode(b)) {
continue;
}
if (b == 0xba) { // program stream header
pshdr h;
if (!Read(h)) {
continue;
}
m_rate = int(h.bitrate / 8);
} else if (b == 0xbb) { // program stream system header
pssyshdr h;
if (!Read(h)) {
continue;
}
} else if ((b >= 0xbd && b < 0xf0) || (b == 0xfd)) { // pes packet
peshdr h;
if (!Read(h, b) || !h.len) {
continue;
}
if (h.type == mpeg2 && h.scrambling) {
ASSERT(0);
return E_FAIL;
}
if (h.fpts) {
if (m_rtMin == _I64_MAX) {
m_rtMin = h.pts;
m_posMin = GetPos();
TRACE(_T("m_rtMin(SearchStreams)=%s\n"), ReftimeToString(m_rtMin));
}
if (m_rtMin < h.pts && m_rtMax < h.pts) {
m_rtMax = h.pts;
m_posMax = GetPos();
TRACE(_T("m_rtMax(SearchStreams)=%s\n"), ReftimeToString(m_rtMax));
}
}
__int64 pos = GetPos();
AddStream(0, b, h.id_ext, h.len);
if (h.len) {
Seek(pos + h.len);
}
}
} else if (m_type == mpeg_ts) {
tshdr h;
if (!Read(h)) {
continue;
}
__int64 pos = GetPos();
//UpdatePrograms(h);
if (h.payload && ISVALIDPID(h.pid)) {
peshdr h2;
if (h.payloadstart && NextMpegStartCode(b, 4) && Read(h2, b)) { // pes packet
if (h2.type == mpeg2 && h2.scrambling) {
ASSERT(0);
return E_FAIL;
}
if (h2.fpts && CalcDuration && (m_AlternativeDuration || (GetMasterStream() && GetMasterStream()->GetHead() == h.pid))) {
if (m_rtPrec != _I64_MIN && abs(h2.pts - m_rtPrec) >= PTS_MAX_BEFORE_WRAP / 2) {
m_bPTSWrap = true;
m_rtMax = h2.pts;
m_posMax = GetPos();
TRACE(_T("PTS Wrap detected --> m_rtMax(SearchStreams)=%s\n"), ReftimeToString(m_rtMax));
}
m_rtPrec = h2.pts;
if ((m_rtMin == _I64_MAX) || (m_rtMin > h2.pts && m_rtMax > h2.pts && !m_bPTSWrap)) {
m_rtMin = h2.pts;
m_posMin = GetPos();
TRACE(_T("m_rtMin(SearchStreams)=%s, PID=%d\n"), ReftimeToString(m_rtMin), h.pid);
}
if (m_rtMax < h2.pts && (m_rtMin < h2.pts || m_bPTSWrap)) {
m_rtMax = h2.pts;
m_posMax = GetPos();
TRACE(_T("m_rtMax(SearchStreams)=%s, PID=%d\n"), ReftimeToString(m_rtMax), h.pid);
// Ugly code : to support BRD H264 seamless playback, CMultiFiles need to update m_rtPTSOffset variable
// each time a new part is open...
// use this code only if Blu-ray is detected
if (m_ClipInfo.IsHdmv()) {
for (size_t i = 0; i < m_ClipInfo.GetStreamNumber(); i++) {
CHdmvClipInfo::Stream* stream = m_ClipInfo.GetStreamByIndex(i);
if (stream->m_Type == VIDEO_STREAM_H264 && m_rtMin == 116506666) {
CComQIPtr<ISyncReader> pReader = pAsyncReader;
if (pReader) {
pReader->SetPTSOffset(&m_rtPTSOffset);
}
//TRACE(_T("UPDATE m_rtPTSOffset(SearchStreams)=%s\n"), ReftimeToString(m_rtPTSOffset));
//TRACE(_T("m_rtMin(Boucle)=%s\n"), ReftimeToString(m_rtMin));
//TRACE(_T("stream=%d\n"), stream->m_Type);
//TRACE(_T("m_rtMax(Boucle)=%s\n"), ReftimeToString(m_rtMax));
//TRACE(_T("m_rtMax - m_rtMin(Boucle)=%s\n"), ReftimeToString(m_rtMax - m_rtMin));
}
}
}
}
}
} else {
b = 0;
}
if (!CalcDuration) {
AddStream(h.pid, b, 0, DWORD(h.bytes - (GetPos() - pos)));
}
}
Seek(h.next);
} else if (m_type == mpeg_pva) {
pvahdr h;
if (!Read(h)) {
continue;
}
if (h.fpts) {
if (m_rtMin == _I64_MAX) {
m_rtMin = h.pts;
m_posMin = GetPos();
}
if (m_rtMin < h.pts && m_rtMax < h.pts) {
m_rtMax = h.pts;
m_posMax = GetPos();
}
}
__int64 pos = GetPos();
if (h.streamid == 1) {
AddStream(h.streamid, 0xe0, 0, h.length);
} else if (h.streamid == 2) {
AddStream(h.streamid, 0xc0, 0, h.length);
}
if (h.length) {
Seek(pos + h.length);
}
}
}
return S_OK;
}
#define IsMpegAudio(stream_type) (stream_type == AUDIO_STREAM_MPEG1 || stream_type == AUDIO_STREAM_MPEG2)
#define IsAACAudio(stream_type) (stream_type == AUDIO_STREAM_AAC)
#define IsAACLATMAudio(stream_type) (stream_type == AUDIO_STREAM_AAC_LATM)
#define IsAC3Audio(stream_type) ( \
stream_type == AUDIO_STREAM_AC3 || stream_type == AUDIO_STREAM_AC3_PLUS || \
stream_type == AUDIO_STREAM_AC3_TRUE_HD || stream_type == SECONDARY_AUDIO_AC3_PLUS || \
stream_type == PES_PRIVATE)
#define IsMPEG2Video(stream_type) (stream_type == VIDEO_STREAM_MPEG2)
#define IsH264Video(stream_type) (stream_type == VIDEO_STREAM_H264)
#define IsVC1Video(stream_type) (stream_type == VIDEO_STREAM_VC1)
DWORD CMpegSplitterFile::AddStream(WORD pid, BYTE pesid, BYTE ps1id, DWORD len)
{
if (pid) {
if (pesid) {
m_pid2pes[pid] = pesid;
} else {
m_pid2pes.Lookup(pid, pesid);
}
}
stream s;
s.pid = pid;
s.pesid = pesid;
s.ps1id = ps1id;
const __int64 start = GetPos();
int type = unknown;
if (pesid >= 0xe0 && pesid < 0xf0) { // mpeg video
// MPEG2
if (type == unknown) {
CMpegSplitterFile::seqhdr h;
if (!m_streams[video].Find(s) && Read(h, len, &s.mt)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsMPEG2Video(stream_type)) {
type = video;
}
} else {
type = video;
}
}
}
// H.264
if (type == unknown) {
Seek(start);
// PPS and SPS can be present on differents packets
// and can also be split into multiple packets
if (!avch.Lookup(pid)) {
memset(&avch[pid], 0, sizeof(CMpegSplitterFile::avchdr));
}
#if defined(MVC_SUPPORT)
if (!m_streams[video].Find(s) && !m_streams[stereo].Find(s) && Read(avch[pid], len, &s.mt)) {
if (avch[pid].spspps[index_subsetsps].complete) {
type = stereo;
} else {
type = video;
}
}
#else
if (!m_streams[video].Find(s) && Read(avch[pid], len, &s.mt)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsH264Video(stream_type)) {
type = video;
}
} else {
type = video;
}
}
#endif
}
} else if (pesid >= 0xc0 && pesid < 0xe0) { // mpeg audio
// AAC
if (type == unknown) {
CMpegSplitterFile::aachdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt, m_type)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsAACAudio(stream_type)) {
type = audio;
}
} else {
type = audio;
}
}
}
// AAC LATM
if (type == unknown) {
Seek(start);
CMpegSplitterFile::latm_aachdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsAACLATMAudio(stream_type)) {
type = audio;
}
} else {
type = audio;
}
}
}
// MPEG Audio
if (type == unknown) {
Seek(start);
CMpegSplitterFile::mpahdr h;
if (!m_streams[audio].Find(s) && Read(h, len, false, &s.mt)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsMpegAudio(stream_type)) {
type = audio;
}
} else {
type = audio;
}
}
}
} else if (pesid == 0xbd || pesid == 0xfd) { // private stream 1
if (s.pid) {
if (!m_streams[audio].Find(s) && !m_streams[video].Find(s)) {
// AC3, E-AC3, TrueHD
if (type == unknown) {
CMpegSplitterFile::ac3hdr h;
if (Read(h, len, &s.mt, true, (m_AC3CoreOnly == 1))) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsAC3Audio(stream_type)) {
type = audio;
}
} else {
type = audio;
}
}
}
// DTS, DTS HD, DTS HD MA
if (type == unknown) {
Seek(start);
CMpegSplitterFile::dtshdr h;
if (Read(h, len, &s.mt, false)) {
type = audio;
}
}
// VC1
if (type == unknown) {
Seek(start);
CMpegSplitterFile::vc1hdr h;
if (!m_streams[video].Find(s) && Read(h, len, &s.mt, m_nVC1_GuidFlag)) {
PES_STREAM_TYPE stream_type = INVALID;
if (GetStreamType(s.pid, stream_type)) {
if (IsVC1Video(stream_type)) {
type = video;
}
} else {
type = video;
}
}
}
// DVB subtitles
if (type == unknown) {
Seek(start);
CMpegSplitterFile::dvbsub h;
if (!m_streams[video].Find(s) && Read(h, len, &s.mt)) {
type = subpic;
}
}
int iProgram;
const CHdmvClipInfo::Stream* pClipInfo;
const program* pProgram = FindProgram(s.pid, iProgram, pClipInfo);
if ((type == unknown) && (pProgram != nullptr)) {
PES_STREAM_TYPE StreamType = INVALID;
Seek(start);
StreamType = pProgram->streams[iProgram].type;
switch (StreamType) {
case AUDIO_STREAM_LPCM: {
CMpegSplitterFile::hdmvlpcmhdr h;
if (!m_streams[audio].Find(s) && Read(h, &s.mt)) {
type = audio;
}
}
break;
case PRESENTATION_GRAPHICS_STREAM: {
CMpegSplitterFile::hdmvsubhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt, pClipInfo ? pClipInfo->m_LanguageCode : nullptr)) {
m_bIsHdmv = true;
type = subpic;
}
}
break;
}
}
} else if ((m_AC3CoreOnly != 1) && m_init) {
int iProgram;
const CHdmvClipInfo::Stream* pClipInfo;
const program* pProgram = FindProgram(s.pid, iProgram, pClipInfo);
if ((type == unknown) && (pProgram != nullptr) && AUDIO_STREAM_AC3_TRUE_HD == pProgram->streams[iProgram].type) {
const stream* source = m_streams[audio].FindStream(s.pid);
if (source && source->mt.subtype == MEDIASUBTYPE_DOLBY_AC3) {
CMpegSplitterFile::ac3hdr h;
if (Read(h, len, &s.mt, false, (m_AC3CoreOnly == 1)) && s.mt.subtype == MEDIASUBTYPE_DOLBY_TRUEHD) {
m_streams[audio].Replace((stream&)*source, s, this);
}
}
}
}
} else if (pesid == 0xfd) {
CMpegSplitterFile::vc1hdr h;
if (!m_streams[video].Find(s) && Read(h, len, &s.mt, m_nVC1_GuidFlag)) {
type = video;
}
} else {
BYTE b = (BYTE)BitRead(8, true);
WORD w = (WORD)BitRead(16, true);
DWORD dw = (DWORD)BitRead(32, true);
if (b >= 0x80 && b < 0x88 || w == 0x0b77) { // ac3
s.ps1id = (b >= 0x80 && b < 0x88) ? (BYTE)(BitRead(32) >> 24) : 0x80;
CMpegSplitterFile::ac3hdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt)) {
type = audio;
}
} else if (b >= 0x88 && b < 0x90 || dw == 0x7ffe8001) { // dts
s.ps1id = (b >= 0x88 && b < 0x90) ? (BYTE)(BitRead(32) >> 24) : 0x88;
CMpegSplitterFile::dtshdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt)) {
type = audio;
}
} else if (b >= 0xa0 && b < 0xa8) { // lpcm
s.ps1id = (BYTE)BitRead(8);
do {
// DVD-Audio LPCM
if (b == 0xa0) {
BitRead(8); // Continuity Counter - counts from 0x00 to 0x1f and then wraps to 0x00.
DWORD headersize = (DWORD)BitRead(16); // LPCM_header_length
if (headersize >= 8 && headersize + 4 < len) {
CMpegSplitterFile::dvdalpcmhdr h;
if (Read(h, len - 4, &s.mt)) {
Seek(start + 4 + headersize);
type = audio;
break;
}
}
}
// DVD-Audio MLP
else if (b == 0xa1 && len > 10) {
BYTE counter = (BYTE)BitRead(8); // Continuity Counter: 0x00..0x1f or 0x20..0x3f or 0x40..0x5f
BitRead(8); // some unknown data
DWORD headersize = (DWORD)BitRead(8); // MLP_header_length (always equal 6?)
BitRead(32); // some unknown data
WORD unknown1 = (WORD)BitRead(16); // 0x0000 or 0x0400
if (counter <= 0x5f && headersize == 6 && (unknown1 == 0x0000 || unknown1 == 0x0400)) { // Maybe it's MLP?
// MLP header may be missing in the first package
CMpegSplitterFile::mlphdr h;
if (!m_streams[audio].Find(s) && Read(h, len - 10, &s.mt, true)) {
// This is exactly the MLP.
Seek(start + 10);
type = audio;
}
Seek(start + 10);
break;
}
}
// DVD LPCM
if (m_streams[audio].Find(s)) {
Seek(start + 7);
} else {
Seek(start + 4);
CMpegSplitterFile::lpcmhdr h;
if (Read(h, &s.mt)) {
type = audio;
}
}
} while (false);
} else if (b >= 0x20 && b < 0x40) { // DVD subpic
s.ps1id = (BYTE)BitRead(8);
CMpegSplitterFile::dvdspuhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt)) {
type = subpic;
}
} else if (b >= 0x70 && b < 0x80) { // SVCD subpic
s.ps1id = (BYTE)BitRead(8);
CMpegSplitterFile::svcdspuhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt)) {
type = subpic;
}
} else if (b >= 0x00 && b < 0x10) { // CVD subpic
s.ps1id = (BYTE)BitRead(8);
CMpegSplitterFile::cvdspuhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt)) {
type = subpic;
}
} else if (w == 0xffa0 || w == 0xffa1) { // ps2-mpg audio
s.ps1id = (BYTE)BitRead(8);
s.pid = (WORD)((BitRead(8) << 8) | BitRead(16)); // pid = 0xa000 | track id
CMpegSplitterFile::ps2audhdr h;
if (!m_streams[audio].Find(s) && Read(h, &s.mt)) {
type = audio;
}
} else if (w == 0xff90) { // ps2-mpg ac3 or subtitles
s.ps1id = (BYTE)BitRead(8);
s.pid = (WORD)((BitRead(8) << 8) | BitRead(16)); // pid = 0x9000 | track id
w = (WORD)BitRead(16, true);
if (w == 0x0b77) {
CMpegSplitterFile::ac3hdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt)) {
type = audio;
}
} else if (w == 0x0000) { // usually zero...
CMpegSplitterFile::ps2subhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt)) {
type = subpic;
}
}
} else if (b >= 0xc0 && b < 0xcf) { // dolby digital/dolby digital +
s.ps1id = (BYTE)BitRead(8);
// skip audio header - 3-byte
BitRead(8);
BitRead(8);
BitRead(8);
CMpegSplitterFile::ac3hdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt)) {
type = audio;
}
} else if (b >= 0xb0 && b < 0xbf) { // truehd
s.ps1id = (BYTE)BitRead(8);
// skip audio header - 3-byte
BitRead(8);
BitRead(8);
BitRead(8);
// TrueHD audio has a 4-byte header
BitRead(8);
CMpegSplitterFile::ac3hdr h;
if (!m_streams[audio].Find(s) && Read(h, len, &s.mt, false, false)) {
type = audio;
}
}
}
} else if (pesid == 0xbe) { // padding
} else if (pesid == 0xbf) { // private stream 2
}
if (type != unknown && !m_streams[type].Find(s)) {
if (s.pid) {
for (int i = 0; i < unknown; i++) {
if (m_streams[i].Find(s)) {
return s;
}
}
}
m_streams[type].Insert(s, this);
}
return s;
}
void CMpegSplitterFile::AddHdmvPGStream(WORD pid, const char* language_code)
{
stream s;
s.pid = pid;
s.pesid = 0xbd;
CMpegSplitterFile::hdmvsubhdr h;
if (!m_streams[subpic].Find(s) && Read(h, &s.mt, language_code)) {
m_streams[subpic].Insert(s, this);
}
}
CAtlList<CMpegSplitterFile::stream>* CMpegSplitterFile::GetMasterStream()
{
return
!m_streams[video].IsEmpty() ? &m_streams[video] :
!m_streams[audio].IsEmpty() ? &m_streams[audio] :
!m_streams[subpic].IsEmpty() ? &m_streams[subpic] :
#if defined(MVC_SUPPORT)
!m_streams[stereo].IsEmpty() ? &m_streams[stereo] :
#endif
nullptr;
}
void CMpegSplitterFile::UpdatePrograms(const tshdr& h, bool UpdateLang)
{
CAutoLock cAutoLock(&m_csProps);
if (h.payload && h.payloadstart && h.pid == 0) {
trsechdr h2;
if (Read(h2) && h2.table_id == 0) {
CAtlMap<WORD, bool> newprograms;
int len = h2.section_length;
len -= 5 + 4;
for (int i = len / 4; i > 0; i--) {
WORD program_number = (WORD)BitRead(16);
BYTE reserved = (BYTE)BitRead(3);
WORD pid = (WORD)BitRead(13);
UNREFERENCED_PARAMETER(reserved);
if (program_number != 0) {
m_programs[pid].program_number = program_number;
newprograms[program_number] = true;
}
}
POSITION pos = m_programs.GetStartPosition();
while (pos) {
const CAtlMap<WORD, program>::CPair* pPair = m_programs.GetNext(pos);
if (!newprograms.Lookup(pPair->m_value.program_number)) {
m_programs.RemoveKey(pPair->m_key);
}
}
}
} else if (CAtlMap<WORD, program>::CPair* pPair = m_programs.Lookup(h.pid)) {
if (h.payload && h.payloadstart) {
trsechdr h2;
if (Read(h2) && h2.table_id == 2) {
int len = h2.section_length;
len -= 5 + 4;
BYTE buffer[1024];
ByteRead(buffer, len);
int max_len = h.bytes - 9;
if (len > max_len) {
memset(pPair->m_value.ts_buffer, 0, sizeof(pPair->m_value.ts_buffer));
pPair->m_value.ts_len_cur = max_len;
pPair->m_value.ts_len_packet = len;
memcpy(pPair->m_value.ts_buffer, buffer, max_len);
} else {
CGolombBuffer gb(buffer, len);
UpdatePrograms(gb, h.pid, UpdateLang);
}
}
} else {
if (pPair->m_value.ts_len_cur > 0) {
int len = pPair->m_value.ts_len_packet - pPair->m_value.ts_len_cur;
if (len > h.bytes) {
ByteRead(pPair->m_value.ts_buffer + pPair->m_value.ts_len_cur, h.bytes);
pPair->m_value.ts_len_cur += h.bytes;
} else {
ByteRead(pPair->m_value.ts_buffer + pPair->m_value.ts_len_cur, pPair->m_value.ts_len_packet - pPair->m_value.ts_len_cur);
CGolombBuffer gb(pPair->m_value.ts_buffer, pPair->m_value.ts_len_packet);
UpdatePrograms(gb, h.pid, UpdateLang);
}
}
}
}
}
void CMpegSplitterFile::UpdatePrograms(CGolombBuffer gb, WORD pid, bool UpdateLang)
{
if (CAtlMap<WORD, program>::CPair* pPair = m_programs.Lookup(pid)) {
memset(pPair->m_value.streams, 0, sizeof(pPair->m_value.streams));
int len = gb.GetSize();
BYTE reserved1 = (BYTE)gb.BitRead(3);
WORD PCR_PID = (WORD)gb.BitRead(13);
BYTE reserved2 = (BYTE)gb.BitRead(4);
WORD program_info_length = (WORD)gb.BitRead(12);
UNREFERENCED_PARAMETER(reserved1);
UNREFERENCED_PARAMETER(PCR_PID);
UNREFERENCED_PARAMETER(reserved2);
len -= (4 + program_info_length);
if (len <= 0) {
return;
}
while (program_info_length-- > 0) {
gb.BitRead(8);
}
for (int i = 0; i < _countof(pPair->m_value.streams) && len >= 5; i++) {
BYTE stream_type = (BYTE)gb.BitRead(8);
BYTE nreserved1 = (BYTE)gb.BitRead(3);
WORD pid = (WORD)gb.BitRead(13);
BYTE nreserved2 = (BYTE)gb.BitRead(4);
WORD ES_info_length = (WORD)gb.BitRead(12);
UNREFERENCED_PARAMETER(nreserved1);
UNREFERENCED_PARAMETER(nreserved2);
pPair->m_value.streams[i].pid = pid;
pPair->m_value.streams[i].type = (PES_STREAM_TYPE)stream_type;
if (m_ForcedSub) {
if (stream_type == PRESENTATION_GRAPHICS_STREAM) {
stream s;
s.pid = pid;
CMpegSplitterFile::hdmvsubhdr hdr;
if (Read(hdr, &s.mt, nullptr)) {
if (!m_streams[subpic].Find(s)) {
m_streams[subpic].Insert(s, this);
}
}
}
}
len -= (5 + ES_info_length);
if (len < 0) {
break;
}
if (ES_info_length <= 2) {
continue;
}
if (UpdateLang) {
int info_length = ES_info_length;
for (;;) {
BYTE descriptor_tag = (BYTE)gb.BitRead(8);
BYTE descriptor_length = (BYTE)gb.BitRead(8);
info_length -= (2 + descriptor_length);
if (info_length < 0) {
break;
}
char ch[4];
switch (descriptor_tag) {
case 0x0a: // ISO 639 language descriptor
case 0x56: // Teletext descriptor
case 0x59: // Subtitling descriptor
gb.ReadBuffer((BYTE*)ch, 3);
ch[3] = 0;
for (int j = 3; j < descriptor_length; j++) {
gb.BitRead(8);
}
if (!(ch[0] == 'u' && ch[1] == 'n' && ch[2] == 'd')) {
m_pPMT_Lang[pid] = ISO6392ToLanguage(ch);
}
break;
default:
for (int j = 0; j < descriptor_length; j++) {
gb.BitRead(8);
}
break;
}
if (info_length <= 2) {
break;
}
}
} else {
while (ES_info_length-- > 0) {
gb.BitRead(8);
}
}
}
pPair->m_value.ts_len_cur = 0;
pPair->m_value.ts_len_packet = 0;
}
}
UINT32 SwapLE(const UINT32& _Value)
{
return (_Value & 0xFF) << 24 | ((_Value >> 8) & 0xFF) << 16 | ((_Value >> 16) & 0xFF) << 8 | ((_Value >> 24) & 0xFF) << 0;
}
UINT16 SwapLE(const UINT16& _Value)
{
return (_Value & 0xFF) << 8 | ((_Value >> 8) & 0xFF) << 0;
}
const CMpegSplitterFile::program* CMpegSplitterFile::FindProgram(WORD pid, int& iStream, const CHdmvClipInfo::Stream*& _pClipInfo)
{
_pClipInfo = m_ClipInfo.FindStream(pid);
iStream = -1;
POSITION pos = m_programs.GetStartPosition();
while (pos) {
program* p = &m_programs.GetNextValue(pos);
for (int i = 0; i < _countof(p->streams); i++) {
if (p->streams[i].pid == pid) {
iStream = i;
return p;
}
}
}
return nullptr;
}
bool CMpegSplitterFile::GetStreamType(WORD pid, PES_STREAM_TYPE& stream_type)
{
int iProgram;
const CHdmvClipInfo::Stream* pClipInfo;
const program* pProgram = FindProgram(pid, iProgram, pClipInfo);
if (pProgram) {
stream_type = pProgram->streams[iProgram].type;
if (stream_type != INVALID) {
return true;
}
}
return false;
}
|