ra144.c 9.81 KB
Newer Older
1 2 3 4
/*
 * Real Audio 1.0 (14.4K)
 * Copyright (c) 2003 the ffmpeg project
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 22
 */

#include "avcodec.h"
23
#include "bitstream.h"
24
#include "acelp_vectors.h"
25
#include "ra144.h"
26

27 28 29 30
#define NBLOCKS         4       /* number of segments within a block */
#define BLOCKSIZE       40      /* (quarter) block size in 16-bit words (80 bytes) */
#define HALFBLOCK       20      /* BLOCKSIZE/2 */
#define BUFFERSIZE      146     /* for do_output */
31 32 33 34


/* internal globals */
typedef struct {
Vitor Sessak's avatar
Vitor Sessak committed
35
    unsigned int     old_energy;        ///< previous frame energy
36 37

    /* the swapped buffers */
38 39
    unsigned int     refl_tables[2][10];
    int16_t          coef_tables[2][10];
Vitor Sessak's avatar
Vitor Sessak committed
40
    unsigned int    *lpc_refl;          ///< LPC reflection coefficients
41
    int16_t         *lpc_coef;          ///< LPC coefficients
Vitor Sessak's avatar
Vitor Sessak committed
42
    unsigned int    *lpc_refl_old;      ///< previous frame LPC reflection coefs
43
    int16_t         *lpc_coef_old;      ///< previous frame LPC coefficients
44 45

    unsigned int buffer[5];
Vitor Sessak's avatar
Vitor Sessak committed
46
    uint16_t adapt_cb[148];             ///< adaptive codebook
47
} RA144Context;
48 49 50

static int ra144_decode_init(AVCodecContext * avctx)
{
51
    RA144Context *ractx = avctx->priv_data;
52

53 54 55 56
    ractx->lpc_refl     = ractx->refl_tables[0];
    ractx->lpc_coef     = ractx->coef_tables[0];
    ractx->lpc_refl_old = ractx->refl_tables[1];
    ractx->lpc_coef_old = ractx->coef_tables[1];
57

58
    return 0;
59 60
}

Vitor Sessak's avatar
Vitor Sessak committed
61
/**
Diego Biurrun's avatar
Diego Biurrun committed
62
 * Evaluate sqrt(x << 24). x must fit in 20 bits. This value is evaluated in an
Vitor Sessak's avatar
Vitor Sessak committed
63 64
 * odd way to make the output identical to the binary decoder.
 */
65 66
static int t_sqrt(unsigned int x)
{
67 68 69 70 71 72
    int s = 0;
    while (x > 0xfff) {
        s++;
        x = x >> 2;
    }

73
    return (ff_sqrt(x << 20) << s) << 2;
74 75 76
}

/* do 'voice' */
77
static void do_voice(const int *a1, int16_t *a2)
78
{
79
    int buffer[10];
80
    int buffer2[10];
Vitor Sessak's avatar
Vitor Sessak committed
81
    int *b1 = buffer;
82
    int *b2 = buffer2;
83
    int x, y;
84

85 86 87
    for (x=0; x<10; x++)
        buffer2[x] = a2[x];

88
    for (x=0; x < 10; x++) {
Vitor Sessak's avatar
Vitor Sessak committed
89 90 91
        b1[x] = a1[x] << 4;

        for (y=0; y < x; y++)
92
            b1[y] = ((a1[x] * b2[x-y-1]) >> 12) + b2[y];
93

94
        FFSWAP(int *, b1, b2);
95
    }
96

Vitor Sessak's avatar
Vitor Sessak committed
97
    for (x=0; x < 10; x++)
98
        a2[x] = buffer2[x] >> 4;
99 100 101
}

/* rotate block */
102
static void rotate_block(const int16_t *source, int16_t *target, int offset)
103
{
Vitor Sessak's avatar
Vitor Sessak committed
104
    int i=0, k=0;
105
    source += BUFFERSIZE - offset;
Vitor Sessak's avatar
Vitor Sessak committed
106 107

    while (i<BLOCKSIZE) {
108
        target[i++] = source[k++];
Vitor Sessak's avatar
Vitor Sessak committed
109 110 111

        if (k == offset)
            k = 0;
112
    }
113 114 115
}

