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.

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