// Port of jxl-render features/noise.rs (noise synthesis).

///|
/// Padding for 5x5 kernel convolution step
let noise_padding : Int32 = 2

///|
/// `render_noise` on plain values.
///
/// - `frame_width`/`frame_height`/`group_dim`: from the frame header (the
///   dimensions after upsampling, see `init_noise`).
/// - `grid_x`/`grid_y`/`grid_b`: the colour channels of the image, whose
///   sample (0, 0) is frame pixel (`left`, `top`).
/// - `left`/`top`/`width`/`height`: `actual_region`, i.e. the image region
///   intersected with the frame and downsampled with the channel shift.
/// - `lut`: `NoiseParameters::lut` (8 values).
pub fn render_noise_raw(
  frame_width : Int32,
  frame_height : Int32,
  group_dim : Int32,
  visible_frames_num : Int32,
  invisible_frames_num : Int32,
  base_correlations_xb : (Float, Float)?,
  grid_x : @grid.Grid[Float],
  grid_y : @grid.Grid[Float],
  grid_b : @grid.Grid[Float],
  left : Int32,
  top : Int32,
  width : Int32,
  height : Int32,
  lut : FixedArray[Float],
  tracker : @grid.AllocTracker?,
) -> Unit raise @bitstream.JxlError {
  let (corr_x, corr_b) = match base_correlations_xb {
    Some(v) => v
    None => ((0.0 : Float), (1.0 : Float))
  }
  let noise_buffer = init_noise(
    visible_frames_num, invisible_frames_num, frame_width, frame_height, group_dim,
    tracker,
  )
  let lut9 = FixedArray::make(9, (0.0 : Float))
  for i in 0..<8 {
    lut9[i] = lut[i]
  }
  lut9[8] = lut[7]
  let bx = grid_x.buf
  let by = grid_y.buf
  let bb = grid_b.buf
  let nxb = noise_buffer[0].buf
  let nyb = noise_buffer[1].buf
  let nbb = noise_buffer[2].buf
  let nstride = noise_buffer[0].width
  for fy in 0.. Float {
  if v > 0.0 {
    v
  } else {
    0.0
  }
}

///|
fn noise_min7(v : Int32) -> Int32 {
  if v < 7 {
    v
  } else {
    7
  }
}

///|
/// Generates the convolved noise images of the whole frame, one per channel.
fn init_noise(
  visible_frames : Int32,
  invisible_frames : Int32,
  frame_width : Int32,
  frame_height : Int32,
  group_dim : Int32,
  tracker : @grid.AllocTracker?,
) -> Array[@grid.Grid[Float]] raise @bitstream.JxlError {
  let seed0 = rng_seed0(visible_frames, invisible_frames)

  // We use header.width and header.height because
  // these are the dimensions after upsampling (the "actual" frame size),
  // and noise synthesis is done after upsampling.
  let width = frame_width
  let height = frame_height
  let groups_per_row = (width + group_dim - 1) / group_dim
  let num_groups = groups_per_row * ((height + group_dim - 1) / group_dim)
  let noise_groups : Array[NoiseGroup] = []
  for group_idx in 0..= 0 && y >= 0 && x < groups_per_row {
          let gi = y * groups_per_row + x
          if gi < noise_groups.length() {
            adjacent_groups[idx] = Some(
              noise_groups[gi].as_subgrid(channel_idx),
            )
          }
        }
      }
      convolve_fill(out_subgrid, adjacent_groups, tracker)
    }
  }
  // The noise groups are dropped here upstream, returning their memory.
  if tracker is Some(t) {
    for g in noise_groups {
      t.free(g.stride * g.height * 3, 4)
    }
  }
  convolved
}

///|
/// Seed for `XorShift128Plus` from the number of ‘visible’ frames decoded so far
/// and the number of ‘invisible’ frames since the previous visible frame.
fn rng_seed0(visible_frames : Int32, invisible_frames : Int32) -> UInt64 {
  (visible_frames.to_uint64() << 32) + invisible_frames.to_uint64()
}

