TreeParser.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. /*
  219. * 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
  220. */
  221. TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type)
  222. {
  223. switch (type) {
  224. case SyntaxElementType::IsInter:
  225. case SyntaxElementType::CompMode:
  226. case SyntaxElementType::CompRef:
  227. case SyntaxElementType::SingleRefP1:
  228. case SyntaxElementType::SingleRefP2:
  229. case SyntaxElementType::MVSign:
  230. case SyntaxElementType::MVClass0Bit:
  231. case SyntaxElementType::MVBit:
  232. case SyntaxElementType::MoreCoefs:
  233. return { binary_tree };
  234. case SyntaxElementType::TXSize:
  235. if (m_decoder.m_max_tx_size == TX_32x32)
  236. return { tx_size_32_tree };
  237. if (m_decoder.m_max_tx_size == TX_16x16)
  238. return { tx_size_16_tree };
  239. return { tx_size_8_tree };
  240. case SyntaxElementType::MVJoint:
  241. return { mv_joint_tree };
  242. case SyntaxElementType::MVClass:
  243. return { mv_class_tree };
  244. case SyntaxElementType::MVClass0FR:
  245. case SyntaxElementType::MVFR:
  246. return { mv_fr_tree };
  247. case SyntaxElementType::MVClass0HP:
  248. case SyntaxElementType::MVHP:
  249. if (m_decoder.m_use_hp)
  250. return { binary_tree };
  251. return { 1 };
  252. case SyntaxElementType::Token:
  253. return { token_tree };
  254. default:
  255. break;
  256. }
  257. VERIFY_NOT_REACHED();
  258. }
  259. /*
  260. * Select a probability with which to read a boolean when decoding a tree, as specified in section 9.3.2
  261. */
  262. u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node)
  263. {
  264. switch (type) {
  265. case SyntaxElementType::IsInter:
  266. return calculate_is_inter_probability();
  267. case SyntaxElementType::CompMode:
  268. return calculate_comp_mode_probability();
  269. case SyntaxElementType::CompRef:
  270. return calculate_comp_ref_probability();
  271. case SyntaxElementType::SingleRefP1:
  272. return calculate_single_ref_p1_probability();
  273. case SyntaxElementType::SingleRefP2:
  274. return calculate_single_ref_p2_probability();
  275. case SyntaxElementType::MVSign:
  276. return m_decoder.m_probability_tables->mv_sign_prob()[m_mv_component];
  277. case SyntaxElementType::MVClass0Bit:
  278. return m_decoder.m_probability_tables->mv_class0_bit_prob()[m_mv_component];
  279. case SyntaxElementType::MVBit:
  280. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  281. return m_decoder.m_probability_tables->mv_bits_prob()[m_mv_component][m_mv_bit];
  282. case SyntaxElementType::TXSize:
  283. return calculate_tx_size_probability(node);
  284. case SyntaxElementType::MVJoint:
  285. return m_decoder.m_probability_tables->mv_joint_probs()[node];
  286. case SyntaxElementType::MVClass:
  287. // Spec doesn't mention node, but the probabilities table has an extra dimension
  288. // so we will use node for that.
  289. return m_decoder.m_probability_tables->mv_class_probs()[m_mv_component][node];
  290. case SyntaxElementType::MVClass0FR:
  291. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  292. return m_decoder.m_probability_tables->mv_class0_fr_probs()[m_mv_component][m_mv_class0_bit][node];
  293. case SyntaxElementType::MVClass0HP:
  294. return m_decoder.m_probability_tables->mv_class0_hp_prob()[m_mv_component];
  295. case SyntaxElementType::MVFR:
  296. return m_decoder.m_probability_tables->mv_fr_probs()[m_mv_component][node];
  297. case SyntaxElementType::MVHP:
  298. return m_decoder.m_probability_tables->mv_hp_prob()[m_mv_component];
  299. case SyntaxElementType::Token:
  300. return calculate_token_probability(node);
  301. case SyntaxElementType::MoreCoefs:
  302. return calculate_more_coefs_probability();
  303. default:
  304. break;
  305. }
  306. VERIFY_NOT_REACHED();
  307. }
  308. #define ABOVE_FRAME_0 m_decoder.m_above_ref_frame[0]
  309. #define ABOVE_FRAME_1 m_decoder.m_above_ref_frame[1]
  310. #define LEFT_FRAME_0 m_decoder.m_left_ref_frame[0]
  311. #define LEFT_FRAME_1 m_decoder.m_left_ref_frame[1]
  312. #define AVAIL_U m_decoder.m_available_u
  313. #define AVAIL_L m_decoder.m_available_l
  314. #define ABOVE_INTRA m_decoder.m_above_intra
  315. #define LEFT_INTRA m_decoder.m_left_intra
  316. #define ABOVE_SINGLE m_decoder.m_above_single
  317. #define LEFT_SINGLE m_decoder.m_left_single
  318. u8 TreeParser::calculate_is_inter_probability()
  319. {
  320. if (AVAIL_U && AVAIL_L) {
  321. m_ctx = (LEFT_INTRA && ABOVE_INTRA) ? 3 : LEFT_INTRA || ABOVE_INTRA;
  322. } else if (AVAIL_U || AVAIL_L) {
  323. m_ctx = 2 * (AVAIL_U ? ABOVE_INTRA : LEFT_INTRA);
  324. } else {
  325. m_ctx = 0;
  326. }
  327. return m_decoder.m_probability_tables->is_inter_prob()[m_ctx];
  328. }
  329. u8 TreeParser::calculate_comp_mode_probability()
  330. {
  331. if (AVAIL_U && AVAIL_L) {
  332. if (ABOVE_SINGLE && LEFT_SINGLE) {
  333. auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  334. auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  335. m_ctx = is_above_fixed ^ is_left_fixed;
  336. } else if (ABOVE_SINGLE) {
  337. auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  338. m_ctx = 2 + (is_above_fixed || ABOVE_INTRA);
  339. } else if (LEFT_SINGLE) {
  340. auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  341. m_ctx = 2 + (is_left_fixed || LEFT_INTRA);
  342. } else {
  343. m_ctx = 4;
  344. }
  345. } else if (AVAIL_U) {
  346. if (ABOVE_SINGLE) {
  347. m_ctx = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
  348. } else {
  349. m_ctx = 3;
  350. }
  351. } else if (AVAIL_L) {
  352. if (LEFT_SINGLE) {
  353. m_ctx = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
  354. } else {
  355. m_ctx = 3;
  356. }
  357. } else {
  358. m_ctx = 1;
  359. }
  360. return m_decoder.m_probability_tables->comp_mode_prob()[m_ctx];
  361. }
  362. u8 TreeParser::calculate_comp_ref_probability()
  363. {
  364. auto fix_ref_idx = m_decoder.m_ref_frame_sign_bias[m_decoder.m_comp_fixed_ref];
  365. auto var_ref_idx = !fix_ref_idx;
  366. if (AVAIL_U && AVAIL_L) {
  367. if (ABOVE_INTRA && LEFT_INTRA) {
  368. m_ctx = 2;
  369. } else if (LEFT_INTRA) {
  370. if (ABOVE_SINGLE) {
  371. m_ctx = 1 + 2 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  372. } else {
  373. m_ctx = 1 + 2 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  374. }
  375. } else if (ABOVE_INTRA) {
  376. if (LEFT_SINGLE) {
  377. m_ctx = 1 + 2 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  378. } else {
  379. m_ctx = 1 + 2 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  380. }
  381. } else {
  382. auto var_ref_above = m_decoder.m_above_ref_frame[ABOVE_SINGLE ? 0 : var_ref_idx];
  383. auto var_ref_left = m_decoder.m_left_ref_frame[LEFT_SINGLE ? 0 : var_ref_idx];
  384. if (var_ref_above == var_ref_left && m_decoder.m_comp_var_ref[1] == var_ref_above) {
  385. m_ctx = 0;
  386. } else if (LEFT_SINGLE && ABOVE_SINGLE) {
  387. if ((var_ref_above == m_decoder.m_comp_fixed_ref && var_ref_left == m_decoder.m_comp_var_ref[0])
  388. || (var_ref_left == m_decoder.m_comp_fixed_ref && var_ref_above == m_decoder.m_comp_var_ref[0])) {
  389. m_ctx = 4;
  390. } else if (var_ref_above == var_ref_left) {
  391. m_ctx = 3;
  392. } else {
  393. m_ctx = 1;
  394. }
  395. } else if (LEFT_SINGLE || ABOVE_SINGLE) {
  396. auto vrfc = LEFT_SINGLE ? var_ref_above : var_ref_left;
  397. auto rfs = ABOVE_SINGLE ? var_ref_above : var_ref_left;
  398. if (vrfc == m_decoder.m_comp_var_ref[1] && rfs != m_decoder.m_comp_var_ref[1]) {
  399. m_ctx = 1;
  400. } else if (rfs == m_decoder.m_comp_var_ref[1] && vrfc != m_decoder.m_comp_var_ref[1]) {
  401. m_ctx = 2;
  402. } else {
  403. m_ctx = 4;
  404. }
  405. } else if (var_ref_above == var_ref_left) {
  406. m_ctx = 4;
  407. } else {
  408. m_ctx = 2;
  409. }
  410. }
  411. } else if (AVAIL_U) {
  412. if (ABOVE_INTRA) {
  413. m_ctx = 2;
  414. } else {
  415. if (ABOVE_SINGLE) {
  416. m_ctx = 3 * (ABOVE_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  417. } else {
  418. m_ctx = 4 * (m_decoder.m_above_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  419. }
  420. }
  421. } else if (AVAIL_L) {
  422. if (LEFT_INTRA) {
  423. m_ctx = 2;
  424. } else {
  425. if (LEFT_SINGLE) {
  426. m_ctx = 3 * (LEFT_FRAME_0 != m_decoder.m_comp_var_ref[1]);
  427. } else {
  428. m_ctx = 4 * (m_decoder.m_left_ref_frame[var_ref_idx] != m_decoder.m_comp_var_ref[1]);
  429. }
  430. }
  431. } else {
  432. m_ctx = 2;
  433. }
  434. return m_decoder.m_probability_tables->comp_ref_prob()[m_ctx];
  435. }
  436. u8 TreeParser::calculate_single_ref_p1_probability()
  437. {
  438. if (AVAIL_U && AVAIL_L) {
  439. if (ABOVE_INTRA && LEFT_INTRA) {
  440. m_ctx = 2;
  441. } else if (LEFT_INTRA) {
  442. if (ABOVE_SINGLE) {
  443. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  444. } else {
  445. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  446. }
  447. } else if (ABOVE_INTRA) {
  448. if (LEFT_SINGLE) {
  449. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  450. } else {
  451. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  452. }
  453. } else {
  454. if (LEFT_SINGLE && ABOVE_SINGLE) {
  455. m_ctx = 2 * (ABOVE_FRAME_0 == LastFrame) + 2 * (LEFT_FRAME_0 == LastFrame);
  456. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  457. auto above_is_last = ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame;
  458. auto left_is_last = LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame;
  459. m_ctx = 1 + (above_is_last || left_is_last);
  460. } else {
  461. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  462. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  463. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  464. m_ctx = crf1 == LastFrame || crf2 == LastFrame;
  465. if (rfs == LastFrame)
  466. m_ctx += 3;
  467. }
  468. }
  469. } else if (AVAIL_U) {
  470. if (ABOVE_INTRA) {
  471. m_ctx = 2;
  472. } else {
  473. if (ABOVE_SINGLE) {
  474. m_ctx = 4 * (ABOVE_FRAME_0 == LastFrame);
  475. } else {
  476. m_ctx = 1 + (ABOVE_FRAME_0 == LastFrame || ABOVE_FRAME_1 == LastFrame);
  477. }
  478. }
  479. } else if (AVAIL_L) {
  480. if (LEFT_INTRA) {
  481. m_ctx = 2;
  482. } else {
  483. if (LEFT_SINGLE) {
  484. m_ctx = 4 * (LEFT_FRAME_0 == LastFrame);
  485. } else {
  486. m_ctx = 1 + (LEFT_FRAME_0 == LastFrame || LEFT_FRAME_1 == LastFrame);
  487. }
  488. }
  489. } else {
  490. m_ctx = 2;
  491. }
  492. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][0];
  493. }
  494. u8 TreeParser::calculate_single_ref_p2_probability()
  495. {
  496. if (AVAIL_U && AVAIL_L) {
  497. if (ABOVE_INTRA && LEFT_INTRA) {
  498. m_ctx = 2;
  499. } else if (LEFT_INTRA) {
  500. if (ABOVE_SINGLE) {
  501. if (ABOVE_FRAME_0 == LastFrame) {
  502. m_ctx = 3;
  503. } else {
  504. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  505. }
  506. } else {
  507. m_ctx = 1 + 2 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  508. }
  509. } else if (ABOVE_INTRA) {
  510. if (LEFT_SINGLE) {
  511. if (LEFT_FRAME_0 == LastFrame) {
  512. m_ctx = 3;
  513. } else {
  514. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  515. }
  516. } else {
  517. m_ctx = 1 + 2 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  518. }
  519. } else {
  520. if (LEFT_SINGLE && ABOVE_SINGLE) {
  521. auto above_last = ABOVE_FRAME_0 == LastFrame;
  522. auto left_last = LEFT_FRAME_0 == LastFrame;
  523. if (above_last && left_last) {
  524. m_ctx = 3;
  525. } else if (above_last) {
  526. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  527. } else if (left_last) {
  528. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  529. } else {
  530. m_ctx = 2 * (ABOVE_FRAME_0 == GoldenFrame) + 2 * (LEFT_FRAME_0 == GoldenFrame);
  531. }
  532. } else if (!LEFT_SINGLE && !ABOVE_SINGLE) {
  533. if (ABOVE_FRAME_0 == LEFT_FRAME_0 && ABOVE_FRAME_1 == LEFT_FRAME_1) {
  534. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  535. } else {
  536. m_ctx = 2;
  537. }
  538. } else {
  539. auto rfs = ABOVE_SINGLE ? ABOVE_FRAME_0 : LEFT_FRAME_0;
  540. auto crf1 = ABOVE_SINGLE ? LEFT_FRAME_0 : ABOVE_FRAME_0;
  541. auto crf2 = ABOVE_SINGLE ? LEFT_FRAME_1 : ABOVE_FRAME_1;
  542. m_ctx = crf1 == GoldenFrame || crf2 == GoldenFrame;
  543. if (rfs == GoldenFrame) {
  544. m_ctx += 3;
  545. } else if (rfs != AltRefFrame) {
  546. m_ctx = 1 + (2 * m_ctx);
  547. }
  548. }
  549. }
  550. } else if (AVAIL_U) {
  551. if (ABOVE_INTRA || (ABOVE_FRAME_0 == LastFrame && ABOVE_SINGLE)) {
  552. m_ctx = 2;
  553. } else if (ABOVE_SINGLE) {
  554. m_ctx = 4 * (ABOVE_FRAME_0 == GoldenFrame);
  555. } else {
  556. m_ctx = 3 * (ABOVE_FRAME_0 == GoldenFrame || ABOVE_FRAME_1 == GoldenFrame);
  557. }
  558. } else if (AVAIL_L) {
  559. if (LEFT_INTRA || (LEFT_FRAME_0 == LastFrame && LEFT_SINGLE)) {
  560. m_ctx = 2;
  561. } else if (LEFT_SINGLE) {
  562. m_ctx = 4 * (LEFT_FRAME_0 == GoldenFrame);
  563. } else {
  564. m_ctx = 3 * (LEFT_FRAME_0 == GoldenFrame || LEFT_FRAME_1 == GoldenFrame);
  565. }
  566. } else {
  567. m_ctx = 2;
  568. }
  569. return m_decoder.m_probability_tables->single_ref_prob()[m_ctx][1];
  570. }
  571. u8 TreeParser::calculate_tx_size_probability(u8 node)
  572. {
  573. auto above = m_decoder.m_max_tx_size;
  574. auto left = m_decoder.m_max_tx_size;
  575. if (AVAIL_U) {
  576. auto u_pos = (m_decoder.m_mi_row - 1) * m_decoder.m_mi_cols + m_decoder.m_mi_col;
  577. if (!m_decoder.m_skips[u_pos])
  578. above = m_decoder.m_tx_sizes[u_pos];
  579. }
  580. if (AVAIL_L) {
  581. auto l_pos = m_decoder.m_mi_row * m_decoder.m_mi_cols + m_decoder.m_mi_col - 1;
  582. if (!m_decoder.m_skips[l_pos])
  583. left = m_decoder.m_tx_sizes[l_pos];
  584. }
  585. if (!AVAIL_L)
  586. left = above;
  587. if (!AVAIL_U)
  588. above = left;
  589. m_ctx = (above + left) > m_decoder.m_max_tx_size;
  590. return m_decoder.m_probability_tables->tx_probs()[m_decoder.m_max_tx_size][m_ctx][node];
  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::IsInter:
  666. increment(m_decoder.m_syntax_element_counter->m_counts_is_inter[m_ctx][value]);
  667. return;
  668. case SyntaxElementType::CompMode:
  669. increment(m_decoder.m_syntax_element_counter->m_counts_comp_mode[m_ctx][value]);
  670. return;
  671. case SyntaxElementType::CompRef:
  672. increment(m_decoder.m_syntax_element_counter->m_counts_comp_ref[m_ctx][value]);
  673. return;
  674. case SyntaxElementType::SingleRefP1:
  675. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][0][value]);
  676. return;
  677. case SyntaxElementType::SingleRefP2:
  678. increment(m_decoder.m_syntax_element_counter->m_counts_single_ref[m_ctx][1][value]);
  679. return;
  680. case SyntaxElementType::MVSign:
  681. increment(m_decoder.m_syntax_element_counter->m_counts_mv_sign[m_mv_component][value]);
  682. return;
  683. case SyntaxElementType::MVClass0Bit:
  684. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_bit[m_mv_component][value]);
  685. return;
  686. case SyntaxElementType::MVBit:
  687. VERIFY(m_mv_bit < MV_OFFSET_BITS);
  688. increment(m_decoder.m_syntax_element_counter->m_counts_mv_bits[m_mv_component][m_mv_bit][value]);
  689. m_mv_bit = 0xFF;
  690. return;
  691. case SyntaxElementType::TXSize:
  692. increment(m_decoder.m_syntax_element_counter->m_counts_tx_size[m_decoder.m_max_tx_size][m_ctx][value]);
  693. return;
  694. case SyntaxElementType::MVJoint:
  695. increment(m_decoder.m_syntax_element_counter->m_counts_mv_joint[value]);
  696. return;
  697. case SyntaxElementType::MVClass:
  698. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class[m_mv_component][value]);
  699. return;
  700. case SyntaxElementType::MVClass0FR:
  701. VERIFY(m_mv_class0_bit < CLASS0_SIZE);
  702. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_fr[m_mv_component][m_mv_class0_bit][value]);
  703. m_mv_class0_bit = 0xFF;
  704. return;
  705. case SyntaxElementType::MVClass0HP:
  706. increment(m_decoder.m_syntax_element_counter->m_counts_mv_class0_hp[m_mv_component][value]);
  707. return;
  708. case SyntaxElementType::MVFR:
  709. increment(m_decoder.m_syntax_element_counter->m_counts_mv_fr[m_mv_component][value]);
  710. return;
  711. case SyntaxElementType::MVHP:
  712. increment(m_decoder.m_syntax_element_counter->m_counts_mv_hp[m_mv_component][value]);
  713. return;
  714. case SyntaxElementType::Token:
  715. 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)]);
  716. return;
  717. case SyntaxElementType::MoreCoefs:
  718. 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]);
  719. return;
  720. default:
  721. break;
  722. }
  723. VERIFY_NOT_REACHED();
  724. }
  725. }