TreeParser.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. ErrorOr<bool> TreeParser::parse_skip(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, Optional<bool> const& above_skip, Optional<bool> const& left_skip)
  208. {
  209. // Probabilities
  210. u8 context = 0;
  211. context += static_cast<u8>(above_skip.value_or(false));
  212. context += static_cast<u8>(left_skip.value_or(false));
  213. u8 probability = probability_table.skip_prob()[context];
  214. auto value = TRY(parse_tree_new<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  215. increment_counter(counter.m_counts_skip[context][value]);
  216. return value;
  217. }
  218. ErrorOr<TXSize> TreeParser::parse_tx_size(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, TXSize max_tx_size, Optional<bool> above_skip, Optional<bool> left_skip, Optional<TXSize> above_tx_size, Optional<TXSize> left_tx_size)
  219. {
  220. // FIXME: Above and left contexts should be in structs.
  221. // Tree
  222. TreeParser::TreeSelection tree { tx_size_8_tree };
  223. if (max_tx_size == TX_16x16)
  224. tree = { tx_size_16_tree };
  225. if (max_tx_size == TX_32x32)
  226. tree = { tx_size_32_tree };
  227. // Probabilities
  228. auto above = max_tx_size;
  229. auto left = max_tx_size;
  230. if (above_skip.has_value() && !above_skip.value()) {
  231. above = above_tx_size.value();
  232. }
  233. if (left_skip.has_value() && !left_skip.value()) {
  234. left = left_tx_size.value();
  235. }
  236. if (!left_skip.has_value())
  237. left = above;
  238. if (!above_skip.has_value())
  239. above = left;
  240. auto context = (above + left) > max_tx_size;
  241. u8 const* probabilities = probability_table.tx_probs()[max_tx_size][context];
  242. auto value = TRY(parse_tree_new<TXSize>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  243. increment_counter(counter.m_counts_tx_size[max_tx_size][context][value]);
  244. return value;
  245. }
  246. ErrorOr<bool> TreeParser::parse_is_inter(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, Optional<bool> above_intra, Optional<bool> left_intra)
  247. {
  248. // FIXME: Above and left contexts should be in structs.
  249. // Probabilities
  250. u8 context = 0;
  251. if (above_intra.has_value() && left_intra.has_value())
  252. context = (left_intra.value() && above_intra.value()) ? 3 : static_cast<u8>(above_intra.value() || left_intra.value());
  253. else if (above_intra.has_value() || left_intra.has_value())
  254. context = 2 * static_cast<u8>(above_intra.has_value() ? above_intra.value() : left_intra.value());
  255. u8 probability = probability_table.is_inter_prob()[context];
  256. auto value = TRY(parse_tree_new<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  257. increment_counter(counter.m_counts_is_inter[context][value]);
  258. return value;
  259. }
  260. ErrorOr<ReferenceMode> TreeParser::parse_comp_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, ReferenceFrameType comp_fixed_ref, Optional<bool> above_single, Optional<bool> left_single, Optional<bool> above_intra, Optional<bool> left_intra, Optional<ReferenceFrameType> above_ref_frame_0, Optional<ReferenceFrameType> left_ref_frame_0)
  261. {
  262. // FIXME: Above and left contexts should be in structs.
  263. // Probabilities
  264. u8 context;
  265. if (above_single.has_value() && left_single.has_value()) {
  266. if (above_single.value() && left_single.value()) {
  267. auto is_above_fixed = above_ref_frame_0.value() == comp_fixed_ref;
  268. auto is_left_fixed = left_ref_frame_0.value() == comp_fixed_ref;
  269. context = is_above_fixed ^ is_left_fixed;
  270. } else if (above_single.value()) {
  271. auto is_above_fixed = above_ref_frame_0.value() == comp_fixed_ref;
  272. context = 2 + static_cast<u8>(is_above_fixed || above_intra.value());
  273. } else if (left_single.value()) {
  274. auto is_left_fixed = left_ref_frame_0.value() == comp_fixed_ref;
  275. context = 2 + static_cast<u8>(is_left_fixed || left_intra.value());
  276. } else {
  277. context = 4;
  278. }
  279. } else if (above_single.has_value()) {
  280. if (above_single.value())
  281. context = above_ref_frame_0.value() == comp_fixed_ref;
  282. else
  283. context = 3;
  284. } else if (left_single.has_value()) {
  285. if (left_single.value())
  286. context = static_cast<u8>(left_ref_frame_0.value() == comp_fixed_ref);
  287. else
  288. context = 3;
  289. } else {
  290. context = 1;
  291. }
  292. u8 probability = probability_table.comp_mode_prob()[context];
  293. auto value = TRY(parse_tree_new<ReferenceMode>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  294. increment_counter(counter.m_counts_comp_mode[context][value]);
  295. return value;
  296. }
  297. /*
  298. * 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
  299. */
  300. TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type)
  301. {
  302. switch (type) {
  303. case SyntaxElementType::CompRef:
  304. case SyntaxElementType::SingleRefP1:
  305. case SyntaxElementType::SingleRefP2:
  306. case SyntaxElementType::MVSign:
  307. case SyntaxElementType::MVClass0Bit:
  308. case SyntaxElementType::MVBit:
  309. case SyntaxElementType::MoreCoefs:
  310. return { binary_tree };
  311. case SyntaxElementType::MVJoint:
  312. return { mv_joint_tree };
  313. case SyntaxElementType::MVClass:
  314. return { mv_class_tree };
  315. case SyntaxElementType::MVClass0FR:
  316. case SyntaxElementType::MVFR:
  317. return { mv_fr_tree };
  318. case SyntaxElementType::MVClass0HP:
  319. case SyntaxElementType::MVHP:
  320. if (m_decoder.m_use_hp)
  321. return { binary_tree };
  322. return { 1 };
  323. case SyntaxElementType::Token:
  324. return { token_tree };
  325. default:
  326. break;
  327. }
  328. VERIFY_NOT_REACHED();
  329. }
  330. /*
  331. * Select a probability with which to read a boolean when decoding a tree, as specified in section 9.3.2
  332. */
  333. u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node)
  334. {
  335. switch (type) {
  336. case SyntaxElementType::CompRef:
  337. return calculate_comp_ref_probability();
  338. case SyntaxElementType::SingleRefP1:
  339. return calculate_single_ref_p1_probability();
  340. case SyntaxElementType::SingleRefP2:
  341. return calculate_single_ref_p2_probability();
  342. case SyntaxElementType::MVSign:
  343. return m_decoder.m_probability_tables->mv_sign_prob()[m_mv_component];
  344. case SyntaxElementType::MVClass0Bit:
  345. return m_decoder.m_probability_tables->mv_class0_bit_prob()[m_mv_component];
  346. case SyntaxElementType::MVBit:
  347. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  348. return m_decoder.m_probability_tables->mv_bits_prob()[m_mv_component][m_mv_bit];
  349. case SyntaxElementType::MVJoint:
  350. return m_decoder.m_probability_tables->mv_joint_probs()[node];
  351. case SyntaxElementType::MVClass:
  352. // Spec doesn't mention node, but the probabilities table has an extra dimension
  353. // so we will use node for that.
  354. return m_decoder.m_probability_tables->mv_class_probs()[m_mv_component][node];
  355. case SyntaxElementType::MVClass0FR:
  356. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  357. return m_decoder.m_probability_tables->mv_class0_fr_probs()[m_mv_component][m_mv_class0_bit][node];
  358. case SyntaxElementType::MVClass0HP:
  359. return m_decoder.m_probability_tables->mv_class0_hp_prob()[m_mv_component];
  360. case SyntaxElementType::MVFR:
  361. return m_decoder.m_probability_tables->mv_fr_probs()[m_mv_component][node];
  362. case SyntaxElementType::MVHP:
  363. return m_decoder.m_probability_tables->mv_hp_prob()[m_mv_component];
  364. case SyntaxElementType::Token:
  365. return calculate_token_probability(node);
  366. case SyntaxElementType::MoreCoefs:
  367. return calculate_more_coefs_probability();
  368. default:
  369. break;
  370. }
  371. VERIFY_NOT_REACHED();
  372. }
  373. #define ABOVE_FRAME_0 m_decoder.m_above_ref_frame[0]
  374. #define ABOVE_FRAME_1 m_decoder.m_above_ref_frame[1]
  375. #define LEFT_FRAME_0 m_decoder.m_left_ref_frame[0]
  376. #define LEFT_FRAME_1 m_decoder.m_left_ref_frame[1]
  377. #define AVAIL_U m_decoder.m_available_u
  378. #define AVAIL_L m_decoder.m_available_l
  379. #define ABOVE_INTRA m_decoder.m_above_intra
  380. #define LEFT_INTRA m_decoder.m_left_intra
  381. #define ABOVE_SINGLE m_decoder.m_above_single
  382. #define LEFT_SINGLE m_decoder.m_left_single
  383. u8 TreeParser::calculate_comp_ref_probability()
  384. {
  385. auto fix_ref_idx = m_decoder.m_ref_frame_sign_bias[m_decoder.m_comp_fixed_ref];
  386. auto var_ref_idx = !fix_ref_idx;
  387. if (AVAIL_U && AVAIL_L) {
  388. if (ABOVE_INTRA && LEFT_INTRA) {
  389. m_ctx = 2;
  390. } else if (LEFT_INTRA) {
  391. if (ABOVE_SINGLE) {
  392. m_ctx = 1 + 2 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  393. } else {
  394. m_ctx = 1 + 2 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  395. }
  396. } else if (ABOVE_INTRA) {
  397. if (LEFT_SINGLE) {
  398. m_ctx = 1 + 2 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  399. } else {
  400. m_ctx = 1 + 2 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  401. }
  402. } else {
  403. auto var_ref_above = m_decoder.m_above_ref_frame[ABOVE_SINGLE ? 0 : var_ref_idx];
  404. auto var_ref_left = m_decoder.m_left_ref_frame[LEFT_SINGLE ? 0 : var_ref_idx];
  405. if (var_ref_above == var_ref_left && m_decoder.m_comp_var_ref[1] == var_ref_above) {
  406. m_ctx = 0;
  407. } else if (LEFT_SINGLE && ABOVE_SINGLE) {
  408. if ((var_ref_above == m_decoder.m_comp_fixed_ref && var_ref_left == m_decoder.m_comp_var_ref[0])
  409. || (var_ref_left == m_decoder.m_comp_fixed_ref && var_ref_above == m_decoder.m_comp_var_ref[0])) {
  410. m_ctx = 4;
  411. } else if (var_ref_above == var_ref_left) {
  412. m_ctx = 3;
  413. } else {
  414. m_ctx = 1;
  415. }
  416. } else if (LEFT_SINGLE || ABOVE_SINGLE) {
  417. auto vrfc = LEFT_SINGLE ? var_ref_above : var_ref_left;
  418. auto rfs = ABOVE_SINGLE ? var_ref_above : var_ref_left;
  419. if (vrfc == m_decoder.m_comp_var_ref[1] && rfs != m_decoder.m_comp_var_ref[1]) {
  420. m_ctx = 1;
  421. } else if (rfs == m_decoder.m_comp_var_ref[1] && vrfc != m_decoder.m_comp_var_ref[1]) {
  422. m_ctx = 2;
  423. } else {
  424. m_ctx = 4;
  425. }
  426. } else if (var_ref_above == var_ref_left) {
  427. m_ctx = 4;
  428. } else {
  429. m_ctx = 2;
  430. }
  431. }
  432. } else if (AVAIL_U) {
  433. if (ABOVE_INTRA) {
  434. m_ctx = 2;
  435. } else {
  436. if (ABOVE_SINGLE) {
  437. m_ctx = 3 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  438. } else {
  439. m_ctx = 4 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  440. }
  441. }
  442. } else if (AVAIL_L) {
  443. if (LEFT_INTRA) {
  444. m_ctx = 2;
  445. } else {
  446. if (LEFT_SINGLE) {
  447. m_ctx = 3 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  448. } else {
  449. m_ctx = 4 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  450. }
  451. }
  452. } else {
  453. m_ctx = 2;
  454. }
  455. return m_decoder.m_probability_tables->comp_ref_prob()[m_ctx];
  456. }
  457. u8 TreeParser::calculate_single_ref_p1_probability()
  458. {
  459. if (AVAIL_U && AVAIL_L) {
  460. if (ABOVE_INTRA && LEFT_INTRA) {
  461. m_ctx = 2;
  462. } else if (LEFT_INTRA) {
  463. if (ABOVE_SINGLE) {
  464. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  465. } else {
  466. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  467. }
  468. } else if (ABOVE_INTRA) {
  469. if (LEFT_SINGLE) {
  470. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  471. } else {
  472. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  473. }
  474. } else {
  475. if (LEFT_SINGLE && ABOVE_SINGLE) {
  476. m_ctx = 2 * (ABOVE_FRAME_0 == LastFrame) + 2 * (LEFT_FRAME_0 == LastFrame);
  477. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  478. auto above_is_last = ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame;
  479. auto left_is_last = LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame;
  480. m_ctx = 1 + (above_is_last || left_is_last);
  481. } else {
  482. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  483. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  484. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  485. m_ctx = crf1 == LastFrame || crf2 == LastFrame;
  486. if (rfs == LastFrame)
  487. m_ctx += 3;
  488. }
  489. }
  490. } else if (AVAIL_U) {
  491. if (ABOVE_INTRA) {
  492. m_ctx = 2;
  493. } else {
  494. if (ABOVE_SINGLE) {
  495. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  496. } else {
  497. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  498. }
  499. }
  500. } else if (AVAIL_L) {
  501. if (LEFT_INTRA) {
  502. m_ctx = 2;
  503. } else {
  504. if (LEFT_SINGLE) {
  505. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  506. } else {
  507. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  508. }
  509. }
  510. } else {
  511. m_ctx = 2;
  512. }
  513. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][0];
  514. }
  515. u8 TreeParser::calculate_single_ref_p2_probability()
  516. {
  517. if (AVAIL_U && AVAIL_L) {
  518. if (ABOVE_INTRA && LEFT_INTRA) {
  519. m_ctx = 2;
  520. } else if (LEFT_INTRA) {
  521. if (ABOVE_SINGLE) {
  522. if (ABOVE_FRAME_0 == LastFrame) {
  523. m_ctx = 3;
  524. } else {
  525. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  526. }
  527. } else {
  528. m_ctx = 1 + 2 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  529. }
  530. } else if (ABOVE_INTRA) {
  531. if (LEFT_SINGLE) {
  532. if (LEFT_FRAME_0 == LastFrame) {
  533. m_ctx = 3;
  534. } else {
  535. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  536. }
  537. } else {
  538. m_ctx = 1 + 2 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  539. }
  540. } else {
  541. if (LEFT_SINGLE && ABOVE_SINGLE) {
  542. auto above_last = ABOVE_FRAME_0 == LastFrame;
  543. auto left_last = LEFT_FRAME_0 == LastFrame;
  544. if (above_last && left_last) {
  545. m_ctx = 3;
  546. } else if (above_last) {
  547. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  548. } else if (left_last) {
  549. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  550. } else {
  551. m_ctx = 2 * (ABOVE_FRAME_0 == GoldenFrame) + 2 * (LEFT_FRAME_0 == GoldenFrame);
  552. }
  553. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  554. if (ABOVE_FRAME_0 == LEFT_FRAME_0 && ABOVE_FRAME_1 == LEFT_FRAME_1) {
  555. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  556. } else {
  557. m_ctx = 2;
  558. }
  559. } else {
  560. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  561. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  562. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  563. m_ctx = crf1 == GoldenFrame || crf2 == GoldenFrame;
  564. if (rfs == GoldenFrame) {
  565. m_ctx += 3;
  566. } else if (rfs != AltRefFrame) {
  567. m_ctx = 1 + (2 * m_ctx);
  568. }
  569. }
  570. }
  571. } else if (AVAIL_U) {
  572. if (ABOVE_INTRA || (ABOVE_FRAME_0 == LastFrame && ABOVE_SINGLE)) {
  573. m_ctx = 2;
  574. } else if (ABOVE_SINGLE) {
  575. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  576. } else {
  577. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  578. }
  579. } else if (AVAIL_L) {
  580. if (LEFT_INTRA || (LEFT_FRAME_0 == LastFrame && LEFT_SINGLE)) {
  581. m_ctx = 2;
  582. } else if (LEFT_SINGLE) {
  583. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  584. } else {
  585. m_ctx = 3 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  586. }
  587. } else {
  588. m_ctx = 2;
  589. }
  590. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][1];
  591. }
  592. void TreeParser::set_tokens_variables(u8 band, u32 c, u32 plane, TXSize tx_size, u32 pos)
  593. {
  594. m_band = band;
  595. m_c = c;
  596. m_plane = plane;
  597. m_tx_size = tx_size;
  598. m_pos = pos;
  599. if (m_c == 0) {
  600. auto sx = m_plane > 0 ? m_decoder.m_subsampling_x : 0;
  601. auto sy = m_plane > 0 ? m_decoder.m_subsampling_y : 0;
  602. auto max_x = (2 * m_decoder.m_mi_cols) >> sx;
  603. auto max_y = (2 * m_decoder.m_mi_rows) >> sy;
  604. u8 numpts = 1 << m_tx_size;
  605. auto x4 = m_start_x >> 2;
  606. auto y4 = m_start_y >> 2;
  607. u32 above = 0;
  608. u32 left = 0;
  609. for (size_t i = 0; i < numpts; i++) {
  610. if (x4 + i < max_x)
  611. above |= m_decoder.m_above_nonzero_context[m_plane][x4 + i];
  612. if (y4 + i < max_y)
  613. left |= m_decoder.m_left_nonzero_context[m_plane][y4 + i];
  614. }
  615. m_ctx = above + left;
  616. } else {
  617. u32 neighbor_0, neighbor_1;
  618. auto n = 4 << m_tx_size;
  619. auto i = m_pos / n;
  620. auto j = m_pos % n;
  621. auto a = i > 0 ? (i - 1) * n + j : 0;
  622. auto a2 = i * n + j - 1;
  623. if (i > 0 && j > 0) {
  624. if (m_decoder.m_tx_type == DCT_ADST) {
  625. neighbor_0 = a;
  626. neighbor_1 = a;
  627. } else if (m_decoder.m_tx_type == ADST_DCT) {
  628. neighbor_0 = a2;
  629. neighbor_1 = a2;
  630. } else {
  631. neighbor_0 = a;
  632. neighbor_1 = a2;
  633. }
  634. } else if (i > 0) {
  635. neighbor_0 = a;
  636. neighbor_1 = a;
  637. } else {
  638. neighbor_0 = a2;
  639. neighbor_1 = a2;
  640. }
  641. m_ctx = (1 + m_decoder.m_token_cache[neighbor_0] + m_decoder.m_token_cache[neighbor_1]) >> 1;
  642. }
  643. }
  644. u8 TreeParser::calculate_more_coefs_probability()
  645. {
  646. return m_decoder.m_probability_tables->coef_probs()[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][0];
  647. }
  648. u8 TreeParser::calculate_token_probability(u8 node)
  649. {
  650. 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)];
  651. if (node < 2)
  652. return prob;
  653. auto x = (prob - 1) / 2;
  654. auto& pareto_table = m_decoder.m_probability_tables->pareto_table();
  655. if (prob & 1)
  656. return pareto_table[x][node - 2];
  657. return (pareto_table[x][node - 2] + pareto_table[x + 1][node - 2]) >> 1;
  658. }
  659. void TreeParser::count_syntax_element(SyntaxElementType type, int value)
  660. {
  661. auto increment = [](u8& count) {
  662. increment_counter(count);
  663. };
  664. switch (type) {
  665. case SyntaxElementType::CompRef:
  666. increment(m_decoder.m_syntax_element_counter->m_counts_comp_ref[m_ctx][value]);
  667. return;
  668. case SyntaxElementType::SingleRefP1:
  669. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][0][value]);
  670. return;
  671. case SyntaxElementType::SingleRefP2:
  672. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][1][value]);
  673. return;
  674. case SyntaxElementType::MVSign:
  675. increment(m_decoder.m_syntax_element_counter->m_counts_mv_sign[m_mv_component][value]);
  676. return;
  677. case SyntaxElementType::MVClass0Bit:
  678. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_bit[m_mv_component][value]);
  679. return;
  680. case SyntaxElementType::MVBit:
  681. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  682. increment(m_decoder.m_syntax_element_counter->m_counts_mv_bits[m_mv_component][m_mv_bit][value]);
  683. m_mv_bit = 0xFF;
  684. return;
  685. case SyntaxElementType::MVJoint:
  686. increment(m_decoder.m_syntax_element_counter->m_counts_mv_joint[value]);
  687. return;
  688. case SyntaxElementType::MVClass:
  689. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class[m_mv_component][value]);
  690. return;
  691. case SyntaxElementType::MVClass0FR:
  692. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  693. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_fr[m_mv_component][m_mv_class0_bit][value]);
  694. m_mv_class0_bit = 0xFF;
  695. return;
  696. case SyntaxElementType::MVClass0HP:
  697. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_hp[m_mv_component][value]);
  698. return;
  699. case SyntaxElementType::MVFR:
  700. increment(m_decoder.m_syntax_element_counter->m_counts_mv_fr[m_mv_component][value]);
  701. return;
  702. case SyntaxElementType::MVHP:
  703. increment(m_decoder.m_syntax_element_counter->m_counts_mv_hp[m_mv_component][value]);
  704. return;
  705. case SyntaxElementType::Token:
  706. 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)]);
  707. return;
  708. case SyntaxElementType::MoreCoefs:
  709. 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]);
  710. return;
  711. default:
  712. break;
  713. }
  714. VERIFY_NOT_REACHED();
  715. }
  716. }