load_flac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. This is the source needed to decode a FLAC into a waveform.
  18. ~ Austen Dicken ([email protected]).
  19. */
  20. #ifdef FLAC_MUSIC
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "SDL_mutex.h"
  25. #include "SDL_endian.h"
  26. #include "SDL_timer.h"
  27. #include "SDL_mixer.h"
  28. #include "dynamic_flac.h"
  29. #include "load_flac.h"
  30. #include <FLAC/stream_decoder.h>
  31. typedef struct {
  32. SDL_RWops* sdl_src;
  33. SDL_AudioSpec* sdl_spec;
  34. Uint8** sdl_audio_buf;
  35. Uint32* sdl_audio_len;
  36. int sdl_audio_read;
  37. FLAC__uint64 flac_total_samples;
  38. unsigned flac_bps;
  39. } FLAC_SDL_Data;
  40. static FLAC__StreamDecoderReadStatus flac_read_load_cb(
  41. const FLAC__StreamDecoder *decoder,
  42. FLAC__byte buffer[],
  43. size_t *bytes,
  44. void *client_data)
  45. {
  46. // make sure there is something to be reading
  47. if (*bytes > 0) {
  48. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  49. *bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte),
  50. *bytes);
  51. if (*bytes == 0) { // error or no data was read (EOF)
  52. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  53. } else { // data was read, continue
  54. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  55. }
  56. } else {
  57. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  58. }
  59. }
  60. static FLAC__StreamDecoderSeekStatus flac_seek_load_cb(
  61. const FLAC__StreamDecoder *decoder,
  62. FLAC__uint64 absolute_byte_offset,
  63. void *client_data)
  64. {
  65. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  66. if (SDL_RWseek (data->sdl_src, absolute_byte_offset, RW_SEEK_SET) < 0) {
  67. return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  68. } else {
  69. return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
  70. }
  71. }
  72. static FLAC__StreamDecoderTellStatus flac_tell_load_cb(
  73. const FLAC__StreamDecoder *decoder,
  74. FLAC__uint64 *absolute_byte_offset,
  75. void *client_data)
  76. {
  77. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  78. Sint64 pos = SDL_RWtell (data->sdl_src);
  79. if (pos < 0) {
  80. return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
  81. } else {
  82. *absolute_byte_offset = (FLAC__uint64)pos;
  83. return FLAC__STREAM_DECODER_TELL_STATUS_OK;
  84. }
  85. }
  86. static FLAC__StreamDecoderLengthStatus flac_length_load_cb(
  87. const FLAC__StreamDecoder *decoder,
  88. FLAC__uint64 *stream_length,
  89. void *client_data)
  90. {
  91. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  92. Sint64 pos = SDL_RWtell (data->sdl_src);
  93. Sint64 length = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
  94. if (SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET) != pos || length < 0) {
  95. /* there was an error attempting to return the stream to the original
  96. * position, or the length was invalid. */
  97. return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
  98. } else {
  99. *stream_length = (FLAC__uint64)length;
  100. return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
  101. }
  102. }
  103. static FLAC__bool flac_eof_load_cb(const FLAC__StreamDecoder *decoder,
  104. void *client_data)
  105. {
  106. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  107. Sint64 pos = SDL_RWtell (data->sdl_src);
  108. Sint64 end = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
  109. // was the original position equal to the end (a.k.a. the seek didn't move)?
  110. if (pos == end) {
  111. // must be EOF
  112. return true;
  113. } else {
  114. // not EOF, return to the original position
  115. SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET);
  116. return false;
  117. }
  118. }
  119. static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
  120. const FLAC__StreamDecoder *decoder,
  121. const FLAC__Frame *frame,
  122. const FLAC__int32 *const buffer[],
  123. void *client_data)
  124. {
  125. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  126. size_t i;
  127. Uint8 *buf;
  128. if (data->flac_total_samples == 0) {
  129. SDL_SetError ("Given FLAC file does not specify its sample count.");
  130. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  131. }
  132. if (data->sdl_spec->channels != 2 || data->flac_bps != 16) {
  133. SDL_SetError ("Current FLAC support is only for 16 bit Stereo files.");
  134. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  135. }
  136. // check if it is the first audio frame so we can initialize the output
  137. // buffer
  138. if (frame->header.number.sample_number == 0) {
  139. *(data->sdl_audio_len) = data->sdl_spec->size;
  140. data->sdl_audio_read = 0;
  141. *(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len));
  142. if (*(data->sdl_audio_buf) == NULL) {
  143. SDL_SetError
  144. ("Unable to allocate memory to store the FLAC stream.");
  145. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  146. }
  147. }
  148. buf = *(data->sdl_audio_buf);
  149. for (i = 0; i < frame->header.blocksize; i++) {
  150. FLAC__int16 i16;
  151. FLAC__uint16 ui16;
  152. i16 = (FLAC__int16)buffer[0][i];
  153. ui16 = (FLAC__uint16)i16;
  154. *(buf + (data->sdl_audio_read++)) = (char)(ui16);
  155. *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
  156. i16 = (FLAC__int16)buffer[1][i];
  157. ui16 = (FLAC__uint16)i16;
  158. *(buf + (data->sdl_audio_read++)) = (char)(ui16);
  159. *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
  160. }
  161. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  162. }
  163. static void flac_metadata_load_cb(
  164. const FLAC__StreamDecoder *decoder,
  165. const FLAC__StreamMetadata *metadata,
  166. void *client_data)
  167. {
  168. FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
  169. FLAC__uint64 total_samples;
  170. unsigned bps;
  171. if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
  172. // save the metadata right now for use later on
  173. *(data->sdl_audio_buf) = NULL;
  174. *(data->sdl_audio_len) = 0;
  175. memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec));
  176. data->sdl_spec->format = AUDIO_S16;
  177. data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate);
  178. data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels);
  179. data->sdl_spec->samples = 8192; /* buffer size */
  180. total_samples = metadata->data.stream_info.total_samples;
  181. bps = metadata->data.stream_info.bits_per_sample;
  182. data->sdl_spec->size = (Uint32)(total_samples * data->sdl_spec->channels * (bps / 8));
  183. data->flac_total_samples = total_samples;
  184. data->flac_bps = bps;
  185. }
  186. }
  187. static void flac_error_load_cb(
  188. const FLAC__StreamDecoder *decoder,
  189. FLAC__StreamDecoderErrorStatus status,
  190. void *client_data)
  191. {
  192. // print an SDL error based on the error status
  193. switch (status) {
  194. case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
  195. SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
  196. break;
  197. case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
  198. SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
  199. break;
  200. case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
  201. SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
  202. break;
  203. case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
  204. SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
  205. break;
  206. default:
  207. SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
  208. break;
  209. }
  210. }
  211. /* don't call this directly; use Mix_LoadWAV_RW() for now. */
  212. SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
  213. SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
  214. {
  215. FLAC__StreamDecoder *decoder = 0;
  216. FLAC__StreamDecoderInitStatus init_status;
  217. int was_error = 1;
  218. int was_init = 0;
  219. Uint32 samplesize;
  220. // create the client data passing information
  221. FLAC_SDL_Data* client_data;
  222. client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data));
  223. if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */
  224. goto done;
  225. if (!Mix_Init(MIX_INIT_FLAC))
  226. goto done;
  227. if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) {
  228. SDL_SetError ("Unable to allocate FLAC decoder.");
  229. goto done;
  230. }
  231. init_status = flac.FLAC__stream_decoder_init_stream (decoder,
  232. flac_read_load_cb, flac_seek_load_cb,
  233. flac_tell_load_cb, flac_length_load_cb,
  234. flac_eof_load_cb, flac_write_load_cb,
  235. flac_metadata_load_cb, flac_error_load_cb,
  236. client_data);
  237. if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
  238. SDL_SetError ("Unable to initialize FLAC stream decoder.");
  239. goto done;
  240. }
  241. was_init = 1;
  242. client_data->sdl_src = src;
  243. client_data->sdl_spec = spec;
  244. client_data->sdl_audio_buf = audio_buf;
  245. client_data->sdl_audio_len = audio_len;
  246. if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) {
  247. SDL_SetError ("Unable to process FLAC file.");
  248. goto done;
  249. }
  250. was_error = 0;
  251. /* Don't return a buffer that isn't a multiple of samplesize */
  252. samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
  253. *audio_len &= ~(samplesize - 1);
  254. done:
  255. if (was_init && decoder) {
  256. flac.FLAC__stream_decoder_finish (decoder);
  257. }
  258. if (decoder) {
  259. flac.FLAC__stream_decoder_delete (decoder);
  260. }
  261. if (freesrc && src) {
  262. SDL_RWclose (src);
  263. }
  264. if (was_error) {
  265. spec = NULL;
  266. }
  267. return spec;
  268. }
  269. #endif // FLAC_MUSIC