set_roi.cc 5.9 KB

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