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_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
68 pixel_ptr + n, pixel_limit); \
69 return AVERROR_INVALIDDATA; \
84 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
103 depth =
AV_RL16(&fli_header[12]);
122 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
136 void *
data,
int *got_frame,
137 const uint8_t *buf,
int buf_size)
142 int stream_ptr_after_color_chunk;
145 unsigned char palette_idx1;
146 unsigned char palette_idx2;
151 unsigned int chunk_size;
159 unsigned char r,
g,
b;
162 int compressed_lines;
164 signed short line_packets;
169 unsigned char *pixels;
170 unsigned int pixel_limit;
181 frame_size = bytestream2_get_le32(&g2);
183 num_chunks = bytestream2_get_le16(&g2);
189 while ((frame_size > 0) && (num_chunks > 0)) {
190 chunk_size = bytestream2_get_le32(&g2);
191 chunk_type = bytestream2_get_le16(&g2);
193 switch (chunk_type) {
207 color_packets = bytestream2_get_le16(&g2);
209 for (i = 0; i < color_packets; i++) {
211 palette_ptr += bytestream2_get_byte(&g2);
214 color_changes = bytestream2_get_byte(&g2);
217 if (color_changes == 0)
220 for (j = 0; j < color_changes; j++) {
224 if ((
unsigned)palette_ptr >= 256)
227 r = bytestream2_get_byte(&g2) << color_shift;
228 g = bytestream2_get_byte(&g2) << color_shift;
229 b = bytestream2_get_byte(&g2) << color_shift;
230 entry = (r << 16) | (g << 8) |
b;
231 if (s->
palette[palette_ptr] != entry)
233 s->
palette[palette_ptr++] = entry;
248 compressed_lines = bytestream2_get_le16(&g2);
249 while (compressed_lines > 0) {
250 line_packets = bytestream2_get_le16(&g2);
251 if ((line_packets & 0xC000) == 0xC000) {
253 line_packets = -line_packets;
255 }
else if ((line_packets & 0xC000) == 0x4000) {
257 }
else if ((line_packets & 0xC000) == 0x8000) {
261 pixels[pixel_ptr] = line_packets & 0xff;
267 for (i = 0; i < line_packets; i++) {
269 pixel_skip = bytestream2_get_byte(&g2);
270 pixel_ptr += pixel_skip;
271 pixel_countdown -= pixel_skip;
272 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
274 byte_run = -byte_run;
275 palette_idx1 = bytestream2_get_byte(&g2);
276 palette_idx2 = bytestream2_get_byte(&g2);
278 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
279 pixels[pixel_ptr++] = palette_idx1;
280 pixels[pixel_ptr++] = palette_idx2;
284 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
285 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
297 starting_line = bytestream2_get_le16(&g2);
301 compressed_lines = bytestream2_get_le16(&g2);
302 while (compressed_lines > 0) {
306 line_packets = bytestream2_get_byte(&g2);
307 if (line_packets > 0) {
308 for (i = 0; i < line_packets; i++) {
310 pixel_skip = bytestream2_get_byte(&g2);
311 pixel_ptr += pixel_skip;
312 pixel_countdown -= pixel_skip;
313 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
316 for (j = 0; j < byte_run; j++, pixel_countdown--) {
317 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
319 }
else if (byte_run < 0) {
320 byte_run = -byte_run;
321 palette_idx1 = bytestream2_get_byte(&g2);
323 for (j = 0; j < byte_run; j++, pixel_countdown--) {
324 pixels[pixel_ptr++] = palette_idx1;
345 for (lines = 0; lines < s->
avctx->
height; lines++) {
351 while (pixel_countdown > 0) {
352 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
359 palette_idx1 = bytestream2_get_byte(&g2);
361 for (j = 0; j < byte_run; j++) {
362 pixels[pixel_ptr++] = palette_idx1;
364 if (pixel_countdown < 0)
366 pixel_countdown, lines);
369 byte_run = -byte_run;
371 for (j = 0; j < byte_run; j++) {
372 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
374 if (pixel_countdown < 0)
376 pixel_countdown, lines);
389 "bigger than image, skipping chunk\n", chunk_size - 6);
410 frame_size -= chunk_size;
419 "and final chunk ptr = %d\n", buf_size,
438 void *
data,
int *got_frame,
439 const uint8_t *buf,
int buf_size)
447 unsigned char palette_idx1;
452 unsigned int chunk_size;
458 int compressed_lines;
459 signed short line_packets;
464 unsigned char *pixels;
466 unsigned int pixel_limit;
478 frame_size = bytestream2_get_le32(&g2);
480 num_chunks = bytestream2_get_le16(&g2);
486 while ((frame_size > 0) && (num_chunks > 0)) {
487 chunk_size = bytestream2_get_le32(&g2);
488 chunk_type = bytestream2_get_le16(&g2);
490 switch (chunk_type) {
497 "Unexpected Palette chunk %d in non-palettized FLC\n",
505 compressed_lines = bytestream2_get_le16(&g2);
506 while (compressed_lines > 0) {
507 line_packets = bytestream2_get_le16(&g2);
508 if (line_packets < 0) {
509 line_packets = -line_packets;
516 for (i = 0; i < line_packets; i++) {
518 pixel_skip = bytestream2_get_byte(&g2);
519 pixel_ptr += (pixel_skip*2);
520 pixel_countdown -= pixel_skip;
521 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
523 byte_run = -byte_run;
524 pixel = bytestream2_get_le16(&g2);
526 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
527 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
532 for (j = 0; j < byte_run; j++, pixel_countdown--) {
533 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
551 memset(pixels, 0x0000,
557 for (lines = 0; lines < s->
avctx->
height; lines++) {
564 while (pixel_countdown > 0) {
565 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
567 palette_idx1 = bytestream2_get_byte(&g2);
569 for (j = 0; j < byte_run; j++) {
570 pixels[pixel_ptr++] = palette_idx1;
572 if (pixel_countdown < 0)
574 pixel_countdown, lines);
577 byte_run = -byte_run;
579 for (j = 0; j < byte_run; j++) {
580 palette_idx1 = bytestream2_get_byte(&g2);
581 pixels[pixel_ptr++] = palette_idx1;
583 if (pixel_countdown < 0)
585 pixel_countdown, lines);
598 while (pixel_countdown > 0) {
599 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
609 for (lines = 0; lines < s->
avctx->
height; lines++) {
616 while (pixel_countdown > 0) {
617 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
619 pixel = bytestream2_get_le16(&g2);
621 for (j = 0; j < byte_run; j++) {
622 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
625 if (pixel_countdown < 0)
630 byte_run = -byte_run;
632 for (j = 0; j < byte_run; j++) {
633 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
636 if (pixel_countdown < 0)
652 "bigger than image, skipping chunk\n", chunk_size - 6);
661 while (pixel_countdown > 0) {
662 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
680 frame_size -= chunk_size;
699 void *
data,
int *got_frame,
700 const uint8_t *buf,
int buf_size)
707 void *
data,
int *got_frame,
711 int buf_size = avpkt->
size;
730 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");