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 avcodec_get_frame_defaults(&s->frame);
132 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
133 void *data, int *got_frame,
134 const uint8_t *buf, int buf_size)
136 FlicDecodeContext *s = avctx->priv_data;
139 int stream_ptr_after_color_chunk;
142 unsigned char palette_idx1;
143 unsigned char palette_idx2;
145 unsigned int frame_size;
148 unsigned int chunk_size;
156 unsigned char r, g, b;
159 int compressed_lines;
161 signed short line_packets;
166 unsigned char *pixels;
167 unsigned int pixel_limit;
169 bytestream2_init(&g2, buf, buf_size);
171 if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) {
172 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
176 pixels = s->frame.data[0];
177 pixel_limit = s->avctx->height * s->frame.linesize[0];
178 frame_size = bytestream2_get_le32(&g2);
179 bytestream2_skip(&g2, 2); /* skip the magic number */
180 num_chunks = bytestream2_get_le16(&g2);
181 bytestream2_skip(&g2, 8); /* skip padding */
185 /* iterate through the chunks */
186 while ((frame_size > 0) && (num_chunks > 0)) {
187 chunk_size = bytestream2_get_le32(&g2);
188 chunk_type = bytestream2_get_le16(&g2);
190 switch (chunk_type) {
193 stream_ptr_after_color_chunk = bytestream2_tell(&g2) + chunk_size - 6;
195 /* check special case: If this file is from the Magic Carpet
196 * game and uses 6-bit colors even though it reports 256-color
197 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
199 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
203 /* set up the palette */
204 color_packets = bytestream2_get_le16(&g2);
206 for (i = 0; i < color_packets; i++) {
207 /* first byte is how many colors to skip */
208 palette_ptr += bytestream2_get_byte(&g2);
210 /* next byte indicates how many entries to change */
211 color_changes = bytestream2_get_byte(&g2);
213 /* if there are 0 color changes, there are actually 256 */
214 if (color_changes == 0)
217 for (j = 0; j < color_changes; j++) {
220 /* wrap around, for good measure */
221 if ((unsigned)palette_ptr >= 256)
224 r = bytestream2_get_byte(&g2) << color_shift;
225 g = bytestream2_get_byte(&g2) << color_shift;
226 b = bytestream2_get_byte(&g2) << color_shift;
227 entry = (r << 16) | (g << 8) | b;
228 if (s->palette[palette_ptr] != entry)
230 s->palette[palette_ptr++] = entry;
234 /* color chunks sometimes have weird 16-bit alignment issues;
235 * therefore, take the hardline approach and skip
236 * to the value calculated w.r.t. the size specified by the color
238 if (stream_ptr_after_color_chunk - bytestream2_tell(&g2) > 0)
239 bytestream2_skip(&g2, stream_ptr_after_color_chunk - bytestream2_tell(&g2));
245 compressed_lines = bytestream2_get_le16(&g2);
246 while (compressed_lines > 0) {
247 line_packets = bytestream2_get_le16(&g2);
248 if ((line_packets & 0xC000) == 0xC000) {
250 line_packets = -line_packets;
251 y_ptr += line_packets * s->frame.linesize[0];
252 } else if ((line_packets & 0xC000) == 0x4000) {
253 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
254 } else if ((line_packets & 0xC000) == 0x8000) {
255 // "last byte" opcode
256 pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
258 pixels[pixel_ptr] = line_packets & 0xff;
263 pixel_countdown = s->avctx->width;
264 for (i = 0; i < line_packets; i++) {
265 /* account for the skip bytes */
266 pixel_skip = bytestream2_get_byte(&g2);
267 pixel_ptr += pixel_skip;
268 pixel_countdown -= pixel_skip;
269 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
271 byte_run = -byte_run;
272 palette_idx1 = bytestream2_get_byte(&g2);
273 palette_idx2 = bytestream2_get_byte(&g2);
274 CHECK_PIXEL_PTR(byte_run * 2);
275 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
276 pixels[pixel_ptr++] = palette_idx1;
277 pixels[pixel_ptr++] = palette_idx2;
280 CHECK_PIXEL_PTR(byte_run * 2);
281 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
282 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
287 y_ptr += s->frame.linesize[0];
293 /* line compressed */
294 starting_line = bytestream2_get_le16(&g2);
296 y_ptr += starting_line * s->frame.linesize[0];
298 compressed_lines = bytestream2_get_le16(&g2);
299 while (compressed_lines > 0) {
302 pixel_countdown = s->avctx->width;
303 line_packets = bytestream2_get_byte(&g2);
304 if (line_packets > 0) {
305 for (i = 0; i < line_packets; i++) {
306 /* account for the skip bytes */
307 pixel_skip = bytestream2_get_byte(&g2);
308 pixel_ptr += pixel_skip;
309 pixel_countdown -= pixel_skip;
310 byte_run = sign_extend(bytestream2_get_byte(&g2),8);
312 CHECK_PIXEL_PTR(byte_run);
313 for (j = 0; j < byte_run; j++, pixel_countdown--) {
314 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
316 } else if (byte_run < 0) {
317 byte_run = -byte_run;
318 palette_idx1 = bytestream2_get_byte(&g2);
319 CHECK_PIXEL_PTR(byte_run);
320 for (j = 0; j < byte_run; j++, pixel_countdown--) {
321 pixels[pixel_ptr++] = palette_idx1;
327 y_ptr += s->frame.linesize[0];
333 /* set the whole frame to color 0 (which is usually black) */
335 s->frame.linesize[0] * s->avctx->height);
339 /* Byte run compression: This chunk type only occurs in the first
340 * FLI frame and it will update the entire frame. */
342 for (lines = 0; lines < s->avctx->height; lines++) {
344 /* disregard the line packets; instead, iterate through all
346 bytestream2_skip(&g2, 1);
347 pixel_countdown = s->avctx->width;
348 while (pixel_countdown > 0) {
349 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
351 av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
352 return AVERROR_INVALIDDATA;
356 palette_idx1 = bytestream2_get_byte(&g2);
357 CHECK_PIXEL_PTR(byte_run);
358 for (j = 0; j < byte_run; j++) {
359 pixels[pixel_ptr++] = palette_idx1;
361 if (pixel_countdown < 0)
362 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
363 pixel_countdown, lines);
365 } else { /* copy bytes if byte_run < 0 */
366 byte_run = -byte_run;
367 CHECK_PIXEL_PTR(byte_run);
368 for (j = 0; j < byte_run; j++) {
369 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
371 if (pixel_countdown < 0)
372 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
373 pixel_countdown, lines);
378 y_ptr += s->frame.linesize[0];
383 /* copy the chunk (uncompressed frame) */
384 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
385 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
386 "bigger than image, skipping chunk\n", chunk_size - 6);
387 bytestream2_skip(&g2, chunk_size - 6);
389 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
390 y_ptr += s->frame.linesize[0]) {
391 bytestream2_get_buffer(&g2, &pixels[y_ptr],
398 /* some sort of a thumbnail? disregard this chunk... */
399 bytestream2_skip(&g2, chunk_size - 6);
403 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
407 frame_size -= chunk_size;
411 /* by the end of the chunk, the stream ptr should equal the frame
412 * size (minus 1, possibly); if it doesn't, issue a warning */
413 if ((bytestream2_get_bytes_left(&g2) != 0) &&
414 (bytestream2_get_bytes_left(&g2) != 1))
415 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
416 "and final chunk ptr = %d\n", buf_size,
417 buf_size - bytestream2_get_bytes_left(&g2));
419 /* make the palette available on the way out */
420 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
421 if (s->new_palette) {
422 s->frame.palette_has_changed = 1;
426 if ((ret = av_frame_ref(data, &s->frame)) < 0)
434 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
435 void *data, int *got_frame,
436 const uint8_t *buf, int buf_size)
438 /* Note, the only difference between the 15Bpp and 16Bpp */
439 /* Format is the pixel format, the packets are processed the same. */
440 FlicDecodeContext *s = avctx->priv_data;
444 unsigned char palette_idx1;
446 unsigned int frame_size;
449 unsigned int chunk_size;
455 int compressed_lines;
456 signed short line_packets;
461 unsigned char *pixels;
463 unsigned int pixel_limit;
465 bytestream2_init(&g2, buf, buf_size);
467 if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) {
468 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
472 pixels = s->frame.data[0];
473 pixel_limit = s->avctx->height * s->frame.linesize[0];
475 frame_size = bytestream2_get_le32(&g2);
476 bytestream2_skip(&g2, 2); /* skip the magic number */
477 num_chunks = bytestream2_get_le16(&g2);
478 bytestream2_skip(&g2, 8); /* skip padding */
482 /* iterate through the chunks */
483 while ((frame_size > 0) && (num_chunks > 0)) {
484 chunk_size = bytestream2_get_le32(&g2);
485 chunk_type = bytestream2_get_le16(&g2);
487 switch (chunk_type) {
490 /* For some reason, it seems that non-palettized flics do
491 * include one of these chunks in their first frame.
492 * Why I do not know, it seems rather extraneous. */
494 "Unexpected Palette chunk %d in non-palettized FLC\n",
496 bytestream2_skip(&g2, chunk_size - 6);
502 compressed_lines = bytestream2_get_le16(&g2);
503 while (compressed_lines > 0) {
504 line_packets = bytestream2_get_le16(&g2);
505 if (line_packets < 0) {
506 line_packets = -line_packets;
507 y_ptr += line_packets * s->frame.linesize[0];
512 pixel_countdown = s->avctx->width;
513 for (i = 0; i < line_packets; i++) {
514 /* account for the skip bytes */
515 pixel_skip = bytestream2_get_byte(&g2);
516 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
517 pixel_countdown -= pixel_skip;
518 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
520 byte_run = -byte_run;
521 pixel = bytestream2_get_le16(&g2);
522 CHECK_PIXEL_PTR(2 * byte_run);
523 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
524 *((signed short*)(&pixels[pixel_ptr])) = pixel;
528 CHECK_PIXEL_PTR(2 * byte_run);
529 for (j = 0; j < byte_run; j++, pixel_countdown--) {
530 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
536 y_ptr += s->frame.linesize[0];
542 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
543 bytestream2_skip(&g2, chunk_size - 6);
547 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
548 memset(pixels, 0x0000,
549 s->frame.linesize[0] * s->avctx->height);
554 for (lines = 0; lines < s->avctx->height; lines++) {
556 /* disregard the line packets; instead, iterate through all
558 bytestream2_skip(&g2, 1);
559 pixel_countdown = (s->avctx->width * 2);
561 while (pixel_countdown > 0) {
562 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
564 palette_idx1 = bytestream2_get_byte(&g2);
565 CHECK_PIXEL_PTR(byte_run);
566 for (j = 0; j < byte_run; j++) {
567 pixels[pixel_ptr++] = palette_idx1;
569 if (pixel_countdown < 0)
570 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
571 pixel_countdown, lines);
573 } else { /* copy bytes if byte_run < 0 */
574 byte_run = -byte_run;
575 CHECK_PIXEL_PTR(byte_run);
576 for (j = 0; j < byte_run; j++) {
577 palette_idx1 = bytestream2_get_byte(&g2);
578 pixels[pixel_ptr++] = palette_idx1;
580 if (pixel_countdown < 0)
581 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
582 pixel_countdown, lines);
587 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
588 * This does not give us any good opportunity to perform word endian conversion
589 * during decompression. So if it is required (i.e., this is not a LE target, we do
590 * a second pass over the line here, swapping the bytes.
594 pixel_countdown = s->avctx->width;
595 while (pixel_countdown > 0) {
596 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
600 y_ptr += s->frame.linesize[0];
606 for (lines = 0; lines < s->avctx->height; lines++) {
608 /* disregard the line packets; instead, iterate through all
610 bytestream2_skip(&g2, 1);
611 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
613 while (pixel_countdown > 0) {
614 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
616 pixel = bytestream2_get_le16(&g2);
617 CHECK_PIXEL_PTR(2 * byte_run);
618 for (j = 0; j < byte_run; j++) {
619 *((signed short*)(&pixels[pixel_ptr])) = pixel;
622 if (pixel_countdown < 0)
623 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
626 } else { /* copy pixels if byte_run < 0 */
627 byte_run = -byte_run;
628 CHECK_PIXEL_PTR(2 * byte_run);
629 for (j = 0; j < byte_run; j++) {
630 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
633 if (pixel_countdown < 0)
634 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
640 y_ptr += s->frame.linesize[0];
646 /* copy the chunk (uncompressed frame) */
647 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
648 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
649 "bigger than image, skipping chunk\n", chunk_size - 6);
650 bytestream2_skip(&g2, chunk_size - 6);
653 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
654 y_ptr += s->frame.linesize[0]) {
656 pixel_countdown = s->avctx->width;
658 while (pixel_countdown > 0) {
659 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
668 /* some sort of a thumbnail? disregard this chunk... */
669 bytestream2_skip(&g2, chunk_size - 6);
673 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
677 frame_size -= chunk_size;
681 /* by the end of the chunk, the stream ptr should equal the frame
682 * size (minus 1, possibly); if it doesn't, issue a warning */
683 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
684 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
685 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
687 if ((ret = av_frame_ref(data, &s->frame)) < 0)
695 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
696 void *data, int *got_frame,
697 const uint8_t *buf, int buf_size)
699 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
700 return AVERROR_PATCHWELCOME;
703 static int flic_decode_frame(AVCodecContext *avctx,
704 void *data, int *got_frame,
707 const uint8_t *buf = avpkt->data;
708 int buf_size = avpkt->size;
709 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
710 return flic_decode_frame_8BPP(avctx, data, got_frame,
713 else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
714 (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
715 return flic_decode_frame_15_16BPP(avctx, data, got_frame,
718 else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
719 return flic_decode_frame_24BPP(avctx, data, got_frame,
723 /* Should not get here, ever as the pix_fmt is processed */
724 /* in flic_decode_init and the above if should deal with */
725 /* the finite set of possibilites allowable by here. */
726 /* But in case we do, just error out. */
727 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
732 static av_cold int flic_decode_end(AVCodecContext *avctx)
734 FlicDecodeContext *s = avctx->priv_data;
736 av_frame_unref(&s->frame);
741 AVCodec ff_flic_decoder = {
743 .type = AVMEDIA_TYPE_VIDEO,
744 .id = AV_CODEC_ID_FLIC,
745 .priv_data_size = sizeof(FlicDecodeContext),
746 .init = flic_decode_init,
747 .close = flic_decode_end,
748 .decode = flic_decode_frame,
749 .capabilities = CODEC_CAP_DR1,
750 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),