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.

148 lines
4.3KB

  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. static av_cold int adx_decode_init(AVCodecContext *avctx)
  34. {
  35. ADXContext *c = avctx->priv_data;
  36. int ret, header_size;
  37. if (avctx->extradata_size < 24)
  38. return AVERROR_INVALIDDATA;
  39. if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
  40. avctx->extradata_size, &header_size,
  41. c->coeff)) < 0) {
  42. av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
  43. return AVERROR_INVALIDDATA;
  44. }
  45. c->channels = avctx->channels;
  46. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  47. return 0;
  48. }
  49. /**
  50. * Decode 32 samples from 18 bytes.
  51. *
  52. * A 16-bit scalar value is applied to 32 residuals, which then have a
  53. * 2nd-order LPC filter applied to it to form the output signal for a single
  54. * channel.
  55. */
  56. static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
  57. {
  58. ADXChannelState *prev = &c->prev[ch];
  59. GetBitContext gb;
  60. int scale = AV_RB16(in);
  61. int i;
  62. int s0, s1, s2, d;
  63. /* check if this is an EOF packet */
  64. if (scale & 0x8000)
  65. return -1;
  66. init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
  67. s1 = prev->s1;
  68. s2 = prev->s2;
  69. for (i = 0; i < BLOCK_SAMPLES; i++) {
  70. d = get_sbits(&gb, 4);
  71. s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
  72. s2 = s1;
  73. s1 = av_clip_int16(s0);
  74. *out = s1;
  75. out += c->channels;
  76. }
  77. prev->s1 = s1;
  78. prev->s2 = s2;
  79. return 0;
  80. }
  81. static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  82. AVPacket *avpkt)
  83. {
  84. int buf_size = avpkt->size;
  85. ADXContext *c = avctx->priv_data;
  86. int16_t *samples = data;
  87. const uint8_t *buf = avpkt->data;
  88. int num_blocks, ch;
  89. if (c->eof) {
  90. *data_size = 0;
  91. return buf_size;
  92. }
  93. /* 18 bytes of data are expanded into 32*2 bytes of audio,
  94. so guard against buffer overflows */
  95. num_blocks = buf_size / (BLOCK_SIZE * c->channels);
  96. if (num_blocks > *data_size / (BLOCK_SAMPLES * c->channels)) {
  97. buf_size = (*data_size / (BLOCK_SAMPLES * c->channels)) * BLOCK_SIZE;
  98. num_blocks = buf_size / (BLOCK_SIZE * c->channels);
  99. }
  100. if (!buf_size || buf_size % (BLOCK_SIZE * avctx->channels)) {
  101. if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
  102. c->eof = 1;
  103. *data_size = 0;
  104. return avpkt->size;
  105. }
  106. return AVERROR_INVALIDDATA;
  107. }
  108. while (num_blocks--) {
  109. for (ch = 0; ch < c->channels; ch++) {
  110. if (adx_decode(c, samples + ch, buf, ch)) {
  111. c->eof = 1;
  112. buf = avpkt->data + avpkt->size;
  113. break;
  114. }
  115. buf_size -= BLOCK_SIZE;
  116. buf += BLOCK_SIZE;
  117. }
  118. samples += BLOCK_SAMPLES * c->channels;
  119. }
  120. *data_size = (uint8_t*)samples - (uint8_t*)data;
  121. return buf - avpkt->data;
  122. }
  123. AVCodec ff_adpcm_adx_decoder = {
  124. .name = "adpcm_adx",
  125. .type = AVMEDIA_TYPE_AUDIO,
  126. .id = CODEC_ID_ADPCM_ADX,
  127. .priv_data_size = sizeof(ADXContext),
  128. .init = adx_decode_init,
  129. .decode = adx_decode_frame,
  130. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  131. };