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.

160 lines
4.5KB

  1. /*
  2. * Westwood SNDx codecs
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "avcodec.h"
  24. /**
  25. * @file
  26. * Westwood SNDx codecs.
  27. *
  28. * Reference documents about VQA format and its audio codecs
  29. * can be found here:
  30. * http://www.multimedia.cx
  31. */
  32. static const int8_t ws_adpcm_2bit[] = { -2, -1, 0, 1};
  33. static const int8_t 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 av_cold int ws_snd_decode_init(AVCodecContext * avctx)
  38. {
  39. // WSSNDContext *c = avctx->priv_data;
  40. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  41. return 0;
  42. }
  43. static int ws_snd_decode_frame(AVCodecContext *avctx,
  44. void *data, int *data_size,
  45. AVPacket *avpkt)
  46. {
  47. const uint8_t *buf = avpkt->data;
  48. int buf_size = avpkt->size;
  49. // WSSNDContext *c = avctx->priv_data;
  50. int in_size, out_size;
  51. int sample = 0;
  52. int i;
  53. short *samples = data;
  54. if (!buf_size)
  55. return 0;
  56. out_size = AV_RL16(&buf[0]);
  57. *data_size = out_size * 2;
  58. in_size = AV_RL16(&buf[2]);
  59. buf += 4;
  60. if (out_size > *data_size) {
  61. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  62. return -1;
  63. }
  64. if (in_size > buf_size) {
  65. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  66. return -1;
  67. }
  68. if (in_size == out_size) {
  69. for (i = 0; i < out_size; i++)
  70. *samples++ = (*buf++ - 0x80) << 8;
  71. return buf_size;
  72. }
  73. while (out_size > 0) {
  74. int code;
  75. uint8_t count;
  76. code = (*buf) >> 6;
  77. count = (*buf) & 0x3F;
  78. buf++;
  79. switch(code) {
  80. case 0: /* ADPCM 2-bit */
  81. for (count++; count > 0; count--) {
  82. code = *buf++;
  83. sample += ws_adpcm_2bit[code & 0x3];
  84. CLIP8(sample);
  85. *samples++ = sample << 8;
  86. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  87. CLIP8(sample);
  88. *samples++ = sample << 8;
  89. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  90. CLIP8(sample);
  91. *samples++ = sample << 8;
  92. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  93. CLIP8(sample);
  94. *samples++ = sample << 8;
  95. out_size -= 4;
  96. }
  97. break;
  98. case 1: /* ADPCM 4-bit */
  99. for (count++; count > 0; count--) {
  100. code = *buf++;
  101. sample += ws_adpcm_4bit[code & 0xF];
  102. CLIP8(sample);
  103. *samples++ = sample << 8;
  104. sample += ws_adpcm_4bit[code >> 4];
  105. CLIP8(sample);
  106. *samples++ = sample << 8;
  107. out_size -= 2;
  108. }
  109. break;
  110. case 2: /* no compression */
  111. if (count & 0x20) { /* big delta */
  112. int8_t t;
  113. t = count;
  114. t <<= 3;
  115. sample += t >> 3;
  116. *samples++ = sample << 8;
  117. out_size--;
  118. } else { /* copy */
  119. for (count++; count > 0; count--) {
  120. *samples++ = (*buf++ - 0x80) << 8;
  121. out_size--;
  122. }
  123. sample = buf[-1] - 0x80;
  124. }
  125. break;
  126. default: /* run */
  127. for(count++; count > 0; count--) {
  128. *samples++ = sample << 8;
  129. out_size--;
  130. }
  131. }
  132. }
  133. return buf_size;
  134. }
  135. AVCodec ff_ws_snd1_decoder = {
  136. "ws_snd1",
  137. AVMEDIA_TYPE_AUDIO,
  138. CODEC_ID_WESTWOOD_SND1,
  139. 0,
  140. ws_snd_decode_init,
  141. NULL,
  142. NULL,
  143. ws_snd_decode_frame,
  144. .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
  145. };