Browse code

avcodec: add Apple Pixlet decoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/12/03 04:30:50
Showing 9 changed files
... ...
@@ -11,6 +11,7 @@ version <next>:
11 11
 - PSD Decoder
12 12
 - 16.8 floating point pcm decoder
13 13
 - 24.0 floating point pcm decoder
14
+- Apple Pixlet decoder
14 15
 
15 16
 version 3.2:
16 17
 - libopenmpt demuxer
... ...
@@ -631,6 +631,7 @@ following image formats are supported:
631 631
 @item ANSI/ASCII art         @tab     @tab  X
632 632
 @item Apple Intermediate Codec @tab     @tab  X
633 633
 @item Apple MJPEG-B          @tab     @tab  X
634
+@item Apple Pixlet           @tab     @tab  X
634 635
 @item Apple ProRes           @tab  X  @tab  X
635 636
 @item Apple QuickDraw        @tab     @tab  X
636 637
     @tab fourcc: qdrw
... ...
@@ -452,6 +452,7 @@ OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmdec.o pnm.o
452 452
 OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o
453 453
 OBJS-$(CONFIG_PGSSUB_DECODER)          += pgssubdec.o
454 454
 OBJS-$(CONFIG_PICTOR_DECODER)          += pictordec.o cga_data.o
455
+OBJS-$(CONFIG_PIXLET_DECODER)          += pixlet.o
455 456
 OBJS-$(CONFIG_PJS_DECODER)             += textdec.o ass.o
456 457
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o pngdsp.o
457 458
 OBJS-$(CONFIG_PNG_ENCODER)             += png.o pngenc.o
... ...
@@ -281,6 +281,7 @@ void avcodec_register_all(void)
281 281
     REGISTER_ENCDEC (PGM,               pgm);
282 282
     REGISTER_ENCDEC (PGMYUV,            pgmyuv);
283 283
     REGISTER_DECODER(PICTOR,            pictor);
284
+    REGISTER_DECODER(PIXLET,            pixlet);
284 285
     REGISTER_ENCDEC (PNG,               png);
285 286
     REGISTER_ENCDEC (PPM,               ppm);
286 287
     REGISTER_ENCDEC (PRORES,            prores);
... ...
@@ -412,6 +412,7 @@ enum AVCodecID {
412 412
     AV_CODEC_ID_SHEERVIDEO,
413 413
     AV_CODEC_ID_YLC,
414 414
     AV_CODEC_ID_PSD,
415
+    AV_CODEC_ID_PIXLET,
415 416
 
416 417
     /* various PCM "codecs" */
417 418
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1339,6 +1339,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1339 1339
         .long_name = NULL_IF_CONFIG_SMALL("YUY2 Lossless Codec"),
1340 1340
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1341 1341
     },
1342
+    {
1343
+        .id        = AV_CODEC_ID_PIXLET,
1344
+        .type      = AVMEDIA_TYPE_VIDEO,
1345
+        .name      = "pixlet",
1346
+        .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
1347
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
1348
+    },
1342 1349
 
1343 1350
     /* image codecs */
