vp8_boolcoder_test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/decoder/dboolhuff.h"
  19. #include "vp8/encoder/boolhuff.h"
  20. #include "vpx/vpx_integer.h"
  21. namespace {
  22. const int num_tests = 10;
  23. // In a real use the 'decrypt_state' parameter will be a pointer to a struct
  24. // with whatever internal state the decryptor uses. For testing we'll just
  25. // xor with a constant key, and decrypt_state will point to the start of
  26. // the original buffer.
  27. const uint8_t secret_key[16] = {
  28. 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
  29. 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0
  30. };
  31. void encrypt_buffer(uint8_t *buffer, size_t size) {
  32. for (size_t i = 0; i < size; ++i) {
  33. buffer[i] ^= secret_key[i & 15];
  34. }
  35. }
  36. void test_decrypt_cb(void *decrypt_state, const uint8_t *input, uint8_t *output,
  37. int count) {
  38. const size_t offset = input - reinterpret_cast<uint8_t *>(decrypt_state);
  39. for (int i = 0; i < count; i++) {
  40. output[i] = input[i] ^ secret_key[(offset + i) & 15];
  41. }
  42. }
  43. } // namespace
  44. using libvpx_test::ACMRandom;
  45. TEST(VP8, TestBitIO) {
  46. ACMRandom rnd(ACMRandom::DeterministicSeed());
  47. for (int n = 0; n < num_tests; ++n) {
  48. for (int method = 0; method <= 7; ++method) { // we generate various proba
  49. const int kBitsToTest = 1000;
  50. uint8_t probas[kBitsToTest];
  51. for (int i = 0; i < kBitsToTest; ++i) {
  52. const int parity = i & 1;
  53. /* clang-format off */
  54. probas[i] =
  55. (method == 0) ? 0 : (method == 1) ? 255 :
  56. (method == 2) ? 128 :
  57. (method == 3) ? rnd.Rand8() :
  58. (method == 4) ? (parity ? 0 : 255) :
  59. // alternate between low and high proba:
  60. (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
  61. (method == 6) ?
  62. (parity ? rnd(64) : 255 - rnd(64)) :
  63. (parity ? rnd(32) : 255 - rnd(32));
  64. /* clang-format on */
  65. }
  66. for (int bit_method = 0; bit_method <= 3; ++bit_method) {
  67. const int random_seed = 6432;
  68. const int kBufferSize = 10000;
  69. ACMRandom bit_rnd(random_seed);
  70. BOOL_CODER bw;
  71. uint8_t bw_buffer[kBufferSize];
  72. vp8_start_encode(&bw, bw_buffer, bw_buffer + kBufferSize);
  73. int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
  74. for (int i = 0; i < kBitsToTest; ++i) {
  75. if (bit_method == 2) {
  76. bit = (i & 1);
  77. } else if (bit_method == 3) {
  78. bit = bit_rnd(2);
  79. }
  80. vp8_encode_bool(&bw, bit, static_cast<int>(probas[i]));
  81. }
  82. vp8_stop_encode(&bw);
  83. BOOL_DECODER br;
  84. encrypt_buffer(bw_buffer, kBufferSize);
  85. vp8dx_start_decode(&br, bw_buffer, kBufferSize, test_decrypt_cb,
  86. reinterpret_cast<void *>(bw_buffer));
  87. bit_rnd.Reset(random_seed);
  88. for (int i = 0; i < kBitsToTest; ++i) {
  89. if (bit_method == 2) {
  90. bit = (i & 1);
  91. } else if (bit_method == 3) {
  92. bit = bit_rnd(2);
  93. }
  94. GTEST_ASSERT_EQ(vp8dx_decode_bool(&br, probas[i]), bit)
  95. << "pos: " << i << " / " << kBitsToTest
  96. << " bit_method: " << bit_method << " method: " << method;
  97. }
  98. }
  99. }
  100. }
  101. }