Decoder.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Decoder.h"
  7. namespace Video::VP9 {
  8. #define RESERVED_ZERO \
  9. if (m_bit_stream->read_bit() != 0) \
  10. return false
  11. Decoder::Decoder()
  12. {
  13. m_probability_tables = make<ProbabilityTables>();
  14. }
  15. bool Decoder::parse_frame(const ByteBuffer& frame_data)
  16. {
  17. m_bit_stream = make<BitStream>(frame_data.data(), frame_data.size());
  18. m_syntax_element_counter = make<SyntaxElementCounter>();
  19. if (!uncompressed_header())
  20. return false;
  21. dbgln("Finished reading uncompressed header");
  22. if (!trailing_bits())
  23. return false;
  24. if (m_header_size_in_bytes == 0) {
  25. // FIXME: Do we really need to read all of these bits?
  26. // while (m_bit_stream->get_position() < m_start_bit_pos + (8 * frame_data.size()))
  27. // RESERVED_ZERO;
  28. dbgln("No header");
  29. return true;
  30. }
  31. m_probability_tables->load_probs(m_frame_context_idx);
  32. m_probability_tables->load_probs2(m_frame_context_idx);
  33. m_syntax_element_counter->clear_counts();
  34. if (!m_bit_stream->init_bool(m_header_size_in_bytes))
  35. return false;
  36. dbgln("Reading compressed header");
  37. if (!compressed_header())
  38. return false;
  39. dbgln("Finished reading compressed header");
  40. if (!m_bit_stream->exit_bool())
  41. return false;
  42. dbgln("Finished reading frame!");
  43. decode_tiles();
  44. return true;
  45. }
  46. bool Decoder::uncompressed_header()
  47. {
  48. auto frame_marker = m_bit_stream->read_f(2);
  49. if (frame_marker != 2)
  50. return false;
  51. auto profile_low_bit = m_bit_stream->read_bit();
  52. auto profile_high_bit = m_bit_stream->read_bit();
  53. m_profile = (profile_high_bit << 1u) + profile_low_bit;
  54. if (m_profile == 3)
  55. RESERVED_ZERO;
  56. auto show_existing_frame = m_bit_stream->read_bit();
  57. if (show_existing_frame) {
  58. m_frame_to_show_map_index = m_bit_stream->read_f(3);
  59. m_header_size_in_bytes = 0;
  60. m_refresh_frame_flags = 0;
  61. m_loop_filter_level = 0;
  62. return true;
  63. }
  64. m_last_frame_type = m_frame_type;
  65. m_frame_type = read_frame_type();
  66. m_show_frame = m_bit_stream->read_bit();
  67. m_error_resilient_mode = m_bit_stream->read_bit();
  68. if (m_frame_type == KeyFrame) {
  69. if (!frame_sync_code())
  70. return false;
  71. if (!color_config())
  72. return false;
  73. if (!frame_size())
  74. return false;
  75. if (!render_size())
  76. return false;
  77. m_refresh_frame_flags = 0xFF;
  78. m_frame_is_intra = true;
  79. } else {
  80. m_frame_is_intra = !m_show_frame && m_bit_stream->read_bit();
  81. if (!m_error_resilient_mode) {
  82. m_reset_frame_context = m_bit_stream->read_f(2);
  83. } else {
  84. m_reset_frame_context = 0;
  85. }
  86. if (m_frame_is_intra) {
  87. if (!frame_sync_code())
  88. return false;
  89. if (m_profile > 0) {
  90. if (!color_config())
  91. return false;
  92. } else {
  93. m_color_space = Bt601;
  94. m_subsampling_x = true;
  95. m_subsampling_y = true;
  96. m_bit_depth = 8;
  97. }
  98. m_refresh_frame_flags = m_bit_stream->read_f8();
  99. if (!frame_size())
  100. return false;
  101. if (!render_size())
  102. return false;
  103. } else {
  104. m_refresh_frame_flags = m_bit_stream->read_f8();
  105. for (auto i = 0; i < 3; i++) {
  106. m_ref_frame_idx[i] = m_bit_stream->read_f(3);
  107. m_ref_frame_sign_bias[LastFrame + i] = m_bit_stream->read_bit();
  108. }
  109. frame_size_with_refs();
  110. m_allow_high_precision_mv = m_bit_stream->read_bit();
  111. read_interpolation_filter();
  112. }
  113. }
  114. if (!m_error_resilient_mode) {
  115. m_refresh_frame_context = m_bit_stream->read_bit();
  116. m_frame_parallel_decoding_mode = m_bit_stream->read_bit();
  117. } else {
  118. m_refresh_frame_context = false;
  119. m_frame_parallel_decoding_mode = true;
  120. }
  121. m_frame_context_idx = m_bit_stream->read_f(2);
  122. if (m_frame_is_intra || m_error_resilient_mode) {
  123. setup_past_independence();
  124. if (m_frame_type == KeyFrame || m_error_resilient_mode || m_reset_frame_context == 3) {
  125. for (auto i = 0; i < 4; i++) {
  126. m_probability_tables->save_probs(i);
  127. }
  128. } else if (m_reset_frame_context == 2) {
  129. m_probability_tables->save_probs(m_frame_context_idx);
  130. }
  131. m_frame_context_idx = 0;
  132. }
  133. loop_filter_params();
  134. quantization_params();
  135. segmentation_params();
  136. tile_info();
  137. m_header_size_in_bytes = m_bit_stream->read_f16();
  138. return true;
  139. }
  140. bool Decoder::frame_sync_code()
  141. {
  142. if (m_bit_stream->read_byte() != 0x49)
  143. return false;
  144. if (m_bit_stream->read_byte() != 0x83)
  145. return false;
  146. return m_bit_stream->read_byte() == 0x42;
  147. }
  148. bool Decoder::color_config()
  149. {
  150. if (m_profile >= 2) {
  151. m_bit_depth = m_bit_stream->read_bit() ? 12 : 10;
  152. } else {
  153. m_bit_depth = 8;
  154. }
  155. auto color_space = m_bit_stream->read_f(3);
  156. if (color_space > RGB)
  157. return false;
  158. m_color_space = static_cast<ColorSpace>(color_space);
  159. if (color_space != RGB) {
  160. m_color_range = read_color_range();
  161. if (m_profile == 1 || m_profile == 3) {
  162. m_subsampling_x = m_bit_stream->read_bit();
  163. m_subsampling_y = m_bit_stream->read_bit();
  164. RESERVED_ZERO;
  165. } else {
  166. m_subsampling_x = true;
  167. m_subsampling_y = true;
  168. }
  169. } else {
  170. m_color_range = FullSwing;
  171. if (m_profile == 1 || m_profile == 3) {
  172. m_subsampling_x = false;
  173. m_subsampling_y = false;
  174. RESERVED_ZERO;
  175. }
  176. }
  177. return true;
  178. }
  179. bool Decoder::frame_size()
  180. {
  181. m_frame_width = m_bit_stream->read_f16() + 1;
  182. m_frame_height = m_bit_stream->read_f16() + 1;
  183. compute_image_size();
  184. return true;
  185. }
  186. bool Decoder::render_size()
  187. {
  188. if (m_bit_stream->read_bit()) {
  189. m_render_width = m_bit_stream->read_f16() + 1;
  190. m_render_height = m_bit_stream->read_f16() + 1;
  191. } else {
  192. m_render_width = m_frame_width;
  193. m_render_height = m_frame_height;
  194. }
  195. return true;
  196. }
  197. bool Decoder::frame_size_with_refs()
  198. {
  199. bool found_ref;
  200. for (auto i = 0; i < 3; i++) {
  201. found_ref = m_bit_stream->read_bit();
  202. if (found_ref) {
  203. // TODO:
  204. // - FrameWidth = RefFrameWidth[ref_frame_idx[ i] ];
  205. // - FrameHeight = RefFrameHeight[ref_frame_idx[ i] ];
  206. break;
  207. }
  208. }
  209. if (!found_ref)
  210. frame_size();
  211. else
  212. compute_image_size();
  213. render_size();
  214. return true;
  215. }
  216. bool Decoder::compute_image_size()
  217. {
  218. m_mi_cols = (m_frame_width + 7u) >> 3u;
  219. m_mi_rows = (m_frame_height + 7u) >> 3u;
  220. m_sb64_cols = (m_mi_cols + 7u) >> 3u;
  221. m_sb64_rows = (m_mi_rows + 7u) >> 3u;
  222. return true;
  223. }
  224. bool Decoder::read_interpolation_filter()
  225. {
  226. if (m_bit_stream->read_bit()) {
  227. m_interpolation_filter = Switchable;
  228. } else {
  229. m_interpolation_filter = literal_to_type[m_bit_stream->read_f(2)];
  230. }
  231. return true;
  232. }
  233. bool Decoder::loop_filter_params()
  234. {
  235. m_loop_filter_level = m_bit_stream->read_f(6);
  236. m_loop_filter_sharpness = m_bit_stream->read_f(3);
  237. m_loop_filter_delta_enabled = m_bit_stream->read_bit();
  238. if (m_loop_filter_delta_enabled) {
  239. if (m_bit_stream->read_bit()) {
  240. for (auto i = 0; i < 4; i++) {
  241. if (m_bit_stream->read_bit()) {
  242. // TODO: loop_filter_ref_deltas[i] = s(6);
  243. }
  244. }
  245. for (auto i = 0; i < 2; i++) {
  246. if (m_bit_stream->read_bit()) {
  247. // TODO: loop_filter_mode_deltas[i] = s(6);
  248. }
  249. }
  250. }
  251. }
  252. return true;
  253. }
  254. bool Decoder::quantization_params()
  255. {
  256. auto base_q_idx = m_bit_stream->read_byte();
  257. auto delta_q_y_dc = read_delta_q();
  258. auto delta_q_uv_dc = read_delta_q();
  259. auto delta_q_uv_ac = read_delta_q();
  260. m_lossless = base_q_idx == 0 && delta_q_y_dc == 0 && delta_q_uv_dc == 0 && delta_q_uv_ac == 0;
  261. return true;
  262. }
  263. i8 Decoder::read_delta_q()
  264. {
  265. if (m_bit_stream->read_bit())
  266. return m_bit_stream->read_s(4);
  267. return 0;
  268. }
  269. bool Decoder::segmentation_params()
  270. {
  271. auto segmentation_enabled = m_bit_stream->read_bit();
  272. if (!segmentation_enabled)
  273. return true;
  274. auto segmentation_update_map = m_bit_stream->read_bit();
  275. if (segmentation_update_map) {
  276. for (auto i = 0; i < 7; i++) {
  277. m_segmentation_tree_probs[i] = read_prob();
  278. }
  279. auto segmentation_temporal_update = m_bit_stream->read_bit();
  280. for (auto i = 0; i < 3; i++) {
  281. m_segmentation_pred_prob[i] = segmentation_temporal_update ? read_prob() : 255;
  282. }
  283. }
  284. if (!m_bit_stream->read_bit())
  285. return true;
  286. m_segmentation_abs_or_delta_update = m_bit_stream->read_bit();
  287. for (auto i = 0; i < MAX_SEGMENTS; i++) {
  288. for (auto j = 0; j < SEG_LVL_MAX; j++) {
  289. auto feature_value = 0;
  290. auto feature_enabled = m_bit_stream->read_bit();
  291. m_feature_enabled[i][j] = feature_enabled;
  292. if (feature_enabled) {
  293. auto bits_to_read = segmentation_feature_bits[j];
  294. feature_value = m_bit_stream->read_f(bits_to_read);
  295. if (segmentation_feature_signed[j]) {
  296. if (m_bit_stream->read_bit())
  297. feature_value = -feature_value;
  298. }
  299. }
  300. m_feature_data[i][j] = feature_value;
  301. }
  302. }
  303. return true;
  304. }
  305. u8 Decoder::read_prob()
  306. {
  307. if (m_bit_stream->read_bit())
  308. return m_bit_stream->read_byte();
  309. return 255;
  310. }
  311. bool Decoder::tile_info()
  312. {
  313. auto min_log2_tile_cols = calc_min_log2_tile_cols();
  314. auto max_log2_tile_cols = calc_max_log2_tile_cols();
  315. m_tile_cols_log2 = min_log2_tile_cols;
  316. while (m_tile_cols_log2 < max_log2_tile_cols) {
  317. if (m_bit_stream->read_bit())
  318. m_tile_cols_log2++;
  319. else
  320. break;
  321. }
  322. m_tile_rows_log2 = m_bit_stream->read_bit();
  323. if (m_tile_rows_log2) {
  324. m_tile_rows_log2 += m_bit_stream->read_bit();
  325. }
  326. return true;
  327. }
  328. u16 Decoder::calc_min_log2_tile_cols()
  329. {
  330. auto min_log_2 = 0u;
  331. while ((u8)(MAX_TILE_WIDTH_B64 << min_log_2) < m_sb64_cols)
  332. min_log_2++;
  333. return min_log_2;
  334. }
  335. u16 Decoder::calc_max_log2_tile_cols()
  336. {
  337. u16 max_log_2 = 1;
  338. while ((m_sb64_cols >> max_log_2) >= MIN_TILE_WIDTH_B64)
  339. max_log_2++;
  340. return max_log_2 - 1;
  341. }
  342. bool Decoder::setup_past_independence()
  343. {
  344. for (auto i = 0; i < 8; i++) {
  345. for (auto j = 0; j < 4; j++) {
  346. m_feature_data[i][j] = 0;
  347. m_feature_enabled[i][j] = false;
  348. }
  349. }
  350. m_segmentation_abs_or_delta_update = false;
  351. for (auto row = 0u; row < m_mi_rows; row++) {
  352. for (auto col = 0u; col < m_mi_cols; col++) {
  353. // TODO: m_prev_segment_ids[row][col] = 0;
  354. }
  355. }
  356. m_loop_filter_delta_enabled = true;
  357. m_loop_filter_ref_deltas[IntraFrame] = 1;
  358. m_loop_filter_ref_deltas[LastFrame] = 0;
  359. m_loop_filter_ref_deltas[GoldenFrame] = -1;
  360. m_loop_filter_ref_deltas[AltRefFrame] = -1;
  361. for (auto i = 0; i < 2; i++) {
  362. m_loop_filter_mode_deltas[i] = 0;
  363. }
  364. m_probability_tables->reset_probs();
  365. return true;
  366. }
  367. bool Decoder::trailing_bits()
  368. {
  369. while (m_bit_stream->get_position() & 7u)
  370. RESERVED_ZERO;
  371. return true;
  372. }
  373. bool Decoder::compressed_header()
  374. {
  375. read_tx_mode();
  376. if (m_tx_mode == TXModeSelect) {
  377. tx_mode_probs();
  378. }
  379. read_coef_probs();
  380. read_skip_prob();
  381. if (!m_frame_is_intra) {
  382. read_inter_mode_probs();
  383. if (m_interpolation_filter == Switchable) {
  384. read_interp_filter_probs();
  385. }
  386. read_is_inter_probs();
  387. frame_reference_mode();
  388. frame_reference_mode_probs();
  389. read_y_mode_probs();
  390. read_partition_probs();
  391. mv_probs();
  392. }
  393. return true;
  394. }
  395. bool Decoder::read_tx_mode()
  396. {
  397. if (m_lossless) {
  398. m_tx_mode = Only4x4;
  399. } else {
  400. auto tx_mode = m_bit_stream->read_literal(2);
  401. if (tx_mode == Allow32x32) {
  402. tx_mode += m_bit_stream->read_literal(1);
  403. }
  404. m_tx_mode = static_cast<TXMode>(tx_mode);
  405. }
  406. return true;
  407. }
  408. bool Decoder::tx_mode_probs()
  409. {
  410. auto& tx_probs = m_probability_tables->tx_probs();
  411. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  412. for (auto j = 0; j < TX_SIZES - 3; j++) {
  413. tx_probs[TX8x8][i][j] = diff_update_prob(tx_probs[TX8x8][i][j]);
  414. }
  415. }
  416. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  417. for (auto j = 0; j < TX_SIZES - 2; j++) {
  418. tx_probs[TX16x16][i][j] = diff_update_prob(tx_probs[TX16x16][i][j]);
  419. }
  420. }
  421. for (auto i = 0; i < TX_SIZE_CONTEXTS; i++) {
  422. for (auto j = 0; j < TX_SIZES - 1; j++) {
  423. tx_probs[TX32x32][i][j] = diff_update_prob(tx_probs[TX32x32][i][j]);
  424. }
  425. }
  426. return true;
  427. }
  428. u8 Decoder::diff_update_prob(u8 prob)
  429. {
  430. if (m_bit_stream->read_bool(252)) {
  431. auto delta_prob = decode_term_subexp();
  432. prob = inv_remap_prob(delta_prob, prob);
  433. }
  434. return prob;
  435. }
  436. u8 Decoder::decode_term_subexp()
  437. {
  438. if (m_bit_stream->read_literal(1) == 0)
  439. return m_bit_stream->read_literal(4);
  440. if (m_bit_stream->read_literal(1) == 0)
  441. return m_bit_stream->read_literal(4) + 16;
  442. if (m_bit_stream->read_literal(1) == 0)
  443. return m_bit_stream->read_literal(4) + 32;
  444. auto v = m_bit_stream->read_literal(7);
  445. if (v < 65)
  446. return v + 64;
  447. return (v << 1u) - 1 + m_bit_stream->read_literal(1);
  448. }
  449. u8 Decoder::inv_remap_prob(u8 delta_prob, u8 prob)
  450. {
  451. u8 m = prob - 1;
  452. auto v = inv_map_table[delta_prob];
  453. if ((m << 1u) <= 255)
  454. return 1 + inv_recenter_nonneg(v, m);
  455. return 255 - inv_recenter_nonneg(v, 254 - m);
  456. }
  457. u8 Decoder::inv_recenter_nonneg(u8 v, u8 m)
  458. {
  459. if (v > 2 * m)
  460. return v;
  461. if (v & 1u)
  462. return m - ((v + 1u) >> 1u);
  463. return m + (v >> 1u);
  464. }
  465. bool Decoder::read_coef_probs()
  466. {
  467. auto max_tx_size = tx_mode_to_biggest_tx_size[m_tx_mode];
  468. for (auto tx_size = TX4x4; tx_size <= max_tx_size; tx_size = static_cast<TXSize>(static_cast<int>(tx_size) + 1)) {
  469. auto update_probs = m_bit_stream->read_literal(1);
  470. if (update_probs == 1) {
  471. for (auto i = 0; i < 2; i++) {
  472. for (auto j = 0; j < 2; j++) {
  473. for (auto k = 0; k < 6; k++) {
  474. auto max_l = (k == 0) ? 3 : 6;
  475. for (auto l = 0; l < max_l; l++) {
  476. for (auto m = 0; m < 3; m++) {
  477. auto& coef_probs = m_probability_tables->coef_probs()[tx_size];
  478. coef_probs[i][j][k][l][m] = diff_update_prob(coef_probs[i][j][k][l][m]);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. return true;
  487. }
  488. bool Decoder::read_skip_prob()
  489. {
  490. for (auto i = 0; i < SKIP_CONTEXTS; i++)
  491. m_probability_tables->skip_prob()[i] = diff_update_prob(m_probability_tables->skip_prob()[i]);
  492. return true;
  493. }
  494. bool Decoder::read_inter_mode_probs()
  495. {
  496. for (auto i = 0; i < INTER_MODE_CONTEXTS; i++) {
  497. for (auto j = 0; j < INTER_MODES - 1; j++)
  498. m_probability_tables->inter_mode_probs()[i][j] = diff_update_prob(m_probability_tables->inter_mode_probs()[i][j]);
  499. }
  500. return true;
  501. }
  502. bool Decoder::read_interp_filter_probs()
  503. {
  504. for (auto i = 0; i < INTERP_FILTER_CONTEXTS; i++) {
  505. for (auto j = 0; j < SWITCHABLE_FILTERS - 1; j++)
  506. m_probability_tables->interp_filter_probs()[i][j] = diff_update_prob(m_probability_tables->interp_filter_probs()[i][j]);
  507. }
  508. return true;
  509. }
  510. bool Decoder::read_is_inter_probs()
  511. {
  512. for (auto i = 0; i < IS_INTER_CONTEXTS; i++)
  513. m_probability_tables->is_inter_prob()[i] = diff_update_prob(m_probability_tables->is_inter_prob()[i]);
  514. return true;
  515. }
  516. bool Decoder::frame_reference_mode()
  517. {
  518. auto compound_reference_allowed = false;
  519. for (size_t i = 2; i <= REFS_PER_FRAME; i++) {
  520. if (m_ref_frame_sign_bias[i] != m_ref_frame_sign_bias[1])
  521. compound_reference_allowed = true;
  522. }
  523. if (compound_reference_allowed) {
  524. auto non_single_reference = m_bit_stream->read_literal(1);
  525. if (non_single_reference == 0) {
  526. m_reference_mode = SingleReference;
  527. } else {
  528. auto reference_select = m_bit_stream->read_literal(1);
  529. if (reference_select == 0)
  530. m_reference_mode = CompoundReference;
  531. else
  532. m_reference_mode = ReferenceModeSelect;
  533. setup_compound_reference_mode();
  534. }
  535. } else {
  536. m_reference_mode = SingleReference;
  537. }
  538. return true;
  539. }
  540. bool Decoder::frame_reference_mode_probs()
  541. {
  542. if (m_reference_mode == ReferenceModeSelect) {
  543. for (auto i = 0; i < COMP_MODE_CONTEXTS; i++) {
  544. auto& comp_mode_prob = m_probability_tables->comp_mode_prob();
  545. comp_mode_prob[i] = diff_update_prob(comp_mode_prob[i]);
  546. }
  547. }
  548. if (m_reference_mode != CompoundReference) {
  549. for (auto i = 0; i < REF_CONTEXTS; i++) {
  550. auto& single_ref_prob = m_probability_tables->single_ref_prob();
  551. single_ref_prob[i][0] = diff_update_prob(single_ref_prob[i][0]);
  552. single_ref_prob[i][1] = diff_update_prob(single_ref_prob[i][1]);
  553. }
  554. }
  555. if (m_reference_mode != SingleReference) {
  556. for (auto i = 0; i < REF_CONTEXTS; i++) {
  557. auto& comp_ref_prob = m_probability_tables->comp_ref_prob();
  558. comp_ref_prob[i] = diff_update_prob(comp_ref_prob[i]);
  559. }
  560. }
  561. return true;
  562. }
  563. bool Decoder::read_y_mode_probs()
  564. {
  565. for (auto i = 0; i < BLOCK_SIZE_GROUPS; i++) {
  566. for (auto j = 0; j < INTRA_MODES - 1; j++) {
  567. auto& y_mode_probs = m_probability_tables->y_mode_probs();
  568. y_mode_probs[i][j] = diff_update_prob(y_mode_probs[i][j]);
  569. }
  570. }
  571. return true;
  572. }
  573. bool Decoder::read_partition_probs()
  574. {
  575. for (auto i = 0; i < PARTITION_CONTEXTS; i++) {
  576. for (auto j = 0; j < PARTITION_TYPES - 1; j++) {
  577. auto& partition_probs = m_probability_tables->partition_probs();
  578. partition_probs[i][j] = diff_update_prob(partition_probs[i][j]);
  579. }
  580. }
  581. return true;
  582. }
  583. bool Decoder::mv_probs()
  584. {
  585. for (auto j = 0; j < MV_JOINTS - 1; j++) {
  586. auto& mv_joint_probs = m_probability_tables->mv_joint_probs();
  587. mv_joint_probs[j] = update_mv_prob(mv_joint_probs[j]);
  588. }
  589. for (auto i = 0; i < 2; i++) {
  590. auto& mv_sign_prob = m_probability_tables->mv_sign_prob();
  591. mv_sign_prob[i] = update_mv_prob(mv_sign_prob[i]);
  592. for (auto j = 0; j < MV_CLASSES - 1; j++) {
  593. auto& mv_class_probs = m_probability_tables->mv_class_probs();
  594. mv_class_probs[i][j] = update_mv_prob(mv_class_probs[i][j]);
  595. }
  596. auto& mv_class0_bit_prob = m_probability_tables->mv_class0_bit_prob();
  597. mv_class0_bit_prob[i] = update_mv_prob(mv_class0_bit_prob[i]);
  598. for (auto j = 0; j < MV_OFFSET_BITS; j++) {
  599. auto& mv_bits_prob = m_probability_tables->mv_bits_prob();
  600. mv_bits_prob[i][j] = update_mv_prob(mv_bits_prob[i][j]);
  601. }
  602. }
  603. for (auto i = 0; i < 2; i++) {
  604. for (auto j = 0; j < CLASS0_SIZE; j++) {
  605. for (auto k = 0; k < MV_FR_SIZE - 1; k++) {
  606. auto& mv_class0_fr_probs = m_probability_tables->mv_class0_fr_probs();
  607. mv_class0_fr_probs[i][j][k] = update_mv_prob(mv_class0_fr_probs[i][j][k]);
  608. }
  609. }
  610. for (auto k = 0; k < MV_FR_SIZE - 1; k++) {
  611. auto& mv_fr_probs = m_probability_tables->mv_fr_probs();
  612. mv_fr_probs[i][k] = update_mv_prob(mv_fr_probs[i][k]);
  613. }
  614. }
  615. if (m_allow_high_precision_mv) {
  616. for (auto i = 0; i < 2; i++) {
  617. auto& mv_class0_hp_prob = m_probability_tables->mv_class0_hp_prob();
  618. auto& mv_hp_prob = m_probability_tables->mv_hp_prob();
  619. mv_class0_hp_prob[i] = update_mv_prob(mv_class0_hp_prob[i]);
  620. mv_hp_prob[i] = update_mv_prob(mv_hp_prob[i]);
  621. }
  622. }
  623. return true;
  624. }
  625. u8 Decoder::update_mv_prob(u8 prob)
  626. {
  627. if (m_bit_stream->read_bool(252)) {
  628. return (m_bit_stream->read_literal(7) << 1u) | 1u;
  629. }
  630. return prob;
  631. }
  632. bool Decoder::setup_compound_reference_mode()
  633. {
  634. if (m_ref_frame_sign_bias[LastFrame] == m_ref_frame_sign_bias[GoldenFrame]) {
  635. m_comp_fixed_ref = AltRefFrame;
  636. m_comp_var_ref[0] = LastFrame;
  637. m_comp_var_ref[1] = GoldenFrame;
  638. } else if (m_ref_frame_sign_bias[LastFrame] == m_ref_frame_sign_bias[AltRefFrame]) {
  639. m_comp_fixed_ref = GoldenFrame;
  640. m_comp_var_ref[0] = LastFrame;
  641. m_comp_var_ref[1] = AltRefFrame;
  642. } else {
  643. m_comp_fixed_ref = LastFrame;
  644. m_comp_var_ref[0] = GoldenFrame;
  645. m_comp_var_ref[1] = AltRefFrame;
  646. }
  647. return true;
  648. }
  649. bool Decoder::decode_tiles()
  650. {
  651. auto tile_cols = 1 << m_tile_cols_log2;
  652. auto tile_rows = 1 << m_tile_rows_log2;
  653. if (!clear_above_context())
  654. return false;
  655. for (auto tile_row = 0; tile_row < tile_rows; tile_row++) {
  656. for (auto tile_col = 0; tile_col < tile_cols; tile_col++) {
  657. auto last_tile = (tile_row == tile_rows - 1) && (tile_col == tile_cols - 1);
  658. // FIXME: Spec has `sz -= tile_size + 4`, but I think we don't need this because our bit stream manages how much data we have left?
  659. auto tile_size = last_tile ? m_bit_stream->bytes_remaining() : m_bit_stream->read_f(32);
  660. m_mi_row_start = get_tile_offset(tile_row, m_mi_rows, m_tile_rows_log2);
  661. m_mi_row_end = get_tile_offset(tile_row + 1, m_mi_rows, m_tile_rows_log2);
  662. m_mi_col_start = get_tile_offset(tile_col, m_mi_cols, m_tile_cols_log2);
  663. m_mi_col_end = get_tile_offset(tile_col + 1, m_mi_cols, m_tile_cols_log2);
  664. m_bit_stream->init_bool(tile_size);
  665. decode_tile();
  666. m_bit_stream->exit_bool();
  667. }
  668. }
  669. return true;
  670. }
  671. bool Decoder::clear_above_context()
  672. {
  673. // FIXME
  674. // When this function is invoked the arrays AboveNonzeroContext, AbovePartitionContext, AboveSegPredContext should be set equal to 0.
  675. // AboveNonzeroContext[0..2][0..MiCols*2-1] = 0
  676. // AboveSegPredContext[0..MiCols-1] = 0
  677. // AbovePartitionContext[0..Sb64Cols*8-1] = 0
  678. return true;
  679. }
  680. u32 Decoder::get_tile_offset(u32 tile_num, u32 mis, u32 tile_size_log2)
  681. {
  682. u32 super_blocks = (mis + 7) >> 3u;
  683. u32 offset = ((tile_num * super_blocks) >> tile_size_log2) << 3u;
  684. return min(offset, mis);
  685. }
  686. bool Decoder::decode_tile()
  687. {
  688. for (auto row = m_mi_row_start; row < m_mi_row_end; row += 8) {
  689. if (!clear_left_context())
  690. return false;
  691. for (auto col = m_mi_col_start; col < m_mi_col_end; col += 8) {
  692. if (!decode_partition(row, col, Block_64x64))
  693. return false;
  694. }
  695. }
  696. return true;
  697. }
  698. bool Decoder::clear_left_context()
  699. {
  700. // FIXME
  701. // When this function is invoked the arrays LeftNonzeroContext, LeftPartitionContext, LeftSegPredContext should be set equal to 0.
  702. // LeftNonzeroContext[0..2][0..MiRows*2-1] = 0
  703. // LeftSegPredContext[0..MiRows-1] = 0
  704. // LeftPartitionContext[0..Sb64Rows*8-1] = 0
  705. return true;
  706. }
  707. bool Decoder::decode_partition(u32 row, u32 col, u8 block_subsize)
  708. {
  709. if (row >= m_mi_rows || col >= m_mi_cols)
  710. return false;
  711. auto num_8x8 = num_8x8_blocks_wide_lookup[block_subsize];
  712. auto half_block_8x8 = num_8x8 >> 1;
  713. auto has_rows = (row + half_block_8x8) < m_mi_rows;
  714. auto has_cols = (col + half_block_8x8) < m_mi_cols;
  715. // FIXME: Parse partition (type: T) as specified by spec in section 9.3
  716. (void)has_rows;
  717. (void)has_cols;
  718. return true;
  719. }
  720. void Decoder::dump_info()
  721. {
  722. dbgln("Frame dimensions: {}x{}", m_frame_width, m_frame_height);
  723. dbgln("Render dimensions: {}x{}", m_render_width, m_render_height);
  724. dbgln("Bit depth: {}", m_bit_depth);
  725. dbgln("Interpolation filter: {}", (u8)m_interpolation_filter);
  726. }
  727. }