]> git.sesse.net Git - vlc/blob - modules/codec/realvideo.c
Merge branch 1.0-bugfix
[vlc] / 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_codec.h>
32
33 #ifdef LOADER
34 /* Need the w32dll loader from mplayer */
35 #   include <wine/winerror.h>
36 #   include <ldt_keeper.h>
37 #   include <wine/windef.h>
38
39 void *WINAPI LoadLibraryA( char *name );
40 void *WINAPI GetProcAddress( void *handle, char *func );
41 int WINAPI FreeLibrary( void *handle );
42 #endif
43
44 #ifndef WINAPI
45 #   define WINAPI
46 #endif
47
48 #if defined(HAVE_DL_DLOPEN)
49 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
50 #       include <dlfcn.h>
51 #   endif
52 #   if defined(HAVE_SYS_DL_H)
53 #       include <sys/dl.h>
54 #   endif
55 #endif
56
57 typedef struct cmsg_data_s
58 {
59     uint32_t data1;
60     uint32_t data2;
61     uint32_t* dimensions;
62 } cmsg_data_t;
63
64 typedef struct transform_in_s
65 {
66     uint32_t len;
67     uint32_t unknown1;
68     uint32_t chunks;
69     uint32_t* extra;
70     uint32_t unknown2;
71     uint32_t timestamp;
72 } transform_in_t;
73
74 // copypaste from demux_real.c - it should match to get it working!
75 typedef struct dp_hdr_s
76 {
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
81 } dp_hdr_t;
82
83 /* we need exact positions */
84 struct rv_init_t
85 {
86     short unk1;
87     short w;
88     short h;
89     short unk3;
90     int unk2;
91     unsigned int * subformat;
92     int unk5;
93     unsigned int * format;
94 } rv_init_t;
95
96 struct decoder_sys_t
97 {
98     /* library */
99 #ifdef LOADER
100     ldt_fs_t    *ldt_fs;
101 #endif
102     void        *handle;
103     void         *rv_handle;
104     int          inited;
105     char         *plane;
106 };
107
108 int dll_type = 1;
109
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*);
114 #ifdef WIN32
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*);
119 #endif
120
121 /*****************************************************************************
122  * Module descriptor
123  *****************************************************************************/
124 static int  Open ( vlc_object_t * );
125 static void Close( vlc_object_t * );
126
127 //static int  OpenPacketizer( vlc_object_t * );
128 static picture_t *DecodeVideo( decoder_t *, block_t ** );
129
130 vlc_module_begin ()
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 )
136 vlc_module_end ()
137
138
139 /*****************************************************************************
140  * Local prototypes
141  *****************************************************************************/
142
143 #ifdef WIN32
144 static void * load_syms(decoder_t *p_dec, const char *path) 
145 {
146     void *handle;
147
148     msg_Dbg( p_dec, "opening win32 dll '%s'", path);
149 #ifdef LOADER
150     Setup_LDT_Keeper();
151 #endif
152     handle = LoadLibraryA(path);
153     msg_Dbg( p_dec, "win32 real codec handle=%p",handle);
154     if (!handle)
155     {
156         msg_Err( p_dec, "Error loading dll");
157         return NULL;
158     }
159
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");
164
165     if (wrvyuv_custom_message && wrvyuv_free && wrvyuv_init && wrvyuv_transform)
166     {
167         dll_type = 1;
168         return handle;
169     }
170     msg_Err( p_dec, "Error resolving symbols! (version incompatibility?)");
171     FreeLibrary(handle);
172     return NULL; // error
173 }
174 #else
175 static void * load_syms_linux(decoder_t *p_dec, const char *path) 
176 {
177     void *handle;
178
179     msg_Dbg( p_dec, "opening shared obj '%s'", path);
180
181     handle = dlopen (path, RTLD_LAZY);
182     if (!handle) 
183     {
184         msg_Err( p_dec,"Error: %s",dlerror());
185         return NULL;
186     }
187
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");
192
193     if(rvyuv_custom_message && rvyuv_free && rvyuv_init && rvyuv_transform)
194     {
195         dll_type = 0;
196         return handle;
197     }
198
199     msg_Err( p_dec,"Error resolving symbols! (version incompatibility?)");
200     dlclose(handle);
201     return 0;
202 }
203 #endif
204
205 static vlc_mutex_t rm_mutex = VLC_STATIC_MUTEX;
206
207 static int InitVideo(decoder_t *p_dec)
208 {
209     int result;
210     struct rv_init_t init_data;
211     char fcc[4];
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     vlc_mutex_lock( &rm_mutex );
313
314     p_sys->handle=NULL;
315     #ifdef WIN32
316     if (dll_type == 1)
317         result=(*wrvyuv_init)(&init_data, &p_sys->handle);
318     else
319     #endif
320         result=(*rvyuv_init)(&init_data, &p_sys->handle);
321     if (result)
322     {
323         msg_Err( p_dec, "Cannot Init real decoder library: %s",  g_decode_path);
324         free( p_sys );
325         return VLC_EGENERIC;
326     }
327
328     /* setup rv30 codec (codec sub-type and image dimensions): */
329     /*if ( p_dec->fmt_in.i_codec == VLC_CODEC_RV30 )*/
330     if (p_vide[1]>=0x20200002)
331     {
332         int i, cmsg_cnt;
333         uint32_t cmsg24[16]={p_dec->fmt_in.video.i_width,p_dec->fmt_in.video.i_height};
334         cmsg_data_t cmsg_data={0x24,1+(p_vide[1]&7), &cmsg24[0]};
335         cmsg_cnt = (p_vide[1]&7)*2;
336         if (i_vide - 8 < cmsg_cnt) {
337                     cmsg_cnt = i_vide - 8;
338         }
339         for (i = 0; i < cmsg_cnt; i++)
340             cmsg24[2+i] = p_vide[8+i]*4;
341         #ifdef WIN32
342         if (dll_type == 1)
343             (*wrvyuv_custom_message)(&cmsg_data,p_sys->handle);
344         else
345         #endif
346             (*rvyuv_custom_message)(&cmsg_data,p_sys->handle);
347     }
348     /*
349     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YV12);
350     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_YUYV);
351      */
352     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_I420);
353      
354     p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
355     p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
356     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
357     p_sys->inited = 0;
358
359     vlc_mutex_unlock( &rm_mutex );
360     return VLC_SUCCESS;
361 }
362
363 /*****************************************************************************
364  * Open: probe the decoder and return score
365  *****************************************************************************
366  * Tries to launch a decoder and return score so that the interface is able
367  * to choose.
368  *****************************************************************************/
369 static int Open( vlc_object_t *p_this )
370 {
371     decoder_t *p_dec = (decoder_t*)p_this;
372
373     /* create a mutex */
374     var_Create( p_this->p_libvlc, "rm_mutex", VLC_VAR_MUTEX );
375
376     switch ( p_dec->fmt_in.i_codec )
377     {
378     case VLC_CODEC_RV10: 
379     case VLC_CODEC_RV20: 
380     case VLC_CODEC_RV30:
381     case VLC_CODEC_RV40: 
382         p_dec->p_sys = NULL;
383         p_dec->pf_decode_video = DecodeVideo;
384         return InitVideo(p_dec);
385
386     default:
387         return VLC_EGENERIC;
388     }
389 }
390
391 /*****************************************************************************
392  * Close:
393  *****************************************************************************/
394 static void Close( vlc_object_t *p_this )
395 {
396     decoder_t     *p_dec = (decoder_t*)p_this;
397     decoder_sys_t *p_sys = p_dec->p_sys;
398
399     /* get lock, avoid segfault */
400     vlc_mutex_lock( &rm_mutex );
401
402     #ifdef WIN32
403     if (dll_type == 1)
404     {
405         if (wrvyuv_free)
406             wrvyuv_free(p_sys->handle);
407     }
408     else
409     #endif
410         if (rvyuv_free)
411             rvyuv_free(p_sys->handle);
412 #ifdef WIN32
413     if (dll_type == 1)
414     {
415         if (p_sys->rv_handle)
416             FreeLibrary(p_sys->rv_handle);
417     }
418     else
419 #endif
420         p_sys->rv_handle=NULL;
421
422     if (p_sys->plane)
423     {
424         free(p_sys->plane);
425         p_sys->plane = NULL;
426     }
427
428     msg_Dbg( p_dec, "FreeLibrary ok." );
429 #ifdef LOADER
430     Restore_LDT_Keeper( p_sys->ldt_fs );
431     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
432 #endif
433     p_sys->inited = 0;
434
435     vlc_mutex_unlock( &rm_mutex );
436
437     free( p_sys );
438 }
439
440 /*****************************************************************************
441  * DecodeVideo:
442  *****************************************************************************/
443 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
444 {
445     decoder_sys_t *p_sys = p_dec->p_sys;
446     block_t       *p_block;
447     picture_t     *p_pic;
448     mtime_t       i_pts;
449     int           result;
450
451     /* We must do open and close in the same thread (unless we do
452      * Setup_LDT_Keeper in the main thread before all others */
453     if ( pp_block == NULL || *pp_block == NULL )
454     {
455         return NULL;
456     }
457     p_block = *pp_block;
458     *pp_block = NULL;
459
460     i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
461
462     vlc_mutex_lock( &rm_mutex );
463
464     p_pic = decoder_NewPicture( p_dec );
465
466     if ( p_pic )
467     {
468         unsigned int transform_out[5];
469         dp_hdr_t dp_hdr;
470         transform_in_t transform_in;
471         uint32_t pkg_len = ((uint32_t*)p_block->p_buffer)[0];
472         unsigned char* dp_data=((unsigned char*)p_block->p_buffer)+8;
473         uint32_t* extra=(uint32_t*)(((char*)p_block->p_buffer)+8+pkg_len);
474         uint32_t img_size;
475
476
477         dp_hdr.len = pkg_len;
478         dp_hdr.chunktab = 8 + pkg_len;
479         dp_hdr.chunks = ((uint32_t*)p_block->p_buffer)[1]-1;
480         dp_hdr.timestamp = i_pts;
481
482         memset(&transform_in, 0, sizeof(transform_in_t));
483
484         transform_in.len = dp_hdr.len;
485         transform_in.extra = extra;
486         transform_in.chunks = dp_hdr.chunks;
487         transform_in.timestamp = dp_hdr.timestamp;
488
489         memset (p_sys->plane, 0, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height *3/2 );
490
491         #ifdef WIN32
492         if (dll_type == 1)
493             result=(*wrvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
494         else
495         #endif
496             result=(*rvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
497
498         /* msg_Warn(p_dec, "Real Size %d X %d", transform_out[3],
499                  transform_out[4]); */
500         /* some bug rm file will print the messages :
501                 [00000551] realvideo decoder warning: Real Size 320 X 240
502                 [00000551] realvideo decoder warning: Real Size 480 X 272
503                 [00000551] realvideo decoder warning: Real Size 320 X 240
504                 [00000551] realvideo decoder warning: Real Size 320 X 240
505                 ...
506             so it needs fixing!
507         */
508         if ( p_sys->inited == 0 )
509         {
510             /* fix and get the correct image size! */
511             if ( p_dec->fmt_in.video.i_width != transform_out[3]
512              || p_dec->fmt_in.video.i_height  != transform_out[4] )
513             {
514                 msg_Warn(p_dec, "Warning, Real's Header give a wrong "
515                          "information about media's width and height!"
516                          "\tRealHeader: \t %d X %d  \t %d X %d",
517                          p_dec->fmt_in.video.i_width,
518                          p_dec->fmt_in.video.i_height,
519                          transform_out[3],transform_out[4]);
520                 
521                 if ( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height >= transform_out[3] * transform_out[4] )
522                 {
523                     p_dec->fmt_out.video.i_width = 
524                     p_dec->fmt_out.video.i_visible_width = 
525                     p_dec->fmt_in.video.i_width = transform_out[3] ;
526
527                     p_dec->fmt_out.video.i_height= 
528                     p_dec->fmt_out.video.i_visible_height = 
529                     p_dec->fmt_in.video.i_height= transform_out[4];
530
531                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
532                 }
533                 else
534                 {
535                     // TODO: realloc plane's size! but [in fact] it maybe not happen! 
536                     msg_Err(p_dec,"plane space not enough ,skip");
537                 }
538             }
539             p_sys->inited = 1;
540         }
541
542         img_size = p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height;
543         memcpy( p_pic->p[0].p_pixels, p_sys->plane,  img_size);
544         memcpy( p_pic->p[1].p_pixels, p_sys->plane + img_size, img_size/4);
545         memcpy( p_pic->p[2].p_pixels, p_sys->plane + img_size * 5/4, img_size/4);
546         p_pic->date = i_pts ;
547
548         /*  real video frame is small( frame and frame's time-shift is short), 
549             so it will become late picture easier (when render-time changed)and
550             droped by video-output.*/
551         p_pic->b_force = 1;
552     }
553
554     vlc_mutex_unlock( &rm_mutex );
555
556     block_Release( p_block );
557     return p_pic;
558 }
559