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.

175 lines
5.2KB

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