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.

184 lines
5.5KB

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