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.
46 #define FLI_256_COLOR 4
54 #define FLI_DTA_BRUN 25
55 #define FLI_DTA_COPY 26
58 #define FLI_TYPE_CODE (0xAF11)
59 #define FLC_FLX_TYPE_CODE (0xAF12)
60 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
61 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
63 #define CHECK_PIXEL_PTR(n) \
64 if (pixel_ptr + n > pixel_limit) { \
65 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
66 pixel_ptr + n, pixel_limit); \
70 typedef struct FlicDecodeContext {
71 AVCodecContext *avctx;
74 unsigned int palette[256];
76 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
79 static int flic_decode_init(AVCodecContext *avctx)
81 FlicDecodeContext *s = avctx->priv_data;
82 unsigned char *fli_header = (unsigned char *)avctx->extradata;
87 s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
88 depth = AV_RL16(&fli_header[12]);
91 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
94 if (s->avctx->extradata_size == 12) {
95 /* special case for magic carpet FLIs */
96 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
97 } else if (s->avctx->extradata_size != 128) {
98 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
102 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
103 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
107 case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
108 case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
109 case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
110 case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
111 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
115 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
119 s->frame.data[0] = NULL;
125 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
126 void *data, int *data_size,
127 uint8_t *buf, int buf_size)
129 FlicDecodeContext *s = avctx->priv_data;
132 int stream_ptr_after_color_chunk;
135 unsigned char palette_idx1;
136 unsigned char palette_idx2;
138 unsigned int frame_size;
141 unsigned int chunk_size;
149 unsigned char r, g, b;
152 int compressed_lines;
154 signed short line_packets;
159 unsigned char *pixels;
162 s->frame.reference = 1;
163 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
164 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
165 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
169 pixels = s->frame.data[0];
170 pixel_limit = s->avctx->height * s->frame.linesize[0];
172 frame_size = AV_RL32(&buf[stream_ptr]);
173 stream_ptr += 6; /* skip the magic number */
174 num_chunks = AV_RL16(&buf[stream_ptr]);
175 stream_ptr += 10; /* skip padding */
179 /* iterate through the chunks */
180 while ((frame_size > 0) && (num_chunks > 0)) {
181 chunk_size = AV_RL32(&buf[stream_ptr]);
183 chunk_type = AV_RL16(&buf[stream_ptr]);
186 switch (chunk_type) {
189 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
191 /* check special case: If this file is from the Magic Carpet
192 * game and uses 6-bit colors even though it reports 256-color
193 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
195 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
199 /* set up the palette */
200 color_packets = AV_RL16(&buf[stream_ptr]);
203 for (i = 0; i < color_packets; i++) {
204 /* first byte is how many colors to skip */
205 palette_ptr += buf[stream_ptr++];
207 /* next byte indicates how many entries to change */
208 color_changes = buf[stream_ptr++];
210 /* if there are 0 color changes, there are actually 256 */
211 if (color_changes == 0)
214 for (j = 0; j < color_changes; j++) {
217 /* wrap around, for good measure */
218 if ((unsigned)palette_ptr >= 256)
221 r = buf[stream_ptr++] << color_shift;
222 g = buf[stream_ptr++] << color_shift;
223 b = buf[stream_ptr++] << color_shift;
224 entry = (r << 16) | (g << 8) | b;
225 if (s->palette[palette_ptr] != entry)
227 s->palette[palette_ptr++] = entry;
231 /* color chunks sometimes have weird 16-bit alignment issues;
232 * therefore, take the hardline approach and set the stream_ptr
233 * to the value calculated w.r.t. the size specified by the color
235 stream_ptr = stream_ptr_after_color_chunk;
241 compressed_lines = AV_RL16(&buf[stream_ptr]);
243 while (compressed_lines > 0) {
244 line_packets = AV_RL16(&buf[stream_ptr]);
246 if ((line_packets & 0xC000) == 0xC000) {
248 line_packets = -line_packets;
249 y_ptr += line_packets * s->frame.linesize[0];
250 } else if ((line_packets & 0xC000) == 0x4000) {
251 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
252 } else if ((line_packets & 0xC000) == 0x8000) {
253 // "last byte" opcode
254 pixels[y_ptr + s->frame.linesize[0] - 1] = line_packets & 0xff;
258 pixel_countdown = s->avctx->width;
259 for (i = 0; i < line_packets; i++) {
260 /* account for the skip bytes */
261 pixel_skip = buf[stream_ptr++];
262 pixel_ptr += pixel_skip;
263 pixel_countdown -= pixel_skip;
264 byte_run = (signed char)(buf[stream_ptr++]);
266 byte_run = -byte_run;
267 palette_idx1 = buf[stream_ptr++];
268 palette_idx2 = buf[stream_ptr++];
269 CHECK_PIXEL_PTR(byte_run);
270 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
271 pixels[pixel_ptr++] = palette_idx1;
272 pixels[pixel_ptr++] = palette_idx2;
275 CHECK_PIXEL_PTR(byte_run * 2);
276 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
277 palette_idx1 = buf[stream_ptr++];
278 pixels[pixel_ptr++] = palette_idx1;
283 y_ptr += s->frame.linesize[0];
289 /* line compressed */
290 starting_line = AV_RL16(&buf[stream_ptr]);
293 y_ptr += starting_line * s->frame.linesize[0];
295 compressed_lines = AV_RL16(&buf[stream_ptr]);
297 while (compressed_lines > 0) {
299 pixel_countdown = s->avctx->width;
300 line_packets = buf[stream_ptr++];
301 if (line_packets > 0) {
302 for (i = 0; i < line_packets; i++) {
303 /* account for the skip bytes */
304 pixel_skip = buf[stream_ptr++];
305 pixel_ptr += pixel_skip;
306 pixel_countdown -= pixel_skip;
307 byte_run = (signed char)(buf[stream_ptr++]);
309 CHECK_PIXEL_PTR(byte_run);
310 for (j = 0; j < byte_run; j++, pixel_countdown--) {
311 palette_idx1 = buf[stream_ptr++];
312 pixels[pixel_ptr++] = palette_idx1;
314 } else if (byte_run < 0) {
315 byte_run = -byte_run;
316 palette_idx1 = buf[stream_ptr++];
317 CHECK_PIXEL_PTR(byte_run);
318 for (j = 0; j < byte_run; j++, pixel_countdown--) {
319 pixels[pixel_ptr++] = palette_idx1;
325 y_ptr += s->frame.linesize[0];
331 /* set the whole frame to color 0 (which is usually black) */
333 s->frame.linesize[0] * s->avctx->height);
337 /* Byte run compression: This chunk type only occurs in the first
338 * FLI frame and it will update the entire frame. */
340 for (lines = 0; lines < s->avctx->height; lines++) {
342 /* disregard the line packets; instead, iterate through all
345 pixel_countdown = s->avctx->width;
346 while (pixel_countdown > 0) {
347 byte_run = (signed char)(buf[stream_ptr++]);
349 palette_idx1 = buf[stream_ptr++];
350 CHECK_PIXEL_PTR(byte_run);
351 for (j = 0; j < byte_run; j++) {
352 pixels[pixel_ptr++] = palette_idx1;
354 if (pixel_countdown < 0)
355 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
356 pixel_countdown, lines);
358 } else { /* copy bytes if byte_run < 0 */
359 byte_run = -byte_run;
360 CHECK_PIXEL_PTR(byte_run);
361 for (j = 0; j < byte_run; j++) {
362 palette_idx1 = buf[stream_ptr++];
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);
372 y_ptr += s->frame.linesize[0];
377 /* copy the chunk (uncompressed frame) */
378 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
379 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
380 "bigger than image, skipping chunk\n", chunk_size - 6);
381 stream_ptr += chunk_size - 6;
383 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
384 y_ptr += s->frame.linesize[0]) {
385 memcpy(&pixels[y_ptr], &buf[stream_ptr],
387 stream_ptr += s->avctx->width;
393 /* some sort of a thumbnail? disregard this chunk... */
394 stream_ptr += chunk_size - 6;
398 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
402 frame_size -= chunk_size;
406 /* by the end of the chunk, the stream ptr should equal the frame
407 * size (minus 1, possibly); if it doesn't, issue a warning */
408 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
409 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
410 "and final chunk ptr = %d\n", buf_size, stream_ptr);
412 /* make the palette available on the way out */
413 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
414 if (s->new_palette) {
415 s->frame.palette_has_changed = 1;
419 *data_size=sizeof(AVFrame);
420 *(AVFrame*)data = s->frame;
425 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
426 void *data, int *data_size,
427 uint8_t *buf, int buf_size)
429 /* Note, the only difference between the 15Bpp and 16Bpp */
430 /* Format is the pixel format, the packets are processed the same. */
431 FlicDecodeContext *s = avctx->priv_data;
435 unsigned char palette_idx1;
437 unsigned int frame_size;
440 unsigned int chunk_size;
446 int compressed_lines;
447 signed short line_packets;
452 unsigned char *pixels;
456 s->frame.reference = 1;
457 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
458 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
459 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
463 pixels = s->frame.data[0];
464 pixel_limit = s->avctx->height * s->frame.linesize[0];
466 frame_size = AV_RL32(&buf[stream_ptr]);
467 stream_ptr += 6; /* skip the magic number */
468 num_chunks = AV_RL16(&buf[stream_ptr]);
469 stream_ptr += 10; /* skip padding */
473 /* iterate through the chunks */
474 while ((frame_size > 0) && (num_chunks > 0)) {
475 chunk_size = AV_RL32(&buf[stream_ptr]);
477 chunk_type = AV_RL16(&buf[stream_ptr]);
480 switch (chunk_type) {
483 /* For some reason, it seems that non-paletised flics do include one of these */
484 /* chunks in their first frame. Why i do not know, it seems rather extraneous */
485 /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
486 stream_ptr = stream_ptr + chunk_size - 6;
492 compressed_lines = AV_RL16(&buf[stream_ptr]);
494 while (compressed_lines > 0) {
495 line_packets = AV_RL16(&buf[stream_ptr]);
497 if (line_packets < 0) {
498 line_packets = -line_packets;
499 y_ptr += line_packets * s->frame.linesize[0];
503 pixel_countdown = s->avctx->width;
504 for (i = 0; i < line_packets; i++) {
505 /* account for the skip bytes */
506 pixel_skip = buf[stream_ptr++];
507 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
508 pixel_countdown -= pixel_skip;
509 byte_run = (signed char)(buf[stream_ptr++]);
511 byte_run = -byte_run;
512 pixel = AV_RL16(&buf[stream_ptr]);
514 CHECK_PIXEL_PTR(byte_run);
515 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
516 *((signed short*)(&pixels[pixel_ptr])) = pixel;
520 CHECK_PIXEL_PTR(byte_run);
521 for (j = 0; j < byte_run; j++, pixel_countdown--) {
522 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
529 y_ptr += s->frame.linesize[0];
535 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
536 stream_ptr = stream_ptr + chunk_size - 6;
540 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
541 memset(pixels, 0x0000,
542 s->frame.linesize[0] * s->avctx->height);
547 for (lines = 0; lines < s->avctx->height; lines++) {
549 /* disregard the line packets; instead, iterate through all
552 pixel_countdown = (s->avctx->width * 2);
554 while (pixel_countdown > 0) {
555 byte_run = (signed char)(buf[stream_ptr++]);
557 palette_idx1 = buf[stream_ptr++];
558 CHECK_PIXEL_PTR(byte_run);
559 for (j = 0; j < byte_run; j++) {
560 pixels[pixel_ptr++] = palette_idx1;
562 if (pixel_countdown < 0)
563 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
564 pixel_countdown, lines);
566 } else { /* copy bytes if byte_run < 0 */
567 byte_run = -byte_run;
568 CHECK_PIXEL_PTR(byte_run);
569 for (j = 0; j < byte_run; j++) {
570 palette_idx1 = buf[stream_ptr++];
571 pixels[pixel_ptr++] = palette_idx1;
573 if (pixel_countdown < 0)
574 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
575 pixel_countdown, lines);
580 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
581 * This does not give us any good oportunity to perform word endian conversion
582 * during decompression. So if it is required (i.e., this is not a LE target, we do
583 * a second pass over the line here, swapping the bytes.
585 #ifdef WORDS_BIGENDIAN
587 pixel_countdown = s->avctx->width;
588 while (pixel_countdown > 0) {
589 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
593 y_ptr += s->frame.linesize[0];
599 for (lines = 0; lines < s->avctx->height; lines++) {
601 /* disregard the line packets; instead, iterate through all
604 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
606 while (pixel_countdown > 0) {
607 byte_run = (signed char)(buf[stream_ptr++]);
609 pixel = AV_RL16(&buf[stream_ptr]);
611 CHECK_PIXEL_PTR(byte_run);
612 for (j = 0; j < byte_run; j++) {
613 *((signed short*)(&pixels[pixel_ptr])) = pixel;
616 if (pixel_countdown < 0)
617 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
620 } else { /* copy pixels if byte_run < 0 */
621 byte_run = -byte_run;
622 CHECK_PIXEL_PTR(byte_run);
623 for (j = 0; j < byte_run; j++) {
624 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
628 if (pixel_countdown < 0)
629 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
635 y_ptr += s->frame.linesize[0];
641 /* copy the chunk (uncompressed frame) */
642 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
643 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
644 "bigger than image, skipping chunk\n", chunk_size - 6);
645 stream_ptr += chunk_size - 6;
648 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
649 y_ptr += s->frame.linesize[0]) {
651 pixel_countdown = s->avctx->width;
653 while (pixel_countdown > 0) {
654 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = AV_RL16(&buf[stream_ptr+pixel_ptr]);
658 stream_ptr += s->avctx->width*2;
664 /* some sort of a thumbnail? disregard this chunk... */
665 stream_ptr += chunk_size - 6;
669 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
673 frame_size -= chunk_size;
677 /* by the end of the chunk, the stream ptr should equal the frame
678 * size (minus 1, possibly); if it doesn't, issue a warning */
679 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
680 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
681 "and final chunk ptr = %d\n", buf_size, stream_ptr);
684 *data_size=sizeof(AVFrame);
685 *(AVFrame*)data = s->frame;
690 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
691 void *data, int *data_size,
692 uint8_t *buf, int buf_size)
694 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
698 static int flic_decode_frame(AVCodecContext *avctx,
699 void *data, int *data_size,
700 uint8_t *buf, int buf_size)
702 if (avctx->pix_fmt == PIX_FMT_PAL8) {
703 return flic_decode_frame_8BPP(avctx, data, data_size,
706 else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
707 (avctx->pix_fmt == PIX_FMT_RGB565)) {
708 return flic_decode_frame_15_16BPP(avctx, data, data_size,
711 else if (avctx->pix_fmt == PIX_FMT_BGR24) {
712 return flic_decode_frame_24BPP(avctx, data, data_size,
716 /* Should not get here, ever as the pix_fmt is processed */
717 /* in flic_decode_init and the above if should deal with */
718 /* the finite set of possibilites allowable by here. */
719 /* But in case we do, just error out. */
720 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
725 static int flic_decode_end(AVCodecContext *avctx)
727 FlicDecodeContext *s = avctx->priv_data;
729 if (s->frame.data[0])
730 avctx->release_buffer(avctx, &s->frame);
735 AVCodec flic_decoder = {
739 sizeof(FlicDecodeContext),