author | Stepan777 <stepik-777@mail.ru> |
Mon, 11 Jun 2012 18:15:30 +0400 | |
changeset 7235 | baa69bd025d9 |
parent 7198 | 5debd5fe526e |
child 7280 | fd707afbc3a2 |
permissions | -rw-r--r-- |
7180 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
19 |
{$INCLUDE "options.inc"} |
|
20 |
||
21 |
unit uVideoRec; |
|
22 |
||
23 |
{$IFDEF UNIX} |
|
24 |
{$LINKLIB avwrapper} |
|
25 |
{$LINKLIB avutil} |
|
26 |
{$LINKLIB avcodec} |
|
27 |
{$LINKLIB avformat} |
|
28 |
{$ENDIF} |
|
29 |
||
30 |
interface |
|
31 |
||
32 |
var flagPrerecording: boolean = false; |
|
33 |
||
34 |
function BeginVideoRecording: Boolean; |
|
35 |
function LoadNextCameraPosition: LongInt; |
|
36 |
procedure EncodeFrame; |
|
37 |
procedure StopVideoRecording; |
|
38 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
39 |
procedure BeginPreRecording; |
7180 | 40 |
procedure StopPreRecording; |
41 |
procedure SaveCameraPosition; |
|
42 |
||
43 |
procedure freeModule; |
|
44 |
||
45 |
implementation |
|
46 |
||
47 |
uses uVariables, uUtils, GLunit, SDLh, SysUtils; |
|
48 |
||
49 |
{$IFDEF WIN32} |
|
50 |
const AVWrapperLibName = 'libavwrapper.dll'; |
|
51 |
{$ENDIF} |
|
52 |
||
53 |
type TAddFileLogRaw = procedure (s: pchar); cdecl; |
|
54 |
||
55 |
{$IFDEF WIN32} |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
56 |
procedure AVWrapper_Init( |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
57 |
AddLog: TAddFileLogRaw; |
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
58 |
filename, finalFilename, soundFile, format, vcodec, acodec, preset: PChar; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
59 |
width, height, framerateNum, framerateDen, frequency, channels, vquality, aquality: LongInt); cdecl; external AVWrapperLibName; |
7180 | 60 |
procedure AVWrapper_Close; cdecl; external AVWrapperLibName; |
61 |
procedure AVWrapper_WriteFrame( pY, pCb, pCr: PByte ); cdecl; external AVWrapperLibName; |
|
62 |
{$ELSE} |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
63 |
procedure AVWrapper_Init( |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
64 |
AddLog: TAddFileLogRaw; |
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
65 |
filename, finalFilename, soundFile, format, vcodec, acodec, preset: PChar; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
66 |
width, height, framerateNum, framerateDen, frequency, channels, vquality, aquality: LongInt); cdecl; external; |
7180 | 67 |
procedure AVWrapper_Close; cdecl; external; |
68 |
procedure AVWrapper_WriteFrame( pY, pCb, pCr: PByte ); cdecl; external; |
|
69 |
{$ENDIF} |
|
70 |
||
71 |
var YCbCr_Planes: array[0..2] of PByte; |
|
72 |
RGB_Buffer: PByte; |
|
73 |
||
74 |
frequency, channels: LongInt; |
|
75 |
||
76 |
cameraFile: TextFile; |
|
77 |
audioFile: File; |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
78 |
|
7180 | 79 |
numPixels: LongInt; |
80 |
||
81 |
firstTick, nframes: Int64; |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
82 |
|
7180 | 83 |
cameraFilePath, soundFilePath: shortstring; |
84 |
||
85 |
function BeginVideoRecording: Boolean; |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
86 |
var filename, finalFilename: shortstring; |
7180 | 87 |
begin |
88 |
AddFileLog('BeginVideoRecording'); |
|
89 |
||
90 |
numPixels:= cScreenWidth*cScreenHeight; |
|
91 |
||
92 |
{$IOCHECKS OFF} |
|
93 |
// open file with prerecorded camera positions |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
94 |
cameraFilePath:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + '.txtin'; |
7180 | 95 |
Assign(cameraFile, cameraFilePath); |
96 |
Reset(cameraFile); |
|
97 |
if IOResult <> 0 then |
|
98 |
begin |
|
99 |
AddFileLog('Error: Could not read from ' + cameraFilePath); |
|
100 |
exit(false); |
|
101 |
end; |
|
102 |
||
103 |
ReadLn(cameraFile, frequency, channels); |
|
104 |
{$IOCHECKS ON} |
|
105 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
106 |
filename:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + #0; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
107 |
finalFilename:= UserPathPrefix + '/Videos/' + cRecPrefix + #0; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
108 |
soundFilePath:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + '.sw' + #0; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
109 |
cAVFormat+= #0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
110 |
cAudioCodec+= #0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
111 |
cVideoCodec+= #0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
112 |
cVideoPreset+= #0; |
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
113 |
AVWrapper_Init(@AddFileLogRaw, @filename[1], @finalFilename[1], @soundFilePath[1], @cAVFormat[1], @cVideoCodec[1], @cAudioCodec[1], @cVideoPreset[1], |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
114 |
cScreenWidth, cScreenHeight, cVideoFramerateNum, cVideoFramerateDen, frequency, channels, cAudioQuality, cVideoQuality); |
7180 | 115 |
|
116 |
YCbCr_Planes[0]:= GetMem(numPixels); |
|
117 |
YCbCr_Planes[1]:= GetMem(numPixels div 4); |
|
118 |
YCbCr_Planes[2]:= GetMem(numPixels div 4); |
|
119 |
||
120 |
if (YCbCr_Planes[0] = nil) or (YCbCr_Planes[1] = nil) or (YCbCr_Planes[2] = nil) then |
|
121 |
begin |
|
122 |
AddFileLog('Error: Could not allocate memory for video recording (YCbCr buffer).'); |
|
123 |
exit(false); |
|
124 |
end; |
|
125 |
||
126 |
RGB_Buffer:= GetMem(4*numPixels); |
|
127 |
if RGB_Buffer = nil then |
|
128 |
begin |
|
129 |
AddFileLog('Error: Could not allocate memory for video recording (RGB buffer).'); |
|
130 |
exit(false); |
|
131 |
end; |
|
132 |
||
133 |
BeginVideoRecording:= true; |
|
134 |
end; |
|
135 |
||
136 |
procedure StopVideoRecording; |
|
137 |
begin |
|
138 |
AddFileLog('StopVideoRecording'); |
|
139 |
FreeMem(YCbCr_Planes[0], numPixels); |
|
140 |
FreeMem(YCbCr_Planes[1], numPixels div 4); |
|
141 |
FreeMem(YCbCr_Planes[2], numPixels div 4); |
|
142 |
FreeMem(RGB_Buffer, 4*numPixels); |
|
143 |
Close(cameraFile); |
|
144 |
AVWrapper_Close(); |
|
145 |
DeleteFile(cameraFilePath); |
|
146 |
DeleteFile(soundFilePath); |
|
147 |
end; |
|
148 |
||
149 |
function pixel(x, y, color: LongInt): LongInt; |
|
150 |
begin |
|
151 |
pixel:= RGB_Buffer[(cScreenHeight-y-1)*cScreenWidth*4 + x*4 + color]; |
|
152 |
end; |
|
153 |
||
154 |
procedure EncodeFrame; |
|
155 |
var x, y, r, g, b: LongInt; |
|
156 |
begin |
|
157 |
// read pixels from OpenGL |
|
158 |
glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_RGBA, GL_UNSIGNED_BYTE, RGB_Buffer); |
|
159 |
||
160 |
// convert to YCbCr 4:2:0 format |
|
161 |
// Y |
|
162 |
for y := 0 to cScreenHeight-1 do |
|
163 |
for x := 0 to cScreenWidth-1 do |
|
164 |
YCbCr_Planes[0][y*cScreenWidth + x]:= Byte(16 + ((16828*pixel(x,y,0) + 33038*pixel(x,y,1) + 6416*pixel(x,y,2)) shr 16)); |
|
165 |
||
166 |
// Cb and Cr |
|
167 |
for y := 0 to cScreenHeight div 2 - 1 do |
|
168 |
for x := 0 to cScreenWidth div 2 - 1 do |
|
169 |
begin |
|
170 |
r:= pixel(2*x,2*y,0) + pixel(2*x+1,2*y,0) + pixel(2*x,2*y+1,0) + pixel(2*x+1,2*y+1,0); |
|
171 |
g:= pixel(2*x,2*y,1) + pixel(2*x+1,2*y,1) + pixel(2*x,2*y+1,1) + pixel(2*x+1,2*y+1,1); |
|
172 |
b:= pixel(2*x,2*y,2) + pixel(2*x+1,2*y,2) + pixel(2*x,2*y+1,2) + pixel(2*x+1,2*y+1,2); |
|
173 |
YCbCr_Planes[1][y*(cScreenWidth div 2) + x]:= Byte(128 + ((-2428*r - 4768*g + 7196*b) shr 16)); |
|
174 |
YCbCr_Planes[2][y*(cScreenWidth div 2) + x]:= Byte(128 + (( 7196*r - 6026*g - 1170*b) shr 16)); |
|
175 |
end; |
|
176 |
||
177 |
AVWrapper_WriteFrame(YCbCr_Planes[0], YCbCr_Planes[1], YCbCr_Planes[2]); |
|
178 |
end; |
|
179 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
180 |
// returns new game ticks |
7180 | 181 |
function LoadNextCameraPosition: LongInt; |
182 |
var NextTime: LongInt; |
|
183 |
NextZoom: LongInt; |
|
184 |
NextWorldDx, NextWorldDy: LongInt; |
|
185 |
begin |
|
186 |
{$IOCHECKS OFF} |
|
187 |
if eof(cameraFile) then |
|
188 |
exit(-1); |
|
189 |
ReadLn(cameraFile, NextTime, NextWorldDx, NextWorldDy, NextZoom); |
|
190 |
{$IOCHECKS ON} |
|
191 |
WorldDx:= NextWorldDx; |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
192 |
WorldDy:= NextWorldDy + cScreenHeight div 2; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
193 |
zoom:= NextZoom/10000*cScreenWidth; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
194 |
ZoomValue:= zoom; |
7180 | 195 |
LoadNextCameraPosition:= NextTime; |
196 |
end; |
|
197 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
198 |
// Callback which records sound. |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
199 |
// This procedure may be called from different thread. |
7180 | 200 |
procedure RecordPostMix(udata: pointer; stream: PByte; len: LongInt); cdecl; |
201 |
begin |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
202 |
udata:= udata; // avoid warning |
7180 | 203 |
{$IOCHECKS OFF} |
204 |
BlockWrite(audioFile, stream^, len); |
|
205 |
{$IOCHECKS ON} |
|
206 |
end; |
|
207 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
208 |
procedure BeginPreRecording; |
7180 | 209 |
var format: word; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
210 |
filePrefix, filename: shortstring; |
7180 | 211 |
begin |
212 |
AddFileLog('BeginPreRecording'); |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
213 |
|
7180 | 214 |
nframes:= 0; |
215 |
firstTick:= SDL_GetTicks(); |
|
216 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
217 |
filePrefix:= FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
218 |
|
7180 | 219 |
Mix_QuerySpec(@frequency, @format, @channels); |
220 |
if format <> $8010 then |
|
221 |
begin |
|
222 |
// TODO: support any audio format |
|
223 |
AddFileLog('Error: Unexpected audio format ' + IntToStr(format)); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
224 |
exit; |
7180 | 225 |
end; |
226 |
||
227 |
{$IOCHECKS OFF} |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
228 |
// create sound file |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
229 |
filename:= UserPathPrefix + '/VideoTemp/' + filePrefix + '.sw'; |
7180 | 230 |
Assign(audioFile, filename); |
231 |
Rewrite(audioFile, 1); |
|
232 |
if IOResult <> 0 then |
|
233 |
begin |
|
234 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
235 |
exit; |
7180 | 236 |
end; |
237 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
238 |
// create file with camera positions |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
239 |
filename:= UserPathPrefix + '/VideoTemp/' + filePrefix + '.txtout'; |
7180 | 240 |
Assign(cameraFile, filename); |
241 |
Rewrite(cameraFile); |
|
242 |
if IOResult <> 0 then |
|
243 |
begin |
|
244 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
245 |
exit; |
7180 | 246 |
end; |
247 |
{$IOCHECKS ON} |
|
248 |
WriteLn(cameraFile, inttostr(frequency) + ' ' + inttostr(channels)); |
|
249 |
||
250 |
// register callback for actual audio recording |
|
251 |
Mix_SetPostMix(@RecordPostMix, nil); |
|
252 |
||
253 |
flagPrerecording:= true; |
|
254 |
end; |
|
255 |
||
256 |
procedure StopPreRecording; |
|
257 |
begin |
|
258 |
AddFileLog('StopPreRecording'); |
|
259 |
flagPrerecording:= false; |
|
260 |
||
261 |
// call SDL_LockAudio because RecordPostMix may be executing right now |
|
262 |
SDL_LockAudio(); |
|
263 |
Close(audioFile); |
|
264 |
Close(cameraFile); |
|
265 |
Mix_SetPostMix(nil, nil); |
|
266 |
SDL_UnlockAudio(); |
|
267 |
end; |
|
268 |
||
269 |
procedure SaveCameraPosition; |
|
270 |
var Ticks: LongInt; |
|
271 |
begin |
|
272 |
Ticks:= SDL_GetTicks(); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
273 |
while (Ticks - firstTick)*cVideoFramerateNum > nframes*cVideoFramerateDen*1000 do |
7180 | 274 |
begin |
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
275 |
WriteLn(cameraFile, inttostr(GameTicks) + ' ' + inttostr(WorldDx) + ' ' + inttostr(WorldDy - cScreenHeight div 2) + ' ' + inttostr(Round(zoom*10000/cScreenWidth))); |
7180 | 276 |
inc(nframes); |
277 |
end; |
|
278 |
end; |
|
279 |
||
280 |
procedure freeModule; |
|
281 |
begin |
|
282 |
if flagPrerecording then |
|
283 |
StopPreRecording(); |
|
284 |
end; |
|
285 |
||
286 |
end. |