FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sga.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 0 293 0.0%
Branches: 0 198 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/common.h"
22 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "bytestream.h"
25 #include "internal.h"
26
27 #define PALDATA_FOLLOWS_TILEDATA 4
28 #define HAVE_COMPRESSED_TILEMAP 32
29 #define HAVE_TILEMAP 128
30
31 typedef struct SGAVideoContext {
32 GetByteContext gb;
33
34 int metadata_size;
35 int tiledata_size;
36 int tiledata_offset;
37 int tilemapdata_size;
38 int tilemapdata_offset;
39 int paldata_size;
40 int paldata_offset;
41 int palmapdata_offset;
42 int palmapdata_size;
43
44 int flags;
45 int nb_pal;
46 int nb_tiles;
47 int tiles_w, tiles_h;
48 int shift;
49 int plus;
50 int swap;
51
52 uint32_t pal[256];
53 uint8_t *tileindex_data;
54 unsigned tileindex_size;
55 uint8_t *palmapindex_data;
56 unsigned palmapindex_size;
57 uint8_t uncompressed[65536];
58 } SGAVideoContext;
59
60 static av_cold int sga_decode_init(AVCodecContext *avctx)
61 {
62 avctx->pix_fmt = AV_PIX_FMT_PAL8;
63 return 0;
64 }
65
66 static int decode_palette(GetByteContext *gb, uint32_t *pal)
67 {
68 GetBitContext gbit;
69
70 if (bytestream2_get_bytes_left(gb) < 18)
71 return AVERROR_INVALIDDATA;
72
73 memset(pal, 0, 16 * sizeof(*pal));
74 init_get_bits8(&gbit, gb->buffer, 18);
75
76 for (int RGBIndex = 0; RGBIndex < 3; RGBIndex++) {
77 for (int index = 0; index < 16; index++) {
78 unsigned color = get_bits1(&gbit) << RGBIndex;
79 pal[15 - index] |= color << (5 + 16);
80 }
81 }
82
83 for (int RGBIndex = 0; RGBIndex < 3; RGBIndex++) {
84 for (int index = 0; index < 16; index++) {
85 unsigned color = get_bits1(&gbit) << RGBIndex;
86 pal[15 - index] |= color << (5 + 8);
87 }
88 }
89
90 for (int RGBIndex = 0; RGBIndex < 3; RGBIndex++) {
91 for (int index = 0; index < 16; index++) {
92 unsigned color = get_bits1(&gbit) << RGBIndex;
93 pal[15 - index] |= color << (5 + 0);
94 }
95 }
96
97 for (int index = 0; index < 16; index++)
98 pal[index] = (0xFFU << 24) | pal[index] | (pal[index] >> 3);
99
100 bytestream2_skip(gb, 18);
101
102 return 0;
103 }
104
105 static int decode_index_palmap(SGAVideoContext *s, AVFrame *frame)
106 {
107 const uint8_t *tt = s->tileindex_data;
108
109 for (int y = 0; y < s->tiles_h; y++) {
110 for (int x = 0; x < s->tiles_w; x++) {
111 int pal_idx = s->palmapindex_data[y * s->tiles_w + x] * 16;
112 uint8_t *dst = frame->data[0] + y * 8 * frame->linesize[0] + x * 8;
113
114 for (int yy = 0; yy < 8; yy++) {
115 for (int xx = 0; xx < 8; xx++)
116 dst[xx] = pal_idx + tt[xx];
117 tt += 8;
118
119 dst += frame->linesize[0];
120 }
121 }
122 }
123
124 return 0;
125 }
126
127 static int decode_index_tilemap(SGAVideoContext *s, AVFrame *frame)
128 {
129 GetByteContext *gb = &s->gb;
130 GetBitContext pm;
131
132 bytestream2_seek(gb, s->tilemapdata_offset, SEEK_SET);
133 if (bytestream2_get_bytes_left(gb) < s->tilemapdata_size)
134 return AVERROR_INVALIDDATA;
135
136 init_get_bits8(&pm, gb->buffer, s->tilemapdata_size);
137
138 for (int y = 0; y < s->tiles_h; y++) {
139 for (int x = 0; x < s->tiles_w; x++) {
140 uint8_t tile[64];
141 int tilemap = get_bits(&pm, 16);
142 int flip_x = (tilemap >> 11) & 1;
143 int flip_y = (tilemap >> 12) & 1;
144 int tindex = av_clip((tilemap & 511) - 1, 0, s->nb_tiles - 1);
145 const uint8_t *tt = s->tileindex_data + tindex * 64;
146 int pal_idx = ((tilemap >> 13) & 3) * 16;
147 uint8_t *dst = frame->data[0] + y * 8 * frame->linesize[0] + x * 8;
148
149 if (!flip_x && !flip_y) {
150 memcpy(tile, tt, 64);
151 } else if (flip_x && flip_y) {
152 for (int i = 0; i < 8; i++) {
153 for (int j = 0; j < 8; j++)
154 tile[i * 8 + j] = tt[(7 - i) * 8 + 7 - j];
155 }
156 } else if (flip_x) {
157 for (int i = 0; i < 8; i++) {
158 for (int j = 0; j < 8; j++)
159 tile[i * 8 + j] = tt[i * 8 + 7 - j];
160 }
161 } else {
162 for (int i = 0; i < 8; i++) {
163 for (int j = 0; j < 8; j++)
164 tile[i * 8 + j] = tt[(7 - i) * 8 + j];
165 }
166 }
167
168 for (int yy = 0; yy < 8; yy++) {
169 for (int xx = 0; xx < 8; xx++)
170 dst[xx] = pal_idx + tile[xx + yy * 8];
171
172 dst += frame->linesize[0];
173 }
174 }
175 }
176
177 return 0;
178 }
179
180 static int decode_index(SGAVideoContext *s, AVFrame *frame)
181 {
182 const uint8_t *src = s->tileindex_data;
183 uint8_t *dst = frame->data[0];
184
185 for (int y = 0; y < frame->height; y += 8) {
186 for (int x = 0; x < frame->width; x += 8) {
187 for (int yy = 0; yy < 8; yy++) {
188 for (int xx = 0; xx < 8; xx++)
189 dst[x + xx + yy * frame->linesize[0]] = src[xx];
190 src += 8;
191 }
192 }
193
194 dst += 8 * frame->linesize[0];
195 }
196
197 return 0;
198 }
199
200 static int lzss_decompress(AVCodecContext *avctx,
201 GetByteContext *gb, uint8_t *dst,
202 int dst_size, int shift, int plus)
203 {
204 int oi = 0;
205
206 while (bytestream2_get_bytes_left(gb) > 0 && oi < dst_size) {
207 uint16_t displace, header = bytestream2_get_be16(gb);
208 int count, offset;
209
210 for (int i = 0; i < 16; i++) {
211 switch (header >> 15) {
212 case 0:
213 if (oi + 2 < dst_size) {
214 dst[oi++] = bytestream2_get_byte(gb);
215 dst[oi++] = bytestream2_get_byte(gb);
216 }
217 break;
218 case 1:
219 displace = bytestream2_get_be16(gb);
220 count = displace >> shift;
221 offset = displace & ((1 << shift) - 1);
222
223 if (displace == 0) {
224 while (bytestream2_get_bytes_left(gb) > 0 &&
225 oi < dst_size)
226 dst[oi++] = bytestream2_get_byte(gb);
227 return oi;
228 }
229
230 count += plus;
231
232 if (offset <= 0)
233 offset = 1;
234 if (oi < offset || oi + count * 2 > dst_size)
235 return AVERROR_INVALIDDATA;
236 for (int j = 0; j < count * 2; j++) {
237 dst[oi] = dst[oi - offset];
238 oi++;
239 }
240 break;
241 }
242
243 header <<= 1;
244 }
245 }
246
247 return AVERROR_INVALIDDATA;
248 }
249
250 static int decode_palmapdata(AVCodecContext *avctx)
251 {
252 SGAVideoContext *s = avctx->priv_data;
253 const int bits = (s->nb_pal + 1) / 2;
254 GetByteContext *gb = &s->gb;
255 GetBitContext pm;
256
257 bytestream2_seek(gb, s->palmapdata_offset, SEEK_SET);
258 if (bytestream2_get_bytes_left(gb) < s->palmapdata_size)
259 return AVERROR_INVALIDDATA;
260 init_get_bits8(&pm, gb->buffer, s->palmapdata_size);
261
262 for (int y = 0; y < s->tiles_h; y++) {
263 uint8_t *dst = s->palmapindex_data + y * s->tiles_w;
264
265 for (int x = 0; x < s->tiles_w; x++)
266 dst[x] = get_bits(&pm, bits);
267
268 dst += s->tiles_w;
269 }
270
271 return 0;
272 }
273
274 static int decode_tiledata(AVCodecContext *avctx)
275 {
276 SGAVideoContext *s = avctx->priv_data;
277 GetByteContext *gb = &s->gb;
278 GetBitContext tm;
279
280 bytestream2_seek(gb, s->tiledata_offset, SEEK_SET);
281 if (bytestream2_get_bytes_left(gb) < s->tiledata_size)
282 return AVERROR_INVALIDDATA;
283 init_get_bits8(&tm, gb->buffer, s->tiledata_size);
284
285 for (int n = 0; n < s->nb_tiles; n++) {
286 uint8_t *dst = s->tileindex_data + n * 64;
287
288 for (int yy = 0; yy < 8; yy++) {
289 for (int xx = 0; xx < 8; xx++)
290 dst[xx] = get_bits(&tm, 4);
291
292 dst += 8;
293 }
294 }
295
296 for (int i = 0; i < s->nb_tiles && s->swap; i++) {
297 uint8_t *dst = s->tileindex_data + i * 64;
298
299 for (int j = 8; j < 64; j += 16) {
300 for (int k = 0; k < 8; k += 2)
301 FFSWAP(uint8_t, dst[j + k], dst[j+k+1]);
302 }
303 }
304
305 return 0;
306 }
307
308 static int sga_decode_frame(AVCodecContext *avctx, void *data,
309 int *got_frame, AVPacket *avpkt)
310 {
311 SGAVideoContext *s = avctx->priv_data;
312 GetByteContext *gb = &s->gb;
313 AVFrame *frame = data;
314 int ret, type;
315
316 if (avpkt->size <= 14)
317 return AVERROR_INVALIDDATA;
318
319 s->flags = avpkt->data[8];
320 s->nb_pal = avpkt->data[9];
321 s->tiles_w = avpkt->data[10];
322 s->tiles_h = avpkt->data[11];
323
324 if (s->nb_pal > 4)
325 return AVERROR_INVALIDDATA;
326
327 if ((ret = ff_set_dimensions(avctx,
328 s->tiles_w * 8,
329 s->tiles_h * 8)) < 0)
330 return ret;
331
332 av_fast_padded_malloc(&s->tileindex_data, &s->tileindex_size,
333 avctx->width * avctx->height);
334 if (!s->tileindex_data)
335 return AVERROR(ENOMEM);
336
337 av_fast_padded_malloc(&s->palmapindex_data, &s->palmapindex_size,
338 s->tiles_w * s->tiles_h);
339 if (!s->palmapindex_data)
340 return AVERROR(ENOMEM);
341
342 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
343 return ret;
344
345 bytestream2_init(gb, avpkt->data, avpkt->size);
346
347 type = bytestream2_get_byte(gb);
348 s->metadata_size = 12 + ((!!(s->flags & HAVE_TILEMAP)) * 2);
349 s->nb_tiles = s->flags & HAVE_TILEMAP ? AV_RB16(avpkt->data + 12) : s->tiles_w * s->tiles_h;
350 if (s->nb_tiles > s->tiles_w * s->tiles_h)
351 return AVERROR_INVALIDDATA;
352
353 av_log(avctx, AV_LOG_DEBUG, "type: %X flags: %X nb_tiles: %d\n", type, s->flags, s->nb_tiles);
354
355 switch (type) {
356 case 0xE7:
357 case 0xCB:
358 case 0xCD:
359 s->swap = 1;
360 s->shift = 12;
361 s->plus = 1;
362 break;
363 case 0xC9:
364 s->swap = 1;
365 s->shift = 13;
366 s->plus = 1;
367 break;
368 case 0xC8:
369 s->swap = 1;
370 s->shift = 13;
371 s->plus = 0;
372 break;
373 case 0xC7:
374 s->swap = 0;
375 s->shift = 13;
376 s->plus = 1;
377 break;
378 case 0xC6:
379 s->swap = 0;
380 s->shift = 13;
381 s->plus = 0;
382 break;
383 }
384
385 if (type == 0xE7) {
386 int offset = s->metadata_size, left;
387 int sizes[3];
388
389 bytestream2_seek(gb, s->metadata_size, SEEK_SET);
390
391 for (int i = 0; i < 3; i++)
392 sizes[i] = bytestream2_get_be16(gb);
393
394 for (int i = 0; i < 3; i++) {
395 int size = sizes[i];
396 int raw = size >> 15;
397
398 size &= (1 << 15) - 1;
399
400 if (raw) {
401 if (bytestream2_get_bytes_left(gb) < size)
402 return AVERROR_INVALIDDATA;
403
404 if (sizeof(s->uncompressed) - offset < size)
405 return AVERROR_INVALIDDATA;
406
407 memcpy(s->uncompressed + offset, gb->buffer, size);
408 bytestream2_skip(gb, size);
409 } else {
410 GetByteContext gb2;
411
412 if (bytestream2_get_bytes_left(gb) < size)
413 return AVERROR_INVALIDDATA;
414
415 bytestream2_init(&gb2, gb->buffer, size);
416 ret = lzss_decompress(avctx, &gb2, s->uncompressed + offset,
417 sizeof(s->uncompressed) - offset, s->shift, s->plus);
418 if (ret < 0)
419 return ret;
420 bytestream2_skip(gb, size);
421 size = ret;
422 }
423
424 offset += size;
425 }
426
427 left = bytestream2_get_bytes_left(gb);
428 if (sizeof(s->uncompressed) - offset < left)
429 return AVERROR_INVALIDDATA;
430
431 bytestream2_get_buffer(gb, s->uncompressed + offset, left);
432
433 offset += left;
434 bytestream2_init(gb, s->uncompressed, offset);
435 }
436
437 switch (type) {
438 case 0xCD:
439 case 0xCB:
440 case 0xC9:
441 case 0xC8:
442 case 0xC7:
443 case 0xC6:
444 bytestream2_seek(gb, s->metadata_size, SEEK_SET);
445 ret = lzss_decompress(avctx, gb, s->uncompressed + s->metadata_size,
446 sizeof(s->uncompressed) - s->metadata_size, s->shift, s->plus);
447 if (ret < 0)
448 return ret;
449 bytestream2_init(gb, s->uncompressed, ret + s->metadata_size);
450 case 0xE7:
451 case 0xC1:
452 s->tiledata_size = s->nb_tiles * 32;
453 s->paldata_size = s->nb_pal * 18;
454 s->tiledata_offset = s->flags & PALDATA_FOLLOWS_TILEDATA ? s->metadata_size : s->metadata_size + s->paldata_size;
455 s->paldata_offset = s->flags & PALDATA_FOLLOWS_TILEDATA ? s->metadata_size + s->tiledata_size : s->metadata_size;
456 s->palmapdata_offset = (s->flags & HAVE_TILEMAP) ? -1 : s->paldata_offset + s->paldata_size;
457 s->palmapdata_size = (s->flags & HAVE_TILEMAP) || s->nb_pal < 2 ? 0 : (s->tiles_w * s->tiles_h * ((s->nb_pal + 1) / 2) + 7) / 8;
458 s->tilemapdata_size = (s->flags & HAVE_TILEMAP) ? s->tiles_w * s->tiles_h * 2 : 0;
459 s->tilemapdata_offset = (s->flags & HAVE_TILEMAP) ? s->paldata_offset + s->paldata_size: -1;
460
461 bytestream2_seek(gb, s->paldata_offset, SEEK_SET);
462 for (int n = 0; n < s->nb_pal; n++) {
463 ret = decode_palette(gb, s->pal + 16 * n);
464 if (ret < 0)
465 return ret;
466 }
467
468 if (s->tiledata_size > 0) {
469 ret = decode_tiledata(avctx);
470 if (ret < 0)
471 return ret;
472 }
473
474 if (s->palmapdata_size > 0) {
475 ret = decode_palmapdata(avctx);
476 if (ret < 0)
477 return ret;
478 }
479
480 if (s->palmapdata_size > 0 && s->tiledata_size > 0) {
481 ret = decode_index_palmap(s, frame);
482 if (ret < 0)
483 return ret;
484 } else if (s->tilemapdata_size > 0 && s->tiledata_size > 0) {
485 ret = decode_index_tilemap(s, frame);
486 if (ret < 0)
487 return ret;
488 } else if (s->tiledata_size > 0) {
489 ret = decode_index(s, frame);
490 if (ret < 0)
491 return ret;
492 }
493 break;
494 default:
495 av_log(avctx, AV_LOG_ERROR, "Unknown type: %X\n", type);
496 return AVERROR_INVALIDDATA;
497 }
498
499 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
500 frame->palette_has_changed = 1;
501 frame->pict_type = AV_PICTURE_TYPE_I;
502 frame->key_frame = 1;
503
504 *got_frame = 1;
505
506 return avpkt->size;
507 }
508
509 static av_cold int sga_decode_end(AVCodecContext *avctx)
510 {
511 SGAVideoContext *s = avctx->priv_data;
512
513 av_freep(&s->tileindex_data);
514 s->tileindex_size = 0;
515
516 av_freep(&s->palmapindex_data);
517 s->palmapindex_size = 0;
518
519 return 0;
520 }
521
522 const AVCodec ff_sga_decoder = {
523 .name = "sga",
524 .long_name = NULL_IF_CONFIG_SMALL("Digital Pictures SGA Video"),
525 .type = AVMEDIA_TYPE_VIDEO,
526 .id = AV_CODEC_ID_SGA_VIDEO,
527 .priv_data_size = sizeof(SGAVideoContext),
528 .init = sga_decode_init,
529 .decode = sga_decode_frame,
530 .close = sga_decode_end,
531 .capabilities = AV_CODEC_CAP_DR1,
532 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
533 };
534