Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vd_realvid.c
blobbabd15d12593b13b8ecf95f20c2ad751df6bb461
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
6 #ifdef HAVE_LIBDL
7 #include <dlfcn.h>
8 #endif
10 #include "mp_msg.h"
11 #include "help_mp.h"
13 #include "vd_internal.h"
14 #include "loader/wine/windef.h"
16 static vd_info_t info = {
17 "RealVideo decoder",
18 "realvid",
19 "Alex Beregszaszi",
20 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
21 "binary real video codecs"
24 LIBVD_EXTERN(realvid)
28 * Structures for data packets. These used to be tables of unsigned ints, but
29 * that does not work on 64 bit platforms (e.g. Alpha). The entries that are
30 * pointers get truncated. Pointers on 64 bit platforms are 8 byte longs.
31 * So we have to use structures so the compiler will assign the proper space
32 * for the pointer.
34 typedef struct cmsg_data_s {
35 uint32_t data1;
36 uint32_t data2;
37 uint32_t* dimensions;
38 } cmsg_data_t;
40 typedef struct transform_in_s {
41 uint32_t len;
42 uint32_t unknown1;
43 uint32_t chunks;
44 uint32_t* extra;
45 uint32_t unknown2;
46 uint32_t timestamp;
47 } transform_in_t;
49 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
50 static unsigned long (*rvyuv_free)(void*);
51 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
52 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
53 #ifdef USE_WIN32DLL
54 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
55 static unsigned long WINAPI (*wrvyuv_free)(void*);
56 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
57 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
58 #endif
60 static void *rv_handle=NULL;
61 static int inited=0;
62 static uint8_t *buffer = NULL;
63 static int bufsz = 0;
64 #ifdef USE_WIN32DLL
65 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
66 #endif
68 void *__builtin_vec_new(unsigned long size) {
69 return malloc(size);
72 void __builtin_vec_delete(void *mem) {
73 free(mem);
76 void __pure_virtual(void) {
77 printf("FATAL: __pure_virtual() called!\n");
78 // exit(1);
81 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
82 void ___brk_addr(void) {exit(0);}
83 char **__environ={NULL};
84 #undef stderr
85 FILE *stderr=NULL;
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 USE_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 int patched = 0;
208 // drv43260.dll
209 if (wrvyuv_transform == (void *)0x634114d0) {
210 // patch away multithreaded decoding, it causes crashes
211 static const uint8_t oldcode[13] = {
212 0x83, 0xbb, 0xf8, 0x05, 0x00, 0x00, 0x01,
213 0x0f, 0x86, 0xd0, 0x00, 0x00, 0x00 };
214 static const uint8_t newcode[13] = {
215 0x31, 0xc0,
216 0x89, 0x83, 0xf8, 0x05, 0x00, 0x00,
217 0xe9, 0xd0, 0x00, 0x00, 0x00 };
218 patched = patch_dll((void *)0x634132fa, oldcode, newcode,
219 sizeof(oldcode));
221 if (!patched)
222 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Could not patch Real codec, this might crash on multi-CPU systems\n");
224 #endif
225 return 1;
227 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Error resolving symbols! (version incompatibility?)\n");
228 FreeLibrary(handle);
229 return 0; // error
231 #endif
233 /* we need exact positions */
234 struct rv_init_t {
235 short unk1;
236 short w;
237 short h;
238 short unk3;
239 int unk2;
240 int subformat;
241 int unk5;
242 int format;
243 } rv_init_t;
245 // init driver
246 static int init(sh_video_t *sh){
247 //unsigned int out_fmt;
248 char *path;
249 int result;
250 // we export codec id and sub-id from demuxer in bitmapinfohdr:
251 unsigned char* extrahdr=(unsigned char*)(sh->bih+1);
252 unsigned int extrahdr_size = sh->bih->biSize - sizeof(BITMAPINFOHEADER);
253 struct rv_init_t init_data;
255 if(extrahdr_size < 8) {
256 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"realvideo: extradata too small (%u)\n", sh->bih->biSize - sizeof(BITMAPINFOHEADER));
257 return 0;
259 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
261 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]));
263 path = malloc(strlen(REALCODEC_PATH)+strlen(sh->codec->dll)+2);
264 if (!path) return 0;
265 sprintf(path, REALCODEC_PATH "/%s", sh->codec->dll);
267 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
268 then try to load the windows ones */
269 #ifdef HAVE_LIBDL
270 if(strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
271 #endif
272 #ifdef USE_WIN32DLL
273 if (!load_syms_windows(sh->codec->dll))
274 #endif
276 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingDLLcodec,sh->codec->dll);
277 mp_msg(MSGT_DECVIDEO,MSGL_HINT,"Read the RealVideo section of the DOCS!\n");
278 free(path);
279 return 0;
281 free(path);
282 // only I420 supported
283 // if((sh->format!=0x30335652) && !mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
284 // init codec:
285 sh->context=NULL;
286 #ifdef USE_WIN32DLL
287 if (dll_type == 1)
288 result=(*wrvyuv_init)(&init_data, &sh->context);
289 else
290 #endif
291 result=(*rvyuv_init)(&init_data, &sh->context);
292 if (result){
293 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't open RealVideo codec, error code: 0x%X \n",result);
294 return 0;
296 // setup rv30 codec (codec sub-type and image dimensions):
297 if((sh->format<=0x30335652) && (be2me_32(((unsigned int*)extrahdr)[1])>=0x20200002)){
298 int i, cmsg_cnt;
299 uint32_t cmsg24[16]={sh->disp_w,sh->disp_h};
300 cmsg_data_t cmsg_data={0x24,1+(extrahdr[1]&7), &cmsg24[0]};
302 mp_msg(MSGT_DECVIDEO,MSGL_V,"realvideo: using cmsg24 with %u elements.\n",extrahdr[1]&7);
303 cmsg_cnt = (extrahdr[1]&7)*2;
304 if (extrahdr_size-8 < cmsg_cnt) {
305 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: not enough extradata (%u) to make %u cmsg24 elements.\n",extrahdr_size-8,extrahdr[1]&7);
306 cmsg_cnt = extrahdr_size-8;
308 for (i = 0; i < cmsg_cnt; i++)
309 cmsg24[2+i] = extrahdr[8+i]*4;
310 if (extrahdr_size-8 > cmsg_cnt)
311 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"realvideo: %u bytes of unknown extradata remaining.\n",extrahdr_size-8-cmsg_cnt);
313 #ifdef USE_WIN32DLL
314 if (dll_type == 1)
315 (*wrvyuv_custom_message)(&cmsg_data,sh->context);
316 else
317 #endif
318 (*rvyuv_custom_message)(&cmsg_data,sh->context);
320 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: RealVideo codec init OK!\n");
321 return 1;
324 // uninit driver
325 static void uninit(sh_video_t *sh){
326 #ifdef USE_WIN32DLL
327 if (dll_type == 1)
329 if (wrvyuv_free) wrvyuv_free(sh->context);
330 } else
331 #endif
332 if(rvyuv_free) rvyuv_free(sh->context);
334 #ifdef USE_WIN32DLL
335 if (dll_type == 1)
337 if (rv_handle) FreeLibrary(rv_handle);
338 } else
339 #endif
340 #ifdef HAVE_LIBDL
341 if(rv_handle) dlclose(rv_handle);
342 #endif
343 rv_handle=NULL;
344 inited = 0;
345 if (buffer)
346 free(buffer);
347 buffer = NULL;
348 bufsz = 0;
351 // copypaste from demux_real.c - it should match to get it working!
352 typedef struct dp_hdr_s {
353 uint32_t chunks; // number of chunks
354 uint32_t timestamp; // timestamp from packet header
355 uint32_t len; // length of actual data
356 uint32_t chunktab; // offset to chunk offset array
357 } dp_hdr_t;
359 // decode a frame
360 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
361 mp_image_t* mpi;
362 unsigned long result;
363 dp_hdr_t* dp_hdr=(dp_hdr_t*)data;
364 unsigned char* dp_data=((unsigned char*)data)+sizeof(dp_hdr_t);
365 uint32_t* extra=(uint32_t*)(((char*)data)+dp_hdr->chunktab);
367 unsigned int transform_out[5];
368 transform_in_t transform_in={
369 dp_hdr->len, // length of the packet (sub-packets appended)
370 0, // unknown, seems to be unused
371 dp_hdr->chunks, // number of sub-packets - 1
372 extra, // table of sub-packet offsets
373 0, // unknown, seems to be unused
374 dp_hdr->timestamp,// timestamp (the integer value from the stream)
377 if(len<=0 || flags&2) return NULL; // skipped frame || hardframedrop
379 if (bufsz < sh->disp_w*sh->disp_h*3/2) {
380 if (buffer) free(buffer);
381 bufsz = sh->disp_w*sh->disp_h*3/2;
382 buffer=malloc(bufsz);
383 if (!buffer) return 0;
386 #ifdef USE_WIN32DLL
387 if (dll_type == 1)
388 result=(*wrvyuv_transform)(dp_data, buffer, &transform_in,
389 transform_out, sh->context);
390 else
391 #endif
392 result=(*rvyuv_transform)(dp_data, buffer, &transform_in,
393 transform_out, sh->context);
395 if(!inited){ // rv30 width/height now known
396 sh->aspect=(float)sh->disp_w/(float)sh->disp_h;
397 sh->disp_w=transform_out[3];
398 sh->disp_h=transform_out[4];
399 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_I420)) return 0;
400 inited=1;
402 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0 /*MP_IMGFLAG_ACCEPT_STRIDE*/,
403 sh->disp_w, sh->disp_h);
404 if(!mpi) return NULL;
405 mpi->planes[0] = buffer;
406 mpi->stride[0] = sh->disp_w;
407 mpi->planes[1] = buffer + sh->disp_w*sh->disp_h;
408 mpi->stride[1] = sh->disp_w / 2;
409 mpi->planes[2] = buffer + sh->disp_w*sh->disp_h*5/4;
410 mpi->stride[2] = sh->disp_w / 2;
412 if(transform_out[0] &&
413 (sh->disp_w != transform_out[3] || sh->disp_h != transform_out[4]))
414 inited = 0;
416 return (result?NULL:mpi);