vo_gl2: Use identical code to -vo gl for -wid mode
[mplayer/glamo.git] / libmpcodecs / vd_realvid.c
blob04ac9efba730e395850b8bd474d5f25ace1a9345
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 "ffmpeg_files/intreadwrite.h"
30 #include "path.h"
32 #include "vd_internal.h"
33 #include "loader/wine/windef.h"
35 static const vd_info_t info = {
36 "RealVideo decoder",
37 "realvid",
38 "Alex Beregszaszi",
39 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
40 "binary real video codecs"
43 LIBVD_EXTERN(realvid)
47 * Structures for data packets. These used to be tables of unsigned ints, but
48 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
49 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
50 * So we have to use structures so the compiler will assign the proper space
51 * for the pointer.
53 typedef struct cmsg_data_s {
54 uint32_t data1;
55 uint32_t data2;
56 uint32_t* dimensions;
57 } cmsg_data_t;
59 typedef struct transform_in_s {
60 uint32_t len;
61 uint32_t unknown1;
62 uint32_t chunks;
63 uint32_t* extra;
64 uint32_t unknown2;
65 uint32_t timestamp;
66 } transform_in_t;
68 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
69 static unsigned long (*rvyuv_free)(void*);
70 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
71 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
72 #ifdef CONFIG_WIN32DLL
73 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
74 static unsigned long WINAPI (*wrvyuv_free)(void*);
75 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
76 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
77 #endif
79 static void *rv_handle=NULL;
80 static int initialized=0;
81 static uint8_t *buffer = NULL;
82 static int bufsz = 0;
83 #ifdef CONFIG_WIN32DLL
84 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
85 #endif
87 void *__builtin_vec_new(unsigned long size) {
88 return malloc(size);
91 void __builtin_vec_delete(void *mem) {
92 free(mem);
95 void __pure_virtual(void) {
96 printf("FATAL: __pure_virtual() called!\n");
97 // exit(1);
100 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
101 void ___brk_addr(void) {exit(0);}
102 char **__environ={NULL};
103 #undef stderr
104 FILE *stderr=NULL;
105 #endif
107 // to set/get/query special features/parameters
108 static int control(sh_video_t *sh,int cmd,void* arg,...){
109 // switch(cmd){
110 // case VDCTRL_QUERY_MAX_PP_LEVEL:
111 // return 9;
112 // case VDCTRL_SET_PP_LEVEL:
113 // vfw_set_postproc(sh,10*(*((int*)arg)));
114 // return CONTROL_OK;
115 // }
116 return CONTROL_UNKNOWN;
119 /* exits program when failure */
120 #ifdef HAVE_LIBDL
121 static int load_syms_linux(char *path) {
122 void *handle;
124 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
125 handle = dlopen (path, RTLD_LAZY);
126 if (!handle) {
127 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
128 return 0;
131 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
132 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
133 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
134 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
136 if(rvyuv_custom_message &&
137 rvyuv_free &&
138 rvyuv_init &&
139 rvyuv_transform)
141 rv_handle = handle;
142 return 1;
145 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
146 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
147 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
148 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
150 if(rvyuv_custom_message &&
151 rvyuv_free &&
152 rvyuv_init &&
153 rvyuv_transform)
155 rv_handle = handle;
156 return 1;
159 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
160 dlclose(handle);
161 return 0;
163 #endif
165 #ifdef CONFIG_WIN32DLL
167 #ifdef WIN32_LOADER
168 #include "loader/ldt_keeper.h"
169 #endif
170 void* WINAPI LoadLibraryA(char* name);
171 void* WINAPI GetProcAddress(void* handle,char* func);
172 int WINAPI FreeLibrary(void *handle);
174 #ifndef WIN32_LOADER
175 void * WINAPI GetModuleHandleA(char *);
176 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
177 const uint8_t *newcode, int codesize) {
178 void *handle = GetModuleHandleA("kernel32");
179 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
180 int res = 0;
181 int prot, tmp;
182 VirtProt = GetProcAddress(handle, "VirtualProtect");
183 // change permissions to PAGE_WRITECOPY
184 if (!VirtProt ||
185 !VirtProt(patchpos, codesize, 0x08, &prot)) {
186 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
187 return 0;
189 if (memcmp(patchpos, oldcode, codesize) == 0) {
190 memcpy(patchpos, newcode, codesize);
191 res = 1;
193 VirtProt(patchpos, codesize, prot, &tmp);
194 return res;
196 #endif
198 static int load_syms_windows(char *path) {
199 void *handle;
201 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
202 #ifdef WIN32_LOADER
203 Setup_LDT_Keeper();
204 #endif
205 handle = LoadLibraryA(path);
206 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
207 if (!handle) {
208 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
209 return 0;
212 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
213 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
214 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
215 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
217 if(wrvyuv_custom_message &&
218 wrvyuv_free &&
219 wrvyuv_init &&
220 wrvyuv_transform)
222 dll_type = 1;
223 rv_handle = handle;
224 #ifndef WIN32_LOADER
226 if (strstr(path, "drv43260.dll")) {
227 int patched;
228 // patch away multithreaded decoding, it causes crashes
229 static const uint8_t oldcode[13] = {
230 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
231 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
232 static const uint8_t newcode[13] = {
233 0x31, 0xc0,
234 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
235 0xe9, 0xd0, 0x00, 0x00, 0x00 };
236 patched = patch_dll(
237 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
238 oldcode, newcode, sizeof(oldcode));
239 if (!patched)
240 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
243 #endif
244 return 1;
247 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
248 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
249 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
250 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
251 if(wrvyuv_custom_message &&
252 wrvyuv_free &&
253 wrvyuv_init &&
254 wrvyuv_transform) {
255 dll_type = 1;
256 rv_handle = handle;
257 return 1;
260 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
261 FreeLibrary(handle);
262 return 0; // error
264 #endif
266 /* we need exact positions */
267 struct rv_init_t {
268 short unk1;
269 short w;
270 short h;
271 short unk3;
272 int unk2;
273 int subformat;
274 int unk5;
275 int format;
276 } rv_init_t;
278 // init driver
279 static int init(sh_video_t *sh){
280 //unsigned int out_fmt;
281 char *path;
282 int result;
283 // we export codec id and sub-id from demuxer in bitmapinfohdr:
284 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
285 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
286 struct rv_init_t init_data;
288 if(extrahdr_size < 8) {
289 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
290 return 0;
292 init_data = (struct rv_init_t){11, sh->disp_w, sh->disp_h, 0, 0, AV_RB32(extrahdr), 1, AV_RB32(extrahdr + 4)}; // rv30
294 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo codec id: 0x%08X sub-id: 0x%08X\n",init_data.format,init_data.subformat);
296 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
297 if (!path) return 0;
298 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
300 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
301 then try to load the windows ones */
302 #ifdef HAVE_LIBDL
303 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
304 #endif
305 #ifdef CONFIG_WIN32DLL
306 if (!load_syms_windows(sh->codec->dll))
307 #endif
309 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"ERROR: Could not open required DirectShow codec %s.\n",sh->codec->dll);
310 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
311 free(path);
312 return 0;
314 free(path);
315 // only I420 supported
316 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
317 // init codec:
318 sh->context=NULL;
319 #ifdef CONFIG_WIN32DLL
320 if (dll_type == 1)
321 result=(*wrvyuv_init)(&init_data, &sh->context);
322 else
323 #endif
324 result=(*rvyuv_init)(&init_data, &sh->context);
325 if (result){
326 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
327 return 0;
329 // setup rv30 codec (codec sub-type and image dimensions):
330 if((sh->format<=0x30335652) && AV_RB32(extrahdr + 4)>=0x20200002){
331 int i, cmsg_cnt;
332 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
333 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
335 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
336 cmsg_cnt = (extrahdr[1]&7)*2;
337 if (extrahdr_size-8 < cmsg_cnt) {
338 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
339 cmsg_cnt = extrahdr_size-8;
341 for (i = 0; i < cmsg_cnt; i++)
342 cmsg24[2+i] = extrahdr[8+i]*4;
343 if (extrahdr_size-8 > cmsg_cnt)
344 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
346 #ifdef CONFIG_WIN32DLL
347 if (dll_type == 1)
348 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
349 else
350 #endif
351 (*rvyuv_custom_message)(&cmsg_data,sh->context);
353 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
354 return 1;
357 // uninit driver
358 static void uninit(sh_video_t *sh){
359 #ifdef CONFIG_WIN32DLL
360 if (dll_type == 1)
362 if (wrvyuv_free) wrvyuv_free(sh->context);
363 } else
364 #endif
365 if(rvyuv_free) rvyuv_free(sh->context);
367 #ifdef CONFIG_WIN32DLL
368 if (dll_type == 1)
370 if (rv_handle) FreeLibrary(rv_handle);
371 } else
372 #endif
373 #ifdef HAVE_LIBDL
374 if(rv_handle) dlclose(rv_handle);
375 #endif
376 rv_handle=NULL;
377 initialized = 0;
378 if (buffer)
379 free(buffer);
380 buffer = NULL;
381 bufsz = 0;
384 // decode a frame
385 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
386 mp_image_t* mpi;
387 unsigned long result;
388 uint8_t *buf = data;
389 int chunks = *buf++;
390 int extra_size = 8*(chunks+1);
391 uint32_t data_size = len-1-extra_size;
392 unsigned char* dp_data=buf+extra_size;
393 uint32_t* extra=(uint32_t*)buf;
394 int i;
396 unsigned int transform_out[5];
397 transform_in_t transform_in={
398 data_size, // length of the packet (sub-packets appended)
399 0, // unknown, seems to be unused
400 chunks, // number of sub-packets - 1
401 extra, // table of sub-packet offsets
402 0, // unknown, seems to be unused
403 0, // timestamp (should be unneded)
406 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
408 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
409 if (buffer) free(buffer);
410 bufsz = sh->disp_w*sh->disp_h*3/2;
411 buffer=malloc(bufsz);
412 if (!buffer) return 0;
415 for (i=0; i<2*(chunks+1); i++)
416 extra[i] = le2me_32(extra[i]);
418 #ifdef CONFIG_WIN32DLL
419 if (dll_type == 1)
420 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
421 transform_out, sh->context);
422 else
423 #endif
424 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
425 transform_out, sh->context);
427 if(!initialized){ // rv30 width/height now known
428 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
429 sh->disp_w=transform_out[3];
430 sh->disp_h=transform_out[4];
431 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
432 initialized=1;
434 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
435 sh->disp_w, sh->disp_h);
436 if(!mpi) return NULL;
437 mpi->planes[0] = buffer;
438 mpi->stride[0] = sh->disp_w;
439 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
440 mpi->stride[1] = sh->disp_w / 2;
441 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
442 mpi->stride[2] = sh->disp_w / 2;
444 if(transform_out[0] &&
445 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
446 initialized = 0;
448 return result ? NULL : mpi;