edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBb07f07aTahinli3 months ago1061 commits
| 1 | //! AV1 bit-primitive writers — bit-exact inverses of the readers in |
| 2 | //! [`ec_av1_syntax::obu`], built on the family [`ec_core::BitWriter`]. |
| 3 | //! |
| 4 | //! Every function here is the inverse of a named AV1 bit-reading descriptor |
| 5 | //! (spec section 4.10 / 5.9); the parser function it mirrors is named in its |
| 6 | //! docs and roundtripped in the tests below. |
| 7 | |
| 8 | use floor_log2; |
| 9 | use BitWriter; |
| 10 | |
| 11 | /// `leb128()` writer (spec 4.10.5); inverts [`ec_av1_syntax::obu::read_leb128`]. |
| 12 | /// |
| 13 | /// Little-endian base-128: each byte carries 7 payload bits and a high |
| 14 | /// continuation bit, set on every byte but the last. A 32-bit value needs at |
| 15 | /// most 5 bytes (the parser accepts up to 8 and rejects anything that does not |
| 16 | /// fit in 32 bits). |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | /// `uvlc()` writer (spec 4.10.3); inverts [`ec_av1_syntax::obu::read_uvlc`]. |
| 31 | /// |
| 32 | /// `0` is a single `1` bit; `u32::MAX` is the 32-leading-zero escape the |
| 33 | /// parser returns verbatim; otherwise `lz` zero bits, a `1` bit, then the |
| 34 | /// `lz`-bit suffix, where `lz = floor_log2(value + 1)` and the suffix is |
| 35 | /// `value - (1 << lz) + 1` (so the decoded value is `suffix + (1 << lz) - 1`). |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | /// `le(n)` writer (spec 4.10.4); inverts [`ec_av1_syntax::obu::read_le`]. |
| 53 | /// |
| 54 | /// `n`-byte little-endian, least-significant byte first. AV1 reads `le(n)` |
| 55 | /// only at a byte boundary; the writer should be byte aligned when called. |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | /// `su(n)` writer (spec 4.10.6); inverts [`ec_av1_syntax::obu::read_su`]. |
| 63 | /// |
| 64 | /// `n`-bit two's complement, delegated to [`BitWriter::write_signed`]. |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | /// `ns(n)` writer (spec 4.10.7); inverts [`ec_av1_syntax::obu::read_ns`]. |
| 70 | /// |
| 71 | /// Non-symmetric unsigned below `n`: `w = floor_log2(n) + 1` bits, split into a |
| 72 | /// `w - 1`-bit leading part and, for the high range, one extra bit. Values |
| 73 | /// `[0, m)` (with `m = (1 << w) - n`) carry themselves in `w - 1` bits; values |
| 74 | /// `[m, n)` carry `(value + m) >> 1` in `w - 1` bits plus the low bit of |
| 75 | /// `value + m`. |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | /// `delta_q()` writer (spec 5.9.13); inverts |
| 93 | /// `ec_av1_syntax::frame::read_delta_q`. |
| 94 | /// |
| 95 | /// One `delta_coded` bit: `0` when the delta is zero, otherwise `1` followed by |
| 96 | /// `su(7)` holding the signed delta. (The frame module is private, so the test |
| 97 | /// reconstructs the read side from [`ec_av1_syntax::obu::read_su`] plus a bit.) |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | /// `trailing_bits()` writer (spec 4.8.4). |
| 108 | /// |
| 109 | /// A required `1` bit followed by zero bits up to the next byte boundary. AV1 |
| 110 | /// ends every OBU that is not a whole number of bytes with this. |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | /// `byte_alignment()` writer (spec 4.8.5). |
| 117 | /// |
| 118 | /// Zero bits up to the next byte boundary — [`BitWriter::align_to_byte`] is the |
| 119 | /// exact inverse of the parser's `while !byte_aligned: read_bit()` loop. |
| 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 |