vp9_boolcoder_test.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <stdlib.h>
  12. #include <string.h>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "test/acm_random.h"
  15. #include "vpx/vpx_integer.h"
  16. #include "vpx_dsp/bitreader.h"
  17. #include "vpx_dsp/bitwriter.h"
  18. using libvpx_test::ACMRandom;
  19. namespace {
  20. const int num_tests = 10;
  21. } // namespace
  22. TEST(VP9, TestBitIO) {
  23. ACMRandom rnd(ACMRandom::DeterministicSeed());
  24. for (int n = 0; n < num_tests; ++n) {
  25. for (int method = 0; method <= 7; ++method) { // we generate various proba
  26. const int kBitsToTest = 1000;
  27. uint8_t probas[kBitsToTest];
  28. for (int i = 0; i < kBitsToTest; ++i) {
  29. const int parity = i & 1;
  30. /* clang-format off */
  31. probas[i] =
  32. (method == 0) ? 0 : (method == 1) ? 255 :
  33. (method == 2) ? 128 :
  34. (method == 3) ? rnd.Rand8() :
  35. (method == 4) ? (parity ? 0 : 255) :
  36. // alternate between low and high proba:
  37. (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
  38. (method == 6) ?
  39. (parity ? rnd(64) : 255 - rnd(64)) :
  40. (parity ? rnd(32) : 255 - rnd(32));
  41. /* clang-format on */
  42. }
  43. for (int bit_method = 0; bit_method <= 3; ++bit_method) {
  44. const int random_seed = 6432;
  45. const int kBufferSize = 10000;
  46. ACMRandom bit_rnd(random_seed);
  47. vpx_writer bw;
  48. uint8_t bw_buffer[kBufferSize];
  49. vpx_start_encode(&bw, bw_buffer);
  50. int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
  51. for (int i = 0; i < kBitsToTest; ++i) {
  52. if (bit_method == 2) {
  53. bit = (i & 1);
  54. } else if (bit_method == 3) {
  55. bit = bit_rnd(2);
  56. }
  57. vpx_write(&bw, bit, static_cast<int>(probas[i]));
  58. }
  59. vpx_stop_encode(&bw);
  60. // First bit should be zero
  61. GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
  62. vpx_reader br;
  63. vpx_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
  64. bit_rnd.Reset(random_seed);
  65. for (int i = 0; i < kBitsToTest; ++i) {
  66. if (bit_method == 2) {
  67. bit = (i & 1);
  68. } else if (bit_method == 3) {
  69. bit = bit_rnd(2);
  70. }
  71. GTEST_ASSERT_EQ(vpx_read(&br, probas[i]), bit)
  72. << "pos: " << i << " / " << kBitsToTest
  73. << " bit_method: " << bit_method << " method: " << method;
  74. }
  75. }
  76. }
  77. }
  78. }