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.

522 lines
15KB

  1. /*
  2. * Lagarith lossless decoder
  3. * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Lagarith lossless decoder
  24. * @author Nathan Caldwell
  25. */
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "mathops.h"
  29. #include "dsputil.h"
  30. #include "lagarithrac.h"
  31. enum LagarithFrameType {
  32. FRAME_RAW = 1, /**< uncompressed */
  33. FRAME_U_RGB24 = 2, /**< unaligned RGB24 */
  34. FRAME_ARITH_YUY2 = 3, /**< arithmetic coded YUY2 */
  35. FRAME_ARITH_RGB24 = 4, /**< arithmetic coded RGB24 */
  36. FRAME_SOLID_GRAY = 5, /**< solid grayscale color frame */
  37. FRAME_SOLID_COLOR = 6, /**< solid non-grayscale color frame */
  38. FRAME_OLD_ARITH_RGB = 7, /**< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
  39. FRAME_ARITH_RGBA = 8, /**< arithmetic coded RGBA */
  40. FRAME_SOLID_RGBA = 9, /**< solid RGBA color frame */
  41. FRAME_ARITH_YV12 = 10, /**< arithmetic coded YV12 */
  42. FRAME_REDUCED_RES = 11, /**< reduced resolution YV12 frame */
  43. };
  44. typedef struct LagarithContext {
  45. AVCodecContext *avctx;
  46. AVFrame picture;
  47. DSPContext dsp;
  48. int zeros; /**< number of consecutive zero bytes encountered */
  49. int zeros_rem; /**< number of zero bytes remaining to output */
  50. } LagarithContext;
  51. /**
  52. * Compute the 52bit mantissa of 1/(double)denom.
  53. * This crazy format uses floats in an entropy coder and we have to match x86
  54. * rounding exactly, thus ordinary floats aren't portable enough.
  55. * @param denom denominator
  56. * @return 52bit mantissa
  57. * @see softfloat_mul
  58. */
  59. static uint64_t softfloat_reciprocal(uint32_t denom)
  60. {
  61. int shift = av_log2(denom - 1) + 1;
  62. uint64_t ret = (1ULL << 52) / denom;
  63. uint64_t err = (1ULL << 52) - ret * denom;
  64. ret <<= shift;
  65. err <<= shift;
  66. err += denom / 2;
  67. return ret + err / denom;
  68. }
  69. /**
  70. * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
  71. * Used in combination with softfloat_reciprocal computes x/(double)denom.
  72. * @param x 32bit integer factor
  73. * @param mantissa mantissa of f with exponent 0
  74. * @return 32bit integer value (x*f)
  75. * @see softfloat_reciprocal
  76. */
  77. static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
  78. {
  79. uint64_t l = x * (mantissa & 0xffffffff);
  80. uint64_t h = x * (mantissa >> 32);
  81. h += l >> 32;
  82. l &= 0xffffffff;
  83. l += 1 << av_log2(h >> 21);
  84. h += l >> 32;
  85. return h >> 20;
  86. }
  87. static uint8_t lag_calc_zero_run(int8_t x)
  88. {
  89. return (x << 1) ^ (x >> 7);
  90. }
  91. static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
  92. {
  93. static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
  94. int i;
  95. int bit = 0;
  96. int bits = 0;
  97. int prevbit = 0;
  98. unsigned val;
  99. for (i = 0; i < 7; i++) {
  100. if (prevbit && bit)
  101. break;
  102. prevbit = bit;
  103. bit = get_bits1(gb);
  104. if (bit && !prevbit)
  105. bits += series[i];
  106. }
  107. bits--;
  108. if (bits < 0 || bits > 31) {
  109. *value = 0;
  110. return -1;
  111. } else if (bits == 0) {
  112. *value = 0;
  113. return 0;
  114. }
  115. val = get_bits_long(gb, bits);
  116. val |= 1 << bits;
  117. *value = val - 1;
  118. return 0;
  119. }
  120. static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
  121. {
  122. int i, j, scale_factor;
  123. unsigned prob, cumulative_target;
  124. unsigned cumul_prob = 0;
  125. unsigned scaled_cumul_prob = 0;
  126. rac->prob[0] = 0;
  127. rac->prob[257] = UINT_MAX;
  128. /* Read probabilities from bitstream */
  129. for (i = 1; i < 257; i++) {
  130. if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
  131. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
  132. return -1;
  133. }
  134. if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
  135. av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
  136. return -1;
  137. }
  138. cumul_prob += rac->prob[i];
  139. if (!rac->prob[i]) {
  140. if (lag_decode_prob(gb, &prob)) {
  141. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
  142. return -1;
  143. }
  144. if (prob > 257 - i)
  145. prob = 257 - i;
  146. for (j = 0; j < prob; j++)
  147. rac->prob[++i] = 0;
  148. }
  149. }
  150. if (!cumul_prob) {
  151. av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
  152. return -1;
  153. }
  154. /* Scale probabilities so cumulative probability is an even power of 2. */
  155. scale_factor = av_log2(cumul_prob);
  156. if (cumul_prob & (cumul_prob - 1)) {
  157. uint64_t mul = softfloat_reciprocal(cumul_prob);
  158. for (i = 1; i < 257; i++) {
  159. rac->prob[i] = softfloat_mul(rac->prob[i], mul);
  160. scaled_cumul_prob += rac->prob[i];
  161. }
  162. scale_factor++;
  163. cumulative_target = 1 << scale_factor;
  164. if (scaled_cumul_prob > cumulative_target) {
  165. av_log(rac->avctx, AV_LOG_ERROR,
  166. "Scaled probabilities are larger than target!\n");
  167. return -1;
  168. }
  169. scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
  170. for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
  171. if (rac->prob[i]) {
  172. rac->prob[i]++;
  173. scaled_cumul_prob--;
  174. }
  175. /* Comment from reference source:
  176. * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
  177. * // since the compression change is negligable and fixing it
  178. * // breaks backwards compatibilty
  179. * b =- (signed int)b;
  180. * b &= 0xFF;
  181. * } else {
  182. * b++;
  183. * b &= 0x7f;
  184. * }
  185. */
  186. }
  187. }
  188. rac->scale = scale_factor;
  189. /* Fill probability array with cumulative probability for each symbol. */
  190. for (i = 1; i < 257; i++)
  191. rac->prob[i] += rac->prob[i - 1];
  192. return 0;
  193. }
  194. static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
  195. uint8_t *diff, int w, int *left,
  196. int *left_top)
  197. {
  198. /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
  199. * However the &0xFF on the gradient predictor yealds incorrect output
  200. * for lagarith.
  201. */
  202. int i;
  203. uint8_t l, lt;
  204. l = *left;
  205. lt = *left_top;
  206. for (i = 0; i < w; i++) {
  207. l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
  208. lt = src1[i];
  209. dst[i] = l;
  210. }
  211. *left = l;
  212. *left_top = lt;
  213. }
  214. static void lag_pred_line(LagarithContext *l, uint8_t *buf,
  215. int width, int stride, int line)
  216. {
  217. int L, TL;
  218. if (!line) {
  219. /* Left prediction only for first line */
  220. L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
  221. width - 1, buf[0]);
  222. return;
  223. } else if (line == 1) {
  224. /* Second line, left predict first pixel, the rest of the line is median predicted */
  225. /* FIXME: In the case of RGB this pixel is top predicted */
  226. TL = buf[-stride];
  227. } else {
  228. /* Top left is 2 rows back, last pixel */
  229. TL = buf[width - (2 * stride) - 1];
  230. }
  231. /* Left pixel is actually prev_row[width] */
  232. L = buf[width - stride - 1];
  233. add_lag_median_prediction(buf, buf - stride, buf,
  234. width, &L, &TL);
  235. }
  236. static int lag_decode_line(LagarithContext *l, lag_rac *rac,
  237. uint8_t *dst, int width, int stride,
  238. int esc_count)
  239. {
  240. int i = 0;
  241. int ret = 0;
  242. if (!esc_count)
  243. esc_count = -1;
  244. /* Output any zeros remaining from the previous run */
  245. handle_zeros:
  246. if (l->zeros_rem) {
  247. int count = FFMIN(l->zeros_rem, width - i);
  248. memset(dst + i, 0, count);
  249. i += count;
  250. l->zeros_rem -= count;
  251. }
  252. while (i < width) {
  253. dst[i] = lag_get_rac(rac);
  254. ret++;
  255. if (dst[i])
  256. l->zeros = 0;
  257. else
  258. l->zeros++;
  259. i++;
  260. if (l->zeros == esc_count) {
  261. int index = lag_get_rac(rac);
  262. ret++;
  263. l->zeros = 0;
  264. l->zeros_rem = lag_calc_zero_run(index);
  265. goto handle_zeros;
  266. }
  267. }
  268. return ret;
  269. }
  270. static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
  271. const uint8_t *src, int width,
  272. int esc_count)
  273. {
  274. int i = 0;
  275. int count;
  276. uint8_t zero_run = 0;
  277. const uint8_t *start = src;
  278. uint8_t mask1 = -(esc_count < 2);
  279. uint8_t mask2 = -(esc_count < 3);
  280. uint8_t *end = dst + (width - 2);
  281. output_zeros:
  282. if (l->zeros_rem) {
  283. count = FFMIN(l->zeros_rem, width - i);
  284. memset(dst, 0, count);
  285. l->zeros_rem -= count;
  286. dst += count;
  287. }
  288. while (dst < end) {
  289. i = 0;
  290. while (!zero_run && dst + i < end) {
  291. i++;
  292. zero_run =
  293. !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
  294. }
  295. if (zero_run) {
  296. zero_run = 0;
  297. i += esc_count;
  298. memcpy(dst, src, i);
  299. dst += i;
  300. l->zeros_rem = lag_calc_zero_run(src[i]);
  301. src += i + 1;
  302. goto output_zeros;
  303. } else {
  304. memcpy(dst, src, i);
  305. src += i;
  306. }
  307. }
  308. return start - src;
  309. }
  310. static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  311. int width, int height, int stride,
  312. const uint8_t *src, int src_size)
  313. {
  314. int i = 0;
  315. int read = 0;
  316. uint32_t length;
  317. uint32_t offset = 1;
  318. int esc_count = src[0];
  319. GetBitContext gb;
  320. lag_rac rac;
  321. rac.avctx = l->avctx;
  322. l->zeros = 0;
  323. if (esc_count < 4) {
  324. length = width * height;
  325. if (esc_count && AV_RL32(src + 1) < length) {
  326. length = AV_RL32(src + 1);
  327. offset += 4;
  328. }
  329. init_get_bits(&gb, src + offset, src_size * 8);
  330. if (lag_read_prob_header(&rac, &gb) < 0)
  331. return -1;
  332. lag_rac_init(&rac, &gb, length - stride);
  333. for (i = 0; i < height; i++)
  334. read += lag_decode_line(l, &rac, dst + (i * stride), width,
  335. stride, esc_count);
  336. if (read > length)
  337. av_log(l->avctx, AV_LOG_WARNING,
  338. "Output more bytes than length (%d of %d)\n", read,
  339. length);
  340. } else if (esc_count < 8) {
  341. esc_count -= 4;
  342. if (esc_count > 0) {
  343. /* Zero run coding only, no range coding. */
  344. for (i = 0; i < height; i++)
  345. src += lag_decode_zero_run_line(l, dst + (i * stride), src,
  346. width, esc_count);
  347. } else {
  348. /* Plane is stored uncompressed */
  349. for (i = 0; i < height; i++) {
  350. memcpy(dst + (i * stride), src, width);
  351. src += width;
  352. }
  353. }
  354. } else if (esc_count == 0xff) {
  355. /* Plane is a solid run of given value */
  356. for (i = 0; i < height; i++)
  357. memset(dst + i * stride, src[1], width);
  358. /* Do not apply prediction.
  359. Note: memset to 0 above, setting first value to src[1]
  360. and applying prediction gives the same result. */
  361. return 0;
  362. } else {
  363. av_log(l->avctx, AV_LOG_ERROR,
  364. "Invalid zero run escape code! (%#x)\n", esc_count);
  365. return -1;
  366. }
  367. for (i = 0; i < height; i++) {
  368. lag_pred_line(l, dst, width, stride, i);
  369. dst += stride;
  370. }
  371. return 0;
  372. }
  373. /**
  374. * Decode a frame.
  375. * @param avctx codec context
  376. * @param data output AVFrame
  377. * @param data_size size of output data or 0 if no picture is returned
  378. * @param avpkt input packet
  379. * @return number of consumed bytes on success or negative if decode fails
  380. */
  381. static int lag_decode_frame(AVCodecContext *avctx,
  382. void *data, int *data_size, AVPacket *avpkt)
  383. {
  384. const uint8_t *buf = avpkt->data;
  385. int buf_size = avpkt->size;
  386. LagarithContext *l = avctx->priv_data;
  387. AVFrame *const p = &l->picture;
  388. uint8_t frametype = 0;
  389. uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
  390. AVFrame *picture = data;
  391. if (p->data[0])
  392. avctx->release_buffer(avctx, p);
  393. p->reference = 0;
  394. p->key_frame = 1;
  395. frametype = buf[0];
  396. offset_gu = AV_RL32(buf + 1);
  397. offset_bv = AV_RL32(buf + 5);
  398. switch (frametype) {
  399. case FRAME_ARITH_YV12:
  400. avctx->pix_fmt = PIX_FMT_YUV420P;
  401. if (avctx->get_buffer(avctx, p) < 0) {
  402. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  403. return -1;
  404. }
  405. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  406. p->linesize[0], buf + offset_ry,
  407. buf_size);
  408. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  409. avctx->height / 2, p->linesize[2],
  410. buf + offset_gu, buf_size);
  411. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  412. avctx->height / 2, p->linesize[1],
  413. buf + offset_bv, buf_size);
  414. break;
  415. default:
  416. av_log(avctx, AV_LOG_ERROR,
  417. "Unsupported Lagarith frame type: %#x\n", frametype);
  418. return -1;
  419. }
  420. *picture = *p;
  421. *data_size = sizeof(AVFrame);
  422. return buf_size;
  423. }
  424. static av_cold int lag_decode_init(AVCodecContext *avctx)
  425. {
  426. LagarithContext *l = avctx->priv_data;
  427. l->avctx = avctx;
  428. dsputil_init(&l->dsp, avctx);
  429. return 0;
  430. }
  431. static av_cold int lag_decode_end(AVCodecContext *avctx)
  432. {
  433. LagarithContext *l = avctx->priv_data;
  434. if (l->picture.data[0])
  435. avctx->release_buffer(avctx, &l->picture);
  436. return 0;
  437. }
  438. AVCodec ff_lagarith_decoder = {
  439. .name = "lagarith",
  440. .type = AVMEDIA_TYPE_VIDEO,
  441. .id = CODEC_ID_LAGARITH,
  442. .priv_data_size = sizeof(LagarithContext),
  443. .init = lag_decode_init,
  444. .close = lag_decode_end,
  445. .decode = lag_decode_frame,
  446. .capabilities = CODEC_CAP_DR1,
  447. .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
  448. };