Decoder.cpp 31 KB

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