/// AV1 default CDF tables for the tx-mode multi-leaf tile path.
///
/// Transcribed from the AOMedia AV1 specification default probability tables
/// via the go-av1 reference decoder (golang.org/x/image fork in _refs/go-av1);
/// see THIRD_PARTY_NOTICES.md for licence details.

///|
/// Default intra_tx_type CDFs for TX_SET_INTRA_1 (4x4 and 8x8 leaves).
/// Indexed [txSizeSqr row][intraDir 0..12][7-symbol CDF + terminator].
let av1_intra_tx_type_set1_cdf : Array[Array[Array[Int]]] = [
  [
    [1535, 8035, 9461, 12751, 23467, 27825, 32768, 0],
    [564, 3335, 9709, 10870, 18143, 28094, 32768, 0],
    [672, 3247, 3676, 11982, 19415, 23127, 32768, 0],
    [5279, 13885, 15487, 18044, 23527, 30252, 32768, 0],
    [4423, 6074, 7985, 10416, 25693, 29298, 32768, 0],
    [1486, 4241, 9460, 10662, 16456, 27694, 32768, 0],
    [439, 2838, 3522, 6737, 18058, 23754, 32768, 0],
    [1190, 4233, 4855, 11670, 20281, 24377, 32768, 0],
    [1045, 4312, 8647, 10159, 18644, 29335, 32768, 0],
    [202, 3734, 4747, 7298, 17127, 24016, 32768, 0],
    [447, 4312, 6819, 8884, 16010, 23858, 32768, 0],
    [277, 4369, 5255, 8905, 16465, 22271, 32768, 0],
    [3409, 5436, 10599, 15599, 19687, 24040, 32768, 0],
  ],
  [
    [1870, 13742, 14530, 16498, 23770, 27698, 32768, 0],
    [326, 8796, 14632, 15079, 19272, 27486, 32768, 0],
    [484, 7576, 7712, 14443, 19159, 22591, 32768, 0],
    [1126, 15340, 15895, 17023, 20896, 30279, 32768, 0],
    [655, 4854, 5249, 5913, 22099, 27138, 32768, 0],
    [1299, 6458, 8885, 9290, 14851, 25497, 32768, 0],
    [311, 5295, 5552, 6885, 16107, 22672, 32768, 0],
    [883, 8059, 8270, 11258, 17289, 21549, 32768, 0],
    [741, 7580, 9318, 10345, 16688, 29046, 32768, 0],
    [110, 7406, 7915, 9195, 16041, 23329, 32768, 0],
    [363, 7974, 9357, 10673, 15629, 24474, 32768, 0],
    [153, 7647, 8112, 9936, 15307, 19996, 32768, 0],
    [3511, 6332, 11165, 15335, 19323, 23594, 32768, 0],
  ],
]

///|
/// Default intra_tx_type CDFs for TX_SET_INTRA_2 (reduced 4x4/8x8 and 16x16).
/// Indexed [row 0=4x4, 1=8x8, 2=16x16][intraDir 0..12][5-symbol CDF + terminator].
let av1_intra_tx_type_set2_cdf : Array[Array[Array[Int]]] = [
  [
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
  ],
  [
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
    [6554, 13107, 19661, 26214, 32768, 0],
  ],
  [
    [1127, 12814, 22772, 27483, 32768, 0],
    [145, 6761, 11980, 26667, 32768, 0],
    [362, 5887, 11678, 16725, 32768, 0],
    [385, 15213, 18587, 30693, 32768, 0],
    [25, 2914, 23134, 27903, 32768, 0],
    [60, 4470, 11749, 23991, 32768, 0],
    [37, 3332, 14511, 21448, 32768, 0],
    [157, 6320, 13036, 17439, 32768, 0],
    [119, 6719, 12906, 29396, 32768, 0],
    [47, 5537, 12576, 21499, 32768, 0],
    [269, 6076, 11258, 23115, 32768, 0],
    [83, 5615, 12001, 17228, 32768, 0],
    [1968, 5556, 12023, 18547, 32768, 0],
  ],
]

