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