/src/libwebp/src/utils/random_utils.h
Line | Count | Source |
1 | | // Copyright 2013 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // Pseudo-random utilities |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #ifndef WEBP_UTILS_RANDOM_UTILS_H_ |
15 | | #define WEBP_UTILS_RANDOM_UTILS_H_ |
16 | | |
17 | | #include <assert.h> |
18 | | |
19 | | #include "src/utils/bounds_safety.h" |
20 | | #include "src/webp/types.h" |
21 | | |
22 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
23 | | |
24 | | #ifdef __cplusplus |
25 | | extern "C" { |
26 | | #endif |
27 | | |
28 | 52.2M | #define VP8_RANDOM_DITHER_FIX 8 // fixed-point precision for dithering |
29 | 104M | #define VP8_RANDOM_TABLE_SIZE 55 |
30 | | |
31 | | typedef struct { |
32 | | int index1, index2; |
33 | | uint32_t tab[VP8_RANDOM_TABLE_SIZE]; |
34 | | int amp; |
35 | | } VP8Random; |
36 | | |
37 | | // Initializes random generator with an amplitude 'dithering' in range [0..1]. |
38 | | void VP8InitRandom(VP8Random* const rg, float dithering); |
39 | | |
40 | | // Returns a centered pseudo-random number with 'num_bits' amplitude. |
41 | | // (uses D.Knuth's Difference-based random generator). |
42 | | // 'amp' is in VP8_RANDOM_DITHER_FIX fixed-point precision. |
43 | | static WEBP_INLINE int VP8RandomBits2(VP8Random* const rg, int num_bits, |
44 | 52.2M | int amp) { |
45 | 52.2M | int diff; |
46 | 52.2M | assert(num_bits + VP8_RANDOM_DITHER_FIX <= 31); |
47 | 52.2M | diff = rg->tab[rg->index1] - rg->tab[rg->index2]; |
48 | 52.2M | if (diff < 0) diff += (1u << 31); |
49 | 52.2M | rg->tab[rg->index1] = diff; |
50 | 52.2M | if (++rg->index1 == VP8_RANDOM_TABLE_SIZE) rg->index1 = 0; |
51 | 52.2M | if (++rg->index2 == VP8_RANDOM_TABLE_SIZE) rg->index2 = 0; |
52 | | // sign-extend, 0-center |
53 | 52.2M | diff = (int)((uint32_t)diff << 1) >> (32 - num_bits); |
54 | 52.2M | diff = (diff * amp) >> VP8_RANDOM_DITHER_FIX; // restrict range |
55 | 52.2M | diff += 1 << (num_bits - 1); // shift back to 0.5-center |
56 | 52.2M | return diff; |
57 | 52.2M | } Unexecuted instantiation: muxedit.c:VP8RandomBits2 Unexecuted instantiation: muxinternal.c:VP8RandomBits2 Unexecuted instantiation: muxread.c:VP8RandomBits2 Unexecuted instantiation: vp8_dec.c:VP8RandomBits2 Unexecuted instantiation: webp_dec.c:VP8RandomBits2 Unexecuted instantiation: dec.c:VP8RandomBits2 Unexecuted instantiation: dec_sse41.c:VP8RandomBits2 Unexecuted instantiation: dec_sse2.c:VP8RandomBits2 Unexecuted instantiation: alpha_dec.c:VP8RandomBits2 Unexecuted instantiation: buffer_dec.c:VP8RandomBits2 frame_dec.c:VP8RandomBits2 Line | Count | Source | 44 | 4.69M | int amp) { | 45 | 4.69M | int diff; | 46 | 4.69M | assert(num_bits + VP8_RANDOM_DITHER_FIX <= 31); | 47 | 4.69M | diff = rg->tab[rg->index1] - rg->tab[rg->index2]; | 48 | 4.69M | if (diff < 0) diff += (1u << 31); | 49 | 4.69M | rg->tab[rg->index1] = diff; | 50 | 4.69M | if (++rg->index1 == VP8_RANDOM_TABLE_SIZE) rg->index1 = 0; | 51 | 4.69M | if (++rg->index2 == VP8_RANDOM_TABLE_SIZE) rg->index2 = 0; | 52 | | // sign-extend, 0-center | 53 | 4.69M | diff = (int)((uint32_t)diff << 1) >> (32 - num_bits); | 54 | 4.69M | diff = (diff * amp) >> VP8_RANDOM_DITHER_FIX; // restrict range | 55 | 4.69M | diff += 1 << (num_bits - 1); // shift back to 0.5-center | 56 | 4.69M | return diff; | 57 | 4.69M | } |
Unexecuted instantiation: io_dec.c:VP8RandomBits2 Unexecuted instantiation: quant_dec.c:VP8RandomBits2 Unexecuted instantiation: tree_dec.c:VP8RandomBits2 Unexecuted instantiation: random_utils.c:VP8RandomBits2 picture_csp_enc.c:VP8RandomBits2 Line | Count | Source | 44 | 47.5M | int amp) { | 45 | 47.5M | int diff; | 46 | 47.5M | assert(num_bits + VP8_RANDOM_DITHER_FIX <= 31); | 47 | 47.5M | diff = rg->tab[rg->index1] - rg->tab[rg->index2]; | 48 | 47.5M | if (diff < 0) diff += (1u << 31); | 49 | 47.5M | rg->tab[rg->index1] = diff; | 50 | 47.5M | if (++rg->index1 == VP8_RANDOM_TABLE_SIZE) rg->index1 = 0; | 51 | 47.5M | if (++rg->index2 == VP8_RANDOM_TABLE_SIZE) rg->index2 = 0; | 52 | | // sign-extend, 0-center | 53 | 47.5M | diff = (int)((uint32_t)diff << 1) >> (32 - num_bits); | 54 | 47.5M | diff = (diff * amp) >> VP8_RANDOM_DITHER_FIX; // restrict range | 55 | 47.5M | diff += 1 << (num_bits - 1); // shift back to 0.5-center | 56 | 47.5M | return diff; | 57 | 47.5M | } |
Unexecuted instantiation: idec_dec.c:VP8RandomBits2 |
58 | | |
59 | 47.5M | static WEBP_INLINE int VP8RandomBits(VP8Random* const rg, int num_bits) { |
60 | 47.5M | return VP8RandomBits2(rg, num_bits, rg->amp); |
61 | 47.5M | } Unexecuted instantiation: muxedit.c:VP8RandomBits Unexecuted instantiation: muxinternal.c:VP8RandomBits Unexecuted instantiation: muxread.c:VP8RandomBits Unexecuted instantiation: vp8_dec.c:VP8RandomBits Unexecuted instantiation: webp_dec.c:VP8RandomBits Unexecuted instantiation: dec.c:VP8RandomBits Unexecuted instantiation: dec_sse41.c:VP8RandomBits Unexecuted instantiation: dec_sse2.c:VP8RandomBits Unexecuted instantiation: alpha_dec.c:VP8RandomBits Unexecuted instantiation: buffer_dec.c:VP8RandomBits Unexecuted instantiation: frame_dec.c:VP8RandomBits Unexecuted instantiation: io_dec.c:VP8RandomBits Unexecuted instantiation: quant_dec.c:VP8RandomBits Unexecuted instantiation: tree_dec.c:VP8RandomBits Unexecuted instantiation: random_utils.c:VP8RandomBits picture_csp_enc.c:VP8RandomBits Line | Count | Source | 59 | 47.5M | static WEBP_INLINE int VP8RandomBits(VP8Random* const rg, int num_bits) { | 60 | 47.5M | return VP8RandomBits2(rg, num_bits, rg->amp); | 61 | 47.5M | } |
Unexecuted instantiation: idec_dec.c:VP8RandomBits |
62 | | |
63 | | #ifdef __cplusplus |
64 | | } // extern "C" |
65 | | #endif |
66 | | |
67 | | #endif // WEBP_UTILS_RANDOM_UTILS_H_ |