mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / ad_realaud.c
blob3f221c61ef3f120d132a4293168f0d1133bbfcbe
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 "path.h"
30 #include "libavutil/attributes.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 void *__builtin_new(unsigned long size);
45 void __builtin_delete(void *ize);
47 void *__builtin_new(unsigned long size) {
48 return malloc(size);
51 // required for cook's uninit:
52 void __builtin_delete(void* ize) {
53 free(ize);
56 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
57 void *__ctype_b=NULL;
58 #endif
60 static unsigned long (*raCloseCodec)(void*);
61 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
62 static unsigned long (*raFreeDecoder)(void*);
63 //static unsigned long (*raGetNumberOfFlavors2)(void);
64 static unsigned long (*raInitDecoder)(void*, void*);
65 static unsigned long (*raOpenCodec)(void*);
66 static unsigned long (*raOpenCodec2)(void*, void*);
67 static unsigned long (*raSetFlavor)(void*,unsigned long);
68 static void (*raSetDLLAccessPath)(char*);
69 static void (*raSetPwd)(char*,char*);
70 #ifdef CONFIG_WIN32DLL
71 static unsigned long WINAPI (*wraCloseCodec)(void*);
72 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
73 static unsigned long WINAPI (*wraFreeDecoder)(void*);
74 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
75 static unsigned long WINAPI (*wraOpenCodec)(void*);
76 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
77 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
78 static void WINAPI (*wraSetDLLAccessPath)(char*);
79 static void WINAPI (*wraSetPwd)(char*,char*);
81 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
82 #endif
84 static void *rv_handle = NULL;
86 #if 0
87 typedef struct {
88 int samplerate;
89 short bits;
90 short channels;
91 int unk1;
92 int unk2;
93 int packetsize;
94 int unk3;
95 void* unk4;
96 } ra_init_t ;
97 #else
100 Probably the linux .so-s were compiled with old GCC without setting
101 packing, so it adds 2 bytes padding after the quality field.
102 In windows it seems that there's no padding in it.
104 -- alex
107 /* linux dlls doesn't need packing */
108 typedef struct /*__attribute__((__packed__))*/ {
109 int samplerate;
110 short bits;
111 short channels;
112 short quality;
113 /* 2bytes padding here, by gcc */
114 int bits_per_frame;
115 int packetsize;
116 int extradata_len;
117 void* extradata;
118 } ra_init_t;
120 /* windows dlls need packed structs (no padding) */
121 typedef struct __attribute__((__packed__)) {
122 int samplerate;
123 short bits;
124 short channels;
125 short quality;
126 int bits_per_frame;
127 int packetsize;
128 int extradata_len;
129 void* extradata;
130 } wra_init_t;
131 #endif
133 #ifdef HAVE_LIBDL
134 static int load_syms_linux(char *path)
136 void *handle;
138 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
139 handle = dlopen(path, RTLD_LAZY);
140 if (!handle)
142 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
143 return 0;
146 raCloseCodec = dlsym(handle, "RACloseCodec");
147 raDecode = dlsym(handle, "RADecode");
148 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
149 raOpenCodec = dlsym(handle, "RAOpenCodec");
150 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
151 raInitDecoder = dlsym(handle, "RAInitDecoder");
152 raSetFlavor = dlsym(handle, "RASetFlavor");
153 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
154 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
156 if (raCloseCodec && raDecode && raFreeDecoder &&
157 (raOpenCodec||raOpenCodec2) && raSetFlavor &&
158 /*raSetDLLAccessPath &&*/ raInitDecoder)
160 rv_handle = handle;
161 return 1;
164 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
165 dlclose(handle);
166 return 0;
168 #endif
170 #ifdef CONFIG_WIN32DLL
172 #ifdef WIN32_LOADER
173 #include "loader/ldt_keeper.h"
174 #endif
175 void* WINAPI LoadLibraryA(char* name);
176 void* WINAPI GetProcAddress(void* handle,char *func);
177 int WINAPI FreeLibrary(void *handle);
179 static int load_syms_windows(char *path)
181 void *handle;
183 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
184 #ifdef WIN32_LOADER
185 Setup_LDT_Keeper();
186 #endif
187 handle = LoadLibraryA(path);
188 if (!handle)
190 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
191 return 0;
194 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
195 wraDecode = GetProcAddress(handle, "RADecode");
196 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
197 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
198 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
199 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
200 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
201 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
202 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
204 if (wraCloseCodec && wraDecode && wraFreeDecoder &&
205 (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
206 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
208 rv_handle = handle;
209 dll_type = 1;
210 return 1;
213 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
214 FreeLibrary(handle);
215 return 0;
218 #endif
221 static int preinit(sh_audio_t *sh){
222 // let's check if the driver is available, return 0 if not.
223 // (you should do that if you use external lib(s) which is optional)
224 unsigned int result;
225 char *path;
227 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
228 if (!path) return 0;
229 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
231 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
232 then try to load the windows ones */
234 #ifdef HAVE_LIBDL
235 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
236 #endif
237 #ifdef CONFIG_WIN32DLL
238 if (!load_syms_windows(sh->codec->dll))
239 #endif
241 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "ERROR: Could not open required DirectShow codec %s.\n", sh->codec->dll);
242 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
243 free(path);
244 return 0;
247 #ifdef CONFIG_WIN32DLL
248 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
249 #else
250 if(raSetDLLAccessPath){
251 #endif
252 // used by 'SIPR'
253 path = realloc(path, strlen(codec_path) + 13);
254 sprintf(path, "DT_Codecs=%s", codec_path);
255 if(path[strlen(path)-1]!='/'){
256 path[strlen(path)+1]=0;
257 path[strlen(path)]='/';
259 path[strlen(path)+1]=0;
260 #ifdef CONFIG_WIN32DLL
261 if (dll_type == 1)
263 int i;
264 for (i=0; i < strlen(path); i++)
265 if (path[i] == '/') path[i] = '\\';
266 wraSetDLLAccessPath(path);
268 else
269 #endif
270 raSetDLLAccessPath(path);
273 #ifdef CONFIG_WIN32DLL
274 if (dll_type == 1){
275 if (wraOpenCodec2) {
276 sprintf(path, "%s\\", codec_path);
277 result = wraOpenCodec2(&sh->context, path);
278 } else
279 result=wraOpenCodec(&sh->context);
280 } else
281 #endif
282 if (raOpenCodec2) {
283 sprintf(path, "%s/", codec_path);
284 result = raOpenCodec2(&sh->context, path);
285 } else
286 result=raOpenCodec(&sh->context);
287 if(result){
288 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
289 return 0;
291 // printf("opencodec ok (result: %x)\n", result);
292 free(path); /* after this it isn't used anymore */
294 sh->samplerate=sh->wf->nSamplesPerSec;
295 sh->samplesize=sh->wf->wBitsPerSample/8;
296 sh->channels=sh->wf->nChannels;
299 ra_init_t init_data={
300 sh->wf->nSamplesPerSec,
301 sh->wf->wBitsPerSample,
302 sh->wf->nChannels,
303 100, // quality
304 sh->wf->nBlockAlign, // subpacket size
305 sh->wf->nBlockAlign, // coded frame size
306 sh->wf->cbSize, // codec data length
307 (char*)(sh->wf+1) // extras
309 #ifdef CONFIG_WIN32DLL
310 wra_init_t winit_data={
311 sh->wf->nSamplesPerSec,
312 sh->wf->wBitsPerSample,
313 sh->wf->nChannels,
314 100, // quality
315 sh->wf->nBlockAlign, // subpacket size
316 sh->wf->nBlockAlign, // coded frame size
317 sh->wf->cbSize, // codec data length
318 (char*)(sh->wf+1) // extras
320 #endif
321 #ifdef CONFIG_WIN32DLL
322 if (dll_type == 1)
323 result=wraInitDecoder(sh->context,&winit_data);
324 else
325 #endif
326 result=raInitDecoder(sh->context,&init_data);
328 if(result){
329 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
330 return 0;
332 // printf("initdecoder ok (result: %x)\n", result);
335 #ifdef CONFIG_WIN32DLL
336 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
337 #else
338 if(raSetPwd){
339 #endif
340 // used by 'SIPR'
341 #ifdef CONFIG_WIN32DLL
342 if (dll_type == 1)
343 wraSetPwd(sh->context,"Ardubancel Quazanga");
344 else
345 #endif
346 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
349 if (sh->format == mmioFOURCC('s','i','p','r')) {
350 short flavor;
352 if (sh->wf->nAvgBytesPerSec > 1531)
353 flavor = 3;
354 else if (sh->wf->nAvgBytesPerSec > 937)
355 flavor = 1;
356 else if (sh->wf->nAvgBytesPerSec > 719)
357 flavor = 0;
358 else
359 flavor = 2;
360 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
362 #ifdef CONFIG_WIN32DLL
363 if (dll_type == 1)
364 result=wraSetFlavor(sh->context,flavor);
365 else
366 #endif
367 result=raSetFlavor(sh->context,flavor);
368 if(result){
369 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
370 return 0;
372 } // sipr flavor
374 sh->i_bps=sh->wf->nAvgBytesPerSec;
376 sh->audio_out_minsize=128000; // no idea how to get... :(
377 sh->audio_in_minsize = sh->wf->nBlockAlign;
379 return 1; // return values: 1=OK 0=ERROR
382 static int init(sh_audio_t *sh_audio){
383 // initialize the decoder, set tables etc...
385 // you can store HANDLE or private struct pointer at sh->context
386 // you can access WAVEFORMATEX header at sh->wf
388 // set sample format/rate parameters if you didn't do it in preinit() yet.
390 return 1; // return values: 1=OK 0=ERROR
393 static void uninit(sh_audio_t *sh){
394 // uninit the decoder etc...
395 // again: you don't have to free() a_in_buffer here! it's done by the core.
396 #ifdef CONFIG_WIN32DLL
397 if (dll_type == 1)
399 if (wraFreeDecoder) wraFreeDecoder(sh->context);
400 if (wraCloseCodec) wraCloseCodec(sh->context);
402 #endif
404 if (raFreeDecoder) raFreeDecoder(sh->context);
405 if (raCloseCodec) raCloseCodec(sh->context);
408 #ifdef CONFIG_WIN32DLL
409 if (dll_type == 1)
411 if (rv_handle) FreeLibrary(rv_handle);
412 } else
413 #endif
414 // this dlclose() causes some memory corruption, and crashes soon (in caller):
415 // if (rv_handle) dlclose(rv_handle);
416 rv_handle = NULL;
419 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
420 int result av_unused;
421 int len=-1;
423 if(sh->a_in_buffer_len<=0){
424 // fill the buffer!
425 if (sh->ds->eof)
426 return 0;
427 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
428 sh->a_in_buffer_size=
429 sh->a_in_buffer_len=sh->wf->nBlockAlign;
432 #ifdef CONFIG_WIN32DLL
433 if (dll_type == 1)
434 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
435 buf, &len, -1);
436 else
437 #endif
438 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
439 buf, &len, -1);
440 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
442 // printf("radecode: %d bytes, res=0x%X \n",len,result);
444 return len; // return value: number of _bytes_ written to output buffer,
445 // or -1 for EOF (or uncorrectable error)
448 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
449 // various optional functions you MAY implement:
450 switch(cmd){
451 case ADCTRL_RESYNC_STREAM:
452 // it is called once after seeking, to resync.
453 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
454 return CONTROL_TRUE;
455 case ADCTRL_SKIP_FRAME:
456 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
457 // of audio data - used to sync audio to video after seeking
458 // if you don't return CONTROL_TRUE, it will defaults to:
459 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
460 return CONTROL_TRUE;
462 return CONTROL_UNKNOWN;