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