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.

161 lines
4.6KB

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