Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libmpcodecs / ad_realaud.c
blob1d475f8ddab8cbf86e5d6f1fdf114f3b0d42296b
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"
30 #include "path.h"
32 #include "ad_internal.h"
33 #include "loader/wine/windef.h"
35 static const ad_info_t info = {
36 "RealAudio decoder",
37 "realaud",
38 "Alex Beregszaszi",
39 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
40 "binary real audio codecs"
43 LIBAD_EXTERN(realaud)
45 /* These functions are required for loading Real binary libs.
46 * Add forward declarations to avoid warnings with -Wmissing-prototypes. */
47 void *__builtin_new(unsigned long size);
48 void __builtin_delete(void *ize);
49 void *__builtin_vec_new(unsigned long size);
50 void __builtin_vec_delete(void *mem);
51 void __pure_virtual(void);
53 void *__builtin_new(unsigned long size)
55 return malloc(size);
58 void __builtin_delete(void* ize)
60 free(ize);
63 void *__builtin_vec_new(unsigned long size)
65 return malloc(size);
68 void __builtin_vec_delete(void *mem)
70 free(mem);
73 void __pure_virtual(void)
75 printf("FATAL: __pure_virtual() called!\n");
76 // exit(1);
79 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
80 void ___brk_addr(void);
81 void ___brk_addr(void) {exit(0);}
82 char **__environ={NULL};
83 #undef stderr
84 FILE *stderr=NULL;
85 void *__ctype_b=NULL;
86 #endif
88 static unsigned long (*raCloseCodec)(void*);
89 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
90 static unsigned long (*raFreeDecoder)(void*);
91 //static unsigned long (*raGetNumberOfFlavors2)(void);
92 static unsigned long (*raInitDecoder)(void*, void*);
93 static unsigned long (*raOpenCodec)(void*);
94 static unsigned long (*raOpenCodec2)(void*, void*);
95 static unsigned long (*raSetFlavor)(void*,unsigned long);
96 static void (*raSetDLLAccessPath)(char*);
97 static void (*raSetPwd)(char*,char*);
98 #ifdef CONFIG_WIN32DLL
99 static unsigned long WINAPI (*wraCloseCodec)(void*);
100 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
101 static unsigned long WINAPI (*wraFreeDecoder)(void*);
102 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
103 static unsigned long WINAPI (*wraOpenCodec)(void*);
104 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
105 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
106 static void WINAPI (*wraSetDLLAccessPath)(char*);
107 static void WINAPI (*wraSetPwd)(char*,char*);
109 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
110 #endif
112 static void *rv_handle = NULL;
114 #if 0
115 typedef struct {
116 int samplerate;
117 short bits;
118 short channels;
119 int unk1;
120 int unk2;
121 int packetsize;
122 int unk3;
123 void* unk4;
124 } ra_init_t ;
125 #else
128 Probably the linux .so-s were compiled with old GCC without setting
129 packing, so it adds 2 bytes padding after the quality field.
130 In windows it seems that there's no padding in it.
132 -- alex
135 /* linux dlls doesn't need packing */
136 typedef struct /*__attribute__((__packed__))*/ {
137 int samplerate;
138 short bits;
139 short channels;
140 short quality;
141 /* 2bytes padding here, by gcc */
142 int bits_per_frame;
143 int packetsize;
144 int extradata_len;
145 void* extradata;
146 } ra_init_t;
148 /* windows dlls need packed structs (no padding) */
149 typedef struct __attribute__((__packed__)) {
150 int samplerate;
151 short bits;
152 short channels;
153 short quality;
154 int bits_per_frame;
155 int packetsize;
156 int extradata_len;
157 void* extradata;
158 } wra_init_t;
159 #endif
161 #ifdef HAVE_LIBDL
162 static int load_syms_linux(char *path)
164 void *handle;
166 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
167 handle = dlopen(path, RTLD_LAZY);
168 if (!handle)
170 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
171 return 0;
174 raCloseCodec = dlsym(handle, "RACloseCodec");
175 raDecode = dlsym(handle, "RADecode");
176 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
177 raOpenCodec = dlsym(handle, "RAOpenCodec");
178 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
179 raInitDecoder = dlsym(handle, "RAInitDecoder");
180 raSetFlavor = dlsym(handle, "RASetFlavor");
181 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
182 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
184 if (raCloseCodec && raDecode && raFreeDecoder &&
185 (raOpenCodec||raOpenCodec2) && raSetFlavor &&
186 /*raSetDLLAccessPath &&*/ raInitDecoder)
188 rv_handle = handle;
189 return 1;
192 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
193 dlclose(handle);
194 return 0;
196 #endif
198 #ifdef CONFIG_WIN32DLL
200 #ifdef WIN32_LOADER
201 #include "loader/ldt_keeper.h"
202 #endif
203 void* WINAPI LoadLibraryA(char* name);
204 void* WINAPI GetProcAddress(void* handle,char *func);
205 int WINAPI FreeLibrary(void *handle);
207 static int load_syms_windows(char *path)
209 void *handle;
211 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
212 #ifdef WIN32_LOADER
213 Setup_LDT_Keeper();
214 #endif
215 handle = LoadLibraryA(path);
216 if (!handle)
218 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
219 return 0;
222 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
223 wraDecode = GetProcAddress(handle, "RADecode");
224 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
225 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
226 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
227 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
228 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
229 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
230 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
232 if (wraCloseCodec && wraDecode && wraFreeDecoder &&
233 (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
234 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
236 rv_handle = handle;
237 dll_type = 1;
238 return 1;
241 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
242 FreeLibrary(handle);
243 return 0;
246 #endif
249 static int preinit(sh_audio_t *sh){
250 // let's check if the driver is available, return 0 if not.
251 // (you should do that if you use external lib(s) which is optional)
252 unsigned int result;
253 char *path;
255 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
256 if (!path) return 0;
257 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
259 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
260 then try to load the windows ones */
262 #ifdef HAVE_LIBDL
263 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
264 #endif
265 #ifdef CONFIG_WIN32DLL
266 if (!load_syms_windows(sh->codec->dll))
267 #endif
269 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingDLLcodec, sh->codec->dll);
270 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
271 free(path);
272 return 0;
275 #ifdef CONFIG_WIN32DLL
276 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
277 #else
278 if(raSetDLLAccessPath){
279 #endif
280 // used by 'SIPR'
281 path = realloc(path, strlen(codec_path) + 13);
282 sprintf(path, "DT_Codecs=%s", codec_path);
283 if(path[strlen(path)-1]!='/'){
284 path[strlen(path)+1]=0;
285 path[strlen(path)]='/';
287 path[strlen(path)+1]=0;
288 #ifdef CONFIG_WIN32DLL
289 if (dll_type == 1)
291 int i;
292 for (i=0; i < strlen(path); i++)
293 if (path[i] == '/') path[i] = '\\';
294 wraSetDLLAccessPath(path);
296 else
297 #endif
298 raSetDLLAccessPath(path);
301 #ifdef CONFIG_WIN32DLL
302 if (dll_type == 1){
303 if (wraOpenCodec2) {
304 sprintf(path, "%s\\", codec_path);
305 result = wraOpenCodec2(&sh->context, path);
306 } else
307 result=wraOpenCodec(&sh->context);
308 } else
309 #endif
310 if (raOpenCodec2) {
311 sprintf(path, "%s/", codec_path);
312 result = raOpenCodec2(&sh->context, path);
313 } else
314 result=raOpenCodec(&sh->context);
315 if(result){
316 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
317 return 0;
319 // printf("opencodec ok (result: %x)\n", result);
320 free(path); /* after this it isn't used anymore */
322 sh->samplerate=sh->wf->nSamplesPerSec;
323 sh->samplesize=sh->wf->wBitsPerSample/8;
324 sh->channels=sh->wf->nChannels;
327 ra_init_t init_data={
328 sh->wf->nSamplesPerSec,
329 sh->wf->wBitsPerSample,
330 sh->wf->nChannels,
331 100, // quality
332 sh->wf->nBlockAlign, // subpacket size
333 sh->wf->nBlockAlign, // coded frame size
334 sh->wf->cbSize, // codec data length
335 (char*)(sh->wf+1) // extras
337 #ifdef CONFIG_WIN32DLL
338 wra_init_t winit_data={
339 sh->wf->nSamplesPerSec,
340 sh->wf->wBitsPerSample,
341 sh->wf->nChannels,
342 100, // quality
343 sh->wf->nBlockAlign, // subpacket size
344 sh->wf->nBlockAlign, // coded frame size
345 sh->wf->cbSize, // codec data length
346 (char*)(sh->wf+1) // extras
348 #endif
349 #ifdef CONFIG_WIN32DLL
350 if (dll_type == 1)
351 result=wraInitDecoder(sh->context,&winit_data);
352 else
353 #endif
354 result=raInitDecoder(sh->context,&init_data);
356 if(result){
357 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
358 return 0;
360 // printf("initdecoder ok (result: %x)\n", result);
363 #ifdef CONFIG_WIN32DLL
364 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
365 #else
366 if(raSetPwd){
367 #endif
368 // used by 'SIPR'
369 #ifdef CONFIG_WIN32DLL
370 if (dll_type == 1)
371 wraSetPwd(sh->context,"Ardubancel Quazanga");
372 else
373 #endif
374 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
377 if (sh->format == mmioFOURCC('s','i','p','r')) {
378 short flavor;
380 if (sh->wf->nAvgBytesPerSec > 1531)
381 flavor = 3;
382 else if (sh->wf->nAvgBytesPerSec > 937)
383 flavor = 1;
384 else if (sh->wf->nAvgBytesPerSec > 719)
385 flavor = 0;
386 else
387 flavor = 2;
388 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
390 #ifdef CONFIG_WIN32DLL
391 if (dll_type == 1)
392 result=wraSetFlavor(sh->context,flavor);
393 else
394 #endif
395 result=raSetFlavor(sh->context,flavor);
396 if(result){
397 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
398 return 0;
400 } // sipr flavor
402 sh->i_bps=sh->wf->nAvgBytesPerSec;
404 sh->audio_out_minsize=128000; // no idea how to get... :(
405 sh->audio_in_minsize = sh->wf->nBlockAlign;
407 return 1; // return values: 1=OK 0=ERROR
410 static int init(sh_audio_t *sh_audio){
411 // initialize the decoder, set tables etc...
413 // you can store HANDLE or private struct pointer at sh->context
414 // you can access WAVEFORMATEX header at sh->wf
416 // set sample format/rate parameters if you didn't do it in preinit() yet.
418 return 1; // return values: 1=OK 0=ERROR
421 static void uninit(sh_audio_t *sh){
422 // uninit the decoder etc...
423 // again: you don't have to free() a_in_buffer here! it's done by the core.
424 #ifdef CONFIG_WIN32DLL
425 if (dll_type == 1)
427 if (wraFreeDecoder) wraFreeDecoder(sh->context);
428 if (wraCloseCodec) wraCloseCodec(sh->context);
430 #endif
432 if (raFreeDecoder) raFreeDecoder(sh->context);
433 if (raCloseCodec) raCloseCodec(sh->context);
436 #ifdef CONFIG_WIN32DLL
437 if (dll_type == 1)
439 if (rv_handle) FreeLibrary(rv_handle);
440 } else
441 #endif
442 // this dlclose() causes some memory corruption, and crashes soon (in caller):
443 // if (rv_handle) dlclose(rv_handle);
444 rv_handle = NULL;
447 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
448 int result;
449 int len=-1;
451 if(sh->a_in_buffer_len<=0){
452 // fill the buffer!
453 if (sh->ds->eof)
454 return 0;
455 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
456 sh->a_in_buffer_size=
457 sh->a_in_buffer_len=sh->wf->nBlockAlign;
460 #ifdef CONFIG_WIN32DLL
461 if (dll_type == 1)
462 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
463 buf, &len, -1);
464 else
465 #endif
466 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
467 buf, &len, -1);
468 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
470 // printf("radecode: %d bytes, res=0x%X \n",len,result);
472 return len; // return value: number of _bytes_ written to output buffer,
473 // or -1 for EOF (or uncorrectable error)
476 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
477 // various optional functions you MAY implement:
478 switch(cmd){
479 case ADCTRL_RESYNC_STREAM:
480 // it is called once after seeking, to resync.
481 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
482 return CONTROL_TRUE;
483 case ADCTRL_SKIP_FRAME:
484 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
485 // of audio data - used to sync audio to video after seeking
486 // if you don't return CONTROL_TRUE, it will defaults to:
487 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
488 return CONTROL_TRUE;
490 return CONTROL_UNKNOWN;