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
22 #include <stdint.h>
23
25
33
35
36 #define PIXLET_MAGIC 0xDEADBEEF
37
40
46
49
52
56
63
65 {
68 return 0;
69 }
70
72 {
74
78 }
79
81 {
86 return 0;
87 }
88
90 {
93
97 if (!
ctx->filter[0] || !
ctx->filter[1] || !
ctx->prediction)
99
100 for (plane = 0; plane < 3; plane++) {
101 unsigned shift = plane > 0;
104
108
111
115
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);
118 }
119 }
120
121 return 0;
122 }
123
126 {
129 unsigned cnt1, nbits, k, j = 0,
i = 0;
131 int rlen, escape,
flag = 0;
132
135
137 if (cnt1 < 8) {
141 escape = ((1 << nbits) - 1) * cnt1;
142 } else {
144 escape =
value + ((1 << nbits) - 1) * cnt1 - 1;
145 }
146 } else {
148 }
149
151 dst[j++] =
value * ((escape +
flag + 1) >> 1);
154 j = 0;
156 }
159
161 continue;
162
166 if (cnt1 > 7) {
168 } else {
172 rlen =
value + escape * cnt1 - 1;
173 } else {
175 rlen = escape * cnt1;
176 }
177 }
178
182
183 for (k = 0; k < rlen; k++) {
184 dst[j++] = 0;
186 j = 0;
188 }
189 }
190
192 flag = rlen < 0xFFFF ? 1 : 0;
193 }
194
197 }
198
202 {
205 unsigned cnt1, shbits, rlen, nbits, length,
i = 0, j = 0, k;
208
212
215 if (nbits > 16)
217 } else {
218 nbits = 1;
219 }
220
221 length = 25 - nbits;
222
224 if (((
state >> 8) + 3) & 0xFFFFFFF)
226 else
228
230 if (cnt1 >= length) {
232 } else {
233 pfx = 14 + ((((uint64_t)(
value - 14)) >> 32) & (
value - 14));
234 if (pfx < 1 || pfx > 25)
236 cnt1 *= (1 << pfx) - 1;
238 if (shbits <= 1) {
240 } else {
242 cnt1 += shbits - 1;
243 }
244 }
245
247 yflag = xflag;
248
249 if (
flag + cnt1 == 0) {
251 } else {
253 tmp = (int64_t)
c * ((yflag + 1) >> 1) + (
c >> 1);
255 }
256
260 j = 0;
262 }
263 state += (int64_t)
d * (uint64_t)yflag - ((int64_t)(
d * (uint64_t)
state) >> 8);
264
266
268 continue;
269
273 if (cnt1 < 8) {
274 if (pfx < 1 || pfx > 25)
276
280 rlen =
value + escape * cnt1 - 1;
281 } else {
283 rlen = escape * cnt1;
284 }
285 } else {
288 else
290
291 rlen =
value + 8 * escape;
292 }
293
294 if (rlen > 0xFFFF ||
i + rlen >
size)
297
298 for (k = 0; k < rlen; k++) {
299 dst[j++] = 0;
301 j = 0;
303 }
304 }
305
307 flag = rlen < 0xFFFF ? 1 : 0;
308 }
309
312 }
313
316 {
320
321 for (
i = 0;
i <
ctx->levels * 3;
i++) {
326 int16_t *dest = (int16_t *)
frame->data[plane] +
327 ctx->band[plane][
i + 1].x +
329 unsigned size =
ctx->band[plane][
i + 1].size;
330 uint32_t magic = bytestream2_get_be32(&
ctx->gb);
331
334 "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
337 }
338
341
347 "error in highpass coefficients for plane %d, band %d\n",
350 }
352 }
353
354 return 0;
355 }
356
359 {
362
364
368 for (j = 1; j <
width; j++) {
371 dst[j] += dst[j-1];
372 }
374 }
375 }
376
378 {
379 int16_t *low, *high, *ll, *lh, *hl, *hh;
382
385 high = &low[hsize + 8];
386
387 memcpy(low, dest,
size);
388 memcpy(high, dest + hsize,
size);
389
390 ll = &low[hsize];
391 lh = &low[hsize];
392 hl = &high[hsize];
393 hh = hl;
394 for (
i = 4, j = 2;
i;
i--, j++, ll--, hh++, lh++, hl--) {
395 low[
i - 5] = low[j - 1];
396 lh[0] = ll[-1];
397 high[
i - 5] = high[j - 2];
398 hh[0] = hl[-2];
399 }
400
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);
408 }
409
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);
419 }
420 }
421
424 int64_t *scaling_h, int64_t *scaling_v)
425 {
427 unsigned scaled_width, scaled_height;
430
434
436 int64_t scale_v = scaling_v[
i];
437 int64_t scale_h = scaling_h[
i];
438 scaled_width <<= 1;
439 scaled_height <<= 1;
440
441 ptr = dest;
442 for (j = 0; j < scaled_height; j++) {
443 filterfn(ptr,
ctx->filter[1], scaled_width, scale_v);
445 }
446
447 for (j = 0; j < scaled_width; j++) {
448 ptr = dest + j;
449 for (k = 0; k < scaled_height; k++) {
452 }
453
455
456 ptr = dest + j;
457 for (k = 0; k < scaled_height; k++) {
460 }
461 }
462 }
463 }
464
466 {
468 int max = (1 << depth) - 1;
469
470 if (
ctx->depth == depth)
471 return;
473
476 }
477
479 int w,
int h,
int depth)
480 {
482 uint16_t *dsty = (uint16_t *)
frame->data[0];
483 int16_t *srcy = (int16_t *)
frame->data[0];
484 ptrdiff_t stridey =
frame->linesize[0] / 2;
485 uint16_t *lut =
ctx->lut;
487
488 for (j = 0; j <
h; j++) {
489 for (
i = 0;
i <
w;
i++) {
492 else if (srcy[
i] > ((1 << depth) - 1))
494 else
495 dsty[
i] = lut[srcy[
i]];
496 }
497 dsty += stridey;
498 srcy += stridey;
499 }
500 }
501
503 {
504 uint16_t *dstu = (uint16_t *)
frame->data[1];
505 uint16_t *dstv = (uint16_t *)
frame->data[2];
506 int16_t *srcu = (int16_t *)
frame->data[1];
507 int16_t *srcv = (int16_t *)
frame->data[2];
508 ptrdiff_t strideu =
frame->linesize[1] / 2;
509 ptrdiff_t stridev =
frame->linesize[2] / 2;
510 const unsigned add = 1 << (depth - 1);
511 const unsigned shift = 16 - depth;
513
514 for (j = 0; j <
h; j++) {
515 for (
i = 0;
i <
w;
i++) {
518 }
519 dstu += strideu;
520 dstv += stridev;
521 srcu += strideu;
522 srcv += stridev;
523 }
524 }
525
528 {
531 unsigned shift = plane > 0;
532 int16_t *dst;
534
535 for (
i =
ctx->levels - 1;
i >= 0;
i--) {
538
541
542 ctx->scaling[plane][
H][
i] = (1000000ULL << 32) /
h;
543 ctx->scaling[plane][
V][
i] = (1000000ULL << 32) / v;
544 }
545
547
548 dst = (int16_t *)
frame->data[plane];
550
555
557 ctx->band[plane][0].width - 1, 0);
560 "error in lowpass coefficients for plane %d, top row\n", plane);
562 }
563
565 ctx->band[plane][0].height - 1, 1,
stride);
568 "error in lowpass coefficients for plane %d, left column\n",
569 plane);
571 }
572
574 (
ctx->band[plane][0].width - 1) * (
ctx->band[plane][0].height - 1),
578 "error in lowpass coefficients for plane %d, rest\n", plane);
580 }
581
586 }
587
591
594
597 ctx->scaling[plane][
V]);
598
599 return 0;
600 }
601
604 {
607 uint32_t pktsize, depth;
608
610
611 pktsize = bytestream2_get_be32(&
ctx->gb);
615 }
616
620
622 if (bytestream2_get_be32(&
ctx->gb) != 1)
625
626 width = bytestream2_get_be32(&
ctx->gb);
628
632
635
636 ctx->levels = bytestream2_get_be32(&
ctx->gb);
639 depth = bytestream2_get_be32(&
ctx->gb);
640 if (depth < 8 || depth > 15) {
643 }
644
646
652
657
664 }
665 }
666
668
672
676
677 for (
i = 0;
i < 3;
i++) {
682 break;
683 }
684
687
688 *got_frame = 1;
689
690 return pktsize;
691 }
692
705 };