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_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
67 pixel_ptr + n, pixel_limit); \
83 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
102 depth =
AV_RL16(&fli_header[12]);
121 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
132 void *
data,
int *got_frame,
133 const uint8_t *buf,
int buf_size)
138 int stream_ptr_after_color_chunk;
141 unsigned char palette_idx1;
142 unsigned char palette_idx2;
147 unsigned int chunk_size;
155 unsigned char r,
g,
b;
158 int compressed_lines;
160 signed short line_packets;
165 unsigned char *pixels;
166 unsigned int pixel_limit;
179 frame_size = bytestream2_get_le32(&g2);
181 num_chunks = bytestream2_get_le16(&g2);
187 while ((frame_size > 0) && (num_chunks > 0)) {
188 chunk_size = bytestream2_get_le32(&g2);
189 chunk_type = bytestream2_get_le16(&g2);
191 switch (chunk_type) {
205 color_packets = bytestream2_get_le16(&g2);
207 for (i = 0; i < color_packets; i++) {
209 palette_ptr += bytestream2_get_byte(&g2);
212 color_changes = bytestream2_get_byte(&g2);
215 if (color_changes == 0)
218 for (j = 0; j < color_changes; j++) {
222 if ((
unsigned)palette_ptr >= 256)
225 r = bytestream2_get_byte(&g2) << color_shift;
226 g = bytestream2_get_byte(&g2) << color_shift;
227 b = bytestream2_get_byte(&g2) << color_shift;
228 entry = (r << 16) | (g << 8) |
b;
229 if (s->
palette[palette_ptr] != entry)
231 s->
palette[palette_ptr++] = entry;
246 compressed_lines = bytestream2_get_le16(&g2);
247 while (compressed_lines > 0) {
248 line_packets = bytestream2_get_le16(&g2);
249 if ((line_packets & 0xC000) == 0xC000) {
251 line_packets = -line_packets;
253 }
else if ((line_packets & 0xC000) == 0x4000) {
255 }
else if ((line_packets & 0xC000) == 0x8000) {
259 pixels[pixel_ptr] = line_packets & 0xff;
265 for (i = 0; i < line_packets; i++) {
267 pixel_skip = bytestream2_get_byte(&g2);
268 pixel_ptr += pixel_skip;
269 pixel_countdown -= pixel_skip;
270 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
272 byte_run = -byte_run;
273 palette_idx1 = bytestream2_get_byte(&g2);
274 palette_idx2 = bytestream2_get_byte(&g2);
276 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
277 pixels[pixel_ptr++] = palette_idx1;
278 pixels[pixel_ptr++] = palette_idx2;
282 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
283 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
295 starting_line = bytestream2_get_le16(&g2);
299 compressed_lines = bytestream2_get_le16(&g2);
300 while (compressed_lines > 0) {
304 line_packets = bytestream2_get_byte(&g2);
305 if (line_packets > 0) {
306 for (i = 0; i < line_packets; i++) {
308 pixel_skip = bytestream2_get_byte(&g2);
309 pixel_ptr += pixel_skip;
310 pixel_countdown -= pixel_skip;
311 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
314 for (j = 0; j < byte_run; j++, pixel_countdown--) {
315 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
317 }
else if (byte_run < 0) {
318 byte_run = -byte_run;
319 palette_idx1 = bytestream2_get_byte(&g2);
321 for (j = 0; j < byte_run; j++, pixel_countdown--) {
322 pixels[pixel_ptr++] = palette_idx1;
343 for (lines = 0; lines < s->
avctx->
height; lines++) {
349 while (pixel_countdown > 0) {
350 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
357 palette_idx1 = bytestream2_get_byte(&g2);
359 for (j = 0; j < byte_run; j++) {
360 pixels[pixel_ptr++] = palette_idx1;
362 if (pixel_countdown < 0)
364 pixel_countdown, lines);
367 byte_run = -byte_run;
369 for (j = 0; j < byte_run; j++) {
370 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
372 if (pixel_countdown < 0)
374 pixel_countdown, lines);
387 "bigger than image, skipping chunk\n", chunk_size - 6);
408 frame_size -= chunk_size;
417 "and final chunk ptr = %d\n", buf_size,
434 void *
data,
int *got_frame,
435 const uint8_t *buf,
int buf_size)
443 unsigned char palette_idx1;
448 unsigned int chunk_size;
454 int compressed_lines;
455 signed short line_packets;
460 unsigned char *pixels;
462 unsigned int pixel_limit;
476 frame_size = bytestream2_get_le32(&g2);
478 num_chunks = bytestream2_get_le16(&g2);
484 while ((frame_size > 0) && (num_chunks > 0)) {
485 chunk_size = bytestream2_get_le32(&g2);
486 chunk_type = bytestream2_get_le16(&g2);
488 switch (chunk_type) {
495 "Unexpected Palette chunk %d in non-palettized FLC\n",
503 compressed_lines = bytestream2_get_le16(&g2);
504 while (compressed_lines > 0) {
505 line_packets = bytestream2_get_le16(&g2);
506 if (line_packets < 0) {
507 line_packets = -line_packets;
514 for (i = 0; i < line_packets; i++) {
516 pixel_skip = bytestream2_get_byte(&g2);
517 pixel_ptr += (pixel_skip*2);
518 pixel_countdown -= pixel_skip;
519 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
521 byte_run = -byte_run;
522 pixel = bytestream2_get_le16(&g2);
524 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
525 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
530 for (j = 0; j < byte_run; j++, pixel_countdown--) {
531 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
549 memset(pixels, 0x0000,
555 for (lines = 0; lines < s->
avctx->
height; lines++) {
562 while (pixel_countdown > 0) {
563 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
565 palette_idx1 = bytestream2_get_byte(&g2);
567 for (j = 0; j < byte_run; j++) {
568 pixels[pixel_ptr++] = palette_idx1;
570 if (pixel_countdown < 0)
572 pixel_countdown, lines);
575 byte_run = -byte_run;
577 for (j = 0; j < byte_run; j++) {
578 palette_idx1 = bytestream2_get_byte(&g2);
579 pixels[pixel_ptr++] = palette_idx1;
581 if (pixel_countdown < 0)
583 pixel_countdown, lines);
596 while (pixel_countdown > 0) {
597 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
607 for (lines = 0; lines < s->
avctx->
height; lines++) {
614 while (pixel_countdown > 0) {
615 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
617 pixel = bytestream2_get_le16(&g2);
619 for (j = 0; j < byte_run; j++) {
620 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
623 if (pixel_countdown < 0)
628 byte_run = -byte_run;
630 for (j = 0; j < byte_run; j++) {
631 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
634 if (pixel_countdown < 0)
650 "bigger than image, skipping chunk\n", chunk_size - 6);
659 while (pixel_countdown > 0) {
660 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
678 frame_size -= chunk_size;
696 void *
data,
int *got_frame,
697 const uint8_t *buf,
int buf_size)
704 void *
data,
int *got_frame,
708 int buf_size = avpkt->
size;
727 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");