ra288.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * RealAudio 2.0 (28.8K)
  3. * Copyright (c) 2003 the ffmpeg project
  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 "ra288.h"
  23. typedef struct {
  24. float history[8];
  25. float output[40];
  26. float pr1[36];
  27. float pr2[10];
  28. int phase, phasep;
  29. float st1a[111],st1b[37],st1[37];
  30. float st2a[38],st2b[11],st2[11];
  31. float sb[41];
  32. float lhist[10];
  33. } Real288_internal;
  34. static int ra288_decode_init(AVCodecContext * avctx)
  35. {
  36. Real288_internal *glob=avctx->priv_data;
  37. memset(glob,0,sizeof(Real288_internal));
  38. return 0;
  39. }
  40. static void prodsum(float *tgt, float *src, int len, int n);
  41. static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table);
  42. static int pred(float *in, float *tgt, int n);
  43. static void colmult(float *tgt, float *m1, const float *m2, int n);
  44. /* initial decode */
  45. static void unpack(unsigned short *tgt, unsigned char *src, unsigned int len)
  46. {
  47. int x,y,z;
  48. int n,temp;
  49. int buffer[len];
  50. for (x=0;x<len;tgt[x++]=0)
  51. buffer[x]=9+(x&1);
  52. for (x=y=z=0;x<len/*was 38*/;x++) {
  53. n=buffer[y]-z;
  54. temp=src[x];
  55. if (n<8) temp&=255>>(8-n);
  56. tgt[y]+=temp<<z;
  57. if (n<=8) {
  58. tgt[++y]+=src[x]>>n;
  59. z=8-n;
  60. } else z+=8;
  61. }
  62. }
  63. static void update(Real288_internal *glob)
  64. {
  65. int x,y;
  66. float buffer1[40],temp1[37];
  67. float buffer2[8],temp2[11];
  68. for (x=0,y=glob->phasep+5;x<40;buffer1[x++]=glob->output[(y++)%40]);
  69. co(36,40,35,buffer1,temp1,glob->st1a,glob->st1b,table1);
  70. if (pred(temp1,glob->st1,36))
  71. colmult(glob->pr1,glob->st1,table1a,36);
  72. for (x=0,y=glob->phase+1;x<8;buffer2[x++]=glob->history[(y++)%8]);
  73. co(10,8,20,buffer2,temp2,glob->st2a,glob->st2b,table2);
  74. if (pred(temp2,glob->st2,10))
  75. colmult(glob->pr2,glob->st2,table2a,10);
  76. }
  77. /* Decode and produce output */
  78. static void decode(Real288_internal *glob, unsigned int input)
  79. {
  80. unsigned int x,y;
  81. float f;
  82. double sum,sumsum;
  83. float *p1,*p2;
  84. float buffer[5];
  85. const float *table;
  86. for (x=36;x--;glob->sb[x+5]=glob->sb[x]);
  87. for (x=5;x--;) {
  88. p1=glob->sb+x;p2=glob->pr1;
  89. for (sum=0,y=36;y--;sum-=(*(++p1))*(*(p2++)));
  90. glob->sb[x]=sum;
  91. }
  92. f=amptable[input&7];
  93. table=codetable+(input>>3)*5;
  94. /* convert log and do rms */
  95. for (sum=32,x=10;x--;sum-=glob->pr2[x]*glob->lhist[x]);
  96. if (sum<0) sum=0; else if (sum>60) sum=60;
  97. sumsum=exp(sum*0.1151292546497)*f; /* pow(10.0,sum/20)*f */
  98. for (sum=0,x=5;x--;) { buffer[x]=table[x]*sumsum; sum+=buffer[x]*buffer[x]; }
  99. if ((sum/=5)<1) sum=1;
  100. /* shift and store */
  101. for (x=10;--x;glob->lhist[x]=glob->lhist[x-1]);
  102. *glob->lhist=glob->history[glob->phase]=10*log10(sum)-32;
  103. for (x=1;x<5;x++) for (y=x;y--;buffer[x]-=glob->pr1[x-y-1]*buffer[y]);
  104. /* output */
  105. for (x=0;x<5;x++) {
  106. f=glob->sb[4-x]+buffer[x];
  107. if (f>4095) f=4095; else if (f<-4095) f=-4095;
  108. glob->output[glob->phasep+x]=glob->sb[4-x]=f;
  109. }
  110. }
  111. /* column multiply */
  112. static void colmult(float *tgt, float *m1, const float *m2, int n)
  113. {
  114. while (n--)
  115. *(tgt++)=(*(m1++))*(*(m2++));
  116. }
  117. static int pred(float *in, float *tgt, int n)
  118. {
  119. int x,y;
  120. float *p1,*p2;
  121. double f0,f1,f2;
  122. float temp;
  123. if (in[n]==0) return 0;
  124. if ((f0=*in)<=0) return 0;
  125. for (x=1;;x++) {
  126. if (n<x) return 1;
  127. p1=in+x;
  128. p2=tgt;
  129. f1=*(p1--);
  130. for (y=x;--y;f1+=(*(p1--))*(*(p2++)));
  131. p1=tgt+x-1;
  132. p2=tgt;
  133. *(p1--)=f2=-f1/f0;
  134. for (y=x>>1;y--;) {
  135. temp=*p2+*p1*f2;
  136. *(p1--)+=*p2*f2;
  137. *(p2++)=temp;
  138. }
  139. if ((f0+=f1*f2)<0) return 0;
  140. }
  141. }
  142. static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table)
  143. {
  144. int a,b,c;
  145. unsigned int x;
  146. float *fp;
  147. float buffer1[37];
  148. float buffer2[37];
  149. float work[111];
  150. /* rotate and multiply */
  151. c=(b=(a=n+i)+j)-i;
  152. fp=st1+i;
  153. for (x=0;x<b;x++) {
  154. if (x==c) fp=in;
  155. work[x]=*(table++)*(*(st1++)=*(fp++));
  156. }
  157. prodsum(buffer1,work+n,i,n);
  158. prodsum(buffer2,work+a,j,n);
  159. for (x=0;x<=n;x++) {
  160. *st2=*st2*(0.5625)+buffer1[x];
  161. out[x]=*(st2++)+buffer2[x];
  162. }
  163. *out*=1.00390625; /* to prevent clipping */
  164. }
  165. /* product sum (lsf) */
  166. static void prodsum(float *tgt, float *src, int len, int n)
  167. {
  168. unsigned int x;
  169. float *p1,*p2;
  170. double sum;
  171. while (n>=0)
  172. {
  173. p1=(p2=src)-n;
  174. for (sum=0,x=len;x--;sum+=(*p1++)*(*p2++));
  175. tgt[n--]=sum;
  176. }
  177. }
  178. static void * decode_block(AVCodecContext * avctx, unsigned char *in, signed short int *out,unsigned len)
  179. {
  180. int x,y;
  181. Real288_internal *glob=avctx->priv_data;
  182. unsigned short int buffer[len];
  183. unpack(buffer,in,len);
  184. for (x=0;x<32;x++)
  185. {
  186. glob->phasep=(glob->phase=x&7)*5;
  187. decode(glob,buffer[x]);
  188. for (y=0;y<5;*(out++)=8*glob->output[glob->phasep+(y++)]);
  189. if (glob->phase==3) update(glob);
  190. }
  191. return out;
  192. }
  193. /* Decode a block (celp) */
  194. static int ra288_decode_frame(AVCodecContext * avctx,
  195. void *data, int *data_size,
  196. uint8_t * buf, int buf_size)
  197. {
  198. void *datao;
  199. if (buf_size < avctx->block_align)
  200. {
  201. av_log(avctx, AV_LOG_ERROR, "ffra288: Error! Input buffer is too small [%d<%d]\n",buf_size,avctx->block_align);
  202. return 0;
  203. }
  204. datao = data;
  205. data = decode_block(avctx, buf, (signed short *)data, avctx->block_align);
  206. *data_size = (char *)data - (char *)datao;
  207. return avctx->block_align;
  208. }
  209. AVCodec ra_288_decoder =
  210. {
  211. "real_288",
  212. CODEC_TYPE_AUDIO,
  213. CODEC_ID_RA_288,
  214. sizeof(Real288_internal),
  215. ra288_decode_init,
  216. NULL,
  217. NULL,
  218. ra288_decode_frame,
  219. };