edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Intra reconstruction of one transform block into the picture (8.4.4.2 and |
| 2 | //! the sample sum of 8.6.7): the reference samples with their z-scan and |
| 3 | //! constrained-intra availability, the prediction, and the residual added and |
| 4 | //! clipped. |
| 5 | //! |
| 6 | //! The parser calls these in decoding order, so every neighbour that the |
| 7 | //! availability rules admit is already reconstructed when it is read. |
| 8 | |
| 9 | use super::pic::PicState; |
| 10 | use crate::intra::{self, Availability, Refs}; |
| 11 | |
| 12 | impl PicState { |
| 13 | /// Whether the sample of component `c` at `(xn, yn)` (component units) is |
| 14 | /// available as an intra reference for the block at `(x, y)` of the same |
| 15 | /// component (8.4.4.2.2): decoded already, and, with constrained intra |
| 16 | /// prediction, an intra coding unit. |
| 17 | fn intra_ref_available(&self, c: usize, x: usize, y: usize, xn: isize, yn: isize) -> bool { |
| 18 | if xn < 0 || yn < 0 { |
| 19 | return false; |
| 20 | } |
| 21 | let (sx, sy) = if c == 0 { |
| 22 | (0, 0) |
| 23 | } else { |
| 24 | (self.sub_x(), self.sub_y()) |
| 25 | }; |
| 26 | let (xl, yl) = ((x << sx) as isize, (y << sy) as isize); |
| 27 | let (xnl, ynl) = (xn << sx, yn << sy); |
| 28 | if !self.available(xl as usize, yl as usize, xnl, ynl) { |
| 29 | return false; |
| 30 | } |
| 31 | !self.constrained_intra_pred || self.info[self.unit(xnl as usize, ynl as usize)].intra |
| 32 | } |
| 33 | |
| 34 | /// Predict the `1 << log2` square of component `c` at component position |
| 35 | /// `(x, y)` with intra mode `mode`, writing the prediction into the plane. |
| 36 | pub fn predict_intra(&mut self, c: usize, x: usize, y: usize, log2: u32, mode: u8) { |
| 37 | self.predict_intra_ext(c, x, y, log2, mode, false); |
| 38 | } |
| 39 | |
| 40 | /// [`PicState::predict_intra`] with `no_boundary` set for a transquant-bypass unit under |
| 41 | /// implicit RDPCM (`disableIntraBoundaryFilter`, 8.4.4.2.6). |
| 42 | pub fn predict_intra_ext( |
| 43 | &mut self, |
| 44 | c: usize, |
| 45 | x: usize, |
| 46 | y: usize, |
| 47 | log2: u32, |
| 48 | mode: u8, |
| 49 | no_boundary: bool, |
| 50 | ) { |
| 51 | let n = 1usize << log2; |
| 52 | let bd = self.bit_depth(c); |
| 53 | let (stride, plane) = self.plane_view(c); |
| 54 | let origin = self.plane_origin(c); |
| 55 | let mut refs = Refs::<u16>::default(); |
| 56 | let mut avail = Availability::default(); |
| 57 | let at = |xn: usize, yn: usize| -> u16 { plane[(yn - origin) * stride + xn] }; |
| 58 | // Availability is constant over a 4x4 luma unit, so test once per unit (4 samples |
| 59 | // of a luma plane; in a chroma plane 4 luma samples are 4 >> sub samples) and fill |
| 60 | // the run. The left column steps by the vertical, the top row by the horizontal. |
| 61 | let (sub_x, sub_y) = if c == 0 { |
| 62 | (0, 0) |
| 63 | } else { |
| 64 | (self.sub_x() as usize, self.sub_y() as usize) |
| 65 | }; |
| 66 | let step_left = (4usize >> sub_y).min(n); |
| 67 | let step_top = (4usize >> sub_x).min(n); |
| 68 | for k in (0..2 * n).step_by(step_left) { |
| 69 | if self.intra_ref_available(c, x, y, x as isize - 1, (y + k) as isize) { |
| 70 | for j in k..k + step_left { |
| 71 | avail.left |= 1 << j; |
| 72 | refs.left[j] = at(x - 1, y + j); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | for k in (0..2 * n).step_by(step_top) { |
| 77 | if self.intra_ref_available(c, x, y, (x + k) as isize, y as isize - 1) { |
| 78 | for j in k..k + step_top { |
| 79 | avail.top |= 1 << j; |
| 80 | refs.top[j] = at(x + j, y - 1); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if self.intra_ref_available(c, x, y, x as isize - 1, y as isize - 1) { |
| 85 | avail.corner = true; |
| 86 | refs.corner = at(x - 1, y - 1); |
| 87 | } |
| 88 | intra::substitute(&mut refs, &avail, n, bd); |
| 89 | let mut pred = vec![0u16; n * n]; |
| 90 | let flags = intra::PredFlags { |
| 91 | smooth: (c == 0 || self.chroma_format_idc == 3) && !self.intra_smoothing_disabled, |
| 92 | strong: c == 0 && self.strong_intra_smoothing, |
| 93 | edge: c == 0, |
| 94 | no_boundary, |
| 95 | }; |
| 96 | intra::predict_ext(&refs, mode, n, flags, bd, &mut pred); |
| 97 | let origin = self.plane_origin(c); |
| 98 | let (stride, plane) = self.plane_view_mut(c); |
| 99 | for j in 0..n { |
| 100 | let row = (y + j - origin) * stride + x; |
| 101 | plane[row..row + n].copy_from_slice(&pred[j * n..j * n + n]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Add a residual block to the prediction already in the plane, clipping |
| 106 | /// to the sample range (8.6.7). |
| 107 | pub fn add_residual(&mut self, c: usize, x: usize, y: usize, n: usize, residual: &[i32]) { |
| 108 | let max = (1i32 << self.bit_depth(c)) - 1; |
| 109 | let origin = self.plane_origin(c); |
| 110 | let (stride, plane) = self.plane_view_mut(c); |
| 111 | for j in 0..n { |
| 112 | let start = (y + j - origin) * stride + x; |
| 113 | let row = &mut plane[start..start + n]; |
| 114 | for (i, sample) in row.iter_mut().enumerate() { |
| 115 | let v = i32::from(*sample) + residual[j * n + i]; |
| 116 | *sample = v.clamp(0, max) as u16; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Write `value` into every sample of an `n` x `n` block of component `c`. |
| 122 | pub fn fill_plane(&mut self, c: usize, x: usize, y: usize, n: usize, value: u16) { |
| 123 | let origin = self.plane_origin(c); |
| 124 | let (stride, plane) = self.plane_view_mut(c); |
| 125 | for j in 0..n { |
| 126 | let start = (y + j - origin) * stride + x; |
| 127 | plane[start..start + n].fill(value); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// The row stride of the chroma planes. |
| 132 | fn chroma_stride(&self) -> usize { |
| 133 | self.width.div_ceil(1 << self.sub_x()) |
| 134 | } |
| 135 | |
| 136 | /// The row stride and the samples of component `c`. |
| 137 | fn plane_view(&self, c: usize) -> (usize, &[u16]) { |
| 138 | match c { |
| 139 | 0 => (self.planes.width, &self.planes.y), |
| 140 | 1 => (self.chroma_stride(), &self.planes.cb), |
| 141 | _ => (self.chroma_stride(), &self.planes.cr), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// Mutable form of [`PicState::plane_view`]. |
| 146 | pub(crate) fn plane_view_mut(&mut self, c: usize) -> (usize, &mut Vec<u16>) { |
| 147 | match c { |
| 148 | 0 => (self.planes.width, &mut self.planes.y), |
| 149 | 1 => (self.chroma_stride(), &mut self.planes.cb), |
| 150 | _ => (self.chroma_stride(), &mut self.planes.cr), |
| 151 | } |
| 152 | } |
| 153 | } |