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.

196 lines
5.8KB

  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/common.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "avcodec.h"
  25. /**
  26. * @file
  27. * Westwood SNDx codecs
  28. *
  29. * Reference documents about VQA format and its audio codecs
  30. * can be found here:
  31. * http://www.multimedia.cx
  32. */
  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. };
  37. typedef struct WSSndContext {
  38. AVFrame frame;
  39. } WSSndContext;
  40. static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
  41. {
  42. WSSndContext *s = avctx->priv_data;
  43. if (avctx->channels != 1) {
  44. av_log_ask_for_sample(avctx, "unsupported number of channels\n");
  45. return AVERROR(EINVAL);
  46. }
  47. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  48. avcodec_get_frame_defaults(&s->frame);
  49. avctx->coded_frame = &s->frame;
  50. return 0;
  51. }
  52. static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
  53. int *got_frame_ptr, AVPacket *avpkt)
  54. {
  55. WSSndContext *s = avctx->priv_data;
  56. const uint8_t *buf = avpkt->data;
  57. int buf_size = avpkt->size;
  58. int in_size, out_size, ret;
  59. int sample = 128;
  60. uint8_t *samples;
  61. uint8_t *samples_end;
  62. if (!buf_size)
  63. return 0;
  64. if (buf_size < 4) {
  65. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  66. return AVERROR(EINVAL);
  67. }
  68. out_size = AV_RL16(&buf[0]);
  69. in_size = AV_RL16(&buf[2]);
  70. buf += 4;
  71. if (in_size > buf_size) {
  72. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  73. return -1;
  74. }
  75. /* get output buffer */
  76. s->frame.nb_samples = out_size;
  77. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  78. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  79. return ret;
  80. }
  81. samples = s->frame.data[0];
  82. samples_end = samples + out_size;
  83. if (in_size == out_size) {
  84. memcpy(samples, buf, out_size);
  85. *got_frame_ptr = 1;
  86. *(AVFrame *)data = s->frame;
  87. return buf_size;
  88. }
  89. while (samples < samples_end && buf - avpkt->data < buf_size) {
  90. int code, smp, size;
  91. uint8_t count;
  92. code = *buf >> 6;
  93. count = *buf & 0x3F;
  94. buf++;
  95. /* make sure we don't write past the output buffer */
  96. switch (code) {
  97. case 0: smp = 4 * (count + 1); break;
  98. case 1: smp = 2 * (count + 1); break;
  99. case 2: smp = (count & 0x20) ? 1 : count + 1; break;
  100. default: smp = count + 1; break;
  101. }
  102. if (samples_end - samples < smp)
  103. break;
  104. /* make sure we don't read past the input buffer */
  105. size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
  106. if ((buf - avpkt->data) + size > buf_size)
  107. break;
  108. switch (code) {
  109. case 0: /* ADPCM 2-bit */
  110. for (count++; count > 0; count--) {
  111. code = *buf++;
  112. sample += ( code & 0x3) - 2;
  113. sample = av_clip_uint8(sample);
  114. *samples++ = sample;
  115. sample += ((code >> 2) & 0x3) - 2;
  116. sample = av_clip_uint8(sample);
  117. *samples++ = sample;
  118. sample += ((code >> 4) & 0x3) - 2;
  119. sample = av_clip_uint8(sample);
  120. *samples++ = sample;
  121. sample += (code >> 6) - 2;
  122. sample = av_clip_uint8(sample);
  123. *samples++ = sample;
  124. }
  125. break;
  126. case 1: /* ADPCM 4-bit */
  127. for (count++; count > 0; count--) {
  128. code = *buf++;
  129. sample += ws_adpcm_4bit[code & 0xF];
  130. sample = av_clip_uint8(sample);
  131. *samples++ = sample;
  132. sample += ws_adpcm_4bit[code >> 4];
  133. sample = av_clip_uint8(sample);
  134. *samples++ = sample;
  135. }
  136. break;
  137. case 2: /* no compression */
  138. if (count & 0x20) { /* big delta */
  139. int8_t t;
  140. t = count;
  141. t <<= 3;
  142. sample += t >> 3;
  143. sample = av_clip_uint8(sample);
  144. *samples++ = sample;
  145. } else { /* copy */
  146. memcpy(samples, buf, smp);
  147. samples += smp;
  148. buf += smp;
  149. sample = buf[-1];
  150. }
  151. break;
  152. default: /* run */
  153. memset(samples, sample, smp);
  154. samples += smp;
  155. }
  156. }
  157. s->frame.nb_samples = samples - s->frame.data[0];
  158. *got_frame_ptr = 1;
  159. *(AVFrame *)data = s->frame;
  160. return buf_size;
  161. }
  162. AVCodec ff_ws_snd1_decoder = {
  163. .name = "ws_snd1",
  164. .type = AVMEDIA_TYPE_AUDIO,
  165. .id = AV_CODEC_ID_WESTWOOD_SND1,
  166. .priv_data_size = sizeof(WSSndContext),
  167. .init = ws_snd_decode_init,
  168. .decode = ws_snd_decode_frame,
  169. .capabilities = CODEC_CAP_DR1,
  170. .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
  171. };