Move ff_sqrt() to libavutil/intmath.h
[libav.git] / libavcodec / ra144.c
CommitLineData
b8414bbd
NK
1/*
2 * Real Audio 1.0 (14.4K)
b83ff6c7
VS
3 *
4 * Copyright (c) 2008 Vitor Sessak
5 * Copyright (c) 2003 Nick Kurshev
6 * Based on public domain decoder at http://www.honeypot.net/audio
b8414bbd 7 *
b78e7197
DB
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
b8414bbd
NK
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
b78e7197 13 * version 2.1 of the License, or (at your option) any later version.
b8414bbd 14 *
b78e7197 15 * FFmpeg is distributed in the hope that it will be useful,
b8414bbd
NK
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
b78e7197 21 * License along with FFmpeg; if not, write to the Free Software
5509bffa 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
b8414bbd
NK
23 */
24
94ca624f 25#include "libavutil/intmath.h"
b8414bbd 26#include "avcodec.h"
9106a698 27#include "get_bits.h"
9085af07 28#include "ra144.h"
4599d22c 29#include "celp_filters.h"
b8414bbd 30
c17fd98c
VS
31#define NBLOCKS 4 ///< number of subblocks within a block
32#define BLOCKSIZE 40 ///< subblock size in 16-bit words
33#define BUFFERSIZE 146 ///< the size of the adaptive codebook
b8414bbd
NK
34
35
b8414bbd 36typedef struct {
643bae38
FL
37 AVCodecContext *avctx;
38
d26a180f 39 unsigned int old_energy; ///< previous frame energy
47eb8187 40
218e453a 41 unsigned int lpc_tables[2][10];
73cc419b
VS
42
43 /** LPC coefficients: lpc_coef[0] is the coefficients of the current frame
36b3e36e 44 * and lpc_coef[1] of the previous one. */
73cc419b
VS
45 unsigned int *lpc_coef[2];
46
47 unsigned int lpc_refl_rms[2];
47eb8187 48
36b3e36e 49 /** The current subblock padded by the last 10 values of the previous one. */
d488e906
VS
50 int16_t curr_sblock[50];
51
36b3e36e
DB
52 /** Adaptive codebook, its size is two units bigger to avoid a
53 * buffer overflow. */
c96a764c 54 uint16_t adapt_cb[146+2];
fc9fcbae 55} RA144Context;
b8414bbd 56
8022a8eb 57static av_cold int ra144_decode_init(AVCodecContext * avctx)
b8414bbd 58{
17a7fc8c 59 RA144Context *ractx = avctx->priv_data;
b8414bbd 60
643bae38
FL
61 ractx->avctx = avctx;
62
73cc419b
VS
63 ractx->lpc_coef[0] = ractx->lpc_tables[0];
64 ractx->lpc_coef[1] = ractx->lpc_tables[1];
b8414bbd 65
fd76c37f 66 avctx->sample_fmt = SAMPLE_FMT_S16;
47eb8187 67 return 0;
b8414bbd
NK
68}
69
e9655b3d 70/**
30bd40f2 71 * Evaluate sqrt(x << 24). x must fit in 20 bits. This value is evaluated in an
e9655b3d
VS
72 * odd way to make the output identical to the binary decoder.
73 */
b8414bbd
NK
74static int t_sqrt(unsigned int x)
75{
70e70867 76 int s = 2;
47eb8187
VS
77 while (x > 0xfff) {
78 s++;
b4d46e8c 79 x >>= 2;
47eb8187
VS
80 }
81
70e70867 82 return ff_sqrt(x << 20) << s;
b8414bbd
NK
83}
84
2f7d6835
VS
85/**
86 * Evaluate the LPC filter coefficients from the reflection coefficients.
907892ee 87 * Does the inverse of the eval_refl() function.
2f7d6835 88 */
80e9f30c 89static void eval_coefs(int *coefs, const int *refl)
b8414bbd 90{
47eb8187 91 int buffer[10];
1d46ba66 92 int *b1 = buffer;
d981252c 93 int *b2 = coefs;
6c9c8b06 94 int i, j;
115329f1 95
6c9c8b06
VS
96 for (i=0; i < 10; i++) {
97 b1[i] = refl[i] << 4;
1d46ba66 98
6c9c8b06
VS
99 for (j=0; j < i; j++)
100 b1[j] = ((refl[i] * b2[i-j-1]) >> 12) + b2[j];
b8414bbd 101
0d493edd 102 FFSWAP(int *, b1, b2);
b8414bbd 103 }
47eb8187 104
6c9c8b06
VS
105 for (i=0; i < 10; i++)
106 coefs[i] >>= 4;
b8414bbd
NK
107}
108
51062b92
VS
109/**
110 * Copy the last offset values of *source to *target. If those values are not
111 * enough to fill the target buffer, fill it with another copy of those values.
112 */
80e9f30c 113static void copy_and_dup(int16_t *target, const int16_t *source, int offset)
b8414bbd 114{
f1bf6ba3 115 source += BUFFERSIZE - offset;
be4488dc 116
b28bdce6
VS
117 memcpy(target, source, FFMIN(BLOCKSIZE, offset)*sizeof(*target));
118 if (offset < BLOCKSIZE)
5983d1c4 119 memcpy(target + offset, source, (BLOCKSIZE - offset)*sizeof(*target));
b8414bbd
NK
120}
121
94be3bb5 122/** inverse root mean square */
536ee174 123static int irms(const int16_t *data)
b8414bbd 124{
c3298582 125 unsigned int i, sum = 0;
47eb8187 126
c3298582
VS
127 for (i=0; i < BLOCKSIZE; i++)
128 sum += data[i] * data[i];
47eb8187
VS
129
130 if (sum == 0)
131 return 0; /* OOPS - division by zero */
132
536ee174 133 return 0x20000000 / (t_sqrt(sum) >> 8);
b8414bbd
NK
134}
135
80e9f30c
VS
136static void add_wav(int16_t *dest, int n, int skip_first, int *m,
137 const int16_t *s1, const int8_t *s2, const int8_t *s3)
b8414bbd 138{
d6ff684b
VS
139 int i;
140 int v[3];
47eb8187 141
d6ff684b 142 v[0] = 0;
f8b5f7c7 143 for (i=!skip_first; i<3; i++)
20242eed 144 v[i] = (gain_val_tab[n][i] * m[i]) >> gain_exp_tab[n];
47eb8187 145
9e27e0d4 146 if (v[0]) {
8d2544bd
VS
147 for (i=0; i < BLOCKSIZE; i++)
148 dest[i] = (s1[i]*v[0] + s2[i]*v[1] + s3[i]*v[2]) >> 12;
9e27e0d4
VS
149 } else {
150 for (i=0; i < BLOCKSIZE; i++)
8d2544bd 151 dest[i] = ( s2[i]*v[1] + s3[i]*v[2]) >> 12;
9e27e0d4 152 }
b8414bbd
NK
153}
154
56a6fac8 155static unsigned int rescale_rms(unsigned int rms, unsigned int energy)
11bb2eb0
VS
156{
157 return (rms * energy) >> 10;
158}
159
160static unsigned int rms(const int *data)
b8414bbd 161{
6c9c8b06 162 int i;
08533939 163 unsigned int res = 0x10000;
2f1cad0e 164 int b = 10;
47eb8187 165
6c9c8b06
VS
166 for (i=0; i < 10; i++) {
167 res = (((0x1000000 - data[i]*data[i]) >> 12) * res) >> 12;
47eb8187
VS
168
169 if (res == 0)
170 return 0;
171
08533939
VS
172 while (res <= 0x3fff) {
173 b++;
174 res <<= 2;
47eb8187 175 }
b8414bbd 176 }
b8414bbd 177
2f1cad0e 178 return t_sqrt(res) >> b;
b8414bbd
NK
179}
180
74adaa3a
VS
181static void do_output_subblock(RA144Context *ractx, const uint16_t *lpc_coefs,
182 int gval, GetBitContext *gb)
47abbd3e 183{
9d3c35fc
VS
184 uint16_t buffer_a[40];
185 uint16_t *block;
ae52bbe5 186 int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
62869244
VS
187 int gain = get_bits(gb, 8);
188 int cb1_idx = get_bits(gb, 7);
189 int cb2_idx = get_bits(gb, 7);
d6ff684b 190 int m[3];
47abbd3e 191
62869244 192 if (cba_idx) {
c31333e1 193 cba_idx += BLOCKSIZE/2 - 1;
80e9f30c 194 copy_and_dup(buffer_a, ractx->adapt_cb, cba_idx);
536ee174 195 m[0] = (irms(buffer_a) * gval) >> 12;
5012cf88
VS
196 } else {
197 m[0] = 0;
47abbd3e
VS
198 }
199
738ef8fd
VS
200 m[1] = (cb1_base[cb1_idx] * gval) >> 8;
201 m[2] = (cb2_base[cb2_idx] * gval) >> 8;
47abbd3e 202
f000bfad 203 memmove(ractx->adapt_cb, ractx->adapt_cb + BLOCKSIZE,
0a03b6f0 204 (BUFFERSIZE - BLOCKSIZE) * sizeof(*ractx->adapt_cb));
6fc4fac0 205
f000bfad 206 block = ractx->adapt_cb + BUFFERSIZE - BLOCKSIZE;
47abbd3e 207
09096c9f 208 add_wav(block, gain, cba_idx, m, cba_idx? buffer_a: NULL,
80e9f30c 209 cb1_vects[cb1_idx], cb2_vects[cb2_idx]);
47abbd3e 210
d5d3536d
VS
211 memcpy(ractx->curr_sblock, ractx->curr_sblock + 40,
212 10*sizeof(*ractx->curr_sblock));
d5d3536d 213
4599d22c
KG
214 if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + 10, lpc_coefs,
215 block, BLOCKSIZE, 10, 1, 0xfff))
95fc77b9 216 memset(ractx->curr_sblock, 0, 50*sizeof(*ractx->curr_sblock));
47abbd3e
VS
217}
218
98cc8e18 219static void int_to_int16(int16_t *out, const int *inp)
27f19ed5
VS
220{
221 int i;
222
7df8af63 223 for (i=0; i < 10; i++)
a90fbeec 224 *out++ = *inp++;
27f19ed5
VS
225}
226
2f7d6835
VS
227/**
228 * Evaluate the reflection coefficients from the filter coefficients.
907892ee 229 * Does the inverse of the eval_coefs() function.
2f7d6835 230 *
36b3e36e 231 * @return 1 if one of the reflection coefficients is greater than
2f7d6835
VS
232 * 4095, 0 if not.
233 */
643bae38 234static int eval_refl(int *refl, const int16_t *coefs, AVCodecContext *avctx)
b8414bbd 235{
b888abe1 236 int b, i, j;
bd0d10c2
VS
237 int buffer1[10];
238 int buffer2[10];
be2c232f
VS
239 int *bp1 = buffer1;
240 int *bp2 = buffer2;
47eb8187 241
be2c232f 242 for (i=0; i < 10; i++)
d981252c 243 buffer2[i] = coefs[i];
47eb8187 244
bdb34c9f 245 refl[9] = bp2[9];
47eb8187 246
bdb34c9f 247 if ((unsigned) bp2[9] + 0x1000 > 0x1fff) {
643bae38 248 av_log(avctx, AV_LOG_ERROR, "Overflow. Broken sample?\n");
b45411e2 249 return 1;
2d73e6c8 250 }
47eb8187 251
b888abe1
VS
252 for (i=8; i >= 0; i--) {
253 b = 0x1000-((bp2[i+1] * bp2[i+1]) >> 12);
47eb8187 254
aa425ada 255 if (!b)
40812fdc 256 b = -2;
47eb8187 257
b888abe1
VS
258 for (j=0; j <= i; j++)
259 bp1[j] = ((bp2[j] - ((refl[i+1] * bp2[i-j]) >> 12)) * (0x1000000 / b)) >> 12;
47eb8187 260
b888abe1 261 if ((unsigned) bp1[i] + 0x1000 > 0x1fff)
c0f0b34b 262 return 1;
47eb8187 263
4fdb41a8
VS
264 refl[i] = bp1[i];
265
ab5e6526 266 FFSWAP(int *, bp1, bp2);
47eb8187 267 }
c0f0b34b 268 return 0;
b8414bbd
NK
269}
270
3e30f46f 271static int interp(RA144Context *ractx, int16_t *out, int a,
afb40e72 272 int copyold, int energy)
b8414bbd 273{
47eb8187 274 int work[10];
27f19ed5 275 int b = NBLOCKS - a;
6c9c8b06 276 int i;
47eb8187 277
36b3e36e
DB
278 // Interpolate block coefficients from the this frame's forth block and
279 // last frame's forth block.
7df8af63 280 for (i=0; i<10; i++)
6c9c8b06 281 out[i] = (a * ractx->lpc_coef[0][i] + b * ractx->lpc_coef[1][i])>> 2;
121ec9e5 282
643bae38 283 if (eval_refl(work, out, ractx->avctx)) {
121ec9e5 284 // The interpolated coefficients are unstable, copy either new or old
36b3e36e 285 // coefficients.
afb40e72
VS
286 int_to_int16(out, ractx->lpc_coef[copyold]);
287 return rescale_rms(ractx->lpc_refl_rms[copyold], energy);
121ec9e5 288 } else {
11bb2eb0 289 return rescale_rms(rms(work), energy);
121ec9e5 290 }
b8414bbd
NK
291}
292
36b3e36e 293/** Uncompress one block (20 bytes -> 160*2 bytes). */
a9764268 294static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
7a00bbad 295 int *data_size, AVPacket *avpkt)
b8414bbd 296{
7a00bbad
TB
297 const uint8_t *buf = avpkt->data;
298 int buf_size = avpkt->size;
ae9b8019 299 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
5116b92d 300 unsigned int refl_rms[4]; // RMS of the reflection coefficients
7df8af63 301 uint16_t block_coefs[4][10]; // LPC coefficients of each sub-block
218e453a 302 unsigned int lpc_refl[10]; // LPC reflection coefficients of the frame
6c9c8b06 303 int i, j;
47eb8187 304 int16_t *data = vdata;
2a7b6f25 305 unsigned int energy;
bd0d10c2 306
27f19ed5 307 RA144Context *ractx = avctx->priv_data;
ae9b8019 308 GetBitContext gb;
47eb8187 309
438abdaa
VS
310 if (*data_size < 2*160)
311 return -1;
312
84490b74
VS
313 if(buf_size < 20) {
314 av_log(avctx, AV_LOG_ERROR,
315 "Frame too small (%d bytes). Truncated file?\n", buf_size);
072f66c9 316 *data_size = 0;
84490b74
VS
317 return buf_size;
318 }
ae9b8019 319 init_get_bits(&gb, buf, 20 * 8);
b8414bbd 320
ae9b8019 321 for (i=0; i<10; i++)
ae70f874 322 lpc_refl[i] = lpc_refl_cb[i][get_bits(&gb, sizes[i])];
b8414bbd 323
73cc419b
VS
324 eval_coefs(ractx->lpc_coef[0], lpc_refl);
325 ractx->lpc_refl_rms[0] = rms(lpc_refl);
b8414bbd 326
7986199f 327 energy = energy_tab[get_bits(&gb, 5)];
b8414bbd 328
3e30f46f
VS
329 refl_rms[0] = interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
330 refl_rms[1] = interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy,
208a2a60 331 t_sqrt(energy*ractx->old_energy) >> 12);
3e30f46f 332 refl_rms[2] = interp(ractx, block_coefs[2], 3, 0, energy);
73cc419b 333 refl_rms[3] = rescale_rms(ractx->lpc_refl_rms[0], energy);
50b8f2cf 334
73cc419b 335 int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
47eb8187 336
6c9c8b06
VS
337 for (i=0; i < 4; i++) {
338 do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
bd0d10c2 339
6c9c8b06
VS
340 for (j=0; j < BLOCKSIZE; j++)
341 *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);
47eb8187
VS
342 }
343
2a7b6f25 344 ractx->old_energy = energy;
73cc419b 345 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
0d493edd 346
73cc419b 347 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
0d493edd 348
37e77035 349 *data_size = 2*160;
47eb8187 350 return 20;
b8414bbd
NK
351}
352
b8414bbd
NK
353AVCodec ra_144_decoder =
354{
355 "real_144",
356 CODEC_TYPE_AUDIO,
357 CODEC_ID_RA_144,
fc9fcbae 358 sizeof(RA144Context),
b8414bbd
NK
359 ra144_decode_init,
360 NULL,
361 NULL,
362 ra144_decode_frame,
fe4bf374 363 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
b8414bbd 364};