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.

190 lines
4.2KB

  1. /*
  2. * CD-ROM XA ADPCM codecs
  3. * Copyright (c) 1999,2003 BERO
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. /**
  21. * @file xa.c
  22. * CD-ROM XA ADPCM codecs.
  23. *
  24. * Reference documents:
  25. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  26. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  27. * readstr http://www.geocities.co.jp/Playtown/2004/
  28. */
  29. typedef struct {
  30. int s1,s2;
  31. } PREV;
  32. typedef struct {
  33. PREV prev[2];
  34. } XAContext;
  35. #define CLIP(s) if (s<-32768) s=-32768; else if (s>32767) s=32767
  36. static void xa_decode(short *out,const unsigned char *in,PREV *prev,int inc)
  37. {
  38. const static int f[5][2] = {
  39. {0,0},
  40. {60,0},
  41. {115,-52},
  42. {98,-55},
  43. {122,-60}
  44. };
  45. int i,j;
  46. for(i=0;i<4;i++) {
  47. int shift,filter,f0,f1;
  48. int s_1,s_2;
  49. shift = 12 - (in[4+i*2] & 15);
  50. filter = in[4+i*2] >> 4;
  51. f0 = f[filter][0];
  52. f1 = f[filter][1];
  53. s_1 = prev->s1;
  54. s_2 = prev->s2;
  55. for(j=0;j<28;j++) {
  56. int d,s,t;
  57. d = in[16+i+j*4];
  58. //t = d&0xf;
  59. //if (t>=8) t-=16;
  60. t = (signed char)(d<<4)>>4;
  61. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  62. CLIP(s);
  63. *out = s;
  64. out += inc;
  65. s_2 = s_1;
  66. s_1 = s;
  67. }
  68. if (inc==2) { /* stereo */
  69. prev->s1 = s_1;
  70. prev->s2 = s_2;
  71. s_1 = prev[1].s1;
  72. s_2 = prev[1].s2;
  73. out = out + 1 - 28*2;
  74. }
  75. shift = 12 - (in[5+i*2] & 15);
  76. filter = in[5+i*2] >> 4;
  77. f0 = f[filter][0];
  78. f1 = f[filter][1];
  79. for(j=0;j<28;j++) {
  80. int d,s,t;
  81. d = in[16+i+j*4];
  82. //t = d>>4;
  83. //if (t>=8) t-=16;
  84. t = (signed char)d >> 4;
  85. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  86. CLIP(s);
  87. *out = s;
  88. out += inc;
  89. s_2 = s_1;
  90. s_1 = s;
  91. }
  92. if (inc==2) { /* stereo */
  93. prev[1].s1 = s_1;
  94. prev[1].s2 = s_2;
  95. out -= 1;
  96. } else {
  97. prev[0].s1 = s_1;
  98. prev[0].s2 = s_2;
  99. }
  100. }
  101. }
  102. static int xa_decode_init(AVCodecContext * avctx)
  103. {
  104. XAContext *c = avctx->priv_data;
  105. printf("xa init %d\n",avctx->channels);
  106. c->prev[0].s1 = 0;
  107. c->prev[0].s2 = 0;
  108. c->prev[1].s1 = 0;
  109. c->prev[1].s2 = 0;
  110. switch(avctx->channels) {
  111. case 1:
  112. case 2:
  113. return 0;
  114. default:
  115. return -1;
  116. }
  117. }
  118. static int xa_decode_frame(AVCodecContext *avctx,
  119. void *data, int *data_size,
  120. const uint8_t *buf0, int buf_size)
  121. {
  122. XAContext *c = avctx->priv_data;
  123. short *samples = data;
  124. const uint8_t *buf = buf0;
  125. // printf("xa decode %d\n",buf_size);
  126. c->prev[0].s1 = 0;
  127. c->prev[0].s2 = 0;
  128. c->prev[1].s1 = 0;
  129. c->prev[1].s2 = 0;
  130. while(buf_size>=128) {
  131. xa_decode(samples,buf,c->prev,avctx->channels);
  132. buf+=128;
  133. samples += 28*8;
  134. buf_size-=128;
  135. }
  136. *data_size = (char*)samples - (char*)data;
  137. return buf-buf0;
  138. }
  139. #define DEFINE_ENCODER(id, name,context) \
  140. AVCodec name ## _encoder = { \
  141. #name, \
  142. CODEC_TYPE_AUDIO, \
  143. id, \
  144. sizeof(context), \
  145. name ## _encode_init, \
  146. name ## _encode_frame, \
  147. name ## _encode_close, \
  148. NULL, \
  149. };
  150. #define DEFINE_DECODER(id, name,context) \
  151. AVCodec name ## _decoder = { \
  152. #name, \
  153. CODEC_TYPE_AUDIO, \
  154. id, \
  155. sizeof(context), \
  156. name ## _decode_init, \
  157. NULL, \
  158. NULL, \
  159. name ## _decode_frame, \
  160. };
  161. DEFINE_DECODER(CODEC_ID_ADPCM_XA,xa,XAContext)