1344 1351
     {
1345 1352
new file mode 100644
... ...
@@ -0,0 +1,672 @@
0
+/*
1
+ * Apple Pixlet decoder
2
+ * Copyright (c) 2016 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdint.h>
22
+
23
+#include "libavutil/imgutils.h"
24
+#include "libavutil/intmath.h"
25
+#include "libavutil/opt.h"
26
+
27
+#include "avcodec.h"
28
+#include "bytestream.h"
29
+#include "get_bits.h"
30
+#include "unary.h"
31
+#include "internal.h"
32
+#include "thread.h"
33
+
34
+#define NB_LEVELS 4
35
+
36
+#define H 0
37
+#define V 1
38
+
39
+typedef struct SubBand {
40
+    unsigned width, height;
41
+    unsigned size;
42
+    unsigned x, y;
43
+} SubBand;
44
+
45
+typedef struct PixletContext {
46
+    AVClass *class;
47
+
48
+    GetByteContext gb;
49
+    GetBitContext gbit;
50
+
51
+    int levels;
52
+    int depth;
53
+    int h, w;
54
+
55
+    int16_t *filter[2];
56
+    int16_t *prediction;
57
+    float scaling[4][2][NB_LEVELS];
58
+    SubBand band[4][NB_LEVELS * 3 + 1];
59
+} PixletContext;
60
+
61
+static int init_decoder(AVCodecContext *avctx)
62
+{
63
+    PixletContext *ctx = avctx->priv_data;
64
+    int i, plane;
65
+
66
+    ctx->filter[0]  = av_malloc_array(ctx->h, sizeof(int16_t));
67
+    ctx->filter[1]  = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
68
+    ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
69
+    if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
70
+        return AVERROR(ENOMEM);
71
+
72
+    for (plane = 0; plane < 3; plane++) {
73
+        unsigned shift = plane > 0;
74
+        unsigned w = ctx->w >> shift;
75
+        unsigned h = ctx->h >> shift;
76
+
77
+        ctx->band[plane][0].width  = w >> NB_LEVELS;
78
+        ctx->band[plane][0].height = h >> NB_LEVELS;
79
+        ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
80
+
81
+        for (i = 0; i < NB_LEVELS * 3; i++) {
82
+            unsigned scale = ctx->levels - (i / 3);
83
+
84
+            ctx->band[plane][i + 1].width  = w >> scale;
85
+            ctx->band[plane][i + 1].height = h >> scale;
86
+            ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
87
+
88
+            ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
89
+            ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
90
+        }
91
+    }
92
+
93
+    return 0;
94
+}
95
+
96
+static void free_buffers(AVCodecContext *avctx)
97
+{
98
+    PixletContext *ctx = avctx->priv_data;
99
+
100
+    av_freep(&ctx->filter[0]);
101
+    av_freep(&ctx->filter[1]);
102
+    av_freep(&ctx->prediction);
103
+}
104
+
105
+static av_cold int pixlet_close(AVCodecContext *avctx)
106
+{
107
+    PixletContext *ctx = avctx->priv_data;
108
+    free_buffers(avctx);
109
+    ctx->w = 0;
110
+    ctx->h = 0;
111
+    return 0;
112
+}
113
+
114
+static av_cold int pixlet_init(AVCodecContext *avctx)
115
+{
116
+    avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
117
+    avctx->color_range = AVCOL_RANGE_JPEG;
118
+    return 0;
119
+}
120
+
121
+static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int width, ptrdiff_t stride)
122
+{
123
+    PixletContext *ctx = avctx->priv_data;
124
+    GetBitContext *b = &ctx->gbit;
125
+    unsigned cnt1, nbits, k, j = 0, i = 0;
126
+    int64_t value, state = 3;
127
+    int rlen, escape, flag = 0;
128
+
129
+    while (i < size) {
130
+        nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
131
+
132
+        cnt1 = get_unary(b, 0, 8);
133
+        if (cnt1 < 8) {
134
+            value = show_bits(b, nbits);
135
+            if (value <= 1) {
136
+                skip_bits(b, nbits - 1);
137
+                escape = ((1 << nbits) - 1) * cnt1;
138
+            } else {
139
+                skip_bits(b, nbits);
140
+                escape = value + ((1 << nbits) - 1) * cnt1 - 1;
141
+            }
142
+        } else {
143
+            escape = get_bits(b, 16);
144
+        }
145
+
146
+        value = -((escape + flag) & 1) | 1;
147
+        dst[j++] = value * ((escape + flag + 1) >> 1);
148
+        i++;
149
+        if (j == width) {
150
+            j = 0;
151
+            dst += stride;
152
+        }
153
+        state = 120 * (escape + flag) + state - (120 * state >> 8);
154
+        flag = 0;
155
+
156
+        if (state * 4 > 0xFF || i >= size)
157
+            continue;
158
+
159
+        nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
160
+        escape = av_mod_uintp2(16383, nbits);
161
+        cnt1 = get_unary(b, 0, 8);
162
+        if (cnt1 > 7) {
163
+            rlen = get_bits(b, 16);
164
+        } else {
165
+            value = show_bits(b, nbits);
166
+            if (value > 1) {
167
+                skip_bits(b, nbits);
168
+                rlen = value + escape * cnt1 - 1;
169
+            } else {
170
+                skip_bits(b, nbits - 1);
171
+                rlen = escape * cnt1;
172
+            }
173
+        }
174
+
175
+        if (i + rlen > size)
176
+            return AVERROR_INVALIDDATA;
177
+        i += rlen;
178
+
179
+        for (k = 0; k < rlen; k++) {
180
+            dst[j++] = 0;
181
+            if (j == width) {
182
+                j = 0;
183
+                dst += stride;
184
+            }
185
+        }
186
+
187
+        state = 0;
188
+        flag = rlen < 0xFFFF ? 1 : 0;
189
+    }
190
+
191
+    align_get_bits(b);
192
+    return get_bits_count(b) >> 3;
193
+}
194
+
195
+static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, int size,
196
+                            int c, int a, int d,
197
+                            int width, ptrdiff_t stride)
198
+{
199
+    PixletContext *ctx = avctx->priv_data;
200
+    GetBitContext *b = &ctx->gbit;
201
+    unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
202
+    int ret, escape, pfx, value, yflag, xflag, flag = 0;
203
+    int64_t state = 3, tmp;
204
+
205
+    if ((ret = init_get_bits8(b, src, bytestream2_get_bytes_left(&ctx->gb))) < 0)
206
+      return ret;
207
+
208
+    if ((a >= 0) + (a ^ (a >> 31)) - (a >> 31) != 1) {
209
+        nbits = 33 - ff_clz((a >= 0) + (a ^ (a >> 31)) - (a >> 31) - 1);
210
+        if (nbits > 16)
211
+            return AVERROR_INVALIDDATA;
212
+    } else {
213
+        nbits = 1;
214
+    }
215
+
216
+    length = 25 - nbits;
217
+
218
+    while (i < size) {
219
+        if (state >> 8 != -3) {
220
+            value = ff_clz((state >> 8) + 3) ^ 0x1F;
221
+        } else {
222
+            value = -1;
223
+        }
224
+
225
+        cnt1 = get_unary(b, 0, length);
226
+
227
+        if (cnt1 >= length) {
228
+            cnt1 = get_bits(b, nbits);
229
+        } else {
230
+            pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
231
+            cnt1 *= (1 << pfx) - 1;
232
+            shbits = show_bits(b, pfx);
233
+            if (shbits <= 1) {
234
+                skip_bits(b, pfx - 1);
235
+            } else {
236
+                skip_bits(b, pfx);
237
+                cnt1 += shbits - 1;
238
+            }
239
+        }
240
+
241
+        xflag = flag + cnt1;
242
+        yflag = xflag;
243
+
244
+        if (flag + cnt1 == 0) {
245
+            value = 0;
246
+        } else {
247
+            xflag &= 1u;
248
+            tmp = c * ((yflag + 1) >> 1) + (c >> 1);
249
+            value = xflag + (tmp ^ -xflag);
250
+        }
251
+
252
+        i++;
253
+        dst[j++] = value;
254
+        if (j == width) {
255
+            j = 0;
256
+            dst += stride;
257
+        }
258
+        state += d * yflag - (d * state >> 8);
259
+
260
+        flag = 0;
261
+
262
+        if (state * 4 > 0xFF || i >= size)
263
+            continue;
264
+
265
+        pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24;
266
+        escape = av_mod_uintp2(16383, pfx);
267
+        cnt1 = get_unary(b, 0, 8);
268
+        if (cnt1 < 8) {
269
+            value = show_bits(b, pfx);
270
+            if (value > 1) {
271
+                skip_bits(b, pfx);
272
+                rlen = value + escape * cnt1 - 1;
273
+            } else {
274
+                skip_bits(b, pfx - 1);
275
+                rlen = escape * cnt1;
276
+            }
277
+        } else {
278
+            if (get_bits1(b))
279
+                value = get_bits(b, 16);
280
+            else
281
+                value = get_bits(b, 8);
282
+
283
+            rlen = value + 8 * escape;
284
+        }
285
+
286
+        if (rlen > 0xFFFF || i + rlen > size)
287
+            return AVERROR_INVALIDDATA;
288
+        i += rlen;
289
+
290
+        for (k = 0; k < rlen; k++) {
291
+            dst[j++] = 0;
292
+            if (j == width) {
293
+                j = 0;
294
+                dst += stride;
295
+            }
296
+        }
297
+
298
+        state = 0;
299
+        flag = rlen < 0xFFFF ? 1 : 0;
300
+    }
301
+
302
+    align_get_bits(b);
303
+    return get_bits_count(b) >> 3;
304
+}
305
+
306
+static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame *frame)
307
+{
308
+    PixletContext *ctx = avctx->priv_data;
309
+    ptrdiff_t stride = frame->linesize[plane] / 2;
310
+    int i, ret;
311
+
312
+    for (i = 0; i < ctx->levels * 3; i++) {
313
+        int32_t a = bytestream2_get_be32(&ctx->gb);
314
+        int32_t b = bytestream2_get_be32(&ctx->gb);
315
+        int32_t c = bytestream2_get_be32(&ctx->gb);
316
+        int32_t d = bytestream2_get_be32(&ctx->gb);
317
+        int16_t *dest = (int16_t *)frame->data[plane] + ctx->band[plane][i + 1].x +
318
+                                               stride * ctx->band[plane][i + 1].y;
319
+        unsigned size = ctx->band[plane][i + 1].size;
320
+        uint32_t magic;
321
+
322
+        magic = bytestream2_get_be32(&ctx->gb);
323
+        if (magic != 0xDEADBEEF) {
324
+            av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%08X for plane %d, band %d\n", magic, plane, i);
325
+            return AVERROR_INVALIDDATA;
326
+        }
327
+
328
+        ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
329
+                               c, (b >= FFABS(a)) ? b : a, d,
330
+                               ctx->band[plane][i + 1].width, stride);
331
+        if (ret < 0) {
332
+            av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, band %d\n", plane, i);
333
+            return ret;
334
+        }
335
+        bytestream2_skip(&ctx->gb, ret);
336
+    }
337
+
338
+    return 0;
339
+}
340
+
341
+static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height, ptrdiff_t stride)
342
+{
343
+    int16_t *next, val;
344
+    int i, j;
345
+
346
+    memset(pred, 0, width * sizeof(*pred));
347
+
348
+    for (i = 0; i < height; i++) {
349
+        val     = pred[0] + dst[0];
350
+        dst[0]  = val;
351
+        pred[0] = val;
352
+        next    = dst + 2;
353
+        for (j = 1; j < width; j++, next++) {
354
+            val       = pred[j] + next[-1];
355
+            next[-1]  = val;
356
+            pred[j]   = val;
357
+            next[-1] += next[-2];
358
+        }
359
+        dst += stride;
360
+    }
361
+}
362
+
363
+static void filter(int16_t *dest, int16_t *tmp, unsigned size, float SCALE)
364
+{
365
+    int16_t *low, *high, *ll, *lh, *hl, *hh;
366
+    int hsize, i, j;
367
+    float value;
368
+
369
+    hsize = size >> 1;
370
+    low = tmp + 4;
371
+    high = &low[hsize + 8];
372
+
373
+    memcpy(low, dest, size);
374
+    memcpy(high, dest + hsize, size);
375
+
376
+    ll = &low[hsize];
377
+    lh = &low[hsize];
378
+    hl = &high[hsize];
379
+    hh = hl;
380
+    for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
381
+        low[i - 5]  = low[j - 1];
382
+        lh[0]       = ll[-1];
383
+        high[i - 5] = high[j - 2];
384
+        hh[0]       = hl[-2];
385
+    }
386
+
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);
394
+    }
395
+
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);
405
+    }
406
+}
407
+
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)
411
+{
412
+    PixletContext *ctx = avctx->priv_data;
413
+    unsigned scaled_width, scaled_height;
414
+    float scale_H, scale_V;
415
+    int16_t *ptr, *tmp;
416
+    int i, j, k;
417
+
418
+    scaled_height = height >> nb_levels;
419
+    scaled_width  = width  >> nb_levels;
420
+    tmp = ctx->filter[0];
421
+
422
+    for (i = 0; i < nb_levels; i++) {
423
+        scaled_width  <<= 1;
424
+        scaled_height <<= 1;
425
+        scale_H = scaling_H[i];
426
+        scale_V = scaling_V[i];
427
+
428
+        ptr = dest;
429
+        for (j = 0; j < scaled_height; j++) {
430
+            filter(ptr, ctx->filter[1], scaled_width, scale_V);
431
+            ptr += stride;
432
+        }
433
+
434
+        for (j = 0; j < scaled_width; j++) {
435
+            ptr = dest + j;
436
+            for (k = 0; k < scaled_height; k++) {
437
+                tmp[k] = *ptr;
438
+                ptr += stride;
439
+            }
440
+
441
+            filter(tmp, ctx->filter[1], scaled_height, scale_H);
442
+
443
+            ptr = dest + j;
444
+            for (k = 0; k < scaled_height; k++) {
445
+                *ptr = tmp[k];
446
+                ptr += stride;
447
+            }
448
+        }
449
+    }
450
+}
451
+
452
+#define SQR(a) ((a) * (a))
453
+
454
+static void postprocess_luma(AVFrame *frame, int w, int h, int depth)
455
+{
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. / ((1 << depth) - 1);
460
+    int i, j;
461
+
462
+    for (j = 0; j < h; j++) {
463
+        for (i = 0; i < w; i++) {
464
+            dsty[i] = SQR(FFMAX(srcy[i], 0) * factor) * 65535;
465
+        }
466
+        dsty += stridey;
467
+        srcy += stridey;
468
+    }
469
+}
470
+
471
+static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
472
+{
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 int add = 1 << (depth - 1);
480
+    const int shift = 16 - depth;
481
+    int i, j;
482
+
483
+    for (j = 0; j < h; j++) {
484
+        for (i = 0; i < w; i++) {
485
+            dstu[i] = (add + srcu[i]) << shift;
486
+            dstv[i] = (add + srcv[i]) << shift;
487
+        }
488
+        dstu += strideu;
489
+        dstv += stridev;
490
+        srcu += strideu;
491
+        srcv += stridev;
492
+    }
493
+}
494
+
495
+static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame)
496
+{
497
+    PixletContext *ctx = avctx->priv_data;
498
+    ptrdiff_t stride = frame->linesize[plane] / 2;
499
+    unsigned shift = plane > 0;
500
+    int16_t *dst;
501
+    int i, ret;
502
+
503
+    for (i = ctx->levels - 1; i >= 0; i--) {
504
+        ctx->scaling[plane][H][i] = 1000000. / sign_extend(bytestream2_get_be32(&ctx->gb), 32);
505
+        ctx->scaling[plane][V][i] = 1000000. / sign_extend(bytestream2_get_be32(&ctx->gb), 32);
506
+    }
507
+
508
+    bytestream2_skip(&ctx->gb, 4);
509
+
510
+    dst = (int16_t *)frame->data[plane];
511
+    dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
512
+
513
+    if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gb),
514
+                              bytestream2_get_bytes_left(&ctx->gb))) < 0)
515
+        return ret;
516
+
517
+    ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1, ctx->band[plane][0].width - 1, 0);
518
+    if (ret < 0) {
519
+        av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane);
520
+        return ret;
521
+    }
522
+
523
+    ret = read_low_coeffs(avctx, dst + stride, ctx->band[plane][0].height - 1, 1, stride);
524
+    if (ret < 0) {
525
+        av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane);
526
+        return ret;
527
+    }
528
+
529
+    ret = read_low_coeffs(avctx, dst + stride + 1,
530
+                          (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
531
+                          ctx->band[plane][0].width - 1, stride);
532
+    if (ret < 0) {
533
+        av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane);
534
+        return ret;
535
+    }
536
+
537
+    bytestream2_skip(&ctx->gb, ret);
538
+    if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
539
+        av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
540
+        return AVERROR_INVALIDDATA;
541
+    }
542
+
543
+    ret = read_highpass(avctx, avpkt->data, plane, frame);
544
+    if (ret < 0)
545
+        return ret;
546
+
547
+    lowpass_prediction(dst, ctx->prediction,
548
+                       ctx->band[plane][0].width, ctx->band[plane][0].height, stride);
549
+
550
+    reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift, ctx->h >> shift,
551
+                   stride, NB_LEVELS, ctx->scaling[plane][H], ctx->scaling[plane][V]);
552
+
553
+    return 0;
554
+}
555
+
556
+static int pixlet_decode_frame(AVCodecContext *avctx, void *data,
557
+                               int *got_frame, AVPacket *avpkt)
558
+{
559
+    PixletContext *ctx = avctx->priv_data;
560
+    int i, w, h, width, height, ret, version;
561
+    AVFrame *p = data;
562
+    ThreadFrame frame = { .f = data };
563
+    uint32_t pktsize;
564
+
565
+    bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
566
+
567
+    pktsize = bytestream2_get_be32(&ctx->gb);
568
+    if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
569
+        av_log(avctx, AV_LOG_ERROR, "Invalid packet size %u.\n", pktsize);
570
+        return AVERROR_INVALIDDATA;
571
+    }
572
+
573
+    version = bytestream2_get_le32(&ctx->gb);
574
+    if (version != 1)
575
+        avpriv_request_sample(avctx, "Version %d", version);
576
+
577
+    bytestream2_skip(&ctx->gb, 4);
578
+    if (bytestream2_get_be32(&ctx->gb) != 1)
579
+        return AVERROR_INVALIDDATA;
580
+    bytestream2_skip(&ctx->gb, 4);
581
+
582
+    width  = bytestream2_get_be32(&ctx->gb);
583
+    height = bytestream2_get_be32(&ctx->gb);
584
+
585
+    w = FFALIGN(width,  1 << (NB_LEVELS + 1));
586
+    h = FFALIGN(height, 1 << (NB_LEVELS + 1));
587
+
588
+    ctx->levels = bytestream2_get_be32(&ctx->gb);
589
+    if (ctx->levels != NB_LEVELS)
590
+        return AVERROR_INVALIDDATA;
591
+    ctx->depth = bytestream2_get_be32(&ctx->gb);
592
+    if (ctx->depth < 8 || ctx->depth > 15) {
593
+        avpriv_request_sample(avctx, "Depth %d", ctx->depth);
594
+        return AVERROR_INVALIDDATA;
595
+    }
596
+
597
+    ret = ff_set_dimensions(avctx, w, h);
598
+    if (ret < 0)
599
+        return ret;
600
+    avctx->width  = width;
601
+    avctx->height = height;
602
+
603
+    if (ctx->w != w || ctx->h != h) {
604
+        free_buffers(avctx);
605
+        ctx->w = w;
606
+        ctx->h = h;
607
+
608
+        ret = init_decoder(avctx);
609
+        if (ret < 0) {
610
+            free_buffers(avctx);
611
+            ctx->w = 0;
612
+            ctx->h = 0;
613
+            return ret;
614
+        }
615
+    }
616
+
617
+    bytestream2_skip(&ctx->gb, 8);
618
+
619
+    p->pict_type = AV_PICTURE_TYPE_I;
620
+    p->key_frame = 1;
621
+    p->color_range = AVCOL_RANGE_JPEG;
622
+
623
+    ret = ff_thread_get_buffer(avctx, &frame, 0);
624
+    if (ret < 0)
625
+        return ret;
626
+
627
+    for (i = 0; i < 3; i++) {
628
+        ret = decode_plane(avctx, i, avpkt, frame.f);
629
+        if (ret < 0)
630
+            return ret;
631
+        if (avctx->flags & AV_CODEC_FLAG_GRAY)
632
+            break;
633
+    }
634
+
635
+    postprocess_luma(frame.f, ctx->w, ctx->h, ctx->depth);
636
+    postprocess_chroma(frame.f, ctx->w >> 1, ctx->h >> 1, ctx->depth);
637
+
638
+    *got_frame = 1;
639
+
640
+    return pktsize;
641
+}
642
+
643
+#if HAVE_THREADS
644
+static int pixlet_init_thread_copy(AVCodecContext *avctx)
645
+{
646
+    PixletContext *ctx = avctx->priv_data;
647
+
648
+    ctx->filter[0] = NULL;
649
+    ctx->filter[1] = NULL;
650
+    ctx->prediction = NULL;
651
+    ctx->w = ctx->h = 0;
652
+
653
+    return 0;
654
+}
655
+#endif
656
+
657
+AVCodec ff_pixlet_decoder = {
658
+    .name             = "pixlet",
659
+    .long_name        = NULL_IF_CONFIG_SMALL("Apple Pixlet"),
660
+    .type             = AVMEDIA_TYPE_VIDEO,
661
+    .id               = AV_CODEC_ID_PIXLET,
662
+    .init             = pixlet_init,
663
+    .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy),
664
+    .close            = pixlet_close,
665
+    .decode           = pixlet_decode_frame,
666
+    .priv_data_size   = sizeof(PixletContext),
667
+    .capabilities     = AV_CODEC_CAP_DR1 |
668
+                        AV_CODEC_CAP_FRAME_THREADS,
669
+    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
670
+                        FF_CODEC_CAP_INIT_CLEANUP,
671
+};
... ...
@@ -28,7 +28,7 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR  69
31
+#define LIBAVCODEC_VERSION_MINOR  70
32 32
 #define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -296,6 +296,8 @@ const AVCodecTag ff_codec_movvideo_tags[] = {
296 296
     { AV_CODEC_ID_SHEERVIDEO, MKTAG('S', 'h', 'r', '6') },
297 297
     { AV_CODEC_ID_SHEERVIDEO, MKTAG('S', 'h', 'r', '7') },
298 298
 
299
+    { AV_CODEC_ID_PIXLET, MKTAG('p', 'x', 'l', 't') },
300
+
299 301
     { AV_CODEC_ID_NONE, 0 },
300 302
 };
301 303