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.

153 lines
4.3KB

  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 av_cold 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. const 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 (out_size > *data_size) {
  56. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  57. return -1;
  58. }
  59. if (in_size > buf_size) {
  60. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  61. return -1;
  62. }
  63. if (in_size == out_size) {
  64. for (i = 0; i < out_size; i++)
  65. *samples++ = (*buf++ - 0x80) << 8;
  66. return buf_size;
  67. }
  68. while (out_size > 0) {
  69. int code;
  70. uint8_t count;
  71. code = (*buf) >> 6;
  72. count = (*buf) & 0x3F;
  73. buf++;
  74. switch(code) {
  75. case 0: /* ADPCM 2-bit */
  76. for (count++; count > 0; count--) {
  77. code = *buf++;
  78. sample += ws_adpcm_2bit[code & 0x3];
  79. CLIP8(sample);
  80. *samples++ = sample << 8;
  81. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  82. CLIP8(sample);
  83. *samples++ = sample << 8;
  84. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  85. CLIP8(sample);
  86. *samples++ = sample << 8;
  87. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  88. CLIP8(sample);
  89. *samples++ = sample << 8;
  90. out_size -= 4;
  91. }
  92. break;
  93. case 1: /* ADPCM 4-bit */
  94. for (count++; count > 0; count--) {
  95. code = *buf++;
  96. sample += ws_adpcm_4bit[code & 0xF];
  97. CLIP8(sample);
  98. *samples++ = sample << 8;
  99. sample += ws_adpcm_4bit[code >> 4];
  100. CLIP8(sample);
  101. *samples++ = sample << 8;
  102. out_size -= 2;
  103. }
  104. break;
  105. case 2: /* no compression */
  106. if (count & 0x20) { /* big delta */
  107. char t;
  108. t = count;
  109. t <<= 3;
  110. sample += t >> 3;
  111. *samples++ = sample << 8;
  112. out_size--;
  113. } else { /* copy */
  114. for (count++; count > 0; count--) {
  115. *samples++ = (*buf++ - 0x80) << 8;
  116. out_size--;
  117. }
  118. sample = buf[-1] - 0x80;
  119. }
  120. break;
  121. default: /* run */
  122. for(count++; count > 0; count--) {
  123. *samples++ = sample << 8;
  124. out_size--;
  125. }
  126. }
  127. }
  128. return buf_size;
  129. }
  130. AVCodec ws_snd1_decoder = {
  131. "ws_snd1",
  132. CODEC_TYPE_AUDIO,
  133. CODEC_ID_WESTWOOD_SND1,
  134. 0,
  135. ws_snd_decode_init,
  136. NULL,
  137. NULL,
  138. ws_snd_decode_frame,
  139. };