Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vfapi-plugin.c « vfapi « contrib « windows « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81547b8bb1a4551c69c8fdaaaf3c5fbbb8a9d55e (plain)
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
/*
 * VFAPI-Plugin
 *
 * Copyright (c) 2006 Peter Schlaile
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 */


#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>

#define	VF_STREAM_VIDEO		0x00000001
#define	VF_STREAM_AUDIO		0x00000002
#define	VF_OK				0x00000000
#define	VF_ERROR			0x80004005

typedef	struct {
	DWORD	dwSize;
	DWORD	dwAPIVersion;	
	DWORD	dwVersion;	
	DWORD	dwSupportStreamType;	
	char	cPluginInfo[256];	
	char	cFileType[256]; 	
} VF_PluginInfo,*LPVF_PluginInfo;

typedef	DWORD	VF_FileHandle,*LPVF_FileHandle;

typedef	struct {
	DWORD	dwSize;			
	DWORD	dwHasStreams;		
} VF_FileInfo,*LPVF_FileInfo;

typedef	struct {
	DWORD	dwSize;			
	DWORD	dwLengthL;	
	DWORD	dwLengthH;	
	DWORD	dwRate;			
	DWORD	dwScale;		
	DWORD	dwWidth;		
	DWORD	dwHeight;		
	DWORD	dwBitCount;		
} VF_StreamInfo_Video,*LPVF_StreamInfo_Video;

typedef	struct {
	DWORD	dwSize;			
	DWORD	dwLengthL;	
	DWORD	dwLengthH;	
	DWORD	dwRate;			
	DWORD	dwScale;		
	DWORD	dwChannels;		
	DWORD	dwBitsPerSample;	
	DWORD	dwBlockAlign;		
} VF_StreamInfo_Audio,*LPVF_StreamInfo_Audio;

typedef	struct {
	DWORD	dwSize;			
	DWORD	dwFrameNumberL;	
	DWORD	dwFrameNumberH;	
	void	*lpData;		
	long	lPitch;			
} VF_ReadData_Video,*LPVF_ReadData_Video;

typedef	struct {
	DWORD	dwSize;			
	LONGLONG	dwSamplePos;	
	DWORD	dwSampleCount;		
	DWORD	dwReadedSampleCount;	
	DWORD	dwBufSize;		
	void	*lpBuf;			
} VF_ReadData_Audio,*LPVF_ReadData_Audio;

typedef	struct {
	DWORD	dwSize;			
	HRESULT (__stdcall *OpenFile)( 
		char *lpFileName, LPVF_FileHandle lpFileHandle );
	HRESULT (__stdcall *CloseFile)( VF_FileHandle hFileHandle );
	HRESULT (__stdcall *GetFileInfo)( VF_FileHandle hFileHandle,
					 LPVF_FileInfo lpFileInfo );
	HRESULT (__stdcall *GetStreamInfo)( VF_FileHandle hFileHandle,
					   DWORD dwStream,void *lpStreamInfo );
	HRESULT (__stdcall *ReadData)( VF_FileHandle hFileHandle,
				      DWORD dwStream,void *lpData ); 
} VF_PluginFunc,*LPVF_PluginFunc;

__declspec(dllexport) HRESULT vfGetPluginInfo( 
	LPVF_PluginInfo lpPluginInfo )
{
	if (!lpPluginInfo || lpPluginInfo->dwSize != sizeof(VF_PluginInfo)) {
		return VF_ERROR;
	}

	lpPluginInfo->dwAPIVersion = 1;
	lpPluginInfo->dwVersion = 1;
	lpPluginInfo->dwSupportStreamType = VF_STREAM_VIDEO;
	strcpy(lpPluginInfo->cPluginInfo, "Blender Frameserver");
	strcpy(lpPluginInfo->cFileType, 
	       "Blender Frame-URL-File (*.blu)|*.blu");

	return VF_OK;
}