///|
/// Seed for `XorShift128Plus` from the coordinates of the top-left pixel of the
/// group within the frame.
fn rng_seed1(x0 : Int32, y0 : Int32) -> UInt64 {
  (x0.to_uint64() << 32) + y0.to_uint64()
}

///|
priv struct NoiseGroup {
  buf : FixedArray[FixedArray[Float]]
  width : Int32
  height : Int32
  stride : Int32
}

///|
fn NoiseGroup::new(
  width : Int32,
  height : Int32,
  seed0 : UInt64,
  seed1 : UInt64,
  tracker : @grid.AllocTracker?,
) -> NoiseGroup raise @bitstream.JxlError {
  let width_n2 = (width + noise_n * 2 - 1) / (noise_n * 2)
  let stride = width_n2 * noise_n * 2
  let elems = stride * height * 3
  if tracker is Some(t) {
    t.alloc(elems, 4)
  }
  let rng = XorShift128Plus::new(seed0, seed1)
  let buf : FixedArray[FixedArray[Float]] = FixedArray::make(3, [])
  let num_iters = width_n2 * height
  let bits = FixedArray::make(noise_n * 2, (0 : UInt32))
  for c in 0..<3 {
    let b = FixedArray::make(num_iters * noise_n * 2, (0.0 : Float))
    for i in 0..> 9) | 0x3f800000,
        )
      }
    }
    buf[c] = b
  }
  { buf, width, height, stride, }
}

///|
fn NoiseGroup::as_subgrid(
  self : NoiseGroup,
  channel_idx : Int32,
) -> @grid.SubGrid[Float] {
  @grid.SubGrid::from_buf(
    self.buf[channel_idx],
    0,
    self.width,
    self.height,
    self.stride,
  )
}

///|
/// Offset of row `y` of `g` in its buffer.
fn noise_row(g : @grid.SubGrid[Float], y : Int32) -> Int32 {
  guard y >= 0 && y < g.height else { abort("noise: row out of bounds") }
  g.offset + y * g.stride
}

