-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetbit.cpp
3076 lines (2817 loc) · 94.4 KB
/
getbit.cpp
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
/*
* Mutated into DGIndex. Modifications Copyright (C) 2004-2008, Donald Graft
*
* Copyright (C) Chia-chen Kuo - April 2001
*
* This file is part of DVD2AVI, a free MPEG-2 decoder
*
* DVD2AVI 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 2, or (at your option)
* any later version.
*
* DVD2AVI 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 GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#define GETBIT_GLOBAL
#include "global.h"
#include "getbit.h"
#include "AC3Dec\ac3.h"
unsigned int start;
int _donread(int fd, void *buffer, unsigned int count)
{
int bytes;
bytes = _read(fd, buffer, count);
return bytes;
}
#define MONO 3
#define STEREO 0
#define LAYER1 3
#define LAYER2 2
#define LAYER3 1
#define LAYERRESERVED 0
#define SAMPLE32K 2
#define SAMPLE44K 0
#define SAMPLERESERVED 3
#define BITRATERESERVED 0xf
int check_audio_syncword(unsigned int audio_id, int layer, int bitrate, int sample, int mode, int emphasis)
{
// Try to validate this syncword by validating semantics of the audio header.
// We're trying to filter out emulated syncwords.
if (layer == LAYERRESERVED)
return 1;
if (bitrate == BITRATERESERVED)
return 1;
if (sample == SAMPLERESERVED)
return 1;
if (layer == LAYER2)
{
if ((bitrate == 1 || bitrate == 2 || bitrate == 3 || bitrate == 5) && mode != MONO)
return 1;
if ((bitrate == 11 || bitrate == 12 || bitrate == 13 || bitrate == 14) && mode == MONO)
return 1;
}
// Emphasis is almost never used. It's less likely than hitting an emulated header. :-)
// if (emphasis != 0)
// return 1;
// The header appears to be valid. Store the audio characteristics.
audio[audio_id].layer = layer;
audio[audio_id].rate = bitrate;
audio[audio_id].sample = sample;
audio[audio_id].mode = mode;
return 0;
}
int PTSDifference(unsigned int apts, unsigned int vpts, int *result)
{
int diff;
if (apts > vpts)
{
diff = (apts - vpts) / 90;
// if (diff > 5000) return 1;
*result = diff;
}
else
{
diff = (vpts - apts) / 90;
// if (diff > 5000) return 1;
*result = -diff;
}
if (diff > 1000 && (D2V_Flag || AudioOnly_Flag) && !CLIActive)
{
MessageBox(hWnd,
"The calculated audio delay is unusually large. This is sometimes\n"
"caused by an initial black video lead in that does not have valid\n"
"timestamps. You can try setting your project range to skip this\n"
"portion of the video. Use the > button to skip GOPS and then hit\n"
"the [ button to set the start of the project. This may give you a\n"
"valid delay value.",
"Audio Delay Warning",
MB_OK | MB_ICONWARNING);
// Reset data timeout.
start = timeGetTime();
}
return 0;
}
FILE *OpenAudio(char *path, char *mode, unsigned int id)
{
// Pick up the first audio file path for
// use in the AVS template.
if (id < LowestAudioId)
{
strcpy(AudioFilePath, path);
LowestAudioId = id;
}
return fopen(path, mode);
}
#define LOCATE \
while (Rdptr >= (Rdbfr + BUFFER_SIZE)) \
{ \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr -= BUFFER_SIZE; \
}
#define DECODE_AC3 \
{ \
if (SystemStream_Flag == TRANSPORT_STREAM && TransportPacketSize == 204) \
Packet_Length -= 16; \
size = 0; \
while (Packet_Length > 0) \
{ \
if (Packet_Length+Rdptr > BUFFER_SIZE+Rdbfr) \
{ \
size = ac3_decode_data(Rdptr, BUFFER_SIZE+Rdbfr-Rdptr, size); \
Packet_Length -= BUFFER_SIZE+Rdbfr-Rdptr; \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr = Rdbfr; \
} \
else \
{ \
size = ac3_decode_data(Rdptr, Packet_Length, size); \
Rdptr += Packet_Length; \
Packet_Length = 0; \
} \
} \
}
// For debugging. Use the macro for release code.
_inline void demux_ac3(int *Packet_Length, unsigned int AUDIO_ID)
{
if (SystemStream_Flag == TRANSPORT_STREAM && TransportPacketSize == 204)
*Packet_Length -= 16;
while (*Packet_Length > 0)
{
if (*Packet_Length+Rdptr > BUFFER_SIZE+Rdbfr)
{
fwrite(Rdptr, BUFFER_SIZE+Rdbfr-Rdptr, 1, audio[AUDIO_ID].file);
*Packet_Length -= BUFFER_SIZE+Rdbfr-Rdptr;
// Read = _donread(Infile[CurrentFile], Rdbfr, BUFFER_SIZE);
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]);
if (Read < BUFFER_SIZE) Next_File();
Rdptr = Rdbfr;
}
else
{
fwrite(Rdptr, *Packet_Length, 1, audio[AUDIO_ID].file);
Rdptr += *Packet_Length;
*Packet_Length = 0;
}
}
}
#define DEMUX_AC3 \
{ \
if (SystemStream_Flag == TRANSPORT_STREAM && TransportPacketSize == 204) \
Packet_Length -= 16; \
while (Packet_Length > 0) \
{ \
if (Packet_Length+Rdptr > BUFFER_SIZE+Rdbfr) \
{ \
fwrite(Rdptr, BUFFER_SIZE+Rdbfr-Rdptr, 1, audio[AUDIO_ID].file); \
Packet_Length -= BUFFER_SIZE+Rdbfr-Rdptr; \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr = Rdbfr; \
} \
else \
{ \
fwrite(Rdptr, Packet_Length, 1, audio[AUDIO_ID].file); \
Rdptr += Packet_Length; \
Packet_Length = 0; \
} \
} \
}
void DemuxLPCM(int *size, int *Packet_Length, unsigned char PCM_Buffer[], unsigned char format)
{
unsigned char tmp[12];
int i;
*size = 0;
while (*Packet_Length > 0)
{
if (*Packet_Length + Rdptr > BUFFER_SIZE + Rdbfr)
{
memcpy(PCM_Buffer + *size, Rdptr, BUFFER_SIZE + Rdbfr - Rdptr);
*size += (BUFFER_SIZE + Rdbfr - Rdptr);
*Packet_Length -= (BUFFER_SIZE + Rdbfr - Rdptr);
// Read = _donread(Infile[CurrentFile], Rdbfr, BUFFER_SIZE);
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]);
if (Read < BUFFER_SIZE)
Next_File();
Rdptr = Rdbfr;
}
else
{
memcpy(PCM_Buffer + *size, Rdptr, *Packet_Length);
Rdptr += *Packet_Length;
*size += *Packet_Length;
*Packet_Length = 0;
}
}
if ((format & 0xc0) == 0)
{
// 16-bit LPCM.
for (i = 0; i < *size; i += 2)
{
tmp[0] = PCM_Buffer[i];
PCM_Buffer[i] = PCM_Buffer[i+1];
PCM_Buffer[i+1] = tmp[0];
}
}
else
{
// 24-bit LPCM.
if ((format & 0x07) + 1 == 1)
{
// Mono
for (i = 0; i < *size; i += 6)
{
tmp[0]=PCM_Buffer[i+4];
tmp[1]=PCM_Buffer[i+1];
tmp[2]=PCM_Buffer[i+0];
tmp[3]=PCM_Buffer[i+5];
tmp[4]=PCM_Buffer[i+3];
tmp[5]=PCM_Buffer[i+2];
memcpy(&PCM_Buffer[i], tmp, 6);
}
}
else
{
// Stereo
for (i = 0; i < *size; i += 12)
{
tmp[0] = PCM_Buffer[i+8];
tmp[1] = PCM_Buffer[i+1];
tmp[2] = PCM_Buffer[i+0];
tmp[3] = PCM_Buffer[i+9];
tmp[4] = PCM_Buffer[i+3];
tmp[5] = PCM_Buffer[i+2];
tmp[6] = PCM_Buffer[i+10];
tmp[7] = PCM_Buffer[i+5];
tmp[8] = PCM_Buffer[i+4];
tmp[9] = PCM_Buffer[i+11];
tmp[10] = PCM_Buffer[i+7];
tmp[11] = PCM_Buffer[i+6];
memcpy(&PCM_Buffer[i], tmp, 12);
}
}
}
}
#define DEMUX_MPA_AAC(fp) \
{ \
if (SystemStream_Flag == TRANSPORT_STREAM && TransportPacketSize == 204) \
Packet_Length -= 16; \
while (Packet_Length > 0) \
{ \
if (Packet_Length+Rdptr > BUFFER_SIZE+Rdbfr) \
{ \
fwrite(Rdptr, BUFFER_SIZE+Rdbfr-Rdptr, 1, (fp)); \
Packet_Length -= BUFFER_SIZE+Rdbfr-Rdptr; \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr = Rdbfr; \
} \
else \
{ \
fwrite(Rdptr, Packet_Length, 1, (fp)); \
Rdptr += Packet_Length; \
Packet_Length = 0; \
} \
} \
}
#define DEMUX_DTS \
{ \
if (SystemStream_Flag == TRANSPORT_STREAM && TransportPacketSize == 204) \
Packet_Length -= 16; \
while (Packet_Length > 0) \
{ \
if (Packet_Length+Rdptr > BUFFER_SIZE+Rdbfr) \
{ \
fwrite(Rdptr, BUFFER_SIZE+Rdbfr-Rdptr, 1, audio[AUDIO_ID].file); \
Packet_Length -= BUFFER_SIZE+Rdbfr-Rdptr; \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr = Rdbfr; \
} \
else \
{ \
fwrite(Rdptr, Packet_Length, 1, audio[AUDIO_ID].file); \
Rdptr += Packet_Length; \
Packet_Length = 0; \
} \
} \
}
static char *FTType[5] = {
"48KHz", "44.1KHz", "44.1KHz", "44.1KHz", "44.1KHz"
};
static char *AC3ModeDash[8] = {
"1+1", "1_0", "2_0", "3_0", "2_1", "3_1", "2_2", "3_2"
};
static char *AC3Mode[8] = {
"1+1", "1/0", "2/0", "3/0", "2/1", "3/1", "2/2", "3/2"
};
static int AC3Rate[32] = {
32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
192, 224, 256, 320, 384, 448, 512, 576, 640,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static char *MPALayer[4] = {
"", "L3", "L2", "L1"
};
static char *MPAExtension[4] = {
"mpa", "mp3", "mp2", "mp1"
};
static char *MPAMode[4] = {
"2ch", "2ch", "2ch", "mono"
};
static char *MPASample[3] = {
"44.1", "48", "32"
};
static char *MPARate[4][15] = {
{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }, // Layer reserved
{ "free", "32", "40", "48", "56", "64", "80", "96", "112", "128", "160", "192", "224", "256", "320" }, // Layer 3
{ "free", "32", "48", "56", "64", "80", "96", "112", "128", "160", "192", "224", "256", "320", "384" }, // Layer 2
{ "free", "32", "64", "96", "128", "160", "192", "224", "256", "288", "320", "352", "384", "416", "448" } // Layer 1
};
unsigned int VideoPTS, AudioPTS;
static unsigned char PCM_Buffer[SECTOR_SIZE];
static short *ptrPCM_Buffer = (short*)PCM_Buffer;
unsigned char *buffer_invalid;
void VideoDemux(void);
void Initialize_Buffer()
{
Rdptr = Rdbfr + BUFFER_SIZE;
Rdmax = Rdptr;
buffer_invalid = (unsigned char *) 0xffffffff;
if (SystemStream_Flag != ELEMENTARY_STREAM && !AudioOnly_Flag)
{
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr = *Rdptr++ << 24;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++ << 16;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++ << 8;
if (Rdptr >= Rdmax)
Next_Packet();
CurrentBfr += *Rdptr++;
Fill_Next();
}
else
{
Fill_Buffer();
CurrentBfr = (*Rdptr << 24) + (*(Rdptr+1) << 16) + (*(Rdptr+2) << 8) + *(Rdptr+3);
Rdptr += 4;
Fill_Next();
}
BitsLeft = 32;
}
// Skips ahead in transport stream by specified number of bytes.
#define SKIP_TRANSPORT_PACKET_BYTES(bytes_to_skip) \
{ \
int temp = (bytes_to_skip); \
while (temp > 0) \
{ \
if (temp + Rdptr > BUFFER_SIZE + Rdbfr) \
{ \
temp -= BUFFER_SIZE + Rdbfr - Rdptr; \
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]); \
if (Read < BUFFER_SIZE) Next_File(); \
Rdptr = Rdbfr; \
} \
else \
{ \
Rdptr += temp; \
temp = 0; \
} \
} \
Packet_Length -= (bytes_to_skip); \
}
// Transport packet data structure.
typedef struct
{
// 1 byte
unsigned char sync_byte; // 8 bits
// 2 bytes
unsigned char transport_error_indicator; // 1 bit
unsigned char payload_unit_start_indicator; // 1 bit
unsigned char transport_priority; // 1 bit
unsigned short pid; // 13 bits
// 1 byte
unsigned char transport_scrambling_control; // 2 bits
unsigned char adaptation_field_control; // 2 bits
unsigned char continuity_counter; // 4 bits
// 1 byte
unsigned char adaptation_field_length; // 8 bits
// 1 byte
unsigned char discontinuity_indicator; // 1 bit
unsigned char random_access_indicator; // 1 bit
unsigned char elementary_stream_priority_indicator; // 1 bit
unsigned char PCR_flag; // 1 bit
unsigned char OPCR_flag; // 1 bit
unsigned char splicing_point_flag; // 1 bit
unsigned char transport_private_data_flag; // 1 bit
unsigned char adaptation_field_extension_flag; // 1 bit
} transport_packet;
transport_packet tp_zeroed = { 0 };
// ATSC transport stream parser.
// We ignore the 'continuity counter' because with some DTV
// broadcasters, this isnt a reliable indicator.
void Next_Transport_Packet()
{
static int i, Packet_Length, Packet_Header_Length, size;
static unsigned int code, flags, VOBCELL_Count, AUDIO_ID = 0;
__int64 PES_PTS, PES_DTS;
unsigned int pts_stamp = 0, dts_stamp = 0;
int PTSDiff;
unsigned int bytes_left;
transport_packet tp;
unsigned int time;
double picture_period;
char ext[4], EXT[4];
start = timeGetTime();
for (;;)
{
PES_PTS = 0;
bytes_left = 0;
Packet_Length = TransportPacketSize; // total length of an MPEG-2 transport packet
tp = tp_zeroed; // to avoid warnings
// If the packet size is 192, then assume it is an M2TS (blueray) file, which has 4 extra bytes
// in front of the sync byte.
if (TransportPacketSize == 192)
{
// Suck up the extra bytes.
Get_Byte();
Get_Byte();
Get_Byte();
Get_Byte();
Packet_Length -= 4;
}
retry_sync:
// Don't loop forever. If we don't get data
// in a reasonable time (5 secs) we exit.
time = timeGetTime();
if (time - start > 5000)
{
MessageBox(hWnd, "Cannot find audio or video data. Ensure that your PIDs\nare set correctly in the Stream menu. Refer to the\nUsers Manual for details.",
NULL, MB_OK | MB_ICONERROR);
ThreadKill(MISC_KILL);
}
// Search for a sync byte. Gives some protection against emulation.
if (Stop_Flag)
ThreadKill(MISC_KILL);
if (Get_Byte() != 0x47)
goto retry_sync;
if (Rdptr - Rdbfr > TransportPacketSize)
{
if (Rdptr[-(TransportPacketSize+1)] != 0x47)
goto retry_sync;
}
else if (Rdbfr + Read - Rdptr > TransportPacketSize - 1)
{
if (Rdptr[+(TransportPacketSize-1)] != 0x47)
goto retry_sync;
}
else
{
// We can't check so just accept this sync byte.
}
// Record the location of the start of the packet. This will be used
// for indexing when an I frame is detected.
if (D2V_Flag)
{
// PackHeaderPosition = _telli64(Infile[CurrentFile])
PackHeaderPosition = _ftelli64(Infile[CurrentFile])
- (__int64)BUFFER_SIZE + (__int64)Rdptr - (__int64)Rdbfr - 1;
}
// For M2TS (blueray) files, index the extra 4 bytes in front of the sync byte,
// because DGDecode will expect to see them.
if (TransportPacketSize == 192)
PackHeaderPosition -= 4;
--Packet_Length; // swallow the sync_byte
code = Get_Short();
Packet_Length = Packet_Length - 2; // swallow the two bytes we just got
tp.pid = (unsigned short) (code & 0x1FFF);
tp.transport_error_indicator = (unsigned char) ((code >> 15) & 0x01);
tp.payload_unit_start_indicator = (unsigned char) ((code >> 14) & 0x01);
tp.transport_priority = (unsigned char) ((code >> 13) & 0x01);
if (tp.transport_error_indicator)
{
// Skip remaining bytes in current packet.
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
// Try the next packet in the stream.
continue;
}
code = Get_Byte();
--Packet_Length; // decrement the 1 byte we just got;
tp.transport_scrambling_control = (unsigned char) ((code >> 6) & 0x03);// 2 bslbf
tp.adaptation_field_control = (unsigned char) ((code >> 4 ) & 0x03);// 2 bslbf
tp.continuity_counter = (unsigned char) (code & 0x0F);// 4 uimsbf
// we don't care about the continuity counter
if (tp.adaptation_field_control == 0)
{
// no payload
// skip remaining bytes in current packet
SKIP_TRANSPORT_PACKET_BYTES( Packet_Length )
continue;
}
// 3) check the Adaptation-header, only so we can determine
// the exact #bytes to skip
if ( tp.adaptation_field_control == 2 || tp.adaptation_field_control == 3)
{
// adaptation field is present
tp.adaptation_field_length = (unsigned char) Get_Byte(); // 8-bits
--Packet_Length; // decrement the 1 byte we just got;
if ( tp.adaptation_field_length != 0 ) // end of field already?
{
code = Get_Byte();
--Packet_Length; // decrement the 1 byte we just got;
tp.discontinuity_indicator = (unsigned char) ((code >> 7) & 0x01); // 1 bslbf
tp.random_access_indicator = (unsigned char) ((code >> 6) & 0x01); // 1 bslbf
tp.elementary_stream_priority_indicator = (unsigned char) ((code >> 5) & 0x01); // 1 bslbf
tp.PCR_flag = (unsigned char) ((code >> 4) & 0x01); // 1 bslbf
tp.OPCR_flag = (unsigned char) ((code >> 3) & 0x01); // 1 bslbf
tp.splicing_point_flag = (unsigned char) ((code >> 2) & 0x01); // 1 bslbf
tp.transport_private_data_flag = (unsigned char) ((code >> 1) & 0x01); // 1 bslbf
tp.adaptation_field_extension_flag = (unsigned char) ((code >> 0) & 0x01); // 1 bslbf
bytes_left = tp.adaptation_field_length - 1;
if (LogTimestamps_Flag && D2V_Flag && tp.PCR_flag && tp.pid == MPEG2_Transport_PCRPID && StartLogging_Flag)
{
__int64 PCR, PCRbase, PCRext, tmp;
char pcr[64];
tmp = (__int64) Get_Byte();
PCRbase = tmp << 25;
tmp = (__int64) Get_Byte();
PCRbase |= tmp << 17;
tmp = (__int64) Get_Byte();
PCRbase |= tmp << 9;
tmp = (__int64) Get_Byte();
PCRbase |= tmp << 1;
tmp = (__int64) Get_Byte();
PCRbase |= tmp >> 7;
PCRext = (tmp & 0x01) << 8;
tmp = (__int64) Get_Byte();
PCRext |= tmp;
PCR = 300 * PCRbase + PCRext;
_i64toa(PCR, pcr, 10);
fprintf(Timestamps, "PCR %s ", pcr);
_i64toa(PCR/27000, pcr, 10);
bytes_left -= 6;
Packet_Length -= 6;
fprintf(Timestamps, "[%sms]\n", pcr);
}
// skip the remainder of the adaptation_field
SKIP_TRANSPORT_PACKET_BYTES( bytes_left )
} // if ( tp.adaptation_field_length != 0 )
} // if ( tp.adaptation_field_control != 1 )
// We've processed the MPEG-2 transport header.
// Any data left in the current transport packet?
if (Packet_Length == 0)
continue;
if (tp.pid && tp.pid == MPEG2_Transport_VideoPID)
{
LOCATE
if (tp.payload_unit_start_indicator)
{
Get_Short();
Get_Short();
Get_Short(); // MPEG2-PES total Packet_Length
Get_Byte(); // skip a byte
flags = Get_Byte();
Packet_Header_Length = Get_Byte();
Packet_Length = Packet_Length - 9; // compensate the bytes we extracted
// Get timestamp, and skip rest of PES-header.
if ((flags & 0x80) && (Packet_Header_Length > 4))
{
PES_PTS = (Get_Byte() & 0x0e) << 29;
PES_PTS |= (Get_Short() & 0xfffe) << 14;
PES_PTS |= (Get_Short()>>1) & 0x7fff;
pts_stamp = (unsigned int) (PES_PTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, "V PTS %u [%ums]\n", pts_stamp, pts_stamp/90);
Packet_Length -= 5;
// DTS is not used. The code is here for analysis and debugging.
if ((flags & 0xc0) == 0xc0)
{
PES_DTS = (Get_Byte() & 0x0e) << 29;
PES_DTS |= (Get_Short() & 0xfffe) << 14;
PES_DTS |= (Get_Short()>>1) & 0x7fff;
dts_stamp = (unsigned int) (PES_DTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, "V DTS %u [%ums]\n", dts_stamp, dts_stamp/90);
Packet_Length -= 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length - 10)
}
else
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length - 5)
}
else
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length);
if (!Start_Flag)
{
// Start_Flag becomes true after the first I frame.
// So VideoPTS will be left at the value corresponding to
// the first I frame.
VideoPTS = pts_stamp;
}
}
Rdmax = Rdptr + Packet_Length;
if (TransportPacketSize == 204)
Rdmax -= 16;
Bitrate_Monitor += (Rdmax - Rdptr);
if (AudioOnly_Flag)
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
return;
}
else if ((Method_Flag == AUDIO_DEMUXALL || Method_Flag == AUDIO_DEMUX) && Start_Flag &&
tp.pid && (tp.pid == MPEG2_Transport_AudioPID) &&
(MPEG2_Transport_AudioType == 3 || MPEG2_Transport_AudioType == 4 || MPEG2_Transport_AudioType == 0xffffffff))
{
// Both MPEG and AAC audio come here. The sync word will be checked to
// distinguish between them.
if (mpafp)
{
if (tp.payload_unit_start_indicator)
{
Get_Short(); // start code and stream id
Get_Short();
Get_Short(); // packet length
Get_Byte(); // flags
code = Get_Byte(); // more flags
Packet_Header_Length = Get_Byte();
Packet_Length -= 9;
if (code & 0x80)
{
// unsigned int dts_stamp;
code = Get_Byte();
PES_PTS = (code & 0x0e) << 29;
PES_PTS |= (Get_Short() & 0xfffe) << 14;
PES_PTS |= (Get_Short()>>1) & 0x7fff;
AudioPTS = (unsigned int) (PES_PTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, " A00 PTS %u [%ums]\n", AudioPTS, AudioPTS/90);
Packet_Length -= 5;
#if 0
if ((code & 0xc0) == 0xc0)
{
PES_DTS = (Get_Byte() & 0x0e) << 29;
PES_DTS |= (Get_Short() & 0xfffe) << 14;
PES_DTS |= (Get_Short()>>1) & 0x7fff;
dts_stamp = (unsigned int) (PES_DTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, "A DTS %u [%ums]\n", dts_stamp, dts_stamp/90);
Packet_Length -= 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length - 10)
}
else
#endif
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length-5)
}
}
DEMUX_MPA_AAC(mpafp);
}
else if (tp.payload_unit_start_indicator)
{
Get_Short(); // start code
Get_Short(); // rest of start code and stream id
Get_Short(); // packet length
Get_Byte(); // flags
code = Get_Byte(); // more flags
Packet_Header_Length = Get_Byte();
Packet_Length -= 9;
if (code & 0x80)
{
// unsigned int dts_stamp;
code = Get_Byte();
PES_PTS = (code & 0x0e) << 29;
PES_PTS |= (Get_Short() & 0xfffe) << 14;
PES_PTS |= (Get_Short()>>1) & 0x7fff;
AudioPTS = (unsigned int) (PES_PTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, " A00 PTS %u [%ums]\n", AudioPTS, AudioPTS/90);
Packet_Length = Packet_Length - 5;
#if 0
if ((code & 0xc0) == 0xc0)
{
PES_DTS = (Get_Byte() & 0x0e) << 29;
PES_DTS |= (Get_Short() & 0xfffe) << 14;
PES_DTS |= (Get_Short()>>1) & 0x7fff;
dts_stamp = (unsigned int) (PES_DTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, "A DTS %u [%ums]\n", dts_stamp, dts_stamp/90);
Packet_Length -= 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length - 10)
}
else
#endif
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length-5)
// Now we're at the start of the audio.
// Find the audio header.
code = Get_Byte();
code = ((code & 0xff) << 8) | Get_Byte();
Packet_Length -= 2;
while ((code & 0xfff8) != 0xfff8 && Packet_Length > 0)
{
emulated3:
code = ((code & 0xff) << 8) | Get_Byte();
Packet_Length--;
}
if ((code & 0xfff8) != 0xfff8)
{
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
continue;
}
// Found the audio header. Now check the layer field.
// For MPEG, layer is 1, 2, or 3. For AAC, it is 0.
// We demux the same for both; only the filename we create is different.
if (((code & 6) >> 1) == 0x00)
{
// AAC audio.
audio[0].type = FORMAT_AAC;
}
else
{
// MPEG audio.
// Try to detect emulated sync words by enforcing semantics.
if (check_audio_syncword(0, (code >> 1) & 3, (Rdptr[0] >> 4) & 0xf, (Rdptr[0] >> 2) & 3, (Rdptr[1] >> 6) & 3, Rdptr[1] & 3))
{
goto emulated3;
}
audio[0].type = FORMAT_MPA;
}
if (AudioOnly_Flag)
{
char *p;
strcpy(szOutput, Infilename[0]);
p = &szOutput[sizeof(szOutput)];
while (*p != '.') p--;
*p = 0;
if (audio[0].type == FORMAT_AAC)
sprintf(szBuffer, "%s PID %03x.aac", szOutput, MPEG2_Transport_AudioPID);
else
sprintf(szBuffer, "%s PID %03x %s %s %s %s.%s", szOutput, MPEG2_Transport_AudioPID,
MPALayer[audio[0].layer], MPAMode[audio[0].mode], MPASample[audio[0].sample],
MPARate[audio[0].layer][audio[0].rate],
UseMPAExtensions ? MPAExtension[0] : MPAExtension[audio[0].layer]);
}
else
{
// Adjust the VideoPTS to account for frame reordering.
if (!PTSAdjustDone)
{
PTSAdjustDone = 1;
picture_period = 1.0 / frame_rate;
VideoPTS -= (int) (LeadingBFrames * picture_period * 90000);
}
if (PTSDifference(AudioPTS, VideoPTS, &PTSDiff))
{
if (audio[0].type == FORMAT_AAC)
sprintf(szBuffer, "%s PID %03x.aac", szOutput, MPEG2_Transport_AudioPID);
else
sprintf(szBuffer, "%s PID %03x %s %s %s %s.%s", szOutput, MPEG2_Transport_AudioPID,
MPALayer[audio[0].layer], MPAMode[audio[0].mode], MPASample[audio[0].sample],
MPARate[audio[0].layer][audio[0].rate],
UseMPAExtensions ? MPAExtension[0] : MPAExtension[audio[0].layer]);
}
else
{
if (audio[0].type == FORMAT_AAC)
sprintf(szBuffer, "%s PID %03x DELAY %dms.aac", szOutput, MPEG2_Transport_AudioPID, PTSDiff);
else
sprintf(szBuffer, "%s PID %03x %s %s %s %s DELAY %dms.%s", szOutput, MPEG2_Transport_AudioPID,
MPALayer[audio[0].layer], MPAMode[audio[0].mode], MPASample[audio[0].sample],
MPARate[audio[0].layer][audio[0].rate], PTSDiff,
UseMPAExtensions ? MPAExtension[0] : MPAExtension[audio[0].layer]);
}
}
Packet_Length += 2;
Rdptr -= 2;
if (D2V_Flag || AudioOnly_Flag)
{
mpafp = OpenAudio(szBuffer, "wb", 0);
if (mpafp == NULL)
{
// Cannot open the output file, Disable further audio processing.
MPEG2_Transport_AudioType = 0xff;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
continue;
}
DEMUX_MPA_AAC(mpafp);
}
}
}
if (AudioOnly_Flag && Info_Flag && !(AudioPktCount++ % 128))
UpdateInfo();
}
else if ((Method_Flag == AUDIO_DEMUXALL || Method_Flag == AUDIO_DEMUX) && Start_Flag &&
tp.pid && (tp.pid == MPEG2_Transport_AudioPID) && (MPEG2_Transport_AudioType == 0x80))
{
if (pcmfp)
{
if (tp.payload_unit_start_indicator)
{
Get_Short(); // start code and stream id
Get_Short();
Get_Short(); // packet length
Get_Byte(); // flags
code = Get_Byte(); // more flags
Packet_Header_Length = Get_Byte();
Packet_Length -= 9;
if (code & 0x80)
{
code = Get_Byte();
PES_PTS = (code & 0x0e) << 29;
PES_PTS |= (Get_Short() & 0xfffe) << 14;
PES_PTS |= (Get_Short()>>1) & 0x7fff;
AudioPTS = (unsigned int) (PES_PTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, " A00 PTS %u [%ums]\n", AudioPTS, AudioPTS/90);
Packet_Length -= 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length-5)
}
// Now we're at the start of the audio.
code = Get_Byte();
code = Get_Byte();
audio[0].format_m2ts = Get_Short();
Packet_Length -= 4;
}
DEMUX_MPA_AAC(pcmfp);
}
else if (tp.payload_unit_start_indicator)
{
audio[0].type = FORMAT_LPCM_M2TS;
strcpy(ext, "pcm");
strcpy(EXT, "pcm");
_strupr(EXT);
Get_Short(); // start code
Get_Short(); // rest of start code and stream id
Get_Short(); // packet length
Get_Byte(); // flags
code = Get_Byte(); // more flags
Packet_Header_Length = Get_Byte();
Packet_Length -= 9;
if (code & 0x80)
{
code = Get_Byte();
PES_PTS = (code & 0x0e) << 29;
PES_PTS |= (Get_Short() & 0xfffe) << 14;
PES_PTS |= (Get_Short()>>1) & 0x7fff;
AudioPTS = (unsigned int) (PES_PTS & 0xffffffff);
if (LogTimestamps_Flag && D2V_Flag && StartLogging_Flag)
fprintf(Timestamps, " A00 PTS %u [%ums]\n", AudioPTS, AudioPTS/90);
Packet_Length = Packet_Length - 5;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Header_Length-5)
// Now we're at the start of the audio.
code = Get_Byte();
code = Get_Byte();
audio[0].format_m2ts = Get_Short();
Packet_Length -= 4;
if (AudioOnly_Flag)
{
char *p;
strcpy(szOutput, Infilename[0]);
p = &szOutput[sizeof(szOutput)];
while (*p != '.') p--;
*p = 0;
sprintf(szBuffer, "%s %s PID %03x.%s", szOutput, EXT, MPEG2_Transport_AudioPID, ext);
}
else
{
// Adjust the VideoPTS to account for frame reordering.
if (!PTSAdjustDone)
{
PTSAdjustDone = 1;
picture_period = 1.0 / frame_rate;
VideoPTS -= (int) (LeadingBFrames * picture_period * 90000);
}
if (PTSDifference(AudioPTS, VideoPTS, &PTSDiff))
sprintf(szBuffer, "%s %s PID %03x.%s",
szOutput, EXT, MPEG2_Transport_AudioPID, ext);
else
sprintf(szBuffer, "%s %s PID %03x DELAY %dms.%s",
szOutput, EXT, MPEG2_Transport_AudioPID, PTSDiff, ext);
}
if (D2V_Flag || AudioOnly_Flag)
{
pcmfp = OpenAudio(szBuffer, "wb", 0);
if (pcmfp == NULL)
{
// Cannot open the output file, Disable further audio processing.
MPEG2_Transport_AudioType = 0xff;
SKIP_TRANSPORT_PACKET_BYTES(Packet_Length);
continue;
}
DEMUX_MPA_AAC(pcmfp);
}
}
}
if (AudioOnly_Flag && Info_Flag && !(AudioPktCount++ % 128))
UpdateInfo();
}
else if (tp.pid && tp.pid == MPEG2_Transport_AudioPID && (MPEG2_Transport_AudioType == 0x81))
{
// We are demuxing AC3 audio.
// search for an MPEG-PES packet header
if (tp.random_access_indicator || tp.payload_unit_start_indicator )
{
LOCATE
code = Get_Short();
code = (code & 0xffff)<<16 | Get_Short();
Packet_Length = Packet_Length - 4; // remove these two bytes