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.

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