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"
40 typedef struct SubBand {
41 unsigned width, height;
46 typedef struct PixletContext {
58 float scaling[4][2][NB_LEVELS];
59 SubBand band[4][NB_LEVELS * 3 + 1];
62 static int init_decoder(AVCodecContext *avctx)
64 PixletContext *ctx = avctx->priv_data;
67 ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
68 ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
69 ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
70 if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
71 return AVERROR(ENOMEM);
73 for (plane = 0; plane < 3; plane++) {
74 unsigned shift = plane > 0;
75 unsigned w = ctx->w >> shift;
76 unsigned h = ctx->h >> shift;
78 ctx->band[plane][0].width = w >> NB_LEVELS;
79 ctx->band[plane][0].height = h >> NB_LEVELS;
80 ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
82 for (i = 0; i < NB_LEVELS * 3; i++) {
83 unsigned scale = ctx->levels - (i / 3);
85 ctx->band[plane][i + 1].width = w >> scale;
86 ctx->band[plane][i + 1].height = h >> scale;
87 ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
89 ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
90 ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
97 static void free_buffers(AVCodecContext *avctx)
99 PixletContext *ctx = avctx->priv_data;
101 av_freep(&ctx->filter[0]);
102 av_freep(&ctx->filter[1]);
103 av_freep(&ctx->prediction);
106 static av_cold int pixlet_close(AVCodecContext *avctx)
108 PixletContext *ctx = avctx->priv_data;
115 static av_cold int pixlet_init(AVCodecContext *avctx)
117 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
118 avctx->color_range = AVCOL_RANGE_JPEG;
122 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int width, ptrdiff_t stride)
124 PixletContext *ctx = avctx->priv_data;
125 GetBitContext *b = &ctx->gbit;
126 unsigned cnt1, nbits, k, j = 0, i = 0;
127 int64_t value, state = 3;
128 int rlen, escape, flag = 0;
131 nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
133 cnt1 = get_unary(b, 0, 8);
135 value = show_bits(b, nbits);
137 skip_bits(b, nbits - 1);
138 escape = ((1 << nbits) - 1) * cnt1;
141 escape = value + ((1 << nbits) - 1) * cnt1 - 1;
144 escape = get_bits(b, 16);
147 value = -((escape + flag) & 1) | 1;
148 dst[j++] = value * ((escape + flag + 1) >> 1);
154 state = 120 * (escape + flag) + state - (120 * state >> 8);
157 if (state * 4 > 0xFF || i >= size)
160 nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
161 escape = av_mod_uintp2(16383, nbits);
162 cnt1 = get_unary(b, 0, 8);
164 rlen = get_bits(b, 16);
166 value = show_bits(b, nbits);
169 rlen = value + escape * cnt1 - 1;
171 skip_bits(b, nbits - 1);
172 rlen = escape * cnt1;
177 return AVERROR_INVALIDDATA;
180 for (k = 0; k < rlen; k++) {
189 flag = rlen < 0xFFFF ? 1 : 0;
193 return get_bits_count(b) >> 3;
196 static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, int size,
198 int width, ptrdiff_t stride)
200 PixletContext *ctx = avctx->priv_data;
201 GetBitContext *b = &ctx->gbit;
202 unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
203 int ret, escape, pfx, value, yflag, xflag, flag = 0;
204 int64_t state = 3, tmp;
206 if ((ret = init_get_bits8(b, src, bytestream2_get_bytes_left(&ctx->gb))) < 0)
209 if ((a >= 0) + (a ^ (a >> 31)) - (a >> 31) != 1) {
210 nbits = 33 - ff_clz((a >= 0) + (a ^ (a >> 31)) - (a >> 31) - 1);
212 return AVERROR_INVALIDDATA;
220 if (state >> 8 != -3) {
221 value = ff_clz((state >> 8) + 3) ^ 0x1F;
226 cnt1 = get_unary(b, 0, length);
228 if (cnt1 >= length) {
229 cnt1 = get_bits(b, nbits);
231 pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
232 cnt1 *= (1 << pfx) - 1;
233 shbits = show_bits(b, pfx);
235 skip_bits(b, pfx - 1);
245 if (flag + cnt1 == 0) {
249 tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
250 value = xflag + (tmp ^ -xflag);
259 state += (int64_t)d * yflag - (d * state >> 8);
263 if (state * 4 > 0xFF || i >= size)
266 pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24;
267 escape = av_mod_uintp2(16383, pfx);
268 cnt1 = get_unary(b, 0, 8);
270 if (pfx < 1 || pfx > 25)
271 return AVERROR_INVALIDDATA;
272 value = show_bits(b, pfx);
275 rlen = value + escape * cnt1 - 1;
277 skip_bits(b, pfx - 1);
278 rlen = escape * cnt1;
282 value = get_bits(b, 16);
284 value = get_bits(b, 8);
286 rlen = value + 8 * escape;
289 if (rlen > 0xFFFF || i + rlen > size)
290 return AVERROR_INVALIDDATA;
293 for (k = 0; k < rlen; k++) {
302 flag = rlen < 0xFFFF ? 1 : 0;
306 return get_bits_count(b) >> 3;
309 static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame *frame)
311 PixletContext *ctx = avctx->priv_data;
312 ptrdiff_t stride = frame->linesize[plane] / 2;
315 for (i = 0; i < ctx->levels * 3; i++) {
316 int32_t a = bytestream2_get_be32(&ctx->gb);
317 int32_t b = bytestream2_get_be32(&ctx->gb);
318 int32_t c = bytestream2_get_be32(&ctx->gb);
319 int32_t d = bytestream2_get_be32(&ctx->gb);
320 int16_t *dest = (int16_t *)frame->data[plane] + ctx->band[plane][i + 1].x +
321 stride * ctx->band[plane][i + 1].y;
322 unsigned size = ctx->band[plane][i + 1].size;
325 magic = bytestream2_get_be32(&ctx->gb);
326 if (magic != 0xDEADBEEF) {
327 av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%08X for plane %d, band %d\n", magic, plane, i);
328 return AVERROR_INVALIDDATA;
331 ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
332 c, (b >= FFABS(a)) ? b : a, d,
333 ctx->band[plane][i + 1].width, stride);
335 av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, band %d\n", plane, i);
338 bytestream2_skip(&ctx->gb, ret);
344 static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height, ptrdiff_t stride)
349 memset(pred, 0, width * sizeof(*pred));
351 for (i = 0; i < height; i++) {
352 val = pred[0] + dst[0];
353 dst[0] = pred[0] = val;
354 for (j = 1; j < width; j++) {
355 val = pred[j] + dst[j];
356 dst[j] = pred[j] = val;
363 static void filter(int16_t *dest, int16_t *tmp, unsigned size, float SCALE)
365 int16_t *low, *high, *ll, *lh, *hl, *hh;
371 high = &low[hsize + 8];
373 memcpy(low, dest, size);
374 memcpy(high, dest + hsize, size);
380 for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
381 low[i - 5] = low[j - 1];
383 high[i - 5] = high[j - 2];
387 for (i = 0; i < hsize; i++) {
388 value = low [i+1] * -0.07576144003329376f +
389 low [i ] * 0.8586296626673486f +
390 low [i-1] * -0.07576144003329376f +
391 high[i ] * 0.3535533905932737f +
392 high[i-1] * 0.3535533905932737f;
393 dest[i * 2] = av_clipf(value * SCALE, INT16_MIN, INT16_MAX);
396 for (i = 0; i < hsize; i++) {
397 value = low [i+2] * -0.01515228715813062f +
398 low [i+1] * 0.3687056777514043f +
399 low [i ] * 0.3687056777514043f +
400 low [i-1] * -0.01515228715813062f +
401 high[i+1] * 0.07071067811865475f +
402 high[i ] * -0.8485281374238569f +
403 high[i-1] * 0.07071067811865475f;
404 dest[i * 2 + 1] = av_clipf(value * SCALE, INT16_MIN, INT16_MAX);
408 static void reconstruction(AVCodecContext *avctx,
409 int16_t *dest, unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,
410 float *scaling_H, float *scaling_V)
412 PixletContext *ctx = avctx->priv_data;
413 unsigned scaled_width, scaled_height;
414 float scale_H, scale_V;
418 scaled_height = height >> nb_levels;
419 scaled_width = width >> nb_levels;
420 tmp = ctx->filter[0];
422 for (i = 0; i < nb_levels; i++) {
425 scale_H = scaling_H[i];
426 scale_V = scaling_V[i];
429 for (j = 0; j < scaled_height; j++) {
430 filter(ptr, ctx->filter[1], scaled_width, scale_V);
434 for (j = 0; j < scaled_width; j++) {
436 for (k = 0; k < scaled_height; k++) {
441 filter(tmp, ctx->filter[1], scaled_height, scale_H);
444 for (k = 0; k < scaled_height; k++) {
452 #define SQR(a) ((a) * (a))
454 static void postprocess_luma(AVFrame *frame, int w, int h, int depth)
456 uint16_t *dsty = (uint16_t *)frame->data[0];
457 int16_t *srcy = (int16_t *)frame->data[0];
458 ptrdiff_t stridey = frame->linesize[0] / 2;
459 const float factor = 1.0f / ((1 << depth) - 1);
462 for (j = 0; j < h; j++) {
463 for (i = 0; i < w; i++) {
464 dsty[i] = SQR(FFMAX(srcy[i], 0) * factor) * 65535;
471 static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
473 uint16_t *dstu = (uint16_t *)frame->data[1];
474 uint16_t *dstv = (uint16_t *)frame->data[2];
475 int16_t *srcu = (int16_t *)frame->data[1];
476 int16_t *srcv = (int16_t *)frame->data[2];
477 ptrdiff_t strideu = frame->linesize[1] / 2;
478 ptrdiff_t stridev = frame->linesize[2] / 2;
479 const unsigned add = 1 << (depth - 1);
480 const unsigned shift = 16 - depth;
483 for (j = 0; j < h; j++) {
484 for (i = 0; i < w; i++) {
485 dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
486 dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
495 static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame)
497 PixletContext *ctx = avctx->priv_data;
498 ptrdiff_t stride = frame->linesize[plane] / 2;
499 unsigned shift = plane > 0;
503 for (i = ctx->levels - 1; i >= 0; i--) {
504 int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
505 int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
508 return AVERROR_INVALIDDATA;
510 ctx->scaling[plane][H][i] = 1000000.0f / h;
511 ctx->scaling[plane][V][i] = 1000000.0f / v;
514 bytestream2_skip(&ctx->gb, 4);
516 dst = (int16_t *)frame->data[plane];
517 dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
519 if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gb),
520 bytestream2_get_bytes_left(&ctx->gb))) < 0)
523 ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1, ctx->band[plane][0].width - 1, 0);
525 av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane);
529 ret = read_low_coeffs(avctx, dst + stride, ctx->band[plane][0].height - 1, 1, stride);
531 av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane);
535 ret = read_low_coeffs(avctx, dst + stride + 1,
536 (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
537 ctx->band[plane][0].width - 1, stride);
539 av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane);
543 bytestream2_skip(&ctx->gb, ret);
544 if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
545 av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
546 return AVERROR_INVALIDDATA;
549 ret = read_highpass(avctx, avpkt->data, plane, frame);
553 lowpass_prediction(dst, ctx->prediction,
554 ctx->band[plane][0].width, ctx->band[plane][0].height, stride);
556 reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift, ctx->h >> shift,
557 stride, NB_LEVELS, ctx->scaling[plane][H], ctx->scaling[plane][V]);
562 static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
563 int *got_frame, AVPacket *avpkt)
565 PixletContext *ctx = avctx->priv_data;
566 int i, w, h, width, height, ret, version;
568 ThreadFrame frame = { .f = data };
571 bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
573 pktsize = bytestream2_get_be32(&ctx->gb);
574 if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
575 av_log(avctx, AV_LOG_ERROR, "Invalid packet size %u.\n", pktsize);
576 return AVERROR_INVALIDDATA;
579 version = bytestream2_get_le32(&ctx->gb);
581 avpriv_request_sample(avctx, "Version %d", version);
583 bytestream2_skip(&ctx->gb, 4);
584 if (bytestream2_get_be32(&ctx->gb) != 1)
585 return AVERROR_INVALIDDATA;
586 bytestream2_skip(&ctx->gb, 4);
588 width = bytestream2_get_be32(&ctx->gb);
589 height = bytestream2_get_be32(&ctx->gb);
591 w = FFALIGN(width, 1 << (NB_LEVELS + 1));
592 h = FFALIGN(height, 1 << (NB_LEVELS + 1));
594 ctx->levels = bytestream2_get_be32(&ctx->gb);
595 if (ctx->levels != NB_LEVELS)
596 return AVERROR_INVALIDDATA;
597 ctx->depth = bytestream2_get_be32(&ctx->gb);
598 if (ctx->depth < 8 || ctx->depth > 15) {
599 avpriv_request_sample(avctx, "Depth %d", ctx->depth);
600 return AVERROR_INVALIDDATA;
603 ret = ff_set_dimensions(avctx, w, h);
606 avctx->width = width;
607 avctx->height = height;
609 if (ctx->w != w || ctx->h != h) {
614 ret = init_decoder(avctx);
623 bytestream2_skip(&ctx->gb, 8);
625 p->pict_type = AV_PICTURE_TYPE_I;
627 p->color_range = AVCOL_RANGE_JPEG;
629 ret = ff_thread_get_buffer(avctx, &frame, 0);
633 for (i = 0; i < 3; i++) {
634 ret = decode_plane(avctx, i, avpkt, frame.f);
637 if (avctx->flags & AV_CODEC_FLAG_GRAY)
641 postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
642 postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
650 static int pixlet_init_thread_copy(AVCodecContext *avctx)
652 PixletContext *ctx = avctx->priv_data;
654 ctx->filter[0] = NULL;
655 ctx->filter[1] = NULL;
656 ctx->prediction = NULL;
663 AVCodec ff_pixlet_decoder = {
665 .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_PIXLET,
669 .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy),
670 .close = pixlet_close,
671 .decode = pixlet_decode_frame,
672 .priv_data_size = sizeof(PixletContext),
673 .capabilities = AV_CODEC_CAP_DR1 |
674 AV_CODEC_CAP_FRAME_THREADS,
675 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
676 FF_CODEC_CAP_INIT_CLEANUP,