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.

708 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 "avcodec.h"
  27. #include "get_bits.h"
  28. #include "mathops.h"
  29. #include "dsputil.h"
  30. #include "lagarithrac.h"
  31. #include "thread.h"
  32. enum LagarithFrameType {
  33. FRAME_RAW = 1, /**< uncompressed */
  34. FRAME_U_RGB24 = 2, /**< unaligned RGB24 */
  35. FRAME_ARITH_YUY2 = 3, /**< arithmetic coded YUY2 */
  36. FRAME_ARITH_RGB24 = 4, /**< arithmetic coded RGB24 */
  37. FRAME_SOLID_GRAY = 5, /**< solid grayscale color frame */
  38. FRAME_SOLID_COLOR = 6, /**< solid non-grayscale color frame */
  39. FRAME_OLD_ARITH_RGB = 7, /**< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
  40. FRAME_ARITH_RGBA = 8, /**< arithmetic coded RGBA */
  41. FRAME_SOLID_RGBA = 9, /**< solid RGBA color frame */
  42. FRAME_ARITH_YV12 = 10, /**< arithmetic coded YV12 */
  43. FRAME_REDUCED_RES = 11, /**< reduced resolution YV12 frame */
  44. };
  45. typedef struct LagarithContext {
  46. AVCodecContext *avctx;
  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 negligible and fixing it
  180. * // breaks backwards compatibility
  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. int i, align_width = (width - 1) & ~31;
  222. /* Left prediction only for first line */
  223. L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
  224. align_width, buf[0]);
  225. for (i = align_width + 1; i < width; i++)
  226. buf[i] += buf[i - 1];
  227. } else {
  228. /* Left pixel is actually prev_row[width] */
  229. L = buf[width - stride - 1];
  230. if (line == 1) {
  231. /* Second line, left predict first pixel, the rest of the line is median predicted
  232. * NOTE: In the case of RGB this pixel is top predicted */
  233. TL = l->avctx->pix_fmt == AV_PIX_FMT_YUV420P ? buf[-stride] : L;
  234. } else {
  235. /* Top left is 2 rows back, last pixel */
  236. TL = buf[width - (2 * stride) - 1];
  237. }
  238. add_lag_median_prediction(buf, buf - stride, buf,
  239. width, &L, &TL);
  240. }
  241. }
  242. static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf,
  243. int width, int stride, int line,
  244. int is_luma)
  245. {
  246. int L, TL;
  247. if (!line) {
  248. int i, align_width;
  249. if (is_luma) {
  250. buf++;
  251. width--;
  252. }
  253. align_width = (width - 1) & ~31;
  254. l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1, align_width, buf[0]);
  255. for (i = align_width + 1; i < width; i++)
  256. buf[i] += buf[i - 1];
  257. return;
  258. }
  259. if (line == 1) {
  260. const int HEAD = is_luma ? 4 : 2;
  261. int i;
  262. L = buf[width - stride - 1];
  263. TL = buf[HEAD - stride - 1];
  264. for (i = 0; i < HEAD; i++) {
  265. L += buf[i];
  266. buf[i] = L;
  267. }
  268. for (; i < width; i++) {
  269. L = mid_pred(L & 0xFF, buf[i - stride], (L + buf[i - stride] - TL) & 0xFF) + buf[i];
  270. TL = buf[i - stride];
  271. buf[i] = L;
  272. }
  273. } else {
  274. TL = buf[width - (2 * stride) - 1];
  275. L = buf[width - stride - 1];
  276. l->dsp.add_hfyu_median_prediction(buf, buf - stride, buf, width,
  277. &L, &TL);
  278. }
  279. }
  280. static int lag_decode_line(LagarithContext *l, lag_rac *rac,
  281. uint8_t *dst, int width, int stride,
  282. int esc_count)
  283. {
  284. int i = 0;
  285. int ret = 0;
  286. if (!esc_count)
  287. esc_count = -1;
  288. /* Output any zeros remaining from the previous run */
  289. handle_zeros:
  290. if (l->zeros_rem) {
  291. int count = FFMIN(l->zeros_rem, width - i);
  292. memset(dst + i, 0, count);
  293. i += count;
  294. l->zeros_rem -= count;
  295. }
  296. while (i < width) {
  297. dst[i] = lag_get_rac(rac);
  298. ret++;
  299. if (dst[i])
  300. l->zeros = 0;
  301. else
  302. l->zeros++;
  303. i++;
  304. if (l->zeros == esc_count) {
  305. int index = lag_get_rac(rac);
  306. ret++;
  307. l->zeros = 0;
  308. l->zeros_rem = lag_calc_zero_run(index);
  309. goto handle_zeros;
  310. }
  311. }
  312. return ret;
  313. }
  314. static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
  315. const uint8_t *src, const uint8_t *src_end,
  316. int width, int esc_count)
  317. {
  318. int i = 0;
  319. int count;
  320. uint8_t zero_run = 0;
  321. const uint8_t *src_start = src;
  322. uint8_t mask1 = -(esc_count < 2);
  323. uint8_t mask2 = -(esc_count < 3);
  324. uint8_t *end = dst + (width - 2);
  325. output_zeros:
  326. if (l->zeros_rem) {
  327. count = FFMIN(l->zeros_rem, width - i);
  328. if (end - dst < count) {
  329. av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
  330. return AVERROR_INVALIDDATA;
  331. }
  332. memset(dst, 0, count);
  333. l->zeros_rem -= count;
  334. dst += count;
  335. }
  336. while (dst < end) {
  337. i = 0;
  338. while (!zero_run && dst + i < end) {
  339. i++;
  340. if (src + i >= src_end)
  341. return AVERROR_INVALIDDATA;
  342. zero_run =
  343. !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
  344. }
  345. if (zero_run) {
  346. zero_run = 0;
  347. i += esc_count;
  348. memcpy(dst, src, i);
  349. dst += i;
  350. l->zeros_rem = lag_calc_zero_run(src[i]);
  351. src += i + 1;
  352. goto output_zeros;
  353. } else {
  354. memcpy(dst, src, i);
  355. src += i;
  356. dst += i;
  357. }
  358. }
  359. return src_start - src;
  360. }
  361. static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  362. int width, int height, int stride,
  363. const uint8_t *src, int src_size)
  364. {
  365. int i = 0;
  366. int read = 0;
  367. uint32_t length;
  368. uint32_t offset = 1;
  369. int esc_count = src[0];
  370. GetBitContext gb;
  371. lag_rac rac;
  372. const uint8_t *src_end = src + src_size;
  373. rac.avctx = l->avctx;
  374. l->zeros = 0;
  375. if (esc_count < 4) {
  376. length = width * height;
  377. if (esc_count && AV_RL32(src + 1) < length) {
  378. length = AV_RL32(src + 1);
  379. offset += 4;
  380. }
  381. init_get_bits(&gb, src + offset, src_size * 8);
  382. if (lag_read_prob_header(&rac, &gb) < 0)
  383. return -1;
  384. ff_lag_rac_init(&rac, &gb, length - stride);
  385. for (i = 0; i < height; i++)
  386. read += lag_decode_line(l, &rac, dst + (i * stride), width,
  387. stride, esc_count);
  388. if (read > length)
  389. av_log(l->avctx, AV_LOG_WARNING,
  390. "Output more bytes than length (%d of %d)\n", read,
  391. length);
  392. } else if (esc_count < 8) {
  393. esc_count -= 4;
  394. if (esc_count > 0) {
  395. /* Zero run coding only, no range coding. */
  396. for (i = 0; i < height; i++) {
  397. int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
  398. src_end, width, esc_count);
  399. if (res < 0)
  400. return res;
  401. src += res;
  402. }
  403. } else {
  404. if (src_size < width * height)
  405. return AVERROR_INVALIDDATA; // buffer not big enough
  406. /* Plane is stored uncompressed */
  407. for (i = 0; i < height; i++) {
  408. memcpy(dst + (i * stride), src, width);
  409. src += width;
  410. }
  411. }
  412. } else if (esc_count == 0xff) {
  413. /* Plane is a solid run of given value */
  414. for (i = 0; i < height; i++)
  415. memset(dst + i * stride, src[1], width);
  416. /* Do not apply prediction.
  417. Note: memset to 0 above, setting first value to src[1]
  418. and applying prediction gives the same result. */
  419. return 0;
  420. } else {
  421. av_log(l->avctx, AV_LOG_ERROR,
  422. "Invalid zero run escape code! (%#x)\n", esc_count);
  423. return -1;
  424. }
  425. if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
  426. for (i = 0; i < height; i++) {
  427. lag_pred_line(l, dst, width, stride, i);
  428. dst += stride;
  429. }
  430. } else {
  431. for (i = 0; i < height; i++) {
  432. lag_pred_line_yuy2(l, dst, width, stride, i,
  433. width == l->avctx->width);
  434. dst += stride;
  435. }
  436. }
  437. return 0;
  438. }
  439. /**
  440. * Decode a frame.
  441. * @param avctx codec context
  442. * @param data output AVFrame
  443. * @param data_size size of output data or 0 if no picture is returned
  444. * @param avpkt input packet
  445. * @return number of consumed bytes on success or negative if decode fails
  446. */
  447. static int lag_decode_frame(AVCodecContext *avctx,
  448. void *data, int *got_frame, AVPacket *avpkt)
  449. {
  450. const uint8_t *buf = avpkt->data;
  451. int buf_size = avpkt->size;
  452. LagarithContext *l = avctx->priv_data;
  453. ThreadFrame frame = { .f = data };
  454. AVFrame *const p = data;
  455. uint8_t frametype = 0;
  456. uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
  457. uint32_t offs[4];
  458. uint8_t *srcs[4], *dst;
  459. int i, j, planes = 3;
  460. p->key_frame = 1;
  461. frametype = buf[0];
  462. offset_gu = AV_RL32(buf + 1);
  463. offset_bv = AV_RL32(buf + 5);
  464. switch (frametype) {
  465. case FRAME_SOLID_RGBA:
  466. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  467. if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
  468. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  469. return -1;
  470. }
  471. dst = p->data[0];
  472. for (j = 0; j < avctx->height; j++) {
  473. for (i = 0; i < avctx->width; i++)
  474. AV_WN32(dst + i * 4, offset_gu);
  475. dst += p->linesize[0];
  476. }
  477. break;
  478. case FRAME_ARITH_RGBA:
  479. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  480. planes = 4;
  481. offset_ry += 4;
  482. offs[3] = AV_RL32(buf + 9);
  483. case FRAME_ARITH_RGB24:
  484. case FRAME_U_RGB24:
  485. if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
  486. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  487. if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
  488. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  489. return -1;
  490. }
  491. offs[0] = offset_bv;
  492. offs[1] = offset_gu;
  493. offs[2] = offset_ry;
  494. if (!l->rgb_planes) {
  495. l->rgb_stride = FFALIGN(avctx->width, 16);
  496. l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * planes + 1);
  497. if (!l->rgb_planes) {
  498. av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
  499. return AVERROR(ENOMEM);
  500. }
  501. }
  502. for (i = 0; i < planes; i++)
  503. srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
  504. if (offset_ry >= buf_size ||
  505. offset_gu >= buf_size ||
  506. offset_bv >= buf_size ||
  507. (planes == 4 && offs[3] >= buf_size)) {
  508. av_log(avctx, AV_LOG_ERROR,
  509. "Invalid frame offsets\n");
  510. return AVERROR_INVALIDDATA;
  511. }
  512. for (i = 0; i < planes; i++)
  513. lag_decode_arith_plane(l, srcs[i],
  514. avctx->width, avctx->height,
  515. -l->rgb_stride, buf + offs[i],
  516. buf_size - offs[i]);
  517. dst = p->data[0];
  518. for (i = 0; i < planes; i++)
  519. srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
  520. for (j = 0; j < avctx->height; j++) {
  521. for (i = 0; i < avctx->width; i++) {
  522. uint8_t r, g, b, a;
  523. r = srcs[0][i];
  524. g = srcs[1][i];
  525. b = srcs[2][i];
  526. r += g;
  527. b += g;
  528. if (frametype == FRAME_ARITH_RGBA) {
  529. a = srcs[3][i];
  530. AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
  531. } else {
  532. dst[i * 3 + 0] = r;
  533. dst[i * 3 + 1] = g;
  534. dst[i * 3 + 2] = b;
  535. }
  536. }
  537. dst += p->linesize[0];
  538. for (i = 0; i < planes; i++)
  539. srcs[i] += l->rgb_stride;
  540. }
  541. break;
  542. case FRAME_ARITH_YUY2:
  543. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  544. if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
  545. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  546. return -1;
  547. }
  548. if (offset_ry >= buf_size ||
  549. offset_gu >= buf_size ||
  550. offset_bv >= buf_size) {
  551. av_log(avctx, AV_LOG_ERROR,
  552. "Invalid frame offsets\n");
  553. return AVERROR_INVALIDDATA;
  554. }
  555. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  556. p->linesize[0], buf + offset_ry,
  557. buf_size - offset_ry);
  558. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  559. avctx->height, p->linesize[1],
  560. buf + offset_gu, buf_size - offset_gu);
  561. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  562. avctx->height, p->linesize[2],
  563. buf + offset_bv, buf_size - offset_bv);
  564. break;
  565. case FRAME_ARITH_YV12:
  566. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  567. if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
  568. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  569. return -1;
  570. }
  571. if (offset_ry >= buf_size ||
  572. offset_gu >= buf_size ||
  573. offset_bv >= buf_size) {
  574. av_log(avctx, AV_LOG_ERROR,
  575. "Invalid frame offsets\n");
  576. return AVERROR_INVALIDDATA;
  577. }
  578. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  579. p->linesize[0], buf + offset_ry,
  580. buf_size - offset_ry);
  581. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  582. avctx->height / 2, p->linesize[2],
  583. buf + offset_gu, buf_size - offset_gu);
  584. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  585. avctx->height / 2, p->linesize[1],
  586. buf + offset_bv, buf_size - offset_bv);
  587. break;
  588. default:
  589. av_log(avctx, AV_LOG_ERROR,
  590. "Unsupported Lagarith frame type: %#x\n", frametype);
  591. return -1;
  592. }
  593. *got_frame = 1;
  594. return buf_size;
  595. }
  596. static av_cold int lag_decode_init(AVCodecContext *avctx)
  597. {
  598. LagarithContext *l = avctx->priv_data;
  599. l->avctx = avctx;
  600. ff_dsputil_init(&l->dsp, avctx);
  601. return 0;
  602. }
  603. static av_cold int lag_decode_end(AVCodecContext *avctx)
  604. {
  605. LagarithContext *l = avctx->priv_data;
  606. av_freep(&l->rgb_planes);
  607. return 0;
  608. }
  609. AVCodec ff_lagarith_decoder = {
  610. .name = "lagarith",
  611. .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
  612. .type = AVMEDIA_TYPE_VIDEO,
  613. .id = AV_CODEC_ID_LAGARITH,
  614. .priv_data_size = sizeof(LagarithContext),
  615. .init = lag_decode_init,
  616. .close = lag_decode_end,
  617. .decode = lag_decode_frame,
  618. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  619. };