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.

145 lines
4.0KB

  1. /*
  2. * Westwood SNDx codecs
  3. * Copyright (c) 2005 Konstantin Shishkov
  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. #include "avcodec.h"
  22. /**
  23. * @file ws-snd.c
  24. * Westwood SNDx codecs.
  25. *
  26. * Reference documents about VQA format and its audio codecs
  27. * can be found here:
  28. * http://www.multimedia.cx
  29. */
  30. static const char ws_adpcm_2bit[] = { -2, -1, 0, 1};
  31. static const char ws_adpcm_4bit[] = {
  32. -9, -8, -6, -5, -4, -3, -2, -1,
  33. 0, 1, 2, 3, 4, 5, 6, 8 };
  34. #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
  35. static int ws_snd_decode_init(AVCodecContext * avctx)
  36. {
  37. // WSSNDContext *c = avctx->priv_data;
  38. return 0;
  39. }
  40. static int ws_snd_decode_frame(AVCodecContext *avctx,
  41. void *data, int *data_size,
  42. uint8_t *buf, int buf_size)
  43. {
  44. // WSSNDContext *c = avctx->priv_data;
  45. int in_size, out_size;
  46. int sample = 0;
  47. int i;
  48. short *samples = data;
  49. if (!buf_size)
  50. return 0;
  51. out_size = AV_RL16(&buf[0]);
  52. *data_size = out_size * 2;
  53. in_size = AV_RL16(&buf[2]);
  54. buf += 4;
  55. if (in_size == out_size) {
  56. for (i = 0; i < out_size; i++)
  57. *samples++ = (*buf++ - 0x80) << 8;
  58. return buf_size;
  59. }
  60. while (out_size > 0) {
  61. int code;
  62. uint8_t count;
  63. code = (*buf) >> 6;
  64. count = (*buf) & 0x3F;
  65. buf++;
  66. switch(code) {
  67. case 0: /* ADPCM 2-bit */
  68. for (count++; count > 0; count--) {
  69. code = *buf++;
  70. sample += ws_adpcm_2bit[code & 0x3];
  71. CLIP8(sample);
  72. *samples++ = sample << 8;
  73. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  74. CLIP8(sample);
  75. *samples++ = sample << 8;
  76. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  77. CLIP8(sample);
  78. *samples++ = sample << 8;
  79. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  80. CLIP8(sample);
  81. *samples++ = sample << 8;
  82. out_size -= 4;
  83. }
  84. break;
  85. case 1: /* ADPCM 4-bit */
  86. for (count++; count > 0; count--) {
  87. code = *buf++;
  88. sample += ws_adpcm_4bit[code & 0xF];
  89. CLIP8(sample);
  90. *samples++ = sample << 8;
  91. sample += ws_adpcm_4bit[code >> 4];
  92. CLIP8(sample);
  93. *samples++ = sample << 8;
  94. out_size -= 2;
  95. }
  96. break;
  97. case 2: /* no compression */
  98. if (count & 0x20) { /* big delta */
  99. char t;
  100. t = count;
  101. t <<= 3;
  102. sample += t >> 3;
  103. *samples++ = sample << 8;
  104. out_size--;
  105. } else { /* copy */
  106. for (count++; count > 0; count--) {
  107. *samples++ = (*buf++ - 0x80) << 8;
  108. out_size--;
  109. }
  110. sample = buf[-1] - 0x80;
  111. }
  112. break;
  113. default: /* run */
  114. for(count++; count > 0; count--) {
  115. *samples++ = sample << 8;
  116. out_size--;
  117. }
  118. }
  119. }
  120. return buf_size;
  121. }
  122. AVCodec ws_snd1_decoder = {
  123. "ws_snd1",
  124. CODEC_TYPE_AUDIO,
  125. CODEC_ID_WESTWOOD_SND1,
  126. 0,
  127. ws_snd_decode_init,
  128. NULL,
  129. NULL,
  130. ws_snd_decode_frame,
  131. };