2 * Real Audio 1.0 (14.4K)
3 * Copyright (c) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "bitstream.h"
26 #define NBLOCKS 4 /* number of segments within a block */
27 #define BLOCKSIZE 40 /* (quarter) block size in 16-bit words (80 bytes) */
28 #define HALFBLOCK 20 /* BLOCKSIZE/2 */
29 #define BUFFERSIZE 146 /* for do_output */
32 /* internal globals */
35 unsigned int gbuf1[4];
36 unsigned short gbuf2[4][30];
38 /* the swapped buffers */
39 unsigned int swapbuffers[4][10];
40 unsigned int *swapbuf1;
41 unsigned int *swapbuf2;
42 unsigned int *swapbuf1alt;
43 unsigned int *swapbuf2alt;
45 unsigned int buffer[5];
46 unsigned short int buffer_2[148];
49 static int ra144_decode_init(AVCodecContext * avctx)
51 Real144_internal *glob = avctx->priv_data;
53 glob->swapbuf1 = glob->swapbuffers[0];
54 glob->swapbuf2 = glob->swapbuffers[1];
55 glob->swapbuf1alt = glob->swapbuffers[2];
56 glob->swapbuf2alt = glob->swapbuffers[3];
61 /* lookup square roots in table */
62 static int t_sqrt(unsigned int x)
70 return (ff_sqrt(x << 20) << s) << 2;
74 static void do_voice(const int *a1, int *a2)
81 for (x=0; x < 10; x++) {
85 b1[y] = ((a1[x] * b2[x-y-1]) >> 12) + b2[y];
87 FFSWAP(int *, b1, b2);
90 for (x=0; x < 10; x++)
95 static void rotate_block(const short *source, short *target, int offset)
98 source += BUFFERSIZE - offset;
100 while (i<BLOCKSIZE) {
101 target[i++] = source[k++];
108 /* inverse root mean square */
109 static int irms(const short *data, int factor)
111 unsigned int i, sum = 0;
113 for (i=0; i < BLOCKSIZE; i++)
114 sum += data[i] * data[i];
117 return 0; /* OOPS - division by zero */
119 return (0x20000000 / (t_sqrt(sum) >> 8)) * factor;
122 /* multiply/add wavetable */
123 static void add_wav(int n, int f, int m1, int m2, int m3, const short *s1,
124 const short *s2, const short *s3, short *dest)
128 const short *ptr, *ptr2;
134 a = (ptr[0] * m1) >> (ptr2[0] + 1);
136 b = (ptr[1] * m2) >> (ptr2[1] + 1);
137 c = (ptr[2] * m3) >> (ptr2[2] + 1);
139 for (i=0; i < BLOCKSIZE; i++)
140 dest[i] = ((*(s1++)) * a + (*(s2++)) * b + (*(s3++)) * c) >> 12;
144 static void final(const short *i1, const short *i2,
145 void *out, int *statbuf, int len)
148 unsigned short int work[50];
151 memcpy(work, statbuf,20);
152 memcpy(work + 10, i2, len * 2);
154 for (i=0; i<len; i++) {
159 sum += i1[9-x] * ptr[x];
163 new_val = ptr[10] - sum;
165 if (new_val < -32768 || new_val > 32767) {
166 memset(out, 0, len * 2);
167 memset(statbuf, 0, 20);
175 memcpy(out, work+10, len * 2);
176 memcpy(statbuf, work + 40, 20);
179 static unsigned int rms(const int *data, int f)
182 unsigned int res = 0x10000;
185 for (x=0; x<10; x++) {
186 res = (((0x1000000 - (*data) * (*data)) >> 12) * res) >> 12;
192 return 0; /* We're screwed, might as well go out with a bang. :P */
194 while (res <= 0x3fff) {
205 res = (res * f) >> 10;
209 /* do quarter-block output */
210 static void do_output_subblock(Real144_internal *glob, const unsigned short *gsp, unsigned int gval, signed short *output_buffer, GetBitContext *gb)
212 unsigned short int buffer_a[40];
213 unsigned short int *block;
215 int a = get_bits(gb, 7);
216 int d = get_bits(gb, 8);
217 int b = get_bits(gb, 7);
218 int c = get_bits(gb, 7);
222 rotate_block(glob->buffer_2, buffer_a, a);
225 e = ((ftable1[b] >> 4) * gval) >> 8;
226 f = ((ftable2[c] >> 4) * gval) >> 8;
229 g = irms(buffer_a, gval) >> 12;
233 memmove(glob->buffer_2, glob->buffer_2 + BLOCKSIZE, (BUFFERSIZE - BLOCKSIZE) * 2);
234 block = glob->buffer_2 + BUFFERSIZE - BLOCKSIZE;
236 add_wav(d, a, g, e, f, buffer_a, etable1[b],
239 final(gsp, block, output_buffer, glob->buffer, BLOCKSIZE);
242 static void dec1(Real144_internal *glob, const int *data, const int *inp,
243 int n, int f, int block_idx)
246 signed short *decsp = glob->gbuf2[block_idx];
248 glob->gbuf1[block_idx] = rms(data, f);
249 end = (ptr = decsp) + (n * 10);
255 static int eq(const short *in, int *target)
265 for (i=0; i < 10; i++)
268 u = target[9] = bp2[9];
270 if (u + 0x1000 > 0x1fff)
271 return 0; /* We're screwed, might as well go out with a bang. :P */
273 for (c=8; c >= 0; c--) {
280 b = 0x1000-((u * u) >> 12);
286 bp1[u] = ((bp2[u] - ((target[c+1] * bp2[c-u]) >> 12)) * (0x1000000 / b)) >> 12;
288 target[c] = u = bp1[c];
290 if ((u + 0x1000) > 0x1fff)
293 FFSWAP(int *, bp1, bp2);
298 static void dec2(Real144_internal *glob, const int *data, const int *inp,
299 int n, int f, const int *inp2, int l)
301 unsigned const int *ptr1,*ptr2;
306 signed short *decsp = glob->gbuf2[l];
307 unsigned short *sptr = decsp;
309 if(l + 1 < NBLOCKS / 2)
310 a = NBLOCKS - (l + 1);
319 for (x=0; x<10*n; x++)
320 *(sptr++) = (a * (*ptr1++) + b * (*ptr2++)) >> 2;
322 result = eq(decsp, work);
325 dec1(glob, data, inp, n, f, l);
327 glob->gbuf1[l] = rms(work, f);
331 /* Uncompress one block (20 bytes -> 160*2 bytes) */
332 static int ra144_decode_frame(AVCodecContext * avctx,
333 void *vdata, int *data_size,
334 const uint8_t * buf, int buf_size)
336 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
339 int16_t *data = vdata;
342 Real144_internal *glob = avctx->priv_data;
346 av_log(avctx, AV_LOG_ERROR,
347 "Frame too small (%d bytes). Truncated file?\n", buf_size);
350 init_get_bits(&gb, buf, 20 * 8);
353 // "<< 1"? Doesn't this make one value out of two of the table useless?
354 glob->swapbuf1[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
356 do_voice(glob->swapbuf1, glob->swapbuf2);
358 val = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
359 a = t_sqrt(val*glob->oldval) >> 12;
361 dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, glob->oldval, glob->swapbuf2, 0);
362 if (glob->oldval < val) {
363 dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, a, glob->swapbuf2alt, 1);
365 dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, a, glob->swapbuf2, 1);
367 dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, val, glob->swapbuf2alt, 2);
368 dec1(glob, glob->swapbuf1, glob->swapbuf2, 3, val, 3);
371 for (c=0; c<4; c++) {
372 do_output_subblock(glob, glob->gbuf2[c], glob->gbuf1[c], data, &gb);
374 for (i=0; i<BLOCKSIZE; i++) {
375 *data = av_clip_int16(*data << 2);
382 FFSWAP(unsigned int *, glob->swapbuf1alt, glob->swapbuf1);
383 FFSWAP(unsigned int *, glob->swapbuf2alt, glob->swapbuf2);
390 AVCodec ra_144_decoder =
395 sizeof(Real144_internal),
400 .long_name = "RealAudio 1.0 (14.4K)",