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.

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