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;
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 avcodec_get_frame_defaults(&s->frame);
124 s->frame.data[0] = NULL;
130 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
131 void *data, int *data_size,
132 const uint8_t *buf, int buf_size)
134 FlicDecodeContext *s = avctx->priv_data;
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 = 3;
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 if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
177 return AVERROR_INVALIDDATA;
178 frame_size = AV_RL32(&buf[stream_ptr]);
179 if (frame_size > buf_size)
180 frame_size = buf_size;
181 stream_ptr += 6; /* skip the magic number */
182 num_chunks = AV_RL16(&buf[stream_ptr]);
183 stream_ptr += 10; /* skip padding */
187 /* iterate through the chunks */
188 while ((frame_size >= 6) && (num_chunks > 0)) {
189 int stream_ptr_after_chunk;
190 chunk_size = AV_RL32(&buf[stream_ptr]);
191 if (chunk_size > frame_size) {
192 av_log(avctx, AV_LOG_WARNING,
193 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
194 chunk_size = frame_size;
196 stream_ptr_after_chunk = stream_ptr + chunk_size;
199 chunk_type = AV_RL16(&buf[stream_ptr]);
202 switch (chunk_type) {
205 /* check special case: If this file is from the Magic Carpet
206 * game and uses 6-bit colors even though it reports 256-color
207 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
209 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
213 /* set up the palette */
214 color_packets = AV_RL16(&buf[stream_ptr]);
217 for (i = 0; i < color_packets; i++) {
218 /* first byte is how many colors to skip */
219 palette_ptr += buf[stream_ptr++];
221 /* next byte indicates how many entries to change */
222 color_changes = buf[stream_ptr++];
224 /* if there are 0 color changes, there are actually 256 */
225 if (color_changes == 0)
228 if (stream_ptr + color_changes * 3 > stream_ptr_after_chunk)
231 for (j = 0; j < color_changes; j++) {
234 /* wrap around, for good measure */
235 if ((unsigned)palette_ptr >= 256)
238 r = buf[stream_ptr++] << color_shift;
239 g = buf[stream_ptr++] << color_shift;
240 b = buf[stream_ptr++] << color_shift;
241 entry = 0xFF << 24 | r << 16 | g << 8 | b;
242 if (color_shift == 2)
243 entry |= entry >> 6 & 0x30303;
244 if (s->palette[palette_ptr] != entry)
246 s->palette[palette_ptr++] = entry;
253 compressed_lines = AV_RL16(&buf[stream_ptr]);
255 while (compressed_lines > 0) {
256 if (stream_ptr + 2 > stream_ptr_after_chunk)
258 line_packets = AV_RL16(&buf[stream_ptr]);
260 if ((line_packets & 0xC000) == 0xC000) {
262 line_packets = -line_packets;
263 y_ptr += line_packets * s->frame.linesize[0];
264 } else if ((line_packets & 0xC000) == 0x4000) {
265 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
266 } else if ((line_packets & 0xC000) == 0x8000) {
267 // "last byte" opcode
268 pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
270 pixels[pixel_ptr] = line_packets & 0xff;
275 pixel_countdown = s->avctx->width;
276 for (i = 0; i < line_packets; i++) {
277 if (stream_ptr + 2 > stream_ptr_after_chunk)
279 /* account for the skip bytes */
280 pixel_skip = buf[stream_ptr++];
281 pixel_ptr += pixel_skip;
282 pixel_countdown -= pixel_skip;
283 byte_run = (signed char)(buf[stream_ptr++]);
285 byte_run = -byte_run;
286 palette_idx1 = buf[stream_ptr++];
287 palette_idx2 = buf[stream_ptr++];
288 CHECK_PIXEL_PTR(byte_run * 2);
289 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
290 pixels[pixel_ptr++] = palette_idx1;
291 pixels[pixel_ptr++] = palette_idx2;
294 CHECK_PIXEL_PTR(byte_run * 2);
295 if (stream_ptr + byte_run * 2 > stream_ptr_after_chunk)
297 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
298 palette_idx1 = buf[stream_ptr++];
299 pixels[pixel_ptr++] = palette_idx1;
304 y_ptr += s->frame.linesize[0];
310 /* line compressed */
311 starting_line = AV_RL16(&buf[stream_ptr]);
314 y_ptr += starting_line * s->frame.linesize[0];
316 compressed_lines = AV_RL16(&buf[stream_ptr]);
318 while (compressed_lines > 0) {
321 pixel_countdown = s->avctx->width;
322 line_packets = buf[stream_ptr++];
323 if (stream_ptr + 2 * line_packets > stream_ptr_after_chunk)
325 if (line_packets > 0) {
326 for (i = 0; i < line_packets; i++) {
327 /* account for the skip bytes */
328 pixel_skip = buf[stream_ptr++];
329 pixel_ptr += pixel_skip;
330 pixel_countdown -= pixel_skip;
331 byte_run = (signed char)(buf[stream_ptr++]);
333 CHECK_PIXEL_PTR(byte_run);
334 if (stream_ptr + byte_run > stream_ptr_after_chunk)
336 for (j = 0; j < byte_run; j++, pixel_countdown--) {
337 palette_idx1 = buf[stream_ptr++];
338 pixels[pixel_ptr++] = palette_idx1;
340 } else if (byte_run < 0) {
341 byte_run = -byte_run;
342 palette_idx1 = buf[stream_ptr++];
343 CHECK_PIXEL_PTR(byte_run);
344 for (j = 0; j < byte_run; j++, pixel_countdown--) {
345 pixels[pixel_ptr++] = palette_idx1;
351 y_ptr += s->frame.linesize[0];
357 /* set the whole frame to color 0 (which is usually black) */
359 s->frame.linesize[0] * s->avctx->height);
363 /* Byte run compression: This chunk type only occurs in the first
364 * FLI frame and it will update the entire frame. */
366 for (lines = 0; lines < s->avctx->height; lines++) {
368 /* disregard the line packets; instead, iterate through all
371 pixel_countdown = s->avctx->width;
372 while (pixel_countdown > 0) {
373 if (stream_ptr + 1 > stream_ptr_after_chunk)
375 byte_run = (signed char)(buf[stream_ptr++]);
377 palette_idx1 = buf[stream_ptr++];
378 CHECK_PIXEL_PTR(byte_run);
379 for (j = 0; j < byte_run; j++) {
380 pixels[pixel_ptr++] = palette_idx1;
382 if (pixel_countdown < 0)
383 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
384 pixel_countdown, lines);
386 } else { /* copy bytes if byte_run < 0 */
387 byte_run = -byte_run;
388 CHECK_PIXEL_PTR(byte_run);
389 if (stream_ptr + byte_run > stream_ptr_after_chunk)
391 for (j = 0; j < byte_run; j++) {
392 palette_idx1 = buf[stream_ptr++];
393 pixels[pixel_ptr++] = palette_idx1;
395 if (pixel_countdown < 0)
396 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
397 pixel_countdown, lines);
402 y_ptr += s->frame.linesize[0];
407 /* copy the chunk (uncompressed frame) */
408 if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
409 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
410 "has incorrect size, skipping chunk\n", chunk_size - 6);
412 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
413 y_ptr += s->frame.linesize[0]) {
414 memcpy(&pixels[y_ptr], &buf[stream_ptr],
416 stream_ptr += s->avctx->width;
422 /* some sort of a thumbnail? disregard this chunk... */
426 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
430 stream_ptr = stream_ptr_after_chunk;
432 frame_size -= chunk_size;
436 /* by the end of the chunk, the stream ptr should equal the frame
437 * size (minus 1, possibly); if it doesn't, issue a warning */
438 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
439 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
440 "and final chunk ptr = %d\n", buf_size, stream_ptr);
442 /* make the palette available on the way out */
443 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
444 if (s->new_palette) {
445 s->frame.palette_has_changed = 1;
449 *data_size=sizeof(AVFrame);
450 *(AVFrame*)data = s->frame;
455 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
456 void *data, int *data_size,
457 const uint8_t *buf, int buf_size)
459 /* Note, the only difference between the 15Bpp and 16Bpp */
460 /* Format is the pixel format, the packets are processed the same. */
461 FlicDecodeContext *s = avctx->priv_data;
465 unsigned char palette_idx1;
467 unsigned int frame_size;
470 unsigned int chunk_size;
476 int compressed_lines;
477 signed short line_packets;
482 unsigned char *pixels;
484 unsigned int pixel_limit;
486 s->frame.reference = 3;
487 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
488 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
489 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
493 pixels = s->frame.data[0];
494 pixel_limit = s->avctx->height * s->frame.linesize[0];
496 frame_size = AV_RL32(&buf[stream_ptr]);
497 stream_ptr += 6; /* skip the magic number */
498 num_chunks = AV_RL16(&buf[stream_ptr]);
499 stream_ptr += 10; /* skip padding */
503 /* iterate through the chunks */
504 while ((frame_size > 0) && (num_chunks > 0)) {
505 chunk_size = AV_RL32(&buf[stream_ptr]);
507 chunk_type = AV_RL16(&buf[stream_ptr]);
510 switch (chunk_type) {
513 /* For some reason, it seems that non-palettized flics do
514 * include one of these chunks in their first frame.
515 * Why I do not know, it seems rather extraneous. */
516 /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
517 stream_ptr = stream_ptr + chunk_size - 6;
523 compressed_lines = AV_RL16(&buf[stream_ptr]);
525 while (compressed_lines > 0) {
526 line_packets = AV_RL16(&buf[stream_ptr]);
528 if (line_packets < 0) {
529 line_packets = -line_packets;
530 y_ptr += line_packets * s->frame.linesize[0];
535 pixel_countdown = s->avctx->width;
536 for (i = 0; i < line_packets; i++) {
537 /* account for the skip bytes */
538 pixel_skip = buf[stream_ptr++];
539 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
540 pixel_countdown -= pixel_skip;
541 byte_run = (signed char)(buf[stream_ptr++]);
543 byte_run = -byte_run;
544 pixel = AV_RL16(&buf[stream_ptr]);
546 CHECK_PIXEL_PTR(2 * byte_run);
547 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
548 *((signed short*)(&pixels[pixel_ptr])) = pixel;
552 CHECK_PIXEL_PTR(2 * byte_run);
553 for (j = 0; j < byte_run; j++, pixel_countdown--) {
554 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
561 y_ptr += s->frame.linesize[0];
567 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
568 stream_ptr = stream_ptr + chunk_size - 6;
572 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
573 memset(pixels, 0x0000,
574 s->frame.linesize[0] * s->avctx->height);
579 for (lines = 0; lines < s->avctx->height; lines++) {
581 /* disregard the line packets; instead, iterate through all
584 pixel_countdown = (s->avctx->width * 2);
586 while (pixel_countdown > 0) {
587 byte_run = (signed char)(buf[stream_ptr++]);
589 palette_idx1 = buf[stream_ptr++];
590 CHECK_PIXEL_PTR(byte_run);
591 for (j = 0; j < byte_run; j++) {
592 pixels[pixel_ptr++] = palette_idx1;
594 if (pixel_countdown < 0)
595 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
596 pixel_countdown, lines);
598 } else { /* copy bytes if byte_run < 0 */
599 byte_run = -byte_run;
600 CHECK_PIXEL_PTR(byte_run);
601 for (j = 0; j < byte_run; j++) {
602 palette_idx1 = buf[stream_ptr++];
603 pixels[pixel_ptr++] = palette_idx1;
605 if (pixel_countdown < 0)
606 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
607 pixel_countdown, lines);
612 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
613 * This does not give us any good oportunity to perform word endian conversion
614 * during decompression. So if it is required (i.e., this is not a LE target, we do
615 * a second pass over the line here, swapping the bytes.
619 pixel_countdown = s->avctx->width;
620 while (pixel_countdown > 0) {
621 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
625 y_ptr += s->frame.linesize[0];
631 for (lines = 0; lines < s->avctx->height; lines++) {
633 /* disregard the line packets; instead, iterate through all
636 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
638 while (pixel_countdown > 0) {
639 byte_run = (signed char)(buf[stream_ptr++]);
641 pixel = AV_RL16(&buf[stream_ptr]);
643 CHECK_PIXEL_PTR(2 * byte_run);
644 for (j = 0; j < byte_run; j++) {
645 *((signed short*)(&pixels[pixel_ptr])) = pixel;
648 if (pixel_countdown < 0)
649 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
652 } else { /* copy pixels if byte_run < 0 */
653 byte_run = -byte_run;
654 CHECK_PIXEL_PTR(2 * byte_run);
655 for (j = 0; j < byte_run; j++) {
656 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
660 if (pixel_countdown < 0)
661 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
667 y_ptr += s->frame.linesize[0];
673 /* copy the chunk (uncompressed frame) */
674 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
675 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
676 "bigger than image, skipping chunk\n", chunk_size - 6);
677 stream_ptr += chunk_size - 6;
680 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
681 y_ptr += s->frame.linesize[0]) {
683 pixel_countdown = s->avctx->width;
685 while (pixel_countdown > 0) {
686 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = AV_RL16(&buf[stream_ptr+pixel_ptr]);
690 stream_ptr += s->avctx->width*2;
696 /* some sort of a thumbnail? disregard this chunk... */
697 stream_ptr += chunk_size - 6;
701 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
705 frame_size -= chunk_size;
709 /* by the end of the chunk, the stream ptr should equal the frame
710 * size (minus 1, possibly); if it doesn't, issue a warning */
711 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
712 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
713 "and final chunk ptr = %d\n", buf_size, stream_ptr);
716 *data_size=sizeof(AVFrame);
717 *(AVFrame*)data = s->frame;
722 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
723 void *data, int *data_size,
724 const uint8_t *buf, int buf_size)
726 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
730 static int flic_decode_frame(AVCodecContext *avctx,
731 void *data, int *data_size,
734 const uint8_t *buf = avpkt->data;
735 int buf_size = avpkt->size;
736 if (avctx->pix_fmt == PIX_FMT_PAL8) {
737 return flic_decode_frame_8BPP(avctx, data, data_size,
740 else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
741 (avctx->pix_fmt == PIX_FMT_RGB565)) {
742 return flic_decode_frame_15_16BPP(avctx, data, data_size,
745 else if (avctx->pix_fmt == PIX_FMT_BGR24) {
746 return flic_decode_frame_24BPP(avctx, data, data_size,
750 /* Should not get here, ever as the pix_fmt is processed */
751 /* in flic_decode_init and the above if should deal with */
752 /* the finite set of possibilites allowable by here. */
753 /* But in case we do, just error out. */
754 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
759 static av_cold int flic_decode_end(AVCodecContext *avctx)
761 FlicDecodeContext *s = avctx->priv_data;
763 if (s->frame.data[0])
764 avctx->release_buffer(avctx, &s->frame);
769 AVCodec ff_flic_decoder = {
771 .type = AVMEDIA_TYPE_VIDEO,
773 .priv_data_size = sizeof(FlicDecodeContext),
774 .init = flic_decode_init,
775 .close = flic_decode_end,
776 .decode = flic_decode_frame,
777 .capabilities = CODEC_CAP_DR1,
778 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),