]> git.sesse.net Git - vlc/blob - modules/codec/realaudio.c
ea44141ce09546139da990e5a60227022d1e9efb
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
20  *****************************************************************************/
21
22 /*****************************************************************************
23  * Preamble
24  *****************************************************************************/
25 #include <vlc/vlc.h>
26 #include <vlc/aout.h>
27 #include <vlc/vout.h>
28 #include <vlc/decoder.h>
29
30 #ifdef LOADER
31 /* Need the w32dll loader from mplayer */
32 #   include <wine/winerror.h>
33 #   include <ldt_keeper.h>
34 #   include <wine/windef.h>
35
36 void *WINAPI LoadLibraryA( char *name );
37 void *WINAPI GetProcAddress( void *handle, char *func );
38 int WINAPI FreeLibrary( void *handle );
39 #endif
40
41 #ifndef WINAPI
42 #   define WINAPI
43 #endif
44
45 #if defined(HAVE_DL_DLOPEN)
46 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
47 #       include <dlfcn.h>
48 #   endif
49 #   if defined(HAVE_SYS_DL_H)
50 #       include <sys/dl.h>
51 #   endif
52 #endif
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 vlc_module_begin();
61     set_description( _("RealAudio library decoder") );
62     set_capability( "decoder", 10 );
63     set_category( CAT_INPUT );
64     set_subcategory( SUBCAT_INPUT_VCODEC );
65     set_callbacks( Open, Close );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static int  OpenDll( decoder_t * );
72 static int  OpenNativeDll( decoder_t *, char *, char * );
73 static int  OpenWin32Dll( decoder_t *, char *, char * );
74 static void CloseDll( decoder_t * );
75
76 static aout_buffer_t *Decode( decoder_t *, block_t ** );
77
78 struct decoder_sys_t
79 {
80     audio_date_t end_date;
81
82     /* Frame buffer for data reordering */
83     int i_subpacket;
84     mtime_t i_packet_pts;
85     int i_frame_size;
86     char *p_frame;
87     int i_frame;
88
89     /* Output buffer */
90     char *p_out_buffer;
91     char *p_out;
92     unsigned int i_out;
93
94     /* Codec params */
95     void *context;
96     int i_subpacket_size;
97     int i_subpacket_h;
98     int i_codec_flavor;
99     int i_coded_frame_size;
100     int i_extra;
101     uint8_t *p_extra;
102
103     void *dll;
104     unsigned long (*raCloseCodec)(void*);
105     unsigned long (*raDecode)(void*, char*, unsigned long, char*,
106                               unsigned int*, long);
107     unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
108     unsigned long (*raFreeDecoder)(void*);
109     void*         (*raGetFlavorProperty)(void*, unsigned long,
110                                          unsigned long, int*);
111     unsigned long (*raInitDecoder)(void*, void*);
112     unsigned long (*raOpenCodec)(void*);
113     unsigned long (*raOpenCodec2)(void*, void*);
114     unsigned long (*raSetFlavor)(void*, unsigned long);
115     void          (*raSetDLLAccessPath)(char*);
116     void          (*raSetPwd)(char*, char*);
117
118 #if defined(LOADER)
119     ldt_fs_t *ldt_fs;
120 #endif
121
122     void *win32_dll;
123     unsigned long WINAPI (*wraCloseCodec)(void*);
124     unsigned long WINAPI (*wraDecode)(void*, char*, unsigned long, char*,
125                                       unsigned int*, long);
126     unsigned long WINAPI (*wraFlush)(unsigned long, unsigned long,
127                                      unsigned long);
128     unsigned long WINAPI (*wraFreeDecoder)(void*);
129     void*         WINAPI (*wraGetFlavorProperty)(void*, unsigned long,
130                                                  unsigned long, int*);
131     unsigned long WINAPI (*wraInitDecoder)(void*, void*);
132     unsigned long WINAPI (*wraOpenCodec)(void*);
133     unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
134     unsigned long WINAPI (*wraSetFlavor)(void*, unsigned long);
135     void          WINAPI (*wraSetDLLAccessPath)(char*);
136     void          WINAPI (*wraSetPwd)(char*, char*);
137 };
138
139 /* linux dlls doesn't need packing */
140 typedef struct /*__attribute__((__packed__))*/ {
141     int samplerate;
142     short bits;
143     short channels;
144     short quality;
145     /* 2bytes padding here, by gcc */
146     int bits_per_frame;
147     int packetsize;
148     int extradata_len;
149     void* extradata;
150 } ra_init_t;
151
152 /* windows dlls need packed structs (no padding) */
153 typedef struct __attribute__((__packed__)) {
154     int samplerate;
155     short bits;
156     short channels;
157     short quality;
158     int bits_per_frame;
159     int packetsize;
160     int extradata_len;
161     void* extradata;
162 } wra_init_t;
163
164 void *__builtin_new(unsigned long size) {return malloc(size);}
165 void __builtin_delete(void *p) {free(p);}
166
167 static int pi_channels_maps[7] =
168 {
169     0,
170     AOUT_CHAN_CENTER,
171     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
172     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
173     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
174      | AOUT_CHAN_REARRIGHT,
175     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
176      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
177     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
178      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
179 };
180
181 /*****************************************************************************
182  * Open: probe the decoder and return score
183  *****************************************************************************
184  * Tries to launch a decoder and return score so that the interface is able
185  * to choose.
186  *****************************************************************************/
187 static int Open( vlc_object_t *p_this )
188 {
189     decoder_t *p_dec = (decoder_t*)p_this;
190     decoder_sys_t *p_sys = p_dec->p_sys;
191
192     switch( p_dec->fmt_in.i_codec )
193     {
194     case VLC_FOURCC('c','o','o','k'):
195         break;
196
197     default:
198         return VLC_EGENERIC;
199     }
200
201     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
202     memset( p_sys, 0, sizeof(decoder_sys_t) );
203
204     if( p_dec->fmt_in.i_extra >= 10 )
205     {
206         p_sys->i_subpacket_size = ((short *)(p_dec->fmt_in.p_extra))[0];
207         p_sys->i_subpacket_h = ((short *)(p_dec->fmt_in.p_extra))[1];
208         p_sys->i_codec_flavor = ((short *)(p_dec->fmt_in.p_extra))[2];
209         p_sys->i_coded_frame_size = ((short *)(p_dec->fmt_in.p_extra))[3];
210         p_sys->i_extra = ((short*)(p_dec->fmt_in.p_extra))[4];
211         p_sys->p_extra = p_dec->fmt_in.p_extra + 10;
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->i_frame_size =
239         p_dec->fmt_in.audio.i_blockalign * p_sys->i_subpacket_h;
240     p_sys->p_frame = malloc( p_sys->i_frame_size );
241     p_sys->i_packet_pts = 0;
242     p_sys->i_subpacket = 0;
243     p_sys->i_frame = 0;
244
245     p_sys->p_out_buffer = malloc( 4096 * 10 );
246     p_sys->p_out = 0;
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     if( p_dec->p_sys->p_frame ) free( p_dec->p_sys->p_frame );
261     if( p_dec->p_sys->p_out_buffer ) free( p_dec->p_sys->p_out_buffer );
262     free( p_dec->p_sys );
263 }
264
265 /*****************************************************************************
266  * OpenDll:
267  *****************************************************************************/
268 static int OpenDll( decoder_t *p_dec )
269 {
270     char *psz_dll;
271     int i, i_result;
272
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/lib64/RealPlayer8/Codecs",
283       "/usr/lib64/RealPlayer9/users/Real/Codecs",
284       "/usr/lib/win32",
285 #endif
286       NULL,
287       NULL,
288       NULL
289     };
290
291 #ifdef WIN32
292     char psz_win32_real_codecs[MAX_PATH + 1];
293     char psz_win32_helix_codecs[MAX_PATH + 1];
294 #endif
295
296     for( i = 0; ppsz_path[i]; i++ )
297     {
298         asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
299                   (char *)&p_dec->fmt_in.i_codec );
300         i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
301         free( psz_dll );
302         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
303     }
304
305 #ifdef WIN32
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     for( i = 0; ppsz_path[i]; i++ )
348     {
349         /* New format */
350         asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
351                   (char *)&p_dec->fmt_in.i_codec );
352         i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
353         free( psz_dll );
354         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
355
356         /* Old format */
357         asprintf( &psz_dll, "%s\\%4.4s3260.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
364     return VLC_EGENERIC;
365 }
366
367 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
368 {
369 #if defined(HAVE_DL_DLOPEN)
370     decoder_sys_t *p_sys = p_dec->p_sys;
371     void *handle = 0, *context = 0;
372     unsigned int i_result;
373     void *p_prop;
374     int i_prop;
375
376     ra_init_t init_data =
377     {
378         p_dec->fmt_in.audio.i_rate,
379         p_dec->fmt_in.audio.i_bitspersample,
380         p_dec->fmt_in.audio.i_channels,
381         100, /* quality */
382         p_sys->i_subpacket_size,
383         p_sys->i_coded_frame_size,
384         p_sys->i_extra, p_sys->p_extra
385     };
386
387     msg_Dbg( p_dec, "opening library '%s'", psz_dll );
388     if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
389     {
390         msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
391                  psz_dll, dlerror() );
392         return VLC_EGENERIC;
393     }
394
395     p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
396     p_sys->raDecode = dlsym( handle, "RADecode" );
397     p_sys->raFlush = dlsym( handle, "RAFlush" );
398     p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
399     p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
400     p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
401     p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
402     p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
403     p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
404     p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
405     p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
406
407     if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
408         !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
409         !p_sys->raDecode || !p_sys->raFreeDecoder ||
410         !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
411         /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
412     {
413         goto error_native;
414     }
415
416     if( p_sys->raOpenCodec2 )
417         i_result = p_sys->raOpenCodec2( &context, psz_path );
418     else
419         i_result = p_sys->raOpenCodec( &context );
420
421     if( i_result )
422     {
423         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
424         goto error_native;
425     }
426
427     i_result = p_sys->raInitDecoder( context, &init_data );
428     if( i_result )
429     {
430         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
431         goto error_native;
432     }
433
434     i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
435     if( i_result )
436     {
437         msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
438                  i_result );
439         goto error_native;
440     }
441
442     p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
443                                          0, &i_prop );
444     msg_Dbg( p_dec, "audio codec: [%d] %s",
445              p_sys->i_codec_flavor, (char *)p_prop );
446
447     p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
448                                          1, &i_prop );
449     if( p_prop )
450     {
451         int i_bps = ((*((int*)p_prop))+4)/8;
452         msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
453                  (*((int*)p_prop))*0.001f, i_bps );
454     }
455
456     p_sys->context = context;
457     p_sys->dll = handle;
458     return VLC_SUCCESS;
459
460  error_native:
461     if( context ) p_sys->raFreeDecoder( context );
462     if( context ) p_sys->raCloseCodec( context );
463     dlclose( handle );
464 #endif
465
466     return VLC_EGENERIC;
467 }
468
469 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
470 {
471 #if defined(LOADER) || defined(WIN32)
472     decoder_sys_t *p_sys = p_dec->p_sys;
473     void *handle = 0, *context = 0;
474     unsigned int i_result;
475     void *p_prop;
476     int i_prop;
477
478     wra_init_t init_data =
479     {
480         p_dec->fmt_in.audio.i_rate,
481         p_dec->fmt_in.audio.i_bitspersample,
482         p_dec->fmt_in.audio.i_channels,
483         100, /* quality */
484         p_sys->i_subpacket_size,
485         p_sys->i_coded_frame_size,
486         p_sys->i_extra, p_sys->p_extra
487     };
488
489     msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
490
491 #ifdef LOADER
492     Setup_LDT_Keeper();
493 #endif
494
495     if( !(handle = LoadLibraryA( psz_dll )) )
496     {
497         msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
498         return VLC_EGENERIC;
499     }
500
501     p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
502     p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
503     p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
504     p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
505     p_sys->wraGetFlavorProperty =
506         GetProcAddress( handle, "RAGetFlavorProperty" );
507     p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
508     p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
509     p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
510     p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
511     p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
512     p_sys->wraSetPwd =
513         GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
514
515     if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
516         !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
517         !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
518         !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
519         /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
520     {
521         FreeLibrary( handle );
522         return VLC_EGENERIC;
523     }
524
525     if( p_sys->wraOpenCodec2 )
526         i_result = p_sys->wraOpenCodec2( &context, psz_path );
527     else
528         i_result = p_sys->wraOpenCodec( &context );
529
530     if( i_result )
531     {
532         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
533         goto error_win32;
534     }
535
536     i_result = p_sys->wraInitDecoder( context, &init_data );
537     if( i_result )
538     {
539         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
540         goto error_win32;
541     }
542
543     i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
544     if( i_result )
545     {
546         msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
547                  i_result );
548         goto error_win32;
549     }
550
551     p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
552                                           0, &i_prop );
553     msg_Dbg( p_dec, "audio codec: [%d] %s",
554              p_sys->i_codec_flavor, (char *)p_prop );
555
556     p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
557                                           1, &i_prop );
558     if( p_prop )
559     {
560         int i_bps = ((*((int*)p_prop))+4)/8;
561         msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
562                  (*((int*)p_prop))*0.001f, i_bps );
563     }
564
565     p_sys->context = context;
566     p_sys->win32_dll = handle;
567     return VLC_SUCCESS;
568
569  error_win32:
570     if( context ) p_sys->wraFreeDecoder( context );
571     if( context ) p_sys->wraCloseCodec( context );
572     FreeLibrary( handle );
573 #endif
574
575     return VLC_EGENERIC;
576 }
577
578 /*****************************************************************************
579  * CloseDll:
580  *****************************************************************************/
581 static void CloseDll( decoder_t *p_dec )
582 {
583     decoder_sys_t *p_sys = p_dec->p_sys;
584
585     if( p_sys->context && p_sys->dll )
586     {
587         p_sys->raFreeDecoder( p_sys->context );
588         p_sys->raCloseCodec( p_sys->context );
589     }
590
591     if( p_sys->context && p_sys->win32_dll )
592     {
593         p_sys->wraFreeDecoder( p_sys->context );
594         p_sys->wraCloseCodec( p_sys->context );
595     }
596
597 #if defined(HAVE_DL_DLOPEN)
598     if( p_sys->dll ) dlclose( p_sys->dll );
599 #endif
600
601 #if defined(LOADER) || defined(WIN32)
602     if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
603
604 #if 0 //def LOADER /* Segfaults */
605     Restore_LDT_Keeper( p_sys->ldt_fs );
606     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
607 #endif
608 #endif
609
610     p_sys->dll = 0;
611     p_sys->win32_dll = 0;
612     p_sys->context = 0;
613 }
614
615 /*****************************************************************************
616  * DecodeAudio:
617  *****************************************************************************/
618 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
619 {
620     decoder_sys_t *p_sys = p_dec->p_sys;
621     aout_buffer_t *p_aout_buffer = 0;
622     block_t *p_block;
623
624 #ifdef LOADER
625     if( !p_sys->win32_dll && !p_sys->dll )
626     {
627         /* We must do open and close in the same thread (unless we do
628          * Setup_LDT_Keeper in the main thread before all others */
629         if( OpenDll( p_dec ) != VLC_SUCCESS )
630         {
631             /* Fatal */
632             p_dec->b_error = VLC_TRUE;
633             return NULL;
634         }
635     }
636 #endif
637
638     /* Output 1024 samples at a time
639      * (the audio output doesn't like big audio packets) */
640     if( p_sys->i_out )
641     {
642         int i_out;
643         int i_samples = p_sys->i_out * 8 /
644             p_dec->fmt_out.audio.i_bitspersample /
645               p_dec->fmt_out.audio.i_channels;
646
647         i_samples = __MIN( i_samples, 1024 );
648         i_out = i_samples * p_dec->fmt_out.audio.i_bitspersample *
649             p_dec->fmt_out.audio.i_channels / 8;
650
651         p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
652         if( p_aout_buffer == NULL ) return NULL;
653         memcpy( p_aout_buffer->p_buffer, p_sys->p_out, i_out );
654
655         p_sys->i_out -= i_out;
656         p_sys->p_out += i_out;
657
658         /* Date management */
659         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
660         p_aout_buffer->end_date =
661             aout_DateIncrement( &p_sys->end_date, i_samples );
662
663         return p_aout_buffer;
664     }
665
666     /* If we have a full frame ready then we can start decoding */
667     if( p_sys->i_frame )
668     {
669         unsigned int i_result;
670
671         p_sys->p_out = p_sys->p_out_buffer;
672
673         if( p_sys->dll )
674             i_result = p_sys->raDecode( p_sys->context, p_sys->p_frame +
675                                         p_sys->i_frame_size - p_sys->i_frame,
676                                         p_dec->fmt_in.audio.i_blockalign,
677                                         p_sys->p_out, &p_sys->i_out, -1 );
678         else
679             i_result = p_sys->wraDecode( p_sys->context, p_sys->p_frame +
680                                          p_sys->i_frame_size - p_sys->i_frame,
681                                          p_dec->fmt_in.audio.i_blockalign,
682                                          p_sys->p_out, &p_sys->i_out, -1 );
683
684 #if 0
685         msg_Err( p_dec, "decoded: %i samples (%i)",
686                  p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
687                  p_dec->fmt_out.audio.i_channels, i_result );
688 #endif
689
690         p_sys->i_frame -= p_dec->fmt_in.audio.i_blockalign;
691
692         return Decode( p_dec, pp_block );
693     }
694
695
696     if( pp_block == NULL || *pp_block == NULL ) return NULL;
697     p_block = *pp_block;
698
699     //msg_Err( p_dec, "Decode: "I64Fd", %i", p_block->i_pts, p_block->i_buffer );
700
701     /* Detect missing subpackets */
702     if( p_sys->i_subpacket && p_block->i_pts > 0 &&
703         p_block->i_pts != p_sys->i_packet_pts )
704     {
705         /* All subpackets in a packet should have the same pts so we must
706          * have dropped some. Clear current frame buffer. */
707         p_sys->i_subpacket = 0;
708         msg_Dbg( p_dec, "detected dropped subpackets" );
709     }
710     if( p_block->i_pts > 0 ) p_sys->i_packet_pts = p_block->i_pts;
711
712     /* Date management */
713     if( /* !p_sys->i_subpacket && */ p_block && p_block->i_pts > 0 &&
714         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
715     {
716         aout_DateSet( &p_sys->end_date, p_block->i_pts );
717     }
718
719 #if 0
720     if( !aout_DateGet( &p_sys->end_date ) )
721     {
722         /* We've just started the stream, wait for the first PTS. */
723         if( p_block ) block_Release( p_block );
724         return NULL;
725     }
726 #endif
727
728     if( p_sys->i_subpacket_size )
729     {
730         /* 'cook' way */
731         int sps = p_sys->i_subpacket_size;
732         int w = p_dec->fmt_in.audio.i_blockalign;
733         int h = p_sys->i_subpacket_h;
734         /* int cfs = p_sys->i_coded_frame_size; */
735         int x, y;
736
737         w /= sps;
738         y = p_sys->i_subpacket;
739
740         for( x = 0; x < w; x++ )
741         {
742             memcpy( p_sys->p_frame + sps * (h*x+((h+1)/2)*(y&1)+(y>>1)),
743                     p_block->p_buffer, sps );
744             p_block->p_buffer += sps;
745         }
746
747         p_sys->i_subpacket = (p_sys->i_subpacket + 1) % p_sys->i_subpacket_h;
748
749         if( !p_sys->i_subpacket )
750         {
751             /* We have a complete frame */
752             p_sys->i_frame = p_sys->i_frame_size;
753             p_aout_buffer = Decode( p_dec, pp_block );
754         }
755     }
756
757     block_Release( p_block );
758     *pp_block = 0;
759     return p_aout_buffer;
760 }