Mercurial > libavcodec.hg
comparison g726.c @ 4119:85438e10d72d libavcodec
reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
author | diego |
---|---|
date | Wed, 01 Nov 2006 21:09:14 +0000 |
parents | c8c591fe26f8 |
children | 9bf957e669f0 |
comparison
equal
deleted
inserted
replaced
4118:a1786732cd62 | 4119:85438e10d72d |
---|---|
31 * G.726 Standard uses rather odd 11bit floating point arithmentic for | 31 * G.726 Standard uses rather odd 11bit floating point arithmentic for |
32 * numerous occasions. It's a mistery to me why they did it this way | 32 * numerous occasions. It's a mistery to me why they did it this way |
33 * instead of simply using 32bit integer arithmetic. | 33 * instead of simply using 32bit integer arithmetic. |
34 */ | 34 */ |
35 typedef struct Float11 { | 35 typedef struct Float11 { |
36 int sign; /**< 1bit sign */ | 36 int sign; /**< 1bit sign */ |
37 int exp; /**< 4bit exponent */ | 37 int exp; /**< 4bit exponent */ |
38 int mant; /**< 6bit mantissa */ | 38 int mant; /**< 6bit mantissa */ |
39 } Float11; | 39 } Float11; |
40 | 40 |
41 static inline Float11* i2f(int16_t i, Float11* f) | 41 static inline Float11* i2f(int16_t i, Float11* f) |
42 { | 42 { |
43 f->sign = (i < 0); | 43 f->sign = (i < 0); |
44 if (f->sign) | 44 if (f->sign) |
45 i = -i; | 45 i = -i; |
46 f->exp = av_log2_16bit(i) + !!i; | 46 f->exp = av_log2_16bit(i) + !!i; |
47 f->mant = i? (i<<6) >> f->exp : | 47 f->mant = i? (i<<6) >> f->exp : 1<<5; |
48 1<<5; | 48 return f; |
49 return f; | |
50 } | 49 } |
51 | 50 |
52 static inline int16_t mult(Float11* f1, Float11* f2) | 51 static inline int16_t mult(Float11* f1, Float11* f2) |
53 { | 52 { |
54 int res, exp; | 53 int res, exp; |
59 return (f1->sign ^ f2->sign) ? -res : res; | 58 return (f1->sign ^ f2->sign) ? -res : res; |
60 } | 59 } |
61 | 60 |
62 static inline int sgn(int value) | 61 static inline int sgn(int value) |
63 { | 62 { |
64 return (value < 0) ? -1 : 1; | 63 return (value < 0) ? -1 : 1; |
65 } | 64 } |
66 | 65 |
67 typedef struct G726Tables { | 66 typedef struct G726Tables { |
68 int bits; /**< bits per sample */ | 67 int bits; /**< bits per sample */ |
69 int* quant; /**< quantization table */ | 68 int* quant; /**< quantization table */ |
70 int* iquant; /**< inverse quantization table */ | 69 int* iquant; /**< inverse quantization table */ |
71 int* W; /**< special table #1 ;-) */ | 70 int* W; /**< special table #1 ;-) */ |
72 int* F; /**< special table #2 */ | 71 int* F; /**< special table #2 */ |
73 } G726Tables; | 72 } G726Tables; |
74 | 73 |
75 typedef struct G726Context { | 74 typedef struct G726Context { |
76 G726Tables* tbls; /**< static tables needed for computation */ | 75 G726Tables* tbls; /**< static tables needed for computation */ |
77 | 76 |
78 Float11 sr[2]; /**< prev. reconstructed samples */ | 77 Float11 sr[2]; /**< prev. reconstructed samples */ |
79 Float11 dq[6]; /**< prev. difference */ | 78 Float11 dq[6]; /**< prev. difference */ |
80 int a[2]; /**< second order predictor coeffs */ | 79 int a[2]; /**< second order predictor coeffs */ |
81 int b[6]; /**< sixth order predictor coeffs */ | 80 int b[6]; /**< sixth order predictor coeffs */ |
82 int pk[2]; /**< signs of prev. 2 sez + dq */ | 81 int pk[2]; /**< signs of prev. 2 sez + dq */ |
83 | 82 |
84 int ap; /**< scale factor control */ | 83 int ap; /**< scale factor control */ |
85 int yu; /**< fast scale factor */ | 84 int yu; /**< fast scale factor */ |
86 int yl; /**< slow scale factor */ | 85 int yl; /**< slow scale factor */ |
87 int dms; /**< short average magnitude of F[i] */ | 86 int dms; /**< short average magnitude of F[i] */ |
88 int dml; /**< long average magnitude of F[i] */ | 87 int dml; /**< long average magnitude of F[i] */ |
89 int td; /**< tone detect */ | 88 int td; /**< tone detect */ |
90 | 89 |
91 int se; /**< estimated signal for the next iteration */ | 90 int se; /**< estimated signal for the next iteration */ |
92 int sez; /**< estimated second order prediction */ | 91 int sez; /**< estimated second order prediction */ |
93 int y; /**< quantizer scaling factor for the next iteration */ | 92 int y; /**< quantizer scaling factor for the next iteration */ |
94 } G726Context; | 93 } G726Context; |
95 | 94 |
96 static int quant_tbl16[] = /**< 16kbit/s 2bits per sample */ | 95 static int quant_tbl16[] = /**< 16kbit/s 2bits per sample */ |
97 { 260, INT_MAX }; | 96 { 260, INT_MAX }; |
98 static int iquant_tbl16[] = | 97 static int iquant_tbl16[] = |
99 { 116, 365, 365, 116 }; | 98 { 116, 365, 365, 116 }; |
100 static int W_tbl16[] = | 99 static int W_tbl16[] = |
101 { -22, 439, 439, -22 }; | 100 { -22, 439, 439, -22 }; |
102 static int F_tbl16[] = | 101 static int F_tbl16[] = |
103 { 0, 7, 7, 0 }; | 102 { 0, 7, 7, 0 }; |
104 | 103 |
105 static int quant_tbl24[] = /**< 24kbit/s 3bits per sample */ | 104 static int quant_tbl24[] = /**< 24kbit/s 3bits per sample */ |
106 { 7, 217, 330, INT_MAX }; | 105 { 7, 217, 330, INT_MAX }; |
107 static int iquant_tbl24[] = | 106 static int iquant_tbl24[] = |
108 { INT_MIN, 135, 273, 373, 373, 273, 135, INT_MIN }; | 107 { INT_MIN, 135, 273, 373, 373, 273, 135, INT_MIN }; |
109 static int W_tbl24[] = | 108 static int W_tbl24[] = |
110 { -4, 30, 137, 582, 582, 137, 30, -4 }; | 109 { -4, 30, 137, 582, 582, 137, 30, -4 }; |
111 static int F_tbl24[] = | 110 static int F_tbl24[] = |
112 { 0, 1, 2, 7, 7, 2, 1, 0 }; | 111 { 0, 1, 2, 7, 7, 2, 1, 0 }; |
113 | 112 |
114 static int quant_tbl32[] = /**< 32kbit/s 4bits per sample */ | 113 static int quant_tbl32[] = /**< 32kbit/s 4bits per sample */ |
115 { -125, 79, 177, 245, 299, 348, 399, INT_MAX }; | 114 { -125, 79, 177, 245, 299, 348, 399, INT_MAX }; |
116 static int iquant_tbl32[] = | 115 static int iquant_tbl32[] = |
117 { INT_MIN, 4, 135, 213, 273, 323, 373, 425, | 116 { INT_MIN, 4, 135, 213, 273, 323, 373, 425, |
118 425, 373, 323, 273, 213, 135, 4, INT_MIN }; | 117 425, 373, 323, 273, 213, 135, 4, INT_MIN }; |
119 static int W_tbl32[] = | 118 static int W_tbl32[] = |
120 { -12, 18, 41, 64, 112, 198, 355, 1122, | 119 { -12, 18, 41, 64, 112, 198, 355, 1122, |
121 1122, 355, 198, 112, 64, 41, 18, -12}; | 120 1122, 355, 198, 112, 64, 41, 18, -12}; |
122 static int F_tbl32[] = | 121 static int F_tbl32[] = |
123 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 }; | 122 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 }; |
124 | 123 |
125 static int quant_tbl40[] = /**< 40kbit/s 5bits per sample */ | 124 static int quant_tbl40[] = /**< 40kbit/s 5bits per sample */ |
126 { -122, -16, 67, 138, 197, 249, 297, 338, | 125 { -122, -16, 67, 138, 197, 249, 297, 338, |
127 377, 412, 444, 474, 501, 527, 552, INT_MAX }; | 126 377, 412, 444, 474, 501, 527, 552, INT_MAX }; |
128 static int iquant_tbl40[] = | 127 static int iquant_tbl40[] = |
129 { INT_MIN, -66, 28, 104, 169, 224, 274, 318, | 128 { INT_MIN, -66, 28, 104, 169, 224, 274, 318, |
130 358, 395, 429, 459, 488, 514, 539, 566, | 129 358, 395, 429, 459, 488, 514, 539, 566, |
149 /** | 148 /** |
150 * Para 4.2.2 page 18: Adaptive quantizer. | 149 * Para 4.2.2 page 18: Adaptive quantizer. |
151 */ | 150 */ |
152 static inline uint8_t quant(G726Context* c, int d) | 151 static inline uint8_t quant(G726Context* c, int d) |
153 { | 152 { |
154 int sign, exp, i, dln; | 153 int sign, exp, i, dln; |
155 | 154 |
156 sign = i = 0; | 155 sign = i = 0; |
157 if (d < 0) { | 156 if (d < 0) { |
158 sign = 1; | 157 sign = 1; |
159 d = -d; | 158 d = -d; |
160 } | 159 } |
161 exp = av_log2_16bit(d); | 160 exp = av_log2_16bit(d); |
162 dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2); | 161 dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2); |
163 | 162 |
164 while (c->tbls->quant[i] < INT_MAX && c->tbls->quant[i] < dln) | 163 while (c->tbls->quant[i] < INT_MAX && c->tbls->quant[i] < dln) |
165 ++i; | 164 ++i; |
166 | 165 |
167 if (sign) | 166 if (sign) |
168 i = ~i; | 167 i = ~i; |
169 if (c->tbls->bits != 2 && i == 0) /* I'm not sure this is a good idea */ | 168 if (c->tbls->bits != 2 && i == 0) /* I'm not sure this is a good idea */ |
170 i = 0xff; | 169 i = 0xff; |
171 | 170 |
172 return i; | 171 return i; |
173 } | 172 } |
174 | 173 |
175 /** | 174 /** |
176 * Para 4.2.3 page 22: Inverse adaptive quantizer. | 175 * Para 4.2.3 page 22: Inverse adaptive quantizer. |
177 */ | 176 */ |
209 dq0 = dq ? sgn(dq) : 0; | 208 dq0 = dq ? sgn(dq) : 0; |
210 if (tr) { | 209 if (tr) { |
211 c->a[0] = 0; | 210 c->a[0] = 0; |
212 c->a[1] = 0; | 211 c->a[1] = 0; |
213 for (i=0; i<6; i++) | 212 for (i=0; i<6; i++) |
214 c->b[i] = 0; | 213 c->b[i] = 0; |
215 } else { | 214 } else { |
216 /* This is a bit crazy, but it really is +255 not +256 */ | 215 /* This is a bit crazy, but it really is +255 not +256 */ |
217 fa1 = clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255); | 216 fa1 = clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255); |
218 | 217 |
219 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7); | 218 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7); |
220 c->a[1] = clip(c->a[1], -12288, 12288); | 219 c->a[1] = clip(c->a[1], -12288, 12288); |
221 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8); | 220 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8); |
222 c->a[0] = clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]); | 221 c->a[0] = clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]); |
223 | 222 |
224 for (i=0; i<6; i++) | 223 for (i=0; i<6; i++) |
225 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8); | 224 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8); |
226 } | 225 } |
227 | 226 |
228 /* Update Dq and Sr and Pk */ | 227 /* Update Dq and Sr and Pk */ |
229 c->pk[1] = c->pk[0]; | 228 c->pk[1] = c->pk[0]; |
230 c->pk[0] = pk0 ? pk0 : 1; | 229 c->pk[0] = pk0 ? pk0 : 1; |
231 c->sr[1] = c->sr[0]; | 230 c->sr[1] = c->sr[0]; |
232 i2f(re_signal, &c->sr[0]); | 231 i2f(re_signal, &c->sr[0]); |
233 for (i=5; i>0; i--) | 232 for (i=5; i>0; i--) |
234 c->dq[i] = c->dq[i-1]; | 233 c->dq[i] = c->dq[i-1]; |
235 i2f(dq, &c->dq[0]); | 234 i2f(dq, &c->dq[0]); |
236 c->dq[0].sign = I >> (c->tbls->bits - 1); /* Isn't it crazy ?!?! */ | 235 c->dq[0].sign = I >> (c->tbls->bits - 1); /* Isn't it crazy ?!?! */ |
237 | 236 |
238 /* Update tone detect [I'm not sure 'tr == 0' is really needed] */ | 237 /* Update tone detect [I'm not sure 'tr == 0' is really needed] */ |
239 c->td = (tr == 0 && c->a[1] < -11776); | 238 c->td = (tr == 0 && c->a[1] < -11776); |
240 | 239 |
241 /* Update Ap */ | 240 /* Update Ap */ |
242 c->dms += ((c->tbls->F[I]<<9) - c->dms) >> 5; | 241 c->dms += ((c->tbls->F[I]<<9) - c->dms) >> 5; |
243 c->dml += ((c->tbls->F[I]<<11) - c->dml) >> 7; | 242 c->dml += ((c->tbls->F[I]<<11) - c->dml) >> 7; |
244 if (tr) | 243 if (tr) |
245 c->ap = 256; | 244 c->ap = 256; |
246 else if (c->y > 1535 && !c->td && (abs((c->dms << 2) - c->dml) < (c->dml >> 3))) | 245 else if (c->y > 1535 && !c->td && (abs((c->dms << 2) - c->dml) < (c->dml >> 3))) |
247 c->ap += (-c->ap) >> 4; | 246 c->ap += (-c->ap) >> 4; |
248 else | 247 else |
249 c->ap += (0x200 - c->ap) >> 4; | 248 c->ap += (0x200 - c->ap) >> 4; |
250 | 249 |
251 /* Update Yu and Yl */ | 250 /* Update Yu and Yl */ |
252 c->yu = clip(c->y + (((c->tbls->W[I] << 5) - c->y) >> 5), 544, 5120); | 251 c->yu = clip(c->y + (((c->tbls->W[I] << 5) - c->y) >> 5), 544, 5120); |
253 c->yl += c->yu + ((-c->yl)>>6); | 252 c->yl += c->yu + ((-c->yl)>>6); |
254 | 253 |
257 c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6; | 256 c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6; |
258 | 257 |
259 /* Next iteration for SE and SEZ */ | 258 /* Next iteration for SE and SEZ */ |
260 c->se = 0; | 259 c->se = 0; |
261 for (i=0; i<6; i++) | 260 for (i=0; i<6; i++) |
262 c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]); | 261 c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]); |
263 c->sez = c->se >> 1; | 262 c->sez = c->se >> 1; |
264 for (i=0; i<2; i++) | 263 for (i=0; i<2; i++) |
265 c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]); | 264 c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]); |
266 c->se >>= 1; | 265 c->se >>= 1; |
267 | 266 |
268 return clip(re_signal << 2, -0xffff, 0xffff); | 267 return clip(re_signal << 2, -0xffff, 0xffff); |
269 } | 268 } |
270 | 269 |
272 { | 271 { |
273 int i; | 272 int i; |
274 | 273 |
275 c->tbls = &G726Tables_pool[bit_rate/8000 - 2]; | 274 c->tbls = &G726Tables_pool[bit_rate/8000 - 2]; |
276 for (i=0; i<2; i++) { | 275 for (i=0; i<2; i++) { |
277 i2f(0, &c->sr[i]); | 276 i2f(0, &c->sr[i]); |
278 c->a[i] = 0; | 277 c->a[i] = 0; |
279 c->pk[i] = 1; | 278 c->pk[i] = 1; |
280 } | 279 } |
281 for (i=0; i<6; i++) { | 280 for (i=0; i<6; i++) { |
282 i2f(0, &c->dq[i]); | 281 i2f(0, &c->dq[i]); |
283 c->b[i] = 0; | 282 c->b[i] = 0; |
284 } | 283 } |
285 c->ap = 0; | 284 c->ap = 0; |
286 c->dms = 0; | 285 c->dms = 0; |
287 c->dml = 0; | 286 c->dml = 0; |
288 c->yu = 544; | 287 c->yu = 544; |
302 } | 301 } |
303 | 302 |
304 #ifdef CONFIG_ENCODERS | 303 #ifdef CONFIG_ENCODERS |
305 static int16_t g726_encode(G726Context* c, int16_t sig) | 304 static int16_t g726_encode(G726Context* c, int16_t sig) |
306 { | 305 { |
307 uint8_t i; | 306 uint8_t i; |
308 | 307 |
309 i = quant(c, sig/4 - c->se) & ((1<<c->tbls->bits) - 1); | 308 i = quant(c, sig/4 - c->se) & ((1<<c->tbls->bits) - 1); |
310 g726_iterate(c, i); | 309 g726_iterate(c, i); |
311 return i; | 310 return i; |
312 } | 311 } |
313 #endif | 312 #endif |
314 | 313 |
315 /* Interfacing to the libavcodec */ | 314 /* Interfacing to the libavcodec */ |
316 | 315 |
317 typedef struct AVG726Context { | 316 typedef struct AVG726Context { |
318 G726Context c; | 317 G726Context c; |
319 int bits_left; | 318 int bits_left; |
320 int bit_buffer; | 319 int bit_buffer; |
321 int code_size; | 320 int code_size; |
322 } AVG726Context; | 321 } AVG726Context; |
323 | 322 |
324 static int g726_init(AVCodecContext * avctx) | 323 static int g726_init(AVCodecContext * avctx) |
325 { | 324 { |
326 AVG726Context* c = (AVG726Context*)avctx->priv_data; | 325 AVG726Context* c = (AVG726Context*)avctx->priv_data; |
363 PutBitContext pb; | 362 PutBitContext pb; |
364 | 363 |
365 init_put_bits(&pb, dst, 1024*1024); | 364 init_put_bits(&pb, dst, 1024*1024); |
366 | 365 |
367 for (; buf_size; buf_size--) | 366 for (; buf_size; buf_size--) |
368 put_bits(&pb, c->code_size, g726_encode(&c->c, *samples++)); | 367 put_bits(&pb, c->code_size, g726_encode(&c->c, *samples++)); |
369 | 368 |
370 flush_put_bits(&pb); | 369 flush_put_bits(&pb); |
371 | 370 |
372 return put_bits_count(&pb)>>3; | 371 return put_bits_count(&pb)>>3; |
373 } | 372 } |