3 * Copyright (c) 2016 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/imgutils.h"
25 #include "libavutil/intmath.h"
26 #include "libavutil/opt.h"
29 #include "bytestream.h"
37 #define PIXLET_MAGIC 0xDEADBEEF
42 typedef struct SubBand {
43 unsigned width, height;
48 typedef struct PixletContext {
60 int64_t scaling[4][2][NB_LEVELS];
62 SubBand band[4][NB_LEVELS * 3 + 1];
65 static av_cold int pixlet_init(AVCodecContext *avctx)
67 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
68 avctx->color_range = AVCOL_RANGE_JPEG;
72 static void free_buffers(AVCodecContext *avctx)
74 PixletContext *ctx = avctx->priv_data;
76 av_freep(&ctx->filter[0]);
77 av_freep(&ctx->filter[1]);
78 av_freep(&ctx->prediction);
81 static av_cold int pixlet_close(AVCodecContext *avctx)
83 PixletContext *ctx = avctx->priv_data;
90 static int init_decoder(AVCodecContext *avctx)
92 PixletContext *ctx = avctx->priv_data;
95 ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
96 ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
97 ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
98 if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
99 return AVERROR(ENOMEM);
101 for (plane = 0; plane < 3; plane++) {
102 unsigned shift = plane > 0;
103 unsigned w = ctx->w >> shift;
104 unsigned h = ctx->h >> shift;
106 ctx->band[plane][0].width = w >> NB_LEVELS;
107 ctx->band[plane][0].height = h >> NB_LEVELS;
108 ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
110 for (i = 0; i < NB_LEVELS * 3; i++) {
111 unsigned scale = ctx->levels - (i / 3);
113 ctx->band[plane][i + 1].width = w >> scale;
114 ctx->band[plane][i + 1].height = h >> scale;
115 ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
117 ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
118 ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
125 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size,
126 int width, ptrdiff_t stride)
128 PixletContext *ctx = avctx->priv_data;
129 GetBitContext *bc = &ctx->bc;
130 unsigned cnt1, nbits, k, j = 0, i = 0;
131 int64_t value, state = 3;
132 int rlen, escape, flag = 0;
135 nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
137 cnt1 = get_unary(bc, 0, 8);
139 value = show_bits(bc, nbits);
141 skip_bits(bc, nbits - 1);
142 escape = ((1 << nbits) - 1) * cnt1;
144 skip_bits(bc, nbits);
145 escape = value + ((1 << nbits) - 1) * cnt1 - 1;
148 escape = get_bits(bc, 16);
151 value = -((escape + flag) & 1) | 1;
152 dst[j++] = value * ((escape + flag + 1) >> 1);
158 state = 120 * (escape + flag) + state - (120 * state >> 8);
161 if (state * 4ULL > 0xFF || i >= size)
164 nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
165 escape = av_mod_uintp2(16383, nbits);
166 cnt1 = get_unary(bc, 0, 8);
168 rlen = get_bits(bc, 16);
170 value = show_bits(bc, nbits);
172 skip_bits(bc, nbits);
173 rlen = value + escape * cnt1 - 1;
175 skip_bits(bc, nbits - 1);
176 rlen = escape * cnt1;
181 return AVERROR_INVALIDDATA;
184 for (k = 0; k < rlen; k++) {
193 flag = rlen < 0xFFFF ? 1 : 0;
197 return get_bits_count(bc) >> 3;
200 static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst,
201 int size, int c, int a, int d,
202 int width, ptrdiff_t stride)
204 PixletContext *ctx = avctx->priv_data;
205 GetBitContext *bc = &ctx->bc;
206 unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
207 int ret, escape, pfx, value, yflag, xflag, flag = 0;
208 int64_t state = 3, tmp;
210 ret = init_get_bits8(bc, src, bytestream2_get_bytes_left(&ctx->gb));
215 nbits = 33 - ff_clz(a ^ (a >> 31));
217 return AVERROR_INVALIDDATA;
225 if (((state >> 8) + 3) & 0xFFFFFFF)
226 value = ff_clz((state >> 8) + 3) ^ 0x1F;
230 cnt1 = get_unary(bc, 0, length);
231 if (cnt1 >= length) {
232 cnt1 = get_bits(bc, nbits);
234 pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
235 if (pfx < 1 || pfx > 25)
236 return AVERROR_INVALIDDATA;
237 cnt1 *= (1 << pfx) - 1;
238 shbits = show_bits(bc, pfx);
240 skip_bits(bc, pfx - 1);
250 if (flag + cnt1 == 0) {
254 tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
255 value = xflag + (tmp ^ -xflag);
264 state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);
268 if ((uint64_t)state > 0xFF / 4 || i >= size)
271 pfx = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
272 escape = av_mod_uintp2(16383, pfx);
273 cnt1 = get_unary(bc, 0, 8);
275 if (pfx < 1 || pfx > 25)
276 return AVERROR_INVALIDDATA;
278 value = show_bits(bc, pfx);
281 rlen = value + escape * cnt1 - 1;
283 skip_bits(bc, pfx - 1);
284 rlen = escape * cnt1;
288 value = get_bits(bc, 16);
290 value = get_bits(bc, 8);
292 rlen = value + 8 * escape;
295 if (rlen > 0xFFFF || i + rlen > size)
296 return AVERROR_INVALIDDATA;
299 for (k = 0; k < rlen; k++) {
308 flag = rlen < 0xFFFF ? 1 : 0;
312 return get_bits_count(bc) >> 3;
315 static int read_highpass(AVCodecContext *avctx, uint8_t *ptr,
316 int plane, AVFrame *frame)
318 PixletContext *ctx = avctx->priv_data;
319 ptrdiff_t stride = frame->linesize[plane] / 2;
322 for (i = 0; i < ctx->levels * 3; i++) {
323 int32_t a = bytestream2_get_be32(&ctx->gb);
324 int32_t b = bytestream2_get_be32(&ctx->gb);
325 int32_t c = bytestream2_get_be32(&ctx->gb);
326 int32_t d = bytestream2_get_be32(&ctx->gb);
327 int16_t *dest = (int16_t *)frame->data[plane] +
328 ctx->band[plane][i + 1].x +
329 ctx->band[plane][i + 1].y * stride;
330 unsigned size = ctx->band[plane][i + 1].size;
331 uint32_t magic = bytestream2_get_be32(&ctx->gb);
333 if (magic != PIXLET_MAGIC) {
334 av_log(avctx, AV_LOG_ERROR,
335 "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
337 return AVERROR_INVALIDDATA;
341 return AVERROR_INVALIDDATA;
343 ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
344 c, (b >= FFABS(a)) ? b : a, d,
345 ctx->band[plane][i + 1].width, stride);
347 av_log(avctx, AV_LOG_ERROR,
348 "error in highpass coefficients for plane %d, band %d\n",
352 bytestream2_skip(&ctx->gb, ret);
358 static void lowpass_prediction(int16_t *dst, int16_t *pred,
359 int width, int height, ptrdiff_t stride)
364 memset(pred, 0, width * sizeof(*pred));
366 for (i = 0; i < height; i++) {
367 val = pred[0] + dst[0];
368 dst[0] = pred[0] = val;
369 for (j = 1; j < width; j++) {
370 val = pred[j] + dst[j];
371 dst[j] = pred[j] = val;
378 static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
380 int16_t *low, *high, *ll, *lh, *hl, *hh;
386 high = &low[hsize + 8];
388 memcpy(low, dest, size);
389 memcpy(high, dest + hsize, size);
395 for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
396 low[i - 5] = low[j - 1];
398 high[i - 5] = high[j - 2];
402 for (i = 0; i < hsize; i++) {
403 value = (int64_t) low [i + 1] * -INT64_C(325392907) +
404 (int64_t) low [i + 0] * INT64_C(3687786320) +
405 (int64_t) low [i - 1] * -INT64_C(325392907) +
406 (int64_t) high[i + 0] * INT64_C(1518500249) +
407 (int64_t) high[i - 1] * INT64_C(1518500249);
408 dest[i * 2] = av_clip_int16(((value >> 32) * scale) >> 32);
411 for (i = 0; i < hsize; i++) {
412 value = (int64_t) low [i + 2] * -INT64_C(65078576) +
413 (int64_t) low [i + 1] * INT64_C(1583578880) +
414 (int64_t) low [i + 0] * INT64_C(1583578880) +
415 (int64_t) low [i - 1] * -INT64_C(65078576) +
416 (int64_t) high[i + 1] * INT64_C(303700064) +
417 (int64_t) high[i + 0] * -INT64_C(3644400640) +
418 (int64_t) high[i - 1] * INT64_C(303700064);
419 dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
423 static void reconstruction(AVCodecContext *avctx, int16_t *dest,
424 unsigned width, unsigned height, ptrdiff_t stride,
425 int64_t *scaling_h, int64_t *scaling_v)
427 PixletContext *ctx = avctx->priv_data;
428 unsigned scaled_width, scaled_height;
432 scaled_width = width >> NB_LEVELS;
433 scaled_height = height >> NB_LEVELS;
434 tmp = ctx->filter[0];
436 for (i = 0; i < NB_LEVELS; i++) {
437 int64_t scale_v = scaling_v[i];
438 int64_t scale_h = scaling_h[i];
443 for (j = 0; j < scaled_height; j++) {
444 filterfn(ptr, ctx->filter[1], scaled_width, scale_v);
448 for (j = 0; j < scaled_width; j++) {
450 for (k = 0; k < scaled_height; k++) {
455 filterfn(tmp, ctx->filter[1], scaled_height, scale_h);
458 for (k = 0; k < scaled_height; k++) {
466 static void build_luma_lut(AVCodecContext *avctx, int depth)
468 PixletContext *ctx = avctx->priv_data;
469 int max = (1 << depth) - 1;
471 if (ctx->depth == depth)
475 for (int i = 0; i < FF_ARRAY_ELEMS(ctx->lut); i++)
476 ctx->lut[i] = ((int64_t)i * i * 65535LL) / max / max;
479 static void postprocess_luma(AVCodecContext *avctx, AVFrame *frame,
480 int w, int h, int depth)
482 PixletContext *ctx = avctx->priv_data;
483 uint16_t *dsty = (uint16_t *)frame->data[0];
484 int16_t *srcy = (int16_t *)frame->data[0];
485 ptrdiff_t stridey = frame->linesize[0] / 2;
486 uint16_t *lut = ctx->lut;
489 for (j = 0; j < h; j++) {
490 for (i = 0; i < w; i++) {
493 else if (srcy[i] > ((1 << depth) - 1))
496 dsty[i] = lut[srcy[i]];
503 static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
505 uint16_t *dstu = (uint16_t *)frame->data[1];
506 uint16_t *dstv = (uint16_t *)frame->data[2];
507 int16_t *srcu = (int16_t *)frame->data[1];
508 int16_t *srcv = (int16_t *)frame->data[2];
509 ptrdiff_t strideu = frame->linesize[1] / 2;
510 ptrdiff_t stridev = frame->linesize[2] / 2;
511 const unsigned add = 1 << (depth - 1);
512 const unsigned shift = 16 - depth;
515 for (j = 0; j < h; j++) {
516 for (i = 0; i < w; i++) {
517 dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
518 dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
527 static int decode_plane(AVCodecContext *avctx, int plane,
528 AVPacket *avpkt, AVFrame *frame)
530 PixletContext *ctx = avctx->priv_data;
531 ptrdiff_t stride = frame->linesize[plane] / 2;
532 unsigned shift = plane > 0;
536 for (i = ctx->levels - 1; i >= 0; i--) {
537 int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
538 int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
541 return AVERROR_INVALIDDATA;
543 ctx->scaling[plane][H][i] = (1000000ULL << 32) / h;
544 ctx->scaling[plane][V][i] = (1000000ULL << 32) / v;
547 bytestream2_skip(&ctx->gb, 4);
549 dst = (int16_t *)frame->data[plane];
550 dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
552 ret = init_get_bits8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),
553 bytestream2_get_bytes_left(&ctx->gb));
557 ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
558 ctx->band[plane][0].width - 1, 0);
560 av_log(avctx, AV_LOG_ERROR,
561 "error in lowpass coefficients for plane %d, top row\n", plane);
565 ret = read_low_coeffs(avctx, dst + stride,
566 ctx->band[plane][0].height - 1, 1, stride);
568 av_log(avctx, AV_LOG_ERROR,
569 "error in lowpass coefficients for plane %d, left column\n",
574 ret = read_low_coeffs(avctx, dst + stride + 1,
575 (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
576 ctx->band[plane][0].width - 1, stride);
578 av_log(avctx, AV_LOG_ERROR,
579 "error in lowpass coefficients for plane %d, rest\n", plane);
583 bytestream2_skip(&ctx->gb, ret);
584 if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
585 av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
586 return AVERROR_INVALIDDATA;
589 ret = read_highpass(avctx, avpkt->data, plane, frame);
593 lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
594 ctx->band[plane][0].height, stride);
596 reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
597 ctx->h >> shift, stride, ctx->scaling[plane][H],
598 ctx->scaling[plane][V]);
603 static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
604 int *got_frame, AVPacket *avpkt)
606 PixletContext *ctx = avctx->priv_data;
607 int i, w, h, width, height, ret, version;
609 ThreadFrame frame = { .f = data };
610 uint32_t pktsize, depth;
612 bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
614 pktsize = bytestream2_get_be32(&ctx->gb);
615 if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
616 av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);
617 return AVERROR_INVALIDDATA;
620 version = bytestream2_get_le32(&ctx->gb);
622 avpriv_request_sample(avctx, "Version %d", version);
624 bytestream2_skip(&ctx->gb, 4);
625 if (bytestream2_get_be32(&ctx->gb) != 1)
626 return AVERROR_INVALIDDATA;
627 bytestream2_skip(&ctx->gb, 4);
629 width = bytestream2_get_be32(&ctx->gb);
630 height = bytestream2_get_be32(&ctx->gb);
632 if ( width > INT_MAX - (1U << (NB_LEVELS + 1))
633 || height > INT_MAX - (1U << (NB_LEVELS + 1)))
634 return AVERROR_INVALIDDATA;
636 w = FFALIGN(width, 1 << (NB_LEVELS + 1));
637 h = FFALIGN(height, 1 << (NB_LEVELS + 1));
639 ctx->levels = bytestream2_get_be32(&ctx->gb);
640 if (ctx->levels != NB_LEVELS)
641 return AVERROR_INVALIDDATA;
642 depth = bytestream2_get_be32(&ctx->gb);
643 if (depth < 8 || depth > 15) {
644 avpriv_request_sample(avctx, "Depth %d", depth);
645 return AVERROR_INVALIDDATA;
648 build_luma_lut(avctx, depth);
650 ret = ff_set_dimensions(avctx, w, h);
653 avctx->width = width;
654 avctx->height = height;
656 if (ctx->w != w || ctx->h != h) {
661 ret = init_decoder(avctx);
670 bytestream2_skip(&ctx->gb, 8);
672 p->pict_type = AV_PICTURE_TYPE_I;
674 p->color_range = AVCOL_RANGE_JPEG;
676 ret = ff_thread_get_buffer(avctx, &frame, 0);
680 for (i = 0; i < 3; i++) {
681 ret = decode_plane(avctx, i, avpkt, frame.f);
684 if (avctx->flags & AV_CODEC_FLAG_GRAY)
688 postprocess_luma(avctx, frame.f, ctx->w, ctx->h, ctx->depth);
689 postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
696 AVCodec ff_pixlet_decoder = {
698 .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
699 .type = AVMEDIA_TYPE_VIDEO,
700 .id = AV_CODEC_ID_PIXLET,
702 .close = pixlet_close,
703 .decode = pixlet_decode_frame,
704 .priv_data_size = sizeof(PixletContext),
705 .capabilities = AV_CODEC_CAP_DR1 |
706 AV_CODEC_CAP_FRAME_THREADS,
707 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
708 FF_CODEC_CAP_INIT_CLEANUP,