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.

177 lines
5.2KB

  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. 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. }
  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. if(AV_RB16(buf) == 0x8000){
  96. int header_size;
  97. if ((ret = avpriv_adx_decode_header(avctx, buf,
  98. 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. if(buf_size < header_size)
  105. return AVERROR_INVALIDDATA;
  106. buf += header_size;
  107. buf_size -= header_size;
  108. }
  109. if(c->channels <= 0)
  110. return AVERROR_INVALIDDATA;
  111. /* calculate number of blocks in the packet */
  112. num_blocks = buf_size / (BLOCK_SIZE * c->channels);
  113. /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
  114. packet */
  115. if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
  116. if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
  117. c->eof = 1;
  118. *got_frame_ptr = 0;
  119. return avpkt->size;
  120. }
  121. return AVERROR_INVALIDDATA;
  122. }
  123. /* get output buffer */
  124. c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
  125. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  126. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  127. return ret;
  128. }
  129. samples = (int16_t *)c->frame.data[0];
  130. while (num_blocks--) {
  131. for (ch = 0; ch < c->channels; ch++) {
  132. if (adx_decode(c, samples + ch, buf, ch)) {
  133. c->eof = 1;
  134. buf = avpkt->data + avpkt->size;
  135. break;
  136. }
  137. buf_size -= BLOCK_SIZE;
  138. buf += BLOCK_SIZE;
  139. }
  140. samples += BLOCK_SAMPLES * c->channels;
  141. }
  142. *got_frame_ptr = 1;
  143. *(AVFrame *)data = c->frame;
  144. return buf - avpkt->data;
  145. }
  146. AVCodec ff_adpcm_adx_decoder = {
  147. .name = "adpcm_adx",
  148. .type = AVMEDIA_TYPE_AUDIO,
  149. .id = CODEC_ID_ADPCM_ADX,
  150. .priv_data_size = sizeof(ADXContext),
  151. .init = adx_decode_init,
  152. .decode = adx_decode_frame,
  153. .capabilities = CODEC_CAP_DR1,
  154. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  155. };