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