options: fix -profile parsing after 2db33ab48c
[mplayer/greg.git] / libmpcodecs / vd_realvid.c
blob8fc843855f561b4dc6fe79ed971f7c664ccc1abe
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 void __builtin_vec_delete(void *mem);
89 void __pure_virtual(void);
91 void *__builtin_vec_new(unsigned long size) {
92 return malloc(size);
95 void __builtin_vec_delete(void *mem) {
96 free(mem);
99 void __pure_virtual(void) {
100 printf("FATAL: __pure_virtual() called!\n");
101 // exit(1);
104 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
105 void ___brk_addr(void) {exit(0);}
106 char **__environ={NULL};
107 #undef stderr
108 FILE *stderr=NULL;
109 #endif
111 // to set/get/query special features/parameters
112 static int control(sh_video_t *sh,int cmd,void* arg,...){
113 // switch(cmd){
114 // case VDCTRL_QUERY_MAX_PP_LEVEL:
115 // return 9;
116 // case VDCTRL_SET_PP_LEVEL:
117 // vfw_set_postproc(sh,10*(*((int*)arg)));
118 // return CONTROL_OK;
119 // }
120 return CONTROL_UNKNOWN;
123 /* exits program when failure */
124 #ifdef HAVE_LIBDL
125 static int load_syms_linux(char *path) {
126 void *handle;
128 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
129 handle = dlopen (path, RTLD_LAZY);
130 if (!handle) {
131 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
132 return 0;
135 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
136 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
137 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
138 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
140 if(rvyuv_custom_message &&
141 rvyuv_free &&
142 rvyuv_init &&
143 rvyuv_transform)
145 rv_handle = handle;
146 return 1;
149 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
150 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
151 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
152 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
154 if(rvyuv_custom_message &&
155 rvyuv_free &&
156 rvyuv_init &&
157 rvyuv_transform)
159 rv_handle = handle;
160 return 1;
163 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
164 dlclose(handle);
165 return 0;
167 #endif
169 #ifdef CONFIG_WIN32DLL
171 #ifdef WIN32_LOADER
172 #include "loader/ldt_keeper.h"
173 #endif
174 void* WINAPI LoadLibraryA(char* name);
175 void* WINAPI GetProcAddress(void* handle,char* func);
176 int WINAPI FreeLibrary(void *handle);
178 #ifndef WIN32_LOADER
179 void * WINAPI GetModuleHandleA(char *);
180 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
181 const uint8_t *newcode, int codesize) {
182 void *handle = GetModuleHandleA("kernel32");
183 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
184 int res = 0;
185 int prot, tmp;
186 VirtProt = GetProcAddress(handle, "VirtualProtect");
187 // change permissions to PAGE_WRITECOPY
188 if (!VirtProt ||
189 !VirtProt(patchpos, codesize, 0x08, &prot)) {
190 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
191 return 0;
193 if (memcmp(patchpos, oldcode, codesize) == 0) {
194 memcpy(patchpos, newcode, codesize);
195 res = 1;
197 VirtProt(patchpos, codesize, prot, &tmp);
198 return res;
200 #endif
202 static int load_syms_windows(char *path) {
203 void *handle;
205 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
206 #ifdef WIN32_LOADER
207 Setup_LDT_Keeper();
208 #endif
209 handle = LoadLibraryA(path);
210 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
211 if (!handle) {
212 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
213 return 0;
216 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
217 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
218 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
219 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
221 if(wrvyuv_custom_message &&
222 wrvyuv_free &&
223 wrvyuv_init &&
224 wrvyuv_transform)
226 dll_type = 1;
227 rv_handle = handle;
228 #ifndef WIN32_LOADER
230 if (strstr(path, "drv43260.dll")) {
231 int patched;
232 // patch away multithreaded decoding, it causes crashes
233 static const uint8_t oldcode[13] = {
234 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
235 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
236 static const uint8_t newcode[13] = {
237 0x31, 0xc0,
238 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
239 0xe9, 0xd0, 0x00, 0x00, 0x00 };
240 patched = patch_dll(
241 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
242 oldcode, newcode, sizeof(oldcode));
243 if (!patched)
244 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
247 #endif
248 return 1;
251 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
252 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
253 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
254 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
255 if(wrvyuv_custom_message &&
256 wrvyuv_free &&
257 wrvyuv_init &&
258 wrvyuv_transform) {
259 dll_type = 1;
260 rv_handle = handle;
261 return 1;
264 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
265 FreeLibrary(handle);
266 return 0; // error
268 #endif
270 /* we need exact positions */
271 struct rv_init_t {
272 short unk1;
273 short w;
274 short h;
275 short unk3;
276 int unk2;
277 int subformat;
278 int unk5;
279 int format;
280 } rv_init_t;
282 // init driver
283 static int init(sh_video_t *sh){
284 //unsigned int out_fmt;
285 char *path;
286 int result;
287 // we export codec id and sub-id from demuxer in bitmapinfohdr:
288 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
289 unsigned int extrahdr_size = sh->bih->biSize - sizeof(*sh->bih);
290 struct rv_init_t init_data;
292 if(extrahdr_size < 8) {
293 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", extrahdr_size);
294 return 0;
296 init_data = (struct rv_init_t){11, sh->disp_w, sh->disp_h, 0, 0, AV_RB32(extrahdr), 1, AV_RB32(extrahdr + 4)}; // rv30
298 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo codec id: 0x%08X sub-id: 0x%08X\n",init_data.format,init_data.subformat);
300 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
301 if (!path) return 0;
302 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
304 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
305 then try to load the windows ones */
306 #ifdef HAVE_LIBDL
307 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
308 #endif
309 #ifdef CONFIG_WIN32DLL
310 if (!load_syms_windows(sh->codec->dll))
311 #endif
313 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"ERROR: Could not open required DirectShow codec %s.\n",sh->codec->dll);
314 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
315 free(path);
316 return 0;
318 free(path);
319 // only I420 supported
320 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
321 // init codec:
322 sh->context=NULL;
323 #ifdef CONFIG_WIN32DLL
324 if (dll_type == 1)
325 result=(*wrvyuv_init)(&init_data, &sh->context);
326 else
327 #endif
328 result=(*rvyuv_init)(&init_data, &sh->context);
329 if (result){
330 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
331 return 0;
333 // setup rv30 codec (codec sub-type and image dimensions):
334 if((sh->format<=0x30335652) && AV_RB32(extrahdr + 4)>=0x20200002){
335 int i, cmsg_cnt;
336 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
337 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
339 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
340 cmsg_cnt = (extrahdr[1]&7)*2;
341 if (extrahdr_size-8 < cmsg_cnt) {
342 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
343 cmsg_cnt = extrahdr_size-8;
345 for (i = 0; i < cmsg_cnt; i++)
346 cmsg24[2+i] = extrahdr[8+i]*4;
347 if (extrahdr_size-8 > cmsg_cnt)
348 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
350 #ifdef CONFIG_WIN32DLL
351 if (dll_type == 1)
352 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
353 else
354 #endif
355 (*rvyuv_custom_message)(&cmsg_data,sh->context);
357 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
358 return 1;
361 // uninit driver
362 static void uninit(sh_video_t *sh){
363 #ifdef CONFIG_WIN32DLL
364 if (dll_type == 1)
366 if (wrvyuv_free) wrvyuv_free(sh->context);
367 } else
368 #endif
369 if(rvyuv_free) rvyuv_free(sh->context);
371 #ifdef CONFIG_WIN32DLL
372 if (dll_type == 1)
374 if (rv_handle) FreeLibrary(rv_handle);
375 } else
376 #endif
377 #ifdef HAVE_LIBDL
378 if(rv_handle) dlclose(rv_handle);
379 #endif
380 rv_handle=NULL;
381 initialized = 0;
382 free(buffer);
383 buffer = NULL;
384 bufsz = 0;
387 // decode a frame
388 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
389 mp_image_t* mpi;
390 unsigned long result;
391 uint8_t *buf = data;
392 int chunks = *buf++;
393 int extra_size = 8*(chunks+1);
394 uint32_t data_size = len-1-extra_size;
395 unsigned char* dp_data=buf+extra_size;
396 uint32_t* extra=(uint32_t*)buf;
397 int i;
399 unsigned int transform_out[5];
400 transform_in_t transform_in={
401 data_size, // length of the packet (sub-packets appended)
402 0, // unknown, seems to be unused
403 chunks, // number of sub-packets - 1
404 extra, // table of sub-packet offsets
405 0, // unknown, seems to be unused
406 0, // timestamp (should be unneded)
409 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
411 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
412 free(buffer);
413 bufsz = sh->disp_w*sh->disp_h*3/2;
414 buffer=malloc(bufsz);
415 if (!buffer) return 0;
418 for (i=0; i<2*(chunks+1); i++)
419 extra[i] = le2me_32(extra[i]);
421 #ifdef CONFIG_WIN32DLL
422 if (dll_type == 1)
423 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
424 transform_out, sh->context);
425 else
426 #endif
427 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
428 transform_out, sh->context);
430 if(!initialized){ // rv30 width/height now known
431 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
432 sh->disp_w=transform_out[3];
433 sh->disp_h=transform_out[4];
434 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
435 initialized=1;
437 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
438 sh->disp_w, sh->disp_h);
439 if(!mpi) return NULL;
440 mpi->planes[0] = buffer;
441 mpi->stride[0] = sh->disp_w;
442 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
443 mpi->stride[1] = sh->disp_w / 2;
444 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
445 mpi->stride[2] = sh->disp_w / 2;
447 if(transform_out[0] &&
448 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
449 initialized = 0;
451 return result ? NULL : mpi;