///|
/// AV1 EOB position CDF tables for transform areas below 1024.
/// Transcribed from go-av1 generated AOMedia AV1 tables; see THIRD_PARTY_NOTICES.md.
let defaultEobPt16Cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [[840, 1039, 1980, 4895, 32768, 0], [370, 671, 1883, 4471, 32768, 0]],
    [[3247, 4950, 9688, 14563, 32768, 0], [1904, 3354, 7763, 14647, 32768, 0]],
  ],
  [
    [[2125, 2551, 5165, 8946, 32768, 0], [513, 765, 1859, 6339, 32768, 0]],
    [[7637, 9498, 14259, 19108, 32768, 0], [2497, 4096, 8866, 16993, 32768, 0]],
  ],
  [
    [[4016, 4897, 8881, 14968, 32768, 0], [716, 1105, 2646, 10056, 32768, 0]],
    [
      [11139, 13270, 18241, 23566, 32768, 0],
      [3192, 5032, 10297, 19755, 32768, 0],
    ],
  ],
  [
    [[6708, 8958, 14746, 22133, 32768, 0], [1222, 2074, 4783, 15410, 32768, 0]],
    [
      [19575, 21766, 26044, 29709, 32768, 0],
      [7297, 10767, 19273, 28194, 32768, 0],
    ],
  ],
]

///|
let defaultEobPt32Cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [
      [400, 520, 977, 2102, 6542, 32768, 0],
      [210, 405, 1315, 3326, 7537, 32768, 0],
    ],
    [
      [2636, 4273, 7588, 11794, 20401, 32768, 0],
      [1786, 3179, 6902, 11357, 19054, 32768, 0],
    ],
  ],
  [
    [
      [989, 1249, 2019, 4151, 10785, 32768, 0],
      [313, 441, 1099, 2917, 8562, 32768, 0],
    ],
    [
      [8394, 10352, 13932, 18855, 26014, 32768, 0],
      [2578, 4124, 8181, 13670, 24234, 32768, 0],
    ],
  ],
  [
    [
      [2515, 3003, 4452, 8162, 16041, 32768, 0],
      [574, 821, 1836, 5089, 13128, 32768, 0],
    ],
    [
      [13468, 16303, 20361, 25105, 29281, 32768, 0],
      [3542, 5502, 10415, 16760, 25644, 32768, 0],
    ],
  ],
  [
    [
      [4617, 5709, 8446, 13584, 23135, 32768, 0],
      [1156, 1702, 3675, 9274, 20539, 32768, 0],
    ],
    [
      [22086, 24282, 27010, 29770, 31743, 32768, 0],
      [7699, 10897, 20891, 26926, 31628, 32768, 0],
    ],
  ],
]

///|
let defaultEobPt64Cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [
      [329, 498, 1101, 1784, 3265, 7758, 32768, 0],
      [335, 730, 1459, 5494, 8755, 12997, 32768, 0],
    ],
    [
      [3505, 5304, 10086, 13814, 17684, 23370, 32768, 0],
      [1563, 2700, 4876, 10911, 14706, 22480, 32768, 0],
    ],
  ],
  [
    [
      [1260, 1446, 2253, 3712, 6652, 13369, 32768, 0],
      [401, 605, 1029, 2563, 5845, 12626, 32768, 0],
    ],
    [
      [8609, 10612, 14624, 18714, 22614, 29024, 32768, 0],
      [1923, 3127, 5867, 9703, 14277, 27100, 32768, 0],
    ],
  ],
  [
    [
      [2374, 2772, 4583, 7276, 12288, 19706, 32768, 0],
      [497, 810, 1315, 3000, 7004, 15641, 32768, 0],
    ],
    [
      [15050, 17126, 21410, 24886, 28156, 30726, 32768, 0],
      [4034, 6290, 10235, 14982, 21214, 28491, 32768, 0],
    ],
  ],
  [
    [
      [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0],
      [1289, 2320, 3971, 7926, 14153, 24291, 32768, 0],
    ],
    [
      [24212, 25708, 28268, 30035, 31307, 32049, 32768, 0],
      [8726, 12378, 19409, 26450, 30038, 32462, 32768, 0],
    ],
  ],
]

