2 * FLI/FLC Animation Video Decoder
3 * Copyright (C) 2003, 2004 the ffmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Autodesk Animator FLI/FLC Video Decoder
25 * by Mike Melanson (melanson@pcisys.net)
26 * for more information on the .fli/.flc file format and all of its many
28 * http://www.compuphase.com/flic.htm
30 * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
31 * colorspace data, depending on the FLC. To use this decoder, be
32 * sure that your demuxer sends the FLI file header to the decoder via
33 * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
34 * large. The only exception is for FLI files from the game "Magic Carpet",
35 * in which the header is only 12 bytes.
42 #include "libavutil/intreadwrite.h"
45 #define FLI_256_COLOR 4
53 #define FLI_DTA_BRUN 25
54 #define FLI_DTA_COPY 26
57 #define FLI_TYPE_CODE (0xAF11)
58 #define FLC_FLX_TYPE_CODE (0xAF12)
59 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
60 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
62 #define CHECK_PIXEL_PTR(n) \
63 if (pixel_ptr + n > pixel_limit) { \
64 av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
65 pixel_ptr + n, pixel_limit); \
66 return AVERROR_INVALIDDATA; \
69 typedef struct FlicDecodeContext {
70 AVCodecContext *avctx;
73 unsigned int palette[256];
75 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
78 static av_cold int flic_decode_init(AVCodecContext *avctx)
80 FlicDecodeContext *s = avctx->priv_data;
81 unsigned char *fli_header = (unsigned char *)avctx->extradata;
86 s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
89 if (s->avctx->extradata_size == 12) {
90 /* special case for magic carpet FLIs */
91 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
93 } else if (s->avctx->extradata_size != 128) {
94 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
97 depth = AV_RL16(&fli_header[12]);
101 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
104 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
105 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
109 case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
110 case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
111 case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
112 case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
113 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
117 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
121 s->frame.data[0] = NULL;
127 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
128 void *data, int *data_size,
129 const uint8_t *buf, int buf_size)
131 FlicDecodeContext *s = avctx->priv_data;
134 int stream_ptr_after_color_chunk;
137 unsigned char palette_idx1;
138 unsigned char palette_idx2;
140 unsigned int frame_size;
143 unsigned int chunk_size;
151 unsigned char r, g, b;
154 int compressed_lines;
156 signed short line_packets;
161 unsigned char *pixels;
162 unsigned int pixel_limit;
164 s->frame.reference = 1;
165 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
166 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
167 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
171 pixels = s->frame.data[0];
172 pixel_limit = s->avctx->height * s->frame.linesize[0];
174 frame_size = AV_RL32(&buf[stream_ptr]);
175 stream_ptr += 6; /* skip the magic number */
176 num_chunks = AV_RL16(&buf[stream_ptr]);
177 stream_ptr += 10; /* skip padding */
181 /* iterate through the chunks */
182 while ((frame_size > 0) && (num_chunks > 0)) {
183 chunk_size = AV_RL32(&buf[stream_ptr]);
184 if (chunk_size > frame_size) {
185 av_log(avctx, AV_LOG_WARNING,
186 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
187 chunk_size = frame_size;
190 chunk_type = AV_RL16(&buf[stream_ptr]);
193 switch (chunk_type) {
196 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
198 /* check special case: If this file is from the Magic Carpet
199 * game and uses 6-bit colors even though it reports 256-color
200 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
202 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
206 /* set up the palette */
207 color_packets = AV_RL16(&buf[stream_ptr]);
210 for (i = 0; i < color_packets; i++) {
211 /* first byte is how many colors to skip */
212 palette_ptr += buf[stream_ptr++];
214 /* next byte indicates how many entries to change */
215 color_changes = buf[stream_ptr++];
217 /* if there are 0 color changes, there are actually 256 */
218 if (color_changes == 0)
221 for (j = 0; j < color_changes; j++) {
224 /* wrap around, for good measure */
225 if ((unsigned)palette_ptr >= 256)
228 r = buf[stream_ptr++] << color_shift;
229 g = buf[stream_ptr++] << color_shift;
230 b = buf[stream_ptr++] << color_shift;
231 entry = (r << 16) | (g << 8) | b;
232 if (s->palette[palette_ptr] != entry)
234 s->palette[palette_ptr++] = entry;
238 /* color chunks sometimes have weird 16-bit alignment issues;
239 * therefore, take the hardline approach and set the stream_ptr
240 * to the value calculated w.r.t. the size specified by the color
242 stream_ptr = stream_ptr_after_color_chunk;
248 compressed_lines = AV_RL16(&buf[stream_ptr]);
250 while (compressed_lines > 0) {
251 line_packets = AV_RL16(&buf[stream_ptr]);
253 if ((line_packets & 0xC000) == 0xC000) {
255 line_packets = -line_packets;
256 y_ptr += line_packets * s->frame.linesize[0];
257 } else if ((line_packets & 0xC000) == 0x4000) {
258 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
259 } else if ((line_packets & 0xC000) == 0x8000) {
260 // "last byte" opcode
261 pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
263 pixels[pixel_ptr] = line_packets & 0xff;
268 pixel_countdown = s->avctx->width;
269 for (i = 0; i < line_packets; i++) {
270 /* account for the skip bytes */
271 pixel_skip = buf[stream_ptr++];
272 pixel_ptr += pixel_skip;
273 pixel_countdown -= pixel_skip;
274 byte_run = (signed char)(buf[stream_ptr++]);
276 byte_run = -byte_run;
277 palette_idx1 = buf[stream_ptr++];
278 palette_idx2 = buf[stream_ptr++];
279 CHECK_PIXEL_PTR(byte_run * 2);
280 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
281 pixels[pixel_ptr++] = palette_idx1;
282 pixels[pixel_ptr++] = palette_idx2;
285 CHECK_PIXEL_PTR(byte_run * 2);
286 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
287 palette_idx1 = buf[stream_ptr++];
288 pixels[pixel_ptr++] = palette_idx1;
293 y_ptr += s->frame.linesize[0];
299 /* line compressed */
300 starting_line = AV_RL16(&buf[stream_ptr]);
303 y_ptr += starting_line * s->frame.linesize[0];
305 compressed_lines = AV_RL16(&buf[stream_ptr]);
307 while (compressed_lines > 0) {
310 pixel_countdown = s->avctx->width;
311 line_packets = buf[stream_ptr++];
312 if (line_packets > 0) {
313 for (i = 0; i < line_packets; i++) {
314 /* account for the skip bytes */
315 pixel_skip = buf[stream_ptr++];
316 pixel_ptr += pixel_skip;
317 pixel_countdown -= pixel_skip;
318 byte_run = (signed char)(buf[stream_ptr++]);
320 CHECK_PIXEL_PTR(byte_run);
321 for (j = 0; j < byte_run; j++, pixel_countdown--) {
322 palette_idx1 = buf[stream_ptr++];
323 pixels[pixel_ptr++] = palette_idx1;
325 } else if (byte_run < 0) {
326 byte_run = -byte_run;
327 palette_idx1 = buf[stream_ptr++];
328 CHECK_PIXEL_PTR(byte_run);
329 for (j = 0; j < byte_run; j++, pixel_countdown--) {
330 pixels[pixel_ptr++] = palette_idx1;
336 y_ptr += s->frame.linesize[0];
342 /* set the whole frame to color 0 (which is usually black) */
344 s->frame.linesize[0] * s->avctx->height);
348 /* Byte run compression: This chunk type only occurs in the first
349 * FLI frame and it will update the entire frame. */
351 for (lines = 0; lines < s->avctx->height; lines++) {
353 /* disregard the line packets; instead, iterate through all
356 pixel_countdown = s->avctx->width;
357 while (pixel_countdown > 0) {
358 byte_run = (signed char)(buf[stream_ptr++]);
360 palette_idx1 = buf[stream_ptr++];
361 CHECK_PIXEL_PTR(byte_run);
362 for (j = 0; j < byte_run; j++) {
363 pixels[pixel_ptr++] = palette_idx1;
365 if (pixel_countdown < 0)
366 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
367 pixel_countdown, lines);
369 } else { /* copy bytes if byte_run < 0 */
370 byte_run = -byte_run;
371 CHECK_PIXEL_PTR(byte_run);
372 for (j = 0; j < byte_run; j++) {
373 palette_idx1 = buf[stream_ptr++];
374 pixels[pixel_ptr++] = palette_idx1;
376 if (pixel_countdown < 0)
377 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
378 pixel_countdown, lines);
383 y_ptr += s->frame.linesize[0];
388 /* copy the chunk (uncompressed frame) */
389 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
390 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
391 "bigger than image, skipping chunk\n", chunk_size - 6);
392 stream_ptr += chunk_size - 6;
394 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
395 y_ptr += s->frame.linesize[0]) {
396 memcpy(&pixels[y_ptr], &buf[stream_ptr],
398 stream_ptr += s->avctx->width;
404 /* some sort of a thumbnail? disregard this chunk... */
405 stream_ptr += chunk_size - 6;
409 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
413 frame_size -= chunk_size;
417 /* by the end of the chunk, the stream ptr should equal the frame
418 * size (minus 1, possibly); if it doesn't, issue a warning */
419 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
420 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
421 "and final chunk ptr = %d\n", buf_size, stream_ptr);
423 /* make the palette available on the way out */
424 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
425 if (s->new_palette) {
426 s->frame.palette_has_changed = 1;
430 *data_size=sizeof(AVFrame);
431 *(AVFrame*)data = s->frame;
436 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
437 void *data, int *data_size,
438 const uint8_t *buf, int buf_size)
440 /* Note, the only difference between the 15Bpp and 16Bpp */
441 /* Format is the pixel format, the packets are processed the same. */
442 FlicDecodeContext *s = avctx->priv_data;
446 unsigned char palette_idx1;
448 unsigned int frame_size;
451 unsigned int chunk_size;
457 int compressed_lines;
458 signed short line_packets;
463 unsigned char *pixels;
465 unsigned int pixel_limit;
467 s->frame.reference = 1;
468 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
469 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
470 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
474 pixels = s->frame.data[0];
475 pixel_limit = s->avctx->height * s->frame.linesize[0];
477 frame_size = AV_RL32(&buf[stream_ptr]);
478 stream_ptr += 6; /* skip the magic number */
479 num_chunks = AV_RL16(&buf[stream_ptr]);
480 stream_ptr += 10; /* skip padding */
484 /* iterate through the chunks */
485 while ((frame_size > 0) && (num_chunks > 0)) {
486 chunk_size = AV_RL32(&buf[stream_ptr]);
488 chunk_type = AV_RL16(&buf[stream_ptr]);
491 switch (chunk_type) {
494 /* For some reason, it seems that non-palettized flics do
495 * include one of these chunks in their first frame.
496 * Why I do not know, it seems rather extraneous. */
497 /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
498 stream_ptr = stream_ptr + chunk_size - 6;
504 compressed_lines = AV_RL16(&buf[stream_ptr]);
506 while (compressed_lines > 0) {
507 line_packets = AV_RL16(&buf[stream_ptr]);
509 if (line_packets < 0) {
510 line_packets = -line_packets;
511 y_ptr += line_packets * s->frame.linesize[0];
516 pixel_countdown = s->avctx->width;
517 for (i = 0; i < line_packets; i++) {
518 /* account for the skip bytes */
519 pixel_skip = buf[stream_ptr++];
520 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
521 pixel_countdown -= pixel_skip;
522 byte_run = (signed char)(buf[stream_ptr++]);
524 byte_run = -byte_run;
525 pixel = AV_RL16(&buf[stream_ptr]);
527 CHECK_PIXEL_PTR(2 * byte_run);
528 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
529 *((signed short*)(&pixels[pixel_ptr])) = pixel;
533 CHECK_PIXEL_PTR(2 * byte_run);
534 for (j = 0; j < byte_run; j++, pixel_countdown--) {
535 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
542 y_ptr += s->frame.linesize[0];
548 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
549 stream_ptr = stream_ptr + chunk_size - 6;
553 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
554 memset(pixels, 0x0000,
555 s->frame.linesize[0] * s->avctx->height);
560 for (lines = 0; lines < s->avctx->height; lines++) {
562 /* disregard the line packets; instead, iterate through all
565 pixel_countdown = (s->avctx->width * 2);
567 while (pixel_countdown > 0) {
568 byte_run = (signed char)(buf[stream_ptr++]);
570 palette_idx1 = buf[stream_ptr++];
571 CHECK_PIXEL_PTR(byte_run);
572 for (j = 0; j < byte_run; j++) {
573 pixels[pixel_ptr++] = palette_idx1;
575 if (pixel_countdown < 0)
576 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
577 pixel_countdown, lines);
579 } else { /* copy bytes if byte_run < 0 */
580 byte_run = -byte_run;
581 CHECK_PIXEL_PTR(byte_run);
582 for (j = 0; j < byte_run; j++) {
583 palette_idx1 = buf[stream_ptr++];
584 pixels[pixel_ptr++] = palette_idx1;
586 if (pixel_countdown < 0)
587 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
588 pixel_countdown, lines);
593 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
594 * This does not give us any good oportunity to perform word endian conversion
595 * during decompression. So if it is required (i.e., this is not a LE target, we do
596 * a second pass over the line here, swapping the bytes.
600 pixel_countdown = s->avctx->width;
601 while (pixel_countdown > 0) {
602 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
606 y_ptr += s->frame.linesize[0];
612 for (lines = 0; lines < s->avctx->height; lines++) {
614 /* disregard the line packets; instead, iterate through all
617 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
619 while (pixel_countdown > 0) {
620 byte_run = (signed char)(buf[stream_ptr++]);
622 pixel = AV_RL16(&buf[stream_ptr]);
624 CHECK_PIXEL_PTR(2 * byte_run);
625 for (j = 0; j < byte_run; j++) {
626 *((signed short*)(&pixels[pixel_ptr])) = pixel;
629 if (pixel_countdown < 0)
630 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
633 } else { /* copy pixels if byte_run < 0 */
634 byte_run = -byte_run;
635 CHECK_PIXEL_PTR(2 * byte_run);
636 for (j = 0; j < byte_run; j++) {
637 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
641 if (pixel_countdown < 0)
642 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
648 y_ptr += s->frame.linesize[0];
654 /* copy the chunk (uncompressed frame) */
655 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
656 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
657 "bigger than image, skipping chunk\n", chunk_size - 6);
658 stream_ptr += chunk_size - 6;
661 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
662 y_ptr += s->frame.linesize[0]) {
664 pixel_countdown = s->avctx->width;
666 while (pixel_countdown > 0) {
667 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = AV_RL16(&buf[stream_ptr+pixel_ptr]);
671 stream_ptr += s->avctx->width*2;
677 /* some sort of a thumbnail? disregard this chunk... */
678 stream_ptr += chunk_size - 6;
682 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
686 frame_size -= chunk_size;
690 /* by the end of the chunk, the stream ptr should equal the frame
691 * size (minus 1, possibly); if it doesn't, issue a warning */
692 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
693 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
694 "and final chunk ptr = %d\n", buf_size, stream_ptr);
697 *data_size=sizeof(AVFrame);
698 *(AVFrame*)data = s->frame;
703 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
704 void *data, int *data_size,
705 const uint8_t *buf, int buf_size)
707 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
711 static int flic_decode_frame(AVCodecContext *avctx,
712 void *data, int *data_size,
715 const uint8_t *buf = avpkt->data;
716 int buf_size = avpkt->size;
717 if (avctx->pix_fmt == PIX_FMT_PAL8) {
718 return flic_decode_frame_8BPP(avctx, data, data_size,
721 else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
722 (avctx->pix_fmt == PIX_FMT_RGB565)) {
723 return flic_decode_frame_15_16BPP(avctx, data, data_size,
726 else if (avctx->pix_fmt == PIX_FMT_BGR24) {
727 return flic_decode_frame_24BPP(avctx, data, data_size,
731 /* Should not get here, ever as the pix_fmt is processed */
732 /* in flic_decode_init and the above if should deal with */
733 /* the finite set of possibilites allowable by here. */
734 /* But in case we do, just error out. */
735 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
740 static av_cold int flic_decode_end(AVCodecContext *avctx)
742 FlicDecodeContext *s = avctx->priv_data;
744 if (s->frame.data[0])
745 avctx->release_buffer(avctx, &s->frame);
750 AVCodec ff_flic_decoder = {
754 sizeof(FlicDecodeContext),
764 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),