TreeParser.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. // Parsing of binary trees is handled here, as defined in sections 9.3.
  14. // Each syntax element is defined in its own section for each overarching section listed here:
  15. // - 9.3.1: Selection of the binary tree to be used.
  16. // - 9.3.2: Probability selection based on context and often the node of the tree.
  17. // - 9.3.4: Counting each syntax element when it is read.
  18. template<typename OutputType>
  19. inline ErrorOr<OutputType> parse_tree(BitStream& bit_stream, TreeParser::TreeSelection tree_selection, Function<u8(u8)> const& probability_getter)
  20. {
  21. // 9.3.3: The tree decoding function.
  22. if (tree_selection.is_single_value())
  23. return static_cast<OutputType>(tree_selection.single_value());
  24. int const* tree = tree_selection.tree();
  25. int n = 0;
  26. do {
  27. u8 node = n >> 1;
  28. n = tree[n + TRY(bit_stream.read_bool(probability_getter(node)))];
  29. } while (n > 0);
  30. return static_cast<OutputType>(-n);
  31. }
  32. inline void increment_counter(u8& counter)
  33. {
  34. counter = min(static_cast<u32>(counter) + 1, 255);
  35. }
  36. 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)
  37. {
  38. // Tree array
  39. TreeParser::TreeSelection tree = { PartitionSplit };
  40. if (has_rows && has_columns)
  41. tree = { partition_tree };
  42. else if (has_rows)
  43. tree = { rows_partition_tree };
  44. else if (has_columns)
  45. tree = { cols_partition_tree };
  46. // Probability array
  47. u32 above = 0;
  48. u32 left = 0;
  49. auto bsl = mi_width_log2_lookup[block_subsize];
  50. auto block_offset = mi_width_log2_lookup[Block_64x64] - bsl;
  51. for (auto i = 0; i < num_8x8; i++) {
  52. above |= above_partition_context[column + i];
  53. left |= left_partition_context[row + i];
  54. }
  55. above = (above & (1 << block_offset)) > 0;
  56. left = (left & (1 << block_offset)) > 0;
  57. auto context = bsl * 4 + left * 2 + above;
  58. u8 const* probabilities = frame_is_intra ? probability_table.kf_partition_probs()[context] : probability_table.partition_probs()[context];
  59. Function<u8(u8)> probability_getter = [&](u8 node) {
  60. if (has_rows && has_columns)
  61. return probabilities[node];
  62. if (has_columns)
  63. return probabilities[1];
  64. return probabilities[2];
  65. };
  66. auto value = TRY(parse_tree<Partition>(bit_stream, tree, probability_getter));
  67. increment_counter(counter.m_counts_partition[context][value]);
  68. return value;
  69. }
  70. ErrorOr<PredictionMode> TreeParser::parse_default_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, BlockSubsize mi_size, FrameBlockContext above, FrameBlockContext left, Array<PredictionMode, 4> const& block_sub_modes, u8 index_x, u8 index_y)
  71. {
  72. // FIXME: This should use a struct for the above and left contexts.
  73. // Tree
  74. TreeParser::TreeSelection tree = { intra_mode_tree };
  75. // Probabilities
  76. PredictionMode above_mode, left_mode;
  77. if (mi_size >= Block_8x8) {
  78. above_mode = above.sub_modes[2];
  79. left_mode = left.sub_modes[1];
  80. } else {
  81. if (index_y > 0)
  82. above_mode = block_sub_modes[index_x];
  83. else
  84. above_mode = above.sub_modes[2 + index_x];
  85. if (index_x > 0)
  86. left_mode = block_sub_modes[index_y << 1];
  87. else
  88. left_mode = left.sub_modes[1 + (index_y << 1)];
  89. }
  90. u8 const* probabilities = probability_table.kf_y_mode_probs()[to_underlying(above_mode)][to_underlying(left_mode)];
  91. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  92. // Default intra mode is not counted.
  93. return value;
  94. }
  95. ErrorOr<PredictionMode> TreeParser::parse_default_uv_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, PredictionMode y_mode)
  96. {
  97. // Tree
  98. TreeParser::TreeSelection tree = { intra_mode_tree };
  99. // Probabilities
  100. u8 const* probabilities = probability_table.kf_uv_mode_prob()[to_underlying(y_mode)];
  101. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  102. // Default UV mode is not counted.
  103. return value;
  104. }
  105. ErrorOr<PredictionMode> TreeParser::parse_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, BlockSubsize mi_size)
  106. {
  107. // Tree
  108. TreeParser::TreeSelection tree = { intra_mode_tree };
  109. // Probabilities
  110. auto context = size_group_lookup[mi_size];
  111. u8 const* probabilities = probability_table.y_mode_probs()[context];
  112. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  113. increment_counter(counter.m_counts_intra_mode[context][to_underlying(value)]);
  114. return value;
  115. }
  116. ErrorOr<PredictionMode> TreeParser::parse_sub_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter)
  117. {
  118. // Tree
  119. TreeParser::TreeSelection tree = { intra_mode_tree };
  120. // Probabilities
  121. u8 const* probabilities = probability_table.y_mode_probs()[0];
  122. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  123. increment_counter(counter.m_counts_intra_mode[0][to_underlying(value)]);
  124. return value;
  125. }
  126. ErrorOr<PredictionMode> TreeParser::parse_uv_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, PredictionMode y_mode)
  127. {
  128. // Tree
  129. TreeParser::TreeSelection tree = { intra_mode_tree };
  130. // Probabilities
  131. u8 const* probabilities = probability_table.uv_mode_probs()[to_underlying(y_mode)];
  132. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  133. increment_counter(counter.m_counts_uv_mode[to_underlying(y_mode)][to_underlying(value)]);
  134. return value;
  135. }
  136. ErrorOr<u8> TreeParser::parse_segment_id(BitStream& bit_stream, Array<u8, 7> const& probabilities)
  137. {
  138. auto value = TRY(parse_tree<u8>(bit_stream, { segment_tree }, [&](u8 node) { return probabilities[node]; }));
  139. // Segment ID is not counted.
  140. return value;
  141. }
  142. ErrorOr<bool> TreeParser::parse_segment_id_predicted(BitStream& bit_stream, Array<u8, 3> const& probabilities, u8 above_seg_pred_context, u8 left_seg_pred_context)
  143. {
  144. auto context = left_seg_pred_context + above_seg_pred_context;
  145. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probabilities[context]; }));
  146. // Segment ID prediction is not counted.
  147. return value;
  148. }
  149. ErrorOr<PredictionMode> TreeParser::parse_inter_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 mode_context_for_ref_frame_0)
  150. {
  151. // Tree
  152. TreeParser::TreeSelection tree = { inter_mode_tree };
  153. // Probabilities
  154. u8 const* probabilities = probability_table.inter_mode_probs()[mode_context_for_ref_frame_0];
  155. auto value = TRY(parse_tree<PredictionMode>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  156. increment_counter(counter.m_counts_inter_mode[mode_context_for_ref_frame_0][to_underlying(value) - to_underlying(PredictionMode::NearestMv)]);
  157. return value;
  158. }
  159. ErrorOr<InterpolationFilter> TreeParser::parse_interpolation_filter(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, FrameBlockContext above, FrameBlockContext left)
  160. {
  161. // FIXME: Above and left context should be provided by a struct.
  162. // Tree
  163. TreeParser::TreeSelection tree = { interp_filter_tree };
  164. // Probabilities
  165. // NOTE: SWITCHABLE_FILTERS is not used in the spec for this function. Therefore, the number
  166. // was demystified by referencing the reference codec libvpx:
  167. // https://github.com/webmproject/libvpx/blob/705bf9de8c96cfe5301451f1d7e5c90a41c64e5f/vp9/common/vp9_pred_common.h#L69
  168. u8 left_interp = !left.is_intra_predicted() ? left.interpolation_filter : SWITCHABLE_FILTERS;
  169. u8 above_interp = !above.is_intra_predicted() ? above.interpolation_filter : SWITCHABLE_FILTERS;
  170. u8 context = SWITCHABLE_FILTERS;
  171. if (above_interp == left_interp || above_interp == SWITCHABLE_FILTERS)
  172. context = left_interp;
  173. else if (left_interp == SWITCHABLE_FILTERS)
  174. context = above_interp;
  175. u8 const* probabilities = probability_table.interp_filter_probs()[context];
  176. auto value = TRY(parse_tree<InterpolationFilter>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  177. increment_counter(counter.m_counts_interp_filter[context][to_underlying(value)]);
  178. return value;
  179. }
  180. ErrorOr<bool> TreeParser::parse_skip(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, FrameBlockContext above, FrameBlockContext left)
  181. {
  182. // Probabilities
  183. u8 context = 0;
  184. context += static_cast<u8>(above.skip_coefficients);
  185. context += static_cast<u8>(left.skip_coefficients);
  186. u8 probability = probability_table.skip_prob()[context];
  187. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  188. increment_counter(counter.m_counts_skip[context][value]);
  189. return value;
  190. }
  191. ErrorOr<TXSize> TreeParser::parse_tx_size(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, TXSize max_tx_size, FrameBlockContext above, FrameBlockContext left)
  192. {
  193. // FIXME: Above and left contexts should be in structs.
  194. // Tree
  195. TreeParser::TreeSelection tree { tx_size_8_tree };
  196. if (max_tx_size == TX_16x16)
  197. tree = { tx_size_16_tree };
  198. if (max_tx_size == TX_32x32)
  199. tree = { tx_size_32_tree };
  200. // Probabilities
  201. auto above_context = max_tx_size;
  202. auto left_context = max_tx_size;
  203. if (above.is_available && !above.skip_coefficients)
  204. above_context = above.tx_size;
  205. if (left.is_available && !left.skip_coefficients)
  206. left_context = left.tx_size;
  207. if (!left.is_available)
  208. left_context = above_context;
  209. if (!above.is_available)
  210. above_context = left_context;
  211. auto context = (above_context + left_context) > max_tx_size;
  212. u8 const* probabilities = probability_table.tx_probs()[max_tx_size][context];
  213. auto value = TRY(parse_tree<TXSize>(bit_stream, tree, [&](u8 node) { return probabilities[node]; }));
  214. increment_counter(counter.m_counts_tx_size[max_tx_size][context][value]);
  215. return value;
  216. }
  217. ErrorOr<bool> TreeParser::parse_block_is_inter_predicted(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, FrameBlockContext above, FrameBlockContext left)
  218. {
  219. // FIXME: Above and left contexts should be in structs.
  220. // Probabilities
  221. u8 context = 0;
  222. if (above.is_available && left.is_available)
  223. context = (left.is_intra_predicted() && above.is_intra_predicted()) ? 3 : static_cast<u8>(above.is_intra_predicted() || left.is_intra_predicted());
  224. else if (above.is_available || left.is_available)
  225. context = 2 * static_cast<u8>(above.is_available ? above.is_intra_predicted() : left.is_intra_predicted());
  226. u8 probability = probability_table.is_inter_prob()[context];
  227. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  228. increment_counter(counter.m_counts_is_inter[context][value]);
  229. return value;
  230. }
  231. ErrorOr<ReferenceMode> TreeParser::parse_comp_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, ReferenceFrameType comp_fixed_ref, FrameBlockContext above, FrameBlockContext left)
  232. {
  233. // FIXME: Above and left contexts should be in structs.
  234. // Probabilities
  235. u8 context;
  236. if (above.is_available && left.is_available) {
  237. if (above.is_single_reference() && left.is_single_reference()) {
  238. auto is_above_fixed = above.ref_frames.primary == comp_fixed_ref;
  239. auto is_left_fixed = left.ref_frames.primary == comp_fixed_ref;
  240. context = is_above_fixed ^ is_left_fixed;
  241. } else if (above.is_single_reference()) {
  242. auto is_above_fixed = above.ref_frames.primary == comp_fixed_ref;
  243. context = 2 + static_cast<u8>(is_above_fixed || above.is_intra_predicted());
  244. } else if (left.is_single_reference()) {
  245. auto is_left_fixed = left.ref_frames.primary == comp_fixed_ref;
  246. context = 2 + static_cast<u8>(is_left_fixed || left.is_intra_predicted());
  247. } else {
  248. context = 4;
  249. }
  250. } else if (above.is_available) {
  251. if (above.is_single_reference())
  252. context = above.ref_frames.primary == comp_fixed_ref;
  253. else
  254. context = 3;
  255. } else if (left.is_available) {
  256. if (left.is_single_reference())
  257. context = static_cast<u8>(left.ref_frames.primary == comp_fixed_ref);
  258. else
  259. context = 3;
  260. } else {
  261. context = 1;
  262. }
  263. u8 probability = probability_table.comp_mode_prob()[context];
  264. auto value = TRY(parse_tree<ReferenceMode>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  265. increment_counter(counter.m_counts_comp_mode[context][value]);
  266. return value;
  267. }
  268. ErrorOr<ReferenceIndex> TreeParser::parse_comp_ref(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, ReferenceFrameType comp_fixed_ref, ReferenceFramePair comp_var_ref, ReferenceIndex variable_reference_index, FrameBlockContext above, FrameBlockContext left)
  269. {
  270. // FIXME: Above and left contexts should be in structs.
  271. // Probabilities
  272. u8 context;
  273. if (above.is_available && left.is_available) {
  274. if (above.is_intra_predicted() && left.is_intra_predicted()) {
  275. context = 2;
  276. } else if (left.is_intra_predicted()) {
  277. if (above.is_single_reference()) {
  278. context = 1 + 2 * (above.ref_frames.primary != comp_var_ref.secondary);
  279. } else {
  280. context = 1 + 2 * (above.ref_frames[variable_reference_index] != comp_var_ref.secondary);
  281. }
  282. } else if (above.is_intra_predicted()) {
  283. if (left.is_single_reference()) {
  284. context = 1 + 2 * (left.ref_frames.primary != comp_var_ref.secondary);
  285. } else {
  286. context = 1 + 2 * (left.ref_frames[variable_reference_index] != comp_var_ref.secondary);
  287. }
  288. } else {
  289. auto var_ref_above = above.is_single_reference() ? above.ref_frames.primary : above.ref_frames[variable_reference_index];
  290. auto var_ref_left = left.is_single_reference() ? left.ref_frames.primary : left.ref_frames[variable_reference_index];
  291. if (var_ref_above == var_ref_left && comp_var_ref.secondary == var_ref_above) {
  292. context = 0;
  293. } else if (left.is_single_reference() && above.is_single_reference()) {
  294. if ((var_ref_above == comp_fixed_ref && var_ref_left == comp_var_ref.primary)
  295. || (var_ref_left == comp_fixed_ref && var_ref_above == comp_var_ref.primary)) {
  296. context = 4;
  297. } else if (var_ref_above == var_ref_left) {
  298. context = 3;
  299. } else {
  300. context = 1;
  301. }
  302. } else if (left.is_single_reference() || above.is_single_reference()) {
  303. auto vrfc = left.is_single_reference() ? var_ref_above : var_ref_left;
  304. auto rfs = above.is_single_reference() ? var_ref_above : var_ref_left;
  305. if (vrfc == comp_var_ref.secondary && rfs != comp_var_ref.secondary) {
  306. context = 1;
  307. } else if (rfs == comp_var_ref.secondary && vrfc != comp_var_ref.secondary) {
  308. context = 2;
  309. } else {
  310. context = 4;
  311. }
  312. } else if (var_ref_above == var_ref_left) {
  313. context = 4;
  314. } else {
  315. context = 2;
  316. }
  317. }
  318. } else if (above.is_available) {
  319. if (above.is_intra_predicted()) {
  320. context = 2;
  321. } else {
  322. if (above.is_single_reference()) {
  323. context = 3 * static_cast<u8>(above.ref_frames.primary != comp_var_ref.secondary);
  324. } else {
  325. context = 4 * static_cast<u8>(above.ref_frames[variable_reference_index] != comp_var_ref.secondary);
  326. }
  327. }
  328. } else if (left.is_available) {
  329. if (left.is_intra_predicted()) {
  330. context = 2;
  331. } else {
  332. if (left.is_single_reference()) {
  333. context = 3 * static_cast<u8>(left.ref_frames.primary != comp_var_ref.secondary);
  334. } else {
  335. context = 4 * static_cast<u8>(left.ref_frames[variable_reference_index] != comp_var_ref.secondary);
  336. }
  337. }
  338. } else {
  339. context = 2;
  340. }
  341. u8 probability = probability_table.comp_ref_prob()[context];
  342. auto value = TRY(parse_tree<ReferenceIndex>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  343. increment_counter(counter.m_counts_comp_ref[context][to_underlying(value)]);
  344. return value;
  345. }
  346. ErrorOr<bool> TreeParser::parse_single_ref_part_1(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, FrameBlockContext above, FrameBlockContext left)
  347. {
  348. // FIXME: Above and left contexts should be in structs.
  349. // Probabilities
  350. u8 context;
  351. if (above.is_available && left.is_available) {
  352. if (above.is_intra_predicted() && left.is_intra_predicted()) {
  353. context = 2;
  354. } else if (left.is_intra_predicted()) {
  355. if (above.is_single_reference()) {
  356. context = 4 * (above.ref_frames.primary == LastFrame);
  357. } else {
  358. context = 1 + (above.ref_frames.primary == LastFrame || above.ref_frames.secondary == LastFrame);
  359. }
  360. } else if (above.is_intra_predicted()) {
  361. if (left.is_single_reference()) {
  362. context = 4 * (left.ref_frames.primary == LastFrame);
  363. } else {
  364. context = 1 + (left.ref_frames.primary == LastFrame || left.ref_frames.secondary == LastFrame);
  365. }
  366. } else {
  367. if (left.is_single_reference() && above.is_single_reference()) {
  368. context = 2 * (above.ref_frames.primary == LastFrame) + 2 * (left.ref_frames.primary == LastFrame);
  369. } else if (!left.is_single_reference() && !above.is_single_reference()) {
  370. auto above_used_last_frame = above.ref_frames.primary == LastFrame || above.ref_frames.secondary == LastFrame;
  371. auto left_used_last_frame = left.ref_frames.primary == LastFrame || left.ref_frames.secondary == LastFrame;
  372. context = 1 + (above_used_last_frame || left_used_last_frame);
  373. } else {
  374. auto single_reference_type = above.is_single_reference() ? above.ref_frames.primary : left.ref_frames.primary;
  375. auto compound_reference_a_type = above.is_single_reference() ? left.ref_frames.primary : above.ref_frames.primary;
  376. auto compound_reference_b_type = above.is_single_reference() ? left.ref_frames.secondary : above.ref_frames.secondary;
  377. context = compound_reference_a_type == LastFrame || compound_reference_b_type == LastFrame;
  378. if (single_reference_type == LastFrame)
  379. context += 3;
  380. }
  381. }
  382. } else if (above.is_available) {
  383. if (above.is_intra_predicted()) {
  384. context = 2;
  385. } else {
  386. if (above.is_single_reference()) {
  387. context = 4 * (above.ref_frames.primary == LastFrame);
  388. } else {
  389. context = 1 + (above.ref_frames.primary == LastFrame || above.ref_frames.secondary == LastFrame);
  390. }
  391. }
  392. } else if (left.is_available) {
  393. if (left.is_intra_predicted()) {
  394. context = 2;
  395. } else {
  396. if (left.is_single_reference()) {
  397. context = 4 * (left.ref_frames.primary == LastFrame);
  398. } else {
  399. context = 1 + (left.ref_frames.primary == LastFrame || left.ref_frames.secondary == LastFrame);
  400. }
  401. }
  402. } else {
  403. context = 2;
  404. }
  405. u8 probability = probability_table.single_ref_prob()[context][0];
  406. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  407. increment_counter(counter.m_counts_single_ref[context][0][value]);
  408. return value;
  409. }
  410. ErrorOr<bool> TreeParser::parse_single_ref_part_2(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, FrameBlockContext above, FrameBlockContext left)
  411. {
  412. // FIXME: Above and left contexts should be in structs.
  413. // Probabilities
  414. u8 context;
  415. if (above.is_available && left.is_available) {
  416. if (above.is_intra_predicted() && left.is_intra_predicted()) {
  417. context = 2;
  418. } else if (left.is_intra_predicted()) {
  419. if (above.is_single_reference()) {
  420. if (above.ref_frames.primary == LastFrame) {
  421. context = 3;
  422. } else {
  423. context = 4 * (above.ref_frames.primary == GoldenFrame);
  424. }
  425. } else {
  426. context = 1 + 2 * (above.ref_frames.primary == GoldenFrame || above.ref_frames.secondary == GoldenFrame);
  427. }
  428. } else if (above.is_intra_predicted()) {
  429. if (left.is_single_reference()) {
  430. if (left.ref_frames.primary == LastFrame) {
  431. context = 3;
  432. } else {
  433. context = 4 * (left.ref_frames.primary == GoldenFrame);
  434. }
  435. } else {
  436. context = 1 + 2 * (left.ref_frames.primary == GoldenFrame || left.ref_frames.secondary == GoldenFrame);
  437. }
  438. } else {
  439. if (left.is_single_reference() && above.is_single_reference()) {
  440. auto above_last = above.ref_frames.primary == LastFrame;
  441. auto left_last = left.ref_frames.primary == LastFrame;
  442. if (above_last && left_last) {
  443. context = 3;
  444. } else if (above_last) {
  445. context = 4 * (left.ref_frames.primary == GoldenFrame);
  446. } else if (left_last) {
  447. context = 4 * (above.ref_frames.primary == GoldenFrame);
  448. } else {
  449. context = 2 * (above.ref_frames.primary == GoldenFrame) + 2 * (left.ref_frames.primary == GoldenFrame);
  450. }
  451. } else if (!left.is_single_reference() && !above.is_single_reference()) {
  452. if (above.ref_frames.primary == left.ref_frames.primary && above.ref_frames.secondary == left.ref_frames.secondary) {
  453. context = 3 * (above.ref_frames.primary == GoldenFrame || above.ref_frames.secondary == GoldenFrame);
  454. } else {
  455. context = 2;
  456. }
  457. } else {
  458. auto single_reference_type = above.is_single_reference() ? above.ref_frames.primary : left.ref_frames.primary;
  459. auto compound_reference_a_type = above.is_single_reference() ? left.ref_frames.primary : above.ref_frames.primary;
  460. auto compound_reference_b_type = above.is_single_reference() ? left.ref_frames.secondary : above.ref_frames.secondary;
  461. context = compound_reference_a_type == GoldenFrame || compound_reference_b_type == GoldenFrame;
  462. if (single_reference_type == GoldenFrame) {
  463. context += 3;
  464. } else if (single_reference_type != AltRefFrame) {
  465. context = 1 + (2 * context);
  466. }
  467. }
  468. }
  469. } else if (above.is_available) {
  470. if (above.is_intra_predicted() || (above.ref_frames.primary == LastFrame && above.is_single_reference())) {
  471. context = 2;
  472. } else if (above.is_single_reference()) {
  473. context = 4 * (above.ref_frames.primary == GoldenFrame);
  474. } else {
  475. context = 3 * (above.ref_frames.primary == GoldenFrame || above.ref_frames.secondary == GoldenFrame);
  476. }
  477. } else if (left.is_available) {
  478. if (left.is_intra_predicted() || (left.ref_frames.primary == LastFrame && left.is_single_reference())) {
  479. context = 2;
  480. } else if (left.is_single_reference()) {
  481. context = 4 * (left.ref_frames.primary == GoldenFrame);
  482. } else {
  483. context = 3 * (left.ref_frames.primary == GoldenFrame || left.ref_frames.secondary == GoldenFrame);
  484. }
  485. } else {
  486. context = 2;
  487. }
  488. u8 probability = probability_table.single_ref_prob()[context][1];
  489. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  490. increment_counter(counter.m_counts_single_ref[context][1][value]);
  491. return value;
  492. }
  493. ErrorOr<MvJoint> TreeParser::parse_motion_vector_joint(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter)
  494. {
  495. auto value = TRY(parse_tree<MvJoint>(bit_stream, { mv_joint_tree }, [&](u8 node) { return probability_table.mv_joint_probs()[node]; }));
  496. increment_counter(counter.m_counts_mv_joint[value]);
  497. return value;
  498. }
  499. ErrorOr<bool> TreeParser::parse_motion_vector_sign(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component)
  500. {
  501. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability_table.mv_sign_prob()[component]; }));
  502. increment_counter(counter.m_counts_mv_sign[component][value]);
  503. return value;
  504. }
  505. ErrorOr<MvClass> TreeParser::parse_motion_vector_class(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component)
  506. {
  507. // Spec doesn't mention node, but the probabilities table has an extra dimension
  508. // so we will use node for that.
  509. auto value = TRY(parse_tree<MvClass>(bit_stream, { mv_class_tree }, [&](u8 node) { return probability_table.mv_class_probs()[component][node]; }));
  510. increment_counter(counter.m_counts_mv_class[component][value]);
  511. return value;
  512. }
  513. ErrorOr<bool> TreeParser::parse_motion_vector_class0_bit(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component)
  514. {
  515. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability_table.mv_class0_bit_prob()[component]; }));
  516. increment_counter(counter.m_counts_mv_class0_bit[component][value]);
  517. return value;
  518. }
  519. ErrorOr<u8> TreeParser::parse_motion_vector_class0_fr(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component, bool class_0_bit)
  520. {
  521. auto value = TRY(parse_tree<u8>(bit_stream, { mv_fr_tree }, [&](u8 node) { return probability_table.mv_class0_fr_probs()[component][class_0_bit][node]; }));
  522. increment_counter(counter.m_counts_mv_class0_fr[component][class_0_bit][value]);
  523. return value;
  524. }
  525. ErrorOr<bool> TreeParser::parse_motion_vector_class0_hp(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component, bool use_hp)
  526. {
  527. TreeParser::TreeSelection tree { 1 };
  528. if (use_hp)
  529. tree = { binary_tree };
  530. auto value = TRY(parse_tree<bool>(bit_stream, tree, [&](u8) { return probability_table.mv_class0_hp_prob()[component]; }));
  531. increment_counter(counter.m_counts_mv_class0_hp[component][value]);
  532. return value;
  533. }
  534. ErrorOr<bool> TreeParser::parse_motion_vector_bit(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component, u8 bit_index)
  535. {
  536. auto value = TRY(parse_tree<bool>(bit_stream, { binary_tree }, [&](u8) { return probability_table.mv_bits_prob()[component][bit_index]; }));
  537. increment_counter(counter.m_counts_mv_bits[component][bit_index][value]);
  538. return value;
  539. }
  540. ErrorOr<u8> TreeParser::parse_motion_vector_fr(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component)
  541. {
  542. auto value = TRY(parse_tree<u8>(bit_stream, { mv_fr_tree }, [&](u8 node) { return probability_table.mv_fr_probs()[component][node]; }));
  543. increment_counter(counter.m_counts_mv_fr[component][value]);
  544. return value;
  545. }
  546. ErrorOr<bool> TreeParser::parse_motion_vector_hp(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, u8 component, bool use_hp)
  547. {
  548. TreeParser::TreeSelection tree { 1 };
  549. if (use_hp)
  550. tree = { binary_tree };
  551. auto value = TRY(parse_tree<u8>(bit_stream, tree, [&](u8) { return probability_table.mv_hp_prob()[component]; }));
  552. increment_counter(counter.m_counts_mv_hp[component][value]);
  553. return value;
  554. }
  555. TokensContext TreeParser::get_tokens_context(bool subsampling_x, bool subsampling_y, u32 rows, u32 columns, Array<Vector<bool>, 3> const& above_nonzero_context, Array<Vector<bool>, 3> const& left_nonzero_context, u8 token_cache[1024], TXSize tx_size, u8 tx_type, u8 plane, u32 start_x, u32 start_y, u32 position, bool is_inter, u8 band, u32 c)
  556. {
  557. u8 context;
  558. if (c == 0) {
  559. auto sx = plane > 0 ? subsampling_x : false;
  560. auto sy = plane > 0 ? subsampling_y : false;
  561. auto max_x = (2 * columns) >> sx;
  562. auto max_y = (2 * rows) >> sy;
  563. u8 numpts = 1 << tx_size;
  564. auto x4 = start_x >> 2;
  565. auto y4 = start_y >> 2;
  566. u32 above = 0;
  567. u32 left = 0;
  568. for (size_t i = 0; i < numpts; i++) {
  569. if (x4 + i < max_x)
  570. above |= above_nonzero_context[plane][x4 + i];
  571. if (y4 + i < max_y)
  572. left |= left_nonzero_context[plane][y4 + i];
  573. }
  574. context = above + left;
  575. } else {
  576. u32 neighbor_0, neighbor_1;
  577. auto n = 4 << tx_size;
  578. auto i = position / n;
  579. auto j = position % n;
  580. auto a = i > 0 ? (i - 1) * n + j : 0;
  581. auto a2 = i * n + j - 1;
  582. if (i > 0 && j > 0) {
  583. if (tx_type == DCT_ADST) {
  584. neighbor_0 = a;
  585. neighbor_1 = a;
  586. } else if (tx_type == ADST_DCT) {
  587. neighbor_0 = a2;
  588. neighbor_1 = a2;
  589. } else {
  590. neighbor_0 = a;
  591. neighbor_1 = a2;
  592. }
  593. } else if (i > 0) {
  594. neighbor_0 = a;
  595. neighbor_1 = a;
  596. } else {
  597. neighbor_0 = a2;
  598. neighbor_1 = a2;
  599. }
  600. context = (1 + token_cache[neighbor_0] + token_cache[neighbor_1]) >> 1;
  601. }
  602. return TokensContext { tx_size, plane > 0, is_inter, band, context };
  603. }
  604. ErrorOr<bool> TreeParser::parse_more_coefficients(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, TokensContext const& context)
  605. {
  606. auto probability = probability_table.coef_probs()[context.m_tx_size][context.m_is_uv_plane][context.m_is_inter][context.m_band][context.m_context_index][0];
  607. auto value = TRY(parse_tree<u8>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
  608. increment_counter(counter.m_counts_more_coefs[context.m_tx_size][context.m_is_uv_plane][context.m_is_inter][context.m_band][context.m_context_index][value]);
  609. return value;
  610. }
  611. ErrorOr<Token> TreeParser::parse_token(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, TokensContext const& context)
  612. {
  613. Function<u8(u8)> probability_getter = [&](u8 node) -> u8 {
  614. auto prob = probability_table.coef_probs()[context.m_tx_size][context.m_is_uv_plane][context.m_is_inter][context.m_band][context.m_context_index][min(2, 1 + node)];
  615. if (node < 2)
  616. return prob;
  617. auto x = (prob - 1) / 2;
  618. auto const& pareto_table = probability_table.pareto_table();
  619. if ((prob & 1) != 0)
  620. return pareto_table[x][node - 2];
  621. return (pareto_table[x][node - 2] + pareto_table[x + 1][node - 2]) >> 1;
  622. };
  623. auto value = TRY(parse_tree<Token>(bit_stream, { token_tree }, probability_getter));
  624. increment_counter(counter.m_counts_token[context.m_tx_size][context.m_is_uv_plane][context.m_is_inter][context.m_band][context.m_context_index][min(2, value)]);
  625. return value;
  626. }
  627. }