48 #define FLI_256_COLOR 4
56 #define FLI_DTA_BRUN 25
57 #define FLI_DTA_COPY 26
60 #define FLI_TYPE_CODE (0xAF11)
61 #define FLC_FLX_TYPE_CODE (0xAF12)
62 #define FLC_DTA_TYPE_CODE (0xAF44)
63 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
65 #define CHECK_PIXEL_PTR(n) \
66 if (pixel_ptr + n > pixel_limit) { \
67 av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
68 pixel_ptr + n, pixel_limit); \
69 return AVERROR_INVALIDDATA; \
84 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
107 for (i = 0; i < 256; i++) {
151 void *
data,
int *got_frame,
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;
195 frame_size = bytestream2_get_le32(&g2);
196 if (frame_size > buf_size)
197 frame_size = buf_size;
199 num_chunks = bytestream2_get_le16(&g2);
205 while ((frame_size >= 6) && (num_chunks > 0)) {
206 int stream_ptr_after_chunk;
207 chunk_size = bytestream2_get_le32(&g2);
208 if (chunk_size > frame_size) {
210 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
215 chunk_type = bytestream2_get_le16(&g2);
217 switch (chunk_type) {
229 color_packets = bytestream2_get_le16(&g2);
231 for (i = 0; i < color_packets; i++) {
233 palette_ptr += bytestream2_get_byte(&g2);
236 color_changes = bytestream2_get_byte(&g2);
239 if (color_changes == 0)
245 for (j = 0; j < color_changes; j++) {
249 if ((
unsigned)palette_ptr >= 256)
252 r = bytestream2_get_byte(&g2) << color_shift;
253 g = bytestream2_get_byte(&g2) << color_shift;
254 b = bytestream2_get_byte(&g2) << color_shift;
255 entry = 0xFF
U << 24 | r << 16 | g << 8 |
b;
256 if (color_shift == 2)
257 entry |= entry >> 6 & 0x30303;
258 if (s->
palette[palette_ptr] != entry)
260 s->
palette[palette_ptr++] = entry;
267 compressed_lines = bytestream2_get_le16(&g2);
268 while (compressed_lines > 0) {
271 line_packets = bytestream2_get_le16(&g2);
272 if ((line_packets & 0xC000) == 0xC000) {
274 line_packets = -line_packets;
276 }
else if ((line_packets & 0xC000) == 0x4000) {
278 }
else if ((line_packets & 0xC000) == 0x8000) {
282 pixels[pixel_ptr] = line_packets & 0xff;
288 for (i = 0; i < line_packets; i++) {
292 pixel_skip = bytestream2_get_byte(&g2);
293 pixel_ptr += pixel_skip;
294 pixel_countdown -= pixel_skip;
295 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
297 byte_run = -byte_run;
298 palette_idx1 = bytestream2_get_byte(&g2);
299 palette_idx2 = bytestream2_get_byte(&g2);
301 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
302 pixels[pixel_ptr++] = palette_idx1;
303 pixels[pixel_ptr++] = palette_idx2;
309 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
310 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
322 starting_line = bytestream2_get_le16(&g2);
326 compressed_lines = bytestream2_get_le16(&g2);
327 while (compressed_lines > 0) {
333 line_packets = bytestream2_get_byte(&g2);
334 if (line_packets > 0) {
335 for (i = 0; i < line_packets; i++) {
339 pixel_skip = bytestream2_get_byte(&g2);
340 pixel_ptr += pixel_skip;
341 pixel_countdown -= pixel_skip;
342 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
347 for (j = 0; j < byte_run; j++, pixel_countdown--) {
348 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
350 }
else if (byte_run < 0) {
351 byte_run = -byte_run;
352 palette_idx1 = bytestream2_get_byte(&g2);
354 for (j = 0; j < byte_run; j++, pixel_countdown--) {
355 pixels[pixel_ptr++] = palette_idx1;
376 for (lines = 0; lines < s->
avctx->
height; lines++) {
382 while (pixel_countdown > 0) {
385 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
392 palette_idx1 = bytestream2_get_byte(&g2);
394 for (j = 0; j < byte_run; j++) {
395 pixels[pixel_ptr++] = palette_idx1;
397 if (pixel_countdown < 0)
399 pixel_countdown, lines);
402 byte_run = -byte_run;
406 for (j = 0; j < byte_run; j++) {
407 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
409 if (pixel_countdown < 0)
411 pixel_countdown, lines);
424 "has incorrect size, skipping chunk\n", chunk_size - 6);
447 frame_size -= chunk_size;
455 "and final chunk ptr = %d\n", buf_size,
474 void *
data,
int *got_frame,
483 unsigned char palette_idx1;
488 unsigned int chunk_size;
494 int compressed_lines;
495 signed short line_packets;
500 unsigned char *pixels;
502 unsigned int pixel_limit;
512 frame_size = bytestream2_get_le32(&g2);
514 num_chunks = bytestream2_get_le16(&g2);
516 if (frame_size > buf_size)
517 frame_size = buf_size;
522 while ((frame_size > 0) && (num_chunks > 0)) {
523 int stream_ptr_after_chunk;
524 chunk_size = bytestream2_get_le32(&g2);
525 if (chunk_size > frame_size) {
527 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
532 chunk_type = bytestream2_get_le16(&g2);
535 switch (chunk_type) {
542 "Unexpected Palette chunk %d in non-palettized FLC\n",
550 compressed_lines = bytestream2_get_le16(&g2);
551 while (compressed_lines > 0) {
554 line_packets = bytestream2_get_le16(&g2);
555 if (line_packets < 0) {
556 line_packets = -line_packets;
563 for (i = 0; i < line_packets; i++) {
567 pixel_skip = bytestream2_get_byte(&g2);
568 pixel_ptr += (pixel_skip*2);
569 pixel_countdown -= pixel_skip;
570 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
572 byte_run = -byte_run;
573 pixel = bytestream2_get_le16(&g2);
575 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
576 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
583 for (j = 0; j < byte_run; j++, pixel_countdown--) {
584 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
602 memset(pixels, 0x0000,
608 for (lines = 0; lines < s->
avctx->
height; lines++) {
615 while (pixel_countdown > 0) {
618 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
620 palette_idx1 = bytestream2_get_byte(&g2);
622 for (j = 0; j < byte_run; j++) {
623 pixels[pixel_ptr++] = palette_idx1;
625 if (pixel_countdown < 0)
627 pixel_countdown, lines);
630 byte_run = -byte_run;
634 for (j = 0; j < byte_run; j++) {
635 palette_idx1 = bytestream2_get_byte(&g2);
636 pixels[pixel_ptr++] = palette_idx1;
638 if (pixel_countdown < 0)
640 pixel_countdown, lines);
653 while (pixel_countdown > 0) {
654 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
664 for (lines = 0; lines < s->
avctx->
height; lines++) {
671 while (pixel_countdown > 0) {
674 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
676 pixel = bytestream2_get_le16(&g2);
678 for (j = 0; j < byte_run; j++) {
679 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
682 if (pixel_countdown < 0)
687 byte_run = -byte_run;
691 for (j = 0; j < byte_run; j++) {
692 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
695 if (pixel_countdown < 0)
711 "bigger than image, skipping chunk\n", chunk_size - 6);
720 while (pixel_countdown > 0) {
721 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
739 frame_size -= chunk_size;
758 void *
data,
int *got_frame,
766 void *
data,
int *got_frame,
770 int buf_size = avpkt->
size;
789 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");