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.

710 lines
21KB

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