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.

682 lines
20KB

  1. /*
  2. * Apple Pixlet decoder
  3. * Copyright (c) 2016 Paul B Mahol
  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. #include <stdint.h>
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/intmath.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "get_bits.h"
  28. #include "unary.h"
  29. #include "internal.h"
  30. #include "thread.h"
  31. #define NB_LEVELS 4
  32. #define H 0
  33. #define V 1
  34. typedef struct SubBand {
  35. unsigned width, height;
  36. unsigned size;
  37. unsigned x, y;
  38. } SubBand;
  39. typedef struct PixletContext {
  40. AVClass *class;
  41. GetByteContext gb;
  42. GetBitContext gbit;
  43. int levels;
  44. int depth;
  45. int h, w;
  46. int16_t *filter[2];
  47. int16_t *prediction;
  48. int64_t scaling[4][2][NB_LEVELS];
  49. SubBand band[4][NB_LEVELS * 3 + 1];
  50. } PixletContext;
  51. static int init_decoder(AVCodecContext *avctx)
  52. {
  53. PixletContext *ctx = avctx->priv_data;
  54. int i, plane;
  55. ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
  56. ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
  57. ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
  58. if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
  59. return AVERROR(ENOMEM);
  60. for (plane = 0; plane < 3; plane++) {
  61. unsigned shift = plane > 0;
  62. unsigned w = ctx->w >> shift;
  63. unsigned h = ctx->h >> shift;
  64. ctx->band[plane][0].width = w >> NB_LEVELS;
  65. ctx->band[plane][0].height = h >> NB_LEVELS;
  66. ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
  67. for (i = 0; i < NB_LEVELS * 3; i++) {
  68. unsigned scale = ctx->levels - (i / 3);
  69. ctx->band[plane][i + 1].width = w >> scale;
  70. ctx->band[plane][i + 1].height = h >> scale;
  71. ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
  72. ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
  73. ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
  74. }
  75. }
  76. return 0;
  77. }
  78. static void free_buffers(AVCodecContext *avctx)
  79. {
  80. PixletContext *ctx = avctx->priv_data;
  81. av_freep(&ctx->filter[0]);
  82. av_freep(&ctx->filter[1]);
  83. av_freep(&ctx->prediction);
  84. }
  85. static av_cold int pixlet_close(AVCodecContext *avctx)
  86. {
  87. PixletContext *ctx = avctx->priv_data;
  88. free_buffers(avctx);
  89. ctx->w = 0;
  90. ctx->h = 0;
  91. return 0;
  92. }
  93. static av_cold int pixlet_init(AVCodecContext *avctx)
  94. {
  95. avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  96. avctx->color_range = AVCOL_RANGE_JPEG;
  97. return 0;
  98. }
  99. static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int width, ptrdiff_t stride)
  100. {
  101. PixletContext *ctx = avctx->priv_data;
  102. GetBitContext *b = &ctx->gbit;
  103. unsigned cnt1, nbits, k, j = 0, i = 0;
  104. int64_t value, state = 3;
  105. int rlen, escape, flag = 0;
  106. while (i < size) {
  107. nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
  108. cnt1 = get_unary(b, 0, 8);
  109. if (cnt1 < 8) {
  110. value = show_bits(b, nbits);
  111. if (value <= 1) {
  112. skip_bits(b, nbits - 1);
  113. escape = ((1 << nbits) - 1) * cnt1;
  114. } else {
  115. skip_bits(b, nbits);
  116. escape = value + ((1 << nbits) - 1) * cnt1 - 1;
  117. }
  118. } else {
  119. escape = get_bits(b, 16);
  120. }
  121. value = -((escape + flag) & 1) | 1;
  122. dst[j++] = value * ((escape + flag + 1) >> 1);
  123. i++;
  124. if (j == width) {
  125. j = 0;
  126. dst += stride;
  127. }
  128. state = 120 * (escape + flag) + state - (120 * state >> 8);
  129. flag = 0;
  130. if (state * 4 > 0xFF || i >= size)
  131. continue;
  132. nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
  133. escape = av_mod_uintp2(16383, nbits);
  134. cnt1 = get_unary(b, 0, 8);
  135. if (cnt1 > 7) {
  136. rlen = get_bits(b, 16);
  137. } else {
  138. value = show_bits(b, nbits);
  139. if (value > 1) {
  140. skip_bits(b, nbits);
  141. rlen = value + escape * cnt1 - 1;
  142. } else {
  143. skip_bits(b, nbits - 1);
  144. rlen = escape * cnt1;
  145. }
  146. }
  147. if (rlen > size - i)
  148. return AVERROR_INVALIDDATA;
  149. i += rlen;
  150. for (k = 0; k < rlen; k++) {
  151. dst[j++] = 0;
  152. if (j == width) {
  153. j = 0;
  154. dst += stride;
  155. }
  156. }
  157. state = 0;
  158. flag = rlen < 0xFFFF ? 1 : 0;
  159. }
  160. align_get_bits(b);
  161. return get_bits_count(b) >> 3;
  162. }
  163. static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, int size,
  164. int c, int a, int d,
  165. int width, ptrdiff_t stride)
  166. {
  167. PixletContext *ctx = avctx->priv_data;
  168. GetBitContext *b = &ctx->gbit;
  169. unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
  170. int ret, escape, pfx, value, yflag, xflag, flag = 0;
  171. int64_t state = 3, tmp;
  172. if ((ret = init_get_bits8(b, src, bytestream2_get_bytes_left(&ctx->gb))) < 0)
  173. return ret;
  174. if ((a >= 0) + (a ^ (a >> 31)) - (a >> 31) != 1) {
  175. nbits = 33 - ff_clz((a >= 0) + (a ^ (a >> 31)) - (a >> 31) - 1);
  176. if (nbits > 16)
  177. return AVERROR_INVALIDDATA;
  178. } else {
  179. nbits = 1;
  180. }
  181. length = 25 - nbits;
  182. while (i < size) {
  183. if (state >> 8 != -3) {
  184. value = ff_clz((state >> 8) + 3) ^ 0x1F;
  185. } else {
  186. value = -1;
  187. }
  188. cnt1 = get_unary(b, 0, length);
  189. if (cnt1 >= length) {
  190. cnt1 = get_bits(b, nbits);
  191. } else {
  192. pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
  193. cnt1 *= (1 << pfx) - 1;
  194. shbits = show_bits(b, pfx);
  195. if (shbits <= 1) {
  196. skip_bits(b, pfx - 1);
  197. } else {
  198. skip_bits(b, pfx);
  199. cnt1 += shbits - 1;
  200. }
  201. }
  202. xflag = flag + cnt1;
  203. yflag = xflag;
  204. if (flag + cnt1 == 0) {
  205. value = 0;
  206. } else {
  207. xflag &= 1u;
  208. tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
  209. value = xflag + (tmp ^ -xflag);
  210. }
  211. i++;
  212. dst[j++] = value;
  213. if (j == width) {
  214. j = 0;
  215. dst += stride;
  216. }
  217. state += (int64_t)d * yflag - (d * state >> 8);
  218. flag = 0;
  219. if (state * 4 > 0xFF || i >= size)
  220. continue;
  221. pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24;
  222. escape = av_mod_uintp2(16383, pfx);
  223. cnt1 = get_unary(b, 0, 8);
  224. if (cnt1 < 8) {
  225. if (pfx < 1 || pfx > 25)
  226. return AVERROR_INVALIDDATA;
  227. value = show_bits(b, pfx);
  228. if (value > 1) {
  229. skip_bits(b, pfx);
  230. rlen = value + escape * cnt1 - 1;
  231. } else {
  232. skip_bits(b, pfx - 1);
  233. rlen = escape * cnt1;
  234. }
  235. } else {
  236. if (get_bits1(b))
  237. value = get_bits(b, 16);
  238. else
  239. value = get_bits(b, 8);
  240. rlen = value + 8 * escape;
  241. }
  242. if (rlen > 0xFFFF || i + rlen > size)
  243. return AVERROR_INVALIDDATA;
  244. i += rlen;
  245. for (k = 0; k < rlen; k++) {
  246. dst[j++] = 0;
  247. if (j == width) {
  248. j = 0;
  249. dst += stride;
  250. }
  251. }
  252. state = 0;
  253. flag = rlen < 0xFFFF ? 1 : 0;
  254. }
  255. align_get_bits(b);
  256. return get_bits_count(b) >> 3;
  257. }
  258. static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame *frame)
  259. {
  260. PixletContext *ctx = avctx->priv_data;
  261. ptrdiff_t stride = frame->linesize[plane] / 2;
  262. int i, ret;
  263. for (i = 0; i < ctx->levels * 3; i++) {
  264. int32_t a = bytestream2_get_be32(&ctx->gb);
  265. int32_t b = bytestream2_get_be32(&ctx->gb);
  266. int32_t c = bytestream2_get_be32(&ctx->gb);
  267. int32_t d = bytestream2_get_be32(&ctx->gb);
  268. int16_t *dest = (int16_t *)frame->data[plane] + ctx->band[plane][i + 1].x +
  269. stride * ctx->band[plane][i + 1].y;
  270. unsigned size = ctx->band[plane][i + 1].size;
  271. uint32_t magic;
  272. magic = bytestream2_get_be32(&ctx->gb);
  273. if (magic != 0xDEADBEEF) {
  274. av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%08"PRIX32
  275. " for plane %d, band %d\n", magic, plane, i);
  276. return AVERROR_INVALIDDATA;
  277. }
  278. ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
  279. c, (b >= FFABS(a)) ? b : a, d,
  280. ctx->band[plane][i + 1].width, stride);
  281. if (ret < 0) {
  282. av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, band %d\n", plane, i);
  283. return ret;
  284. }
  285. bytestream2_skip(&ctx->gb, ret);
  286. }
  287. return 0;
  288. }
  289. static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height, ptrdiff_t stride)
  290. {
  291. int16_t val;
  292. int i, j;
  293. memset(pred, 0, width * sizeof(*pred));
  294. for (i = 0; i < height; i++) {
  295. val = pred[0] + dst[0];
  296. dst[0] = pred[0] = val;
  297. for (j = 1; j < width; j++) {
  298. val = pred[j] + dst[j];
  299. dst[j] = pred[j] = val;
  300. dst[j] += dst[j-1];
  301. }
  302. dst += stride;
  303. }
  304. }
  305. static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
  306. {
  307. int16_t *low, *high, *ll, *lh, *hl, *hh;
  308. int hsize, i, j;
  309. int64_t value;
  310. hsize = size >> 1;
  311. low = tmp + 4;
  312. high = &low[hsize + 8];
  313. memcpy(low, dest, size);
  314. memcpy(high, dest + hsize, size);
  315. ll = &low[hsize];
  316. lh = &low[hsize];
  317. hl = &high[hsize];
  318. hh = hl;
  319. for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
  320. low[i - 5] = low[j - 1];
  321. lh[0] = ll[-1];
  322. high[i - 5] = high[j - 2];
  323. hh[0] = hl[-2];
  324. }
  325. for (i = 0; i < hsize; i++) {
  326. value = (int64_t) low [i + 1] * -INT64_C(325392907) +
  327. (int64_t) low [i + 0] * INT64_C(3687786320) +
  328. (int64_t) low [i - 1] * -INT64_C(325392907) +
  329. (int64_t) high[i + 0] * INT64_C(1518500249) +
  330. (int64_t) high[i - 1] * INT64_C(1518500249);
  331. dest[i * 2] = av_clip_int16(((value >> 32) * scale) >> 32);
  332. }
  333. for (i = 0; i < hsize; i++) {
  334. value = (int64_t) low [i + 2] * -INT64_C(65078576) +
  335. (int64_t) low [i + 1] * INT64_C(1583578880) +
  336. (int64_t) low [i + 0] * INT64_C(1583578880) +
  337. (int64_t) low [i - 1] * -INT64_C(65078576) +
  338. (int64_t) high[i + 1] * INT64_C(303700064) +
  339. (int64_t) high[i + 0] * -INT64_C(3644400640) +
  340. (int64_t) high[i - 1] * INT64_C(303700064);
  341. dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
  342. }
  343. }
  344. static void reconstruction(AVCodecContext *avctx,
  345. int16_t *dest, unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,
  346. int64_t *scaling_H, int64_t *scaling_V)
  347. {
  348. PixletContext *ctx = avctx->priv_data;
  349. unsigned scaled_width, scaled_height;
  350. int64_t scale_H, scale_V;
  351. int16_t *ptr, *tmp;
  352. int i, j, k;
  353. scaled_height = height >> nb_levels;
  354. scaled_width = width >> nb_levels;
  355. tmp = ctx->filter[0];
  356. for (i = 0; i < nb_levels; i++) {
  357. scaled_width <<= 1;
  358. scaled_height <<= 1;
  359. scale_H = scaling_H[i];
  360. scale_V = scaling_V[i];
  361. ptr = dest;
  362. for (j = 0; j < scaled_height; j++) {
  363. filterfn(ptr, ctx->filter[1], scaled_width, scale_V);
  364. ptr += stride;
  365. }
  366. for (j = 0; j < scaled_width; j++) {
  367. ptr = dest + j;
  368. for (k = 0; k < scaled_height; k++) {
  369. tmp[k] = *ptr;
  370. ptr += stride;
  371. }
  372. filterfn(tmp, ctx->filter[1], scaled_height, scale_H);
  373. ptr = dest + j;
  374. for (k = 0; k < scaled_height; k++) {
  375. *ptr = tmp[k];
  376. ptr += stride;
  377. }
  378. }
  379. }
  380. }
  381. static void postprocess_luma(AVFrame *frame, int w, int h, int depth)
  382. {
  383. uint16_t *dsty = (uint16_t *)frame->data[0];
  384. int16_t *srcy = (int16_t *)frame->data[0];
  385. ptrdiff_t stridey = frame->linesize[0] / 2;
  386. int i, j;
  387. for (j = 0; j < h; j++) {
  388. for (i = 0; i < w; i++) {
  389. if (srcy[i] <= 0)
  390. dsty[i] = 0;
  391. else if (srcy[i] > ((1 << depth) - 1))
  392. dsty[i] = 65535;
  393. else
  394. dsty[i] = ((int64_t) srcy[i] * srcy[i] * 65535) /
  395. ((1 << depth) - 1) / ((1 << depth) - 1);
  396. }
  397. dsty += stridey;
  398. srcy += stridey;
  399. }
  400. }
  401. static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
  402. {
  403. uint16_t *dstu = (uint16_t *)frame->data[1];
  404. uint16_t *dstv = (uint16_t *)frame->data[2];
  405. int16_t *srcu = (int16_t *)frame->data[1];
  406. int16_t *srcv = (int16_t *)frame->data[2];
  407. ptrdiff_t strideu = frame->linesize[1] / 2;
  408. ptrdiff_t stridev = frame->linesize[2] / 2;
  409. const unsigned add = 1 << (depth - 1);
  410. const unsigned shift = 16 - depth;
  411. int i, j;
  412. for (j = 0; j < h; j++) {
  413. for (i = 0; i < w; i++) {
  414. dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
  415. dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
  416. }
  417. dstu += strideu;
  418. dstv += stridev;
  419. srcu += strideu;
  420. srcv += stridev;
  421. }
  422. }
  423. static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame)
  424. {
  425. PixletContext *ctx = avctx->priv_data;
  426. ptrdiff_t stride = frame->linesize[plane] / 2;
  427. unsigned shift = plane > 0;
  428. int16_t *dst;
  429. int i, ret;
  430. for (i = ctx->levels - 1; i >= 0; i--) {
  431. int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
  432. int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
  433. if (!h || !v)
  434. return AVERROR_INVALIDDATA;
  435. ctx->scaling[plane][H][i] = (1000000ULL << 32) / h;
  436. ctx->scaling[plane][V][i] = (1000000ULL << 32) / v;
  437. }
  438. bytestream2_skip(&ctx->gb, 4);
  439. dst = (int16_t *)frame->data[plane];
  440. dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
  441. if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gb),
  442. bytestream2_get_bytes_left(&ctx->gb))) < 0)
  443. return ret;
  444. ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1, ctx->band[plane][0].width - 1, 0);
  445. if (ret < 0) {
  446. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane);
  447. return ret;
  448. }
  449. ret = read_low_coeffs(avctx, dst + stride, ctx->band[plane][0].height - 1, 1, stride);
  450. if (ret < 0) {
  451. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane);
  452. return ret;
  453. }
  454. ret = read_low_coeffs(avctx, dst + stride + 1,
  455. (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
  456. ctx->band[plane][0].width - 1, stride);
  457. if (ret < 0) {
  458. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane);
  459. return ret;
  460. }
  461. bytestream2_skip(&ctx->gb, ret);
  462. if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
  463. av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
  464. return AVERROR_INVALIDDATA;
  465. }
  466. ret = read_highpass(avctx, avpkt->data, plane, frame);
  467. if (ret < 0)
  468. return ret;
  469. lowpass_prediction(dst, ctx->prediction,
  470. ctx->band[plane][0].width, ctx->band[plane][0].height, stride);
  471. reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift, ctx->h >> shift,
  472. stride, NB_LEVELS, ctx->scaling[plane][H], ctx->scaling[plane][V]);
  473. return 0;
  474. }
  475. static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
  476. int *got_frame, AVPacket *avpkt)
  477. {
  478. PixletContext *ctx = avctx->priv_data;
  479. int i, w, h, width, height, ret, version;
  480. AVFrame *p = data;
  481. ThreadFrame frame = { .f = data };
  482. uint32_t pktsize;
  483. bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
  484. pktsize = bytestream2_get_be32(&ctx->gb);
  485. if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
  486. av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);
  487. return AVERROR_INVALIDDATA;
  488. }
  489. version = bytestream2_get_le32(&ctx->gb);
  490. if (version != 1)
  491. avpriv_request_sample(avctx, "Version %d", version);
  492. bytestream2_skip(&ctx->gb, 4);
  493. if (bytestream2_get_be32(&ctx->gb) != 1)
  494. return AVERROR_INVALIDDATA;
  495. bytestream2_skip(&ctx->gb, 4);
  496. width = bytestream2_get_be32(&ctx->gb);
  497. height = bytestream2_get_be32(&ctx->gb);
  498. w = FFALIGN(width, 1 << (NB_LEVELS + 1));
  499. h = FFALIGN(height, 1 << (NB_LEVELS + 1));
  500. ctx->levels = bytestream2_get_be32(&ctx->gb);
  501. if (ctx->levels != NB_LEVELS)
  502. return AVERROR_INVALIDDATA;
  503. ctx->depth = bytestream2_get_be32(&ctx->gb);
  504. if (ctx->depth < 8 || ctx->depth > 15) {
  505. avpriv_request_sample(avctx, "Depth %d", ctx->depth);
  506. return AVERROR_INVALIDDATA;
  507. }
  508. ret = ff_set_dimensions(avctx, w, h);
  509. if (ret < 0)
  510. return ret;
  511. avctx->width = width;
  512. avctx->height = height;
  513. if (ctx->w != w || ctx->h != h) {
  514. free_buffers(avctx);
  515. ctx->w = w;
  516. ctx->h = h;
  517. ret = init_decoder(avctx);
  518. if (ret < 0) {
  519. free_buffers(avctx);
  520. ctx->w = 0;
  521. ctx->h = 0;
  522. return ret;
  523. }
  524. }
  525. bytestream2_skip(&ctx->gb, 8);
  526. p->pict_type = AV_PICTURE_TYPE_I;
  527. p->key_frame = 1;
  528. p->color_range = AVCOL_RANGE_JPEG;
  529. ret = ff_thread_get_buffer(avctx, &frame, 0);
  530. if (ret < 0)
  531. return ret;
  532. for (i = 0; i < 3; i++) {
  533. ret = decode_plane(avctx, i, avpkt, frame.f);
  534. if (ret < 0)
  535. return ret;
  536. if (avctx->flags & AV_CODEC_FLAG_GRAY)
  537. break;
  538. }
  539. postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
  540. postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
  541. *got_frame = 1;
  542. return pktsize;
  543. }
  544. #if HAVE_THREADS
  545. static int pixlet_init_thread_copy(AVCodecContext *avctx)
  546. {
  547. PixletContext *ctx = avctx->priv_data;
  548. ctx->filter[0] = NULL;
  549. ctx->filter[1] = NULL;
  550. ctx->prediction = NULL;
  551. ctx->w = ctx->h = 0;
  552. return 0;
  553. }
  554. #endif
  555. AVCodec ff_pixlet_decoder = {
  556. .name = "pixlet",
  557. .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. .id = AV_CODEC_ID_PIXLET,
  560. .init = pixlet_init,
  561. .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy),
  562. .close = pixlet_close,
  563. .decode = pixlet_decode_frame,
  564. .priv_data_size = sizeof(PixletContext),
  565. .capabilities = AV_CODEC_CAP_DR1 |
  566. AV_CODEC_CAP_FRAME_THREADS,
  567. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  568. FF_CODEC_CAP_INIT_CLEANUP,
  569. };