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.

185 lines
5.4KB

  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. static av_cold int ws_snd_decode_init(AVCodecContext * avctx)
  37. {
  38. // WSSNDContext *c = avctx->priv_data;
  39. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  40. return 0;
  41. }
  42. static int ws_snd_decode_frame(AVCodecContext *avctx,
  43. void *data, int *data_size,
  44. AVPacket *avpkt)
  45. {
  46. const uint8_t *buf = avpkt->data;
  47. int buf_size = avpkt->size;
  48. // WSSNDContext *c = avctx->priv_data;
  49. int in_size, out_size;
  50. int sample = 128;
  51. int i;
  52. uint8_t *samples = data;
  53. if (!buf_size)
  54. return 0;
  55. if (buf_size < 4) {
  56. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  57. return AVERROR(EINVAL);
  58. }
  59. out_size = AV_RL16(&buf[0]);
  60. in_size = AV_RL16(&buf[2]);
  61. buf += 4;
  62. if (out_size > *data_size) {
  63. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  64. return -1;
  65. }
  66. if (in_size > buf_size) {
  67. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  68. return -1;
  69. }
  70. if (in_size == out_size) {
  71. for (i = 0; i < out_size; i++)
  72. *samples++ = *buf++;
  73. *data_size = out_size;
  74. return buf_size;
  75. }
  76. while (out_size > 0 && buf - avpkt->data < buf_size) {
  77. int code, smp, size;
  78. uint8_t count;
  79. code = (*buf) >> 6;
  80. count = (*buf) & 0x3F;
  81. buf++;
  82. /* make sure we don't write more than out_size samples */
  83. switch (code) {
  84. case 0: smp = 4*(count+1); break;
  85. case 1: smp = 2*(count+1); break;
  86. case 2: smp = (count & 0x20) ? 1 : count + 1; break;
  87. default: smp = count + 1; break;
  88. }
  89. if (out_size < smp) {
  90. out_size = 0;
  91. break;
  92. }
  93. /* make sure we don't read past the input buffer */
  94. size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
  95. if ((buf - avpkt->data) + size > buf_size)
  96. break;
  97. switch(code) {
  98. case 0: /* ADPCM 2-bit */
  99. for (count++; count > 0; count--) {
  100. code = *buf++;
  101. sample += ws_adpcm_2bit[code & 0x3];
  102. sample = av_clip_uint8(sample);
  103. *samples++ = sample;
  104. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  105. sample = av_clip_uint8(sample);
  106. *samples++ = sample;
  107. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  108. sample = av_clip_uint8(sample);
  109. *samples++ = sample;
  110. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  111. sample = av_clip_uint8(sample);
  112. *samples++ = sample;
  113. out_size -= 4;
  114. }
  115. break;
  116. case 1: /* ADPCM 4-bit */
  117. for (count++; count > 0; count--) {
  118. code = *buf++;
  119. sample += ws_adpcm_4bit[code & 0xF];
  120. sample = av_clip_uint8(sample);
  121. *samples++ = sample;
  122. sample += ws_adpcm_4bit[code >> 4];
  123. sample = av_clip_uint8(sample);
  124. *samples++ = sample;
  125. out_size -= 2;
  126. }
  127. break;
  128. case 2: /* no compression */
  129. if (count & 0x20) { /* big delta */
  130. int8_t t;
  131. t = count;
  132. t <<= 3;
  133. sample += t >> 3;
  134. sample = av_clip_uint8(sample);
  135. *samples++ = sample;
  136. out_size--;
  137. } else { /* copy */
  138. for (count++; count > 0; count--) {
  139. *samples++ = *buf++;
  140. out_size--;
  141. }
  142. sample = buf[-1];
  143. }
  144. break;
  145. default: /* run */
  146. for(count++; count > 0; count--) {
  147. *samples++ = sample;
  148. out_size--;
  149. }
  150. }
  151. }
  152. *data_size = samples - (uint8_t *)data;
  153. return buf_size;
  154. }
  155. AVCodec ff_ws_snd1_decoder = {
  156. "ws_snd1",
  157. AVMEDIA_TYPE_AUDIO,
  158. CODEC_ID_WESTWOOD_SND1,
  159. 0,
  160. ws_snd_decode_init,
  161. NULL,
  162. NULL,
  163. ws_snd_decode_frame,
  164. .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
  165. };