14 #include "vd_internal.h"
15 #include "loader/wine/windef.h"
17 static vd_info_t info
= {
21 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
22 "binary real video codecs"
29 * Structures for data packets. These used to be tables of unsigned ints, but
30 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
31 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
32 * So we have to use structures so the compiler will assign the proper space
35 typedef struct cmsg_data_s
{
41 typedef struct transform_in_s
{
50 static unsigned long (*rvyuv_custom_message
)(cmsg_data_t
* ,void*);
51 static unsigned long (*rvyuv_free
)(void*);
52 static unsigned long (*rvyuv_init
)(void*, void*); // initdata,context
53 static unsigned long (*rvyuv_transform
)(char*, char*,transform_in_t
*,unsigned int*,void*);
54 #ifdef CONFIG_WIN32DLL
55 static unsigned long WINAPI (*wrvyuv_custom_message
)(cmsg_data_t
* ,void*);
56 static unsigned long WINAPI (*wrvyuv_free
)(void*);
57 static unsigned long WINAPI (*wrvyuv_init
)(void*, void*); // initdata,context
58 static unsigned long WINAPI (*wrvyuv_transform
)(char*, char*,transform_in_t
*,unsigned int*,void*);
61 static void *rv_handle
=NULL
;
62 static int initialized
=0;
63 static uint8_t *buffer
= NULL
;
65 #ifdef CONFIG_WIN32DLL
66 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
69 void *__builtin_vec_new(unsigned long size
) {
73 void __builtin_vec_delete(void *mem
) {
77 void __pure_virtual(void) {
78 printf("FATAL: __pure_virtual() called!\n");
82 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
83 void ___brk_addr(void) {exit(0);}
84 char **__environ
={NULL
};
89 // to set/get/query special features/parameters
90 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...){
92 // case VDCTRL_QUERY_MAX_PP_LEVEL:
94 // case VDCTRL_SET_PP_LEVEL:
95 // vfw_set_postproc(sh,10*(*((int*)arg)));
98 return CONTROL_UNKNOWN
;
101 /* exits program when failure */
103 static int load_syms_linux(char *path
) {
106 mp_msg(MSGT_DECVIDEO
,MSGL_V
, "opening shared obj '%s'\n", path
);
107 handle
= dlopen (path
, RTLD_LAZY
);
109 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"Error: %s\n",dlerror());
113 rvyuv_custom_message
= dlsym(handle
, "RV20toYUV420CustomMessage");
114 rvyuv_free
= dlsym(handle
, "RV20toYUV420Free");
115 rvyuv_init
= dlsym(handle
, "RV20toYUV420Init");
116 rvyuv_transform
= dlsym(handle
, "RV20toYUV420Transform");
118 if(rvyuv_custom_message
&&
127 rvyuv_custom_message
= dlsym(handle
, "RV40toYUV420CustomMessage");
128 rvyuv_free
= dlsym(handle
, "RV40toYUV420Free");
129 rvyuv_init
= dlsym(handle
, "RV40toYUV420Init");
130 rvyuv_transform
= dlsym(handle
, "RV40toYUV420Transform");
132 if(rvyuv_custom_message
&&
141 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"Error resolving symbols! (version incompatibility?)\n");
147 #ifdef CONFIG_WIN32DLL
150 #include "loader/ldt_keeper.h"
152 void* WINAPI
LoadLibraryA(char* name
);
153 void* WINAPI
GetProcAddress(void* handle
,char* func
);
154 int WINAPI
FreeLibrary(void *handle
);
157 void * WINAPI
GetModuleHandleA(char *);
158 static int patch_dll(uint8_t *patchpos
, const uint8_t *oldcode
,
159 const uint8_t *newcode
, int codesize
) {
160 void *handle
= GetModuleHandleA("kernel32");
161 int WINAPI (*VirtProt
)(void *, unsigned, int, int *);
164 VirtProt
= GetProcAddress(handle
, "VirtualProtect");
165 // change permissions to PAGE_WRITECOPY
167 !VirtProt(patchpos
, codesize
, 0x08, &prot
)) {
168 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "VirtualProtect failed at %p\n", patchpos
);
171 if (memcmp(patchpos
, oldcode
, codesize
) == 0) {
172 memcpy(patchpos
, newcode
, codesize
);
175 VirtProt(patchpos
, codesize
, prot
, &tmp
);
180 static int load_syms_windows(char *path
) {
183 mp_msg(MSGT_DECVIDEO
,MSGL_V
, "opening win32 dll '%s'\n", path
);
187 handle
= LoadLibraryA(path
);
188 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"win32 real codec handle=%p \n",handle
);
190 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"Error loading dll\n");
194 wrvyuv_custom_message
= GetProcAddress(handle
, "RV20toYUV420CustomMessage");
195 wrvyuv_free
= GetProcAddress(handle
, "RV20toYUV420Free");
196 wrvyuv_init
= GetProcAddress(handle
, "RV20toYUV420Init");
197 wrvyuv_transform
= GetProcAddress(handle
, "RV20toYUV420Transform");
199 if(wrvyuv_custom_message
&&
208 if (strstr(path
, "drv43260.dll")) {
210 // patch away multithreaded decoding, it causes crashes
211 static const uint8_t oldcode
[13] = {
212 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
213 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
214 static const uint8_t newcode
[13] = {
216 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
217 0xe9, 0xd0, 0x00, 0x00, 0x00 };
219 (char*)wrvyuv_transform
+ 0x634132fa - 0x634114d0,
220 oldcode
, newcode
, sizeof(oldcode
));
222 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Could not patch Real codec, this might crash on multi-CPU systems\n");
229 wrvyuv_custom_message
= GetProcAddress(handle
, "RV40toYUV420CustomMessage");
230 wrvyuv_free
= GetProcAddress(handle
, "RV40toYUV420Free");
231 wrvyuv_init
= GetProcAddress(handle
, "RV40toYUV420Init");
232 wrvyuv_transform
= GetProcAddress(handle
, "RV40toYUV420Transform");
233 if(wrvyuv_custom_message
&&
242 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"Error resolving symbols! (version incompatibility?)\n");
248 /* we need exact positions */
261 static int init(sh_video_t
*sh
){
262 //unsigned int out_fmt;
265 // we export codec id and sub-id from demuxer in bitmapinfohdr:
266 unsigned char* extrahdr
=(unsigned char*)(sh
->bih
+1);
267 unsigned int extrahdr_size
= sh
->bih
->biSize
- sizeof(BITMAPINFOHEADER
);
268 struct rv_init_t init_data
;
270 if(extrahdr_size
< 8) {
271 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"realvideo: extradata too small (%u)\n", sh
->bih
->biSize
- sizeof(BITMAPINFOHEADER
));
274 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
276 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]));
278 path
= malloc(strlen(REALCODEC_PATH
)+strlen(sh
->codec
->dll
)+2);
280 sprintf(path
, REALCODEC_PATH
"/%s", sh
->codec
->dll
);
282 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
283 then try to load the windows ones */
285 if(strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
287 #ifdef CONFIG_WIN32DLL
288 if (!load_syms_windows(sh
->codec
->dll
))
291 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,MSGTR_MissingDLLcodec
,sh
->codec
->dll
);
292 mp_msg(MSGT_DECVIDEO
,MSGL_HINT
,"Read the RealVideo section of the DOCS!\n");
297 // only I420 supported
298 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
301 #ifdef CONFIG_WIN32DLL
303 result
=(*wrvyuv_init
)(&init_data
, &sh
->context
);
306 result
=(*rvyuv_init
)(&init_data
, &sh
->context
);
308 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"Couldn't open RealVideo codec, error code: 0x%X \n",result
);
311 // setup rv30 codec (codec sub-type and image dimensions):
312 if((sh
->format
<=0x30335652) && (be2me_32(((unsigned int*)extrahdr
)[1])>=0x20200002)){
314 uint32_t cmsg24
[16]={sh
->disp_w
,sh
->disp_h
};
315 cmsg_data_t cmsg_data
={0x24,1+(extrahdr
[1]&7), &cmsg24
[0]};
317 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"realvideo: using cmsg24 with %u elements.\n",extrahdr
[1]&7);
318 cmsg_cnt
= (extrahdr
[1]&7)*2;
319 if (extrahdr_size
-8 < cmsg_cnt
) {
320 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size
-8,extrahdr
[1]&7);
321 cmsg_cnt
= extrahdr_size
-8;
323 for (i
= 0; i
< cmsg_cnt
; i
++)
324 cmsg24
[2+i
] = extrahdr
[8+i
]*4;
325 if (extrahdr_size
-8 > cmsg_cnt
)
326 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size
-8-cmsg_cnt
);
328 #ifdef CONFIG_WIN32DLL
330 (*wrvyuv_custom_message
)(&cmsg_data
,sh
->context
);
333 (*rvyuv_custom_message
)(&cmsg_data
,sh
->context
);
335 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"INFO: RealVideo codec init OK!\n");
340 static void uninit(sh_video_t
*sh
){
341 #ifdef CONFIG_WIN32DLL
344 if (wrvyuv_free
) wrvyuv_free(sh
->context
);
347 if(rvyuv_free
) rvyuv_free(sh
->context
);
349 #ifdef CONFIG_WIN32DLL
352 if (rv_handle
) FreeLibrary(rv_handle
);
356 if(rv_handle
) dlclose(rv_handle
);
367 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
369 unsigned long result
;
372 int extra_size
= 8*(chunks
+1);
373 uint32_t data_size
= len
-1-extra_size
;
374 unsigned char* dp_data
=buf
+extra_size
;
375 uint32_t* extra
=(uint32_t*)buf
;
378 unsigned int transform_out
[5];
379 transform_in_t transform_in
={
380 data_size
, // length of the packet (sub-packets appended)
381 0, // unknown, seems to be unused
382 chunks
, // number of sub-packets - 1
383 extra
, // table of sub-packet offsets
384 0, // unknown, seems to be unused
385 0, // timestamp (should be unneded)
388 if(len
<=0 || flags
&2) return NULL
; // skipped frame || hardframedrop
390 if (bufsz
< sh
->disp_w
*sh
->disp_h
*3/2) {
391 if (buffer
) free(buffer
);
392 bufsz
= sh
->disp_w
*sh
->disp_h
*3/2;
393 buffer
=malloc(bufsz
);
394 if (!buffer
) return 0;
397 for (i
=0; i
<2*(chunks
+1); i
++)
398 extra
[i
] = le2me_32(extra
[i
]);
400 #ifdef CONFIG_WIN32DLL
402 result
=(*wrvyuv_transform
)(dp_data
, buffer
, &transform_in
,
403 transform_out
, sh
->context
);
406 result
=(*rvyuv_transform
)(dp_data
, buffer
, &transform_in
,
407 transform_out
, sh
->context
);
409 if(!initialized
){ // rv30 width/height now known
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;
416 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
417 sh
->disp_w
, sh
->disp_h
);
418 if(!mpi
) return NULL
;
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;
426 if(transform_out
[0] &&
427 (sh
->disp_w
!= transform_out
[3] || sh
->disp_h
!= transform_out
[4]))
430 return result
? NULL
: mpi
;