adxdec.c 4.5 KB

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