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.

189 lines
5.6KB

  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. #include "internal.h"
  26. /**
  27. * @file
  28. * SEGA CRI adx codecs.
  29. *
  30. * Reference documents:
  31. * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
  32. * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
  33. */
  34. static av_cold int adx_decode_init(AVCodecContext *avctx)
  35. {
  36. ADXContext *c = avctx->priv_data;
  37. int ret, header_size;
  38. if (avctx->extradata_size >= 24) {
  39. if ((ret = ff_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. c->header_parsed = 1;
  47. }
  48. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  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, int offset,
  59. 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. out += offset;
  71. s1 = prev->s1;
  72. s2 = prev->s2;
  73. for (i = 0; i < BLOCK_SAMPLES; i++) {
  74. d = get_sbits(&gb, 4);
  75. s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
  76. s2 = s1;
  77. s1 = av_clip_int16(s0);
  78. *out++ = s1;
  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. AVFrame *frame = data;
  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 = ff_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. frame->nb_samples = num_blocks * BLOCK_SAMPLES;
  128. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  129. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  130. return ret;
  131. }
  132. samples = (int16_t **)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. return buf - avpkt->data;
  148. }
  149. static void adx_decode_flush(AVCodecContext *avctx)
  150. {
  151. ADXContext *c = avctx->priv_data;
  152. memset(c->prev, 0, sizeof(c->prev));
  153. c->eof = 0;
  154. }
  155. AVCodec ff_adpcm_adx_decoder = {
  156. .name = "adpcm_adx",
  157. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  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 = AV_CODEC_CAP_DR1,
  165. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  166. AV_SAMPLE_FMT_NONE },
  167. };