BsFLACDecoder.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsFLACDecoder.h"
  4. #include "FileSystem/BsDataStream.h"
  5. namespace bs
  6. {
  7. FLAC__StreamDecoderReadStatus streamRead(const FLAC__StreamDecoder*, FLAC__byte buffer[], size_t* bytes, void* clientData)
  8. {
  9. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  10. INT64 count = (INT64)data->stream->read(buffer, *bytes);
  11. if (count > 0)
  12. {
  13. *bytes = (size_t)count;
  14. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  15. }
  16. if (count == 0)
  17. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  18. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  19. }
  20. FLAC__StreamDecoderSeekStatus streamSeek(const FLAC__StreamDecoder*, FLAC__uint64 absoluteByteOffset, void* clientData)
  21. {
  22. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  23. data->stream->seek(data->streamOffset + (UINT32)absoluteByteOffset);
  24. INT64 position = (INT64)(data->stream->tell() - data->streamOffset);
  25. if (position >= 0)
  26. return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
  27. else
  28. return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  29. }
  30. FLAC__StreamDecoderTellStatus streamTell(const FLAC__StreamDecoder*, FLAC__uint64* absoluteByteOffset, void* clientData)
  31. {
  32. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  33. INT64 position = (INT64)(data->stream->tell() - data->streamOffset);
  34. if (position >= 0)
  35. {
  36. *absoluteByteOffset = position;
  37. return FLAC__STREAM_DECODER_TELL_STATUS_OK;
  38. }
  39. else
  40. {
  41. return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
  42. }
  43. }
  44. FLAC__StreamDecoderLengthStatus streamLength(const FLAC__StreamDecoder*, FLAC__uint64* streamLength, void* clientData)
  45. {
  46. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  47. *streamLength = data->stream->size() - data->streamOffset;
  48. return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
  49. }
  50. FLAC__bool streamEof(const FLAC__StreamDecoder*, void* clientData)
  51. {
  52. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  53. return data->stream->eof();
  54. }
  55. FLAC__StreamDecoderWriteStatus streamWrite(const FLAC__StreamDecoder*, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* clientData)
  56. {
  57. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  58. if (!data->output) // Seek
  59. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  60. UINT32 bytesPerSample = data->info.bitDepth / 8;
  61. // If we received more data than we need, store it in the overflow buffer
  62. UINT32 frameSamples = frame->header.blocksize * frame->header.channels;
  63. if (data->samplesToRead < frameSamples)
  64. {
  65. UINT32 numExtraSamples = frameSamples - data->samplesToRead;
  66. UINT32 extraBytes = numExtraSamples * bytesPerSample;
  67. data->overflow.reserve(extraBytes);
  68. }
  69. assert(bytesPerSample <= 4);
  70. for (UINT32 i = 0; i < frame->header.blocksize; i++)
  71. {
  72. for (UINT32 j = 0; j < frame->header.channels; j++)
  73. {
  74. if (data->samplesToRead > 0)
  75. {
  76. memcpy(data->output, &buffer[j][i], bytesPerSample);
  77. data->output += bytesPerSample;
  78. data->samplesToRead--;
  79. }
  80. else
  81. {
  82. UINT8 sample[4];
  83. memcpy(sample, &buffer[j][i], bytesPerSample);
  84. for(UINT32 k = 0; k < bytesPerSample; k++)
  85. data->overflow.push_back(sample[k]);
  86. }
  87. }
  88. }
  89. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  90. }
  91. void streamMetadata(const FLAC__StreamDecoder*, const FLAC__StreamMetadata* meta, void* clientData)
  92. {
  93. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  94. if (meta->type == FLAC__METADATA_TYPE_STREAMINFO)
  95. {
  96. data->info.numSamples = (UINT32)meta->data.stream_info.total_samples * meta->data.stream_info.channels;
  97. data->info.sampleRate = meta->data.stream_info.sample_rate;
  98. data->info.numChannels = meta->data.stream_info.channels;
  99. data->info.bitDepth = meta->data.stream_info.bits_per_sample;
  100. }
  101. }
  102. void streamError(const FLAC__StreamDecoder*, FLAC__StreamDecoderErrorStatus, void* clientData)
  103. {
  104. FLACDecoderData* data = (FLACDecoderData*)(clientData);
  105. data->error = true;
  106. }
  107. FLACDecoder::FLACDecoder()
  108. :mDecoder(nullptr)
  109. { }
  110. FLACDecoder::~FLACDecoder()
  111. {
  112. close();
  113. }
  114. bool FLACDecoder::isValid(const SPtr<DataStream>& stream, UINT32 offset)
  115. {
  116. stream->seek(offset);
  117. FLAC__StreamDecoder* decoder = FLAC__stream_decoder_new();
  118. if (!decoder)
  119. return false;
  120. FLACDecoderData data;
  121. data.stream = stream;
  122. mData.streamOffset = offset;
  123. FLAC__stream_decoder_init_stream(decoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof,
  124. &streamWrite, nullptr, &streamError, &data);
  125. bool valid = FLAC__stream_decoder_process_until_end_of_metadata(decoder) != 0;
  126. FLAC__stream_decoder_finish(decoder);
  127. FLAC__stream_decoder_delete(decoder);
  128. return valid && !data.error;
  129. }
  130. bool FLACDecoder::open(const SPtr<DataStream>& stream, AudioDataInfo& info, UINT32 offset)
  131. {
  132. if (stream == nullptr)
  133. return false;
  134. stream->seek(offset);
  135. mDecoder = FLAC__stream_decoder_new();
  136. if (mDecoder == nullptr)
  137. {
  138. LOGERR("Failed to open a FLAC file.");
  139. return false;
  140. }
  141. mData.stream = stream;
  142. mData.streamOffset = offset;
  143. FLAC__stream_decoder_init_stream(mDecoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof,
  144. &streamWrite, &streamMetadata, &streamError, &mData);
  145. if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder))
  146. {
  147. close();
  148. LOGERR("Failed to open a FLAC file.");
  149. return false;
  150. }
  151. info = mData.info;
  152. return true;
  153. }
  154. void FLACDecoder::seek(UINT32 offset)
  155. {
  156. mData.output = nullptr;
  157. mData.samplesToRead = 0;
  158. mData.overflow.clear();
  159. FLAC__stream_decoder_seek_absolute(mDecoder, offset);
  160. }
  161. UINT32 FLACDecoder::read(UINT8* samples, UINT32 numSamples)
  162. {
  163. UINT32 overflowSize = (UINT32)mData.overflow.size();
  164. UINT32 overflowNumSamples = 0;
  165. UINT32 bytesPerSample = mData.info.bitDepth / 8;
  166. if (overflowSize > 0)
  167. {
  168. UINT32 sampleSize = numSamples * bytesPerSample;
  169. if (overflowSize > sampleSize)
  170. {
  171. std::copy(mData.overflow.begin(), mData.overflow.begin() + sampleSize, samples);
  172. mData.overflow.erase(mData.overflow.begin(), mData.overflow.begin() + sampleSize);
  173. return numSamples;
  174. }
  175. else
  176. std::copy(mData.overflow.begin(), mData.overflow.end(), samples);
  177. overflowNumSamples = overflowSize / bytesPerSample;
  178. }
  179. mData.output = samples + overflowSize;
  180. mData.samplesToRead = numSamples - overflowNumSamples;
  181. mData.overflow.clear();
  182. while (mData.samplesToRead > 0)
  183. {
  184. if (!FLAC__stream_decoder_process_single(mDecoder))
  185. break;
  186. if (FLAC__stream_decoder_get_state(mDecoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
  187. break;
  188. }
  189. return numSamples - mData.samplesToRead;
  190. }
  191. void FLACDecoder::close()
  192. {
  193. if (mDecoder != nullptr)
  194. {
  195. FLAC__stream_decoder_finish(mDecoder);
  196. FLAC__stream_decoder_delete(mDecoder);
  197. mDecoder = nullptr;
  198. }
  199. }
  200. }