loader.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // Dynamically loads the vorbis file shared library / dll
  3. //
  4. #include "loader.h"
  5. typedef void (*alProc)(void);
  6. //
  7. // Windows --------------------------------------------------------------------
  8. //
  9. #ifdef _WIN32
  10. #define WIN32_LEAN_AND_MEAN 1
  11. #include <windows.h>
  12. static HMODULE libvbf;
  13. static int open_libvbf(void) {
  14. libvbf = LoadLibraryA("libvorbisfile.dll");
  15. if (libvbf == NULL) {
  16. return -1;
  17. }
  18. return 0;
  19. }
  20. static void close_libvbf(void) {
  21. FreeLibrary(libvbf);
  22. }
  23. static alProc get_proc(const char *proc) {
  24. return (alProc) GetProcAddress(libvbf, proc);
  25. }
  26. //
  27. // Mac --------------------------------------------------------------------
  28. //
  29. #elif defined(__APPLE__)
  30. #include <dlfcn.h>
  31. static void *libvbf;
  32. static int open_libvbf(void) {
  33. libvbf = dlopen("/System/Library/Frameworks/libvorbisfile.framework/libvorbisfile", RTLD_LAZY | RTLD_GLOBAL);
  34. if (!libvbf) {
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. static void close_libvbf(void) {
  40. dlclose(libvbf);
  41. }
  42. static void* get_proc(const char *proc) {
  43. void* res;
  44. *(void **)(&res) = dlsym(libvbf, proc);
  45. return res;
  46. }
  47. //
  48. // Linux --------------------------------------------------------------------
  49. //
  50. #else
  51. #include <dlfcn.h>
  52. static void *libvbf;
  53. static char* lib_names[] = {
  54. "libvorbisfile.so.3",
  55. "libvorbisfile.so",
  56. NULL
  57. };
  58. static int open_libvbf(void) {
  59. int i = 0;
  60. while (lib_names[i] != NULL) {
  61. libvbf = dlopen(lib_names[i], RTLD_LAZY | RTLD_GLOBAL);
  62. if (libvbf != NULL) {
  63. dlerror(); // clear errors
  64. return 0;
  65. }
  66. i++;
  67. }
  68. return -1;
  69. }
  70. static void close_libvbf(void) {
  71. dlclose(libvbf);
  72. }
  73. static alProc get_proc(const char *proc) {
  74. return dlsym(libvbf, proc);
  75. }
  76. #endif
  77. // Prototypes of local functions
  78. static void load_procs(void);
  79. // Pointers to functions loaded from shared library
  80. LPOVCLEAR p_ov_clear;
  81. LPOVFOPEN p_ov_fopen;
  82. LPOVOPEN p_ov_open;
  83. LPOVOPENCALLBACKS p_ov_open_callbacks;
  84. LPOVTEST p_ov_test;
  85. LPOVTESTCALLBACKS p_ov_test_callbacks;
  86. LPOVTESTOPEN p_ov_test_open;
  87. LPOVBITRATE p_ov_bitrate;
  88. LPOVBITRATEINSTANT p_ov_bitrate_instant;
  89. LPOVSTREAMS p_ov_streams;
  90. LPOVSEEKABLE p_ov_seekable;
  91. LPOVSERIALNUMBER p_ov_serialnumber;
  92. LPOVRAWTOTAL p_ov_raw_total;
  93. LPOVPCMTOTAL p_ov_pcm_total;
  94. LPOVTIMETOTAL p_ov_time_total;
  95. LPOVRAWSEEK p_ov_raw_seek;
  96. LPOVPCMSEEK p_ov_pcm_seek;
  97. LPOVPCMSEEKPAGE p_ov_pcm_seek_page;
  98. LPOVTIMESEEK p_ov_time_seek;
  99. LPOVTIMESEEKPAGE p_ov_time_seek_page;
  100. LPOVRAWSEEKLAP p_ov_raw_seek_lap;
  101. LPOVPCMSEEKLAP p_ov_pcm_seek_lap;
  102. LPOVPCMSEEKPAGELAP p_ov_pcm_seek_page_lap;
  103. LPOVTIMESEEKLAP p_ov_time_seek_lap;
  104. LPOVTIMESEEKPAGELAP p_ov_time_seek_page_lap;
  105. LPOVRAWTELL p_ov_raw_tell;
  106. LPOVPCMTELL p_ov_pcm_tell;
  107. LPOVTIMETELL p_ov_time_tell;
  108. LPOVINFO p_ov_info;
  109. LPOVCOMMENT p_ov_comment;
  110. LPOVREADFLOAT p_ov_read_float;
  111. LPOVREADFILTER p_ov_read_filter;
  112. LPOVREAD p_ov_read;
  113. LPOVCROSSLAP p_ov_crosslap;
  114. LPOVHALFRATE p_ov_halfrate;
  115. LPOVHALFRATEP p_ov_halfrate_p;
  116. // Load functions from shared library
  117. int vorbisfile_load() {
  118. int res = open_libvbf();
  119. if (res) {
  120. return res;
  121. }
  122. load_procs();
  123. return 0;
  124. }
  125. // Loads function addresses and store in the pointers
  126. static void load_procs(void) {
  127. p_ov_clear = (LPOVCLEAR)get_proc("ov_clear");
  128. p_ov_fopen = (LPOVFOPEN)get_proc("ov_fopen");
  129. p_ov_open = (LPOVOPEN)get_proc("ov_open");
  130. p_ov_open_callbacks = (LPOVOPENCALLBACKS)get_proc("ov_open_callbacks");
  131. p_ov_test = (LPOVTEST)get_proc("ov_test");
  132. p_ov_test_callbacks = (LPOVTESTCALLBACKS)get_proc("ov_test_callbacks");
  133. p_ov_test_open = (LPOVTESTOPEN)get_proc("ov_test_open");
  134. p_ov_bitrate = (LPOVBITRATE)get_proc("ov_bitrate");
  135. p_ov_bitrate_instant = (LPOVBITRATEINSTANT)get_proc("ov_bitrate_instant");
  136. p_ov_streams = (LPOVSTREAMS)get_proc("ov_streams");
  137. p_ov_seekable = (LPOVSEEKABLE)get_proc("ov_seekable");
  138. p_ov_serialnumber = (LPOVSERIALNUMBER)get_proc("ov_serialnumber");
  139. p_ov_raw_total = (LPOVRAWTOTAL)get_proc("ov_raw_total");
  140. p_ov_pcm_total = (LPOVPCMTOTAL)get_proc("ov_pcm_total");
  141. p_ov_time_total = (LPOVTIMETOTAL)get_proc("ov_time_total");
  142. p_ov_raw_seek = (LPOVRAWSEEK)get_proc("ov_raw_seek");
  143. p_ov_pcm_seek = (LPOVPCMSEEK)get_proc("ov_pcm_seek");
  144. p_ov_pcm_seek_page = (LPOVPCMSEEKPAGE)get_proc("ov_pcm_seek_page");
  145. p_ov_time_seek = (LPOVTIMESEEK)get_proc("ov_time_seek");
  146. p_ov_time_seek_page = (LPOVTIMESEEKPAGE)get_proc("ov_time_seek_page");
  147. p_ov_raw_seek_lap = (LPOVRAWSEEKLAP)get_proc("ov_raw_seek_lap");
  148. p_ov_pcm_seek_lap = (LPOVPCMSEEKLAP)get_proc("ov_pcm_seek_lap");
  149. p_ov_pcm_seek_page_lap = (LPOVPCMSEEKPAGELAP)get_proc("ov_pcm_seek_page_lap");
  150. p_ov_time_seek_lap = (LPOVTIMESEEKLAP)get_proc("ov_time_seek_lap");
  151. p_ov_time_seek_page_lap = (LPOVTIMESEEKPAGELAP)get_proc("ov_time_seek_page_lap");
  152. p_ov_raw_tell = (LPOVRAWTELL)get_proc("ov_raw_tell");
  153. p_ov_pcm_tell = (LPOVPCMTELL)get_proc("ov_pcm_tell");
  154. p_ov_time_tell = (LPOVTIMETELL)get_proc("ov_time_tell");
  155. p_ov_info = (LPOVINFO)get_proc("ov_info");
  156. p_ov_comment = (LPOVCOMMENT)get_proc("ov_comment");
  157. p_ov_read_float = (LPOVREADFLOAT)get_proc("ov_read_float");
  158. p_ov_read_filter = (LPOVREADFILTER)get_proc("ov_read_filter");
  159. p_ov_read = (LPOVREAD)get_proc("ov_read");
  160. p_ov_crosslap = (LPOVCROSSLAP)get_proc("ov_crosslap");
  161. p_ov_halfrate = (LPOVHALFRATE)get_proc("ov_halfrate");
  162. p_ov_halfrate_p = (LPOVHALFRATEP)get_proc("ov_halfrate_p");
  163. }
  164. //
  165. // Go code cannot directly call the vorbis file function pointers loaded dynamically
  166. // The following C functions call the corresponding function pointers and can be
  167. // called by Go code.
  168. //
  169. int ov_clear(OggVorbis_File *vf) {
  170. return p_ov_clear(vf);
  171. }
  172. int ov_fopen(const char *path,OggVorbis_File *vf) {
  173. return p_ov_fopen(path, vf);
  174. }
  175. int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes) {
  176. return ov_open(f, vf, initial, ibytes);
  177. }
  178. int ov_open_callbacks(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks) {
  179. return p_ov_open_callbacks(datasource, vf, initial, ibytes, callbacks);
  180. }
  181. int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes) {
  182. return p_ov_test(f, vf, initial, ibytes);
  183. }
  184. int ov_test_callbacks(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks) {
  185. return p_ov_test_callbacks(datasource, vf, initial, ibytes, callbacks);
  186. }
  187. int ov_test_open(OggVorbis_File *vf) {
  188. return p_ov_test_open(vf);
  189. }
  190. long ov_bitrate(OggVorbis_File *vf,int i) {
  191. return p_ov_bitrate(vf, i);
  192. }
  193. long ov_bitrate_instant(OggVorbis_File *vf) {
  194. return p_ov_bitrate_instant(vf);
  195. }
  196. long ov_streams(OggVorbis_File *vf) {
  197. return p_ov_streams(vf);
  198. }
  199. long ov_seekable(OggVorbis_File *vf) {
  200. return p_ov_seekable(vf);
  201. }
  202. long ov_serialnumber(OggVorbis_File *vf,int i) {
  203. return p_ov_serialnumber(vf, i);
  204. }
  205. ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i) {
  206. return p_ov_raw_total(vf, i);
  207. }
  208. ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i) {
  209. return p_ov_pcm_total(vf, i);
  210. }
  211. double ov_time_total(OggVorbis_File *vf,int i) {
  212. return p_ov_time_total(vf, i);
  213. }
  214. int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos) {
  215. return p_ov_raw_seek(vf, pos);
  216. }
  217. int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos) {
  218. return p_ov_pcm_seek(vf, pos);
  219. }
  220. int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos) {
  221. return p_ov_pcm_seek_page(vf, pos);
  222. }
  223. int ov_time_seek(OggVorbis_File *vf,double pos) {
  224. return p_ov_time_seek(vf, pos);
  225. }
  226. int ov_time_seek_page(OggVorbis_File *vf,double pos) {
  227. return p_ov_time_seek(vf, pos);
  228. }
  229. int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos) {
  230. return p_ov_raw_seek_lap(vf, pos);
  231. }
  232. int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos) {
  233. return p_ov_pcm_seek(vf, pos);
  234. }
  235. int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos) {
  236. return p_ov_pcm_seek_page_lap(vf, pos);
  237. }
  238. int ov_time_seek_lap(OggVorbis_File *vf,double pos) {
  239. return p_ov_time_seek_lap(vf, pos);
  240. }
  241. int ov_time_seek_page_lap(OggVorbis_File *vf,double pos) {
  242. return p_ov_time_seek_page_lap(vf, pos);
  243. }
  244. ogg_int64_t ov_raw_tell(OggVorbis_File *vf) {
  245. return p_ov_raw_tell(vf);
  246. }
  247. ogg_int64_t ov_pcm_tell(OggVorbis_File *vf) {
  248. return p_ov_pcm_tell(vf);
  249. }
  250. double ov_time_tell(OggVorbis_File *vf) {
  251. return p_ov_time_tell(vf);
  252. }
  253. vorbis_info *ov_info(OggVorbis_File *vf,int link) {
  254. return p_ov_info(vf, link);
  255. }
  256. vorbis_comment *ov_comment(OggVorbis_File *vf,int link) {
  257. return p_ov_comment(vf, link);
  258. }
  259. long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples, int *bitstream) {
  260. return p_ov_read_float(vf, pcm_channels, samples, bitstream);
  261. }
  262. long ov_read_filter(OggVorbis_File *vf,char *buffer,int length, int bigendianp,int word,int sgned,int *bitstream,
  263. void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param) {
  264. return p_ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, filter, filter_param);
  265. }
  266. long ov_read(OggVorbis_File *vf,char *buffer,int length, int bigendianp,int word,int sgned,int *bitstream) {
  267. return p_ov_read(vf, buffer, length, bigendianp, word, sgned, bitstream);
  268. }
  269. int ov_crosslap(OggVorbis_File *vf1,OggVorbis_File *vf2) {
  270. return p_ov_crosslap(vf1, vf2);
  271. }
  272. int ov_halfrate(OggVorbis_File *vf,int flag) {
  273. return p_ov_halfrate(vf, flag);
  274. }
  275. int ov_halfrate_p(OggVorbis_File *vf) {
  276. return p_ov_halfrate_p(vf);
  277. }