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.

466 lines
11KB

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