typo fixes
[mplayer/greg.git] / libmpcodecs / ad_realaud.c
blob2517f2c3499d1a72a92db97de13377b009cec817
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 #include "config.h"
8 #ifdef USE_REALCODECS
10 //#include <stddef.h>
11 #ifdef HAVE_LIBDL
12 #include <dlfcn.h>
13 #endif
14 #include "help_mp.h"
16 #include "ad_internal.h"
17 #include "wine/windef.h"
19 static ad_info_t info = {
20 "RealAudio decoder",
21 "realaud",
22 "Alex Beregszaszi",
23 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
24 "binary real audio codecs"
27 LIBAD_EXTERN(realaud)
29 void *__builtin_new(unsigned long size) {
30 return malloc(size);
33 // required for cook's uninit:
34 void __builtin_delete(void* ize) {
35 free(ize);
38 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
39 void *__ctype_b=NULL;
40 #endif
42 static unsigned long (*raCloseCodec)(void*);
43 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
44 static unsigned long (*raFlush)(unsigned long,unsigned long,unsigned long);
45 static unsigned long (*raFreeDecoder)(void*);
46 static void* (*raGetFlavorProperty)(void*,unsigned long,unsigned long,int*);
47 //static unsigned long (*raGetNumberOfFlavors2)(void);
48 static unsigned long (*raInitDecoder)(void*, void*);
49 static unsigned long (*raOpenCodec)(void*);
50 static unsigned long (*raOpenCodec2)(void*, void*);
51 static unsigned long (*raSetFlavor)(void*,unsigned long);
52 static void (*raSetDLLAccessPath)(char*);
53 static void (*raSetPwd)(char*,char*);
54 #ifdef USE_WIN32DLL
55 static unsigned long WINAPI (*wraCloseCodec)(void*);
56 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
57 static unsigned long WINAPI (*wraFlush)(unsigned long,unsigned long,unsigned long);
58 static unsigned long WINAPI (*wraFreeDecoder)(void*);
59 static void* WINAPI (*wraGetFlavorProperty)(void*,unsigned long,unsigned long,int*);
60 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
61 static unsigned long WINAPI (*wraOpenCodec)(void*);
62 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
63 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
64 static void WINAPI (*wraSetDLLAccessPath)(char*);
65 static void WINAPI (*wraSetPwd)(char*,char*);
67 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
68 #endif
70 static void *rv_handle = NULL;
72 #if 0
73 typedef struct {
74 int samplerate;
75 short bits;
76 short channels;
77 int unk1;
78 int unk2;
79 int packetsize;
80 int unk3;
81 void* unk4;
82 } ra_init_t ;
83 #else
86 Probably the linux .so-s were compiled with old GCC without setting
87 packing, so it adds 2 bytes padding after the quality field.
88 In windows it seems that there's no padding in it.
90 -- alex
93 /* linux dlls doesn't need packing */
94 typedef struct /*__attribute__((__packed__))*/ {
95 int samplerate;
96 short bits;
97 short channels;
98 short quality;
99 /* 2bytes padding here, by gcc */
100 int bits_per_frame;
101 int packetsize;
102 int extradata_len;
103 void* extradata;
104 } ra_init_t;
106 /* windows dlls need packed structs (no padding) */
107 typedef struct __attribute__((__packed__)) {
108 int samplerate;
109 short bits;
110 short channels;
111 short quality;
112 int bits_per_frame;
113 int packetsize;
114 int extradata_len;
115 void* extradata;
116 } wra_init_t;
117 #endif
119 #ifdef HAVE_LIBDL
120 static int load_syms_linux(char *path)
122 void *handle;
124 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
125 handle = dlopen(path, RTLD_LAZY);
126 if (!handle)
128 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
129 return 0;
132 raCloseCodec = dlsym(handle, "RACloseCodec");
133 raDecode = dlsym(handle, "RADecode");
134 raFlush = dlsym(handle, "RAFlush");
135 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
136 raGetFlavorProperty = dlsym(handle, "RAGetFlavorProperty");
137 raOpenCodec = dlsym(handle, "RAOpenCodec");
138 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
139 raInitDecoder = dlsym(handle, "RAInitDecoder");
140 raSetFlavor = dlsym(handle, "RASetFlavor");
141 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
142 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
144 if (raCloseCodec && raDecode && /*raFlush && */raFreeDecoder &&
145 raGetFlavorProperty && (raOpenCodec||raOpenCodec2) && raSetFlavor &&
146 /*raSetDLLAccessPath &&*/ raInitDecoder)
148 rv_handle = handle;
149 return 1;
152 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
153 dlclose(handle);
154 return 0;
156 #endif
158 #ifdef USE_WIN32DLL
160 #ifdef WIN32_LOADER
161 #include "loader/ldt_keeper.h"
162 #endif
163 void* WINAPI LoadLibraryA(char* name);
164 void* WINAPI GetProcAddress(void* handle,char *func);
165 int WINAPI FreeLibrary(void *handle);
167 static int load_syms_windows(char *path)
169 void *handle;
171 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
172 #ifdef WIN32_LOADER
173 Setup_LDT_Keeper();
174 #endif
175 handle = LoadLibraryA(path);
176 if (!handle)
178 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
179 return 0;
182 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
183 wraDecode = GetProcAddress(handle, "RADecode");
184 wraFlush = GetProcAddress(handle, "RAFlush");
185 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
186 wraGetFlavorProperty = GetProcAddress(handle, "RAGetFlavorProperty");
187 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
188 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
189 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
190 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
191 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
192 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
194 if (wraCloseCodec && wraDecode && /*wraFlush && */wraFreeDecoder &&
195 wraGetFlavorProperty && (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
196 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
198 rv_handle = handle;
199 dll_type = 1;
200 return 1;
203 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
204 FreeLibrary(handle);
205 return 0;
208 #endif
211 static int preinit(sh_audio_t *sh){
212 // let's check if the driver is available, return 0 if not.
213 // (you should do that if you use external lib(s) which is optional)
214 unsigned int result;
215 int len=0;
216 void* prop;
217 char *path;
219 path = malloc(strlen(REALCODEC_PATH)+strlen(sh->codec->dll)+2);
220 if (!path) return 0;
221 sprintf(path, REALCODEC_PATH "/%s", sh->codec->dll);
223 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
224 then try to load the windows ones */
226 #ifdef HAVE_LIBDL
227 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
228 #endif
229 #ifdef USE_WIN32DLL
230 if (!load_syms_windows(sh->codec->dll))
231 #endif
233 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingDLLcodec, sh->codec->dll);
234 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
235 free(path);
236 return 0;
239 #ifdef USE_WIN32DLL
240 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
241 #else
242 if(raSetDLLAccessPath){
243 #endif
244 int i;
245 // used by 'SIPR'
246 path = realloc(path, strlen(REALCODEC_PATH) + 13);
247 sprintf(path, "DT_Codecs=" REALCODEC_PATH);
248 if(path[strlen(path)-1]!='/'){
249 path[strlen(path)+1]=0;
250 path[strlen(path)]='/';
252 path[strlen(path)+1]=0;
253 #ifdef USE_WIN32DLL
254 if (dll_type == 1)
256 for (i=0; i < strlen(path); i++)
257 if (path[i] == '/') path[i] = '\\';
258 wraSetDLLAccessPath(path);
260 else
261 #endif
262 raSetDLLAccessPath(path);
265 #ifdef USE_WIN32DLL
266 if (dll_type == 1){
267 if(wraOpenCodec2)
268 result=wraOpenCodec2(&sh->context,REALCODEC_PATH "\\");
269 else
270 result=wraOpenCodec(&sh->context);
271 } else
272 #endif
273 if(raOpenCodec2)
274 result=raOpenCodec2(&sh->context,REALCODEC_PATH "/");
275 else
276 result=raOpenCodec(&sh->context);
277 if(result){
278 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
279 return 0;
281 // printf("opencodec ok (result: %x)\n", result);
282 free(path); /* after this it isn't used anymore */
284 sh->samplerate=sh->wf->nSamplesPerSec;
285 sh->samplesize=sh->wf->wBitsPerSample/8;
286 sh->channels=sh->wf->nChannels;
289 ra_init_t init_data={
290 sh->wf->nSamplesPerSec,
291 sh->wf->wBitsPerSample,
292 sh->wf->nChannels,
293 100, // quality
294 sh->wf->nBlockAlign, // subpacket size
295 sh->wf->nBlockAlign, // coded frame size
296 sh->wf->cbSize, // codec data length
297 (char*)(sh->wf+1) // extras
299 #ifdef USE_WIN32DLL
300 wra_init_t winit_data={
301 sh->wf->nSamplesPerSec,
302 sh->wf->wBitsPerSample,
303 sh->wf->nChannels,
304 100, // quality
305 sh->wf->nBlockAlign, // subpacket size
306 sh->wf->nBlockAlign, // coded frame size
307 sh->wf->cbSize, // codec data length
308 (char*)(sh->wf+1) // extras
310 #endif
311 #ifdef USE_WIN32DLL
312 if (dll_type == 1)
313 result=wraInitDecoder(sh->context,&winit_data);
314 else
315 #endif
316 result=raInitDecoder(sh->context,&init_data);
318 if(result){
319 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
320 return 0;
322 // printf("initdecoder ok (result: %x)\n", result);
325 #ifdef USE_WIN32DLL
326 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
327 #else
328 if(raSetPwd){
329 #endif
330 // used by 'SIPR'
331 #ifdef USE_WIN32DLL
332 if (dll_type == 1)
333 wraSetPwd(sh->context,"Ardubancel Quazanga");
334 else
335 #endif
336 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
339 if (sh->format == mmioFOURCC('s','i','p','r')) {
340 short flavor;
342 if (sh->wf->nAvgBytesPerSec > 1531)
343 flavor = 3;
344 else if (sh->wf->nAvgBytesPerSec > 937)
345 flavor = 1;
346 else if (sh->wf->nAvgBytesPerSec > 719)
347 flavor = 0;
348 else
349 flavor = 2;
350 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
352 #ifdef USE_WIN32DLL
353 if (dll_type == 1)
354 result=wraSetFlavor(sh->context,flavor);
355 else
356 #endif
357 result=raSetFlavor(sh->context,flavor);
358 if(result){
359 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
360 return 0;
362 } // sipr flavor
364 sh->i_bps=sh->wf->nAvgBytesPerSec;
366 sh->audio_out_minsize=128000; // no idea how to get... :(
367 sh->audio_in_minsize = sh->wf->nBlockAlign;
369 return 1; // return values: 1=OK 0=ERROR
372 static int init(sh_audio_t *sh_audio){
373 // initialize the decoder, set tables etc...
375 // you can store HANDLE or private struct pointer at sh->context
376 // you can access WAVEFORMATEX header at sh->wf
378 // set sample format/rate parameters if you didn't do it in preinit() yet.
380 return 1; // return values: 1=OK 0=ERROR
383 static void uninit(sh_audio_t *sh){
384 // uninit the decoder etc...
385 // again: you don't have to free() a_in_buffer here! it's done by the core.
386 #ifdef USE_WIN32DLL
387 if (dll_type == 1)
389 if (wraFreeDecoder) wraFreeDecoder(sh->context);
390 if (wraCloseCodec) wraCloseCodec(sh->context);
392 #endif
394 if (raFreeDecoder) raFreeDecoder(sh->context);
395 if (raCloseCodec) raCloseCodec(sh->context);
398 #ifdef USE_WIN32DLL
399 if (dll_type == 1)
401 if (rv_handle) FreeLibrary(rv_handle);
402 } else
403 #endif
404 // this dlclose() causes some memory corruption, and crashes soon (in caller):
405 // if (rv_handle) dlclose(rv_handle);
406 rv_handle = NULL;
409 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
410 int result;
411 int len=-1;
413 if(sh->a_in_buffer_len<=0){
414 // fill the buffer!
415 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
416 sh->a_in_buffer_size=
417 sh->a_in_buffer_len=sh->wf->nBlockAlign;
420 #ifdef USE_WIN32DLL
421 if (dll_type == 1)
422 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
423 buf, &len, -1);
424 else
425 #endif
426 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
427 buf, &len, -1);
428 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
430 // printf("radecode: %d bytes, res=0x%X \n",len,result);
432 return len; // return value: number of _bytes_ written to output buffer,
433 // or -1 for EOF (or uncorrectable error)
436 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
437 // various optional functions you MAY implement:
438 switch(cmd){
439 case ADCTRL_RESYNC_STREAM:
440 // it is called once after seeking, to resync.
441 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
442 return CONTROL_TRUE;
443 case ADCTRL_SKIP_FRAME:
444 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
445 // of audio data - used to sync audio to video after seeking
446 // if you don't return CONTROL_TRUE, it will defaults to:
447 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
448 return CONTROL_TRUE;
450 return CONTROL_UNKNOWN;
453 #endif