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.

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