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;
98 if (
s->avctx->extradata_size == 12) {
106 for (
i = 0;
i < 256;
i++) {
119 s->fli_type =
AV_RL16(&fli_header[4]);
120 depth =
AV_RL16(&fli_header[12]);
137 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
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;
183 unsigned char *pixels;
184 unsigned int pixel_limit;
191 pixels =
s->frame->data[0];
192 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
199 num_chunks = bytestream2_get_le16(&g2);
208 while ((
frame_size >= 6) && (num_chunks > 0) &&
210 int stream_ptr_after_chunk;
211 chunk_size = bytestream2_get_le32(&g2);
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 if (y_ptr > pixel_limit)
277 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
278 if ((line_packets & 0xC000) == 0xC000) {
280 line_packets = -line_packets;
281 if (line_packets >
s->avctx->height)
283 y_ptr += line_packets *
s->frame->linesize[0];
284 }
else if ((line_packets & 0xC000) == 0x4000) {
286 }
else if ((line_packets & 0xC000) == 0x8000) {
288 pixel_ptr= y_ptr +
s->frame->linesize[0] - 1;
290 pixels[pixel_ptr] = line_packets & 0xff;
295 pixel_countdown =
s->avctx->width;
296 for (
i = 0;
i < line_packets;
i++) {
300 pixel_skip = bytestream2_get_byte(&g2);
301 pixel_ptr += pixel_skip;
302 pixel_countdown -= pixel_skip;
303 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
305 byte_run = -byte_run;
306 palette_idx1 = bytestream2_get_byte(&g2);
307 palette_idx2 = bytestream2_get_byte(&g2);
309 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
310 pixels[pixel_ptr++] = palette_idx1;
311 pixels[pixel_ptr++] = palette_idx2;
317 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
318 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
323 y_ptr +=
s->frame->linesize[0];
330 starting_line = bytestream2_get_le16(&g2);
331 if (starting_line >=
s->avctx->height)
334 y_ptr += starting_line *
s->frame->linesize[0];
336 compressed_lines = bytestream2_get_le16(&g2);
337 while (compressed_lines > 0) {
340 pixel_countdown =
s->avctx->width;
343 line_packets = bytestream2_get_byte(&g2);
344 if (line_packets > 0) {
345 for (
i = 0;
i < line_packets;
i++) {
349 pixel_skip = bytestream2_get_byte(&g2);
350 pixel_ptr += pixel_skip;
351 pixel_countdown -= pixel_skip;
352 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
357 for (j = 0; j < byte_run; j++, pixel_countdown--) {
358 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
360 }
else if (byte_run < 0) {
361 byte_run = -byte_run;
362 palette_idx1 = bytestream2_get_byte(&g2);
364 for (j = 0; j < byte_run; j++, pixel_countdown--) {
365 pixels[pixel_ptr++] = palette_idx1;
371 y_ptr +=
s->frame->linesize[0];
379 s->frame->linesize[0] *
s->avctx->height);
386 for (lines = 0; lines <
s->avctx->height; lines++) {
391 pixel_countdown =
s->avctx->width;
392 while (pixel_countdown > 0) {
395 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
402 palette_idx1 = bytestream2_get_byte(&g2);
404 for (j = 0; j < byte_run; j++) {
405 pixels[pixel_ptr++] = palette_idx1;
407 if (pixel_countdown < 0)
409 pixel_countdown, lines);
412 byte_run = -byte_run;
416 for (j = 0; j < byte_run; j++) {
417 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
419 if (pixel_countdown < 0)
421 pixel_countdown, lines);
426 y_ptr +=
s->frame->linesize[0];
432 if (chunk_size - 6 !=
FFALIGN(
s->avctx->width, 4) *
s->avctx->height) {
434 "has incorrect size, skipping chunk\n", chunk_size - 6);
437 for (y_ptr = 0; y_ptr <
s->frame->linesize[0] *
s->avctx->height;
438 y_ptr +=
s->frame->linesize[0]) {
441 if (
s->avctx->width & 3)
471 "and final chunk ptr = %d\n", buf_size,
476 if (
s->new_palette) {
477 s->frame->palette_has_changed = 1;
490 void *
data,
int *got_frame,
491 const uint8_t *buf,
int buf_size)
499 unsigned char palette_idx1;
504 unsigned int chunk_size;
510 int compressed_lines;
516 unsigned char *pixels;
518 unsigned int pixel_limit;
525 pixels =
s->frame->data[0];
526 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
530 num_chunks = bytestream2_get_le16(&g2);
540 while ((
frame_size > 0) && (num_chunks > 0) &&
542 int stream_ptr_after_chunk;
543 chunk_size = bytestream2_get_le32(&g2);
546 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
551 chunk_type = bytestream2_get_le16(&g2);
554 switch (chunk_type) {
561 "Unexpected Palette chunk %d in non-palettized FLC\n",
569 compressed_lines = bytestream2_get_le16(&g2);
570 while (compressed_lines > 0) {
573 if (y_ptr > pixel_limit)
575 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
576 if (line_packets < 0) {
577 line_packets = -line_packets;
578 if (line_packets >
s->avctx->height)
580 y_ptr += line_packets *
s->frame->linesize[0];
585 pixel_countdown =
s->avctx->width;
586 for (
i = 0;
i < line_packets;
i++) {
590 pixel_skip = bytestream2_get_byte(&g2);
591 pixel_ptr += (pixel_skip*2);
592 pixel_countdown -= pixel_skip;
593 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
595 byte_run = -byte_run;
596 pixel = bytestream2_get_le16(&g2);
598 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
599 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
606 for (j = 0; j < byte_run; j++, pixel_countdown--) {
607 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
613 y_ptr +=
s->frame->linesize[0];
625 memset(pixels, 0x0000,
626 s->frame->linesize[0] *
s->avctx->height);
631 for (lines = 0; lines <
s->avctx->height; lines++) {
636 pixel_countdown = (
s->avctx->width * 2);
638 while (pixel_countdown > 0) {
641 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
643 palette_idx1 = bytestream2_get_byte(&g2);
645 for (j = 0; j < byte_run; j++) {
646 pixels[pixel_ptr++] = palette_idx1;
648 if (pixel_countdown < 0)
650 pixel_countdown, lines);
653 byte_run = -byte_run;
657 for (j = 0; j < byte_run; j++) {
658 palette_idx1 = bytestream2_get_byte(&g2);
659 pixels[pixel_ptr++] = palette_idx1;
661 if (pixel_countdown < 0)
663 pixel_countdown, lines);
675 pixel_countdown =
s->avctx->width;
676 while (pixel_countdown > 0) {
677 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
681 y_ptr +=
s->frame->linesize[0];
687 for (lines = 0; lines <
s->avctx->height; lines++) {
692 pixel_countdown =
s->avctx->width;
694 while (pixel_countdown > 0) {
697 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
699 pixel = bytestream2_get_le16(&g2);
701 for (j = 0; j < byte_run; j++) {
702 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
705 if (pixel_countdown < 0)
710 byte_run = -byte_run;
714 for (j = 0; j < byte_run; j++) {
715 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
718 if (pixel_countdown < 0)
725 y_ptr +=
s->frame->linesize[0];
732 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*2) {
734 "bigger than image, skipping chunk\n", chunk_size - 6);
740 for (y_ptr = 0; y_ptr <
s->frame->linesize[0] *
s->avctx->height;
741 y_ptr +=
s->frame->linesize[0]) {
743 pixel_countdown =
s->avctx->width;
745 while (pixel_countdown > 0) {
746 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
750 if (
s->avctx->width & 1)
792 void *
data,
int *got_frame,
793 const uint8_t *buf,
int buf_size)
799 unsigned char palette_idx1;
804 unsigned int chunk_size;
810 int compressed_lines;
816 unsigned char *pixels;
818 unsigned int pixel_limit;
825 pixels =
s->frame->data[0];
826 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
830 num_chunks = bytestream2_get_le16(&g2);
840 while ((
frame_size > 0) && (num_chunks > 0) &&
842 int stream_ptr_after_chunk;
843 chunk_size = bytestream2_get_le32(&g2);
846 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
851 chunk_type = bytestream2_get_le16(&g2);
854 switch (chunk_type) {
861 "Unexpected Palette chunk %d in non-palettized FLC\n",
869 compressed_lines = bytestream2_get_le16(&g2);
870 while (compressed_lines > 0) {
873 if (y_ptr > pixel_limit)
875 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
876 if (line_packets < 0) {
877 line_packets = -line_packets;
878 if (line_packets >
s->avctx->height)
880 y_ptr += line_packets *
s->frame->linesize[0];
885 pixel_countdown =
s->avctx->width;
886 for (
i = 0;
i < line_packets;
i++) {
890 pixel_skip = bytestream2_get_byte(&g2);
891 pixel_ptr += (pixel_skip*3);
892 pixel_countdown -= pixel_skip;
893 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
895 byte_run = -byte_run;
896 pixel = bytestream2_get_le24(&g2);
898 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
906 for (j = 0; j < byte_run; j++, pixel_countdown--) {
907 pixel = bytestream2_get_le24(&g2);
914 y_ptr +=
s->frame->linesize[0];
927 s->frame->linesize[0] *
s->avctx->height);
932 for (lines = 0; lines <
s->avctx->height; lines++) {
937 pixel_countdown = (
s->avctx->width * 3);
939 while (pixel_countdown > 0) {
942 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
944 palette_idx1 = bytestream2_get_byte(&g2);
946 for (j = 0; j < byte_run; j++) {
947 pixels[pixel_ptr++] = palette_idx1;
949 if (pixel_countdown < 0)
951 pixel_countdown, lines);
954 byte_run = -byte_run;
958 for (j = 0; j < byte_run; j++) {
959 palette_idx1 = bytestream2_get_byte(&g2);
960 pixels[pixel_ptr++] = palette_idx1;
962 if (pixel_countdown < 0)
964 pixel_countdown, lines);
969 y_ptr +=
s->frame->linesize[0];
975 for (lines = 0; lines <
s->avctx->height; lines++) {
980 pixel_countdown =
s->avctx->width;
982 while (pixel_countdown > 0) {
985 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
987 pixel = bytestream2_get_le24(&g2);
989 for (j = 0; j < byte_run; j++) {
993 if (pixel_countdown < 0)
998 byte_run = -byte_run;
1002 for (j = 0; j < byte_run; j++) {
1003 pixel = bytestream2_get_le24(&g2);
1007 if (pixel_countdown < 0)
1014 y_ptr +=
s->frame->linesize[0];
1021 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*3) {
1023 "bigger than image, skipping chunk\n", chunk_size - 6);
1026 for (y_ptr = 0; y_ptr <
s->frame->linesize[0] *
s->avctx->height;
1027 y_ptr +=
s->frame->linesize[0]) {
1030 if (
s->avctx->width & 1)
1072 void *
data,
int *got_frame,
1075 const uint8_t *buf = avpkt->
data;
1076 int buf_size = avpkt->
size;
1093 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");