1
0

avi_adpcm_decoder.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "avi_adpcm_decoder.h"
  2. #include "avi_ima_adpcm_decoder.h"
  3. #pragma pack(push, 1)
  4. typedef int16_t ms_adpcm_coefficients[2];
  5. struct ms_adpcm_format
  6. {
  7. nsavi::audio_format format;
  8. uint16_t samples_per_block;
  9. uint16_t number_of_coefficients;
  10. ms_adpcm_coefficients coefficients[1];
  11. };
  12. #pragma pack(pop)
  13. int AVIDecoder::CreateAudioDecoder(const nsavi::AVIH *avi_header,
  14. const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data,
  15. unsigned int preferred_bits, unsigned int max_channels, bool floating_point,
  16. ifc_aviaudiodecoder **decoder)
  17. {
  18. const nsavi::audio_format *format = (const nsavi::audio_format *)stream_format;
  19. if (format->format == nsavi::audio_format_ms_adpcm)
  20. {
  21. // TODO: verify waveformat sizes
  22. *decoder = new MS_ADPCM_AVIDecoder( (const ms_adpcm_format *)format, stream_header);
  23. return CREATEDECODER_SUCCESS;
  24. }
  25. else if (format->format == nsavi::audio_format_ima_adpcm)
  26. {
  27. // TODO: verify waveformat sizes
  28. *decoder = new IMA_ADPCM_AVIDecoder((const ima_adpcm_format *)format, stream_header);
  29. return CREATEDECODER_SUCCESS;
  30. }
  31. return CREATEDECODER_NOT_MINE;
  32. }
  33. #define CBCLASS AVIDecoder
  34. START_DISPATCH;
  35. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  36. END_DISPATCH;
  37. #undef CBCLASS
  38. int ms_adpcm_adaptationtable[] = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 };
  39. //int ms_adpcm_adaptcoeff1[] = { 256, 512, 0, 192, 240, 460, 392 } ;
  40. //int ms_adpcm_adaptcoeff2[] = { 0, -256, 0, 64, 0, -208, -232 } ;
  41. MS_ADPCM_AVIDecoder::MS_ADPCM_AVIDecoder(const ms_adpcm_format *adpcmformat, const nsavi::STRH *stream_header) : adpcmformat(adpcmformat), stream_header(stream_header)
  42. {
  43. }
  44. int MS_ADPCM_AVIDecoder::OutputFrameSize(size_t *frame_size)
  45. {
  46. int channels = adpcmformat->format.channels;
  47. *frame_size = ((adpcmformat->format.block_align - 7*channels)*2 + 2*channels) * 2;
  48. return AVI_SUCCESS;
  49. }
  50. int MS_ADPCM_AVIDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  51. {
  52. if (adpcmformat)
  53. {
  54. *sampleRate = adpcmformat->format.sample_rate;
  55. *channels = adpcmformat->format.channels;
  56. *bitsPerSample = 16;
  57. *isFloat = false;
  58. return AVI_SUCCESS;
  59. }
  60. else
  61. {
  62. return AVI_FAILURE;
  63. }
  64. }
  65. int MS_ADPCM_AVIDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  66. {
  67. const ms_adpcm_coefficients *ms_adpcm_adaptcoeff = adpcmformat->coefficients;
  68. // TODO: use default coef values if they aren't present
  69. if (adpcmformat->format.channels == 1)
  70. {
  71. size_t adpcm_stream_length = *inputBufferBytes;
  72. if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
  73. return AVI_FAILURE;
  74. adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
  75. if (adpcm_stream_length < 7)
  76. return AVI_FAILURE;
  77. int16_t *out16 = (int16_t *)outputBuffer;
  78. size_t out16_length = *outputBufferBytes/2;
  79. const uint8_t *adpcm8 = (const uint8_t *)(*inputBuffer);
  80. uint8_t block_predictor = *adpcm8++;
  81. if (block_predictor > adpcmformat->number_of_coefficients)
  82. return AVI_FAILURE;
  83. int32_t coef1 = ms_adpcm_adaptcoeff[block_predictor][0];
  84. int32_t coef2 = ms_adpcm_adaptcoeff[block_predictor][1];
  85. const uint16_t *adpcm16 = (const uint16_t *)adpcm8;
  86. int16_t delta = *adpcm16++;
  87. int16_t sample1 = out16[1] = *adpcm16++;
  88. int16_t sample2 = out16[0] = *adpcm16++;
  89. int i=2;
  90. adpcm_stream_length-=7;
  91. adpcm8 = (const uint8_t *)adpcm16;
  92. while (adpcm_stream_length-- && out16_length)
  93. {
  94. int32_t predictor = ((int32_t)sample1 * coef1 + (int32_t)sample2 * coef2)>>8;
  95. uint32_t nibble = *adpcm8 >> 4;
  96. int32_t signed_nibble = ((int32_t)nibble << 28) >> 28;
  97. predictor += signed_nibble*delta;
  98. predictor = max(predictor, -32768);
  99. predictor = min(predictor, 32767);
  100. sample2=sample1;
  101. sample1=out16[i++]=predictor;
  102. out16_length--;
  103. delta = (ms_adpcm_adaptationtable[nibble]*delta)>>8;
  104. delta = max(delta, 16);
  105. predictor = ((int32_t)sample1 * coef1 + (int32_t)sample2 * coef2)>>8;
  106. nibble = *adpcm8++ & 0x0F;
  107. signed_nibble = ((int32_t)nibble << 28) >> 28;
  108. predictor += signed_nibble*delta;
  109. predictor = max(predictor, -32768);
  110. predictor = min(predictor, 32767);
  111. sample2=sample1;
  112. sample1=out16[i++]=predictor;
  113. out16_length--;
  114. delta = (ms_adpcm_adaptationtable[nibble]*delta)>>8;
  115. delta = max(delta, 16);
  116. }
  117. *inputBufferBytes -= adpcmformat->format.block_align;
  118. *inputBuffer = (void *)adpcm8;
  119. *outputBufferBytes = i*2;
  120. return AVI_SUCCESS;
  121. }
  122. else if (adpcmformat->format.channels == 2)
  123. {
  124. size_t adpcm_stream_length = *inputBufferBytes;
  125. if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
  126. return AVI_FAILURE;
  127. adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
  128. if (adpcm_stream_length < 14)
  129. return AVI_FAILURE;
  130. int16_t *out16 = (int16_t *)outputBuffer;
  131. size_t out16_length = *outputBufferBytes/2;
  132. const uint8_t *adpcm8 = (const uint8_t *)(*inputBuffer);
  133. uint8_t block_predictor_left = *adpcm8++;
  134. if (block_predictor_left > adpcmformat->number_of_coefficients)
  135. return AVI_FAILURE;
  136. uint8_t block_predictor_right = *adpcm8++;
  137. if (block_predictor_right > adpcmformat->number_of_coefficients)
  138. return AVI_FAILURE;
  139. int32_t coef1_left = ms_adpcm_adaptcoeff[block_predictor_left][0];
  140. int32_t coef2_left = ms_adpcm_adaptcoeff[block_predictor_left][1];
  141. int32_t coef1_right = ms_adpcm_adaptcoeff[block_predictor_right][0];
  142. int32_t coef2_right = ms_adpcm_adaptcoeff[block_predictor_right][1];
  143. const uint16_t *adpcm16 = (const uint16_t *)adpcm8;
  144. int16_t delta_left = *adpcm16++;
  145. int16_t delta_right = *adpcm16++;
  146. int16_t sample1_left = out16[2] = *adpcm16++;
  147. int16_t sample1_right = out16[3] = *adpcm16++;
  148. int16_t sample2_left = out16[0] = *adpcm16++;
  149. int16_t sample2_right = out16[1] = *adpcm16++;
  150. int i=4;
  151. adpcm_stream_length-=14;
  152. adpcm8 = (const uint8_t *)adpcm16;
  153. while (adpcm_stream_length-- && out16_length)
  154. {
  155. int32_t predictor = ((int32_t)sample1_left * coef1_left + (int32_t)sample2_left * coef2_left)>>8;
  156. uint32_t nibble = *adpcm8 >> 4;
  157. int32_t signed_nibble = ((int32_t)nibble << 28) >> 28;
  158. predictor += signed_nibble*delta_left;
  159. predictor = max(predictor, -32768);
  160. predictor = min(predictor, 32767);
  161. sample2_left=sample1_left;
  162. sample1_left=out16[i++]=predictor;
  163. out16_length--;
  164. delta_left = (ms_adpcm_adaptationtable[nibble]*delta_left)>>8;
  165. delta_left = max(delta_left, 16);
  166. predictor = ((int32_t)sample1_right * coef1_right + (int32_t)sample2_right * coef2_right)>>8;
  167. nibble = *adpcm8++ & 0x0F;
  168. signed_nibble = ((int32_t)nibble << 28) >> 28;
  169. predictor += signed_nibble*delta_right;
  170. predictor = max(predictor, -32768);
  171. predictor = min(predictor, 32767);
  172. sample2_right=sample1_right;
  173. sample1_right=out16[i++]=predictor;
  174. out16_length--;
  175. delta_right = (ms_adpcm_adaptationtable[nibble]*delta_right)>>8;
  176. delta_right = max(delta_right, 16);
  177. }
  178. *inputBufferBytes -= adpcmformat->format.block_align;
  179. *inputBuffer = (void *)adpcm8;
  180. *outputBufferBytes = i*2;
  181. return AVI_SUCCESS;
  182. }
  183. return AVI_FAILURE;
  184. }
  185. void MS_ADPCM_AVIDecoder::Close()
  186. {
  187. delete this;
  188. }
  189. #define CBCLASS MS_ADPCM_AVIDecoder
  190. START_DISPATCH;
  191. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  192. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  193. CB(DECODE_CHUNK, DecodeChunk)
  194. VCB(CLOSE, Close)
  195. END_DISPATCH;
  196. #undef CBCLASS