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"
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_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
65 pixel_ptr + n, pixel_limit); \
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;
84 if (avctx->extradata_size != 12 &&
85 avctx->extradata_size != 128) {
86 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
87 return AVERROR_INVALIDDATA;
92 s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
95 if (s->avctx->extradata_size == 12) {
96 /* special case for magic carpet FLIs */
97 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
100 depth = AV_RL16(&fli_header[12]);
104 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
107 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
108 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
112 case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
113 case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
114 case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
115 case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
116 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
119 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
123 s->frame.data[0] = NULL;
129 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
130 void *data, int *data_size,
131 const uint8_t *buf, int buf_size)
133 FlicDecodeContext *s = avctx->priv_data;
136 int stream_ptr_after_color_chunk;
139 unsigned char palette_idx1;
140 unsigned char palette_idx2;
142 unsigned int frame_size;
145 unsigned int chunk_size;
153 unsigned char r, g, b;
156 int compressed_lines;
158 signed short line_packets;
163 unsigned char *pixels;
164 unsigned int pixel_limit;
166 s->frame.reference = 1;
167 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
168 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
169 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
173 pixels = s->frame.data[0];
174 pixel_limit = s->avctx->height * s->frame.linesize[0];
176 frame_size = AV_RL32(&buf[stream_ptr]);
177 stream_ptr += 6; /* skip the magic number */
178 num_chunks = AV_RL16(&buf[stream_ptr]);
179 stream_ptr += 10; /* skip padding */
183 /* iterate through the chunks */
184 while ((frame_size > 0) && (num_chunks > 0)) {
185 chunk_size = AV_RL32(&buf[stream_ptr]);
187 chunk_type = AV_RL16(&buf[stream_ptr]);
190 switch (chunk_type) {
193 stream_ptr_after_color_chunk = stream_ptr + 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 = AV_RL16(&buf[stream_ptr]);
207 for (i = 0; i < color_packets; i++) {
208 /* first byte is how many colors to skip */
209 palette_ptr += buf[stream_ptr++];
211 /* next byte indicates how many entries to change */
212 color_changes = buf[stream_ptr++];
214 /* if there are 0 color changes, there are actually 256 */
215 if (color_changes == 0)
218 for (j = 0; j < color_changes; j++) {
221 /* wrap around, for good measure */
222 if ((unsigned)palette_ptr >= 256)
225 r = buf[stream_ptr++] << color_shift;
226 g = buf[stream_ptr++] << color_shift;
227 b = buf[stream_ptr++] << color_shift;
228 entry = (r << 16) | (g << 8) | b;
229 if (s->palette[palette_ptr] != entry)
231 s->palette[palette_ptr++] = entry;
235 /* color chunks sometimes have weird 16-bit alignment issues;
236 * therefore, take the hardline approach and set the stream_ptr
237 * to the value calculated w.r.t. the size specified by the color
239 stream_ptr = stream_ptr_after_color_chunk;
245 compressed_lines = AV_RL16(&buf[stream_ptr]);
247 while (compressed_lines > 0) {
248 line_packets = AV_RL16(&buf[stream_ptr]);
250 if ((line_packets & 0xC000) == 0xC000) {
252 line_packets = -line_packets;
253 y_ptr += line_packets * s->frame.linesize[0];
254 } else if ((line_packets & 0xC000) == 0x4000) {
255 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
256 } else if ((line_packets & 0xC000) == 0x8000) {
257 // "last byte" opcode
258 pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
260 pixels[pixel_ptr] = line_packets & 0xff;
265 pixel_countdown = s->avctx->width;
266 for (i = 0; i < line_packets; i++) {
267 /* account for the skip bytes */
268 pixel_skip = buf[stream_ptr++];
269 pixel_ptr += pixel_skip;
270 pixel_countdown -= pixel_skip;
271 byte_run = (signed char)(buf[stream_ptr++]);
273 byte_run = -byte_run;
274 palette_idx1 = buf[stream_ptr++];
275 palette_idx2 = buf[stream_ptr++];
276 CHECK_PIXEL_PTR(byte_run * 2);
277 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
278 pixels[pixel_ptr++] = palette_idx1;
279 pixels[pixel_ptr++] = palette_idx2;
282 CHECK_PIXEL_PTR(byte_run * 2);
283 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
284 palette_idx1 = buf[stream_ptr++];
285 pixels[pixel_ptr++] = palette_idx1;
290 y_ptr += s->frame.linesize[0];
296 /* line compressed */
297 starting_line = AV_RL16(&buf[stream_ptr]);
300 y_ptr += starting_line * s->frame.linesize[0];
302 compressed_lines = AV_RL16(&buf[stream_ptr]);
304 while (compressed_lines > 0) {
307 pixel_countdown = s->avctx->width;
308 line_packets = buf[stream_ptr++];
309 if (line_packets > 0) {
310 for (i = 0; i < line_packets; i++) {
311 /* account for the skip bytes */
312 pixel_skip = buf[stream_ptr++];
313 pixel_ptr += pixel_skip;
314 pixel_countdown -= pixel_skip;
315 byte_run = (signed char)(buf[stream_ptr++]);
317 CHECK_PIXEL_PTR(byte_run);
318 for (j = 0; j < byte_run; j++, pixel_countdown--) {
319 palette_idx1 = buf[stream_ptr++];
320 pixels[pixel_ptr++] = palette_idx1;
322 } else if (byte_run < 0) {
323 byte_run = -byte_run;
324 palette_idx1 = buf[stream_ptr++];
325 CHECK_PIXEL_PTR(byte_run);
326 for (j = 0; j < byte_run; j++, pixel_countdown--) {
327 pixels[pixel_ptr++] = palette_idx1;
333 y_ptr += s->frame.linesize[0];
339 /* set the whole frame to color 0 (which is usually black) */
341 s->frame.linesize[0] * s->avctx->height);
345 /* Byte run compression: This chunk type only occurs in the first
346 * FLI frame and it will update the entire frame. */
348 for (lines = 0; lines < s->avctx->height; lines++) {
350 /* disregard the line packets; instead, iterate through all
353 pixel_countdown = s->avctx->width;
354 while (pixel_countdown > 0) {
355 byte_run = (signed char)(buf[stream_ptr++]);
357 palette_idx1 = buf[stream_ptr++];
358 CHECK_PIXEL_PTR(byte_run);
359 for (j = 0; j < byte_run; j++) {
360 pixels[pixel_ptr++] = palette_idx1;
362 if (pixel_countdown < 0)
363 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
364 pixel_countdown, lines);
366 } else { /* copy bytes if byte_run < 0 */
367 byte_run = -byte_run;
368 CHECK_PIXEL_PTR(byte_run);
369 for (j = 0; j < byte_run; j++) {
370 palette_idx1 = buf[stream_ptr++];
371 pixels[pixel_ptr++] = palette_idx1;
373 if (pixel_countdown < 0)
374 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
375 pixel_countdown, lines);
380 y_ptr += s->frame.linesize[0];
385 /* copy the chunk (uncompressed frame) */
386 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
387 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
388 "bigger than image, skipping chunk\n", chunk_size - 6);
389 stream_ptr += chunk_size - 6;
391 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
392 y_ptr += s->frame.linesize[0]) {
393 memcpy(&pixels[y_ptr], &buf[stream_ptr],
395 stream_ptr += s->avctx->width;
401 /* some sort of a thumbnail? disregard this chunk... */
402 stream_ptr += 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 ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
417 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
418 "and final chunk ptr = %d\n", buf_size, stream_ptr);
420 /* make the palette available on the way out */
421 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
422 if (s->new_palette) {
423 s->frame.palette_has_changed = 1;
427 *data_size=sizeof(AVFrame);
428 *(AVFrame*)data = s->frame;
433 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
434 void *data, int *data_size,
435 const uint8_t *buf, int buf_size)
437 /* Note, the only difference between the 15Bpp and 16Bpp */
438 /* Format is the pixel format, the packets are processed the same. */
439 FlicDecodeContext *s = avctx->priv_data;
443 unsigned char palette_idx1;
445 unsigned int frame_size;
448 unsigned int chunk_size;
454 int compressed_lines;
455 signed short line_packets;
460 unsigned char *pixels;
462 unsigned int pixel_limit;
464 s->frame.reference = 1;
465 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
466 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
467 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
471 pixels = s->frame.data[0];
472 pixel_limit = s->avctx->height * s->frame.linesize[0];
474 frame_size = AV_RL32(&buf[stream_ptr]);
475 stream_ptr += 6; /* skip the magic number */
476 num_chunks = AV_RL16(&buf[stream_ptr]);
477 stream_ptr += 10; /* skip padding */
481 /* iterate through the chunks */
482 while ((frame_size > 0) && (num_chunks > 0)) {
483 chunk_size = AV_RL32(&buf[stream_ptr]);
485 chunk_type = AV_RL16(&buf[stream_ptr]);
488 switch (chunk_type) {
491 /* For some reason, it seems that non-palettized flics do
492 * include one of these chunks in their first frame.
493 * Why I do not know, it seems rather extraneous. */
494 /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
495 stream_ptr = stream_ptr + chunk_size - 6;
501 compressed_lines = AV_RL16(&buf[stream_ptr]);
503 while (compressed_lines > 0) {
504 line_packets = AV_RL16(&buf[stream_ptr]);
506 if (line_packets < 0) {
507 line_packets = -line_packets;
508 y_ptr += line_packets * s->frame.linesize[0];
513 pixel_countdown = s->avctx->width;
514 for (i = 0; i < line_packets; i++) {
515 /* account for the skip bytes */
516 pixel_skip = buf[stream_ptr++];
517 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
518 pixel_countdown -= pixel_skip;
519 byte_run = (signed char)(buf[stream_ptr++]);
521 byte_run = -byte_run;
522 pixel = AV_RL16(&buf[stream_ptr]);
524 CHECK_PIXEL_PTR(2 * byte_run);
525 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
526 *((signed short*)(&pixels[pixel_ptr])) = pixel;
530 CHECK_PIXEL_PTR(2 * byte_run);
531 for (j = 0; j < byte_run; j++, pixel_countdown--) {
532 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
539 y_ptr += s->frame.linesize[0];
545 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
546 stream_ptr = stream_ptr + 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
562 pixel_countdown = (s->avctx->width * 2);
564 while (pixel_countdown > 0) {
565 byte_run = (signed char)(buf[stream_ptr++]);
567 palette_idx1 = buf[stream_ptr++];
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 = buf[stream_ptr++];
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 oportunity 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
614 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
616 while (pixel_countdown > 0) {
617 byte_run = (signed char)(buf[stream_ptr++]);
619 pixel = AV_RL16(&buf[stream_ptr]);
621 CHECK_PIXEL_PTR(2 * byte_run);
622 for (j = 0; j < byte_run; j++) {
623 *((signed short*)(&pixels[pixel_ptr])) = pixel;
626 if (pixel_countdown < 0)
627 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
630 } else { /* copy pixels if byte_run < 0 */
631 byte_run = -byte_run;
632 CHECK_PIXEL_PTR(2 * byte_run);
633 for (j = 0; j < byte_run; j++) {
634 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
638 if (pixel_countdown < 0)
639 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
645 y_ptr += s->frame.linesize[0];
651 /* copy the chunk (uncompressed frame) */
652 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
653 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
654 "bigger than image, skipping chunk\n", chunk_size - 6);
655 stream_ptr += chunk_size - 6;
658 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
659 y_ptr += s->frame.linesize[0]) {
661 pixel_countdown = s->avctx->width;
663 while (pixel_countdown > 0) {
664 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = AV_RL16(&buf[stream_ptr+pixel_ptr]);
668 stream_ptr += s->avctx->width*2;
674 /* some sort of a thumbnail? disregard this chunk... */
675 stream_ptr += chunk_size - 6;
679 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
683 frame_size -= chunk_size;
687 /* by the end of the chunk, the stream ptr should equal the frame
688 * size (minus 1, possibly); if it doesn't, issue a warning */
689 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
690 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
691 "and final chunk ptr = %d\n", buf_size, stream_ptr);
694 *data_size=sizeof(AVFrame);
695 *(AVFrame*)data = s->frame;
700 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
701 void *data, int *data_size,
702 const uint8_t *buf, int buf_size)
704 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
708 static int flic_decode_frame(AVCodecContext *avctx,
709 void *data, int *data_size,
712 const uint8_t *buf = avpkt->data;
713 int buf_size = avpkt->size;
714 if (avctx->pix_fmt == PIX_FMT_PAL8) {
715 return flic_decode_frame_8BPP(avctx, data, data_size,
718 else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
719 (avctx->pix_fmt == PIX_FMT_RGB565)) {
720 return flic_decode_frame_15_16BPP(avctx, data, data_size,
723 else if (avctx->pix_fmt == PIX_FMT_BGR24) {
724 return flic_decode_frame_24BPP(avctx, data, data_size,
728 /* Should not get here, ever as the pix_fmt is processed */
729 /* in flic_decode_init and the above if should deal with */
730 /* the finite set of possibilites allowable by here. */
731 /* But in case we do, just error out. */
732 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
737 static av_cold int flic_decode_end(AVCodecContext *avctx)
739 FlicDecodeContext *s = avctx->priv_data;
741 if (s->frame.data[0])
742 avctx->release_buffer(avctx, &s->frame);
747 AVCodec ff_flic_decoder = {
749 .type = AVMEDIA_TYPE_VIDEO,
751 .priv_data_size = sizeof(FlicDecodeContext),
752 .init = flic_decode_init,
753 .close = flic_decode_end,
754 .decode = flic_decode_frame,
755 .capabilities = CODEC_CAP_DR1,
756 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),