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>
32 #include <vlc_codec.h>
35 /* Need the w32dll loader from mplayer */
36 # include <wine/winerror.h>
37 # include <ldt_keeper.h>
38 # include <wine/windef.h>
40 void *WINAPI LoadLibraryA( char *name );
41 void *WINAPI GetProcAddress( void *handle, char *func );
42 int WINAPI FreeLibrary( void *handle );
49 #if defined(HAVE_DL_DLOPEN)
50 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
53 # if defined(HAVE_SYS_DL_H)
58 typedef struct cmsg_data_s
65 typedef struct transform_in_s
75 // copypaste from demux_real.c - it should match to get it working!
76 typedef struct dp_hdr_s
78 uint32_t chunks; // number of chunks
79 uint32_t timestamp; // timestamp from packet header
80 uint32_t len; // length of actual data
81 uint32_t chunktab; // offset to chunk offset array
84 /* we need exact positions */
92 unsigned int * subformat;
94 unsigned int * format;
111 static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
112 static unsigned long (*rvyuv_free)(void*);
113 static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
114 static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
116 static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
117 static unsigned long WINAPI (*wrvyuv_free)(void*);
118 static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
119 static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
122 /*****************************************************************************
124 *****************************************************************************/
125 static int Open ( vlc_object_t * );
126 static void Close( vlc_object_t * );
128 //static int OpenPacketizer( vlc_object_t * );
129 static picture_t *DecodeVideo( decoder_t *, block_t ** );
132 set_description( N_("RealVideo library decoder") )
133 set_capability( "decoder", 10 )
134 set_category( CAT_INPUT )
135 set_subcategory( SUBCAT_INPUT_VCODEC )
136 set_callbacks( Open, Close )
140 /*****************************************************************************
142 *****************************************************************************/
145 static void * load_syms(decoder_t *p_dec, const char *path)
149 msg_Dbg( p_dec, "opening win32 dll '%s'", path);
153 handle = LoadLibraryA(path);
154 msg_Dbg( p_dec, "win32 real codec handle=%p",handle);
157 msg_Err( p_dec, "Error loading dll");
161 wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
162 wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
163 wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
164 wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
166 if (wrvyuv_custom_message && wrvyuv_free && wrvyuv_init && wrvyuv_transform)
171 msg_Err( p_dec, "Error resolving symbols! (version incompatibility?)");
173 return NULL; // error
176 static void * load_syms_linux(decoder_t *p_dec, const char *path)
180 msg_Dbg( p_dec, "opening shared obj '%s'", path);
182 handle = dlopen (path, RTLD_LAZY);
185 msg_Err( p_dec,"Error: %s",dlerror());
189 rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
190 rvyuv_free = dlsym(handle, "RV20toYUV420Free");
191 rvyuv_init = dlsym(handle, "RV20toYUV420Init");
192 rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
194 if(rvyuv_custom_message && rvyuv_free && rvyuv_init && rvyuv_transform)
200 msg_Err( p_dec,"Error resolving symbols! (version incompatibility?)");
206 static vlc_mutex_t rm_mutex = VLC_STATIC_MUTEX;
208 static int InitVideo(decoder_t *p_dec)
211 struct rv_init_t init_data;
215 int i_vide = p_dec->fmt_in.i_extra;
216 unsigned int *p_vide = p_dec->fmt_in.p_extra;
217 decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
218 memset(p_sys,0,sizeof( decoder_sys_t ) );
222 msg_Err( p_dec, "missing extra info" );
226 if (p_sys->plane) free(p_sys->plane);
227 p_sys->plane = malloc (p_dec->fmt_in.video.i_width*p_dec->fmt_in.video.i_height*3/2 + 1024 );
228 if (NULL == p_sys->plane)
230 msg_Err( p_dec, "cannot alloc plane buffer" );
235 p_dec->p_sys = p_sys;
236 p_dec->pf_decode_video = DecodeVideo;
238 memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
240 init_data.w = p_dec->fmt_in.video.i_width ;
241 init_data.h = p_dec->fmt_in.video.i_height ;
244 init_data.subformat = (unsigned int*)p_vide[0];
246 init_data.format = (unsigned int*)p_vide[1];
248 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
249 then try to load the windows ones */
250 bool b_so_opened = false;
253 g_decode_path="plugins\\drv43260.dll";
255 if( (p_sys->rv_handle = load_syms(p_dec, g_decode_path)) )
258 static const char psz_paths[] =
262 "/usr/local/RealPlayer8/Codecs\0"
263 "/usr/RealPlayer8/Codecs\0"
264 "/usr/lib/RealPlayer8/Codecs\0"
265 "/opt/RealPlayer8/Codecs\0"
266 "/usr/lib/RealPlayer9/users/Real/Codecs\0"
267 "/usr/lib/RealPlayer10/codecs\0"
268 "/usr/lib/RealPlayer10GOLD/codecs\0"
269 "/usr/lib/helix/player/codecs\0"
270 "/usr/lib64/RealPlayer8/Codecs\0"
271 "/usr/lib64/RealPlayer9/users/Real/Codecs\0"
272 "/usr/lib64/RealPlayer10/codecs\0"
273 "/usr/lib64/RealPlayer10GOLD/codecs\0"
274 "/usr/local/lib/codecs\0"
278 for( size_t i = 0; psz_paths[i]; i += strlen( psz_paths + i ) + 1 )
280 if( asprintf( &g_decode_path, "%s/drv4.so.6.0", psz_paths + i ) != -1 )
282 p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
283 free( g_decode_path );
285 if( p_sys->rv_handle )
291 if( asprintf( &g_decode_path, "%s/drv3.so.6.0", psz_paths + i ) != -1 )
293 p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
294 free( g_decode_path );
296 if( p_sys->rv_handle )
302 msg_Dbg( p_dec, "Cannot load real decoder library: %s", g_decode_path);
308 msg_Err( p_dec, "Cannot any real decoder library" );
313 vlc_mutex_lock( &rm_mutex );
318 result=(*wrvyuv_init)(&init_data, &p_sys->handle);
321 result=(*rvyuv_init)(&init_data, &p_sys->handle);
324 msg_Err( p_dec, "Cannot Init real decoder library: %s", g_decode_path);
329 /* setup rv30 codec (codec sub-type and image dimensions): */
330 /*if ( p_dec->fmt_in.i_codec == VLC_FOURCC('R','V','3','0') )*/
331 if (p_vide[1]>=0x20200002)
334 uint32_t cmsg24[16]={p_dec->fmt_in.video.i_width,p_dec->fmt_in.video.i_height};
335 cmsg_data_t cmsg_data={0x24,1+(p_vide[1]&7), &cmsg24[0]};
336 cmsg_cnt = (p_vide[1]&7)*2;
337 if (i_vide - 8 < cmsg_cnt) {
338 cmsg_cnt = i_vide - 8;
340 for (i = 0; i < cmsg_cnt; i++)
341 cmsg24[2+i] = p_vide[8+i]*4;
344 (*wrvyuv_custom_message)(&cmsg_data,p_sys->handle);
347 (*rvyuv_custom_message)(&cmsg_data,p_sys->handle);
350 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y','V','1','2' ));
351 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y','U','Y','2' ));
353 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'I', '4', '2', '0'));
355 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
356 p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
357 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
360 vlc_mutex_unlock( &rm_mutex );
364 /*****************************************************************************
365 * Open: probe the decoder and return score
366 *****************************************************************************
367 * Tries to launch a decoder and return score so that the interface is able
369 *****************************************************************************/
370 static int Open( vlc_object_t *p_this )
372 decoder_t *p_dec = (decoder_t*)p_this;
375 var_Create( p_this->p_libvlc, "rm_mutex", VLC_VAR_MUTEX );
377 switch ( p_dec->fmt_in.i_codec )
379 case VLC_FOURCC('R','V','1','0'):
380 case VLC_FOURCC('R','V','2','0'):
381 case VLC_FOURCC('R','V','3','0'):
382 case VLC_FOURCC('R','V','4','0'):
384 p_dec->pf_decode_video = DecodeVideo;
385 return InitVideo(p_dec);
392 /*****************************************************************************
394 *****************************************************************************/
395 static void Close( vlc_object_t *p_this )
397 decoder_t *p_dec = (decoder_t*)p_this;
398 decoder_sys_t *p_sys = p_dec->p_sys;
400 /* get lock, avoid segfault */
401 vlc_mutex_lock( &rm_mutex );
407 wrvyuv_free(p_sys->handle);
412 rvyuv_free(p_sys->handle);
416 if (p_sys->rv_handle)
417 FreeLibrary(p_sys->rv_handle);
421 p_sys->rv_handle=NULL;
429 msg_Dbg( p_dec, "FreeLibrary ok." );
431 Restore_LDT_Keeper( p_sys->ldt_fs );
432 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
436 vlc_mutex_unlock( &rm_mutex );
441 /*****************************************************************************
443 *****************************************************************************/
444 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
446 decoder_sys_t *p_sys = p_dec->p_sys;
452 /* We must do open and close in the same thread (unless we do
453 * Setup_LDT_Keeper in the main thread before all others */
454 if ( pp_block == NULL || *pp_block == NULL )
461 i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
463 vlc_mutex_lock( &rm_mutex );
465 p_pic = decoder_NewPicture( p_dec );
469 unsigned int transform_out[5];
471 transform_in_t transform_in;
472 uint32_t pkg_len = ((uint32_t*)p_block->p_buffer)[0];
473 unsigned char* dp_data=((unsigned char*)p_block->p_buffer)+8;
474 uint32_t* extra=(uint32_t*)(((char*)p_block->p_buffer)+8+pkg_len);
478 dp_hdr.len = pkg_len;
479 dp_hdr.chunktab = 8 + pkg_len;
480 dp_hdr.chunks = ((uint32_t*)p_block->p_buffer)[1]-1;
481 dp_hdr.timestamp = i_pts;
483 memset(&transform_in, 0, sizeof(transform_in_t));
485 transform_in.len = dp_hdr.len;
486 transform_in.extra = extra;
487 transform_in.chunks = dp_hdr.chunks;
488 transform_in.timestamp = dp_hdr.timestamp;
490 memset (p_sys->plane, 0, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height *3/2 );
494 result=(*wrvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
497 result=(*rvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
499 /* msg_Warn(p_dec, "Real Size %d X %d", transform_out[3],
500 transform_out[4]); */
501 /* some bug rm file will print the messages :
502 [00000551] realvideo decoder warning: Real Size 320 X 240
503 [00000551] realvideo decoder warning: Real Size 480 X 272
504 [00000551] realvideo decoder warning: Real Size 320 X 240
505 [00000551] realvideo decoder warning: Real Size 320 X 240
509 if ( p_sys->inited == 0 )
511 /* fix and get the correct image size! */
512 if ( p_dec->fmt_in.video.i_width != transform_out[3]
513 || p_dec->fmt_in.video.i_height != transform_out[4] )
515 msg_Warn(p_dec, "Warning, Real's Header give a wrong "
516 "information about media's width and height!"
517 "\tRealHeader: \t %d X %d \t %d X %d",
518 p_dec->fmt_in.video.i_width,
519 p_dec->fmt_in.video.i_height,
520 transform_out[3],transform_out[4]);
522 if ( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height >= transform_out[3] * transform_out[4] )
524 p_dec->fmt_out.video.i_width =
525 p_dec->fmt_out.video.i_visible_width =
526 p_dec->fmt_in.video.i_width = transform_out[3] ;
528 p_dec->fmt_out.video.i_height=
529 p_dec->fmt_out.video.i_visible_height =
530 p_dec->fmt_in.video.i_height= transform_out[4];
532 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
536 // TODO: realloc plane's size! but [in fact] it maybe not happen!
537 msg_Err(p_dec,"plane space not enough ,skip");
543 img_size = p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height;
544 memcpy( p_pic->p[0].p_pixels, p_sys->plane, img_size);
545 memcpy( p_pic->p[1].p_pixels, p_sys->plane + img_size, img_size/4);
546 memcpy( p_pic->p[2].p_pixels, p_sys->plane + img_size * 5/4, img_size/4);
547 p_pic->date = i_pts ;
549 /* real video frame is small( frame and frame's time-shift is short),
550 so it will become late picture easier (when render-time changed)and
551 droped by video-output.*/
555 vlc_mutex_unlock( &rm_mutex );
557 block_Release( p_block );