FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/g2meet.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 775 903 85.8%
Functions: 32 32 100.0%
Branches: 473 600 78.8%

Line Branch Exec Source
1 /*
2 * Go2Webinar / Go2Meeting decoder
3 * Copyright (c) 2012 Konstantin Shishkov
4 * Copyright (c) 2013 Maxim Poliakovski
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Go2Webinar / Go2Meeting decoder
26 */
27
28 #include <inttypes.h>
29 #include <zlib.h>
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/mem_internal.h"
35
36 #include "avcodec.h"
37 #include "blockdsp.h"
38 #include "bytestream.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "elsdec.h"
42 #include "get_bits.h"
43 #include "idctdsp.h"
44 #include "jpegtables.h"
45 #include "mjpegdec.h"
46
47 #define EPIC_PIX_STACK_SIZE 1024
48 #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1)
49
50 enum ChunkType {
51 DISPLAY_INFO = 0xC8,
52 TILE_DATA,
53 CURSOR_POS,
54 CURSOR_SHAPE,
55 CHUNK_CC,
56 CHUNK_CD
57 };
58
59 enum Compression {
60 COMPR_EPIC_J_B = 2,
61 COMPR_KEMPF_J_B,
62 };
63
64 /* These tables are already permuted according to ff_zigzag_direct */
65 static const uint8_t luma_quant[64] = {
66 8, 6, 6, 7, 6, 5, 8, 7,
67 7, 7, 9, 9, 8, 10, 12, 20,
68 13, 12, 11, 11, 12, 25, 18, 19,
69 15, 20, 29, 26, 31, 30, 29, 26,
70 28, 28, 32, 36, 46, 39, 32, 34,
71 44, 35, 28, 28, 40, 55, 41, 44,
72 48, 49, 52, 52, 52, 31, 39, 57,
73 61, 56, 50, 60, 46, 51, 52, 50,
74 };
75
76 static const uint8_t chroma_quant[64] = {
77 9, 9, 9, 12, 11, 12, 24, 13,
78 13, 24, 50, 33, 28, 33, 50, 50,
79 50, 50, 50, 50, 50, 50, 50, 50,
80 50, 50, 50, 50, 50, 50, 50, 50,
81 50, 50, 50, 50, 50, 50, 50, 50,
82 50, 50, 50, 50, 50, 50, 50, 50,
83 50, 50, 50, 50, 50, 50, 50, 50,
84 50, 50, 50, 50, 50, 50, 50, 50,
85 };
86
87 typedef struct ePICPixListElem {
88 struct ePICPixListElem *next;
89 uint32_t pixel;
90 uint8_t rung;
91 } ePICPixListElem;
92
93 typedef struct ePICPixHashElem {
94 uint32_t pix_id;
95 struct ePICPixListElem *list;
96 } ePICPixHashElem;
97
98 #define EPIC_HASH_SIZE 256
99 typedef struct ePICPixHash {
100 ePICPixHashElem *bucket[EPIC_HASH_SIZE];
101 int bucket_size[EPIC_HASH_SIZE];
102 int bucket_fill[EPIC_HASH_SIZE];
103 } ePICPixHash;
104
105 typedef struct ePICContext {
106 ElsDecCtx els_ctx;
107 int next_run_pos;
108 ElsUnsignedRung unsigned_rung;
109 uint8_t W_flag_rung;
110 uint8_t N_flag_rung;
111 uint8_t W_ctx_rung[256];
112 uint8_t N_ctx_rung[512];
113 uint8_t nw_pred_rung[256];
114 uint8_t ne_pred_rung[256];
115 uint8_t prev_row_rung[14];
116 uint8_t runlen_zeroes[14];
117 uint8_t runlen_one;
118 int stack_pos;
119 uint32_t stack[EPIC_PIX_STACK_SIZE];
120 ePICPixHash hash;
121 } ePICContext;
122
123 typedef struct JPGContext {
124 BlockDSPContext bdsp;
125 IDCTDSPContext idsp;
126 uint8_t permutated_scantable[64];
127
128 VLC dc_vlc[2], ac_vlc[2];
129 int prev_dc[3];
130 DECLARE_ALIGNED(32, int16_t, block)[6][64];
131
132 uint8_t *buf;
133 } JPGContext;
134
135 typedef struct G2MContext {
136 ePICContext ec;
137 JPGContext jc;
138
139 int version;
140
141 int compression;
142 int width, height, bpp;
143 int orig_width, orig_height;
144 int tile_width, tile_height;
145 int tiles_x, tiles_y, tile_x, tile_y;
146
147 int got_header;
148
149 uint8_t *framebuf;
150 int framebuf_stride;
151 unsigned int framebuf_allocated;
152
153 uint8_t *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
154 int tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
155 int swapuv;
156
157 uint8_t *kempf_buf, *kempf_flags;
158
159 uint8_t *cursor;
160 int cursor_stride;
161 int cursor_fmt;
162 int cursor_w, cursor_h, cursor_x, cursor_y;
163 int cursor_hot_x, cursor_hot_y;
164 } G2MContext;
165
166 6 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
167 {
168 int ret;
169
170 6 ret = ff_mjpeg_build_vlc(&c->dc_vlc[0], ff_mjpeg_bits_dc_luminance,
171 ff_mjpeg_val_dc, 0, avctx);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret)
173 return ret;
174 6 ret = ff_mjpeg_build_vlc(&c->dc_vlc[1], ff_mjpeg_bits_dc_chrominance,
175 ff_mjpeg_val_dc, 0, avctx);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret)
177 return ret;
178 6 ret = ff_mjpeg_build_vlc(&c->ac_vlc[0], ff_mjpeg_bits_ac_luminance,
179 ff_mjpeg_val_ac_luminance, 1, avctx);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret)
181 return ret;
182 6 ret = ff_mjpeg_build_vlc(&c->ac_vlc[1], ff_mjpeg_bits_ac_chrominance,
183 ff_mjpeg_val_ac_chrominance, 1, avctx);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret)
185 return ret;
186
187 6 ff_blockdsp_init(&c->bdsp);
188 6 ff_idctdsp_init(&c->idsp, avctx);
189 6 ff_permute_scantable(c->permutated_scantable, ff_zigzag_direct,
190 6 c->idsp.idct_permutation);
191
192 6 return 0;
193 }
194
195 6 static av_cold void jpg_free_context(JPGContext *ctx)
196 {
197 int i;
198
199
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (i = 0; i < 2; i++) {
200 12 ff_vlc_free(&ctx->dc_vlc[i]);
201 12 ff_vlc_free(&ctx->ac_vlc[i]);
202 }
203
204 6 av_freep(&ctx->buf);
205 6 }
206
207 159 static void jpg_unescape(const uint8_t *src, int src_size,
208 uint8_t *dst, int *dst_size)
209 {
210 159 const uint8_t *src_end = src + src_size;
211 159 uint8_t *dst_start = dst;
212
213
2/2
✓ Branch 0 taken 155059 times.
✓ Branch 1 taken 159 times.
155218 while (src < src_end) {
214 155059 uint8_t x = *src++;
215
216 155059 *dst++ = x;
217
218
3/4
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 154166 times.
✓ Branch 2 taken 893 times.
✗ Branch 3 not taken.
155059 if (x == 0xFF && !*src)
219 893 src++;
220 }
221 159 *dst_size = dst - dst_start;
222 159 }
223
224 38284 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
225 int plane, int16_t *block)
226 {
227 int dc, val, pos;
228 38284 const int is_chroma = !!plane;
229
2/2
✓ Branch 0 taken 13158 times.
✓ Branch 1 taken 25126 times.
38284 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
230
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38284 times.
38284 if (get_bits_left(gb) < 1)
232 return AVERROR_INVALIDDATA;
233
234 38284 c->bdsp.clear_block(block);
235 38284 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 2);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38284 times.
38284 if (dc < 0)
237 return AVERROR_INVALIDDATA;
238
2/2
✓ Branch 0 taken 22214 times.
✓ Branch 1 taken 16070 times.
38284 if (dc)
239 22214 dc = get_xbits(gb, dc);
240 38284 dc = dc * qmat[0] + c->prev_dc[plane];
241 38284 block[0] = dc;
242 38284 c->prev_dc[plane] = dc;
243
244 38284 pos = 0;
245
2/2
✓ Branch 0 taken 224976 times.
✓ Branch 1 taken 382 times.
225358 while (pos < 63) {
246 224976 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 2);
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224976 times.
224976 if (val < 0)
248 return AVERROR_INVALIDDATA;
249 224976 pos += val >> 4;
250 224976 val &= 0xF;
251
2/2
✓ Branch 0 taken 37902 times.
✓ Branch 1 taken 187074 times.
224976 if (pos > 63)
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37902 times.
37902 return val ? AVERROR_INVALIDDATA : 0;
253
2/2
✓ Branch 0 taken 186680 times.
✓ Branch 1 taken 394 times.
187074 if (val) {
254 186680 int nbits = val;
255
256 186680 val = get_xbits(gb, nbits);
257 186680 val *= qmat[pos];
258 186680 block[c->permutated_scantable[pos]] = val;
259 }
260 }
261 382 return 0;
262 }
263
264 1684224 static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
265 {
266 1684224 out[ridx] = av_clip_uint8(Y + (91881 * V + 32768 >> 16));
267 1684224 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
268 1684224 out[2 - ridx] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
269 1684224 }
270
271 159 static int jpg_decode_data(JPGContext *c, int width, int height,
272 const uint8_t *src, int src_size,
273 uint8_t *dst, int dst_stride,
274 const uint8_t *mask, int mask_stride, int num_mbs,
275 int swapuv)
276 {
277 GetBitContext gb;
278 int mb_w, mb_h, mb_x, mb_y, i, j;
279 int bx, by;
280 int unesc_size;
281 int ret;
282
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 109 times.
159 const int ridx = swapuv ? 2 : 0;
283
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 if ((ret = av_reallocp(&c->buf,
285 159 src_size + AV_INPUT_BUFFER_PADDING_SIZE)) < 0)
286 return ret;
287 159 jpg_unescape(src, src_size, c->buf, &unesc_size);
288 159 memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
159 if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
290 return ret;
291
292 159 width = FFALIGN(width, 16);
293 159 mb_w = width >> 4;
294 159 mb_h = (height + 15) >> 4;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 if (!num_mbs)
297 num_mbs = mb_w * mb_h * 4;
298
299
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 159 times.
636 for (i = 0; i < 3; i++)
300 477 c->prev_dc[i] = 1024;
301 159 bx =
302 159 by = 0;
303 159 c->bdsp.clear_blocks(c->block[0]);
304
1/2
✓ Branch 0 taken 1069 times.
✗ Branch 1 not taken.
1069 for (mb_y = 0; mb_y < mb_h; mb_y++) {
305
2/2
✓ Branch 0 taken 11016 times.
✓ Branch 1 taken 910 times.
11926 for (mb_x = 0; mb_x < mb_w; mb_x++) {
306
5/6
✓ Branch 0 taken 11016 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4671 times.
✓ Branch 3 taken 6345 times.
✓ Branch 4 taken 4574 times.
✓ Branch 5 taken 97 times.
11016 if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
307
2/2
✓ Branch 0 taken 4458 times.
✓ Branch 1 taken 116 times.
4574 !mask[mb_x * 2 + mask_stride] &&
308
2/2
✓ Branch 0 taken 4437 times.
✓ Branch 1 taken 21 times.
4458 !mask[mb_x * 2 + 1 + mask_stride]) {
309 4437 bx += 16;
310 4437 continue;
311 }
312
2/2
✓ Branch 0 taken 13158 times.
✓ Branch 1 taken 6579 times.
19737 for (j = 0; j < 2; j++) {
313
2/2
✓ Branch 0 taken 26316 times.
✓ Branch 1 taken 13158 times.
39474 for (i = 0; i < 2; i++) {
314
3/4
✓ Branch 0 taken 26316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1190 times.
✓ Branch 3 taken 25126 times.
26316 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
315 1190 continue;
316 25126 num_mbs--;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25126 times.
25126 if ((ret = jpg_decode_block(c, &gb, 0,
318 25126 c->block[i + j * 2])) != 0)
319 return ret;
320 25126 c->idsp.idct(c->block[i + j * 2]);
321 }
322 }
323
2/2
✓ Branch 0 taken 13158 times.
✓ Branch 1 taken 6579 times.
19737 for (i = 1; i < 3; i++) {
324
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13158 times.
13158 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
325 return ret;
326 13158 c->idsp.idct(c->block[i + 3]);
327 }
328
329
2/2
✓ Branch 0 taken 105264 times.
✓ Branch 1 taken 6579 times.
111843 for (j = 0; j < 16; j++) {
330 105264 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
331
2/2
✓ Branch 0 taken 1684224 times.
✓ Branch 1 taken 105264 times.
1789488 for (i = 0; i < 16; i++) {
332 int Y, U, V;
333
334 1684224 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
335 1684224 U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
336 1684224 V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
337 1684224 yuv2rgb(out + i * 3, ridx, Y, U, V);
338 }
339 }
340
341
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 6420 times.
6579 if (!num_mbs)
342 159 return 0;
343 6420 bx += 16;
344 }
345 910 bx = 0;
346 910 by += 16;
347
1/2
✓ Branch 0 taken 910 times.
✗ Branch 1 not taken.
910 if (mask)
348 910 mask += mask_stride * 2;
349 }
350
351 return 0;
352 }
353
354 #define LOAD_NEIGHBOURS(x) \
355 W = curr_row[(x) - 1]; \
356 N = above_row[(x)]; \
357 WW = curr_row[(x) - 2]; \
358 NW = above_row[(x) - 1]; \
359 NE = above_row[(x) + 1]; \
360 NN = above2_row[(x)]; \
361 NNW = above2_row[(x) - 1]; \
362 NWW = above_row[(x) - 2]; \
363 NNE = above2_row[(x) + 1]
364
365 #define UPDATE_NEIGHBOURS(x) \
366 NNW = NN; \
367 NN = NNE; \
368 NWW = NW; \
369 NW = N; \
370 N = NE; \
371 NE = above_row[(x) + 1]; \
372 NNE = above2_row[(x) + 1]
373
374 #define R_shift 16
375 #define G_shift 8
376 #define B_shift 0
377
378 /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
379 97781 static int djb2_hash(uint32_t key)
380 {
381 97781 uint32_t h = 5381;
382
383 97781 h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
384 97781 h = (h * 33) ^ ((key >> 16) & 0xFF);
385 97781 h = (h * 33) ^ ((key >> 8) & 0xFF);
386 97781 h = (h * 33) ^ (key & 0xFF);
387
388 97781 return h & (EPIC_HASH_SIZE - 1);
389 }
390
391 136 static void epic_hash_init(ePICPixHash *hash)
392 {
393 136 memset(hash, 0, sizeof(*hash));
394 136 }
395
396 97247 static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
397 {
398 97247 int i, idx = djb2_hash(key);
399 97247 ePICPixHashElem *bucket = hash->bucket[idx];
400
401
2/2
✓ Branch 0 taken 94232 times.
✓ Branch 1 taken 3067 times.
97299 for (i = 0; i < hash->bucket_fill[idx]; i++)
402
2/2
✓ Branch 0 taken 94180 times.
✓ Branch 1 taken 52 times.
94232 if (bucket[i].pix_id == key)
403 94180 return &bucket[i];
404
405 3067 return NULL;
406 }
407
408 534 static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
409 {
410 ePICPixHashElem *bucket, *ret;
411 534 int idx = djb2_hash(key);
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534 times.
534 if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
414 return NULL;
415
416
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 3 times.
534 if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
417 531 int new_size = hash->bucket_size[idx] + 16;
418 531 bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 531 times.
531 if (!bucket)
420 return NULL;
421 531 hash->bucket[idx] = bucket;
422 531 hash->bucket_size[idx] = new_size;
423 }
424
425 534 ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
426 534 memset(ret, 0, sizeof(*ret));
427 534 ret->pix_id = key;
428 534 return ret;
429 }
430
431 1658 static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
432 {
433 ePICPixListElem *new_elem;
434 1658 ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
435
436
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 1124 times.
1658 if (!hash_elem) {
437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 534 times.
534 if (!(hash_elem = epic_hash_add(hash, key)))
438 return AVERROR(ENOMEM);
439 }
440
441 1658 new_elem = av_mallocz(sizeof(*new_elem));
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1658 times.
1658 if (!new_elem)
443 return AVERROR(ENOMEM);
444
445 1658 new_elem->pixel = pix;
446 1658 new_elem->next = hash_elem->list;
447 1658 hash_elem->list = new_elem;
448
449 1658 return 0;
450 }
451
452 41955 static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
453 uint32_t pix)
454 {
455 41955 ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
456
457
3/4
✓ Branch 0 taken 39956 times.
✓ Branch 1 taken 1999 times.
✓ Branch 2 taken 39956 times.
✗ Branch 3 not taken.
41955 if (hash_elem != NULL && hash_elem->list != NULL)
458 39956 return 1;
459
460 1999 return 0;
461 }
462
463 136 static void epic_free_pixel_cache(ePICPixHash *hash)
464 {
465 int i, j;
466
467
2/2
✓ Branch 0 taken 34816 times.
✓ Branch 1 taken 136 times.
34952 for (i = 0; i < EPIC_HASH_SIZE; i++) {
468
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 34816 times.
35350 for (j = 0; j < hash->bucket_fill[i]; j++) {
469 534 ePICPixListElem *list_elem = hash->bucket[i][j].list;
470
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 534 times.
2192 while (list_elem) {
471 1658 ePICPixListElem *tmp = list_elem->next;
472 1658 av_free(list_elem);
473 1658 list_elem = tmp;
474 }
475 }
476 34816 av_freep(&hash->bucket[i]);
477 34816 hash->bucket_size[i] =
478 34816 hash->bucket_fill[i] = 0;
479 }
480 136 }
481
482 510904 static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
483 {
484 int i;
485 510904 int n = FFMIN(dc->stack_pos, EPIC_PIX_STACK_SIZE);
486
487
2/2
✓ Branch 0 taken 806184 times.
✓ Branch 1 taken 357504 times.
1163688 for (i = 0; i < n; i++)
488
2/2
✓ Branch 0 taken 153400 times.
✓ Branch 1 taken 652784 times.
806184 if (dc->stack[i] == pix)
489 153400 break;
490
491 510904 return i != n;
492 }
493
494 #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
495
496 9558 static inline int epic_decode_component_pred(ePICContext *dc,
497 int N, int W, int NW)
498 {
499 9558 unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
500 9558 return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
501 }
502
503 4739 static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
504 const uint32_t *curr_row,
505 const uint32_t *above_row)
506 {
507 uint32_t N, W, NW, pred;
508 unsigned delta;
509 int GN, GW, GNW, R, G, B;
510
511
4/4
✓ Branch 0 taken 3657 times.
✓ Branch 1 taken 1082 times.
✓ Branch 2 taken 3186 times.
✓ Branch 3 taken 471 times.
4739 if (x && y) {
512 3186 W = curr_row[x - 1];
513 3186 N = above_row[x];
514 3186 NW = above_row[x - 1];
515
516 3186 GN = (N >> G_shift) & 0xFF;
517 3186 GW = (W >> G_shift) & 0xFF;
518 3186 GNW = (NW >> G_shift) & 0xFF;
519
520 3186 G = epic_decode_component_pred(dc, GN, GW, GNW);
521
522 6372 R = G + epic_decode_component_pred(dc,
523 3186 ((N >> R_shift) & 0xFF) - GN,
524 3186 ((W >> R_shift) & 0xFF) - GW,
525 3186 ((NW >> R_shift) & 0xFF) - GNW);
526
527 3186 B = G + epic_decode_component_pred(dc,
528 3186 ((N >> B_shift) & 0xFF) - GN,
529 3186 ((W >> B_shift) & 0xFF) - GW,
530 3186 ((NW >> B_shift) & 0xFF) - GNW);
531 } else {
532
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 1082 times.
1553 if (x)
533 471 pred = curr_row[x - 1];
534 else
535 1082 pred = above_row[x];
536
537 1553 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
538 1553 R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
539
540 1553 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
541 1553 G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
542
543 1553 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
544 1553 B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
545 }
546
547
6/12
✓ Branch 0 taken 4739 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4739 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4739 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4739 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4739 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 4739 times.
4739 if (R<0 || G<0 || B<0 || R > 255 || G > 255 || B > 255) {
548 avpriv_request_sample(NULL, "RGB %d %d %d (out of range)", R, G, B);
549 return 0;
550 }
551
552 4739 return (R << R_shift) | (G << G_shift) | (B << B_shift);
553 }
554
555 99449 static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
556 uint32_t *pPix, uint32_t pix)
557 {
558
2/2
✓ Branch 1 taken 91084 times.
✓ Branch 2 taken 8365 times.
99449 if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
559 91084 *pPix = pix;
560 91084 return 1;
561 }
562 8365 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
563 8365 return 0;
564 }
565
566 95756 static int epic_handle_edges(ePICContext *dc, int x, int y,
567 const uint32_t *curr_row,
568 const uint32_t *above_row, uint32_t *pPix)
569 {
570 uint32_t pix;
571
572
4/4
✓ Branch 0 taken 17184 times.
✓ Branch 1 taken 78572 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 17048 times.
95756 if (!x && !y) { /* special case: top-left pixel */
573 /* the top-left pixel is coded independently with 3 unsigned numbers */
574 136 *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
575 136 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
576 136 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
577 136 return 1;
578 }
579
580
2/2
✓ Branch 0 taken 78572 times.
✓ Branch 1 taken 17048 times.
95620 if (x) { /* predict from W first */
581 78572 pix = curr_row[x - 1];
582
2/2
✓ Branch 1 taken 72049 times.
✓ Branch 2 taken 6523 times.
78572 if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
583 72049 return 1;
584 }
585
586
2/2
✓ Branch 0 taken 22080 times.
✓ Branch 1 taken 1491 times.
23571 if (y) { /* then try to predict from N */
587 22080 pix = above_row[x];
588
4/4
✓ Branch 0 taken 5032 times.
✓ Branch 1 taken 17048 times.
✓ Branch 2 taken 3829 times.
✓ Branch 3 taken 1203 times.
22080 if (!dc->stack_pos || dc->stack[0] != pix) {
589
2/2
✓ Branch 1 taken 19035 times.
✓ Branch 2 taken 1842 times.
20877 if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
590 19035 return 1;
591 }
592 }
593
594 4536 return 0;
595 }
596
597 218240 static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
598 const uint32_t *curr_row,
599 const uint32_t *above_row,
600 const uint32_t *above2_row,
601 uint32_t *pPix, int *pRun)
602 {
603 218240 int idx, got_pixel = 0, WWneW, old_WWneW = 0;
604 uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
605
606 218240 *pRun = 0;
607
608 218240 LOAD_NEIGHBOURS(x);
609
610
2/2
✓ Branch 0 taken 199455 times.
✓ Branch 1 taken 18785 times.
218240 if (dc->next_run_pos == x) {
611 /* can't reuse W for the new pixel in this case */
612 199455 WWneW = 1;
613 } else {
614
2/2
✓ Branch 0 taken 3742 times.
✓ Branch 1 taken 15043 times.
18785 idx = (WW != W) << 7 |
615
2/2
✓ Branch 0 taken 1686 times.
✓ Branch 1 taken 17099 times.
18785 (NW != W) << 6 |
616
2/2
✓ Branch 0 taken 1703 times.
✓ Branch 1 taken 17082 times.
18785 (N != NE) << 5 |
617
2/2
✓ Branch 0 taken 2227 times.
✓ Branch 1 taken 16558 times.
18785 (NW != N) << 4 |
618
2/2
✓ Branch 0 taken 2155 times.
✓ Branch 1 taken 16630 times.
18785 (NWW != NW) << 3 |
619
2/2
✓ Branch 0 taken 1969 times.
✓ Branch 1 taken 16816 times.
18785 (NNE != NE) << 2 |
620
2/2
✓ Branch 0 taken 1883 times.
✓ Branch 1 taken 16902 times.
18785 (NN != N) << 1 |
621 18785 (NNW != NW);
622 18785 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18785 times.
18785 if (WWneW < 0)
624 return WWneW;
625 }
626
627
2/2
✓ Branch 0 taken 201691 times.
✓ Branch 1 taken 16549 times.
218240 if (WWneW)
628 201691 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
629 else {
630 16549 *pPix = W;
631 16549 got_pixel = 1;
632 }
633
634 do {
635 503572 int NWneW = 1;
636
2/2
✓ Branch 0 taken 237740 times.
✓ Branch 1 taken 265832 times.
503572 if (got_pixel) // pixel value already known (derived from either W or N)
637 237740 NWneW = *pPix != N;
638 else { // pixel value is unknown and will be decoded later
639
2/2
✓ Branch 0 taken 201691 times.
✓ Branch 1 taken 64141 times.
265832 NWneW = *pRun ? NWneW : NW != W;
640
641 /* TODO: RFC this mess! */
642
4/5
✓ Branch 0 taken 126306 times.
✓ Branch 1 taken 139526 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 176548 times.
✓ Branch 4 taken 89284 times.
265832 switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
643 case 0:
644 break; // do nothing here
645 176548 case 3:
646 case 5:
647 case 6:
648 case 7:
649
2/2
✓ Branch 1 taken 152558 times.
✓ Branch 2 taken 23990 times.
176548 if (!is_pixel_on_stack(dc, N)) {
650 457674 idx = WWneW << 8 |
651
4/4
✓ Branch 0 taken 131415 times.
✓ Branch 1 taken 21143 times.
✓ Branch 2 taken 73563 times.
✓ Branch 3 taken 57852 times.
152558 (*pRun ? old_WWneW : WW != W) << 7 |
652 305116 NWneW << 6 |
653
2/2
✓ Branch 0 taken 56675 times.
✓ Branch 1 taken 95883 times.
152558 (N != NE) << 5 |
654
2/2
✓ Branch 0 taken 102316 times.
✓ Branch 1 taken 50242 times.
152558 (NW != N) << 4 |
655
2/2
✓ Branch 0 taken 63177 times.
✓ Branch 1 taken 89381 times.
152558 (NWW != NW) << 3 |
656
2/2
✓ Branch 0 taken 40781 times.
✓ Branch 1 taken 111777 times.
152558 (NNE != NE) << 2 |
657
2/2
✓ Branch 0 taken 48988 times.
✓ Branch 1 taken 103570 times.
152558 (NN != N) << 1 |
658 152558 (NNW != NW);
659
2/2
✓ Branch 1 taken 129319 times.
✓ Branch 2 taken 23239 times.
152558 if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
660 129319 NWneW = 0;
661 129319 *pPix = N;
662 129319 got_pixel = 1;
663 129319 break;
664 }
665 }
666 av_fallthrough;
667 default:
668 136513 NWneW = 1;
669 136513 old_WWneW = WWneW;
670
2/2
✓ Branch 1 taken 23239 times.
✓ Branch 2 taken 113274 times.
136513 if (!is_pixel_on_stack(dc, N))
671 23239 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
672 }
673 }
674
675 503572 (*pRun)++;
676
2/2
✓ Branch 0 taken 3378 times.
✓ Branch 1 taken 500194 times.
503572 if (x + *pRun >= tile_width - 1)
677 3378 break;
678
679 500194 UPDATE_NEIGHBOURS(x + *pRun);
680
681
6/6
✓ Branch 0 taken 233276 times.
✓ Branch 1 taken 266918 times.
✓ Branch 2 taken 123216 times.
✓ Branch 3 taken 110060 times.
✓ Branch 4 taken 99184 times.
✓ Branch 5 taken 24032 times.
500194 if (!NWneW && NW == N && N == NE) {
682 int pos, run, rle;
683 99184 int start_pos = x + *pRun;
684
685 /* scan for a run of pix in the line above */
686 99184 uint32_t pix = above_row[start_pos + 1];
687
2/2
✓ Branch 0 taken 2496269 times.
✓ Branch 1 taken 15422 times.
2511691 for (pos = start_pos + 2; pos < tile_width; pos++)
688
2/2
✓ Branch 0 taken 83762 times.
✓ Branch 1 taken 2412507 times.
2496269 if (!(above_row[pos] == pix))
689 83762 break;
690 99184 run = pos - start_pos - 1;
691 99184 idx = av_ceil_log2(run);
692
2/2
✓ Branch 1 taken 66738 times.
✓ Branch 2 taken 32446 times.
99184 if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
693 66738 *pRun += run;
694 else {
695 int flag;
696 /* run-length is coded as plain binary number of idx - 1 bits */
697
2/2
✓ Branch 0 taken 99317 times.
✓ Branch 1 taken 32446 times.
131763 for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
698
4/4
✓ Branch 0 taken 94613 times.
✓ Branch 1 taken 4704 times.
✓ Branch 2 taken 33844 times.
✓ Branch 3 taken 60769 times.
193930 if ((1 << pos) + rle < run &&
699
2/2
✓ Branch 0 taken 25102 times.
✓ Branch 1 taken 69511 times.
94613 ff_els_decode_bit(&dc->els_ctx,
700 flag ? &dc->runlen_one
701 : &dc->runlen_zeroes[pos])) {
702 33844 flag = 1;
703 33844 rle |= 1 << pos;
704 }
705 }
706 32446 *pRun += rle;
707 32446 break; // return immediately
708 }
709
2/2
✓ Branch 0 taken 13519 times.
✓ Branch 1 taken 53219 times.
66738 if (x + *pRun >= tile_width - 1)
710 13519 break;
711
712 53219 LOAD_NEIGHBOURS(x + *pRun);
713 53219 WWneW = 0;
714 53219 NWneW = 0;
715 }
716
717 908458 idx = WWneW << 7 |
718 908458 NWneW << 6 |
719
2/2
✓ Branch 0 taken 201628 times.
✓ Branch 1 taken 252601 times.
454229 (N != NE) << 5 |
720
2/2
✓ Branch 0 taken 201195 times.
✓ Branch 1 taken 253034 times.
454229 (NW != N) << 4 |
721
2/2
✓ Branch 0 taken 144163 times.
✓ Branch 1 taken 310066 times.
454229 (NWW != NW) << 3 |
722
2/2
✓ Branch 0 taken 185477 times.
✓ Branch 1 taken 268752 times.
454229 (NNE != NE) << 2 |
723
2/2
✓ Branch 0 taken 187945 times.
✓ Branch 1 taken 266284 times.
454229 (NN != N) << 1 |
724 454229 (NNW != NW);
725 454229 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
726
2/2
✓ Branch 0 taken 285332 times.
✓ Branch 1 taken 168897 times.
454229 } while (!WWneW);
727
728 218240 dc->next_run_pos = x + *pRun;
729 218240 return got_pixel;
730 }
731
732 36049 static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
733 uint32_t *pPix, uint32_t pix)
734 {
735
2/2
✓ Branch 1 taken 22192 times.
✓ Branch 2 taken 13857 times.
36049 if (ff_els_decode_bit(&dc->els_ctx, rung)) {
736 22192 *pPix = pix;
737 22192 return 1;
738 }
739 13857 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
740 13857 return 0;
741 }
742
743 76908 static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
744 int tile_width, const uint32_t *curr_row,
745 const uint32_t *above_row, uint32_t *pPix)
746 {
747 int pos;
748
749 /* try to reuse the NW pixel first */
750
4/4
✓ Branch 0 taken 75465 times.
✓ Branch 1 taken 1443 times.
✓ Branch 2 taken 73974 times.
✓ Branch 3 taken 1491 times.
76908 if (x && y) {
751 73974 uint32_t NW = above_row[x - 1];
752
6/6
✓ Branch 0 taken 26314 times.
✓ Branch 1 taken 47660 times.
✓ Branch 2 taken 20304 times.
✓ Branch 3 taken 6010 times.
✓ Branch 5 taken 20189 times.
✓ Branch 6 taken 115 times.
73974 if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
753
2/2
✓ Branch 1 taken 12191 times.
✓ Branch 2 taken 7998 times.
20189 if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
754 12191 return 1;
755 }
756 }
757
758 /* try to reuse the NE[x + run, y] pixel */
759 64717 pos = x + run - 1;
760
4/4
✓ Branch 0 taken 64303 times.
✓ Branch 1 taken 414 times.
✓ Branch 2 taken 62826 times.
✓ Branch 3 taken 1477 times.
64717 if (pos < tile_width - 1 && y) {
761 62826 uint32_t NE = above_row[pos + 1];
762
4/4
✓ Branch 0 taken 21217 times.
✓ Branch 1 taken 41609 times.
✓ Branch 3 taken 15860 times.
✓ Branch 4 taken 5357 times.
62826 if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
763
2/2
✓ Branch 1 taken 10001 times.
✓ Branch 2 taken 5859 times.
15860 if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
764 10001 return 1;
765 }
766 }
767
768 54716 return 0;
769 }
770
771 53634 static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
772 {
773 53634 ePICPixListElem *list, *prev = NULL;
774 53634 ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
775
776
3/4
✓ Branch 0 taken 53100 times.
✓ Branch 1 taken 534 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 53100 times.
53634 if (!hash_elem || !hash_elem->list)
777 534 return 0;
778
779 53100 list = hash_elem->list;
780
2/2
✓ Branch 0 taken 153582 times.
✓ Branch 1 taken 1124 times.
154706 while (list) {
781
2/2
✓ Branch 1 taken 142918 times.
✓ Branch 2 taken 10664 times.
153582 if (!is_pixel_on_stack(dc, list->pixel)) {
782
2/2
✓ Branch 1 taken 51976 times.
✓ Branch 2 taken 90942 times.
142918 if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
783 51976 *pPix = list->pixel;
784
2/2
✓ Branch 0 taken 36609 times.
✓ Branch 1 taken 15367 times.
51976 if (list != hash_elem->list) {
785 36609 prev->next = list->next;
786 36609 list->next = hash_elem->list;
787 36609 hash_elem->list = list;
788 }
789 51976 return 1;
790 }
791 90942 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
792 }
793 101606 prev = list;
794 101606 list = list->next;
795 }
796
797 1124 return 0;
798 }
799
800 136 static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
801 int tile_width, int stride)
802 {
803 int x, y;
804 uint32_t pix;
805 136 uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
806
807
2/2
✓ Branch 0 taken 17184 times.
✓ Branch 1 taken 136 times.
17320 for (y = 0; y < tile_height; y++, out += stride) {
808 17184 above2_row = above_row;
809 17184 above_row = curr_row;
810 17184 curr_row = (uint32_t *) out;
811
812
2/2
✓ Branch 0 taken 315995 times.
✓ Branch 1 taken 17184 times.
333179 for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 315995 times.
315995 if (dc->els_ctx.err)
814 return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
815
816 315995 pix = curr_row[x - 1]; // get W pixel
817
818
4/4
✓ Branch 0 taken 293467 times.
✓ Branch 1 taken 22528 times.
✓ Branch 2 taken 259371 times.
✓ Branch 3 taken 34096 times.
315995 if (y >= 1 && x >= 2 &&
819
4/4
✓ Branch 0 taken 101977 times.
✓ Branch 1 taken 157394 times.
✓ Branch 2 taken 59168 times.
✓ Branch 3 taken 42809 times.
259371 pix != curr_row[x - 2] && pix != above_row[x - 1] &&
820
6/6
✓ Branch 0 taken 49937 times.
✓ Branch 1 taken 9231 times.
✓ Branch 2 taken 41955 times.
✓ Branch 3 taken 7982 times.
✓ Branch 4 taken 1999 times.
✓ Branch 5 taken 39956 times.
101123 pix != above_row[x - 2] && pix != above_row[x] &&
821 41955 !epic_cache_entries_for_pixel(&dc->hash, pix)) {
822 1999 curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
823 1999 x++;
824 } else {
825 int got_pixel, run;
826 313996 dc->stack_pos = 0; // empty stack
827
828
6/6
✓ Branch 0 taken 268956 times.
✓ Branch 1 taken 45040 times.
✓ Branch 2 taken 235132 times.
✓ Branch 3 taken 33824 times.
✓ Branch 4 taken 16892 times.
✓ Branch 5 taken 218240 times.
313996 if (y < 2 || x < 2 || x == tile_width - 1) {
829 95756 run = 1;
830 95756 got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
831 } else {
832 218240 got_pixel = epic_decode_run_length(dc, x, y, tile_width,
833 curr_row, above_row,
834 above2_row, &pix, &run);
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218240 times.
218240 if (got_pixel < 0)
836 return got_pixel;
837 }
838
839
4/4
✓ Branch 0 taken 76908 times.
✓ Branch 1 taken 237088 times.
✓ Branch 3 taken 54716 times.
✓ Branch 4 taken 22192 times.
313996 if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
840 tile_width, curr_row,
841 above_row, &pix)) {
842 54716 uint32_t ref_pix = curr_row[x - 1];
843
4/4
✓ Branch 0 taken 53634 times.
✓ Branch 1 taken 1082 times.
✓ Branch 3 taken 1658 times.
✓ Branch 4 taken 51976 times.
54716 if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
844 2740 pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2740 times.
2740 if (is_pixel_on_stack(dc, pix))
846 return AVERROR_INVALIDDATA;
847
848
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 1082 times.
2740 if (x) {
849 1658 int ret = epic_add_pixel_to_cache(&dc->hash,
850 ref_pix,
851 pix);
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1658 times.
1658 if (ret)
853 return ret;
854 }
855 }
856 }
857
2/2
✓ Branch 0 taken 2845745 times.
✓ Branch 1 taken 313996 times.
3159741 for (; run > 0; x++, run--)
858 2845745 curr_row[x] = pix;
859 }
860 }
861 }
862
863 136 return 0;
864 }
865
866 136 static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
867 const uint8_t *src, size_t src_size,
868 AVCodecContext *avctx)
869 {
870 136 uint8_t prefix, mask = 0x80;
871 int extrabytes, tile_width, tile_height, awidth, aheight;
872 size_t els_dsize;
873 uint8_t *dst;
874
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (!src_size)
876 return 0;
877
878 /* get data size of the ELS partition as unsigned variable-length integer */
879 136 prefix = *src++;
880 136 src_size--;
881
3/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
224 for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
882 88 mask >>= 1;
883
2/4
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
136 if (extrabytes > 3 || src_size < extrabytes) {
884 av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
885 return AVERROR_INVALIDDATA;
886 }
887
888 136 els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
889
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 136 times.
224 while (extrabytes-- > 0) {
890 88 els_dsize = (els_dsize << 8) | *src++;
891 88 src_size--;
892 }
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (src_size < els_dsize) {
895 av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %zu, got %zu\n",
896 els_dsize, src_size);
897 return AVERROR_INVALIDDATA;
898 }
899
900 136 tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
901 136 tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
902 136 awidth = FFALIGN(tile_width, 16);
903 136 aheight = FFALIGN(tile_height, 16);
904
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (tile_width > (1 << FF_ARRAY_ELEMS(c->ec.prev_row_rung))) {
906 avpriv_request_sample(avctx, "large tile width");
907 return AVERROR_INVALIDDATA;
908 }
909
910
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (els_dsize) {
911 int ret, i, j, k;
912 uint8_t tr_r, tr_g, tr_b, *buf;
913 uint32_t *in;
914 /* ELS decoder initializations */
915 136 memset(&c->ec, 0, sizeof(c->ec));
916 136 ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
917 136 epic_hash_init(&c->ec.hash);
918
919 /* decode transparent pixel value */
920 136 tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
921 136 tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
922 136 tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (c->ec.els_ctx.err != 0) {
924 av_log(avctx, AV_LOG_ERROR,
925 "ePIC: couldn't decode transparency pixel!\n");
926 ff_els_decoder_uninit(&c->ec.unsigned_rung);
927 return AVERROR_INVALIDDATA;
928 }
929
930 136 ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
931 c->epic_buf_stride);
932
933 136 epic_free_pixel_cache(&c->ec.hash);
934 136 ff_els_decoder_uninit(&c->ec.unsigned_rung);
935
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret) {
937 av_log(avctx, AV_LOG_ERROR,
938 "ePIC: tile decoding failed, frame=%"PRId64", tile_x=%d, tile_y=%d\n",
939 avctx->frame_num, tile_x, tile_y);
940 return AVERROR_INVALIDDATA;
941 }
942
943 136 buf = c->epic_buf;
944 136 dst = c->framebuf + tile_x * c->tile_width * 3 +
945 136 tile_y * c->tile_height * c->framebuf_stride;
946
947
2/2
✓ Branch 0 taken 17184 times.
✓ Branch 1 taken 136 times.
17320 for (j = 0; j < tile_height; j++) {
948 17184 uint8_t *out = dst;
949 17184 in = (uint32_t *) buf;
950
2/2
✓ Branch 0 taken 2847744 times.
✓ Branch 1 taken 17184 times.
2864928 for (i = 0; i < tile_width; i++) {
951 2847744 out[0] = (in[i] >> R_shift) & 0xFF;
952 2847744 out[1] = (in[i] >> G_shift) & 0xFF;
953 2847744 out[2] = (in[i] >> B_shift) & 0xFF;
954 2847744 out += 3;
955 }
956 17184 buf += c->epic_buf_stride;
957 17184 dst += c->framebuf_stride;
958 }
959
960
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 44 times.
136 if (src_size > els_dsize) {
961 uint8_t *jpg;
962 uint32_t tr;
963 92 int bstride = FFALIGN(tile_width, 16) >> 3;
964 92 int nblocks = 0;
965 92 int estride = c->epic_buf_stride >> 2;
966
967 92 src += els_dsize;
968 92 src_size -= els_dsize;
969
970 92 in = (uint32_t *) c->epic_buf;
971 92 tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
972
973 92 memset(c->kempf_flags, 0,
974 92 (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
975
2/2
✓ Branch 0 taken 1451 times.
✓ Branch 1 taken 92 times.
1543 for (j = 0; j < tile_height; j += 8) {
976
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 1451 times.
32605 for (i = 0; i < tile_width; i += 8) {
977 31154 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
978
2/2
✓ Branch 0 taken 1096594 times.
✓ Branch 1 taken 16412 times.
1113006 for (k = 0; k < 8 * 8; k++) {
979
2/2
✓ Branch 0 taken 14742 times.
✓ Branch 1 taken 1081852 times.
1096594 if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
980 14742 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
981 14742 nblocks++;
982 14742 break;
983 }
984 }
985 }
986 1451 in += 8 * estride;
987 }
988
989 92 memset(c->jpeg_tile, 0, c->tile_stride * aheight);
990 92 jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
991 c->jpeg_tile, c->tile_stride,
992 92 c->kempf_flags, bstride, nblocks, c->swapuv);
993
994 92 in = (uint32_t *) c->epic_buf;
995 92 dst = c->framebuf + tile_x * c->tile_width * 3 +
996 92 tile_y * c->tile_height * c->framebuf_stride;
997 92 jpg = c->jpeg_tile;
998
2/2
✓ Branch 0 taken 11580 times.
✓ Branch 1 taken 92 times.
11672 for (j = 0; j < tile_height; j++) {
999
2/2
✓ Branch 0 taken 1988928 times.
✓ Branch 1 taken 11580 times.
2000508 for (i = 0; i < tile_width; i++)
1000
2/2
✓ Branch 0 taken 717442 times.
✓ Branch 1 taken 1271486 times.
1988928 if (in[i] == tr)
1001 717442 memcpy(dst + i * 3, jpg + i * 3, 3);
1002 11580 in += c->epic_buf_stride >> 2;
1003 11580 dst += c->framebuf_stride;
1004 11580 jpg += c->tile_stride;
1005 }
1006 }
1007 } else {
1008 dst = c->framebuf + tile_x * c->tile_width * 3 +
1009 tile_y * c->tile_height * c->framebuf_stride;
1010 return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
1011 dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
1012 }
1013
1014 136 return 0;
1015 }
1016
1017 81 static int kempf_restore_buf(const uint8_t *src, int len,
1018 uint8_t *dst, int stride,
1019 const uint8_t *jpeg_tile, int tile_stride,
1020 int width, int height,
1021 const uint8_t *pal, int npal, int tidx)
1022 {
1023 GetBitContext gb;
1024 int i, j, nb, col;
1025 int ret;
1026 81 int align_width = FFALIGN(width, 16);
1027
1028
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
81 if ((ret = init_get_bits8(&gb, src, len)) < 0)
1029 return ret;
1030
1031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (npal <= 2) nb = 1;
1032
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 66 times.
81 else if (npal <= 4) nb = 2;
1033
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 29 times.
66 else if (npal <= 16) nb = 4;
1034 29 else nb = 8;
1035
1036
4/4
✓ Branch 0 taken 8576 times.
✓ Branch 1 taken 1792 times.
✓ Branch 2 taken 10368 times.
✓ Branch 3 taken 81 times.
10449 for (j = 0; j < height; j++, dst += stride, jpeg_tile = FF_PTR_ADD(jpeg_tile, tile_stride)) {
1037
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10368 times.
10368 if (get_bits(&gb, 8))
1038 continue;
1039
2/2
✓ Branch 0 taken 1644544 times.
✓ Branch 1 taken 10368 times.
1654912 for (i = 0; i < width; i++) {
1040 1644544 col = get_bits(&gb, nb);
1041
2/2
✓ Branch 0 taken 1323579 times.
✓ Branch 1 taken 320965 times.
1644544 if (col != tidx)
1042 1323579 memcpy(dst + i * 3, pal + col * 3, 3);
1043 else
1044 320965 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
1045 }
1046 10368 skip_bits_long(&gb, nb * (align_width - width));
1047 }
1048
1049 81 return 0;
1050 }
1051
1052 101 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
1053 const uint8_t *src, int src_size)
1054 {
1055 int width, height;
1056 101 int hdr, zsize, npal, tidx = -1, ret;
1057 101 const uint8_t *src_end = src + src_size;
1058 uint8_t pal[768], transp[3];
1059 101 uLongf dlen = (c->tile_width + 1) * c->tile_height;
1060 int sub_type;
1061 int nblocks, cblocks, bstride;
1062 int bits, bitbuf, coded;
1063 101 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
1064 101 tile_y * c->tile_height * c->framebuf_stride;
1065
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (src_size < 2)
1067 return AVERROR_INVALIDDATA;
1068
1069 101 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
1070 101 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
1071
1072 101 hdr = *src++;
1073 101 sub_type = hdr >> 5;
1074
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 81 times.
101 if (sub_type == 0) {
1075 20 memcpy(transp, src, 3);
1076 20 src += 3;
1077
2/2
✓ Branch 0 taken 2560 times.
✓ Branch 1 taken 20 times.
2580 for (int j = 0; j < height; j++, dst += c->framebuf_stride)
1078
2/2
✓ Branch 0 taken 401408 times.
✓ Branch 1 taken 2560 times.
403968 for (int i = 0; i < width; i++)
1079 401408 memcpy(dst + i * 3, transp, 3);
1080 20 return 0;
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 } else if (sub_type == 1) {
1082 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
1083 dst, c->framebuf_stride, NULL, 0, 0, 0);
1084 }
1085
1086
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 14 times.
81 if (sub_type != 2) {
1087 67 memcpy(transp, src, 3);
1088 67 src += 3;
1089 }
1090 81 npal = *src++ + 1;
1091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (src_end - src < npal * 3)
1092 return AVERROR_INVALIDDATA;
1093 81 memcpy(pal, src, npal * 3);
1094 81 src += npal * 3;
1095
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 14 times.
81 if (sub_type != 2) {
1096
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 for (int i = 0; i < npal; i++) {
1097
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 67 times.
134 if (!memcmp(pal + i * 3, transp, 3)) {
1098 67 tidx = i;
1099 67 break;
1100 }
1101 }
1102 }
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (src_end - src < 2)
1105 return 0;
1106 81 zsize = (src[0] << 8) | src[1];
1107 81 src += 2;
1108
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (src_end - src < zsize + (sub_type != 2))
1110 return AVERROR_INVALIDDATA;
1111
1112 81 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (ret)
1114 return AVERROR_INVALIDDATA;
1115 81 src += zsize;
1116
1117
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 67 times.
81 if (sub_type == 2) {
1118 14 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1119 NULL, 0, width, height, pal, npal, tidx);
1120 14 return 0;
1121 }
1122
1123 67 nblocks = *src++ + 1;
1124 67 cblocks = 0;
1125 67 bstride = FFALIGN(width, 16) >> 3;
1126 // blocks are coded LSB and we need normal bitreader for JPEG data
1127 67 bits = 0;
1128
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 67 times.
603 for (int i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
1129
2/2
✓ Branch 0 taken 5448 times.
✓ Branch 1 taken 536 times.
5984 for (int j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
1130
2/2
✓ Branch 0 taken 681 times.
✓ Branch 1 taken 4767 times.
5448 if (!bits) {
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 681 times.
681 if (src >= src_end)
1132 return AVERROR_INVALIDDATA;
1133 681 bitbuf = *src++;
1134 681 bits = 8;
1135 }
1136 5448 coded = bitbuf & 1;
1137 5448 bits--;
1138 5448 bitbuf >>= 1;
1139 5448 cblocks += coded;
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5448 times.
5448 if (cblocks > nblocks)
1141 return AVERROR_INVALIDDATA;
1142 5448 c->kempf_flags[j * 2 + i * 2 * bstride] =
1143 5448 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
1144 5448 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
1145 5448 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
1146 }
1147 }
1148
1149 67 memset(c->jpeg_tile, 0, c->tile_stride * height);
1150 67 jpg_decode_data(&c->jc, width, height, src, src_end - src,
1151 c->jpeg_tile, c->tile_stride,
1152 67 c->kempf_flags, bstride, nblocks * 4, 0);
1153
1154 67 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1155 67 c->jpeg_tile, c->tile_stride,
1156 width, height, pal, npal, tidx);
1157
1158 67 return 0;
1159 }
1160
1161 4 static int g2m_init_buffers(G2MContext *c)
1162 {
1163 int aligned_height;
1164
1165 4 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
1166 4 aligned_height = c->height + 15;
1167
1168 4 av_fast_mallocz(&c->framebuf, &c->framebuf_allocated, c->framebuf_stride * aligned_height);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!c->framebuf)
1170 return AVERROR(ENOMEM);
1171
1172
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 if (!c->synth_tile || !c->jpeg_tile ||
1173
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 (c->compression == 2 && !c->epic_buf_base) ||
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 c->old_tile_w < c->tile_width ||
1175 c->old_tile_h < c->tile_height) {
1176 4 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
1177 4 c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
1178 4 aligned_height = FFALIGN(c->tile_height, 16);
1179 4 av_freep(&c->synth_tile);
1180 4 av_freep(&c->jpeg_tile);
1181 4 av_freep(&c->kempf_buf);
1182 4 av_freep(&c->kempf_flags);
1183 4 av_freep(&c->epic_buf_base);
1184 4 c->epic_buf = NULL;
1185 4 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
1186 4 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
1187 4 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
1188 AV_INPUT_BUFFER_PADDING_SIZE);
1189 4 c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
1190
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (!c->synth_tile || !c->jpeg_tile ||
1191
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 !c->kempf_buf || !c->kempf_flags)
1192 return AVERROR(ENOMEM);
1193
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (c->compression == 2) {
1194 3 c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!c->epic_buf_base)
1196 return AVERROR(ENOMEM);
1197 3 c->epic_buf = c->epic_buf_base + 4;
1198 }
1199 }
1200
1201 4 return 0;
1202 }
1203
1204 14 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
1205 GetByteContext *gb)
1206 {
1207 int i, j, k;
1208 uint8_t *dst;
1209 uint32_t bits;
1210 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
1211 uint32_t cursor_hot_x, cursor_hot_y;
1212 int cursor_fmt, err;
1213
1214 14 cur_size = bytestream2_get_be32(gb);
1215 14 cursor_w = bytestream2_get_byte(gb);
1216 14 cursor_h = bytestream2_get_byte(gb);
1217 14 cursor_hot_x = bytestream2_get_byte(gb);
1218 14 cursor_hot_y = bytestream2_get_byte(gb);
1219 14 cursor_fmt = bytestream2_get_byte(gb);
1220
1221
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
14 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1222
1223
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 if (cursor_w < 1 || cursor_w > 256 ||
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 cursor_h < 1 || cursor_h > 256) {
1225 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1226 cursor_w, cursor_h);
1227 return AVERROR_INVALIDDATA;
1228 }
1229
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1230 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1231 cursor_hot_x, cursor_hot_y);
1232 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
1233 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
1234 }
1235
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 c->cursor_w * c->cursor_h / 4 > cur_size) {
1237 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1238 cur_size, bytestream2_get_bytes_left(gb));
1239 return AVERROR_INVALIDDATA;
1240 }
1241
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
14 if (cursor_fmt != 1 && cursor_fmt != 32) {
1242 avpriv_report_missing_feature(avctx, "Cursor format %d",
1243 cursor_fmt);
1244 return AVERROR_PATCHWELCOME;
1245 }
1246
1247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1248 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1249 return err;
1250 }
1251
1252 14 c->cursor_w = cursor_w;
1253 14 c->cursor_h = cursor_h;
1254 14 c->cursor_hot_x = cursor_hot_x;
1255 14 c->cursor_hot_y = cursor_hot_y;
1256 14 c->cursor_fmt = cursor_fmt;
1257 14 c->cursor_stride = cursor_stride;
1258
1259 14 dst = c->cursor;
1260
2/3
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
14 switch (c->cursor_fmt) {
1261 13 case 1: // old monochrome
1262
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 13 times.
429 for (j = 0; j < c->cursor_h; j++) {
1263
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 416 times.
832 for (i = 0; i < c->cursor_w; i += 32) {
1264 416 bits = bytestream2_get_be32(gb);
1265
2/2
✓ Branch 0 taken 13312 times.
✓ Branch 1 taken 416 times.
13728 for (k = 0; k < 32; k++) {
1266 13312 dst[0] = !!(bits & 0x80000000);
1267 13312 dst += 4;
1268 13312 bits <<= 1;
1269 }
1270 }
1271 }
1272
1273 13 dst = c->cursor;
1274
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 13 times.
429 for (j = 0; j < c->cursor_h; j++) {
1275
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 416 times.
832 for (i = 0; i < c->cursor_w; i += 32) {
1276 416 bits = bytestream2_get_be32(gb);
1277
2/2
✓ Branch 0 taken 13312 times.
✓ Branch 1 taken 416 times.
13728 for (k = 0; k < 32; k++) {
1278 13312 int mask_bit = !!(bits & 0x80000000);
1279
3/3
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 920 times.
✓ Branch 2 taken 11568 times.
13312 switch (dst[0] * 2 + mask_bit) {
1280 824 case 0:
1281 824 dst[0] = 0xFF;
1282 824 dst[1] = 0x00;
1283 824 dst[2] = 0x00;
1284 824 dst[3] = 0x00;
1285 824 break;
1286 920 case 1:
1287 920 dst[0] = 0xFF;
1288 920 dst[1] = 0xFF;
1289 920 dst[2] = 0xFF;
1290 920 dst[3] = 0xFF;
1291 920 break;
1292 11568 default:
1293 11568 dst[0] = 0x00;
1294 11568 dst[1] = 0x00;
1295 11568 dst[2] = 0x00;
1296 11568 dst[3] = 0x00;
1297 }
1298 13312 dst += 4;
1299 13312 bits <<= 1;
1300 }
1301 }
1302 }
1303 13 break;
1304 1 case 32: // full colour
1305 /* skip monochrome version of the cursor and decode RGBA instead */
1306 1 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
1307
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
33 for (j = 0; j < c->cursor_h; j++) {
1308
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 32 times.
1056 for (i = 0; i < c->cursor_w; i++) {
1309 1024 int val = bytestream2_get_be32(gb);
1310 1024 *dst++ = val >> 0;
1311 1024 *dst++ = val >> 8;
1312 1024 *dst++ = val >> 16;
1313 1024 *dst++ = val >> 24;
1314 }
1315 }
1316 1 break;
1317 default:
1318 return AVERROR_PATCHWELCOME;
1319 }
1320 14 return 0;
1321 }
1322
1323 #define APPLY_ALPHA(src, new, alpha) \
1324 src = (src * (256 - alpha) + new * alpha) >> 8
1325
1326 251 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
1327 {
1328 int i, j;
1329 int x, y, w, h;
1330 const uint8_t *cursor;
1331
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (!c->cursor)
1333 return;
1334
1335 251 x = c->cursor_x - c->cursor_hot_x;
1336 251 y = c->cursor_y - c->cursor_hot_y;
1337
1338 251 cursor = c->cursor;
1339 251 w = c->cursor_w;
1340 251 h = c->cursor_h;
1341
1342
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 195 times.
251 if (x + w > c->width)
1343 56 w = c->width - x;
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (y + h > c->height)
1345 h = c->height - y;
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (x < 0) {
1347 w += x;
1348 cursor += -x * 4;
1349 } else {
1350 251 dst += x * 3;
1351 }
1352
1353
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 248 times.
251 if (y < 0)
1354 3 h += y;
1355
2/4
✓ Branch 0 taken 251 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 251 times.
251 if (w < 0 || h < 0)
1356 return;
1357
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 248 times.
251 if (y < 0) {
1358 3 cursor += -y * c->cursor_stride;
1359 } else {
1360 248 dst += y * stride;
1361 }
1362
1363
2/2
✓ Branch 0 taken 8014 times.
✓ Branch 1 taken 251 times.
8265 for (j = 0; j < h; j++) {
1364
2/2
✓ Branch 0 taken 231008 times.
✓ Branch 1 taken 8014 times.
239022 for (i = 0; i < w; i++) {
1365 231008 uint8_t alpha = cursor[i * 4];
1366 231008 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
1367 231008 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
1368 231008 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
1369 }
1370 8014 dst += stride;
1371 8014 cursor += c->cursor_stride;
1372 }
1373 }
1374
1375 251 static int g2m_decode_frame(AVCodecContext *avctx, AVFrame *pic,
1376 int *got_picture_ptr, AVPacket *avpkt)
1377 {
1378 251 const uint8_t *buf = avpkt->data;
1379 251 int buf_size = avpkt->size;
1380 251 G2MContext *c = avctx->priv_data;
1381 GetByteContext bc, tbc;
1382 int magic;
1383 251 int got_header = 0;
1384 uint32_t chunk_size, r_mask, g_mask, b_mask;
1385 int chunk_type, chunk_start;
1386 int i;
1387 int ret;
1388
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (buf_size < 12) {
1390 av_log(avctx, AV_LOG_ERROR,
1391 "Frame should have at least 12 bytes, got %d instead\n",
1392 buf_size);
1393 return AVERROR_INVALIDDATA;
1394 }
1395
1396 251 bytestream2_init(&bc, buf, buf_size);
1397
1398 251 magic = bytestream2_get_be32(&bc);
1399
1/2
✓ Branch 0 taken 251 times.
✗ Branch 1 not taken.
251 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1400
2/4
✓ Branch 0 taken 251 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 251 times.
251 (magic & 0xF) < 2 || (magic & 0xF) > 5) {
1401 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
1402 return AVERROR_INVALIDDATA;
1403 }
1404
1405 251 c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
1406
1407
2/2
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 251 times.
769 while (bytestream2_get_bytes_left(&bc) > 5) {
1408 518 chunk_size = bytestream2_get_le32(&bc) - 1;
1409 518 chunk_type = bytestream2_get_byte(&bc);
1410 518 chunk_start = bytestream2_tell(&bc);
1411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
518 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1412 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
1413 chunk_size, chunk_type);
1414 break;
1415 }
1416
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 237 times.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
518 switch (chunk_type) {
1417 4 case DISPLAY_INFO:
1418 4 got_header =
1419 4 c->got_header = 0;
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (chunk_size < 21) {
1421 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
1422 chunk_size);
1423 break;
1424 }
1425 4 c->width = bytestream2_get_be32(&bc);
1426 4 c->height = bytestream2_get_be32(&bc);
1427
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (c->width < 16 || c->height < 16) {
1428 av_log(avctx, AV_LOG_ERROR,
1429 "Invalid frame dimensions %dx%d\n",
1430 c->width, c->height);
1431 ret = AVERROR_INVALIDDATA;
1432 goto header_fail;
1433 }
1434
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (c->width != avctx->width || c->height != avctx->height) {
1435 ret = ff_set_dimensions(avctx, c->width, c->height);
1436 if (ret < 0)
1437 goto header_fail;
1438 }
1439 4 c->compression = bytestream2_get_be32(&bc);
1440
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 if (c->compression != 2 && c->compression != 3) {
1441 avpriv_report_missing_feature(avctx, "Compression method %d",
1442 c->compression);
1443 ret = AVERROR_PATCHWELCOME;
1444 goto header_fail;
1445 }
1446 4 c->tile_width = bytestream2_get_be32(&bc);
1447 4 c->tile_height = bytestream2_get_be32(&bc);
1448
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (c->tile_width <= 0 || c->tile_height <= 0 ||
1449
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 ((c->tile_width | c->tile_height) & 0xF) ||
1450
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
8 c->tile_width * (uint64_t)c->tile_height >= INT_MAX / 4 ||
1451 4 av_image_check_size2(c->tile_width, c->tile_height, avctx->max_pixels, avctx->pix_fmt, 0, avctx) < 0
1452 ) {
1453 av_log(avctx, AV_LOG_ERROR,
1454 "Invalid tile dimensions %dx%d\n",
1455 c->tile_width, c->tile_height);
1456 ret = AVERROR_INVALIDDATA;
1457 goto header_fail;
1458 }
1459 4 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
1460 4 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1461 4 c->bpp = bytestream2_get_byte(&bc);
1462
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (c->bpp == 32) {
1463
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (bytestream2_get_bytes_left(&bc) < 16 ||
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (chunk_size - 21) < 16) {
1465 av_log(avctx, AV_LOG_ERROR,
1466 "Display info: missing bitmasks!\n");
1467 ret = AVERROR_INVALIDDATA;
1468 goto header_fail;
1469 }
1470 4 r_mask = bytestream2_get_be32(&bc);
1471 4 g_mask = bytestream2_get_be32(&bc);
1472 4 b_mask = bytestream2_get_be32(&bc);
1473
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
1474 avpriv_report_missing_feature(avctx,
1475 "Bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32,
1476 r_mask, g_mask, b_mask);
1477 ret = AVERROR_PATCHWELCOME;
1478 goto header_fail;
1479 }
1480 } else {
1481 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1482 ret = AVERROR_PATCHWELCOME;
1483 goto header_fail;
1484 }
1485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (g2m_init_buffers(c)) {
1486 ret = AVERROR(ENOMEM);
1487 goto header_fail;
1488 }
1489 4 got_header = 1;
1490 4 break;
1491 237 case TILE_DATA:
1492
2/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 237 times.
237 if (!c->tiles_x || !c->tiles_y) {
1493 av_log(avctx, AV_LOG_WARNING,
1494 "No display info - skipping tile\n");
1495 break;
1496 }
1497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (chunk_size < 2) {
1498 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
1499 chunk_size);
1500 break;
1501 }
1502 237 c->tile_x = bytestream2_get_byte(&bc);
1503 237 c->tile_y = bytestream2_get_byte(&bc);
1504
2/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 237 times.
237 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
1505 av_log(avctx, AV_LOG_ERROR,
1506 "Invalid tile pos %d,%d (in %dx%d grid)\n",
1507 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
1508 break;
1509 }
1510 237 ret = 0;
1511
2/3
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
237 switch (c->compression) {
1512 136 case COMPR_EPIC_J_B:
1513 272 ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
1514 136 buf + bytestream2_tell(&bc),
1515 136 chunk_size - 2, avctx);
1516 136 break;
1517 101 case COMPR_KEMPF_J_B:
1518 202 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
1519 101 buf + bytestream2_tell(&bc),
1520 101 chunk_size - 2);
1521 101 break;
1522 }
1523
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
237 if (ret && c->framebuf)
1524 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
1525 c->tile_x, c->tile_y);
1526 237 break;
1527 249 case CURSOR_POS:
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 if (chunk_size < 5) {
1529 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
1530 chunk_size);
1531 break;
1532 }
1533 249 c->cursor_x = bytestream2_get_be16(&bc);
1534 249 c->cursor_y = bytestream2_get_be16(&bc);
1535 249 break;
1536 14 case CURSOR_SHAPE:
1537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (chunk_size < 8) {
1538 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
1539 chunk_size);
1540 break;
1541 }
1542 14 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
1543 14 chunk_size - 4);
1544 14 g2m_load_cursor(avctx, c, &tbc);
1545 14 break;
1546 14 case CHUNK_CC:
1547 case CHUNK_CD:
1548 14 break;
1549 default:
1550 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
1551 chunk_type);
1552 }
1553
1554 /* navigate to next chunk */
1555 518 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
1556 }
1557
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 247 times.
251 if (got_header)
1558 4 c->got_header = 1;
1559
1560
3/6
✓ Branch 0 taken 251 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 251 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 251 times.
✗ Branch 5 not taken.
251 if (c->width && c->height && c->framebuf) {
1561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 251 times.
251 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
1562 return ret;
1563
1564
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 247 times.
251 if (got_header)
1565 4 pic->flags |= AV_FRAME_FLAG_KEY;
1566 else
1567 247 pic->flags &= ~AV_FRAME_FLAG_KEY;
1568
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 247 times.
251 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1569
1570
2/2
✓ Branch 0 taken 214300 times.
✓ Branch 1 taken 251 times.
214551 for (i = 0; i < avctx->height; i++)
1571 214300 memcpy(pic->data[0] + i * pic->linesize[0],
1572 214300 c->framebuf + i * c->framebuf_stride,
1573 214300 c->width * 3);
1574 251 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
1575
1576 251 *got_picture_ptr = 1;
1577 }
1578
1579 251 return buf_size;
1580
1581 header_fail:
1582 c->width =
1583 c->height = 0;
1584 c->tiles_x =
1585 c->tiles_y = 0;
1586 c->tile_width =
1587 c->tile_height = 0;
1588 return ret;
1589 }
1590
1591 6 static av_cold int g2m_decode_init(AVCodecContext *avctx)
1592 {
1593 6 G2MContext *const c = avctx->priv_data;
1594 int ret;
1595
1596
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
1597 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
1598 return AVERROR(ENOMEM);
1599 }
1600
1601 6 avctx->pix_fmt = AV_PIX_FMT_RGB24;
1602
1603 // store original sizes and check against those if resize happens
1604 6 c->orig_width = avctx->width;
1605 6 c->orig_height = avctx->height;
1606
1607 6 return 0;
1608 }
1609
1610 6 static av_cold int g2m_decode_end(AVCodecContext *avctx)
1611 {
1612 6 G2MContext *const c = avctx->priv_data;
1613
1614 6 jpg_free_context(&c->jc);
1615
1616 6 av_freep(&c->epic_buf_base);
1617 6 c->epic_buf = NULL;
1618 6 av_freep(&c->kempf_buf);
1619 6 av_freep(&c->kempf_flags);
1620 6 av_freep(&c->synth_tile);
1621 6 av_freep(&c->jpeg_tile);
1622 6 av_freep(&c->cursor);
1623 6 av_freep(&c->framebuf);
1624 6 c->framebuf_allocated = 0;
1625
1626 6 return 0;
1627 }
1628
1629 const FFCodec ff_g2m_decoder = {
1630 .p.name = "g2m",
1631 CODEC_LONG_NAME("Go2Meeting"),
1632 .p.type = AVMEDIA_TYPE_VIDEO,
1633 .p.id = AV_CODEC_ID_G2M,
1634 .priv_data_size = sizeof(G2MContext),
1635 .init = g2m_decode_init,
1636 .close = g2m_decode_end,
1637 FF_CODEC_DECODE_CB(g2m_decode_frame),
1638 .p.capabilities = AV_CODEC_CAP_DR1,
1639 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1640 };
1641