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.

159 lines
4.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 "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. /**
  24. * @file
  25. * Westwood SNDx codecs.
  26. *
  27. * Reference documents about VQA format and its audio codecs
  28. * can be found here:
  29. * http://www.multimedia.cx
  30. */
  31. static const char ws_adpcm_2bit[] = { -2, -1, 0, 1};
  32. static const char ws_adpcm_4bit[] = {
  33. -9, -8, -6, -5, -4, -3, -2, -1,
  34. 0, 1, 2, 3, 4, 5, 6, 8 };
  35. #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
  36. static av_cold int ws_snd_decode_init(AVCodecContext * avctx)
  37. {
  38. // WSSNDContext *c = avctx->priv_data;
  39. avctx->sample_fmt = SAMPLE_FMT_S16;
  40. return 0;
  41. }
  42. static int ws_snd_decode_frame(AVCodecContext *avctx,
  43. void *data, int *data_size,
  44. AVPacket *avpkt)
  45. {
  46. const uint8_t *buf = avpkt->data;
  47. int buf_size = avpkt->size;
  48. // WSSNDContext *c = avctx->priv_data;
  49. int in_size, out_size;
  50. int sample = 0;
  51. int i;
  52. short *samples = data;
  53. if (!buf_size)
  54. return 0;
  55. out_size = AV_RL16(&buf[0]);
  56. *data_size = out_size * 2;
  57. in_size = AV_RL16(&buf[2]);
  58. buf += 4;
  59. if (out_size > *data_size) {
  60. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  61. return -1;
  62. }
  63. if (in_size > buf_size) {
  64. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  65. return -1;
  66. }
  67. if (in_size == out_size) {
  68. for (i = 0; i < out_size; i++)
  69. *samples++ = (*buf++ - 0x80) << 8;
  70. return buf_size;
  71. }
  72. while (out_size > 0) {
  73. int code;
  74. uint8_t count;
  75. code = (*buf) >> 6;
  76. count = (*buf) & 0x3F;
  77. buf++;
  78. switch(code) {
  79. case 0: /* ADPCM 2-bit */
  80. for (count++; count > 0; count--) {
  81. code = *buf++;
  82. sample += ws_adpcm_2bit[code & 0x3];
  83. CLIP8(sample);
  84. *samples++ = sample << 8;
  85. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  86. CLIP8(sample);
  87. *samples++ = sample << 8;
  88. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  89. CLIP8(sample);
  90. *samples++ = sample << 8;
  91. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  92. CLIP8(sample);
  93. *samples++ = sample << 8;
  94. out_size -= 4;
  95. }
  96. break;
  97. case 1: /* ADPCM 4-bit */
  98. for (count++; count > 0; count--) {
  99. code = *buf++;
  100. sample += ws_adpcm_4bit[code & 0xF];
  101. CLIP8(sample);
  102. *samples++ = sample << 8;
  103. sample += ws_adpcm_4bit[code >> 4];
  104. CLIP8(sample);
  105. *samples++ = sample << 8;
  106. out_size -= 2;
  107. }
  108. break;
  109. case 2: /* no compression */
  110. if (count & 0x20) { /* big delta */
  111. char t;
  112. t = count;
  113. t <<= 3;
  114. sample += t >> 3;
  115. *samples++ = sample << 8;
  116. out_size--;
  117. } else { /* copy */
  118. for (count++; count > 0; count--) {
  119. *samples++ = (*buf++ - 0x80) << 8;
  120. out_size--;
  121. }
  122. sample = buf[-1] - 0x80;
  123. }
  124. break;
  125. default: /* run */
  126. for(count++; count > 0; count--) {
  127. *samples++ = sample << 8;
  128. out_size--;
  129. }
  130. }
  131. }
  132. return buf_size;
  133. }
  134. AVCodec ws_snd1_decoder = {
  135. "ws_snd1",
  136. AVMEDIA_TYPE_AUDIO,
  137. CODEC_ID_WESTWOOD_SND1,
  138. 0,
  139. ws_snd_decode_init,
  140. NULL,
  141. NULL,
  142. ws_snd_decode_frame,
  143. .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
  144. };