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;
132 void *
data,
int *data_size,
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;
144 unsigned int frame_size;
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);
352 palette_idx1 = bytestream2_get_byte(&g2);
354 for (j = 0; j < byte_run; j++) {
355 pixels[pixel_ptr++] = palette_idx1;
357 if (pixel_countdown < 0)
359 pixel_countdown, lines);
362 byte_run = -byte_run;
364 for (j = 0; j < byte_run; j++) {
365 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
367 if (pixel_countdown < 0)
369 pixel_countdown, lines);
382 "bigger than image, skipping chunk\n", chunk_size - 6);
403 frame_size -= chunk_size;
412 "and final chunk ptr = %d\n", buf_size,
429 void *
data,
int *data_size,
430 const uint8_t *buf,
int buf_size)
438 unsigned char palette_idx1;
440 unsigned int frame_size;
443 unsigned int chunk_size;
449 int compressed_lines;
450 signed short line_packets;
455 unsigned char *pixels;
457 unsigned int pixel_limit;
471 frame_size = bytestream2_get_le32(&g2);
473 num_chunks = bytestream2_get_le16(&g2);
479 while ((frame_size > 0) && (num_chunks > 0)) {
480 chunk_size = bytestream2_get_le32(&g2);
481 chunk_type = bytestream2_get_le16(&g2);
483 switch (chunk_type) {
496 compressed_lines = bytestream2_get_le16(&g2);
497 while (compressed_lines > 0) {
498 line_packets = bytestream2_get_le16(&g2);
499 if (line_packets < 0) {
500 line_packets = -line_packets;
507 for (i = 0; i < line_packets; i++) {
509 pixel_skip = bytestream2_get_byte(&g2);
510 pixel_ptr += (pixel_skip*2);
511 pixel_countdown -= pixel_skip;
512 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
514 byte_run = -byte_run;
515 pixel = bytestream2_get_le16(&g2);
517 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
518 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
523 for (j = 0; j < byte_run; j++, pixel_countdown--) {
524 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
542 memset(pixels, 0x0000,
548 for (lines = 0; lines < s->
avctx->
height; lines++) {
555 while (pixel_countdown > 0) {
556 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
558 palette_idx1 = bytestream2_get_byte(&g2);
560 for (j = 0; j < byte_run; j++) {
561 pixels[pixel_ptr++] = palette_idx1;
563 if (pixel_countdown < 0)
565 pixel_countdown, lines);
568 byte_run = -byte_run;
570 for (j = 0; j < byte_run; j++) {
571 palette_idx1 = bytestream2_get_byte(&g2);
572 pixels[pixel_ptr++] = palette_idx1;
574 if (pixel_countdown < 0)
576 pixel_countdown, lines);
589 while (pixel_countdown > 0) {
590 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
600 for (lines = 0; lines < s->
avctx->
height; lines++) {
607 while (pixel_countdown > 0) {
608 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
610 pixel = bytestream2_get_le16(&g2);
612 for (j = 0; j < byte_run; j++) {
613 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
616 if (pixel_countdown < 0)
621 byte_run = -byte_run;
623 for (j = 0; j < byte_run; j++) {
624 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
627 if (pixel_countdown < 0)
643 "bigger than image, skipping chunk\n", chunk_size - 6);
652 while (pixel_countdown > 0) {
653 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
671 frame_size -= chunk_size;
689 void *
data,
int *data_size,
690 const uint8_t *buf,
int buf_size)
697 void *
data,
int *data_size,
700 const uint8_t *buf = avpkt->
data;
701 int buf_size = avpkt->
size;
720 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");