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.

447 lines
10KB

  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 oldval;
  31. unsigned int gbuf1[4];
  32. unsigned short gbuf2[120];
  33. unsigned int *decptr; /* decoder ptr */
  34. signed short *decsp;
  35. /* the swapped buffers */
  36. unsigned int swapbuffers[4][10];
  37. unsigned int *swapbuf1;
  38. unsigned int *swapbuf2;
  39. unsigned int *swapbuf1alt;
  40. unsigned int *swapbuf2alt;
  41. unsigned int buffer[5];
  42. unsigned short int buffer_2[148];
  43. unsigned short *sptr;
  44. } Real144_internal;
  45. static int ra144_decode_init(AVCodecContext * avctx)
  46. {
  47. Real144_internal *glob = avctx->priv_data;
  48. glob->swapbuf1 = glob->swapbuffers[0];
  49. glob->swapbuf2 = glob->swapbuffers[1];
  50. glob->swapbuf1alt = glob->swapbuffers[2];
  51. glob->swapbuf2alt = glob->swapbuffers[3];
  52. return 0;
  53. }
  54. /* lookup square roots in table */
  55. static int t_sqrt(unsigned int x)
  56. {
  57. int s = 0;
  58. while (x > 0xfff) {
  59. s++;
  60. x = x >> 2;
  61. }
  62. return (ff_sqrt(x << 20) << s) << 2;
  63. }
  64. /* do 'voice' */
  65. static void do_voice(const int *a1, int *a2)
  66. {
  67. int buffer[10];
  68. int *b1 = buffer;
  69. int *b2 = a2;
  70. int x, y;
  71. for (x=0; x < 10; x++) {
  72. b1[x] = a1[x] << 4;
  73. for (y=0; y < x; y++)
  74. b1[y] = ((a1[x] * b2[x-y-1]) >> 12) + b2[y];
  75. FFSWAP(int *, b1, b2);
  76. }
  77. for (x=0; x < 10; x++)
  78. a2[x] >>= 4;
  79. }
  80. /* rotate block */
  81. static void rotate_block(const short *source, short *target, int offset)
  82. {
  83. int i=0, k=0;
  84. source += BUFFERSIZE - offset;
  85. while (i<BLOCKSIZE) {
  86. target[i++] = source[k++];
  87. if (k == offset)
  88. k = 0;
  89. }
  90. }
  91. /* inverse root mean square */
  92. static int irms(const short *data, int factor)
  93. {
  94. const short *p1, *p2;
  95. unsigned int sum;
  96. p2 = (p1 = data) + BLOCKSIZE;
  97. for (sum=0; p2 > p1; p1++)
  98. sum += (*p1) * (*p1);
  99. if (sum == 0)
  100. return 0; /* OOPS - division by zero */
  101. return (0x20000000 / (t_sqrt(sum) >> 8)) * factor;
  102. }
  103. /* multiply/add wavetable */
  104. static void add_wav(int n, int f, int m1, int m2, int m3, const short *s1,
  105. const short *s2, const short *s3, short *dest)
  106. {
  107. int a, b, c, i;
  108. const short *ptr, *ptr2;
  109. ptr = wavtable1 + n * 9;
  110. ptr2 = wavtable2 + n * 9;
  111. if (f != 0)
  112. a = ((*ptr) * m1) >> ((*ptr2) + 1);
  113. else
  114. a = 0;
  115. ptr++;
  116. ptr2++;
  117. b = ((*ptr) * m2) >> ((*ptr2) + 1);
  118. ptr++;
  119. ptr2++;
  120. c = ((*ptr) * m3) >> ((*ptr2) + 1);
  121. if (f != 0)
  122. for (i=0; i < BLOCKSIZE; i++)
  123. dest[i] = ((*(s1++)) * a + (*(s2++)) * b + (*(s3++)) * c) >> 12;
  124. else
  125. for (i=0; i < BLOCKSIZE; i++)
  126. dest[i] = ((*(s2++)) * b + (*(s3++)) * c) >> 12;
  127. }
  128. static void final(const short *i1, const short *i2,
  129. void *out, int *statbuf, int len)
  130. {
  131. int x, sum, i;
  132. int buffer[10];
  133. short *ptr;
  134. short *ptr2;
  135. unsigned short int work[50];
  136. memcpy(work, statbuf,20);
  137. memcpy(work + 10, i2, len * 2);
  138. for(i=0; i<10; i++)
  139. buffer[9-i] = i1[i];
  140. ptr2 = (ptr = work) + len;
  141. while (ptr < ptr2) {
  142. for(sum=0, x=0; x<=9; x++)
  143. sum += buffer[x] * (ptr[x]);
  144. sum = sum >> 12;
  145. x = ptr[10] - sum;
  146. if (x<-32768 || x>32767) {
  147. memset(out, 0, len * 2);
  148. memset(statbuf, 0, 20);
  149. return;
  150. }
  151. ptr[10] = x;
  152. ptr++;
  153. }
  154. memcpy(out, ptr+10 - len, len * 2);
  155. memcpy(statbuf, ptr, 20);
  156. }
  157. static unsigned int rms(const int *data, int f)
  158. {
  159. const int *c;
  160. int x;
  161. unsigned int res;
  162. int b;
  163. c = data;
  164. b = 0;
  165. res = 0x10000;
  166. for (x=0; x<10; x++) {
  167. res = (((0x1000000 - (*c) * (*c)) >> 12) * res) >> 12;
  168. if (res == 0)
  169. return 0;
  170. if (res <= 0x3fff) {
  171. while (res <= 0x3fff) {
  172. b++;
  173. res <<= 2;
  174. }
  175. } else {
  176. if (res > 0x10000)
  177. return 0; /* We're screwed, might as well go out with a bang. :P */
  178. }
  179. c++;
  180. }
  181. if (res > 0)
  182. res = t_sqrt(res);
  183. res >>= (b + 10);
  184. res = (res * f) >> 10;
  185. return res;
  186. }
  187. /* do quarter-block output */
  188. static void do_output_subblock(Real144_internal *glob, const unsigned short *gsp, unsigned int gval, signed short *output_buffer, GetBitContext *gb)
  189. {
  190. unsigned short int buffer_a[40];
  191. unsigned short int *block;
  192. int e, f, g;
  193. int a = get_bits(gb, 7);
  194. int d = get_bits(gb, 8);
  195. int b = get_bits(gb, 7);
  196. int c = get_bits(gb, 7);
  197. if (a) {
  198. a += HALFBLOCK - 1;
  199. rotate_block(glob->buffer_2, buffer_a, a);
  200. }
  201. e = ((ftable1[b] >> 4) * gval) >> 8;
  202. f = ((ftable2[c] >> 4) * gval) >> 8;
  203. if (a)
  204. g = irms(buffer_a, gval) >> 12;
  205. else
  206. g = 0;
  207. memmove(glob->buffer_2, glob->buffer_2 + BLOCKSIZE, (BUFFERSIZE - BLOCKSIZE) * 2);
  208. block = glob->buffer_2 + BUFFERSIZE - BLOCKSIZE;
  209. add_wav(d, a, g, e, f, buffer_a, etable1[b],
  210. etable2[c], block);
  211. final(gsp, block, output_buffer, glob->buffer, BLOCKSIZE);
  212. }
  213. static void dec1(Real144_internal *glob, const int *data, const int *inp,
  214. int n, int f)
  215. {
  216. short *ptr,*end;
  217. *(glob->decptr++) = rms(data, f);
  218. end = (ptr = glob->decsp) + (n * 10);
  219. while (ptr < end)
  220. *(ptr++) = *(inp++);
  221. }
  222. static int eq(const short *in, int *target)
  223. {
  224. int retval;
  225. int a;
  226. int b;
  227. int c;
  228. unsigned int u;
  229. const short *sptr;
  230. int *ptr1, *ptr2, *ptr3;
  231. int *bp1, *bp2;
  232. int buffer1[10];
  233. int buffer2[10];
  234. retval = 0;
  235. bp1 = buffer1;
  236. bp2 = buffer2;
  237. ptr2 = (ptr3 = buffer2) + 9;
  238. sptr = in;
  239. while (ptr2 >= ptr3)
  240. *(ptr3++) = *(sptr++);
  241. target += 9;
  242. a = bp2[9];
  243. *target = a;
  244. if (a + 0x1000 > 0x1fff)
  245. return 0; /* We're screwed, might as well go out with a bang. :P */
  246. c = 8;
  247. u = a;
  248. while (c >= 0) {
  249. if (u == 0x1000)
  250. u++;
  251. if (u == 0xfffff000)
  252. u--;
  253. b = 0x1000-((u * u) >> 12);
  254. if (b == 0)
  255. b++;
  256. ptr2 = bp1;
  257. ptr1 = (ptr3 = bp2) + c;
  258. for (u=0; u<=c; u++)
  259. *(ptr2++) = ((*(ptr3++) - (((*target) * (*(ptr1--))) >> 12)) * (0x1000000 / b)) >> 12;
  260. *(--target) = u = bp1[(c--)];
  261. if ((u + 0x1000) > 0x1fff)
  262. retval = 1;
  263. FFSWAP(int *, bp1, bp2);
  264. }
  265. return retval;
  266. }
  267. static void dec2(Real144_internal *glob, const int *data, const int *inp,
  268. int n, int f, const int *inp2, int l)
  269. {
  270. unsigned const int *ptr1,*ptr2;
  271. int work[10];
  272. int a,b;
  273. int x;
  274. int result;
  275. if(l + 1 < NBLOCKS / 2)
  276. a = NBLOCKS - (l + 1);
  277. else
  278. a = l + 1;
  279. b = NBLOCKS - a;
  280. if (l == 0) {
  281. glob->decsp = glob->sptr = glob->gbuf2;
  282. glob->decptr = glob->gbuf1;
  283. }
  284. ptr1 = inp;
  285. ptr2 = inp2;
  286. for (x=0; x<10*n; x++)
  287. *(glob->sptr++) = (a * (*ptr1++) + b * (*ptr2++)) >> 2;
  288. result = eq(glob->decsp, work);
  289. if (result == 1) {
  290. dec1(glob, data, inp, n, f);
  291. } else {
  292. *(glob->decptr++) = rms(work, f);
  293. }
  294. glob->decsp += n * 10;
  295. }
  296. /* Uncompress one block (20 bytes -> 160*2 bytes) */
  297. static int ra144_decode_frame(AVCodecContext * avctx,
  298. void *vdata, int *data_size,
  299. const uint8_t * buf, int buf_size)
  300. {
  301. static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
  302. unsigned int a, b, c;
  303. int i;
  304. signed short *shptr;
  305. int16_t *data = vdata;
  306. unsigned int val;
  307. Real144_internal *glob = avctx->priv_data;
  308. GetBitContext gb;
  309. if(buf_size == 0)
  310. return 0;
  311. init_get_bits(&gb, buf, 20 * 8);
  312. for (i=0; i<10; i++)
  313. // "<< 1"? Doesn't this make one value out of two of the table useless?
  314. glob->swapbuf1[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
  315. do_voice(glob->swapbuf1, glob->swapbuf2);
  316. val = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
  317. a = t_sqrt(val*glob->oldval) >> 12;
  318. dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, glob->oldval, glob->swapbuf2, 0);
  319. if (glob->oldval < val) {
  320. dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, a, glob->swapbuf2alt, 1);
  321. } else {
  322. dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, a, glob->swapbuf2, 1);
  323. }
  324. dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, val, glob->swapbuf2alt, 2);
  325. dec1(glob, glob->swapbuf1, glob->swapbuf2, 3, val);
  326. /* do output */
  327. for (b=0, c=0; c<4; c++) {
  328. unsigned int gval = glob->gbuf1[c];
  329. unsigned short *gsp = glob->gbuf2 + b;
  330. signed short output_buffer[40];
  331. do_output_subblock(glob, gsp, gval, output_buffer, &gb);
  332. shptr = output_buffer;
  333. while (shptr < output_buffer + BLOCKSIZE)
  334. *data++ = av_clip_int16(*(shptr++) << 2);
  335. b += 30;
  336. }
  337. glob->oldval = val;
  338. FFSWAP(unsigned int *, glob->swapbuf1alt, glob->swapbuf1);
  339. FFSWAP(unsigned int *, glob->swapbuf2alt, glob->swapbuf2);
  340. *data_size = 2*160;
  341. return 20;
  342. }
  343. AVCodec ra_144_decoder =
  344. {
  345. "real_144",
  346. CODEC_TYPE_AUDIO,
  347. CODEC_ID_RA_144,
  348. sizeof(Real144_internal),
  349. ra144_decode_init,
  350. NULL,
  351. NULL,
  352. ra144_decode_frame,
  353. .long_name = "RealAudio 1.0 (14.4K)",
  354. };