aom_timer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2016, 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. #ifndef AOM_AOM_PORTS_AOM_TIMER_H_
  12. #define AOM_AOM_PORTS_AOM_TIMER_H_
  13. #include "config/aom_config.h"
  14. #if CONFIG_OS_SUPPORT
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #if defined(_WIN32)
  18. /*
  19. * Win32 specific includes
  20. */
  21. #undef NOMINMAX
  22. #define NOMINMAX
  23. #undef WIN32_LEAN_AND_MEAN
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #else
  27. /*
  28. * POSIX specific includes
  29. */
  30. #include <sys/time.h>
  31. /* timersub is not provided by msys at this time. */
  32. #ifndef timersub
  33. #define timersub(a, b, result) \
  34. do { \
  35. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  36. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  37. if ((result)->tv_usec < 0) { \
  38. --(result)->tv_sec; \
  39. (result)->tv_usec += 1000000; \
  40. } \
  41. } while (0)
  42. #endif
  43. #endif
  44. struct aom_usec_timer {
  45. #if defined(_WIN32)
  46. LARGE_INTEGER begin, end;
  47. #else
  48. struct timeval begin, end;
  49. #endif
  50. };
  51. static INLINE void aom_usec_timer_start(struct aom_usec_timer *t) {
  52. #if defined(_WIN32)
  53. QueryPerformanceCounter(&t->begin);
  54. #else
  55. gettimeofday(&t->begin, NULL);
  56. #endif
  57. }
  58. static INLINE void aom_usec_timer_mark(struct aom_usec_timer *t) {
  59. #if defined(_WIN32)
  60. QueryPerformanceCounter(&t->end);
  61. #else
  62. gettimeofday(&t->end, NULL);
  63. #endif
  64. }
  65. static INLINE int64_t aom_usec_timer_elapsed(struct aom_usec_timer *t) {
  66. #if defined(_WIN32)
  67. LARGE_INTEGER freq, diff;
  68. diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
  69. QueryPerformanceFrequency(&freq);
  70. return diff.QuadPart * 1000000 / freq.QuadPart;
  71. #else
  72. struct timeval diff;
  73. timersub(&t->end, &t->begin, &diff);
  74. return ((int64_t)diff.tv_sec) * 1000000 + diff.tv_usec;
  75. #endif
  76. }
  77. #else /* CONFIG_OS_SUPPORT = 0*/
  78. /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
  79. #ifndef timersub
  80. #define timersub(a, b, result)
  81. #endif
  82. struct aom_usec_timer {
  83. void *dummy;
  84. };
  85. static INLINE void aom_usec_timer_start(struct aom_usec_timer *t) { (void)t; }
  86. static INLINE void aom_usec_timer_mark(struct aom_usec_timer *t) { (void)t; }
  87. static INLINE int aom_usec_timer_elapsed(struct aom_usec_timer *t) {
  88. (void)t;
  89. return 0;
  90. }
  91. #endif /* CONFIG_OS_SUPPORT */
  92. #endif // AOM_AOM_PORTS_AOM_TIMER_H_