Merge svn changes up to r30907
[mplayer/glamo.git] / libmpcodecs / vd_realvid.c
blobbe67210f88f0534f5a53d00dad38b15a2785cc2d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
22 #include "config.h"
24 #ifdef HAVE_LIBDL
25 #include <dlfcn.h>
26 #endif
28 #include "mp_msg.h"
29 #include "mpbswap.h"
31 #include "vd_internal.h"
32 #include "loader/wine/windef.h"
34 static const vd_info_t info = {
35 "RealVideo decoder",
36 "realvid",
37 "Alex Beregszaszi",
38 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
39 "binary real video codecs"
42 LIBVD_EXTERN(realvid)
46 * Structures for data packets. These used to be tables of unsigned ints, but
47 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
48 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
49 * So we have to use structures so the compiler will assign the proper space
50 * for the pointer.
52 typedef struct cmsg_data_s {
53 uint32_t data1;
54 uint32_t data2;
55 uint32_t* dimensions;
56 } cmsg_data_t;
58 typedef struct transform_in_s {
59 uint32_t len;
60 uint32_t unknown1;
61 uint32_t chunks;
62 uint32_t* extra;
63 uint32_t unknown2;
64 uint32_t timestamp;
65 } transform_in_t;
67 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
68 static unsigned long (*rvyuv_free)(void*);
69 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
70 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
71 #ifdef CONFIG_WIN32DLL
72 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
73 static unsigned long WINAPI (*wrvyuv_free)(void*);
74 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
75 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
76 #endif
78 static void *rv_handle=NULL;
79 static int initialized=0;
80 static uint8_t *buffer = NULL;
81 static int bufsz = 0;
82 #ifdef CONFIG_WIN32DLL
83 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
84 #endif
86 void *__builtin_vec_new(unsigned long size) {
87 return malloc(size);
90 void __builtin_vec_delete(void *mem) {
91 free(mem);
94 void __pure_virtual(void) {
95 printf("FATAL: __pure_virtual() called!\n");
96 // exit(1);
99 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
100 void ___brk_addr(void) {exit(0);}
101 char **__environ={NULL};
102 #undef stderr
103 FILE *stderr=NULL;
104 #endif
106 // to set/get/query special features/parameters
107 static int control(sh_video_t *sh,int cmd,void* arg,...){
108 // switch(cmd){
109 // case VDCTRL_QUERY_MAX_PP_LEVEL:
110 // return 9;
111 // case VDCTRL_SET_PP_LEVEL:
112 // vfw_set_postproc(sh,10*(*((int*)arg)));
113 // return CONTROL_OK;
114 // }
115 return CONTROL_UNKNOWN;
118 /* exits program when failure */
119 #ifdef HAVE_LIBDL
120 static int load_syms_linux(char *path) {
121 void *handle;
123 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
124 handle = dlopen (path, RTLD_LAZY);
125 if (!handle) {
126 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
127 return 0;
130 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
131 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
132 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
133 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
135 if(rvyuv_custom_message &&
136 rvyuv_free &&
137 rvyuv_init &&
138 rvyuv_transform)
140 rv_handle = handle;
141 return 1;
144 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
145 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
146 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
147 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
149 if(rvyuv_custom_message &&
150 rvyuv_free &&
151 rvyuv_init &&
152 rvyuv_transform)
154 rv_handle = handle;
155 return 1;
158 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
159 dlclose(handle);
160 return 0;
162 #endif
164 #ifdef CONFIG_WIN32DLL
166 #ifdef WIN32_LOADER
167 #include "loader/ldt_keeper.h"
168 #endif
169 void* WINAPI LoadLibraryA(char* name);
170 void* WINAPI GetProcAddress(void* handle,char* func);
171 int WINAPI FreeLibrary(void *handle);
173 #ifndef WIN32_LOADER
174 void * WINAPI GetModuleHandleA(char *);
175 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
176 const uint8_t *newcode, int codesize) {
177 void *handle = GetModuleHandleA("kernel32");
178 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
179 int res = 0;
180 int prot, tmp;
181 VirtProt = GetProcAddress(handle, "VirtualProtect");
182 // change permissions to PAGE_WRITECOPY
183 if (!VirtProt ||
184 !VirtProt(patchpos, codesize, 0x08, &prot)) {
185 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
186 return 0;
188 if (memcmp(patchpos, oldcode, codesize) == 0) {
189 memcpy(patchpos, newcode, codesize);
190 res = 1;
192 VirtProt(patchpos, codesize, prot, &tmp);
193 return res;
195 #endif
197 static int load_syms_windows(char *path) {
198 void *handle;
200 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
201 #ifdef WIN32_LOADER
202 Setup_LDT_Keeper();
203 #endif
204 handle = LoadLibraryA(path);
205 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
206 if (!handle) {
207 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
208 return 0;
211 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
212 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
213 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
214 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
216 if(wrvyuv_custom_message &&
217 wrvyuv_free &&
218 wrvyuv_init &&
219 wrvyuv_transform)
221 dll_type = 1;
222 rv_handle = handle;
223 #ifndef WIN32_LOADER
225 if (strstr(path, "drv43260.dll")) {
226 int patched;
227 // patch away multithreaded decoding, it causes crashes
228 static const uint8_t oldcode[13] = {
229 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
230 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
231 static const uint8_t newcode[13] = {
232 0x31, 0xc0,
233 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
234 0xe9, 0xd0, 0x00, 0x00, 0x00 };
235 patched = patch_dll(
236 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
237 oldcode, newcode, sizeof(oldcode));
238 if (!patched)
239 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
242 #endif
243 return 1;
246 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
247 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
248 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
249 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
250 if(wrvyuv_custom_message &&
251 wrvyuv_free &&
252 wrvyuv_init &&
253 wrvyuv_transform) {
254 dll_type = 1;
255 rv_handle = handle;
256 return 1;
259 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
260 FreeLibrary(handle);
261 return 0; // error
263 #endif
265 /* we need exact positions */
266 struct rv_init_t {
267 short unk1;
268 short w;
269 short h;
270 short unk3;
271 int unk2;
272 int subformat;
273 int unk5;
274 int format;
275 } rv_init_t;
277 // init driver
278 static int init(sh_video_t *sh){
279 //unsigned int out_fmt;
280 char *path;
281 int result;
282 // we export codec id and sub-id from demuxer in bitmapinfohdr:
283 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
284 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
285 struct rv_init_t init_data;
287 if(extrahdr_size < 8) {
288 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
289 return 0;
291 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
293 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]));
295 path = malloc(strlen(BINARY_CODECS_PATH) + strlen(sh->codec->dll) + 2);
296 if (!path) return 0;
297 sprintf(path, BINARY_CODECS_PATH "/%s", sh->codec->dll);
299 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
300 then try to load the windows ones */
301 #ifdef HAVE_LIBDL
302 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
303 #endif
304 #ifdef CONFIG_WIN32DLL
305 if (!load_syms_windows(sh->codec->dll))
306 #endif
308 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"ERROR: Could not open required DirectShow codec %s.\n",sh->codec->dll);
309 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
310 free(path);
311 return 0;
313 free(path);
314 // only I420 supported
315 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
316 // init codec:
317 sh->context=NULL;
318 #ifdef CONFIG_WIN32DLL
319 if (dll_type == 1)
320 result=(*wrvyuv_init)(&init_data, &sh->context);
321 else
322 #endif
323 result=(*rvyuv_init)(&init_data, &sh->context);
324 if (result){
325 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
326 return 0;
328 // setup rv30 codec (codec sub-type and image dimensions):
329 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
330 int i, cmsg_cnt;
331 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
332 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
334 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
335 cmsg_cnt = (extrahdr[1]&7)*2;
336 if (extrahdr_size-8 < cmsg_cnt) {
337 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
338 cmsg_cnt = extrahdr_size-8;
340 for (i = 0; i < cmsg_cnt; i++)
341 cmsg24[2+i] = extrahdr[8+i]*4;
342 if (extrahdr_size-8 > cmsg_cnt)
343 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
345 #ifdef CONFIG_WIN32DLL
346 if (dll_type == 1)
347 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
348 else
349 #endif
350 (*rvyuv_custom_message)(&cmsg_data,sh->context);
352 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
353 return 1;
356 // uninit driver
357 static void uninit(sh_video_t *sh){
358 #ifdef CONFIG_WIN32DLL
359 if (dll_type == 1)
361 if (wrvyuv_free) wrvyuv_free(sh->context);
362 } else
363 #endif
364 if(rvyuv_free) rvyuv_free(sh->context);
366 #ifdef CONFIG_WIN32DLL
367 if (dll_type == 1)
369 if (rv_handle) FreeLibrary(rv_handle);
370 } else
371 #endif
372 #ifdef HAVE_LIBDL
373 if(rv_handle) dlclose(rv_handle);
374 #endif
375 rv_handle=NULL;
376 initialized = 0;
377 if (buffer)
378 free(buffer);
379 buffer = NULL;
380 bufsz = 0;
383 // decode a frame
384 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
385 mp_image_t* mpi;
386 unsigned long result;
387 uint8_t *buf = data;
388 int chunks = *buf++;
389 int extra_size = 8*(chunks+1);
390 uint32_t data_size = len-1-extra_size;
391 unsigned char* dp_data=buf+extra_size;
392 uint32_t* extra=(uint32_t*)buf;
393 int i;
395 unsigned int transform_out[5];
396 transform_in_t transform_in={
397 data_size, // length of the packet (sub-packets appended)
398 0, // unknown, seems to be unused
399 chunks, // number of sub-packets - 1
400 extra, // table of sub-packet offsets
401 0, // unknown, seems to be unused
402 0, // timestamp (should be unneded)
405 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
407 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
408 if (buffer) free(buffer);
409 bufsz = sh->disp_w*sh->disp_h*3/2;
410 buffer=malloc(bufsz);
411 if (!buffer) return 0;
414 for (i=0; i<2*(chunks+1); i++)
415 extra[i] = le2me_32(extra[i]);
417 #ifdef CONFIG_WIN32DLL
418 if (dll_type == 1)
419 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
420 transform_out, sh->context);
421 else
422 #endif
423 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
424 transform_out, sh->context);
426 if(!initialized){ // rv30 width/height now known
427 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
428 sh->disp_w=transform_out[3];
429 sh->disp_h=transform_out[4];
430 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
431 initialized=1;
433 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
434 sh->disp_w, sh->disp_h);
435 if(!mpi) return NULL;
436 mpi->planes[0] = buffer;
437 mpi->stride[0] = sh->disp_w;
438 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
439 mpi->stride[1] = sh->disp_w / 2;
440 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
441 mpi->stride[2] = sh->disp_w / 2;
443 if(transform_out[0] &&
444 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
445 initialized = 0;
447 return result ? NULL : mpi;