vp9.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring> // for std::memcpy
  5. #include <numeric>
  6. #include "video_core/command_classes/codecs/vp9.h"
  7. #include "video_core/gpu.h"
  8. #include "video_core/memory_manager.h"
  9. namespace Tegra::Decoder {
  10. namespace {
  11. // Default compressed header probabilities once frame context resets
  12. constexpr Vp9EntropyProbs default_probs{
  13. .y_mode_prob{
  14. 65, 32, 18, 144, 162, 194, 41, 51, 98, 132, 68, 18, 165, 217, 196, 45, 40, 78,
  15. 173, 80, 19, 176, 240, 193, 64, 35, 46, 221, 135, 38, 194, 248, 121, 96, 85, 29,
  16. },
  17. .partition_prob{
  18. 199, 122, 141, 0, 147, 63, 159, 0, 148, 133, 118, 0, 121, 104, 114, 0,
  19. 174, 73, 87, 0, 92, 41, 83, 0, 82, 99, 50, 0, 53, 39, 39, 0,
  20. 177, 58, 59, 0, 68, 26, 63, 0, 52, 79, 25, 0, 17, 14, 12, 0,
  21. 222, 34, 30, 0, 72, 16, 44, 0, 58, 32, 12, 0, 10, 7, 6, 0,
  22. },
  23. .coef_probs{
  24. 195, 29, 183, 84, 49, 136, 8, 42, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 31, 107, 169, 35, 99, 159, 17, 82, 140, 8, 66, 114, 2, 44, 76, 1, 19, 32,
  26. 40, 132, 201, 29, 114, 187, 13, 91, 157, 7, 75, 127, 3, 58, 95, 1, 28, 47,
  27. 69, 142, 221, 42, 122, 201, 15, 91, 159, 6, 67, 121, 1, 42, 77, 1, 17, 31,
  28. 102, 148, 228, 67, 117, 204, 17, 82, 154, 6, 59, 114, 2, 39, 75, 1, 15, 29,
  29. 156, 57, 233, 119, 57, 212, 58, 48, 163, 29, 40, 124, 12, 30, 81, 3, 12, 31,
  30. 191, 107, 226, 124, 117, 204, 25, 99, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 29, 148, 210, 37, 126, 194, 8, 93, 157, 2, 68, 118, 1, 39, 69, 1, 17, 33,
  32. 41, 151, 213, 27, 123, 193, 3, 82, 144, 1, 58, 105, 1, 32, 60, 1, 13, 26,
  33. 59, 159, 220, 23, 126, 198, 4, 88, 151, 1, 66, 114, 1, 38, 71, 1, 18, 34,
  34. 114, 136, 232, 51, 114, 207, 11, 83, 155, 3, 56, 105, 1, 33, 65, 1, 17, 34,
  35. 149, 65, 234, 121, 57, 215, 61, 49, 166, 28, 36, 114, 12, 25, 76, 3, 16, 42,
  36. 214, 49, 220, 132, 63, 188, 42, 65, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37. 85, 137, 221, 104, 131, 216, 49, 111, 192, 21, 87, 155, 2, 49, 87, 1, 16, 28,
  38. 89, 163, 230, 90, 137, 220, 29, 100, 183, 10, 70, 135, 2, 42, 81, 1, 17, 33,
  39. 108, 167, 237, 55, 133, 222, 15, 97, 179, 4, 72, 135, 1, 45, 85, 1, 19, 38,
  40. 124, 146, 240, 66, 124, 224, 17, 88, 175, 4, 58, 122, 1, 36, 75, 1, 18, 37,
  41. 141, 79, 241, 126, 70, 227, 66, 58, 182, 30, 44, 136, 12, 34, 96, 2, 20, 47,
  42. 229, 99, 249, 143, 111, 235, 46, 109, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 82, 158, 236, 94, 146, 224, 25, 117, 191, 9, 87, 149, 3, 56, 99, 1, 33, 57,
  44. 83, 167, 237, 68, 145, 222, 10, 103, 177, 2, 72, 131, 1, 41, 79, 1, 20, 39,
  45. 99, 167, 239, 47, 141, 224, 10, 104, 178, 2, 73, 133, 1, 44, 85, 1, 22, 47,
  46. 127, 145, 243, 71, 129, 228, 17, 93, 177, 3, 61, 124, 1, 41, 84, 1, 21, 52,
  47. 157, 78, 244, 140, 72, 231, 69, 58, 184, 31, 44, 137, 14, 38, 105, 8, 23, 61,
  48. 125, 34, 187, 52, 41, 133, 6, 31, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 37, 109, 153, 51, 102, 147, 23, 87, 128, 8, 67, 101, 1, 41, 63, 1, 19, 29,
  50. 31, 154, 185, 17, 127, 175, 6, 96, 145, 2, 73, 114, 1, 51, 82, 1, 28, 45,
  51. 23, 163, 200, 10, 131, 185, 2, 93, 148, 1, 67, 111, 1, 41, 69, 1, 14, 24,
  52. 29, 176, 217, 12, 145, 201, 3, 101, 156, 1, 69, 111, 1, 39, 63, 1, 14, 23,
  53. 57, 192, 233, 25, 154, 215, 6, 109, 167, 3, 78, 118, 1, 48, 69, 1, 21, 29,
  54. 202, 105, 245, 108, 106, 216, 18, 90, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 33, 172, 219, 64, 149, 206, 14, 117, 177, 5, 90, 141, 2, 61, 95, 1, 37, 57,
  56. 33, 179, 220, 11, 140, 198, 1, 89, 148, 1, 60, 104, 1, 33, 57, 1, 12, 21,
  57. 30, 181, 221, 8, 141, 198, 1, 87, 145, 1, 58, 100, 1, 31, 55, 1, 12, 20,
  58. 32, 186, 224, 7, 142, 198, 1, 86, 143, 1, 58, 100, 1, 31, 55, 1, 12, 22,
  59. 57, 192, 227, 20, 143, 204, 3, 96, 154, 1, 68, 112, 1, 42, 69, 1, 19, 32,
  60. 212, 35, 215, 113, 47, 169, 29, 48, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 74, 129, 203, 106, 120, 203, 49, 107, 178, 19, 84, 144, 4, 50, 84, 1, 15, 25,
  62. 71, 172, 217, 44, 141, 209, 15, 102, 173, 6, 76, 133, 2, 51, 89, 1, 24, 42,
  63. 64, 185, 231, 31, 148, 216, 8, 103, 175, 3, 74, 131, 1, 46, 81, 1, 18, 30,
  64. 65, 196, 235, 25, 157, 221, 5, 105, 174, 1, 67, 120, 1, 38, 69, 1, 15, 30,
  65. 65, 204, 238, 30, 156, 224, 7, 107, 177, 2, 70, 124, 1, 42, 73, 1, 18, 34,
  66. 225, 86, 251, 144, 104, 235, 42, 99, 181, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 85, 175, 239, 112, 165, 229, 29, 136, 200, 12, 103, 162, 6, 77, 123, 2, 53, 84,
  68. 75, 183, 239, 30, 155, 221, 3, 106, 171, 1, 74, 128, 1, 44, 76, 1, 17, 28,
  69. 73, 185, 240, 27, 159, 222, 2, 107, 172, 1, 75, 127, 1, 42, 73, 1, 17, 29,
  70. 62, 190, 238, 21, 159, 222, 2, 107, 172, 1, 72, 122, 1, 40, 71, 1, 18, 32,
  71. 61, 199, 240, 27, 161, 226, 4, 113, 180, 1, 76, 129, 1, 46, 80, 1, 23, 41,
  72. 7, 27, 153, 5, 30, 95, 1, 16, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 50, 75, 127, 57, 75, 124, 27, 67, 108, 10, 54, 86, 1, 33, 52, 1, 12, 18,
  74. 43, 125, 151, 26, 108, 148, 7, 83, 122, 2, 59, 89, 1, 38, 60, 1, 17, 27,
  75. 23, 144, 163, 13, 112, 154, 2, 75, 117, 1, 50, 81, 1, 31, 51, 1, 14, 23,
  76. 18, 162, 185, 6, 123, 171, 1, 78, 125, 1, 51, 86, 1, 31, 54, 1, 14, 23,
  77. 15, 199, 227, 3, 150, 204, 1, 91, 146, 1, 55, 95, 1, 30, 53, 1, 11, 20,
  78. 19, 55, 240, 19, 59, 196, 3, 52, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 41, 166, 207, 104, 153, 199, 31, 123, 181, 14, 101, 152, 5, 72, 106, 1, 36, 52,
  80. 35, 176, 211, 12, 131, 190, 2, 88, 144, 1, 60, 101, 1, 36, 60, 1, 16, 28,
  81. 28, 183, 213, 8, 134, 191, 1, 86, 142, 1, 56, 96, 1, 30, 53, 1, 12, 20,
  82. 20, 190, 215, 4, 135, 192, 1, 84, 139, 1, 53, 91, 1, 28, 49, 1, 11, 20,
  83. 13, 196, 216, 2, 137, 192, 1, 86, 143, 1, 57, 99, 1, 32, 56, 1, 13, 24,
  84. 211, 29, 217, 96, 47, 156, 22, 43, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 78, 120, 193, 111, 116, 186, 46, 102, 164, 15, 80, 128, 2, 49, 76, 1, 18, 28,
  86. 71, 161, 203, 42, 132, 192, 10, 98, 150, 3, 69, 109, 1, 44, 70, 1, 18, 29,
  87. 57, 186, 211, 30, 140, 196, 4, 93, 146, 1, 62, 102, 1, 38, 65, 1, 16, 27,
  88. 47, 199, 217, 14, 145, 196, 1, 88, 142, 1, 57, 98, 1, 36, 62, 1, 15, 26,
  89. 26, 219, 229, 5, 155, 207, 1, 94, 151, 1, 60, 104, 1, 36, 62, 1, 16, 28,
  90. 233, 29, 248, 146, 47, 220, 43, 52, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. 100, 163, 232, 179, 161, 222, 63, 142, 204, 37, 113, 174, 26, 89, 137, 18, 68, 97,
  92. 85, 181, 230, 32, 146, 209, 7, 100, 164, 3, 71, 121, 1, 45, 77, 1, 18, 30,
  93. 65, 187, 230, 20, 148, 207, 2, 97, 159, 1, 68, 116, 1, 40, 70, 1, 14, 29,
  94. 40, 194, 227, 8, 147, 204, 1, 94, 155, 1, 65, 112, 1, 39, 66, 1, 14, 26,
  95. 16, 208, 228, 3, 151, 207, 1, 98, 160, 1, 67, 117, 1, 41, 74, 1, 17, 31,
  96. 17, 38, 140, 7, 34, 80, 1, 17, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 37, 75, 128, 41, 76, 128, 26, 66, 116, 12, 52, 94, 2, 32, 55, 1, 10, 16,
  98. 50, 127, 154, 37, 109, 152, 16, 82, 121, 5, 59, 85, 1, 35, 54, 1, 13, 20,
  99. 40, 142, 167, 17, 110, 157, 2, 71, 112, 1, 44, 72, 1, 27, 45, 1, 11, 17,
  100. 30, 175, 188, 9, 124, 169, 1, 74, 116, 1, 48, 78, 1, 30, 49, 1, 11, 18,
  101. 10, 222, 223, 2, 150, 194, 1, 83, 128, 1, 48, 79, 1, 27, 45, 1, 11, 17,
  102. 36, 41, 235, 29, 36, 193, 10, 27, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. 85, 165, 222, 177, 162, 215, 110, 135, 195, 57, 113, 168, 23, 83, 120, 10, 49, 61,
  104. 85, 190, 223, 36, 139, 200, 5, 90, 146, 1, 60, 103, 1, 38, 65, 1, 18, 30,
  105. 72, 202, 223, 23, 141, 199, 2, 86, 140, 1, 56, 97, 1, 36, 61, 1, 16, 27,
  106. 55, 218, 225, 13, 145, 200, 1, 86, 141, 1, 57, 99, 1, 35, 61, 1, 13, 22,
  107. 15, 235, 212, 1, 132, 184, 1, 84, 139, 1, 57, 97, 1, 34, 56, 1, 14, 23,
  108. 181, 21, 201, 61, 37, 123, 10, 38, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 47, 106, 172, 95, 104, 173, 42, 93, 159, 18, 77, 131, 4, 50, 81, 1, 17, 23,
  110. 62, 147, 199, 44, 130, 189, 28, 102, 154, 18, 75, 115, 2, 44, 65, 1, 12, 19,
  111. 55, 153, 210, 24, 130, 194, 3, 93, 146, 1, 61, 97, 1, 31, 50, 1, 10, 16,
  112. 49, 186, 223, 17, 148, 204, 1, 96, 142, 1, 53, 83, 1, 26, 44, 1, 11, 17,
  113. 13, 217, 212, 2, 136, 180, 1, 78, 124, 1, 50, 83, 1, 29, 49, 1, 14, 23,
  114. 197, 13, 247, 82, 17, 222, 25, 17, 162, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. 126, 186, 247, 234, 191, 243, 176, 177, 234, 104, 158, 220, 66, 128, 186, 55, 90, 137,
  116. 111, 197, 242, 46, 158, 219, 9, 104, 171, 2, 65, 125, 1, 44, 80, 1, 17, 91,
  117. 104, 208, 245, 39, 168, 224, 3, 109, 162, 1, 79, 124, 1, 50, 102, 1, 43, 102,
  118. 84, 220, 246, 31, 177, 231, 2, 115, 180, 1, 79, 134, 1, 55, 77, 1, 60, 79,
  119. 43, 243, 240, 8, 180, 217, 1, 115, 166, 1, 84, 121, 1, 51, 67, 1, 16, 6,
  120. },
  121. .switchable_interp_prob{235, 162, 36, 255, 34, 3, 149, 144},
  122. .inter_mode_prob{
  123. 2, 173, 34, 0, 7, 145, 85, 0, 7, 166, 63, 0, 7, 94,
  124. 66, 0, 8, 64, 46, 0, 17, 81, 31, 0, 25, 29, 30, 0,
  125. },
  126. .intra_inter_prob{9, 102, 187, 225},
  127. .comp_inter_prob{9, 102, 187, 225, 0},
  128. .single_ref_prob{33, 16, 77, 74, 142, 142, 172, 170, 238, 247},
  129. .comp_ref_prob{50, 126, 123, 221, 226},
  130. .tx_32x32_prob{3, 136, 37, 5, 52, 13},
  131. .tx_16x16_prob{20, 152, 15, 101},
  132. .tx_8x8_prob{100, 66},
  133. .skip_probs{192, 128, 64},
  134. .joints{32, 64, 96},
  135. .sign{128, 128},
  136. .classes{
  137. 224, 144, 192, 168, 192, 176, 192, 198, 198, 245,
  138. 216, 128, 176, 160, 176, 176, 192, 198, 198, 208,
  139. },
  140. .class_0{216, 208},
  141. .prob_bits{
  142. 136, 140, 148, 160, 176, 192, 224, 234, 234, 240,
  143. 136, 140, 148, 160, 176, 192, 224, 234, 234, 240,
  144. },
  145. .class_0_fr{128, 128, 64, 96, 112, 64, 128, 128, 64, 96, 112, 64},
  146. .fr{64, 96, 64, 64, 96, 64},
  147. .class_0_hp{160, 160},
  148. .high_precision{128, 128},
  149. };
  150. constexpr std::array<s32, 256> norm_lut{
  151. 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  152. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  153. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  154. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  155. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  156. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  159. };
  160. constexpr std::array<s32, 254> map_lut{
  161. 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
  162. 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 2, 50, 51, 52, 53, 54,
  163. 55, 56, 57, 58, 59, 60, 61, 3, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
  164. 73, 4, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 5, 86, 87, 88, 89,
  165. 90, 91, 92, 93, 94, 95, 96, 97, 6, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  166. 108, 109, 7, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 8, 122, 123, 124,
  167. 125, 126, 127, 128, 129, 130, 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142,
  168. 143, 144, 145, 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11, 158, 159,
  169. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171, 172, 173, 174, 175, 176, 177,
  170. 178, 179, 180, 181, 13, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 14, 194,
  171. 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 15, 206, 207, 208, 209, 210, 211, 212,
  172. 213, 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 17,
  173. 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 18, 242, 243, 244, 245, 246, 247,
  174. 248, 249, 250, 251, 252, 253, 19,
  175. };
  176. // 6.2.14 Tile size calculation
  177. [[nodiscard]] s32 CalcMinLog2TileCols(s32 frame_width) {
  178. const s32 sb64_cols = (frame_width + 63) / 64;
  179. s32 min_log2 = 0;
  180. while ((64 << min_log2) < sb64_cols) {
  181. min_log2++;
  182. }
  183. return min_log2;
  184. }
  185. [[nodiscard]] s32 CalcMaxLog2TileCols(s32 frame_width) {
  186. const s32 sb64_cols = (frame_width + 63) / 64;
  187. s32 max_log2 = 1;
  188. while ((sb64_cols >> max_log2) >= 4) {
  189. max_log2++;
  190. }
  191. return max_log2 - 1;
  192. }
  193. // Recenters probability. Based on section 6.3.6 of VP9 Specification
  194. [[nodiscard]] s32 RecenterNonNeg(s32 new_prob, s32 old_prob) {
  195. if (new_prob > old_prob * 2) {
  196. return new_prob;
  197. }
  198. if (new_prob >= old_prob) {
  199. return (new_prob - old_prob) * 2;
  200. }
  201. return (old_prob - new_prob) * 2 - 1;
  202. }
  203. // Adjusts old_prob depending on new_prob. Based on section 6.3.5 of VP9 Specification
  204. [[nodiscard]] s32 RemapProbability(s32 new_prob, s32 old_prob) {
  205. new_prob--;
  206. old_prob--;
  207. std::size_t index{};
  208. if (old_prob * 2 <= 0xff) {
  209. index = static_cast<std::size_t>(std::max(0, RecenterNonNeg(new_prob, old_prob) - 1));
  210. } else {
  211. index = static_cast<std::size_t>(
  212. std::max(0, RecenterNonNeg(0xff - 1 - new_prob, 0xff - 1 - old_prob) - 1));
  213. }
  214. return map_lut[index];
  215. }
  216. } // Anonymous namespace
  217. VP9::VP9(GPU& gpu_) : gpu{gpu_} {}
  218. VP9::~VP9() = default;
  219. void VP9::WriteProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) {
  220. const bool update = new_prob != old_prob;
  221. writer.Write(update, diff_update_probability);
  222. if (update) {
  223. WriteProbabilityDelta(writer, new_prob, old_prob);
  224. }
  225. }
  226. template <typename T, std::size_t N>
  227. void VP9::WriteProbabilityUpdate(VpxRangeEncoder& writer, const std::array<T, N>& new_prob,
  228. const std::array<T, N>& old_prob) {
  229. for (std::size_t offset = 0; offset < new_prob.size(); ++offset) {
  230. WriteProbabilityUpdate(writer, new_prob[offset], old_prob[offset]);
  231. }
  232. }
  233. template <typename T, std::size_t N>
  234. void VP9::WriteProbabilityUpdateAligned4(VpxRangeEncoder& writer, const std::array<T, N>& new_prob,
  235. const std::array<T, N>& old_prob) {
  236. for (std::size_t offset = 0; offset < new_prob.size(); offset += 4) {
  237. WriteProbabilityUpdate(writer, new_prob[offset + 0], old_prob[offset + 0]);
  238. WriteProbabilityUpdate(writer, new_prob[offset + 1], old_prob[offset + 1]);
  239. WriteProbabilityUpdate(writer, new_prob[offset + 2], old_prob[offset + 2]);
  240. }
  241. }
  242. void VP9::WriteProbabilityDelta(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) {
  243. const int delta = RemapProbability(new_prob, old_prob);
  244. EncodeTermSubExp(writer, delta);
  245. }
  246. void VP9::EncodeTermSubExp(VpxRangeEncoder& writer, s32 value) {
  247. if (WriteLessThan(writer, value, 16)) {
  248. writer.Write(value, 4);
  249. } else if (WriteLessThan(writer, value, 32)) {
  250. writer.Write(value - 16, 4);
  251. } else if (WriteLessThan(writer, value, 64)) {
  252. writer.Write(value - 32, 5);
  253. } else {
  254. value -= 64;
  255. constexpr s32 size = 8;
  256. const s32 mask = (1 << size) - 191;
  257. const s32 delta = value - mask;
  258. if (delta < 0) {
  259. writer.Write(value, size - 1);
  260. } else {
  261. writer.Write(delta / 2 + mask, size - 1);
  262. writer.Write(delta & 1, 1);
  263. }
  264. }
  265. }
  266. bool VP9::WriteLessThan(VpxRangeEncoder& writer, s32 value, s32 test) {
  267. const bool is_lt = value < test;
  268. writer.Write(!is_lt);
  269. return is_lt;
  270. }
  271. void VP9::WriteCoefProbabilityUpdate(VpxRangeEncoder& writer, s32 tx_mode,
  272. const std::array<u8, 1728>& new_prob,
  273. const std::array<u8, 1728>& old_prob) {
  274. constexpr u32 block_bytes = 2 * 2 * 6 * 6 * 3;
  275. const auto needs_update = [&](u32 base_index) {
  276. return !std::equal(new_prob.begin() + base_index,
  277. new_prob.begin() + base_index + block_bytes,
  278. old_prob.begin() + base_index);
  279. };
  280. for (u32 block_index = 0; block_index < 4; block_index++) {
  281. const u32 base_index = block_index * block_bytes;
  282. const bool update = needs_update(base_index);
  283. writer.Write(update);
  284. if (update) {
  285. u32 index = base_index;
  286. for (s32 i = 0; i < 2; i++) {
  287. for (s32 j = 0; j < 2; j++) {
  288. for (s32 k = 0; k < 6; k++) {
  289. for (s32 l = 0; l < 6; l++) {
  290. if (k != 0 || l < 3) {
  291. WriteProbabilityUpdate(writer, new_prob[index + 0],
  292. old_prob[index + 0]);
  293. WriteProbabilityUpdate(writer, new_prob[index + 1],
  294. old_prob[index + 1]);
  295. WriteProbabilityUpdate(writer, new_prob[index + 2],
  296. old_prob[index + 2]);
  297. }
  298. index += 3;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. if (block_index == static_cast<u32>(tx_mode)) {
  305. break;
  306. }
  307. }
  308. }
  309. void VP9::WriteMvProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) {
  310. const bool update = new_prob != old_prob;
  311. writer.Write(update, diff_update_probability);
  312. if (update) {
  313. writer.Write(new_prob >> 1, 7);
  314. }
  315. }
  316. Vp9PictureInfo VP9::GetVp9PictureInfo(const NvdecCommon::NvdecRegisters& state) {
  317. PictureInfo picture_info{};
  318. gpu.MemoryManager().ReadBlock(state.picture_info_offset, &picture_info, sizeof(PictureInfo));
  319. Vp9PictureInfo vp9_info = picture_info.Convert();
  320. InsertEntropy(state.vp9_entropy_probs_offset, vp9_info.entropy);
  321. // surface_luma_offset[0:3] contains the address of the reference frame offsets in the following
  322. // order: last, golden, altref, current. It may be worthwhile to track the updates done here
  323. // to avoid buffering frame data needed for reference frame updating in the header composition.
  324. std::memcpy(vp9_info.frame_offsets.data(), state.surface_luma_offset.data(), 4 * sizeof(u64));
  325. return vp9_info;
  326. }
  327. void VP9::InsertEntropy(u64 offset, Vp9EntropyProbs& dst) {
  328. EntropyProbs entropy{};
  329. gpu.MemoryManager().ReadBlock(offset, &entropy, sizeof(EntropyProbs));
  330. entropy.Convert(dst);
  331. }
  332. Vp9FrameContainer VP9::GetCurrentFrame(const NvdecCommon::NvdecRegisters& state) {
  333. Vp9FrameContainer frame{};
  334. {
  335. gpu.SyncGuestHost();
  336. frame.info = GetVp9PictureInfo(state);
  337. frame.bit_stream.resize(frame.info.bitstream_size);
  338. gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.bit_stream.data(),
  339. frame.info.bitstream_size);
  340. }
  341. // Buffer two frames, saving the last show frame info
  342. if (!next_next_frame.bit_stream.empty()) {
  343. Vp9FrameContainer temp{
  344. .info = frame.info,
  345. .bit_stream = std::move(frame.bit_stream),
  346. };
  347. next_next_frame.info.show_frame = frame.info.last_frame_shown;
  348. frame.info = next_next_frame.info;
  349. frame.bit_stream = std::move(next_next_frame.bit_stream);
  350. next_next_frame = std::move(temp);
  351. if (!next_frame.bit_stream.empty()) {
  352. Vp9FrameContainer temp2{
  353. .info = frame.info,
  354. .bit_stream = std::move(frame.bit_stream),
  355. };
  356. next_frame.info.show_frame = frame.info.last_frame_shown;
  357. frame.info = next_frame.info;
  358. frame.bit_stream = std::move(next_frame.bit_stream);
  359. next_frame = std::move(temp2);
  360. } else {
  361. next_frame.info = frame.info;
  362. next_frame.bit_stream = std::move(frame.bit_stream);
  363. }
  364. } else {
  365. next_next_frame.info = frame.info;
  366. next_next_frame.bit_stream = std::move(frame.bit_stream);
  367. }
  368. return frame;
  369. }
  370. std::vector<u8> VP9::ComposeCompressedHeader() {
  371. VpxRangeEncoder writer{};
  372. const bool update_probs = current_frame_info.show_frame && !current_frame_info.is_key_frame;
  373. if (!current_frame_info.lossless) {
  374. if (static_cast<u32>(current_frame_info.transform_mode) >= 3) {
  375. writer.Write(3, 2);
  376. writer.Write(current_frame_info.transform_mode == 4);
  377. } else {
  378. writer.Write(current_frame_info.transform_mode, 2);
  379. }
  380. }
  381. if (current_frame_info.transform_mode == 4) {
  382. // tx_mode_probs() in the spec
  383. WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_8x8_prob,
  384. prev_frame_probs.tx_8x8_prob);
  385. WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_16x16_prob,
  386. prev_frame_probs.tx_16x16_prob);
  387. WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_32x32_prob,
  388. prev_frame_probs.tx_32x32_prob);
  389. if (update_probs) {
  390. prev_frame_probs.tx_8x8_prob = current_frame_info.entropy.tx_8x8_prob;
  391. prev_frame_probs.tx_16x16_prob = current_frame_info.entropy.tx_16x16_prob;
  392. prev_frame_probs.tx_32x32_prob = current_frame_info.entropy.tx_32x32_prob;
  393. }
  394. }
  395. // read_coef_probs() in the spec
  396. WriteCoefProbabilityUpdate(writer, current_frame_info.transform_mode,
  397. current_frame_info.entropy.coef_probs, prev_frame_probs.coef_probs);
  398. // read_skip_probs() in the spec
  399. WriteProbabilityUpdate(writer, current_frame_info.entropy.skip_probs,
  400. prev_frame_probs.skip_probs);
  401. if (update_probs) {
  402. prev_frame_probs.coef_probs = current_frame_info.entropy.coef_probs;
  403. prev_frame_probs.skip_probs = current_frame_info.entropy.skip_probs;
  404. }
  405. if (!current_frame_info.intra_only) {
  406. // read_inter_probs() in the spec
  407. WriteProbabilityUpdateAligned4(writer, current_frame_info.entropy.inter_mode_prob,
  408. prev_frame_probs.inter_mode_prob);
  409. if (current_frame_info.interp_filter == 4) {
  410. // read_interp_filter_probs() in the spec
  411. WriteProbabilityUpdate(writer, current_frame_info.entropy.switchable_interp_prob,
  412. prev_frame_probs.switchable_interp_prob);
  413. if (update_probs) {
  414. prev_frame_probs.switchable_interp_prob =
  415. current_frame_info.entropy.switchable_interp_prob;
  416. }
  417. }
  418. // read_is_inter_probs() in the spec
  419. WriteProbabilityUpdate(writer, current_frame_info.entropy.intra_inter_prob,
  420. prev_frame_probs.intra_inter_prob);
  421. // frame_reference_mode() in the spec
  422. if ((current_frame_info.ref_frame_sign_bias[1] & 1) !=
  423. (current_frame_info.ref_frame_sign_bias[2] & 1) ||
  424. (current_frame_info.ref_frame_sign_bias[1] & 1) !=
  425. (current_frame_info.ref_frame_sign_bias[3] & 1)) {
  426. if (current_frame_info.reference_mode >= 1) {
  427. writer.Write(1, 1);
  428. writer.Write(current_frame_info.reference_mode == 2);
  429. } else {
  430. writer.Write(0, 1);
  431. }
  432. }
  433. // frame_reference_mode_probs() in the spec
  434. if (current_frame_info.reference_mode == 2) {
  435. WriteProbabilityUpdate(writer, current_frame_info.entropy.comp_inter_prob,
  436. prev_frame_probs.comp_inter_prob);
  437. if (update_probs) {
  438. prev_frame_probs.comp_inter_prob = current_frame_info.entropy.comp_inter_prob;
  439. }
  440. }
  441. if (current_frame_info.reference_mode != 1) {
  442. WriteProbabilityUpdate(writer, current_frame_info.entropy.single_ref_prob,
  443. prev_frame_probs.single_ref_prob);
  444. if (update_probs) {
  445. prev_frame_probs.single_ref_prob = current_frame_info.entropy.single_ref_prob;
  446. }
  447. }
  448. if (current_frame_info.reference_mode != 0) {
  449. WriteProbabilityUpdate(writer, current_frame_info.entropy.comp_ref_prob,
  450. prev_frame_probs.comp_ref_prob);
  451. if (update_probs) {
  452. prev_frame_probs.comp_ref_prob = current_frame_info.entropy.comp_ref_prob;
  453. }
  454. }
  455. // read_y_mode_probs
  456. for (std::size_t index = 0; index < current_frame_info.entropy.y_mode_prob.size();
  457. ++index) {
  458. WriteProbabilityUpdate(writer, current_frame_info.entropy.y_mode_prob[index],
  459. prev_frame_probs.y_mode_prob[index]);
  460. }
  461. // read_partition_probs
  462. WriteProbabilityUpdateAligned4(writer, current_frame_info.entropy.partition_prob,
  463. prev_frame_probs.partition_prob);
  464. // mv_probs
  465. for (s32 i = 0; i < 3; i++) {
  466. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.joints[i],
  467. prev_frame_probs.joints[i]);
  468. }
  469. if (update_probs) {
  470. prev_frame_probs.inter_mode_prob = current_frame_info.entropy.inter_mode_prob;
  471. prev_frame_probs.intra_inter_prob = current_frame_info.entropy.intra_inter_prob;
  472. prev_frame_probs.y_mode_prob = current_frame_info.entropy.y_mode_prob;
  473. prev_frame_probs.partition_prob = current_frame_info.entropy.partition_prob;
  474. prev_frame_probs.joints = current_frame_info.entropy.joints;
  475. }
  476. for (s32 i = 0; i < 2; i++) {
  477. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.sign[i],
  478. prev_frame_probs.sign[i]);
  479. for (s32 j = 0; j < 10; j++) {
  480. const int index = i * 10 + j;
  481. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.classes[index],
  482. prev_frame_probs.classes[index]);
  483. }
  484. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0[i],
  485. prev_frame_probs.class_0[i]);
  486. for (s32 j = 0; j < 10; j++) {
  487. const int index = i * 10 + j;
  488. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.prob_bits[index],
  489. prev_frame_probs.prob_bits[index]);
  490. }
  491. }
  492. for (s32 i = 0; i < 2; i++) {
  493. for (s32 j = 0; j < 2; j++) {
  494. for (s32 k = 0; k < 3; k++) {
  495. const int index = i * 2 * 3 + j * 3 + k;
  496. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0_fr[index],
  497. prev_frame_probs.class_0_fr[index]);
  498. }
  499. }
  500. for (s32 j = 0; j < 3; j++) {
  501. const int index = i * 3 + j;
  502. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.fr[index],
  503. prev_frame_probs.fr[index]);
  504. }
  505. }
  506. if (current_frame_info.allow_high_precision_mv) {
  507. for (s32 index = 0; index < 2; index++) {
  508. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0_hp[index],
  509. prev_frame_probs.class_0_hp[index]);
  510. WriteMvProbabilityUpdate(writer, current_frame_info.entropy.high_precision[index],
  511. prev_frame_probs.high_precision[index]);
  512. }
  513. }
  514. // save previous probs
  515. if (update_probs) {
  516. prev_frame_probs.sign = current_frame_info.entropy.sign;
  517. prev_frame_probs.classes = current_frame_info.entropy.classes;
  518. prev_frame_probs.class_0 = current_frame_info.entropy.class_0;
  519. prev_frame_probs.prob_bits = current_frame_info.entropy.prob_bits;
  520. prev_frame_probs.class_0_fr = current_frame_info.entropy.class_0_fr;
  521. prev_frame_probs.fr = current_frame_info.entropy.fr;
  522. prev_frame_probs.class_0_hp = current_frame_info.entropy.class_0_hp;
  523. prev_frame_probs.high_precision = current_frame_info.entropy.high_precision;
  524. }
  525. }
  526. writer.End();
  527. return writer.GetBuffer();
  528. }
  529. VpxBitStreamWriter VP9::ComposeUncompressedHeader() {
  530. VpxBitStreamWriter uncomp_writer{};
  531. uncomp_writer.WriteU(2, 2); // Frame marker.
  532. uncomp_writer.WriteU(0, 2); // Profile.
  533. uncomp_writer.WriteBit(false); // Show existing frame.
  534. uncomp_writer.WriteBit(!current_frame_info.is_key_frame); // is key frame?
  535. uncomp_writer.WriteBit(current_frame_info.show_frame); // show frame?
  536. uncomp_writer.WriteBit(current_frame_info.error_resilient_mode); // error reslience
  537. if (current_frame_info.is_key_frame) {
  538. uncomp_writer.WriteU(frame_sync_code, 24);
  539. uncomp_writer.WriteU(0, 3); // Color space.
  540. uncomp_writer.WriteU(0, 1); // Color range.
  541. uncomp_writer.WriteU(current_frame_info.frame_size.width - 1, 16);
  542. uncomp_writer.WriteU(current_frame_info.frame_size.height - 1, 16);
  543. uncomp_writer.WriteBit(false); // Render and frame size different.
  544. // Reset context
  545. prev_frame_probs = default_probs;
  546. swap_next_golden = false;
  547. loop_filter_ref_deltas.fill(0);
  548. loop_filter_mode_deltas.fill(0);
  549. // allow frames offsets to stabilize before checking for golden frames
  550. grace_period = 4;
  551. // On key frames, all frame slots are set to the current frame,
  552. // so the value of the selected slot doesn't really matter.
  553. frame_ctxs.fill({current_frame_number, false, default_probs});
  554. // intra only, meaning the frame can be recreated with no other references
  555. current_frame_info.intra_only = true;
  556. } else {
  557. if (!current_frame_info.show_frame) {
  558. uncomp_writer.WriteBit(current_frame_info.intra_only);
  559. if (!current_frame_info.last_frame_was_key) {
  560. swap_next_golden = !swap_next_golden;
  561. }
  562. } else {
  563. current_frame_info.intra_only = false;
  564. }
  565. if (!current_frame_info.error_resilient_mode) {
  566. uncomp_writer.WriteU(0, 2); // Reset frame context.
  567. }
  568. // Last, Golden, Altref frames
  569. std::array<s32, 3> ref_frame_index{0, 1, 2};
  570. // Set when next frame is hidden
  571. // altref and golden references are swapped
  572. if (swap_next_golden) {
  573. ref_frame_index = std::array<s32, 3>{0, 2, 1};
  574. }
  575. // update Last Frame
  576. u64 refresh_frame_flags = 1;
  577. // golden frame may refresh, determined if the next golden frame offset is changed
  578. bool golden_refresh = false;
  579. if (grace_period <= 0) {
  580. for (s32 index = 1; index < 3; ++index) {
  581. if (current_frame_info.frame_offsets[index] !=
  582. next_frame.info.frame_offsets[index]) {
  583. current_frame_info.refresh_frame[index] = true;
  584. golden_refresh = true;
  585. grace_period = 3;
  586. }
  587. }
  588. }
  589. if (current_frame_info.show_frame &&
  590. (!next_frame.info.show_frame || next_frame.info.is_key_frame)) {
  591. // Update golden frame
  592. refresh_frame_flags = swap_next_golden ? 2 : 4;
  593. }
  594. if (!current_frame_info.show_frame) {
  595. // Update altref
  596. refresh_frame_flags = swap_next_golden ? 2 : 4;
  597. } else if (golden_refresh) {
  598. refresh_frame_flags = 3;
  599. }
  600. if (current_frame_info.intra_only) {
  601. uncomp_writer.WriteU(frame_sync_code, 24);
  602. uncomp_writer.WriteU(static_cast<s32>(refresh_frame_flags), 8);
  603. uncomp_writer.WriteU(current_frame_info.frame_size.width - 1, 16);
  604. uncomp_writer.WriteU(current_frame_info.frame_size.height - 1, 16);
  605. uncomp_writer.WriteBit(false); // Render and frame size different.
  606. } else {
  607. uncomp_writer.WriteU(static_cast<s32>(refresh_frame_flags), 8);
  608. for (s32 index = 1; index < 4; index++) {
  609. uncomp_writer.WriteU(ref_frame_index[index - 1], 3);
  610. uncomp_writer.WriteU(current_frame_info.ref_frame_sign_bias[index], 1);
  611. }
  612. uncomp_writer.WriteBit(true); // Frame size with refs.
  613. uncomp_writer.WriteBit(false); // Render and frame size different.
  614. uncomp_writer.WriteBit(current_frame_info.allow_high_precision_mv);
  615. uncomp_writer.WriteBit(current_frame_info.interp_filter == 4);
  616. if (current_frame_info.interp_filter != 4) {
  617. uncomp_writer.WriteU(current_frame_info.interp_filter, 2);
  618. }
  619. }
  620. }
  621. if (!current_frame_info.error_resilient_mode) {
  622. uncomp_writer.WriteBit(true); // Refresh frame context. where do i get this info from?
  623. uncomp_writer.WriteBit(true); // Frame parallel decoding mode.
  624. }
  625. int frame_ctx_idx = 0;
  626. if (!current_frame_info.show_frame) {
  627. frame_ctx_idx = 1;
  628. }
  629. uncomp_writer.WriteU(frame_ctx_idx, 2); // Frame context index.
  630. prev_frame_probs =
  631. frame_ctxs[frame_ctx_idx].probs; // reference probabilities for compressed header
  632. frame_ctxs[frame_ctx_idx] = {current_frame_number, false, current_frame_info.entropy};
  633. uncomp_writer.WriteU(current_frame_info.first_level, 6);
  634. uncomp_writer.WriteU(current_frame_info.sharpness_level, 3);
  635. uncomp_writer.WriteBit(current_frame_info.mode_ref_delta_enabled);
  636. if (current_frame_info.mode_ref_delta_enabled) {
  637. // check if ref deltas are different, update accordingly
  638. std::array<bool, 4> update_loop_filter_ref_deltas;
  639. std::array<bool, 2> update_loop_filter_mode_deltas;
  640. bool loop_filter_delta_update = false;
  641. for (std::size_t index = 0; index < current_frame_info.ref_deltas.size(); index++) {
  642. const s8 old_deltas = loop_filter_ref_deltas[index];
  643. const s8 new_deltas = current_frame_info.ref_deltas[index];
  644. const bool differing_delta = old_deltas != new_deltas;
  645. update_loop_filter_ref_deltas[index] = differing_delta;
  646. loop_filter_delta_update |= differing_delta;
  647. }
  648. for (std::size_t index = 0; index < current_frame_info.mode_deltas.size(); index++) {
  649. const s8 old_deltas = loop_filter_mode_deltas[index];
  650. const s8 new_deltas = current_frame_info.mode_deltas[index];
  651. const bool differing_delta = old_deltas != new_deltas;
  652. update_loop_filter_mode_deltas[index] = differing_delta;
  653. loop_filter_delta_update |= differing_delta;
  654. }
  655. uncomp_writer.WriteBit(loop_filter_delta_update);
  656. if (loop_filter_delta_update) {
  657. for (std::size_t index = 0; index < current_frame_info.ref_deltas.size(); index++) {
  658. uncomp_writer.WriteBit(update_loop_filter_ref_deltas[index]);
  659. if (update_loop_filter_ref_deltas[index]) {
  660. uncomp_writer.WriteS(current_frame_info.ref_deltas[index], 6);
  661. }
  662. }
  663. for (std::size_t index = 0; index < current_frame_info.mode_deltas.size(); index++) {
  664. uncomp_writer.WriteBit(update_loop_filter_mode_deltas[index]);
  665. if (update_loop_filter_mode_deltas[index]) {
  666. uncomp_writer.WriteS(current_frame_info.mode_deltas[index], 6);
  667. }
  668. }
  669. // save new deltas
  670. loop_filter_ref_deltas = current_frame_info.ref_deltas;
  671. loop_filter_mode_deltas = current_frame_info.mode_deltas;
  672. }
  673. }
  674. uncomp_writer.WriteU(current_frame_info.base_q_index, 8);
  675. uncomp_writer.WriteDeltaQ(current_frame_info.y_dc_delta_q);
  676. uncomp_writer.WriteDeltaQ(current_frame_info.uv_dc_delta_q);
  677. uncomp_writer.WriteDeltaQ(current_frame_info.uv_ac_delta_q);
  678. uncomp_writer.WriteBit(false); // Segmentation enabled (TODO).
  679. const s32 min_tile_cols_log2 = CalcMinLog2TileCols(current_frame_info.frame_size.width);
  680. const s32 max_tile_cols_log2 = CalcMaxLog2TileCols(current_frame_info.frame_size.width);
  681. const s32 tile_cols_log2_diff = current_frame_info.log2_tile_cols - min_tile_cols_log2;
  682. const s32 tile_cols_log2_inc_mask = (1 << tile_cols_log2_diff) - 1;
  683. // If it's less than the maximum, we need to add an extra 0 on the bitstream
  684. // to indicate that it should stop reading.
  685. if (current_frame_info.log2_tile_cols < max_tile_cols_log2) {
  686. uncomp_writer.WriteU(tile_cols_log2_inc_mask << 1, tile_cols_log2_diff + 1);
  687. } else {
  688. uncomp_writer.WriteU(tile_cols_log2_inc_mask, tile_cols_log2_diff);
  689. }
  690. const bool tile_rows_log2_is_nonzero = current_frame_info.log2_tile_rows != 0;
  691. uncomp_writer.WriteBit(tile_rows_log2_is_nonzero);
  692. if (tile_rows_log2_is_nonzero) {
  693. uncomp_writer.WriteBit(current_frame_info.log2_tile_rows > 1);
  694. }
  695. return uncomp_writer;
  696. }
  697. const std::vector<u8>& VP9::ComposeFrameHeader(const NvdecCommon::NvdecRegisters& state) {
  698. std::vector<u8> bitstream;
  699. {
  700. Vp9FrameContainer curr_frame = GetCurrentFrame(state);
  701. current_frame_info = curr_frame.info;
  702. bitstream = std::move(curr_frame.bit_stream);
  703. }
  704. // The uncompressed header routine sets PrevProb parameters needed for the compressed header
  705. auto uncomp_writer = ComposeUncompressedHeader();
  706. std::vector<u8> compressed_header = ComposeCompressedHeader();
  707. uncomp_writer.WriteU(static_cast<s32>(compressed_header.size()), 16);
  708. uncomp_writer.Flush();
  709. std::vector<u8> uncompressed_header = uncomp_writer.GetByteArray();
  710. // Write headers and frame to buffer
  711. frame.resize(uncompressed_header.size() + compressed_header.size() + bitstream.size());
  712. std::memcpy(frame.data(), uncompressed_header.data(), uncompressed_header.size());
  713. std::memcpy(frame.data() + uncompressed_header.size(), compressed_header.data(),
  714. compressed_header.size());
  715. std::memcpy(frame.data() + uncompressed_header.size() + compressed_header.size(),
  716. bitstream.data(), bitstream.size());
  717. // keep track of frame number
  718. current_frame_number++;
  719. grace_period--;
  720. // don't display hidden frames
  721. hidden = !current_frame_info.show_frame;
  722. return frame;
  723. }
  724. VpxRangeEncoder::VpxRangeEncoder() {
  725. Write(false);
  726. }
  727. VpxRangeEncoder::~VpxRangeEncoder() = default;
  728. void VpxRangeEncoder::Write(s32 value, s32 value_size) {
  729. for (s32 bit = value_size - 1; bit >= 0; bit--) {
  730. Write(((value >> bit) & 1) != 0);
  731. }
  732. }
  733. void VpxRangeEncoder::Write(bool bit) {
  734. Write(bit, half_probability);
  735. }
  736. void VpxRangeEncoder::Write(bool bit, s32 probability) {
  737. u32 local_range = range;
  738. const u32 split = 1 + (((local_range - 1) * static_cast<u32>(probability)) >> 8);
  739. local_range = split;
  740. if (bit) {
  741. low_value += split;
  742. local_range = range - split;
  743. }
  744. s32 shift = norm_lut[local_range];
  745. local_range <<= shift;
  746. count += shift;
  747. if (count >= 0) {
  748. const s32 offset = shift - count;
  749. if (((low_value << (offset - 1)) >> 31) != 0) {
  750. const s32 current_pos = static_cast<s32>(base_stream.GetPosition());
  751. base_stream.Seek(-1, Common::SeekOrigin::FromCurrentPos);
  752. while (PeekByte() == 0xff) {
  753. base_stream.WriteByte(0);
  754. base_stream.Seek(-2, Common::SeekOrigin::FromCurrentPos);
  755. }
  756. base_stream.WriteByte(static_cast<u8>((PeekByte() + 1)));
  757. base_stream.Seek(current_pos, Common::SeekOrigin::SetOrigin);
  758. }
  759. base_stream.WriteByte(static_cast<u8>((low_value >> (24 - offset))));
  760. low_value <<= offset;
  761. shift = count;
  762. low_value &= 0xffffff;
  763. count -= 8;
  764. }
  765. low_value <<= shift;
  766. range = local_range;
  767. }
  768. void VpxRangeEncoder::End() {
  769. for (std::size_t index = 0; index < 32; ++index) {
  770. Write(false);
  771. }
  772. }
  773. u8 VpxRangeEncoder::PeekByte() {
  774. const u8 value = base_stream.ReadByte();
  775. base_stream.Seek(-1, Common::SeekOrigin::FromCurrentPos);
  776. return value;
  777. }
  778. VpxBitStreamWriter::VpxBitStreamWriter() = default;
  779. VpxBitStreamWriter::~VpxBitStreamWriter() = default;
  780. void VpxBitStreamWriter::WriteU(u32 value, u32 value_size) {
  781. WriteBits(value, value_size);
  782. }
  783. void VpxBitStreamWriter::WriteS(s32 value, u32 value_size) {
  784. const bool sign = value < 0;
  785. if (sign) {
  786. value = -value;
  787. }
  788. WriteBits(static_cast<u32>(value << 1) | (sign ? 1 : 0), value_size + 1);
  789. }
  790. void VpxBitStreamWriter::WriteDeltaQ(u32 value) {
  791. const bool delta_coded = value != 0;
  792. WriteBit(delta_coded);
  793. if (delta_coded) {
  794. WriteBits(value, 4);
  795. }
  796. }
  797. void VpxBitStreamWriter::WriteBits(u32 value, u32 bit_count) {
  798. s32 value_pos = 0;
  799. s32 remaining = bit_count;
  800. while (remaining > 0) {
  801. s32 copy_size = remaining;
  802. const s32 free = GetFreeBufferBits();
  803. if (copy_size > free) {
  804. copy_size = free;
  805. }
  806. const s32 mask = (1 << copy_size) - 1;
  807. const s32 src_shift = (bit_count - value_pos) - copy_size;
  808. const s32 dst_shift = (buffer_size - buffer_pos) - copy_size;
  809. buffer |= ((value >> src_shift) & mask) << dst_shift;
  810. value_pos += copy_size;
  811. buffer_pos += copy_size;
  812. remaining -= copy_size;
  813. }
  814. }
  815. void VpxBitStreamWriter::WriteBit(bool state) {
  816. WriteBits(state ? 1 : 0, 1);
  817. }
  818. s32 VpxBitStreamWriter::GetFreeBufferBits() {
  819. if (buffer_pos == buffer_size) {
  820. Flush();
  821. }
  822. return buffer_size - buffer_pos;
  823. }
  824. void VpxBitStreamWriter::Flush() {
  825. if (buffer_pos == 0) {
  826. return;
  827. }
  828. byte_array.push_back(static_cast<u8>(buffer));
  829. buffer = 0;
  830. buffer_pos = 0;
  831. }
  832. std::vector<u8>& VpxBitStreamWriter::GetByteArray() {
  833. return byte_array;
  834. }
  835. const std::vector<u8>& VpxBitStreamWriter::GetByteArray() const {
  836. return byte_array;
  837. }
  838. } // namespace Tegra::Decoder