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.

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