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