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.

402 lines
9.5KB

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