/* inverse root mean square */
116
static int irms(const int16_t *data, int factor)
117
{
Vitor Sessak's avatar
Vitor Sessak committed
118
    unsigned int i, sum = 0;
119

Vitor Sessak's avatar
Vitor Sessak committed
120 121
    for (i=0; i < BLOCKSIZE; i++)
        sum += data[i] * data[i];
122 123 124 125 126

    if (sum == 0)
        return 0; /* OOPS - division by zero */

    return (0x20000000 / (t_sqrt(sum) >> 8)) * factor;
127 128 129
}

/* multiply/add wavetable */
130 131
static void add_wav(int n, int skip_first, int *m, const int16_t *s1,
                    const int8_t *s2, const int8_t *s3, int16_t *dest)
132
{
133 134
    int i;
    int v[3];
135

136
    v[0] = 0;
Vitor Sessak's avatar
Vitor Sessak committed
137
    for (i=!skip_first; i<3; i++)
138
        v[i] = (wavtable1[n][i] * m[i]) >> (wavtable2[n][i] + 1);
139

Vitor Sessak's avatar
Vitor Sessak committed
140
    for (i=0; i < BLOCKSIZE; i++)
141
        dest[i] = ((*(s1++))*v[0] + (*(s2++))*v[1] + (*(s3++))*v[2]) >> 12;
142 143 144
}


145
static void final(const int16_t *i1, const int16_t *i2,
146
                  void *out, int *statbuf, int len)
147
{
Vitor Sessak's avatar
Vitor Sessak committed
148
    int x, i;
149 150
    uint16_t work[50];
    int16_t *ptr = work;
151

152 153
    memcpy(work, statbuf,20);
    memcpy(work + 10, i2, len * 2);
154

Vitor Sessak's avatar
Vitor Sessak committed
155 156
    for (i=0; i<len; i++) {
        int sum = 0;
157
        int new_val;
158

Vitor Sessak's avatar
Vitor Sessak committed
159 160
        for(x=0; x<10; x++)
            sum += i1[9-x] * ptr[x];
161

Vitor Sessak's avatar
Vitor Sessak committed
162
        sum >>= 12;
163

164 165 166
        new_val = ptr[10] - sum;

        if (new_val < -32768 || new_val > 32767) {
167 168 169 170 171
            memset(out, 0, len * 2);
            memset(statbuf, 0, 20);
            return;
        }

172
        ptr[10] = new_val;
173
        ptr++;
174
    }
Vitor Sessak's avatar
Vitor Sessak committed
175 176 177

    memcpy(out, work+10, len * 2);
    memcpy(statbuf, work + 40, 20);
178 179
}

180
static unsigned int rms(const int *data, int f)
181
{
182
    int x;
Vitor Sessak's avatar
Vitor Sessak committed
183 184
    unsigned int res = 0x10000;
    int b = 0;
185 186

    for (x=0; x<10; x++) {
Vitor Sessak's avatar
Vitor Sessak committed
187
        res = (((0x1000000 - (*data) * (*data)) >> 12) * res) >> 12;
188 189 190 191

        if (res == 0)
            return 0;

Vitor Sessak's avatar
Indent  
Vitor Sessak committed
192 193
        if (res > 0x10000)
            return 0; /* We're screwed, might as well go out with a bang. :P */
Vitor Sessak's avatar
Vitor Sessak committed
194 195 196 197

        while (res <= 0x3fff) {
            b++;
            res <<= 2;
198
        }
Vitor Sessak's avatar
Vitor Sessak committed
199
        data++;
200 201
    }

202 203 204 205 206 207
    if (res > 0)
        res = t_sqrt(res);

    res >>= (b + 10);
    res = (res * f) >> 10;
    return res;
208 209
}

