Fix compilation after FFmpeg r22565.
[mplayer/glamo.git] / libmpcodecs / vd_realvid.c
blob110498ab90daa07ccccd80100fb601ede42c80aa
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 "help_mp.h"
30 #include "mpbswap.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 // to set/get/query special features/parameters
88 static int control(sh_video_t *sh,int cmd,void* arg,...){
89 // switch(cmd){
90 // case VDCTRL_QUERY_MAX_PP_LEVEL:
91 // return 9;
92 // case VDCTRL_SET_PP_LEVEL:
93 // vfw_set_postproc(sh,10*(*((int*)arg)));
94 // return CONTROL_OK;
95 // }
96 return CONTROL_UNKNOWN;
99 /* exits program when failure */
100 #ifdef HAVE_LIBDL
101 static int load_syms_linux(char *path) {
102 void *handle;
104 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
105 handle = dlopen (path, RTLD_LAZY);
106 if (!handle) {
107 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
108 return 0;
111 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
112 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
113 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
114 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
116 if(rvyuv_custom_message &&
117 rvyuv_free &&
118 rvyuv_init &&
119 rvyuv_transform)
121 rv_handle = handle;
122 return 1;
125 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
126 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
127 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
128 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
130 if(rvyuv_custom_message &&
131 rvyuv_free &&
132 rvyuv_init &&
133 rvyuv_transform)
135 rv_handle = handle;
136 return 1;
139 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
140 dlclose(handle);
141 return 0;
143 #endif
145 #ifdef CONFIG_WIN32DLL
147 #ifdef WIN32_LOADER
148 #include "loader/ldt_keeper.h"
149 #endif
150 void* WINAPI LoadLibraryA(char* name);
151 void* WINAPI GetProcAddress(void* handle,char* func);
152 int WINAPI FreeLibrary(void *handle);
154 #ifndef WIN32_LOADER
155 void * WINAPI GetModuleHandleA(char *);
156 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
157 const uint8_t *newcode, int codesize) {
158 void *handle = GetModuleHandleA("kernel32");
159 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
160 int res = 0;
161 int prot, tmp;
162 VirtProt = GetProcAddress(handle, "VirtualProtect");
163 // change permissions to PAGE_WRITECOPY
164 if (!VirtProt ||
165 !VirtProt(patchpos, codesize, 0x08, &prot)) {
166 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
167 return 0;
169 if (memcmp(patchpos, oldcode, codesize) == 0) {
170 memcpy(patchpos, newcode, codesize);
171 res = 1;
173 VirtProt(patchpos, codesize, prot, &tmp);
174 return res;
176 #endif
178 static int load_syms_windows(char *path) {
179 void *handle;
181 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
182 #ifdef WIN32_LOADER
183 Setup_LDT_Keeper();
184 #endif
185 handle = LoadLibraryA(path);
186 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
187 if (!handle) {
188 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
189 return 0;
192 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
193 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
194 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
195 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
197 if(wrvyuv_custom_message &&
198 wrvyuv_free &&
199 wrvyuv_init &&
200 wrvyuv_transform)
202 dll_type = 1;
203 rv_handle = handle;
204 #ifndef WIN32_LOADER
206 if (strstr(path, "drv43260.dll")) {
207 int patched;
208 // patch away multithreaded decoding, it causes crashes
209 static const uint8_t oldcode[13] = {
210 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
211 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
212 static const uint8_t newcode[13] = {
213 0x31, 0xc0,
214 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
215 0xe9, 0xd0, 0x00, 0x00, 0x00 };
216 patched = patch_dll(
217 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
218 oldcode, newcode, sizeof(oldcode));
219 if (!patched)
220 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
223 #endif
224 return 1;
227 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
228 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
229 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
230 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
231 if(wrvyuv_custom_message &&
232 wrvyuv_free &&
233 wrvyuv_init &&
234 wrvyuv_transform) {
235 dll_type = 1;
236 rv_handle = handle;
237 return 1;
240 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
241 FreeLibrary(handle);
242 return 0; // error
244 #endif
246 /* we need exact positions */
247 struct rv_init_t {
248 short unk1;
249 short w;
250 short h;
251 short unk3;
252 int unk2;
253 int subformat;
254 int unk5;
255 int format;
256 } rv_init_t;
258 // init driver
259 static int init(sh_video_t *sh){
260 //unsigned int out_fmt;
261 char *path;
262 int result;
263 // we export codec id and sub-id from demuxer in bitmapinfohdr:
264 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
265 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
266 struct rv_init_t init_data;
268 if(extrahdr_size < 8) {
269 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
270 return 0;
272 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 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]));
276 path = malloc(strlen(BINARY_CODECS_PATH) + strlen(sh->codec->dll) + 2);
277 if (!path) return 0;
278 sprintf(path, BINARY_CODECS_PATH "/%s", sh->codec->dll);
280 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
281 then try to load the windows ones */
282 #ifdef HAVE_LIBDL
283 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
284 #endif
285 #ifdef CONFIG_WIN32DLL
286 if (!load_syms_windows(sh->codec->dll))
287 #endif
289 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingDLLcodec,sh->codec->dll);
290 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
291 free(path);
292 return 0;
294 free(path);
295 // only I420 supported
296 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
297 // init codec:
298 sh->context=NULL;
299 #ifdef CONFIG_WIN32DLL
300 if (dll_type == 1)
301 result=(*wrvyuv_init)(&init_data, &sh->context);
302 else
303 #endif
304 result=(*rvyuv_init)(&init_data, &sh->context);
305 if (result){
306 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
307 return 0;
309 // setup rv30 codec (codec sub-type and image dimensions):
310 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
311 int i, cmsg_cnt;
312 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
313 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
315 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
316 cmsg_cnt = (extrahdr[1]&7)*2;
317 if (extrahdr_size-8 < cmsg_cnt) {
318 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
319 cmsg_cnt = extrahdr_size-8;
321 for (i = 0; i < cmsg_cnt; i++)
322 cmsg24[2+i] = extrahdr[8+i]*4;
323 if (extrahdr_size-8 > cmsg_cnt)
324 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
326 #ifdef CONFIG_WIN32DLL
327 if (dll_type == 1)
328 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
329 else
330 #endif
331 (*rvyuv_custom_message)(&cmsg_data,sh->context);
333 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
334 return 1;
337 // uninit driver
338 static void uninit(sh_video_t *sh){
339 #ifdef CONFIG_WIN32DLL
340 if (dll_type == 1)
342 if (wrvyuv_free) wrvyuv_free(sh->context);
343 } else
344 #endif
345 if(rvyuv_free) rvyuv_free(sh->context);
347 #ifdef CONFIG_WIN32DLL
348 if (dll_type == 1)
350 if (rv_handle) FreeLibrary(rv_handle);
351 } else
352 #endif
353 #ifdef HAVE_LIBDL
354 if(rv_handle) dlclose(rv_handle);
355 #endif
356 rv_handle=NULL;
357 initialized = 0;
358 if (buffer)
359 free(buffer);
360 buffer = NULL;
361 bufsz = 0;
364 // decode a frame
365 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
366 mp_image_t* mpi;
367 unsigned long result;
368 uint8_t *buf = data;
369 int chunks = *buf++;
370 int extra_size = 8*(chunks+1);
371 uint32_t data_size = len-1-extra_size;
372 unsigned char* dp_data=buf+extra_size;
373 uint32_t* extra=(uint32_t*)buf;
374 int i;
376 unsigned int transform_out[5];
377 transform_in_t transform_in={
378 data_size, // length of the packet (sub-packets appended)
379 0, // unknown, seems to be unused
380 chunks, // number of sub-packets - 1
381 extra, // table of sub-packet offsets
382 0, // unknown, seems to be unused
383 0, // timestamp (should be unneded)
386 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
388 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
389 if (buffer) free(buffer);
390 bufsz = sh->disp_w*sh->disp_h*3/2;
391 buffer=malloc(bufsz);
392 if (!buffer) return 0;
395 for (i=0; i<2*(chunks+1); i++)
396 extra[i] = le2me_32(extra[i]);
398 #ifdef CONFIG_WIN32DLL
399 if (dll_type == 1)
400 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
401 transform_out, sh->context);
402 else
403 #endif
404 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
405 transform_out, sh->context);
407 if(!initialized){ // rv30 width/height now known
408 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
409 sh->disp_w=transform_out[3];
410 sh->disp_h=transform_out[4];
411 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
412 initialized=1;
414 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
415 sh->disp_w, sh->disp_h);
416 if(!mpi) return NULL;
417 mpi->planes[0] = buffer;
418 mpi->stride[0] = sh->disp_w;
419 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
420 mpi->stride[1] = sh->disp_w / 2;
421 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
422 mpi->stride[2] = sh->disp_w / 2;
424 if(transform_out[0] &&
425 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
426 initialized = 0;
428 return result ? NULL : mpi;