Decoder.cpp 25 KB

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