ra144.c 13 KB

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