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.

518 lines
12KB

  1. /*
  2. * Real Audio 1.0 (14.4K)
  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 "ra144.h"
  21. #define DATABLOCK1 20 /* size of 14.4 input block in bytes */
  22. #define DATACHUNK1 1440 /* size of 14.4 input chunk in bytes */
  23. #define AUDIOBLOCK 160 /* size of output block in 16-bit words (320 bytes) */
  24. #define AUDIOBUFFER 12288 /* size of output buffer in 16-bit words (24576 bytes) */
  25. /* consts */
  26. #define NBLOCKS 4 /* number of segments within a block */
  27. #define BLOCKSIZE 40 /* (quarter) block size in 16-bit words (80 bytes) */
  28. #define HALFBLOCK 20 /* BLOCKSIZE/2 */
  29. #define BUFFERSIZE 146 /* for do_output */
  30. /* internal globals */
  31. typedef struct {
  32. unsigned int resetflag, val, oldval;
  33. unsigned int unpacked[28]; /* buffer for unpacked input */
  34. unsigned int *iptr; /* pointer to current input (from unpacked) */
  35. unsigned int gval;
  36. unsigned short *gsp;
  37. unsigned int gbuf1[8];
  38. unsigned short gbuf2[120];
  39. signed short output_buffer[40];
  40. unsigned int *decptr; /* decoder ptr */
  41. signed short *decsp;
  42. /* the swapped buffers */
  43. unsigned int swapb1a[10];
  44. unsigned int swapb2a[10];
  45. unsigned int swapb1b[10];
  46. unsigned int swapb2b[10];
  47. unsigned int *swapbuf1;
  48. unsigned int *swapbuf2;
  49. unsigned int *swapbuf1alt;
  50. unsigned int *swapbuf2alt;
  51. unsigned int buffer[5];
  52. unsigned short int buffer_2[148];
  53. unsigned short int buffer_a[40];
  54. unsigned short int buffer_b[40];
  55. unsigned short int buffer_c[40];
  56. unsigned short int buffer_d[40];
  57. unsigned short int work[50];
  58. unsigned short *sptr;
  59. int buffer1[10];
  60. int buffer2[10];
  61. signed short wavtable1[2304];
  62. unsigned short wavtable2[2304];
  63. } Real144_internal;
  64. static int ra144_decode_init(AVCodecContext * avctx)
  65. {
  66. Real144_internal *glob=avctx->priv_data;
  67. memset(glob,0,sizeof(Real144_internal));
  68. glob->resetflag=1;
  69. glob->swapbuf1=glob->swapb1a;
  70. glob->swapbuf2=glob->swapb2a;
  71. glob->swapbuf1alt=glob->swapb1b;
  72. glob->swapbuf2alt=glob->swapb2b;
  73. memcpy(glob->wavtable1,wavtable1,sizeof(wavtable1));
  74. memcpy(glob->wavtable2,wavtable2,sizeof(wavtable2));
  75. return 0;
  76. }
  77. static void final(Real144_internal *glob, short *i1, short *i2, void *out, int *statbuf, int len);
  78. static void add_wav(Real144_internal *glob, int n, int f, int m1, int m2, int m3, short *s1, short *s2, short *s3, short *dest);
  79. static int irms(short *data, int factor);
  80. static void rotate_block(short *source, short *target, int offset);
  81. /* lookup square roots in table */
  82. static int t_sqrt(unsigned int x)
  83. {
  84. int s=0;
  85. while (x>0xfff) { s++; x=x>>2; }
  86. return (sqrt_table[x]<<s)<<2;
  87. }
  88. /* do 'voice' */
  89. static void do_voice(int *a1, int *a2)
  90. {
  91. int buffer[10];
  92. int *b1,*b2;
  93. int x,y;
  94. int *ptr,*tmp;
  95. b1=buffer;
  96. b2=a2;
  97. for (x=0;x<10;x++) {
  98. b1[x]=(*a1)<<4;
  99. if(x>0) {
  100. ptr=b2+x;
  101. for (y=0;y<=x-1;y++)
  102. b1[y]=(((*a1)*(*(--ptr)))>>12)+b2[y];
  103. }
  104. tmp=b1;
  105. b1=b2;
  106. b2=tmp;
  107. a1++;
  108. }
  109. ptr=a2+10;
  110. while (ptr>a2) (*a2++)>>=4;
  111. }
  112. /* do quarter-block output */
  113. static void do_output_subblock(Real144_internal *glob, int x)
  114. {
  115. int a,b,c,d,e,f,g;
  116. if (x==1) memset(glob->buffer,0,20);
  117. if ((*glob->iptr)==0) a=0;
  118. else a=(*glob->iptr)+HALFBLOCK-1;
  119. glob->iptr++;
  120. b=*(glob->iptr++);
  121. c=*(glob->iptr++);
  122. d=*(glob->iptr++);
  123. if (a) rotate_block(glob->buffer_2,glob->buffer_a,a);
  124. memcpy(glob->buffer_b,etable1+b*BLOCKSIZE,BLOCKSIZE*2);
  125. e=((ftable1[b]>>4)*glob->gval)>>8;
  126. memcpy(glob->buffer_c,etable2+c*BLOCKSIZE,BLOCKSIZE*2);
  127. f=((ftable2[c]>>4)*glob->gval)>>8;
  128. if (a) g=irms(glob->buffer_a,glob->gval)>>12;
  129. else g=0;
  130. add_wav(glob,d,a,g,e,f,glob->buffer_a,glob->buffer_b,glob->buffer_c,glob->buffer_d);
  131. memmove(glob->buffer_2,glob->buffer_2+BLOCKSIZE,(BUFFERSIZE-BLOCKSIZE)*2);
  132. memcpy(glob->buffer_2+BUFFERSIZE-BLOCKSIZE,glob->buffer_d,BLOCKSIZE*2);
  133. final(glob,glob->gsp,glob->buffer_d,glob->output_buffer,glob->buffer,BLOCKSIZE);
  134. }
  135. /* rotate block */
  136. static void rotate_block(short *source, short *target, int offset)
  137. {
  138. short *end;
  139. short *ptr1;
  140. short *ptr2;
  141. short *ptr3;
  142. ptr2=source+BUFFERSIZE;
  143. ptr3=ptr1=ptr2-offset;
  144. end=target+BLOCKSIZE;
  145. while (target<end) {
  146. *(target++)=*(ptr3++);
  147. if (ptr3==ptr2) ptr3=ptr1;
  148. }
  149. }
  150. /* inverse root mean square */
  151. static int irms(short *data, int factor)
  152. {
  153. short *p1,*p2;
  154. unsigned int sum;
  155. p2=(p1=data)+BLOCKSIZE;
  156. for (sum=0;p2>p1;p1++) sum+=(*p1)*(*p1);
  157. if (sum==0) return 0; /* OOPS - division by zero */
  158. return (0x20000000/(t_sqrt(sum)>>8))*factor;
  159. }
  160. /* multiply/add wavetable */
  161. static void add_wav(Real144_internal *glob, int n, int f, int m1, int m2, int m3, short *s1, short *s2, short *s3, short *dest)
  162. {
  163. int a,b,c;
  164. short *ptr,*ptr2;
  165. ptr=glob->wavtable1+n*9;
  166. ptr2=glob->wavtable2+n*9;
  167. if (f!=0) {
  168. a=((*ptr)*m1)>>((*ptr2)+1);
  169. } else {
  170. a=0;
  171. }
  172. ptr++;ptr2++;
  173. b=((*ptr)*m2)>>((*ptr2)+1);
  174. ptr++;ptr2++;
  175. c=((*ptr)*m3)>>((*ptr2)+1);
  176. ptr2=(ptr=dest)+BLOCKSIZE;
  177. if (f!=0)
  178. while (ptr<ptr2)
  179. *(ptr++)=((*(s1++))*a+(*(s2++))*b+(*(s3++))*c)>>12;
  180. else
  181. while (ptr<ptr2)
  182. *(ptr++)=((*(s2++))*b+(*(s3++))*c)>>12;
  183. }
  184. static void final(Real144_internal *glob, short *i1, short *i2, void *out, int *statbuf, int len)
  185. {
  186. int x,sum;
  187. int buffer[10];
  188. short *ptr;
  189. short *ptr2;
  190. memcpy(glob->work,statbuf,20);
  191. memcpy(glob->work+10,i2,len*2);
  192. buffer[9]=i1[0];
  193. buffer[8]=i1[1];
  194. buffer[7]=i1[2];
  195. buffer[6]=i1[3];
  196. buffer[5]=i1[4];
  197. buffer[4]=i1[5];
  198. buffer[3]=i1[6];
  199. buffer[2]=i1[7];
  200. buffer[1]=i1[8];
  201. buffer[0]=i1[9];
  202. ptr2=(ptr=glob->work)+len;
  203. while (ptr<ptr2) {
  204. for(sum=0,x=0;x<=9;x++)
  205. sum+=buffer[x]*(ptr[x]);
  206. sum=sum>>12;
  207. x=ptr[10]-sum;
  208. if (x<-32768 || x>32767)
  209. {
  210. memset(out,0,len*2);
  211. memset(statbuf,0,20);
  212. return;
  213. }
  214. ptr[10]=x;
  215. ptr++;
  216. }
  217. memcpy(out,ptr+10-len,len*2);
  218. memcpy(statbuf,ptr,20);
  219. }
  220. /* Decode 20-byte input */
  221. static void unpack_input(unsigned char *input, unsigned int *output)
  222. {
  223. unsigned int outbuffer[28];
  224. unsigned short inbuffer[10];
  225. unsigned int x;
  226. unsigned int *ptr;
  227. /* fix endianness */
  228. for (x=0;x<20;x+=2)
  229. inbuffer[x/2]=(input[x]<<8)+input[x+1];
  230. /* unpack */
  231. ptr=outbuffer;
  232. *(ptr++)=27;
  233. *(ptr++)=(inbuffer[0]>>10)&0x3f;
  234. *(ptr++)=(inbuffer[0]>>5)&0x1f;
  235. *(ptr++)=inbuffer[0]&0x1f;
  236. *(ptr++)=(inbuffer[1]>>12)&0xf;
  237. *(ptr++)=(inbuffer[1]>>8)&0xf;
  238. *(ptr++)=(inbuffer[1]>>5)&7;
  239. *(ptr++)=(inbuffer[1]>>2)&7;
  240. *(ptr++)=((inbuffer[1]<<1)&6)|((inbuffer[2]>>15)&1);
  241. *(ptr++)=(inbuffer[2]>>12)&7;
  242. *(ptr++)=(inbuffer[2]>>10)&3;
  243. *(ptr++)=(inbuffer[2]>>5)&0x1f;
  244. *(ptr++)=((inbuffer[2]<<2)&0x7c)|((inbuffer[3]>>14)&3);
  245. *(ptr++)=(inbuffer[3]>>6)&0xff;
  246. *(ptr++)=((inbuffer[3]<<1)&0x7e)|((inbuffer[4]>>15)&1);
  247. *(ptr++)=(inbuffer[4]>>8)&0x7f;
  248. *(ptr++)=(inbuffer[4]>>1)&0x7f;
  249. *(ptr++)=((inbuffer[4]<<7)&0x80)|((inbuffer[5]>>9)&0x7f);
  250. *(ptr++)=(inbuffer[5]>>2)&0x7f;
  251. *(ptr++)=((inbuffer[5]<<5)&0x60)|((inbuffer[6]>>11)&0x1f);
  252. *(ptr++)=(inbuffer[6]>>4)&0x7f;
  253. *(ptr++)=((inbuffer[6]<<4)&0xf0)|((inbuffer[7]>>12)&0xf);
  254. *(ptr++)=(inbuffer[7]>>5)&0x7f;
  255. *(ptr++)=((inbuffer[7]<<2)&0x7c)|((inbuffer[8]>>14)&3);
  256. *(ptr++)=(inbuffer[8]>>7)&0x7f;
  257. *(ptr++)=((inbuffer[8]<<1)&0xfe)|((inbuffer[9]>>15)&1);
  258. *(ptr++)=(inbuffer[9]>>8)&0x7f;
  259. *(ptr++)=(inbuffer[9]>>1)&0x7f;
  260. *(output++)=outbuffer[11];
  261. for (x=1;x<11;*(output++)=outbuffer[x++]);
  262. ptr=outbuffer+12;
  263. for (x=0;x<16;x+=4)
  264. {
  265. *(output++)=ptr[x];
  266. *(output++)=ptr[x+2];
  267. *(output++)=ptr[x+3];
  268. *(output++)=ptr[x+1];
  269. }
  270. }
  271. static unsigned int rms(int *data, int f)
  272. {
  273. int *c;
  274. int x;
  275. unsigned int res;
  276. int b;
  277. c=data;
  278. b=0;
  279. res=0x10000;
  280. for (x=0;x<10;x++)
  281. {
  282. res=(((0x1000000-(*c)*(*c))>>12)*res)>>12;
  283. if (res==0) return 0;
  284. if (res<=0x3fff)
  285. {
  286. while (res<=0x3fff)
  287. {
  288. b++;
  289. res<<=2;
  290. }
  291. } else {
  292. if (res>0x10000)
  293. return 0; /* We're screwed, might as well go out with a bang. :P */
  294. }
  295. c++;
  296. }
  297. if (res>0) res=t_sqrt(res);
  298. res>>=(b+10);
  299. res=(res*f)>>10;
  300. return res;
  301. }
  302. static void dec1(Real144_internal *glob, int *data, int *inp, int n, int f)
  303. {
  304. short *ptr,*end;
  305. *(glob->decptr++)=rms(data,f);
  306. glob->decptr++;
  307. end=(ptr=glob->decsp)+(n*10);
  308. while (ptr<end) *(ptr++)=*(inp++);
  309. }
  310. static int eq(Real144_internal *glob, short *in, int *target)
  311. {
  312. int retval;
  313. int a;
  314. int b;
  315. int c;
  316. unsigned int u;
  317. short *sptr;
  318. int *ptr1,*ptr2,*ptr3;
  319. int *bp1,*bp2,*temp;
  320. retval=0;
  321. bp1=glob->buffer1;
  322. bp2=glob->buffer2;
  323. ptr2=(ptr3=glob->buffer2)+9;
  324. sptr=in;
  325. while (ptr2>=ptr3)
  326. *(ptr3++)=*(sptr++);
  327. target+=9;
  328. a=bp2[9];
  329. *target=a;
  330. if (a+0x1000>0x1fff)
  331. return 0; /* We're screwed, might as well go out with a bang. :P */
  332. c=8;u=a;
  333. while (c>=0)
  334. {
  335. if (u==0x1000) u++;
  336. if (u==0xfffff000) u--;
  337. b=0x1000-((u*u)>>12);
  338. if (b==0) b++;
  339. ptr2=bp1;
  340. ptr1=(ptr3=bp2)+c;
  341. for (u=0;u<=c;u++)
  342. *(ptr2++)=((*(ptr3++)-(((*target)*(*(ptr1--)))>>12))*(0x1000000/b))>>12;
  343. *(--target)=u=bp1[(c--)];
  344. if ((u+0x1000)>0x1fff) retval=1;
  345. temp=bp2;
  346. bp2=bp1;
  347. bp1=temp;
  348. }
  349. return retval;
  350. }
  351. static void dec2(Real144_internal *glob, int *data, int *inp, int n, int f, int *inp2, int l)
  352. {
  353. unsigned int *ptr1,*ptr2;
  354. int work[10];
  355. int a,b;
  356. int x;
  357. int result;
  358. if(l+1<NBLOCKS/2) a=NBLOCKS-(l+1);
  359. else a=l+1;
  360. b=NBLOCKS-a;
  361. if (l==0)
  362. {
  363. glob->decsp=glob->sptr=glob->gbuf2;
  364. glob->decptr=glob->gbuf1;
  365. }
  366. ptr1=inp;
  367. ptr2=inp2;
  368. for (x=0;x<10*n;x++)
  369. *(glob->sptr++)=(a*(*ptr1++)+b*(*ptr2++))>>2;
  370. result=eq(glob,glob->decsp,work);
  371. if (result==1)
  372. {
  373. dec1(glob,data,inp,n,f);
  374. } else {
  375. *(glob->decptr++)=rms(work,f);
  376. glob->decptr++;
  377. }
  378. glob->decsp+=n*10;
  379. }
  380. /* Uncompress one block (20 bytes -> 160*2 bytes) */
  381. static int ra144_decode_frame(AVCodecContext * avctx,
  382. void *vdata, int *data_size,
  383. uint8_t * buf, int buf_size)
  384. {
  385. unsigned int a,b,c;
  386. long s;
  387. signed short *shptr;
  388. unsigned int *lptr,*temp;
  389. const short **dptr;
  390. int16_t *datao;
  391. int16_t *data = vdata;
  392. Real144_internal *glob=avctx->priv_data;
  393. if(buf_size==0)
  394. return 0;
  395. datao = data;
  396. unpack_input(buf,glob->unpacked);
  397. glob->iptr=glob->unpacked;
  398. glob->val=decodetable[0][(*(glob->iptr++))<<1];
  399. dptr=decodetable+1;
  400. lptr=glob->swapbuf1;
  401. while (lptr<glob->swapbuf1+10)
  402. *(lptr++)=(*(dptr++))[(*(glob->iptr++))<<1];
  403. do_voice(glob->swapbuf1,glob->swapbuf2);
  404. a=t_sqrt(glob->val*glob->oldval)>>12;
  405. for (c=0;c<NBLOCKS;c++) {
  406. if (c==(NBLOCKS-1)) {
  407. dec1(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val);
  408. } else {
  409. if (c*2==(NBLOCKS-2)) {
  410. if (glob->oldval<glob->val) {
  411. dec2(glob,glob->swapbuf1,glob->swapbuf2,3,a,glob->swapbuf2alt,c);
  412. } else {
  413. dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,a,glob->swapbuf2,c);
  414. }
  415. } else {
  416. if (c*2<(NBLOCKS-2)) {
  417. dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,glob->oldval,glob->swapbuf2,c);
  418. } else {
  419. dec2(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val,glob->swapbuf2alt,c);
  420. }
  421. }
  422. }
  423. }
  424. /* do output */
  425. for (b=0,c=0;c<4;c++) {
  426. glob->gval=glob->gbuf1[c*2];
  427. glob->gsp=glob->gbuf2+b;
  428. do_output_subblock(glob,glob->resetflag);
  429. glob->resetflag=0;
  430. shptr=glob->output_buffer;
  431. while (shptr<glob->output_buffer+BLOCKSIZE) {
  432. s=*(shptr++)<<2;
  433. *data=s;
  434. if (s>32767) *data=32767;
  435. if (s<-32767) *data=-32768;
  436. data++;
  437. }
  438. b+=30;
  439. }
  440. glob->oldval=glob->val;
  441. temp=glob->swapbuf1alt;
  442. glob->swapbuf1alt=glob->swapbuf1;
  443. glob->swapbuf1=temp;
  444. temp=glob->swapbuf2alt;
  445. glob->swapbuf2alt=glob->swapbuf2;
  446. glob->swapbuf2=temp;
  447. *data_size=(data-datao)*sizeof(*data);
  448. return 20;
  449. }
  450. AVCodec ra_144_decoder =
  451. {
  452. "real_144",
  453. CODEC_TYPE_AUDIO,
  454. CODEC_ID_RA_144,
  455. sizeof(Real144_internal),
  456. ra144_decode_init,
  457. NULL,
  458. NULL,
  459. ra144_decode_frame,
  460. };