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.

642 lines
19KB

  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. uint8_t *rgb_planes;
  51. int rgb_stride;
  52. } LagarithContext;
  53. /**
  54. * Compute the 52bit mantissa of 1/(double)denom.
  55. * This crazy format uses floats in an entropy coder and we have to match x86
  56. * rounding exactly, thus ordinary floats aren't portable enough.
  57. * @param denom denominator
  58. * @return 52bit mantissa
  59. * @see softfloat_mul
  60. */
  61. static uint64_t softfloat_reciprocal(uint32_t denom)
  62. {
  63. int shift = av_log2(denom - 1) + 1;
  64. uint64_t ret = (1ULL << 52) / denom;
  65. uint64_t err = (1ULL << 52) - ret * denom;
  66. ret <<= shift;
  67. err <<= shift;
  68. err += denom / 2;
  69. return ret + err / denom;
  70. }
  71. /**
  72. * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
  73. * Used in combination with softfloat_reciprocal computes x/(double)denom.
  74. * @param x 32bit integer factor
  75. * @param mantissa mantissa of f with exponent 0
  76. * @return 32bit integer value (x*f)
  77. * @see softfloat_reciprocal
  78. */
  79. static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
  80. {
  81. uint64_t l = x * (mantissa & 0xffffffff);
  82. uint64_t h = x * (mantissa >> 32);
  83. h += l >> 32;
  84. l &= 0xffffffff;
  85. l += 1 << av_log2(h >> 21);
  86. h += l >> 32;
  87. return h >> 20;
  88. }
  89. static uint8_t lag_calc_zero_run(int8_t x)
  90. {
  91. return (x << 1) ^ (x >> 7);
  92. }
  93. static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
  94. {
  95. static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
  96. int i;
  97. int bit = 0;
  98. int bits = 0;
  99. int prevbit = 0;
  100. unsigned val;
  101. for (i = 0; i < 7; i++) {
  102. if (prevbit && bit)
  103. break;
  104. prevbit = bit;
  105. bit = get_bits1(gb);
  106. if (bit && !prevbit)
  107. bits += series[i];
  108. }
  109. bits--;
  110. if (bits < 0 || bits > 31) {
  111. *value = 0;
  112. return -1;
  113. } else if (bits == 0) {
  114. *value = 0;
  115. return 0;
  116. }
  117. val = get_bits_long(gb, bits);
  118. val |= 1 << bits;
  119. *value = val - 1;
  120. return 0;
  121. }
  122. static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
  123. {
  124. int i, j, scale_factor;
  125. unsigned prob, cumulative_target;
  126. unsigned cumul_prob = 0;
  127. unsigned scaled_cumul_prob = 0;
  128. rac->prob[0] = 0;
  129. rac->prob[257] = UINT_MAX;
  130. /* Read probabilities from bitstream */
  131. for (i = 1; i < 257; i++) {
  132. if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
  133. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
  134. return -1;
  135. }
  136. if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
  137. av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
  138. return -1;
  139. }
  140. cumul_prob += rac->prob[i];
  141. if (!rac->prob[i]) {
  142. if (lag_decode_prob(gb, &prob)) {
  143. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
  144. return -1;
  145. }
  146. if (prob > 257 - i)
  147. prob = 257 - i;
  148. for (j = 0; j < prob; j++)
  149. rac->prob[++i] = 0;
  150. }
  151. }
  152. if (!cumul_prob) {
  153. av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
  154. return -1;
  155. }
  156. /* Scale probabilities so cumulative probability is an even power of 2. */
  157. scale_factor = av_log2(cumul_prob);
  158. if (cumul_prob & (cumul_prob - 1)) {
  159. uint64_t mul = softfloat_reciprocal(cumul_prob);
  160. for (i = 1; i < 257; i++) {
  161. rac->prob[i] = softfloat_mul(rac->prob[i], mul);
  162. scaled_cumul_prob += rac->prob[i];
  163. }
  164. scale_factor++;
  165. cumulative_target = 1 << scale_factor;
  166. if (scaled_cumul_prob > cumulative_target) {
  167. av_log(rac->avctx, AV_LOG_ERROR,
  168. "Scaled probabilities are larger than target!\n");
  169. return -1;
  170. }
  171. scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
  172. for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
  173. if (rac->prob[i]) {
  174. rac->prob[i]++;
  175. scaled_cumul_prob--;
  176. }
  177. /* Comment from reference source:
  178. * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
  179. * // since the compression change is negligable and fixing it
  180. * // breaks backwards compatibilty
  181. * b =- (signed int)b;
  182. * b &= 0xFF;
  183. * } else {
  184. * b++;
  185. * b &= 0x7f;
  186. * }
  187. */
  188. }
  189. }
  190. rac->scale = scale_factor;
  191. /* Fill probability array with cumulative probability for each symbol. */
  192. for (i = 1; i < 257; i++)
  193. rac->prob[i] += rac->prob[i - 1];
  194. return 0;
  195. }
  196. static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
  197. uint8_t *diff, int w, int *left,
  198. int *left_top)
  199. {
  200. /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
  201. * However the &0xFF on the gradient predictor yealds incorrect output
  202. * for lagarith.
  203. */
  204. int i;
  205. uint8_t l, lt;
  206. l = *left;
  207. lt = *left_top;
  208. for (i = 0; i < w; i++) {
  209. l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
  210. lt = src1[i];
  211. dst[i] = l;
  212. }
  213. *left = l;
  214. *left_top = lt;
  215. }
  216. static void lag_pred_line(LagarithContext *l, uint8_t *buf,
  217. int width, int stride, int line)
  218. {
  219. int L, TL;
  220. if (!line) {
  221. /* Left prediction only for first line */
  222. L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
  223. width - 1, buf[0]);
  224. } else {
  225. /* Left pixel is actually prev_row[width] */
  226. L = buf[width - stride - 1];
  227. if (line == 1) {
  228. /* Second line, left predict first pixel, the rest of the line is median predicted
  229. * NOTE: In the case of RGB this pixel is top predicted */
  230. TL = l->avctx->pix_fmt == PIX_FMT_YUV420P ? buf[-stride] : L;
  231. } else {
  232. /* Top left is 2 rows back, last pixel */
  233. TL = buf[width - (2 * stride) - 1];
  234. }
  235. add_lag_median_prediction(buf, buf - stride, buf,
  236. width, &L, &TL);
  237. }
  238. }
  239. static int lag_decode_line(LagarithContext *l, lag_rac *rac,
  240. uint8_t *dst, int width, int stride,
  241. int esc_count)
  242. {
  243. int i = 0;
  244. int ret = 0;
  245. if (!esc_count)
  246. esc_count = -1;
  247. /* Output any zeros remaining from the previous run */
  248. handle_zeros:
  249. if (l->zeros_rem) {
  250. int count = FFMIN(l->zeros_rem, width - i);
  251. memset(dst + i, 0, count);
  252. i += count;
  253. l->zeros_rem -= count;
  254. }
  255. while (i < width) {
  256. dst[i] = lag_get_rac(rac);
  257. ret++;
  258. if (dst[i])
  259. l->zeros = 0;
  260. else
  261. l->zeros++;
  262. i++;
  263. if (l->zeros == esc_count) {
  264. int index = lag_get_rac(rac);
  265. ret++;
  266. l->zeros = 0;
  267. l->zeros_rem = lag_calc_zero_run(index);
  268. goto handle_zeros;
  269. }
  270. }
  271. return ret;
  272. }
  273. static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
  274. const uint8_t *src, const uint8_t *src_end,
  275. int width, int esc_count)
  276. {
  277. int i = 0;
  278. int count;
  279. uint8_t zero_run = 0;
  280. const uint8_t *src_start = src;
  281. uint8_t mask1 = -(esc_count < 2);
  282. uint8_t mask2 = -(esc_count < 3);
  283. uint8_t *end = dst + (width - 2);
  284. output_zeros:
  285. if (l->zeros_rem) {
  286. count = FFMIN(l->zeros_rem, width - i);
  287. if(end - dst < count) {
  288. av_log(l->avctx, AV_LOG_ERROR, "too many zeros remaining\n");
  289. return AVERROR_INVALIDDATA;
  290. }
  291. memset(dst, 0, count);
  292. l->zeros_rem -= count;
  293. dst += count;
  294. }
  295. while (dst < end) {
  296. i = 0;
  297. while (!zero_run && dst + i < end) {
  298. i++;
  299. if (i+2 >= src_end - src)
  300. return AVERROR_INVALIDDATA;
  301. zero_run =
  302. !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
  303. }
  304. if (zero_run) {
  305. zero_run = 0;
  306. i += esc_count;
  307. memcpy(dst, src, i);
  308. dst += i;
  309. l->zeros_rem = lag_calc_zero_run(src[i]);
  310. src += i + 1;
  311. goto output_zeros;
  312. } else {
  313. memcpy(dst, src, i);
  314. src += i;
  315. dst += i;
  316. }
  317. }
  318. return src - src_start;
  319. }
  320. static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  321. int width, int height, int stride,
  322. const uint8_t *src, int src_size)
  323. {
  324. int i = 0;
  325. int read = 0;
  326. uint32_t length;
  327. uint32_t offset = 1;
  328. int esc_count;
  329. GetBitContext gb;
  330. lag_rac rac;
  331. const uint8_t *src_end = src + src_size;
  332. rac.avctx = l->avctx;
  333. l->zeros = 0;
  334. if(src_size < 2)
  335. return AVERROR_INVALIDDATA;
  336. esc_count = src[0];
  337. if (esc_count < 4) {
  338. length = width * height;
  339. if(src_size < 5)
  340. return AVERROR_INVALIDDATA;
  341. if (esc_count && AV_RL32(src + 1) < length) {
  342. length = AV_RL32(src + 1);
  343. offset += 4;
  344. }
  345. init_get_bits(&gb, src + offset, src_size * 8);
  346. if (lag_read_prob_header(&rac, &gb) < 0)
  347. return -1;
  348. ff_lag_rac_init(&rac, &gb, length - stride);
  349. for (i = 0; i < height; i++)
  350. read += lag_decode_line(l, &rac, dst + (i * stride), width,
  351. stride, esc_count);
  352. if (read > length)
  353. av_log(l->avctx, AV_LOG_WARNING,
  354. "Output more bytes than length (%d of %d)\n", read,
  355. length);
  356. } else if (esc_count < 8) {
  357. esc_count -= 4;
  358. if (esc_count > 0) {
  359. /* Zero run coding only, no range coding. */
  360. for (i = 0; i < height; i++) {
  361. int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
  362. src_end, width, esc_count);
  363. if (res < 0)
  364. return res;
  365. src += res;
  366. }
  367. } else {
  368. if (src_size < width * height)
  369. return AVERROR_INVALIDDATA; // buffer not big enough
  370. /* Plane is stored uncompressed */
  371. for (i = 0; i < height; i++) {
  372. memcpy(dst + (i * stride), src, width);
  373. src += width;
  374. }
  375. }
  376. } else if (esc_count == 0xff) {
  377. /* Plane is a solid run of given value */
  378. for (i = 0; i < height; i++)
  379. memset(dst + i * stride, src[1], width);
  380. /* Do not apply prediction.
  381. Note: memset to 0 above, setting first value to src[1]
  382. and applying prediction gives the same result. */
  383. return 0;
  384. } else {
  385. av_log(l->avctx, AV_LOG_ERROR,
  386. "Invalid zero run escape code! (%#x)\n", esc_count);
  387. return -1;
  388. }
  389. for (i = 0; i < height; i++) {
  390. lag_pred_line(l, dst, width, stride, i);
  391. dst += stride;
  392. }
  393. return 0;
  394. }
  395. /**
  396. * Decode a frame.
  397. * @param avctx codec context
  398. * @param data output AVFrame
  399. * @param data_size size of output data or 0 if no picture is returned
  400. * @param avpkt input packet
  401. * @return number of consumed bytes on success or negative if decode fails
  402. */
  403. static int lag_decode_frame(AVCodecContext *avctx,
  404. void *data, int *data_size, AVPacket *avpkt)
  405. {
  406. const uint8_t *buf = avpkt->data;
  407. unsigned int buf_size = avpkt->size;
  408. LagarithContext *l = avctx->priv_data;
  409. AVFrame *const p = &l->picture;
  410. uint8_t frametype = 0;
  411. uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
  412. int offs[4];
  413. uint8_t *srcs[4], *dst;
  414. int i, j, planes = 3;
  415. AVFrame *picture = data;
  416. if (p->data[0])
  417. avctx->release_buffer(avctx, p);
  418. p->reference = 0;
  419. p->key_frame = 1;
  420. frametype = buf[0];
  421. offset_gu = AV_RL32(buf + 1);
  422. offset_bv = AV_RL32(buf + 5);
  423. switch (frametype) {
  424. case FRAME_SOLID_RGBA:
  425. avctx->pix_fmt = PIX_FMT_RGB32;
  426. if (avctx->get_buffer(avctx, p) < 0) {
  427. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  428. return -1;
  429. }
  430. dst = p->data[0];
  431. for (j = 0; j < avctx->height; j++) {
  432. for (i = 0; i < avctx->width; i++)
  433. AV_WN32(dst + i * 4, offset_gu);
  434. dst += p->linesize[0];
  435. }
  436. break;
  437. case FRAME_ARITH_RGBA:
  438. avctx->pix_fmt = PIX_FMT_RGB32;
  439. planes = 4;
  440. offset_ry += 4;
  441. offs[3] = AV_RL32(buf + 9);
  442. case FRAME_ARITH_RGB24:
  443. case FRAME_U_RGB24:
  444. if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
  445. avctx->pix_fmt = PIX_FMT_RGB24;
  446. if (avctx->get_buffer(avctx, p) < 0) {
  447. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  448. return -1;
  449. }
  450. offs[0] = offset_bv;
  451. offs[1] = offset_gu;
  452. offs[2] = offset_ry;
  453. if (!l->rgb_planes) {
  454. l->rgb_stride = FFALIGN(avctx->width, 16);
  455. l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * planes + 16);
  456. if (!l->rgb_planes) {
  457. av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
  458. return AVERROR(ENOMEM);
  459. }
  460. }
  461. for (i = 0; i < planes; i++)
  462. srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
  463. for (i = 0; i < planes; i++)
  464. if (buf_size <= offs[i]) {
  465. av_log(avctx, AV_LOG_ERROR,
  466. "Invalid frame offsets\n");
  467. return AVERROR_INVALIDDATA;
  468. }
  469. for (i = 0; i < planes; i++)
  470. lag_decode_arith_plane(l, srcs[i],
  471. avctx->width, avctx->height,
  472. -l->rgb_stride, buf + offs[i],
  473. buf_size - offs[i]);
  474. dst = p->data[0];
  475. for (i = 0; i < planes; i++)
  476. srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
  477. for (j = 0; j < avctx->height; j++) {
  478. for (i = 0; i < avctx->width; i++) {
  479. uint8_t r, g, b, a;
  480. r = srcs[0][i];
  481. g = srcs[1][i];
  482. b = srcs[2][i];
  483. r += g;
  484. b += g;
  485. if (frametype == FRAME_ARITH_RGBA) {
  486. a = srcs[3][i];
  487. AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
  488. } else {
  489. dst[i * 3 + 0] = r;
  490. dst[i * 3 + 1] = g;
  491. dst[i * 3 + 2] = b;
  492. }
  493. }
  494. dst += p->linesize[0];
  495. for (i = 0; i < planes; i++)
  496. srcs[i] += l->rgb_stride;
  497. }
  498. break;
  499. case FRAME_ARITH_YV12:
  500. avctx->pix_fmt = PIX_FMT_YUV420P;
  501. if (avctx->get_buffer(avctx, p) < 0) {
  502. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  503. return -1;
  504. }
  505. if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
  506. return AVERROR_INVALIDDATA;
  507. }
  508. if (offset_ry >= buf_size ||
  509. offset_gu >= buf_size ||
  510. offset_bv >= buf_size) {
  511. av_log(avctx, AV_LOG_ERROR,
  512. "Invalid frame offsets\n");
  513. return AVERROR_INVALIDDATA;
  514. }
  515. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  516. p->linesize[0], buf + offset_ry,
  517. buf_size - offset_ry);
  518. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  519. avctx->height / 2, p->linesize[2],
  520. buf + offset_gu, buf_size - offset_gu);
  521. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  522. avctx->height / 2, p->linesize[1],
  523. buf + offset_bv, buf_size - offset_bv);
  524. break;
  525. default:
  526. av_log(avctx, AV_LOG_ERROR,
  527. "Unsupported Lagarith frame type: %#x\n", frametype);
  528. return -1;
  529. }
  530. *picture = *p;
  531. *data_size = sizeof(AVFrame);
  532. return buf_size;
  533. }
  534. static av_cold int lag_decode_init(AVCodecContext *avctx)
  535. {
  536. LagarithContext *l = avctx->priv_data;
  537. l->avctx = avctx;
  538. ff_dsputil_init(&l->dsp, avctx);
  539. return 0;
  540. }
  541. static av_cold int lag_decode_end(AVCodecContext *avctx)
  542. {
  543. LagarithContext *l = avctx->priv_data;
  544. if (l->picture.data[0])
  545. avctx->release_buffer(avctx, &l->picture);
  546. av_freep(&l->rgb_planes);
  547. return 0;
  548. }
  549. AVCodec ff_lagarith_decoder = {
  550. .name = "lagarith",
  551. .type = AVMEDIA_TYPE_VIDEO,
  552. .id = CODEC_ID_LAGARITH,
  553. .priv_data_size = sizeof(LagarithContext),
  554. .init = lag_decode_init,
  555. .close = lag_decode_end,
  556. .decode = lag_decode_frame,
  557. .capabilities = CODEC_CAP_DR1,
  558. .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
  559. };