• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
13 
14 #include <cstdlib>
15 
16 #include "aom_dsp/entenc.h"
17 #include "aom_dsp/entdec.h"
18 
TEST(EC_TEST,random_ec_test)19 TEST(EC_TEST, random_ec_test) {
20   od_ec_enc enc;
21   od_ec_dec dec;
22   int sz;
23   int i;
24   int ret;
25   unsigned int seed;
26   unsigned char *ptr;
27   uint32_t ptr_sz;
28   char *seed_str;
29   ret = 0;
30   seed_str = getenv("EC_TEST_SEED");
31   if (seed_str) {
32     seed = atoi(seed_str);
33   } else {
34     seed = 0xdaa1a;
35   }
36   srand(seed);
37   od_ec_enc_init(&enc, 1);
38   /*Test compatibility between multiple different encode/decode routines.*/
39   for (i = 0; i < 409600; i++) {
40     unsigned *fz;
41     unsigned *fts;
42     unsigned *data;
43     unsigned *tell;
44     unsigned *enc_method;
45     int j;
46     sz = rand() / ((RAND_MAX >> (rand() % 9U)) + 1U);
47     fz = (unsigned *)malloc(sz * sizeof(*fz));
48     fts = (unsigned *)malloc(sz * sizeof(*fts));
49     data = (unsigned *)malloc(sz * sizeof(*data));
50     tell = (unsigned *)malloc((sz + 1) * sizeof(*tell));
51     enc_method = (unsigned *)malloc(sz * sizeof(*enc_method));
52     od_ec_enc_reset(&enc);
53     tell[0] = od_ec_enc_tell_frac(&enc);
54     for (j = 0; j < sz; j++) {
55       data[j] = rand() / ((RAND_MAX >> 1) + 1);
56 
57       fts[j] = CDF_PROB_BITS;
58       fz[j] = (rand() % (CDF_PROB_TOP - 2)) >> (CDF_PROB_BITS - fts[j]);
59       fz[j] = OD_MAXI(fz[j], 1);
60       enc_method[j] = 3 + (rand() & 1);
61       switch (enc_method[j]) {
62         case 3: {
63           od_ec_encode_bool_q15(&enc, data[j],
64                                 OD_ICDF(fz[j] << (CDF_PROB_BITS - fts[j])));
65           break;
66         }
67         case 4: {
68           uint16_t cdf[2];
69           cdf[0] = OD_ICDF(fz[j]);
70           cdf[1] = OD_ICDF(1U << fts[j]);
71           od_ec_encode_cdf_q15(&enc, data[j], cdf, 2);
72           break;
73         }
74       }
75 
76       tell[j + 1] = od_ec_enc_tell_frac(&enc);
77     }
78     ptr = od_ec_enc_done(&enc, &ptr_sz);
79     EXPECT_GE(((od_ec_enc_tell(&enc) + 7U) >> 3), ptr_sz)
80         << "od_ec_enc_tell() lied: "
81            "there's "
82         << ptr_sz << " bytes instead of " << ((od_ec_enc_tell(&enc) + 7) >> 3)
83         << " (Random seed: " << seed << ")\n";
84     od_ec_dec_init(&dec, ptr, ptr_sz);
85     EXPECT_EQ(od_ec_dec_tell_frac(&dec), tell[0])
86         << "od_ec_dec_tell() mismatch between encoder and decoder "
87            "at symbol 0: "
88         << (unsigned)od_ec_dec_tell_frac(&dec) << " instead of " << tell[0]
89         << " (Random seed: " << seed << ").\n";
90     for (j = 0; j < sz; j++) {
91       int dec_method;
92       unsigned int sym = data[j] + 1;  // Initialize sym to an invalid value.
93 
94       if (CDF_SHIFT == 0) {
95         dec_method = 3 + (rand() & 1);
96       } else {
97         dec_method = enc_method[j];
98       }
99       switch (dec_method) {
100         case 3: {
101           sym = od_ec_decode_bool_q15(
102               &dec, OD_ICDF(fz[j] << (CDF_PROB_BITS - fts[j])));
103           break;
104         }
105         case 4: {
106           uint16_t cdf[2];
107           cdf[0] = OD_ICDF(fz[j]);
108           cdf[1] = OD_ICDF(1U << fts[j]);
109           sym = od_ec_decode_cdf_q15(&dec, cdf, 2);
110           break;
111         }
112       }
113 
114       EXPECT_EQ(sym, data[j])
115           << "Decoded " << sym << " instead of " << data[j]
116           << " with fz=" << fz[j] << " and ftb=" << fts[j] << "at position "
117           << j << " of " << sz << " (Random seed: " << seed << ").\n"
118           << "Encoding method: " << enc_method[j]
119           << " decoding method: " << dec_method << "\n";
120       EXPECT_EQ(od_ec_dec_tell_frac(&dec), tell[j + 1])
121           << "od_ec_dec_tell() mismatch between encoder and "
122              "decoder at symbol "
123           << j + 1 << ": " << (unsigned)od_ec_dec_tell_frac(&dec)
124           << " instead of " << tell[j + 1] << " (Random seed: " << seed
125           << ").\n";
126     }
127     free(enc_method);
128     free(tell);
129     free(data);
130     free(fts);
131     free(fz);
132   }
133   od_ec_enc_reset(&enc);
134   if (CDF_SHIFT == 0) {
135     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
136     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
137     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
138     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
139     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(24576));
140     od_ec_enc_patch_initial_bits(&enc, 3, 2);
141     EXPECT_FALSE(enc.error) << "od_ec_enc_patch_initial_bits() failed.\n";
142     od_ec_enc_patch_initial_bits(&enc, 0, 5);
143     EXPECT_TRUE(enc.error)
144         << "od_ec_enc_patch_initial_bits() didn't fail when it should have.\n";
145     od_ec_enc_reset(&enc);
146     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
147     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(16384));
148     od_ec_encode_bool_q15(&enc, 1, OD_ICDF(32256));
149     od_ec_encode_bool_q15(&enc, 0, OD_ICDF(24576));
150     od_ec_enc_patch_initial_bits(&enc, 0, 2);
151     EXPECT_FALSE(enc.error) << "od_ec_enc_patch_initial_bits() failed.\n";
152     ptr = od_ec_enc_done(&enc, &ptr_sz);
153     EXPECT_EQ(ptr_sz, 2u);
154     EXPECT_EQ(ptr[0], 63)
155         << "Got " << ptr[0]
156         << " when expecting 63 for od_ec_enc_patch_initial_bits().\n";
157   }
158   od_ec_enc_clear(&enc);
159   EXPECT_EQ(ret, 0);
160 }
161