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/BGR24. To use this decoder, be
31 * sure that your demuxer sends the FLI file header to the decoder via
32 * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
33 * large. The only exception is for FLI files from the game "Magic Carpet",
34 * in which the header is only 12 bytes.
41 #include "libavutil/intreadwrite.h"
43 #include "bytestream.h"
47 #define FLI_256_COLOR 4
55 #define FLI_DTA_BRUN 25
56 #define FLI_DTA_COPY 26
59 #define FLI_TYPE_CODE (0xAF11)
60 #define FLC_FLX_TYPE_CODE (0xAF12)
61 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
62 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
64 #define CHECK_PIXEL_PTR(n) \
65 if (pixel_ptr + n > pixel_limit) { \
66 av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
67 pixel_ptr + n, pixel_limit); \
68 return AVERROR_INVALIDDATA; \
71 typedef struct FlicDecodeContext {
72 AVCodecContext *avctx;
75 unsigned int palette[256];
77 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
80 static av_cold int flic_decode_init(AVCodecContext *avctx)
82 FlicDecodeContext *s = avctx->priv_data;
83 unsigned char *fli_header = (unsigned char *)avctx->extradata;
86 if (avctx->extradata_size != 0 &&
87 avctx->extradata_size != 12 &&
88 avctx->extradata_size != 128 &&
89 avctx->extradata_size != 256 &&
90 avctx->extradata_size != 904 &&
91 avctx->extradata_size != 1024) {
92 av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
93 return AVERROR_INVALIDDATA;
98 if (s->avctx->extradata_size == 12) {
99 /* special case for magic carpet FLIs */
100 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
102 } else if (avctx->extradata_size == 1024) {
103 uint8_t *ptr = avctx->extradata;
106 for (i = 0; i < 256; i++) {
107 s->palette[i] = AV_RL32(ptr);
111 /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
112 } else if (avctx->extradata_size == 0 ||
113 avctx->extradata_size == 256 ||
114 /* see FFmpeg ticket #1234 */
115 avctx->extradata_size == 904) {
116 s->fli_type = FLI_TYPE_CODE;
119 s->fli_type = AV_RL16(&fli_header[4]);
120 depth = AV_RL16(&fli_header[12]);
124 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
127 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
128 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
132 case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
133 case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
134 case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
135 case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
137 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
138 return AVERROR_INVALIDDATA;
141 s->frame = av_frame_alloc();
143 return AVERROR(ENOMEM);
150 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
151 void *data, int *got_frame,
152 const uint8_t *buf, int buf_size)
154 FlicDecodeContext *s = avctx->priv_data;
159 unsigned char palette_idx1;
160 unsigned char palette_idx2;
162 unsigned int frame_size;
165 unsigned int chunk_size;
173 unsigned char r, g, b;
176 int compressed_lines;
178 signed short line_packets;
183 unsigned char *pixels;
184 unsigned int pixel_limit;
186 bytestream2_init(&g2, buf, buf_size);
188 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
191 pixels = s->frame->data[0];
192 pixel_limit = s->avctx->height * s->frame->linesize[0];
193 if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
194 return AVERROR_INVALIDDATA;
195 frame_size = bytestream2_get_le32(&g2);
196 if (frame_size > buf_size)
197 frame_size = buf_size;
198 bytestream2_skip(&g2, 2); /* skip the magic number */
199 num_chunks = bytestream2_get_le16(&g2);
200 bytestream2_skip(&g2, 8); /* skip padding */
204 /* iterate through the chunks */
205 while ((frame_size >= 6) && (num_chunks > 0) &&
206 bytestream2_get_bytes_left(&g2) >= 4) {
207 int stream_ptr_after_chunk;
208 chunk_size = bytestream2_get_le32(&g2);
209 if (chunk_size > frame_size) {
210 av_log(avctx, AV_LOG_WARNING,
211 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
212 chunk_size = frame_size;
214 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
216 chunk_type = bytestream2_get_le16(&g2);
218 switch (chunk_type) {
221 /* check special case: If this file is from the Magic Carpet
222 * game and uses 6-bit colors even though it reports 256-color
223 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
225 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
229 /* set up the palette */
230 color_packets = bytestream2_get_le16(&g2);
232 for (i = 0; i < color_packets; i++) {
233 /* first byte is how many colors to skip */
234 palette_ptr += bytestream2_get_byte(&g2);
236 /* next byte indicates how many entries to change */
237 color_changes = bytestream2_get_byte(&g2);
239 /* if there are 0 color changes, there are actually 256 */
240 if (color_changes == 0)
243 if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
246 for (j = 0; j < color_changes; j++) {
249 /* wrap around, for good measure */
250 if ((unsigned)palette_ptr >= 256)
253 r = bytestream2_get_byte(&g2) << color_shift;
254 g = bytestream2_get_byte(&g2) << color_shift;
255 b = bytestream2_get_byte(&g2) << color_shift;
256 entry = 0xFFU << 24 | r << 16 | g << 8 | b;
257 if (color_shift == 2)
258 entry |= entry >> 6 & 0x30303;
259 if (s->palette[palette_ptr] != entry)
261 s->palette[palette_ptr++] = entry;
268 compressed_lines = bytestream2_get_le16(&g2);
269 while (compressed_lines > 0) {
270 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
272 line_packets = bytestream2_get_le16(&g2);
273 if ((line_packets & 0xC000) == 0xC000) {
275 line_packets = -line_packets;
276 y_ptr += line_packets * s->frame->linesize[0];
277 } else if ((line_packets & 0xC000) == 0x4000) {
278 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
279 } else if ((line_packets & 0xC000) == 0x8000) {
280 // "last byte" opcode
281 pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
283 pixels[pixel_ptr] = line_packets & 0xff;
288 pixel_countdown = s->avctx->width;
289 for (i = 0; i < line_packets; i++) {
290 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
292 /* account for the skip bytes */
293 pixel_skip = bytestream2_get_byte(&g2);
294 pixel_ptr += pixel_skip;
295 pixel_countdown -= pixel_skip;
296 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
298 byte_run = -byte_run;
299 palette_idx1 = bytestream2_get_byte(&g2);
300 palette_idx2 = bytestream2_get_byte(&g2);
301 CHECK_PIXEL_PTR(byte_run * 2);
302 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
303 pixels[pixel_ptr++] = palette_idx1;
304 pixels[pixel_ptr++] = palette_idx2;
307 CHECK_PIXEL_PTR(byte_run * 2);
308 if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
310 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
311 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
316 y_ptr += s->frame->linesize[0];
322 /* line compressed */
323 starting_line = bytestream2_get_le16(&g2);
325 y_ptr += starting_line * s->frame->linesize[0];
327 compressed_lines = bytestream2_get_le16(&g2);
328 while (compressed_lines > 0) {
331 pixel_countdown = s->avctx->width;
332 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
334 line_packets = bytestream2_get_byte(&g2);
335 if (line_packets > 0) {
336 for (i = 0; i < line_packets; i++) {
337 /* account for the skip bytes */
338 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
340 pixel_skip = bytestream2_get_byte(&g2);
341 pixel_ptr += pixel_skip;
342 pixel_countdown -= pixel_skip;
343 byte_run = sign_extend(bytestream2_get_byte(&g2),8);
345 CHECK_PIXEL_PTR(byte_run);
346 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
348 for (j = 0; j < byte_run; j++, pixel_countdown--) {
349 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
351 } else if (byte_run < 0) {
352 byte_run = -byte_run;
353 palette_idx1 = bytestream2_get_byte(&g2);
354 CHECK_PIXEL_PTR(byte_run);
355 for (j = 0; j < byte_run; j++, pixel_countdown--) {
356 pixels[pixel_ptr++] = palette_idx1;
362 y_ptr += s->frame->linesize[0];
368 /* set the whole frame to color 0 (which is usually black) */
370 s->frame->linesize[0] * s->avctx->height);
374 /* Byte run compression: This chunk type only occurs in the first
375 * FLI frame and it will update the entire frame. */
377 for (lines = 0; lines < s->avctx->height; lines++) {
379 /* disregard the line packets; instead, iterate through all
381 bytestream2_skip(&g2, 1);
382 pixel_countdown = s->avctx->width;
383 while (pixel_countdown > 0) {
384 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
386 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
388 av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
389 return AVERROR_INVALIDDATA;
393 palette_idx1 = bytestream2_get_byte(&g2);
394 CHECK_PIXEL_PTR(byte_run);
395 for (j = 0; j < byte_run; j++) {
396 pixels[pixel_ptr++] = palette_idx1;
398 if (pixel_countdown < 0)
399 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
400 pixel_countdown, lines);
402 } else { /* copy bytes if byte_run < 0 */
403 byte_run = -byte_run;
404 CHECK_PIXEL_PTR(byte_run);
405 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
407 for (j = 0; j < byte_run; j++) {
408 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
410 if (pixel_countdown < 0)
411 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
412 pixel_countdown, lines);
417 y_ptr += s->frame->linesize[0];
422 /* copy the chunk (uncompressed frame) */
423 if (chunk_size - 6 != FFALIGN(s->avctx->width, 4) * s->avctx->height) {
424 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
425 "has incorrect size, skipping chunk\n", chunk_size - 6);
426 bytestream2_skip(&g2, chunk_size - 6);
428 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
429 y_ptr += s->frame->linesize[0]) {
430 bytestream2_get_buffer(&g2, &pixels[y_ptr],
432 if (s->avctx->width & 3)
433 bytestream2_skip(&g2, 4 - (s->avctx->width & 3));
439 /* some sort of a thumbnail? disregard this chunk... */
443 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
447 if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
448 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
450 frame_size -= chunk_size;
454 /* by the end of the chunk, the stream ptr should equal the frame
455 * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
456 if (bytestream2_get_bytes_left(&g2) > 2)
457 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
458 "and final chunk ptr = %d\n", buf_size,
459 buf_size - bytestream2_get_bytes_left(&g2));
461 /* make the palette available on the way out */
462 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
463 if (s->new_palette) {
464 s->frame->palette_has_changed = 1;
468 if ((ret = av_frame_ref(data, s->frame)) < 0)
476 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
477 void *data, int *got_frame,
478 const uint8_t *buf, int buf_size)
480 /* Note, the only difference between the 15Bpp and 16Bpp */
481 /* Format is the pixel format, the packets are processed the same. */
482 FlicDecodeContext *s = avctx->priv_data;
486 unsigned char palette_idx1;
488 unsigned int frame_size;
491 unsigned int chunk_size;
497 int compressed_lines;
498 signed short line_packets;
503 unsigned char *pixels;
505 unsigned int pixel_limit;
507 bytestream2_init(&g2, buf, buf_size);
509 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
512 pixels = s->frame->data[0];
513 pixel_limit = s->avctx->height * s->frame->linesize[0];
515 frame_size = bytestream2_get_le32(&g2);
516 bytestream2_skip(&g2, 2); /* skip the magic number */
517 num_chunks = bytestream2_get_le16(&g2);
518 bytestream2_skip(&g2, 8); /* skip padding */
519 if (frame_size > buf_size)
520 frame_size = buf_size;
524 /* iterate through the chunks */
525 while ((frame_size > 0) && (num_chunks > 0) &&
526 bytestream2_get_bytes_left(&g2) >= 4) {
527 int stream_ptr_after_chunk;
528 chunk_size = bytestream2_get_le32(&g2);
529 if (chunk_size > frame_size) {
530 av_log(avctx, AV_LOG_WARNING,
531 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
532 chunk_size = frame_size;
534 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
536 chunk_type = bytestream2_get_le16(&g2);
539 switch (chunk_type) {
542 /* For some reason, it seems that non-palettized flics do
543 * include one of these chunks in their first frame.
544 * Why I do not know, it seems rather extraneous. */
546 "Unexpected Palette chunk %d in non-palettized FLC\n",
548 bytestream2_skip(&g2, chunk_size - 6);
554 compressed_lines = bytestream2_get_le16(&g2);
555 while (compressed_lines > 0) {
556 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
558 line_packets = bytestream2_get_le16(&g2);
559 if (line_packets < 0) {
560 line_packets = -line_packets;
561 y_ptr += line_packets * s->frame->linesize[0];
566 pixel_countdown = s->avctx->width;
567 for (i = 0; i < line_packets; i++) {
568 /* account for the skip bytes */
569 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
571 pixel_skip = bytestream2_get_byte(&g2);
572 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
573 pixel_countdown -= pixel_skip;
574 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
576 byte_run = -byte_run;
577 pixel = bytestream2_get_le16(&g2);
578 CHECK_PIXEL_PTR(2 * byte_run);
579 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
580 *((signed short*)(&pixels[pixel_ptr])) = pixel;
584 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
586 CHECK_PIXEL_PTR(2 * byte_run);
587 for (j = 0; j < byte_run; j++, pixel_countdown--) {
588 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
594 y_ptr += s->frame->linesize[0];
600 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
601 bytestream2_skip(&g2, chunk_size - 6);
605 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
606 memset(pixels, 0x0000,
607 s->frame->linesize[0] * s->avctx->height);
612 for (lines = 0; lines < s->avctx->height; lines++) {
614 /* disregard the line packets; instead, iterate through all
616 bytestream2_skip(&g2, 1);
617 pixel_countdown = (s->avctx->width * 2);
619 while (pixel_countdown > 0) {
620 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
622 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
624 palette_idx1 = bytestream2_get_byte(&g2);
625 CHECK_PIXEL_PTR(byte_run);
626 for (j = 0; j < byte_run; j++) {
627 pixels[pixel_ptr++] = palette_idx1;
629 if (pixel_countdown < 0)
630 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
631 pixel_countdown, lines);
633 } else { /* copy bytes if byte_run < 0 */
634 byte_run = -byte_run;
635 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
637 CHECK_PIXEL_PTR(byte_run);
638 for (j = 0; j < byte_run; j++) {
639 palette_idx1 = bytestream2_get_byte(&g2);
640 pixels[pixel_ptr++] = palette_idx1;
642 if (pixel_countdown < 0)
643 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
644 pixel_countdown, lines);
649 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
650 * This does not give us any good opportunity to perform word endian conversion
651 * during decompression. So if it is required (i.e., this is not a LE target, we do
652 * a second pass over the line here, swapping the bytes.
656 pixel_countdown = s->avctx->width;
657 while (pixel_countdown > 0) {
658 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
662 y_ptr += s->frame->linesize[0];
668 for (lines = 0; lines < s->avctx->height; lines++) {
670 /* disregard the line packets; instead, iterate through all
672 bytestream2_skip(&g2, 1);
673 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
675 while (pixel_countdown > 0) {
676 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
678 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
680 pixel = bytestream2_get_le16(&g2);
681 CHECK_PIXEL_PTR(2 * byte_run);
682 for (j = 0; j < byte_run; j++) {
683 *((signed short*)(&pixels[pixel_ptr])) = pixel;
686 if (pixel_countdown < 0)
687 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
690 } else { /* copy pixels if byte_run < 0 */
691 byte_run = -byte_run;
692 if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
694 CHECK_PIXEL_PTR(2 * byte_run);
695 for (j = 0; j < byte_run; j++) {
696 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
699 if (pixel_countdown < 0)
700 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
706 y_ptr += s->frame->linesize[0];
712 /* copy the chunk (uncompressed frame) */
713 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
714 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
715 "bigger than image, skipping chunk\n", chunk_size - 6);
716 bytestream2_skip(&g2, chunk_size - 6);
719 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
720 y_ptr += s->frame->linesize[0]) {
722 pixel_countdown = s->avctx->width;
724 while (pixel_countdown > 0) {
725 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
729 if (s->avctx->width & 1)
730 bytestream2_skip(&g2, 2);
736 /* some sort of a thumbnail? disregard this chunk... */
737 bytestream2_skip(&g2, chunk_size - 6);
741 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
745 frame_size -= chunk_size;
749 /* by the end of the chunk, the stream ptr should equal the frame
750 * size (minus 1, possibly); if it doesn't, issue a warning */
751 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
752 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
753 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
755 if ((ret = av_frame_ref(data, s->frame)) < 0)
763 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
764 void *data, int *got_frame,
765 const uint8_t *buf, int buf_size)
767 FlicDecodeContext *s = avctx->priv_data;
771 unsigned char palette_idx1;
773 unsigned int frame_size;
776 unsigned int chunk_size;
782 int compressed_lines;
783 signed short line_packets;
788 unsigned char *pixels;
790 unsigned int pixel_limit;
792 bytestream2_init(&g2, buf, buf_size);
794 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
797 pixels = s->frame->data[0];
798 pixel_limit = s->avctx->height * s->frame->linesize[0];
800 frame_size = bytestream2_get_le32(&g2);
801 bytestream2_skip(&g2, 2); /* skip the magic number */
802 num_chunks = bytestream2_get_le16(&g2);
803 bytestream2_skip(&g2, 8); /* skip padding */
804 if (frame_size > buf_size)
805 frame_size = buf_size;
809 /* iterate through the chunks */
810 while ((frame_size > 0) && (num_chunks > 0) &&
811 bytestream2_get_bytes_left(&g2) >= 4) {
812 int stream_ptr_after_chunk;
813 chunk_size = bytestream2_get_le32(&g2);
814 if (chunk_size > frame_size) {
815 av_log(avctx, AV_LOG_WARNING,
816 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
817 chunk_size = frame_size;
819 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
821 chunk_type = bytestream2_get_le16(&g2);
824 switch (chunk_type) {
827 /* For some reason, it seems that non-palettized flics do
828 * include one of these chunks in their first frame.
829 * Why I do not know, it seems rather extraneous. */
831 "Unexpected Palette chunk %d in non-palettized FLC\n",
833 bytestream2_skip(&g2, chunk_size - 6);
839 compressed_lines = bytestream2_get_le16(&g2);
840 while (compressed_lines > 0) {
841 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
843 line_packets = bytestream2_get_le16(&g2);
844 if (line_packets < 0) {
845 line_packets = -line_packets;
846 y_ptr += line_packets * s->frame->linesize[0];
851 pixel_countdown = s->avctx->width;
852 for (i = 0; i < line_packets; i++) {
853 /* account for the skip bytes */
854 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
856 pixel_skip = bytestream2_get_byte(&g2);
857 pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
858 pixel_countdown -= pixel_skip;
859 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
861 byte_run = -byte_run;
862 pixel = bytestream2_get_le24(&g2);
863 CHECK_PIXEL_PTR(3 * byte_run);
864 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
865 AV_WL24(&pixels[pixel_ptr], pixel);
869 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
871 CHECK_PIXEL_PTR(2 * byte_run);
872 for (j = 0; j < byte_run; j++, pixel_countdown--) {
873 pixel = bytestream2_get_le24(&g2);
874 AV_WL24(&pixels[pixel_ptr], pixel);
880 y_ptr += s->frame->linesize[0];
886 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
887 bytestream2_skip(&g2, chunk_size - 6);
891 /* set the whole frame to 0x00 which is black for 24 bit mode. */
893 s->frame->linesize[0] * s->avctx->height);
898 for (lines = 0; lines < s->avctx->height; lines++) {
900 /* disregard the line packets; instead, iterate through all
902 bytestream2_skip(&g2, 1);
903 pixel_countdown = (s->avctx->width * 3);
905 while (pixel_countdown > 0) {
906 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
908 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
910 palette_idx1 = bytestream2_get_byte(&g2);
911 CHECK_PIXEL_PTR(byte_run);
912 for (j = 0; j < byte_run; j++) {
913 pixels[pixel_ptr++] = palette_idx1;
915 if (pixel_countdown < 0)
916 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
917 pixel_countdown, lines);
919 } else { /* copy bytes if byte_run < 0 */
920 byte_run = -byte_run;
921 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
923 CHECK_PIXEL_PTR(byte_run);
924 for (j = 0; j < byte_run; j++) {
925 palette_idx1 = bytestream2_get_byte(&g2);
926 pixels[pixel_ptr++] = palette_idx1;
928 if (pixel_countdown < 0)
929 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
930 pixel_countdown, lines);
935 y_ptr += s->frame->linesize[0];
941 for (lines = 0; lines < s->avctx->height; lines++) {
943 /* disregard the line packets; instead, iterate through all
945 bytestream2_skip(&g2, 1);
946 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
948 while (pixel_countdown > 0) {
949 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
951 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
953 pixel = bytestream2_get_le24(&g2);
954 CHECK_PIXEL_PTR(3 * byte_run);
955 for (j = 0; j < byte_run; j++) {
956 AV_WL24(pixels + pixel_ptr, pixel);
959 if (pixel_countdown < 0)
960 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
963 } else { /* copy pixels if byte_run < 0 */
964 byte_run = -byte_run;
965 if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
967 CHECK_PIXEL_PTR(3 * byte_run);
968 for (j = 0; j < byte_run; j++) {
969 pixel = bytestream2_get_le24(&g2);
970 AV_WL24(pixels + pixel_ptr, pixel);
973 if (pixel_countdown < 0)
974 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
980 y_ptr += s->frame->linesize[0];
986 /* copy the chunk (uncompressed frame) */
987 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
988 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
989 "bigger than image, skipping chunk\n", chunk_size - 6);
990 bytestream2_skip(&g2, chunk_size - 6);
992 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
993 y_ptr += s->frame->linesize[0]) {
995 pixel_countdown = s->avctx->width;
997 while (pixel_countdown > 0) {
998 pixel = bytestream2_get_le24(&g2);
999 AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
1003 if (s->avctx->width & 1)
1004 bytestream2_skip(&g2, 3);
1010 /* some sort of a thumbnail? disregard this chunk... */
1011 bytestream2_skip(&g2, chunk_size - 6);
1015 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
1019 frame_size -= chunk_size;
1023 /* by the end of the chunk, the stream ptr should equal the frame
1024 * size (minus 1, possibly); if it doesn't, issue a warning */
1025 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
1026 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
1027 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
1029 if ((ret = av_frame_ref(data, s->frame)) < 0)
1037 static int flic_decode_frame(AVCodecContext *avctx,
1038 void *data, int *got_frame,
1041 const uint8_t *buf = avpkt->data;
1042 int buf_size = avpkt->size;
1043 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1044 return flic_decode_frame_8BPP(avctx, data, got_frame,
1046 } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
1047 (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
1048 return flic_decode_frame_15_16BPP(avctx, data, got_frame,
1050 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
1051 return flic_decode_frame_24BPP(avctx, data, got_frame,
1055 /* Should not get here, ever as the pix_fmt is processed */
1056 /* in flic_decode_init and the above if should deal with */
1057 /* the finite set of possibilities allowable by here. */
1058 /* But in case we do, just error out. */
1059 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
1064 static av_cold int flic_decode_end(AVCodecContext *avctx)
1066 FlicDecodeContext *s = avctx->priv_data;
1068 av_frame_free(&s->frame);
1073 AVCodec ff_flic_decoder = {
1075 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
1076 .type = AVMEDIA_TYPE_VIDEO,
1077 .id = AV_CODEC_ID_FLIC,
1078 .priv_data_size = sizeof(FlicDecodeContext),
1079 .init = flic_decode_init,
1080 .close = flic_decode_end,
1081 .decode = flic_decode_frame,
1082 .capabilities = AV_CODEC_CAP_DR1,