vp9.cpp 46 KB

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