1 /*****************************************************************************
2 * realvideo.c: a realvideo decoder that uses the real's dll
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * Copyright (C) 2008 Wang Bo
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
22 /*****************************************************************************
24 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_codec.h>
34 /* Need the w32dll loader from mplayer */
35 # include <wine/winerror.h>
36 # include <ldt_keeper.h>
37 # include <wine/windef.h>
39 void *WINAPI LoadLibraryA( char *name );
40 void *WINAPI GetProcAddress( void *handle, char *func );
41 int WINAPI FreeLibrary( void *handle );
48 #if defined(HAVE_DL_DLOPEN)
49 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
52 # if defined(HAVE_SYS_DL_H)
57 typedef struct cmsg_data_s
64 typedef struct transform_in_s
74 // copypaste from demux_real.c - it should match to get it working!
75 typedef struct dp_hdr_s
77 uint32_t chunks; // number of chunks
78 uint32_t timestamp; // timestamp from packet header
79 uint32_t len; // length of actual data
80 uint32_t chunktab; // offset to chunk offset array
83 /* we need exact positions */
91 unsigned int * subformat;
93 unsigned int * format;
110 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
111 static unsigned long (*rvyuv_free)(void*);
112 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
113 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
115 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
116 static unsigned long WINAPI (*wrvyuv_free)(void*);
117 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
118 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
121 /*****************************************************************************
123 *****************************************************************************/
124 static int Open ( vlc_object_t * );
125 static void Close( vlc_object_t * );
127 //static int OpenPacketizer( vlc_object_t * );
128 static picture_t *DecodeVideo( decoder_t *, block_t ** );
131 set_description( N_("RealVideo library decoder") )
132 set_capability( "decoder", 10 )
133 set_category( CAT_INPUT )
134 set_subcategory( SUBCAT_INPUT_VCODEC )
135 set_callbacks( Open, Close )
139 /*****************************************************************************
141 *****************************************************************************/
144 static void * load_syms(decoder_t *p_dec, const char *path)
148 msg_Dbg( p_dec, "opening win32 dll '%s'", path);
152 handle = LoadLibraryA(path);
153 msg_Dbg( p_dec, "win32 real codec handle=%p",handle);
156 msg_Err( p_dec, "Error loading dll");
160 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
161 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
162 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
163 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
165 if (wrvyuv_custom_message && wrvyuv_free && wrvyuv_init && wrvyuv_transform)
170 msg_Err( p_dec, "Error resolving symbols! (version incompatibility?)");
172 return NULL; // error
175 static void * load_syms_linux(decoder_t *p_dec, const char *path)
179 msg_Dbg( p_dec, "opening shared obj '%s'", path);
181 handle = dlopen (path, RTLD_LAZY);
184 msg_Err( p_dec,"Error: %s",dlerror());
188 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
189 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
190 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
191 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
193 if(rvyuv_custom_message && rvyuv_free && rvyuv_init && rvyuv_transform)
199 msg_Err( p_dec,"Error resolving symbols! (version incompatibility?)");
205 static vlc_mutex_t rm_mutex = VLC_STATIC_MUTEX;
207 static int InitVideo(decoder_t *p_dec)
210 struct rv_init_t init_data;
214 int i_vide = p_dec->fmt_in.i_extra;
215 unsigned int *p_vide = p_dec->fmt_in.p_extra;
216 decoder_sys_t *p_sys = calloc( 1, sizeof( decoder_sys_t ) );
223 msg_Err( p_dec, "missing extra info" );
227 free( p_sys->plane );
228 p_sys->plane = malloc (p_dec->fmt_in.video.i_width*p_dec->fmt_in.video.i_height*3/2 + 1024 );
229 if (NULL == p_sys->plane)
231 msg_Err( p_dec, "cannot alloc plane buffer" );
236 p_dec->p_sys = p_sys;
237 p_dec->pf_decode_video = DecodeVideo;
239 memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
241 init_data.w = p_dec->fmt_in.video.i_width ;
242 init_data.h = p_dec->fmt_in.video.i_height ;
245 init_data.subformat = (unsigned int*)p_vide[0];
247 init_data.format = (unsigned int*)p_vide[1];
249 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
250 then try to load the windows ones */
251 bool b_so_opened = false;
254 g_decode_path="plugins\\drv43260.dll";
256 if( (p_sys->rv_handle = load_syms(p_dec, g_decode_path)) )
259 static const char psz_paths[] =
263 "/usr/local/RealPlayer8/Codecs\0"
264 "/usr/RealPlayer8/Codecs\0"
265 "/usr/lib/RealPlayer8/Codecs\0"
266 "/opt/RealPlayer8/Codecs\0"
267 "/usr/lib/RealPlayer9/users/Real/Codecs\0"
268 "/usr/lib/RealPlayer10/codecs\0"
269 "/usr/lib/RealPlayer10GOLD/codecs\0"
270 "/usr/lib/helix/player/codecs\0"
271 "/usr/lib64/RealPlayer8/Codecs\0"
272 "/usr/lib64/RealPlayer9/users/Real/Codecs\0"
273 "/usr/lib64/RealPlayer10/codecs\0"
274 "/usr/lib64/RealPlayer10GOLD/codecs\0"
275 "/usr/local/lib/codecs\0"
279 for( size_t i = 0; psz_paths[i]; i += strlen( psz_paths + i ) + 1 )
281 if( asprintf( &g_decode_path, "%s/drv4.so.6.0", psz_paths + i ) != -1 )
283 p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
284 free( g_decode_path );
286 if( p_sys->rv_handle )
292 if( asprintf( &g_decode_path, "%s/drv3.so.6.0", psz_paths + i ) != -1 )
294 p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
295 free( g_decode_path );
297 if( p_sys->rv_handle )
303 msg_Dbg( p_dec, "Cannot load real decoder library: %s", g_decode_path);
309 msg_Err( p_dec, "Cannot any real decoder library" );
314 vlc_mutex_lock( &rm_mutex );
319 result=(*wrvyuv_init)(&init_data, &p_sys->handle);
322 result=(*rvyuv_init)(&init_data, &p_sys->handle);
325 msg_Err( p_dec, "Cannot Init real decoder library: %s", g_decode_path);
330 /* setup rv30 codec (codec sub-type and image dimensions): */
331 /*if ( p_dec->fmt_in.i_codec == VLC_CODEC_RV30 )*/
332 if (p_vide[1]>=0x20200002)
335 uint32_t cmsg24[16]={p_dec->fmt_in.video.i_width,p_dec->fmt_in.video.i_height};
336 cmsg_data_t cmsg_data={0x24,1+(p_vide[1]&7), &cmsg24[0]};
337 cmsg_cnt = (p_vide[1]&7)*2;
338 if (i_vide - 8 < cmsg_cnt) {
339 cmsg_cnt = i_vide - 8;
341 for (i = 0; i < cmsg_cnt; i++)
342 cmsg24[2+i] = p_vide[8+i]*4;
345 (*wrvyuv_custom_message)(&cmsg_data,p_sys->handle);
348 (*rvyuv_custom_message)(&cmsg_data,p_sys->handle);
351 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YV12);
352 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YUYV);
354 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_I420);
356 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
357 p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
358 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
361 vlc_mutex_unlock( &rm_mutex );
365 /*****************************************************************************
366 * Open: probe the decoder and return score
367 *****************************************************************************
368 * Tries to launch a decoder and return score so that the interface is able
370 *****************************************************************************/
371 static int Open( vlc_object_t *p_this )
373 decoder_t *p_dec = (decoder_t*)p_this;
376 var_Create( p_this->p_libvlc, "rm_mutex", VLC_VAR_MUTEX );
378 switch ( p_dec->fmt_in.i_codec )
385 p_dec->pf_decode_video = DecodeVideo;
386 return InitVideo(p_dec);
393 /*****************************************************************************
395 *****************************************************************************/
396 static void Close( vlc_object_t *p_this )
398 decoder_t *p_dec = (decoder_t*)p_this;
399 decoder_sys_t *p_sys = p_dec->p_sys;
401 /* get lock, avoid segfault */
402 vlc_mutex_lock( &rm_mutex );
408 wrvyuv_free(p_sys->handle);
413 rvyuv_free(p_sys->handle);
417 if (p_sys->rv_handle)
418 FreeLibrary(p_sys->rv_handle);
422 p_sys->rv_handle=NULL;
424 free( p_sys->plane );
427 msg_Dbg( p_dec, "FreeLibrary ok." );
429 Restore_LDT_Keeper( p_sys->ldt_fs );
430 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
434 vlc_mutex_unlock( &rm_mutex );
439 /*****************************************************************************
441 *****************************************************************************/
442 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
444 decoder_sys_t *p_sys = p_dec->p_sys;
450 /* We must do open and close in the same thread (unless we do
451 * Setup_LDT_Keeper in the main thread before all others */
452 if ( pp_block == NULL || *pp_block == NULL )
459 i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
461 vlc_mutex_lock( &rm_mutex );
463 p_pic = decoder_NewPicture( p_dec );
467 unsigned int transform_out[5];
469 transform_in_t transform_in;
470 uint32_t pkg_len = ((uint32_t*)p_block->p_buffer)[0];
471 unsigned char* dp_data=((unsigned char*)p_block->p_buffer)+8;
472 uint32_t* extra=(uint32_t*)(((char*)p_block->p_buffer)+8+pkg_len);
476 dp_hdr.len = pkg_len;
477 dp_hdr.chunktab = 8 + pkg_len;
478 dp_hdr.chunks = ((uint32_t*)p_block->p_buffer)[1]-1;
479 dp_hdr.timestamp = i_pts;
481 memset(&transform_in, 0, sizeof(transform_in_t));
483 transform_in.len = dp_hdr.len;
484 transform_in.extra = extra;
485 transform_in.chunks = dp_hdr.chunks;
486 transform_in.timestamp = dp_hdr.timestamp;
488 memset (p_sys->plane, 0, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height *3/2 );
492 result=(*wrvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
495 result=(*rvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
497 /* msg_Warn(p_dec, "Real Size %d X %d", transform_out[3],
498 transform_out[4]); */
499 /* some bug rm file will print the messages :
500 [00000551] realvideo decoder warning: Real Size 320 X 240
501 [00000551] realvideo decoder warning: Real Size 480 X 272
502 [00000551] realvideo decoder warning: Real Size 320 X 240
503 [00000551] realvideo decoder warning: Real Size 320 X 240
507 if ( p_sys->inited == 0 )
509 /* fix and get the correct image size! */
510 if ( p_dec->fmt_in.video.i_width != transform_out[3]
511 || p_dec->fmt_in.video.i_height != transform_out[4] )
513 msg_Warn(p_dec, "Warning, Real's Header give a wrong "
514 "information about media's width and height!"
515 "\tRealHeader: \t %d X %d \t %d X %d",
516 p_dec->fmt_in.video.i_width,
517 p_dec->fmt_in.video.i_height,
518 transform_out[3],transform_out[4]);
520 if ( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height >= transform_out[3] * transform_out[4] )
522 p_dec->fmt_out.video.i_width =
523 p_dec->fmt_out.video.i_visible_width =
524 p_dec->fmt_in.video.i_width = transform_out[3] ;
526 p_dec->fmt_out.video.i_height=
527 p_dec->fmt_out.video.i_visible_height =
528 p_dec->fmt_in.video.i_height= transform_out[4];
530 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
534 // TODO: realloc plane's size! but [in fact] it maybe not happen!
535 msg_Err(p_dec,"plane space not enough ,skip");
541 img_size = p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height;
542 memcpy( p_pic->p[0].p_pixels, p_sys->plane, img_size);
543 memcpy( p_pic->p[1].p_pixels, p_sys->plane + img_size, img_size/4);
544 memcpy( p_pic->p[2].p_pixels, p_sys->plane + img_size * 5/4, img_size/4);
545 p_pic->date = i_pts ;
547 /* real video frame is small( frame and frame's time-shift is short),
548 so it will become late picture easier (when render-time changed)and
549 droped by video-output.*/
553 vlc_mutex_unlock( &rm_mutex );
555 block_Release( p_block );