47 #define FLI_256_COLOR 4
55 #define FLI_DTA_BRUN 25
56 #define FLI_DTA_COPY 26
59 #define FLI_TYPE_CODE (0xAF11)
60 #define FLC_FLX_TYPE_CODE (0xAF12)
61 #define FLC_DTA_TYPE_CODE (0xAF44)
62 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
64 #define CHECK_PIXEL_PTR(n) \
65 if (pixel_ptr + n > pixel_limit) { \
66 av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
67 pixel_ptr + n, pixel_limit); \
68 return AVERROR_INVALIDDATA; \
83 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
106 for (i = 0; i < 256; i++) {
151 void *
data,
int *got_frame,
152 const uint8_t *buf,
int buf_size)
159 unsigned char palette_idx1;
160 unsigned char palette_idx2;
165 unsigned int chunk_size;
173 unsigned char r,
g,
b;
176 int compressed_lines;
178 signed short line_packets;
183 unsigned char *pixels;
184 unsigned int pixel_limit;
199 frame_size = bytestream2_get_le32(&g2);
200 if (frame_size > buf_size)
201 frame_size = buf_size;
203 num_chunks = bytestream2_get_le16(&g2);
209 while ((frame_size >= 6) && (num_chunks > 0)) {
210 int stream_ptr_after_chunk;
211 chunk_size = bytestream2_get_le32(&g2);
212 if (chunk_size > frame_size) {
214 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
219 chunk_type = bytestream2_get_le16(&g2);
221 switch (chunk_type) {
233 color_packets = bytestream2_get_le16(&g2);
235 for (i = 0; i < color_packets; i++) {
237 palette_ptr += bytestream2_get_byte(&g2);
240 color_changes = bytestream2_get_byte(&g2);
243 if (color_changes == 0)
249 for (j = 0; j < color_changes; j++) {
253 if ((
unsigned)palette_ptr >= 256)
256 r = bytestream2_get_byte(&g2) << color_shift;
257 g = bytestream2_get_byte(&g2) << color_shift;
258 b = bytestream2_get_byte(&g2) << color_shift;
259 entry = 0xFF
U << 24 | r << 16 | g << 8 |
b;
260 if (color_shift == 2)
261 entry |= entry >> 6 & 0x30303;
262 if (s->
palette[palette_ptr] != entry)
264 s->
palette[palette_ptr++] = entry;
271 compressed_lines = bytestream2_get_le16(&g2);
272 while (compressed_lines > 0) {
275 line_packets = bytestream2_get_le16(&g2);
276 if ((line_packets & 0xC000) == 0xC000) {
278 line_packets = -line_packets;
280 }
else if ((line_packets & 0xC000) == 0x4000) {
282 }
else if ((line_packets & 0xC000) == 0x8000) {
286 pixels[pixel_ptr] = line_packets & 0xff;
292 for (i = 0; i < line_packets; i++) {
296 pixel_skip = bytestream2_get_byte(&g2);
297 pixel_ptr += pixel_skip;
298 pixel_countdown -= pixel_skip;
299 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
301 byte_run = -byte_run;
302 palette_idx1 = bytestream2_get_byte(&g2);
303 palette_idx2 = bytestream2_get_byte(&g2);
305 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
306 pixels[pixel_ptr++] = palette_idx1;
307 pixels[pixel_ptr++] = palette_idx2;
313 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
314 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
326 starting_line = bytestream2_get_le16(&g2);
330 compressed_lines = bytestream2_get_le16(&g2);
331 while (compressed_lines > 0) {
337 line_packets = bytestream2_get_byte(&g2);
338 if (line_packets > 0) {
339 for (i = 0; i < line_packets; i++) {
343 pixel_skip = bytestream2_get_byte(&g2);
344 pixel_ptr += pixel_skip;
345 pixel_countdown -= pixel_skip;
346 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
351 for (j = 0; j < byte_run; j++, pixel_countdown--) {
352 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
354 }
else if (byte_run < 0) {
355 byte_run = -byte_run;
356 palette_idx1 = bytestream2_get_byte(&g2);
358 for (j = 0; j < byte_run; j++, pixel_countdown--) {
359 pixels[pixel_ptr++] = palette_idx1;
380 for (lines = 0; lines < s->
avctx->
height; lines++) {
386 while (pixel_countdown > 0) {
389 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
396 palette_idx1 = bytestream2_get_byte(&g2);
398 for (j = 0; j < byte_run; j++) {
399 pixels[pixel_ptr++] = palette_idx1;
401 if (pixel_countdown < 0)
403 pixel_countdown, lines);
406 byte_run = -byte_run;
410 for (j = 0; j < byte_run; j++) {
411 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
413 if (pixel_countdown < 0)
415 pixel_countdown, lines);
428 "has incorrect size, skipping chunk\n", chunk_size - 6);
451 frame_size -= chunk_size;
459 "and final chunk ptr = %d\n", buf_size,
476 void *
data,
int *got_frame,
477 const uint8_t *buf,
int buf_size)
485 unsigned char palette_idx1;
490 unsigned int chunk_size;
496 int compressed_lines;
497 signed short line_packets;
502 unsigned char *pixels;
504 unsigned int pixel_limit;
518 frame_size = bytestream2_get_le32(&g2);
520 num_chunks = bytestream2_get_le16(&g2);
522 if (frame_size > buf_size)
523 frame_size = buf_size;
528 while ((frame_size > 0) && (num_chunks > 0)) {
529 int stream_ptr_after_chunk;
530 chunk_size = bytestream2_get_le32(&g2);
531 if (chunk_size > frame_size) {
533 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
538 chunk_type = bytestream2_get_le16(&g2);
541 switch (chunk_type) {
548 "Unexpected Palette chunk %d in non-palettized FLC\n",
556 compressed_lines = bytestream2_get_le16(&g2);
557 while (compressed_lines > 0) {
560 line_packets = bytestream2_get_le16(&g2);
561 if (line_packets < 0) {
562 line_packets = -line_packets;
569 for (i = 0; i < line_packets; i++) {
573 pixel_skip = bytestream2_get_byte(&g2);
574 pixel_ptr += (pixel_skip*2);
575 pixel_countdown -= pixel_skip;
576 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
578 byte_run = -byte_run;
579 pixel = bytestream2_get_le16(&g2);
581 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
582 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
589 for (j = 0; j < byte_run; j++, pixel_countdown--) {
590 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
608 memset(pixels, 0x0000,
614 for (lines = 0; lines < s->
avctx->
height; lines++) {
621 while (pixel_countdown > 0) {
624 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
626 palette_idx1 = bytestream2_get_byte(&g2);
628 for (j = 0; j < byte_run; j++) {
629 pixels[pixel_ptr++] = palette_idx1;
631 if (pixel_countdown < 0)
633 pixel_countdown, lines);
636 byte_run = -byte_run;
640 for (j = 0; j < byte_run; j++) {
641 palette_idx1 = bytestream2_get_byte(&g2);
642 pixels[pixel_ptr++] = palette_idx1;
644 if (pixel_countdown < 0)
646 pixel_countdown, lines);
659 while (pixel_countdown > 0) {
660 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
670 for (lines = 0; lines < s->
avctx->
height; lines++) {
677 while (pixel_countdown > 0) {
680 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
682 pixel = bytestream2_get_le16(&g2);
684 for (j = 0; j < byte_run; j++) {
685 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
688 if (pixel_countdown < 0)
693 byte_run = -byte_run;
697 for (j = 0; j < byte_run; j++) {
698 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
701 if (pixel_countdown < 0)
717 "bigger than image, skipping chunk\n", chunk_size - 6);
726 while (pixel_countdown > 0) {
727 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
745 frame_size -= chunk_size;
763 void *
data,
int *got_frame,
764 const uint8_t *buf,
int buf_size)
771 void *
data,
int *got_frame,
775 int buf_size = avpkt->
size;
794 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");