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.

148 lines
4.1KB

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