dynamic_flac.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. Implementation of the dynamic loading functionality for libFLAC.
  18. ~ Austen Dicken ([email protected])
  19. */
  20. #ifdef FLAC_MUSIC
  21. #include "SDL_loadso.h"
  22. #include "SDL_mixer.h"
  23. #include "dynamic_flac.h"
  24. flac_loader flac = {
  25. 0, NULL
  26. };
  27. #ifdef FLAC_DYNAMIC
  28. int Mix_InitFLAC()
  29. {
  30. if ( flac.loaded == 0 ) {
  31. flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
  32. if ( flac.handle == NULL ) {
  33. return -1;
  34. }
  35. flac.FLAC__stream_decoder_new =
  36. (FLAC__StreamDecoder *(*)())
  37. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new");
  38. if ( flac.FLAC__stream_decoder_new == NULL ) {
  39. SDL_UnloadObject(flac.handle);
  40. return -1;
  41. }
  42. flac.FLAC__stream_decoder_delete =
  43. (void (*)(FLAC__StreamDecoder *))
  44. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete");
  45. if ( flac.FLAC__stream_decoder_delete == NULL ) {
  46. SDL_UnloadObject(flac.handle);
  47. return -1;
  48. }
  49. flac.FLAC__stream_decoder_init_stream =
  50. (FLAC__StreamDecoderInitStatus (*)(
  51. FLAC__StreamDecoder *,
  52. FLAC__StreamDecoderReadCallback,
  53. FLAC__StreamDecoderSeekCallback,
  54. FLAC__StreamDecoderTellCallback,
  55. FLAC__StreamDecoderLengthCallback,
  56. FLAC__StreamDecoderEofCallback,
  57. FLAC__StreamDecoderWriteCallback,
  58. FLAC__StreamDecoderMetadataCallback,
  59. FLAC__StreamDecoderErrorCallback,
  60. void *))
  61. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream");
  62. if ( flac.FLAC__stream_decoder_init_stream == NULL ) {
  63. SDL_UnloadObject(flac.handle);
  64. return -1;
  65. }
  66. flac.FLAC__stream_decoder_finish =
  67. (FLAC__bool (*)(FLAC__StreamDecoder *))
  68. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish");
  69. if ( flac.FLAC__stream_decoder_finish == NULL ) {
  70. SDL_UnloadObject(flac.handle);
  71. return -1;
  72. }
  73. flac.FLAC__stream_decoder_flush =
  74. (FLAC__bool (*)(FLAC__StreamDecoder *))
  75. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush");
  76. if ( flac.FLAC__stream_decoder_flush == NULL ) {
  77. SDL_UnloadObject(flac.handle);
  78. return -1;
  79. }
  80. flac.FLAC__stream_decoder_process_single =
  81. (FLAC__bool (*)(FLAC__StreamDecoder *))
  82. SDL_LoadFunction(flac.handle,
  83. "FLAC__stream_decoder_process_single");
  84. if ( flac.FLAC__stream_decoder_process_single == NULL ) {
  85. SDL_UnloadObject(flac.handle);
  86. return -1;
  87. }
  88. flac.FLAC__stream_decoder_process_until_end_of_metadata =
  89. (FLAC__bool (*)(FLAC__StreamDecoder *))
  90. SDL_LoadFunction(flac.handle,
  91. "FLAC__stream_decoder_process_until_end_of_metadata");
  92. if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) {
  93. SDL_UnloadObject(flac.handle);
  94. return -1;
  95. }
  96. flac.FLAC__stream_decoder_process_until_end_of_stream =
  97. (FLAC__bool (*)(FLAC__StreamDecoder *))
  98. SDL_LoadFunction(flac.handle,
  99. "FLAC__stream_decoder_process_until_end_of_stream");
  100. if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) {
  101. SDL_UnloadObject(flac.handle);
  102. return -1;
  103. }
  104. flac.FLAC__stream_decoder_seek_absolute =
  105. (FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
  106. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute");
  107. if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) {
  108. SDL_UnloadObject(flac.handle);
  109. return -1;
  110. }
  111. flac.FLAC__stream_decoder_get_state =
  112. (FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
  113. SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state");
  114. if ( flac.FLAC__stream_decoder_get_state == NULL ) {
  115. SDL_UnloadObject(flac.handle);
  116. return -1;
  117. }
  118. }
  119. ++flac.loaded;
  120. return 0;
  121. }
  122. void Mix_QuitFLAC()
  123. {
  124. if ( flac.loaded == 0 ) {
  125. return;
  126. }
  127. if ( flac.loaded == 1 ) {
  128. SDL_UnloadObject(flac.handle);
  129. }
  130. --flac.loaded;
  131. }
  132. #else
  133. int Mix_InitFLAC()
  134. {
  135. if ( flac.loaded == 0 ) {
  136. #ifdef __MACOSX__
  137. extern FLAC__StreamDecoder *FLAC__stream_decoder_new(void) __attribute__((weak_import));
  138. if ( FLAC__stream_decoder_new == NULL )
  139. {
  140. /* Missing weakly linked framework */
  141. Mix_SetError("Missing FLAC.framework");
  142. return -1;
  143. }
  144. #endif // __MACOSX__
  145. flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
  146. flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
  147. flac.FLAC__stream_decoder_init_stream =
  148. FLAC__stream_decoder_init_stream;
  149. flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish;
  150. flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush;
  151. flac.FLAC__stream_decoder_process_single =
  152. FLAC__stream_decoder_process_single;
  153. flac.FLAC__stream_decoder_process_until_end_of_metadata =
  154. FLAC__stream_decoder_process_until_end_of_metadata;
  155. flac.FLAC__stream_decoder_process_until_end_of_stream =
  156. FLAC__stream_decoder_process_until_end_of_stream;
  157. flac.FLAC__stream_decoder_seek_absolute =
  158. FLAC__stream_decoder_seek_absolute;
  159. flac.FLAC__stream_decoder_get_state =
  160. FLAC__stream_decoder_get_state;
  161. }
  162. ++flac.loaded;
  163. return 0;
  164. }
  165. void Mix_QuitFLAC()
  166. {
  167. if ( flac.loaded == 0 ) {
  168. return;
  169. }
  170. if ( flac.loaded == 1 ) {
  171. }
  172. --flac.loaded;
  173. }
  174. #endif /* FLAC_DYNAMIC */
  175. #endif /* FLAC_MUSIC */