-
Notifications
You must be signed in to change notification settings - Fork 12
/
dmMainU.pas
242 lines (211 loc) · 6.42 KB
/
dmMainU.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
unit dmMainU;
interface
{$I Defines.inc}
{.$DEFINE MANUALYUY2TORGB} //debug ON /OFF
uses
Windows, Graphics, SysUtils, Classes, Forms, ExtCtrls,
IdThreadMgr, IdThreadMgrDefault, IdBaseComponent, IdComponent, IdTCPServer,
DSPack, DirectShow9,
CommonU, VideoCoDec;
type
TYUY2Word = packed record
Y : Byte;
UV : Byte;
end;
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[0..32767] of TRGBTriple;
TdmMain = class(TDataModule)
fgMain: TFilterGraph;
sgVideo: TSampleGrabber;
dsfCam: TFilter;
TCPServer: TIdTCPServer;
ThdMgr: TIdThreadMgrDefault;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure TCPServerExecute(AThread: TIdPeerThread);
procedure sgVideoBuffer(sender: TObject; SampleTime: Double;
pBuffer: Pointer; BufferLen: Integer);
private
YUY2Stream: TMemoryStream;
{$IFDEF MANUALYUY2TORGB}
LastFrame: TBitmap;
{$ENDIF}
LastPacket: TFramePacket;
public
FrameWidth: Integer;
FrameHeight: Integer;
VideoCoDec: TVideoCoDec;
procedure UpdateVideoFormat(InputFormat: TBitmapInfoHeader);
end;
var
dmMain: TdmMain;
MREWS: TMultiReadExclusiveWriteSynchronizer;
implementation
uses
DisplayU, Preview;
{$R *.dfm}
{-------------------------------------------------------------------------------
Procedure: YUY2ToBMP
Author: Michael Andersen - slightly modified by Lee_Nover
Date: 18-nov-2002
Arguments: const aWidth, aHeight: Integer; MemBuf: TMemoryStream
Result: TBitmap
-------------------------------------------------------------------------------}
{$IFDEF MANUALYUY2TORGB}
function YUY2ToBMP(const aWidth, aHeight: Integer; MemBuf: TMemoryStream; bmp: TBitmap = nil): TBitmap;
function FixValue(const x: Double): Byte;
var
v: Integer;
begin
v := Round(x);
if v > 255 then
Result := 255
else if v < 0 then
Result := 0
else
Result := Byte(v);
end;
var
BufferYUV: array[0..32767] of TYUY2Word;
i, j, Y, U, V: Integer;
Row: PRGBTripleArray;
begin
// quick hack - change this
if Assigned(bmp) then
Result:=bmp
else
Result := TBitmap.Create;
Result.PixelFormat := pf24Bit;
Result.Width := aWidth;
Result.Height := aHeight;
MemBuf.Position := 0;
for j := 0 to aHeight-1 do begin
MemBuf.Read(BufferYUV, Round((SizeOf(BufferYUV) / 32768) * aWidth) );
Row := Result.Scanline[j];
for i := 0 to aWidth-1 do begin
if i mod 2 = 0 then begin
Y := BufferYUV[i].Y;
U := BufferYUV[i].UV;
V := BufferYUV[i+1].UV;
end else begin
Y := BufferYUV[i].Y;
U := BufferYUV[i-1].UV;
V := BufferYUV[i].UV
end;
with Row[i] do begin
rgbtRed := FixValue( 1.164383 * (Y -16) + 1.596027 * (V-128) );
rgbtGreen := FixValue( 1.164383 * (Y - 16) - (0.391762 * (U-128)) - (0.812968 * (V-128)) );
rgbtBlue := FixValue( 1.164383 * (Y - 16) + 2.017232 * (U-128) );
end;
end;
end;
end;
{$ENDIF}
procedure TdmMain.DataModuleCreate(Sender: TObject);
begin
ZeroMemory(@LastPacket, SizeOf(TFramePacket));
VideoCoDec := TVideoCoDec.Create;
MREWS := TMultiReadExclusiveWriteSynchronizer.Create;
YUY2Stream := TMemoryStream.Create;
{$IFDEF MANUALYUY2TORGB}
LastFrame := TBitmap.Create;
{$ENDIF}
TCPServer.DefaultPort := 33000;
//TCPServer.Active := True;
end;
procedure TdmMain.DataModuleDestroy(Sender: TObject);
begin
TCPServer.Active := False;
FreeAndNil(MREWS);
{$IFDEF MANUALYUY2TORGB}
FreeAndNil(LastFrame);
{$ENDIF}
FreeAndNil(YUY2Stream);
FreeAndNil(VideoCoDec);
sgVideo.MediaType := nil;
end;
procedure TdmMain.TCPServerExecute(AThread: TIdPeerThread);
var
CH: TCommHeader;
FP: TFramePacket;
bmih: TBitmapInfoHeader;
begin
// ALL messages should have a standard packet (easier coding)
AThread.Connection.ReadBuffer(CH, SizeOf(CH));
// check what the client wants
case CH.DPType of
1: // the client wants frame format and refresh rate
begin
bmih := VideoCoDec.BIOutput.bmiHeader;
// set the size of the data part to the size of the header
CH.DPSize := SizeOf(bmih);
CH.DPExtra := 30; // hardcoded .. find out the real RR from the source
// send the header
SendData(AThread.Connection, CH, @bmih);
end;
2: // request for frame
begin
// synch the copying of the last frame
MREWS.BeginRead;
try
FP := CopyFrame(LastPacket);
finally
MREWS.EndRead;
end;
// send the frame
SendFrame(AThread.Connection, CH, FP);
// free the copied packet
FreeFrame(FP);
end;
end;
end;
procedure TdmMain.UpdateVideoFormat(InputFormat: TBitmapInfoHeader);
var
bmihOut: TBitmapInfoHeader;
FrameRate: Integer;
begin
bmihOut := InputFormat;
InputFormat.biCompression := 0;
FrameRate := 30;
VideoCoDec.Finish;
VideoCoDec.ForceKeyFrameRate := true;
VideoCoDec.Init(InputFormat, bmihOut, 100, 10);
VideoCoDec.SetDataRate(1024, 1000 * 1000 div FrameRate, 1);
if not VideoCoDec.StartCompressor then
DisplayF.Caption := TranslateICError(VideoCoDec.LastError)
else
//DisplayF.Caption := VideoCoDec.CodecDescription;
DisplayF.Caption := 'Delphi Streaming';
frmPreview.ClientHeight := InputFormat.biHeight;
frmPreview.ClientWidth := InputFormat.biWidth;
end;
procedure TdmMain.sgVideoBuffer(sender: TObject; SampleTime: Double;
pBuffer: Pointer; BufferLen: Integer);
var
p: PByte;
begin
if not VideoCoDec.CompressorStarted then
Exit;
{$IFDEF MANUALYUY2TORGB}
// the data is in YUY2 format because of overlay so we need to convert it to RGB
YUY2Stream.Clear;
YUY2Stream.Write(pBuffer^, BufferLen);
YUY2ToBMP(FrameWidth, FrameHeight, YUY2Stream, LastFrame);
{$ENDIF}
MREWS.BeginWrite; // remove to gain performance but at desynch risk
with LastPacket do
try
{$IFDEF MANUALYUY2TORGB}
p := VideoCoDec.PackBitmap(LastFrame, KeyFrame, Size);
{$ELSE}
p := VideoCoDec.PackFrame(pBuffer, KeyFrame, Size);
{$ENDIF}
if Size < 1 then
Exit;
ReallocMem(Data, Size);
CopyMemory(Data, p, Size);
finally
MREWS.EndWrite; // remove to gain performance but at desynch risk
end;
end;
end.