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
27
34
36
37 #define PIXLET_MAGIC 0xDEADBEEF
38
41
47
50
53
57
64
66 {
69 return 0;
70 }
71
73 {
75
79 }
80
82 {
87 return 0;
88 }
89
91 {
94
98 if (!
ctx->filter[0] || !
ctx->filter[1] || !
ctx->prediction)
100
101 for (plane = 0; plane < 3; plane++) {
102 unsigned shift = plane > 0;
105
109
112
116
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);
119 }
120 }
121
122 return 0;
123 }
124
127 {
130 unsigned cnt1, nbits, k, j = 0,
i = 0;
132 int rlen, escape,
flag = 0;
133
136
138 if (cnt1 < 8) {
142 escape = ((1 << nbits) - 1) * cnt1;
143 } else {
145 escape =
value + ((1 << nbits) - 1) * cnt1 - 1;
146 }
147 } else {
149 }
150
152 dst[j++] =
value * ((escape +
flag + 1) >> 1);
155 j = 0;
157 }
160
162 continue;
163
167 if (cnt1 > 7) {
169 } else {
173 rlen =
value + escape * cnt1 - 1;
174 } else {
176 rlen = escape * cnt1;
177 }
178 }
179
183
184 for (k = 0; k < rlen; k++) {
185 dst[j++] = 0;
187 j = 0;
189 }
190 }
191
193 flag = rlen < 0xFFFF ? 1 : 0;
194 }
195
198 }
199
203 {
206 unsigned cnt1, shbits, rlen, nbits, length,
i = 0, j = 0, k;
209
213
216 if (nbits > 16)
218 } else {
219 nbits = 1;
220 }
221
222 length = 25 - nbits;
223
225 if (((
state >> 8) + 3) & 0xFFFFFFF)
227 else
229
231 if (cnt1 >= length) {
233 } else {
234 pfx = 14 + ((((uint64_t)(
value - 14)) >> 32) & (
value - 14));
235 if (pfx < 1 || pfx > 25)
237 cnt1 *= (1 << pfx) - 1;
239 if (shbits <= 1) {
241 } else {
243 cnt1 += shbits - 1;
244 }
245 }
246
248 yflag = xflag;
249
250 if (
flag + cnt1 == 0) {
252 } else {
254 tmp = (int64_t)
c * ((yflag + 1) >> 1) + (
c >> 1);
256 }
257
261 j = 0;
263 }
264 state += (int64_t)
d * (uint64_t)yflag - ((int64_t)(
d * (uint64_t)
state) >> 8);
265
267
269 continue;
270
274 if (cnt1 < 8) {
275 if (pfx < 1 || pfx > 25)
277
281 rlen =
value + escape * cnt1 - 1;
282 } else {
284 rlen = escape * cnt1;
285 }
286 } else {
289 else
291
292 rlen =
value + 8 * escape;
293 }
294
295 if (rlen > 0xFFFF ||
i + rlen >
size)
298
299 for (k = 0; k < rlen; k++) {
300 dst[j++] = 0;
302 j = 0;
304 }
305 }
306
308 flag = rlen < 0xFFFF ? 1 : 0;
309 }
310
313 }
314
317 {
321
322 for (
i = 0;
i <
ctx->levels * 3;
i++) {
327 int16_t *dest = (int16_t *)
frame->data[plane] +
328 ctx->band[plane][
i + 1].x +
330 unsigned size =
ctx->band[plane][
i + 1].size;
331 uint32_t magic = bytestream2_get_be32(&
ctx->gb);
332
335 "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
338 }
339
342
348 "error in highpass coefficients for plane %d, band %d\n",
351 }
353 }
354
355 return 0;
356 }
357
360 {
363
365
369 for (j = 1; j <
width; j++) {
372 dst[j] += dst[j-1];
373 }
375 }
376 }
377
379 {
380 int16_t *low, *high, *ll, *lh, *hl, *hh;
383
386 high = &low[hsize + 8];
387
388 memcpy(low, dest,
size);
389 memcpy(high, dest + hsize,
size);
390
391 ll = &low[hsize];
392 lh = &low[hsize];
393 hl = &high[hsize];
394 hh = hl;
395 for (
i = 4, j = 2;
i;
i--, j++, ll--, hh++, lh++, hl--) {
396 low[
i - 5] = low[j - 1];
397 lh[0] = ll[-1];
398 high[
i - 5] = high[j - 2];
399 hh[0] = hl[-2];
400 }
401
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);
409 }
410
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);
420 }
421 }
422
425 int64_t *scaling_h, int64_t *scaling_v)
426 {
428 unsigned scaled_width, scaled_height;
431
435
437 int64_t scale_v = scaling_v[
i];
438 int64_t scale_h = scaling_h[
i];
439 scaled_width <<= 1;
440 scaled_height <<= 1;
441
442 ptr = dest;
443 for (j = 0; j < scaled_height; j++) {
444 filterfn(ptr,
ctx->filter[1], scaled_width, scale_v);
446 }
447
448 for (j = 0; j < scaled_width; j++) {
449 ptr = dest + j;
450 for (k = 0; k < scaled_height; k++) {
453 }
454
456
457 ptr = dest + j;
458 for (k = 0; k < scaled_height; k++) {
461 }
462 }
463 }
464 }
465
467 {
469 int max = (1 << depth) - 1;
470
471 if (
ctx->depth == depth)
472 return;
474
477 }
478
480 int w,
int h,
int depth)
481 {
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;
488
489 for (j = 0; j <
h; j++) {
490 for (
i = 0;
i <
w;
i++) {
493 else if (srcy[
i] > ((1 << depth) - 1))
495 else
496 dsty[
i] = lut[srcy[
i]];
497 }
498 dsty += stridey;
499 srcy += stridey;
500 }
501 }
502
504 {
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;
514
515 for (j = 0; j <
h; j++) {
516 for (
i = 0;
i <
w;
i++) {
519 }
520 dstu += strideu;
521 dstv += stridev;
522 srcu += strideu;
523 srcv += stridev;
524 }
525 }
526
529 {
532 unsigned shift = plane > 0;
533 int16_t *dst;
535
536 for (
i =
ctx->levels - 1;
i >= 0;
i--) {
539
542
543 ctx->scaling[plane][
H][
i] = (1000000ULL << 32) /
h;
544 ctx->scaling[plane][
V][
i] = (1000000ULL << 32) / v;
545 }
546
548
549 dst = (int16_t *)
frame->data[plane];
551
556
558 ctx->band[plane][0].width - 1, 0);
561 "error in lowpass coefficients for plane %d, top row\n", plane);
563 }
564
566 ctx->band[plane][0].height - 1, 1,
stride);
569 "error in lowpass coefficients for plane %d, left column\n",
570 plane);
572 }
573
575 (
ctx->band[plane][0].width - 1) * (
ctx->band[plane][0].height - 1),
579 "error in lowpass coefficients for plane %d, rest\n", plane);
581 }
582
587 }
588
592
595
598 ctx->scaling[plane][
V]);
599
600 return 0;
601 }
602
605 {
610 uint32_t pktsize, depth;
611
613
614 pktsize = bytestream2_get_be32(&
ctx->gb);
618 }
619
623
625 if (bytestream2_get_be32(&
ctx->gb) != 1)
628
629 width = bytestream2_get_be32(&
ctx->gb);
631
635
638
639 ctx->levels = bytestream2_get_be32(&
ctx->gb);
642 depth = bytestream2_get_be32(&
ctx->gb);
643 if (depth < 8 || depth > 15) {
646 }
647
649
655
660
667 }
668 }
669
671
675
679
680 for (
i = 0;
i < 3;
i++) {
685 break;
686 }
687
690
691 *got_frame = 1;
692
693 return pktsize;
694 }
695
709 };