40d0542c72d4b5d9769d3400c7b7a8cd33c97427
[vlc.git] / modules / codec / realvideo.c
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
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 /*****************************************************************************
23  * Preamble
24  *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_vout.h>
32 #include <vlc_codec.h>
33
34 #ifdef LOADER
35 /* Need the w32dll loader from mplayer */
36 #   include <wine/winerror.h>
37 #   include <ldt_keeper.h>
38 #   include <wine/windef.h>
39
40 void *WINAPI LoadLibraryA( char *name );
41 void *WINAPI GetProcAddress( void *handle, char *func );
42 int WINAPI FreeLibrary( void *handle );
43 #endif
44
45 #ifndef WINAPI
46 #   define WINAPI
47 #endif
48
49 #if defined(HAVE_DL_DLOPEN)
50 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
51 #       include <dlfcn.h>
52 #   endif
53 #   if defined(HAVE_SYS_DL_H)
54 #       include <sys/dl.h>
55 #   endif
56 #endif
57
58 typedef struct cmsg_data_s
59 {
60     uint32_t data1;
61     uint32_t data2;
62     uint32_t* dimensions;
63 } cmsg_data_t;
64
65 typedef struct transform_in_s
66 {
67     uint32_t len;
68     uint32_t unknown1;
69     uint32_t chunks;
70     uint32_t* extra;
71     uint32_t unknown2;
72     uint32_t timestamp;
73 } transform_in_t;
74
75 // copypaste from demux_real.c - it should match to get it working!
76 typedef struct dp_hdr_s
77 {
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
82 } dp_hdr_t;
83
84 /* we need exact positions */
85 struct rv_init_t
86 {
87     short unk1;
88     short w;
89     short h;
90     short unk3;
91     int unk2;
92     unsigned int * subformat;
93     int unk5;
94     unsigned int * format;
95 } rv_init_t;
96
97 struct decoder_sys_t
98 {
99     /* library */
100 #ifdef LOADER
101     ldt_fs_t    *ldt_fs;
102 #endif
103     void        *handle;
104     void         *rv_handle;
105     int          inited;
106     char         *plane;
107 };
108
109 int dll_type = 1;
110
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*);
115 #ifdef WIN32
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*);
120 #endif
121
122 /*****************************************************************************
123  * Module descriptor
124  *****************************************************************************/
125 static int  Open ( vlc_object_t * );
126 static void Close( vlc_object_t * );
127
128 //static int  OpenPacketizer( vlc_object_t * );
129 static picture_t *DecodeVideo( decoder_t *, block_t ** );
130
131 vlc_module_begin();
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 );
137 vlc_module_end();
138
139
140 /*****************************************************************************
141  * Local prototypes
142  *****************************************************************************/
143
144 #ifdef WIN32
145 static void * load_syms(decoder_t *p_dec, const char *path) 
146 {
147     void *handle;
148
149     msg_Dbg( p_dec, "opening win32 dll '%s'\n", path);
150 #ifdef LOADER
151     Setup_LDT_Keeper();
152 #endif
153     handle = LoadLibraryA(path);
154     msg_Dbg( p_dec, "win32 real codec handle=%p  \n",handle);
155     if (!handle)
156     {
157         msg_Err( p_dec, "Error loading dll\n");
158         return NULL;
159     }
160
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");
165
166     if (wrvyuv_custom_message && wrvyuv_free && wrvyuv_init && wrvyuv_transform)
167     {
168         dll_type = 1;
169         return handle;
170     }
171     msg_Err( p_dec, "Error resolving symbols! (version incompatibility?)\n");
172     FreeLibrary(handle);
173     return NULL; // error
174 }
175 #else
176 static void * load_syms_linux(decoder_t *p_dec, const char *path) 
177 {
178     void *handle;
179
180     msg_Dbg( p_dec, "opening shared obj '%s'\n", path);
181
182     handle = dlopen (path, RTLD_LAZY);
183     if (!handle) 
184     {
185         msg_Err( p_dec,"Error: %s\n",dlerror());
186         return NULL;
187     }
188
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");
193
194     if(rvyuv_custom_message && rvyuv_free && rvyuv_init && rvyuv_transform)
195     {
196         dll_type = 0;
197         return handle;
198     }
199
200     msg_Err( p_dec,"Error resolving symbols! (version incompatibility?)\n");
201     dlclose(handle);
202     return 0;
203 }
204 #endif
205
206 static int InitVideo(decoder_t *p_dec)
207 {
208     int result;
209     struct rv_init_t init_data;
210     char fcc[4];
211     vlc_mutex_t  *lock;
212     char *g_decode_path;
213
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 = malloc( sizeof( decoder_sys_t ) );
217     memset(p_sys,0,sizeof( decoder_sys_t ) );
218
219     if( i_vide < 8 )
220     {
221             msg_Err( p_dec, "missing extra info" );
222             free( p_sys );
223             return VLC_EGENERIC;
224     }
225     if (p_sys->plane) free(p_sys->plane);
226     p_sys->plane = malloc (p_dec->fmt_in.video.i_width*p_dec->fmt_in.video.i_height*3/2 + 1024 );
227     if (NULL == p_sys->plane)
228     {
229         msg_Err( p_dec, "cannot alloc plane buffer" );
230         free( p_sys );
231         return VLC_EGENERIC;
232     }
233
234     p_dec->p_sys = p_sys;
235     p_dec->pf_decode_video = DecodeVideo;
236
237     memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
238     init_data.unk1 = 11;
239     init_data.w = p_dec->fmt_in.video.i_width ;
240     init_data.h = p_dec->fmt_in.video.i_height ;
241     init_data.unk3 = 0;
242     init_data.unk2 = 0;
243     init_data.subformat = (unsigned int*)p_vide[0];
244     init_data.unk5 = 1;
245     init_data.format = (unsigned int*)p_vide[1];
246
247     /* first try to load linux dlls, if failed and we're supporting win32 dlls,
248        then try to load the windows ones */
249     bool b_so_opened = false;
250
251 #ifdef WIN32
252     g_decode_path="plugins\\drv43260.dll";
253
254     if( (p_sys->rv_handle = load_syms(p_dec, g_decode_path)) )
255         b_so_opened = true;
256 #else
257     static const char psz_paths[] =
258     {
259         "/usr/lib/win32\0"
260         "/usr/lib/codecs\0"
261         "/usr/local/RealPlayer8/Codecs\0"
262         "/usr/RealPlayer8/Codecs\0"
263         "/usr/lib/RealPlayer8/Codecs\0"
264         "/opt/RealPlayer8/Codecs\0"
265         "/usr/lib/RealPlayer9/users/Real/Codecs\0"
266         "/usr/lib/RealPlayer10/codecs\0"
267         "/usr/lib/RealPlayer10GOLD/codecs\0"
268         "/usr/lib/helix/player/codecs\0"
269         "/usr/lib64/RealPlayer8/Codecs\0"
270         "/usr/lib64/RealPlayer9/users/Real/Codecs\0"
271         "/usr/lib64/RealPlayer10/codecs\0"
272         "/usr/lib64/RealPlayer10GOLD/codecs\0"
273         "/usr/local/lib/codecs\0"
274         "\0"
275     };
276
277     for( size_t i = 0; psz_paths[i]; i += strlen( psz_paths + i ) + 1 )
278     {
279         if( asprintf( &g_decode_path, "%s/drv4.so.6.0", psz_paths + i ) != -1 )
280         {
281             p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
282             free( g_decode_path );
283         }
284         if( p_sys->rv_handle )
285         {
286             b_so_opened = true;
287             break;
288         }
289
290         if( asprintf( &g_decode_path, "%s/drv3.so.6.0", psz_paths + i ) != -1 )
291         {
292             p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path);
293             free( g_decode_path );
294         }
295         if( p_sys->rv_handle )
296         {
297             b_so_opened = true;
298             break;
299         }
300
301         msg_Dbg( p_dec, "Cannot load real decoder library: %s", g_decode_path);
302     }
303 #endif
304
305     if(!b_so_opened )
306     {
307         msg_Err( p_dec, "Cannot any real decoder library" );
308         free( p_sys );
309         return VLC_EGENERIC;
310     }
311
312     lock = var_AcquireMutex( "rm_mutex" );
313     if ( lock == NULL )
314         return VLC_EGENERIC;
315
316     p_sys->handle=NULL;
317     #ifdef WIN32
318     if (dll_type == 1)
319         result=(*wrvyuv_init)(&init_data, &p_sys->handle);
320     else
321     #endif
322         result=(*rvyuv_init)(&init_data, &p_sys->handle);
323     if (result)
324     {
325         msg_Err( p_dec, "Cannot Init real decoder library: %s",  g_decode_path);
326         free( p_sys );
327         return VLC_EGENERIC;
328     }
329
330     /* setup rv30 codec (codec sub-type and image dimensions): */
331     /*if ( p_dec->fmt_in.i_codec == VLC_FOURCC('R','V','3','0') )*/
332     if (p_vide[1]>=0x20200002)
333     {
334         int i, cmsg_cnt;
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;
340         }
341         for (i = 0; i < cmsg_cnt; i++)
342             cmsg24[2+i] = p_vide[8+i]*4;
343         #ifdef WIN32
344         if (dll_type == 1)
345             (*wrvyuv_custom_message)(&cmsg_data,p_sys->handle);
346         else
347         #endif
348             (*rvyuv_custom_message)(&cmsg_data,p_sys->handle);
349     }
350     /*
351     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y','V','1','2' ));
352     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y','U','Y','2' ));
353      */
354     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'I', '4', '2', '0'));
355      
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;
359     p_sys->inited = 0;
360
361     vlc_mutex_unlock( lock );
362     return VLC_SUCCESS;
363 }
364
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
369  * to choose.
370  *****************************************************************************/
371 static int Open( vlc_object_t *p_this )
372 {
373     decoder_t *p_dec = (decoder_t*)p_this;
374
375     /* create a mutex */
376     var_Create( p_this->p_libvlc, "rm_mutex", VLC_VAR_MUTEX );
377
378     switch ( p_dec->fmt_in.i_codec )
379     {
380     case VLC_FOURCC('R','V','1','0'): 
381     case VLC_FOURCC('R','V','2','0'): 
382     case VLC_FOURCC('R','V','3','0'):
383     case VLC_FOURCC('R','V','4','0'): 
384         p_dec->p_sys = NULL;
385         p_dec->pf_decode_video = DecodeVideo;
386         return InitVideo(p_dec);
387
388     default:
389         return VLC_EGENERIC;
390     }
391 }
392
393 /*****************************************************************************
394  * Close:
395  *****************************************************************************/
396 static void Close( vlc_object_t *p_this )
397 {
398     decoder_t     *p_dec = (decoder_t*)p_this;
399     decoder_sys_t *p_sys = p_dec->p_sys;
400     vlc_mutex_t   *lock;
401
402     /* get lock, avoid segfault */
403     lock = var_AcquireMutex( "rm_mutex" );
404
405     #ifdef WIN32
406     if (dll_type == 1)
407     {
408         if (wrvyuv_free)
409             wrvyuv_free(p_sys->handle);
410     }
411     else
412     #endif
413         if (rvyuv_free)
414             rvyuv_free(p_sys->handle);
415 #ifdef WIN32
416     if (dll_type == 1)
417     {
418         if (p_sys->rv_handle)
419             FreeLibrary(p_sys->rv_handle);
420     }
421     else
422 #endif
423         p_sys->rv_handle=NULL;
424
425     if (p_sys->plane)
426     {
427         free(p_sys->plane);
428         p_sys->plane = NULL;
429     }
430
431     msg_Dbg( p_dec, "FreeLibrary ok." );
432 #ifdef LOADER
433     Restore_LDT_Keeper( p_sys->ldt_fs );
434     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
435 #endif
436     p_sys->inited = 0;
437
438     if ( lock )
439     vlc_mutex_unlock( lock );
440
441     if ( p_sys )
442         free( p_sys );
443 }
444
445 /*****************************************************************************
446  * DecodeVideo:
447  *****************************************************************************/
448 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
449 {
450     decoder_sys_t *p_sys = p_dec->p_sys;
451     vlc_mutex_t   *lock;
452     block_t       *p_block;
453     picture_t     *p_pic;
454     mtime_t       i_pts;
455     int           result;
456
457     /* We must do open and close in the same thread (unless we do
458      * Setup_LDT_Keeper in the main thread before all others */
459     if ( pp_block == NULL || *pp_block == NULL )
460     {
461         return NULL;
462     }
463     p_block = *pp_block;
464     *pp_block = NULL;
465
466     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
467
468     lock = var_AcquireMutex( "rm_mutex" );
469     if ( lock == NULL )
470         return NULL;
471
472     p_pic = p_dec->pf_vout_buffer_new( p_dec );
473
474     if ( p_pic )
475     {
476         unsigned int transform_out[5];
477         dp_hdr_t dp_hdr;
478         transform_in_t transform_in;
479         uint32_t pkg_len = ((uint32_t*)p_block->p_buffer)[0];
480         unsigned char* dp_data=((unsigned char*)p_block->p_buffer)+8;
481         uint32_t* extra=(uint32_t*)(((char*)p_block->p_buffer)+8+pkg_len);
482         uint32_t img_size;
483
484
485         dp_hdr.len = pkg_len;
486         dp_hdr.chunktab = 8 + pkg_len;
487         dp_hdr.chunks = ((uint32_t*)p_block->p_buffer)[1]-1;
488         dp_hdr.timestamp = i_pts;
489
490         memset(&transform_in, 0, sizeof(transform_in_t));
491
492         transform_in.len = dp_hdr.len;
493         transform_in.extra = extra;
494         transform_in.chunks = dp_hdr.chunks;
495         transform_in.timestamp = dp_hdr.timestamp;
496
497         memset (p_sys->plane, 0, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height *3/2 );
498
499         #ifdef WIN32
500         if (dll_type == 1)
501             result=(*wrvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
502         else
503         #endif
504             result=(*rvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
505
506         /* msg_Warn(p_dec, "Real Size %d X %d", transform_out[3],
507                  transform_out[4]); */
508         /* some bug rm file will print the messages :
509                 [00000551] realvideo decoder warning: Real Size 320 X 240
510                 [00000551] realvideo decoder warning: Real Size 480 X 272
511                 [00000551] realvideo decoder warning: Real Size 320 X 240
512                 [00000551] realvideo decoder warning: Real Size 320 X 240
513                 ...
514             so it needs fixing!
515         */
516         if ( p_sys->inited == 0 )
517         {
518             /* fix and get the correct image size! */
519             if ( p_dec->fmt_in.video.i_width != transform_out[3]
520              || p_dec->fmt_in.video.i_height  != transform_out[4] )
521             {
522                 msg_Warn(p_dec, "Warning, Real's Header give a wrong "
523                          "information about media's width and height!\n"
524                          "\tRealHeader: \t %d X %d  \t %d X %d",
525                          p_dec->fmt_in.video.i_width,
526                          p_dec->fmt_in.video.i_height,
527                          transform_out[3],transform_out[4]);
528                 
529                 if ( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height >= transform_out[3] * transform_out[4] )
530                 {
531                     p_dec->fmt_out.video.i_width = 
532                     p_dec->fmt_out.video.i_visible_width = 
533                     p_dec->fmt_in.video.i_width = transform_out[3] ;
534
535                     p_dec->fmt_out.video.i_height= 
536                     p_dec->fmt_out.video.i_visible_height = 
537                     p_dec->fmt_in.video.i_height= transform_out[4];
538
539                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
540                 }
541                 else
542                 {
543                     // TODO: realloc plane's size! but [in fact] it maybe not happen! 
544                     msg_Err(p_dec,"plane space not enough ,skip");
545                 }
546             }
547             p_sys->inited = 1;
548         }
549
550         img_size = p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height;
551         memcpy( p_pic->p[0].p_pixels, p_sys->plane,  img_size);
552         memcpy( p_pic->p[1].p_pixels, p_sys->plane + img_size, img_size/4);
553         memcpy( p_pic->p[2].p_pixels, p_sys->plane + img_size * 5/4, img_size/4);
554         p_pic->date = i_pts ;
555
556         /*  real video frame is small( frame and frame's time-shift is short), 
557             so it will become late picture easier (when render-time changed)and
558             droped by video-output.*/
559         p_pic->b_force = 1;
560     }
561
562     vlc_mutex_unlock( lock );
563
564     block_Release( p_block );
565     return p_pic;
566 }
567