1 /*
2  * This is a small DLL that works as a wrapper for the actual realdrv2.so.6.0
3  * DLL from RealPlayer 8.0.
4  *
5  * This file is part of MPlayer.
6  *
7  * MPlayer is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * MPlayer is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 /*
23    Assuming that RACloseCodec is the last call.
24 */
25 
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <dlfcn.h>
31 #include <sys/time.h>
32 
33 typedef unsigned long ulong;
34 
35 ulong (*rvyuvCustomMessage)(ulong,ulong);
36 ulong (*rvyuvFree)(ulong);
37 ulong (*rvyuvHiveMessage)(ulong,ulong);
38 ulong (*rvyuvInit)(ulong,ulong);
39 ulong (*rvyuvTransform)(ulong,ulong,ulong,ulong,ulong);
40 ulong (*rvyuvRNFRUFree)(ulong);
41 ulong (*rvyuvRNFRUGetFrame)(ulong,ulong,ulong);
42 ulong (*rvyuvRNFRUInit)(ulong,ulong,ulong);
43 ulong (*rvyuvRNFRUSetup)(ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong);
44 
45 //void (*setDLLAccessPath)(ulong);
46 
47 int b_dlOpened=0;
48 void *handle=NULL;
49 
50 /* exits program when failure */
loadSyms(void)51 void loadSyms(void) {
52 	fputs("loadSyms()\n", stderr);
53 	if (!b_dlOpened) {
54 		char *error;
55 
56 		fputs("opening dll...\n",stderr);
57 		handle = dlopen ("/usr/local/RealPlayer8/Codecs/realdrv2.so.6.0", RTLD_LAZY);
58 		if (!handle) {
59 			fputs (dlerror(), stderr);
60 			exit(1);
61 		}
62 
63 		rvyuvCustomMessage = dlsym(handle, "RV20toYUV420CustomMessage");
64 		if ((error = dlerror()) != NULL)  {
65 			fprintf (stderr, "dlsym(rvyuvCustomMessage): %s\n", error);
66 			exit(1);
67 		}
68 		fprintf(stderr, "RV20toYUV420CustomMessage()=0x%0x\n", rvyuvCustomMessage);
69 		rvyuvFree = dlsym(handle, "RV20toYUV420Free");
70 		if ((error = dlerror()) != NULL)  {
71 			fprintf (stderr, "dlsym(rvyuvFree): %s\n", error);
72 			exit(1);
73 		}
74 		fprintf(stderr, "RV20toYUV420Free()=0x%0x\n", rvyuvFree);
75 		rvyuvHiveMessage = dlsym(handle, "RV20toYUV420HiveMessage");
76 		if ((error = dlerror()) != NULL)  {
77 			fprintf (stderr, "dlsym(rvyuvHiveMessage): %s\n", error);
78 			exit(1);
79 		}
80 		fprintf(stderr, "RV20toYUV420HiveMessage()=0x%0x\n", rvyuvHiveMessage);
81 		rvyuvInit = dlsym(handle, "RV20toYUV420Init");
82 		if ((error = dlerror()) != NULL)  {
83 			fprintf (stderr, "dlsym(rvyuvInit): %s\n", error);
84 			exit(1);
85 		}
86 		fprintf(stderr, "RV20toYUV420Init()=0x%0x\n", rvyuvInit);
87 		rvyuvTransform = dlsym(handle, "RV20toYUV420Transform");
88 		if ((error = dlerror()) != NULL)  {
89 			fprintf (stderr, "dlsym(rvyuvTransform): %s\n", error);
90 			exit(1);
91 		}
92 		fprintf(stderr, "RV20toYUV420Transform()=0x%0x\n", rvyuvTransform);
93 		rvyuvRNFRUFree = dlsym(handle, "RV20toYUV420_RN_FRU_Free");
94 		if ((error = dlerror()) != NULL)  {
95 			fprintf (stderr, "dlsym(rvyuvRNFRUFree): %s\n", error);
96 			exit(1);
97 		}
98 		fprintf(stderr, "RV20toYUV420_RN_FRU_Free()=0x%0x\n", rvyuvRNFRUFree);
99 		rvyuvRNFRUGetFrame = dlsym(handle, "RV20toYUV420_RN_FRU_GetFrame");
100 		if ((error = dlerror()) != NULL)  {
101 			fprintf (stderr, "dlsym(rvyuvRNFRUGetFrame): %s\n", error);
102 			exit(1);
103 		}
104 		fprintf(stderr, "RV20toYUV420_RN_FRU_GetFrame()=0x%0x\n", rvyuvRNFRUGetFrame);
105 		rvyuvRNFRUInit = dlsym(handle, "RV20toYUV420_RN_FRU_Init");
106 		if ((error = dlerror()) != NULL)  {
107 			fprintf (stderr, "dlsym(rvyuvRNFRUInit): %s\n", error);
108 			exit(1);
109 		}
110 		fprintf(stderr, "RV20toYUV420_RN_FRU_Init()=0x%0x\n", rvyuvRNFRUInit);
111 		rvyuvRNFRUSetup = dlsym(handle, "RV20toYUV420_RN_FRU_Setup");
112 		if ((error = dlerror()) != NULL)  {
113 			fprintf (stderr, "dlsym(rvyuvRNFRUSetup): %s\n", error);
114 			exit(1);
115 		}
116 		fprintf(stderr, "RV20toYUV420_RN_FRU_Setup()=0x%0x\n", rvyuvRNFRUSetup);
117 
118 /*		setDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
119 		if ((error = dlerror()) != NULL)  {
120 			fprintf (stderr, "dlsym(SetDLLAccessPath): %s\n", error);
121 			exit(1);
122 		}
123 */		b_dlOpened=1;
124 	}
125 }
126 
closeDll(void)127 void closeDll(void) {
128 	if (handle) {
129 		b_dlOpened=0;
130 		dlclose(handle);
131 		handle=NULL;
132 	}
133 }
134 
_init(void)135 void _init(void) {
136 	loadSyms();
137 }
138 
139 struct timeval tv1, tv2;
140 
tic(void)141 void tic(void) {
142 	gettimeofday(&tv1, NULL);
143 }
144 
toc(void)145 void toc(void) {
146 	long secs, usecs;
147 	gettimeofday(&tv2, NULL);
148 	secs=tv2.tv_sec-tv1.tv_sec;
149 	usecs=tv2.tv_usec-tv1.tv_usec;
150 	if (usecs<0) {
151 		usecs+=1000000;
152 		--secs;
153 	}
154 //	fprintf(stderr, "Duration: %ld.%.6lds\n", secs, usecs);
155 }
156 
157 
hexdump(void * pos,int len)158 static void hexdump(void *pos, int len) {
159 	unsigned char *cpos=pos, *cpos1;
160 	int lines=(len+15)>>4;
161 	while(lines--) {
162 		int len1=len, i;
163 		fprintf(stderr, "#R# %0x  ", (int)cpos-(int)pos);
164 		cpos1=cpos;
165 		for (i=0;i<16;i++) {
166 			if (len1>0) {
167 				fprintf(stderr, "%02x ", *(cpos++));
168 			} else {
169 				fprintf(stderr, "   ");
170 			}
171 			len1--;
172 		}
173 		fputs("  ", stderr);
174 		cpos=cpos1;
175 		for (i=0;i<16;i++) {
176 			if (len>0) {
177 				unsigned char ch=(*(cpos++));
178 				if ((ch<32)||(ch>127)) ch='.';
179 				fputc(ch, stderr);
180 			}
181 			len--;
182 		}
183 		fputs("\n", stderr);
184 	}
185 	fputc('\n', stderr);
186 }
187 
188 
RV20toYUV420CustomMessage(ulong * p1,ulong p2)189 ulong RV20toYUV420CustomMessage(ulong* p1,ulong p2) {
190 	ulong result;
191 //	ulong *pp1=p1;
192 	ulong temp[16];
193 	fprintf(stderr, "#R# => RV20toYUV420CustomMessage(%p,%p) [%ld,%ld,%ld] \n", p1, p2, p1[0],p1[1],p1[2]);
194 
195 /*
196 	if(p1[0]!=17) return 0;
197 
198 	if(p1[1]!=1) return 0;
199 
200 	if(p1[0]==105) return 0;
201 	if(p1[0]==3) return 0;
202 	if(p1[0]==18) return 0;
203 	if(p1[0]==30) return 0;
204 */
205 
206 	if(p1[0]==0x24){
207 	    hexdump(p1[2],64);
208 	    memset(temp,0x77,16*4);
209 	    memcpy(temp,p1[2],16);
210 	    p1[2]=temp;
211 	} else {
212 	    switch(p1[0]){
213 	    case 17:
214 		if(p1[1]==1)
215 		break;
216 	    case 18:
217 	    case 30:
218 	    case 3:
219 	    default:
220 		return 0;
221 	    }
222 	}
223 //	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
224 //	hexdump((void*)p1, 12);
225 //	if (pp1[0]==0x24) {
226 //		hexdump((void*)(pp1[2]),128);
227 //	}
228 //	tic();
229 	result=(*rvyuvCustomMessage)(p1,p2);
230 //	toc();
231 	fprintf(stderr, "#R# <= RV20toYUV420CustomMessage --> 0x%0lx(%ld)\n", result, result);
232 	return result;
233 }
234 
RV20toYUV420Free(ulong p1)235 ulong RV20toYUV420Free(ulong p1) {
236 	ulong result;
237 	fprintf(stderr, "RV20toYUV420Free(ulong p1=0x%0lx(%ld))\n", p1, p1);
238 //	hexdump((void*)p1, 44);
239 	tic();
240 	result=(*rvyuvFree)(p1);
241 	toc();
242 //	hexdump((void*)p1, 44);
243 	fprintf(stderr, "RV20toYUV420Free --> 0x%0lx(%ld)\n\n\n", result, result);
244 	return result;
245 }
246 
247 char h_temp[32768];
248 
RV20toYUV420HiveMessage(ulong * p1,ulong p2)249 ulong RV20toYUV420HiveMessage(ulong *p1,ulong p2) {
250 	ulong result;
251 	fprintf(stderr, "#R# RV20toYUV420HiveMessage(%p,%p)\n", p1, p2);
252 //	    p1->constant,p1->width,p1->height,p1->format1,p1->format2);
253 //	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
254 //	hexdump((void*)p1, sizeof(struct init_data));
255 
256 	fprintf(stderr,">HIVE %ld %p\n",p1[0],p1[1]);
257 
258 	if(p1[0]!=0) return 0;
259 
260 	p1[1]=0x10000000;
261 	return 0;
262 
263 //	fprintf(stderr,"COPY INIT DATA!\n");
264 	memset(h_temp,0x77,1000);
265 	memcpy(h_temp,p1,4);
266 //	fprintf(stderr,"COPY OK!\n");
267 
268 //	tic();
269 //	result=(*rvyuvHiveMessage)(p1,p2);
270 	result=(*rvyuvHiveMessage)(h_temp,p2);
271 //	toc();
272 
273 	fprintf(stderr,"COPY INIT DATA!\n");
274 	memcpy(p1,h_temp,8);
275 	fprintf(stderr,"COPY OK!\n");
276 
277 	memset(h_temp,0x77,1000);
278 
279 //	p1[0]=0;
280 //	p1[1]=0x20000000;
281 
282 	fprintf(stderr,"<HIVE %ld %p\n",p1[0],p1[1]);
283 
284 //	hexdump((void*)p1, sizeof(struct init_data));
285 //	hexdump((void*)p1, 8);
286 	fprintf(stderr, "#R# RV20toYUV420HiveMessage --> 0x%0lx(%ld)\n\n", result, result);
287 	return result;
288 }
289 
290 struct init_data {
291 	short constant; //=0xb;
292 	short width, height;
293 	short x1,x2,x3;
294 	// 12
295 	ulong format1;
296 	long  x4;
297 	ulong format2;
298 //	long unknown[32];
299 };
300 
301 static char i_temp[32768];
302 
RV20toYUV420Init(ulong p1,ulong p2)303 ulong RV20toYUV420Init(ulong p1,ulong p2) {
304 	ulong result;
305 	fprintf(stderr, "#R# RV20toYUV420Init(ulong p1=0x%0lx(%ld), ", p1, p1);
306 	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
307 
308 	fprintf(stderr,"COPY INIT DATA!\n");
309 	memcpy(i_temp,p1,24);
310 	p1=i_temp;
311 	fprintf(stderr,"COPY OK!\n");
312 
313 	hexdump((void*)p1, 24);
314 	tic();
315 	result=(*rvyuvInit)(p1,p2);
316 	toc();
317 	hexdump((void*)p1, 24);
318 
319 	memset(i_temp,0x77,1000);
320 
321 //	hexdump(*((void**)p2), 512);
322 	fprintf(stderr, "#R# RV20toYUV420Init --> 0x%0lx(%ld)\n\n\n", result, result);
323 	return result;
324 }
325 
build_crc(unsigned char * pch,unsigned long len)326 unsigned long build_crc(unsigned char *pch, unsigned long len) {
327 	unsigned long crc=0, a;
328 //	unsigned long b;
329 	// it's not the real crc function, but so what...
330 	while (len--) {
331 		a=*(pch++);
332 //		a=a+(a<<6);
333 //		a^=0x555;
334 //		b=(crc>>29)&7;
335 //		crc=((crc<<3)+b)^a;
336 		crc^=a;
337 	}
338 	return crc;
339 }
340 
341 #define MIN(a,b) ((a)<(b)?(a):(b))
342 
343 // p1=input data (stream)
344 // p2=output buffer
345 // p3=input struct
346 // p4=output struct
347 // p5=rvyuv_main
RV20toYUV420Transform(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5)348 ulong RV20toYUV420Transform(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5) {
349 
350 //result=RV20toYUV420Transform(char *input_stream, char *output_data,
351 //	struct transin *, struct transout *, struct rvyuvMain *);
352 
353 	ulong result;
354 	ulong *pp3=p3;
355 	ulong *pp4=p4;
356 	void *v;
357 	ulong temp[128];
358 	int i;
359 
360 	unsigned long crc_src, crc0;
361 //	unsigned long len, crc1, crc2;
362 	unsigned char *pch=(char *)p1;
363 	fprintf(stderr, "#R# RV20toYUV420Transform(in=%p,out=%p,tin=%p,tout=%p,yuv=%p)\n",p1,p2,p3,p4,p5);
364 	// input data, length=*p3
365 //	hexdump((void*)p1, /*MIN(64,*/ *((ulong*)p3) /*)*/ );
366 //	v=p5;
367 //	v+=0x3c;
368 //	v=*((void **)v);
369 //	pp3=v;
370 //	len=pp3[3]*pp3[4]*3/2;
371 //	pch=p2;
372 //	while(--len) *(pch++)=0;
373 //	hexdump((char*)p2, 64);
374 //	hexdump((void*)p3, 32);
375 //	hexdump((void*)p5, 64);
376 //	pp3=p3;
377 //	if (pp3[3]>1024) {
378 //		hexdump((void*)(pp3[3]),32);
379 //		pp3=pp3[3];
380 //	}
381 
382 	pp3=p3;
383 	// it's not the real crc function, but so what...
384 	pch=p1;
385 	crc_src=build_crc(pch, pp3[0]);
386 
387 	pp4=pp3[3];
388 	fprintf(stderr,"transin1[%p]: {%ld/%ld} ",pp4,pp3[2],pp3[0]);
389 //	pp4[0],pp4[1],pp4[2],pp4[3],
390 //	pp4[4],pp4[5],pp4[6],pp4[7]);
391 
392 	memset(temp,0x77,128*4);
393 
394 	memcpy(temp,pp4,8*(pp3[2]+1));
395 	for(i=0;i<=pp3[2];i++){
396 	    fprintf(stderr," %p(%ld)",temp[i*2],temp[i*2+1]);
397 	}
398         fprintf(stderr,"\n");
399 
400 
401 	pp3[3]=pp4=temp;
402 
403 //	pp4[2]=
404 //	pp4[3]=
405 //	pp4[4]=NULL;
406 
407 	//pp4[6]=pp4[5];
408 
409 	v=p5;
410 /*	fprintf(stderr, "rvyuvMain=0x%0x\n", v);
411 	v+=0x3c;
412 	v=*((void **)v);
413 	fprintf(stderr, "[$+3ch]=0x%0x\n", v);
414 	hexdump(v, 512);
415 	v+=0x60;
416 	v=*((void **)v);
417 	fprintf(stderr, "[$+60h]=0x%0x\n", v);
418 	hexdump(v, 512);
419 	v+=0x28;
420 	v=*((void **)v);
421 	fprintf(stderr, "[$+28h]=0x%0x\n", v);
422 	hexdump(v, 512);
423 */
424 /*	v+=0x178;
425 	hexdump(v, 16);
426 	v=*((void **)v);
427 	if (v>0x8000000) {
428 		fprintf(stderr, "[$+178h]=0x%0x\n", v);
429 		hexdump(v, 128);
430 	}
431 */
432 //	tic();
433 	result=(*rvyuvTransform)(p1,p2,p3,p4,p5);
434 //	toc();
435 
436 	crc0=build_crc(p2, 176*144);
437 //	crc1=build_crc(p2+pp4[3]*pp4[4]/2, pp4[3]*pp4[4]/2);
438 //	crc2=build_crc(p2+pp4[3]*pp4[4], pp4[3]*pp4[4]/2);
439 
440 //	pp3=p3;
441 	// TRANSFORM: <timestamp> <numblocks> <len> <crc_src> <crc_dest> <p4[4]>
442 //	fprintf(stderr, "TRAFO:\t%ld\t%ld\t%ld\t%.8lX\t%.8lX\t%ld\n",
443 //		pp3[5], pp3[2], pp3[0], crc_src, crc0, pp3[4]);
444 	fprintf(stderr, "#R# Decode: %ld(%ld) [%08lX] pts=%ld -> %ld [%08lX]\n",
445 	    pp3[0],pp3[2],crc_src,pp3[5],
446 	    result,crc0);
447 
448 	// output
449 //	hexdump((char*)p2, /*64*/ pp4[3]*pp4[4]/2);
450 //	hexdump((void*)p4, 20);
451 //	hexdump((void*)p5, 512);
452 //	fprintf(stderr, "RV20toYUV420Transform --> 0x%0lx(%ld)\n\n\n", result, result);
453 	return result;
454 }
455 
RV20toYUV420_RN_FRU_Free(ulong p1)456 ulong RV20toYUV420_RN_FRU_Free(ulong p1) {
457 	ulong result;
458 	fprintf(stderr, "RV20toYUV420_RN_FRU_Free(ulong p1=0x%0lx(%ld))\n", p1, p1);
459 	tic();
460 	result=(*rvyuvRNFRUFree)(p1);
461 	toc();
462 	fprintf(stderr, "RV20toYUV420_RN_FRU_Free --> 0x%0lx(%ld)\n\n\n", result, result);
463 	return result;
464 }
465 
RV20toYUV420_RN_FRU_GetFrame(ulong p1,ulong p2,ulong p3)466 ulong RV20toYUV420_RN_FRU_GetFrame(ulong p1,ulong p2,ulong p3) {
467 	ulong result;
468 //	fprintf(stderr, "RV20toYUV420_RN_FRU_GetFrame(ulong p1=0x%0lx(%ld), ", p1, p1);
469 //	fprintf(stderr, "ulong p2=0x%0lx(%ld),\n\t", p2, p2);
470 //	fprintf(stderr, "ulong p3=0x%0lx(%ld))\n", p3, p3);
471 //	hexdump((void*)p1, 44);
472 	tic();
473 	result=(*rvyuvRNFRUGetFrame)(p1,p2,p3);
474 	toc();
475 //	hexdump((void*)p1, 44);
476 //	fprintf(stderr, "RV20toYUV420_RN_FRU_GetFrame --> 0x%0lx(%ld)\n\n\n", result, result);
477 	return result;
478 }
479 
RV20toYUV420_RN_FRU_Init(ulong p1,ulong p2,ulong p3)480 ulong RV20toYUV420_RN_FRU_Init(ulong p1,ulong p2,ulong p3) {
481 	ulong result;
482 //	fprintf(stderr, "RV20toYUV420_RN_FRU_Init(ulong p1=0x%0lx(%ld), ", p1, p1);
483 //	fprintf(stderr, "ulong p2=0x%0lx(%ld),\n\t", p2, p2);
484 //	fprintf(stderr, "ulong p3=0x%0lx(%ld))\n", p3, p3);
485 //	hexdump((void*)p1, 44);
486 	tic();
487 	result=(*rvyuvRNFRUInit)(p1,p2,p3);
488 	toc();
489 //	hexdump((void*)p1, 44);
490 //	fprintf(stderr, "RV20toYUV420_RN_FRU_Init --> 0x%0lx(%ld)\n\n\n", result, result);
491 	return result;
492 }
493 
RV20toYUV420_RN_FRU_Setup(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5,ulong p6,ulong p7,ulong p8)494 ulong RV20toYUV420_RN_FRU_Setup(ulong p1,ulong p2,ulong p3,ulong p4,
495 	ulong p5,ulong p6,ulong p7,ulong p8) {
496 	ulong result;
497 //	fprintf(stderr, "RV20toYUV420_RN_FRU_Setup(ulong p1=0x%0lx(%ld), ", p1, p1);
498 //	fprintf(stderr, "ulong p2=0x%0lx(%ld),\n\t", p2, p2);
499 //	fprintf(stderr, "ulong p3=0x%0lx(%ld), ", p3, p3);
500 //	fprintf(stderr, "ulong p4=0x%0lx(%ld),\n\t", p4, p4);
501 //	fprintf(stderr, "ulong p5=0x%0lx(%ld), ", p5, p5);
502 //	fprintf(stderr, "ulong p6=0x%0lx(%ld),\n\t", p6, p6);
503 //	fprintf(stderr, "ulong p7=0x%0lx(%ld), ", p7, p7);
504 //	fprintf(stderr, "ulong p8=0x%0lx(%ld))\n", p8, p8);
505 //	hexdump((void*)p1, 44);
506 	tic();
507 	result=(*rvyuvRNFRUSetup)(p1,p2,p3,p4,p5,p6,p7,p8);
508 	toc();
509 //	hexdump((void*)p1, 44);
510 //	fprintf(stderr, "RV20toYUV420_RN_FRU_Setup --> 0x%0lx(%ld)\n\n\n", result, result);
511 	return result;
512 }
513 
514 /*void  SetDLLAccessPath(ulong p1) {
515 	fprintf(stderr, "SetDLLAccessPath(ulong p1=0x%0lx(%ld))\n", p1, p1);
516 	hexdump((void*)p1, 44);
517 	(*setDLLAccessPath)(p1);
518 	hexdump((void*)p1, 44);
519 	fprintf(stderr, "--> void\n\n\n");
520 }
521 */
522