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.

193 lines
5.8KB

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