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.

186 lines
5.4KB

  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. if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
  39. avctx->extradata_size, &header_size,
  40. c->coeff)) < 0) {
  41. av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. c->channels = avctx->channels;
  45. c->header_parsed = 1;
  46. }
  47. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  48. avcodec_get_frame_defaults(&c->frame);
  49. avctx->coded_frame = &c->frame;
  50. return 0;
  51. }
  52. /**
  53. * Decode 32 samples from 18 bytes.
  54. *
  55. * A 16-bit scalar value is applied to 32 residuals, which then have a
  56. * 2nd-order LPC filter applied to it to form the output signal for a single
  57. * channel.
  58. */
  59. static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
  60. {
  61. ADXChannelState *prev = &c->prev[ch];
  62. GetBitContext gb;
  63. int scale = AV_RB16(in);
  64. int i;
  65. int s0, s1, s2, d;
  66. /* check if this is an EOF packet */
  67. if (scale & 0x8000)
  68. return -1;
  69. init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
  70. s1 = prev->s1;
  71. s2 = prev->s2;
  72. for (i = 0; i < BLOCK_SAMPLES; i++) {
  73. d = get_sbits(&gb, 4);
  74. s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
  75. s2 = s1;
  76. s1 = av_clip_int16(s0);
  77. *out = s1;
  78. out += c->channels;
  79. }
  80. prev->s1 = s1;
  81. prev->s2 = s2;
  82. return 0;
  83. }
  84. static int adx_decode_frame(AVCodecContext *avctx, void *data,
  85. int *got_frame_ptr, AVPacket *avpkt)
  86. {
  87. int buf_size = avpkt->size;
  88. ADXContext *c = avctx->priv_data;
  89. int16_t *samples;
  90. const uint8_t *buf = avpkt->data;
  91. int num_blocks, ch, ret;
  92. if (c->eof) {
  93. *got_frame_ptr = 0;
  94. return buf_size;
  95. }
  96. if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
  97. int header_size;
  98. if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size,
  99. c->coeff)) < 0) {
  100. av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
  101. return AVERROR_INVALIDDATA;
  102. }
  103. c->channels = avctx->channels;
  104. c->header_parsed = 1;
  105. if (buf_size < header_size)
  106. return AVERROR_INVALIDDATA;
  107. buf += header_size;
  108. buf_size -= header_size;
  109. }
  110. if (!c->header_parsed)
  111. return AVERROR_INVALIDDATA;
  112. /* calculate number of blocks in the packet */
  113. num_blocks = buf_size / (BLOCK_SIZE * c->channels);
  114. /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
  115. packet */
  116. if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
  117. if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
  118. c->eof = 1;
  119. *got_frame_ptr = 0;
  120. return avpkt->size;
  121. }
  122. return AVERROR_INVALIDDATA;
  123. }
  124. /* get output buffer */
  125. c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
  126. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  127. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  128. return ret;
  129. }
  130. samples = (int16_t *)c->frame.data[0];
  131. while (num_blocks--) {
  132. for (ch = 0; ch < c->channels; ch++) {
  133. if (adx_decode(c, samples + ch, buf, ch)) {
  134. c->eof = 1;
  135. buf = avpkt->data + avpkt->size;
  136. break;
  137. }
  138. buf_size -= BLOCK_SIZE;
  139. buf += BLOCK_SIZE;
  140. }
  141. samples += BLOCK_SAMPLES * c->channels;
  142. }
  143. *got_frame_ptr = 1;
  144. *(AVFrame *)data = c->frame;
  145. return buf - avpkt->data;
  146. }
  147. static void adx_decode_flush(AVCodecContext *avctx)
  148. {
  149. ADXContext *c = avctx->priv_data;
  150. memset(c->prev, 0, sizeof(c->prev));
  151. c->eof = 0;
  152. }
  153. AVCodec ff_adpcm_adx_decoder = {
  154. .name = "adpcm_adx",
  155. .type = AVMEDIA_TYPE_AUDIO,
  156. .id = CODEC_ID_ADPCM_ADX,
  157. .priv_data_size = sizeof(ADXContext),
  158. .init = adx_decode_init,
  159. .decode = adx_decode_frame,
  160. .flush = adx_decode_flush,
  161. .capabilities = CODEC_CAP_DR1,
  162. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  163. };