-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMPEG.Mod
executable file
·2547 lines (2195 loc) · 75.3 KB
/
MPEG.Mod
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
MODULE MPEG;
IMPORT
SYSTEM, Codec := Codecs, Raster, Streams, KernelLog, Files, WMGraphics, MPEGTables,
WM := WMWindowManager, Rectangles := WMRectangles, Kernel, Commands,
Util := MPEGUtilities, Base;
CONST
(* Video start codes in numeric order *)
SCPicture* = CHR(000H);
(* CHR(001H)
:
CHR(0AFH) are slice start codes
CHR(0B0H) and
CHR(0B1H) are reserved *)
SCUserData* = CHR(0B2H);
SCSequenceHeader* = CHR(0B3H);
SCSequenceError* = CHR(0B4H);
SCExtension* = CHR(0B5H);
(* CHR(0B6H) is reserved *)
SCSequenceEnd* = CHR(0B7H);
SCGOP* = CHR(0B8H);
(* System start codes in numeric order *)
SCSystemEnd* = CHR(0B9H);
SCPack* = CHR(0BAH);
SCSystemHeader* = CHR(0BBH);
SCReservedStream* = CHR(0BCH);
SCPrivateStream* = CHR(0BDH);
SCPaddingStream* = CHR(0BEH);
SCPrivateStream2* = CHR(0BFH);
(* CHR(0C0H)
:
CHR(0DFH) are audio streams 0..31
CHR(0E0H)
:
CHR(0EFH) are video streams 0..15
CHR(0F0H)
:
CHR(0FFH) are reserved streams 0..15 *)
(* Picture Structures (MPEG-2 only) *)
PicStructReserved* = 0;
PicStructTopField* = 1;
PicStructBottomField* = 2;
PicStructFrame* = 3;
(* Frame Motion Types *)
FMTReserved* = 0;
FMTField* = 1;
FMTFrame* = 2;
FMTDualPrime* = 3;
(* index in MotionVectorInfos *)
forward = 0;
backward = 1;
horizontal = 0;
vertical = 1;
mv1 = 0; (* first motion vector *)
mv2 = 1; (* second motion vector (MPEG-1 always uses just the first one) *)
TYPE
Name=Base.Name;
(* required by the demultiplexer to keep track of its streams *)
StreamType = RECORD
stream*: Codec.DemuxStream;
idByte*: CHAR;
pos: LONGINT;
bytesLeftInPacket: LONGINT;
eos: BOOLEAN; (* end of stream *)
END;
(* Window of a very (!) simple stand-alone player *)
TYPE PW* = OBJECT(WM.BufferWindow)
PROCEDURE & InitNew*(w, h:LONGINT; alpha:BOOLEAN);
BEGIN
Init(w, h, alpha);
manager := WM.GetDefaultManager();
WM.DefaultAddWindow(SELF);
END InitNew;
(* Overwrite draw procedure because we do not want any interpolation *)
PROCEDURE Draw*(canvas : WMGraphics.Canvas; w, h, q : LONGINT);
BEGIN
Draw^(canvas, w, h, 0)
END Draw;
PROCEDURE Close;
BEGIN
Close^;
END Close;
END PW;
(* Decoder for an MPEG Video Sequence *)
Decoder* = OBJECT(Codec.VideoDecoder)
VAR
(* Video Information *)
videoWidth, videoHeight: LONGINT;
videoWidthDiv2, videoHeightDiv2: LONGINT;
videoWidthDiv16, videoHeightDiv16: LONGINT;
aspectRatioIndex, frameRateIndex: LONGINT;
bitRate: LONGINT;
stream*: Util.BitStream; (* the stream we read from *)
reader: Util.StreamReader; (* allows to read VLCs and other information from the stream *)
idct: Util.IDCT; (* performs the iDCT *)
yuv2rgb: Util.ColorSpace; (* performs the colorspace transformation YUV -> RGB *)
dequantizer: Util.Dequantizer; (* performs the dequantization of intra and non-intra quantizer matrices *)
blocks: Util.BlockActions; (* required for motion compensation *)
intraQM: Util.PointerToArrayOfLONGINT; (* intra quantizer matrix *)
nonintraQM: Util.PointerToArrayOfLONGINT; (* non-intra quantizer matrix *)
curFrame: Util.Frame; (* the dequantized and iDCT'ed YUV values *)
prevRef, nextRef: Util.Frame; (* previous and next reference picture *)
nextFrameToRender: Util.Frame; (* the next frame ready to be rendered *)
mvinfos: Util.MotionVectorInfos; (* everything that is somehow connected to motion vectors *)
frameNr: LONGINT; (* number of the current frame (restarts for each GOP) *)
realFrameNr: LONGINT; (* number of the current frame (restarts at the beginning of the movie) *)
time: LONGINT; (* current time in milliseconds *)
mspf: LONGINT; (* milliseconds per frame *)
hasMoreFrames*: BOOLEAN; (* FALSE at the end of a video sequence *)
(* MPEG-2 stuff *)
MPEG2: BOOLEAN; (* TRUE -> MPEG-2; FALSE -> MPEG-1 *)
MainProfile: BOOLEAN; (* TRUE -> Main Profile; FALSE -> Simple Profile *)
LevelID: LONGINT; (* 1 -> Low, 2 -> Main, 3 -> High1440, 4 -> High *)
ChromaFormat: LONGINT; (* 1 -> 4:2:0, 2 -> 4:2:2, 3-> 4:4:4 *)
picExt: Util.PicCodingExt; (* some infos about the picture *)
(* less important VARs *)
mbSkipped: BOOLEAN; (* TRUE if last macroblock was skipped (required for DC prediction) *)
dcY, dcCb, dcCr: LONGINT; (* DC coefficient prediction for Y, Cb and Cr blocks *)
mbMotionForwOld, mbMotionBackOld: BOOLEAN; (* required for skipped macroblocks of B-Frames *)
mbIntraOld: BOOLEAN;
(* local in picture *)
mbAddress: LONGINT; (* Address of the current macroblock *)
mbAddressLast: LONGINT; (* Address of the last coded macroblock *)
mbAddressLastIntra: LONGINT;
macroblockNr: INTEGER; (* number of the macroblock currently decoded (in slice) *)
(* local in macroblock *)
frameMotionType: LONGINT;
dctType: BOOLEAN;
block: Util.PointerToArrayOfLONGINT; (* current block of a macroblock *)
frametype: LONGINT;
loop*:BOOLEAN;
(* Constructor *)
PROCEDURE &Init*;
VAR
i: SHORTINT;
BEGIN
NEW(idct);
NEW(yuv2rgb);
NEW(dequantizer);
NEW(picExt);
NEW(mvinfos);
NEW(blocks);
NEW(block, 64);
hasMoreFrames := TRUE;
realFrameNr := -1;
(* init QMs with default values *)
NEW(intraQM, 64);
NEW(nonintraQM, 64);
FOR i := 0 TO 63 DO
intraQM[i] := MPEGTables.IQM[i];
nonintraQM[i] := 16;
END;
END Init;
(* Open a video stream by reading the sequence header *)
PROCEDURE Open*(in : Streams.Reader; VAR res : LONGINT);
VAR
marker: CHAR;
BEGIN
res := Codec.ResFailed;
IF ~(in IS Codec.DemuxStream) THEN RETURN END;
NEW(stream, in(Codec.DemuxStream));
NEW(reader, stream);
frameNr := -1;
(* read (next) start code *)
IF ~GotoNextMarker(stream, marker) THEN
(* stream does not start with a startcode *)
KernelLog.String("this is not a legal MPEG video stream (no startcode found)"); KernelLog.Ln;
RETURN;
END;
(* check if startcode is legal *)
IF marker # SCSequenceHeader THEN
IF marker = CHR(0BAH) THEN
KernelLog.String("This is a multiplexed (audio & video) MPEG stream. Use the demultiplexer."); KernelLog.Ln;
ELSE
(* video sequence must start with 00 00 01 B3 *)
KernelLog.String("This is not a valid Video Stream (Marker="); KernelLog.Hex(ORD(marker), -1);
KernelLog.String(")"); KernelLog.Ln;
END;
RETURN;
END;
(* skip the startcode *)
stream.SkipBits(32);
IF ParseSequenceHeader() THEN
(* create image buffers *)
videoWidthDiv2 := videoWidth DIV 2;
videoHeightDiv2 := videoHeight DIV 2;
videoWidthDiv16 := videoWidth DIV 16;
videoHeightDiv16 := videoHeight DIV 16;
NEW(curFrame);
NEW(curFrame.buffer, videoHeight * videoWidth + 2 * (videoHeightDiv2 * videoWidthDiv2));
curFrame.cbOffset := videoHeight * videoWidth;
curFrame.crOffset := videoHeight * videoWidth + videoHeightDiv2 * videoWidthDiv2;
curFrame.frameNr := -1;
NEW(prevRef);
NEW(prevRef.buffer, videoHeight * videoWidth + 2 * (videoHeightDiv2 * videoWidthDiv2));
prevRef.cbOffset := videoHeight * videoWidth;
prevRef.crOffset := videoHeight * videoWidth + videoHeightDiv2 * videoWidthDiv2;
prevRef.frameNr := -1;
NEW(nextRef);
NEW(nextRef.buffer, videoHeight * videoWidth + 2 * (videoHeightDiv2 * videoWidthDiv2));
nextRef.cbOffset := videoHeight * videoWidth;
nextRef.crOffset := videoHeight * videoWidth + videoHeightDiv2 * videoWidthDiv2;
nextRef.frameNr := -1;
res := Codec.ResOk;
RETURN;
ELSE
KernelLog.String("Failed parsing (first) Sequence Header"); KernelLog.Ln;
END;
END Open;
PROCEDURE HasMoreData*(): BOOLEAN;
BEGIN
RETURN hasMoreFrames;
END HasMoreData;
PROCEDURE ParseSequenceHeader(): BOOLEAN;
VAR
marker: CHAR;
BEGIN
videoWidth := stream.GetBits(12);
videoHeight := stream.GetBits(12);
(* we just extend the video to a multiple of 16. not perfect, but it works... *)
IF videoWidth MOD 16 # 0 THEN
videoWidth := ((videoWidth DIV 16) + 1) * 16;
END;
IF videoHeight MOD 16 # 0 THEN
videoHeight := ((videoHeight DIV 16) + 1) * 16;
END;
aspectRatioIndex := stream.GetBits(4);
frameRateIndex := stream.GetBits(4);
CASE frameRateIndex OF
1: mspf := 42 (* 23.976 fps -> 41.70837... *)
| 2: mspf := 42 (* 24 fps -> 41.66666... *)
| 3: mspf := 40 (* 25 fps -> 40.00000... *)
| 4: mspf := 33 (* 29.97 fps -> 33.36670... *)
| 5: mspf := 33 (* 30 fps -> 33.33333... *)
| 6: mspf := 20 (* 50 fps -> 20.00000... *)
| 7: mspf := 17 (* 59.94 fps -> 16.68335... *)
| 8: mspf := 17 (* 60 fps -> 16.66666... *)
ELSE
mspf := 40; (* illegal framerate, just assume something *)
KernelLog.String("Unknown Framerate Index: "); KernelLog.Int(frameRateIndex, 0); KernelLog.Ln;
END;
bitRate := stream.GetBits(18);
stream.SkipBits(1); (* marker bit *)
stream.SkipBits(10); (* vbv buffer size *)
stream.SkipBits(1); (* constrained bit *)
IF stream.GetBits(1) = 1 THEN
(* intra quantizer matrix coming... *)
reader.ReadQuantizerMatrix(intraQM);
IF reader.eof THEN RETURN FALSE END;
END;
IF stream.GetBits(1) = 1 THEN
(* non-intra quantizer matrix coming *)
reader.ReadQuantizerMatrix(nonintraQM);
IF reader.eof THEN RETURN FALSE END;
END;
IF ~stream.HasMoreData() THEN hasMoreFrames := FALSE; RETURN FALSE END;
IF ~GotoNextMarker(stream, marker) THEN
RETURN FALSE;
END;
(* read extension block(s) if present *)
IF marker = SCExtension THEN
(* This is an MPEG-2 stream! *)
MPEG2 := TRUE;
REPEAT
IF marker = SCExtension THEN
stream.SkipBits(32);
IF ~ReadExtension() THEN RETURN FALSE END;
IF ~GotoNextMarker(stream, marker) THEN RETURN FALSE END;
ELSE
(* skip user data - they are unimportant for the decoder *)
stream.SkipBits(32);
IF ~GotoNextMarker(stream, marker) THEN RETURN FALSE END;
END;
UNTIL (marker # SCExtension) & (marker # SCUserData);
ELSE
(* This is an MPEG-1 stream! *)
MPEG2 := FALSE;
WHILE marker = SCUserData DO
stream.SkipBits(32);
IF ~GotoNextMarker(stream, marker) THEN
RETURN FALSE;
END;
END;
END;
RETURN TRUE;
END ParseSequenceHeader;
(* Read an extension. It is assumed that the stream is currently at the end of an extension start code *)
PROCEDURE ReadExtension(): BOOLEAN;
VAR
fourbits: LONGINT;
tmp: BOOLEAN;
BEGIN
fourbits := stream.GetBits(4);
CASE fourbits OF
0, 5, 6, 9..15:
(* not supported by MP@ML or not defined by the standard *)
KernelLog.String("Extension not supported: "); KernelLog.Int(stream.ShowBits(4), 0); KernelLog.Ln;
RETURN FALSE;
| 1: (* sequence extension *)
tmp := reader.ReadSequenceExtension(MainProfile, LevelID, ChromaFormat, videoWidth, videoHeight);
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
| 2: (* sequence display extension *)
tmp := reader.ReadSequenceDisplayExtension();
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
| 3: (* quant matrix extension *)
tmp := reader.ReadQuantMatrixExtension();
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
| 4: (* copyright extension *)
tmp := reader.ReadCopyrightExtension();
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
| 7: (* picture display extension *)
tmp := reader.ReadPictureDisplayExtension();
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
| 8: (* picture coding extension *)
tmp := reader.ReadPictureCodingExtension(picExt, mvinfos);
IF reader.eof THEN
RETURN FALSE;
ELSE
RETURN tmp;
END;
ELSE
hasMoreFrames := FALSE;
RETURN FALSE;
END;
(* we can't come here... *)
RETURN FALSE;
END ReadExtension;
(* parse an SMTPE timecode (25 bits) *)
PROCEDURE ReadTimecode;
VAR
h, min, sec, frames: LONGINT;
BEGIN
stream.SkipBits(1);
h := stream.GetBits(5);
min := stream.GetBits(6);
stream.SkipBits(1);
sec := stream.GetBits(6);
frames := stream.GetBits(6);
(* the timecode may not be used for seeking because it does not
always start at 0:00:00.00 for all movies *)
(*
KernelLog.String("Timecode: ");
KernelLog.Int(h, 0); KernelLog.String(":");
KernelLog.Int(min, 0); KernelLog.String(":");
KernelLog.Int(sec, 0); KernelLog.String(".");
KernelLog.Int(frames, 0); KernelLog.Ln;
*)
IF ~stream.HasMoreData() THEN
hasMoreFrames := FALSE;
END;
END ReadTimecode;
(* return some information about the video stream *)
PROCEDURE GetVideoInfo*(VAR width, height, millisecondsPerFrame : LONGINT);
BEGIN
width := videoWidth;
height := videoHeight;
millisecondsPerFrame := mspf;
END GetVideoInfo;
PROCEDURE CanSeek*() : BOOLEAN;
BEGIN
RETURN TRUE
END CanSeek;
PROCEDURE GetCurrentFrame*() : LONGINT;
BEGIN
RETURN realFrameNr;
END GetCurrentFrame;
PROCEDURE GetCurrentTime*() : LONGINT;
BEGIN
RETURN time;
END GetCurrentTime;
PROCEDURE SeekFrame*(frame : LONGINT; goKeyFrame : BOOLEAN; VAR res : LONGINT);
VAR
i: LONGINT;
code: CHAR;
lastIntraPosOld, lastIntraNrOld, lastIntraPos, lastIntraNr, lastFramePos: LONGINT;
nrB, nrBOld: LONGINT;
countB: BOOLEAN;
type: LONGINT;
BEGIN
res := Codec.ResFailed;
(* start at the beginning *)
stream.Reset;
IF ~GotoNextMarker(stream, code) THEN RETURN END;
stream.SkipBits(32);
IF ~ParseSequenceHeader() THEN RETURN END;
lastIntraPos := stream.Pos();
lastFramePos := stream.Pos();
(* to be sure, we need to decode the last 2 i-frames. consider (decoding!) order IBBPBBIB *)
FOR i := 0 TO frame DO
(* skip frame i *)
type := SkipNext();
IF type = 1 THEN
(* skipped frame was an I-frame *)
lastIntraPosOld := lastIntraPos;
lastIntraNrOld := lastIntraNr;
lastIntraPos := lastFramePos;
lastIntraNr := i;
countB := TRUE;
nrBOld := nrB;
nrB := 0;
ELSE
IF countB THEN
IF type = 3 THEN
INC(nrB);
ELSE
countB := FALSE;
END;
END;
END;
lastFramePos := stream.Pos();
END;
(* jump to the second last I-frame *)
stream.SetPos(lastIntraPosOld);
realFrameNr := lastIntraNrOld;
frameNr := 10000; (* expected frameNr > tempRef => frameNr gets adjusted *)
Next();
FOR i := 1 TO nrBOld DO
type := SkipNext();
END;
DEC(frameNr, nrBOld);
FOR i := lastIntraNrOld+1 TO lastIntraNr-1 DO
Next();
END;
IF ~goKeyFrame THEN
FOR i := lastIntraNr TO frame DO
Next();
END;
END;
res := Codec.ResOk;
END SeekFrame;
PROCEDURE SeekMillisecond*(millisecond : LONGINT; goKeyFrame : BOOLEAN; VAR res : LONGINT);
VAR
newframe: LONGINT;
BEGIN
newframe := millisecond DIV mspf;
SeekFrame(newframe, goKeyFrame, res);
time := newframe * mspf;
END SeekMillisecond;
(* skips one frame *)
PROCEDURE SkipNext(): LONGINT;
VAR
marker: CHAR;
picType: LONGINT; (* 1=I-, 2=P-, 3=B-, 4=D-Picture, other values are illegal *)
tempRef: LONGINT; (* temporal reference *)
nextCode: LONGINT;
tmpFrame: Util.Frame; (* required to switch two frames *)
BEGIN
IF ~hasMoreFrames THEN RETURN -1 END;
INC(frameNr);
INC(realFrameNr);
IF frameNr = nextRef.frameNr THEN
(* we have already decoded this frame, just take it from the nextRef buffer *)
tmpFrame := curFrame;
curFrame := nextRef;
nextRef := tmpFrame;
ELSE
(* decode one or two frames *)
REPEAT
mbAddress := -1;
mbAddressLast := -1;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
WHILE marker # SCPicture DO
IF marker = SCSequenceHeader THEN
stream.SkipBits(32);
IF ~ParseSequenceHeader() THEN
hasMoreFrames := FALSE;
RETURN -1;
END;
ELSIF marker = SCGOP THEN
stream.SkipBits(32);
ReadTimecode; (* SMPTE Timecode *)
stream.SkipBits(1); (* closed GOP -> if closed wipe out prev and next buffer (black) *)
stream.SkipBits(1); (* broken link *)
stream.SkipBits(5); (* not used, should be 0... *)
(* temporal reference restarts at zero *)
frameNr := 0;
prevRef.frameNr := -1; (* make sure they are at most used as reference *)
nextRef.frameNr := -1; (* make sure they are at most used as reference *)
ELSE
KernelLog.String("Unexpected marker found: "); KernelLog.Hex(ORD(marker), -1); KernelLog.Ln;
RETURN -1;
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
END;
(* parse picture header *)
stream.SkipBits(32);
tempRef := stream.GetBits(10); (* temporal reference *)
curFrame.frameNr := tempRef;
picType := stream.GetBits(3); (* the picture type (I, P, B or D) *)
frametype := picType;
curFrame.picType := picType;
stream.SkipBits(16); (* vbv buffer delay -> not relevant for us *)
IF (picType = 2) OR (picType = 3) THEN
stream.SkipBits(4);
END;
IF picType = 3 THEN
stream.SkipBits(4);
END;
WHILE stream.ShowBits(1) = 1 DO
stream.SkipBits(1); (* extra information follows *)
stream.SkipBits(8); (* undefined by the standard *)
END;
stream.SkipBits(1); (* skip '0' marker bit *)
IF ~stream.HasMoreData() THEN
hasMoreFrames := FALSE;
RETURN -1;
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
(* read extension data if present *)
IF marker = SCExtension THEN
IF ~MPEG2 THEN
HALT(1234);
END;
stream.SkipBits(32);
IF stream.ShowBits(4) # 8 THEN
RETURN -1;
ELSE
stream.SkipBits(4);
END;
IF ~reader.ReadPictureCodingExtension(picExt, mvinfos) THEN RETURN -1 END;
IF reader.eof THEN hasMoreFrames := FALSE; RETURN 0 END;
IF picExt.framePredFrameDct THEN frameMotionType := FMTFrame END;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
ELSIF MPEG2 THEN
(* MPEG-2 requires a picture extension ! *)
KernelLog.String("MPEG-2 picture extension not found"); KernelLog.Ln;
HALT(1234);
END;
(* read user data if present *)
IF marker = SCUserData THEN
stream.SkipBits(32);
WHILE stream.ShowBits(24) # 1 DO
stream.SkipBits(8);
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN -1 END;
END;
(* now we are really ready to decode the picture *)
IF (picType = 1) OR (picType = 2) OR (picType = 3) THEN
REPEAT
(* Skip Slice *)
stream.SkipBits(32);
WHILE stream.ShowBits(24) # 1 DO
stream.SkipBits(8);
END;
nextCode := stream.ShowBits(32);
UNTIL ~(nextCode > 100H) & (nextCode <= 1AFH);
ELSIF picType = 4 THEN
(* D-Picture *)
RETURN -1; (* D-Pictures not supported *)
ELSE
(* illegal or reserved value *)
RETURN -1;
END;
IF tempRef > frameNr THEN
(* dont display the frame yet - it is a future reference for upcoming B-Pics *)
tmpFrame := nextRef;
nextRef := curFrame;
curFrame := tmpFrame;
END;
UNTIL tempRef <= frameNr;
END;
(* now we are sure that the correct frame is in the curFrame buffer *)
(* store I- and P-Pictures as prediction for further pics *)
IF (curFrame.picType = 1) OR (curFrame.picType = 2) THEN
tmpFrame := prevRef;
prevRef := curFrame;
curFrame := tmpFrame;
nextFrameToRender := prevRef;
ELSE
nextFrameToRender := curFrame;
END;
RETURN picType;
END SkipNext;
(* Prepare the next frame *)
PROCEDURE Next*;
VAR
marker: CHAR;
picType: LONGINT; (* 1=I-, 2=P-, 3=B-, 4=D-Picture, other values are illegal *)
tempRef: LONGINT; (* temporal reference *)
res: BOOLEAN;
nextCode,result: LONGINT;
tmpFrame: Util.Frame; (* required to switch two frames *)
BEGIN
IF ~hasMoreFrames THEN
loop:=TRUE;
KernelLog.String("LOOPING ");
RETURN
END;
INC(frameNr);
INC(realFrameNr);
INC(time, mspf);
IF frameNr = nextRef.frameNr THEN
(* we have already decoded this frame, just take it from the nextRef buffer *)
tmpFrame := curFrame;
curFrame := nextRef;
nextRef := tmpFrame;
ELSE
(* decode one or two frames *)
REPEAT
mbAddress := -1;
mbAddressLast := -1;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
WHILE marker # SCPicture DO
IF marker = SCSequenceHeader THEN
stream.SkipBits(32);
IF ~ParseSequenceHeader() THEN
hasMoreFrames := FALSE;
RETURN END;
ELSIF marker = SCGOP THEN
stream.SkipBits(32);
ReadTimecode; (* SMPTE Timecode *)
stream.SkipBits(1); (* closed GOP -> if closed wipe out prev and next buffer (black) *)
stream.SkipBits(1); (* broken link *)
stream.SkipBits(5); (* not used, should be 0... *)
(* temporal reference restarts at zero *)
frameNr := 0;
prevRef.frameNr := -1; (* make sure they are at most used as reference *)
nextRef.frameNr := -1; (* make sure they are at most used as reference *)
ELSIF marker = SCSequenceEnd THEN
(* video sequence finished - there are no more frames to be decoded *)
KernelLog.String("LOOPING ");
SeekFrame(1,TRUE,result);
RETURN;
ELSE
KernelLog.String("Unexpected marker found: "); KernelLog.Hex(ORD(marker), -1); KernelLog.Ln;
RETURN;
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
END;
(* parse picture header *)
stream.SkipBits(32);
tempRef := stream.GetBits(10); (* temporal reference *)
curFrame.frameNr := tempRef;
picType := stream.GetBits(3); (* the picture type (I, P, B or D) *)
frametype := picType;
curFrame.picType := picType;
stream.SkipBits(16); (* vbv buffer delay -> not relevant for us *)
IF tempRef < frameNr THEN
frameNr := tempRef;
END;
IF (picType = 2) OR (picType = 3) THEN
IF stream.ShowBits(1) = 1 THEN
mvinfos.fullPel[mv1][forward] := TRUE;
ELSE
mvinfos.fullPel[mv1][forward] := FALSE;
END;
stream.SkipBits(1);
mvinfos.fCode[mv1][forward] := stream.GetBits(3);
mvinfos.rSize[mv1][forward] := mvinfos.fCode[mv1][forward] - 1;
mvinfos.f[mv1][forward] := SYSTEM.VAL(LONGINT, {mvinfos.rSize[mv1][forward]}); (* 2 ^ rSize *)
END;
IF picType = 3 THEN
IF stream.ShowBits(1) = 1 THEN
mvinfos.fullPel[mv1][backward] := TRUE;
ELSE
mvinfos.fullPel[mv1][backward] := FALSE;
END;
stream.SkipBits(1);
mvinfos.fCode[mv1][backward] := stream.GetBits(3);
mvinfos.rSize[mv1][backward] := mvinfos.fCode[mv1][backward] - 1;
mvinfos.f[mv1][backward] := SYSTEM.VAL(LONGINT, {mvinfos.rSize[mv1][backward]}); (* 2 ^ rSize *)
END;
WHILE stream.ShowBits(1) = 1 DO
stream.SkipBits(1); (* extra information follows *)
stream.SkipBits(8); (* undefined by the standard *)
END;
stream.SkipBits(1); (* skip '0' marker bit *)
IF ~stream.HasMoreData() OR reader.eof THEN
hasMoreFrames := FALSE;
RETURN;
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
(* read extension data if present *)
IF marker = SCExtension THEN
IF ~MPEG2 THEN
HALT(1234);
END;
stream.SkipBits(32);
IF stream.ShowBits(4) # 8 THEN
RETURN;
ELSE
stream.SkipBits(4);
END;
IF ~reader.ReadPictureCodingExtension(picExt, mvinfos) THEN RETURN END;
IF reader.eof THEN hasMoreFrames := FALSE; RETURN END;
IF picExt.framePredFrameDct THEN frameMotionType := FMTFrame END;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
ELSIF MPEG2 THEN
(* MPEG-2 requires a picture extension ! *)
KernelLog.String("MPEG-2 picture extension not found"); KernelLog.Ln;
HALT(1234);
END;
(* read user data if present *)
IF marker = SCUserData THEN
stream.SkipBits(32);
WHILE stream.ShowBits(24) # 1 DO
stream.SkipBits(8);
END;
IF ~GotoNextMarker(stream, marker) THEN RETURN END;
END;
(* now we are really ready to decode the picture *)
IF (picType = 1) OR (picType = 2) OR (picType = 3) THEN
REPEAT
res := DecodeSlice(picType);
IF ~res THEN
hasMoreFrames:=FALSE;
RETURN
END;
nextCode := stream.ShowBits(32);
UNTIL ~(res & (nextCode > 100H) & (nextCode <= 1AFH));
ELSIF picType = 4 THEN
(* D-Picture *)
RETURN; (* D-Pictures not supported *)
ELSE
(* illegal or reserved value *)
RETURN;
END;
IF tempRef > frameNr THEN
(* dont display the frame yet - it is a future reference for upcoming B-Pics *)
tmpFrame := nextRef;
nextRef := curFrame;
curFrame := tmpFrame;
END;
UNTIL tempRef <= frameNr;
END;
(* now we are sure that the correct frame is in the curFrame buffer *)
(* store I- and P-Pictures as prediction for further pics *)
IF (curFrame.picType = 1) OR (curFrame.picType = 2) THEN
tmpFrame := prevRef;
prevRef := curFrame;
curFrame := tmpFrame;
nextFrameToRender := prevRef;
ELSE
nextFrameToRender := curFrame;
END;
END Next;
(* Decode one slice. Precondition: stream is positioned at the beginning of a slice *)
PROCEDURE DecodeSlice(type: LONGINT):BOOLEAN;
VAR
quantScale: LONGINT;
marker: CHAR;
BEGIN
(* re-init DC prediction *)
IF MPEG2 THEN
dcY := MPEGTables.DCP[picExt.dcPrecision];
dcCb := MPEGTables.DCP[picExt.dcPrecision];
dcCr := MPEGTables.DCP[picExt.dcPrecision];
ELSE
dcY := 8 * 128;
dcCb := 8 * 128;
dcCr := 8 * 128;
END;
(* re-init motion vector prediction *)
mvinfos.pmv[mv1][forward][horizontal] := 0;
mvinfos.pmv[mv1][forward][vertical] := 0;
mvinfos.pmv[mv1][backward][horizontal] := 0;
mvinfos.pmv[mv1][backward][vertical] := 0;
mvinfos.pmv[mv2][forward][horizontal] := 0;
mvinfos.pmv[mv2][forward][vertical] := 0;
mvinfos.pmv[mv2][backward][horizontal] := 0;
mvinfos.pmv[mv2][backward][vertical] := 0;
mvinfos.motionVerticalFieldSelect[mv1][forward] := FALSE;
mvinfos.motionVerticalFieldSelect[mv1][backward] := FALSE;
mvinfos.motionVerticalFieldSelect[mv2][forward] := TRUE;
mvinfos.motionVerticalFieldSelect[mv2][backward] := TRUE;
macroblockNr := 0;
stream.SkipBits(24);
mbAddress := ((stream.GetBits(8)-1) * videoWidthDiv16) - 1;
mbAddressLast := mbAddress;
quantScale := stream.GetBits(5);
IF quantScale < 0 THEN hasMoreFrames := FALSE; RETURN FALSE END;
IF MPEG2 THEN
(* translate qscalecode to qscale *)
IF picExt.qScaleType THEN
(* take from table 1 *)
quantScale := MPEGTables.QS1[quantScale];
ELSE
(* take from table 0 *)
quantScale := MPEGTables.QS0[quantScale];
END;
END;
(* extra slice information, not yet defined by the standard *)
WHILE stream.ShowBits(1) = 1 DO
stream.SkipBits(9);
END;
stream.SkipBits(1);
(* decode all macroblocks in this slice *)
WHILE stream.ShowBits(23) # 0 DO
IF ~DecodeMacroBlock(type, quantScale) THEN hasMoreFrames := FALSE; RETURN FALSE END;
END;
RETURN GotoNextMarker(stream, marker);
END DecodeSlice;
(* Decode one macroblock *)
PROCEDURE DecodeMacroBlock(type: LONGINT; VAR quantScale: LONGINT):BOOLEAN;
VAR
tmp: LONGINT;
cbp: LONGINT;
cbpBits: LONGINT;
mbIntra, mbPattern, mbMotionBack, mbMotionForw, mbQuant: BOOLEAN;
i: LONGINT;
offsetX, offsetY, offsetXDiv2, offsetYDiv2: LONGINT;
first: BOOLEAN; (* whether or not a block is the first one coded in a macroblock *)
fpmf, fpmb: LONGINT; (* full pel multiplier forward & backward *)
yoffs, yincr: LONGINT; (* parameters for CopyBlock - different for interlaced/non-interlaced blocks *)
BEGIN
INC(macroblockNr);
(* skip stuffing *)
WHILE stream.ShowBits(11) = 15 DO
stream.SkipBits(11);
END;
(* read the macroblock address *)
WHILE stream.ShowBits(11) = 8 DO
stream.SkipBits(11);
INC(mbAddress, 33);
END;
tmp := reader.ReadAddressIncrement();
IF reader.eof THEN hasMoreFrames := FALSE END;
IF tmp # -1 THEN
INC(mbAddress, tmp);
ELSE
RETURN FALSE;
END;
mbSkipped := (mbAddress - mbAddressLast) > 1;