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.

190 lines
5.5KB

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