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.

234 lines
7.8KB

  1. /*
  2. * Sierra VMD audio decoder
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Sierra VMD audio decoder
  23. * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
  24. * for more information on the Sierra VMD format, visit:
  25. * http://www.pcisys.net/~melanson/codecs/
  26. *
  27. * The audio decoder, expects each encoded data
  28. * chunk to be prepended with the appropriate 16-byte frame information
  29. * record from the VMD file. It does not require the 0x330-byte VMD file
  30. * header, but it does need the audio setup parameters passed in through
  31. * normal libavcodec API means.
  32. */
  33. #include <string.h>
  34. #include "libavutil/channel_layout.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/intreadwrite.h"
  37. #include "avcodec.h"
  38. #include "internal.h"
  39. #define BLOCK_TYPE_AUDIO 1
  40. #define BLOCK_TYPE_INITIAL 2
  41. #define BLOCK_TYPE_SILENCE 3
  42. typedef struct VmdAudioContext {
  43. int out_bps;
  44. int chunk_size;
  45. } VmdAudioContext;
  46. static const uint16_t vmdaudio_table[128] = {
  47. 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
  48. 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
  49. 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
  50. 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
  51. 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
  52. 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
  53. 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
  54. 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
  55. 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
  56. 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
  57. 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
  58. 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
  59. 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
  60. };
  61. static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
  62. {
  63. VmdAudioContext *s = avctx->priv_data;
  64. if (avctx->channels < 1 || avctx->channels > 2) {
  65. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  66. return AVERROR(EINVAL);
  67. }
  68. if (avctx->block_align < 1) {
  69. av_log(avctx, AV_LOG_ERROR, "invalid block align\n");
  70. return AVERROR(EINVAL);
  71. }
  72. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
  73. AV_CH_LAYOUT_STEREO;
  74. if (avctx->bits_per_coded_sample == 16)
  75. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  76. else
  77. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  78. s->out_bps = av_get_bytes_per_sample(avctx->sample_fmt);
  79. s->chunk_size = avctx->block_align + avctx->channels * (s->out_bps == 2);
  80. av_log(avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, "
  81. "block align = %d, sample rate = %d\n",
  82. avctx->channels, avctx->bits_per_coded_sample, avctx->block_align,
  83. avctx->sample_rate);
  84. return 0;
  85. }
  86. static void decode_audio_s16(int16_t *out, const uint8_t *buf, int buf_size,
  87. int channels)
  88. {
  89. int ch;
  90. const uint8_t *buf_end = buf + buf_size;
  91. int predictor[2];
  92. int st = channels - 1;
  93. /* decode initial raw sample */
  94. for (ch = 0; ch < channels; ch++) {
  95. predictor[ch] = (int16_t)AV_RL16(buf);
  96. buf += 2;
  97. *out++ = predictor[ch];
  98. }
  99. /* decode DPCM samples */
  100. ch = 0;
  101. while (buf < buf_end) {
  102. uint8_t b = *buf++;
  103. if (b & 0x80)
  104. predictor[ch] -= vmdaudio_table[b & 0x7F];
  105. else
  106. predictor[ch] += vmdaudio_table[b];
  107. predictor[ch] = av_clip_int16(predictor[ch]);
  108. *out++ = predictor[ch];
  109. ch ^= st;
  110. }
  111. }
  112. static int vmdaudio_decode_frame(AVCodecContext *avctx, void *data,
  113. int *got_frame_ptr, AVPacket *avpkt)
  114. {
  115. AVFrame *frame = data;
  116. const uint8_t *buf = avpkt->data;
  117. const uint8_t *buf_end;
  118. int buf_size = avpkt->size;
  119. VmdAudioContext *s = avctx->priv_data;
  120. int block_type, silent_chunks, audio_chunks;
  121. int ret;
  122. uint8_t *output_samples_u8;
  123. int16_t *output_samples_s16;
  124. if (buf_size < 16) {
  125. av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");
  126. *got_frame_ptr = 0;
  127. return buf_size;
  128. }
  129. block_type = buf[6];
  130. if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {
  131. av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);
  132. return AVERROR(EINVAL);
  133. }
  134. buf += 16;
  135. buf_size -= 16;
  136. /* get number of silent chunks */
  137. silent_chunks = 0;
  138. if (block_type == BLOCK_TYPE_INITIAL) {
  139. uint32_t flags;
  140. if (buf_size < 4) {
  141. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  142. return AVERROR(EINVAL);
  143. }
  144. flags = AV_RB32(buf);
  145. silent_chunks = av_popcount(flags);
  146. buf += 4;
  147. buf_size -= 4;
  148. } else if (block_type == BLOCK_TYPE_SILENCE) {
  149. silent_chunks = 1;
  150. buf_size = 0; // should already be zero but set it just to be sure
  151. }
  152. /* ensure output buffer is large enough */
  153. audio_chunks = buf_size / s->chunk_size;
  154. /* drop incomplete chunks */
  155. buf_size = audio_chunks * s->chunk_size;
  156. /* get output buffer */
  157. frame->nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) /
  158. avctx->channels;
  159. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  160. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  161. return ret;
  162. }
  163. output_samples_u8 = frame->data[0];
  164. output_samples_s16 = (int16_t *)frame->data[0];
  165. /* decode silent chunks */
  166. if (silent_chunks > 0) {
  167. int silent_size = FFMIN(avctx->block_align * silent_chunks,
  168. frame->nb_samples * avctx->channels);
  169. if (s->out_bps == 2) {
  170. memset(output_samples_s16, 0x00, silent_size * 2);
  171. output_samples_s16 += silent_size;
  172. } else {
  173. memset(output_samples_u8, 0x80, silent_size);
  174. output_samples_u8 += silent_size;
  175. }
  176. }
  177. /* decode audio chunks */
  178. if (audio_chunks > 0) {
  179. buf_end = buf + (buf_size & ~(avctx->channels > 1));
  180. while (buf + s->chunk_size <= buf_end) {
  181. if (s->out_bps == 2) {
  182. decode_audio_s16(output_samples_s16, buf, s->chunk_size,
  183. avctx->channels);
  184. output_samples_s16 += avctx->block_align;
  185. } else {
  186. memcpy(output_samples_u8, buf, s->chunk_size);
  187. output_samples_u8 += avctx->block_align;
  188. }
  189. buf += s->chunk_size;
  190. }
  191. }
  192. *got_frame_ptr = 1;
  193. return avpkt->size;
  194. }
  195. AVCodec ff_vmdaudio_decoder = {
  196. .name = "vmdaudio",
  197. .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD audio"),
  198. .type = AVMEDIA_TYPE_AUDIO,
  199. .id = AV_CODEC_ID_VMDAUDIO,
  200. .priv_data_size = sizeof(VmdAudioContext),
  201. .init = vmdaudio_decode_init,
  202. .decode = vmdaudio_decode_frame,
  203. .capabilities = AV_CODEC_CAP_DR1,
  204. };