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.

279 lines
6.7KB

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