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