You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.8KB

  1. /*
  2. * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "mem.h"
  23. #include "intreadwrite.h"
  24. #include "murmur3.h"
  25. typedef struct AVMurMur3 {
  26. uint64_t h1, h2;
  27. uint8_t state[16];
  28. int state_pos;
  29. uint64_t len;
  30. } AVMurMur3;
  31. AVMurMur3 *av_murmur3_alloc(void)
  32. {
  33. return av_mallocz(sizeof(AVMurMur3));
  34. }
  35. void av_murmur3_init_seeded(AVMurMur3 *c, uint64_t seed)
  36. {
  37. memset(c, 0, sizeof(*c));
  38. c->h1 = c->h2 = seed;
  39. }
  40. void av_murmur3_init(AVMurMur3 *c)
  41. {
  42. // arbitrary random number as seed
  43. av_murmur3_init_seeded(c, 0x725acc55daddca55);
  44. }
  45. static const uint64_t c1 = UINT64_C(0x87c37b91114253d5);
  46. static const uint64_t c2 = UINT64_C(0x4cf5ad432745937f);
  47. #define ROT(a, b) (((a) << (b)) | ((a) >> (64 - (b))))
  48. static uint64_t inline get_k1(const uint8_t *src)
  49. {
  50. uint64_t k = AV_RL64(src);
  51. k *= c1;
  52. k = ROT(k, 31);
  53. k *= c2;
  54. return k;
  55. }
  56. static inline uint64_t get_k2(const uint8_t *src)
  57. {
  58. uint64_t k = AV_RL64(src + 8);
  59. k *= c2;
  60. k = ROT(k, 33);
  61. k *= c1;
  62. return k;
  63. }
  64. static inline uint64_t update_h1(uint64_t k, uint64_t h1, uint64_t h2)
  65. {
  66. k ^= h1;
  67. k = ROT(k, 27);
  68. k += h2;
  69. k *= 5;
  70. k += 0x52dce729;
  71. return k;
  72. }
  73. static inline uint64_t update_h2(uint64_t k, uint64_t h1, uint64_t h2)
  74. {
  75. k ^= h2;
  76. k = ROT(k, 31);
  77. k += h1;
  78. k *= 5;
  79. k += 0x38495ab5;
  80. return k;
  81. }
  82. #if FF_API_CRYPTO_SIZE_T
  83. void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, int len)
  84. #else
  85. void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, size_t len)
  86. #endif
  87. {
  88. const uint8_t *end;
  89. uint64_t h1 = c->h1, h2 = c->h2;
  90. uint64_t k1, k2;
  91. if (len <= 0) return;
  92. c->len += len;
  93. if (c->state_pos > 0) {
  94. while (c->state_pos < 16) {
  95. c->state[c->state_pos++] = *src++;
  96. if (--len <= 0) return;
  97. }
  98. c->state_pos = 0;
  99. k1 = get_k1(c->state);
  100. k2 = get_k2(c->state);
  101. h1 = update_h1(k1, h1, h2);
  102. h2 = update_h2(k2, h1, h2);
  103. }
  104. end = src + (len & ~15);
  105. while (src < end) {
  106. // These could be done sequentially instead
  107. // of interleaved, but like this is over 10% faster
  108. k1 = get_k1(src);
  109. k2 = get_k2(src);
  110. h1 = update_h1(k1, h1, h2);
  111. h2 = update_h2(k2, h1, h2);
  112. src += 16;
  113. }
  114. c->h1 = h1;
  115. c->h2 = h2;
  116. len &= 15;
  117. if (len > 0) {
  118. memcpy(c->state, src, len);
  119. c->state_pos = len;
  120. }
  121. }
  122. static inline uint64_t fmix(uint64_t k)
  123. {
  124. k ^= k >> 33;
  125. k *= UINT64_C(0xff51afd7ed558ccd);
  126. k ^= k >> 33;
  127. k *= UINT64_C(0xc4ceb9fe1a85ec53);
  128. k ^= k >> 33;
  129. return k;
  130. }
  131. void av_murmur3_final(AVMurMur3 *c, uint8_t dst[16])
  132. {
  133. uint64_t h1 = c->h1, h2 = c->h2;
  134. memset(c->state + c->state_pos, 0, sizeof(c->state) - c->state_pos);
  135. h1 ^= get_k1(c->state) ^ c->len;
  136. h2 ^= get_k2(c->state) ^ c->len;
  137. h1 += h2;
  138. h2 += h1;
  139. h1 = fmix(h1);
  140. h2 = fmix(h2);
  141. h1 += h2;
  142. h2 += h1;
  143. AV_WL64(dst, h1);
  144. AV_WL64(dst + 8, h2);
  145. }