Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpcodecs / vd_realvid.c
blob24d68657b2e6b137abc86355628eeb0723f177f4
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"
31 #include "path.h"
33 #include "vd_internal.h"
34 #include "loader/wine/windef.h"
36 static const vd_info_t info = {
37 "RealVideo decoder",
38 "realvid",
39 "Alex Beregszaszi",
40 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
41 "binary real video codecs"
44 LIBVD_EXTERN(realvid)
48 * Structures for data packets. These used to be tables of unsigned ints, but
49 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
50 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
51 * So we have to use structures so the compiler will assign the proper space
52 * for the pointer.
54 typedef struct cmsg_data_s {
55 uint32_t data1;
56 uint32_t data2;
57 uint32_t* dimensions;
58 } cmsg_data_t;
60 typedef struct transform_in_s {
61 uint32_t len;
62 uint32_t unknown1;
63 uint32_t chunks;
64 uint32_t* extra;
65 uint32_t unknown2;
66 uint32_t timestamp;
67 } transform_in_t;
69 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
70 static unsigned long (*rvyuv_free)(void*);
71 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
72 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
73 #ifdef CONFIG_WIN32DLL
74 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
75 static unsigned long WINAPI (*wrvyuv_free)(void*);
76 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
77 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
78 #endif
80 static void *rv_handle=NULL;
81 static int initialized=0;
82 static uint8_t *buffer = NULL;
83 static int bufsz = 0;
84 #ifdef CONFIG_WIN32DLL
85 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
86 #endif
88 // to set/get/query special features/parameters
89 static int control(sh_video_t *sh,int cmd,void* arg,...){
90 // switch(cmd){
91 // case VDCTRL_QUERY_MAX_PP_LEVEL:
92 // return 9;
93 // case VDCTRL_SET_PP_LEVEL:
94 // vfw_set_postproc(sh,10*(*((int*)arg)));
95 // return CONTROL_OK;
96 // }
97 return CONTROL_UNKNOWN;
100 /* exits program when failure */
101 #ifdef HAVE_LIBDL
102 static int load_syms_linux(char *path) {
103 void *handle;
105 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening shared obj '%s'\n", path);
106 handle = dlopen (path, RTLD_LAZY);
107 if (!handle) {
108 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error: %s\n",dlerror());
109 return 0;
112 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
113 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
114 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
115 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
117 if(rvyuv_custom_message &&
118 rvyuv_free &&
119 rvyuv_init &&
120 rvyuv_transform)
122 rv_handle = handle;
123 return 1;
126 rvyuv_custom_message = dlsym(handle, "RV40toYUV420CustomMessage");
127 rvyuv_free = dlsym(handle, "RV40toYUV420Free");
128 rvyuv_init = dlsym(handle, "RV40toYUV420Init");
129 rvyuv_transform = dlsym(handle, "RV40toYUV420Transform");
131 if(rvyuv_custom_message &&
132 rvyuv_free &&
133 rvyuv_init &&
134 rvyuv_transform)
136 rv_handle = handle;
137 return 1;
140 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
141 dlclose(handle);
142 return 0;
144 #endif
146 #ifdef CONFIG_WIN32DLL
148 #ifdef WIN32_LOADER
149 #include "loader/ldt_keeper.h"
150 #endif
151 void* WINAPI LoadLibraryA(char* name);
152 void* WINAPI GetProcAddress(void* handle,char* func);
153 int WINAPI FreeLibrary(void *handle);
155 #ifndef WIN32_LOADER
156 void * WINAPI GetModuleHandleA(char *);
157 static int patch_dll(uint8_t *patchpos, const uint8_t *oldcode,
158 const uint8_t *newcode, int codesize) {
159 void *handle = GetModuleHandleA("kernel32");
160 int WINAPI (*VirtProt)(void *, unsigned, int, int *);
161 int res = 0;
162 int prot, tmp;
163 VirtProt = GetProcAddress(handle, "VirtualProtect");
164 // change permissions to PAGE_WRITECOPY
165 if (!VirtProt ||
166 !VirtProt(patchpos, codesize, 0x08, &prot)) {
167 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "VirtualProtect failed at %p\n", patchpos);
168 return 0;
170 if (memcmp(patchpos, oldcode, codesize) == 0) {
171 memcpy(patchpos, newcode, codesize);
172 res = 1;
174 VirtProt(patchpos, codesize, prot, &tmp);
175 return res;
177 #endif
179 static int load_syms_windows(char *path) {
180 void *handle;
182 mp_msg(MSGT_DECVIDEO,MSGL_V, "opening win32 dll '%s'\n", path);
183 #ifdef WIN32_LOADER
184 Setup_LDT_Keeper();
185 #endif
186 handle = LoadLibraryA(path);
187 mp_msg(MSGT_DECVIDEO,MSGL_V,"win32 real codec handle=%p \n",handle);
188 if (!handle) {
189 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error loading dll\n");
190 return 0;
193 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
194 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
195 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
196 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
198 if(wrvyuv_custom_message &&
199 wrvyuv_free &&
200 wrvyuv_init &&
201 wrvyuv_transform)
203 dll_type = 1;
204 rv_handle = handle;
205 #ifndef WIN32_LOADER
207 if (strstr(path, "drv43260.dll")) {
208 int patched;
209 // patch away multithreaded decoding, it causes crashes
210 static const uint8_t oldcode[13] = {
211 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
212 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
213 static const uint8_t newcode[13] = {
214 0x31, 0xc0,
215 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
216 0xe9, 0xd0, 0x00, 0x00, 0x00 };
217 patched = patch_dll(
218 (char*)wrvyuv_transform + 0x634132fa - 0x634114d0,
219 oldcode, newcode, sizeof(oldcode));
220 if (!patched)
221 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
224 #endif
225 return 1;
228 wrvyuv_custom_message = GetProcAddress(handle, "RV40toYUV420CustomMessage");
229 wrvyuv_free = GetProcAddress(handle, "RV40toYUV420Free");
230 wrvyuv_init = GetProcAddress(handle, "RV40toYUV420Init");
231 wrvyuv_transform = GetProcAddress(handle, "RV40toYUV420Transform");
232 if(wrvyuv_custom_message &&
233 wrvyuv_free &&
234 wrvyuv_init &&
235 wrvyuv_transform) {
236 dll_type = 1;
237 rv_handle = handle;
238 return 1;
241 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
242 FreeLibrary(handle);
243 return 0; // error
245 #endif
247 /* we need exact positions */
248 struct rv_init_t {
249 short unk1;
250 short w;
251 short h;
252 short unk3;
253 int unk2;
254 int subformat;
255 int unk5;
256 int format;
257 } rv_init_t;
259 // init driver
260 static int init(sh_video_t *sh){
261 //unsigned int out_fmt;
262 char *path;
263 int result;
264 // we export codec id and sub-id from demuxer in bitmapinfohdr:
265 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
266 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
267 struct rv_init_t init_data;
269 if(extrahdr_size < 8) {
270 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
271 return 0;
273 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
275 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]));
277 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
278 if (!path) return 0;
279 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
281 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
282 then try to load the windows ones */
283 #ifdef HAVE_LIBDL
284 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
285 #endif
286 #ifdef CONFIG_WIN32DLL
287 if (!load_syms_windows(sh->codec->dll))
288 #endif
290 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingDLLcodec,sh->codec->dll);
291 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
292 free(path);
293 return 0;
295 free(path);
296 // only I420 supported
297 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
298 // init codec:
299 sh->context=NULL;
300 #ifdef CONFIG_WIN32DLL
301 if (dll_type == 1)
302 result=(*wrvyuv_init)(&init_data, &sh->context);
303 else
304 #endif
305 result=(*rvyuv_init)(&init_data, &sh->context);
306 if (result){
307 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
308 return 0;
310 // setup rv30 codec (codec sub-type and image dimensions):
311 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
312 int i, cmsg_cnt;
313 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
314 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
316 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
317 cmsg_cnt = (extrahdr[1]&7)*2;
318 if (extrahdr_size-8 < cmsg_cnt) {
319 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
320 cmsg_cnt = extrahdr_size-8;
322 for (i = 0; i < cmsg_cnt; i++)
323 cmsg24[2+i] = extrahdr[8+i]*4;
324 if (extrahdr_size-8 > cmsg_cnt)
325 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
327 #ifdef CONFIG_WIN32DLL
328 if (dll_type == 1)
329 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
330 else
331 #endif
332 (*rvyuv_custom_message)(&cmsg_data,sh->context);
334 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
335 return 1;
338 // uninit driver
339 static void uninit(sh_video_t *sh){
340 #ifdef CONFIG_WIN32DLL
341 if (dll_type == 1)
343 if (wrvyuv_free) wrvyuv_free(sh->context);
344 } else
345 #endif
346 if(rvyuv_free) rvyuv_free(sh->context);
348 #ifdef CONFIG_WIN32DLL
349 if (dll_type == 1)
351 if (rv_handle) FreeLibrary(rv_handle);
352 } else
353 #endif
354 #ifdef HAVE_LIBDL
355 if(rv_handle) dlclose(rv_handle);
356 #endif
357 rv_handle=NULL;
358 initialized = 0;
359 if (buffer)
360 free(buffer);
361 buffer = NULL;
362 bufsz = 0;
365 // decode a frame
366 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
367 mp_image_t* mpi;
368 unsigned long result;
369 uint8_t *buf = data;
370 int chunks = *buf++;
371 int extra_size = 8*(chunks+1);
372 uint32_t data_size = len-1-extra_size;
373 unsigned char* dp_data=buf+extra_size;
374 uint32_t* extra=(uint32_t*)buf;
375 int i;
377 unsigned int transform_out[5];
378 transform_in_t transform_in={
379 data_size, // length of the packet (sub-packets appended)
380 0, // unknown, seems to be unused
381 chunks, // number of sub-packets - 1
382 extra, // table of sub-packet offsets
383 0, // unknown, seems to be unused
384 0, // timestamp (should be unneded)
387 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
389 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
390 if (buffer) free(buffer);
391 bufsz = sh->disp_w*sh->disp_h*3/2;
392 buffer=malloc(bufsz);
393 if (!buffer) return 0;
396 for (i=0; i<2*(chunks+1); i++)
397 extra[i] = le2me_32(extra[i]);
399 #ifdef CONFIG_WIN32DLL
400 if (dll_type == 1)
401 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
402 transform_out, sh->context);
403 else
404 #endif
405 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
406 transform_out, sh->context);
408 if(!initialized){ // rv30 width/height now known
409 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
410 sh->disp_w=transform_out[3];
411 sh->disp_h=transform_out[4];
412 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
413 initialized=1;
415 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
416 sh->disp_w, sh->disp_h);
417 if(!mpi) return NULL;
418 mpi->planes[0] = buffer;
419 mpi->stride[0] = sh->disp_w;
420 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
421 mpi->stride[1] = sh->disp_w / 2;
422 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
423 mpi->stride[2] = sh->disp_w / 2;
425 if(transform_out[0] &&
426 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
427 initialized = 0;
429 return result ? NULL : mpi;