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.

750 lines
23KB

  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. 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 > 256 - i)
  147. prob = 256 - 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 <= 128; i++) {
  161. rac->prob[i] = softfloat_mul(rac->prob[i], mul);
  162. scaled_cumul_prob += rac->prob[i];
  163. }
  164. if (scaled_cumul_prob <= 0) {
  165. av_log(rac->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n");
  166. return AVERROR_INVALIDDATA;
  167. }
  168. for (; i < 257; i++) {
  169. rac->prob[i] = softfloat_mul(rac->prob[i], mul);
  170. scaled_cumul_prob += rac->prob[i];
  171. }
  172. scale_factor++;
  173. cumulative_target = 1 << scale_factor;
  174. if (scaled_cumul_prob > cumulative_target) {
  175. av_log(rac->avctx, AV_LOG_ERROR,
  176. "Scaled probabilities are larger than target!\n");
  177. return -1;
  178. }
  179. scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
  180. for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
  181. if (rac->prob[i]) {
  182. rac->prob[i]++;
  183. scaled_cumul_prob--;
  184. }
  185. /* Comment from reference source:
  186. * if (b & 0x80 == 0) { // order of operations is 'wrong'; it has been left this way
  187. * // since the compression change is negligible and fixing it
  188. * // breaks backwards compatibility
  189. * b =- (signed int)b;
  190. * b &= 0xFF;
  191. * } else {
  192. * b++;
  193. * b &= 0x7f;
  194. * }
  195. */
  196. }
  197. }
  198. rac->scale = scale_factor;
  199. /* Fill probability array with cumulative probability for each symbol. */
  200. for (i = 1; i < 257; i++)
  201. rac->prob[i] += rac->prob[i - 1];
  202. return 0;
  203. }
  204. static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
  205. uint8_t *diff, int w, int *left,
  206. int *left_top)
  207. {
  208. /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
  209. * However the &0xFF on the gradient predictor yealds incorrect output
  210. * for lagarith.
  211. */
  212. int i;
  213. uint8_t l, lt;
  214. l = *left;
  215. lt = *left_top;
  216. for (i = 0; i < w; i++) {
  217. l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
  218. lt = src1[i];
  219. dst[i] = l;
  220. }
  221. *left = l;
  222. *left_top = lt;
  223. }
  224. static void lag_pred_line(LagarithContext *l, uint8_t *buf,
  225. int width, int stride, int line)
  226. {
  227. int L, TL;
  228. if (!line) {
  229. /* Left prediction only for first line */
  230. L = l->dsp.add_hfyu_left_prediction(buf, buf,
  231. width, 0);
  232. } else {
  233. /* Left pixel is actually prev_row[width] */
  234. L = buf[width - stride - 1];
  235. if (line == 1) {
  236. /* Second line, left predict first pixel, the rest of the line is median predicted
  237. * NOTE: In the case of RGB this pixel is top predicted */
  238. TL = l->avctx->pix_fmt == AV_PIX_FMT_YUV420P ? buf[-stride] : L;
  239. } else {
  240. /* Top left is 2 rows back, last pixel */
  241. TL = buf[width - (2 * stride) - 1];
  242. }
  243. add_lag_median_prediction(buf, buf - stride, buf,
  244. width, &L, &TL);
  245. }
  246. }
  247. static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf,
  248. int width, int stride, int line,
  249. int is_luma)
  250. {
  251. int L, TL;
  252. if (!line) {
  253. L= buf[0];
  254. if (is_luma)
  255. buf[0] = 0;
  256. l->dsp.add_hfyu_left_prediction(buf, buf, width, 0);
  257. if (is_luma)
  258. buf[0] = L;
  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. avpriv_request_sample(l->avctx, "zero_run_line");
  328. return AVERROR_PATCHWELCOME;
  329. output_zeros:
  330. if (l->zeros_rem) {
  331. count = FFMIN(l->zeros_rem, width - i);
  332. if (end - dst < count) {
  333. av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
  334. return AVERROR_INVALIDDATA;
  335. }
  336. memset(dst, 0, count);
  337. l->zeros_rem -= count;
  338. dst += count;
  339. }
  340. while (dst < end) {
  341. i = 0;
  342. while (!zero_run && dst + i < end) {
  343. i++;
  344. if (i+2 >= src_end - src)
  345. return AVERROR_INVALIDDATA;
  346. zero_run =
  347. !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
  348. }
  349. if (zero_run) {
  350. zero_run = 0;
  351. i += esc_count;
  352. memcpy(dst, src, i);
  353. dst += i;
  354. l->zeros_rem = lag_calc_zero_run(src[i]);
  355. src += i + 1;
  356. goto output_zeros;
  357. } else {
  358. memcpy(dst, src, i);
  359. src += i;
  360. dst += i;
  361. }
  362. }
  363. return src - src_start;
  364. }
  365. static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  366. int width, int height, int stride,
  367. const uint8_t *src, int src_size)
  368. {
  369. int i = 0;
  370. int read = 0;
  371. uint32_t length;
  372. uint32_t offset = 1;
  373. int esc_count;
  374. GetBitContext gb;
  375. lag_rac rac;
  376. const uint8_t *src_end = src + src_size;
  377. rac.avctx = l->avctx;
  378. l->zeros = 0;
  379. if(src_size < 2)
  380. return AVERROR_INVALIDDATA;
  381. esc_count = src[0];
  382. if (esc_count < 4) {
  383. length = width * height;
  384. if(src_size < 5)
  385. return AVERROR_INVALIDDATA;
  386. if (esc_count && AV_RL32(src + 1) < length) {
  387. length = AV_RL32(src + 1);
  388. offset += 4;
  389. }
  390. init_get_bits(&gb, src + offset, (src_size - offset) * 8);
  391. if (lag_read_prob_header(&rac, &gb) < 0)
  392. return -1;
  393. ff_lag_rac_init(&rac, &gb, length - stride);
  394. for (i = 0; i < height; i++)
  395. read += lag_decode_line(l, &rac, dst + (i * stride), width,
  396. stride, esc_count);
  397. if (read > length)
  398. av_log(l->avctx, AV_LOG_WARNING,
  399. "Output more bytes than length (%d of %d)\n", read,
  400. length);
  401. } else if (esc_count < 8) {
  402. esc_count -= 4;
  403. src ++;
  404. src_size --;
  405. if (esc_count > 0) {
  406. /* Zero run coding only, no range coding. */
  407. for (i = 0; i < height; i++) {
  408. int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
  409. src_end, width, esc_count);
  410. if (res < 0)
  411. return res;
  412. src += res;
  413. }
  414. } else {
  415. if (src_size < width * height)
  416. return AVERROR_INVALIDDATA; // buffer not big enough
  417. /* Plane is stored uncompressed */
  418. for (i = 0; i < height; i++) {
  419. memcpy(dst + (i * stride), src, width);
  420. src += width;
  421. }
  422. }
  423. } else if (esc_count == 0xff) {
  424. /* Plane is a solid run of given value */
  425. for (i = 0; i < height; i++)
  426. memset(dst + i * stride, src[1], width);
  427. /* Do not apply prediction.
  428. Note: memset to 0 above, setting first value to src[1]
  429. and applying prediction gives the same result. */
  430. return 0;
  431. } else {
  432. av_log(l->avctx, AV_LOG_ERROR,
  433. "Invalid zero run escape code! (%#x)\n", esc_count);
  434. return -1;
  435. }
  436. if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
  437. for (i = 0; i < height; i++) {
  438. lag_pred_line(l, dst, width, stride, i);
  439. dst += stride;
  440. }
  441. } else {
  442. for (i = 0; i < height; i++) {
  443. lag_pred_line_yuy2(l, dst, width, stride, i,
  444. width == l->avctx->width);
  445. dst += stride;
  446. }
  447. }
  448. return 0;
  449. }
  450. /**
  451. * Decode a frame.
  452. * @param avctx codec context
  453. * @param data output AVFrame
  454. * @param data_size size of output data or 0 if no picture is returned
  455. * @param avpkt input packet
  456. * @return number of consumed bytes on success or negative if decode fails
  457. */
  458. static int lag_decode_frame(AVCodecContext *avctx,
  459. void *data, int *got_frame, AVPacket *avpkt)
  460. {
  461. const uint8_t *buf = avpkt->data;
  462. unsigned int buf_size = avpkt->size;
  463. LagarithContext *l = avctx->priv_data;
  464. ThreadFrame frame = { .f = data };
  465. AVFrame *const p = data;
  466. uint8_t frametype = 0;
  467. uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
  468. uint32_t offs[4];
  469. uint8_t *srcs[4], *dst;
  470. int i, j, planes = 3;
  471. int ret;
  472. p->key_frame = 1;
  473. frametype = buf[0];
  474. offset_gu = AV_RL32(buf + 1);
  475. offset_bv = AV_RL32(buf + 5);
  476. switch (frametype) {
  477. case FRAME_SOLID_RGBA:
  478. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  479. case FRAME_SOLID_GRAY:
  480. if (frametype == FRAME_SOLID_GRAY)
  481. if (avctx->bits_per_coded_sample == 24) {
  482. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  483. } else {
  484. avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  485. planes = 4;
  486. }
  487. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  488. return ret;
  489. dst = p->data[0];
  490. if (frametype == FRAME_SOLID_RGBA) {
  491. for (j = 0; j < avctx->height; j++) {
  492. for (i = 0; i < avctx->width; i++)
  493. AV_WN32(dst + i * 4, offset_gu);
  494. dst += p->linesize[0];
  495. }
  496. } else {
  497. for (j = 0; j < avctx->height; j++) {
  498. memset(dst, buf[1], avctx->width * planes);
  499. dst += p->linesize[0];
  500. }
  501. }
  502. break;
  503. case FRAME_SOLID_COLOR:
  504. if (avctx->bits_per_coded_sample == 24) {
  505. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  506. } else {
  507. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  508. offset_gu |= 0xFFU << 24;
  509. }
  510. if ((ret = ff_thread_get_buffer(avctx, &frame,0)) < 0)
  511. return ret;
  512. dst = p->data[0];
  513. for (j = 0; j < avctx->height; j++) {
  514. for (i = 0; i < avctx->width; i++)
  515. if (avctx->bits_per_coded_sample == 24) {
  516. AV_WB24(dst + i * 3, offset_gu);
  517. } else {
  518. AV_WN32(dst + i * 4, offset_gu);
  519. }
  520. dst += p->linesize[0];
  521. }
  522. break;
  523. case FRAME_ARITH_RGBA:
  524. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  525. planes = 4;
  526. offset_ry += 4;
  527. offs[3] = AV_RL32(buf + 9);
  528. case FRAME_ARITH_RGB24:
  529. case FRAME_U_RGB24:
  530. if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
  531. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  532. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  533. return ret;
  534. offs[0] = offset_bv;
  535. offs[1] = offset_gu;
  536. offs[2] = offset_ry;
  537. if (!l->rgb_planes) {
  538. l->rgb_stride = FFALIGN(avctx->width, 16);
  539. l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * 4 + 16);
  540. if (!l->rgb_planes) {
  541. av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
  542. return AVERROR(ENOMEM);
  543. }
  544. }
  545. for (i = 0; i < planes; i++)
  546. srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
  547. for (i = 0; i < planes; i++)
  548. if (buf_size <= offs[i]) {
  549. av_log(avctx, AV_LOG_ERROR,
  550. "Invalid frame offsets\n");
  551. return AVERROR_INVALIDDATA;
  552. }
  553. for (i = 0; i < planes; i++)
  554. lag_decode_arith_plane(l, srcs[i],
  555. avctx->width, avctx->height,
  556. -l->rgb_stride, buf + offs[i],
  557. buf_size - offs[i]);
  558. dst = p->data[0];
  559. for (i = 0; i < planes; i++)
  560. srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
  561. for (j = 0; j < avctx->height; j++) {
  562. for (i = 0; i < avctx->width; i++) {
  563. uint8_t r, g, b, a;
  564. r = srcs[0][i];
  565. g = srcs[1][i];
  566. b = srcs[2][i];
  567. r += g;
  568. b += g;
  569. if (frametype == FRAME_ARITH_RGBA) {
  570. a = srcs[3][i];
  571. AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
  572. } else {
  573. dst[i * 3 + 0] = r;
  574. dst[i * 3 + 1] = g;
  575. dst[i * 3 + 2] = b;
  576. }
  577. }
  578. dst += p->linesize[0];
  579. for (i = 0; i < planes; i++)
  580. srcs[i] += l->rgb_stride;
  581. }
  582. break;
  583. case FRAME_ARITH_YUY2:
  584. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  585. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  586. return ret;
  587. if (offset_ry >= buf_size ||
  588. offset_gu >= buf_size ||
  589. offset_bv >= buf_size) {
  590. av_log(avctx, AV_LOG_ERROR,
  591. "Invalid frame offsets\n");
  592. return AVERROR_INVALIDDATA;
  593. }
  594. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  595. p->linesize[0], buf + offset_ry,
  596. buf_size - offset_ry);
  597. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  598. avctx->height, p->linesize[1],
  599. buf + offset_gu, buf_size - offset_gu);
  600. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  601. avctx->height, p->linesize[2],
  602. buf + offset_bv, buf_size - offset_bv);
  603. break;
  604. case FRAME_ARITH_YV12:
  605. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  606. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  607. return ret;
  608. if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
  609. return AVERROR_INVALIDDATA;
  610. }
  611. if (offset_ry >= buf_size ||
  612. offset_gu >= buf_size ||
  613. offset_bv >= buf_size) {
  614. av_log(avctx, AV_LOG_ERROR,
  615. "Invalid frame offsets\n");
  616. return AVERROR_INVALIDDATA;
  617. }
  618. lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
  619. p->linesize[0], buf + offset_ry,
  620. buf_size - offset_ry);
  621. lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
  622. avctx->height / 2, p->linesize[2],
  623. buf + offset_gu, buf_size - offset_gu);
  624. lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
  625. avctx->height / 2, p->linesize[1],
  626. buf + offset_bv, buf_size - offset_bv);
  627. break;
  628. default:
  629. av_log(avctx, AV_LOG_ERROR,
  630. "Unsupported Lagarith frame type: %#x\n", frametype);
  631. return AVERROR_PATCHWELCOME;
  632. }
  633. *got_frame = 1;
  634. return buf_size;
  635. }
  636. static av_cold int lag_decode_init(AVCodecContext *avctx)
  637. {
  638. LagarithContext *l = avctx->priv_data;
  639. l->avctx = avctx;
  640. ff_dsputil_init(&l->dsp, avctx);
  641. return 0;
  642. }
  643. static av_cold int lag_decode_end(AVCodecContext *avctx)
  644. {
  645. LagarithContext *l = avctx->priv_data;
  646. av_freep(&l->rgb_planes);
  647. return 0;
  648. }
  649. AVCodec ff_lagarith_decoder = {
  650. .name = "lagarith",
  651. .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
  652. .type = AVMEDIA_TYPE_VIDEO,
  653. .id = AV_CODEC_ID_LAGARITH,
  654. .priv_data_size = sizeof(LagarithContext),
  655. .init = lag_decode_init,
  656. .close = lag_decode_end,
  657. .decode = lag_decode_frame,
  658. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  659. };