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.

684 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 * 4ULL > 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. if (pfx < 1 || pfx > 25)
  194. return AVERROR_INVALIDDATA;
  195. cnt1 *= (1 << pfx) - 1;
  196. shbits = show_bits(b, pfx);
  197. if (shbits <= 1) {
  198. skip_bits(b, pfx - 1);
  199. } else {
  200. skip_bits(b, pfx);
  201. cnt1 += shbits - 1;
  202. }
  203. }
  204. xflag = flag + cnt1;
  205. yflag = xflag;
  206. if (flag + cnt1 == 0) {
  207. value = 0;
  208. } else {
  209. xflag &= 1u;
  210. tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
  211. value = xflag + (tmp ^ -xflag);
  212. }
  213. i++;
  214. dst[j++] = value;
  215. if (j == width) {
  216. j = 0;
  217. dst += stride;
  218. }
  219. state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);
  220. flag = 0;
  221. if (state * 4ULL > 0xFF || i >= size)
  222. continue;
  223. pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24;
  224. escape = av_mod_uintp2(16383, pfx);
  225. cnt1 = get_unary(b, 0, 8);
  226. if (cnt1 < 8) {
  227. if (pfx < 1 || pfx > 25)
  228. return AVERROR_INVALIDDATA;
  229. value = show_bits(b, pfx);
  230. if (value > 1) {
  231. skip_bits(b, pfx);
  232. rlen = value + escape * cnt1 - 1;
  233. } else {
  234. skip_bits(b, pfx - 1);
  235. rlen = escape * cnt1;
  236. }
  237. } else {
  238. if (get_bits1(b))
  239. value = get_bits(b, 16);
  240. else
  241. value = get_bits(b, 8);
  242. rlen = value + 8 * escape;
  243. }
  244. if (rlen > 0xFFFF || i + rlen > size)
  245. return AVERROR_INVALIDDATA;
  246. i += rlen;
  247. for (k = 0; k < rlen; k++) {
  248. dst[j++] = 0;
  249. if (j == width) {
  250. j = 0;
  251. dst += stride;
  252. }
  253. }
  254. state = 0;
  255. flag = rlen < 0xFFFF ? 1 : 0;
  256. }
  257. align_get_bits(b);
  258. return get_bits_count(b) >> 3;
  259. }
  260. static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame *frame)
  261. {
  262. PixletContext *ctx = avctx->priv_data;
  263. ptrdiff_t stride = frame->linesize[plane] / 2;
  264. int i, ret;
  265. for (i = 0; i < ctx->levels * 3; i++) {
  266. int32_t a = bytestream2_get_be32(&ctx->gb);
  267. int32_t b = bytestream2_get_be32(&ctx->gb);
  268. int32_t c = bytestream2_get_be32(&ctx->gb);
  269. int32_t d = bytestream2_get_be32(&ctx->gb);
  270. int16_t *dest = (int16_t *)frame->data[plane] + ctx->band[plane][i + 1].x +
  271. stride * ctx->band[plane][i + 1].y;
  272. unsigned size = ctx->band[plane][i + 1].size;
  273. uint32_t magic;
  274. magic = bytestream2_get_be32(&ctx->gb);
  275. if (magic != 0xDEADBEEF) {
  276. av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%08"PRIX32
  277. " for plane %d, band %d\n", magic, plane, i);
  278. return AVERROR_INVALIDDATA;
  279. }
  280. ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
  281. c, (b >= FFABS(a)) ? b : a, d,
  282. ctx->band[plane][i + 1].width, stride);
  283. if (ret < 0) {
  284. av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, band %d\n", plane, i);
  285. return ret;
  286. }
  287. bytestream2_skip(&ctx->gb, ret);
  288. }
  289. return 0;
  290. }
  291. static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height, ptrdiff_t stride)
  292. {
  293. int16_t val;
  294. int i, j;
  295. memset(pred, 0, width * sizeof(*pred));
  296. for (i = 0; i < height; i++) {
  297. val = pred[0] + dst[0];
  298. dst[0] = pred[0] = val;
  299. for (j = 1; j < width; j++) {
  300. val = pred[j] + dst[j];
  301. dst[j] = pred[j] = val;
  302. dst[j] += dst[j-1];
  303. }
  304. dst += stride;
  305. }
  306. }
  307. static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
  308. {
  309. int16_t *low, *high, *ll, *lh, *hl, *hh;
  310. int hsize, i, j;
  311. int64_t value;
  312. hsize = size >> 1;
  313. low = tmp + 4;
  314. high = &low[hsize + 8];
  315. memcpy(low, dest, size);
  316. memcpy(high, dest + hsize, size);
  317. ll = &low[hsize];
  318. lh = &low[hsize];
  319. hl = &high[hsize];
  320. hh = hl;
  321. for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
  322. low[i - 5] = low[j - 1];
  323. lh[0] = ll[-1];
  324. high[i - 5] = high[j - 2];
  325. hh[0] = hl[-2];
  326. }
  327. for (i = 0; i < hsize; i++) {
  328. value = (int64_t) low [i + 1] * -INT64_C(325392907) +
  329. (int64_t) low [i + 0] * INT64_C(3687786320) +
  330. (int64_t) low [i - 1] * -INT64_C(325392907) +
  331. (int64_t) high[i + 0] * INT64_C(1518500249) +
  332. (int64_t) high[i - 1] * INT64_C(1518500249);
  333. dest[i * 2] = av_clip_int16(((value >> 32) * scale) >> 32);
  334. }
  335. for (i = 0; i < hsize; i++) {
  336. value = (int64_t) low [i + 2] * -INT64_C(65078576) +
  337. (int64_t) low [i + 1] * INT64_C(1583578880) +
  338. (int64_t) low [i + 0] * INT64_C(1583578880) +
  339. (int64_t) low [i - 1] * -INT64_C(65078576) +
  340. (int64_t) high[i + 1] * INT64_C(303700064) +
  341. (int64_t) high[i + 0] * -INT64_C(3644400640) +
  342. (int64_t) high[i - 1] * INT64_C(303700064);
  343. dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
  344. }
  345. }
  346. static void reconstruction(AVCodecContext *avctx,
  347. int16_t *dest, unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,
  348. int64_t *scaling_H, int64_t *scaling_V)
  349. {
  350. PixletContext *ctx = avctx->priv_data;
  351. unsigned scaled_width, scaled_height;
  352. int64_t scale_H, scale_V;
  353. int16_t *ptr, *tmp;
  354. int i, j, k;
  355. scaled_height = height >> nb_levels;
  356. scaled_width = width >> nb_levels;
  357. tmp = ctx->filter[0];
  358. for (i = 0; i < nb_levels; i++) {
  359. scaled_width <<= 1;
  360. scaled_height <<= 1;
  361. scale_H = scaling_H[i];
  362. scale_V = scaling_V[i];
  363. ptr = dest;
  364. for (j = 0; j < scaled_height; j++) {
  365. filterfn(ptr, ctx->filter[1], scaled_width, scale_V);
  366. ptr += stride;
  367. }
  368. for (j = 0; j < scaled_width; j++) {
  369. ptr = dest + j;
  370. for (k = 0; k < scaled_height; k++) {
  371. tmp[k] = *ptr;
  372. ptr += stride;
  373. }
  374. filterfn(tmp, ctx->filter[1], scaled_height, scale_H);
  375. ptr = dest + j;
  376. for (k = 0; k < scaled_height; k++) {
  377. *ptr = tmp[k];
  378. ptr += stride;
  379. }
  380. }
  381. }
  382. }
  383. static void postprocess_luma(AVFrame *frame, int w, int h, int depth)
  384. {
  385. uint16_t *dsty = (uint16_t *)frame->data[0];
  386. int16_t *srcy = (int16_t *)frame->data[0];
  387. ptrdiff_t stridey = frame->linesize[0] / 2;
  388. int i, j;
  389. for (j = 0; j < h; j++) {
  390. for (i = 0; i < w; i++) {
  391. if (srcy[i] <= 0)
  392. dsty[i] = 0;
  393. else if (srcy[i] > ((1 << depth) - 1))
  394. dsty[i] = 65535;
  395. else
  396. dsty[i] = ((int64_t) srcy[i] * srcy[i] * 65535) /
  397. ((1 << depth) - 1) / ((1 << depth) - 1);
  398. }
  399. dsty += stridey;
  400. srcy += stridey;
  401. }
  402. }
  403. static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
  404. {
  405. uint16_t *dstu = (uint16_t *)frame->data[1];
  406. uint16_t *dstv = (uint16_t *)frame->data[2];
  407. int16_t *srcu = (int16_t *)frame->data[1];
  408. int16_t *srcv = (int16_t *)frame->data[2];
  409. ptrdiff_t strideu = frame->linesize[1] / 2;
  410. ptrdiff_t stridev = frame->linesize[2] / 2;
  411. const unsigned add = 1 << (depth - 1);
  412. const unsigned shift = 16 - depth;
  413. int i, j;
  414. for (j = 0; j < h; j++) {
  415. for (i = 0; i < w; i++) {
  416. dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
  417. dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
  418. }
  419. dstu += strideu;
  420. dstv += stridev;
  421. srcu += strideu;
  422. srcv += stridev;
  423. }
  424. }
  425. static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame)
  426. {
  427. PixletContext *ctx = avctx->priv_data;
  428. ptrdiff_t stride = frame->linesize[plane] / 2;
  429. unsigned shift = plane > 0;
  430. int16_t *dst;
  431. int i, ret;
  432. for (i = ctx->levels - 1; i >= 0; i--) {
  433. int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
  434. int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
  435. if (!h || !v)
  436. return AVERROR_INVALIDDATA;
  437. ctx->scaling[plane][H][i] = (1000000ULL << 32) / h;
  438. ctx->scaling[plane][V][i] = (1000000ULL << 32) / v;
  439. }
  440. bytestream2_skip(&ctx->gb, 4);
  441. dst = (int16_t *)frame->data[plane];
  442. dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
  443. if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gb),
  444. bytestream2_get_bytes_left(&ctx->gb))) < 0)
  445. return ret;
  446. ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1, ctx->band[plane][0].width - 1, 0);
  447. if (ret < 0) {
  448. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane);
  449. return ret;
  450. }
  451. ret = read_low_coeffs(avctx, dst + stride, ctx->band[plane][0].height - 1, 1, stride);
  452. if (ret < 0) {
  453. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane);
  454. return ret;
  455. }
  456. ret = read_low_coeffs(avctx, dst + stride + 1,
  457. (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
  458. ctx->band[plane][0].width - 1, stride);
  459. if (ret < 0) {
  460. av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane);
  461. return ret;
  462. }
  463. bytestream2_skip(&ctx->gb, ret);
  464. if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
  465. av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
  466. return AVERROR_INVALIDDATA;
  467. }
  468. ret = read_highpass(avctx, avpkt->data, plane, frame);
  469. if (ret < 0)
  470. return ret;
  471. lowpass_prediction(dst, ctx->prediction,
  472. ctx->band[plane][0].width, ctx->band[plane][0].height, stride);
  473. reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift, ctx->h >> shift,
  474. stride, NB_LEVELS, ctx->scaling[plane][H], ctx->scaling[plane][V]);
  475. return 0;
  476. }
  477. static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
  478. int *got_frame, AVPacket *avpkt)
  479. {
  480. PixletContext *ctx = avctx->priv_data;
  481. int i, w, h, width, height, ret, version;
  482. AVFrame *p = data;
  483. ThreadFrame frame = { .f = data };
  484. uint32_t pktsize;
  485. bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
  486. pktsize = bytestream2_get_be32(&ctx->gb);
  487. if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
  488. av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);
  489. return AVERROR_INVALIDDATA;
  490. }
  491. version = bytestream2_get_le32(&ctx->gb);
  492. if (version != 1)
  493. avpriv_request_sample(avctx, "Version %d", version);
  494. bytestream2_skip(&ctx->gb, 4);
  495. if (bytestream2_get_be32(&ctx->gb) != 1)
  496. return AVERROR_INVALIDDATA;
  497. bytestream2_skip(&ctx->gb, 4);
  498. width = bytestream2_get_be32(&ctx->gb);
  499. height = bytestream2_get_be32(&ctx->gb);
  500. w = FFALIGN(width, 1 << (NB_LEVELS + 1));
  501. h = FFALIGN(height, 1 << (NB_LEVELS + 1));
  502. ctx->levels = bytestream2_get_be32(&ctx->gb);
  503. if (ctx->levels != NB_LEVELS)
  504. return AVERROR_INVALIDDATA;
  505. ctx->depth = bytestream2_get_be32(&ctx->gb);
  506. if (ctx->depth < 8 || ctx->depth > 15) {
  507. avpriv_request_sample(avctx, "Depth %d", ctx->depth);
  508. return AVERROR_INVALIDDATA;
  509. }
  510. ret = ff_set_dimensions(avctx, w, h);
  511. if (ret < 0)
  512. return ret;
  513. avctx->width = width;
  514. avctx->height = height;
  515. if (ctx->w != w || ctx->h != h) {
  516. free_buffers(avctx);
  517. ctx->w = w;
  518. ctx->h = h;
  519. ret = init_decoder(avctx);
  520. if (ret < 0) {
  521. free_buffers(avctx);
  522. ctx->w = 0;
  523. ctx->h = 0;
  524. return ret;
  525. }
  526. }
  527. bytestream2_skip(&ctx->gb, 8);
  528. p->pict_type = AV_PICTURE_TYPE_I;
  529. p->key_frame = 1;
  530. p->color_range = AVCOL_RANGE_JPEG;
  531. ret = ff_thread_get_buffer(avctx, &frame, 0);
  532. if (ret < 0)
  533. return ret;
  534. for (i = 0; i < 3; i++) {
  535. ret = decode_plane(avctx, i, avpkt, frame.f);
  536. if (ret < 0)
  537. return ret;
  538. if (avctx->flags & AV_CODEC_FLAG_GRAY)
  539. break;
  540. }
  541. postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
  542. postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
  543. *got_frame = 1;
  544. return pktsize;
  545. }
  546. #if HAVE_THREADS
  547. static int pixlet_init_thread_copy(AVCodecContext *avctx)
  548. {
  549. PixletContext *ctx = avctx->priv_data;
  550. ctx->filter[0] = NULL;
  551. ctx->filter[1] = NULL;
  552. ctx->prediction = NULL;
  553. ctx->w = ctx->h = 0;
  554. return 0;
  555. }
  556. #endif
  557. AVCodec ff_pixlet_decoder = {
  558. .name = "pixlet",
  559. .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
  560. .type = AVMEDIA_TYPE_VIDEO,
  561. .id = AV_CODEC_ID_PIXLET,
  562. .init = pixlet_init,
  563. .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy),
  564. .close = pixlet_close,
  565. .decode = pixlet_decode_frame,
  566. .priv_data_size = sizeof(PixletContext),
  567. .capabilities = AV_CODEC_CAP_DR1 |
  568. AV_CODEC_CAP_FRAME_THREADS,
  569. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  570. FF_CODEC_CAP_INIT_CLEANUP,
  571. };