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