comparison mpegaudio.c @ 89:2e88e3afecd0 libavcodec

corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
author glantau
date Mon, 17 Sep 2001 21:19:09 +0000
parents 608c7f964bca
children 5fc0c3af3fe4
comparison
equal deleted inserted replaced
88:06f63b58d2a8 89:2e88e3afecd0
1 /* 1 /*
2 * The simplest mpeg audio layer 2 encoder 2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000 Gerard Lantau. 3 * Copyright (c) 2000, 2001 Gerard Lantau.
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
18 */ 18 */
19 #include "avcodec.h" 19 #include "avcodec.h"
20 #include <math.h> 20 #include <math.h>
21 #include "mpegaudio.h" 21 #include "mpegaudio.h"
22 22
23 #define DCT_BITS 14 /* number of bits for the DCT */ 23 /* currently, cannot change these constants (need to modify
24 #define MUL(a,b) (((a) * (b)) >> DCT_BITS) 24 quantization stage) */
25 #define FIX(a) ((int)((a) * (1 << DCT_BITS))) 25 #define FRAC_BITS 15
26 #define WFRAC_BITS 14
27 #define MUL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
28 #define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
26 29
27 #define SAMPLES_BUF_SIZE 4096 30 #define SAMPLES_BUF_SIZE 4096
28 31
29 typedef struct MpegAudioContext { 32 typedef struct MpegAudioContext {
30 PutBitContext pb; 33 PutBitContext pb;
117 for(i=0;i<s->nb_channels;i++) 120 for(i=0;i<s->nb_channels;i++)
118 s->samples_offset[i] = 0; 121 s->samples_offset[i] = 0;
119 122
120 for(i=0;i<257;i++) { 123 for(i=0;i<257;i++) {
121 int v; 124 int v;
122 v = (mpa_enwindow[i] + 2) >> 2; 125 v = mpa_enwindow[i];
126 #if WFRAC_BITS != 16
127 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
128 #endif
123 filter_bank[i] = v; 129 filter_bank[i] = v;
124 if ((i & 63) != 0) 130 if ((i & 63) != 0)
125 v = -v; 131 v = -v;
126 if (i != 0) 132 if (i != 0)
127 filter_bank[512 - i] = v; 133 filter_bank[512 - i] = v;
166 172
167 return 0; 173 return 0;
168 } 174 }
169 175
170 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */ 176 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
171 static void idct32(int *out, int *tab, int sblimit, int left_shift) 177 static void idct32(int *out, int *tab)
172 { 178 {
173 int i, j; 179 int i, j;
174 int *t, *t1, xr; 180 int *t, *t1, xr;
175 const int *xp = costab32; 181 const int *xp = costab32;
176 182
281 t1 += 2; 287 t1 += 2;
282 xp++; 288 xp++;
283 } while (t >= tab); 289 } while (t >= tab);
284 290
285 for(i=0;i<32;i++) { 291 for(i=0;i<32;i++) {
286 out[i] = tab[bitinv32[i]] << left_shift; 292 out[i] = tab[bitinv32[i]];
287 } 293 }
288 } 294 }
295
296 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
289 297
290 static void filter(MpegAudioContext *s, int ch, short *samples, int incr) 298 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
291 { 299 {
292 short *p, *q; 300 short *p, *q;
293 int sum, offset, i, j, norm, n; 301 int sum, offset, i, j;
294 short tmp[64]; 302 int tmp[64];
295 int tmp1[32]; 303 int tmp1[32];
296 int *out; 304 int *out;
297 305
298 // print_pow1(samples, 1152); 306 // print_pow1(samples, 1152);
299 307
317 sum += p[3*64] * q[3*64]; 325 sum += p[3*64] * q[3*64];
318 sum += p[4*64] * q[4*64]; 326 sum += p[4*64] * q[4*64];
319 sum += p[5*64] * q[5*64]; 327 sum += p[5*64] * q[5*64];
320 sum += p[6*64] * q[6*64]; 328 sum += p[6*64] * q[6*64];
321 sum += p[7*64] * q[7*64]; 329 sum += p[7*64] * q[7*64];
322 tmp[i] = sum >> 14; 330 tmp[i] = sum;
323 p++; 331 p++;
324 q++; 332 q++;
325 } 333 }
326 tmp1[0] = tmp[16]; 334 tmp1[0] = tmp[16] >> WSHIFT;
327 for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16-i]; 335 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
328 for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]-tmp[80-i]; 336 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
329 337
330 /* integer IDCT 32 with normalization. XXX: There may be some 338 idct32(out, tmp1);
331 overflow left */
332 norm = 0;
333 for(i=0;i<32;i++) {
334 norm |= abs(tmp1[i]);
335 }
336 n = av_log2(norm) - 12;
337 if (n > 0) {
338 for(i=0;i<32;i++)
339 tmp1[i] >>= n;
340 } else {
341 n = 0;
342 }
343
344 idct32(out, tmp1, s->sblimit, n);
345 339
346 /* advance of 32 samples */ 340 /* advance of 32 samples */
347 offset -= 32; 341 offset -= 32;
348 out += 32; 342 out += 32;
349 /* handle the wrap around */ 343 /* handle the wrap around */
389 index++; 383 index++;
390 } else { 384 } else {
391 index = 0; /* very unlikely case of overflow */ 385 index = 0; /* very unlikely case of overflow */
392 } 386 }
393 } else { 387 } else {
394 index = 63; 388 index = 62; /* value 63 is not allowed */
395 } 389 }
396 390
397 #if 0 391 #if 0
398 printf("%2d:%d in=%x %x %d\n", 392 printf("%2d:%d in=%x %x %d\n",
399 j, i, vmax, scale_factor_table[index], index); 393 j, i, vmax, scale_factor_table[index], index);
400 #endif 394 #endif
401 /* store the scale factor */ 395 /* store the scale factor */