dx50 = DX50
[mplayer/glamo.git] / libmpcodecs / ad_realaud.c
blobc7b5389f24c2fa1bb62c5457619a80b638d3385c
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"
18 static ad_info_t info = {
19 "RealAudio decoder",
20 "realaud",
21 "A'rpi", // win32 dlls support by alex
22 "Florian Schneider",
23 "binary real audio codecs"
26 LIBAD_EXTERN(realaud)
28 void *__builtin_new(unsigned long size) {
29 return malloc(size);
32 // required for cook's uninit:
33 void __builtin_delete(void* ize) {
34 free(ize);
37 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
38 void *__ctype_b=NULL;
39 #endif
41 static unsigned long (*raCloseCodec)(void*);
42 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
43 static unsigned long (*raFlush)(unsigned long,unsigned long,unsigned long);
44 static unsigned long (*raFreeDecoder)(void*);
45 static void* (*raGetFlavorProperty)(void*,unsigned long,unsigned long,int*);
46 //static unsigned long (*raGetNumberOfFlavors2)(void);
47 static unsigned long (*raInitDecoder)(void*, void*);
48 static unsigned long (*raOpenCodec)(void*);
49 static unsigned long (*raOpenCodec2)(void*, void*);
50 static unsigned long (*raSetFlavor)(void*,unsigned long);
51 static void (*raSetDLLAccessPath)(char*);
52 static void (*raSetPwd)(char*,char*);
53 #ifdef USE_WIN32DLL
54 static unsigned long WINAPI (*wraCloseCodec)(void*);
55 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
56 static unsigned long WINAPI (*wraFlush)(unsigned long,unsigned long,unsigned long);
57 static unsigned long WINAPI (*wraFreeDecoder)(void*);
58 static void* WINAPI (*wraGetFlavorProperty)(void*,unsigned long,unsigned long,int*);
59 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
60 static unsigned long WINAPI (*wraOpenCodec)(void*);
61 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
62 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
63 static void WINAPI (*wraSetDLLAccessPath)(char*);
64 static void WINAPI (*wraSetPwd)(char*,char*);
66 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
67 #endif
68 static void *rv_handle = NULL;
70 #if 0
71 typedef struct {
72 int samplerate;
73 short bits;
74 short channels;
75 int unk1;
76 int unk2;
77 int packetsize;
78 int unk3;
79 void* unk4;
80 } ra_init_t ;
81 #else
84 Probably the linux .so-s were compiled with old GCC without setting
85 packing, so it adds 2 bytes padding after the quality field.
86 In windows it seems that there's no padding in it.
88 -- alex
91 /* linux dlls doesn't need packing */
92 typedef struct /*__attribute__((__packed__))*/ {
93 int samplerate;
94 short bits;
95 short channels;
96 short quality;
97 /* 2bytes padding here, by gcc */
98 int bits_per_frame;
99 int packetsize;
100 int extradata_len;
101 void* extradata;
102 } ra_init_t;
104 /* windows dlls need packed structs (no padding) */
105 typedef struct __attribute__((__packed__)) {
106 int samplerate;
107 short bits;
108 short channels;
109 short quality;
110 int bits_per_frame;
111 int packetsize;
112 int extradata_len;
113 void* extradata;
114 } wra_init_t;
115 #endif
117 #ifdef HAVE_LIBDL
118 static int load_syms_linux(char *path)
120 void *handle;
122 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "opening shared obj '%s'\n", path);
123 handle = dlopen(path, RTLD_LAZY);
124 if (!handle)
126 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
127 return 0;
130 raCloseCodec = dlsym(handle, "RACloseCodec");
131 raDecode = dlsym(handle, "RADecode");
132 raFlush = dlsym(handle, "RAFlush");
133 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
134 raGetFlavorProperty = dlsym(handle, "RAGetFlavorProperty");
135 raOpenCodec = dlsym(handle, "RAOpenCodec");
136 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
137 raInitDecoder = dlsym(handle, "RAInitDecoder");
138 raSetFlavor = dlsym(handle, "RASetFlavor");
139 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
140 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
142 if (raCloseCodec && raDecode && /*raFlush && */raFreeDecoder &&
143 raGetFlavorProperty && (raOpenCodec||raOpenCodec2) && raSetFlavor &&
144 /*raSetDLLAccessPath &&*/ raInitDecoder)
146 rv_handle = handle;
147 return 1;
150 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
151 dlclose(handle);
152 return 0;
154 #endif
156 #ifdef USE_WIN32DLL
158 #ifdef WIN32_LOADER
159 #include "../loader/ldt_keeper.h"
160 #endif
161 void* WINAPI LoadLibraryA(char* name);
162 void* WINAPI GetProcAddress(void* handle,char *func);
163 int WINAPI FreeLibrary(void *handle);
165 static int load_syms_windows(char *path)
167 void *handle;
169 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "opening win32 dll '%s'\n", path);
170 #ifdef WIN32_LOADER
171 Setup_LDT_Keeper();
172 #endif
173 handle = LoadLibraryA(path);
174 if (!handle)
176 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
177 return 0;
180 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
181 wraDecode = GetProcAddress(handle, "RADecode");
182 wraFlush = GetProcAddress(handle, "RAFlush");
183 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
184 wraGetFlavorProperty = GetProcAddress(handle, "RAGetFlavorProperty");
185 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
186 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
187 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
188 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
189 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
190 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
192 if (wraCloseCodec && wraDecode && /*wraFlush && */wraFreeDecoder &&
193 wraGetFlavorProperty && (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
194 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
196 rv_handle = handle;
197 dll_type = 1;
198 return 1;
201 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
202 FreeLibrary(handle);
203 return 0;
206 #endif
208 static int preinit(sh_audio_t *sh){
209 // let's check if the driver is available, return 0 if not.
210 // (you should do that if you use external lib(s) which is optional)
211 unsigned int result;
212 int len=0;
213 void* prop;
214 char *path;
216 path = malloc(strlen(REALCODEC_PATH)+strlen(sh->codec->dll)+2);
217 if (!path) return 0;
218 sprintf(path, REALCODEC_PATH "/%s", sh->codec->dll);
220 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
221 then try to load the windows ones */
222 #ifdef HAVE_LIBDL
223 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
224 #endif
225 #ifdef USE_WIN32DLL
226 if (!load_syms_windows(sh->codec->dll))
227 #endif
229 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingDLLcodec, sh->codec->dll);
230 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
231 free(path);
232 return 0;
235 #ifdef USE_WIN32DLL
236 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
237 #else
238 if(raSetDLLAccessPath){
239 #endif
240 int i;
241 // used by 'SIPR'
242 path = realloc(path, strlen(REALCODEC_PATH) + 12);
243 sprintf(path, "DT_Codecs=" REALCODEC_PATH);
244 if(path[strlen(path)-1]!='/'){
245 path[strlen(path)+1]=0;
246 path[strlen(path)]='/';
248 path[strlen(path)+1]=0;
249 #ifdef USE_WIN32DLL
250 if (dll_type == 1)
252 for (i=0; i < strlen(path); i++)
253 if (path[i] == '/') path[i] = '\\';
254 wraSetDLLAccessPath(path);
256 else
257 #endif
258 raSetDLLAccessPath(path);
261 #ifdef USE_WIN32DLL
262 if (dll_type == 1){
263 if(wraOpenCodec2)
264 result=wraOpenCodec2(&sh->context,REALCODEC_PATH "\\");
265 else
266 result=wraOpenCodec(&sh->context);
267 } else
268 #endif
269 if(raOpenCodec2)
270 result=raOpenCodec2(&sh->context,REALCODEC_PATH "/");
271 else
272 result=raOpenCodec(&sh->context);
273 if(result){
274 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
275 return 0;
277 // printf("opencodec ok (result: %x)\n", result);
278 free(path); /* after this it isn't used anymore */
280 sh->samplerate=sh->wf->nSamplesPerSec;
281 sh->samplesize=sh->wf->wBitsPerSample/8;
282 sh->channels=sh->wf->nChannels;
285 ra_init_t init_data={
286 sh->wf->nSamplesPerSec,
287 sh->wf->wBitsPerSample,
288 sh->wf->nChannels,
289 100, // quality
290 ((short*)(sh->wf+1))[0], // subpacket size
291 ((short*)(sh->wf+1))[3], // coded frame size
292 ((short*)(sh->wf+1))[4], // codec data length
293 ((char*)(sh->wf+1))+10 // extras
295 #ifdef USE_WIN32DLL
296 wra_init_t winit_data={
297 sh->wf->nSamplesPerSec,
298 sh->wf->wBitsPerSample,
299 sh->wf->nChannels,
300 100, // quality
301 ((short*)(sh->wf+1))[0], // subpacket size
302 ((short*)(sh->wf+1))[3], // coded frame size
303 ((short*)(sh->wf+1))[4], // codec data length
304 ((char*)(sh->wf+1))+10 // extras
306 if (dll_type == 1)
307 result=wraInitDecoder(sh->context,&winit_data);
308 else
309 #endif
310 result=raInitDecoder(sh->context,&init_data);
311 if(result){
312 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
313 return 0;
315 // printf("initdecoder ok (result: %x)\n", result);
318 #ifdef USE_WIN32DLL
319 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
320 #else
321 if(raSetPwd){
322 #endif
323 // used by 'SIPR'
324 #ifdef USE_WIN32DLL
325 if (dll_type == 1)
326 wraSetPwd(sh->context,"Ardubancel Quazanga");
327 else
328 #endif
329 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
332 #ifdef USE_WIN32DLL
333 if (dll_type == 1)
334 result=wraSetFlavor(sh->context,((short*)(sh->wf+1))[2]);
335 else
336 #endif
337 result=raSetFlavor(sh->context,((short*)(sh->wf+1))[2]);
338 if(result){
339 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
340 return 0;
343 #ifdef USE_WIN32DLL
344 if (dll_type == 1)
345 prop=wraGetFlavorProperty(sh->context,((short*)(sh->wf+1))[2],0,&len);
346 else
347 #endif
348 prop=raGetFlavorProperty(sh->context,((short*)(sh->wf+1))[2],0,&len);
349 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Audio codec: [%d] %s\n",((short*)(sh->wf+1))[2],prop);
351 #ifdef USE_WIN32DLL
352 if (dll_type == 1)
353 prop=wraGetFlavorProperty(sh->context,((short*)(sh->wf+1))[2],1,&len);
354 else
355 #endif
356 prop=raGetFlavorProperty(sh->context,((short*)(sh->wf+1))[2],1,&len);
357 if(prop){
358 sh->i_bps=((*((int*)prop))+4)/8;
359 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Audio bitrate: %5.3f kbit/s (%d bps) \n",(*((int*)prop))*0.001f,sh->i_bps);
360 } else
361 sh->i_bps=12000; // dunno :((( [12000 seems to be OK for crash.rmvb too]
363 // prop=raGetFlavorProperty(sh->context,((short*)(sh->wf+1))[2],0x13,&len);
364 // mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Samples/block?: %d \n",(*((int*)prop)));
366 sh->audio_out_minsize=128000; // no idea how to get... :(
367 sh->audio_in_minsize=((short*)(sh->wf+1))[1]*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);
397 #ifdef USE_WIN32DLL
398 if (dll_type == 1)
400 if (rv_handle) FreeLibrary(rv_handle);
401 } else
402 #endif
403 // this dlclose() causes some memory corruption, and crashes soon (in caller):
404 // if (rv_handle) dlclose(rv_handle);
405 rv_handle = NULL;
408 static unsigned char sipr_swaps[38][2]={
409 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
410 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
411 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
412 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
413 {77,80} };
415 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
416 int result;
417 int len=-1;
418 int sps=((short*)(sh->wf+1))[0];
419 int w=sh->wf->nBlockAlign; // 5
420 int h=((short*)(sh->wf+1))[1];
421 int cfs=((short*)(sh->wf+1))[3];
423 // printf("bs=%d sps=%d w=%d h=%d \n",sh->wf->nBlockAlign,sps,w,h);
425 #if 1
426 if(sh->a_in_buffer_len<=0){
427 // fill the buffer!
428 if (sh->format == mmioFOURCC('1','4','_','4')) {
429 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
430 sh->a_in_buffer_size=
431 sh->a_in_buffer_len=sh->wf->nBlockAlign;
432 } else
433 if (sh->format == mmioFOURCC('2','8','_','8')) {
434 int i,j;
435 for (j = 0; j < h; j++)
436 for (i = 0; i < h/2; i++)
437 demux_read_data(sh->ds, sh->a_in_buffer+i*2*w+j*cfs, cfs);
438 sh->a_in_buffer_size=
439 sh->a_in_buffer_len=sh->wf->nBlockAlign*h;
440 } else
441 if(!sps){
442 // 'sipr' way
443 int j,n;
444 int bs=h*w*2/96; // nibbles per subpacket
445 unsigned char *p=sh->a_in_buffer;
446 demux_read_data(sh->ds, p, h*w);
447 for(n=0;n<38;n++){
448 int i=bs*sipr_swaps[n][0];
449 int o=bs*sipr_swaps[n][1];
450 // swap nibbles of block 'i' with 'o' TODO: optimize
451 for(j=0;j<bs;j++){
452 int x=(i&1) ? (p[(i>>1)]>>4) : (p[(i>>1)]&15);
453 int y=(o&1) ? (p[(o>>1)]>>4) : (p[(o>>1)]&15);
454 if(o&1) p[(o>>1)]=(p[(o>>1)]&0x0F)|(x<<4);
455 else p[(o>>1)]=(p[(o>>1)]&0xF0)|x;
456 if(i&1) p[(i>>1)]=(p[(i>>1)]&0x0F)|(y<<4);
457 else p[(i>>1)]=(p[(i>>1)]&0xF0)|y;
458 ++i;++o;
461 sh->a_in_buffer_size=
462 sh->a_in_buffer_len=w*h;
463 } else {
464 // 'cook' way
465 int x,y;
466 w/=sps;
467 for(y=0;y<h;y++)
468 for(x=0;x<w;x++){
469 demux_read_data(sh->ds, sh->a_in_buffer+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
471 sh->a_in_buffer_size=
472 sh->a_in_buffer_len=w*h*sps;
476 #else
477 if(sh->a_in_buffer_len<=0){
478 // fill the buffer!
479 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
480 sh->a_in_buffer_size=
481 sh->a_in_buffer_len=sh->wf->nBlockAlign;
483 #endif
485 #ifdef USE_WIN32DLL
486 if (dll_type == 1)
487 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
488 buf, &len, -1);
489 else
490 #endif
491 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
492 buf, &len, -1);
493 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
495 // printf("radecode: %d bytes, res=0x%X \n",len,result);
497 return len; // return value: number of _bytes_ written to output buffer,
498 // or -1 for EOF (or uncorrectable error)
501 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
502 // various optional functions you MAY implement:
503 switch(cmd){
504 case ADCTRL_RESYNC_STREAM:
505 // it is called once after seeking, to resync.
506 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
507 return CONTROL_TRUE;
508 case ADCTRL_SKIP_FRAME:
509 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
510 // of audio data - used to sync audio to video after seeking
511 // if you don't return CONTROL_TRUE, it will defaults to:
512 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
513 return CONTROL_TRUE;
515 return CONTROL_UNKNOWN;
518 #endif