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.

181 lines
4.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. /**
  25. * @file
  26. * SEGA CRI adx codecs.
  27. *
  28. * Reference documents:
  29. * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
  30. * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
  31. */
  32. static av_cold int adx_decode_init(AVCodecContext *avctx)
  33. {
  34. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  35. return 0;
  36. }
  37. /* 18 bytes <-> 32 samples */
  38. static void adx_decode(int16_t *out,const unsigned char *in,
  39. ADXChannelState *prev)
  40. {
  41. int scale = AV_RB16(in);
  42. int i;
  43. int s0,s1,s2,d;
  44. // printf("%x ",scale);
  45. in+=2;
  46. s1 = prev->s1;
  47. s2 = prev->s2;
  48. for(i=0;i<16;i++) {
  49. d = in[i];
  50. // d>>=4; if (d&8) d-=16;
  51. d = ((signed char)d >> 4);
  52. s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
  53. s2 = s1;
  54. s1 = av_clip_int16(s0);
  55. *out++=s1;
  56. d = in[i];
  57. //d&=15; if (d&8) d-=16;
  58. d = ((signed char)(d<<4) >> 4);
  59. s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
  60. s2 = s1;
  61. s1 = av_clip_int16(s0);
  62. *out++=s1;
  63. }
  64. prev->s1 = s1;
  65. prev->s2 = s2;
  66. }
  67. static void adx_decode_stereo(int16_t *out,const unsigned char *in,
  68. ADXChannelState *prev)
  69. {
  70. short tmp[32*2];
  71. int i;
  72. adx_decode(tmp ,in ,prev);
  73. adx_decode(tmp+32,in+18,prev+1);
  74. for(i=0;i<32;i++) {
  75. out[i*2] = tmp[i];
  76. out[i*2+1] = tmp[i+32];
  77. }
  78. }
  79. /* return data offset or 0 */
  80. static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
  81. {
  82. int offset;
  83. if (buf[0]!=0x80) return 0;
  84. offset = (AV_RB32(buf)^0x80000000)+4;
  85. if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
  86. avctx->channels = buf[7];
  87. avctx->sample_rate = AV_RB32(buf+8);
  88. avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
  89. return offset;
  90. }
  91. static int adx_decode_frame(AVCodecContext *avctx,
  92. void *data, int *data_size,
  93. AVPacket *avpkt)
  94. {
  95. const uint8_t *buf0 = avpkt->data;
  96. int buf_size = avpkt->size;
  97. ADXContext *c = avctx->priv_data;
  98. int16_t *samples = data;
  99. const uint8_t *buf = buf0;
  100. int rest = buf_size;
  101. if (!c->header_parsed) {
  102. int hdrsize = adx_decode_header(avctx,buf,rest);
  103. if (hdrsize==0) return -1;
  104. c->header_parsed = 1;
  105. buf += hdrsize;
  106. rest -= hdrsize;
  107. }
  108. /* 18 bytes of data are expanded into 32*2 bytes of audio,
  109. so guard against buffer overflows */
  110. if(rest/18 > *data_size/64)
  111. rest = (*data_size/64) * 18;
  112. if (c->in_temp) {
  113. int copysize = 18*avctx->channels - c->in_temp;
  114. memcpy(c->dec_temp+c->in_temp,buf,copysize);
  115. rest -= copysize;
  116. buf += copysize;
  117. if (avctx->channels==1) {
  118. adx_decode(samples,c->dec_temp,c->prev);
  119. samples += 32;
  120. } else {
  121. adx_decode_stereo(samples,c->dec_temp,c->prev);
  122. samples += 32*2;
  123. }
  124. }
  125. //
  126. if (avctx->channels==1) {
  127. while(rest>=18) {
  128. adx_decode(samples,buf,c->prev);
  129. rest-=18;
  130. buf+=18;
  131. samples+=32;
  132. }
  133. } else {
  134. while(rest>=18*2) {
  135. adx_decode_stereo(samples,buf,c->prev);
  136. rest-=18*2;
  137. buf+=18*2;
  138. samples+=32*2;
  139. }
  140. }
  141. //
  142. c->in_temp = rest;
  143. if (rest) {
  144. memcpy(c->dec_temp,buf,rest);
  145. buf+=rest;
  146. }
  147. *data_size = (uint8_t*)samples - (uint8_t*)data;
  148. // printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
  149. return buf-buf0;
  150. }
  151. AVCodec ff_adpcm_adx_decoder = {
  152. .name = "adpcm_adx",
  153. .type = AVMEDIA_TYPE_AUDIO,
  154. .id = CODEC_ID_ADPCM_ADX,
  155. .priv_data_size = sizeof(ADXContext),
  156. .init = adx_decode_init,
  157. .decode = adx_decode_frame,
  158. .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
  159. };