You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.7KB

  1. /*
  2. * Assorted DPCM codecs
  3. * Copyright (c) 2003 The ffmpeg Project.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file: dpcm.c
  21. * Assorted DPCM (differential pulse code modulation) audio codecs
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the specific data formats, visit:
  24. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  25. */
  26. #include "avcodec.h"
  27. typedef struct DPCMContext {
  28. int channels;
  29. short roq_square_array[256];
  30. int last_delta[2];
  31. } DPCMContext;
  32. #define SATURATE_S16(x) if (x < -32768) x = -32768; \
  33. else if (x > 32767) x = 32767;
  34. #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
  35. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  36. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  37. (((uint8_t*)(x))[2] << 16) | \
  38. (((uint8_t*)(x))[1] << 8) | \
  39. ((uint8_t*)(x))[0])
  40. static int interplay_delta_table[] = {
  41. 0, 1, 2, 3, 4, 5, 6, 7,
  42. 8, 9, 10, 11, 12, 13, 14, 15,
  43. 16, 17, 18, 19, 20, 21, 22, 23,
  44. 24, 25, 26, 27, 28, 29, 30, 31,
  45. 32, 33, 34, 35, 36, 37, 38, 39,
  46. 40, 41, 42, 43, 47, 51, 56, 61,
  47. 66, 72, 79, 86, 94, 102, 112, 122,
  48. 133, 145, 158, 173, 189, 206, 225, 245,
  49. 267, 292, 318, 348, 379, 414, 452, 493,
  50. 538, 587, 640, 699, 763, 832, 908, 991,
  51. 1081, 1180, 1288, 1405, 1534, 1673, 1826, 1993,
  52. 2175, 2373, 2590, 2826, 3084, 3365, 3672, 4008,
  53. 4373, 4772, 5208, 5683, 6202, 6767, 7385, 8059,
  54. 8794, 9597, 10472, 11428, 12471, 13609, 14851, 16206,
  55. 17685, 19298, 21060, 22981, 25078, 27367, 29864, 32589,
  56. -29973, -26728, -23186, -19322, -15105, -10503, -5481, -1,
  57. 1, 1, 5481, 10503, 15105, 19322, 23186, 26728,
  58. 29973, -32589, -29864, -27367, -25078, -22981, -21060, -19298,
  59. -17685, -16206, -14851, -13609, -12471, -11428, -10472, -9597,
  60. -8794, -8059, -7385, -6767, -6202, -5683, -5208, -4772,
  61. -4373, -4008, -3672, -3365, -3084, -2826, -2590, -2373,
  62. -2175, -1993, -1826, -1673, -1534, -1405, -1288, -1180,
  63. -1081, -991, -908, -832, -763, -699, -640, -587,
  64. -538, -493, -452, -414, -379, -348, -318, -292,
  65. -267, -245, -225, -206, -189, -173, -158, -145,
  66. -133, -122, -112, -102, -94, -86, -79, -72,
  67. -66, -61, -56, -51, -47, -43, -42, -41,
  68. -40, -39, -38, -37, -36, -35, -34, -33,
  69. -32, -31, -30, -29, -28, -27, -26, -25,
  70. -24, -23, -22, -21, -20, -19, -18, -17,
  71. -16, -15, -14, -13, -12, -11, -10, -9,
  72. -8, -7, -6, -5, -4, -3, -2, -1
  73. };
  74. static int dpcm_decode_init(AVCodecContext *avctx)
  75. {
  76. DPCMContext *s = avctx->priv_data;
  77. int i;
  78. short square;
  79. s->channels = avctx->channels;
  80. switch(avctx->codec->id) {
  81. case CODEC_ID_ROQ_DPCM:
  82. /* initialize square table */
  83. for (i = 0; i < 128; i++) {
  84. square = i * i;
  85. s->roq_square_array[i] = square;
  86. s->roq_square_array[i + 128] = -square;
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. return 0;
  93. }
  94. static int dpcm_decode_frame(AVCodecContext *avctx,
  95. void *data, int *data_size,
  96. uint8_t *buf, int buf_size)
  97. {
  98. DPCMContext *s = avctx->priv_data;
  99. int in, out = 0;
  100. int i;
  101. int predictor[2];
  102. int channel_number = 0;
  103. short *output_samples = data;
  104. int sequence_number;
  105. switch(avctx->codec->id) {
  106. case CODEC_ID_ROQ_DPCM:
  107. if (s->channels == 1)
  108. predictor[0] = LE_16(&buf[6]);
  109. else {
  110. predictor[0] = buf[7] << 8;
  111. predictor[1] = buf[6] << 8;
  112. }
  113. SE_16BIT(predictor[0]);
  114. SE_16BIT(predictor[1]);
  115. /* decode the samples */
  116. for (in = 8, out = 0; in < buf_size; in++, out++) {
  117. predictor[channel_number] += s->roq_square_array[buf[in]];
  118. SATURATE_S16(predictor[channel_number]);
  119. output_samples[out] = predictor[channel_number];
  120. /* toggle channel */
  121. channel_number ^= s->channels - 1;
  122. }
  123. break;
  124. case CODEC_ID_INTERPLAY_DPCM:
  125. in = 0;
  126. sequence_number = LE_16(&buf[in]);
  127. in += 6; /* skip over the stream mask and stream length */
  128. if (sequence_number == 1) {
  129. predictor[0] = LE_16(&buf[in]);
  130. in += 2;
  131. SE_16BIT(predictor[0])
  132. if (s->channels == 2) {
  133. predictor[1] = LE_16(&buf[in]);
  134. SE_16BIT(predictor[1])
  135. in += 2;
  136. }
  137. } else {
  138. for (i = 0; i < s->channels; i++)
  139. predictor[i] = s->last_delta[i];
  140. }
  141. while (in < buf_size) {
  142. predictor[channel_number] += interplay_delta_table[buf[in++]];
  143. SATURATE_S16(predictor[channel_number]);
  144. output_samples[out++] = predictor[channel_number];
  145. /* toggle channel */
  146. channel_number ^= s->channels - 1;
  147. }
  148. /* save predictors for next round */
  149. for (i = 0; i < s->channels; i++)
  150. s->last_delta[i] = predictor[i];
  151. break;
  152. }
  153. *data_size = out * sizeof(short);
  154. return buf_size;
  155. }
  156. AVCodec roq_dpcm_decoder = {
  157. "roq_dpcm",
  158. CODEC_TYPE_AUDIO,
  159. CODEC_ID_ROQ_DPCM,
  160. sizeof(DPCMContext),
  161. dpcm_decode_init,
  162. NULL,
  163. NULL,
  164. dpcm_decode_frame,
  165. };
  166. AVCodec interplay_dpcm_decoder = {
  167. "interplay_dpcm",
  168. CODEC_TYPE_AUDIO,
  169. CODEC_ID_INTERPLAY_DPCM,
  170. sizeof(DPCMContext),
  171. dpcm_decode_init,
  172. NULL,
  173. NULL,
  174. dpcm_decode_frame,
  175. };