forked from MakeMagazinDE/GRBLize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VFrames.pas
1050 lines (890 loc) · 31.8 KB
/
VFrames.pas
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
unit VFrames;
(******************************************************************************
VFrames.pas
Class TVideoImage
About
The TVideoImage class provides a simplified access to the class TVideoSample
from source unit VSample.pas.
It is used to access WebCams and similar Video-capture devices via DirectShow.
Its focus is on acquiring single images (frames) from the running video stream
sent by the cameras. There exist methods to control properties (e.g. size,
brightness etc.)
Acquisition usually is fast enough to simulate running video.
No audio support.
History
Version 1.6
2012-07-09
Support for 8-bit Grayscale images. Reduces time for image expansion for some types
of compressions. (But not for all, e.g. RGB!)
Some memory leaks fixed.
Version 1.5
GDI+ support for MJPG, if GDI+ available
YUY2 relaxed check of data size to support 1280*720 video size for Microsoft LifeCam Cinema
Version 1.4
Added support for YUY2 (YUYV, YUNV), MJPG, I420 (YV12, IYUV)
Version 1.3
07.09.2008
Added Video-Size and Video-property control
Added check for extreme CPU load
Version 1.2
30.08.2008
Added Pause and Resume
Version 1.1
26.07.2008
Contact:
michael@grizzlymotion.com
Copyright
For copyrights of the DirectX Header ports see the original source files.
Other code (unless stated otherwise, see comments): Copyright (C) M. Braun
Licence:
The lion share of this project lies within the ports of the DirectX header
files (which are under the Mozilla Public License Version 1.1), and the
original SDK sample files from Microsoft (END-USER LICENSE AGREEMENT FOR
MICROSOFT SOFTWARE DirectX 9.0 Software Development Kit Update (Summer 2003))
My own contribution compared to that work is very small (although it cost me
lots of time), but still is "significant enough" to fulfill Microsofts licence
agreement ;)
So I think, the ZLib licence (http://www.zlib.net/zlib_license.html)
should be sufficient for my code contributions.
Please note:
There exist much more complete alternatives (incl. sound, AVI etc.):
- DSPack (http://www.progdigy.com/)
- TVideoCapture by Egor Averchenkov (can be found at http://www.torry.net)
******************************************************************************)
interface
USES Windows, Messages, Controls, Forms, SysUtils, Graphics, Classes,
AppEvnts, MMSystem, DirectShow9, JPEG, UGDIPlus, Math,
VSample;
CONST
CBufferCnt = 3; // Triple-Buffer
TYPE
TNewVideoFrameEvent = procedure(Sender : TObject; Width, Height: integer; DataPtr: pointer) of object;
TVideoProperty = (VP_Brightness,
VP_Contrast,
VP_Hue,
VP_Saturation,
VP_Sharpness,
VP_Gamma,
VP_ColorEnable,
VP_WhiteBalance,
VP_BacklightCompensation,
VP_Gain);
TVideoImage = class
private
VideoSample : TVideoSample;
OnNewFrameBusy: boolean;
fVideoRunning : boolean;
fBusy : boolean;
fGray8Bit : boolean;
fSkipCnt : integer;
fFrameCnt : integer;
f30FrameTick : cardinal;
fFPS : double; // "Real" fps, even if not all frames will be displayed.
fWidth,
fHeight : integer;
fFourCC : cardinal;
fBitmap : TBitmap;
fBitmapGray : TBitmap;
fDisplayCanvas: TCanvas;
fImagePtr : ARRAY[0..CBufferCnt] OF pointer; // Local copy of image data
fImagePtrSize : ARRAY[0..CBufferCnt] OF integer;
fImagePtrIndex: integer;
fMessageHWND : HWND;
fMsgNewFrame : uint;
fOnNewFrame : TNewVideoFrameEvent;
AppEvent : TApplicationEvents;
IdleEventTick : cardinal;
ValueY_298,
ValueU_100,
ValueU_516,
ValueV_409,
ValueV_208 : ARRAY[byte] OF integer;
ValueL_255 : ARRAY[byte] OF byte;
ValueClip : ARRAY[-1023..1023] OF byte;
GrayConvR,
GrayConvG,
GrayConvB : ARRAY[0..255] OF integer;
fYUY2TablesPrepared : boolean;
JPG : TJPEGImage;
MemStream : TMemoryStream;
fImageUnpacked: boolean;
procedure PaintFrame;
procedure UnpackFrame(Size: integer; pData: pointer);
procedure WndProc(var Msg: TMessage);
function VideoSampleIsPaused: boolean;
procedure AppEventsIdle(Sender: TObject; var Done: Boolean);
procedure CallBack(pb : pbytearray; var Size: integer);
function TranslateProperty(const VP: TVideoProperty; VAR VPAP: TVideoProcAmpProperty): HResult;
PROCEDURE PrepareGrayBMP(VAR BM : TBitmap; W, H: integer);
PROCEDURE PrepareTables;
procedure YUY2_to_RGB(pData: pointer);
procedure YUY2_to_Gray8Bit(pData: pointer);
procedure I420_to_RGB(pData: pointer);
procedure I420_to_Gray8Bit(pData: pointer);
procedure RGB_to_Gray8Bit(pData: pointer);
public
constructor Create;
destructor Destroy; override;
procedure Free;
property IsPaused: boolean read VideoSampleIsPaused;
property VideoRunning : boolean read fVideoRunning;
property VideoWidth: integer read fWidth;
property VideoHeight: integer read fHeight;
property Gray8Bit: boolean read fGray8Bit write fGray8Bit;
property OnNewVideoFrame : TNewVideoFrameEvent read fOnNewFrame write fOnNewFrame;
property FramesPerSecond: double read fFPS;
property FramesSkipped: integer read fSkipCnt;
procedure GetListOfDevices(DeviceList: TStringList);
procedure VideoStop;
procedure VideoPause;
procedure VideoResume;
function VideoStart(DeviceName: string): integer;
procedure GetBitmap(BMP: TBitmap);
procedure SetDisplayCanvas(Canvas: TCanvas);
procedure ShowProperty;
procedure ShowProperty_Stream;
FUNCTION ShowVfWCaptureDlg: HResult;
procedure GetBrightnessSettings(VAR Actual: integer);
procedure SetBrightnessSettings(const Actual: integer);
PROCEDURE GetListOfSupportedVideoSizes(VidSize: TStringList);
PROCEDURE SetResolutionByIndex(Index: integer);
FUNCTION GetVideoPropertySettings( VP : TVideoProperty;
VAR MinVal, MaxVal,
StepSize, Default,
Actual : integer;
VAR AutoMode: boolean): HResult;
FUNCTION SetVideoPropertySettings(VP: TVideoProperty; Actual: integer; AutoMode: boolean): HResult;
PROCEDURE Convert24ToGray(BM24: TBitmap; BMGray: TBitmap);
end;
FUNCTION GetVideoPropertyName(VP: TVideoProperty): string;
// http://www.fourcc.org/yuv.php#UYVY
CONST
FourCC_YUY2 = $32595559;
FourCC_YUYV = $56595559;
FourCC_YUNV = $564E5559;
FourCC_MJPG = $47504A4D;
FourCC_I420 = $30323449;
FourCC_YV12 = $32315659;
FourCC_IYUV = $56555949;
implementation
FUNCTION GetVideoPropertyName(VP: TVideoProperty): string;
BEGIN
CASE VP OF
VP_Brightness : Result := 'Brightness';
VP_Contrast : Result := 'Contrast';
VP_Hue : Result := 'Hue';
VP_Saturation : Result := 'Saturation';
VP_Sharpness : Result := 'Sharpness';
VP_Gamma : Result := 'Gamma';
VP_ColorEnable : Result := 'ColorEnable';
VP_WhiteBalance : Result := 'WhiteBalance';
VP_BacklightCompensation: Result := 'Backlight';
VP_Gain : Result := 'Gain';
END; {case}
END;
(* Finally, callback seems to work. Previously it only ran for a few seconds.
The reason for that seemed to be a deadlock (see http://msdn.microsoft.com/en-us/library/ms786692(VS.85).aspx)
Now the image data is copied immediatly, and a message is sent to invoke the
display of the data. *)
procedure TVideoImage.CallBack(pb : pbytearray; var Size: integer);
var
i : integer;
T1 : cardinal;
begin
Inc(fFrameCnt);
// Calculate "Frames per second"...
T1 := TimeGetTime;
IF fFrameCnt mod 30 = 0 then
begin
if f30FrameTick > 0 then
fFPS := 30000 / (T1-f30FrameTick);
f30FrameTick := T1;
end;
// führt auf Windows 7 zu unendlich kleinen Frameraten! -cm
{
// Does the application run in unhealthy CPU usage?
// Check, if no idle event has occured for at least 1 sec.
// If so, skip current frame and give application time to "breathe".
IF Abs(T1-IdleEventTick) > 1000 then
begin
Inc(fSkipCnt);
exit;
end;
}
// Adjust pointer to image data if necessary
i := (fImagePtrIndex+1) mod CBufferCnt;
IF fImagePtrSize[i] <> Size then
begin
IF fImagePtrSize[i] > 0 then
FreeMem(fImagePtr[i], fImagePtrSize[i]);
fImagePtrSize[i] := Size;
GetMem(fImagePtr[i], fImagePtrSize[i]);
end;
// Save image data to local memory
move(pb^, fImagePtr[i]^, Size);
fImagePtrIndex := i;
fImageUnpacked := false;
// This routine is called by the video software and therefore runs within their thread.
// Posting a message to our own HWND will transport the information to the main thread.
PostMessage(fMessageHWND, fMsgNewFrame, Size, integer(fImagePtr[i]));
sleep(0);
end;
// Own windows message handler only to get the "New Video Frame has arrived" message.
// Used to get the information out of the Camera-Thread into the application's thread.
// Otherwise we would run into a deadlock.
procedure TVideoImage.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = fMsgNewFrame then
try
IF not fBusy then
begin
fBusy := true;
fImageUnpacked := false;
PaintFrame; // If a Display-Canvas has been set, paint video image on it.
IF assigned(fOnNewFrame) then
fOnNewFrame(self, fWidth, fHeight, fImagePtr[fImagePtrIndex]);
fBusy := false;
end
else Inc(fSkipCnt);
except
Application.HandleException(Self);
fBusy := false;
end
else Result := DefWindowProc(fMessageHWND, Msg, wParam, lParam);
end;
constructor TVideoImage.Create;
VAR
i : integer;
begin
inherited Create;
fVideoRunning := false;
OnNewFrameBusy := false;
fBitmap := TBitmap.Create;
fBitmapGray := TBitmap.Create;
fDisplayCanvas := nil;
fWidth := 0;
fHeight := 0;
fFourCC := 0;
FOR i := 0 TO CBufferCnt-1 DO
BEGIN
fImagePtr[i] := nil;
fImagePtrSize[i] := 0;
END;
fMsgNewFrame := wm_user+662;
fOnNewFrame := nil;
fBusy := false;
// Create a HWND that can capture some messages for us...
fMessageHWND := AllocateHWND(WndProc);
AppEvent := TApplicationEvents.Create(Application.MainForm);
AppEvent.OnIdle := AppEventsIdle;
JPG := TJPEGImage.Create;
// JPG.Performance := jpBestSpeed;
MemStream := TMemoryStream.Create;
fGray8Bit := false;
FOR i := 0 TO 255 DO
BEGIN
GrayConvR[i] := 100 * i;
GrayConvG[i] := 128 * i;
GrayConvB[i] := 28 * i +127;
END;
PrepareTables;
end;
// Check, when the last OnIdle message arrived. Save a time stamp.
// Used to check the CPU load. If necessary, we will skip video frames...
procedure TVideoImage.AppEventsIdle(Sender: TObject; var Done: Boolean);
begin
IdleEventTick := TimeGetTime;
Done := true;
end;
destructor TVideoImage.Destroy;
VAR
i : integer;
begin
FOR i := CBufferCnt-1 DOWNTO 0 DO
IF fImagePtrSize[i] <> 0 then
begin
FreeMem(fImagePtr[i], fImagePtrSize[i]);
fImagePtr[i] := nil;
fImagePtrSize[i] := 0;
end;
DeallocateHWnd(fMessageHWND);
inherited Destroy;
end;
procedure TVideoImage.Free;
begin
fDisplayCanvas := nil;
fBitmapGray.Free;
fBitmap.Free;
JPG.Free;
AppEvent.OnIdle := nil;
AppEvent.Free;
AppEvent := nil;
MemStream.Free;
inherited Free;
end;
// For Properties see also http://msdn.microsoft.com/en-us/library/ms786938(VS.85).aspx
function TVideoImage.TranslateProperty(const VP: TVideoProperty; VAR VPAP: TVideoProcAmpProperty): HResult;
begin
Result := S_OK;
CASE VP OF
VP_Brightness : VPAP := VideoProcAmp_Brightness;
VP_Contrast : VPAP := VideoProcAmp_Contrast;
VP_Hue : VPAP := VideoProcAmp_Hue;
VP_Saturation : VPAP := VideoProcAmp_Saturation;
VP_Sharpness : VPAP := VideoProcAmp_Sharpness;
VP_Gamma : VPAP := VideoProcAmp_Gamma;
VP_ColorEnable : VPAP := VideoProcAmp_ColorEnable;
VP_WhiteBalance : VPAP := VideoProcAmp_WhiteBalance;
VP_BacklightCompensation : VPAP := VideoProcAmp_BacklightCompensation;
VP_Gain : VPAP := VideoProcAmp_Gain;
else Result := S_False;
END; {case}
end;
FUNCTION TVideoImage.GetVideoPropertySettings(VP: TVideoProperty; VAR MinVal, MaxVal, StepSize, Default, Actual: integer; VAR AutoMode: boolean): HResult;
VAR
VPAP : TVideoProcAmpProperty;
pCapsFlags : TVideoProcAmpFlags;
BEGIN
Result := S_FALSE;
MinVal := -1;
MaxVal := -1;
StepSize := 0;
Default := 0;
Actual := 0;
AutoMode := true;
IF not(assigned(VideoSample)) or Failed(TranslateProperty(VP, VPAP)) then
exit;
Result := TranslateProperty(VP, VPAP);
IF Failed(Result) then
exit;
Result := VideoSample.GetVideoPropAmpEx(VPAP, MinVal, MaxVal, StepSize, Default, pCapsFlags, Actual);
IF Failed(Result) then
begin
MinVal := -1;
MaxVal := -1;
StepSize := 0;
Default := 0;
Actual := 0;
AutoMode := true;
end
else begin
AutoMode := pCapsFlags <> VideoProcAmp_Flags_Manual;
end;
END;
FUNCTION TVideoImage.SetVideoPropertySettings(VP: TVideoProperty; Actual: integer; AutoMode: boolean): HResult;
VAR
VPAP : TVideoProcAmpProperty;
pCapsFlags : TVideoProcAmpFlags;
BEGIN
Result := TranslateProperty(VP, VPAP);
IF not(assigned(VideoSample)) or Failed(Result) then
exit;
IF AutoMode
then pCapsFlags := VideoProcAmp_Flags_Auto
else pCapsFlags := VideoProcAmp_Flags_Manual;
Result := VideoSample.SetVideoPropAmpEx(VPAP, pCapsFlags, Actual);
END;
procedure TVideoImage.GetListOfDevices(DeviceList: TStringList);
begin
GetCaptureDeviceList(DeviceList);
end;
procedure TVideoImage.VideoPause;
begin
if not assigned(VideoSample) then
exit;
VideoSample.PauseVideo;
end;
procedure TVideoImage.VideoResume;
begin
if not assigned(VideoSample) then
exit;
VideoSample.ResumeVideo;
end;
procedure TVideoImage.VideoStop;
begin
fFPS := 0;
if not assigned(VideoSample) then
exit;
try
VideoSample.Free;
VideoSample := nil;
except
end;
fVideoRunning := false;
end;
function TVideoImage.VideoStart(DeviceName: string): integer;
VAR
hr : HResult;
st : string;
W, H : integer;
FourCC : cardinal;
begin
fSkipCnt := 0;
fFrameCnt := 0;
f30FrameTick := 0;
fFPS := 0;
fImageUnpacked := false;
Result := 0;
if assigned(VideoSample) then
VideoStop;
VideoSample := TVideoSample.Create(Application.MainForm.Handle, false, 0, HR); // No longer force RGB24
try
hr := VideoSample.StartVideo(DeviceName, false, st) // Not visible. Displays itself...
except
hr := -1;
end;
if Failed(hr)
then begin
VideoStop;
// ShowMessage(DXGetErrorDescription9A(hr));
Result := 1;
end
else begin
hr := VideoSample.GetStreamInfo(W, H, FourCC);
IF Failed(HR)
then begin
VideoStop;
Result := 1;
end
else BEGIN
fWidth := W;
fHeight := H;
fFourCC := FourCC;
FBitmap.PixelFormat := pf24bit;
FBitmap.Width := W;
FBitmap.Height := H;
PrepareGrayBMP(FBitmapGray, W, H);
VideoSample.SetCallBack(CallBack); // Do not call GDI routines in Callback!
END;
end;
end;
function TVideoImage.VideoSampleIsPaused: boolean;
begin
if assigned(VideoSample)
then Result := VideoSample.PlayState = PS_PAUSED
else Result := false;
end;
// Create an 8bit grayscale palette image with width W and Height H.
PROCEDURE TVideoImage.PrepareGrayBMP(VAR BM : TBitmap; W, H: integer);
TYPE
TLogPal = packed record
palVersion: Word;
palNumEntries: Word;
palPalEntry: array[0..255] of TPaletteEntry; // In contrast to original declaration uses 255 instead of 0
end;
VAR
Pal : TLogPal;
_Pal : tagLogPalette absolute Pal; // Trick! ;)
dw : LongWord;
BEGIN
WITH Pal DO
BEGIN
palVersion:=$300;
palNumEntries:=256;
FOR dw := 0 TO 255 DO
palPalEntry[dw] := TPaletteEntry(dw * $010101);
END;
BM.width := W;
BM.Height := H;
BM.Transparent := false;
BM.pixelformat := pf8bit;
BM.Palette := CreatePalette(_Pal);
END; {PrepareGrayBMP}
PROCEDURE TVideoImage.Convert24ToGray(BM24: TBitmap; BMGray: TBitmap);
{ - Convert a 24bit RGB bitmap into a 8bit grayscale image }
//type
// tbytearray = ARRAY[0..16387] OF byte;
// pbytearray = ^tbytearray;
//VAR
// p24, p8 : pbytearray;
// X, Y, X3 : integer;
BEGIN
IF BM24.PixelFormat = pf8bit then
begin
BMGray.assign(BM24);
exit;
end;
if (BM24.Width <> BMGray.Width) or (BM24.Height <> BMGray.Height) or (BMGray.PixelFormat <> pf8bit) then
PrepareGrayBMP(BMGray, BM24.Width, bm24.Height);
{ This is the do-it-yourself way of converting RGB to GrayScale:
FOR Y := BM24.height-1 DOWNTO 0 do
begin
p24 := BM24.ScanLine[Y];
p8 := BMGray.ScanLine[Y];
X3 := 0;
FOR X := 0 TO BMGray.Width-1 DO
begin
p8^[X] := (GrayConvB[p24^[X3]] + GrayConvG[p24^[X3+1]] + GrayConvR[p24^[X3+2]]) div 256;
Inc(X3, 3);
end;
end;
}
BMGray.Canvas.Draw(0, 0, BM24);
END;
PROCEDURE TVideoImage.PrepareTables;
VAR
i : integer;
BEGIN
IF fYUY2TablesPrepared then
exit;
FOR i := 0 TO 255 DO
BEGIN
{ http://msdn.microsoft.com/en-us/library/ms893078.aspx
ValueY_298[i] := (i- 16) * 298 + 128; // -4640 .. 71350
ValueU_100[i] := (i-128) * 100; // -12800 .. 12700
ValueU_516[i] := (i-128) * 516; // -66048 .. 65532
ValueV_409[i] := (i-128) * 409; // -52352 .. 51943
ValueV_208[i] := (i-128) * 208; // -26624 .. 26416
}
// http://en.wikipedia.org/wiki/YCbCr (ITU-R BT.601)
ValueY_298[i] := round(i * 298.082);
ValueU_100[i] := round(i * -100.291);
ValueU_516[i] := round(i * 516.412 - 276.836*256);
ValueV_409[i] := round(i * 408.583 - 222.921*256);
ValueV_208[i] := round(i * -208.120 + 135.576*256);
ValueL_255[i] := Min(255, round(i * 298.082 / 255));
END;
FillChar(ValueClip, SizeOf(ValueClip), #0);
FOR i := 0 TO 255 DO
ValueClip[i] := i;
FOR i := 256 TO 1023 DO
ValueClip[i] := 255;
fYUY2TablesPrepared := true;
END;
procedure TVideoImage.I420_to_RGB(pData: pointer);
// http://en.wikipedia.org/wiki/YCbCr
VAR
L, X, Y : integer;
ps : pbyte;
pY, pU, pV : pbyte;
begin
pY := pData;
PrepareTables;
FOR Y := 0 TO fBitmap.Height-1 DO
BEGIN
ps := fBitmap.ScanLine[Y];
pU := pData;
Inc(pU, fBitmap.Width*(fBitmap.height+ Y div 4));
pV := PU;
Inc(pV, fBitmap.Width*fBitmap.height div 4);
FOR X := 0 TO (fBitmap.Width div 2)-1 DO
begin
L := ValueY_298[pY^];
ps^ := ValueClip[(L + ValueU_516[pU^] ) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueU_100[pU^] + ValueV_208[pV^]) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueV_409[pV^]) div 256];
Inc(ps);
Inc(pY);
L := ValueY_298[pY^];
ps^ := ValueClip[(L + ValueU_516[pU^] ) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueU_100[pU^] + ValueV_208[pV^]) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueV_409[pV^]) div 256];
Inc(ps);
Inc(pY);
Inc(pU);
Inc(pV);
end;
END;
end;
procedure TVideoImage.I420_to_Gray8Bit(pData: pointer);
// http://en.wikipedia.org/wiki/YCbCr
var
Y : integer;
pY : pbyte;
begin
pY := pData;
FOR Y := 0 TO fBitmapGray.Height-1 DO
begin
move(pY^, fBitmapGray.ScanLine[Y]^, fBitmapGray.Width);
Inc(pY, fBitmapGray.Width);
end;
end;
procedure TVideoImage.YUY2_to_RGB(pData: pointer);
// http://msdn.microsoft.com/en-us/library/ms893078.aspx
// http://en.wikipedia.org/wiki/YCbCr
type
TFour = ARRAY[0..3] OF byte;
VAR
L, X, Y : integer;
ps : pbyte;
pf : ^TFour;
begin
pf := pData;
PrepareTables;
FOR Y := 0 TO fBitmap.Height-1 DO
BEGIN
ps := fBitmap.ScanLine[Y];
FOR X := 0 TO (fBitmap.Width div 2)-1 DO
begin
L := ValueY_298[pf^[0]];
ps^ := ValueClip[(L + ValueU_516[pf^[1]] ) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueU_100[pf^[1]] + ValueV_208[pf^[3]]) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueV_409[pf^[3]]) div 256];
Inc(ps);
L := ValueY_298[pf^[2]];
ps^ := ValueClip[(L + ValueU_516[pf^[1]] ) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueU_100[pf^[1]] + ValueV_208[pf^[3]]) div 256];
Inc(ps);
ps^ := ValueClip[(L + ValueV_409[pf^[3]]) div 256];
Inc(ps);
Inc(pf);
end;
END;
end;
procedure TVideoImage.YUY2_to_Gray8Bit(pData: pointer);
// http://msdn.microsoft.com/en-us/library/ms893078.aspx
// http://en.wikipedia.org/wiki/YCbCr
type
TFour = ARRAY[0..3] OF byte;
VAR
X, Y : integer;
ps : pbyte;
pf : ^byte;
begin
pf := pData;
FOR Y := 0 TO fBitmapGray.Height-1 DO
BEGIN
ps := fBitmapGray.ScanLine[Y];
FOR X := 0 TO (fBitmapGray.Width div 2)-1 DO
begin
ps^ := pf^;
Inc(ps);
Inc(pf, 2);
ps^ := pf^;
Inc(ps);
Inc(pf, 2);
end;
END;
end;
procedure TVideoImage.RGB_to_Gray8Bit(pData: pointer);
type
TRGB = ARRAY[0..5] OF byte;
TPTRGB = ^TRGB;
TWordArr = ARRAY[0..5759] OF word;
TPTWordArr = ^TWordArr;
VAR
X, Y : integer;
p8 : TPTWordArr;
pf : TPTRGB;
begin
pf := pData;
FOR Y := fBitmapGray.height-1 DOWNTO 0 do
begin
p8 := fBitmapGray.ScanLine[Y];
FOR X := 0 TO fBitmapGray.Width div 2-1 DO
begin
p8^[X] := ((GrayConvB[pf^[3]] + GrayConvG[pf^[4]] + GrayConvR[pf^[5]]) and $FF00) +
(GrayConvB[pf^[0]] + GrayConvG[pf^[1]] + GrayConvR[pf^[2]]) shr 8;
Inc(pf);
end;
end;
end;
procedure TVideoImage.PaintFrame;
BEGIN
// Paint FBitmap to fDisplayCanvas, if available
if assigned(fDisplayCanvas) then
begin
IF not fImageUnpacked then
UnpackFrame(fImagePtrSize[fImagePtrIndex], fImagePtr[fImagePtrIndex]);
IF fDisplayCanvas.LockCount < 1 then
begin
fDisplayCanvas.lock;
try
IF fGray8Bit
then fDisplayCanvas.Draw(0, 0, fBitmapGray)
else fDisplayCanvas.Draw(0, 0, fBitmap);
finally
fDisplayCanvas.unlock;
end;
end;
end;
END;
procedure TVideoImage.UnpackFrame(Size: integer; pData: pointer);
var
{f : file;}
Unknown : boolean;
FourCCSt: string[4];
begin
IF pData = nil
then exit;
Unknown := false;
try
Case fFourCC OF
0 : BEGIN
IF (Size = fWidth*fHeight*3)
then begin
if fGray8Bit
then RGB_to_Gray8Bit(pData) // Okay, this is when Grayscale is much slower than color :(
else move(pData^, FBitmap.scanline[fHeight-1]^, Size);
end
else Unknown := true;
END;
FourCC_YUY2,
FourCC_YUYV,
FourCC_YUNV : BEGIN
Unknown := (Size <> fWidth*fHeight*2);
IF Unknown then
begin
// Special treatment in case too much data is sent.
// e.g. Microsoft LifeCam Cinema delivers 1280*1080*2 Bytes
// when 1280*720 was selected. The extra Bytes do not
// contain video data. One third of the data (921600 Bytes)
// is wasted by the driver!
if (Size > fWidth * fHeight * 2) then
Unknown := (Size div (2 * fWidth)) mod 4 <> 0; // Width a multiple of 4? Maybe OK.
end;
IF not(Unknown) then
begin
IF fGray8Bit
then YUY2_to_Gray8Bit(pData)
else YUY2_to_RGB(pData);
end;
END;
FourCC_MJPG : BEGIN
try
MemStream.Clear;
MemStream.SetSize(Size);
MemStream.Position := 0;
MemStream.WriteBuffer(pData^, Size);
MemStream.Position := 0;
IF GDIPlusAvailable // GDI+ version is faster than Delphi's JPEG library
then begin
if fGray8Bit
then GDIPlus_LoadBMPStream2(MemStream, FBitmapGray)
else GDIPlus_LoadBMPStream2(MemStream, FBitmap);
end
else begin
JPG.Grayscale := fGray8Bit;
JPG.LoadFromStream(MemStream);
if fGray8Bit
then FBitmapGray.Canvas.Draw(0, 0, JPG)
else FBitmap.Canvas.Draw(0, 0, JPG);
end;
except
Unknown := true;
end;
END;
FourCC_I420,
FourCC_YV12,
FourCC_IYUV : BEGIN
Unknown := (Size <> (fWidth*fHeight*3) div 2);
IF not Unknown then
IF fGray8Bit
then I420_to_Gray8Bit(pData)
else I420_to_RGB(pData);
END;
else BEGIN
{
assignfile(f, 'Unknown_Frame.dat');
rewrite(f, 1);
Blockwrite(f, pData^, Size);
closefile(f);
}
Unknown := true;
END;
end; {case}
IF Unknown then
begin
IF fFourCC = 0
then FourCCSt := 'RGB'
else begin
FourCCSt := ' ';
move(fFourCC, FourCCSt[1], 4);
end;
FBitmap.Canvas.TextOut(0, 0, 'Unknown compression');
FBitmap.Canvas.TextOut(0, FBitmap.Canvas.TextHeight('X'), 'DataSize: '+INtToStr(Size)+' FourCC: '+FourCCSt);
end;
fImageUnpacked := true;
except
end;
end;
procedure TVideoImage.GetBitmap(BMP: TBitmap);
begin
IF not fImageUnpacked then
UnpackFrame(fImagePtrSize[fImagePtrIndex], fImagePtr[fImagePtrIndex]);
if fGray8Bit
then BMP.Assign(fBitmapGray)
else BMP.Assign(fBitmap);
(*
BMP.PixelFormat := pf24bit;
BMP.Width := fBitmap.Width;
BMP.Height := fBitmap.Height;
move(fBitmap.ScanLine[fBitmap.Height-1]^, BMP.ScanLine[BMP.height-1]^, BMP.Height*BMP.Width*3);
//BMP.Canvas.Draw(0, 0, fBitmap);
*)
end;
procedure TVideoImage.SetDisplayCanvas(Canvas: TCanvas);
begin
fDisplayCanvas := Canvas;
end;
procedure TVideoImage.ShowProperty;
begin
VideoSample.ShowPropertyDialog;
end;
procedure TVideoImage.ShowProperty_Stream;
var
hr : HResult;
W, H : integer;
FourCC : cardinal;
begin
VideoSample.ShowPropertyDialog_CaptureStream;
hr := VideoSample.GetStreamInfo(W, H, FourCC);
IF Failed(HR)
then begin
VideoStop;
end
else BEGIN
fWidth := W;
fHeight := H;
fFourCC := FourCC;
FBitmap.PixelFormat := pf24bit;
FBitmap.Width := W;
FBitmap.Height := H;
PrepareGrayBMP(FBitmapGray, W, H);
VideoSample.SetCallBack(CallBack);
END;
end;