TreeParser.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include "Enums.h"
  9. #include "LookupTables.h"
  10. #include "Parser.h"
  11. #include "TreeParser.h"
  12. namespace Video::VP9 {
  13. template<typename T>
  14. ErrorOr<T> TreeParser::parse_tree(SyntaxElementType type)
  15. {
  16. auto tree_selection = select_tree(type);
  17. int value;
  18. if (tree_selection.is_single_value()) {
  19. value = tree_selection.single_value();
  20. } else {
  21. auto tree = tree_selection.tree();
  22. int n = 0;
  23. do {
  24. n = tree[n + TRY(m_decoder.m_bit_stream->read_bool(select_tree_probability(type, n >> 1)))];
  25. } while (n > 0);
  26. value = -n;
  27. }
  28. count_syntax_element(type, value);
  29. return static_cast<T>(value);
  30. }
  31. template ErrorOr<int> TreeParser::parse_tree(SyntaxElementType);
  32. template ErrorOr<bool> TreeParser::parse_tree(SyntaxElementType);
  33. template ErrorOr<u8> TreeParser::parse_tree(SyntaxElementType);
  34. template ErrorOr<u32> TreeParser::parse_tree(SyntaxElementType);
  35. template ErrorOr<PredictionMode> TreeParser::parse_tree(SyntaxElementType);
  36. template ErrorOr<TXSize> TreeParser::parse_tree(SyntaxElementType);
  37. template ErrorOr<InterpolationFilter> TreeParser::parse_tree(SyntaxElementType);
  38. template ErrorOr<ReferenceMode> TreeParser::parse_tree(SyntaxElementType);
  39. template ErrorOr<Token> TreeParser::parse_tree(SyntaxElementType);
  40. template ErrorOr<MvClass> TreeParser::parse_tree(SyntaxElementType);
  41. template ErrorOr<MvJoint> TreeParser::parse_tree(SyntaxElementType);
  42. template<typename OutputType>
  43. inline ErrorOr<OutputType> parse_tree_new(BitStream& bit_stream, TreeParser::TreeSelection tree_selection, Function<u8(u8)> const& probability_getter)
  44. {
  45. if (tree_selection.is_single_value())
  46. return static_cast<OutputType>(tree_selection.single_value());
  47. int const* tree = tree_selection.tree();
  48. int n = 0;
  49. do {
  50. u8 node = n >> 1;
  51. n = tree[n + TRY(bit_stream.read_bool(probability_getter(node)))];
  52. } while (n > 0);
  53. return static_cast<OutputType>(-n);
  54. }
  55. inline void increment_counter(u8& counter)
  56. {
  57. counter = min(static_cast<u32>(counter) + 1, 255);
  58. }
  59. ErrorOr<Partition> TreeParser::parse_partition(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, bool has_rows, bool has_columns, BlockSubsize block_subsize, u8 num_8x8, Vector<u8> const& above_partition_context, Vector<u8> const& left_partition_context, u32 row, u32 column, bool frame_is_intra)
  60. {
  61. // Tree array
  62. TreeParser::TreeSelection tree = { PartitionSplit };
  63. if (has_rows && has_columns)
  64. tree = { partition_tree };
  65. else if (has_rows)
  66. tree = { rows_partition_tree };
  67. else if (has_columns)
  68. tree = { cols_partition_tree };
  69. // Probability array
  70. u32 above = 0;
  71. u32 left = 0;
  72. auto bsl = mi_width_log2_lookup[block_subsize];
  73. auto block_offset = mi_width_log2_lookup[Block_64x64] - bsl;
  74. for (auto i = 0; i < num_8x8; i++) {
  75. above |= above_partition_context[column + i];
  76. left |= left_partition_context[row + i];
  77. }
  78. above = (above & (1 << block_offset)) > 0;
  79. left = (left & (1 << block_offset)) > 0;
  80. auto context = bsl * 4 + left * 2 + above;
  81. u8 const* probabilities = frame_is_intra ? probability_table.kf_partition_probs()[context] : probability_table.partition_probs()[context];
  82. Function<u8(u8)> probability_getter = [&](u8 node) {
  83. if (has_rows && has_columns)
  84. return probabilities[node];
  85. if (has_columns)
  86. return probabilities[1];
  87. return probabilities[2];
  88. };
  89. auto value = TRY(parse_tree_new<Partition>(bit_stream, tree, probability_getter));
  90. increment_counter(counter.m_counts_partition[context][value]);
  91. return value;
  92. }
  93. ErrorOr<PredictionMode> TreeParser::parse_default_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, BlockSubsize mi_size, Optional<Array<PredictionMode, 4> const&> above_context, Optional<Array<PredictionMode, 4> const&> left_context, PredictionMode block_sub_modes[4], u8 index_x, u8 index_y)
  94. {
  95. // FIXME: This should use a struct for the above and left contexts.
  96. // Tree
  97. TreeParser::TreeSelection tree = { intra_mode_tree };
  98. // Probabilities
  99. PredictionMode above_mode, left_mode;
  100. if (mi_size >= Block_8x8) {
  101. above_mode = above_context.has_value() ? above_context.value()[2] : PredictionMode::DcPred;
  102. left_mode = left_context.has_value() ? left_context.value()[1] : PredictionMode::DcPred;
  103. } else {
  104. if (index_y > 0)
  105. above_mode = block_sub_modes[index_x];
  106. else
  107. above_mode = above_context.has_value() ? above_context.value()[2 + index_x] : PredictionMode::DcPred;
  108. if (index_x > 0)
  109. left_mode = block_sub_modes[index_y << 1];
  110. else
  111. left_mode = left_context.has_value() ? left_context.value()[1 + (index_y << 1)] : PredictionMode::DcPred;
  112. }
  113. u8 const* probabilities = probability_table.kf_y_mode_probs()[to_underlying(above_mode)][to_underlying(left_mode)];
  114. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  115. // Default intra mode is not counted.
  116. return value;
  117. }
  118. ErrorOr<PredictionMode> TreeParser::parse_default_uv_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, PredictionMode y_mode)
  119. {
  120. // Tree
  121. TreeParser::TreeSelection tree = { intra_mode_tree };
  122. // Probabilities
  123. u8 const* probabilities = probability_table.kf_uv_mode_prob()[to_underlying(y_mode)];
  124. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  125. // Default UV mode is not counted.
  126. return value;
  127. }
  128. ErrorOr<PredictionMode> TreeParser::parse_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, BlockSubsize mi_size)
  129. {
  130. // Tree
  131. TreeParser::TreeSelection tree = { intra_mode_tree };
  132. // Probabilities
  133. auto context = size_group_lookup[mi_size];
  134. u8 const* probabilities = probability_table.y_mode_probs()[context];
  135. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  136. increment_counter(counter.m_counts_intra_mode[context][to_underlying(value)]);
  137. return value;
  138. }
  139. ErrorOr<PredictionMode> TreeParser::parse_sub_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter)
  140. {
  141. // Tree
  142. TreeParser::TreeSelection tree = { intra_mode_tree };
  143. // Probabilities
  144. u8 const* probabilities = probability_table.y_mode_probs()[0];
  145. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  146. increment_counter(counter.m_counts_intra_mode[0][to_underlying(value)]);
  147. return value;
  148. }
  149. ErrorOr<PredictionMode> TreeParser::parse_uv_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, PredictionMode y_mode)
  150. {
  151. // Tree
  152. TreeParser::TreeSelection tree = { intra_mode_tree };
  153. // Probabilities
  154. u8 const* probabilities = probability_table.uv_mode_probs()[to_underlying(y_mode)];
  155. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  156. increment_counter(counter.m_counts_uv_mode[to_underlying(y_mode)][to_underlying(value)]);
  157. return value;
  158. }
  159. ErrorOr<u8> TreeParser::parse_segment_id(BitStream& bit_stream, u8 const probabilities[7])
  160. {
  161. auto value = TRY(parse_tree_new<u8>(bit_stream, { segment_tree }, [&](u8 node) { return probabilities[node]; }));
  162. // Segment ID is not counted.
  163. return value;
  164. }
  165. ErrorOr<bool> TreeParser::parse_segment_id_predicted(BitStream& bit_stream, u8 const probabilities[3], u8 above_seg_pred_context, u8 left_seg_pred_context)
  166. {
  167. auto context = left_seg_pred_context + above_seg_pred_context;
  168. auto value = TRY(parse_tree_new<bool>(bit_stream, { binary_tree }, [&](u8) { return probabilities[context]; }));
  169. // Segment ID prediction is not counted.
  170. return value;
  171. }
  172. ErrorOr<PredictionMode> TreeParser::parse_inter_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 mode_context_for_ref_frame_0)
  173. {
  174. // Tree
  175. TreeParser::TreeSelection tree = { inter_mode_tree };
  176. // Probabilities
  177. u8 const* probabilities = probability_table.inter_mode_probs()[mode_context_for_ref_frame_0];
  178. auto value = TRY(parse_tree_new<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  179. increment_counter(counter.m_counts_inter_mode[mode_context_for_ref_frame_0][to_underlying(value) - to_underlying(PredictionMode::NearestMv)]);
  180. return value;
  181. }
  182. ErrorOr<InterpolationFilter> TreeParser::parse_interpolation_filter(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, Optional<ReferenceFrameType> above_ref_frame, Optional<ReferenceFrameType> left_ref_frame, Optional<InterpolationFilter> above_interpolation_filter, Optional<InterpolationFilter> left_interpolation_filter)
  183. {
  184. // FIXME: Above and left context should be provided by a struct.
  185. // Tree
  186. TreeParser::TreeSelection tree = { interp_filter_tree };
  187. // Probabilities
  188. // NOTE: SWITCHABLE_FILTERS is not used in the spec for this function. Therefore, the number
  189. // was demystified by referencing the reference codec libvpx:
  190. // https://github.com/webmproject/libvpx/blob/705bf9de8c96cfe5301451f1d7e5c90a41c64e5f/vp9/common/vp9_pred_common.h#L69
  191. u8 left_interp = (left_ref_frame.has_value() && left_ref_frame.value() > IntraFrame)
  192. ? left_interpolation_filter.value()
  193. : SWITCHABLE_FILTERS;
  194. u8 above_interp = (above_ref_frame.has_value() && above_ref_frame.value() > IntraFrame)
  195. ? above_interpolation_filter.value()
  196. : SWITCHABLE_FILTERS;
  197. u8 context = SWITCHABLE_FILTERS;
  198. if (above_interp == left_interp || above_interp == SWITCHABLE_FILTERS)
  199. context = left_interp;
  200. else if (left_interp == SWITCHABLE_FILTERS)
  201. context = above_interp;
  202. u8 const* probabilities = probability_table.interp_filter_probs()[context];
  203. auto value = TRY(parse_tree_new<InterpolationFilter>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  204. increment_counter(counter.m_counts_interp_filter[context][to_underlying(value)]);
  205. return value;
  206. }
  207. /*
  208. * Select a tree value based on the type of syntax element being parsed, as well as some parser state, as specified in section 9.3.1
  209. */
  210. TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type)
  211. {
  212. switch (type) {
  213. case SyntaxElementType::Skip:
  214. case SyntaxElementType::IsInter:
  215. case SyntaxElementType::CompMode:
  216. case SyntaxElementType::CompRef:
  217. case SyntaxElementType::SingleRefP1:
  218. case SyntaxElementType::SingleRefP2:
  219. case SyntaxElementType::MVSign:
  220. case SyntaxElementType::MVClass0Bit:
  221. case SyntaxElementType::MVBit:
  222. case SyntaxElementType::MoreCoefs:
  223. return { binary_tree };
  224. case SyntaxElementType::TXSize:
  225. if (m_decoder.m_max_tx_size == TX_32x32)
  226. return { tx_size_32_tree };
  227. if (m_decoder.m_max_tx_size == TX_16x16)
  228. return { tx_size_16_tree };
  229. return { tx_size_8_tree };
  230. case SyntaxElementType::MVJoint:
  231. return { mv_joint_tree };
  232. case SyntaxElementType::MVClass:
  233. return { mv_class_tree };
  234. case SyntaxElementType::MVClass0FR:
  235. case SyntaxElementType::MVFR:
  236. return { mv_fr_tree };
  237. case SyntaxElementType::MVClass0HP:
  238. case SyntaxElementType::MVHP:
  239. if (m_decoder.m_use_hp)
  240. return { binary_tree };
  241. return { 1 };
  242. case SyntaxElementType::Token:
  243. return { token_tree };
  244. default:
  245. break;
  246. }
  247. VERIFY_NOT_REACHED();
  248. }
  249. /*
  250. * Select a probability with which to read a boolean when decoding a tree, as specified in section 9.3.2
  251. */
  252. u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node)
  253. {
  254. switch (type) {
  255. case SyntaxElementType::Skip:
  256. return calculate_skip_probability();
  257. case SyntaxElementType::IsInter:
  258. return calculate_is_inter_probability();
  259. case SyntaxElementType::CompMode:
  260. return calculate_comp_mode_probability();
  261. case SyntaxElementType::CompRef:
  262. return calculate_comp_ref_probability();
  263. case SyntaxElementType::SingleRefP1:
  264. return calculate_single_ref_p1_probability();
  265. case SyntaxElementType::SingleRefP2:
  266. return calculate_single_ref_p2_probability();
  267. case SyntaxElementType::MVSign:
  268. return m_decoder.m_probability_tables->mv_sign_prob()[m_mv_component];
  269. case SyntaxElementType::MVClass0Bit:
  270. return m_decoder.m_probability_tables->mv_class0_bit_prob()[m_mv_component];
  271. case SyntaxElementType::MVBit:
  272. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  273. return m_decoder.m_probability_tables->mv_bits_prob()[m_mv_component][m_mv_bit];
  274. case SyntaxElementType::TXSize:
  275. return calculate_tx_size_probability(node);
  276. case SyntaxElementType::MVJoint:
  277. return m_decoder.m_probability_tables->mv_joint_probs()[node];
  278. case SyntaxElementType::MVClass:
  279. // Spec doesn't mention node, but the probabilities table has an extra dimension
  280. // so we will use node for that.
  281. return m_decoder.m_probability_tables->mv_class_probs()[m_mv_component][node];
  282. case SyntaxElementType::MVClass0FR:
  283. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  284. return m_decoder.m_probability_tables->mv_class0_fr_probs()[m_mv_component][m_mv_class0_bit][node];
  285. case SyntaxElementType::MVClass0HP:
  286. return m_decoder.m_probability_tables->mv_class0_hp_prob()[m_mv_component];
  287. case SyntaxElementType::MVFR:
  288. return m_decoder.m_probability_tables->mv_fr_probs()[m_mv_component][node];
  289. case SyntaxElementType::MVHP:
  290. return m_decoder.m_probability_tables->mv_hp_prob()[m_mv_component];
  291. case SyntaxElementType::Token:
  292. return calculate_token_probability(node);
  293. case SyntaxElementType::MoreCoefs:
  294. return calculate_more_coefs_probability();
  295. default:
  296. break;
  297. }
  298. VERIFY_NOT_REACHED();
  299. }
  300. #define ABOVE_FRAME_0 m_decoder.m_above_ref_frame[0]
  301. #define ABOVE_FRAME_1 m_decoder.m_above_ref_frame[1]
  302. #define LEFT_FRAME_0 m_decoder.m_left_ref_frame[0]
  303. #define LEFT_FRAME_1 m_decoder.m_left_ref_frame[1]
  304. #define AVAIL_U m_decoder.m_available_u
  305. #define AVAIL_L m_decoder.m_available_l
  306. #define ABOVE_INTRA m_decoder.m_above_intra
  307. #define LEFT_INTRA m_decoder.m_left_intra
  308. #define ABOVE_SINGLE m_decoder.m_above_single
  309. #define LEFT_SINGLE m_decoder.m_left_single
  310. u8 TreeParser::calculate_skip_probability()
  311. {
  312. m_ctx = 0;
  313. if (AVAIL_U)
  314. m_ctx += m_decoder.m_skips[(m_decoder.m_mi_row - 1) * m_decoder.m_mi_cols + m_decoder.m_mi_col];
  315. if (AVAIL_L)
  316. m_ctx += m_decoder.m_skips[m_decoder.m_mi_row * m_decoder.m_mi_cols + m_decoder.m_mi_col - 1];
  317. return m_decoder.m_probability_tables->skip_prob()[m_ctx];
  318. }
  319. u8 TreeParser::calculate_is_inter_probability()
  320. {
  321. if (AVAIL_U && AVAIL_L) {
  322. m_ctx = (LEFT_INTRA && ABOVE_INTRA) ? 3 : LEFT_INTRA || ABOVE_INTRA;
  323. } else if (AVAIL_U || AVAIL_L) {
  324. m_ctx = 2 * (AVAIL_U ? ABOVE_INTRA : LEFT_INTRA);
  325. } else {
  326. m_ctx = 0;
  327. }
  328. return m_decoder.m_probability_tables->is_inter_prob()[m_ctx];
  329. }
  330. u8 TreeParser::calculate_comp_mode_probability()
  331. {
  332. if (AVAIL_U && AVAIL_L) {
  333. if (ABOVE_SINGLE && LEFT_SINGLE) {
  334. auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  335. auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  336. m_ctx = is_above_fixed ^ is_left_fixed;
  337. } else if (ABOVE_SINGLE) {
  338. auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  339. m_ctx = 2 + (is_above_fixed || ABOVE_INTRA);
  340. } else if (LEFT_SINGLE) {
  341. auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  342. m_ctx = 2 + (is_left_fixed || LEFT_INTRA);
  343. } else {
  344. m_ctx = 4;
  345. }
  346. } else if (AVAIL_U) {
  347. if (ABOVE_SINGLE) {
  348. m_ctx = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  349. } else {
  350. m_ctx = 3;
  351. }
  352. } else if (AVAIL_L) {
  353. if (LEFT_SINGLE) {
  354. m_ctx = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  355. } else {
  356. m_ctx = 3;
  357. }
  358. } else {
  359. m_ctx = 1;
  360. }
  361. return m_decoder.m_probability_tables->comp_mode_prob()[m_ctx];
  362. }
  363. u8 TreeParser::calculate_comp_ref_probability()
  364. {
  365. auto fix_ref_idx = m_decoder.m_ref_frame_sign_bias[m_decoder.m_comp_fixed_ref];
  366. auto var_ref_idx = !fix_ref_idx;
  367. if (AVAIL_U && AVAIL_L) {
  368. if (ABOVE_INTRA && LEFT_INTRA) {
  369. m_ctx = 2;
  370. } else if (LEFT_INTRA) {
  371. if (ABOVE_SINGLE) {
  372. m_ctx = 1 + 2 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  373. } else {
  374. m_ctx = 1 + 2 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  375. }
  376. } else if (ABOVE_INTRA) {
  377. if (LEFT_SINGLE) {
  378. m_ctx = 1 + 2 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  379. } else {
  380. m_ctx = 1 + 2 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  381. }
  382. } else {
  383. auto var_ref_above = m_decoder.m_above_ref_frame[ABOVE_SINGLE ? 0 : var_ref_idx];
  384. auto var_ref_left = m_decoder.m_left_ref_frame[LEFT_SINGLE ? 0 : var_ref_idx];
  385. if (var_ref_above == var_ref_left && m_decoder.m_comp_var_ref[1] == var_ref_above) {
  386. m_ctx = 0;
  387. } else if (LEFT_SINGLE && ABOVE_SINGLE) {
  388. if ((var_ref_above == m_decoder.m_comp_fixed_ref && var_ref_left == m_decoder.m_comp_var_ref[0])
  389. || (var_ref_left == m_decoder.m_comp_fixed_ref && var_ref_above == m_decoder.m_comp_var_ref[0])) {
  390. m_ctx = 4;
  391. } else if (var_ref_above == var_ref_left) {
  392. m_ctx = 3;
  393. } else {
  394. m_ctx = 1;
  395. }
  396. } else if (LEFT_SINGLE || ABOVE_SINGLE) {
  397. auto vrfc = LEFT_SINGLE ? var_ref_above : var_ref_left;
  398. auto rfs = ABOVE_SINGLE ? var_ref_above : var_ref_left;
  399. if (vrfc == m_decoder.m_comp_var_ref[1] && rfs != m_decoder.m_comp_var_ref[1]) {
  400. m_ctx = 1;
  401. } else if (rfs == m_decoder.m_comp_var_ref[1] && vrfc != m_decoder.m_comp_var_ref[1]) {
  402. m_ctx = 2;
  403. } else {
  404. m_ctx = 4;
  405. }
  406. } else if (var_ref_above == var_ref_left) {
  407. m_ctx = 4;
  408. } else {
  409. m_ctx = 2;
  410. }
  411. }
  412. } else if (AVAIL_U) {
  413. if (ABOVE_INTRA) {
  414. m_ctx = 2;
  415. } else {
  416. if (ABOVE_SINGLE) {
  417. m_ctx = 3 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  418. } else {
  419. m_ctx = 4 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  420. }
  421. }
  422. } else if (AVAIL_L) {
  423. if (LEFT_INTRA) {
  424. m_ctx = 2;
  425. } else {
  426. if (LEFT_SINGLE) {
  427. m_ctx = 3 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  428. } else {
  429. m_ctx = 4 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  430. }
  431. }
  432. } else {
  433. m_ctx = 2;
  434. }
  435. return m_decoder.m_probability_tables->comp_ref_prob()[m_ctx];
  436. }
  437. u8 TreeParser::calculate_single_ref_p1_probability()
  438. {
  439. if (AVAIL_U && AVAIL_L) {
  440. if (ABOVE_INTRA && LEFT_INTRA) {
  441. m_ctx = 2;
  442. } else if (LEFT_INTRA) {
  443. if (ABOVE_SINGLE) {
  444. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  445. } else {
  446. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  447. }
  448. } else if (ABOVE_INTRA) {
  449. if (LEFT_SINGLE) {
  450. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  451. } else {
  452. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  453. }
  454. } else {
  455. if (LEFT_SINGLE && ABOVE_SINGLE) {
  456. m_ctx = 2 * (ABOVE_FRAME_0 == LastFrame) + 2 * (LEFT_FRAME_0 == LastFrame);
  457. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  458. auto above_is_last = ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame;
  459. auto left_is_last = LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame;
  460. m_ctx = 1 + (above_is_last || left_is_last);
  461. } else {
  462. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  463. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  464. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  465. m_ctx = crf1 == LastFrame || crf2 == LastFrame;
  466. if (rfs == LastFrame)
  467. m_ctx += 3;
  468. }
  469. }
  470. } else if (AVAIL_U) {
  471. if (ABOVE_INTRA) {
  472. m_ctx = 2;
  473. } else {
  474. if (ABOVE_SINGLE) {
  475. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  476. } else {
  477. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  478. }
  479. }
  480. } else if (AVAIL_L) {
  481. if (LEFT_INTRA) {
  482. m_ctx = 2;
  483. } else {
  484. if (LEFT_SINGLE) {
  485. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  486. } else {
  487. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  488. }
  489. }
  490. } else {
  491. m_ctx = 2;
  492. }
  493. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][0];
  494. }
  495. u8 TreeParser::calculate_single_ref_p2_probability()
  496. {
  497. if (AVAIL_U && AVAIL_L) {
  498. if (ABOVE_INTRA && LEFT_INTRA) {
  499. m_ctx = 2;
  500. } else if (LEFT_INTRA) {
  501. if (ABOVE_SINGLE) {
  502. if (ABOVE_FRAME_0 == LastFrame) {
  503. m_ctx = 3;
  504. } else {
  505. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  506. }
  507. } else {
  508. m_ctx = 1 + 2 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  509. }
  510. } else if (ABOVE_INTRA) {
  511. if (LEFT_SINGLE) {
  512. if (LEFT_FRAME_0 == LastFrame) {
  513. m_ctx = 3;
  514. } else {
  515. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  516. }
  517. } else {
  518. m_ctx = 1 + 2 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  519. }
  520. } else {
  521. if (LEFT_SINGLE && ABOVE_SINGLE) {
  522. auto above_last = ABOVE_FRAME_0 == LastFrame;
  523. auto left_last = LEFT_FRAME_0 == LastFrame;
  524. if (above_last && left_last) {
  525. m_ctx = 3;
  526. } else if (above_last) {
  527. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  528. } else if (left_last) {
  529. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  530. } else {
  531. m_ctx = 2 * (ABOVE_FRAME_0 == GoldenFrame) + 2 * (LEFT_FRAME_0 == GoldenFrame);
  532. }
  533. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  534. if (ABOVE_FRAME_0 == LEFT_FRAME_0 && ABOVE_FRAME_1 == LEFT_FRAME_1) {
  535. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  536. } else {
  537. m_ctx = 2;
  538. }
  539. } else {
  540. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  541. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  542. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  543. m_ctx = crf1 == GoldenFrame || crf2 == GoldenFrame;
  544. if (rfs == GoldenFrame) {
  545. m_ctx += 3;
  546. } else if (rfs != AltRefFrame) {
  547. m_ctx = 1 + (2 * m_ctx);
  548. }
  549. }
  550. }
  551. } else if (AVAIL_U) {
  552. if (ABOVE_INTRA || (ABOVE_FRAME_0 == LastFrame && ABOVE_SINGLE)) {
  553. m_ctx = 2;
  554. } else if (ABOVE_SINGLE) {
  555. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  556. } else {
  557. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  558. }
  559. } else if (AVAIL_L) {
  560. if (LEFT_INTRA || (LEFT_FRAME_0 == LastFrame && LEFT_SINGLE)) {
  561. m_ctx = 2;
  562. } else if (LEFT_SINGLE) {
  563. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  564. } else {
  565. m_ctx = 3 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  566. }
  567. } else {
  568. m_ctx = 2;
  569. }
  570. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][1];
  571. }
  572. u8 TreeParser::calculate_tx_size_probability(u8 node)
  573. {
  574. auto above = m_decoder.m_max_tx_size;
  575. auto left = m_decoder.m_max_tx_size;
  576. if (AVAIL_U) {
  577. auto u_pos = (m_decoder.m_mi_row - 1) * m_decoder.m_mi_cols + m_decoder.m_mi_col;
  578. if (!m_decoder.m_skips[u_pos])
  579. above = m_decoder.m_tx_sizes[u_pos];
  580. }
  581. if (AVAIL_L) {
  582. auto l_pos = m_decoder.m_mi_row * m_decoder.m_mi_cols + m_decoder.m_mi_col - 1;
  583. if (!m_decoder.m_skips[l_pos])
  584. left = m_decoder.m_tx_sizes[l_pos];
  585. }
  586. if (!AVAIL_L)
  587. left = above;
  588. if (!AVAIL_U)
  589. above = left;
  590. m_ctx = (above + left) > m_decoder.m_max_tx_size;
  591. return m_decoder.m_probability_tables->tx_probs()[m_decoder.m_max_tx_size][m_ctx][node];
  592. }
  593. void TreeParser::set_tokens_variables(u8 band, u32 c, u32 plane, TXSize tx_size, u32 pos)
  594. {
  595. m_band = band;
  596. m_c = c;
  597. m_plane = plane;
  598. m_tx_size = tx_size;
  599. m_pos = pos;
  600. if (m_c == 0) {
  601. auto sx = m_plane > 0 ? m_decoder.m_subsampling_x : 0;
  602. auto sy = m_plane > 0 ? m_decoder.m_subsampling_y : 0;
  603. auto max_x = (2 * m_decoder.m_mi_cols) >> sx;
  604. auto max_y = (2 * m_decoder.m_mi_rows) >> sy;
  605. u8 numpts = 1 << m_tx_size;
  606. auto x4 = m_start_x >> 2;
  607. auto y4 = m_start_y >> 2;
  608. u32 above = 0;
  609. u32 left = 0;
  610. for (size_t i = 0; i < numpts; i++) {
  611. if (x4 + i < max_x)
  612. above |= m_decoder.m_above_nonzero_context[m_plane][x4 + i];
  613. if (y4 + i < max_y)
  614. left |= m_decoder.m_left_nonzero_context[m_plane][y4 + i];
  615. }
  616. m_ctx = above + left;
  617. } else {
  618. u32 neighbor_0, neighbor_1;
  619. auto n = 4 << m_tx_size;
  620. auto i = m_pos / n;
  621. auto j = m_pos % n;
  622. auto a = i > 0 ? (i - 1) * n + j : 0;
  623. auto a2 = i * n + j - 1;
  624. if (i > 0 && j > 0) {
  625. if (m_decoder.m_tx_type == DCT_ADST) {
  626. neighbor_0 = a;
  627. neighbor_1 = a;
  628. } else if (m_decoder.m_tx_type == ADST_DCT) {
  629. neighbor_0 = a2;
  630. neighbor_1 = a2;
  631. } else {
  632. neighbor_0 = a;
  633. neighbor_1 = a2;
  634. }
  635. } else if (i > 0) {
  636. neighbor_0 = a;
  637. neighbor_1 = a;
  638. } else {
  639. neighbor_0 = a2;
  640. neighbor_1 = a2;
  641. }
  642. m_ctx = (1 + m_decoder.m_token_cache[neighbor_0] + m_decoder.m_token_cache[neighbor_1]) >> 1;
  643. }
  644. }
  645. u8 TreeParser::calculate_more_coefs_probability()
  646. {
  647. return m_decoder.m_probability_tables->coef_probs()[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][0];
  648. }
  649. u8 TreeParser::calculate_token_probability(u8 node)
  650. {
  651. auto prob = m_decoder.m_probability_tables->coef_probs()[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][min(2, 1 + node)];
  652. if (node < 2)
  653. return prob;
  654. auto x = (prob - 1) / 2;
  655. auto& pareto_table = m_decoder.m_probability_tables->pareto_table();
  656. if (prob & 1)
  657. return pareto_table[x][node - 2];
  658. return (pareto_table[x][node - 2] + pareto_table[x + 1][node - 2]) >> 1;
  659. }
  660. void TreeParser::count_syntax_element(SyntaxElementType type, int value)
  661. {
  662. auto increment = [](u8& count) {
  663. increment_counter(count);
  664. };
  665. switch (type) {
  666. case SyntaxElementType::Skip:
  667. increment(m_decoder.m_syntax_element_counter->m_counts_skip[m_ctx][value]);
  668. return;
  669. case SyntaxElementType::IsInter:
  670. increment(m_decoder.m_syntax_element_counter->m_counts_is_inter[m_ctx][value]);
  671. return;
  672. case SyntaxElementType::CompMode:
  673. increment(m_decoder.m_syntax_element_counter->m_counts_comp_mode[m_ctx][value]);
  674. return;
  675. case SyntaxElementType::CompRef:
  676. increment(m_decoder.m_syntax_element_counter->m_counts_comp_ref[m_ctx][value]);
  677. return;
  678. case SyntaxElementType::SingleRefP1:
  679. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][0][value]);
  680. return;
  681. case SyntaxElementType::SingleRefP2:
  682. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][1][value]);
  683. return;
  684. case SyntaxElementType::MVSign:
  685. increment(m_decoder.m_syntax_element_counter->m_counts_mv_sign[m_mv_component][value]);
  686. return;
  687. case SyntaxElementType::MVClass0Bit:
  688. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_bit[m_mv_component][value]);
  689. return;
  690. case SyntaxElementType::MVBit:
  691. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  692. increment(m_decoder.m_syntax_element_counter->m_counts_mv_bits[m_mv_component][m_mv_bit][value]);
  693. m_mv_bit = 0xFF;
  694. return;
  695. case SyntaxElementType::TXSize:
  696. increment(m_decoder.m_syntax_element_counter->m_counts_tx_size[m_decoder.m_max_tx_size][m_ctx][value]);
  697. return;
  698. case SyntaxElementType::MVJoint:
  699. increment(m_decoder.m_syntax_element_counter->m_counts_mv_joint[value]);
  700. return;
  701. case SyntaxElementType::MVClass:
  702. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class[m_mv_component][value]);
  703. return;
  704. case SyntaxElementType::MVClass0FR:
  705. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  706. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_fr[m_mv_component][m_mv_class0_bit][value]);
  707. m_mv_class0_bit = 0xFF;
  708. return;
  709. case SyntaxElementType::MVClass0HP:
  710. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_hp[m_mv_component][value]);
  711. return;
  712. case SyntaxElementType::MVFR:
  713. increment(m_decoder.m_syntax_element_counter->m_counts_mv_fr[m_mv_component][value]);
  714. return;
  715. case SyntaxElementType::MVHP:
  716. increment(m_decoder.m_syntax_element_counter->m_counts_mv_hp[m_mv_component][value]);
  717. return;
  718. case SyntaxElementType::Token:
  719. increment(m_decoder.m_syntax_element_counter->m_counts_token[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][min(2, value)]);
  720. return;
  721. case SyntaxElementType::MoreCoefs:
  722. increment(m_decoder.m_syntax_element_counter->m_counts_more_coefs[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][value]);
  723. return;
  724. default:
  725. break;
  726. }
  727. VERIFY_NOT_REACHED();
  728. }
  729. }