1 /*
2 * FLI/FLC Animation Video Decoder
3 * Copyright (C) 2003, 2004 the ffmpeg project
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
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
27 * variations, visit:
28 * http://www.compuphase.com/flic.htm
29 *
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.
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
47
48 #define FLI_256_COLOR 4
56 #define FLI_DTA_BRUN 25
57 #define FLI_DTA_COPY 26
59
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)
64
65 #define CHECK_PIXEL_PTR(n) \
66 if (pixel_ptr + n > pixel_limit) { \
67 av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
68 pixel_ptr + n, pixel_limit); \
69 return AVERROR_INVALIDDATA; \
70 } \
71
75
78 int fli_type;
/* either 0xAF11 or 0xAF12, affects palette resolution */
80
82 {
84 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
86
95 }
96
98
100 /* special case for magic carpet FLIs */
105 int i;
106
107 for (i = 0; i < 256; i++) {
109 ptr += 4;
110 }
112 /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
115 /* see FFmpeg ticket #1234 */
119 } else {
122 }
123
125 depth = 8;
/* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
126 }
127
129 depth = 15;
/* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
130 }
131
139 default :
142 }
143
147
149
150 return 0;
151 }
152
154 void *
data,
int *got_frame,
156 {
158
160 int pixel_ptr;
161 int palette_ptr;
162 unsigned char palette_idx1;
163 unsigned char palette_idx2;
164
166 int num_chunks;
167
168 unsigned int chunk_size;
169 int chunk_type;
170
172
173 int color_packets;
174 int color_changes;
175 int color_shift;
176 unsigned char r,
g,
b;
177
178 int lines;
179 int compressed_lines;
180 int starting_line;
181 signed short line_packets;
182 int y_ptr;
183 int byte_run;
184 int pixel_skip;
185 int pixel_countdown;
186 unsigned char *pixels;
187 unsigned int pixel_limit;
188
190
193
198 frame_size = bytestream2_get_le32(&g2);
199 if (frame_size > buf_size)
200 frame_size = buf_size;
202 num_chunks = bytestream2_get_le16(&g2);
204
205 frame_size -= 16;
206
207 /* iterate through the chunks */
208 while ((frame_size >= 6) && (num_chunks > 0) &&
210 int stream_ptr_after_chunk;
211 chunk_size = bytestream2_get_le32(&g2);
212 if (chunk_size > frame_size) {
214 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
216 }
218
219 chunk_type = bytestream2_get_le16(&g2);
220
221 switch (chunk_type) {
224 /* check special case: If this file is from the Magic Carpet
225 * game and uses 6-bit colors even though it reports 256-color
226 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
227 * initialization) */
229 color_shift = 0;
230 else
231 color_shift = 2;
232 /* set up the palette */
233 color_packets = bytestream2_get_le16(&g2);
234 palette_ptr = 0;
235 for (i = 0; i < color_packets; i++) {
236 /* first byte is how many colors to skip */
237 palette_ptr += bytestream2_get_byte(&g2);
238
239 /* next byte indicates how many entries to change */
240 color_changes = bytestream2_get_byte(&g2);
241
242 /* if there are 0 color changes, there are actually 256 */
243 if (color_changes == 0)
244 color_changes = 256;
245
247 break;
248
249 for (j = 0; j < color_changes; j++) {
250 unsigned int entry;
251
252 /* wrap around, for good measure */
253 if ((unsigned)palette_ptr >= 256)
254 palette_ptr = 0;
255
256 r = bytestream2_get_byte(&g2) << color_shift;
257 g = bytestream2_get_byte(&g2) << color_shift;
258 b = bytestream2_get_byte(&g2) << color_shift;
259 entry = 0xFF
U << 24 | r << 16 | g << 8 |
b;
260 if (color_shift == 2)
261 entry |= entry >> 6 & 0x30303;
262 if (s->
palette[palette_ptr] != entry)
264 s->
palette[palette_ptr++] = entry;
265 }
266 }
267 break;
268
270 y_ptr = 0;
271 compressed_lines = bytestream2_get_le16(&g2);
272 while (compressed_lines > 0) {
274 break;
275 line_packets = bytestream2_get_le16(&g2);
276 if ((line_packets & 0xC000) == 0xC000) {
277 // line skip opcode
278 line_packets = -line_packets;
280 } else if ((line_packets & 0xC000) == 0x4000) {
282 } else if ((line_packets & 0xC000) == 0x8000) {
283 // "last byte" opcode
286 pixels[pixel_ptr] = line_packets & 0xff;
287 } else {
288 compressed_lines--;
289 pixel_ptr = y_ptr;
292 for (i = 0; i < line_packets; i++) {
294 break;
295 /* account for the skip bytes */
296 pixel_skip = bytestream2_get_byte(&g2);
297 pixel_ptr += pixel_skip;
298 pixel_countdown -= pixel_skip;
299 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
300 if (byte_run < 0) {
301 byte_run = -byte_run;
302 palette_idx1 = bytestream2_get_byte(&g2);
303 palette_idx2 = bytestream2_get_byte(&g2);
305 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
306 pixels[pixel_ptr++] = palette_idx1;
307 pixels[pixel_ptr++] = palette_idx2;
308 }
309 } else {
312 break;
313 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
314 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
315 }
316 }
317 }
318
320 }
321 }
322 break;
323
325 /* line compressed */
326 starting_line = bytestream2_get_le16(&g2);
327 y_ptr = 0;
329
330 compressed_lines = bytestream2_get_le16(&g2);
331 while (compressed_lines > 0) {
332 pixel_ptr = y_ptr;
336 break;
337 line_packets = bytestream2_get_byte(&g2);
338 if (line_packets > 0) {
339 for (i = 0; i < line_packets; i++) {
340 /* account for the skip bytes */
342 break;
343 pixel_skip = bytestream2_get_byte(&g2);
344 pixel_ptr += pixel_skip;
345 pixel_countdown -= pixel_skip;
346 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
347 if (byte_run > 0) {
350 break;
351 for (j = 0; j < byte_run; j++, pixel_countdown--) {
352 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
353 }
354 } else if (byte_run < 0) {
355 byte_run = -byte_run;
356 palette_idx1 = bytestream2_get_byte(&g2);
358 for (j = 0; j < byte_run; j++, pixel_countdown--) {
359 pixels[pixel_ptr++] = palette_idx1;
360 }
361 }
362 }
363 }
364
366 compressed_lines--;
367 }
368 break;
369
371 /* set the whole frame to color 0 (which is usually black) */
372 memset(pixels, 0,
374 break;
375
377 /* Byte run compression: This chunk type only occurs in the first
378 * FLI frame and it will update the entire frame. */
379 y_ptr = 0;
380 for (lines = 0; lines < s->
avctx->
height; lines++) {
381 pixel_ptr = y_ptr;
382 /* disregard the line packets; instead, iterate through all
383 * pixels on a row */
386 while (pixel_countdown > 0) {
388 break;
389 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
390 if (!byte_run) {
393 }
394
395 if (byte_run > 0) {
396 palette_idx1 = bytestream2_get_byte(&g2);
398 for (j = 0; j < byte_run; j++) {
399 pixels[pixel_ptr++] = palette_idx1;
400 pixel_countdown--;
401 if (pixel_countdown < 0)
403 pixel_countdown, lines);
404 }
405 } else { /* copy bytes if byte_run < 0 */
406 byte_run = -byte_run;
409 break;
410 for (j = 0; j < byte_run; j++) {
411 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
412 pixel_countdown--;
413 if (pixel_countdown < 0)
415 pixel_countdown, lines);
416 }
417 }
418 }
419
421 }
422 break;
423
425 /* copy the chunk (uncompressed frame) */
428 "has incorrect size, skipping chunk\n", chunk_size - 6);
430 } else {
435 }
436 }
437 break;
438
440 /* some sort of a thumbnail? disregard this chunk... */
441 break;
442
443 default:
445 break;
446 }
447
450
451 frame_size -= chunk_size;
452 num_chunks--;
453 }
454
455 /* by the end of the chunk, the stream ptr should equal the frame
456 * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
459 "and final chunk ptr = %d\n", buf_size,
461
462 /* make the palette available on the way out */
467 }
468
471
472 *got_frame = 1;
473
474 return buf_size;
475 }
476
478 void *
data,
int *got_frame,
480 {
481 /* Note, the only difference between the 15Bpp and 16Bpp */
482 /* Format is the pixel format, the packets are processed the same. */
484
486 int pixel_ptr;
487 unsigned char palette_idx1;
488
490 int num_chunks;
491
492 unsigned int chunk_size;
493 int chunk_type;
494
496
497 int lines;
498 int compressed_lines;
499 signed short line_packets;
500 int y_ptr;
501 int byte_run;
502 int pixel_skip;
503 int pixel_countdown;
504 unsigned char *pixels;
506 unsigned int pixel_limit;
507
509
512
515
516 frame_size = bytestream2_get_le32(&g2);
518 num_chunks = bytestream2_get_le16(&g2);
520 if (frame_size > buf_size)
521 frame_size = buf_size;
522
523 frame_size -= 16;
524
525 /* iterate through the chunks */
526 while ((frame_size > 0) && (num_chunks > 0) &&
528 int stream_ptr_after_chunk;
529 chunk_size = bytestream2_get_le32(&g2);
530 if (chunk_size > frame_size) {
532 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
534 }
536
537 chunk_type = bytestream2_get_le16(&g2);
538
539
540 switch (chunk_type) {
543 /* For some reason, it seems that non-palettized flics do
544 * include one of these chunks in their first frame.
545 * Why I do not know, it seems rather extraneous. */
547 "Unexpected Palette chunk %d in non-palettized FLC\n",
548 chunk_type);
550 break;
551
554 y_ptr = 0;
555 compressed_lines = bytestream2_get_le16(&g2);
556 while (compressed_lines > 0) {
558 break;
559 line_packets = bytestream2_get_le16(&g2);
560 if (line_packets < 0) {
561 line_packets = -line_packets;
563 } else {
564 compressed_lines--;
565 pixel_ptr = y_ptr;
568 for (i = 0; i < line_packets; i++) {
569 /* account for the skip bytes */
571 break;
572 pixel_skip = bytestream2_get_byte(&g2);
573 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
574 pixel_countdown -= pixel_skip;
575 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
576 if (byte_run < 0) {
577 byte_run = -byte_run;
578 pixel = bytestream2_get_le16(&g2);
580 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
581 *((signed short*)(&pixels[pixel_ptr])) = pixel;
582 pixel_ptr += 2;
583 }
584 } else {
586 break;
588 for (j = 0; j < byte_run; j++, pixel_countdown--) {
589 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
590 pixel_ptr += 2;
591 }
592 }
593 }
594
596 }
597 }
598 break;
599
603 break;
604
606 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
607 memset(pixels, 0x0000,
609 break;
610
612 y_ptr = 0;
613 for (lines = 0; lines < s->
avctx->
height; lines++) {
614 pixel_ptr = y_ptr;
615 /* disregard the line packets; instead, iterate through all
616 * pixels on a row */
619
620 while (pixel_countdown > 0) {
622 break;
623 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
624 if (byte_run > 0) {
625 palette_idx1 = bytestream2_get_byte(&g2);
627 for (j = 0; j < byte_run; j++) {
628 pixels[pixel_ptr++] = palette_idx1;
629 pixel_countdown--;
630 if (pixel_countdown < 0)
632 pixel_countdown, lines);
633 }
634 } else { /* copy bytes if byte_run < 0 */
635 byte_run = -byte_run;
637 break;
639 for (j = 0; j < byte_run; j++) {
640 palette_idx1 = bytestream2_get_byte(&g2);
641 pixels[pixel_ptr++] = palette_idx1;
642 pixel_countdown--;
643 if (pixel_countdown < 0)
645 pixel_countdown, lines);
646 }
647 }
648 }
649
650 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
651 * This does not give us any good opportunity to perform word endian conversion
652 * during decompression. So if it is required (i.e., this is not a LE target, we do
653 * a second pass over the line here, swapping the bytes.
654 */
655 #if HAVE_BIGENDIAN
656 pixel_ptr = y_ptr;
658 while (pixel_countdown > 0) {
659 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
660 pixel_ptr += 2;
661 }
662 #endif
664 }
665 break;
666
668 y_ptr = 0;
669 for (lines = 0; lines < s->
avctx->
height; lines++) {
670 pixel_ptr = y_ptr;
671 /* disregard the line packets; instead, iterate through all
672 * pixels on a row */
674 pixel_countdown = s->
avctx->
width;
/* Width is in pixels, not bytes */
675
676 while (pixel_countdown > 0) {
678 break;
679 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
680 if (byte_run > 0) {
681 pixel = bytestream2_get_le16(&g2);
683 for (j = 0; j < byte_run; j++) {
684 *((signed short*)(&pixels[pixel_ptr])) = pixel;
685 pixel_ptr += 2;
686 pixel_countdown--;
687 if (pixel_countdown < 0)
689 pixel_countdown);
690 }
691 } else { /* copy pixels if byte_run < 0 */
692 byte_run = -byte_run;
694 break;
696 for (j = 0; j < byte_run; j++) {
697 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
698 pixel_ptr += 2;
699 pixel_countdown--;
700 if (pixel_countdown < 0)
702 pixel_countdown);
703 }
704 }
705 }
706
708 }
709 break;
710
713 /* copy the chunk (uncompressed frame) */
716 "bigger than image, skipping chunk\n", chunk_size - 6);
718 } else {
719
722
724 pixel_ptr = 0;
725 while (pixel_countdown > 0) {
726 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
727 pixel_ptr += 2;
728 pixel_countdown--;
729 }
730 }
731 }
732 break;
733
735 /* some sort of a thumbnail? disregard this chunk... */
737 break;
738
739 default:
741 break;
742 }
743
744 frame_size -= chunk_size;
745 num_chunks--;
746 }
747
748 /* by the end of the chunk, the stream ptr should equal the frame
749 * size (minus 1, possibly); if it doesn't, issue a warning */
753
756
757 *got_frame = 1;
758
759 return buf_size;
760 }
761
763 void *
data,
int *got_frame,
765 {
768 }
769
771 void *
data,
int *got_frame,
773 {
775 int buf_size = avpkt->
size;
778 buf, buf_size);
779 }
783 buf, buf_size);
784 }
787 buf, buf_size);
788 }
789
790 /* Should not get here, ever as the pix_fmt is processed */
791 /* in flic_decode_init and the above if should deal with */
792 /* the finite set of possibilites allowable by here. */
793 /* But in case we do, just error out. */
794 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");
796 }
797
798
800 {
802
804
805 return 0;
806 }
807
818 };