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