6341
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #include "config.h"
|
|
5
|
10093
|
6 #ifdef HAVE_LIBDL
|
6341
|
7 #include <dlfcn.h>
|
10093
|
8 #endif
|
6341
|
9
|
|
10 #include "mp_msg.h"
|
|
11 #include "help_mp.h"
|
|
12
|
|
13 #include "vd_internal.h"
|
22577
|
14 #include "loader/wine/windef.h"
|
6341
|
15
|
|
16 static vd_info_t info = {
|
6404
|
17 "RealVideo decoder",
|
7174
|
18 "realvid",
|
12628
|
19 "Alex Beregszaszi",
|
|
20 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
|
6341
|
21 "binary real video codecs"
|
|
22 };
|
|
23
|
7174
|
24 LIBVD_EXTERN(realvid)
|
6341
|
25
|
|
26
|
8522
|
27 /*
|
|
28 * Structures for data packets. These used to be tables of unsigned ints, but
|
|
29 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
|
|
30 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
|
|
31 * So we have to use structures so the compiler will assign the proper space
|
|
32 * for the pointer.
|
|
33 */
|
|
34 typedef struct cmsg_data_s {
|
|
35 uint32_t data1;
|
|
36 uint32_t data2;
|
|
37 uint32_t* dimensions;
|
|
38 } cmsg_data_t;
|
|
39
|
|
40 typedef struct transform_in_s {
|
|
41 uint32_t len;
|
|
42 uint32_t unknown1;
|
|
43 uint32_t chunks;
|
|
44 uint32_t* extra;
|
|
45 uint32_t unknown2;
|
|
46 uint32_t timestamp;
|
|
47 } transform_in_t;
|
|
48
|
|
49 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
|
7728
|
50 static unsigned long (*rvyuv_free)(void*);
|
|
51 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
|
8522
|
52 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
|
8525
|
53 #ifdef USE_WIN32DLL
|
|
54 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
|
|
55 static unsigned long WINAPI (*wrvyuv_free)(void*);
|
|
56 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
|
|
57 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
|
|
58 #endif
|
6341
|
59
|
7728
|
60 static void *rv_handle=NULL;
|
25962
|
61 static int initialized=0;
|
21559
|
62 static uint8_t *buffer = NULL;
|
21563
|
63 static int bufsz = 0;
|
8525
|
64 #ifdef USE_WIN32DLL
|
|
65 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
|
|
66 #endif
|
6341
|
67
|
|
68 void *__builtin_vec_new(unsigned long size) {
|
|
69 return malloc(size);
|
|
70 }
|
|
71
|
|
72 void __builtin_vec_delete(void *mem) {
|
|
73 free(mem);
|
|
74 }
|
|
75
|
|
76 void __pure_virtual(void) {
|
6361
|
77 printf("FATAL: __pure_virtual() called!\n");
|
|
78 // exit(1);
|
6341
|
79 }
|
|
80
|
15566
|
81 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
6361
|
82 void ___brk_addr(void) {exit(0);}
|
6404
|
83 char **__environ={NULL};
|
6361
|
84 #undef stderr
|
6404
|
85 FILE *stderr=NULL;
|
6361
|
86 #endif
|
6341
|
87
|
|
88 // to set/get/query special features/parameters
|
|
89 static int control(sh_video_t *sh,int cmd,void* arg,...){
|
|
90 // switch(cmd){
|
|
91 // case VDCTRL_QUERY_MAX_PP_LEVEL:
|
|
92 // return 9;
|
|
93 // case VDCTRL_SET_PP_LEVEL:
|
|
94 // vfw_set_postproc(sh,10*(*((int*)arg)));
|
|
95 // return CONTROL_OK;
|
|
96 // }
|
|
97 return CONTROL_UNKNOWN;
|
|
98 }
|
|
99
|
|
100 /* exits program when failure */
|
10093
|
101 #ifdef HAVE_LIBDL
|
7728
|
102 static int load_syms_linux(char *path) {
|
6341
|
103 void *handle;
|
|
104
|
17721
|
105 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
|
9386
|
106 handle = dlopen (path, RTLD_LAZY);
|
6341
|
107 if (!handle) {
|
6361
|
108 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
|
6341
|
109 return 0;
|
|
110 }
|
|
111
|
|
112 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
|
|
113 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
|
|
114 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
|
|
115 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
|
6361
|
116
|
|
117 if(rvyuv_custom_message &&
|
|
118 rvyuv_free &&
|
|
119 rvyuv_init &&
|
9386
|
120 rvyuv_transform)
|
|
121 {
|
|
122 rv_handle = handle;
|
|
123 return 1;
|
|
124 }
|
13860
|
125
|
|
126 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
|
|
127 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
|
|
128 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
|
|
129 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
|
|
130
|
|
131 if(rvyuv_custom_message &&
|
|
132 rvyuv_free &&
|
|
133 rvyuv_init &&
|
|
134 rvyuv_transform)
|
|
135 {
|
|
136 rv_handle = handle;
|
|
137 return 1;
|
|
138 }
|
6361
|
139
|
|
140 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
|
9386
|
141 dlclose(handle);
|
6361
|
142 return 0;
|
6341
|
143 }
|
10093
|
144 #endif
|
6341
|
145
|
6543
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
146 #ifdef USE_WIN32DLL
|
7127
|
147
|
10093
|
148 #ifdef WIN32_LOADER
|
17012
|
149 #include "loader/ldt_keeper.h"
|
10093
|
150 #endif
|
8525
|
151 void* WINAPI LoadLibraryA(char* name);
|
|
152 void* WINAPI GetProcAddress(void* handle,char* func);
|
|
153 int WINAPI FreeLibrary(void *handle);
|
7127
|
154
|
24087
|
155 #ifndef WIN32_LOADER
|
|
156 void * WINAPI GetModuleHandleA(char *);
|
|
157 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
|
|
158 const uint8_t *newcode, int codesize) {
|
|
159 void *handle = GetModuleHandleA("kernel32");
|
|
160 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
|
|
161 int res = 0;
|
|
162 int prot, tmp;
|
|
163 VirtProt = GetProcAddress(handle, "VirtualProtect");
|
|
164 // change permissions to PAGE_WRITECOPY
|
|
165 if (!VirtProt ||
|
|
166 !VirtProt(patchpos, codesize, 0x08, &prot)) {
|
|
167 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
|
|
168 return 0;
|
|
169 }
|
|
170 if (memcmp(patchpos, oldcode, codesize) == 0) {
|
|
171 memcpy(patchpos, newcode, codesize);
|
|
172 res = 1;
|
|
173 }
|
|
174 VirtProt(patchpos, codesize, prot, &tmp);
|
|
175 return res;
|
|
176 }
|
|
177 #endif
|
|
178
|
7728
|
179 static int load_syms_windows(char *path) {
|
6360
|
180 void *handle;
|
8525
|
181
|
17721
|
182 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
|
10093
|
183 #ifdef WIN32_LOADER
|
6360
|
184 Setup_LDT_Keeper();
|
10093
|
185 #endif
|
9386
|
186 handle = LoadLibraryA(path);
|
6361
|
187 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
|
8525
|
188 if (!handle) {
|
|
189 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
|
|
190 return 0;
|
|
191 }
|
6360
|
192
|
8525
|
193 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
|
|
194 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
|
|
195 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
|
|
196 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
|
9386
|
197
|
8525
|
198 if(wrvyuv_custom_message &&
|
|
199 wrvyuv_free &&
|
|
200 wrvyuv_init &&
|
9386
|
201 wrvyuv_transform)
|
|
202 {
|
|
203 dll_type = 1;
|
|
204 rv_handle = handle;
|
24087
|
205 #ifndef WIN32_LOADER
|
|
206 {
|
26389
|
207 if (strstr(path, "drv43260.dll")) {
|
26385
|
208 int patched;
|
24087
|
209 // patch away multithreaded decoding, it causes crashes
|
|
210 static const uint8_t oldcode[13] = {
|
|
211 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
|
|
212 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
|
|
213 static const uint8_t newcode[13] = {
|
|
214 0x31, 0xc0,
|
|
215 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
|
|
216 0xe9, 0xd0, 0x00, 0x00, 0x00 };
|
26388
|
217 patched = patch_dll(
|
|
218 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
|
|
219 oldcode, newcode, sizeof(oldcode));
|
26386
|
220 if (!patched)
|
|
221 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
|
26385
|
222 }
|
24087
|
223 }
|
|
224 #endif
|
9386
|
225 return 1;
|
|
226 }
|
26384
|
227
|
|
228 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
|
|
229 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
|
|
230 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
|
|
231 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
|
|
232 if(wrvyuv_custom_message &&
|
|
233 wrvyuv_free &&
|
|
234 wrvyuv_init &&
|
26398
|
235 wrvyuv_transform) {
|
|
236 dll_type = 1;
|
|
237 rv_handle = handle;
|
26384
|
238 return 1;
|
26398
|
239 }
|
26384
|
240
|
8525
|
241 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
|
9386
|
242 FreeLibrary(handle);
|
6360
|
243 return 0; // error
|
|
244 }
|
|
245 #endif
|
|
246
|
6341
|
247 /* we need exact positions */
|
|
248 struct rv_init_t {
|
|
249 short unk1;
|
|
250 short w;
|
|
251 short h;
|
|
252 short unk3;
|
|
253 int unk2;
|
6345
|
254 int subformat;
|
6341
|
255 int unk5;
|
|
256 int format;
|
|
257 } rv_init_t;
|
|
258
|
|
259 // init driver
|
|
260 static int init(sh_video_t *sh){
|
|
261 //unsigned int out_fmt;
|
10102
|
262 char *path;
|
6341
|
263 int result;
|
6345
|
264 // we export codec id and sub-id from demuxer in bitmapinfohdr:
|
20920
|
265 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
|
|
266 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
|
|
267 struct rv_init_t init_data;
|
6345
|
268
|
20920
|
269 if(extrahdr_size < 8) {
|
|
270 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
|
|
271 return 0;
|
|
272 }
|
|
273 init_data = (struct rv_init_t){11, sh->disp_w, sh->disp_h, 0, 0, be2me_32(((unsigned int*)extrahdr)[0]), 1, be2me_32(((unsigned int*)extrahdr)[1])}; // rv30
|
|
274
|
|
275 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo codec id: 0x%08X sub-id: 0x%08X\n",be2me_32(((unsigned int*)extrahdr)[1]),be2me_32(((unsigned int*)extrahdr)[0]));
|
6341
|
276
|
10102
|
277 path = malloc(strlen(REALCODEC_PATH)+strlen(sh->codec->dll)+2);
|
|
278 if (!path) return 0;
|
6404
|
279 sprintf(path, REALCODEC_PATH "/%s", sh->codec->dll);
|
6543
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
280
|
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
281 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
|
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
282 then try to load the windows ones */
|
10093
|
283 #ifdef HAVE_LIBDL
|
|
284 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
|
|
285 #endif
|
6544
222f6da66fa3
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
286 #ifdef USE_WIN32DLL
|
10444
|
287 if (!load_syms_windows(sh->codec->dll))
|
6543
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
288 #endif
|
70a9c9f695a2
first try to load linux dlls, if failed and we're supporting win32 dlls, then try to load the windows ones
alex
diff
changeset
|
289 {
|
6341
|
290 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingDLLcodec,sh->codec->dll);
|
9386
|
291 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
|
10102
|
292 free(path);
|
6341
|
293 return 0;
|
|
294 }
|
10102
|
295 free(path);
|
6341
|
296 // only I420 supported
|
9552
|
297 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
|
6341
|
298 // init codec:
|
|
299 sh->context=NULL;
|
8525
|
300 #ifdef USE_WIN32DLL
|
|
301 if (dll_type == 1)
|
|
302 result=(*wrvyuv_init)(&init_data, &sh->context);
|
|
303 else
|
|
304 #endif
|
6341
|
305 result=(*rvyuv_init)(&init_data, &sh->context);
|
|
306 if (result){
|
|
307 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
|
|
308 return 0;
|
|
309 }
|
6345
|
310 // setup rv30 codec (codec sub-type and image dimensions):
|
20920
|
311 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
|
|
312 int i, cmsg_cnt;
|
|
313 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
|
|
314 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
|
|
315
|
|
316 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
|
|
317 cmsg_cnt = (extrahdr[1]&7)*2;
|
|
318 if (extrahdr_size-8 < cmsg_cnt) {
|
|
319 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
|
|
320 cmsg_cnt = extrahdr_size-8;
|
|
321 }
|
|
322 for (i = 0; i < cmsg_cnt; i++)
|
|
323 cmsg24[2+i] = extrahdr[8+i]*4;
|
|
324 if (extrahdr_size-8 > cmsg_cnt)
|
|
325 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
|
8525
|
326
|
|
327 #ifdef USE_WIN32DLL
|
|
328 if (dll_type == 1)
|
|
329 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
|
|
330 else
|
|
331 #endif
|
8522
|
332 (*rvyuv_custom_message)(&cmsg_data,sh->context);
|
6345
|
333 }
|
6341
|
334 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
|
|
335 return 1;
|
|
336 }
|
|
337
|
|
338 // uninit driver
|
|
339 static void uninit(sh_video_t *sh){
|
8525
|
340 #ifdef USE_WIN32DLL
|
|
341 if (dll_type == 1)
|
|
342 {
|
|
343 if (wrvyuv_free) wrvyuv_free(sh->context);
|
|
344 } else
|
|
345 #endif
|
7728
|
346 if(rvyuv_free) rvyuv_free(sh->context);
|
8525
|
347
|
|
348 #ifdef USE_WIN32DLL
|
|
349 if (dll_type == 1)
|
|
350 {
|
|
351 if (rv_handle) FreeLibrary(rv_handle);
|
|
352 } else
|
|
353 #endif
|
10093
|
354 #ifdef HAVE_LIBDL
|
6345
|
355 if(rv_handle) dlclose(rv_handle);
|
10093
|
356 #endif
|
6345
|
357 rv_handle=NULL;
|
25962
|
358 initialized = 0;
|
21559
|
359 if (buffer)
|
|
360 free(buffer);
|
|
361 buffer = NULL;
|
21563
|
362 bufsz = 0;
|
6341
|
363 }
|
|
364
|
7082
|
365 // copypaste from demux_real.c - it should match to get it working!
|
|
366 typedef struct dp_hdr_s {
|
|
367 uint32_t chunks; // number of chunks
|
|
368 uint32_t timestamp; // timestamp from packet header
|
|
369 uint32_t len; // length of actual data
|
|
370 uint32_t chunktab; // offset to chunk offset array
|
|
371 } dp_hdr_t;
|
|
372
|
6341
|
373 // decode a frame
|
|
374 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
|
|
375 mp_image_t* mpi;
|
6745
|
376 unsigned long result;
|
7082
|
377 dp_hdr_t* dp_hdr=(dp_hdr_t*)data;
|
|
378 unsigned char* dp_data=((unsigned char*)data)+sizeof(dp_hdr_t);
|
|
379 uint32_t* extra=(uint32_t*)(((char*)data)+dp_hdr->chunktab);
|
|
380
|
8522
|
381 unsigned int transform_out[5];
|
|
382 transform_in_t transform_in={
|
7082
|
383 dp_hdr->len, // length of the packet (sub-packets appended)
|
6341
|
384 0, // unknown, seems to be unused
|
7082
|
385 dp_hdr->chunks, // number of sub-packets - 1
|
8522
|
386 extra, // table of sub-packet offsets
|
6341
|
387 0, // unknown, seems to be unused
|
25598
|
388 0, // timestamp (should be unneded)
|
6341
|
389 };
|
|
390
|
|
391 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
|
|
392
|
21563
|
393 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
|
21559
|
394 if (buffer) free(buffer);
|
21563
|
395 bufsz = sh->disp_w*sh->disp_h*3/2;
|
|
396 buffer=malloc(bufsz);
|
9552
|
397 if (!buffer) return 0;
|
|
398 }
|
6341
|
399
|
8525
|
400 #ifdef USE_WIN32DLL
|
|
401 if (dll_type == 1)
|
9552
|
402 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
|
8525
|
403 transform_out, sh->context);
|
|
404 else
|
|
405 #endif
|
9552
|
406 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
|
6341
|
407 transform_out, sh->context);
|
|
408
|
25962
|
409 if(!initialized){ // rv30 width/height now known
|
9552
|
410 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
|
|
411 sh->disp_w=transform_out[3];
|
|
412 sh->disp_h=transform_out[4];
|
|
413 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
|
25962
|
414 initialized=1;
|
21559
|
415 }
|
|
416 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
|
9552
|
417 sh->disp_w, sh->disp_h);
|
|
418 if(!mpi) return NULL;
|
21559
|
419 mpi->planes[0] = buffer;
|
|
420 mpi->stride[0] = sh->disp_w;
|
|
421 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
|
|
422 mpi->stride[1] = sh->disp_w / 2;
|
|
423 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
|
|
424 mpi->stride[2] = sh->disp_w / 2;
|
9552
|
425
|
21559
|
426 if(transform_out[0] &&
|
|
427 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
|
25962
|
428 initialized = 0;
|
21559
|
429
|
26754
|
430 return result ? NULL : mpi;
|
6341
|
431 }
|