1 // SPDX-License-Identifier: GPL-2.0
5 * Helper functions to generate a raw byte sequence payload from values.
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/v4l2-controls.h>
13 #include <linux/device.h>
14 #include <linux/export.h>
15 #include <linux/log2.h>
19 void rbsp_init(struct rbsp *rbsp, void *addr, size_t size,
20 struct nal_rbsp_ops *ops)
32 void rbsp_unsupported(struct rbsp *rbsp)
34 rbsp->error = -EINVAL;
37 static int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value);
38 static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value);
41 * When reading or writing, the emulation_prevention_three_byte is detected
42 * only when the 2 one bits need to be inserted. Therefore, we are not
43 * actually adding the 0x3 byte, but the 2 one bits and the six 0 bits of the
46 #define EMULATION_PREVENTION_THREE_BYTE (0x3 << 6)
48 static int add_emulation_prevention_three_byte(struct rbsp *rbsp)
50 rbsp->num_consecutive_zeros = 0;
51 rbsp_write_bits(rbsp, 8, EMULATION_PREVENTION_THREE_BYTE);
56 static int discard_emulation_prevention_three_byte(struct rbsp *rbsp)
60 rbsp->num_consecutive_zeros = 0;
61 rbsp_read_bits(rbsp, 8, &tmp);
62 if (tmp != EMULATION_PREVENTION_THREE_BYTE)
68 static inline int rbsp_read_bit(struct rbsp *rbsp)
75 if (rbsp->num_consecutive_zeros == 22) {
76 err = discard_emulation_prevention_three_byte(rbsp);
81 shift = 7 - (rbsp->pos % 8);
83 if (ofs >= rbsp->size)
86 bit = (rbsp->data[ofs] >> shift) & 1;
91 (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0)))
92 rbsp->num_consecutive_zeros = 0;
94 rbsp->num_consecutive_zeros++;
99 static inline int rbsp_write_bit(struct rbsp *rbsp, bool value)
104 if (rbsp->num_consecutive_zeros == 22)
105 add_emulation_prevention_three_byte(rbsp);
107 shift = 7 - (rbsp->pos % 8);
109 if (ofs >= rbsp->size)
112 rbsp->data[ofs] &= ~(1 << shift);
113 rbsp->data[ofs] |= value << shift;
118 (rbsp->num_consecutive_zeros < 7 && (rbsp->pos % 8 == 0))) {
119 rbsp->num_consecutive_zeros = 0;
121 rbsp->num_consecutive_zeros++;
127 static inline int rbsp_read_bits(struct rbsp *rbsp, int n, unsigned int *value)
131 unsigned int tmp = 0;
133 if (n > 8 * sizeof(*value))
136 for (i = n; i > 0; i--) {
137 bit = rbsp_read_bit(rbsp);
140 tmp |= bit << (i - 1);
149 static int rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int value)
153 if (n > 8 * sizeof(value))
157 ret = rbsp_write_bit(rbsp, (value >> n) & 1);
165 static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *value)
167 int leading_zero_bits = 0;
168 unsigned int tmp = 0;
171 while ((ret = rbsp_read_bit(rbsp)) == 0)
176 if (leading_zero_bits > 0) {
177 ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
183 *value = (1 << leading_zero_bits) - 1 + tmp;
188 static int rbsp_write_uev(struct rbsp *rbsp, unsigned int *value)
191 int leading_zero_bits;
196 leading_zero_bits = ilog2(*value + 1);
198 ret = rbsp_write_bits(rbsp, leading_zero_bits, 0);
202 return rbsp_write_bits(rbsp, leading_zero_bits + 1, *value + 1);
205 static int rbsp_read_sev(struct rbsp *rbsp, int *value)
210 ret = rbsp_read_uev(rbsp, &tmp);
216 *value = (tmp + 1) / 2;
224 static int rbsp_write_sev(struct rbsp *rbsp, int *value)
232 tmp = (2 * (*value)) | 1;
236 return rbsp_write_uev(rbsp, &tmp);
239 static int __rbsp_write_bit(struct rbsp *rbsp, int *value)
241 return rbsp_write_bit(rbsp, *value);
244 static int __rbsp_write_bits(struct rbsp *rbsp, int n, unsigned int *value)
246 return rbsp_write_bits(rbsp, n, *value);
249 struct nal_rbsp_ops write = {
250 .rbsp_bit = __rbsp_write_bit,
251 .rbsp_bits = __rbsp_write_bits,
252 .rbsp_uev = rbsp_write_uev,
253 .rbsp_sev = rbsp_write_sev,
256 static int __rbsp_read_bit(struct rbsp *rbsp, int *value)
258 int tmp = rbsp_read_bit(rbsp);
267 struct nal_rbsp_ops read = {
268 .rbsp_bit = __rbsp_read_bit,
269 .rbsp_bits = rbsp_read_bits,
270 .rbsp_uev = rbsp_read_uev,
271 .rbsp_sev = rbsp_read_sev,
274 void rbsp_bit(struct rbsp *rbsp, int *value)
278 rbsp->error = rbsp->ops->rbsp_bit(rbsp, value);
281 void rbsp_bits(struct rbsp *rbsp, int n, int *value)
285 rbsp->error = rbsp->ops->rbsp_bits(rbsp, n, value);
288 void rbsp_uev(struct rbsp *rbsp, unsigned int *value)
292 rbsp->error = rbsp->ops->rbsp_uev(rbsp, value);
295 void rbsp_sev(struct rbsp *rbsp, int *value)
299 rbsp->error = rbsp->ops->rbsp_sev(rbsp, value);
302 void rbsp_trailing_bits(struct rbsp *rbsp)
304 unsigned int rbsp_stop_one_bit = 1;
305 unsigned int rbsp_alignment_zero_bit = 0;
307 rbsp_bit(rbsp, &rbsp_stop_one_bit);
308 rbsp_bits(rbsp, round_up(rbsp->pos, 8) - rbsp->pos,
309 &rbsp_alignment_zero_bit);