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