decode_ntom.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Mpeg Layer-1,2,3 audio decoder
  3. * ------------------------------
  4. * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
  5. * See also 'README'
  6. *
  7. * N->M down/up sampling. Not optimized for speed.
  8. */
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include "mpg123.h"
  13. #define WRITE_SAMPLE(samples,sum,clip) \
  14. if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
  15. else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
  16. else { *(samples) = sum; }
  17. #define NTOM_MUL (32768)
  18. static unsigned long ntom_val[2] = { NTOM_MUL>>1,NTOM_MUL>>1 };
  19. static unsigned long ntom_step = NTOM_MUL;
  20. void synth_ntom_set_step(long m,long n)
  21. {
  22. if(param.verbose > 1)
  23. fprintf(stderr,"Init rate converter: %ld->%ld\n",m,n);
  24. if(n >= 96000 || m >= 96000 || m == 0 || n == 0) {
  25. fprintf(stderr,"NtoM converter: illegal rates\n");
  26. exit(1);
  27. }
  28. n *= NTOM_MUL;
  29. ntom_step = n / m;
  30. if(ntom_step > 8*NTOM_MUL) {
  31. fprintf(stderr,"max. 1:8 conversion allowed!\n");
  32. exit(1);
  33. }
  34. ntom_val[0] = ntom_val[1] = NTOM_MUL>>1;
  35. }
  36. int synth_ntom_8bit(real *bandPtr,int channel,unsigned char *samples,int *pnt)
  37. {
  38. short samples_tmp[8*64];
  39. short *tmp1 = samples_tmp + channel;
  40. int i,ret;
  41. int pnt1 = 0;
  42. ret = synth_ntom(bandPtr,channel,(unsigned char *) samples_tmp,&pnt1);
  43. samples += channel + *pnt;
  44. for(i=0;i<(pnt1>>2);i++) {
  45. *samples = conv16to8[*tmp1>>AUSHIFT];
  46. samples += 2;
  47. tmp1 += 2;
  48. }
  49. *pnt += pnt1>>1;
  50. return ret;
  51. }
  52. int synth_ntom_8bit_mono(real *bandPtr,unsigned char *samples,int *pnt)
  53. {
  54. short samples_tmp[8*64];
  55. short *tmp1 = samples_tmp;
  56. int i,ret;
  57. int pnt1 = 0;
  58. ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
  59. samples += *pnt;
  60. for(i=0;i<(pnt1>>2);i++) {
  61. *samples++ = conv16to8[*tmp1>>AUSHIFT];
  62. tmp1 += 2;
  63. }
  64. *pnt += pnt1 >> 2;
  65. return ret;
  66. }
  67. int synth_ntom_8bit_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
  68. {
  69. short samples_tmp[8*64];
  70. short *tmp1 = samples_tmp;
  71. int i,ret;
  72. int pnt1 = 0;
  73. ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
  74. samples += *pnt;
  75. for(i=0;i<(pnt1>>2);i++) {
  76. *samples++ = conv16to8[*tmp1>>AUSHIFT];
  77. *samples++ = conv16to8[*tmp1>>AUSHIFT];
  78. tmp1 += 2;
  79. }
  80. *pnt += pnt1 >> 1;
  81. return ret;
  82. }
  83. int synth_ntom_mono(real *bandPtr,unsigned char *samples,int *pnt)
  84. {
  85. short samples_tmp[8*64];
  86. short *tmp1 = samples_tmp;
  87. int i,ret;
  88. int pnt1 = 0;
  89. ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
  90. samples += *pnt;
  91. for(i=0;i<(pnt1>>2);i++) {
  92. *( (short *)samples) = *tmp1;
  93. samples += 2;
  94. tmp1 += 2;
  95. }
  96. *pnt += pnt1 >> 1;
  97. return ret;
  98. }
  99. int synth_ntom_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
  100. {
  101. int i,ret;
  102. int pnt1 = *pnt;
  103. ret = synth_ntom(bandPtr,0,samples,pnt);
  104. samples += pnt1;
  105. for(i=0;i<((*pnt-pnt1)>>2);i++) {
  106. ((short *)samples)[1] = ((short *)samples)[0];
  107. samples+=4;
  108. }
  109. return ret;
  110. }
  111. int synth_ntom(real *bandPtr,int channel,unsigned char *out,int *pnt)
  112. {
  113. static real buffs[2][2][0x110];
  114. static const int step = 2;
  115. static int bo = 1;
  116. short *samples = (short *) (out + *pnt);
  117. real *b0,(*buf)[0x110];
  118. int clip = 0;
  119. int bo1;
  120. int ntom;
  121. if(equalfile)
  122. do_equalizer(bandPtr,channel);
  123. if(!channel) {
  124. bo--;
  125. bo &= 0xf;
  126. buf = buffs[0];
  127. ntom = ntom_val[1] = ntom_val[0];
  128. }
  129. else {
  130. samples++;
  131. out += 2; /* to compute the right *pnt value */
  132. buf = buffs[1];
  133. ntom = ntom_val[1];
  134. }
  135. if(bo & 0x1) {
  136. b0 = buf[0];
  137. bo1 = bo;
  138. dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
  139. }
  140. else {
  141. b0 = buf[1];
  142. bo1 = bo+1;
  143. dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
  144. }
  145. {
  146. register int j;
  147. real *window = decwin + 16 - bo1;
  148. for (j=16;j;j--,window+=0x10)
  149. {
  150. real sum;
  151. ntom += ntom_step;
  152. if(ntom < NTOM_MUL) {
  153. window += 16;
  154. b0 += 16;
  155. continue;
  156. }
  157. sum = *window++ * *b0++;
  158. sum -= *window++ * *b0++;
  159. sum += *window++ * *b0++;
  160. sum -= *window++ * *b0++;
  161. sum += *window++ * *b0++;
  162. sum -= *window++ * *b0++;
  163. sum += *window++ * *b0++;
  164. sum -= *window++ * *b0++;
  165. sum += *window++ * *b0++;
  166. sum -= *window++ * *b0++;
  167. sum += *window++ * *b0++;
  168. sum -= *window++ * *b0++;
  169. sum += *window++ * *b0++;
  170. sum -= *window++ * *b0++;
  171. sum += *window++ * *b0++;
  172. sum -= *window++ * *b0++;
  173. while(ntom >= NTOM_MUL) {
  174. WRITE_SAMPLE(samples,sum,clip);
  175. samples += step;
  176. ntom -= NTOM_MUL;
  177. }
  178. }
  179. ntom += ntom_step;
  180. if(ntom >= NTOM_MUL)
  181. {
  182. real sum;
  183. sum = window[0x0] * b0[0x0];
  184. sum += window[0x2] * b0[0x2];
  185. sum += window[0x4] * b0[0x4];
  186. sum += window[0x6] * b0[0x6];
  187. sum += window[0x8] * b0[0x8];
  188. sum += window[0xA] * b0[0xA];
  189. sum += window[0xC] * b0[0xC];
  190. sum += window[0xE] * b0[0xE];
  191. while(ntom >= NTOM_MUL) {
  192. WRITE_SAMPLE(samples,sum,clip);
  193. samples += step;
  194. ntom -= NTOM_MUL;
  195. }
  196. }
  197. b0-=0x10,window-=0x20;
  198. window += bo1<<1;
  199. for (j=15;j;j--,b0-=0x20,window-=0x10)
  200. {
  201. real sum;
  202. ntom += ntom_step;
  203. if(ntom < NTOM_MUL) {
  204. window -= 16;
  205. b0 += 16;
  206. continue;
  207. }
  208. sum = -*(--window) * *b0++;
  209. sum -= *(--window) * *b0++;
  210. sum -= *(--window) * *b0++;
  211. sum -= *(--window) * *b0++;
  212. sum -= *(--window) * *b0++;
  213. sum -= *(--window) * *b0++;
  214. sum -= *(--window) * *b0++;
  215. sum -= *(--window) * *b0++;
  216. sum -= *(--window) * *b0++;
  217. sum -= *(--window) * *b0++;
  218. sum -= *(--window) * *b0++;
  219. sum -= *(--window) * *b0++;
  220. sum -= *(--window) * *b0++;
  221. sum -= *(--window) * *b0++;
  222. sum -= *(--window) * *b0++;
  223. sum -= *(--window) * *b0++;
  224. while(ntom >= NTOM_MUL) {
  225. WRITE_SAMPLE(samples,sum,clip);
  226. samples += step;
  227. ntom -= NTOM_MUL;
  228. }
  229. }
  230. }
  231. ntom_val[channel] = ntom;
  232. *pnt = ((unsigned char *) samples - out);
  233. return clip;
  234. }