static unsigned long getipaddress(const char * ipaddr)
{
	struct hostent  *host;
	unsigned long   ip;

	if (((ip = inet_addr(ipaddr)) == INADDR_NONE)
	&& strcmp(ipaddr, "255.255.255.255") != 0) {
		if ((host = gethostbyname(ipaddr)) != NULL) {
			memcpy(&ip, host->h_addr, sizeof(ip));
		}
	}

	return (ip);
}

static void my_send(SOCKET sock, char * str)
{
	send(sock, str, strlen(str), 0);
}

static int my_recv(SOCKET sock, char * line, int maxlen)
{
	int got = 0;
	int toget = maxlen;

	while (toget > 0) {
		got = recv(sock, line, toget, 0);
		if (got <= 0) {
			return got;
		}
		toget -= got;
		line += got;
	}
	return maxlen;
}

static int my_gets(SOCKET sock, char * line, int maxlen)
{
	int last_rval = 0;

	while (((last_rval = my_recv(sock, line, 1)) == 1) && maxlen > 0) {
		if (*line == '\n') {
			line++;
			*line = 0;
			break;
		} else {
			line++;
			maxlen--;
		}
	}
	return last_rval;
}

typedef struct conndesc_ {
	struct sockaddr_in      addr;
	int width;
	int height;
	int start;
	int end;
	int rate;
	int ratescale;
} conndesc;



HRESULT __stdcall VF_OpenFileFunc_Blen( 
	char *lpFileName, LPVF_FileHandle lpFileHandle )
{
	conndesc * rval;
	char * host;
	char * p;
	int port;
	SOCKET s_in;
	char buf[256];
	struct sockaddr_in      addr;
	FILE* fp;

	p = lpFileName;
	while (*p && *p != '.') p++;
	if (*p) p++;
	if (strcmp(p, "blu") != 0) {
		return VF_ERROR;
	}

	fp = fopen(lpFileName, "r");
	if (!fp) {
		return VF_ERROR;
	}
	fgets(buf, 256, fp);
	fclose(fp);

	host = buf;
	p = host;
	while (*p && *p != ':') p++;
	if (*p) p++;
	p[-1] = 0;
	port = atoi(p);
	if (!port) {
		port = 8080;
	}

	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = getipaddress(host);

	s_in = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (s_in < 0) {
		return VF_ERROR;
	}

	if (connect(s_in, (struct sockaddr*) &addr,
		    sizeof(addr)) < 0) {
		closesocket(s_in);
		return VF_ERROR;
	}

	rval = (conndesc*) malloc(sizeof(conndesc));

	rval->addr = addr;

	my_send(s_in, "GET /info.txt HTTP/1.0\n\n");

	for (;;) {
		char * key;
		char * val;

		if (my_gets(s_in, buf, 250) <= 0) {
			break;
		}

		key = buf;
		val = buf;
		while (*val && *val != ' ') val++;
		if (*val) {
			*val = 0;
			val++;
			
			if (strcmp(key, "width") == 0) {
				rval->width = atoi(val);
			} else if (strcmp(key, "height") == 0) {
				rval->height = atoi(val);
			} else if (strcmp(key, "start") == 0) {
				rval->start = atoi(val);
			} else if (strcmp(key, "end") == 0) {
				rval->end = atoi(val);
			} else if (strcmp(key, "rate") == 0) {
				rval->rate = atoi(val);
			} else if (strcmp(key, "ratescale") == 0) {
				rval->ratescale = atoi(val);
			}
		}
	}

	closesocket(s_in);

	*lpFileHandle = (VF_FileHandle) rval;

	return VF_OK;
}

HRESULT __stdcall VF_CloseFileFunc_Blen( 
	VF_FileHandle hFileHandle )
{
	free((conndesc*) hFileHandle);

	return VF_OK;
}

