mkv_flac_decoder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include"mkv_flac_decoder.h"
  2. #include "main.h"
  3. static FLAC__StreamDecoderReadStatus Packet_Read(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  4. {
  5. packet_client_data_t packet = (packet_client_data_t)client_data;
  6. size_t to_copy = *bytes;
  7. if (to_copy > packet->buffer_length) {
  8. to_copy = packet->buffer_length;
  9. }
  10. memcpy(buffer, packet->buffer, to_copy);
  11. *bytes = to_copy;
  12. packet->buffer += to_copy;
  13. packet->buffer_length -= to_copy;
  14. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  15. }
  16. static FLAC__StreamDecoderSeekStatus Packet_Seek(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  17. {
  18. return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
  19. }
  20. static FLAC__StreamDecoderTellStatus Packet_Tell(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  21. {
  22. return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
  23. }
  24. static FLAC__StreamDecoderLengthStatus Packet_Length(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  25. {
  26. return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
  27. }
  28. static FLAC__bool Packet_EOF(const FLAC__StreamDecoder *decoder, void *client_data)
  29. {
  30. return 0;
  31. }
  32. static void OnError(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  33. {
  34. //client_data=client_data; // dummy line so i can set a breakpoint
  35. }
  36. static void OnMetadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  37. {
  38. packet_client_data_t packet = (packet_client_data_t)client_data;
  39. switch(metadata->type)
  40. {
  41. case FLAC__METADATA_TYPE_STREAMINFO:
  42. {
  43. packet->frame_size = metadata->data.stream_info.max_blocksize;
  44. packet->bps=metadata->data.stream_info.bits_per_sample;
  45. packet->bytes_per_sample = (packet->bps + 7) / 8;
  46. packet->channels=metadata->data.stream_info.channels;
  47. packet->sample_rate=metadata->data.stream_info.sample_rate;
  48. packet->samples=metadata->data.stream_info.total_samples;
  49. }
  50. break;
  51. }
  52. }
  53. static FLAC__StreamDecoderWriteStatus OnAudio(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
  54. {
  55. packet_client_data_t packet = (packet_client_data_t)client_data;
  56. size_t byteLength = packet->bytes_per_sample * packet->channels * frame->header.blocksize;
  57. if (byteLength > packet->outputBufferBytes[0]) {
  58. FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  59. }
  60. InterleaveAndTruncate(buffer, packet->outputBuffer, packet->bytes_per_sample * 8, packet->channels, frame->header.blocksize);
  61. packet->outputBufferBytes[0] = byteLength;
  62. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  63. }
  64. MKVFLACDecoder *MKVFLACDecoder::Create(const nsmkv::TrackEntryData *track_entry_data, const nsmkv::AudioData *audio_data, unsigned int preferred_bits, unsigned int max_channels)
  65. {
  66. FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new();
  67. if (!decoder) {
  68. return 0;
  69. }
  70. packet_client_data_t packet = new packet_client_data_s;
  71. packet->buffer = 0;
  72. packet->buffer_length = 0;
  73. if(FLAC__stream_decoder_init_stream(
  74. decoder,
  75. Packet_Read,
  76. Packet_Seek,
  77. Packet_Tell,
  78. Packet_Length,
  79. Packet_EOF,
  80. OnAudio,
  81. OnMetadata,
  82. OnError,
  83. packet
  84. ) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
  85. {
  86. delete packet;
  87. FLAC__stream_decoder_delete(decoder);
  88. return 0;
  89. }
  90. packet->buffer = (const uint8_t *)track_entry_data->codec_private;
  91. packet->buffer_length = track_entry_data->codec_private_len;
  92. if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
  93. delete packet;
  94. FLAC__stream_decoder_delete(decoder);
  95. return 0;
  96. }
  97. MKVFLACDecoder *mkv_decoder = new MKVFLACDecoder(decoder, packet, preferred_bits);
  98. if (!mkv_decoder) {
  99. delete packet;
  100. FLAC__stream_decoder_delete(decoder);
  101. return 0;
  102. }
  103. return mkv_decoder;
  104. }
  105. MKVFLACDecoder::MKVFLACDecoder(FLAC__StreamDecoder *decoder, packet_client_data_t packet, unsigned int bps)
  106. : decoder(decoder), packet(packet), bps(bps)
  107. {
  108. }
  109. MKVFLACDecoder::~MKVFLACDecoder()
  110. {
  111. delete packet;
  112. FLAC__stream_decoder_delete(decoder);
  113. }
  114. int MKVFLACDecoder::OutputFrameSize(size_t *frame_size)
  115. {
  116. *frame_size = packet->frame_size * packet->bytes_per_sample * packet->channels;
  117. return MKV_SUCCESS;
  118. }
  119. int MKVFLACDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  120. {
  121. *sampleRate = packet->sample_rate;
  122. *channels = packet->channels;
  123. *bitsPerSample = packet->bps;
  124. *isFloat = false;
  125. return MKV_SUCCESS;
  126. }
  127. int MKVFLACDecoder::DecodeBlock(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  128. {
  129. packet->buffer = (const uint8_t *)inputBuffer;
  130. packet->buffer_length = inputBufferBytes;
  131. packet->outputBuffer = outputBuffer;
  132. packet->outputBufferBytes = outputBufferBytes;
  133. if (FLAC__stream_decoder_process_single(decoder) == 0) {
  134. return MKV_FAILURE;
  135. }
  136. return MKV_SUCCESS;
  137. }
  138. void MKVFLACDecoder::Flush()
  139. {
  140. FLAC__stream_decoder_flush(decoder);
  141. }
  142. void MKVFLACDecoder::Close()
  143. {
  144. delete this;
  145. }
  146. #define CBCLASS MKVFLACDecoder
  147. START_DISPATCH;
  148. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  149. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  150. CB(DECODE_BLOCK, DecodeBlock)
  151. VCB(FLUSH, Flush)
  152. VCB(CLOSE, Close)
  153. END_DISPATCH;
  154. #undef CBCLASS
  155. int MKVDecoder::CreateAudioDecoder(const char *codec_id, const nsmkv::TrackEntryData *track_entry_data, const nsmkv::AudioData *audio_data, unsigned int preferred_bits, unsigned int max_channels,bool floating_point, ifc_mkvaudiodecoder **decoder)
  156. {
  157. if (!strcmp(codec_id, "A_FLAC"))
  158. {
  159. MKVFLACDecoder *flac_decoder = MKVFLACDecoder::Create(track_entry_data, audio_data, preferred_bits, max_channels);
  160. if (flac_decoder)
  161. {
  162. *decoder = flac_decoder;
  163. return CREATEDECODER_SUCCESS;
  164. }
  165. return CREATEDECODER_FAILURE;
  166. }
  167. return CREATEDECODER_NOT_MINE;
  168. }
  169. #define CBCLASS MKVDecoder
  170. START_DISPATCH;
  171. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  172. END_DISPATCH;
  173. #undef CBCLASS