FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pixlet.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 334 404 82.7%
Functions: 15 15 100.0%
Branches: 113 176 64.2%

Line Branch Exec Source
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
24 #include "libavutil/intmath.h"
25 #include "libavutil/mem.h"
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31 #include "get_bits.h"
32 #include "thread.h"
33 #include "unary.h"
34
35 #define NB_LEVELS 4
36
37 #define PIXLET_MAGIC 0xDEADBEEF
38
39 #define H 0
40 #define V 1
41
42 typedef struct SubBand {
43 unsigned width, height;
44 unsigned size;
45 unsigned x, y;
46 } SubBand;
47
48 typedef struct PixletContext {
49 AVClass *class;
50
51 GetByteContext gb;
52 GetBitContext bc;
53
54 int levels;
55 int depth;
56 int w, h;
57
58 int16_t *filter[2];
59 int16_t *prediction;
60 int64_t scaling[4][2][NB_LEVELS];
61 uint16_t lut[65536];
62 SubBand band[4][NB_LEVELS * 3 + 1];
63 } PixletContext;
64
65 2 static av_cold int pixlet_init(AVCodecContext *avctx)
66 {
67 2 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
68 2 avctx->color_range = AVCOL_RANGE_JPEG;
69 2 return 0;
70 }
71
72 3 static void free_buffers(AVCodecContext *avctx)
73 {
74 3 PixletContext *ctx = avctx->priv_data;
75
76 3 av_freep(&ctx->filter[0]);
77 3 av_freep(&ctx->filter[1]);
78 3 av_freep(&ctx->prediction);
79 3 }
80
81 2 static av_cold int pixlet_close(AVCodecContext *avctx)
82 {
83 2 PixletContext *ctx = avctx->priv_data;
84 2 free_buffers(avctx);
85 2 ctx->w = 0;
86 2 ctx->h = 0;
87 2 return 0;
88 }
89
90 1 static int init_decoder(AVCodecContext *avctx)
91 {
92 1 PixletContext *ctx = avctx->priv_data;
93 int i, plane;
94
95 1 ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
96 1 ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
97 1 ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
98
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
99 return AVERROR(ENOMEM);
100
101
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (plane = 0; plane < 3; plane++) {
102 3 unsigned shift = plane > 0;
103 3 unsigned w = ctx->w >> shift;
104 3 unsigned h = ctx->h >> shift;
105
106 3 ctx->band[plane][0].width = w >> NB_LEVELS;
107 3 ctx->band[plane][0].height = h >> NB_LEVELS;
108 3 ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
109
110
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
39 for (i = 0; i < NB_LEVELS * 3; i++) {
111 36 unsigned scale = ctx->levels - (i / 3);
112
113 36 ctx->band[plane][i + 1].width = w >> scale;
114 36 ctx->band[plane][i + 1].height = h >> scale;
115 36 ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
116
117 36 ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
118 36 ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
119 }
120 }
121
122 1 return 0;
123 }
124
125 9 static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size,
126 int width, ptrdiff_t stride)
127 {
128 9 PixletContext *ctx = avctx->priv_data;
129 9 GetBitContext *bc = &ctx->bc;
130 9 unsigned cnt1, nbits, k, j = 0, i = 0;
131 9 int64_t value, state = 3;
132 9 int rlen, escape, flag = 0;
133
134
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 while (i < size) {
135 3 nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
136
137 3 cnt1 = get_unary(bc, 0, 8);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (cnt1 < 8) {
139 value = show_bits(bc, nbits);
140 if (value <= 1) {
141 skip_bits(bc, nbits - 1);
142 escape = ((1 << nbits) - 1) * cnt1;
143 } else {
144 skip_bits(bc, nbits);
145 escape = value + ((1 << nbits) - 1) * cnt1 - 1;
146 }
147 } else {
148 3 escape = get_bits(bc, 16);
149 }
150
151 3 value = -((escape + flag) & 1) | 1;
152 3 dst[j++] = value * ((escape + flag + 1) >> 1);
153 3 i++;
154
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (j == width) {
155 3 j = 0;
156 3 dst += stride;
157 }
158 3 state = 120 * (escape + flag) + state - (120 * state >> 8);
159 3 flag = 0;
160
161
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (state * 4ULL > 0xFF || i >= size)
162 3 continue;
163
164 nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
165 escape = av_zero_extend(16383, nbits);
166 cnt1 = get_unary(bc, 0, 8);
167 if (cnt1 > 7) {
168 rlen = get_bits(bc, 16);
169 } else {
170 value = show_bits(bc, nbits);
171 if (value > 1) {
172 skip_bits(bc, nbits);
173 rlen = value + escape * cnt1 - 1;
174 } else {
175 skip_bits(bc, nbits - 1);
176 rlen = escape * cnt1;
177 }
178 }
179
180 if (rlen > size - i)
181 return AVERROR_INVALIDDATA;
182 i += rlen;
183
184 for (k = 0; k < rlen; k++) {
185 dst[j++] = 0;
186 if (j == width) {
187 j = 0;
188 dst += stride;
189 }
190 }
191
192 state = 0;
193 flag = rlen < 0xFFFF ? 1 : 0;
194 }
195
196 9 align_get_bits(bc);
197 9 return get_bits_count(bc) >> 3;
198 }
199
200 36 static int read_high_coeffs(AVCodecContext *avctx, const uint8_t *src, int16_t *dst,
201 int size, int c, int a, int d,
202 int width, ptrdiff_t stride)
203 {
204 36 PixletContext *ctx = avctx->priv_data;
205 36 GetBitContext *bc = &ctx->bc;
206 36 unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
207 36 int ret, escape, pfx, value, yflag, xflag, flag = 0;
208 36 int64_t state = 3, tmp;
209
210 36 ret = init_get_bits8(bc, src, bytestream2_get_bytes_left(&ctx->gb));
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (ret < 0)
212 return ret;
213
214
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (a ^ (a >> 31)) {
215 36 nbits = 33 - ff_clz(a ^ (a >> 31));
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (nbits > 16)
217 return AVERROR_INVALIDDATA;
218 } else {
219 nbits = 1;
220 }
221
222 36 length = 25 - nbits;
223
224
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 36 times.
751 while (i < size) {
225
1/2
✓ Branch 0 taken 715 times.
✗ Branch 1 not taken.
715 if (((state >> 8) + 3) & 0xFFFFFFF)
226 715 value = ff_clz((state >> 8) + 3) ^ 0x1F;
227 else
228 value = -1;
229
230 715 cnt1 = get_unary(bc, 0, length);
231
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 669 times.
715 if (cnt1 >= length) {
232 46 cnt1 = get_bits(bc, nbits);
233 } else {
234 669 pfx = FFMIN(value, 14);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 if (pfx < 1)
236 return AVERROR_INVALIDDATA;
237 669 cnt1 *= (1 << pfx) - 1;
238 669 shbits = show_bits(bc, pfx);
239
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 275 times.
669 if (shbits <= 1) {
240 394 skip_bits(bc, pfx - 1);
241 } else {
242 275 skip_bits(bc, pfx);
243 275 cnt1 += shbits - 1;
244 }
245 }
246
247 715 xflag = flag + cnt1;
248 715 yflag = xflag;
249
250
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 464 times.
715 if (flag + cnt1 == 0) {
251 251 value = 0;
252 } else {
253 464 xflag &= 1u;
254 464 tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
255 464 value = xflag + (tmp ^ -xflag);
256 }
257
258 715 i++;
259 715 dst[j++] = value;
260
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 630 times.
715 if (j == width) {
261 85 j = 0;
262 85 dst += stride;
263 }
264 715 state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);
265
266 715 flag = 0;
267
268
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 649 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 64 times.
715 if ((uint64_t)state > 0xFF / 4 || i >= size)
269 651 continue;
270
271
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 pfx = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
272 64 escape = av_zero_extend(16383, pfx);
273 64 cnt1 = get_unary(bc, 0, 8);
274
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 2 times.
64 if (cnt1 < 8) {
275
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (pfx < 1 || pfx > 25)
276 return AVERROR_INVALIDDATA;
277
278 62 value = show_bits(bc, pfx);
279
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 22 times.
62 if (value > 1) {
280 40 skip_bits(bc, pfx);
281 40 rlen = value + escape * cnt1 - 1;
282 } else {
283 22 skip_bits(bc, pfx - 1);
284 22 rlen = escape * cnt1;
285 }
286 } else {
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits1(bc))
288 value = get_bits(bc, 16);
289 else
290 2 value = get_bits(bc, 8);
291
292 2 rlen = value + 8 * escape;
293 }
294
295
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 if (rlen > 0xFFFF || i + rlen > size)
296 return AVERROR_INVALIDDATA;
297 64 i += rlen;
298
299
2/2
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 64 times.
879 for (k = 0; k < rlen; k++) {
300 815 dst[j++] = 0;
301
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 720 times.
815 if (j == width) {
302 95 j = 0;
303 95 dst += stride;
304 }
305 }
306
307 64 state = 0;
308 64 flag = rlen < 0xFFFF ? 1 : 0;
309 }
310
311 36 align_get_bits(bc);
312 36 return get_bits_count(bc) >> 3;
313 }
314
315 3 static int read_highpass(AVCodecContext *avctx, const uint8_t *ptr,
316 int plane, AVFrame *frame)
317 {
318 3 PixletContext *ctx = avctx->priv_data;
319 3 ptrdiff_t stride = frame->linesize[plane] / 2;
320 int i, ret;
321
322
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
39 for (i = 0; i < ctx->levels * 3; i++) {
323 36 int32_t a = bytestream2_get_be32(&ctx->gb);
324 36 int32_t b = bytestream2_get_be32(&ctx->gb);
325 36 int32_t c = bytestream2_get_be32(&ctx->gb);
326 36 int32_t d = bytestream2_get_be32(&ctx->gb);
327 36 int16_t *dest = (int16_t *)frame->data[plane] +
328 36 ctx->band[plane][i + 1].x +
329 36 ctx->band[plane][i + 1].y * stride;
330 36 unsigned size = ctx->band[plane][i + 1].size;
331 36 uint32_t magic = bytestream2_get_be32(&ctx->gb);
332
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (magic != PIXLET_MAGIC) {
334 av_log(avctx, AV_LOG_ERROR,
335 "wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
336 magic, plane, i);
337 return AVERROR_INVALIDDATA;
338 }
339
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (a == INT32_MIN)
341 return AVERROR_INVALIDDATA;
342
343 36 ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gb), dest, size,
344 36 c, (b >= FFABS(a)) ? b : a, d,
345
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 ctx->band[plane][i + 1].width, stride);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (ret < 0) {
347 av_log(avctx, AV_LOG_ERROR,
348 "error in highpass coefficients for plane %d, band %d\n",
349 plane, i);
350 return ret;
351 }
352 36 bytestream2_skip(&ctx->gb, ret);
353 }
354
355 3 return 0;
356 }
357
358 3 static void lowpass_prediction(int16_t *dst, int16_t *pred,
359 int width, int height, ptrdiff_t stride)
360 {
361 int16_t val;
362 int i, j;
363
364 3 memset(pred, 0, width * sizeof(*pred));
365
366
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 for (i = 0; i < height; i++) {
367 4 val = pred[0] + dst[0];
368 4 dst[0] = pred[0] = val;
369
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (j = 1; j < width; j++) {
370 2 val = pred[j] + dst[j];
371 2 dst[j] = pred[j] = val;
372 2 dst[j] += dst[j-1];
373 }
374 4 dst += stride;
375 }
376 3 }
377
378 240 static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
379 {
380 int16_t *low, *high, *ll, *lh, *hl, *hh;
381 int hsize, i, j;
382 int64_t value;
383
384 240 hsize = size >> 1;
385 240 low = tmp + 4;
386 240 high = &low[hsize + 8];
387
388 240 memcpy(low, dest, size);
389 240 memcpy(high, dest + hsize, size);
390
391 240 ll = &low[hsize];
392 240 lh = &low[hsize];
393 240 hl = &high[hsize];
394 240 hh = hl;
395
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 240 times.
1200 for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) {
396 960 low[i - 5] = low[j - 1];
397 960 lh[0] = ll[-1];
398 960 high[i - 5] = high[j - 2];
399 960 hh[0] = hl[-2];
400 }
401
402
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 240 times.
2280 for (i = 0; i < hsize; i++) {
403 2040 value = (int64_t) low [i + 1] * -INT64_C(325392907) +
404 2040 (int64_t) low [i + 0] * INT64_C(3687786320) +
405 2040 (int64_t) low [i - 1] * -INT64_C(325392907) +
406 2040 (int64_t) high[i + 0] * INT64_C(1518500249) +
407 2040 (int64_t) high[i - 1] * INT64_C(1518500249);
408 2040 dest[i * 2] = av_clip_int16(((value >> 32) * (uint64_t)scale) >> 32);
409 }
410
411
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 240 times.
2280 for (i = 0; i < hsize; i++) {
412 2040 value = (int64_t) low [i + 2] * -INT64_C(65078576) +
413 2040 (int64_t) low [i + 1] * INT64_C(1583578880) +
414 2040 (int64_t) low [i + 0] * INT64_C(1583578880) +
415 2040 (int64_t) low [i - 1] * -INT64_C(65078576) +
416 2040 (int64_t) high[i + 1] * INT64_C(303700064) +
417 2040 (int64_t) high[i + 0] * -INT64_C(3644400640) +
418 2040 (int64_t) high[i - 1] * INT64_C(303700064);
419 2040 dest[i * 2 + 1] = av_clip_int16(((value >> 32) * (uint64_t)scale) >> 32);
420 }
421 240 }
422
423 3 static void reconstruction(AVCodecContext *avctx, int16_t *dest,
424 unsigned width, unsigned height, ptrdiff_t stride,
425 int64_t *scaling_h, int64_t *scaling_v)
426 {
427 3 PixletContext *ctx = avctx->priv_data;
428 unsigned scaled_width, scaled_height;
429 int16_t *ptr, *tmp;
430 int i, j, k;
431
432 3 scaled_width = width >> NB_LEVELS;
433 3 scaled_height = height >> NB_LEVELS;
434 3 tmp = ctx->filter[0];
435
436
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (i = 0; i < NB_LEVELS; i++) {
437 12 int64_t scale_v = scaling_v[i];
438 12 int64_t scale_h = scaling_h[i];
439 12 scaled_width <<= 1;
440 12 scaled_height <<= 1;
441
442 12 ptr = dest;
443
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 12 times.
132 for (j = 0; j < scaled_height; j++) {
444 120 filterfn(ptr, ctx->filter[1], scaled_width, scale_v);
445 120 ptr += stride;
446 }
447
448
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 12 times.
132 for (j = 0; j < scaled_width; j++) {
449 120 ptr = dest + j;
450
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 120 times.
2160 for (k = 0; k < scaled_height; k++) {
451 2040 tmp[k] = *ptr;
452 2040 ptr += stride;
453 }
454
455 120 filterfn(tmp, ctx->filter[1], scaled_height, scale_h);
456
457 120 ptr = dest + j;
458
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 120 times.
2160 for (k = 0; k < scaled_height; k++) {
459 2040 *ptr = tmp[k];
460 2040 ptr += stride;
461 }
462 }
463 }
464 3 }
465
466 1 static void build_luma_lut(AVCodecContext *avctx, int depth)
467 {
468 1 PixletContext *ctx = avctx->priv_data;
469 1 int max = (1 << depth) - 1;
470
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ctx->depth == depth)
472 return;
473 1 ctx->depth = depth;
474
475
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 1 times.
65537 for (int i = 0; i < FF_ARRAY_ELEMS(ctx->lut); i++)
476 65536 ctx->lut[i] = ((int64_t)i * i * 65535LL) / max / max;
477 }
478
479 1 static void postprocess_luma(AVCodecContext *avctx, AVFrame *frame,
480 int w, int h, int depth)
481 {
482 1 PixletContext *ctx = avctx->priv_data;
483 1 uint16_t *dsty = (uint16_t *)frame->data[0];
484 1 int16_t *srcy = (int16_t *)frame->data[0];
485 1 ptrdiff_t stridey = frame->linesize[0] / 2;
486 1 uint16_t *lut = ctx->lut;
487 int i, j;
488
489
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
33 for (j = 0; j < h; j++) {
490
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 32 times.
1056 for (i = 0; i < w; i++) {
491
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1023 times.
1024 if (srcy[i] <= 0)
492 1 dsty[i] = 0;
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1023 times.
1023 else if (srcy[i] > ((1 << depth) - 1))
494 dsty[i] = 65535;
495 else
496 1023 dsty[i] = lut[srcy[i]];
497 }
498 32 dsty += stridey;
499 32 srcy += stridey;
500 }
501 1 }
502
503 1 static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
504 {
505 1 uint16_t *dstu = (uint16_t *)frame->data[1];
506 1 uint16_t *dstv = (uint16_t *)frame->data[2];
507 1 int16_t *srcu = (int16_t *)frame->data[1];
508 1 int16_t *srcv = (int16_t *)frame->data[2];
509 1 ptrdiff_t strideu = frame->linesize[1] / 2;
510 1 ptrdiff_t stridev = frame->linesize[2] / 2;
511 1 const unsigned add = 1 << (depth - 1);
512 1 const unsigned shift = 16 - depth;
513 int i, j;
514
515
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (j = 0; j < h; j++) {
516
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 16 times.
272 for (i = 0; i < w; i++) {
517 256 dstu[i] = av_clip_uintp2_c(add + srcu[i], depth) << shift;
518 256 dstv[i] = av_clip_uintp2_c(add + srcv[i], depth) << shift;
519 }
520 16 dstu += strideu;
521 16 dstv += stridev;
522 16 srcu += strideu;
523 16 srcv += stridev;
524 }
525 1 }
526
527 3 static int decode_plane(AVCodecContext *avctx, int plane,
528 const AVPacket *avpkt, AVFrame *frame)
529 {
530 3 PixletContext *ctx = avctx->priv_data;
531 3 ptrdiff_t stride = frame->linesize[plane] / 2;
532 3 unsigned shift = plane > 0;
533 int16_t *dst;
534 int i, ret;
535
536
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (i = ctx->levels - 1; i >= 0; i--) {
537 12 int32_t h = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
538 12 int32_t v = sign_extend(bytestream2_get_be32(&ctx->gb), 32);
539
540
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if (!h || !v)
541 return AVERROR_INVALIDDATA;
542
543 12 ctx->scaling[plane][H][i] = (1000000ULL << 32) / h;
544 12 ctx->scaling[plane][V][i] = (1000000ULL << 32) / v;
545 }
546
547 3 bytestream2_skip(&ctx->gb, 4);
548
549 3 dst = (int16_t *)frame->data[plane];
550 3 dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
551
552 3 ret = init_get_bits8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),
553 bytestream2_get_bytes_left(&ctx->gb));
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
555 return ret;
556
557 3 ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
558 3 ctx->band[plane][0].width - 1, 0);
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
560 av_log(avctx, AV_LOG_ERROR,
561 "error in lowpass coefficients for plane %d, top row\n", plane);
562 return ret;
563 }
564
565 3 ret = read_low_coeffs(avctx, dst + stride,
566 3 ctx->band[plane][0].height - 1, 1, stride);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
568 av_log(avctx, AV_LOG_ERROR,
569 "error in lowpass coefficients for plane %d, left column\n",
570 plane);
571 return ret;
572 }
573
574 3 ret = read_low_coeffs(avctx, dst + stride + 1,
575 3 (ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
576 3 ctx->band[plane][0].width - 1, stride);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
578 av_log(avctx, AV_LOG_ERROR,
579 "error in lowpass coefficients for plane %d, rest\n", plane);
580 return ret;
581 }
582
583 3 bytestream2_skip(&ctx->gb, ret);
584
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(&ctx->gb) <= 0) {
585 av_log(avctx, AV_LOG_ERROR, "no bytes left\n");
586 return AVERROR_INVALIDDATA;
587 }
588
589 3 ret = read_highpass(avctx, avpkt->data, plane, frame);
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
591 return ret;
592
593 3 lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
594 3 ctx->band[plane][0].height, stride);
595
596 3 reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
597 3 ctx->h >> shift, stride, ctx->scaling[plane][H],
598 3 ctx->scaling[plane][V]);
599
600 3 return 0;
601 }
602
603 1 static int pixlet_decode_frame(AVCodecContext *avctx, AVFrame *p,
604 int *got_frame, AVPacket *avpkt)
605 {
606 1 PixletContext *ctx = avctx->priv_data;
607 int i, w, h, width, height, ret, version;
608 uint32_t pktsize, depth;
609
610 1 bytestream2_init(&ctx->gb, avpkt->data, avpkt->size);
611
612 1 pktsize = bytestream2_get_be32(&ctx->gb);
613
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (pktsize <= 44 + (NB_LEVELS * 8 + 6) * 3 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gb)) {
614 av_log(avctx, AV_LOG_ERROR, "Invalid packet size %"PRIu32"\n", pktsize);
615 return AVERROR_INVALIDDATA;
616 }
617
618 1 version = bytestream2_get_le32(&ctx->gb);
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (version != 1)
620 avpriv_request_sample(avctx, "Version %d", version);
621
622 1 bytestream2_skip(&ctx->gb, 4);
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (bytestream2_get_be32(&ctx->gb) != 1)
624 return AVERROR_INVALIDDATA;
625 1 bytestream2_skip(&ctx->gb, 4);
626
627 1 width = bytestream2_get_be32(&ctx->gb);
628 1 height = bytestream2_get_be32(&ctx->gb);
629
630
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if ( width > INT_MAX - (1U << (NB_LEVELS + 1))
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 || height > INT_MAX - (1U << (NB_LEVELS + 1)))
632 return AVERROR_INVALIDDATA;
633
634 1 w = FFALIGN(width, 1 << (NB_LEVELS + 1));
635 1 h = FFALIGN(height, 1 << (NB_LEVELS + 1));
636
637 1 ctx->levels = bytestream2_get_be32(&ctx->gb);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ctx->levels != NB_LEVELS)
639 return AVERROR_INVALIDDATA;
640 1 depth = bytestream2_get_be32(&ctx->gb);
641
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (depth < 8 || depth > 15) {
642 avpriv_request_sample(avctx, "Depth %d", depth);
643 return AVERROR_INVALIDDATA;
644 }
645
646 1 build_luma_lut(avctx, depth);
647
648 1 ret = ff_set_dimensions(avctx, w, h);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
650 return ret;
651 1 avctx->width = width;
652 1 avctx->height = height;
653
654
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (ctx->w != w || ctx->h != h) {
655 1 free_buffers(avctx);
656 1 ctx->w = w;
657 1 ctx->h = h;
658
659 1 ret = init_decoder(avctx);
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
661 free_buffers(avctx);
662 ctx->w = 0;
663 ctx->h = 0;
664 return ret;
665 }
666 }
667
668 1 bytestream2_skip(&ctx->gb, 8);
669
670 1 p->color_range = AVCOL_RANGE_JPEG;
671
672 1 ret = ff_thread_get_buffer(avctx, p, 0);
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
674 return ret;
675
676
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < 3; i++) {
677 3 ret = decode_plane(avctx, i, avpkt, p);
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
679 return ret;
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->flags & AV_CODEC_FLAG_GRAY)
681 break;
682 }
683
684 1 postprocess_luma(avctx, p, ctx->w, ctx->h, ctx->depth);
685 1 postprocess_chroma(p, ctx->w >> 1, ctx->h >> 1, ctx->depth);
686
687 1 *got_frame = 1;
688
689 1 return pktsize;
690 }
691
692 const FFCodec ff_pixlet_decoder = {
693 .p.name = "pixlet",
694 CODEC_LONG_NAME("Apple Pixlet"),
695 .p.type = AVMEDIA_TYPE_VIDEO,
696 .p.id = AV_CODEC_ID_PIXLET,
697 .init = pixlet_init,
698 .close = pixlet_close,
699 FF_CODEC_DECODE_CB(pixlet_decode_frame),
700 .priv_data_size = sizeof(PixletContext),
701 .p.capabilities = AV_CODEC_CAP_DR1 |
702 AV_CODEC_CAP_FRAME_THREADS,
703 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
704 };
705