TreeParser.cpp 31 KB

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