adxdec.c 4.3 KB

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