ws-snd1.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 libavcodec/ws-snd1.c
  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. const uint8_t *buf, int buf_size)
  45. {
  46. // WSSNDContext *c = avctx->priv_data;
  47. int in_size, out_size;
  48. int sample = 0;
  49. int i;
  50. short *samples = data;
  51. if (!buf_size)
  52. return 0;
  53. out_size = AV_RL16(&buf[0]);
  54. *data_size = out_size * 2;
  55. in_size = AV_RL16(&buf[2]);
  56. buf += 4;
  57. if (out_size > *data_size) {
  58. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  59. return -1;
  60. }
  61. if (in_size > buf_size) {
  62. av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
  63. return -1;
  64. }
  65. if (in_size == out_size) {
  66. for (i = 0; i < out_size; i++)
  67. *samples++ = (*buf++ - 0x80) << 8;
  68. return buf_size;
  69. }
  70. while (out_size > 0) {
  71. int code;
  72. uint8_t count;
  73. code = (*buf) >> 6;
  74. count = (*buf) & 0x3F;
  75. buf++;
  76. switch(code) {
  77. case 0: /* ADPCM 2-bit */
  78. for (count++; count > 0; count--) {
  79. code = *buf++;
  80. sample += ws_adpcm_2bit[code & 0x3];
  81. CLIP8(sample);
  82. *samples++ = sample << 8;
  83. sample += ws_adpcm_2bit[(code >> 2) & 0x3];
  84. CLIP8(sample);
  85. *samples++ = sample << 8;
  86. sample += ws_adpcm_2bit[(code >> 4) & 0x3];
  87. CLIP8(sample);
  88. *samples++ = sample << 8;
  89. sample += ws_adpcm_2bit[(code >> 6) & 0x3];
  90. CLIP8(sample);
  91. *samples++ = sample << 8;
  92. out_size -= 4;
  93. }
  94. break;
  95. case 1: /* ADPCM 4-bit */
  96. for (count++; count > 0; count--) {
  97. code = *buf++;
  98. sample += ws_adpcm_4bit[code & 0xF];
  99. CLIP8(sample);
  100. *samples++ = sample << 8;
  101. sample += ws_adpcm_4bit[code >> 4];
  102. CLIP8(sample);
  103. *samples++ = sample << 8;
  104. out_size -= 2;
  105. }
  106. break;
  107. case 2: /* no compression */
  108. if (count & 0x20) { /* big delta */
  109. char t;
  110. t = count;
  111. t <<= 3;
  112. sample += t >> 3;
  113. *samples++ = sample << 8;
  114. out_size--;
  115. } else { /* copy */
  116. for (count++; count > 0; count--) {
  117. *samples++ = (*buf++ - 0x80) << 8;
  118. out_size--;
  119. }
  120. sample = buf[-1] - 0x80;
  121. }
  122. break;
  123. default: /* run */
  124. for(count++; count > 0; count--) {
  125. *samples++ = sample << 8;
  126. out_size--;
  127. }
  128. }
  129. }
  130. return buf_size;
  131. }
  132. AVCodec ws_snd1_decoder = {
  133. "ws_snd1",
  134. CODEC_TYPE_AUDIO,
  135. CODEC_ID_WESTWOOD_SND1,
  136. 0,
  137. ws_snd_decode_init,
  138. NULL,
  139. NULL,
  140. ws_snd_decode_frame,
  141. .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
  142. };