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.

553 lines
13KB

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