Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libmpcodecs / vd_realvid.c
blob93b303fe8fe07df90fa5a28b218165661ece1350
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
6 #ifdef HAVE_LIBDL
7 #include <dlfcn.h>
8 #endif
10 #include "mp_msg.h"
11 #include "help_mp.h"
12 #include "mpbswap.h"
14 #include "vd_internal.h"
15 #include "loader/wine/windef.h"
17 static vd_info_t info = {
18 "RealVideo decoder",
19 "realvid",
20 "Alex Beregszaszi",
21 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
22 "binary real video codecs"
25 LIBVD_EXTERN(realvid)
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
33 * for the pointer.
35 typedef struct cmsg_data_s {
36 uint32_t data1;
37 uint32_t data2;
38 uint32_t* dimensions;
39 } cmsg_data_t;
41 typedef struct transform_in_s {
42 uint32_t len;
43 uint32_t unknown1;
44 uint32_t chunks;
45 uint32_t* extra;
46 uint32_t unknown2;
47 uint32_t timestamp;
48 } transform_in_t;
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*);
59 #endif
61 static void *rv_handle=NULL;
62 static int initialized=0;
63 static uint8_t *buffer = NULL;
64 static int bufsz = 0;
65 #ifdef CONFIG_WIN32DLL
66 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
67 #endif
69 void *__builtin_vec_new(unsigned long size) {
70 return malloc(size);
73 void __builtin_vec_delete(void *mem) {
74 free(mem);
77 void __pure_virtual(void) {
78 printf("FATAL: __pure_virtual() called!\n");
79 // exit(1);
82 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
83 void ___brk_addr(void) {exit(0);}
84 char **__environ={NULL};
85 #undef stderr
86 FILE *stderr=NULL;
87 #endif
89 // to set/get/query special features/parameters
90 static int control(sh_video_t *sh,int cmd,void* arg,...){
91 // switch(cmd){
92 // case VDCTRL_QUERY_MAX_PP_LEVEL:
93 // return 9;
94 // case VDCTRL_SET_PP_LEVEL:
95 // vfw_set_postproc(sh,10*(*((int*)arg)));
96 // return CONTROL_OK;
97 // }
98 return CONTROL_UNKNOWN;
101 /* exits program when failure */
102 #ifdef HAVE_LIBDL
103 static int load_syms_linux(char *path) {
104 void *handle;
106 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
107 handle = dlopen (path, RTLD_LAZY);
108 if (!handle) {
109 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
110 return 0;
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 &&
119 rvyuv_free &&
120 rvyuv_init &&
121 rvyuv_transform)
123 rv_handle = handle;
124 return 1;
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 &&
133 rvyuv_free &&
134 rvyuv_init &&
135 rvyuv_transform)
137 rv_handle = handle;
138 return 1;
141 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
142 dlclose(handle);
143 return 0;
145 #endif
147 #ifdef CONFIG_WIN32DLL
149 #ifdef WIN32_LOADER
150 #include "loader/ldt_keeper.h"
151 #endif
152 void* WINAPI LoadLibraryA(char* name);
153 void* WINAPI GetProcAddress(void* handle,char* func);
154 int WINAPI FreeLibrary(void *handle);
156 #ifndef WIN32_LOADER
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 *);
162 int res = 0;
163 int prot, tmp;
164 VirtProt = GetProcAddress(handle, "VirtualProtect");
165 // change permissions to PAGE_WRITECOPY
166 if (!VirtProt ||
167 !VirtProt(patchpos, codesize, 0x08, &prot)) {
168 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
169 return 0;
171 if (memcmp(patchpos, oldcode, codesize) == 0) {
172 memcpy(patchpos, newcode, codesize);
173 res = 1;
175 VirtProt(patchpos, codesize, prot, &tmp);
176 return res;
178 #endif
180 static int load_syms_windows(char *path) {
181 void *handle;
183 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
184 #ifdef WIN32_LOADER
185 Setup_LDT_Keeper();
186 #endif
187 handle = LoadLibraryA(path);
188 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
189 if (!handle) {
190 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
191 return 0;
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 &&
200 wrvyuv_free &&
201 wrvyuv_init &&
202 wrvyuv_transform)
204 dll_type = 1;
205 rv_handle = handle;
206 #ifndef WIN32_LOADER
208 if (strstr(path, "drv43260.dll")) {
209 int patched;
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] = {
215 0x31, 0xc0,
216 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
217 0xe9, 0xd0, 0x00, 0x00, 0x00 };
218 patched = patch_dll(
219 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
220 oldcode, newcode, sizeof(oldcode));
221 if (!patched)
222 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
225 #endif
226 return 1;
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 &&
234 wrvyuv_free &&
235 wrvyuv_init &&
236 wrvyuv_transform) {
237 dll_type = 1;
238 rv_handle = handle;
239 return 1;
242 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
243 FreeLibrary(handle);
244 return 0; // error
246 #endif
248 /* we need exact positions */
249 struct rv_init_t {
250 short unk1;
251 short w;
252 short h;
253 short unk3;
254 int unk2;
255 int subformat;
256 int unk5;
257 int format;
258 } rv_init_t;
260 // init driver
261 static int init(sh_video_t *sh){
262 //unsigned int out_fmt;
263 char *path;
264 int result;
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));
272 return 0;
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);
279 if (!path) return 0;
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 */
284 #ifdef HAVE_LIBDL
285 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
286 #endif
287 #ifdef CONFIG_WIN32DLL
288 if (!load_syms_windows(sh->codec->dll))
289 #endif
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");
293 free(path);
294 return 0;
296 free(path);
297 // only I420 supported
298 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
299 // init codec:
300 sh->context=NULL;
301 #ifdef CONFIG_WIN32DLL
302 if (dll_type == 1)
303 result=(*wrvyuv_init)(&init_data, &sh->context);
304 else
305 #endif
306 result=(*rvyuv_init)(&init_data, &sh->context);
307 if (result){
308 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
309 return 0;
311 // setup rv30 codec (codec sub-type and image dimensions):
312 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
313 int i, cmsg_cnt;
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
329 if (dll_type == 1)
330 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
331 else
332 #endif
333 (*rvyuv_custom_message)(&cmsg_data,sh->context);
335 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
336 return 1;
339 // uninit driver
340 static void uninit(sh_video_t *sh){
341 #ifdef CONFIG_WIN32DLL
342 if (dll_type == 1)
344 if (wrvyuv_free) wrvyuv_free(sh->context);
345 } else
346 #endif
347 if(rvyuv_free) rvyuv_free(sh->context);
349 #ifdef CONFIG_WIN32DLL
350 if (dll_type == 1)
352 if (rv_handle) FreeLibrary(rv_handle);
353 } else
354 #endif
355 #ifdef HAVE_LIBDL
356 if(rv_handle) dlclose(rv_handle);
357 #endif
358 rv_handle=NULL;
359 initialized = 0;
360 if (buffer)
361 free(buffer);
362 buffer = NULL;
363 bufsz = 0;
366 // decode a frame
367 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
368 mp_image_t* mpi;
369 unsigned long result;
370 uint8_t *buf = data;
371 int chunks = *buf++;
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;
376 int i;
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
401 if (dll_type == 1)
402 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
403 transform_out, sh->context);
404 else
405 #endif
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;
414 initialized=1;
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]))
428 initialized = 0;
430 return result ? NULL : mpi;