You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
13KB

  1. /*
  2. * ELS (Entropy Logarithmic-Scale) decoder
  3. *
  4. * Copyright (c) 2013 Maxim Poliakovski
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Entropy Logarithmic-Scale binary arithmetic decoder
  25. */
  26. #include <math.h>
  27. #include <stdint.h>
  28. #include "libavutil/common.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avcodec.h"
  31. #include "elsdec.h"
  32. /* ELS coder constants and structures. */
  33. #define ELS_JOTS_PER_BYTE 36
  34. #define ELS_MAX (1 << 24)
  35. #define RUNG_SPACE (64 * sizeof(ElsRungNode))
  36. /* ELS coder tables. */
  37. static const struct Ladder {
  38. int8_t AMps;
  39. int8_t ALps;
  40. uint8_t next0;
  41. uint8_t next1;
  42. } Ladder[174] = {
  43. { -6, -5, 2, 1 },
  44. { -2, -12, 3, 6 },
  45. { -2, -12, 4, 6 },
  46. { -1, -16, 7, 5 },
  47. { -1, -16, 8, 10 },
  48. { -5, -6, 11, 9 },
  49. { -6, -5, 10, 5 },
  50. { -1, -18, 13, 11 },
  51. { -1, -18, 12, 14 },
  52. { -6, -5, 15, 18 },
  53. { -5, -6, 14, 9 },
  54. { -3, -8, 17, 15 },
  55. { -1, -20, 20, 16 },
  56. { -1, -20, 23, 17 },
  57. { -3, -8, 16, 18 },
  58. { -5, -6, 19, 26 },
  59. { -3, -9, 22, 24 },
  60. { -3, -9, 21, 19 },
  61. { -5, -6, 24, 26 },
  62. { -4, -7, 27, 25 },
  63. { -1, -22, 34, 28 },
  64. { -2, -11, 29, 27 },
  65. { -2, -11, 28, 30 },
  66. { -1, -22, 39, 29 },
  67. { -4, -7, 30, 32 },
  68. { -6, -5, 33, 31 },
  69. { -6, -5, 32, 25 },
  70. { -3, -8, 35, 33 },
  71. { -2, -12, 36, 38 },
  72. { -2, -12, 37, 35 },
  73. { -3, -8, 38, 40 },
  74. { -6, -5, 41, 48 },
  75. { -6, -5, 40, 31 },
  76. { -5, -6, 43, 41 },
  77. { -1, -24, 94, 42 },
  78. { -3, -8, 45, 43 },
  79. { -2, -12, 42, 44 },
  80. { -2, -12, 47, 45 },
  81. { -3, -8, 44, 46 },
  82. { -1, -24, 125, 47 },
  83. { -5, -6, 46, 48 },
  84. { -6, -5, 49, 49 },
  85. { -2, -13, 152, 164 },
  86. { -4, -7, 51, 49 },
  87. { -3, -9, 164, 168 },
  88. { -3, -9, 55, 51 },
  89. { -4, -7, 168, 170 },
  90. { -2, -13, 67, 55 },
  91. { -6, -5, 170, 49 },
  92. { -6, -5, 51, 170 },
  93. { -1, -72, 50, 74 },
  94. { -4, -7, 53, 49 },
  95. { -1, -61, 50, 74 },
  96. { -3, -8, 55, 49 },
  97. { -1, -51, 52, 76 },
  98. { -3, -9, 57, 51 },
  99. { -1, -46, 54, 76 },
  100. { -2, -10, 59, 53 },
  101. { -1, -43, 56, 78 },
  102. { -2, -11, 61, 53 },
  103. { -1, -41, 58, 80 },
  104. { -2, -12, 63, 55 },
  105. { -1, -39, 60, 82 },
  106. { -2, -12, 65, 55 },
  107. { -1, -37, 62, 84 },
  108. { -2, -13, 67, 57 },
  109. { -1, -36, 64, 86 },
  110. { -1, -14, 69, 59 },
  111. { -1, -35, 66, 88 },
  112. { -1, -14, 71, 59 },
  113. { -1, -34, 68, 90 },
  114. { -1, -15, 73, 61 },
  115. { -1, -33, 70, 92 },
  116. { -1, -15, 75, 61 },
  117. { -1, -32, 72, 94 },
  118. { -1, -15, 77, 63 },
  119. { -1, -31, 74, 96 },
  120. { -1, -16, 79, 65 },
  121. { -1, -31, 76, 98 },
  122. { -1, -16, 81, 67 },
  123. { -1, -30, 78, 100 },
  124. { -1, -17, 83, 67 },
  125. { -1, -29, 80, 102 },
  126. { -1, -17, 85, 69 },
  127. { -1, -29, 82, 104 },
  128. { -1, -18, 87, 71 },
  129. { -1, -28, 84, 104 },
  130. { -1, -18, 89, 73 },
  131. { -1, -28, 86, 108 },
  132. { -1, -18, 91, 73 },
  133. { -1, -27, 88, 108 },
  134. { -1, -19, 93, 75 },
  135. { -1, -27, 90, 112 },
  136. { -1, -19, 95, 77 },
  137. { -1, -26, 92, 112 },
  138. { -1, -20, 97, 79 },
  139. { -1, -26, 94, 114 },
  140. { -1, -20, 99, 81 },
  141. { -1, -25, 96, 116 },
  142. { -1, -20, 101, 83 },
  143. { -1, -25, 98, 118 },
  144. { -1, -21, 103, 83 },
  145. { -1, -24, 100, 120 },
  146. { -1, -21, 105, 85 },
  147. { -1, -24, 102, 122 },
  148. { -1, -22, 107, 87 },
  149. { -1, -23, 104, 124 },
  150. { -1, -22, 109, 89 },
  151. { -1, -23, 106, 126 },
  152. { -1, -22, 111, 91 },
  153. { -1, -22, 108, 128 },
  154. { -1, -23, 113, 93 },
  155. { -1, -22, 110, 130 },
  156. { -1, -23, 115, 95 },
  157. { -1, -22, 112, 132 },
  158. { -1, -24, 117, 97 },
  159. { -1, -21, 114, 134 },
  160. { -1, -24, 119, 99 },
  161. { -1, -21, 116, 136 },
  162. { -1, -25, 121, 101 },
  163. { -1, -20, 118, 136 },
  164. { -1, -25, 123, 103 },
  165. { -1, -20, 120, 138 },
  166. { -1, -26, 125, 105 },
  167. { -1, -20, 122, 140 },
  168. { -1, -26, 127, 107 },
  169. { -1, -19, 124, 142 },
  170. { -1, -27, 129, 107 },
  171. { -1, -19, 126, 144 },
  172. { -1, -27, 131, 111 },
  173. { -1, -18, 128, 146 },
  174. { -1, -28, 133, 111 },
  175. { -1, -18, 130, 146 },
  176. { -1, -28, 135, 115 },
  177. { -1, -18, 132, 148 },
  178. { -1, -29, 137, 115 },
  179. { -1, -17, 134, 150 },
  180. { -1, -29, 139, 117 },
  181. { -1, -17, 136, 152 },
  182. { -1, -30, 141, 119 },
  183. { -1, -16, 138, 152 },
  184. { -1, -31, 143, 121 },
  185. { -1, -16, 140, 154 },
  186. { -1, -31, 145, 123 },
  187. { -1, -15, 142, 156 },
  188. { -1, -32, 147, 125 },
  189. { -1, -15, 144, 158 },
  190. { -1, -33, 149, 127 },
  191. { -1, -15, 146, 158 },
  192. { -1, -34, 151, 129 },
  193. { -1, -14, 148, 160 },
  194. { -1, -35, 153, 131 },
  195. { -1, -14, 150, 160 },
  196. { -1, -36, 155, 133 },
  197. { -2, -13, 152, 162 },
  198. { -1, -37, 157, 135 },
  199. { -2, -12, 154, 164 },
  200. { -1, -39, 159, 137 },
  201. { -2, -12, 156, 164 },
  202. { -1, -41, 161, 139 },
  203. { -2, -11, 158, 166 },
  204. { -1, -43, 163, 141 },
  205. { -2, -10, 160, 166 },
  206. { -1, -46, 165, 143 },
  207. { -3, -9, 162, 168 },
  208. { -1, -51, 167, 143 },
  209. { -3, -8, 164, 170 },
  210. { -1, -61, 169, 145 },
  211. { -4, -7, 166, 170 },
  212. { -1, -72, 169, 145 },
  213. { -6, -5, 168, 49 },
  214. { 0, -108, 171, 171 },
  215. { 0, -108, 172, 172 },
  216. { -6, -5, 173, 173 },
  217. };
  218. static const uint32_t els_exp_tab[ELS_JOTS_PER_BYTE * 4 + 1] = {
  219. 0, 0, 0, 0, 0, 0, 0, 0,
  220. 0, 0, 0, 0, 0, 0, 0, 0,
  221. 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0,
  223. 0, 0, 0, 0, 1, 1, 1, 1,
  224. 1, 2, 2, 2, 3, 4, 4, 5,
  225. 6, 7, 8, 10, 11, 13, 16, 18,
  226. 21, 25, 29, 34, 40, 47, 54, 64,
  227. 74, 87, 101, 118, 138, 161, 188, 219,
  228. 256, 298, 348, 406, 474, 552, 645, 752,
  229. 877, 1024, 1194, 1393, 1625, 1896, 2211, 2580,
  230. 3010, 3511, 4096, 4778, 5573, 6501, 7584, 8847,
  231. 10321, 12040, 14045, 16384, 19112, 22295, 26007, 30339,
  232. 35391, 41285, 48160, 56180, 65536, 76288, 89088, 103936,
  233. 121344, 141312, 165120, 192512, 224512, 262144, 305664, 356608,
  234. 416000, 485376, 566016, 660480, 770560, 898816, 1048576, 1223168,
  235. 1426688, 1664256, 1941504, 2264832, 2642176, 3082240, 3595520, 4194304,
  236. 4892672, 5707520, 6657792, 7766784, 9060096, 10568960, 12328960, 14382080,
  237. 16777216,
  238. };
  239. void ff_els_decoder_init(ElsDecCtx *ctx, const uint8_t *in, size_t data_size)
  240. {
  241. int nbytes;
  242. /* consume up to 3 bytes from the input data */
  243. if (data_size >= 3) {
  244. ctx->x = AV_RB24(in);
  245. nbytes = 3;
  246. } else if (data_size == 2) {
  247. ctx->x = AV_RB16(in);
  248. nbytes = 2;
  249. } else {
  250. ctx->x = *in;
  251. nbytes = 1;
  252. }
  253. ctx->in_buf = in + nbytes;
  254. ctx->data_size = data_size - nbytes;
  255. ctx->err = 0;
  256. ctx->j = ELS_JOTS_PER_BYTE;
  257. ctx->t = ELS_MAX;
  258. ctx->diff = FFMIN(ELS_MAX - ctx->x,
  259. ELS_MAX - els_exp_tab[ELS_JOTS_PER_BYTE * 4 - 1]);
  260. }
  261. void ff_els_decoder_uninit(ElsUnsignedRung *rung)
  262. {
  263. av_free(rung->rem_rung_list);
  264. }
  265. static int els_import_byte(ElsDecCtx *ctx)
  266. {
  267. if (!ctx->data_size) {
  268. ctx->err = AVERROR_EOF;
  269. return AVERROR_EOF;
  270. }
  271. ctx->x = (ctx->x << 8) | *ctx->in_buf++;
  272. ctx->data_size--;
  273. ctx->j += ELS_JOTS_PER_BYTE;
  274. ctx->t <<= 8;
  275. return 0;
  276. }
  277. int ff_els_decode_bit(ElsDecCtx *ctx, uint8_t *rung)
  278. {
  279. int z, bit, ret;
  280. const uint32_t *pAllowable = &els_exp_tab[ELS_JOTS_PER_BYTE * 3];
  281. if (ctx->err)
  282. return 0;
  283. z = pAllowable[ctx->j + Ladder[*rung].ALps];
  284. ctx->t -= z;
  285. ctx->diff -= z;
  286. if (ctx->diff > 0)
  287. return *rung & 1; /* shortcut for x < t > pAllowable[j - 1] */
  288. if (ctx->t > ctx->x) { /* decode most probable symbol (MPS) */
  289. ctx->j += Ladder[*rung].AMps;
  290. while (ctx->t > pAllowable[ctx->j])
  291. ctx->j++;
  292. if (ctx->j <= 0) { /* MPS: import one byte from bytestream. */
  293. ret = els_import_byte(ctx);
  294. if (ret < 0)
  295. return ret;
  296. }
  297. z = ctx->t;
  298. bit = *rung & 1;
  299. *rung = Ladder[*rung].next0;
  300. } else { /* decode less probable symbol (LPS) */
  301. ctx->x -= ctx->t;
  302. ctx->t = z;
  303. ctx->j += Ladder[*rung].ALps;
  304. if (ctx->j <= 0) {
  305. /* LPS: import one byte from bytestream. */
  306. z <<= 8;
  307. ret = els_import_byte(ctx);
  308. if (ret < 0)
  309. return ret;
  310. if (ctx->j <= 0) {
  311. /* LPS: import second byte from bytestream. */
  312. z <<= 8;
  313. ret = els_import_byte(ctx);
  314. if (ret < 0)
  315. return ret;
  316. while (pAllowable[ctx->j - 1] >= z)
  317. ctx->j--;
  318. }
  319. }
  320. bit = !(*rung & 1);
  321. *rung = Ladder[*rung].next1;
  322. }
  323. ctx->diff = FFMIN(z - ctx->x, z - pAllowable[ctx->j - 1]);
  324. return bit;
  325. }
  326. unsigned ff_els_decode_unsigned(ElsDecCtx *ctx, ElsUnsignedRung *ur)
  327. {
  328. int i, n, r, bit;
  329. ElsRungNode *rung_node;
  330. if (ctx->err)
  331. return 0;
  332. /* decode unary prefix */
  333. for (n = 0; n < ELS_EXPGOLOMB_LEN + 1; n++)
  334. if (ff_els_decode_bit(ctx, &ur->prefix_rung[n]))
  335. break;
  336. /* handle the error/overflow case */
  337. if (ctx->err || n >= ELS_EXPGOLOMB_LEN) {
  338. ctx->err = AVERROR_INVALIDDATA;
  339. return 0;
  340. }
  341. /* handle the zero case */
  342. if (!n)
  343. return 0;
  344. /* initialize probability tree */
  345. if (!ur->rem_rung_list) {
  346. ur->rem_rung_list = av_realloc(NULL, RUNG_SPACE);
  347. if (!ur->rem_rung_list) {
  348. ctx->err = AVERROR(ENOMEM);
  349. return 0;
  350. }
  351. memset(ur->rem_rung_list, 0, RUNG_SPACE);
  352. ur->rung_list_size = RUNG_SPACE;
  353. ur->avail_index = ELS_EXPGOLOMB_LEN;
  354. }
  355. /* decode the remainder */
  356. for (i = 0, r = 0, bit = 0; i < n; i++) {
  357. if (!i)
  358. rung_node = &ur->rem_rung_list[n];
  359. else {
  360. if (!rung_node->next_index) {
  361. if (ur->rung_list_size <= (ur->avail_index + 2) * sizeof(ElsRungNode)) {
  362. // remember rung_node position
  363. ptrdiff_t pos = rung_node - ur->rem_rung_list;
  364. ur->rem_rung_list = av_realloc(ur->rem_rung_list,
  365. ur->rung_list_size +
  366. RUNG_SPACE);
  367. if (!ur->rem_rung_list) {
  368. av_free(ur->rem_rung_list);
  369. ctx->err = AVERROR(ENOMEM);
  370. return 0;
  371. }
  372. memset((uint8_t *) ur->rem_rung_list + ur->rung_list_size, 0,
  373. RUNG_SPACE);
  374. ur->rung_list_size += RUNG_SPACE;
  375. // restore rung_node position in the new list
  376. rung_node = &ur->rem_rung_list[pos];
  377. }
  378. rung_node->next_index = ur->avail_index;
  379. ur->avail_index += 2;
  380. }
  381. rung_node = &ur->rem_rung_list[rung_node->next_index + bit];
  382. }
  383. bit = ff_els_decode_bit(ctx, &rung_node->rung);
  384. if (ctx->err)
  385. return bit;
  386. r = (r << 1) + bit;
  387. }
  388. return (1 << n) - 1 + r; /* make value from exp golomb code */
  389. }