Parser.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <LibGfx/Point.h>
  9. #include <LibGfx/Size.h>
  10. #include "Decoder.h"
  11. #include "Parser.h"
  12. #include "Utilities.h"
  13. #if defined(AK_COMPILER_GCC)
  14. # pragma GCC optimize("O3")
  15. #endif
  16. namespace Video::VP9 {
  17. #define TRY_READ(expression) DECODER_TRY(DecoderErrorCategory::Corrupted, expression)
  18. Parser::Parser(Decoder& decoder)
  19. : m_probability_tables(make<ProbabilityTables>())
  20. , m_decoder(decoder)
  21. {
  22. }
  23. Parser::~Parser()
  24. {
  25. }
  26. Vector<size_t> Parser::parse_superframe_sizes(ReadonlyBytes frame_data)
  27. {
  28. if (frame_data.size() < 1)
  29. return {};
  30. // The decoder determines the presence of a superframe by:
  31. // 1. parsing the final byte of the chunk and checking that the superframe_marker equals 0b110,
  32. // If the checks in steps 1 and 3 both pass, then the chunk is determined to contain a superframe and each
  33. // frame in the superframe is passed to the decoding process in turn.
  34. // Otherwise, the chunk is determined to not contain a superframe, and the whole chunk is passed to the
  35. // decoding process.
  36. // NOTE: Reading from span data will be quicker than spinning up a BitStream.
  37. u8 superframe_byte = frame_data[frame_data.size() - 1];
  38. // NOTE: We have to read out of the byte from the little end first, hence the padding bits in the masks below.
  39. u8 superframe_marker = superframe_byte & 0b1110'0000;
  40. if (superframe_marker == 0b1100'0000) {
  41. u8 bytes_per_framesize = ((superframe_byte >> 3) & 0b11) + 1;
  42. u8 frames_in_superframe = (superframe_byte & 0b111) + 1;
  43. // 2. setting the total size of the superframe_index SzIndex equal to 2 + NumFrames * SzBytes,
  44. size_t index_size = 2 + bytes_per_framesize * frames_in_superframe;
  45. if (index_size > frame_data.size())
  46. return {};
  47. auto superframe_header_data = frame_data.data() + frame_data.size() - index_size;
  48. u8 start_superframe_byte = *(superframe_header_data++);
  49. // 3. checking that the first byte of the superframe_index matches the final byte.
  50. if (superframe_byte != start_superframe_byte)
  51. return {};
  52. Vector<size_t> result;
  53. for (u8 i = 0; i < frames_in_superframe; i++) {
  54. size_t frame_size = 0;
  55. for (u8 j = 0; j < bytes_per_framesize; j++)
  56. frame_size |= (static_cast<size_t>(*(superframe_header_data++)) << (j * 8));
  57. result.append(frame_size);
  58. }
  59. return result;
  60. }
  61. return {};
  62. }
  63. /* (6.1) */
  64. DecoderErrorOr<FrameContext> Parser::parse_frame(ReadonlyBytes frame_data)
  65. {
  66. m_bit_stream = make<BitStream>(frame_data.data(), frame_data.size());
  67. m_syntax_element_counter = make<SyntaxElementCounter>();
  68. auto frame_context = TRY(uncompressed_header());
  69. if (!trailing_bits())
  70. return DecoderError::corrupted("Trailing bits were non-zero"sv);
  71. // FIXME: This should not be an error. Spec says that we consume padding bits until the end of the sample.
  72. if (frame_context.header_size_in_bytes == 0)
  73. return DecoderError::corrupted("Frame header is zero-sized"sv);
  74. m_probability_tables->load_probs(frame_context.probability_context_index);
  75. m_probability_tables->load_probs2(frame_context.probability_context_index);
  76. m_syntax_element_counter->clear_counts();
  77. TRY_READ(m_bit_stream->init_bool(frame_context.header_size_in_bytes));
  78. TRY(compressed_header(frame_context));
  79. TRY_READ(m_bit_stream->exit_bool());
  80. TRY(m_decoder.allocate_buffers(frame_context));
  81. TRY(decode_tiles(frame_context));
  82. TRY(refresh_probs(frame_context));
  83. m_previous_frame_type = frame_context.type;
  84. m_previous_frame_size = frame_context.size();
  85. m_previous_show_frame = frame_context.shows_a_frame();
  86. m_previous_color_config = frame_context.color_config;
  87. m_previous_loop_filter_ref_deltas = frame_context.loop_filter_reference_deltas;
  88. m_previous_loop_filter_mode_deltas = frame_context.loop_filter_mode_deltas;
  89. if (frame_context.segmentation_enabled) {
  90. m_previous_should_use_absolute_segment_base_quantizer = frame_context.should_use_absolute_segment_base_quantizer;
  91. m_previous_segmentation_features = frame_context.segmentation_features;
  92. }
  93. return frame_context;
  94. }
  95. bool Parser::trailing_bits()
  96. {
  97. while (m_bit_stream->bits_remaining() & 7u) {
  98. if (MUST(m_bit_stream->read_bit()))
  99. return false;
  100. }
  101. return true;
  102. }
  103. DecoderErrorOr<void> Parser::refresh_probs(FrameContext const& frame_context)
  104. {
  105. if (!frame_context.error_resilient_mode && !frame_context.parallel_decoding_mode) {
  106. m_probability_tables->load_probs(frame_context.probability_context_index);
  107. TRY(m_decoder.adapt_coef_probs(frame_context.is_inter_predicted()));
  108. if (frame_context.is_inter_predicted()) {
  109. m_probability_tables->load_probs2(frame_context.probability_context_index);
  110. TRY(m_decoder.adapt_non_coef_probs(frame_context));
  111. }
  112. }
  113. if (frame_context.should_replace_probability_context)
  114. m_probability_tables->save_probs(frame_context.probability_context_index);
  115. return {};
  116. }
  117. DecoderErrorOr<ColorRange> Parser::read_color_range()
  118. {
  119. if (TRY_READ(m_bit_stream->read_bit()))
  120. return ColorRange::Full;
  121. return ColorRange::Studio;
  122. }
  123. /* (6.2) */
  124. DecoderErrorOr<FrameContext> Parser::uncompressed_header()
  125. {
  126. // NOTE: m_reusable_frame_block_contexts does not need to retain any data between frame decodes.
  127. // This is only stored so that we don't need to allocate a frame's block contexts on each
  128. // call to this function, since it will rarely change sizes.
  129. FrameContext frame_context { m_reusable_frame_block_contexts };
  130. frame_context.color_config = m_previous_color_config;
  131. auto frame_marker = TRY_READ(m_bit_stream->read_bits(2));
  132. if (frame_marker != 2)
  133. return DecoderError::corrupted("uncompressed_header: Frame marker must be 2"sv);
  134. auto profile_low_bit = TRY_READ(m_bit_stream->read_bit());
  135. auto profile_high_bit = TRY_READ(m_bit_stream->read_bit());
  136. frame_context.profile = (profile_high_bit << 1u) + profile_low_bit;
  137. if (frame_context.profile == 3 && TRY_READ(m_bit_stream->read_bit()))
  138. return DecoderError::corrupted("uncompressed_header: Profile 3 reserved bit was non-zero"sv);
  139. if (TRY_READ(m_bit_stream->read_bit())) {
  140. frame_context.set_existing_frame_to_show(TRY_READ(m_bit_stream->read_bits(3)));
  141. return frame_context;
  142. }
  143. bool is_keyframe = !TRY_READ(m_bit_stream->read_bit());
  144. if (!TRY_READ(m_bit_stream->read_bit()))
  145. frame_context.set_frame_hidden();
  146. frame_context.error_resilient_mode = TRY_READ(m_bit_stream->read_bit());
  147. FrameType type;
  148. Gfx::Size<u32> frame_size;
  149. Gfx::Size<u32> render_size;
  150. u8 reference_frames_to_update_flags = 0xFF; // Save frame to all reference indices by default.
  151. enum class ResetProbabilities : u8 {
  152. No = 0,
  153. // 1 also means No here, but we don't need to do anything with the No case.
  154. OnlyCurrent = 2,
  155. All = 3,
  156. };
  157. ResetProbabilities reset_frame_context = ResetProbabilities::All;
  158. if (is_keyframe) {
  159. type = FrameType::KeyFrame;
  160. TRY(frame_sync_code());
  161. frame_context.color_config = TRY(parse_color_config(frame_context));
  162. frame_size = TRY(parse_frame_size());
  163. render_size = TRY(parse_render_size(frame_size));
  164. } else {
  165. if (!frame_context.shows_a_frame() && TRY_READ(m_bit_stream->read_bit())) {
  166. type = FrameType::IntraOnlyFrame;
  167. } else {
  168. type = FrameType::InterFrame;
  169. reset_frame_context = ResetProbabilities::No;
  170. }
  171. if (!frame_context.error_resilient_mode)
  172. reset_frame_context = static_cast<ResetProbabilities>(TRY_READ(m_bit_stream->read_bits(2)));
  173. if (type == FrameType::IntraOnlyFrame) {
  174. TRY(frame_sync_code());
  175. frame_context.color_config = frame_context.profile > 0 ? TRY(parse_color_config(frame_context)) : ColorConfig();
  176. reference_frames_to_update_flags = TRY_READ(m_bit_stream->read_f8());
  177. frame_size = TRY(parse_frame_size());
  178. render_size = TRY(parse_render_size(frame_size));
  179. } else {
  180. reference_frames_to_update_flags = TRY_READ(m_bit_stream->read_f8());
  181. for (auto i = 0; i < 3; i++) {
  182. frame_context.reference_frame_indices[i] = TRY_READ(m_bit_stream->read_bits(3));
  183. frame_context.reference_frame_sign_biases[LastFrame + i] = TRY_READ(m_bit_stream->read_bit());
  184. }
  185. frame_size = TRY(parse_frame_size_with_refs(frame_context.reference_frame_indices));
  186. render_size = TRY(parse_render_size(frame_size));
  187. frame_context.high_precision_motion_vectors_allowed = TRY_READ(m_bit_stream->read_bit());
  188. frame_context.interpolation_filter = TRY(read_interpolation_filter());
  189. }
  190. }
  191. bool should_replace_probability_context = false;
  192. bool parallel_decoding_mode = true;
  193. if (!frame_context.error_resilient_mode) {
  194. should_replace_probability_context = TRY_READ(m_bit_stream->read_bit());
  195. parallel_decoding_mode = TRY_READ(m_bit_stream->read_bit());
  196. }
  197. u8 probability_context_index = TRY_READ(m_bit_stream->read_bits(2));
  198. switch (reset_frame_context) {
  199. case ResetProbabilities::All:
  200. setup_past_independence();
  201. for (auto i = 0; i < 4; i++) {
  202. m_probability_tables->save_probs(i);
  203. }
  204. probability_context_index = 0;
  205. break;
  206. case ResetProbabilities::OnlyCurrent:
  207. setup_past_independence();
  208. m_probability_tables->save_probs(probability_context_index);
  209. probability_context_index = 0;
  210. break;
  211. default:
  212. break;
  213. }
  214. frame_context.type = type;
  215. DECODER_TRY_ALLOC(frame_context.set_size(frame_size));
  216. frame_context.render_size = render_size;
  217. TRY(compute_image_size(frame_context));
  218. frame_context.reference_frames_to_update_flags = reference_frames_to_update_flags;
  219. frame_context.parallel_decoding_mode = parallel_decoding_mode;
  220. frame_context.should_replace_probability_context = should_replace_probability_context;
  221. frame_context.probability_context_index = probability_context_index;
  222. TRY(loop_filter_params(frame_context));
  223. TRY(quantization_params(frame_context));
  224. TRY(segmentation_params(frame_context));
  225. TRY(parse_tile_counts(frame_context));
  226. frame_context.header_size_in_bytes = TRY_READ(m_bit_stream->read_f16());
  227. return frame_context;
  228. }
  229. DecoderErrorOr<void> Parser::frame_sync_code()
  230. {
  231. if (TRY_READ(m_bit_stream->read_f8()) != 0x49)
  232. return DecoderError::corrupted("frame_sync_code: Byte 0 was not 0x49."sv);
  233. if (TRY_READ(m_bit_stream->read_f8()) != 0x83)
  234. return DecoderError::corrupted("frame_sync_code: Byte 1 was not 0x83."sv);
  235. if (TRY_READ(m_bit_stream->read_f8()) != 0x42)
  236. return DecoderError::corrupted("frame_sync_code: Byte 2 was not 0x42."sv);
  237. return {};
  238. }
  239. DecoderErrorOr<ColorConfig> Parser::parse_color_config(FrameContext const& frame_context)
  240. {
  241. // (6.2.2) color_config( )
  242. u8 bit_depth;
  243. if (frame_context.profile >= 2) {
  244. bit_depth = TRY_READ(m_bit_stream->read_bit()) ? 12 : 10;
  245. } else {
  246. bit_depth = 8;
  247. }
  248. auto color_space = static_cast<ColorSpace>(TRY_READ(m_bit_stream->read_bits(3)));
  249. VERIFY(color_space <= ColorSpace::RGB);
  250. ColorRange color_range;
  251. bool subsampling_x, subsampling_y;
  252. if (color_space != ColorSpace::RGB) {
  253. color_range = TRY(read_color_range());
  254. if (frame_context.profile == 1 || frame_context.profile == 3) {
  255. subsampling_x = TRY_READ(m_bit_stream->read_bit());
  256. subsampling_y = TRY_READ(m_bit_stream->read_bit());
  257. if (TRY_READ(m_bit_stream->read_bit()))
  258. return DecoderError::corrupted("color_config: Subsampling reserved zero was set"sv);
  259. } else {
  260. subsampling_x = true;
  261. subsampling_y = true;
  262. }
  263. } else {
  264. color_range = ColorRange::Full;
  265. if (frame_context.profile == 1 || frame_context.profile == 3) {
  266. subsampling_x = false;
  267. subsampling_y = false;
  268. if (TRY_READ(m_bit_stream->read_bit()))
  269. return DecoderError::corrupted("color_config: RGB reserved zero was set"sv);
  270. } else {
  271. // FIXME: Spec does not specify the subsampling value here. Is this an error or should we set a default?
  272. VERIFY_NOT_REACHED();
  273. }
  274. }
  275. return ColorConfig { bit_depth, color_space, color_range, subsampling_x, subsampling_y };
  276. }
  277. DecoderErrorOr<Gfx::Size<u32>> Parser::parse_frame_size()
  278. {
  279. return Gfx::Size<u32> { TRY_READ(m_bit_stream->read_f16()) + 1, TRY_READ(m_bit_stream->read_f16()) + 1 };
  280. }
  281. DecoderErrorOr<Gfx::Size<u32>> Parser::parse_render_size(Gfx::Size<u32> frame_size)
  282. {
  283. // FIXME: This function should save this bit as a value in the FrameContext. The bit can be
  284. // used in files where the pixel aspect ratio changes between samples in the video.
  285. // If the bit is set, the pixel aspect ratio should be recalculated, whereas if only
  286. // the frame size has changed and the render size is unadjusted, then the pixel aspect
  287. // ratio should be retained and the new render size determined based on that.
  288. // See the Firefox source code here:
  289. // https://searchfox.org/mozilla-central/source/dom/media/platforms/wrappers/MediaChangeMonitor.cpp#268-276
  290. if (!TRY_READ(m_bit_stream->read_bit()))
  291. return frame_size;
  292. return Gfx::Size<u32> { TRY_READ(m_bit_stream->read_f16()) + 1, TRY_READ(m_bit_stream->read_f16()) + 1 };
  293. }
  294. DecoderErrorOr<Gfx::Size<u32>> Parser::parse_frame_size_with_refs(Array<u8, 3> const& reference_indices)
  295. {
  296. Optional<Gfx::Size<u32>> size;
  297. for (auto frame_index : reference_indices) {
  298. if (TRY_READ(m_bit_stream->read_bit())) {
  299. size.emplace(m_ref_frame_size[frame_index]);
  300. break;
  301. }
  302. }
  303. if (size.has_value())
  304. return size.value();
  305. return TRY(parse_frame_size());
  306. }
  307. DecoderErrorOr<void> Parser::compute_image_size(FrameContext& frame_context)
  308. {
  309. // 7.2.6 Compute image size semantics
  310. // When compute_image_size is invoked, the following ordered steps occur:
  311. // 1. If this is the first time compute_image_size is invoked, or if either FrameWidth or FrameHeight have
  312. // changed in value compared to the previous time this function was invoked, then the segmentation map is
  313. // cleared to all zeros by setting SegmentId[ row ][ col ] equal to 0 for row = 0..MiRows-1 and col =
  314. // 0..MiCols-1.
  315. // FIXME: What does this mean? SegmentIds is scoped to one frame, so it will not contain values here. It's
  316. // also suspicious that spec refers to this as SegmentId rather than SegmentIds (plural). Is this
  317. // supposed to refer to PrevSegmentIds?
  318. bool first_invoke = m_is_first_compute_image_size_invoke;
  319. m_is_first_compute_image_size_invoke = false;
  320. bool same_size = m_previous_frame_size == frame_context.size();
  321. // 2. The variable UsePrevFrameMvs is set equal to 1 if all of the following conditions are true:
  322. // a. This is not the first time compute_image_size is invoked.
  323. // b. Both FrameWidth and FrameHeight have the same value compared to the previous time this function
  324. // was invoked.
  325. // c. show_frame was equal to 1 the previous time this function was invoked.
  326. // d. error_resilient_mode is equal to 0.
  327. // e. FrameIsIntra is equal to 0.
  328. // Otherwise, UsePrevFrameMvs is set equal to 0.
  329. m_use_prev_frame_mvs = !first_invoke && same_size && m_previous_show_frame && !frame_context.error_resilient_mode && frame_context.is_inter_predicted();
  330. return {};
  331. }
  332. DecoderErrorOr<InterpolationFilter> Parser::read_interpolation_filter()
  333. {
  334. if (TRY_READ(m_bit_stream->read_bit())) {
  335. return InterpolationFilter::Switchable;
  336. }
  337. return literal_to_type[TRY_READ(m_bit_stream->read_bits(2))];
  338. }
  339. DecoderErrorOr<void> Parser::loop_filter_params(FrameContext& frame_context)
  340. {
  341. frame_context.loop_filter_level = TRY_READ(m_bit_stream->read_bits(6));
  342. frame_context.loop_filter_sharpness = TRY_READ(m_bit_stream->read_bits(3));
  343. frame_context.loop_filter_delta_enabled = TRY_READ(m_bit_stream->read_bit());
  344. auto reference_deltas = m_previous_loop_filter_ref_deltas;
  345. auto mode_deltas = m_previous_loop_filter_mode_deltas;
  346. if (frame_context.loop_filter_delta_enabled && TRY_READ(m_bit_stream->read_bit())) {
  347. for (auto& loop_filter_ref_delta : reference_deltas) {
  348. if (TRY_READ(m_bit_stream->read_bit()))
  349. loop_filter_ref_delta = TRY_READ(m_bit_stream->read_s(6));
  350. }
  351. for (auto& loop_filter_mode_delta : mode_deltas) {
  352. if (TRY_READ(m_bit_stream->read_bit()))
  353. loop_filter_mode_delta = TRY_READ(m_bit_stream->read_s(6));
  354. }
  355. }
  356. frame_context.loop_filter_reference_deltas = reference_deltas;
  357. frame_context.loop_filter_mode_deltas = mode_deltas;
  358. return {};
  359. }
  360. DecoderErrorOr<void> Parser::quantization_params(FrameContext& frame_context)
  361. {
  362. frame_context.base_quantizer_index = TRY_READ(m_bit_stream->read_f8());
  363. frame_context.y_dc_quantizer_index_delta = TRY(read_delta_q());
  364. frame_context.uv_dc_quantizer_index_delta = TRY(read_delta_q());
  365. frame_context.uv_ac_quantizer_index_delta = TRY(read_delta_q());
  366. return {};
  367. }
  368. DecoderErrorOr<i8> Parser::read_delta_q()
  369. {
  370. if (TRY_READ(m_bit_stream->read_bit()))
  371. return TRY_READ(m_bit_stream->read_s(4));
  372. return 0;
  373. }
  374. DecoderErrorOr<void> Parser::segmentation_params(FrameContext& frame_context)
  375. {
  376. frame_context.segmentation_enabled = TRY_READ(m_bit_stream->read_bit());
  377. if (!frame_context.segmentation_enabled)
  378. return {};
  379. frame_context.should_use_absolute_segment_base_quantizer = m_previous_should_use_absolute_segment_base_quantizer;
  380. frame_context.segmentation_features = m_previous_segmentation_features;
  381. if (TRY_READ(m_bit_stream->read_bit())) {
  382. frame_context.use_full_segment_id_tree = true;
  383. for (auto& segmentation_tree_prob : frame_context.full_segment_id_tree_probabilities)
  384. segmentation_tree_prob = TRY(read_prob());
  385. if (TRY_READ(m_bit_stream->read_bit())) {
  386. frame_context.use_predicted_segment_id_tree = true;
  387. for (auto& segmentation_pred_prob : frame_context.predicted_segment_id_tree_probabilities)
  388. segmentation_pred_prob = TRY(read_prob());
  389. }
  390. }
  391. auto segmentation_update_data = (TRY_READ(m_bit_stream->read_bit()));
  392. if (!segmentation_update_data)
  393. return {};
  394. frame_context.should_use_absolute_segment_base_quantizer = TRY_READ(m_bit_stream->read_bit());
  395. for (auto i = 0; i < MAX_SEGMENTS; i++) {
  396. for (auto j = 0; j < SEG_LVL_MAX; j++) {
  397. auto& feature = frame_context.segmentation_features[i][j];
  398. feature.enabled = TRY_READ(m_bit_stream->read_bit());
  399. if (feature.enabled) {
  400. auto bits_to_read = segmentation_feature_bits[j];
  401. feature.value = TRY_READ(m_bit_stream->read_bits(bits_to_read));
  402. if (segmentation_feature_signed[j]) {
  403. if (TRY_READ(m_bit_stream->read_bit()))
  404. feature.value = -feature.value;
  405. }
  406. }
  407. }
  408. }
  409. return {};
  410. }
  411. DecoderErrorOr<u8> Parser::read_prob()
  412. {
  413. if (TRY_READ(m_bit_stream->read_bit()))
  414. return TRY_READ(m_bit_stream->read_f8());
  415. return 255;
  416. }
  417. static u16 calc_min_log2_of_tile_columns(u32 superblock_columns)
  418. {
  419. auto min_log_2 = 0u;
  420. while ((u32)(MAX_TILE_WIDTH_B64 << min_log_2) < superblock_columns)
  421. min_log_2++;
  422. return min_log_2;
  423. }
  424. static u16 calc_max_log2_tile_cols(u32 superblock_columns)
  425. {
  426. u16 max_log_2 = 1;
  427. while ((superblock_columns >> max_log_2) >= MIN_TILE_WIDTH_B64)
  428. max_log_2++;
  429. return max_log_2 - 1;
  430. }
  431. DecoderErrorOr<void> Parser::parse_tile_counts(FrameContext& frame_context)
  432. {
  433. auto superblock_columns = frame_context.superblock_columns();
  434. auto log2_of_tile_columns = calc_min_log2_of_tile_columns(superblock_columns);
  435. auto log2_of_tile_columns_maximum = calc_max_log2_tile_cols(superblock_columns);
  436. while (log2_of_tile_columns < log2_of_tile_columns_maximum) {
  437. if (TRY_READ(m_bit_stream->read_bit()))
  438. log2_of_tile_columns++;
  439. else
  440. break;
  441. }
  442. u16 log2_of_tile_rows = TRY_READ(m_bit_stream->read_bit());
  443. if (log2_of_tile_rows > 0) {
  444. log2_of_tile_rows += TRY_READ(m_bit_stream->read_bit());
  445. }
  446. frame_context.log2_of_tile_counts = Gfx::Size<u16>(log2_of_tile_columns, log2_of_tile_rows);
  447. return {};
  448. }
  449. void Parser::setup_past_independence()
  450. {
  451. m_previous_block_contexts.reset();
  452. m_previous_loop_filter_ref_deltas[IntraFrame] = 1;
  453. m_previous_loop_filter_ref_deltas[LastFrame] = 0;
  454. m_previous_loop_filter_ref_deltas[GoldenFrame] = -1;
  455. m_previous_loop_filter_ref_deltas[AltRefFrame] = -1;
  456. m_previous_loop_filter_mode_deltas.fill(0);
  457. m_previous_should_use_absolute_segment_base_quantizer = false;
  458. for (auto& segment_levels : m_previous_segmentation_features)
  459. segment_levels.fill({ false, 0 });
  460. m_probability_tables->reset_probs();
  461. }
  462. DecoderErrorOr<void> Parser::compressed_header(FrameContext& frame_context)
  463. {
  464. frame_context.transform_mode = TRY(read_tx_mode(frame_context));
  465. if (frame_context.transform_mode == TXModeSelect)
  466. TRY(tx_mode_probs());
  467. TRY(read_coef_probs(frame_context.transform_mode));
  468. TRY(read_skip_prob());
  469. if (frame_context.is_inter_predicted()) {
  470. TRY(read_inter_mode_probs());
  471. if (frame_context.interpolation_filter == Switchable)
  472. TRY(read_interp_filter_probs());
  473. TRY(read_is_inter_probs());
  474. TRY(frame_reference_mode(frame_context));
  475. TRY(frame_reference_mode_probs(frame_context));
  476. TRY(read_y_mode_probs());
  477. TRY(read_partition_probs());
  478. TRY(mv_probs(frame_context));
  479. }
  480. return {};
  481. }
  482. DecoderErrorOr<TXMode> Parser::read_tx_mode(FrameContext const& frame_context)
  483. {
  484. if (frame_context.is_lossless()) {
  485. return TXMode::Only_4x4;
  486. }
  487. auto tx_mode = TRY_READ(m_bit_stream->read_literal(2));
  488. if (tx_mode == Allow_32x32)
  489. tx_mode += TRY_READ(m_bit_stream->read_literal(1));
  490. return static_cast<TXMode>(tx_mode);
  491. }
  492. DecoderErrorOr<void> Parser::tx_mode_probs()
  493. {
  494. auto& tx_probs = m_probability_tables->tx_probs();
  495. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  496. for (auto j = 0; j < TX_SIZES - 3; j++)
  497. tx_probs[TX_8x8][i][j] = TRY(diff_update_prob(tx_probs[TX_8x8][i][j]));
  498. }
  499. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  500. for (auto j = 0; j < TX_SIZES - 2; j++)
  501. tx_probs[TX_16x16][i][j] = TRY(diff_update_prob(tx_probs[TX_16x16][i][j]));
  502. }
  503. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  504. for (auto j = 0; j < TX_SIZES - 1; j++)
  505. tx_probs[TX_32x32][i][j] = TRY(diff_update_prob(tx_probs[TX_32x32][i][j]));
  506. }
  507. return {};
  508. }
  509. DecoderErrorOr<u8> Parser::diff_update_prob(u8 prob)
  510. {
  511. auto update_prob = TRY_READ(m_bit_stream->read_bool(252));
  512. if (update_prob) {
  513. auto delta_prob = TRY(decode_term_subexp());
  514. prob = inv_remap_prob(delta_prob, prob);
  515. }
  516. return prob;
  517. }
  518. DecoderErrorOr<u8> Parser::decode_term_subexp()
  519. {
  520. if (TRY_READ(m_bit_stream->read_literal(1)) == 0)
  521. return TRY_READ(m_bit_stream->read_literal(4));
  522. if (TRY_READ(m_bit_stream->read_literal(1)) == 0)
  523. return TRY_READ(m_bit_stream->read_literal(4)) + 16;
  524. if (TRY_READ(m_bit_stream->read_literal(1)) == 0)
  525. return TRY_READ(m_bit_stream->read_literal(5)) + 32;
  526. auto v = TRY_READ(m_bit_stream->read_literal(7));
  527. if (v < 65)
  528. return v + 64;
  529. return (v << 1u) - 1 + TRY_READ(m_bit_stream->read_literal(1));
  530. }
  531. u8 Parser::inv_remap_prob(u8 delta_prob, u8 prob)
  532. {
  533. u8 m = prob - 1;
  534. auto v = inv_map_table[delta_prob];
  535. if ((m << 1u) <= 255)
  536. return 1 + inv_recenter_nonneg(v, m);
  537. return 255 - inv_recenter_nonneg(v, 254 - m);
  538. }
  539. u8 Parser::inv_recenter_nonneg(u8 v, u8 m)
  540. {
  541. if (v > 2 * m)
  542. return v;
  543. if (v & 1u)
  544. return m - ((v + 1u) >> 1u);
  545. return m + (v >> 1u);
  546. }
  547. DecoderErrorOr<void> Parser::read_coef_probs(TXMode transform_mode)
  548. {
  549. auto max_tx_size = tx_mode_to_biggest_tx_size[transform_mode];
  550. for (u8 tx_size = 0; tx_size <= max_tx_size; tx_size++) {
  551. auto update_probs = TRY_READ(m_bit_stream->read_literal(1));
  552. if (update_probs == 1) {
  553. for (auto i = 0; i < 2; i++) {
  554. for (auto j = 0; j < 2; j++) {
  555. for (auto k = 0; k < 6; k++) {
  556. auto max_l = (k == 0) ? 3 : 6;
  557. for (auto l = 0; l < max_l; l++) {
  558. for (auto m = 0; m < 3; m++) {
  559. auto& prob = m_probability_tables->coef_probs()[tx_size][i][j][k][l][m];
  560. prob = TRY(diff_update_prob(prob));
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }
  568. return {};
  569. }
  570. DecoderErrorOr<void> Parser::read_skip_prob()
  571. {
  572. for (auto i = 0; i < SKIP_CONTEXTS; i++)
  573. m_probability_tables->skip_prob()[i] = TRY(diff_update_prob(m_probability_tables->skip_prob()[i]));
  574. return {};
  575. }
  576. DecoderErrorOr<void> Parser::read_inter_mode_probs()
  577. {
  578. for (auto i = 0; i < INTER_MODE_CONTEXTS; i++) {
  579. for (auto j = 0; j < INTER_MODES - 1; j++)
  580. m_probability_tables->inter_mode_probs()[i][j] = TRY(diff_update_prob(m_probability_tables->inter_mode_probs()[i][j]));
  581. }
  582. return {};
  583. }
  584. DecoderErrorOr<void> Parser::read_interp_filter_probs()
  585. {
  586. for (auto i = 0; i < INTERP_FILTER_CONTEXTS; i++) {
  587. for (auto j = 0; j < SWITCHABLE_FILTERS - 1; j++)
  588. m_probability_tables->interp_filter_probs()[i][j] = TRY(diff_update_prob(m_probability_tables->interp_filter_probs()[i][j]));
  589. }
  590. return {};
  591. }
  592. DecoderErrorOr<void> Parser::read_is_inter_probs()
  593. {
  594. for (auto i = 0; i < IS_INTER_CONTEXTS; i++)
  595. m_probability_tables->is_inter_prob()[i] = TRY(diff_update_prob(m_probability_tables->is_inter_prob()[i]));
  596. return {};
  597. }
  598. static void setup_compound_reference_mode(FrameContext& frame_context)
  599. {
  600. ReferenceFrameType fixed_reference;
  601. ReferenceFramePair variable_references;
  602. if (frame_context.reference_frame_sign_biases[LastFrame] == frame_context.reference_frame_sign_biases[GoldenFrame]) {
  603. fixed_reference = AltRefFrame;
  604. variable_references = { LastFrame, GoldenFrame };
  605. } else if (frame_context.reference_frame_sign_biases[LastFrame] == frame_context.reference_frame_sign_biases[AltRefFrame]) {
  606. fixed_reference = GoldenFrame;
  607. variable_references = { LastFrame, AltRefFrame };
  608. } else {
  609. fixed_reference = LastFrame;
  610. variable_references = { GoldenFrame, AltRefFrame };
  611. }
  612. frame_context.fixed_reference_type = fixed_reference;
  613. frame_context.variable_reference_types = variable_references;
  614. }
  615. DecoderErrorOr<void> Parser::frame_reference_mode(FrameContext& frame_context)
  616. {
  617. auto compound_reference_allowed = false;
  618. for (size_t i = 2; i <= REFS_PER_FRAME; i++) {
  619. if (frame_context.reference_frame_sign_biases[i] != frame_context.reference_frame_sign_biases[1])
  620. compound_reference_allowed = true;
  621. }
  622. ReferenceMode reference_mode;
  623. if (compound_reference_allowed) {
  624. auto non_single_reference = TRY_READ(m_bit_stream->read_literal(1));
  625. if (non_single_reference == 0) {
  626. reference_mode = SingleReference;
  627. } else {
  628. auto reference_select = TRY_READ(m_bit_stream->read_literal(1));
  629. if (reference_select == 0)
  630. reference_mode = CompoundReference;
  631. else
  632. reference_mode = ReferenceModeSelect;
  633. }
  634. } else {
  635. reference_mode = SingleReference;
  636. }
  637. frame_context.reference_mode = reference_mode;
  638. if (reference_mode != SingleReference)
  639. setup_compound_reference_mode(frame_context);
  640. return {};
  641. }
  642. DecoderErrorOr<void> Parser::frame_reference_mode_probs(FrameContext const& frame_context)
  643. {
  644. if (frame_context.reference_mode == ReferenceModeSelect) {
  645. for (auto i = 0; i < COMP_MODE_CONTEXTS; i++) {
  646. auto& comp_mode_prob = m_probability_tables->comp_mode_prob();
  647. comp_mode_prob[i] = TRY(diff_update_prob(comp_mode_prob[i]));
  648. }
  649. }
  650. if (frame_context.reference_mode != CompoundReference) {
  651. for (auto i = 0; i < REF_CONTEXTS; i++) {
  652. auto& single_ref_prob = m_probability_tables->single_ref_prob();
  653. single_ref_prob[i][0] = TRY(diff_update_prob(single_ref_prob[i][0]));
  654. single_ref_prob[i][1] = TRY(diff_update_prob(single_ref_prob[i][1]));
  655. }
  656. }
  657. if (frame_context.reference_mode != SingleReference) {
  658. for (auto i = 0; i < REF_CONTEXTS; i++) {
  659. auto& comp_ref_prob = m_probability_tables->comp_ref_prob();
  660. comp_ref_prob[i] = TRY(diff_update_prob(comp_ref_prob[i]));
  661. }
  662. }
  663. return {};
  664. }
  665. DecoderErrorOr<void> Parser::read_y_mode_probs()
  666. {
  667. for (auto i = 0; i < BLOCK_SIZE_GROUPS; i++) {
  668. for (auto j = 0; j < INTRA_MODES - 1; j++) {
  669. auto& y_mode_probs = m_probability_tables->y_mode_probs();
  670. y_mode_probs[i][j] = TRY(diff_update_prob(y_mode_probs[i][j]));
  671. }
  672. }
  673. return {};
  674. }
  675. DecoderErrorOr<void> Parser::read_partition_probs()
  676. {
  677. for (auto i = 0; i < PARTITION_CONTEXTS; i++) {
  678. for (auto j = 0; j < PARTITION_TYPES - 1; j++) {
  679. auto& partition_probs = m_probability_tables->partition_probs();
  680. partition_probs[i][j] = TRY(diff_update_prob(partition_probs[i][j]));
  681. }
  682. }
  683. return {};
  684. }
  685. DecoderErrorOr<void> Parser::mv_probs(FrameContext const& frame_context)
  686. {
  687. for (auto j = 0; j < MV_JOINTS - 1; j++) {
  688. auto& mv_joint_probs = m_probability_tables->mv_joint_probs();
  689. mv_joint_probs[j] = TRY(update_mv_prob(mv_joint_probs[j]));
  690. }
  691. for (auto i = 0; i < 2; i++) {
  692. auto& mv_sign_prob = m_probability_tables->mv_sign_prob();
  693. mv_sign_prob[i] = TRY(update_mv_prob(mv_sign_prob[i]));
  694. for (auto j = 0; j < MV_CLASSES - 1; j++) {
  695. auto& mv_class_probs = m_probability_tables->mv_class_probs();
  696. mv_class_probs[i][j] = TRY(update_mv_prob(mv_class_probs[i][j]));
  697. }
  698. auto& mv_class0_bit_prob = m_probability_tables->mv_class0_bit_prob();
  699. mv_class0_bit_prob[i] = TRY(update_mv_prob(mv_class0_bit_prob[i]));
  700. for (auto j = 0; j < MV_OFFSET_BITS; j++) {
  701. auto& mv_bits_prob = m_probability_tables->mv_bits_prob();
  702. mv_bits_prob[i][j] = TRY(update_mv_prob(mv_bits_prob[i][j]));
  703. }
  704. }
  705. for (auto i = 0; i < 2; i++) {
  706. for (auto j = 0; j < CLASS0_SIZE; j++) {
  707. for (auto k = 0; k < MV_FR_SIZE - 1; k++) {
  708. auto& mv_class0_fr_probs = m_probability_tables->mv_class0_fr_probs();
  709. mv_class0_fr_probs[i][j][k] = TRY(update_mv_prob(mv_class0_fr_probs[i][j][k]));
  710. }
  711. }
  712. for (auto k = 0; k < MV_FR_SIZE - 1; k++) {
  713. auto& mv_fr_probs = m_probability_tables->mv_fr_probs();
  714. mv_fr_probs[i][k] = TRY(update_mv_prob(mv_fr_probs[i][k]));
  715. }
  716. }
  717. if (frame_context.high_precision_motion_vectors_allowed) {
  718. for (auto i = 0; i < 2; i++) {
  719. auto& mv_class0_hp_prob = m_probability_tables->mv_class0_hp_prob();
  720. auto& mv_hp_prob = m_probability_tables->mv_hp_prob();
  721. mv_class0_hp_prob[i] = TRY(update_mv_prob(mv_class0_hp_prob[i]));
  722. mv_hp_prob[i] = TRY(update_mv_prob(mv_hp_prob[i]));
  723. }
  724. }
  725. return {};
  726. }
  727. DecoderErrorOr<u8> Parser::update_mv_prob(u8 prob)
  728. {
  729. if (TRY_READ(m_bit_stream->read_bool(252))) {
  730. return (TRY_READ(m_bit_stream->read_literal(7)) << 1u) | 1u;
  731. }
  732. return prob;
  733. }
  734. DecoderErrorOr<void> Parser::decode_tiles(FrameContext& frame_context)
  735. {
  736. auto log2_dimensions = frame_context.log2_of_tile_counts;
  737. auto tile_cols = 1 << log2_dimensions.width();
  738. auto tile_rows = 1 << log2_dimensions.height();
  739. clear_above_context(frame_context);
  740. for (auto tile_row = 0; tile_row < tile_rows; tile_row++) {
  741. for (auto tile_col = 0; tile_col < tile_cols; tile_col++) {
  742. auto last_tile = (tile_row == tile_rows - 1) && (tile_col == tile_cols - 1);
  743. u64 tile_size;
  744. if (last_tile)
  745. tile_size = m_bit_stream->bytes_remaining();
  746. else
  747. tile_size = TRY_READ(m_bit_stream->read_bits(32));
  748. auto rows_start = get_tile_offset(tile_row, frame_context.rows(), log2_dimensions.height());
  749. auto rows_end = get_tile_offset(tile_row + 1, frame_context.rows(), log2_dimensions.height());
  750. auto columns_start = get_tile_offset(tile_col, frame_context.columns(), log2_dimensions.width());
  751. auto columns_end = get_tile_offset(tile_col + 1, frame_context.columns(), log2_dimensions.width());
  752. auto tile_context = TileContext(frame_context, rows_start, rows_end, columns_start, columns_end);
  753. TRY_READ(m_bit_stream->init_bool(tile_size));
  754. TRY(decode_tile(tile_context));
  755. TRY_READ(m_bit_stream->exit_bool());
  756. }
  757. }
  758. return {};
  759. }
  760. template<typename T>
  761. void Parser::clear_context(Vector<T>& context, size_t size)
  762. {
  763. context.resize_and_keep_capacity(size);
  764. __builtin_memset(context.data(), 0, sizeof(T) * size);
  765. }
  766. template<typename T>
  767. void Parser::clear_context(Vector<Vector<T>>& context, size_t outer_size, size_t inner_size)
  768. {
  769. if (context.size() < outer_size)
  770. context.resize(outer_size);
  771. for (auto& sub_vector : context)
  772. clear_context(sub_vector, inner_size);
  773. }
  774. void Parser::clear_above_context(FrameContext& frame_context)
  775. {
  776. for (auto i = 0u; i < m_above_nonzero_context.size(); i++)
  777. clear_context(m_above_nonzero_context[i], 2 * frame_context.columns());
  778. clear_context(m_above_seg_pred_context, frame_context.columns());
  779. clear_context(m_above_partition_context, frame_context.superblock_columns() * 8);
  780. }
  781. u32 Parser::get_tile_offset(u32 tile_num, u32 mis, u32 tile_size_log2)
  782. {
  783. u32 super_blocks = (mis + 7) >> 3u;
  784. u32 offset = ((tile_num * super_blocks) >> tile_size_log2) << 3u;
  785. return min(offset, mis);
  786. }
  787. DecoderErrorOr<void> Parser::decode_tile(TileContext& tile_context)
  788. {
  789. for (auto row = tile_context.rows_start; row < tile_context.rows_end; row += 8) {
  790. clear_left_context(tile_context);
  791. for (auto col = tile_context.columns_start; col < tile_context.columns_end; col += 8) {
  792. TRY(decode_partition(tile_context, row, col, Block_64x64));
  793. }
  794. }
  795. return {};
  796. }
  797. void Parser::clear_left_context(TileContext& tile_context)
  798. {
  799. for (auto i = 0u; i < m_left_nonzero_context.size(); i++)
  800. clear_context(m_left_nonzero_context[i], 2 * tile_context.frame_context.rows());
  801. clear_context(m_left_seg_pred_context, tile_context.frame_context.rows());
  802. clear_context(m_left_partition_context, tile_context.frame_context.superblock_rows() * 8);
  803. }
  804. DecoderErrorOr<void> Parser::decode_partition(TileContext& tile_context, u32 row, u32 column, BlockSubsize subsize)
  805. {
  806. if (row >= tile_context.frame_context.rows() || column >= tile_context.frame_context.columns())
  807. return {};
  808. u8 num_8x8 = num_8x8_blocks_wide_lookup[subsize];
  809. auto half_block_8x8 = num_8x8 >> 1;
  810. bool has_rows = (row + half_block_8x8) < tile_context.frame_context.rows();
  811. bool has_cols = (column + half_block_8x8) < tile_context.frame_context.columns();
  812. auto partition = TRY_READ(TreeParser::parse_partition(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, has_rows, has_cols, subsize, num_8x8, m_above_partition_context, m_left_partition_context, row, column, !tile_context.frame_context.is_inter_predicted()));
  813. auto child_subsize = subsize_lookup[partition][subsize];
  814. if (child_subsize < Block_8x8 || partition == PartitionNone) {
  815. TRY(decode_block(tile_context, row, column, child_subsize));
  816. } else if (partition == PartitionHorizontal) {
  817. TRY(decode_block(tile_context, row, column, child_subsize));
  818. if (has_rows)
  819. TRY(decode_block(tile_context, row + half_block_8x8, column, child_subsize));
  820. } else if (partition == PartitionVertical) {
  821. TRY(decode_block(tile_context, row, column, child_subsize));
  822. if (has_cols)
  823. TRY(decode_block(tile_context, row, column + half_block_8x8, child_subsize));
  824. } else {
  825. TRY(decode_partition(tile_context, row, column, child_subsize));
  826. TRY(decode_partition(tile_context, row, column + half_block_8x8, child_subsize));
  827. TRY(decode_partition(tile_context, row + half_block_8x8, column, child_subsize));
  828. TRY(decode_partition(tile_context, row + half_block_8x8, column + half_block_8x8, child_subsize));
  829. }
  830. if (subsize == Block_8x8 || partition != PartitionSplit) {
  831. auto above_context = 15 >> b_width_log2_lookup[child_subsize];
  832. auto left_context = 15 >> b_height_log2_lookup[child_subsize];
  833. for (size_t i = 0; i < num_8x8; i++) {
  834. m_above_partition_context[column + i] = above_context;
  835. m_left_partition_context[row + i] = left_context;
  836. }
  837. }
  838. return {};
  839. }
  840. size_t Parser::get_image_index(FrameContext const& frame_context, u32 row, u32 column) const
  841. {
  842. VERIFY(row < frame_context.rows() && column < frame_context.columns());
  843. return row * frame_context.columns() + column;
  844. }
  845. DecoderErrorOr<void> Parser::decode_block(TileContext& tile_context, u32 row, u32 column, BlockSubsize subsize)
  846. {
  847. auto above_context = row > 0 ? tile_context.frame_block_contexts().at(row - 1, column) : FrameBlockContext();
  848. auto left_context = column > tile_context.columns_start ? tile_context.frame_block_contexts().at(row, column - 1) : FrameBlockContext();
  849. auto block_context = BlockContext(tile_context, row, column, subsize);
  850. TRY(mode_info(block_context, above_context, left_context));
  851. auto had_residual_tokens = TRY(residual(block_context, above_context.is_available, left_context.is_available));
  852. if (block_context.is_inter_predicted() && subsize >= Block_8x8 && !had_residual_tokens)
  853. block_context.should_skip_residuals = true;
  854. for (size_t y = 0; y < block_context.contexts_view.height(); y++) {
  855. for (size_t x = 0; x < block_context.contexts_view.width(); x++) {
  856. auto sub_block_context = FrameBlockContext { true, block_context.should_skip_residuals, block_context.tx_size, block_context.y_prediction_mode(), block_context.sub_block_prediction_modes, block_context.interpolation_filter, block_context.reference_frame_types, block_context.sub_block_motion_vectors, block_context.segment_id };
  857. block_context.contexts_view.at(y, x) = sub_block_context;
  858. VERIFY(block_context.frame_block_contexts().at(row + y, column + x).tx_size == sub_block_context.tx_size);
  859. }
  860. }
  861. return {};
  862. }
  863. DecoderErrorOr<void> Parser::mode_info(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  864. {
  865. if (block_context.frame_context.is_inter_predicted())
  866. TRY(inter_frame_mode_info(block_context, above_context, left_context));
  867. else
  868. TRY(intra_frame_mode_info(block_context, above_context, left_context));
  869. return {};
  870. }
  871. DecoderErrorOr<void> Parser::intra_frame_mode_info(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  872. {
  873. block_context.reference_frame_types = { ReferenceFrameType::None, ReferenceFrameType::None };
  874. VERIFY(!block_context.is_inter_predicted());
  875. TRY(set_intra_segment_id(block_context));
  876. block_context.should_skip_residuals = TRY(read_should_skip_residuals(block_context, above_context, left_context));
  877. block_context.tx_size = TRY(read_tx_size(block_context, above_context, left_context, true));
  878. // FIXME: This if statement is also present in parse_default_intra_mode. The selection of parameters for
  879. // the probability table lookup should be inlined here.
  880. if (block_context.size >= Block_8x8) {
  881. auto mode = TRY_READ(TreeParser::parse_default_intra_mode(*m_bit_stream, *m_probability_tables, block_context.size, above_context, left_context, block_context.sub_block_prediction_modes, 0, 0));
  882. for (auto& block_sub_mode : block_context.sub_block_prediction_modes)
  883. block_sub_mode = mode;
  884. } else {
  885. auto size_in_4x4_blocks = block_context.get_size_in_4x4_blocks();
  886. for (auto idy = 0; idy < 2; idy += size_in_4x4_blocks.height()) {
  887. for (auto idx = 0; idx < 2; idx += size_in_4x4_blocks.width()) {
  888. auto sub_mode = TRY_READ(TreeParser::parse_default_intra_mode(*m_bit_stream, *m_probability_tables, block_context.size, above_context, left_context, block_context.sub_block_prediction_modes, idx, idy));
  889. for (auto y = 0; y < size_in_4x4_blocks.height(); y++) {
  890. for (auto x = 0; x < size_in_4x4_blocks.width(); x++) {
  891. auto index = (idy + y) * 2 + idx + x;
  892. block_context.sub_block_prediction_modes[index] = sub_mode;
  893. }
  894. }
  895. }
  896. }
  897. }
  898. block_context.uv_prediction_mode = TRY_READ(TreeParser::parse_default_uv_mode(*m_bit_stream, *m_probability_tables, block_context.y_prediction_mode()));
  899. return {};
  900. }
  901. DecoderErrorOr<void> Parser::set_intra_segment_id(BlockContext& block_context)
  902. {
  903. if (block_context.frame_context.segmentation_enabled && block_context.frame_context.use_full_segment_id_tree)
  904. block_context.segment_id = TRY_READ(TreeParser::parse_segment_id(*m_bit_stream, block_context.frame_context.full_segment_id_tree_probabilities));
  905. else
  906. block_context.segment_id = 0;
  907. return {};
  908. }
  909. DecoderErrorOr<bool> Parser::read_should_skip_residuals(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  910. {
  911. if (seg_feature_active(block_context, SEG_LVL_SKIP))
  912. return true;
  913. return TRY_READ(TreeParser::parse_skip(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_context, left_context));
  914. }
  915. bool Parser::seg_feature_active(BlockContext const& block_context, u8 feature)
  916. {
  917. return block_context.frame_context.segmentation_features[block_context.segment_id][feature].enabled;
  918. }
  919. DecoderErrorOr<TXSize> Parser::read_tx_size(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context, bool allow_select)
  920. {
  921. auto max_tx_size = max_txsize_lookup[block_context.size];
  922. if (allow_select && block_context.frame_context.transform_mode == TXModeSelect && block_context.size >= Block_8x8)
  923. return (TRY_READ(TreeParser::parse_tx_size(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, max_tx_size, above_context, left_context)));
  924. return min(max_tx_size, tx_mode_to_biggest_tx_size[block_context.frame_context.transform_mode]);
  925. }
  926. DecoderErrorOr<void> Parser::inter_frame_mode_info(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  927. {
  928. TRY(set_inter_segment_id(block_context));
  929. block_context.should_skip_residuals = TRY(read_should_skip_residuals(block_context, above_context, left_context));
  930. auto is_inter = TRY(read_is_inter(block_context, above_context, left_context));
  931. block_context.tx_size = TRY(read_tx_size(block_context, above_context, left_context, !block_context.should_skip_residuals || !is_inter));
  932. if (is_inter) {
  933. TRY(inter_block_mode_info(block_context, above_context, left_context));
  934. } else {
  935. TRY(intra_block_mode_info(block_context));
  936. }
  937. return {};
  938. }
  939. DecoderErrorOr<void> Parser::set_inter_segment_id(BlockContext& block_context)
  940. {
  941. if (!block_context.frame_context.segmentation_enabled) {
  942. block_context.segment_id = 0;
  943. return {};
  944. }
  945. auto predicted_segment_id = get_segment_id(block_context);
  946. if (!block_context.frame_context.use_full_segment_id_tree) {
  947. block_context.segment_id = predicted_segment_id;
  948. return {};
  949. }
  950. if (!block_context.frame_context.use_predicted_segment_id_tree) {
  951. block_context.segment_id = TRY_READ(TreeParser::parse_segment_id(*m_bit_stream, block_context.frame_context.full_segment_id_tree_probabilities));
  952. return {};
  953. }
  954. auto seg_id_predicted = TRY_READ(TreeParser::parse_segment_id_predicted(*m_bit_stream, block_context.frame_context.predicted_segment_id_tree_probabilities, m_left_seg_pred_context[block_context.row], m_above_seg_pred_context[block_context.column]));
  955. if (seg_id_predicted)
  956. block_context.segment_id = predicted_segment_id;
  957. else
  958. block_context.segment_id = TRY_READ(TreeParser::parse_segment_id(*m_bit_stream, block_context.frame_context.full_segment_id_tree_probabilities));
  959. for (size_t i = 0; i < num_8x8_blocks_wide_lookup[block_context.size]; i++) {
  960. auto index = block_context.column + i;
  961. // (7.4.1) AboveSegPredContext[ i ] only needs to be set to 0 for i = 0..MiCols-1.
  962. if (index < m_above_seg_pred_context.size())
  963. m_above_seg_pred_context[index] = seg_id_predicted;
  964. }
  965. for (size_t i = 0; i < num_8x8_blocks_high_lookup[block_context.size]; i++) {
  966. auto index = block_context.row + i;
  967. // (7.4.1) LeftSegPredContext[ i ] only needs to be set to 0 for i = 0..MiRows-1.
  968. if (index < m_above_seg_pred_context.size())
  969. m_left_seg_pred_context[block_context.row + i] = seg_id_predicted;
  970. }
  971. return {};
  972. }
  973. u8 Parser::get_segment_id(BlockContext const& block_context)
  974. {
  975. auto bw = num_8x8_blocks_wide_lookup[block_context.size];
  976. auto bh = num_8x8_blocks_high_lookup[block_context.size];
  977. auto xmis = min(block_context.frame_context.columns() - block_context.column, (u32)bw);
  978. auto ymis = min(block_context.frame_context.rows() - block_context.row, (u32)bh);
  979. u8 segment = 7;
  980. for (size_t y = 0; y < ymis; y++) {
  981. for (size_t x = 0; x < xmis; x++) {
  982. segment = min(segment, m_previous_block_contexts.index_at(block_context.row + y, block_context.column + x));
  983. }
  984. }
  985. return segment;
  986. }
  987. DecoderErrorOr<bool> Parser::read_is_inter(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  988. {
  989. if (seg_feature_active(block_context, SEG_LVL_REF_FRAME))
  990. return block_context.frame_context.segmentation_features[block_context.segment_id][SEG_LVL_REF_FRAME].value != IntraFrame;
  991. return TRY_READ(TreeParser::parse_block_is_inter_predicted(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_context, left_context));
  992. }
  993. DecoderErrorOr<void> Parser::intra_block_mode_info(BlockContext& block_context)
  994. {
  995. block_context.reference_frame_types = { ReferenceFrameType::None, ReferenceFrameType::None };
  996. VERIFY(!block_context.is_inter_predicted());
  997. auto& sub_modes = block_context.sub_block_prediction_modes;
  998. if (block_context.size >= Block_8x8) {
  999. auto mode = TRY_READ(TreeParser::parse_intra_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, block_context.size));
  1000. for (auto& block_sub_mode : sub_modes)
  1001. block_sub_mode = mode;
  1002. } else {
  1003. auto size_in_4x4_blocks = block_context.get_size_in_4x4_blocks();
  1004. for (auto idy = 0; idy < 2; idy += size_in_4x4_blocks.height()) {
  1005. for (auto idx = 0; idx < 2; idx += size_in_4x4_blocks.width()) {
  1006. auto sub_intra_mode = TRY_READ(TreeParser::parse_sub_intra_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter));
  1007. for (auto y = 0; y < size_in_4x4_blocks.height(); y++) {
  1008. for (auto x = 0; x < size_in_4x4_blocks.width(); x++)
  1009. sub_modes[(idy + y) * 2 + idx + x] = sub_intra_mode;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. block_context.uv_prediction_mode = TRY_READ(TreeParser::parse_uv_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, block_context.y_prediction_mode()));
  1015. return {};
  1016. }
  1017. static void select_best_reference_motion_vectors(BlockContext& block_context, MotionVectorPair reference_motion_vectors, BlockMotionVectorCandidates& candidates, ReferenceIndex);
  1018. DecoderErrorOr<void> Parser::inter_block_mode_info(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  1019. {
  1020. TRY(read_ref_frames(block_context, above_context, left_context));
  1021. VERIFY(block_context.is_inter_predicted());
  1022. BlockMotionVectorCandidates motion_vector_candidates;
  1023. auto reference_motion_vectors = find_reference_motion_vectors(block_context, block_context.reference_frame_types.primary, -1);
  1024. select_best_reference_motion_vectors(block_context, reference_motion_vectors, motion_vector_candidates, ReferenceIndex::Primary);
  1025. if (block_context.is_compound()) {
  1026. auto reference_motion_vectors = find_reference_motion_vectors(block_context, block_context.reference_frame_types.secondary, -1);
  1027. select_best_reference_motion_vectors(block_context, reference_motion_vectors, motion_vector_candidates, ReferenceIndex::Secondary);
  1028. }
  1029. if (seg_feature_active(block_context, SEG_LVL_SKIP)) {
  1030. block_context.y_prediction_mode() = PredictionMode::ZeroMv;
  1031. } else if (block_context.size >= Block_8x8) {
  1032. block_context.y_prediction_mode() = TRY_READ(TreeParser::parse_inter_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, m_mode_context[block_context.reference_frame_types.primary]));
  1033. }
  1034. if (block_context.frame_context.interpolation_filter == Switchable)
  1035. block_context.interpolation_filter = TRY_READ(TreeParser::parse_interpolation_filter(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_context, left_context));
  1036. else
  1037. block_context.interpolation_filter = block_context.frame_context.interpolation_filter;
  1038. if (block_context.size < Block_8x8) {
  1039. auto size_in_4x4_blocks = block_context.get_size_in_4x4_blocks();
  1040. for (auto idy = 0; idy < 2; idy += size_in_4x4_blocks.height()) {
  1041. for (auto idx = 0; idx < 2; idx += size_in_4x4_blocks.width()) {
  1042. block_context.y_prediction_mode() = TRY_READ(TreeParser::parse_inter_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, m_mode_context[block_context.reference_frame_types.primary]));
  1043. if (block_context.y_prediction_mode() == PredictionMode::NearestMv || block_context.y_prediction_mode() == PredictionMode::NearMv) {
  1044. select_best_sub_block_reference_motion_vectors(block_context, motion_vector_candidates, idy * 2 + idx, ReferenceIndex::Primary);
  1045. if (block_context.is_compound())
  1046. select_best_sub_block_reference_motion_vectors(block_context, motion_vector_candidates, idy * 2 + idx, ReferenceIndex::Secondary);
  1047. }
  1048. auto new_motion_vector_pair = TRY(get_motion_vector(block_context, motion_vector_candidates));
  1049. for (auto y = 0; y < size_in_4x4_blocks.height(); y++) {
  1050. for (auto x = 0; x < size_in_4x4_blocks.width(); x++) {
  1051. auto sub_block_index = (idy + y) * 2 + idx + x;
  1052. block_context.sub_block_motion_vectors[sub_block_index] = new_motion_vector_pair;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. return {};
  1058. }
  1059. auto new_motion_vector_pair = TRY(get_motion_vector(block_context, motion_vector_candidates));
  1060. for (auto block = 0; block < 4; block++)
  1061. block_context.sub_block_motion_vectors[block] = new_motion_vector_pair;
  1062. return {};
  1063. }
  1064. DecoderErrorOr<void> Parser::read_ref_frames(BlockContext& block_context, FrameBlockContext above_context, FrameBlockContext left_context)
  1065. {
  1066. if (seg_feature_active(block_context, SEG_LVL_REF_FRAME)) {
  1067. block_context.reference_frame_types = { static_cast<ReferenceFrameType>(block_context.frame_context.segmentation_features[block_context.segment_id][SEG_LVL_REF_FRAME].value), None };
  1068. return {};
  1069. }
  1070. ReferenceMode compound_mode = block_context.frame_context.reference_mode;
  1071. auto fixed_reference = block_context.frame_context.fixed_reference_type;
  1072. if (compound_mode == ReferenceModeSelect)
  1073. compound_mode = TRY_READ(TreeParser::parse_comp_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, fixed_reference, above_context, left_context));
  1074. if (compound_mode == CompoundReference) {
  1075. auto variable_references = block_context.frame_context.variable_reference_types;
  1076. auto fixed_reference_index = ReferenceIndex::Primary;
  1077. auto variable_reference_index = ReferenceIndex::Secondary;
  1078. if (block_context.frame_context.reference_frame_sign_biases[fixed_reference])
  1079. swap(fixed_reference_index, variable_reference_index);
  1080. auto variable_reference_selection = TRY_READ(TreeParser::parse_comp_ref(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, fixed_reference, variable_references, variable_reference_index, above_context, left_context));
  1081. block_context.reference_frame_types[fixed_reference_index] = fixed_reference;
  1082. block_context.reference_frame_types[variable_reference_index] = variable_references[variable_reference_selection];
  1083. return {};
  1084. }
  1085. // FIXME: Maybe consolidate this into a tree. Context is different between part 1 and 2 but still, it would look nice here.
  1086. ReferenceFrameType primary_type = ReferenceFrameType::LastFrame;
  1087. auto single_ref_p1 = TRY_READ(TreeParser::parse_single_ref_part_1(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_context, left_context));
  1088. if (single_ref_p1) {
  1089. auto single_ref_p2 = TRY_READ(TreeParser::parse_single_ref_part_2(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_context, left_context));
  1090. primary_type = single_ref_p2 ? ReferenceFrameType::AltRefFrame : ReferenceFrameType::GoldenFrame;
  1091. }
  1092. block_context.reference_frame_types = { primary_type, ReferenceFrameType::None };
  1093. return {};
  1094. }
  1095. // assign_mv( isCompound ) in the spec.
  1096. DecoderErrorOr<MotionVectorPair> Parser::get_motion_vector(BlockContext const& block_context, BlockMotionVectorCandidates const& candidates)
  1097. {
  1098. MotionVectorPair result;
  1099. auto read_one = [&](ReferenceIndex index) -> DecoderErrorOr<void> {
  1100. switch (block_context.y_prediction_mode()) {
  1101. case PredictionMode::NewMv:
  1102. result[index] = TRY(read_motion_vector(block_context, candidates, index));
  1103. break;
  1104. case PredictionMode::NearestMv:
  1105. result[index] = candidates[index].nearest_vector;
  1106. break;
  1107. case PredictionMode::NearMv:
  1108. result[index] = candidates[index].near_vector;
  1109. break;
  1110. default:
  1111. result[index] = {};
  1112. break;
  1113. }
  1114. return {};
  1115. };
  1116. TRY(read_one(ReferenceIndex::Primary));
  1117. if (block_context.is_compound())
  1118. TRY(read_one(ReferenceIndex::Secondary));
  1119. return result;
  1120. }
  1121. // use_mv_hp( deltaMv ) in the spec.
  1122. static bool should_use_high_precision_motion_vector(MotionVector const& delta_vector)
  1123. {
  1124. return (abs(delta_vector.row()) >> 3) < COMPANDED_MVREF_THRESH && (abs(delta_vector.column()) >> 3) < COMPANDED_MVREF_THRESH;
  1125. }
  1126. // read_mv( ref ) in the spec.
  1127. DecoderErrorOr<MotionVector> Parser::read_motion_vector(BlockContext const& block_context, BlockMotionVectorCandidates const& candidates, ReferenceIndex reference_index)
  1128. {
  1129. m_use_hp = block_context.frame_context.high_precision_motion_vectors_allowed && should_use_high_precision_motion_vector(candidates[reference_index].best_vector);
  1130. MotionVector delta_vector;
  1131. auto joint = TRY_READ(TreeParser::parse_motion_vector_joint(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter));
  1132. if ((joint & MotionVectorNonZeroRow) != 0)
  1133. delta_vector.set_row(TRY(read_single_motion_vector_component(0)));
  1134. if ((joint & MotionVectorNonZeroColumn) != 0)
  1135. delta_vector.set_column(TRY(read_single_motion_vector_component(1)));
  1136. return candidates[reference_index].best_vector + delta_vector;
  1137. }
  1138. // read_mv_component( comp ) in the spec.
  1139. DecoderErrorOr<i32> Parser::read_single_motion_vector_component(u8 component)
  1140. {
  1141. auto mv_sign = TRY_READ(TreeParser::parse_motion_vector_sign(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component));
  1142. auto mv_class = TRY_READ(TreeParser::parse_motion_vector_class(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component));
  1143. u32 magnitude;
  1144. if (mv_class == MvClass0) {
  1145. auto mv_class0_bit = TRY_READ(TreeParser::parse_motion_vector_class0_bit(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component));
  1146. auto mv_class0_fr = TRY_READ(TreeParser::parse_motion_vector_class0_fr(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component, mv_class0_bit));
  1147. auto mv_class0_hp = TRY_READ(TreeParser::parse_motion_vector_class0_hp(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component, m_use_hp));
  1148. magnitude = ((mv_class0_bit << 3) | (mv_class0_fr << 1) | mv_class0_hp) + 1;
  1149. } else {
  1150. u32 bits = 0;
  1151. for (u8 i = 0; i < mv_class; i++) {
  1152. auto mv_bit = TRY_READ(TreeParser::parse_motion_vector_bit(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component, i));
  1153. bits |= mv_bit << i;
  1154. }
  1155. magnitude = CLASS0_SIZE << (mv_class + 2);
  1156. auto mv_fr = TRY_READ(TreeParser::parse_motion_vector_fr(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component));
  1157. auto mv_hp = TRY_READ(TreeParser::parse_motion_vector_hp(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, component, m_use_hp));
  1158. magnitude += ((bits << 3) | (mv_fr << 1) | mv_hp) + 1;
  1159. }
  1160. return (mv_sign ? -1 : 1) * static_cast<i32>(magnitude);
  1161. }
  1162. Gfx::Point<size_t> Parser::get_decoded_point_for_plane(FrameContext const& frame_context, u32 column, u32 row, u8 plane)
  1163. {
  1164. (void)frame_context;
  1165. if (plane == 0)
  1166. return { column * 8, row * 8 };
  1167. return { (column * 8) >> frame_context.color_config.subsampling_x, (row * 8) >> frame_context.color_config.subsampling_y };
  1168. }
  1169. Gfx::Size<size_t> Parser::get_decoded_size_for_plane(FrameContext const& frame_context, u8 plane)
  1170. {
  1171. auto point = get_decoded_point_for_plane(frame_context, frame_context.columns(), frame_context.rows(), plane);
  1172. return { point.x(), point.y() };
  1173. }
  1174. static BlockSubsize get_plane_block_size(bool subsampling_x, bool subsampling_y, u32 subsize, u8 plane)
  1175. {
  1176. auto sub_x = (plane > 0) ? subsampling_x : 0;
  1177. auto sub_y = (plane > 0) ? subsampling_y : 0;
  1178. return ss_size_lookup[subsize][sub_x][sub_y];
  1179. }
  1180. static TXSize get_uv_tx_size(bool subsampling_x, bool subsampling_y, TXSize tx_size, BlockSubsize size)
  1181. {
  1182. if (size < Block_8x8)
  1183. return TX_4x4;
  1184. return min(tx_size, max_txsize_lookup[get_plane_block_size(subsampling_x, subsampling_y, size, 1)]);
  1185. }
  1186. static u8 select_transform_type(BlockContext const& block_context, u8 plane, TXSize tx_size, u32 block_index)
  1187. {
  1188. if (plane > 0 || tx_size == TX_32x32)
  1189. return DCT_DCT;
  1190. if (tx_size == TX_4x4) {
  1191. if (block_context.frame_context.is_lossless() || block_context.is_inter_predicted())
  1192. return DCT_DCT;
  1193. return mode_to_txfm_map[to_underlying(block_context.size < Block_8x8 ? block_context.sub_block_prediction_modes[block_index] : block_context.y_prediction_mode())];
  1194. }
  1195. return mode_to_txfm_map[to_underlying(block_context.y_prediction_mode())];
  1196. }
  1197. DecoderErrorOr<bool> Parser::residual(BlockContext& block_context, bool has_block_above, bool has_block_left)
  1198. {
  1199. bool had_residual_tokens = false;
  1200. auto block_size = block_context.size < Block_8x8 ? Block_8x8 : block_context.size;
  1201. for (u8 plane = 0; plane < 3; plane++) {
  1202. auto tx_size = (plane > 0) ? get_uv_tx_size(block_context.frame_context.color_config.subsampling_x, block_context.frame_context.color_config.subsampling_y, block_context.tx_size, block_context.size) : block_context.tx_size;
  1203. auto step = 1 << tx_size;
  1204. auto plane_size = get_plane_block_size(block_context.frame_context.color_config.subsampling_x, block_context.frame_context.color_config.subsampling_y, block_size, plane);
  1205. auto num_4x4_w = num_4x4_blocks_wide_lookup[plane_size];
  1206. auto num_4x4_h = num_4x4_blocks_high_lookup[plane_size];
  1207. auto sub_x = (plane > 0) ? block_context.frame_context.color_config.subsampling_x : 0;
  1208. auto sub_y = (plane > 0) ? block_context.frame_context.color_config.subsampling_y : 0;
  1209. auto base_x = (block_context.column * 8) >> sub_x;
  1210. auto base_y = (block_context.row * 8) >> sub_y;
  1211. if (block_context.is_inter_predicted()) {
  1212. if (block_context.size < Block_8x8) {
  1213. for (auto y = 0; y < num_4x4_h; y++) {
  1214. for (auto x = 0; x < num_4x4_w; x++) {
  1215. TRY(m_decoder.predict_inter(plane, block_context, base_x + (4 * x), base_y + (4 * y), 4, 4, (y * num_4x4_w) + x));
  1216. }
  1217. }
  1218. } else {
  1219. TRY(m_decoder.predict_inter(plane, block_context, base_x, base_y, num_4x4_w * 4, num_4x4_h * 4, 0));
  1220. }
  1221. }
  1222. auto max_x = (block_context.frame_context.columns() * 8) >> sub_x;
  1223. auto max_y = (block_context.frame_context.rows() * 8) >> sub_y;
  1224. auto block_index = 0;
  1225. for (auto y = 0; y < num_4x4_h; y += step) {
  1226. for (auto x = 0; x < num_4x4_w; x += step) {
  1227. auto start_x = base_x + (4 * x);
  1228. auto start_y = base_y + (4 * y);
  1229. auto non_zero = false;
  1230. if (start_x < max_x && start_y < max_y) {
  1231. if (!block_context.is_inter_predicted())
  1232. TRY(m_decoder.predict_intra(plane, block_context, start_x, start_y, has_block_left || x > 0, has_block_above || y > 0, (x + step) < num_4x4_w, tx_size, block_index));
  1233. if (!block_context.should_skip_residuals) {
  1234. auto transform_type = select_transform_type(block_context, plane, tx_size, block_index);
  1235. non_zero = TRY(tokens(block_context, plane, start_x, start_y, tx_size, transform_type));
  1236. had_residual_tokens = had_residual_tokens || non_zero;
  1237. TRY(m_decoder.reconstruct(plane, block_context, start_x, start_y, tx_size, transform_type));
  1238. }
  1239. }
  1240. auto& above_sub_context = m_above_nonzero_context[plane];
  1241. auto above_sub_context_index = start_x >> 2;
  1242. auto above_sub_context_end = min(above_sub_context_index + step, above_sub_context.size());
  1243. for (; above_sub_context_index < above_sub_context_end; above_sub_context_index++)
  1244. above_sub_context[above_sub_context_index] = non_zero;
  1245. auto& left_sub_context = m_left_nonzero_context[plane];
  1246. auto left_sub_context_index = start_y >> 2;
  1247. auto left_sub_context_end = min(left_sub_context_index + step, left_sub_context.size());
  1248. for (; left_sub_context_index < left_sub_context_end; left_sub_context_index++)
  1249. left_sub_context[left_sub_context_index] = non_zero;
  1250. block_index++;
  1251. }
  1252. }
  1253. }
  1254. return had_residual_tokens;
  1255. }
  1256. static u32 const* get_scan(TXSize tx_size, u8 transform_type)
  1257. {
  1258. if (tx_size == TX_4x4) {
  1259. if (transform_type == ADST_DCT)
  1260. return row_scan_4x4;
  1261. if (transform_type == DCT_ADST)
  1262. return col_scan_4x4;
  1263. return default_scan_4x4;
  1264. }
  1265. if (tx_size == TX_8x8) {
  1266. if (transform_type == ADST_DCT)
  1267. return row_scan_8x8;
  1268. if (transform_type == DCT_ADST)
  1269. return col_scan_8x8;
  1270. return default_scan_8x8;
  1271. }
  1272. if (tx_size == TX_16x16) {
  1273. if (transform_type == ADST_DCT)
  1274. return row_scan_16x16;
  1275. if (transform_type == DCT_ADST)
  1276. return col_scan_16x16;
  1277. return default_scan_16x16;
  1278. }
  1279. return default_scan_32x32;
  1280. }
  1281. DecoderErrorOr<bool> Parser::tokens(BlockContext& block_context, size_t plane, u32 start_x, u32 start_y, TXSize tx_size, u8 transform_type)
  1282. {
  1283. u32 segment_eob = 16 << (tx_size << 1);
  1284. auto const* scan = get_scan(tx_size, transform_type);
  1285. auto check_eob = true;
  1286. u32 c = 0;
  1287. for (; c < segment_eob; c++) {
  1288. auto pos = scan[c];
  1289. auto band = (tx_size == TX_4x4) ? coefband_4x4[c] : coefband_8x8plus[c];
  1290. auto tokens_context = TreeParser::get_tokens_context(block_context.frame_context.color_config.subsampling_x, block_context.frame_context.color_config.subsampling_y, block_context.frame_context.rows(), block_context.frame_context.columns(), m_above_nonzero_context, m_left_nonzero_context, m_token_cache, tx_size, transform_type, plane, start_x, start_y, pos, block_context.is_inter_predicted(), band, c);
  1291. if (check_eob) {
  1292. auto more_coefs = TRY_READ(TreeParser::parse_more_coefficients(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, tokens_context));
  1293. if (!more_coefs)
  1294. break;
  1295. }
  1296. auto token = TRY_READ(TreeParser::parse_token(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, tokens_context));
  1297. m_token_cache[pos] = energy_class[token];
  1298. if (token == ZeroToken) {
  1299. m_tokens[pos] = 0;
  1300. check_eob = false;
  1301. } else {
  1302. i32 coef = TRY(read_coef(block_context.frame_context.color_config.bit_depth, token));
  1303. bool sign_bit = TRY_READ(m_bit_stream->read_literal(1));
  1304. m_tokens[pos] = sign_bit ? -coef : coef;
  1305. check_eob = true;
  1306. }
  1307. }
  1308. for (u32 i = c; i < segment_eob; i++)
  1309. m_tokens[scan[i]] = 0;
  1310. return c > 0;
  1311. }
  1312. DecoderErrorOr<i32> Parser::read_coef(u8 bit_depth, Token token)
  1313. {
  1314. auto cat = extra_bits[token][0];
  1315. auto num_extra = extra_bits[token][1];
  1316. u32 coef = extra_bits[token][2];
  1317. if (token == DctValCat6) {
  1318. for (size_t e = 0; e < (u8)(bit_depth - 8); e++) {
  1319. auto high_bit = TRY_READ(m_bit_stream->read_bool(255));
  1320. coef += high_bit << (5 + bit_depth - e);
  1321. }
  1322. }
  1323. for (size_t e = 0; e < num_extra; e++) {
  1324. auto coef_bit = TRY_READ(m_bit_stream->read_bool(cat_probs[cat][e]));
  1325. coef += coef_bit << (num_extra - 1 - e);
  1326. }
  1327. return coef;
  1328. }
  1329. // is_inside( candidateR, candidateC ) in the spec.
  1330. static bool motion_vector_is_inside_tile(TileContext const& tile_context, MotionVector vector)
  1331. {
  1332. if (vector.row() < 0)
  1333. return false;
  1334. if (vector.column() < 0)
  1335. return false;
  1336. u32 row_positive = vector.row();
  1337. u32 column_positive = vector.column();
  1338. return row_positive < tile_context.frame_context.rows() && column_positive >= tile_context.columns_start && column_positive < tile_context.columns_end;
  1339. }
  1340. // add_mv_ref_list( refList ) in the spec.
  1341. static void add_motion_vector_to_list_deduped(MotionVector const& vector, Vector<MotionVector, 2>& list)
  1342. {
  1343. if (list.size() >= 2)
  1344. return;
  1345. if (list.size() == 1 && vector == list[0])
  1346. return;
  1347. list.append(vector);
  1348. }
  1349. // get_block_mv( candidateR, candidateC, refList, usePrev ) in the spec.
  1350. MotionVectorCandidate Parser::get_motion_vector_from_current_or_previous_frame(BlockContext const& block_context, MotionVector candidate_vector, ReferenceIndex reference_index, bool use_prev)
  1351. {
  1352. if (use_prev) {
  1353. auto const& prev_context = m_previous_block_contexts.at(candidate_vector.row(), candidate_vector.column());
  1354. return { prev_context.ref_frames[reference_index], prev_context.primary_motion_vector_pair[reference_index] };
  1355. }
  1356. auto const& current_context = block_context.frame_block_contexts().at(candidate_vector.row(), candidate_vector.column());
  1357. return { current_context.ref_frames[reference_index], current_context.primary_motion_vector_pair()[reference_index] };
  1358. }
  1359. // if_same_ref_frame_add_mv( candidateR, candidateC, refFrame, usePrev ) in the spec.
  1360. void Parser::add_motion_vector_if_reference_frame_type_is_same(BlockContext const& block_context, MotionVector candidate_vector, ReferenceFrameType ref_frame, Vector<MotionVector, 2>& list, bool use_prev)
  1361. {
  1362. for (auto i = 0u; i < 2; i++) {
  1363. auto candidate = get_motion_vector_from_current_or_previous_frame(block_context, candidate_vector, static_cast<ReferenceIndex>(i), use_prev);
  1364. if (candidate.type == ref_frame) {
  1365. add_motion_vector_to_list_deduped(candidate.vector, list);
  1366. return;
  1367. }
  1368. }
  1369. }
  1370. // scale_mv( refList, refFrame ) in the spec.
  1371. static void apply_sign_bias_to_motion_vector(FrameContext const& frame_context, MotionVectorCandidate& candidate, ReferenceFrameType ref_frame)
  1372. {
  1373. if (frame_context.reference_frame_sign_biases[candidate.type] != frame_context.reference_frame_sign_biases[ref_frame])
  1374. candidate.vector *= -1;
  1375. }
  1376. // if_diff_ref_frame_add_mv( candidateR, candidateC, refFrame, usePrev ) in the spec.
  1377. void Parser::add_motion_vector_if_reference_frame_type_is_different(BlockContext const& block_context, MotionVector candidate_vector, ReferenceFrameType ref_frame, Vector<MotionVector, 2>& list, bool use_prev)
  1378. {
  1379. auto first_candidate = get_motion_vector_from_current_or_previous_frame(block_context, candidate_vector, ReferenceIndex::Primary, use_prev);
  1380. if (first_candidate.type > ReferenceFrameType::IntraFrame && first_candidate.type != ref_frame) {
  1381. apply_sign_bias_to_motion_vector(block_context.frame_context, first_candidate, ref_frame);
  1382. add_motion_vector_to_list_deduped(first_candidate.vector, list);
  1383. }
  1384. auto second_candidate = get_motion_vector_from_current_or_previous_frame(block_context, candidate_vector, ReferenceIndex::Secondary, use_prev);
  1385. auto mvs_are_same = first_candidate.vector == second_candidate.vector;
  1386. if (second_candidate.type > ReferenceFrameType::IntraFrame && second_candidate.type != ref_frame && !mvs_are_same) {
  1387. apply_sign_bias_to_motion_vector(block_context.frame_context, second_candidate, ref_frame);
  1388. add_motion_vector_to_list_deduped(second_candidate.vector, list);
  1389. }
  1390. }
  1391. // This function handles both clamp_mv_row( mvec, border ) and clamp_mv_col( mvec, border ) in the spec.
  1392. static MotionVector clamp_motion_vector(BlockContext const& block_context, MotionVector vector, i32 border)
  1393. {
  1394. i32 blocks_high = num_8x8_blocks_high_lookup[block_context.size];
  1395. // Casts must be done here to prevent subtraction underflow from wrapping the values.
  1396. i32 mb_to_top_edge = -8 * (static_cast<i32>(block_context.row) * MI_SIZE);
  1397. i32 mb_to_bottom_edge = 8 * ((static_cast<i32>(block_context.frame_context.rows()) - blocks_high - static_cast<i32>(block_context.row)) * MI_SIZE);
  1398. i32 blocks_wide = num_8x8_blocks_wide_lookup[block_context.size];
  1399. i32 mb_to_left_edge = -8 * (static_cast<i32>(block_context.column) * MI_SIZE);
  1400. i32 mb_to_right_edge = 8 * ((static_cast<i32>(block_context.frame_context.columns()) - blocks_wide - static_cast<i32>(block_context.column)) * MI_SIZE);
  1401. return {
  1402. clip_3(mb_to_top_edge - border, mb_to_bottom_edge + border, vector.row()),
  1403. clip_3(mb_to_left_edge - border, mb_to_right_edge + border, vector.column())
  1404. };
  1405. }
  1406. // 6.5.1 Find MV refs syntax
  1407. // find_mv_refs( refFrame, block ) in the spec.
  1408. MotionVectorPair Parser::find_reference_motion_vectors(BlockContext const& block_context, ReferenceFrameType reference_frame, i32 block)
  1409. {
  1410. bool different_ref_found = false;
  1411. u8 context_counter = 0;
  1412. Vector<MotionVector, 2> list;
  1413. MotionVector base_coordinates = MotionVector(block_context.row, block_context.column);
  1414. for (auto i = 0u; i < 2; i++) {
  1415. auto offset_vector = mv_ref_blocks[block_context.size][i];
  1416. auto candidate = base_coordinates + offset_vector;
  1417. if (motion_vector_is_inside_tile(block_context.tile_context, candidate)) {
  1418. different_ref_found = true;
  1419. auto context = block_context.frame_block_contexts().at(candidate.row(), candidate.column());
  1420. context_counter += mode_2_counter[to_underlying(context.y_mode)];
  1421. for (auto i = 0u; i < 2; i++) {
  1422. auto reference_index = static_cast<ReferenceIndex>(i);
  1423. if (context.ref_frames[reference_index] == reference_frame) {
  1424. // This section up until add_mv_ref_list() is defined in spec as get_sub_block_mv().
  1425. constexpr u8 idx_n_column_to_subblock[4][2] = {
  1426. { 1, 2 },
  1427. { 1, 3 },
  1428. { 3, 2 },
  1429. { 3, 3 }
  1430. };
  1431. auto index = block >= 0 ? idx_n_column_to_subblock[block][offset_vector.column() == 0] : 3;
  1432. add_motion_vector_to_list_deduped(context.sub_block_motion_vectors[index][reference_index], list);
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. for (auto i = 2u; i < MVREF_NEIGHBOURS; i++) {
  1439. MotionVector candidate = base_coordinates + mv_ref_blocks[block_context.size][i];
  1440. if (motion_vector_is_inside_tile(block_context.tile_context, candidate)) {
  1441. different_ref_found = true;
  1442. add_motion_vector_if_reference_frame_type_is_same(block_context, candidate, reference_frame, list, false);
  1443. }
  1444. }
  1445. if (m_use_prev_frame_mvs)
  1446. add_motion_vector_if_reference_frame_type_is_same(block_context, base_coordinates, reference_frame, list, true);
  1447. if (different_ref_found) {
  1448. for (auto i = 0u; i < MVREF_NEIGHBOURS; i++) {
  1449. MotionVector candidate = base_coordinates + mv_ref_blocks[block_context.size][i];
  1450. if (motion_vector_is_inside_tile(block_context.tile_context, candidate))
  1451. add_motion_vector_if_reference_frame_type_is_different(block_context, candidate, reference_frame, list, false);
  1452. }
  1453. }
  1454. if (m_use_prev_frame_mvs)
  1455. add_motion_vector_if_reference_frame_type_is_different(block_context, base_coordinates, reference_frame, list, true);
  1456. m_mode_context[reference_frame] = counter_to_context[context_counter];
  1457. for (auto i = 0u; i < list.size(); i++) {
  1458. // clamp_mv_ref( i ) in the spec.
  1459. list[i] = clamp_motion_vector(block_context, list[i], MV_BORDER);
  1460. }
  1461. MotionVectorPair result;
  1462. for (auto i = 0u; i < list.size(); i++)
  1463. result[static_cast<ReferenceIndex>(i)] = list[i];
  1464. return result;
  1465. }
  1466. // find_best_ref_mvs( refList ) in the spec.
  1467. static void select_best_reference_motion_vectors(BlockContext& block_context, MotionVectorPair reference_motion_vectors, BlockMotionVectorCandidates& candidates, ReferenceIndex reference_index)
  1468. {
  1469. auto adjust_and_clamp_vector = [&](MotionVector& vector) {
  1470. auto delta_row = vector.row();
  1471. auto delta_column = vector.column();
  1472. if (!block_context.frame_context.high_precision_motion_vectors_allowed || !should_use_high_precision_motion_vector(vector)) {
  1473. if ((delta_row & 1) != 0)
  1474. delta_row += delta_row > 0 ? -1 : 1;
  1475. if ((delta_column & 1) != 0)
  1476. delta_column += delta_column > 0 ? -1 : 1;
  1477. }
  1478. vector = { delta_row, delta_column };
  1479. vector = clamp_motion_vector(block_context, vector, (BORDERINPIXELS - INTERP_EXTEND) << 3);
  1480. };
  1481. adjust_and_clamp_vector(reference_motion_vectors.primary);
  1482. adjust_and_clamp_vector(reference_motion_vectors.secondary);
  1483. candidates[reference_index].nearest_vector = reference_motion_vectors.primary;
  1484. candidates[reference_index].near_vector = reference_motion_vectors.secondary;
  1485. candidates[reference_index].best_vector = reference_motion_vectors.primary;
  1486. }
  1487. // append_sub8x8_mvs( block, refList ) in the spec.
  1488. void Parser::select_best_sub_block_reference_motion_vectors(BlockContext const& block_context, BlockMotionVectorCandidates& candidates, i32 block, ReferenceIndex reference_index)
  1489. {
  1490. Array<MotionVector, 2> sub_8x8_mvs;
  1491. MotionVectorPair reference_motion_vectors = find_reference_motion_vectors(block_context, block_context.reference_frame_types[reference_index], block);
  1492. auto destination_index = 0;
  1493. if (block == 0) {
  1494. sub_8x8_mvs[destination_index++] = reference_motion_vectors.primary;
  1495. sub_8x8_mvs[destination_index++] = reference_motion_vectors.secondary;
  1496. } else if (block <= 2) {
  1497. sub_8x8_mvs[destination_index++] = block_context.sub_block_motion_vectors[0][reference_index];
  1498. } else {
  1499. sub_8x8_mvs[destination_index++] = block_context.sub_block_motion_vectors[2][reference_index];
  1500. for (auto index = 1; index >= 0 && destination_index < 2; index--) {
  1501. auto block_vector = block_context.sub_block_motion_vectors[index][reference_index];
  1502. if (block_vector != sub_8x8_mvs[0])
  1503. sub_8x8_mvs[destination_index++] = block_vector;
  1504. }
  1505. }
  1506. for (auto n = 0u; n < 2 && destination_index < 2; n++) {
  1507. auto ref_list_vector = reference_motion_vectors[static_cast<ReferenceIndex>(n)];
  1508. if (ref_list_vector != sub_8x8_mvs[0])
  1509. sub_8x8_mvs[destination_index++] = ref_list_vector;
  1510. }
  1511. if (destination_index < 2)
  1512. sub_8x8_mvs[destination_index++] = {};
  1513. candidates[reference_index].nearest_vector = sub_8x8_mvs[0];
  1514. candidates[reference_index].near_vector = sub_8x8_mvs[1];
  1515. }
  1516. }