HRESULT __stdcall VF_GetFileInfoFunc_Blen( 
	VF_FileHandle hFileHandle,
	LPVF_FileInfo lpFileInfo )
{
	conndesc * c = (conndesc*) hFileHandle;
	if (c == 0) { 
		return VF_ERROR; 
	}

	if (lpFileInfo->dwSize != sizeof(VF_FileInfo)) {
		return VF_ERROR;
	}
	
	lpFileInfo->dwHasStreams = VF_STREAM_VIDEO;

	return VF_OK;
}

HRESULT __stdcall VF_GetStreamInfoFunc_Blen( 
	VF_FileHandle hFileHandle,
	DWORD dwStream,void *lpStreamInfo )
{
	conndesc * c = (conndesc*) hFileHandle;

	LPVF_StreamInfo_Video v = (LPVF_StreamInfo_Video) lpStreamInfo;

	if (c == 0 || dwStream != VF_STREAM_VIDEO || v == 0) { 
		return VF_ERROR;
	}

	v->dwLengthL = c->end - c->start;
	v->dwLengthH = 0;
	v->dwScale = c->ratescale;
	v->dwRate = c->rate;
	v->dwWidth = c->width;
	v->dwHeight = c->height;
	v->dwBitCount = 24;

	return VF_OK;
}

HRESULT __stdcall VF_ReadDataFunc_Blen( 
	VF_FileHandle hFileHandle,
	DWORD dwStream,void *lpData )
{
	char req[256];
	char buf[256];
	SOCKET s_in;
	int width;
	int height;
	int y;
	int rval;
	unsigned char * framebuf;

	conndesc * c = (conndesc*) hFileHandle;
	LPVF_ReadData_Video v = (LPVF_ReadData_Video) lpData;

	if (c == 0 || dwStream != VF_STREAM_VIDEO || v == 0) { 
		return VF_ERROR;
	}

	s_in = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (s_in < 0) {
		return VF_ERROR;
	}

	if (connect(s_in, (struct sockaddr*) &c->addr,
		    sizeof(c->addr)) < 0) {
		goto errout;
	}

	sprintf(req, "GET /images/ppm/%d.ppm HTTP/1.0\n\n",
		(int) (v->dwFrameNumberL) + c->start);

	my_send(s_in, req);

	do {
		if (my_gets(s_in, buf, 256) <= 0) {
			goto errout;
		}
	} while (strcmp(buf, "P6\n") != 0);

	do {
		rval = my_gets(s_in, buf, 256);
	} while ( (buf[0] == '#' || buf[0] == '\n') && rval >= 0);

	if (sscanf(buf, "%d %d\n", &width, &height) != 2) {
		goto errout;
	}

	if (width != c->width || height != c->height) {
		goto errout;
	}

	my_gets(s_in, buf, 256); /* 255 */

	framebuf = (unsigned char*) v->lpData;

	for (y = 0; y < height; y++) {
		unsigned char * p = framebuf + v->lPitch * y;
		unsigned char * e = p + width * 3;

		my_recv(s_in, (char*) p, width * 3);
		while (p != e) {
			unsigned char tmp = p[2];
			p[2] = p[0];
			p[0] = tmp;

			p += 3;
		}
	}
	closesocket(s_in);
	return VF_OK;
 errout:
	closesocket(s_in);
	return VF_ERROR;
}

__declspec(dllexport) HRESULT vfGetPluginFunc( 
	LPVF_PluginFunc lpPluginFunc )
{
	if (!lpPluginFunc || lpPluginFunc->dwSize != sizeof(VF_PluginFunc)) {
		return VF_ERROR;
	}

	lpPluginFunc->OpenFile = VF_OpenFileFunc_Blen;
	lpPluginFunc->CloseFile = VF_CloseFileFunc_Blen;
	lpPluginFunc->GetFileInfo = VF_GetFileInfoFunc_Blen;
	lpPluginFunc->GetStreamInfo = VF_GetStreamInfoFunc_Blen;
	lpPluginFunc->ReadData = VF_ReadDataFunc_Blen;

	return VF_OK;
}