rle.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #if defined(__GNUC__) && !defined(__clang__)
  5. # define RLE_FORCE_O3 __attribute__((optimize("O3")))
  6. #else
  7. # define RLE_FORCE_O3
  8. #endif
  9. #define ENC_REPEAT_COUNT 3
  10. #define MAX_SIZE 4
  11. static inline int get_repetition_count(const uint8_t * src, size_t count, size_t size, int line_length)
  12. {
  13. uint8_t rep_val[MAX_SIZE];
  14. memcpy(rep_val, src, size);
  15. src += size;
  16. count--;
  17. int length = 1;
  18. while (count > 0 && length < line_length) {
  19. uint8_t val[MAX_SIZE];
  20. memcpy(val, src, size);
  21. if (memcmp(val, rep_val, size) != 0)
  22. break;
  23. src += size;
  24. count--;
  25. length++;
  26. }
  27. return length;
  28. }
  29. static int get_non_repetition_count(const uint8_t * src, size_t count, size_t size, int line_length)
  30. {
  31. if (count < 2)
  32. return count;
  33. uint8_t v1[MAX_SIZE];
  34. uint8_t v2[MAX_SIZE];
  35. memcpy(v1, src, size);
  36. memcpy(v2, src + size, size);
  37. src += size * 2;
  38. count -= 2;
  39. if (memcmp(v1, v2, size) == 0)
  40. return 1;
  41. int length = 1;
  42. while (count > 0 && length < line_length) {
  43. uint8_t v3[MAX_SIZE];
  44. memcpy(v3, src, size);
  45. if (memcmp(v2, v3, size) == 0)
  46. break;
  47. memcpy(v2, v3, size);
  48. src += size;
  49. count--;
  50. length++;
  51. }
  52. if (count == 0 && length < line_length)
  53. length++;
  54. return length;
  55. }
  56. /**
  57. * @brief RLE encode
  58. *
  59. * @param out_buf pointer to encoded output buffer
  60. * @param out_size size of output buffer in bytes
  61. * @param in_buf pointer to input buffer
  62. * @param in_count size of input buffer in elements
  63. * @param size size of each element in bytes
  64. *
  65. * @retval number of bytes actually encoded if out_buf is not NULL
  66. * @retval total encoded bytes if out_buf is NULL
  67. */
  68. int rle_compress(const uint8_t* in_buf, uint8_t * out_buf, size_t in_count, size_t out_size, size_t size)
  69. {
  70. int enc_size = 0;
  71. while (in_count > 0) {
  72. int count = get_repetition_count(in_buf, in_count, size, 127);
  73. int copy_len, sign;
  74. if (count >= ENC_REPEAT_COUNT) {
  75. copy_len = size;
  76. sign = 0x80;
  77. } else {
  78. count = get_non_repetition_count(in_buf, in_count, size, 127);
  79. copy_len = size * count;
  80. sign = 0x00;
  81. }
  82. if (out_buf != NULL) {
  83. if (out_size < copy_len + 1)
  84. break;
  85. out_buf[0] = sign | count;
  86. memcpy(&out_buf[1], in_buf, copy_len);
  87. out_buf += copy_len + 1;
  88. out_size -= copy_len + 1;
  89. }
  90. enc_size += copy_len + 1;
  91. in_buf += size * count;
  92. in_count -= count;
  93. }
  94. return enc_size;
  95. }
  96. RLE_FORCE_O3
  97. static inline void fill_repetition(uint8_t * dest, const uint8_t * src, size_t count, size_t size)
  98. {
  99. if (size == 1) {
  100. memset(dest, src[0], count);
  101. } else if (size == 2) {
  102. if (((uintptr_t)dest & 0x1) == 0) {
  103. uint16_t val = src[0] | ((uint16_t)src[1] << 8);
  104. for (int i = count; i > 0; i--) {
  105. *(uint16_t *)dest = val;
  106. dest += size;
  107. }
  108. } else {
  109. for (int i = count; i > 0; i--) {
  110. dest[0] = src[0];
  111. dest[1] = src[1];
  112. dest += size;
  113. }
  114. }
  115. } else if (size == 4) {
  116. if (((uintptr_t)dest & 0x3) == 0) {
  117. uint32_t val = src[0] | ((uint32_t)src[1] << 8) |
  118. ((uint32_t)src[2] << 16) | ((uint32_t)src[3] << 24);
  119. for (int i = count; i > 0; i--) {
  120. *(uint32_t *)dest = val;
  121. dest += size;
  122. }
  123. } else {
  124. for (int i = count; i > 0; i--) {
  125. dest[0] = src[0];
  126. dest[1] = src[1];
  127. dest[2] = src[2];
  128. dest[3] = src[3];
  129. dest += size;
  130. }
  131. }
  132. } else if (size == 3) {
  133. for (int i = count; i > 0; i--) {
  134. dest[0] = src[0];
  135. dest[1] = src[1];
  136. dest[2] = src[2];
  137. dest += size;
  138. }
  139. } else { /* common case */
  140. for (int i = count; i > 0; i--) {
  141. memcpy(dest, src, size);
  142. dest += size;
  143. }
  144. }
  145. }
  146. /**
  147. * @brief RLE decode
  148. *
  149. * @param out_buf pointer to output buffer
  150. * @param out_count size of output buffer in elements
  151. * @param in_buf pointer to encoded input buffer
  152. * @param in_size size of input buffer in bytes
  153. * @param size size of each element in bytes
  154. *
  155. * @retval number of bytes actually decoded if out_buf is not NULL
  156. * @retval total decoded bytes if out_buf is NULL
  157. */
  158. RLE_FORCE_O3
  159. int rle_decompress(const uint8_t * in_buf, uint8_t * out_buf, int in_size, int out_count, size_t size)
  160. {
  161. int dec_size = 0;
  162. if (out_buf == NULL)
  163. out_count = INT32_MAX;
  164. while (in_size > 0 && out_count > 0) {
  165. uint8_t sign = in_buf[0];
  166. int count = sign & 0x7F;
  167. if (count > out_count)
  168. count = out_count;
  169. out_count -= count;
  170. dec_size += size * count;
  171. if ((sign & 0x80) != 0) {
  172. if (out_buf != NULL) {
  173. fill_repetition(out_buf, &in_buf[1], count, size);
  174. out_buf += size * count;
  175. }
  176. in_buf += (1 + size);
  177. in_size -= (1 + size);
  178. } else {
  179. if (out_buf != NULL) {
  180. memcpy(out_buf, &in_buf[1], size * count);
  181. out_buf += size * count;
  182. }
  183. in_buf += (1 + size * count);
  184. in_size -= (1 + size * count);
  185. }
  186. }
  187. return dec_size;
  188. }