Decoder.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Decoder.h"
  7. #include "Utilities.h"
  8. namespace Video::VP9 {
  9. Decoder::Decoder()
  10. : m_parser(make<Parser>(*this))
  11. {
  12. }
  13. bool Decoder::decode_frame(ByteBuffer const& frame_data)
  14. {
  15. SAFE_CALL(m_parser->parse_frame(frame_data));
  16. // TODO:
  17. // - #2
  18. // - #3
  19. // - #4
  20. SAFE_CALL(update_reference_frames());
  21. return true;
  22. }
  23. void Decoder::dump_frame_info()
  24. {
  25. m_parser->dump_info();
  26. }
  27. u8 Decoder::merge_prob(u8 pre_prob, u8 count_0, u8 count_1, u8 count_sat, u8 max_update_factor)
  28. {
  29. auto total_decode_count = count_0 + count_1;
  30. auto prob = (total_decode_count == 0) ? 128 : clip_3(1, 255, (count_0 * 256 + (total_decode_count >> 1)) / total_decode_count);
  31. auto count = min(total_decode_count, count_sat);
  32. auto factor = (max_update_factor * count) / count_sat;
  33. return round_2(pre_prob * (256 - factor) + (prob * factor), 8);
  34. }
  35. u8 Decoder::merge_probs(int const* tree, int index, u8* probs, u8* counts, u8 count_sat, u8 max_update_factor)
  36. {
  37. auto s = tree[index];
  38. auto left_count = (s <= 0) ? counts[-s] : merge_probs(tree, s, probs, counts, count_sat, max_update_factor);
  39. auto r = tree[index + 1];
  40. auto right_count = (r <= 0) ? counts[-r] : merge_probs(tree, r, probs, counts, count_sat, max_update_factor);
  41. probs[index >> 1] = merge_prob(probs[index >> 1], left_count, right_count, count_sat, max_update_factor);
  42. return left_count + right_count;
  43. }
  44. bool Decoder::adapt_coef_probs()
  45. {
  46. u8 update_factor;
  47. if (m_parser->m_frame_is_intra || m_parser->m_last_frame_type != KeyFrame)
  48. update_factor = 112;
  49. else
  50. update_factor = 128;
  51. for (size_t t = 0; t < 4; t++) {
  52. for (size_t i = 0; i < 2; i++) {
  53. for (size_t j = 0; j < 2; j++) {
  54. for (size_t k = 0; k < 6; k++) {
  55. size_t max_l = (k == 0) ? 3 : 6;
  56. for (size_t l = 0; l < max_l; l++) {
  57. auto& coef_probs = m_parser->m_probability_tables->coef_probs()[t][i][j][k][l];
  58. merge_probs(small_token_tree, 2, coef_probs,
  59. m_parser->m_syntax_element_counter->m_counts_token[t][i][j][k][l],
  60. 24, update_factor);
  61. merge_probs(binary_tree, 0, coef_probs,
  62. m_parser->m_syntax_element_counter->m_counts_more_coefs[t][i][j][k][l],
  63. 24, update_factor);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. return true;
  70. }
  71. #define ADAPT_PROB_TABLE(name, size) \
  72. do { \
  73. for (size_t i = 0; i < (size); i++) { \
  74. auto table = probs.name##_prob(); \
  75. table[i] = adapt_prob(table[i], counter.m_counts_##name[i]); \
  76. } \
  77. } while (0)
  78. #define ADAPT_TREE(tree_name, prob_name, count_name, size) \
  79. do { \
  80. for (size_t i = 0; i < (size); i++) { \
  81. adapt_probs(tree_name##_tree, probs.prob_name##_probs()[i], counter.m_counts_##count_name[i]); \
  82. } \
  83. } while (0)
  84. bool Decoder::adapt_non_coef_probs()
  85. {
  86. auto& probs = *m_parser->m_probability_tables;
  87. auto& counter = *m_parser->m_syntax_element_counter;
  88. ADAPT_PROB_TABLE(is_inter, IS_INTER_CONTEXTS);
  89. ADAPT_PROB_TABLE(comp_mode, COMP_MODE_CONTEXTS);
  90. ADAPT_PROB_TABLE(comp_ref, REF_CONTEXTS);
  91. for (size_t i = 0; i < REF_CONTEXTS; i++) {
  92. for (size_t j = 0; j < 2; j++)
  93. probs.single_ref_prob()[i][j] = adapt_prob(probs.single_ref_prob()[i][j], counter.m_counts_single_ref[i][j]);
  94. }
  95. ADAPT_TREE(inter_mode, inter_mode, inter_mode, INTER_MODE_CONTEXTS);
  96. ADAPT_TREE(intra_mode, y_mode, intra_mode, INTER_MODE_CONTEXTS);
  97. ADAPT_TREE(intra_mode, uv_mode, uv_mode, INTER_MODE_CONTEXTS);
  98. ADAPT_TREE(partition, partition, partition, INTER_MODE_CONTEXTS);
  99. ADAPT_PROB_TABLE(skip, SKIP_CONTEXTS);
  100. if (m_parser->m_interpolation_filter == Switchable) {
  101. ADAPT_TREE(interp_filter, interp_filter, interp_filter, INTERP_FILTER_CONTEXTS);
  102. }
  103. if (m_parser->m_tx_mode == TXModeSelect) {
  104. for (size_t i = 0; i < TX_SIZE_CONTEXTS; i++) {
  105. auto& tx_probs = probs.tx_probs();
  106. auto& tx_counts = counter.m_counts_tx_size;
  107. adapt_probs(tx_size_8_tree, tx_probs[TX_8x8][i], tx_counts[TX_8x8][i]);
  108. adapt_probs(tx_size_16_tree, tx_probs[TX_16x16][i], tx_counts[TX_16x16][i]);
  109. adapt_probs(tx_size_32_tree, tx_probs[TX_32x32][i], tx_counts[TX_32x32][i]);
  110. }
  111. }
  112. adapt_probs(mv_joint_tree, probs.mv_joint_probs(), counter.m_counts_mv_joint);
  113. for (size_t i = 0; i < 2; i++) {
  114. probs.mv_sign_prob()[i] = adapt_prob(probs.mv_sign_prob()[i], counter.m_counts_mv_sign[i]);
  115. adapt_probs(mv_class_tree, probs.mv_class_probs()[i], counter.m_counts_mv_class[i]);
  116. probs.mv_class0_bit_prob()[i] = adapt_prob(probs.mv_class0_bit_prob()[i], counter.m_counts_mv_class0_bit[i]);
  117. for (size_t j = 0; j < MV_OFFSET_BITS; j++)
  118. probs.mv_bits_prob()[i][j] = adapt_prob(probs.mv_bits_prob()[i][j], counter.m_counts_mv_bits[i][j]);
  119. for (size_t j = 0; j < CLASS0_SIZE; j++)
  120. adapt_probs(mv_fr_tree, probs.mv_class0_fr_probs()[i][j], counter.m_counts_mv_class0_fr[i][j]);
  121. adapt_probs(mv_fr_tree, probs.mv_fr_probs()[i], counter.m_counts_mv_fr[i]);
  122. if (m_parser->m_allow_high_precision_mv) {
  123. probs.mv_class0_hp_prob()[i] = adapt_prob(probs.mv_class0_hp_prob()[i], counter.m_counts_mv_class0_hp[i]);
  124. probs.mv_hp_prob()[i] = adapt_prob(probs.mv_hp_prob()[i], counter.m_counts_mv_hp[i]);
  125. }
  126. }
  127. return true;
  128. }
  129. void Decoder::adapt_probs(int const* tree, u8* probs, u8* counts)
  130. {
  131. merge_probs(tree, 0, probs, counts, COUNT_SAT, MAX_UPDATE_FACTOR);
  132. }
  133. u8 Decoder::adapt_prob(u8 prob, u8 counts[2])
  134. {
  135. return merge_prob(prob, counts[0], counts[1], COUNT_SAT, MAX_UPDATE_FACTOR);
  136. }
  137. bool Decoder::predict_intra(size_t, u32, u32, bool, bool, bool, TXSize, u32)
  138. {
  139. // TODO: Implement
  140. return true;
  141. }
  142. bool Decoder::predict_inter(size_t, u32, u32, u32, u32, u32)
  143. {
  144. // TODO: Implement
  145. return true;
  146. }
  147. bool Decoder::reconstruct(size_t, u32, u32, TXSize)
  148. {
  149. // TODO: Implement
  150. return true;
  151. }
  152. bool Decoder::update_reference_frames()
  153. {
  154. for (auto i = 0; i < NUM_REF_FRAMES; i++) {
  155. dbgln("updating frame {}? {}", i, (m_parser->m_refresh_frame_flags & (1 << i)) == 1);
  156. if ((m_parser->m_refresh_frame_flags & (1 << i)) != 1)
  157. continue;
  158. m_parser->m_ref_frame_width[i] = m_parser->m_frame_width;
  159. m_parser->m_ref_frame_height[i] = m_parser->m_frame_height;
  160. // TODO: 1.3-1.7
  161. }
  162. // TODO: 2.1-2.2
  163. return true;
  164. }
  165. }