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];
61 SubBand band[4][NB_LEVELS * 3 + 1];
64 static av_cold int pixlet_init(AVCodecContext *avctx)
66 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
67 avctx->color_range = AVCOL_RANGE_JPEG;
71 static void free_buffers(AVCodecContext *avctx)
73 PixletContext *ctx = avctx->priv_data;
75 av_freep(&ctx->filter[0]);
76 av_freep(&ctx->filter[1]);
77 av_freep(&ctx->prediction);
80 static av_cold int pixlet_close(AVCodecContext *avctx)
82 PixletContext *ctx = avctx->priv_data;
89 static int init_decoder(AVCodecContext *avctx)
91 PixletContext *ctx = avctx->priv_data;
94 ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
95 ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
96 ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
97 if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
98 return AVERROR(ENOMEM);
100 for (plane = 0; plane < 3; plane++) {
101 unsigned shift = plane > 0;
102 unsigned w = ctx->w >> shift;
103 unsigned h = ctx->h >> shift;
105 ctx->band[plane][0].width = w >> NB_LEVELS;
106 ctx->band[plane][0].height = h >> NB_LEVELS;
107 ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
109 for (i = 0; i < NB_LEVELS * 3; i++) {
110 unsigned scale = ctx->levels - (i / 3);
112 ctx->band[plane][i + 1].width = w >> scale;
113 ctx->band[plane][i + 1].height = h >> scale;
114 ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
116 ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
117 ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
124 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size,
125 int width, ptrdiff_t stride)
127 PixletContext *ctx = avctx->priv_data;
128 GetBitContext *bc = &ctx->bc;
129 unsigned cnt1, nbits, k, j = 0, i = 0;
130 int64_t value, state = 3;
131 int rlen, escape, flag = 0;
134 nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
136 cnt1 = get_unary(bc, 0, 8);
138 value = show_bits(bc, nbits);
140 skip_bits(bc, nbits - 1);
141 escape = ((1 << nbits) - 1) * cnt1;
143 skip_bits(bc, nbits);
144 escape = value + ((1 << nbits) - 1) * cnt1 - 1;
147 escape = get_bits(bc, 16);
150 value = -((escape + flag) & 1) | 1;
151 dst[j++] = value * ((escape + flag + 1) >> 1);
157 state = 120 * (escape + flag) + state - (120 * state >> 8);
160 if (state * 4ULL > 0xFF || i >= size)
163 nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
164 escape = av_mod_uintp2(16383, nbits);
165 cnt1 = get_unary(bc, 0, 8);
167 rlen = get_bits(bc, 16);
169 value = show_bits(bc, nbits);
171 skip_bits(bc, nbits);
172 rlen = value + escape * cnt1 - 1;
174 skip_bits(bc, nbits - 1);
175 rlen = escape * cnt1;
180 return AVERROR_INVALIDDATA;
183 for (k = 0; k < rlen; k++) {
192 flag = rlen < 0xFFFF ? 1 : 0;
196 return get_bits_count(bc) >> 3;
199 static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst,
200 int size, int c, int a, int d,
201 int width, ptrdiff_t stride)
203 PixletContext *ctx = avctx->priv_data;
204 GetBitContext *bc = &ctx->bc;
205 unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
206 int ret, escape, pfx, value, yflag, xflag, flag = 0;
207 int64_t state = 3, tmp;
209 ret = init_get_bits8(bc, src, bytestream2_get_bytes_left(&ctx->gb));
214 nbits = 33 - ff_clz(a ^ (a >> 31));
216 return AVERROR_INVALIDDATA;
224 if (state >> 8 != -3)
225 value = ff_clz((state >> 8) + 3) ^ 0x1F;
229 cnt1 = get_unary(bc, 0, length);
230 if (cnt1 >= length) {
231 cnt1 = get_bits(bc, nbits);
233 pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
234 if (pfx < 1 || pfx > 25)
235 return AVERROR_INVALIDDATA;
236 cnt1 *= (1 << pfx) - 1;
237 shbits = show_bits(bc, pfx);
239 skip_bits(bc, pfx - 1);
249 if (flag + cnt1 == 0) {
253 tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
254 value = xflag + (tmp ^ -xflag);
263 state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);
267 if ((uint64_t)state > 0xFF / 4 || i >= size)
270 pfx = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
271 escape = av_mod_uintp2(16383, pfx);
272 cnt1 = get_unary(bc, 0, 8);
274 if (pfx < 1 || pfx > 25)
275 return AVERROR_INVALIDDATA;
277 value = show_bits(bc, pfx);
280 rlen = value + escape * cnt1 - 1;
282 skip_bits(bc, pfx - 1);
283 rlen = escape * cnt1;
287 value = get_bits(bc, 16);
289 value = get_bits(bc, 8);
291 rlen = value + 8 * escape;
294 if (rlen > 0xFFFF || i + rlen > size)
295 return AVERROR_INVALIDDATA;
298 for (k = 0; k < rlen; k++) {
307 flag = rlen < 0xFFFF ? 1 : 0;
311 return get_bits_count(bc) >> 3;
314 static int read_highpass(AVCodecContext *avctx, uint8_t *ptr,
315 int plane, AVFrame *frame)
317 PixletContext *ctx = avctx->priv_data;
318 ptrdiff_t stride = frame->linesize[plane] / 2;
321 for (i = 0; i < ctx->levels * 3; i++) {
322 int32_t a = bytestream2_get_be32(&ctx->gb);
323 int32_t b = bytestream2_get_be32(&ctx->gb);
324 int32_t c = bytestream2_get_be32(&ctx->gb);
325 int32_t d = bytestream2_get_be32(&ctx->gb);
326 int16_t *dest = (int16_t *)frame->data[plane] +
327 ctx->band[plane][i + 1].x +
328 ctx->band[plane][i + 1].y * stride;
329 unsigned size = ctx->band[plane][i + 1].size;
330 uint32_t magic = bytestream2_get_be32(&ctx->gb);
332 if (magic != PIXLET_MAGIC) {
333 av_log(avctx, AV_LOG_ERROR,
334 "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
336 return AVERROR_INVALIDDATA;
340 return AVERROR_INVALIDDATA;
342 ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
343 c, (b >= FFABS(a)) ? b : a, d,
344 ctx->band[plane][i + 1].width, stride);
346 av_log(avctx, AV_LOG_ERROR,
347 "error in highpass coefficients for plane %d, band %d\n",
351 bytestream2_skip(&ctx->gb, ret);
357 static void lowpass_prediction(int16_t *dst, int16_t *pred,
358 int width, int height, ptrdiff_t stride)
363 memset(pred, 0, width * sizeof(*pred));
365 for (i = 0; i < height; i++) {
366 val = pred[0] + dst[0];
367 dst[0] = pred[0] = val;
368 for (j = 1; j < width; j++) {
369 val = pred[j] + dst[j];
370 dst[j] = pred[j] = val;
377 static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
379 int16_t *low, *high, *ll, *lh, *hl, *hh;
385 high = &low[hsize + 8];
387 memcpy(low, dest, size);
388 memcpy(high, dest + hsize, size);
394 for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
395 low[i - 5] = low[j - 1];
397 high[i - 5] = high[j - 2];
401 for (i = 0; i < hsize; i++) {
402 value = (int64_t) low [i + 1] * -INT64_C(325392907) +
403 (int64_t) low [i + 0] * INT64_C(3687786320) +
404 (int64_t) low [i - 1] * -INT64_C(325392907) +
405 (int64_t) high[i + 0] * INT64_C(1518500249) +
406 (int64_t) high[i - 1] * INT64_C(1518500249);
407 dest[i * 2] = av_clip_int16(((value >> 32) * scale) >> 32);
410 for (i = 0; i < hsize; i++) {
411 value = (int64_t) low [i + 2] * -INT64_C(65078576) +
412 (int64_t) low [i + 1] * INT64_C(1583578880) +
413 (int64_t) low [i + 0] * INT64_C(1583578880) +
414 (int64_t) low [i - 1] * -INT64_C(65078576) +
415 (int64_t) high[i + 1] * INT64_C(303700064) +
416 (int64_t) high[i + 0] * -INT64_C(3644400640) +
417 (int64_t) high[i - 1] * INT64_C(303700064);
418 dest[i * 2 + 1] = av_clip_int16(((value >> 32) * scale) >> 32);
422 static void reconstruction(AVCodecContext *avctx, int16_t *dest,
423 unsigned width, unsigned height, ptrdiff_t stride,
424 int64_t *scaling_h, int64_t *scaling_v)
426 PixletContext *ctx = avctx->priv_data;
427 unsigned scaled_width, scaled_height;
431 scaled_width = width >> NB_LEVELS;
432 scaled_height = height >> NB_LEVELS;
433 tmp = ctx->filter[0];
435 for (i = 0; i < NB_LEVELS; i++) {
436 int64_t scale_v = scaling_v[i];
437 int64_t scale_h = scaling_h[i];
442 for (j = 0; j < scaled_height; j++) {
443 filterfn(ptr, ctx->filter[1], scaled_width, scale_v);
447 for (j = 0; j < scaled_width; j++) {
449 for (k = 0; k < scaled_height; k++) {
454 filterfn(tmp, ctx->filter[1], scaled_height, scale_h);
457 for (k = 0; k < scaled_height; k++) {
465 static void postprocess_luma(AVFrame *frame, int w, int h, int depth)
467 uint16_t *dsty = (uint16_t *)frame->data[0];
468 int16_t *srcy = (int16_t *)frame->data[0];
469 ptrdiff_t stridey = frame->linesize[0] / 2;
472 for (j = 0; j < h; j++) {
473 for (i = 0; i < w; i++) {
476 else if (srcy[i] > ((1 << depth) - 1))
479 dsty[i] = ((int64_t) srcy[i] * srcy[i] * 65535) /
480 ((1 << depth) - 1) / ((1 << depth) - 1);
487 static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
489 uint16_t *dstu = (uint16_t *)frame->data[1];
490 uint16_t *dstv = (uint16_t *)frame->data[2];
491 int16_t *srcu = (int16_t *)frame->data[1];
492 int16_t *srcv = (int16_t *)frame->data[2];
493 ptrdiff_t strideu = frame->linesize[1] / 2;
494 ptrdiff_t stridev = frame->linesize[2] / 2;
495 const unsigned add = 1 << (depth - 1);
496 const unsigned shift = 16 - depth;
499 for (j = 0; j < h; j++) {
500 for (i = 0; i < w; i++) {
501 dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
502 dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
511 static int decode_plane(AVCodecContext *avctx, int plane,
512 AVPacket *avpkt, AVFrame *frame)
514 PixletContext *ctx = avctx->priv_data;
515 ptrdiff_t stride = frame->linesize[plane] / 2;
516 unsigned shift = plane > 0;
520 for (i = ctx->levels - 1; i >= 0; i--) {
521 int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
522 int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
525 return AVERROR_INVALIDDATA;
527 ctx->scaling[plane][H][i] = (1000000ULL << 32) / h;
528 ctx->scaling[plane][V][i] = (1000000ULL << 32) / v;
531 bytestream2_skip(&ctx->gb, 4);
533 dst = (int16_t *)frame->data[plane];
534 dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
536 ret = init_get_bits8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),
537 bytestream2_get_bytes_left(&ctx->gb));
541 ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
542 ctx->band[plane][0].width - 1, 0);
544 av_log(avctx, AV_LOG_ERROR,
545 "error in lowpass coefficients for plane %d, top row\n", plane);
549 ret = read_low_coeffs(avctx, dst + stride,
550 ctx->band[plane][0].height - 1, 1, stride);
552 av_log(avctx, AV_LOG_ERROR,
553 "error in lowpass coefficients for plane %d, left column\n",
558 ret = read_low_coeffs(avctx, dst + stride + 1,
559 (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
560 ctx->band[plane][0].width - 1, stride);
562 av_log(avctx, AV_LOG_ERROR,
563 "error in lowpass coefficients for plane %d, rest\n", plane);
567 bytestream2_skip(&ctx->gb, ret);
568 if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
569 av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
570 return AVERROR_INVALIDDATA;
573 ret = read_highpass(avctx, avpkt->data, plane, frame);
577 lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
578 ctx->band[plane][0].height, stride);
580 reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
581 ctx->h >> shift, stride, ctx->scaling[plane][H],
582 ctx->scaling[plane][V]);
587 static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
588 int *got_frame, AVPacket *avpkt)
590 PixletContext *ctx = avctx->priv_data;
591 int i, w, h, width, height, ret, version;
593 ThreadFrame frame = { .f = data };
596 bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
598 pktsize = bytestream2_get_be32(&ctx->gb);
599 if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
600 av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);
601 return AVERROR_INVALIDDATA;
604 version = bytestream2_get_le32(&ctx->gb);
606 avpriv_request_sample(avctx, "Version %d", version);
608 bytestream2_skip(&ctx->gb, 4);
609 if (bytestream2_get_be32(&ctx->gb) != 1)
610 return AVERROR_INVALIDDATA;
611 bytestream2_skip(&ctx->gb, 4);
613 width = bytestream2_get_be32(&ctx->gb);
614 height = bytestream2_get_be32(&ctx->gb);
616 if ( width > INT_MAX - (1U << (NB_LEVELS + 1))
617 || height > INT_MAX - (1U << (NB_LEVELS + 1)))
618 return AVERROR_INVALIDDATA;
620 w = FFALIGN(width, 1 << (NB_LEVELS + 1));
621 h = FFALIGN(height, 1 << (NB_LEVELS + 1));
623 ctx->levels = bytestream2_get_be32(&ctx->gb);
624 if (ctx->levels != NB_LEVELS)
625 return AVERROR_INVALIDDATA;
626 ctx->depth = bytestream2_get_be32(&ctx->gb);
627 if (ctx->depth < 8 || ctx->depth > 15) {
628 avpriv_request_sample(avctx, "Depth %d", ctx->depth);
629 return AVERROR_INVALIDDATA;
632 ret = ff_set_dimensions(avctx, w, h);
635 avctx->width = width;
636 avctx->height = height;
638 if (ctx->w != w || ctx->h != h) {
643 ret = init_decoder(avctx);
652 bytestream2_skip(&ctx->gb, 8);
654 p->pict_type = AV_PICTURE_TYPE_I;
656 p->color_range = AVCOL_RANGE_JPEG;
658 ret = ff_thread_get_buffer(avctx, &frame, 0);
662 for (i = 0; i < 3; i++) {
663 ret = decode_plane(avctx, i, avpkt, frame.f);
666 if (avctx->flags & AV_CODEC_FLAG_GRAY)
670 postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
671 postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
679 static int pixlet_init_thread_copy(AVCodecContext *avctx)
681 PixletContext *ctx = avctx->priv_data;
683 ctx->filter[0] = NULL;
684 ctx->filter[1] = NULL;
685 ctx->prediction = NULL;
691 #endif /* HAVE_THREADS */
693 AVCodec ff_pixlet_decoder = {
695 .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
696 .type = AVMEDIA_TYPE_VIDEO,
697 .id = AV_CODEC_ID_PIXLET,
699 .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy),
700 .close = pixlet_close,
701 .decode = pixlet_decode_frame,
702 .priv_data_size = sizeof(PixletContext),
703 .capabilities = AV_CODEC_CAP_DR1 |
704 AV_CODEC_CAP_FRAME_THREADS,
705 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
706 FF_CODEC_CAP_INIT_CLEANUP,