Merge svn changes up to r30907
[mplayer/glamo.git] / libmpcodecs / ad_realaud.c
blobdd8b2947ece862dc775b58578002d380c2c12836
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
30 #include "ad_internal.h"
31 #include "loader/wine/windef.h"
33 static const ad_info_t info = {
34 "RealAudio decoder",
35 "realaud",
36 "Alex Beregszaszi",
37 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
38 "binary real audio codecs"
41 LIBAD_EXTERN(realaud)
43 void *__builtin_new(unsigned long size) {
44 return malloc(size);
47 // required for cook's uninit:
48 void __builtin_delete(void* ize) {
49 free(ize);
52 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
53 void *__ctype_b=NULL;
54 #endif
56 static unsigned long (*raCloseCodec)(void*);
57 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
58 static unsigned long (*raFreeDecoder)(void*);
59 //static unsigned long (*raGetNumberOfFlavors2)(void);
60 static unsigned long (*raInitDecoder)(void*, void*);
61 static unsigned long (*raOpenCodec)(void*);
62 static unsigned long (*raOpenCodec2)(void*, void*);
63 static unsigned long (*raSetFlavor)(void*,unsigned long);
64 static void (*raSetDLLAccessPath)(char*);
65 static void (*raSetPwd)(char*,char*);
66 #ifdef CONFIG_WIN32DLL
67 static unsigned long WINAPI (*wraCloseCodec)(void*);
68 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
69 static unsigned long WINAPI (*wraFreeDecoder)(void*);
70 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
71 static unsigned long WINAPI (*wraOpenCodec)(void*);
72 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
73 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
74 static void WINAPI (*wraSetDLLAccessPath)(char*);
75 static void WINAPI (*wraSetPwd)(char*,char*);
77 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
78 #endif
80 static void *rv_handle = NULL;
82 #if 0
83 typedef struct {
84 int samplerate;
85 short bits;
86 short channels;
87 int unk1;
88 int unk2;
89 int packetsize;
90 int unk3;
91 void* unk4;
92 } ra_init_t ;
93 #else
96 Probably the linux .so-s were compiled with old GCC without setting
97 packing, so it adds 2 bytes padding after the quality field.
98 In windows it seems that there's no padding in it.
100 -- alex
103 /* linux dlls doesn't need packing */
104 typedef struct /*__attribute__((__packed__))*/ {
105 int samplerate;
106 short bits;
107 short channels;
108 short quality;
109 /* 2bytes padding here, by gcc */
110 int bits_per_frame;
111 int packetsize;
112 int extradata_len;
113 void* extradata;
114 } ra_init_t;
116 /* windows dlls need packed structs (no padding) */
117 typedef struct __attribute__((__packed__)) {
118 int samplerate;
119 short bits;
120 short channels;
121 short quality;
122 int bits_per_frame;
123 int packetsize;
124 int extradata_len;
125 void* extradata;
126 } wra_init_t;
127 #endif
129 #ifdef HAVE_LIBDL
130 static int load_syms_linux(char *path)
132 void *handle;
134 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
135 handle = dlopen(path, RTLD_LAZY);
136 if (!handle)
138 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
139 return 0;
142 raCloseCodec = dlsym(handle, "RACloseCodec");
143 raDecode = dlsym(handle, "RADecode");
144 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
145 raOpenCodec = dlsym(handle, "RAOpenCodec");
146 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
147 raInitDecoder = dlsym(handle, "RAInitDecoder");
148 raSetFlavor = dlsym(handle, "RASetFlavor");
149 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
150 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
152 if (raCloseCodec && raDecode && raFreeDecoder &&
153 (raOpenCodec||raOpenCodec2) && raSetFlavor &&
154 /*raSetDLLAccessPath &&*/ raInitDecoder)
156 rv_handle = handle;
157 return 1;
160 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
161 dlclose(handle);
162 return 0;
164 #endif
166 #ifdef CONFIG_WIN32DLL
168 #ifdef WIN32_LOADER
169 #include "loader/ldt_keeper.h"
170 #endif
171 void* WINAPI LoadLibraryA(char* name);
172 void* WINAPI GetProcAddress(void* handle,char *func);
173 int WINAPI FreeLibrary(void *handle);
175 static int load_syms_windows(char *path)
177 void *handle;
179 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
180 #ifdef WIN32_LOADER
181 Setup_LDT_Keeper();
182 #endif
183 handle = LoadLibraryA(path);
184 if (!handle)
186 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
187 return 0;
190 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
191 wraDecode = GetProcAddress(handle, "RADecode");
192 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
193 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
194 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
195 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
196 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
197 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
198 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
200 if (wraCloseCodec && wraDecode && wraFreeDecoder &&
201 (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
202 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
204 rv_handle = handle;
205 dll_type = 1;
206 return 1;
209 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
210 FreeLibrary(handle);
211 return 0;
214 #endif
217 static int preinit(sh_audio_t *sh){
218 // let's check if the driver is available, return 0 if not.
219 // (you should do that if you use external lib(s) which is optional)
220 unsigned int result;
221 char *path;
223 path = malloc(strlen(BINARY_CODECS_PATH) + strlen(sh->codec->dll) + 2);
224 if (!path) return 0;
225 sprintf(path, BINARY_CODECS_PATH "/%s", sh->codec->dll);
227 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
228 then try to load the windows ones */
230 #ifdef HAVE_LIBDL
231 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
232 #endif
233 #ifdef CONFIG_WIN32DLL
234 if (!load_syms_windows(sh->codec->dll))
235 #endif
237 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "ERROR: Could not open required DirectShow codec %s.\n", sh->codec->dll);
238 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
239 free(path);
240 return 0;
243 #ifdef CONFIG_WIN32DLL
244 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
245 #else
246 if(raSetDLLAccessPath){
247 #endif
248 // used by 'SIPR'
249 path = realloc(path, strlen(BINARY_CODECS_PATH) + 13);
250 sprintf(path, "DT_Codecs=" BINARY_CODECS_PATH);
251 if(path[strlen(path)-1]!='/'){
252 path[strlen(path)+1]=0;
253 path[strlen(path)]='/';
255 path[strlen(path)+1]=0;
256 #ifdef CONFIG_WIN32DLL
257 if (dll_type == 1)
259 int i;
260 for (i=0; i < strlen(path); i++)
261 if (path[i] == '/') path[i] = '\\';
262 wraSetDLLAccessPath(path);
264 else
265 #endif
266 raSetDLLAccessPath(path);
269 #ifdef CONFIG_WIN32DLL
270 if (dll_type == 1){
271 if(wraOpenCodec2)
272 result = wraOpenCodec2(&sh->context, BINARY_CODECS_PATH "\\");
273 else
274 result=wraOpenCodec(&sh->context);
275 } else
276 #endif
277 if(raOpenCodec2)
278 result = raOpenCodec2(&sh->context, BINARY_CODECS_PATH "/");
279 else
280 result=raOpenCodec(&sh->context);
281 if(result){
282 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
283 return 0;
285 // printf("opencodec ok (result: %x)\n", result);
286 free(path); /* after this it isn't used anymore */
288 sh->samplerate=sh->wf->nSamplesPerSec;
289 sh->samplesize=sh->wf->wBitsPerSample/8;
290 sh->channels=sh->wf->nChannels;
293 ra_init_t init_data={
294 sh->wf->nSamplesPerSec,
295 sh->wf->wBitsPerSample,
296 sh->wf->nChannels,
297 100, // quality
298 sh->wf->nBlockAlign, // subpacket size
299 sh->wf->nBlockAlign, // coded frame size
300 sh->wf->cbSize, // codec data length
301 (char*)(sh->wf+1) // extras
303 #ifdef CONFIG_WIN32DLL
304 wra_init_t winit_data={
305 sh->wf->nSamplesPerSec,
306 sh->wf->wBitsPerSample,
307 sh->wf->nChannels,
308 100, // quality
309 sh->wf->nBlockAlign, // subpacket size
310 sh->wf->nBlockAlign, // coded frame size
311 sh->wf->cbSize, // codec data length
312 (char*)(sh->wf+1) // extras
314 #endif
315 #ifdef CONFIG_WIN32DLL
316 if (dll_type == 1)
317 result=wraInitDecoder(sh->context,&winit_data);
318 else
319 #endif
320 result=raInitDecoder(sh->context,&init_data);
322 if(result){
323 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
324 return 0;
326 // printf("initdecoder ok (result: %x)\n", result);
329 #ifdef CONFIG_WIN32DLL
330 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
331 #else
332 if(raSetPwd){
333 #endif
334 // used by 'SIPR'
335 #ifdef CONFIG_WIN32DLL
336 if (dll_type == 1)
337 wraSetPwd(sh->context,"Ardubancel Quazanga");
338 else
339 #endif
340 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
343 if (sh->format == mmioFOURCC('s','i','p','r')) {
344 short flavor;
346 if (sh->wf->nAvgBytesPerSec > 1531)
347 flavor = 3;
348 else if (sh->wf->nAvgBytesPerSec > 937)
349 flavor = 1;
350 else if (sh->wf->nAvgBytesPerSec > 719)
351 flavor = 0;
352 else
353 flavor = 2;
354 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
356 #ifdef CONFIG_WIN32DLL
357 if (dll_type == 1)
358 result=wraSetFlavor(sh->context,flavor);
359 else
360 #endif
361 result=raSetFlavor(sh->context,flavor);
362 if(result){
363 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
364 return 0;
366 } // sipr flavor
368 sh->i_bps=sh->wf->nAvgBytesPerSec;
370 sh->audio_out_minsize=128000; // no idea how to get... :(
371 sh->audio_in_minsize = sh->wf->nBlockAlign;
373 return 1; // return values: 1=OK 0=ERROR
376 static int init(sh_audio_t *sh_audio){
377 // initialize the decoder, set tables etc...
379 // you can store HANDLE or private struct pointer at sh->context
380 // you can access WAVEFORMATEX header at sh->wf
382 // set sample format/rate parameters if you didn't do it in preinit() yet.
384 return 1; // return values: 1=OK 0=ERROR
387 static void uninit(sh_audio_t *sh){
388 // uninit the decoder etc...
389 // again: you don't have to free() a_in_buffer here! it's done by the core.
390 #ifdef CONFIG_WIN32DLL
391 if (dll_type == 1)
393 if (wraFreeDecoder) wraFreeDecoder(sh->context);
394 if (wraCloseCodec) wraCloseCodec(sh->context);
396 #endif
398 if (raFreeDecoder) raFreeDecoder(sh->context);
399 if (raCloseCodec) raCloseCodec(sh->context);
402 #ifdef CONFIG_WIN32DLL
403 if (dll_type == 1)
405 if (rv_handle) FreeLibrary(rv_handle);
406 } else
407 #endif
408 // this dlclose() causes some memory corruption, and crashes soon (in caller):
409 // if (rv_handle) dlclose(rv_handle);
410 rv_handle = NULL;
413 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
414 int result;
415 int len=-1;
417 if(sh->a_in_buffer_len<=0){
418 // fill the buffer!
419 if (sh->ds->eof)
420 return 0;
421 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
422 sh->a_in_buffer_size=
423 sh->a_in_buffer_len=sh->wf->nBlockAlign;
426 #ifdef CONFIG_WIN32DLL
427 if (dll_type == 1)
428 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
429 buf, &len, -1);
430 else
431 #endif
432 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
433 buf, &len, -1);
434 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
436 // printf("radecode: %d bytes, res=0x%X \n",len,result);
438 return len; // return value: number of _bytes_ written to output buffer,
439 // or -1 for EOF (or uncorrectable error)
442 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
443 // various optional functions you MAY implement:
444 switch(cmd){
445 case ADCTRL_RESYNC_STREAM:
446 // it is called once after seeking, to resync.
447 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
448 return CONTROL_TRUE;
449 case ADCTRL_SKIP_FRAME:
450 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
451 // of audio data - used to sync audio to video after seeking
452 // if you don't return CONTROL_TRUE, it will defaults to:
453 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
454 return CONTROL_TRUE;
456 return CONTROL_UNKNOWN;