2 * FLI/FLC Animation Video Decoder
3 * Copyright (C) 2003, 2004 the ffmpeg project
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
44 #include "bytestream.h"
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) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
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; \
72 typedef struct FlicDecodeContext {
73 AVCodecContext *avctx;
76 unsigned int palette[256];
78 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
81 static av_cold int flic_decode_init(AVCodecContext *avctx)
83 FlicDecodeContext *s = avctx->priv_data;
84 unsigned char *fli_header = (unsigned char *)avctx->extradata;
87 if (avctx->extradata_size != 12 &&
88 avctx->extradata_size != 128) {
89 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
90 return AVERROR_INVALIDDATA;
95 s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
98 if (s->avctx->extradata_size == 12) {
99 /* special case for magic carpet FLIs */
100 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
103 depth = AV_RL16(&fli_header[12]);
107 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
110 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
111 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
115 case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
116 case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
117 case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
118 case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
119 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
120 return AVERROR_PATCHWELCOME;
122 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
123 return AVERROR_INVALIDDATA;
126 s->frame = av_frame_alloc();
128 return AVERROR(ENOMEM);
135 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
136 void *data, int *got_frame,
137 const uint8_t *buf, int buf_size)
139 FlicDecodeContext *s = avctx->priv_data;
142 int stream_ptr_after_color_chunk;
145 unsigned char palette_idx1;
146 unsigned char palette_idx2;
148 unsigned int frame_size;
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;
172 bytestream2_init(&g2, buf, buf_size);
174 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
175 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
179 pixels = s->frame->data[0];
180 pixel_limit = s->avctx->height * s->frame->linesize[0];
181 frame_size = bytestream2_get_le32(&g2);
182 bytestream2_skip(&g2, 2); /* skip the magic number */
183 num_chunks = bytestream2_get_le16(&g2);
184 bytestream2_skip(&g2, 8); /* skip padding */
188 /* iterate through the chunks */
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) {
196 stream_ptr_after_color_chunk = bytestream2_tell(&g2) + 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 = bytestream2_get_le16(&g2);
209 for (i = 0; i < color_packets; i++) {
210 /* first byte is how many colors to skip */
211 palette_ptr += bytestream2_get_byte(&g2);
213 /* next byte indicates how many entries to change */
214 color_changes = bytestream2_get_byte(&g2);
216 /* if there are 0 color changes, there are actually 256 */
217 if (color_changes == 0)
220 for (j = 0; j < color_changes; j++) {
223 /* wrap around, for good measure */
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;
237 /* color chunks sometimes have weird 16-bit alignment issues;
238 * therefore, take the hardline approach and skip
239 * to the value calculated w.r.t. the size specified by the color
241 if (stream_ptr_after_color_chunk - bytestream2_tell(&g2) > 0)
242 bytestream2_skip(&g2, stream_ptr_after_color_chunk - bytestream2_tell(&g2));
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;
254 y_ptr += line_packets * s->frame->linesize[0];
255 } else if ((line_packets & 0xC000) == 0x4000) {
256 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
257 } else if ((line_packets & 0xC000) == 0x8000) {
258 // "last byte" opcode
259 pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
261 pixels[pixel_ptr] = line_packets & 0xff;
266 pixel_countdown = s->avctx->width;
267 for (i = 0; i < line_packets; i++) {
268 /* account for the skip bytes */
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);
277 CHECK_PIXEL_PTR(byte_run * 2);
278 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
279 pixels[pixel_ptr++] = palette_idx1;
280 pixels[pixel_ptr++] = palette_idx2;
283 CHECK_PIXEL_PTR(byte_run * 2);
284 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
285 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
290 y_ptr += s->frame->linesize[0];
296 /* line compressed */
297 starting_line = bytestream2_get_le16(&g2);
299 y_ptr += starting_line * s->frame->linesize[0];
301 compressed_lines = bytestream2_get_le16(&g2);
302 while (compressed_lines > 0) {
305 pixel_countdown = s->avctx->width;
306 line_packets = bytestream2_get_byte(&g2);
307 if (line_packets > 0) {
308 for (i = 0; i < line_packets; i++) {
309 /* account for the skip bytes */
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);
315 CHECK_PIXEL_PTR(byte_run);
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);
322 CHECK_PIXEL_PTR(byte_run);
323 for (j = 0; j < byte_run; j++, pixel_countdown--) {
324 pixels[pixel_ptr++] = palette_idx1;
330 y_ptr += s->frame->linesize[0];
336 /* set the whole frame to color 0 (which is usually black) */
338 s->frame->linesize[0] * s->avctx->height);
342 /* Byte run compression: This chunk type only occurs in the first
343 * FLI frame and it will update the entire frame. */
345 for (lines = 0; lines < s->avctx->height; lines++) {
347 /* disregard the line packets; instead, iterate through all
349 bytestream2_skip(&g2, 1);
350 pixel_countdown = s->avctx->width;
351 while (pixel_countdown > 0) {
352 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
354 av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
355 return AVERROR_INVALIDDATA;
359 palette_idx1 = bytestream2_get_byte(&g2);
360 CHECK_PIXEL_PTR(byte_run);
361 for (j = 0; j < byte_run; j++) {
362 pixels[pixel_ptr++] = palette_idx1;
364 if (pixel_countdown < 0)
365 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
366 pixel_countdown, lines);
368 } else { /* copy bytes if byte_run < 0 */
369 byte_run = -byte_run;
370 CHECK_PIXEL_PTR(byte_run);
371 for (j = 0; j < byte_run; j++) {
372 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
374 if (pixel_countdown < 0)
375 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
376 pixel_countdown, lines);
381 y_ptr += s->frame->linesize[0];
386 /* copy the chunk (uncompressed frame) */
387 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
388 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
389 "bigger than image, skipping chunk\n", chunk_size - 6);
390 bytestream2_skip(&g2, chunk_size - 6);
392 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
393 y_ptr += s->frame->linesize[0]) {
394 bytestream2_get_buffer(&g2, &pixels[y_ptr],
401 /* some sort of a thumbnail? disregard this chunk... */
402 bytestream2_skip(&g2, chunk_size - 6);
406 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
410 frame_size -= chunk_size;
414 /* by the end of the chunk, the stream ptr should equal the frame
415 * size (minus 1, possibly); if it doesn't, issue a warning */
416 if ((bytestream2_get_bytes_left(&g2) != 0) &&
417 (bytestream2_get_bytes_left(&g2) != 1))
418 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
419 "and final chunk ptr = %d\n", buf_size,
420 buf_size - bytestream2_get_bytes_left(&g2));
422 /* make the palette available on the way out */
423 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
424 if (s->new_palette) {
425 s->frame->palette_has_changed = 1;
429 if ((ret = av_frame_ref(data, s->frame)) < 0)
437 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
438 void *data, int *got_frame,
439 const uint8_t *buf, int buf_size)
441 /* Note, the only difference between the 15Bpp and 16Bpp */
442 /* Format is the pixel format, the packets are processed the same. */
443 FlicDecodeContext *s = avctx->priv_data;
447 unsigned char palette_idx1;
449 unsigned int frame_size;
452 unsigned int chunk_size;
458 int compressed_lines;
459 signed short line_packets;
464 unsigned char *pixels;
466 unsigned int pixel_limit;
468 bytestream2_init(&g2, buf, buf_size);
470 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
471 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
475 pixels = s->frame->data[0];
476 pixel_limit = s->avctx->height * s->frame->linesize[0];
478 frame_size = bytestream2_get_le32(&g2);
479 bytestream2_skip(&g2, 2); /* skip the magic number */
480 num_chunks = bytestream2_get_le16(&g2);
481 bytestream2_skip(&g2, 8); /* skip padding */
485 /* iterate through the chunks */
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) {
493 /* For some reason, it seems that non-palettized flics do
494 * include one of these chunks in their first frame.
495 * Why I do not know, it seems rather extraneous. */
497 "Unexpected Palette chunk %d in non-palettized FLC\n",
499 bytestream2_skip(&g2, chunk_size - 6);
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;
510 y_ptr += line_packets * s->frame->linesize[0];
515 pixel_countdown = s->avctx->width;
516 for (i = 0; i < line_packets; i++) {
517 /* account for the skip bytes */
518 pixel_skip = bytestream2_get_byte(&g2);
519 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
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);
525 CHECK_PIXEL_PTR(2 * byte_run);
526 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
527 *((signed short*)(&pixels[pixel_ptr])) = pixel;
531 CHECK_PIXEL_PTR(2 * byte_run);
532 for (j = 0; j < byte_run; j++, pixel_countdown--) {
533 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
539 y_ptr += s->frame->linesize[0];
545 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
546 bytestream2_skip(&g2, chunk_size - 6);
550 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
551 memset(pixels, 0x0000,
552 s->frame->linesize[0] * s->avctx->height);
557 for (lines = 0; lines < s->avctx->height; lines++) {
559 /* disregard the line packets; instead, iterate through all
561 bytestream2_skip(&g2, 1);
562 pixel_countdown = (s->avctx->width * 2);
564 while (pixel_countdown > 0) {
565 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
567 palette_idx1 = bytestream2_get_byte(&g2);
568 CHECK_PIXEL_PTR(byte_run);
569 for (j = 0; j < byte_run; j++) {
570 pixels[pixel_ptr++] = palette_idx1;
572 if (pixel_countdown < 0)
573 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
574 pixel_countdown, lines);
576 } else { /* copy bytes if byte_run < 0 */
577 byte_run = -byte_run;
578 CHECK_PIXEL_PTR(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)
584 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
585 pixel_countdown, lines);
590 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
591 * This does not give us any good opportunity to perform word endian conversion
592 * during decompression. So if it is required (i.e., this is not a LE target, we do
593 * a second pass over the line here, swapping the bytes.
597 pixel_countdown = s->avctx->width;
598 while (pixel_countdown > 0) {
599 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
603 y_ptr += s->frame->linesize[0];
609 for (lines = 0; lines < s->avctx->height; lines++) {
611 /* disregard the line packets; instead, iterate through all
613 bytestream2_skip(&g2, 1);
614 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
616 while (pixel_countdown > 0) {
617 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
619 pixel = bytestream2_get_le16(&g2);
620 CHECK_PIXEL_PTR(2 * byte_run);
621 for (j = 0; j < byte_run; j++) {
622 *((signed short*)(&pixels[pixel_ptr])) = pixel;
625 if (pixel_countdown < 0)
626 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
629 } else { /* copy pixels if byte_run < 0 */
630 byte_run = -byte_run;
631 CHECK_PIXEL_PTR(2 * 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)
637 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
643 y_ptr += s->frame->linesize[0];
649 /* copy the chunk (uncompressed frame) */
650 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
651 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
652 "bigger than image, skipping chunk\n", chunk_size - 6);
653 bytestream2_skip(&g2, chunk_size - 6);
656 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
657 y_ptr += s->frame->linesize[0]) {
659 pixel_countdown = s->avctx->width;
661 while (pixel_countdown > 0) {
662 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
671 /* some sort of a thumbnail? disregard this chunk... */
672 bytestream2_skip(&g2, chunk_size - 6);
676 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
680 frame_size -= chunk_size;
684 /* by the end of the chunk, the stream ptr should equal the frame
685 * size (minus 1, possibly); if it doesn't, issue a warning */
686 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
687 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
688 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
690 if ((ret = av_frame_ref(data, s->frame)) < 0)
698 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
699 void *data, int *got_frame,
700 const uint8_t *buf, int buf_size)
702 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
703 return AVERROR_PATCHWELCOME;
706 static int flic_decode_frame(AVCodecContext *avctx,
707 void *data, int *got_frame,
710 const uint8_t *buf = avpkt->data;
711 int buf_size = avpkt->size;
712 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
713 return flic_decode_frame_8BPP(avctx, data, got_frame,
716 else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
717 (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
718 return flic_decode_frame_15_16BPP(avctx, data, got_frame,
721 else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
722 return flic_decode_frame_24BPP(avctx, data, got_frame,
726 /* Should not get here, ever as the pix_fmt is processed */
727 /* in flic_decode_init and the above if should deal with */
728 /* the finite set of possibilites allowable by here. */
729 /* But in case we do, just error out. */
730 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
735 static av_cold int flic_decode_end(AVCodecContext *avctx)
737 FlicDecodeContext *s = avctx->priv_data;
739 av_frame_free(&s->frame);
744 AVCodec ff_flic_decoder = {
746 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
747 .type = AVMEDIA_TYPE_VIDEO,
748 .id = AV_CODEC_ID_FLIC,
749 .priv_data_size = sizeof(FlicDecodeContext),
750 .init = flic_decode_init,
751 .close = flic_decode_end,
752 .decode = flic_decode_frame,
753 .capabilities = AV_CODEC_CAP_DR1,