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.

351 lines
13KB

  1. /*
  2. * Assorted DPCM codecs
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Assorted DPCM (differential pulse code modulation) audio codecs
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * Xan DPCM decoder by Mario Brito (mbrito@student.dei.uc.pt)
  26. * for more information on the specific data formats, visit:
  27. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  28. * SOL DPCMs implemented by Konstantin Shishkov
  29. *
  30. * Note about using the Xan DPCM decoder: Xan DPCM is used in AVI files
  31. * found in the Wing Commander IV computer game. These AVI files contain
  32. * WAVEFORMAT headers which report the audio format as 0x01: raw PCM.
  33. * Clearly incorrect. To detect Xan DPCM, you will probably have to
  34. * special-case your AVI demuxer to use Xan DPCM if the file uses 'Xxan'
  35. * (Xan video) for its video codec. Alternately, such AVI files also contain
  36. * the fourcc 'Axan' in the 'auds' chunk of the AVI header.
  37. */
  38. #include "libavutil/intreadwrite.h"
  39. #include "avcodec.h"
  40. #include "bytestream.h"
  41. #include "mathops.h"
  42. typedef struct DPCMContext {
  43. AVFrame frame;
  44. int channels;
  45. int16_t roq_square_array[256];
  46. int sample[2]; ///< previous sample (for SOL_DPCM)
  47. const int8_t *sol_table; ///< delta table for SOL_DPCM
  48. } DPCMContext;
  49. static const int16_t interplay_delta_table[] = {
  50. 0, 1, 2, 3, 4, 5, 6, 7,
  51. 8, 9, 10, 11, 12, 13, 14, 15,
  52. 16, 17, 18, 19, 20, 21, 22, 23,
  53. 24, 25, 26, 27, 28, 29, 30, 31,
  54. 32, 33, 34, 35, 36, 37, 38, 39,
  55. 40, 41, 42, 43, 47, 51, 56, 61,
  56. 66, 72, 79, 86, 94, 102, 112, 122,
  57. 133, 145, 158, 173, 189, 206, 225, 245,
  58. 267, 292, 318, 348, 379, 414, 452, 493,
  59. 538, 587, 640, 699, 763, 832, 908, 991,
  60. 1081, 1180, 1288, 1405, 1534, 1673, 1826, 1993,
  61. 2175, 2373, 2590, 2826, 3084, 3365, 3672, 4008,
  62. 4373, 4772, 5208, 5683, 6202, 6767, 7385, 8059,
  63. 8794, 9597, 10472, 11428, 12471, 13609, 14851, 16206,
  64. 17685, 19298, 21060, 22981, 25078, 27367, 29864, 32589,
  65. -29973, -26728, -23186, -19322, -15105, -10503, -5481, -1,
  66. 1, 1, 5481, 10503, 15105, 19322, 23186, 26728,
  67. 29973, -32589, -29864, -27367, -25078, -22981, -21060, -19298,
  68. -17685, -16206, -14851, -13609, -12471, -11428, -10472, -9597,
  69. -8794, -8059, -7385, -6767, -6202, -5683, -5208, -4772,
  70. -4373, -4008, -3672, -3365, -3084, -2826, -2590, -2373,
  71. -2175, -1993, -1826, -1673, -1534, -1405, -1288, -1180,
  72. -1081, -991, -908, -832, -763, -699, -640, -587,
  73. -538, -493, -452, -414, -379, -348, -318, -292,
  74. -267, -245, -225, -206, -189, -173, -158, -145,
  75. -133, -122, -112, -102, -94, -86, -79, -72,
  76. -66, -61, -56, -51, -47, -43, -42, -41,
  77. -40, -39, -38, -37, -36, -35, -34, -33,
  78. -32, -31, -30, -29, -28, -27, -26, -25,
  79. -24, -23, -22, -21, -20, -19, -18, -17,
  80. -16, -15, -14, -13, -12, -11, -10, -9,
  81. -8, -7, -6, -5, -4, -3, -2, -1
  82. };
  83. static const int8_t sol_table_old[16] = {
  84. 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15,
  85. -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0
  86. };
  87. static const int8_t sol_table_new[16] = {
  88. 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15,
  89. 0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15
  90. };
  91. static const int16_t sol_table_16[128] = {
  92. 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
  93. 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
  94. 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
  95. 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
  96. 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
  97. 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
  98. 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
  99. 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
  100. 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
  101. 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
  102. 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
  103. 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
  104. 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
  105. };
  106. static av_cold int dpcm_decode_init(AVCodecContext *avctx)
  107. {
  108. DPCMContext *s = avctx->priv_data;
  109. int i;
  110. if (avctx->channels < 1 || avctx->channels > 2) {
  111. av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
  112. return AVERROR(EINVAL);
  113. }
  114. s->channels = avctx->channels;
  115. s->sample[0] = s->sample[1] = 0;
  116. switch(avctx->codec->id) {
  117. case AV_CODEC_ID_ROQ_DPCM:
  118. /* initialize square table */
  119. for (i = 0; i < 128; i++) {
  120. int16_t square = i * i;
  121. s->roq_square_array[i ] = square;
  122. s->roq_square_array[i + 128] = -square;
  123. }
  124. break;
  125. case AV_CODEC_ID_SOL_DPCM:
  126. switch(avctx->codec_tag){
  127. case 1:
  128. s->sol_table = sol_table_old;
  129. s->sample[0] = s->sample[1] = 0x80;
  130. break;
  131. case 2:
  132. s->sol_table = sol_table_new;
  133. s->sample[0] = s->sample[1] = 0x80;
  134. break;
  135. case 3:
  136. break;
  137. default:
  138. av_log(avctx, AV_LOG_ERROR, "Unknown SOL subcodec\n");
  139. return -1;
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. if (avctx->codec->id == AV_CODEC_ID_SOL_DPCM && avctx->codec_tag != 3)
  146. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  147. else
  148. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  149. avcodec_get_frame_defaults(&s->frame);
  150. avctx->coded_frame = &s->frame;
  151. return 0;
  152. }
  153. static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
  154. int *got_frame_ptr, AVPacket *avpkt)
  155. {
  156. int buf_size = avpkt->size;
  157. DPCMContext *s = avctx->priv_data;
  158. int out = 0, ret;
  159. int predictor[2];
  160. int ch = 0;
  161. int stereo = s->channels - 1;
  162. int16_t *output_samples, *samples_end;
  163. GetByteContext gb;
  164. if (stereo && (buf_size & 1))
  165. buf_size--;
  166. bytestream2_init(&gb, avpkt->data, buf_size);
  167. /* calculate output size */
  168. switch(avctx->codec->id) {
  169. case AV_CODEC_ID_ROQ_DPCM:
  170. out = buf_size - 8;
  171. break;
  172. case AV_CODEC_ID_INTERPLAY_DPCM:
  173. out = buf_size - 6 - s->channels;
  174. break;
  175. case AV_CODEC_ID_XAN_DPCM:
  176. out = buf_size - 2 * s->channels;
  177. break;
  178. case AV_CODEC_ID_SOL_DPCM:
  179. if (avctx->codec_tag != 3)
  180. out = buf_size * 2;
  181. else
  182. out = buf_size;
  183. break;
  184. }
  185. if (out <= 0) {
  186. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  187. return AVERROR(EINVAL);
  188. }
  189. if (out % s->channels) {
  190. av_log(avctx, AV_LOG_WARNING, "channels have differing number of samples\n");
  191. }
  192. /* get output buffer */
  193. s->frame.nb_samples = (out + s->channels - 1) / s->channels;
  194. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  195. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  196. return ret;
  197. }
  198. output_samples = (int16_t *)s->frame.data[0];
  199. samples_end = output_samples + out;
  200. switch(avctx->codec->id) {
  201. case AV_CODEC_ID_ROQ_DPCM:
  202. bytestream2_skipu(&gb, 6);
  203. if (stereo) {
  204. predictor[1] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
  205. predictor[0] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
  206. } else {
  207. predictor[0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  208. }
  209. /* decode the samples */
  210. while (output_samples < samples_end) {
  211. predictor[ch] += s->roq_square_array[bytestream2_get_byteu(&gb)];
  212. predictor[ch] = av_clip_int16(predictor[ch]);
  213. *output_samples++ = predictor[ch];
  214. /* toggle channel */
  215. ch ^= stereo;
  216. }
  217. break;
  218. case AV_CODEC_ID_INTERPLAY_DPCM:
  219. bytestream2_skipu(&gb, 6); /* skip over the stream mask and stream length */
  220. for (ch = 0; ch < s->channels; ch++) {
  221. predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
  222. *output_samples++ = predictor[ch];
  223. }
  224. ch = 0;
  225. while (output_samples < samples_end) {
  226. predictor[ch] += interplay_delta_table[bytestream2_get_byteu(&gb)];
  227. predictor[ch] = av_clip_int16(predictor[ch]);
  228. *output_samples++ = predictor[ch];
  229. /* toggle channel */
  230. ch ^= stereo;
  231. }
  232. break;
  233. case AV_CODEC_ID_XAN_DPCM:
  234. {
  235. int shift[2] = { 4, 4 };
  236. for (ch = 0; ch < s->channels; ch++)
  237. predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
  238. ch = 0;
  239. while (output_samples < samples_end) {
  240. int diff = bytestream2_get_byteu(&gb);
  241. int n = diff & 3;
  242. if (n == 3)
  243. shift[ch]++;
  244. else
  245. shift[ch] -= (2 * n);
  246. diff = sign_extend((diff &~ 3) << 8, 16);
  247. /* saturate the shifter to a lower limit of 0 */
  248. if (shift[ch] < 0)
  249. shift[ch] = 0;
  250. diff >>= shift[ch];
  251. predictor[ch] += diff;
  252. predictor[ch] = av_clip_int16(predictor[ch]);
  253. *output_samples++ = predictor[ch];
  254. /* toggle channel */
  255. ch ^= stereo;
  256. }
  257. break;
  258. }
  259. case AV_CODEC_ID_SOL_DPCM:
  260. if (avctx->codec_tag != 3) {
  261. uint8_t *output_samples_u8 = s->frame.data[0],
  262. *samples_end_u8 = output_samples_u8 + out;
  263. while (output_samples_u8 < samples_end_u8) {
  264. int n = bytestream2_get_byteu(&gb);
  265. s->sample[0] += s->sol_table[n >> 4];
  266. s->sample[0] = av_clip_uint8(s->sample[0]);
  267. *output_samples_u8++ = s->sample[0];
  268. s->sample[stereo] += s->sol_table[n & 0x0F];
  269. s->sample[stereo] = av_clip_uint8(s->sample[stereo]);
  270. *output_samples_u8++ = s->sample[stereo];
  271. }
  272. } else {
  273. while (output_samples < samples_end) {
  274. int n = bytestream2_get_byteu(&gb);
  275. if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F];
  276. else s->sample[ch] += sol_table_16[n & 0x7F];
  277. s->sample[ch] = av_clip_int16(s->sample[ch]);
  278. *output_samples++ = s->sample[ch];
  279. /* toggle channel */
  280. ch ^= stereo;
  281. }
  282. }
  283. break;
  284. }
  285. *got_frame_ptr = 1;
  286. *(AVFrame *)data = s->frame;
  287. return avpkt->size;
  288. }
  289. #define DPCM_DECODER(id_, name_, long_name_) \
  290. AVCodec ff_ ## name_ ## _decoder = { \
  291. .name = #name_, \
  292. .type = AVMEDIA_TYPE_AUDIO, \
  293. .id = id_, \
  294. .priv_data_size = sizeof(DPCMContext), \
  295. .init = dpcm_decode_init, \
  296. .decode = dpcm_decode_frame, \
  297. .capabilities = CODEC_CAP_DR1, \
  298. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  299. }
  300. DPCM_DECODER(AV_CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay");
  301. DPCM_DECODER(AV_CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ");
  302. DPCM_DECODER(AV_CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol");
  303. DPCM_DECODER(AV_CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan");