]> git.sesse.net Git - vlc/blob - modules/codec/realaudio.c
b68cffc9bda378ab47641a7b3b9ad1184d6e68cb
[vlc] / modules / codec / realaudio.c
1 /*****************************************************************************
2  * realaudio.c: a realaudio decoder that uses the realaudio library/dll
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
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_aout.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 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 vlc_module_begin ()
65     set_description( N_("RealAudio library decoder") )
66     set_capability( "decoder", 10 )
67     set_category( CAT_INPUT )
68     set_subcategory( SUBCAT_INPUT_VCODEC )
69     set_callbacks( Open, Close )
70 vlc_module_end ()
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int  OpenDll( decoder_t * );
76 #ifndef WIN32
77 static int  OpenNativeDll( decoder_t *, char *, char * );
78 #endif
79 static int  OpenWin32Dll( decoder_t *, char *, char * );
80 static void CloseDll( decoder_t * );
81
82 static aout_buffer_t *Decode( decoder_t *, block_t ** );
83
84 struct decoder_sys_t
85 {
86     date_t end_date;
87
88     /* Output buffer */
89     char *p_out;
90     unsigned int i_out;
91
92     /* Codec params */
93     void *context;
94     short int i_codec_flavor;
95
96     void *dll;
97     unsigned long (*raCloseCodec)(void*);
98     unsigned long (*raDecode)(void*, char*, unsigned long, char*,
99                               unsigned int*, long);
100     unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
101     unsigned long (*raFreeDecoder)(void*);
102     void*         (*raGetFlavorProperty)(void*, unsigned long,
103                                          unsigned long, int*);
104     unsigned long (*raInitDecoder)(void*, void*);
105     unsigned long (*raOpenCodec)(void*);
106     unsigned long (*raOpenCodec2)(void*, void*);
107     unsigned long (*raSetFlavor)(void*, unsigned long);
108     void          (*raSetDLLAccessPath)(char*);
109     void          (*raSetPwd)(char*, char*);
110
111 #if defined(LOADER)
112     ldt_fs_t *ldt_fs;
113 #endif
114
115     void *win32_dll;
116     unsigned long (WINAPI *wraCloseCodec)(void*);
117     unsigned long (WINAPI *wraDecode)(void*, char*, unsigned long, char*,
118                                       unsigned int*, long);
119     unsigned long (WINAPI *wraFlush)(unsigned long, unsigned long,
120                                      unsigned long);
121     unsigned long (WINAPI *wraFreeDecoder)(void*);
122     void*         (WINAPI *wraGetFlavorProperty)(void*, unsigned long,
123                                                  unsigned long, int*);
124     unsigned long (WINAPI *wraInitDecoder)(void*, void*);
125     unsigned long (WINAPI *wraOpenCodec)(void*);
126     unsigned long (WINAPI *wraOpenCodec2)(void*, void*);
127     unsigned long (WINAPI *wraSetFlavor)(void*, unsigned long);
128     void          (WINAPI *wraSetDLLAccessPath)(char*);
129     void          (WINAPI *wraSetPwd)(char*, char*);
130 };
131
132 /* linux dlls doesn't need packing */
133 typedef struct /*__attribute__((__packed__))*/ {
134     int samplerate;
135     short bits;
136     short channels;
137     short quality;
138     /* 2bytes padding here, by gcc */
139     int bits_per_frame;
140     int packetsize;
141     int extradata_len;
142     void* extradata;
143 } ra_init_t;
144
145 /* windows dlls need packed structs (no padding) */
146 typedef struct __attribute__((__packed__)) {
147     int samplerate;
148     short bits;
149     short channels;
150     short quality;
151     int bits_per_frame;
152     int packetsize;
153     int extradata_len;
154     void* extradata;
155 } wra_init_t;
156
157 void *__builtin_new(unsigned long size) {return malloc(size);}
158 void __builtin_delete(void *p) {free(p);}
159
160 static const int pi_channels_maps[7] =
161 {
162     0,
163     AOUT_CHAN_CENTER,
164     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
165     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
166     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
167      | AOUT_CHAN_REARRIGHT,
168     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
169      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
170     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
171      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
172 };
173
174 /*****************************************************************************
175  * Open: probe the decoder and return score
176  *****************************************************************************
177  * Tries to launch a decoder and return score so that the interface is able
178  * to choose.
179  *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
181 {
182     decoder_t *p_dec = (decoder_t*)p_this;
183     decoder_sys_t *p_sys = p_dec->p_sys;
184
185     switch( p_dec->fmt_in.i_codec )
186     {
187     case VLC_CODEC_COOK:
188     case VLC_CODEC_ATRAC3:
189     case VLC_FOURCC('s','i','p','r'):
190         break;
191
192     default:
193         return VLC_EGENERIC;
194     }
195
196     /* Channel detection */
197     if( p_dec->fmt_in.audio.i_channels <= 0 ||
198         p_dec->fmt_in.audio.i_channels > 6 )
199     {
200         msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
201                  p_dec->fmt_in.audio.i_channels );
202         return VLC_EGENERIC;
203     }
204
205     p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
206     if( !p_sys )
207         return VLC_ENOMEM;
208
209     /* Flavor for SIPR codecs */
210     p_sys->i_codec_flavor = -1;
211     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','i','p','r') )
212     {
213         p_sys->i_codec_flavor = p_dec->fmt_in.audio.i_flavor;
214         msg_Dbg( p_dec, "Got sipr flavor %d", p_sys->i_codec_flavor );
215     }
216
217     if( OpenDll( p_dec ) != VLC_SUCCESS )
218     {
219         free( p_sys );
220         return VLC_EGENERIC;
221     }
222
223 #ifdef LOADER
224     if( p_sys->win32_dll ) Close( p_this );
225 #endif
226
227     es_format_Init( &p_dec->fmt_out, AUDIO_ES, VLC_CODEC_S16N );
228     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
229     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
230     p_dec->fmt_out.audio.i_bitspersample =
231         p_dec->fmt_in.audio.i_bitspersample;
232     p_dec->fmt_out.audio.i_physical_channels =
233     p_dec->fmt_out.audio.i_original_channels =
234         pi_channels_maps[p_dec->fmt_out.audio.i_channels];
235
236     date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
237     date_Set( &p_sys->end_date, 0 );
238
239     p_dec->pf_decode_audio = Decode;
240
241     p_sys->p_out = malloc( 4096 * 10 );
242     if( !p_sys->p_out )
243     {
244         free( p_sys );
245         return VLC_ENOMEM;
246     }
247     p_sys->i_out = 0;
248
249     return VLC_SUCCESS;
250 }
251
252 /*****************************************************************************
253  * Close:
254  *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
256 {
257     decoder_t *p_dec = (decoder_t*)p_this;
258
259     CloseDll( p_dec );
260     free( p_dec->p_sys->p_out );
261     free( p_dec->p_sys );
262 }
263
264 /*****************************************************************************
265  * OpenDll:
266  *****************************************************************************/
267 static int OpenDll( decoder_t *p_dec )
268 {
269     char *psz_dll;
270     int i, i_result;
271
272     /** Find the good path for the dlls.**/
273     char *ppsz_path[] =
274     {
275       ".",
276 #ifndef WIN32
277       "/usr/local/RealPlayer8/Codecs",
278       "/usr/RealPlayer8/Codecs",
279       "/usr/lib/RealPlayer8/Codecs",
280       "/opt/RealPlayer8/Codecs",
281       "/usr/lib/RealPlayer9/users/Real/Codecs",
282       "/usr/lib/RealPlayer10/codecs",
283       "/usr/lib/RealPlayer10GOLD/codecs",
284       "/usr/lib/helix/player/codecs",
285       "/usr/lib64/RealPlayer8/Codecs",
286       "/usr/lib64/RealPlayer9/users/Real/Codecs",
287       "/usr/lib64/RealPlayer10/codecs",
288       "/usr/lib64/RealPlayer10GOLD/codecs",
289       "/usr/lib/win32",
290       "/usr/lib/codecs",
291       "/usr/local/lib/codecs",
292 #endif
293       NULL,
294       NULL,
295       NULL
296     };
297
298 #ifdef WIN32
299     char psz_win32_real_codecs[MAX_PATH + 1];
300     char psz_win32_helix_codecs[MAX_PATH + 1];
301
302     {
303         HKEY h_key;
304         DWORD i_type, i_data = MAX_PATH + 1, i_index = 1;
305         char *p_data;
306
307         p_data = psz_win32_real_codecs;
308         if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
309                           _T("Software\\RealNetworks\\Preferences\\DT_Codecs"),
310                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
311         {
312              if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
313                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
314                  i_type == REG_SZ )
315              {
316                  int i_len = strlen( p_data );
317                  if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
318                  ppsz_path[i_index++] = p_data;
319                  msg_Err( p_dec, "Real: %s", p_data );
320              }
321              RegCloseKey( h_key );
322         }
323
324         p_data = psz_win32_helix_codecs;
325         if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
326                           _T("Helix\\HelixSDK\\10.0\\Preferences\\DT_Codecs"),
327                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
328         {
329              if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
330                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
331                  i_type == REG_SZ )
332              {
333                  int i_len = strlen( p_data );
334                  if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
335                  ppsz_path[i_index++] = p_data;
336                  msg_Err( p_dec, "Real: %s", p_data );
337              }
338              RegCloseKey( h_key );
339         }
340     }
341 #endif
342
343
344     /** Try the native libraries first **/
345 #ifndef WIN32
346     for( i = 0; ppsz_path[i]; i++ )
347     {
348         /* Old format */
349         if( asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
350                   (char *)&p_dec->fmt_in.i_codec ) != -1 )
351         {
352             i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
353             free( psz_dll );
354             if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
355         }
356
357         /* New format */
358         if( asprintf( &psz_dll, "%s/%4.4s.so", ppsz_path[i],
359                   (char *)&p_dec->fmt_in.i_codec ) != -1 )
360         {
361             i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
362             free( psz_dll );
363             if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
364         }
365     }
366 #endif
367
368     /** Or use the WIN32 dlls **/
369 #if defined(LOADER) || defined(WIN32)
370     for( i = 0; ppsz_path[i]; i++ )
371     {
372         /* New format */
373         if( asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
374                   (char *)&p_dec->fmt_in.i_codec ) != -1 )
375         {
376             i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
377             free( psz_dll );
378             if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
379         }
380
381         /* Old format */
382         if( asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
383                   (char *)&p_dec->fmt_in.i_codec ) != -1 )
384         {
385             i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
386             free( psz_dll );
387             if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
388         }
389     }
390 #endif
391
392     return VLC_EGENERIC;
393 }
394
395 #ifndef WIN32
396 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
397 {
398 #if defined(HAVE_DL_DLOPEN)
399     decoder_sys_t *p_sys = p_dec->p_sys;
400     void *handle = 0, *context = 0;
401     unsigned int i_result;
402     void *p_prop;
403     int i_prop;
404
405     ra_init_t init_data =
406     {
407         p_dec->fmt_in.audio.i_rate,
408         p_dec->fmt_in.audio.i_bitspersample,
409         p_dec->fmt_in.audio.i_channels,
410         100, /* quality */
411         p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
412         p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
413         p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
414     };
415
416     msg_Dbg( p_dec, "opening library '%s'", psz_dll );
417     if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
418     {
419         msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
420                  psz_dll, dlerror() );
421         return VLC_EGENERIC;
422     }
423
424     p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
425     p_sys->raDecode = dlsym( handle, "RADecode" );
426     p_sys->raFlush = dlsym( handle, "RAFlush" );
427     p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
428     p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
429     p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
430     p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
431     p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
432     p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
433     p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
434     p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
435
436     if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
437         !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
438         !p_sys->raDecode || !p_sys->raFreeDecoder ||
439         !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
440         /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
441     {
442         goto error_native;
443     }
444
445     if( p_sys->raOpenCodec2 )
446         i_result = p_sys->raOpenCodec2( &context, psz_path );
447     else
448         i_result = p_sys->raOpenCodec( &context );
449
450     if( i_result )
451     {
452         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
453         goto error_native;
454     }
455
456     i_result = p_sys->raInitDecoder( context, &init_data );
457     if( i_result )
458     {
459         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
460         goto error_native;
461     }
462
463     if( p_sys->i_codec_flavor >= 0 )
464     {
465         i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
466         if( i_result )
467         {
468             msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
469                      i_result );
470             goto error_native;
471         }
472
473         p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
474                                              0, &i_prop );
475         msg_Dbg( p_dec, "audio codec: [%d] %s",
476                  p_sys->i_codec_flavor, (char *)p_prop );
477
478         p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
479                                              1, &i_prop );
480         if( p_prop )
481         {
482             int i_bps = ((*((int*)p_prop))+4)/8;
483             msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
484                      (*((int*)p_prop))*0.001f, i_bps );
485         }
486     }
487
488     p_sys->context = context;
489     p_sys->dll = handle;
490     return VLC_SUCCESS;
491
492  error_native:
493     if( context ) p_sys->raFreeDecoder( context );
494     if( context ) p_sys->raCloseCodec( context );
495     dlclose( handle );
496 #else
497     VLC_UNUSED( p_dec );
498     VLC_UNUSED( psz_path);
499     VLC_UNUSED( psz_dll );
500 #endif
501
502     return VLC_EGENERIC;
503 }
504 #endif /* Win32 */
505
506 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
507 {
508 #if defined(LOADER) || defined(WIN32)
509     decoder_sys_t *p_sys = p_dec->p_sys;
510     void *handle = 0, *context = 0;
511     unsigned int i_result;
512     void *p_prop;
513     int i_prop;
514
515     wra_init_t init_data =
516     {
517         p_dec->fmt_in.audio.i_rate,
518         p_dec->fmt_in.audio.i_bitspersample,
519         p_dec->fmt_in.audio.i_channels,
520         100, /* quality */
521         p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
522         p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
523         p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
524     };
525
526     msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
527
528 #ifdef LOADER
529     Setup_LDT_Keeper();
530 #endif
531
532     if( !(handle = LoadLibraryA( psz_dll )) )
533     {
534         msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
535         return VLC_EGENERIC;
536     }
537
538     p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
539     p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
540     p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
541     p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
542     p_sys->wraGetFlavorProperty =
543         GetProcAddress( handle, "RAGetFlavorProperty" );
544     p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
545     p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
546     p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
547     p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
548     p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
549     p_sys->wraSetPwd =
550         GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
551
552     if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
553         !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
554         !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
555         !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
556         /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
557     {
558         FreeLibrary( handle );
559         return VLC_EGENERIC;
560     }
561
562     if( p_sys->wraOpenCodec2 )
563         i_result = p_sys->wraOpenCodec2( &context, psz_path );
564     else
565         i_result = p_sys->wraOpenCodec( &context );
566
567     if( i_result )
568     {
569         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
570         goto error_win32;
571     }
572
573     i_result = p_sys->wraInitDecoder( context, &init_data );
574     if( i_result )
575     {
576         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
577         goto error_win32;
578     }
579
580     if( p_sys->i_codec_flavor >= 0 )
581     {
582         i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
583         if( i_result )
584         {
585             msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
586                      i_result );
587             goto error_win32;
588         }
589
590         p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
591                                               0, &i_prop );
592         msg_Dbg( p_dec, "audio codec: [%d] %s",
593                  p_sys->i_codec_flavor, (char *)p_prop );
594
595         p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
596                                               1, &i_prop );
597         if( p_prop )
598         {
599             int i_bps = ((*((int*)p_prop))+4)/8;
600             msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
601                      (*((int*)p_prop))*0.001f, i_bps );
602         }
603     }
604
605     p_sys->context = context;
606     p_sys->win32_dll = handle;
607     return VLC_SUCCESS;
608
609  error_win32:
610     if( context ) p_sys->wraFreeDecoder( context );
611     if( context ) p_sys->wraCloseCodec( context );
612     FreeLibrary( handle );
613 #endif
614
615     return VLC_EGENERIC;
616 }
617
618 /*****************************************************************************
619  * CloseDll:
620  *****************************************************************************/
621 static void CloseDll( decoder_t *p_dec )
622 {
623     decoder_sys_t *p_sys = p_dec->p_sys;
624
625     if( p_sys->context && p_sys->dll )
626     {
627         p_sys->raFreeDecoder( p_sys->context );
628         p_sys->raCloseCodec( p_sys->context );
629     }
630
631     if( p_sys->context && p_sys->win32_dll )
632     {
633         p_sys->wraFreeDecoder( p_sys->context );
634         p_sys->wraCloseCodec( p_sys->context );
635     }
636
637 #if defined(HAVE_DL_DLOPEN)
638     if( p_sys->dll ) dlclose( p_sys->dll );
639 #endif
640
641 #if defined(LOADER) || defined(WIN32)
642     if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
643
644 #if 0 //def LOADER /* Segfaults */
645     Restore_LDT_Keeper( p_sys->ldt_fs );
646     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
647 #endif
648 #endif
649
650     p_sys->dll = 0;
651     p_sys->win32_dll = 0;
652     p_sys->context = 0;
653 }
654
655 /*****************************************************************************
656  * DecodeAudio:
657  *****************************************************************************/
658 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
659 {
660     decoder_sys_t *p_sys = p_dec->p_sys;
661     aout_buffer_t *p_aout_buffer = 0;
662     unsigned int i_result;
663     int i_samples;
664     block_t *p_block;
665
666 #ifdef LOADER
667     if( !p_sys->win32_dll && !p_sys->dll )
668     {
669         /* We must do open and close in the same thread (unless we do
670          * Setup_LDT_Keeper in the main thread before all others */
671         if( OpenDll( p_dec ) != VLC_SUCCESS )
672         {
673             /* Fatal */
674             p_dec->b_error = true;
675             return NULL;
676         }
677     }
678 #endif
679
680     if( pp_block == NULL || *pp_block == NULL ) return NULL;
681     p_block = *pp_block;
682
683     if( p_sys->dll )
684         i_result = p_sys->raDecode( p_sys->context, (char *)p_block->p_buffer,
685                                     (unsigned long)p_block->i_buffer,
686                                     p_sys->p_out, &p_sys->i_out, -1 );
687     else
688         i_result = p_sys->wraDecode( p_sys->context, (char *)p_block->p_buffer,
689                                      (unsigned long)p_block->i_buffer,
690                                      p_sys->p_out, &p_sys->i_out, -1 );
691
692 #if 0
693     msg_Err( p_dec, "decoded: %i samples (%i)",
694              p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
695              p_dec->fmt_out.audio.i_channels, i_result );
696 #endif
697
698     /* Date management */
699     if( p_block->i_pts > 0 &&
700         p_block->i_pts != date_Get( &p_sys->end_date ) )
701     {
702         date_Set( &p_sys->end_date, p_block->i_pts );
703     }
704
705     if( !date_Get( &p_sys->end_date ) )
706     {
707         /* We've just started the stream, wait for the first PTS. */
708         if( p_block ) block_Release( p_block );
709         return NULL;
710     }
711
712     i_samples = p_sys->i_out * 8 /
713         p_dec->fmt_out.audio.i_bitspersample /p_dec->fmt_out.audio.i_channels;
714
715     p_aout_buffer =
716         decoder_NewAudioBuffer( p_dec, i_samples );
717     if( p_aout_buffer )
718     {
719         memcpy( p_aout_buffer->p_buffer, p_sys->p_out, p_sys->i_out );
720
721         /* Date management */
722         p_aout_buffer->start_date = date_Get( &p_sys->end_date );
723         p_aout_buffer->end_date =
724             date_Increment( &p_sys->end_date, i_samples );
725     }
726
727     block_Release( p_block );
728     *pp_block = 0;
729     return p_aout_buffer;
730 }