mkv_vorbis_decoder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "mkv_vorbis_decoder.h"
  2. #include "../nsmkv/Lacing.h"
  3. #include "../nsmkv/Cluster.h"
  4. #include <math.h>
  5. int MKVDecoderCreator::CreateAudioDecoder(const char *codec_id,
  6. const nsmkv::TrackEntryData *track_entry_data, const nsmkv::AudioData *audio_data,
  7. unsigned int preferred_bits, unsigned int max_channels, bool floating_point,
  8. ifc_mkvaudiodecoder **decoder)
  9. {
  10. if (!strcmp(codec_id, "A_VORBIS"))
  11. {
  12. MKVVorbis *vorbis = new MKVVorbis;
  13. vorbis_info_init(&vorbis->info);
  14. vorbis_comment_init(&vorbis->comment);
  15. nsmkv::LacingState lacing_state;
  16. if (nsmkv::Lacing::GetState(nsmkv::BlockBinary::XIPH_LACING, (const uint8_t *)track_entry_data->codec_private, track_entry_data->codec_private_len, &lacing_state))
  17. {
  18. const uint8_t *frame;
  19. size_t frame_len;
  20. uint16_t frame_number=0;
  21. while (nsmkv::Lacing::GetFrame(frame_number, (const uint8_t *)track_entry_data->codec_private, track_entry_data->codec_private_len, &frame, &frame_len, &lacing_state))
  22. {
  23. ogg_packet packet = {const_cast<uint8_t *>(frame), (long)frame_len, (frame_number==0), 0, 0 /*-1?*/, vorbis->packet_number++};
  24. int ret = vorbis_synthesis_headerin(&vorbis->info, &vorbis->comment, &packet);
  25. if (ret != 0)
  26. goto bail;
  27. frame_number++;
  28. }
  29. if (vorbis_synthesis_init(&vorbis->dsp, &vorbis->info) == 0
  30. && vorbis_block_init(&vorbis->dsp, &vorbis->block) == 0)
  31. {
  32. vorbis->bps = preferred_bits?preferred_bits:16;
  33. *decoder = vorbis;
  34. return CREATEDECODER_SUCCESS;
  35. }
  36. }
  37. bail:
  38. delete vorbis;
  39. return CREATEDECODER_FAILURE;
  40. }
  41. return CREATEDECODER_NOT_MINE;
  42. }
  43. #define CBCLASS MKVDecoderCreator
  44. START_DISPATCH;
  45. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  46. END_DISPATCH;
  47. #undef CBCLASS
  48. MKVVorbis::MKVVorbis()
  49. {
  50. bps=16;
  51. packet_number=0;
  52. }
  53. #define PA_CLIP_( val, min, max )\
  54. { val = ((val) < (min)) ? (min) : (((val) > (max)) ? (max) : (val)); }
  55. #if defined(_M_IX86)
  56. static __inline long float_to_long(double t)
  57. {
  58. long r;
  59. __asm fld t
  60. __asm fistp r
  61. return r;
  62. }
  63. #else
  64. #define float_to_long(x) ((long)( x ))
  65. #endif
  66. inline static void clip(double &x, double a, double b)
  67. {
  68. double x1 = fabs (x - a);
  69. double x2 = fabs (x - b);
  70. x = x1 + (a + b);
  71. x -= x2;
  72. x *= 0.5;
  73. }
  74. static void Float32_To_Int24_Clip(void *destinationBuffer, void *sourceBuffer, size_t count, size_t channels, double gain)
  75. {
  76. float *src = (float*)sourceBuffer;
  77. unsigned char *dest = (unsigned char*)destinationBuffer;
  78. gain*=65536.*32768.;
  79. while ( count-- )
  80. {
  81. /* convert to 32 bit and drop the low 8 bits */
  82. double scaled = *src * gain;
  83. clip( scaled, -2147483648., 2147483647.);
  84. signed long temp = (signed long) scaled;
  85. dest[0] = (unsigned char)(temp >> 8);
  86. dest[1] = (unsigned char)(temp >> 16);
  87. dest[2] = (unsigned char)(temp >> 24);
  88. src++;
  89. dest += 3*channels;
  90. }
  91. }
  92. static void Float32_To_Int16_Clip(void *destinationBuffer, void *sourceBuffer, size_t count, size_t channels, double gain)
  93. {
  94. float *src = (float*)sourceBuffer;
  95. signed short *dest = (signed short*)destinationBuffer;
  96. gain*=32768.0;
  97. while ( count-- )
  98. {
  99. long samp = float_to_long((*src) * gain/* - 0.5*/);
  100. PA_CLIP_( samp, -0x8000, 0x7FFF );
  101. *dest = (signed short) samp;
  102. src ++;
  103. dest += channels;
  104. }
  105. }
  106. int MKVVorbis::DecodeBlock(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  107. {
  108. uint8_t *out = (uint8_t *)outputBuffer;
  109. ogg_packet packet = {(uint8_t *)inputBuffer, (long)inputBufferBytes, 0, 0, 0 -1, packet_number++};
  110. int ret = vorbis_synthesis(&block, &packet);
  111. if (ret == 0)
  112. {
  113. vorbis_synthesis_blockin(&dsp,&block);
  114. long channels = info.channels;
  115. float **pcm;
  116. int samples = vorbis_synthesis_pcmout(&dsp, &pcm);
  117. if (samples)
  118. {
  119. switch(bps)
  120. {
  121. case 16:
  122. for(int i=0;i<channels;i++)
  123. {
  124. Float32_To_Int16_Clip(out, pcm[i], samples, channels, 1.0 /*gain*/);
  125. out+=2;
  126. }
  127. break;
  128. case 24:
  129. for(int i=0;i<channels;i++)
  130. {
  131. Float32_To_Int24_Clip(out, pcm[i], samples, channels, 1.0 /*gain*/);
  132. out+=3;
  133. }
  134. break;
  135. }
  136. }
  137. *outputBufferBytes = samples*channels*bps/8;
  138. // let the decoder know we're processed them
  139. vorbis_synthesis_read(&dsp,samples);
  140. return MKV_SUCCESS;
  141. }
  142. return MKV_FAILURE;
  143. }
  144. int MKVVorbis::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  145. {
  146. *sampleRate = info.rate;
  147. *channels = info.channels;
  148. *bitsPerSample = bps;
  149. *isFloat = false; // TODO
  150. return MKV_SUCCESS;
  151. }
  152. void MKVVorbis::Flush()
  153. {
  154. vorbis_synthesis_restart(&dsp);
  155. }
  156. void MKVVorbis::Close()
  157. {
  158. // TODO: benski> verify
  159. vorbis_info_clear(&info);
  160. vorbis_comment_clear(&comment);
  161. vorbis_dsp_clear(&dsp);
  162. vorbis_block_clear(&block);
  163. delete this;
  164. }
  165. #define CBCLASS MKVVorbis
  166. START_DISPATCH;
  167. //CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  168. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  169. CB(DECODE_BLOCK, DecodeBlock)
  170. VCB(FLUSH, Flush)
  171. VCB(CLOSE, Close)
  172. END_DISPATCH;
  173. #undef CBCLASS