avi_ima_adpcm_decoder.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "avi_ima_adpcm_decoder.h"
  2. #include "../f263/BitReader.h"
  3. #pragma pack(push, 1)
  4. struct ima_adpcm_format
  5. {
  6. nsavi::audio_format format;
  7. uint16_t samples_per_block;
  8. };
  9. #pragma pack(pop)
  10. IMA_ADPCM_AVIDecoder::IMA_ADPCM_AVIDecoder(const ima_adpcm_format *adpcmformat, const nsavi::STRH *stream_header) : adpcmformat(adpcmformat), stream_header(stream_header)
  11. {
  12. }
  13. int IMA_ADPCM_AVIDecoder::OutputFrameSize(size_t *frame_size)
  14. {
  15. int channels = adpcmformat->format.channels;
  16. *frame_size = ((adpcmformat->format.block_align - 7*channels)*2 + 2*channels) * 2;
  17. return AVI_SUCCESS;
  18. }
  19. int IMA_ADPCM_AVIDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  20. {
  21. if (adpcmformat)
  22. {
  23. *sampleRate = adpcmformat->format.sample_rate;
  24. *channels = adpcmformat->format.channels;
  25. *bitsPerSample = 16;
  26. *isFloat = false;
  27. return AVI_SUCCESS;
  28. }
  29. else
  30. {
  31. return AVI_FAILURE;
  32. }
  33. }
  34. static int index_table[16] = {
  35. -1, -1, -1, -1, 2, 4, 6, 8,
  36. -1, -1, -1, -1, 2, 4, 6, 8
  37. };
  38. static int step_table[89] = {
  39. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  40. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  41. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  42. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  43. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  44. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  45. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  46. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  47. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  48. };
  49. int IMA_ADPCM_AVIDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  50. {
  51. if (adpcmformat->format.channels == 1)
  52. {
  53. size_t adpcm_stream_length = *inputBufferBytes;
  54. if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
  55. return AVI_FAILURE;
  56. adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
  57. if (adpcm_stream_length < 7)
  58. return AVI_FAILURE;
  59. int16_t *out16 = (int16_t *)outputBuffer;
  60. size_t out16_length = *outputBufferBytes/2;
  61. const uint8_t *adpcm_data = (const uint8_t *)(*inputBuffer);
  62. int predictor = *(int16_t *)adpcm_data;
  63. *out16++ = predictor;
  64. adpcm_data+=2;
  65. out16_length--;
  66. int step_index = *adpcm_data;
  67. if (step_index > 88)
  68. return AVI_FAILURE;
  69. adpcm_data+=2;
  70. BitReader reader;
  71. reader.data = adpcm_data;
  72. reader.numBits = (uint32_t)(*inputBufferBytes - 4)*8;
  73. while (reader.numBits >= 8 && out16_length)
  74. {
  75. int diff, step, nibble;
  76. step = step_table[step_index];
  77. nibble = reader.getbits(4);
  78. step_index += index_table[nibble];
  79. step_index = min(step_index, 88);
  80. step_index = max(step_index, 0);
  81. diff = step>>3;
  82. if(nibble&4)
  83. diff += step;
  84. if(nibble&2)
  85. diff += step>>1;
  86. if(nibble&1)
  87. diff += step>>2;
  88. if (nibble&8)
  89. predictor -= diff;
  90. else
  91. predictor += diff;
  92. predictor = min(predictor, 32767);
  93. predictor = max(predictor, -32768);
  94. *out16++ = predictor;
  95. out16_length--;
  96. }
  97. *inputBuffer = (uint8_t *)(*inputBuffer) + adpcm_stream_length;
  98. *inputBufferBytes -= adpcm_stream_length;
  99. *outputBufferBytes = adpcmformat->samples_per_block*2;
  100. return AVI_SUCCESS;
  101. }
  102. else if (adpcmformat->format.channels == 2)
  103. {
  104. size_t adpcm_stream_length = *inputBufferBytes;
  105. if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
  106. return AVI_FAILURE;
  107. adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
  108. if (adpcm_stream_length < 8)
  109. return AVI_FAILURE;
  110. int16_t *out16 = (int16_t *)outputBuffer;
  111. size_t out16_length = *outputBufferBytes/2;
  112. const uint8_t *adpcm_data = (const uint8_t *)(*inputBuffer);
  113. int predictor_left = *(int16_t *)adpcm_data;
  114. *out16++ = predictor_left;
  115. adpcm_data+=2;
  116. out16_length--;
  117. int step_index_left = *adpcm_data;
  118. if (step_index_left > 88)
  119. return AVI_FAILURE;
  120. adpcm_data+=2;
  121. int predictor_right = *(int16_t *)adpcm_data;
  122. *out16++ = predictor_right;
  123. adpcm_data+=2;
  124. out16_length--;
  125. int step_index_right = *adpcm_data;
  126. if (step_index_right > 88)
  127. return AVI_FAILURE;
  128. adpcm_data+=2;
  129. BitReader reader;
  130. reader.data = adpcm_data;
  131. reader.numBits = (uint32_t)(*inputBufferBytes - 8)*8;
  132. while (reader.numBits >= 8 && out16_length > 15)
  133. {
  134. int nibbles_left[8] = {0};
  135. int nibbles_right[8] = {0};
  136. for (int i=0;i<8;i++)
  137. nibbles_left[i] = reader.getbits(4);
  138. for (int i=0;i<8;i++)
  139. nibbles_right[i] = reader.getbits(4);
  140. for (int i=0;i<8;i++)
  141. {
  142. int diff, step, nibble;
  143. step = step_table[step_index_left];
  144. nibble = nibbles_left[i];
  145. step_index_left += index_table[nibble];
  146. step_index_left = min(step_index_left, 88);
  147. step_index_left = max(step_index_left, 0);
  148. diff = step>>3;
  149. if(nibble&4)
  150. diff += step;
  151. if(nibble&2)
  152. diff += step>>1;
  153. if(nibble&1)
  154. diff += step>>2;
  155. if (nibble&8)
  156. predictor_left -= diff;
  157. else
  158. predictor_left += diff;
  159. predictor_left = min(predictor_left, 32767);
  160. predictor_left = max(predictor_left, -32768);
  161. *out16++ = predictor_left;
  162. out16_length--;
  163. step = step_table[step_index_right];
  164. nibble =nibbles_right[i];
  165. step_index_right += index_table[nibble];
  166. step_index_right = min(step_index_right, 88);
  167. step_index_right = max(step_index_right, 0);
  168. diff = step>>3;
  169. if(nibble&4)
  170. diff += step;
  171. if(nibble&2)
  172. diff += step>>1;
  173. if(nibble&1)
  174. diff += step>>2;
  175. if (nibble&8)
  176. predictor_right -= diff;
  177. else
  178. predictor_right += diff;
  179. predictor_right = min(predictor_right, 32767);
  180. predictor_right = max(predictor_right, -32768);
  181. *out16++ = predictor_right;
  182. out16_length--;
  183. }
  184. }
  185. *inputBuffer = (uint8_t *)(*inputBuffer) + adpcm_stream_length;
  186. *inputBufferBytes -= adpcm_stream_length;
  187. *outputBufferBytes = adpcmformat->samples_per_block*4;
  188. return AVI_SUCCESS;
  189. }
  190. return AVI_FAILURE;
  191. }
  192. void IMA_ADPCM_AVIDecoder::Close()
  193. {
  194. delete this;
  195. }
  196. #define CBCLASS IMA_ADPCM_AVIDecoder
  197. START_DISPATCH;
  198. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  199. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  200. CB(DECODE_CHUNK, DecodeChunk)
  201. VCB(CLOSE, Close)
  202. END_DISPATCH;
  203. #undef CBCLASS