Mercurial > libavcodec.hg
comparison ac3dec.c @ 5330:37de2d864f03 libavcodec
use random number generator from libavutil
author | jbr |
---|---|
date | Sat, 14 Jul 2007 23:57:05 +0000 |
parents | 06e7dda51f65 |
children | b24bcdd0ae86 |
comparison
equal
deleted
inserted
replaced
5329:06e7dda51f65 | 5330:37de2d864f03 |
---|---|
5 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com). | 5 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com). |
6 * | 6 * |
7 * For exponent decoding the code is inspired by the code in liba52 by | 7 * For exponent decoding the code is inspired by the code in liba52 by |
8 * Michel Lespinasse and Aaron Holtzman. | 8 * Michel Lespinasse and Aaron Holtzman. |
9 * http://liba52.sourceforge.net | 9 * http://liba52.sourceforge.net |
10 * | |
11 * The Mersenne Twister is based on code written by Makoto Matsumoto and | |
12 * Takuji Nishimura. | |
13 * | 10 * |
14 * This file is part of FFmpeg. | 11 * This file is part of FFmpeg. |
15 * | 12 * |
16 * FFmpeg is free software; you can redistribute it and/or | 13 * FFmpeg is free software; you can redistribute it and/or |
17 * modify it under the terms of the GNU General Public | 14 * modify it under the terms of the GNU General Public |
37 | 34 |
38 #include "avcodec.h" | 35 #include "avcodec.h" |
39 #include "ac3tab.h" | 36 #include "ac3tab.h" |
40 #include "bitstream.h" | 37 #include "bitstream.h" |
41 #include "dsputil.h" | 38 #include "dsputil.h" |
39 #include "random.h" | |
42 | 40 |
43 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; | 41 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; |
44 | 42 |
45 /* table for exponent to scale_factor mapping | 43 /* table for exponent to scale_factor mapping |
46 * scale_factor[i] = 2 ^ -(i + 15) | 44 * scale_factor[i] = 2 ^ -(i + 15) |
114 #define AC3_INPUT_3F 0x03 | 112 #define AC3_INPUT_3F 0x03 |
115 #define AC3_INPUT_2F_1R 0x04 | 113 #define AC3_INPUT_2F_1R 0x04 |
116 #define AC3_INPUT_3F_1R 0x05 | 114 #define AC3_INPUT_3F_1R 0x05 |
117 #define AC3_INPUT_2F_2R 0x06 | 115 #define AC3_INPUT_2F_2R 0x06 |
118 #define AC3_INPUT_3F_2R 0x07 | 116 #define AC3_INPUT_3F_2R 0x07 |
119 | |
120 /* Mersenne Twister */ | |
121 #define NMT 624 | |
122 #define MMT 397 | |
123 #define MATRIX_A 0x9908b0df | |
124 #define UPPER_MASK 0x80000000 | |
125 #define LOWER_MASK 0x7fffffff | |
126 | |
127 | |
128 typedef struct { | |
129 uint32_t mt[NMT]; | |
130 int mti; | |
131 } dither_state; | |
132 /* Mersenne Twister */ | |
133 | 117 |
134 typedef struct { | 118 typedef struct { |
135 uint16_t crc1; | 119 uint16_t crc1; |
136 uint8_t fscod; | 120 uint8_t fscod; |
137 | 121 |
219 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing | 203 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing |
220 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients | 204 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients |
221 | 205 |
222 /* Miscellaneous. */ | 206 /* Miscellaneous. */ |
223 GetBitContext gb; | 207 GetBitContext gb; |
224 dither_state dith_state; //for dither generation | 208 AVRandomState dith_state; //for dither generation |
225 } AC3DecodeContext; | 209 } AC3DecodeContext; |
226 | |
227 | |
228 /* BEGIN Mersenne Twister Code. */ | |
229 static void dither_seed(dither_state *state, uint32_t seed) | |
230 { | |
231 static const uint32_t mag01[2] = { 0x00, MATRIX_A }; | |
232 uint32_t y; | |
233 int kk; | |
234 | |
235 if (seed == 0) | |
236 seed = 0x7ba05e; //default seed to my birthday! | |
237 | |
238 state->mt[0] = seed; | |
239 for (state->mti = 1; state->mti < NMT; state->mti++) | |
240 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1); | |
241 | |
242 for (kk = 0; kk < NMT - MMT; kk++) { | |
243 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
244 state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01]; | |
245 } | |
246 for (;kk < NMT - 1; kk++) { | |
247 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
248 state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01]; | |
249 } | |
250 y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK); | |
251 state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01]; | |
252 | |
253 state->mti = 0; | |
254 } | |
255 | |
256 static int16_t dither_int16(dither_state *state) | |
257 { | |
258 uint32_t y; | |
259 | |
260 if (state->mti >= NMT) | |
261 state->mti = 0; | |
262 | |
263 y = state->mt[state->mti++]; | |
264 y ^= (y >> 11); | |
265 y ^= ((y << 7) & 0x9d2c5680); | |
266 y ^= ((y << 15) & 0xefc60000); | |
267 y ^= (y >> 18); | |
268 | |
269 return ((y << 16) >> 16); | |
270 } | |
271 /* END Mersenne Twister */ | |
272 | 210 |
273 /*********** BEGIN INIT HELPER FUNCTIONS ***********/ | 211 /*********** BEGIN INIT HELPER FUNCTIONS ***********/ |
274 /** | 212 /** |
275 * Generate a Kaiser-Bessel Derived Window. | 213 * Generate a Kaiser-Bessel Derived Window. |
276 */ | 214 */ |
429 ac3_tables_init(); | 367 ac3_tables_init(); |
430 ff_mdct_init(&ctx->imdct_256, 8, 1); | 368 ff_mdct_init(&ctx->imdct_256, 8, 1); |
431 ff_mdct_init(&ctx->imdct_512, 9, 1); | 369 ff_mdct_init(&ctx->imdct_512, 9, 1); |
432 ac3_window_init(ctx->window); | 370 ac3_window_init(ctx->window); |
433 dsputil_init(&ctx->dsp, avctx); | 371 dsputil_init(&ctx->dsp, avctx); |
434 dither_seed(&ctx->dith_state, 0); | 372 av_init_random(0, &ctx->dith_state); |
435 | 373 |
436 return 0; | 374 return 0; |
437 } | 375 } |
438 /*********** END INIT FUNCTIONS ***********/ | 376 /*********** END INIT FUNCTIONS ***********/ |
439 | 377 |
896 switch(tbap) { | 834 switch(tbap) { |
897 case 0: | 835 case 0: |
898 for (ch = 0; ch < ctx->nfchans; ch++) | 836 for (ch = 0; ch < ctx->nfchans; ch++) |
899 if (((ctx->chincpl) >> ch) & 1) { | 837 if (((ctx->chincpl) >> ch) & 1) { |
900 if ((ctx->dithflag >> ch) & 1) { | 838 if ((ctx->dithflag >> ch) & 1) { |
901 TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors); | 839 TRANSFORM_COEFF(cplcoeff, av_random(&ctx->dith_state) & 0xFFFF, exps[start], scale_factors); |
902 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB; | 840 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB; |
903 } else | 841 } else |
904 ctx->transform_coeffs[ch + 1][start] = 0; | 842 ctx->transform_coeffs[ch + 1][start] = 0; |
905 } | 843 } |
906 start++; | 844 start++; |
994 if (!dithflag) { | 932 if (!dithflag) { |
995 coeffs[i] = 0; | 933 coeffs[i] = 0; |
996 continue; | 934 continue; |
997 } | 935 } |
998 else { | 936 else { |
999 TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors); | 937 TRANSFORM_COEFF(coeffs[i], av_random(&ctx->dith_state) & 0xFFFF, exps[i], factors); |
1000 coeffs[i] *= LEVEL_MINUS_3DB; | 938 coeffs[i] *= LEVEL_MINUS_3DB; |
1001 continue; | 939 continue; |
1002 } | 940 } |
1003 | 941 |
1004 case 1: | 942 case 1: |