///|
/// Default all_zero (txb_skip) CDFs.
/// Indexed [q context 0..3][tx size context 0..4][neighbour context 0..12].
let av1_txb_skip_cdf : Array[Array[Array[Array[Int]]]] = [
  [
    [
      [31849, 32768, 0],
      [5892, 32768, 0],
      [12112, 32768, 0],
      [21935, 32768, 0],
      [20289, 32768, 0],
      [27473, 32768, 0],
      [32487, 32768, 0],
      [7654, 32768, 0],
      [19473, 32768, 0],
      [29984, 32768, 0],
      [9961, 32768, 0],
      [30242, 32768, 0],
      [32117, 32768, 0],
    ],
    [
      [31548, 32768, 0],
      [1549, 32768, 0],
      [10130, 32768, 0],
      [16656, 32768, 0],
      [18591, 32768, 0],
      [26308, 32768, 0],
      [32537, 32768, 0],
      [5403, 32768, 0],
      [18096, 32768, 0],
      [30003, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [29957, 32768, 0],
      [5391, 32768, 0],
      [18039, 32768, 0],
      [23566, 32768, 0],
      [22431, 32768, 0],
      [25822, 32768, 0],
      [32197, 32768, 0],
      [3778, 32768, 0],
      [15336, 32768, 0],
      [28981, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [17920, 32768, 0],
      [1818, 32768, 0],
      [7282, 32768, 0],
      [25273, 32768, 0],
      [10923, 32768, 0],
      [31554, 32768, 0],
      [32624, 32768, 0],
      [1366, 32768, 0],
      [15628, 32768, 0],
      [30462, 32768, 0],
      [146, 32768, 0],
      [5132, 32768, 0],
      [31657, 32768, 0],
    ],
    [
      [6308, 32768, 0],
      [117, 32768, 0],
      [1638, 32768, 0],
      [2161, 32768, 0],
      [16384, 32768, 0],
      [10923, 32768, 0],
      [30247, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
  ],
  [
    [
      [30371, 32768, 0],
      [7570, 32768, 0],
      [13155, 32768, 0],
      [20751, 32768, 0],
      [20969, 32768, 0],
      [27067, 32768, 0],
      [32013, 32768, 0],
      [5495, 32768, 0],
      [17942, 32768, 0],
      [28280, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [31782, 32768, 0],
      [1836, 32768, 0],
      [10689, 32768, 0],
      [17604, 32768, 0],
      [21622, 32768, 0],
      [27518, 32768, 0],
      [32399, 32768, 0],
      [4419, 32768, 0],
      [16294, 32768, 0],
      [28345, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [31901, 32768, 0],
      [10311, 32768, 0],
      [18047, 32768, 0],
      [24806, 32768, 0],
      [23288, 32768, 0],
      [27914, 32768, 0],
      [32296, 32768, 0],
      [4215, 32768, 0],
      [15756, 32768, 0],
      [28341, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [26726, 32768, 0],
      [1045, 32768, 0],
      [11703, 32768, 0],
      [20590, 32768, 0],
      [18554, 32768, 0],
      [25970, 32768, 0],
      [31938, 32768, 0],
      [5583, 32768, 0],
      [21313, 32768, 0],
      [29390, 32768, 0],
      [641, 32768, 0],
      [22265, 32768, 0],
      [31452, 32768, 0],
    ],
    [
      [26584, 32768, 0],
      [188, 32768, 0],
      [8847, 32768, 0],
      [24519, 32768, 0],
      [22938, 32768, 0],
      [30583, 32768, 0],
      [32608, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
  ],
  [
    [
      [29614, 32768, 0],
      [9068, 32768, 0],
      [12924, 32768, 0],
      [19538, 32768, 0],
      [17737, 32768, 0],
      [24619, 32768, 0],
      [30642, 32768, 0],
      [4119, 32768, 0],
      [16026, 32768, 0],
      [25657, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [31957, 32768, 0],
      [3230, 32768, 0],
      [11153, 32768, 0],
      [18123, 32768, 0],
      [20143, 32768, 0],
      [26536, 32768, 0],
      [31986, 32768, 0],
      [3050, 32768, 0],
      [14603, 32768, 0],
      [25155, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [32363, 32768, 0],
      [10692, 32768, 0],
      [19090, 32768, 0],
      [24357, 32768, 0],
      [24442, 32768, 0],
      [28312, 32768, 0],
      [32169, 32768, 0],
      [3648, 32768, 0],
      [15690, 32768, 0],
      [26815, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [30669, 32768, 0],
      [3832, 32768, 0],
      [11663, 32768, 0],
      [18889, 32768, 0],
      [19782, 32768, 0],
      [23313, 32768, 0],
      [31330, 32768, 0],
      [5124, 32768, 0],
      [18719, 32768, 0],
      [28468, 32768, 0],
      [3082, 32768, 0],
      [20982, 32768, 0],
      [29443, 32768, 0],
    ],
    [
      [28573, 32768, 0],
      [3183, 32768, 0],
      [17802, 32768, 0],
      [25977, 32768, 0],
      [26677, 32768, 0],
      [27832, 32768, 0],
      [32387, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
  ],
  [
    [
      [26887, 32768, 0],
      [6729, 32768, 0],
      [10361, 32768, 0],
      [17442, 32768, 0],
      [15045, 32768, 0],
      [22478, 32768, 0],
      [29072, 32768, 0],
      [2713, 32768, 0],
      [11861, 32768, 0],
      [20773, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [31903, 32768, 0],
      [2044, 32768, 0],
      [7528, 32768, 0],
      [14618, 32768, 0],
      [16182, 32768, 0],
      [24168, 32768, 0],
      [31037, 32768, 0],
      [2786, 32768, 0],
      [11194, 32768, 0],
      [20155, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [32510, 32768, 0],
      [8430, 32768, 0],
      [17318, 32768, 0],
      [24154, 32768, 0],
      [23674, 32768, 0],
      [28789, 32768, 0],
      [32139, 32768, 0],
      [3440, 32768, 0],
      [13117, 32768, 0],
      [22702, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
    [
      [31671, 32768, 0],
      [2056, 32768, 0],
      [11746, 32768, 0],
      [16852, 32768, 0],
      [18635, 32768, 0],
      [24715, 32768, 0],
      [31484, 32768, 0],
      [4656, 32768, 0],
      [16074, 32768, 0],
      [24704, 32768, 0],
      [1806, 32768, 0],
      [14645, 32768, 0],
      [25336, 32768, 0],
    ],
    [
      [31539, 32768, 0],
      [8433, 32768, 0],
      [20576, 32768, 0],
      [27904, 32768, 0],
      [27852, 32768, 0],
      [30026, 32768, 0],
      [32441, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
      [16384, 32768, 0],
    ],
  ],
]

///|
/// Position offsets for the coeff_base 2D context (square transforms).
/// Indexed [4x4, 8x8, 16x16, 32x32, 64x64 rows][min(row,4)][min(col,4)].
let av1_coeff_base_ctx_offset : Array[Array[Array[Int]]] = [
  [
    [0, 1, 6, 6, 0],
    [1, 6, 6, 21, 0],
    [6, 6, 21, 21, 0],
    [6, 21, 21, 21, 0],
    [0, 0, 0, 0, 0],
  ],
  [
    [0, 1, 6, 6, 21],
    [1, 6, 6, 21, 21],
    [6, 6, 21, 21, 21],
    [6, 21, 21, 21, 21],
    [21, 21, 21, 21, 21],
  ],
  [
    [0, 1, 6, 6, 21],
    [1, 6, 6, 21, 21],
    [6, 6, 21, 21, 21],
    [6, 21, 21, 21, 21],
    [21, 21, 21, 21, 21],
  ],
  [
    [0, 1, 6, 6, 21],
    [1, 6, 6, 21, 21],
    [6, 6, 21, 21, 21],
    [6, 21, 21, 21, 21],
    [21, 21, 21, 21, 21],
  ],
  [
    [0, 1, 6, 6, 21],
    [1, 6, 6, 21, 21],
    [6, 6, 21, 21, 21],
    [6, 21, 21, 21, 21],
    [21, 21, 21, 21, 21],
  ],
]

///|
/// Per-decode copies of the adaptive tx entropy tables. The default tables
/// above must never be adapted in place: a decode that mutated them would
/// corrupt every later decode in the same process. One instance is created
/// per tile decode and shared by every symbol read within it, so the CDFs
/// adapt across leaves exactly as the bitstream expects.
priv struct Av1TxEntTables {
  txb_skip : Array[Array[Array[Int]]]
  tx_set1 : Array[Array[Array[Int]]]
  tx_set2 : Array[Array[Array[Int]]]
}

///|
/// Builds the per-decode table set for one effective q context. Only the
/// active qctx slice of the all_zero table is retained.
fn av1_tx_ent_tables(base_q_idx : Int) -> Av1TxEntTables {
  let qctx = av1_dc_q_context(base_q_idx)
  let txb : Array[Array[Array[Int]]] = []
  for tx in 0..<5 {
    let rows : Array[Array[Int]] = []
    for ctx in 0..<13 {
      rows.push(av1_txb_skip_cdf[qctx][tx][ctx].copy())
    }
    txb.push(rows)
  }
  let set1 : Array[Array[Array[Int]]] = []
  for row in 0..<2 {
    let dirs : Array[Array[Int]] = []
    for dir in 0..<13 {
      dirs.push(av1_intra_tx_type_set1_cdf[row][dir].copy())
    }
    set1.push(dirs)
  }
  let set2 : Array[Array[Array[Int]]] = []
  for row in 0..<3 {
    let dirs : Array[Array[Int]] = []
    for dir in 0..<13 {
      dirs.push(av1_intra_tx_type_set2_cdf[row][dir].copy())
    }
    set2.push(dirs)
  }
  { txb_skip: txb, tx_set1: set1, tx_set2: set2, }
}