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