210
/* do quarter-block output */
211
static void do_output_subblock(RA144Context *ractx,
212 213
                               const uint16_t  *gsp, unsigned int gval,
                               int16_t *output_buffer, GetBitContext *gb)
214
{
215 216
    uint16_t buffer_a[40];
    uint16_t *block;
Diego Biurrun's avatar
Diego Biurrun committed
217
    int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
218 219 220
    int gain    = get_bits(gb, 8);
    int cb1_idx = get_bits(gb, 7);
    int cb2_idx = get_bits(gb, 7);
221
    int m[3];
222

223 224 225
    if (cba_idx) {
        cba_idx += HALFBLOCK - 1;
        rotate_block(ractx->adapt_cb, buffer_a, cba_idx);
Vitor Sessak's avatar
Vitor Sessak committed
226 227 228
        m[0] = irms(buffer_a, gval) >> 12;
    } else {
        m[0] = 0;
229 230
    }

231 232
    m[1] = ((ftable1[cb1_idx] >> 4) * gval) >> 8;
    m[2] = ((ftable2[cb2_idx] >> 4) * gval) >> 8;
233

234
    memmove(ractx->adapt_cb, ractx->adapt_cb + BLOCKSIZE,
Vitor Sessak's avatar
Vitor Sessak committed
235 236
            (BUFFERSIZE - BLOCKSIZE) * 2);

237
    block = ractx->adapt_cb + BUFFERSIZE - BLOCKSIZE;
238

239 240
    add_wav(gain, cba_idx, m, buffer_a, etable1[cb1_idx], etable2[cb2_idx],
            block);
241

242
    final(gsp, block, output_buffer, ractx->buffer, BLOCKSIZE);
243 244
}

245
static int eq(const int16_t *in, int *target)
246
{
Vitor Sessak's avatar
Vitor Sessak committed
247 248
    int retval = 0;
    int b, c, i;
249
    unsigned int u;
250 251
    int buffer1[10];
    int buffer2[10];
Vitor Sessak's avatar
Vitor Sessak committed
252 253
    int *bp1 = buffer1;
    int *bp2 = buffer2;
254

Vitor Sessak's avatar
Vitor Sessak committed
255 256
    for (i=0; i < 10; i++)
        buffer2[i] = in[i];
257

Vitor Sessak's avatar
Vitor Sessak committed
258
    u = target[9] = bp2[9];
259

Vitor Sessak's avatar
Vitor Sessak committed
260
    if (u + 0x1000 > 0x1fff)
261 262
        return 0; /* We're screwed, might as well go out with a bang. :P */

Vitor Sessak's avatar
Vitor Sessak committed
263
    for (c=8; c >= 0; c--) {
264 265 266 267 268 269 270 271 272 273 274 275
        if (u == 0x1000)
            u++;

        if (u == 0xfffff000)
            u--;

        b = 0x1000-((u * u) >> 12);

        if (b == 0)
            b++;

        for (u=0; u<=c; u++)
Vitor Sessak's avatar
Vitor Sessak committed
276
            bp1[u] = ((bp2[u] - ((target[c+1] * bp2[c-u]) >> 12)) * (0x1000000 / b)) >> 12;
277

Vitor Sessak's avatar
Vitor Sessak committed
278
        target[c] = u = bp1[c];
279 280 281 282

        if ((u + 0x1000) > 0x1fff)
            retval = 1;

283
        FFSWAP(int *, bp1, bp2);
284 285
    }
    return retval;
286 287
}

288 289
static int dec2(RA144Context *ractx, int16_t *decsp, int block_num,
                int copynew, int f)