///|
let defaultEobPt128Cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [
      [219, 482, 1140, 2091, 3680, 6028, 12586, 32768, 0],
      [371, 699, 1254, 4830, 9479, 12562, 17497, 32768, 0],
    ],
    [
      [5245, 7456, 12880, 15852, 20033, 23932, 27608, 32768, 0],
      [2054, 3472, 5869, 14232, 18242, 20590, 26752, 32768, 0],
    ],
  ],
  [
    [
      [685, 933, 1488, 2714, 4766, 8562, 19254, 32768, 0],
      [217, 352, 618, 2303, 5261, 9969, 17472, 32768, 0],
    ],
    [
      [8045, 11200, 15497, 19595, 23948, 27408, 30938, 32768, 0],
      [2310, 4160, 7471, 14997, 17931, 20768, 30240, 32768, 0],
    ],
  ],
  [
    [
      [1366, 1738, 2527, 5016, 9355, 15797, 24643, 32768, 0],
      [354, 558, 944, 2760, 7287, 14037, 21779, 32768, 0],
    ],
    [
      [13627, 16246, 20173, 24429, 27948, 30415, 31863, 32768, 0],
      [6275, 9889, 14769, 23164, 27988, 30493, 32272, 32768, 0],
    ],
  ],
  [
    [
      [3472, 4885, 7489, 12481, 18517, 24536, 29635, 32768, 0],
      [886, 1731, 3271, 8469, 15569, 22126, 28383, 32768, 0],
    ],
    [
      [24313, 26062, 28385, 30107, 31217, 31898, 32345, 32768, 0],
      [9165, 13282, 21150, 30286, 31894, 32571, 32712, 32768, 0],
    ],
  ],
]

///|
let defaultEobPt256Cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [
      [310, 584, 1887, 3589, 6168, 8611, 11352, 15652, 32768, 0],
      [998, 1850, 2998, 5604, 17341, 19888, 22899, 25583, 32768, 0],
    ],
    [
      [2520, 3240, 5952, 8870, 12577, 17558, 19954, 24168, 32768, 0],
      [2203, 4130, 7435, 10739, 20652, 23681, 25609, 27261, 32768, 0],
    ],
  ],
  [
    [
      [1448, 2109, 4151, 6263, 9329, 13260, 17944, 23300, 32768, 0],
      [399, 1019, 1749, 3038, 10444, 15546, 22739, 27294, 32768, 0],
    ],
    [
      [6402, 8148, 12623, 15072, 18728, 22847, 26447, 29377, 32768, 0],
      [1674, 3252, 5734, 10159, 22397, 23802, 24821, 30940, 32768, 0],
    ],
  ],
  [
    [
      [3089, 3920, 6038, 9460, 14266, 19881, 25766, 29176, 32768, 0],
      [1084, 2358, 3488, 5122, 11483, 18103, 26023, 29799, 32768, 0],
    ],
    [
      [11514, 13794, 17480, 20754, 24361, 27378, 29492, 31277, 32768, 0],
      [6571, 9610, 15516, 21826, 29092, 30829, 31842, 32708, 32768, 0],
    ],
  ],
  [
    [
      [5348, 7113, 11820, 15924, 22106, 26777, 30334, 31757, 32768, 0],
      [2453, 4474, 6307, 8777, 16474, 22975, 29000, 31547, 32768, 0],
    ],
    [
      [23110, 24597, 27140, 28894, 30167, 30927, 31392, 32094, 32768, 0],
      [9998, 17661, 25178, 28097, 31308, 32038, 32403, 32695, 32768, 0],
    ],
  ],
]

///|
let defaultEobPt512Cdf : Array[Array[Array[Int]]] = [
  [
    [641, 983, 3707, 5430, 10234, 14958, 18788, 23412, 26061, 32768, 0],
    [5095, 6446, 9996, 13354, 16017, 17986, 20919, 26129, 29140, 32768, 0],
  ],
  [
    [1230, 2278, 5035, 7776, 11871, 15346, 19590, 24584, 28749, 32768, 0],
    [7265, 9979, 15819, 19250, 21780, 23846, 26478, 28396, 31811, 32768, 0],
  ],
  [
    [2624, 3936, 6480, 9686, 13979, 17726, 23267, 28410, 31078, 32768, 0],
    [12015, 14769, 19588, 22052, 24222, 25812, 27300, 29219, 32114, 32768, 0],
  ],
  [
    [5927, 7809, 10923, 14597, 19439, 24135, 28456, 31142, 32060, 32768, 0],
    [21093, 23043, 25742, 27658, 29097, 29716, 30073, 30820, 31956, 32768, 0],
  ],
]

///|
pub fn av1_coeff_eob_small_default(
  kind : Int,
  q_context : Int,
  plane : Int,
  tx_class : Int,
) -> Array[Int] {
  match kind {
    0 => defaultEobPt16Cdf[q_context][plane][tx_class].copy()
    1 => defaultEobPt32Cdf[q_context][plane][tx_class].copy()
    2 => defaultEobPt64Cdf[q_context][plane][tx_class].copy()
    3 => defaultEobPt128Cdf[q_context][plane][tx_class].copy()
    4 => defaultEobPt256Cdf[q_context][plane][tx_class].copy()
    5 => defaultEobPt512Cdf[q_context][plane].copy()
    _ => abort("invalid AV1 EOB table kind")
  }
}

///|
test "AV1 small EOB tables expose valid CDF leaves" {
  for kind in 0..<6 {
    let cdf = av1_coeff_eob_small_default(kind, 0, 0, 0)
    assert_eq(cdf[cdf.length() - 2], 32768)
  }
}