comparison ra144.c @ 6934:ac6f00bed029 libavcodec

Revert r13499, log: Make lpc coefficients 16 bit wide Only one of my samples didn't decode bit-exact after this change, but better be safe than sorry.
author vitor
date Fri, 30 May 2008 19:46:43 +0000
parents d3974b9cde43
children 080e7ee48ed1
comparison
equal deleted inserted replaced
6933:74f920fb6c01 6934:ac6f00bed029
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22 #include "avcodec.h" 22 #include "avcodec.h"
23 #include "bitstream.h" 23 #include "bitstream.h"
24 #include "acelp_vectors.h"
25 #include "ra144.h" 24 #include "ra144.h"
26 25
27 #define NBLOCKS 4 /* number of segments within a block */ 26 #define NBLOCKS 4 /* number of segments within a block */
28 #define BLOCKSIZE 40 /* (quarter) block size in 16-bit words (80 bytes) */ 27 #define BLOCKSIZE 40 /* (quarter) block size in 16-bit words (80 bytes) */
29 #define HALFBLOCK 20 /* BLOCKSIZE/2 */ 28 #define HALFBLOCK 20 /* BLOCKSIZE/2 */
33 /* internal globals */ 32 /* internal globals */
34 typedef struct { 33 typedef struct {
35 unsigned int old_energy; ///< previous frame energy 34 unsigned int old_energy; ///< previous frame energy
36 35
37 /* the swapped buffers */ 36 /* the swapped buffers */
38 unsigned int refl_tables[2][10]; 37 unsigned int lpc_tables[4][10];
39 int16_t coef_tables[2][10];
40 unsigned int *lpc_refl; ///< LPC reflection coefficients 38 unsigned int *lpc_refl; ///< LPC reflection coefficients
41 int16_t *lpc_coef; ///< LPC coefficients 39 unsigned int *lpc_coef; ///< LPC coefficients
42 unsigned int *lpc_refl_old; ///< previous frame LPC reflection coefs 40 unsigned int *lpc_refl_old; ///< previous frame LPC reflection coefs
43 int16_t *lpc_coef_old; ///< previous frame LPC coefficients 41 unsigned int *lpc_coef_old; ///< previous frame LPC coefficients
44 42
45 unsigned int buffer[5]; 43 unsigned int buffer[5];
46 uint16_t adapt_cb[148]; ///< adaptive codebook 44 uint16_t adapt_cb[148]; ///< adaptive codebook
47 } RA144Context; 45 } RA144Context;
48 46
49 static int ra144_decode_init(AVCodecContext * avctx) 47 static int ra144_decode_init(AVCodecContext * avctx)
50 { 48 {
51 RA144Context *ractx = avctx->priv_data; 49 RA144Context *ractx = avctx->priv_data;
52 50
53 ractx->lpc_refl = ractx->refl_tables[0]; 51 ractx->lpc_refl = ractx->lpc_tables[0];
54 ractx->lpc_coef = ractx->coef_tables[0]; 52 ractx->lpc_coef = ractx->lpc_tables[1];
55 ractx->lpc_refl_old = ractx->refl_tables[1]; 53 ractx->lpc_refl_old = ractx->lpc_tables[2];
56 ractx->lpc_coef_old = ractx->coef_tables[1]; 54 ractx->lpc_coef_old = ractx->lpc_tables[3];
57 55
58 return 0; 56 return 0;
59 } 57 }
60 58
61 /** 59 /**
72 70
73 return (ff_sqrt(x << 20) << s) << 2; 71 return (ff_sqrt(x << 20) << s) << 2;
74 } 72 }
75 73
76 /* do 'voice' */ 74 /* do 'voice' */
77 static void do_voice(const int *a1, int16_t *a2) 75 static void do_voice(const int *a1, int *a2)
78 { 76 {
79 int buffer[10]; 77 int buffer[10];
80 int buffer2[10];
81 int *b1 = buffer; 78 int *b1 = buffer;
82 int *b2 = buffer2; 79 int *b2 = a2;
83 int x, y; 80 int x, y;
84
85 for (x=0; x<10; x++)
86 buffer2[x] = a2[x];
87 81
88 for (x=0; x < 10; x++) { 82 for (x=0; x < 10; x++) {
89 b1[x] = a1[x] << 4; 83 b1[x] = a1[x] << 4;
90 84
91 for (y=0; y < x; y++) 85 for (y=0; y < x; y++)
93 87
94 FFSWAP(int *, b1, b2); 88 FFSWAP(int *, b1, b2);
95 } 89 }
96 90
97 for (x=0; x < 10; x++) 91 for (x=0; x < 10; x++)
98 a2[x] = buffer2[x] >> 4; 92 a2[x] >>= 4;
99 } 93 }
100 94
101 /* rotate block */ 95 /* rotate block */
102 static void rotate_block(const int16_t *source, int16_t *target, int offset) 96 static void rotate_block(const int16_t *source, int16_t *target, int offset)
103 { 97 {
240 block); 234 block);
241 235
242 final(gsp, block, output_buffer, ractx->buffer, BLOCKSIZE); 236 final(gsp, block, output_buffer, ractx->buffer, BLOCKSIZE);
243 } 237 }
244 238
239 static int dec1(int16_t *decsp, const int *data, const int *inp, int f)
240 {
241 int i;
242
243 for (i=0; i<30; i++)
244 *(decsp++) = *(inp++);
245
246 return rms(data, f);
247 }
248
245 static int eq(const int16_t *in, int *target) 249 static int eq(const int16_t *in, int *target)
246 { 250 {
247 int retval = 0; 251 int retval = 0;
248 int b, c, i; 252 int b, c, i;
249 unsigned int u; 253 unsigned int u;
287 291
288 static int dec2(RA144Context *ractx, int16_t *decsp, int block_num, 292 static int dec2(RA144Context *ractx, int16_t *decsp, int block_num,
289 int copynew, int f) 293 int copynew, int f)
290 { 294 {
291 int work[10]; 295 int work[10];
296 int a = block_num + 1;
297 int b = NBLOCKS - a;
298 int x;
292 299
293 // Interpolate block coefficients from the this frame forth block and 300 // Interpolate block coefficients from the this frame forth block and
294 // last frame forth block 301 // last frame forth block
295 ff_acelp_weighted_vector_sum(decsp, ractx->lpc_coef, ractx->lpc_coef_old, 302 for (x=0; x<30; x++)
296 block_num + 1, 3 - block_num, 0, 2, 30); 303 decsp[x] = (a * ractx->lpc_coef[x] + b * ractx->lpc_coef_old[x])>> 2;
297 304
298 if (eq(decsp, work)) { 305 if (eq(decsp, work)) {
299 // The interpolated coefficients are unstable, copy either new or old 306 // The interpolated coefficients are unstable, copy either new or old
300 // coefficients 307 // coefficients
301 if (copynew) { 308 if (copynew)
302 memcpy(decsp, ractx->lpc_coef, 30*sizeof(*decsp)); 309 return dec1(decsp, ractx->lpc_refl, ractx->lpc_coef, f);
303 return rms(ractx->lpc_refl, f); 310 else
304 } else { 311 return dec1(decsp, ractx->lpc_refl_old, ractx->lpc_coef_old, f);
305 memcpy(decsp, ractx->lpc_coef_old, 30*sizeof(*decsp));
306 return rms(ractx->lpc_refl_old, f);
307 }
308 } else { 312 } else {
309 return rms(work, f); 313 return rms(work, f);
310 } 314 }
311 } 315 }
312 316
314 static int ra144_decode_frame(AVCodecContext * avctx, 318 static int ra144_decode_frame(AVCodecContext * avctx,
315 void *vdata, int *data_size, 319 void *vdata, int *data_size,
316 const uint8_t * buf, int buf_size) 320 const uint8_t * buf, int buf_size)
317 { 321 {
318 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2}; 322 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
319 RA144Context *ractx = avctx->priv_data; 323 unsigned int gbuf1[4];
320 unsigned int refl_rms[4]; 324 uint16_t gbuf2[4][30];
321 uint16_t coef_table[3][30]; 325 unsigned int a, c;
322 uint16_t *gbuf2[4] =
323 {coef_table[0], coef_table[1], coef_table[2], ractx->lpc_coef};
324 unsigned int c;
325 int i; 326 int i;
326 int16_t *data = vdata; 327 int16_t *data = vdata;
327 unsigned int energy; 328 unsigned int energy;
328 329
330 RA144Context *ractx = avctx->priv_data;
329 GetBitContext gb; 331 GetBitContext gb;
330 332
331 if(buf_size < 20) { 333 if(buf_size < 20) {
332 av_log(avctx, AV_LOG_ERROR, 334 av_log(avctx, AV_LOG_ERROR,
333 "Frame too small (%d bytes). Truncated file?\n", buf_size); 335 "Frame too small (%d bytes). Truncated file?\n", buf_size);
340 ractx->lpc_refl[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1]; 342 ractx->lpc_refl[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
341 343
342 do_voice(ractx->lpc_refl, ractx->lpc_coef); 344 do_voice(ractx->lpc_refl, ractx->lpc_coef);
343 345
344 energy = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries? 346 energy = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
345 347 a = t_sqrt(energy*ractx->old_energy) >> 12;
346 refl_rms[0] = dec2(ractx, gbuf2[0], 0, 0, ractx->old_energy); 348
347 refl_rms[1] = dec2(ractx, gbuf2[1], 1, energy > ractx->old_energy, 349 gbuf1[0] = dec2(ractx, gbuf2[0], 0, 0, ractx->old_energy);
348 t_sqrt(energy*ractx->old_energy) >> 12); 350 gbuf1[1] = dec2(ractx, gbuf2[1], 1, energy > ractx->old_energy, a);
349 refl_rms[2] = dec2(ractx, gbuf2[2], 2, 1, energy); 351 gbuf1[2] = dec2(ractx, gbuf2[2], 2, 1, energy);
350 refl_rms[3] = rms(ractx->lpc_refl, energy); 352 gbuf1[3] = dec1(gbuf2[3], ractx->lpc_refl, ractx->lpc_coef, energy);
351 353
352 /* do output */ 354 /* do output */
353 for (c=0; c<4; c++) { 355 for (c=0; c<4; c++) {
354 do_output_subblock(ractx, gbuf2[c], refl_rms[c], data, &gb); 356 do_output_subblock(ractx, gbuf2[c], gbuf1[c], data, &gb);
355 357
356 for (i=0; i<BLOCKSIZE; i++) { 358 for (i=0; i<BLOCKSIZE; i++) {
357 *data = av_clip_int16(*data << 2); 359 *data = av_clip_int16(*data << 2);
358 data++; 360 data++;
359 } 361 }
360 } 362 }
361 363
362 ractx->old_energy = energy; 364 ractx->old_energy = energy;
363 365
364 FFSWAP(unsigned int *, ractx->lpc_refl_old, ractx->lpc_refl); 366 FFSWAP(unsigned int *, ractx->lpc_refl_old, ractx->lpc_refl);
365 FFSWAP(int16_t * , ractx->lpc_coef_old, ractx->lpc_coef); 367 FFSWAP(unsigned int *, ractx->lpc_coef_old, ractx->lpc_coef);
366 368
367 *data_size = 2*160; 369 *data_size = 2*160;
368 return 20; 370 return 20;
369 } 371 }
370 372