QMArithmeticDecoder.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Error.h>
  7. #include <LibGfx/ImageFormats/QMArithmeticDecoder.h>
  8. namespace Gfx {
  9. // Table E.1 – Qe values and probability estimation process
  10. // See also E.1.2 Coding conventions and approximations
  11. // and E.2.5 Probability estimation.
  12. struct QeEntry {
  13. u16 qe; // Sub-interval for the less probable symbol.
  14. u16 nmps; // Next index if the more probable symbol is decoded
  15. u16 nlps; // Next index if the less probable symbol is decoded
  16. u16 switch_flag; // See second-to-last paragraph in E.1.2.
  17. };
  18. constexpr auto qe_table = to_array<QeEntry>({
  19. { 0x5601, 1, 1, 1 },
  20. { 0x3401, 2, 6, 0 },
  21. { 0x1801, 3, 9, 0 },
  22. { 0x0AC1, 4, 12, 0 },
  23. { 0x0521, 5, 29, 0 },
  24. { 0x0221, 38, 33, 0 },
  25. { 0x5601, 7, 6, 1 },
  26. { 0x5401, 8, 14, 0 },
  27. { 0x4801, 9, 14, 0 },
  28. { 0x3801, 10, 14, 0 },
  29. { 0x3001, 11, 17, 0 },
  30. { 0x2401, 12, 18, 0 },
  31. { 0x1C01, 13, 20, 0 },
  32. { 0x1601, 29, 21, 0 },
  33. { 0x5601, 15, 14, 1 },
  34. { 0x5401, 16, 14, 0 },
  35. { 0x5101, 17, 15, 0 },
  36. { 0x4801, 18, 16, 0 },
  37. { 0x3801, 19, 17, 0 },
  38. { 0x3401, 20, 18, 0 },
  39. { 0x3001, 21, 19, 0 },
  40. { 0x2801, 22, 19, 0 },
  41. { 0x2401, 23, 20, 0 },
  42. { 0x2201, 24, 21, 0 },
  43. { 0x1C01, 25, 22, 0 },
  44. { 0x1801, 26, 23, 0 },
  45. { 0x1601, 27, 24, 0 },
  46. { 0x1401, 28, 25, 0 },
  47. { 0x1201, 29, 26, 0 },
  48. { 0x1101, 30, 27, 0 },
  49. { 0x0AC1, 31, 28, 0 },
  50. { 0x09C1, 32, 29, 0 },
  51. { 0x08A1, 33, 30, 0 },
  52. { 0x0521, 34, 31, 0 },
  53. { 0x0441, 35, 32, 0 },
  54. { 0x02A1, 36, 33, 0 },
  55. { 0x0221, 37, 34, 0 },
  56. { 0x0141, 38, 35, 0 },
  57. { 0x0111, 39, 36, 0 },
  58. { 0x0085, 40, 37, 0 },
  59. { 0x0049, 41, 38, 0 },
  60. { 0x0025, 42, 39, 0 },
  61. { 0x0015, 43, 40, 0 },
  62. { 0x0009, 44, 41, 0 },
  63. { 0x0005, 45, 42, 0 },
  64. { 0x0001, 45, 43, 0 },
  65. { 0x5601, 46, 46, 0 },
  66. });
  67. ErrorOr<QMArithmeticDecoder> QMArithmeticDecoder::initialize(ReadonlyBytes data)
  68. {
  69. QMArithmeticDecoder decoder { data };
  70. decoder.INITDEC();
  71. return decoder;
  72. }
  73. bool QMArithmeticDecoder::get_next_bit(Context& context)
  74. {
  75. CX = &context;
  76. // Useful for comparing to Table H.1 – Encoder and decoder trace data.
  77. // dbg("I={} MPS={} A={:#x} C={:#x} CT={} B={:#x}", I(CX), MPS(CX), A, C, CT, B());
  78. u8 D = DECODE();
  79. // dbgln(" -> D={}", D);
  80. return D;
  81. }
  82. u16 QMArithmeticDecoder::Qe(u16 index) { return qe_table[index].qe; }
  83. u8 QMArithmeticDecoder::NMPS(u16 index) { return qe_table[index].nmps; }
  84. u8 QMArithmeticDecoder::NLPS(u16 index) { return qe_table[index].nlps; }
  85. u8 QMArithmeticDecoder::SWITCH(u16 index) { return qe_table[index].switch_flag; }
  86. u8 QMArithmeticDecoder::B(size_t offset) const
  87. {
  88. // E.2.10 Minimization of the compressed data
  89. // "the convention is used in the decoder that when a marker code is encountered,
  90. // 1-bits (without bit stuffing) are supplied to the decoder until the coding interval is complete."
  91. if (BP + offset >= m_data.size())
  92. return 0xFF;
  93. return m_data[BP + offset];
  94. }
  95. void QMArithmeticDecoder::INITDEC()
  96. {
  97. // E.3.5 Initialization of the decoder (INITDEC)
  98. // Figure G.1 – Initialization of the software conventions decoder
  99. // "BP, the pointer to the compressed data, is initialized to BPST (pointing to the first compressed byte)."
  100. auto const BPST = 0;
  101. BP = BPST;
  102. C = (B() ^ 0xFF) << 16;
  103. BYTEIN();
  104. C = C << 7;
  105. CT = CT - 7;
  106. A = 0x8000;
  107. }
  108. u8 QMArithmeticDecoder::DECODE()
  109. {
  110. // E.3.2 Decoding a decision (DECODE)
  111. // Figure G.2 – Decoding an MPS or an LPS in the software-conventions decoder
  112. u8 D;
  113. A = A - Qe(I(CX));
  114. if (C < ((u32)A << 16)) { // `(C_high < A)` in spec
  115. if ((A & 0x8000) == 0) {
  116. D = MPS_EXCHANGE();
  117. RENORMD();
  118. } else {
  119. D = MPS(CX);
  120. }
  121. } else {
  122. C = C - ((u32)A << 16); // `C_high = C_high - A` in spec
  123. D = LPS_EXCHANGE();
  124. RENORMD();
  125. }
  126. return D;
  127. }
  128. u8 QMArithmeticDecoder::MPS_EXCHANGE()
  129. {
  130. // Figure E.16 – Decoder MPS path conditional exchange procedure
  131. u8 D;
  132. if (A < Qe(I(CX))) {
  133. D = 1 - MPS(CX);
  134. if (SWITCH(I(CX)) == 1) {
  135. MPS(CX) = 1 - MPS(CX);
  136. }
  137. I(CX) = NLPS(I(CX));
  138. } else {
  139. D = MPS(CX);
  140. I(CX) = NMPS(I(CX));
  141. }
  142. return D;
  143. }
  144. u8 QMArithmeticDecoder::LPS_EXCHANGE()
  145. {
  146. // Figure E.17 – Decoder LPS path conditional exchange procedure
  147. u8 D;
  148. if (A < Qe(I(CX))) {
  149. A = Qe(I(CX));
  150. D = MPS(CX);
  151. I(CX) = NMPS(I(CX));
  152. } else {
  153. A = Qe(I(CX));
  154. D = 1 - MPS(CX);
  155. if (SWITCH(I(CX)) == 1) {
  156. MPS(CX) = 1 - MPS(CX);
  157. }
  158. I(CX) = NLPS(I(CX));
  159. }
  160. return D;
  161. }
  162. void QMArithmeticDecoder::RENORMD()
  163. {
  164. // E.3.3 Renormalization in the decoder (RENORMD)
  165. // Figure E.18 – Decoder renormalization procedure
  166. do {
  167. if (CT == 0)
  168. BYTEIN();
  169. A = A << 1;
  170. C = C << 1;
  171. CT = CT - 1;
  172. } while ((A & 0x8000) == 0);
  173. }
  174. void QMArithmeticDecoder::BYTEIN()
  175. {
  176. // E.3.4 Compressed data input (BYTEIN)
  177. // Figure G.3 – Inserting a new byte into the C register in the software-conventions decoder
  178. if (B() == 0xFF) {
  179. if (B(1) > 0x8F) {
  180. CT = 8;
  181. } else {
  182. BP = BP + 1;
  183. C = C + 0xFE00 - (B() << 9);
  184. CT = 7;
  185. }
  186. } else {
  187. BP = BP + 1;
  188. C = C + 0xFF00 - (B() << 8);
  189. CT = 8;
  190. }
  191. }
  192. }