nal-rbsp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019-2020 Pengutronix, Michael Tretter <[email protected]>
  4. *
  5. * Helper functions to generate a raw byte sequence payload from values.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #include <linux/v4l2-controls.h>
  11. #include <linux/device.h>
  12. #include <linux/export.h>
  13. #include <linux/log2.h>
  14. #include "nal-rbsp.h"
  15. void rbsp_init(struct rbsp *rbsp, void *addr, size_t size,
  16. struct nal_rbsp_ops *ops)
  17. {
  18. if (!rbsp)
  19. return;
  20. rbsp->data = addr;
  21. rbsp->size = size;
  22. rbsp->pos = 0;
  23. rbsp->ops = ops;
  24. rbsp->error = 0;
  25. }
  26. void rbsp_unsupported(struct rbsp *rbsp)
  27. {
  28. rbsp->error = -EINVAL;
  29. }
  30. static int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value);
  31. static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value);
  32. /*
  33. * When reading or writing, the emulation_prevention_three_byte is detected
  34. * only when the 2 one bits need to be inserted. Therefore, we are not
  35. * actually adding the 0x3 byte, but the 2 one bits and the six 0 bits of the
  36. * next byte.
  37. */
  38. #define EMULATION_PREVENTION_THREE_BYTE (0x3 << 6)
  39. static int add_emulation_prevention_three_byte(struct rbsp *rbsp)
  40. {
  41. rbsp->num_consecutive_zeros = 0;
  42. rbsp_write_bits(rbsp, 8, EMULATION_PREVENTION_THREE_BYTE);
  43. return 0;
  44. }
  45. static int discard_emulation_prevention_three_byte(struct rbsp *rbsp)
  46. {
  47. unsigned int tmp = 0;
  48. rbsp->num_consecutive_zeros = 0;
  49. rbsp_read_bits(rbsp, 8, &tmp);
  50. if (tmp != EMULATION_PREVENTION_THREE_BYTE)
  51. return -EINVAL;
  52. return 0;
  53. }
  54. static inline int rbsp_read_bit(struct rbsp *rbsp)
  55. {
  56. int shift;
  57. int ofs;
  58. int bit;
  59. int err;
  60. if (rbsp->num_consecutive_zeros == 22) {
  61. err = discard_emulation_prevention_three_byte(rbsp);
  62. if (err)
  63. return err;
  64. }
  65. shift = 7 - (rbsp->pos % 8);
  66. ofs = rbsp->pos / 8;
  67. if (ofs >= rbsp->size)
  68. return -EINVAL;
  69. bit = (rbsp->data[ofs] >> shift) & 1;
  70. rbsp->pos++;
  71. if (bit == 1 ||
  72. (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0)))
  73. rbsp->num_consecutive_zeros = 0;
  74. else
  75. rbsp->num_consecutive_zeros++;
  76. return bit;
  77. }
  78. static inline int rbsp_write_bit(struct rbsp *rbsp, bool value)
  79. {
  80. int shift;
  81. int ofs;
  82. if (rbsp->num_consecutive_zeros == 22)
  83. add_emulation_prevention_three_byte(rbsp);
  84. shift = 7 - (rbsp->pos % 8);
  85. ofs = rbsp->pos / 8;
  86. if (ofs >= rbsp->size)
  87. return -EINVAL;
  88. rbsp->data[ofs] &= ~(1 << shift);
  89. rbsp->data[ofs] |= value << shift;
  90. rbsp->pos++;
  91. if (value ||
  92. (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0))) {
  93. rbsp->num_consecutive_zeros = 0;
  94. } else {
  95. rbsp->num_consecutive_zeros++;
  96. }
  97. return 0;
  98. }
  99. static inline int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value)
  100. {
  101. int i;
  102. int bit;
  103. unsigned int tmp = 0;
  104. if (n > 8 * sizeof(*value))
  105. return -EINVAL;
  106. for (i = n; i > 0; i--) {
  107. bit = rbsp_read_bit(rbsp);
  108. if (bit < 0)
  109. return bit;
  110. tmp |= bit << (i - 1);
  111. }
  112. if (value)
  113. *value = tmp;
  114. return 0;
  115. }
  116. static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value)
  117. {
  118. int ret;
  119. if (n > 8 * sizeof(value))
  120. return -EINVAL;
  121. while (n--) {
  122. ret = rbsp_write_bit(rbsp, (value >> n) & 1);
  123. if (ret)
  124. return ret;
  125. }
  126. return 0;
  127. }
  128. static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *value)
  129. {
  130. int leading_zero_bits = 0;
  131. unsigned int tmp = 0;
  132. int ret;
  133. while ((ret = rbsp_read_bit(rbsp)) == 0)
  134. leading_zero_bits++;
  135. if (ret < 0)
  136. return ret;
  137. if (leading_zero_bits > 0) {
  138. ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
  139. if (ret)
  140. return ret;
  141. }
  142. if (value)
  143. *value = (1 << leading_zero_bits) - 1 + tmp;
  144. return 0;
  145. }
  146. static int rbsp_write_uev(struct rbsp *rbsp, unsigned int *value)
  147. {
  148. int ret;
  149. int leading_zero_bits;
  150. if (!value)
  151. return -EINVAL;
  152. leading_zero_bits = ilog2(*value + 1);
  153. ret = rbsp_write_bits(rbsp, leading_zero_bits, 0);
  154. if (ret)
  155. return ret;
  156. return rbsp_write_bits(rbsp, leading_zero_bits + 1, *value + 1);
  157. }
  158. static int rbsp_read_sev(struct rbsp *rbsp, int *value)
  159. {
  160. int ret;
  161. unsigned int tmp;
  162. ret = rbsp_read_uev(rbsp, &tmp);
  163. if (ret)
  164. return ret;
  165. if (value) {
  166. if (tmp & 1)
  167. *value = (tmp + 1) / 2;
  168. else
  169. *value = -(tmp / 2);
  170. }
  171. return 0;
  172. }
  173. static int rbsp_write_sev(struct rbsp *rbsp, int *value)
  174. {
  175. unsigned int tmp;
  176. if (!value)
  177. return -EINVAL;
  178. if (*value > 0)
  179. tmp = (2 * (*value)) | 1;
  180. else
  181. tmp = -2 * (*value);
  182. return rbsp_write_uev(rbsp, &tmp);
  183. }
  184. static int __rbsp_write_bit(struct rbsp *rbsp, int *value)
  185. {
  186. return rbsp_write_bit(rbsp, *value);
  187. }
  188. static int __rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int *value)
  189. {
  190. return rbsp_write_bits(rbsp, n, *value);
  191. }
  192. struct nal_rbsp_ops write = {
  193. .rbsp_bit = __rbsp_write_bit,
  194. .rbsp_bits = __rbsp_write_bits,
  195. .rbsp_uev = rbsp_write_uev,
  196. .rbsp_sev = rbsp_write_sev,
  197. };
  198. static int __rbsp_read_bit(struct rbsp *rbsp, int *value)
  199. {
  200. int tmp = rbsp_read_bit(rbsp);
  201. if (tmp < 0)
  202. return tmp;
  203. *value = tmp;
  204. return 0;
  205. }
  206. struct nal_rbsp_ops read = {
  207. .rbsp_bit = __rbsp_read_bit,
  208. .rbsp_bits = rbsp_read_bits,
  209. .rbsp_uev = rbsp_read_uev,
  210. .rbsp_sev = rbsp_read_sev,
  211. };
  212. void rbsp_bit(struct rbsp *rbsp, int *value)
  213. {
  214. if (rbsp->error)
  215. return;
  216. rbsp->error = rbsp->ops->rbsp_bit(rbsp, value);
  217. }
  218. void rbsp_bits(struct rbsp *rbsp, int n, int *value)
  219. {
  220. if (rbsp->error)
  221. return;
  222. rbsp->error = rbsp->ops->rbsp_bits(rbsp, n, value);
  223. }
  224. void rbsp_uev(struct rbsp *rbsp, unsigned int *value)
  225. {
  226. if (rbsp->error)
  227. return;
  228. rbsp->error = rbsp->ops->rbsp_uev(rbsp, value);
  229. }
  230. void rbsp_sev(struct rbsp *rbsp, int *value)
  231. {
  232. if (rbsp->error)
  233. return;
  234. rbsp->error = rbsp->ops->rbsp_sev(rbsp, value);
  235. }
  236. void rbsp_trailing_bits(struct rbsp *rbsp)
  237. {
  238. unsigned int rbsp_stop_one_bit = 1;
  239. unsigned int rbsp_alignment_zero_bit = 0;
  240. rbsp_bit(rbsp, &rbsp_stop_one_bit);
  241. rbsp_bits(rbsp, round_up(rbsp->pos, 8) - rbsp->pos,
  242. &rbsp_alignment_zero_bit);
  243. }