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.

187 lines
5.5KB

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