sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / libmpcodecs / ad_realaud.c
blob4f179e69c2458d221664edc60828689d2c37d119
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include "config.h"
25 //#include <stddef.h>
26 #ifdef HAVE_LIBDL
27 #include <dlfcn.h>
28 #endif
29 #include "help_mp.h"
31 #include "ad_internal.h"
32 #include "loader/wine/windef.h"
34 static const ad_info_t info = {
35 "RealAudio decoder",
36 "realaud",
37 "Alex Beregszaszi",
38 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
39 "binary real audio codecs"
42 LIBAD_EXTERN(realaud)
44 /* These functions are required for loading Real binary libs.
45 * Add forward declarations to avoid warnings with -Wmissing-prototypes. */
46 void *__builtin_new(unsigned long size);
47 void __builtin_delete(void *ize);
48 void *__builtin_vec_new(unsigned long size);
49 void __builtin_vec_delete(void *mem);
50 void __pure_virtual(void);
52 void *__builtin_new(unsigned long size)
54 return malloc(size);
57 void __builtin_delete(void* ize)
59 free(ize);
62 void *__builtin_vec_new(unsigned long size)
64 return malloc(size);
67 void __builtin_vec_delete(void *mem)
69 free(mem);
72 void __pure_virtual(void)
74 printf("FATAL: __pure_virtual() called!\n");
75 // exit(1);
78 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
79 void ___brk_addr(void);
80 void ___brk_addr(void) {exit(0);}
81 char **__environ={NULL};
82 #undef stderr
83 FILE *stderr=NULL;
84 void *__ctype_b=NULL;
85 #endif
87 static unsigned long (*raCloseCodec)(void*);
88 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
89 static unsigned long (*raFreeDecoder)(void*);
90 //static unsigned long (*raGetNumberOfFlavors2)(void);
91 static unsigned long (*raInitDecoder)(void*, void*);
92 static unsigned long (*raOpenCodec)(void*);
93 static unsigned long (*raOpenCodec2)(void*, void*);
94 static unsigned long (*raSetFlavor)(void*,unsigned long);
95 static void (*raSetDLLAccessPath)(char*);
96 static void (*raSetPwd)(char*,char*);
97 #ifdef CONFIG_WIN32DLL
98 static unsigned long WINAPI (*wraCloseCodec)(void*);
99 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
100 static unsigned long WINAPI (*wraFreeDecoder)(void*);
101 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
102 static unsigned long WINAPI (*wraOpenCodec)(void*);
103 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
104 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
105 static void WINAPI (*wraSetDLLAccessPath)(char*);
106 static void WINAPI (*wraSetPwd)(char*,char*);
108 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
109 #endif
111 static void *rv_handle = NULL;
113 #if 0
114 typedef struct {
115 int samplerate;
116 short bits;
117 short channels;
118 int unk1;
119 int unk2;
120 int packetsize;
121 int unk3;
122 void* unk4;
123 } ra_init_t ;
124 #else
127 Probably the linux .so-s were compiled with old GCC without setting
128 packing, so it adds 2 bytes padding after the quality field.
129 In windows it seems that there's no padding in it.
131 -- alex
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;
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 #endif
160 #ifdef HAVE_LIBDL
161 static int load_syms_linux(char *path)
163 void *handle;
165 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
166 handle = dlopen(path, RTLD_LAZY);
167 if (!handle)
169 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
170 return 0;
173 raCloseCodec = dlsym(handle, "RACloseCodec");
174 raDecode = dlsym(handle, "RADecode");
175 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
176 raOpenCodec = dlsym(handle, "RAOpenCodec");
177 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
178 raInitDecoder = dlsym(handle, "RAInitDecoder");
179 raSetFlavor = dlsym(handle, "RASetFlavor");
180 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
181 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
183 if (raCloseCodec && raDecode && raFreeDecoder &&
184 (raOpenCodec||raOpenCodec2) && raSetFlavor &&
185 /*raSetDLLAccessPath &&*/ raInitDecoder)
187 rv_handle = handle;
188 return 1;
191 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
192 dlclose(handle);
193 return 0;
195 #endif
197 #ifdef CONFIG_WIN32DLL
199 #ifdef WIN32_LOADER
200 #include "loader/ldt_keeper.h"
201 #endif
202 void* WINAPI LoadLibraryA(char* name);
203 void* WINAPI GetProcAddress(void* handle,char *func);
204 int WINAPI FreeLibrary(void *handle);
206 static int load_syms_windows(char *path)
208 void *handle;
210 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
211 #ifdef WIN32_LOADER
212 Setup_LDT_Keeper();
213 #endif
214 handle = LoadLibraryA(path);
215 if (!handle)
217 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
218 return 0;
221 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
222 wraDecode = GetProcAddress(handle, "RADecode");
223 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
224 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
225 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
226 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
227 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
228 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
229 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
231 if (wraCloseCodec && wraDecode && wraFreeDecoder &&
232 (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
233 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
235 rv_handle = handle;
236 dll_type = 1;
237 return 1;
240 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
241 FreeLibrary(handle);
242 return 0;
245 #endif
248 static int preinit(sh_audio_t *sh){
249 // let's check if the driver is available, return 0 if not.
250 // (you should do that if you use external lib(s) which is optional)
251 unsigned int result;
252 char *path;
254 path = malloc(strlen(BINARY_CODECS_PATH) + strlen(sh->codec->dll) + 2);
255 if (!path) return 0;
256 sprintf(path, BINARY_CODECS_PATH "/%s", sh->codec->dll);
258 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
259 then try to load the windows ones */
261 #ifdef HAVE_LIBDL
262 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
263 #endif
264 #ifdef CONFIG_WIN32DLL
265 if (!load_syms_windows(sh->codec->dll))
266 #endif
268 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingDLLcodec, sh->codec->dll);
269 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
270 free(path);
271 return 0;
274 #ifdef CONFIG_WIN32DLL
275 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
276 #else
277 if(raSetDLLAccessPath){
278 #endif
279 // used by 'SIPR'
280 path = realloc(path, strlen(BINARY_CODECS_PATH) + 13);
281 sprintf(path, "DT_Codecs=" BINARY_CODECS_PATH);
282 if(path[strlen(path)-1]!='/'){
283 path[strlen(path)+1]=0;
284 path[strlen(path)]='/';
286 path[strlen(path)+1]=0;
287 #ifdef CONFIG_WIN32DLL
288 if (dll_type == 1)
290 int i;
291 for (i=0; i < strlen(path); i++)
292 if (path[i] == '/') path[i] = '\\';
293 wraSetDLLAccessPath(path);
295 else
296 #endif
297 raSetDLLAccessPath(path);
300 #ifdef CONFIG_WIN32DLL
301 if (dll_type == 1){
302 if(wraOpenCodec2)
303 result = wraOpenCodec2(&sh->context, BINARY_CODECS_PATH "\\");
304 else
305 result=wraOpenCodec(&sh->context);
306 } else
307 #endif
308 if(raOpenCodec2)
309 result = raOpenCodec2(&sh->context, BINARY_CODECS_PATH "/");
310 else
311 result=raOpenCodec(&sh->context);
312 if(result){
313 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
314 return 0;
316 // printf("opencodec ok (result: %x)\n", result);
317 free(path); /* after this it isn't used anymore */
319 sh->samplerate=sh->wf->nSamplesPerSec;
320 sh->samplesize=sh->wf->wBitsPerSample/8;
321 sh->channels=sh->wf->nChannels;
324 ra_init_t init_data={
325 sh->wf->nSamplesPerSec,
326 sh->wf->wBitsPerSample,
327 sh->wf->nChannels,
328 100, // quality
329 sh->wf->nBlockAlign, // subpacket size
330 sh->wf->nBlockAlign, // coded frame size
331 sh->wf->cbSize, // codec data length
332 (char*)(sh->wf+1) // extras
334 #ifdef CONFIG_WIN32DLL
335 wra_init_t winit_data={
336 sh->wf->nSamplesPerSec,
337 sh->wf->wBitsPerSample,
338 sh->wf->nChannels,
339 100, // quality
340 sh->wf->nBlockAlign, // subpacket size
341 sh->wf->nBlockAlign, // coded frame size
342 sh->wf->cbSize, // codec data length
343 (char*)(sh->wf+1) // extras
345 #endif
346 #ifdef CONFIG_WIN32DLL
347 if (dll_type == 1)
348 result=wraInitDecoder(sh->context,&winit_data);
349 else
350 #endif
351 result=raInitDecoder(sh->context,&init_data);
353 if(result){
354 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
355 return 0;
357 // printf("initdecoder ok (result: %x)\n", result);
360 #ifdef CONFIG_WIN32DLL
361 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
362 #else
363 if(raSetPwd){
364 #endif
365 // used by 'SIPR'
366 #ifdef CONFIG_WIN32DLL
367 if (dll_type == 1)
368 wraSetPwd(sh->context,"Ardubancel Quazanga");
369 else
370 #endif
371 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
374 if (sh->format == mmioFOURCC('s','i','p','r')) {
375 short flavor;
377 if (sh->wf->nAvgBytesPerSec > 1531)
378 flavor = 3;
379 else if (sh->wf->nAvgBytesPerSec > 937)
380 flavor = 1;
381 else if (sh->wf->nAvgBytesPerSec > 719)
382 flavor = 0;
383 else
384 flavor = 2;
385 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
387 #ifdef CONFIG_WIN32DLL
388 if (dll_type == 1)
389 result=wraSetFlavor(sh->context,flavor);
390 else
391 #endif
392 result=raSetFlavor(sh->context,flavor);
393 if(result){
394 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
395 return 0;
397 } // sipr flavor
399 sh->i_bps=sh->wf->nAvgBytesPerSec;
401 sh->audio_out_minsize=128000; // no idea how to get... :(
402 sh->audio_in_minsize = sh->wf->nBlockAlign;
404 return 1; // return values: 1=OK 0=ERROR
407 static int init(sh_audio_t *sh_audio){
408 // initialize the decoder, set tables etc...
410 // you can store HANDLE or private struct pointer at sh->context
411 // you can access WAVEFORMATEX header at sh->wf
413 // set sample format/rate parameters if you didn't do it in preinit() yet.
415 return 1; // return values: 1=OK 0=ERROR
418 static void uninit(sh_audio_t *sh){
419 // uninit the decoder etc...
420 // again: you don't have to free() a_in_buffer here! it's done by the core.
421 #ifdef CONFIG_WIN32DLL
422 if (dll_type == 1)
424 if (wraFreeDecoder) wraFreeDecoder(sh->context);
425 if (wraCloseCodec) wraCloseCodec(sh->context);
427 #endif
429 if (raFreeDecoder) raFreeDecoder(sh->context);
430 if (raCloseCodec) raCloseCodec(sh->context);
433 #ifdef CONFIG_WIN32DLL
434 if (dll_type == 1)
436 if (rv_handle) FreeLibrary(rv_handle);
437 } else
438 #endif
439 // this dlclose() causes some memory corruption, and crashes soon (in caller):
440 // if (rv_handle) dlclose(rv_handle);
441 rv_handle = NULL;
444 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
445 int result;
446 int len=-1;
448 if(sh->a_in_buffer_len<=0){
449 // fill the buffer!
450 if (sh->ds->eof)
451 return 0;
452 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
453 sh->a_in_buffer_size=
454 sh->a_in_buffer_len=sh->wf->nBlockAlign;
457 #ifdef CONFIG_WIN32DLL
458 if (dll_type == 1)
459 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
460 buf, &len, -1);
461 else
462 #endif
463 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
464 buf, &len, -1);
465 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
467 // printf("radecode: %d bytes, res=0x%X \n",len,result);
469 return len; // return value: number of _bytes_ written to output buffer,
470 // or -1 for EOF (or uncorrectable error)
473 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
474 // various optional functions you MAY implement:
475 switch(cmd){
476 case ADCTRL_RESYNC_STREAM:
477 // it is called once after seeking, to resync.
478 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
479 return CONTROL_TRUE;
480 case ADCTRL_SKIP_FRAME:
481 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
482 // of audio data - used to sync audio to video after seeking
483 // if you don't return CONTROL_TRUE, it will defaults to:
484 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
485 return CONTROL_TRUE;
487 return CONTROL_UNKNOWN;