290
{
291 292
    int work[10];

293 294
    // Interpolate block coefficients from the this frame forth block and
    // last frame forth block
295
    ff_acelp_weighted_vector_sum(decsp, ractx->lpc_coef, ractx->lpc_coef_old,
Vitor Sessak's avatar
Vitor Sessak committed
296
                                 block_num + 1, 3 - block_num, 0, 2, 30);
297 298 299 300

    if (eq(decsp, work)) {
        // The interpolated coefficients are unstable, copy either new or old
        // coefficients
Vitor Sessak's avatar
Vitor Sessak committed
301 302 303 304 305 306 307
        if (copynew) {
            memcpy(decsp, ractx->lpc_coef, 30*sizeof(*decsp));
            return rms(ractx->lpc_refl, f);
        } else {
            memcpy(decsp, ractx->lpc_coef_old, 30*sizeof(*decsp));
            return rms(ractx->lpc_refl_old, f);
        }
308
    } else {
309
        return rms(work, f);
310
    }
311 312 313 314
}

/* Uncompress one block (20 bytes -> 160*2 bytes) */
static int ra144_decode_frame(AVCodecContext * avctx,
Vitor Sessak's avatar
Vitor Sessak committed
315 316
                              void *vdata, int *data_size,
                              const uint8_t * buf, int buf_size)
317
{
318
    static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
Vitor Sessak's avatar
Vitor Sessak committed
319
    RA144Context *ractx = avctx->priv_data;
320
    unsigned int gbuf1[4];
Vitor Sessak's avatar
Vitor Sessak committed
321 322 323
    uint16_t coef_table[3][30];
    uint16_t *gbuf2[4] =
        {coef_table[0], coef_table[1], coef_table[2], ractx->lpc_coef};
Vitor Sessak's avatar
Vitor Sessak committed
324
    unsigned int c;
325
    int i;
326
    int16_t *data = vdata;
Vitor Sessak's avatar
Vitor Sessak committed
327
    unsigned int energy;
328

329
    GetBitContext gb;
330

331 332 333 334 335
    if(buf_size < 20) {
        av_log(avctx, AV_LOG_ERROR,
               "Frame too small (%d bytes). Truncated file?\n", buf_size);
        return buf_size;
    }
336
    init_get_bits(&gb, buf, 20 * 8);
337

338 339
    for (i=0; i<10; i++)
        // "<< 1"? Doesn't this make one value out of two of the table useless?
340
        ractx->lpc_refl[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
341

342
    do_voice(ractx->lpc_refl, ractx->lpc_coef);
343

Vitor Sessak's avatar
Vitor Sessak committed
344
    energy = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
345

346
    gbuf1[0] = dec2(ractx, gbuf2[0], 0, 0, ractx->old_energy);
Vitor Sessak's avatar
Vitor Sessak committed
347 348
    gbuf1[1] = dec2(ractx, gbuf2[1], 1, energy > ractx->old_energy,
                    t_sqrt(energy*ractx->old_energy) >> 12);
349
    gbuf1[2] = dec2(ractx, gbuf2[2], 2, 1, energy);
Vitor Sessak's avatar
Vitor Sessak committed
350
    gbuf1[3] = rms(ractx->lpc_refl, energy);
351 352

    /* do output */
353
    for (c=0; c<4; c++) {
354
        do_output_subblock(ractx, gbuf2[c], gbuf1[c], data, &gb);
355

356 357 358 359
        for (i=0; i<BLOCKSIZE; i++) {
            *data = av_clip_int16(*data << 2);
            data++;
        }
360 361
    }

Vitor Sessak's avatar
Vitor Sessak committed
362
    ractx->old_energy = energy;
363

364
    FFSWAP(unsigned int *, ractx->lpc_refl_old, ractx->lpc_refl);
365
    FFSWAP(int16_t *     , ractx->lpc_coef_old, ractx->lpc_coef);
366

Vitor Sessak's avatar
Vitor Sessak committed
367
    *data_size = 2*160;
368
    return 20;
369 370 371 372 373 374 375 376
}


AVCodec ra_144_decoder =
{
    "real_144",
    CODEC_TYPE_AUDIO,
    CODEC_ID_RA_144,
377
    sizeof(RA144Context),
378 379 380 381
    ra144_decode_init,
    NULL,
    NULL,
    ra144_decode_frame,
382
    .long_name = "RealAudio 1.0 (14.4K)",
383
};