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.

732 lines
22KB

  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. #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. AVFrame picture;
  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_stride;
  53. } LagarithContext;
  54. /**
  55. * Compute the 52bit mantissa of 1/(double)denom.
  56. * This crazy format uses floats in an entropy coder and we have to match x86
  57. * rounding exactly, thus ordinary floats aren't portable enough.
  58. * @param denom denominator
  59. * @return 52bit mantissa
  60. * @see softfloat_mul
  61. */
  62. static uint64_t softfloat_reciprocal(uint32_t denom)
  63. {
  64. int shift = av_log2(denom - 1) + 1;
  65. uint64_t ret = (1ULL << 52) / denom;
  66. uint64_t err = (1ULL << 52) - ret * denom;
  67. ret <<= shift;
  68. err <<= shift;
  69. err += denom / 2;
  70. return ret + err / denom;
  71. }
  72. /**
  73. * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
  74. * Used in combination with softfloat_reciprocal computes x/(double)denom.
  75. * @param x 32bit integer factor
  76. * @param mantissa mantissa of f with exponent 0
  77. * @return 32bit integer value (x*f)
  78. * @see softfloat_reciprocal
  79. */
  80. static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
  81. {
  82. uint64_t l = x * (mantissa & 0xffffffff);
  83. uint64_t h = x * (mantissa >> 32);
  84. h += l >> 32;
  85. l &= 0xffffffff;
  86. l += 1 << av_log2(h >> 21);
  87. h += l >> 32;
  88. return h >> 20;
  89. }
  90. static uint8_t lag_calc_zero_run(int8_t x)
  91. {
  92. return (x << 1) ^ (x >> 7);
  93. }
  94. static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
  95. {
  96. static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
  97. int i;
  98. int bit = 0;
  99. int bits = 0;
  100. int prevbit = 0;
  101. unsigned val;
  102. for (i = 0; i < 7; i++) {
  103. if (prevbit && bit)
  104. break;
  105. prevbit = bit;
  106. bit = get_bits1(gb);
  107. if (bit && !prevbit)
  108. bits += series[i];
  109. }
  110. bits--;
  111. if (bits < 0 || bits > 31) {
  112. *value = 0;
  113. return -1;
  114. } else if (bits == 0) {
  115. *value = 0;
  116. return 0;
  117. }
  118. val = get_bits_long(gb, bits);
  119. val |= 1 << bits;
  120. *value = val - 1;
  121. return 0;
  122. }
  123. static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
  124. {
  125. int i, j, scale_factor;
  126. unsigned prob, cumulative_target;
  127. unsigned cumul_prob = 0;
  128. unsigned scaled_cumul_prob = 0;
  129. rac->prob[0] = 0;
  130. rac->prob[257] = UINT_MAX;
  131. /* Read probabilities from bitstream */
  132. for (i = 1; i < 257; i++) {
  133. if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
  134. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
  135. return -1;
  136. }
  137. if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
  138. av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
  139. return -1;
  140. }
  141. cumul_prob += rac->prob[i];
  142. if (!rac->prob[i]) {
  143. if (lag_decode_prob(gb, &prob)) {
  144. av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
  145. return -1;
  146. }
  147. if (prob > 256 - i)
  148. prob = 256 - i;
  149. for (j = 0; j < prob; j++)
  150. rac->prob[++i] = 0;
  151. }
  152. }
  153. if (!cumul_prob) {
  154. av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
  155. return -1;
  156. }
  157. /* Scale probabilities so cumulative probability is an even power of 2. */
  158. scale_factor = av_log2(cumul_prob);
  159. if (cumul_prob & (cumul_prob - 1)) {
  160. uint64_t mul = softfloat_reciprocal(cumul_prob);
  161. for (i = 1; i < 257; i++) {
  162. rac->prob[i] = softfloat_mul(rac->prob[i], mul);
  163. scaled_cumul_prob += rac->prob[i];
  164. }
  165. scale_factor++;
  166. cumulative_target = 1 << scale_factor;
  167. if (scaled_cumul_prob > cumulative_target) {
  168. av_log(rac->avctx, AV_LOG_ERROR,
  169. "Scaled probabilities are larger than target!\n");
  170. return -1;
  171. }
  172. scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
  173. for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
  174. if (rac->prob[i]) {
  175. rac->prob[i]++;
  176. scaled_cumul_prob--;
  177. }
  178. /* Comment from reference source:
  179. * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
  180. * // since the compression change is negligible and fixing it
  181. * // breaks backwards compatibility
  182. * b =- (signed int)b;
  183. * b &= 0xFF;
  184. * } else {
  185. * b++;
  186. * b &= 0x7f;
  187. * }
  188. */
  189. }
  190. }
  191. rac->scale = scale_factor;
  192. /* Fill probability array with cumulative probability for each symbol. */
  193. for (i = 1; i < 257; i++)
  194. rac->prob[i] += rac->prob[i - 1];
  195. return 0;
  196. }
  197. static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
  198. uint8_t *diff, int w, int *left,
  199. int *left_top)
  200. {
  201. /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
  202. * However the &0xFF on the gradient predictor yealds incorrect output
  203. * for lagarith.
  204. */
  205. int i;
  206. uint8_t l, lt;
  207. l = *left;
  208. lt = *left_top;
  209. for (i = 0; i < w; i++) {
  210. l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
  211. lt = src1[i];
  212. dst[i] = l;
  213. }
  214. *left = l;
  215. *left_top = lt;
  216. }
  217. static void lag_pred_line(LagarithContext *l, uint8_t *buf,
  218. int width, int stride, int line)
  219. {
  220. int L, TL;
  221. if (!line) {
  222. /* Left prediction only for first line */
  223. L = l->dsp.add_hfyu_left_prediction(buf, buf,
  224. width, 0);
  225. } else {
  226. /* Left pixel is actually prev_row[width] */
  227. L = buf[width - stride - 1];
  228. if (line == 1) {
  229. /* Second line, left predict first pixel, the rest of the line is median predicted
  230. * NOTE: In the case of RGB this pixel is top predicted */
  231. TL = l->avctx->pix_fmt == AV_PIX_FMT_YUV420P ? buf[-stride] : L;
  232. } else {
  233. /* Top left is 2 rows back, last pixel */
  234. TL = buf[width - (2 * stride) - 1];
  235. }
  236. add_lag_median_prediction(buf, buf - stride, buf,
  237. width, &L, &TL);
  238. }
  239. }
  240. static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf,
  241. int width, int stride, int line,
  242. int is_luma)
  243. {
  244. int L, TL;
  245. if (!line) {
  246. L= buf[0];
  247. if (is_luma)
  248. buf[0] = 0;
  249. l->dsp.add_hfyu_left_prediction(buf, buf, width, 0);
  250. if (is_luma)
  251. buf[0] = L;
  252. return;
  253. }
  254. if (line == 1) {
  255. const int HEAD = is_luma ? 4 : 2;
  256. int i;
  257. L = buf[width - stride - 1];
  258. TL = buf[HEAD - stride - 1];
  259. for (i = 0; i < HEAD; i++) {
  260. L += buf[i];
  261. buf[i] = L;
  262. }
  263. for (; i<width; i++) {
  264. L = mid_pred(L&0xFF, buf[i-stride], (L + buf[i-stride] - TL)&0xFF) + buf[i];
  265. TL = buf[i-stride];
  266. buf[i]= L;
  267. }
  268. } else {
  269. TL = buf[width - (2 * stride) - 1];
  270. L = buf[width - stride - 1];
  271. l->dsp.add_hfyu_median_prediction(buf, buf - stride, buf, width,
  272. &L, &TL);
  273. }
  274. }
  275. static int lag_decode_line(LagarithContext *l, lag_rac *rac,
  276. uint8_t *dst, int width, int stride,
  277. int esc_count)
  278. {
  279. int i = 0;
  280. int ret = 0;
  281. if (!esc_count)
  282. esc_count = -1;
  283. /* Output any zeros remaining from the previous run */
  284. handle_zeros:
  285. if (l->zeros_rem) {
  286. int count = FFMIN(l->zeros_rem, width - i);
  287. memset(dst + i, 0, count);
  288. i += count;
  289. l->zeros_rem -= count;
  290. }
  291. while (i < width) {
  292. dst[i] = lag_get_rac(rac);
  293. ret++;
  294. if (dst[i])
  295. l->zeros = 0;
  296. else
  297. l->zeros++;
  298. i++;
  299. if (l->zeros == esc_count) {
  300. int index = lag_get_rac(rac);
  301. ret++;
  302. l->zeros = 0;
  303. l->zeros_rem = lag_calc_zero_run(index);
  304. goto handle_zeros;
  305. }
  306. }
  307. return ret;
  308. }
  309. static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
  310. const uint8_t *src, const uint8_t *src_end,
  311. int width, int esc_count)
  312. {
  313. int i = 0;
  314. int count;
  315. uint8_t zero_run = 0;
  316. const uint8_t *src_start = src;
  317. uint8_t mask1 = -(esc_count < 2);
  318. uint8_t mask2 = -(esc_count < 3);
  319. uint8_t *end = dst + (width - 2);
  320. output_zeros:
  321. if (l->zeros_rem) {
  322. count = FFMIN(l->zeros_rem, width - i);
  323. if (end - dst < count) {
  324. av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
  325. return AVERROR_INVALIDDATA;
  326. }
  327. memset(dst, 0, count);
  328. l->zeros_rem -= count;
  329. dst += count;
  330. }
  331. while (dst < end) {
  332. i = 0;
  333. while (!zero_run && dst + i < end) {
  334. i++;
  335. if (i+2 >= src_end - src)
  336. return AVERROR_INVALIDDATA;
  337. zero_run =
  338. !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
  339. }
  340. if (zero_run) {
  341. zero_run = 0;
  342. i += esc_count;
  343. memcpy(dst, src, i);
  344. dst += i;
  345. l->zeros_rem = lag_calc_zero_run(src[i]);
  346. src += i + 1;
  347. goto output_zeros;
  348. } else {
  349. memcpy(dst, src, i);
  350. src += i;
  351. dst += i;
  352. }
  353. }
  354. return src - src_start;
  355. }
  356. static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  357. int width, int height, int stride,
  358. const uint8_t *src, int src_size)
  359. {
  360. int i = 0;
  361. int read = 0;
  362. uint32_t length;
  363. uint32_t offset = 1;
  364. int esc_count;
  365. GetBitContext gb;
  366. lag_rac rac;
  367. const uint8_t *src_end = src + src_size;
  368. rac.avctx = l->avctx;
  369. l->zeros = 0;
  370. if(src_size < 2)
  371. return AVERROR_INVALIDDATA;
  372. esc_count = src[0];
  373. if (esc_count < 4) {
  374. length = width * height;
  375. if(src_size < 5)
  376. return AVERROR_INVALIDDATA;
  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. unsigned int buf_size = avpkt->size;
  452. LagarithContext *l = avctx->priv_data;
  453. AVFrame *const p = &l->picture;
  454. uint8_t frametype = 0;
  455. uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
  456. uint32_t offs[4];
  457. uint8_t *srcs[4], *dst;
  458. int i, j, planes = 3;
  459. int ret;
  460. AVFrame *picture = data;
  461. if (p->data[0])
  462. ff_thread_release_buffer(avctx, p);
  463. p->reference = 0;
  464. p->key_frame = 1;
  465. frametype = buf[0];
  466. offset_gu = AV_RL32(buf + 1);
  467. offset_bv = AV_RL32(buf + 5);
  468. switch (frametype) {
  469. case FRAME_SOLID_RGBA:
  470. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  471. case FRAME_SOLID_GRAY:
  472. if (frametype == FRAME_SOLID_GRAY)
  473. if (avctx->bits_per_coded_sample == 24) {
  474. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  475. } else {
  476. avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  477. planes = 4;
  478. }
  479. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  480. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  481. return ret;
  482. }
  483. dst = p->data[0];
  484. if (frametype == FRAME_SOLID_RGBA) {
  485. for (j = 0; j < avctx->height; j++) {
  486. for (i = 0; i < avctx->width; i++)
  487. AV_WN32(dst + i * 4, offset_gu);
  488. dst += p->linesize[0];
  489. }
  490. } else {
  491. for (j = 0; j < avctx->height; j++) {
  492. memset(dst, buf[1], avctx->width * planes);
  493. dst += p->linesize[0];
  494. }
  495. }
  496. break;
  497. case FRAME_ARITH_RGBA:
  498. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  499. planes = 4;
  500. offset_ry += 4;
  501. offs[3] = AV_RL32(buf + 9);
  502. case FRAME_ARITH_RGB24:
  503. case FRAME_U_RGB24:
  504. if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
  505. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  506. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  507. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  508. return ret;
  509. }
  510. offs[0] = offset_bv;
  511. offs[1] = offset_gu;
  512. offs[2] = offset_ry;
  513. if (!l->rgb_planes) {
  514. l->rgb_stride = FFALIGN(avctx->width, 16);
  515. l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * 4 + 16);
  516. if (!l->rgb_planes) {
  517. av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
  518. return AVERROR(ENOMEM);
  519. }
  520. }
  521. for (i = 0; i < planes; i++)
  522. srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
  523. for (i = 0; i < planes; i++)
  524. if (buf_size <= offs[i]) {
  525. av_log(avctx, AV_LOG_ERROR,
  526. "Invalid frame offsets\n");
  527. return AVERROR_INVALIDDATA;
  528. }
  529. for (i = 0; i < planes; i++)
  530. lag_decode_arith_plane(l, srcs[i],
  531. avctx->width, avctx->height,
  532. -l->rgb_stride, buf + offs[i],
  533. buf_size - offs[i]);
  534. dst = p->data[0];
  535. for (i = 0; i < planes; i++)
  536. srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
  537. for (j = 0; j < avctx->height; j++) {
  538. for (i = 0; i < avctx->width; i++) {
  539. uint8_t r, g, b, a;
  540. r = srcs[0][i];
  541. g = srcs[1][i];
  542. b = srcs[2][i];
  543. r += g;
  544. b += g;
  545. if (frametype == FRAME_ARITH_RGBA) {
  546. a = srcs[3][i];
  547. AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
  548. } else {
  549. dst[i * 3 + 0] = r;
  550. dst[i * 3 + 1] = g;
  551. dst[i * 3 + 2] = b;
  552. }
  553. }
  554. dst += p->linesize[0];
  555. for (i = 0; i < planes; i++)
  556. srcs[i] += l->rgb_stride;
  557. }
  558. break;
  559. case FRAME_ARITH_YUY2:
  560. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  561. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  562. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  563. return ret;
  564. }
  565. if (offset_ry >= buf_size ||
  566. offset_gu >= buf_size ||
  567. offset_bv >= buf_size) {
  568. av_log(avctx, AV_LOG_ERROR,
  569. "Invalid frame offsets\n");
  570. return AVERROR_INVALIDDATA;
  571. }
  572. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  573. p->linesize[0], buf + offset_ry,
  574. buf_size - offset_ry);
  575. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  576. avctx->height, p->linesize[1],
  577. buf + offset_gu, buf_size - offset_gu);
  578. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  579. avctx->height, p->linesize[2],
  580. buf + offset_bv, buf_size - offset_bv);
  581. break;
  582. case FRAME_ARITH_YV12:
  583. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  584. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  585. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  586. return ret;
  587. }
  588. if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
  589. return AVERROR_INVALIDDATA;
  590. }
  591. if (offset_ry >= buf_size ||
  592. offset_gu >= buf_size ||
  593. offset_bv >= buf_size) {
  594. av_log(avctx, AV_LOG_ERROR,
  595. "Invalid frame offsets\n");
  596. return AVERROR_INVALIDDATA;
  597. }
  598. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  599. p->linesize[0], buf + offset_ry,
  600. buf_size - offset_ry);
  601. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  602. avctx->height / 2, p->linesize[2],
  603. buf + offset_gu, buf_size - offset_gu);
  604. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  605. avctx->height / 2, p->linesize[1],
  606. buf + offset_bv, buf_size - offset_bv);
  607. break;
  608. default:
  609. av_log(avctx, AV_LOG_ERROR,
  610. "Unsupported Lagarith frame type: %#x\n", frametype);
  611. return AVERROR_PATCHWELCOME;
  612. }
  613. *picture = *p;
  614. *got_frame = 1;
  615. return buf_size;
  616. }
  617. static av_cold int lag_decode_init(AVCodecContext *avctx)
  618. {
  619. LagarithContext *l = avctx->priv_data;
  620. l->avctx = avctx;
  621. ff_dsputil_init(&l->dsp, avctx);
  622. return 0;
  623. }
  624. static av_cold int lag_decode_end(AVCodecContext *avctx)
  625. {
  626. LagarithContext *l = avctx->priv_data;
  627. if (l->picture.data[0])
  628. ff_thread_release_buffer(avctx, &l->picture);
  629. av_freep(&l->rgb_planes);
  630. return 0;
  631. }
  632. AVCodec ff_lagarith_decoder = {
  633. .name = "lagarith",
  634. .type = AVMEDIA_TYPE_VIDEO,
  635. .id = AV_CODEC_ID_LAGARITH,
  636. .priv_data_size = sizeof(LagarithContext),
  637. .init = lag_decode_init,
  638. .close = lag_decode_end,
  639. .decode = lag_decode_frame,
  640. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  641. .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
  642. };