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.

189 lines
5.7KB

  1. /*
  2. * ADX ADPCM codecs
  3. * Copyright (c) 2001,2003 BERO
  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 "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. #include "adx.h"
  24. #include "get_bits.h"
  25. /**
  26. * @file
  27. * SEGA CRI adx codecs.
  28. *
  29. * Reference documents:
  30. * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
  31. * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
  32. */
  33. #define BLOCK_SIZE 18
  34. #define BLOCK_SAMPLES 32
  35. static av_cold int adx_decode_init(AVCodecContext *avctx)
  36. {
  37. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  38. return 0;
  39. }
  40. /**
  41. * Decode 32 samples from 18 bytes.
  42. *
  43. * A 16-bit scalar value is applied to 32 residuals, which then have a
  44. * 2nd-order LPC filter applied to it to form the output signal for a single
  45. * channel.
  46. */
  47. static void adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
  48. {
  49. ADXChannelState *prev = &c->prev[ch];
  50. GetBitContext gb;
  51. int scale = AV_RB16(in);
  52. int i;
  53. int s0, s1, s2, d;
  54. init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
  55. s1 = prev->s1;
  56. s2 = prev->s2;
  57. for (i = 0; i < BLOCK_SAMPLES; i++) {
  58. d = get_sbits(&gb, 4);
  59. s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
  60. s2 = s1;
  61. s1 = av_clip_int16(s0);
  62. *out = s1;
  63. out += c->channels;
  64. }
  65. prev->s1 = s1;
  66. prev->s2 = s2;
  67. }
  68. /**
  69. * Decode stream header.
  70. *
  71. * @param avctx codec context
  72. * @param buf packet data
  73. * @param bufsize packet size
  74. * @return data offset or negative error code if header is invalid
  75. */
  76. static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
  77. int bufsize)
  78. {
  79. ADXContext *c = avctx->priv_data;
  80. int offset, cutoff;
  81. if (AV_RB16(buf) != 0x8000)
  82. return AVERROR_INVALIDDATA;
  83. offset = AV_RB16(buf + 2) + 4;
  84. if (bufsize < offset || memcmp(buf + offset - 6, "(c)CRI", 6))
  85. return AVERROR_INVALIDDATA;
  86. /* check for encoding=3 block_size=18, sample_size=4 */
  87. if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
  88. av_log_ask_for_sample(avctx, "unsupported ADX format\n");
  89. return AVERROR_PATCHWELCOME;
  90. }
  91. c->channels = avctx->channels = buf[7];
  92. if (avctx->channels > 2)
  93. return AVERROR_INVALIDDATA;
  94. avctx->sample_rate = AV_RB32(buf + 8);
  95. if (avctx->sample_rate < 1 ||
  96. avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
  97. return AVERROR_INVALIDDATA;
  98. avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
  99. cutoff = AV_RB16(buf + 16);
  100. ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
  101. return offset;
  102. }
  103. static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  104. AVPacket *avpkt)
  105. {
  106. const uint8_t *buf0 = avpkt->data;
  107. int buf_size = avpkt->size;
  108. ADXContext *c = avctx->priv_data;
  109. int16_t *samples = data;
  110. const uint8_t *buf = buf0;
  111. int rest = buf_size;
  112. int num_blocks;
  113. if (!c->header_parsed) {
  114. int hdrsize = adx_decode_header(avctx, buf, rest);
  115. if (hdrsize < 0) {
  116. av_log(avctx, AV_LOG_ERROR, "invalid stream header\n");
  117. return hdrsize;
  118. }
  119. c->header_parsed = 1;
  120. buf += hdrsize;
  121. rest -= hdrsize;
  122. }
  123. /* 18 bytes of data are expanded into 32*2 bytes of audio,
  124. so guard against buffer overflows */
  125. num_blocks = (rest + c->in_temp) / (BLOCK_SIZE * c->channels);
  126. if (num_blocks > *data_size / (BLOCK_SAMPLES * c->channels)) {
  127. rest = (*data_size / (BLOCK_SAMPLES * c->channels)) * BLOCK_SIZE;
  128. num_blocks = (rest + c->in_temp) / (BLOCK_SIZE * c->channels);
  129. }
  130. if (!num_blocks) {
  131. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  132. return AVERROR_INVALIDDATA;
  133. }
  134. if (c->in_temp) {
  135. int copysize = BLOCK_SIZE * avctx->channels - c->in_temp;
  136. memcpy(c->dec_temp + c->in_temp, buf, copysize);
  137. rest -= copysize;
  138. buf += copysize;
  139. adx_decode(c, samples, c->dec_temp, 0);
  140. if (avctx->channels == 2)
  141. adx_decode(c, samples + 1, c->dec_temp + BLOCK_SIZE, 1);
  142. samples += BLOCK_SAMPLES * c->channels;
  143. num_blocks--;
  144. }
  145. while (num_blocks--) {
  146. adx_decode(c, samples, buf, 0);
  147. if (c->channels == 2)
  148. adx_decode(c, samples + 1, buf + BLOCK_SIZE, 1);
  149. rest -= BLOCK_SIZE * c->channels;
  150. buf += BLOCK_SIZE * c->channels;
  151. samples += BLOCK_SAMPLES * c->channels;
  152. }
  153. c->in_temp = rest;
  154. if (rest) {
  155. memcpy(c->dec_temp, buf, rest);
  156. buf += rest;
  157. }
  158. *data_size = (uint8_t*)samples - (uint8_t*)data;
  159. return buf - buf0;
  160. }
  161. AVCodec ff_adpcm_adx_decoder = {
  162. .name = "adpcm_adx",
  163. .type = AVMEDIA_TYPE_AUDIO,
  164. .id = CODEC_ID_ADPCM_ADX,
  165. .priv_data_size = sizeof(ADXContext),
  166. .init = adx_decode_init,
  167. .decode = adx_decode_frame,
  168. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  169. };