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.

191 lines
5.7KB

  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_S16P;
  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, int offset,
  60. const uint8_t *in, int ch)
  61. {
  62. ADXChannelState *prev = &c->prev[ch];
  63. GetBitContext gb;
  64. int scale = AV_RB16(in);
  65. int i;
  66. int s0, s1, s2, d;
  67. /* check if this is an EOF packet */
  68. if (scale & 0x8000)
  69. return -1;
  70. init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
  71. out += offset;
  72. s1 = prev->s1;
  73. s2 = prev->s2;
  74. for (i = 0; i < BLOCK_SAMPLES; i++) {
  75. d = get_sbits(&gb, 4);
  76. s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
  77. s2 = s1;
  78. s1 = av_clip_int16(s0);
  79. *out++ = s1;
  80. }
  81. prev->s1 = s1;
  82. prev->s2 = s2;
  83. return 0;
  84. }
  85. static int adx_decode_frame(AVCodecContext *avctx, void *data,
  86. int *got_frame_ptr, AVPacket *avpkt)
  87. {
  88. int buf_size = avpkt->size;
  89. ADXContext *c = avctx->priv_data;
  90. int16_t **samples;
  91. int samples_offset;
  92. const uint8_t *buf = avpkt->data;
  93. int num_blocks, ch, ret;
  94. if (c->eof) {
  95. *got_frame_ptr = 0;
  96. return buf_size;
  97. }
  98. if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
  99. int header_size;
  100. if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size,
  101. c->coeff)) < 0) {
  102. av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
  103. return AVERROR_INVALIDDATA;
  104. }
  105. c->channels = avctx->channels;
  106. c->header_parsed = 1;
  107. if (buf_size < header_size)
  108. return AVERROR_INVALIDDATA;
  109. buf += header_size;
  110. buf_size -= header_size;
  111. }
  112. if (!c->header_parsed)
  113. return AVERROR_INVALIDDATA;
  114. /* calculate number of blocks in the packet */
  115. num_blocks = buf_size / (BLOCK_SIZE * c->channels);
  116. /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
  117. packet */
  118. if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
  119. if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
  120. c->eof = 1;
  121. *got_frame_ptr = 0;
  122. return avpkt->size;
  123. }
  124. return AVERROR_INVALIDDATA;
  125. }
  126. /* get output buffer */
  127. c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
  128. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  129. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  130. return ret;
  131. }
  132. samples = (int16_t **)c->frame.extended_data;
  133. samples_offset = 0;
  134. while (num_blocks--) {
  135. for (ch = 0; ch < c->channels; ch++) {
  136. if (adx_decode(c, samples[ch], samples_offset, buf, ch)) {
  137. c->eof = 1;
  138. buf = avpkt->data + avpkt->size;
  139. break;
  140. }
  141. buf_size -= BLOCK_SIZE;
  142. buf += BLOCK_SIZE;
  143. }
  144. samples_offset += BLOCK_SAMPLES;
  145. }
  146. *got_frame_ptr = 1;
  147. *(AVFrame *)data = c->frame;
  148. return buf - avpkt->data;
  149. }
  150. static void adx_decode_flush(AVCodecContext *avctx)
  151. {
  152. ADXContext *c = avctx->priv_data;
  153. memset(c->prev, 0, sizeof(c->prev));
  154. c->eof = 0;
  155. }
  156. AVCodec ff_adpcm_adx_decoder = {
  157. .name = "adpcm_adx",
  158. .type = AVMEDIA_TYPE_AUDIO,
  159. .id = AV_CODEC_ID_ADPCM_ADX,
  160. .priv_data_size = sizeof(ADXContext),
  161. .init = adx_decode_init,
  162. .decode = adx_decode_frame,
  163. .flush = adx_decode_flush,
  164. .capabilities = CODEC_CAP_DR1,
  165. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  166. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  167. AV_SAMPLE_FMT_NONE },
  168. };