///|
fn convolve_fill(
  out : @grid.SubGrid[Float],
  adjacent_groups : FixedArray[@grid.SubGrid[Float]?],
  tracker : @grid.AllocTracker?,
) -> Unit raise @bitstream.JxlError {
  let this = adjacent_groups[4].unwrap()
  let width = out.width
  let height = out.height
  guard this.width == width && this.height == height else {
    abort("noise: group size mismatch")
  }
  let rows = @grid.Grid::with_alloc_tracker(
    width + noise_padding * 2,
    1 + noise_padding * 2,
    (0.0 : Float),
    tracker,
  )
  let input_width = rows.width
  let rbuf = rows.buf
  if adjacent_groups[1] is Some(c) {
    let l = adjacent_groups[0]
    let r = adjacent_groups[2]
    for offset_y in -2..<0 {
      fill_padded_row(
        rbuf,
        (2 + offset_y) * input_width,
        input_width,
        c,
        c.height + offset_y,
        l.map(fn(l) { (l, l.height + offset_y) }),
        r.map(fn(r) { (r, r.height + offset_y) }),
      )
    }
  } else if height >= 2 {
    let c = this
    let l = adjacent_groups[3]
    let r = adjacent_groups[5]
    for offset_y in -2..<0 {
      let y = -(offset_y + 1)
      fill_padded_row(
        rbuf,
        (2 + offset_y) * input_width,
        input_width,
        c,
        y,
        l.map(fn(l) { (l, y) }),
        r.map(fn(r) { (r, y) }),
      )
    }
  } else {
    let c = this
    let l = adjacent_groups[3]
    let r = adjacent_groups[5]
    for y in 0..<2 {
      fill_padded_row(
        rbuf,
        y * input_width,
        input_width,
        c,
        0,
        l.map(fn(l) { (l, 0) }),
        r.map(fn(r) { (r, 0) }),
      )
    }
  }
  for y in 0..<3 {
    fill_once(rbuf, (2 + y) * input_width, input_width, y, adjacent_groups)
  }
  let obuf = out.buf
  for y in 0.. Unit {
  let this = adjacent_groups[4].unwrap()
  let height = this.height
  let (source_y, c, l, r) = if fill_y >= height {
    (
      fill_y - height,
      adjacent_groups[7],
      adjacent_groups[6],
      adjacent_groups[8],
    )
  } else {
    (fill_y, adjacent_groups[4], adjacent_groups[3], adjacent_groups[5])
  }
  let (source_y, c, l, r) = match c {
    Some(c) => (source_y, c, l, r)
    None =>
      if source_y <= height - 1 {
        (height - 1 - source_y, this, adjacent_groups[3], adjacent_groups[5])
      } else {
        let dy = source_y - height + 1
        match adjacent_groups[1] {
          Some(c) => (c.height - dy, c, adjacent_groups[0], adjacent_groups[2])
          None => (0, this, adjacent_groups[3], adjacent_groups[5])
        }
      }
  }
  fill_padded_row(
    out,
    out_off,
    out_len,
    c,
    source_y,
    l.map(fn(l) { (l, source_y) }),
    r.map(fn(r) { (r, source_y) }),
  )
}

///|
/// `fill_padded_row`: `this`, `left` and `right` are rows given as a grid and
/// a row index.
fn fill_padded_row(
  out : FixedArray[Float],
  out_off : Int32,
  out_len : Int32,
  this : @grid.SubGrid[Float],
  this_y : Int32,
  left : (@grid.SubGrid[Float], Int32)?,
  right : (@grid.SubGrid[Float], Int32)?,
) -> Unit {
  let this_len = this.width
  guard out_len == this_len + noise_padding * 2 else {
    abort("noise: padded row length mismatch")
  }
  let tb = this.buf
  let trow = noise_row(this, this_y)
  match left {
    Some((l, ly)) => {
      let lrow = noise_row(l, ly)
      out[out_off] = l.buf[lrow + l.width - 2]
      out[out_off + 1] = l.buf[lrow + l.width - 1]
    }
    None =>
      if this_len >= noise_padding {
        out[out_off] = tb[trow + 1]
        out[out_off + 1] = tb[trow]
      } else {
        out[out_off] = tb[trow]
        out[out_off + 1] = tb[trow]
      }
  }
  for i in 0.. {
      let rrow = noise_row(r, ry)
      if r.width >= noise_padding {
        out[out_off + out_len - 2] = r.buf[rrow]
        out[out_off + out_len - 1] = r.buf[rrow + 1]
      } else {
        out[out_off + out_len - 2] = r.buf[rrow]
        out[out_off + out_len - 1] = r.buf[rrow]
      }
    }
    None => {
      out[out_off + out_len - 2] = out[out_off + out_len - 3]
      out[out_off + out_len - 1] = out[out_off + out_len - 4]
    }
  }
}

///|
let noise_n : Int32 = 8

///|
/// Shift-register pseudo-random number generator
priv struct XorShift128Plus {
  s0 : FixedArray[UInt64]
  s1 : FixedArray[UInt64]
}

///|
/// Initialize a new XorShift128+ PRNG.
fn XorShift128Plus::new(seed0 : UInt64, seed1 : UInt64) -> XorShift128Plus {
  let s0 = FixedArray::make(noise_n, 0UL)
  let s1 = FixedArray::make(noise_n, 0UL)
  s0[0] = split_mix_64(seed0 + 0x9E3779B97F4A7C15UL)
  s1[0] = split_mix_64(seed1 + 0x9E3779B97F4A7C15UL)
  for i in 1.. Unit {
  for i in 0..> 32).to_uint()
  }
}

///|
/// One lane of `fill_batch`.
fn XorShift128Plus::next_u64(self : XorShift128Plus, i : Int32) -> UInt64 {
  let mut s1 = self.s0[i]
  let s0 = self.s1[i]
  let ret = s1 + s0
  self.s0[i] = s0
  s1 = s1 ^ (s1 << 23)
  self.s1[i] = s1 ^ (s0 ^ (s1 >> 18) ^ (s0 >> 5))
  ret
}

///|
/// Pseudo-random number generator used to calculate initial state of
/// `XorShift128Plus`
fn split_mix_64(z : UInt64) -> UInt64 {
  let z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL
  let z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL
  z ^ (z >> 31)
}