comparison g726.c @ 2979:bfabfdf9ce55 libavcodec

COSMETICS: tabs --> spaces, some prettyprinting
author diego
date Thu, 22 Dec 2005 01:10:11 +0000
parents ef2149182f1c
children 0b546eab515d
comparison
equal deleted inserted replaced
2978:403183bbb505 2979:bfabfdf9ce55
29 * G.726 Standard uses rather odd 11bit floating point arithmentic for 29 * G.726 Standard uses rather odd 11bit floating point arithmentic for
30 * numerous occasions. It's a mistery to me why they did it this way 30 * numerous occasions. It's a mistery to me why they did it this way
31 * instead of simply using 32bit integer arithmetic. 31 * instead of simply using 32bit integer arithmetic.
32 */ 32 */
33 typedef struct Float11 { 33 typedef struct Float11 {
34 int sign; /**< 1bit sign */ 34 int sign; /**< 1bit sign */
35 int exp; /**< 4bit exponent */ 35 int exp; /**< 4bit exponent */
36 int mant; /**< 6bit mantissa */ 36 int mant; /**< 6bit mantissa */
37 } Float11; 37 } Float11;
38 38
39 static inline Float11* i2f(int16_t i, Float11* f) 39 static inline Float11* i2f(int16_t i, Float11* f)
40 { 40 {
41 f->sign = (i < 0); 41 f->sign = (i < 0);
42 if (f->sign) 42 if (f->sign)
43 i = -i; 43 i = -i;
44 f->exp = av_log2_16bit(i) + !!i; 44 f->exp = av_log2_16bit(i) + !!i;
45 f->mant = i? (i<<6) >> f->exp : 45 f->mant = i? (i<<6) >> f->exp :
46 1<<5; 46 1<<5;
47 return f; 47 return f;
48 } 48 }
49 49
50 static inline int16_t mult(Float11* f1, Float11* f2) 50 static inline int16_t mult(Float11* f1, Float11* f2)
51 { 51 {
52 int res, exp; 52 int res, exp;
53 53
54 exp = f1->exp + f2->exp; 54 exp = f1->exp + f2->exp;
55 res = (((f1->mant * f2->mant) + 0x30) >> 4) << 7; 55 res = (((f1->mant * f2->mant) + 0x30) >> 4) << 7;
56 res = exp > 26 ? res << (exp - 26) : res >> (26 - exp); 56 res = exp > 26 ? res << (exp - 26) : res >> (26 - exp);
57 return (f1->sign ^ f2->sign) ? -res : res; 57 return (f1->sign ^ f2->sign) ? -res : res;
58 } 58 }
59 59
60 static inline int sgn(int value) 60 static inline int sgn(int value)
61 { 61 {
62 return (value < 0) ? -1 : 1; 62 return (value < 0) ? -1 : 1;
63 } 63 }
64 64
65 typedef struct G726Tables { 65 typedef struct G726Tables {
66 int bits; /**< bits per sample */ 66 int bits; /**< bits per sample */
67 int* quant; /**< quantization table */ 67 int* quant; /**< quantization table */
68 int* iquant; /**< inverse quantization table */ 68 int* iquant; /**< inverse quantization table */
69 int* W; /**< special table #1 ;-) */ 69 int* W; /**< special table #1 ;-) */
70 int* F; /**< special table #2 */ 70 int* F; /**< special table #2 */
71 } G726Tables; 71 } G726Tables;
72 72
73 typedef struct G726Context { 73 typedef struct G726Context {
74 G726Tables* tbls; /**< static tables needed for computation */ 74 G726Tables* tbls; /**< static tables needed for computation */
75 75
76 Float11 sr[2]; /**< prev. reconstructed samples */ 76 Float11 sr[2]; /**< prev. reconstructed samples */
77 Float11 dq[6]; /**< prev. difference */ 77 Float11 dq[6]; /**< prev. difference */
78 int a[2]; /**< second order predictor coeffs */ 78 int a[2]; /**< second order predictor coeffs */
79 int b[6]; /**< sixth order predictor coeffs */ 79 int b[6]; /**< sixth order predictor coeffs */
80 int pk[2]; /**< signs of prev. 2 sez + dq */ 80 int pk[2]; /**< signs of prev. 2 sez + dq */
81 81
82 int ap; /**< scale factor control */ 82 int ap; /**< scale factor control */
83 int yu; /**< fast scale factor */ 83 int yu; /**< fast scale factor */
84 int yl; /**< slow scale factor */ 84 int yl; /**< slow scale factor */
85 int dms; /**< short average magnitude of F[i] */ 85 int dms; /**< short average magnitude of F[i] */
86 int dml; /**< long average magnitude of F[i] */ 86 int dml; /**< long average magnitude of F[i] */
87 int td; /**< tone detect */ 87 int td; /**< tone detect */
88 88
89 int se; /**< estimated signal for the next iteration */ 89 int se; /**< estimated signal for the next iteration */
90 int sez; /**< estimated second order prediction */ 90 int sez; /**< estimated second order prediction */
91 int y; /**< quantizer scaling factor for the next iteration */ 91 int y; /**< quantizer scaling factor for the next iteration */
92 } G726Context; 92 } G726Context;
93 93
94 static int quant_tbl16[] = /**< 16kbit/s 2bits per sample */ 94 static int quant_tbl16[] = /**< 16kbit/s 2bits per sample */
95 { 260, INT_MAX }; 95 { 260, INT_MAX };
96 static int iquant_tbl16[] = 96 static int iquant_tbl16[] =
111 111
112 static int quant_tbl32[] = /**< 32kbit/s 4bits per sample */ 112 static int quant_tbl32[] = /**< 32kbit/s 4bits per sample */
113 { -125, 79, 177, 245, 299, 348, 399, INT_MAX }; 113 { -125, 79, 177, 245, 299, 348, 399, INT_MAX };
114 static int iquant_tbl32[] = 114 static int iquant_tbl32[] =
115 { INT_MIN, 4, 135, 213, 273, 323, 373, 425, 115 { INT_MIN, 4, 135, 213, 273, 323, 373, 425,
116 425, 373, 323, 273, 213, 135, 4, INT_MIN }; 116 425, 373, 323, 273, 213, 135, 4, INT_MIN };
117 static int W_tbl32[] = 117 static int W_tbl32[] =
118 { -12, 18, 41, 64, 112, 198, 355, 1122, 118 { -12, 18, 41, 64, 112, 198, 355, 1122,
119 1122, 355, 198, 112, 64, 41, 18, -12}; 119 1122, 355, 198, 112, 64, 41, 18, -12};
120 static int F_tbl32[] = 120 static int F_tbl32[] =
121 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 }; 121 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
122 122
123 static int quant_tbl40[] = /**< 40kbit/s 5bits per sample */ 123 static int quant_tbl40[] = /**< 40kbit/s 5bits per sample */
124 { -122, -16, 67, 138, 197, 249, 297, 338, 124 { -122, -16, 67, 138, 197, 249, 297, 338,
125 377, 412, 444, 474, 501, 527, 552, INT_MAX }; 125 377, 412, 444, 474, 501, 527, 552, INT_MAX };
126 static int iquant_tbl40[] = 126 static int iquant_tbl40[] =
127 { INT_MIN, -66, 28, 104, 169, 224, 274, 318, 127 { INT_MIN, -66, 28, 104, 169, 224, 274, 318,
128 358, 395, 429, 459, 488, 514, 539, 566, 128 358, 395, 429, 459, 488, 514, 539, 566,
129 566, 539, 514, 488, 459, 429, 395, 358, 129 566, 539, 514, 488, 459, 429, 395, 358,
130 318, 274, 224, 169, 104, 28, -66, INT_MIN }; 130 318, 274, 224, 169, 104, 28, -66, INT_MIN };
131 static int W_tbl40[] = 131 static int W_tbl40[] =
132 { 14, 14, 24, 39, 40, 41, 58, 100, 132 { 14, 14, 24, 39, 40, 41, 58, 100,
133 141, 179, 219, 280, 358, 440, 529, 696, 133 141, 179, 219, 280, 358, 440, 529, 696,
134 696, 529, 440, 358, 280, 219, 179, 141, 134 696, 529, 440, 358, 280, 219, 179, 141,
135 100, 58, 41, 40, 39, 24, 14, 14 }; 135 100, 58, 41, 40, 39, 24, 14, 14 };
136 static int F_tbl40[] = 136 static int F_tbl40[] =
137 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6, 137 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
138 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; 138 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
139 139
140 static G726Tables G726Tables_pool[] = 140 static G726Tables G726Tables_pool[] =
141 {{ 2, quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 }, 141 {{ 2, quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
142 { 3, quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 }, 142 { 3, quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
143 { 4, quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 }, 143 { 4, quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
144 { 5, quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }}; 144 { 5, quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
145 145
146 146
147 /** 147 /**
148 * Para 4.2.2 page 18: Adaptive quantizer. 148 * Para 4.2.2 page 18: Adaptive quantizer.
205 /* Update second order predictor coefficient A2 and A1 */ 205 /* Update second order predictor coefficient A2 and A1 */
206 pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0; 206 pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
207 dq0 = dq ? sgn(dq) : 0; 207 dq0 = dq ? sgn(dq) : 0;
208 if (tr) { 208 if (tr) {
209 c->a[0] = 0; 209 c->a[0] = 0;
210 c->a[1] = 0; 210 c->a[1] = 0;
211 for (i=0; i<6; i++) 211 for (i=0; i<6; i++)
212 c->b[i] = 0; 212 c->b[i] = 0;
213 } else { 213 } else {
214 /* This is a bit crazy, but it really is +255 not +256 */ 214 /* This is a bit crazy, but it really is +255 not +256 */
215 fa1 = clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255); 215 fa1 = clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
216 216
217 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7); 217 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
218 c->a[1] = clip(c->a[1], -12288, 12288); 218 c->a[1] = clip(c->a[1], -12288, 12288);
219 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8); 219 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
220 c->a[0] = clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]); 220 c->a[0] = clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
221 221
222 for (i=0; i<6; i++) 222 for (i=0; i<6; i++)
223 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8); 223 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
224 } 224 }
225 225
226 /* Update Dq and Sr and Pk */ 226 /* Update Dq and Sr and Pk */
227 c->pk[1] = c->pk[0]; 227 c->pk[1] = c->pk[0];
228 c->pk[0] = pk0 ? pk0 : 1; 228 c->pk[0] = pk0 ? pk0 : 1;
321 { 321 {
322 AVG726Context* c = (AVG726Context*)avctx->priv_data; 322 AVG726Context* c = (AVG726Context*)avctx->priv_data;
323 323
324 if (avctx->channels != 1 || 324 if (avctx->channels != 1 ||
325 (avctx->bit_rate != 16000 && avctx->bit_rate != 24000 && 325 (avctx->bit_rate != 16000 && avctx->bit_rate != 24000 &&
326 avctx->bit_rate != 32000 && avctx->bit_rate != 40000)) { 326 avctx->bit_rate != 32000 && avctx->bit_rate != 40000)) {
327 av_log(avctx, AV_LOG_ERROR, "G726: unsupported audio format\n"); 327 av_log(avctx, AV_LOG_ERROR, "G726: unsupported audio format\n");
328 return -1; 328 return -1;
329 } 329 }
330 if (avctx->sample_rate != 8000 && avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL) { 330 if (avctx->sample_rate != 8000 && avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL) {
331 av_log(avctx, AV_LOG_ERROR, "G726: unsupported audio format\n"); 331 av_log(avctx, AV_LOG_ERROR, "G726: unsupported audio format\n");
332 return -1; 332 return -1;
333 } 333 }
334 g726_reset(&c->c, avctx->bit_rate); 334 g726_reset(&c->c, avctx->bit_rate);
335 c->code_size = c->c.tbls->bits; 335 c->code_size = c->c.tbls->bits;
336 c->bit_buffer = 0; 336 c->bit_buffer = 0;
337 c->bits_left = 0; 337 c->bits_left = 0;
382 382
383 mask = (1<<c->code_size) - 1; 383 mask = (1<<c->code_size) - 1;
384 init_get_bits(&gb, buf, buf_size * 8); 384 init_get_bits(&gb, buf, buf_size * 8);
385 if (c->bits_left) { 385 if (c->bits_left) {
386 int s = c->code_size - c->bits_left;; 386 int s = c->code_size - c->bits_left;;
387 code = (c->bit_buffer << s) | get_bits(&gb, s); 387 code = (c->bit_buffer << s) | get_bits(&gb, s);
388 *samples++ = g726_decode(&c->c, code & mask); 388 *samples++ = g726_decode(&c->c, code & mask);
389 } 389 }
390 390
391 while (get_bits_count(&gb) + c->code_size <= buf_size*8) 391 while (get_bits_count(&gb) + c->code_size <= buf_size*8)
392 *samples++ = g726_decode(&c->c, get_bits(&gb, c->code_size) & mask); 392 *samples++ = g726_decode(&c->c, get_bits(&gb, c->code_size) & mask);
393 393
394 c->bits_left = buf_size*8 - get_bits_count(&gb); 394 c->bits_left = buf_size*8 - get_bits_count(&gb);
395 c->bit_buffer = get_bits(&gb, c->bits_left); 395 c->bit_buffer = get_bits(&gb, c->bits_left);
396 396
397 out: 397 out: