1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 
18 #include "third_party/googletest/src/include/gtest/gtest.h"
19 #include "test/acm_random.h"
20 #include "vp8/encoder/onyx_int.h"
21 #include "vpx/vpx_integer.h"
22 #include "vpx_mem/vpx_mem.h"
23 
24 using libvpx_test::ACMRandom;
25 
26 namespace {
27 
TEST(VP8RoiMapTest,ParameterCheck)28 TEST(VP8RoiMapTest, ParameterCheck) {
29   ACMRandom rnd(ACMRandom::DeterministicSeed());
30   int delta_q[MAX_MB_SEGMENTS] = { -2, -25, 0, 31 };
31   int delta_lf[MAX_MB_SEGMENTS] = { -2, -25, 0, 31 };
32   unsigned int threshold[MAX_MB_SEGMENTS] = { 0, 100, 200, 300 };
33 
34   const int internalq_trans[] = {
35     0,  1,  2,  3,  4,  5,  7,   8,   9,   10,  12,  13,  15,  17,  18,  19,
36     20, 21, 23, 24, 25, 26, 27,  28,  29,  30,  31,  33,  35,  37,  39,  41,
37     43, 45, 47, 49, 51, 53, 55,  57,  59,  61,  64,  67,  70,  73,  76,  79,
38     82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124, 127,
39   };
40 
41   // Initialize elements of cpi with valid defaults.
42   VP8_COMP cpi;
43   cpi.mb.e_mbd.mb_segement_abs_delta = SEGMENT_DELTADATA;
44   cpi.cyclic_refresh_mode_enabled = 0;
45   cpi.mb.e_mbd.segmentation_enabled = 0;
46   cpi.mb.e_mbd.update_mb_segmentation_map = 0;
47   cpi.mb.e_mbd.update_mb_segmentation_data = 0;
48   cpi.common.mb_rows = 240 >> 4;
49   cpi.common.mb_cols = 320 >> 4;
50   const int mbs = (cpi.common.mb_rows * cpi.common.mb_cols);
51   memset(cpi.segment_feature_data, 0, sizeof(cpi.segment_feature_data));
52 
53   // Segment map
54   cpi.segmentation_map = reinterpret_cast<unsigned char *>(vpx_calloc(mbs, 1));
55 
56   // Allocate memory for the source memory map.
57   unsigned char *roi_map =
58       reinterpret_cast<unsigned char *>(vpx_calloc(mbs, 1));
59   memset(&roi_map[mbs >> 2], 1, (mbs >> 2));
60   memset(&roi_map[mbs >> 1], 2, (mbs >> 2));
61   memset(&roi_map[mbs - (mbs >> 2)], 3, (mbs >> 2));
62 
63   // Do a test call with valid parameters.
64   int roi_retval =
65       vp8_set_roimap(&cpi, roi_map, cpi.common.mb_rows, cpi.common.mb_cols,
66                      delta_q, delta_lf, threshold);
67   EXPECT_EQ(0, roi_retval)
68       << "vp8_set_roimap roi failed with default test parameters";
69 
70   // Check that the values in the cpi structure get set as expected.
71   if (roi_retval == 0) {
72     // Check that the segment map got set.
73     const int mapcompare = memcmp(roi_map, cpi.segmentation_map, mbs);
74     EXPECT_EQ(0, mapcompare) << "segment map error";
75 
76     // Check the q deltas (note the need to translate into
77     // the interanl range of 0-127.
78     for (int i = 0; i < MAX_MB_SEGMENTS; ++i) {
79       const int transq = internalq_trans[abs(delta_q[i])];
80       if (abs(cpi.segment_feature_data[MB_LVL_ALT_Q][i]) != transq) {
81         EXPECT_EQ(transq, cpi.segment_feature_data[MB_LVL_ALT_Q][i])
82             << "segment delta_q  error";
83         break;
84       }
85     }
86 
87     // Check the loop filter deltas
88     for (int i = 0; i < MAX_MB_SEGMENTS; ++i) {
89       if (cpi.segment_feature_data[MB_LVL_ALT_LF][i] != delta_lf[i]) {
90         EXPECT_EQ(delta_lf[i], cpi.segment_feature_data[MB_LVL_ALT_LF][i])
91             << "segment delta_lf error";
92         break;
93       }
94     }
95 
96     // Check the breakout thresholds
97     for (int i = 0; i < MAX_MB_SEGMENTS; ++i) {
98       unsigned int breakout =
99           static_cast<unsigned int>(cpi.segment_encode_breakout[i]);
100 
101       if (threshold[i] != breakout) {
102         EXPECT_EQ(threshold[i], breakout) << "breakout threshold error";
103         break;
104       }
105     }
106 
107     // Segmentation, and segmentation update flages should be set.
108     EXPECT_EQ(1, cpi.mb.e_mbd.segmentation_enabled)
109         << "segmentation_enabled error";
110     EXPECT_EQ(1, cpi.mb.e_mbd.update_mb_segmentation_map)
111         << "update_mb_segmentation_map error";
112     EXPECT_EQ(1, cpi.mb.e_mbd.update_mb_segmentation_data)
113         << "update_mb_segmentation_data error";
114 
115     // Try a range of delta q and lf parameters (some legal, some not)
116     for (int i = 0; i < 1000; ++i) {
117       int rand_deltas[4];
118       int deltas_valid;
119       rand_deltas[0] = rnd(160) - 80;
120       rand_deltas[1] = rnd(160) - 80;
121       rand_deltas[2] = rnd(160) - 80;
122       rand_deltas[3] = rnd(160) - 80;
123 
124       deltas_valid =
125           ((abs(rand_deltas[0]) <= 63) && (abs(rand_deltas[1]) <= 63) &&
126            (abs(rand_deltas[2]) <= 63) && (abs(rand_deltas[3]) <= 63))
127               ? 0
128               : -1;
129 
130       // Test with random delta q values.
131       roi_retval =
132           vp8_set_roimap(&cpi, roi_map, cpi.common.mb_rows, cpi.common.mb_cols,
133                          rand_deltas, delta_lf, threshold);
134       EXPECT_EQ(deltas_valid, roi_retval) << "dq range check error";
135 
136       // One delta_q error shown at a time
137       if (deltas_valid != roi_retval) break;
138 
139       // Test with random loop filter values.
140       roi_retval =
141           vp8_set_roimap(&cpi, roi_map, cpi.common.mb_rows, cpi.common.mb_cols,
142                          delta_q, rand_deltas, threshold);
143       EXPECT_EQ(deltas_valid, roi_retval) << "dlf range check error";
144 
145       // One delta loop filter error shown at a time
146       if (deltas_valid != roi_retval) break;
147     }
148 
149     // Test invalid number of rows or colums.
150     roi_retval =
151         vp8_set_roimap(&cpi, roi_map, cpi.common.mb_rows + 1,
152                        cpi.common.mb_cols, delta_q, delta_lf, threshold);
153     EXPECT_EQ(-1, roi_retval) << "MB rows bounds check error";
154 
155     roi_retval =
156         vp8_set_roimap(&cpi, roi_map, cpi.common.mb_rows,
157                        cpi.common.mb_cols - 1, delta_q, delta_lf, threshold);
158     EXPECT_EQ(-1, roi_retval) << "MB cols bounds check error";
159   }
160 
161   // Free allocated memory
162   if (cpi.segmentation_map) vpx_free(cpi.segmentation_map);
163   if (roi_map) vpx_free(roi_map);
164 };
165 
166 }  // namespace
167