find_subfiles: try to determine if a .sub file is text or vobsub
[mplayer/greg.git] / libmpcodecs / ad_realaud.c
blob0b7fb219f78553686ab76349366f286cb5dcb951
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"
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 return malloc(size);
48 // required for cook's uninit:
49 void __builtin_delete(void* ize) {
50 free(ize);
53 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
54 void *__ctype_b=NULL;
55 #endif
57 static unsigned long (*raCloseCodec)(void*);
58 static unsigned long (*raDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
59 static unsigned long (*raFreeDecoder)(void*);
60 //static unsigned long (*raGetNumberOfFlavors2)(void);
61 static unsigned long (*raInitDecoder)(void*, void*);
62 static unsigned long (*raOpenCodec)(void*);
63 static unsigned long (*raOpenCodec2)(void*, void*);
64 static unsigned long (*raSetFlavor)(void*,unsigned long);
65 static void (*raSetDLLAccessPath)(char*);
66 static void (*raSetPwd)(char*,char*);
67 #ifdef CONFIG_WIN32DLL
68 static unsigned long WINAPI (*wraCloseCodec)(void*);
69 static unsigned long WINAPI (*wraDecode)(void*, char*,unsigned long,char*,unsigned int*,long);
70 static unsigned long WINAPI (*wraFreeDecoder)(void*);
71 static unsigned long WINAPI (*wraInitDecoder)(void*, void*);
72 static unsigned long WINAPI (*wraOpenCodec)(void*);
73 static unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
74 static unsigned long WINAPI (*wraSetFlavor)(void*,unsigned long);
75 static void WINAPI (*wraSetDLLAccessPath)(char*);
76 static void WINAPI (*wraSetPwd)(char*,char*);
78 static int dll_type = 0; /* 0 = unix dlopen, 1 = win32 dll */
79 #endif
81 static void *rv_handle = NULL;
83 #if 0
84 typedef struct {
85 int samplerate;
86 short bits;
87 short channels;
88 int unk1;
89 int unk2;
90 int packetsize;
91 int unk3;
92 void* unk4;
93 } ra_init_t ;
94 #else
97 Probably the linux .so-s were compiled with old GCC without setting
98 packing, so it adds 2 bytes padding after the quality field.
99 In windows it seems that there's no padding in it.
101 -- alex
104 /* linux dlls doesn't need packing */
105 typedef struct /*__attribute__((__packed__))*/ {
106 int samplerate;
107 short bits;
108 short channels;
109 short quality;
110 /* 2bytes padding here, by gcc */
111 int bits_per_frame;
112 int packetsize;
113 int extradata_len;
114 void* extradata;
115 } ra_init_t;
117 /* windows dlls need packed structs (no padding) */
118 typedef struct __attribute__((__packed__)) {
119 int samplerate;
120 short bits;
121 short channels;
122 short quality;
123 int bits_per_frame;
124 int packetsize;
125 int extradata_len;
126 void* extradata;
127 } wra_init_t;
128 #endif
130 #ifdef HAVE_LIBDL
131 static int load_syms_linux(char *path)
133 void *handle;
135 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening shared obj '%s'\n", path);
136 handle = dlopen(path, RTLD_LAZY);
137 if (!handle)
139 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error: %s\n", dlerror());
140 return 0;
143 raCloseCodec = dlsym(handle, "RACloseCodec");
144 raDecode = dlsym(handle, "RADecode");
145 raFreeDecoder = dlsym(handle, "RAFreeDecoder");
146 raOpenCodec = dlsym(handle, "RAOpenCodec");
147 raOpenCodec2 = dlsym(handle, "RAOpenCodec2");
148 raInitDecoder = dlsym(handle, "RAInitDecoder");
149 raSetFlavor = dlsym(handle, "RASetFlavor");
150 raSetDLLAccessPath = dlsym(handle, "SetDLLAccessPath");
151 raSetPwd = dlsym(handle, "RASetPwd"); // optional, used by SIPR
153 if (raCloseCodec && raDecode && raFreeDecoder &&
154 (raOpenCodec||raOpenCodec2) && raSetFlavor &&
155 /*raSetDLLAccessPath &&*/ raInitDecoder)
157 rv_handle = handle;
158 return 1;
161 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
162 dlclose(handle);
163 return 0;
165 #endif
167 #ifdef CONFIG_WIN32DLL
169 #ifdef WIN32_LOADER
170 #include "loader/ldt_keeper.h"
171 #endif
172 void* WINAPI LoadLibraryA(char* name);
173 void* WINAPI GetProcAddress(void* handle,char *func);
174 int WINAPI FreeLibrary(void *handle);
176 static int load_syms_windows(char *path)
178 void *handle;
180 mp_msg(MSGT_DECVIDEO, MSGL_V, "opening win32 dll '%s'\n", path);
181 #ifdef WIN32_LOADER
182 Setup_LDT_Keeper();
183 #endif
184 handle = LoadLibraryA(path);
185 if (!handle)
187 mp_msg(MSGT_DECVIDEO, MSGL_WARN, "Error loading dll\n");
188 return 0;
191 wraCloseCodec = GetProcAddress(handle, "RACloseCodec");
192 wraDecode = GetProcAddress(handle, "RADecode");
193 wraFreeDecoder = GetProcAddress(handle, "RAFreeDecoder");
194 wraOpenCodec = GetProcAddress(handle, "RAOpenCodec");
195 wraOpenCodec2 = GetProcAddress(handle, "RAOpenCodec2");
196 wraInitDecoder = GetProcAddress(handle, "RAInitDecoder");
197 wraSetFlavor = GetProcAddress(handle, "RASetFlavor");
198 wraSetDLLAccessPath = GetProcAddress(handle, "SetDLLAccessPath");
199 wraSetPwd = GetProcAddress(handle, "RASetPwd"); // optional, used by SIPR
201 if (wraCloseCodec && wraDecode && wraFreeDecoder &&
202 (wraOpenCodec || wraOpenCodec2) && wraSetFlavor &&
203 /*wraSetDLLAccessPath &&*/ wraInitDecoder)
205 rv_handle = handle;
206 dll_type = 1;
207 return 1;
210 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Cannot resolve symbols - incompatible dll: %s\n",path);
211 FreeLibrary(handle);
212 return 0;
215 #endif
218 static int preinit(sh_audio_t *sh){
219 // let's check if the driver is available, return 0 if not.
220 // (you should do that if you use external lib(s) which is optional)
221 unsigned int result;
222 char *path;
224 path = malloc(strlen(codec_path) + strlen(sh->codec->dll) + 2);
225 if (!path) return 0;
226 sprintf(path, "%s/%s", codec_path, sh->codec->dll);
228 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
229 then try to load the windows ones */
231 #ifdef HAVE_LIBDL
232 if (strstr(sh->codec->dll,".dll") || !load_syms_linux(path))
233 #endif
234 #ifdef CONFIG_WIN32DLL
235 if (!load_syms_windows(sh->codec->dll))
236 #endif
238 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "ERROR: Could not open required DirectShow codec %s.\n", sh->codec->dll);
239 mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Read the RealAudio section of the DOCS!\n");
240 free(path);
241 return 0;
244 #ifdef CONFIG_WIN32DLL
245 if((raSetDLLAccessPath && dll_type == 0) || (wraSetDLLAccessPath && dll_type == 1)){
246 #else
247 if(raSetDLLAccessPath){
248 #endif
249 // used by 'SIPR'
250 path = realloc(path, strlen(codec_path) + 13);
251 sprintf(path, "DT_Codecs=%s", codec_path);
252 if(path[strlen(path)-1]!='/'){
253 path[strlen(path)+1]=0;
254 path[strlen(path)]='/';
256 path[strlen(path)+1]=0;
257 #ifdef CONFIG_WIN32DLL
258 if (dll_type == 1)
260 int i;
261 for (i=0; i < strlen(path); i++)
262 if (path[i] == '/') path[i] = '\\';
263 wraSetDLLAccessPath(path);
265 else
266 #endif
267 raSetDLLAccessPath(path);
270 #ifdef CONFIG_WIN32DLL
271 if (dll_type == 1){
272 if (wraOpenCodec2) {
273 sprintf(path, "%s\\", codec_path);
274 result = wraOpenCodec2(&sh->context, path);
275 } else
276 result=wraOpenCodec(&sh->context);
277 } else
278 #endif
279 if (raOpenCodec2) {
280 sprintf(path, "%s/", codec_path);
281 result = raOpenCodec2(&sh->context, path);
282 } else
283 result=raOpenCodec(&sh->context);
284 if(result){
285 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder open failed, error code: 0x%X\n",result);
286 return 0;
288 // printf("opencodec ok (result: %x)\n", result);
289 free(path); /* after this it isn't used anymore */
291 sh->samplerate=sh->wf->nSamplesPerSec;
292 sh->samplesize=sh->wf->wBitsPerSample/8;
293 sh->channels=sh->wf->nChannels;
296 ra_init_t init_data={
297 sh->wf->nSamplesPerSec,
298 sh->wf->wBitsPerSample,
299 sh->wf->nChannels,
300 100, // quality
301 sh->wf->nBlockAlign, // subpacket size
302 sh->wf->nBlockAlign, // coded frame size
303 sh->wf->cbSize, // codec data length
304 (char*)(sh->wf+1) // extras
306 #ifdef CONFIG_WIN32DLL
307 wra_init_t winit_data={
308 sh->wf->nSamplesPerSec,
309 sh->wf->wBitsPerSample,
310 sh->wf->nChannels,
311 100, // quality
312 sh->wf->nBlockAlign, // subpacket size
313 sh->wf->nBlockAlign, // coded frame size
314 sh->wf->cbSize, // codec data length
315 (char*)(sh->wf+1) // extras
317 #endif
318 #ifdef CONFIG_WIN32DLL
319 if (dll_type == 1)
320 result=wraInitDecoder(sh->context,&winit_data);
321 else
322 #endif
323 result=raInitDecoder(sh->context,&init_data);
325 if(result){
326 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder init failed, error code: 0x%X\n",result);
327 return 0;
329 // printf("initdecoder ok (result: %x)\n", result);
332 #ifdef CONFIG_WIN32DLL
333 if((raSetPwd && dll_type == 0) || (wraSetPwd && dll_type == 1)){
334 #else
335 if(raSetPwd){
336 #endif
337 // used by 'SIPR'
338 #ifdef CONFIG_WIN32DLL
339 if (dll_type == 1)
340 wraSetPwd(sh->context,"Ardubancel Quazanga");
341 else
342 #endif
343 raSetPwd(sh->context,"Ardubancel Quazanga"); // set password... lol.
346 if (sh->format == mmioFOURCC('s','i','p','r')) {
347 short flavor;
349 if (sh->wf->nAvgBytesPerSec > 1531)
350 flavor = 3;
351 else if (sh->wf->nAvgBytesPerSec > 937)
352 flavor = 1;
353 else if (sh->wf->nAvgBytesPerSec > 719)
354 flavor = 0;
355 else
356 flavor = 2;
357 mp_msg(MSGT_DECAUDIO,MSGL_V,"Got sipr flavor %d from bitrate %d\n",flavor, sh->wf->nAvgBytesPerSec);
359 #ifdef CONFIG_WIN32DLL
360 if (dll_type == 1)
361 result=wraSetFlavor(sh->context,flavor);
362 else
363 #endif
364 result=raSetFlavor(sh->context,flavor);
365 if(result){
366 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Decoder flavor setup failed, error code: 0x%X\n",result);
367 return 0;
369 } // sipr flavor
371 sh->i_bps=sh->wf->nAvgBytesPerSec;
373 sh->audio_out_minsize=128000; // no idea how to get... :(
374 sh->audio_in_minsize = sh->wf->nBlockAlign;
376 return 1; // return values: 1=OK 0=ERROR
379 static int init(sh_audio_t *sh_audio){
380 // initialize the decoder, set tables etc...
382 // you can store HANDLE or private struct pointer at sh->context
383 // you can access WAVEFORMATEX header at sh->wf
385 // set sample format/rate parameters if you didn't do it in preinit() yet.
387 return 1; // return values: 1=OK 0=ERROR
390 static void uninit(sh_audio_t *sh){
391 // uninit the decoder etc...
392 // again: you don't have to free() a_in_buffer here! it's done by the core.
393 #ifdef CONFIG_WIN32DLL
394 if (dll_type == 1)
396 if (wraFreeDecoder) wraFreeDecoder(sh->context);
397 if (wraCloseCodec) wraCloseCodec(sh->context);
399 #endif
401 if (raFreeDecoder) raFreeDecoder(sh->context);
402 if (raCloseCodec) raCloseCodec(sh->context);
405 #ifdef CONFIG_WIN32DLL
406 if (dll_type == 1)
408 if (rv_handle) FreeLibrary(rv_handle);
409 } else
410 #endif
411 // this dlclose() causes some memory corruption, and crashes soon (in caller):
412 // if (rv_handle) dlclose(rv_handle);
413 rv_handle = NULL;
416 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
417 int result;
418 int len=-1;
420 if(sh->a_in_buffer_len<=0){
421 // fill the buffer!
422 if (sh->ds->eof)
423 return 0;
424 demux_read_data(sh->ds, sh->a_in_buffer, sh->wf->nBlockAlign);
425 sh->a_in_buffer_size=
426 sh->a_in_buffer_len=sh->wf->nBlockAlign;
429 #ifdef CONFIG_WIN32DLL
430 if (dll_type == 1)
431 result=wraDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
432 buf, &len, -1);
433 else
434 #endif
435 result=raDecode(sh->context, sh->a_in_buffer+sh->a_in_buffer_size-sh->a_in_buffer_len, sh->wf->nBlockAlign,
436 buf, &len, -1);
437 sh->a_in_buffer_len-=sh->wf->nBlockAlign;
439 // printf("radecode: %d bytes, res=0x%X \n",len,result);
441 return len; // return value: number of _bytes_ written to output buffer,
442 // or -1 for EOF (or uncorrectable error)
445 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
446 // various optional functions you MAY implement:
447 switch(cmd){
448 case ADCTRL_RESYNC_STREAM:
449 // it is called once after seeking, to resync.
450 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
451 return CONTROL_TRUE;
452 case ADCTRL_SKIP_FRAME:
453 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
454 // of audio data - used to sync audio to video after seeking
455 // if you don't return CONTROL_TRUE, it will defaults to:
456 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
457 return CONTROL_TRUE;
459 return CONTROL_UNKNOWN;