1 /*
2  * This is a small DLL that works as a wrapper for the actual realdrv4.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 //000000000000a410 g    DF .text  0000000000000043  G2          RV20toYUV420Free
36 //000000000000a6c0 g    DF .text  0000000000000060  G2          RV20toYUV420CustomMessage
37 //000000000000a200 g    DF .text  000000000000020c  G2          RV20toYUV420Init
38 //000000000000a724 g    DF .text  0000000000000132  G2          RV20toYUV420HiveMessage
39 //000000000000a458 g    DF .text  0000000000000262  G2          RV20toYUV420Transform
40 
41 ulong (*rvyuvCustomMessage)(ulong,ulong);
42 ulong (*rvyuvFree)(ulong);
43 ulong (*rvyuvHiveMessage)(ulong,ulong);
44 ulong (*rvyuvInit)(ulong,ulong);
45 ulong (*rvyuvTransform)(ulong,ulong,ulong,ulong,ulong);
46 
47 //void (*setDLLAccessPath)(ulong);
48 
49 int b_dlOpened=0;
50 void *handle=NULL;
51 
52 /* exits program when failure */
loadSyms(void)53 void loadSyms(void) {
54 	fputs("loadSyms()\n", stderr);
55 	if (!b_dlOpened) {
56 		char *error;
57 
58 		fputs("opening dll...\n",stderr);
59 		handle = dlopen ("/usr/local/RealPlayer8/Codecs/realdrv4.so.6.0", RTLD_LAZY);
60 		if (!handle) {
61 			fputs (dlerror(), stderr);
62 			exit(1);
63 		}
64 
65 		rvyuvCustomMessage = dlsym(handle, "RV20toYUV420CustomMessage");
66 		if ((error = dlerror()) != NULL)  {
67 			fprintf (stderr, "dlsym(rvyuvCustomMessage): %s\n", error);
68 			exit(1);
69 		}
70 		fprintf(stderr, "RV20toYUV420CustomMessage()=0x%0x\n", rvyuvCustomMessage);
71 		rvyuvFree = dlsym(handle, "RV20toYUV420Free");
72 		if ((error = dlerror()) != NULL)  {
73 			fprintf (stderr, "dlsym(rvyuvFree): %s\n", error);
74 			exit(1);
75 		}
76 		fprintf(stderr, "RV20toYUV420Free()=0x%0x\n", rvyuvFree);
77 		rvyuvHiveMessage = dlsym(handle, "RV20toYUV420HiveMessage");
78 		if ((error = dlerror()) != NULL)  {
79 			fprintf (stderr, "dlsym(rvyuvHiveMessage): %s\n", error);
80 			exit(1);
81 		}
82 		fprintf(stderr, "RV20toYUV420HiveMessage()=0x%0x\n", rvyuvHiveMessage);
83 		rvyuvInit = dlsym(handle, "RV20toYUV420Init");
84 		if ((error = dlerror()) != NULL)  {
85 			fprintf (stderr, "dlsym(rvyuvInit): %s\n", error);
86 			exit(1);
87 		}
88 		fprintf(stderr, "RV20toYUV420Init()=0x%0x\n", rvyuvInit);
89 		rvyuvTransform = dlsym(handle, "RV20toYUV420Transform");
90 		if ((error = dlerror()) != NULL)  {
91 			fprintf (stderr, "dlsym(rvyuvTransform): %s\n", error);
92 			exit(1);
93 		}
94 		fprintf(stderr, "RV20toYUV420Transform()=0x%0x\n", rvyuvTransform);
95 		b_dlOpened=1;
96 	}
97 }
98 
closeDll(void)99 void closeDll(void) {
100 	if (handle) {
101 		b_dlOpened=0;
102 		dlclose(handle);
103 		handle=NULL;
104 	}
105 }
106 
_init(void)107 void _init(void) {
108 	loadSyms();
109 }
110 
111 struct timeval tv1, tv2;
112 
tic(void)113 void tic(void) {
114 	gettimeofday(&tv1, NULL);
115 }
116 
toc(void)117 void toc(void) {
118 	long secs, usecs;
119 	gettimeofday(&tv2, NULL);
120 	secs=tv2.tv_sec-tv1.tv_sec;
121 	usecs=tv2.tv_usec-tv1.tv_usec;
122 	if (usecs<0) {
123 		usecs+=1000000;
124 		--secs;
125 	}
126 //	fprintf(stderr, "Duration: %ld.%.6lds\n", secs, usecs);
127 }
128 
129 
hexdump(void * pos,int len)130 static void hexdump(void *pos, int len) {
131 	unsigned char *cpos=pos, *cpos1;
132 	int lines=(len+15)>>4;
133 	while(lines--) {
134 		int len1=len, i;
135 		fprintf(stderr, "#R# %0x  ", (int)cpos-(int)pos);
136 		cpos1=cpos;
137 		for (i=0;i<16;i++) {
138 			if (len1>0) {
139 				fprintf(stderr, "%02x ", *(cpos++));
140 			} else {
141 				fprintf(stderr, "   ");
142 			}
143 			len1--;
144 		}
145 		fputs("  ", stderr);
146 		cpos=cpos1;
147 		for (i=0;i<16;i++) {
148 			if (len>0) {
149 				unsigned char ch=(*(cpos++));
150 				if ((ch<32)||(ch>127)) ch='.';
151 				fputc(ch, stderr);
152 			}
153 			len--;
154 		}
155 		fputs("\n", stderr);
156 	}
157 	fputc('\n', stderr);
158 }
159 
160 
RV20toYUV420CustomMessage(ulong * p1,ulong p2)161 ulong RV20toYUV420CustomMessage(ulong* p1,ulong p2) {
162 	ulong result;
163 //	ulong *pp1=p1;
164 //	ulong temp[16];
165 	fprintf(stderr, "#R# => RV20toYUV420CustomMessage(%p,%p) [%ld,%ld,%ld] \n", p1, p2, p1[0],p1[1],p1[2]);
166 #if 0
167 	if(p1[0]==0x24){
168 	    hexdump(p1[2],64);
169 	    memset(temp,0x77,16*4);
170 	    memcpy(temp,p1[2],16);
171 	    p1[2]=temp;
172 	} else {
173 	    return 0;
174 	}
175 #endif
176 
177 //	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
178 //	hexdump((void*)p1, 12);
179 //	if (pp1[0]==0x24) {
180 //		hexdump((void*)(pp1[2]),128);
181 //	}
182 //	tic();
183 	result=(*rvyuvCustomMessage)(p1,p2);
184 //	toc();
185 	fprintf(stderr, "#R# <= RV20toYUV420CustomMessage --> 0x%0lx(%ld)\n", result, result);
186 	return result;
187 }
188 
RV20toYUV420Free(ulong p1)189 ulong RV20toYUV420Free(ulong p1) {
190 	ulong result;
191 	fprintf(stderr, "RV20toYUV420Free(ulong p1=0x%0lx(%ld))\n", p1, p1);
192 //	hexdump((void*)p1, 44);
193 	tic();
194 	result=(*rvyuvFree)(p1);
195 	toc();
196 //	hexdump((void*)p1, 44);
197 	fprintf(stderr, "RV20toYUV420Free --> 0x%0lx(%ld)\n\n\n", result, result);
198 	return result;
199 }
200 
201 char h_temp[32768];
202 
RV20toYUV420HiveMessage(ulong * p1,ulong p2)203 ulong RV20toYUV420HiveMessage(ulong *p1,ulong p2) {
204 	ulong result;
205 	fprintf(stderr, "#R# RV20toYUV420HiveMessage(%p,%p)\n", p1, p2);
206 //	    p1->constant,p1->width,p1->height,p1->format1,p1->format2);
207 //	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
208 //	hexdump((void*)p1, sizeof(struct init_data));
209 
210 	fprintf(stderr,">HIVE %ld %p\n",p1[0],p1[1]);
211 
212 	fprintf(stderr,"COPY INIT DATA!\n");
213 	memset(h_temp,0x77,1000);
214 	memcpy(h_temp,p1,4);
215 	fprintf(stderr,"COPY OK!\n");
216 
217 //	tic();
218 //	result=(*rvyuvHiveMessage)(p1,p2);
219 	result=(*rvyuvHiveMessage)(h_temp,p2);
220 //	toc();
221 
222 	fprintf(stderr,"COPY INIT DATA!\n");
223 	memcpy(p1,h_temp,8);
224 	fprintf(stderr,"COPY OK!\n");
225 
226 	memset(h_temp,0x77,1000);
227 
228 //	p1[0]=0;
229 //	p1[1]=0x20000000;
230 
231 	fprintf(stderr,"<HIVE %ld %p\n",p1[0],p1[1]);
232 
233 //	hexdump((void*)p1, sizeof(struct init_data));
234 //	hexdump((void*)p1, 8);
235 	fprintf(stderr, "#R# RV20toYUV420HiveMessage --> 0x%0lx(%ld)\n\n", result, result);
236 	return result;
237 }
238 
239 struct init_data {
240 	short constant; //=0xb;
241 	short width, height;
242 	short x1,x2,x3;
243 	// 12
244 	ulong format1;
245 	long  x4;
246 	ulong format2;
247 //	long unknown[32];
248 };
249 
250 static char i_temp[32768];
251 
RV20toYUV420Init(ulong p1,ulong p2)252 ulong RV20toYUV420Init(ulong p1,ulong p2) {
253 	ulong result;
254 	fprintf(stderr, "#R# RV20toYUV420Init(ulong p1=0x%0lx(%ld), ", p1, p1);
255 	fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
256 
257 	fprintf(stderr,"COPY INIT DATA!\n");
258 	memcpy(i_temp,p1,24);
259 	p1=i_temp;
260 	fprintf(stderr,"COPY OK!\n");
261 
262 	hexdump((void*)p1, 24);
263 	tic();
264 	result=(*rvyuvInit)(p1,p2);
265 	toc();
266 	hexdump((void*)p1, 24);
267 
268 	memset(i_temp,0x77,1000);
269 
270 //	hexdump(*((void**)p2), 512);
271 	fprintf(stderr, "#R# RV20toYUV420Init --> 0x%0lx(%ld)\n\n\n", result, result);
272 	return result;
273 }
274 
build_crc(unsigned char * pch,unsigned long len)275 unsigned long build_crc(unsigned char *pch, unsigned long len) {
276 	unsigned long crc=0, a;
277 //	unsigned long b;
278 	// it's not the real crc function, but so what...
279 	while (len--) {
280 		a=*(pch++);
281 //		a=a+(a<<6);
282 //		a^=0x555;
283 //		b=(crc>>29)&7;
284 //		crc=((crc<<3)+b)^a;
285 		crc^=a;
286 	}
287 	return crc;
288 }
289 
290 #define MIN(a,b) ((a)<(b)?(a):(b))
291 
292 // p1=input data (stream)
293 // p2=output buffer
294 // p3=input struct
295 // p4=output struct
296 // p5=rvyuv_main
RV20toYUV420Transform(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5)297 ulong RV20toYUV420Transform(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5) {
298 
299 //result=RV20toYUV420Transform(char *input_stream, char *output_data,
300 //	struct transin *, struct transout *, struct rvyuvMain *);
301 
302 	ulong result;
303 	ulong *pp3=p3;
304 	ulong *pp4=p4;
305 	void *v;
306 	ulong temp[128];
307 	int i;
308 
309 	unsigned long crc_src, crc0;
310 //	unsigned long len, crc1, crc2;
311 	unsigned char *pch=(char *)p1;
312 	fprintf(stderr, "#R# RV20toYUV420Transform(in=%p,out=%p,tin=%p,tout=%p,yuv=%p)\n",p1,p2,p3,p4,p5);
313 	// input data, length=*p3
314 //	hexdump((void*)p1, /*MIN(64,*/ *((ulong*)p3) /*)*/ );
315 //	v=p5;
316 //	v+=0x3c;
317 //	v=*((void **)v);
318 //	pp3=v;
319 //	len=pp3[3]*pp3[4]*3/2;
320 //	pch=p2;
321 //	while(--len) *(pch++)=0;
322 //	hexdump((char*)p2, 64);
323 //	hexdump((void*)p3, 32);
324 //	hexdump((void*)p5, 64);
325 //	pp3=p3;
326 //	if (pp3[3]>1024) {
327 //		hexdump((void*)(pp3[3]),32);
328 //		pp3=pp3[3];
329 //	}
330 
331 	pp3=p3;
332 	// it's not the real crc function, but so what...
333 	pch=p1;
334 	crc_src=build_crc(pch, pp3[0]);
335 
336 	pp4=pp3[3];
337 	fprintf(stderr,"transin1[%p]: {%ld/%ld} ",pp4,pp3[2],pp3[0]);
338 //	pp4[0],pp4[1],pp4[2],pp4[3],
339 //	pp4[4],pp4[5],pp4[6],pp4[7]);
340 
341 	memset(temp,0x77,128*4);
342 
343 	memcpy(temp,pp4,8*(pp3[2]+1));
344 	for(i=0;i<=pp3[2];i++){
345 	    fprintf(stderr," %p(%ld)",temp[i*2],temp[i*2+1]);
346 	}
347         fprintf(stderr,"\n");
348 
349 
350 	pp3[3]=pp4=temp;
351 
352 //	pp4[2]=
353 //	pp4[3]=
354 //	pp4[4]=NULL;
355 
356 	//pp4[6]=pp4[5];
357 
358 	v=p5;
359 /*	fprintf(stderr, "rvyuvMain=0x%0x\n", v);
360 	v+=0x3c;
361 	v=*((void **)v);
362 	fprintf(stderr, "[$+3ch]=0x%0x\n", v);
363 	hexdump(v, 512);
364 	v+=0x60;
365 	v=*((void **)v);
366 	fprintf(stderr, "[$+60h]=0x%0x\n", v);
367 	hexdump(v, 512);
368 	v+=0x28;
369 	v=*((void **)v);
370 	fprintf(stderr, "[$+28h]=0x%0x\n", v);
371 	hexdump(v, 512);
372 */
373 /*	v+=0x178;
374 	hexdump(v, 16);
375 	v=*((void **)v);
376 	if (v>0x8000000) {
377 		fprintf(stderr, "[$+178h]=0x%0x\n", v);
378 		hexdump(v, 128);
379 	}
380 */
381 //	tic();
382 	result=(*rvyuvTransform)(p1,p2,p3,p4,p5);
383 //	toc();
384 
385 	crc0=build_crc(p2, 176*144);
386 //	crc1=build_crc(p2+pp4[3]*pp4[4]/2, pp4[3]*pp4[4]/2);
387 //	crc2=build_crc(p2+pp4[3]*pp4[4], pp4[3]*pp4[4]/2);
388 
389 //	pp3=p3;
390 	// TRANSFORM: <timestamp> <numblocks> <len> <crc_src> <crc_dest> <p4[4]>
391 //	fprintf(stderr, "TRAFO:\t%ld\t%ld\t%ld\t%.8lX\t%.8lX\t%ld\n",
392 //		pp3[5], pp3[2], pp3[0], crc_src, crc0, pp3[4]);
393 	fprintf(stderr, "#R# Decode: %ld(%ld) [%08lX] pts=%ld -> %ld [%08lX]\n",
394 	    pp3[0],pp3[2],crc_src,pp3[5],
395 	    result,crc0);
396 
397 	// output
398 //	hexdump((char*)p2, /*64*/ pp4[3]*pp4[4]/2);
399 //	hexdump((void*)p4, 20);
400 //	hexdump((void*)p5, 512);
401 //	fprintf(stderr, "RV20toYUV420Transform --> 0x%0lx(%ld)\n\n\n", result, result);
402 	return result;
403 }
404