// AV1 intra prediction (spec 7.11.2) for 8-bit samples, with the intra
// edge filter disabled (enable_intra_edge_filter = 0 in our sequence
// header) and without filter intra / CfL / palette.

///|
fn av1_sm_weights(log2 : Int32) -> FixedArray[Int32] {
  match log2 {
    2 => av1_sm_weights_4
    3 => av1_sm_weights_8
    4 => av1_sm_weights_16
    5 => av1_sm_weights_32
    _ => av1_sm_weights_64
  }
}

///|
/// Scratch edge buffers: `above[i + 1]` is AboveRow[ i ] (i >= -1) and
/// `left[i + 1]` is LeftCol[ i ].
priv struct Av1Edges {
  above : FixedArray[Int32]
  left : FixedArray[Int32]
}

///|
fn Av1Edges::new() -> Av1Edges {
  { above: FixedArray::make(160, 0), left: FixedArray::make(160, 0), }
}

///|
/// Predict the w x h block at (x, y) of `buf` in place from its
/// reconstructed neighbours. `max_x` / `max_y` are the last valid sample
/// coordinates of the (mode-info aligned) plane.
fn av1_predict_intra(
  buf : FixedArray[Int32],
  stride : Int32,
  x : Int32,
  y : Int32,
  have_left : Bool,
  have_above : Bool,
  have_above_right : Bool,
  have_below_left : Bool,
  mode : Int32,
  angle_delta : Int32,
  log2w : Int32,
  log2h : Int32,
  max_x : Int32,
  max_y : Int32,
  e : Av1Edges,
) -> Unit {
  let w = 1 << log2w
  let h = 1 << log2h
  // Entries up to w + h + 16 (the spec's clamped reads define them).
  let n = w + h + 16
  let above = e.above
  let left = e.left
  if !have_above && have_left {
    let v = buf[y * stride + x - 1]
    for i in 0..= 1 && mode <= 8 {
    // Directional.
    let p_angle = av1_mode_to_angle[mode] + angle_delta * 3
    if p_angle == 90 {
      for i in 0..> 1) & 0x1f
        for j in 0..> 6) + j
          buf[base_off + i * stride + j] = if base < max_base_x {
            av1_round2(
              above[base + 1] * (32 - shift) + above[base + 2] * shift,
              5,
            )
          } else {
            above[max_base_x + 1]
          }
        }
      }
    } else if p_angle < 180 {
      let dx = av1_dr_intra_derivative[180 - p_angle]
      let dy = av1_dr_intra_derivative[p_angle - 90]
      for i in 0..> 6
          buf[base_off + i * stride + j] = if base >= -1 {
            let shift = (idx >> 1) & 0x1f
            av1_round2(
              above[base + 1] * (32 - shift) + above[base + 2] * shift,
              5,
            )
          } else {
            let idx2 = (i << 6) - (j + 1) * dy
            let base2 = idx2 >> 6
            let shift2 = (idx2 >> 1) & 0x1f
            av1_round2(
              left[base2 + 1] * (32 - shift2) + left[base2 + 2] * shift2,
              5,
            )
          }
        }
      }
    } else {
      let dy = av1_dr_intra_derivative[270 - p_angle]
      for j in 0..> 1) & 0x1f
        for i in 0..> 6) + i
          buf[base_off + i * stride + j] = av1_round2(
            left[base + 1] * (32 - shift) + left[base + 2] * shift,
            5,
          )
        }
      }
    }
  } else if mode == 9 {
    // SMOOTH
    let wx = av1_sm_weights(log2w)
    let wy = av1_sm_weights(log2h)
    let bl = left[h]
    let tr = above[w]
    for i in 0..> 1
      sum / (w + h)
    } else if have_left {
      let mut sum = 0
      for k in 0..> 1)) >> log2h)
    } else if have_above {
      let mut sum = 0
      for k in 0..> 1)) >> log2w)
    } else {
      128
    }
    for i in 0.. Int32 {
  if v < 0 {
    0
  } else if v > 255 {
    255
  } else {
    v
  }
}