Parser.cpp 79 KB

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