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 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
454 frame_size -= chunk_size;
458 /* by the end of the chunk, the stream ptr should equal the frame
459 * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
460 if (bytestream2_get_bytes_left(&g2) > 2)
461 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
462 "and final chunk ptr = %d\n", buf_size,
463 buf_size - bytestream2_get_bytes_left(&g2));
465 /* make the palette available on the way out */
466 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
467 if (s->new_palette) {
468 s->frame->palette_has_changed = 1;
472 if ((ret = av_frame_ref(data, s->frame)) < 0)
480 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
481 void *data, int *got_frame,
482 const uint8_t *buf, int buf_size)
484 /* Note, the only difference between the 15Bpp and 16Bpp */
485 /* Format is the pixel format, the packets are processed the same. */
486 FlicDecodeContext *s = avctx->priv_data;
490 unsigned char palette_idx1;
492 unsigned int frame_size;
495 unsigned int chunk_size;
501 int compressed_lines;
502 signed short line_packets;
507 unsigned char *pixels;
509 unsigned int pixel_limit;
511 bytestream2_init(&g2, buf, buf_size);
513 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
516 pixels = s->frame->data[0];
517 pixel_limit = s->avctx->height * s->frame->linesize[0];
519 frame_size = bytestream2_get_le32(&g2);
520 bytestream2_skip(&g2, 2); /* skip the magic number */
521 num_chunks = bytestream2_get_le16(&g2);
522 bytestream2_skip(&g2, 8); /* skip padding */
523 if (frame_size > buf_size)
524 frame_size = buf_size;
528 /* iterate through the chunks */
529 while ((frame_size > 0) && (num_chunks > 0) &&
530 bytestream2_get_bytes_left(&g2) >= 4) {
531 int stream_ptr_after_chunk;
532 chunk_size = bytestream2_get_le32(&g2);
533 if (chunk_size > frame_size) {
534 av_log(avctx, AV_LOG_WARNING,
535 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
536 chunk_size = frame_size;
538 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
540 chunk_type = bytestream2_get_le16(&g2);
543 switch (chunk_type) {
546 /* For some reason, it seems that non-palettized flics do
547 * include one of these chunks in their first frame.
548 * Why I do not know, it seems rather extraneous. */
550 "Unexpected Palette chunk %d in non-palettized FLC\n",
552 bytestream2_skip(&g2, chunk_size - 6);
558 compressed_lines = bytestream2_get_le16(&g2);
559 while (compressed_lines > 0) {
560 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
562 line_packets = bytestream2_get_le16(&g2);
563 if (line_packets < 0) {
564 line_packets = -line_packets;
565 y_ptr += line_packets * s->frame->linesize[0];
570 pixel_countdown = s->avctx->width;
571 for (i = 0; i < line_packets; i++) {
572 /* account for the skip bytes */
573 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
575 pixel_skip = bytestream2_get_byte(&g2);
576 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
577 pixel_countdown -= pixel_skip;
578 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
580 byte_run = -byte_run;
581 pixel = bytestream2_get_le16(&g2);
582 CHECK_PIXEL_PTR(2 * byte_run);
583 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
584 *((signed short*)(&pixels[pixel_ptr])) = pixel;
588 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
590 CHECK_PIXEL_PTR(2 * byte_run);
591 for (j = 0; j < byte_run; j++, pixel_countdown--) {
592 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
598 y_ptr += s->frame->linesize[0];
604 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
605 bytestream2_skip(&g2, chunk_size - 6);
609 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
610 memset(pixels, 0x0000,
611 s->frame->linesize[0] * s->avctx->height);
616 for (lines = 0; lines < s->avctx->height; lines++) {
618 /* disregard the line packets; instead, iterate through all
620 bytestream2_skip(&g2, 1);
621 pixel_countdown = (s->avctx->width * 2);
623 while (pixel_countdown > 0) {
624 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
626 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
628 palette_idx1 = bytestream2_get_byte(&g2);
629 CHECK_PIXEL_PTR(byte_run);
630 for (j = 0; j < byte_run; j++) {
631 pixels[pixel_ptr++] = palette_idx1;
633 if (pixel_countdown < 0)
634 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
635 pixel_countdown, lines);
637 } else { /* copy bytes if byte_run < 0 */
638 byte_run = -byte_run;
639 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
641 CHECK_PIXEL_PTR(byte_run);
642 for (j = 0; j < byte_run; j++) {
643 palette_idx1 = bytestream2_get_byte(&g2);
644 pixels[pixel_ptr++] = palette_idx1;
646 if (pixel_countdown < 0)
647 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
648 pixel_countdown, lines);
653 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
654 * This does not give us any good opportunity to perform word endian conversion
655 * during decompression. So if it is required (i.e., this is not a LE target, we do
656 * a second pass over the line here, swapping the bytes.
660 pixel_countdown = s->avctx->width;
661 while (pixel_countdown > 0) {
662 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
666 y_ptr += s->frame->linesize[0];
672 for (lines = 0; lines < s->avctx->height; lines++) {
674 /* disregard the line packets; instead, iterate through all
676 bytestream2_skip(&g2, 1);
677 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
679 while (pixel_countdown > 0) {
680 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
682 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
684 pixel = bytestream2_get_le16(&g2);
685 CHECK_PIXEL_PTR(2 * byte_run);
686 for (j = 0; j < byte_run; j++) {
687 *((signed short*)(&pixels[pixel_ptr])) = pixel;
690 if (pixel_countdown < 0)
691 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
694 } else { /* copy pixels if byte_run < 0 */
695 byte_run = -byte_run;
696 if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
698 CHECK_PIXEL_PTR(2 * byte_run);
699 for (j = 0; j < byte_run; j++) {
700 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
703 if (pixel_countdown < 0)
704 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
710 y_ptr += s->frame->linesize[0];
716 /* copy the chunk (uncompressed frame) */
717 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
718 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
719 "bigger than image, skipping chunk\n", chunk_size - 6);
720 bytestream2_skip(&g2, chunk_size - 6);
723 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
724 y_ptr += s->frame->linesize[0]) {
726 pixel_countdown = s->avctx->width;
728 while (pixel_countdown > 0) {
729 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
733 if (s->avctx->width & 1)
734 bytestream2_skip(&g2, 2);
740 /* some sort of a thumbnail? disregard this chunk... */
741 bytestream2_skip(&g2, chunk_size - 6);
745 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
749 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
750 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
752 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
756 frame_size -= chunk_size;
760 /* by the end of the chunk, the stream ptr should equal the frame
761 * size (minus 1, possibly); if it doesn't, issue a warning */
762 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
763 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
764 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
766 if ((ret = av_frame_ref(data, s->frame)) < 0)
774 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
775 void *data, int *got_frame,
776 const uint8_t *buf, int buf_size)
778 FlicDecodeContext *s = avctx->priv_data;
782 unsigned char palette_idx1;
784 unsigned int frame_size;
787 unsigned int chunk_size;
793 int compressed_lines;
794 signed short line_packets;
799 unsigned char *pixels;
801 unsigned int pixel_limit;
803 bytestream2_init(&g2, buf, buf_size);
805 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
808 pixels = s->frame->data[0];
809 pixel_limit = s->avctx->height * s->frame->linesize[0];
811 frame_size = bytestream2_get_le32(&g2);
812 bytestream2_skip(&g2, 2); /* skip the magic number */
813 num_chunks = bytestream2_get_le16(&g2);
814 bytestream2_skip(&g2, 8); /* skip padding */
815 if (frame_size > buf_size)
816 frame_size = buf_size;
820 /* iterate through the chunks */
821 while ((frame_size > 0) && (num_chunks > 0) &&
822 bytestream2_get_bytes_left(&g2) >= 4) {
823 int stream_ptr_after_chunk;
824 chunk_size = bytestream2_get_le32(&g2);
825 if (chunk_size > frame_size) {
826 av_log(avctx, AV_LOG_WARNING,
827 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
828 chunk_size = frame_size;
830 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
832 chunk_type = bytestream2_get_le16(&g2);
835 switch (chunk_type) {
838 /* For some reason, it seems that non-palettized flics do
839 * include one of these chunks in their first frame.
840 * Why I do not know, it seems rather extraneous. */
842 "Unexpected Palette chunk %d in non-palettized FLC\n",
844 bytestream2_skip(&g2, chunk_size - 6);
850 compressed_lines = bytestream2_get_le16(&g2);
851 while (compressed_lines > 0) {
852 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
854 line_packets = bytestream2_get_le16(&g2);
855 if (line_packets < 0) {
856 line_packets = -line_packets;
857 y_ptr += line_packets * s->frame->linesize[0];
862 pixel_countdown = s->avctx->width;
863 for (i = 0; i < line_packets; i++) {
864 /* account for the skip bytes */
865 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
867 pixel_skip = bytestream2_get_byte(&g2);
868 pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
869 pixel_countdown -= pixel_skip;
870 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
872 byte_run = -byte_run;
873 pixel = bytestream2_get_le24(&g2);
874 CHECK_PIXEL_PTR(3 * byte_run);
875 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
876 AV_WL24(&pixels[pixel_ptr], pixel);
880 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
882 CHECK_PIXEL_PTR(2 * byte_run);
883 for (j = 0; j < byte_run; j++, pixel_countdown--) {
884 pixel = bytestream2_get_le24(&g2);
885 AV_WL24(&pixels[pixel_ptr], pixel);
891 y_ptr += s->frame->linesize[0];
897 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
898 bytestream2_skip(&g2, chunk_size - 6);
902 /* set the whole frame to 0x00 which is black for 24 bit mode. */
904 s->frame->linesize[0] * s->avctx->height);
909 for (lines = 0; lines < s->avctx->height; lines++) {
911 /* disregard the line packets; instead, iterate through all
913 bytestream2_skip(&g2, 1);
914 pixel_countdown = (s->avctx->width * 3);
916 while (pixel_countdown > 0) {
917 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
919 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
921 palette_idx1 = bytestream2_get_byte(&g2);
922 CHECK_PIXEL_PTR(byte_run);
923 for (j = 0; j < byte_run; j++) {
924 pixels[pixel_ptr++] = palette_idx1;
926 if (pixel_countdown < 0)
927 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
928 pixel_countdown, lines);
930 } else { /* copy bytes if byte_run < 0 */
931 byte_run = -byte_run;
932 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
934 CHECK_PIXEL_PTR(byte_run);
935 for (j = 0; j < byte_run; j++) {
936 palette_idx1 = bytestream2_get_byte(&g2);
937 pixels[pixel_ptr++] = palette_idx1;
939 if (pixel_countdown < 0)
940 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
941 pixel_countdown, lines);
946 y_ptr += s->frame->linesize[0];
952 for (lines = 0; lines < s->avctx->height; lines++) {
954 /* disregard the line packets; instead, iterate through all
956 bytestream2_skip(&g2, 1);
957 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
959 while (pixel_countdown > 0) {
960 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
962 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
964 pixel = bytestream2_get_le24(&g2);
965 CHECK_PIXEL_PTR(3 * byte_run);
966 for (j = 0; j < byte_run; j++) {
967 AV_WL24(pixels + pixel_ptr, pixel);
970 if (pixel_countdown < 0)
971 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
974 } else { /* copy pixels if byte_run < 0 */
975 byte_run = -byte_run;
976 if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
978 CHECK_PIXEL_PTR(3 * byte_run);
979 for (j = 0; j < byte_run; j++) {
980 pixel = bytestream2_get_le24(&g2);
981 AV_WL24(pixels + pixel_ptr, pixel);
984 if (pixel_countdown < 0)
985 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
991 y_ptr += s->frame->linesize[0];
997 /* copy the chunk (uncompressed frame) */
998 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
999 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
1000 "bigger than image, skipping chunk\n", chunk_size - 6);
1001 bytestream2_skip(&g2, chunk_size - 6);
1003 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
1004 y_ptr += s->frame->linesize[0]) {
1006 pixel_countdown = s->avctx->width;
1008 while (pixel_countdown > 0) {
1009 pixel = bytestream2_get_le24(&g2);
1010 AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
1014 if (s->avctx->width & 1)
1015 bytestream2_skip(&g2, 3);
1021 /* some sort of a thumbnail? disregard this chunk... */
1022 bytestream2_skip(&g2, chunk_size - 6);
1026 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
1030 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
1031 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
1033 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
1037 frame_size -= chunk_size;
1041 /* by the end of the chunk, the stream ptr should equal the frame
1042 * size (minus 1, possibly); if it doesn't, issue a warning */
1043 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
1044 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
1045 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
1047 if ((ret = av_frame_ref(data, s->frame)) < 0)
1055 static int flic_decode_frame(AVCodecContext *avctx,
1056 void *data, int *got_frame,
1059 const uint8_t *buf = avpkt->data;
1060 int buf_size = avpkt->size;
1061 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1062 return flic_decode_frame_8BPP(avctx, data, got_frame,
1064 } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
1065 (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
1066 return flic_decode_frame_15_16BPP(avctx, data, got_frame,
1068 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
1069 return flic_decode_frame_24BPP(avctx, data, got_frame,
1073 /* Should not get here, ever as the pix_fmt is processed */
1074 /* in flic_decode_init and the above if should deal with */
1075 /* the finite set of possibilities allowable by here. */
1076 /* But in case we do, just error out. */
1077 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
1082 static av_cold int flic_decode_end(AVCodecContext *avctx)
1084 FlicDecodeContext *s = avctx->priv_data;
1086 av_frame_free(&s->frame);
1091 AVCodec ff_flic_decoder = {
1093 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
1094 .type = AVMEDIA_TYPE_VIDEO,
1095 .id = AV_CODEC_ID_FLIC,
1096 .priv_data_size = sizeof(FlicDecodeContext),
1097 .init = flic_decode_init,
1098 .close = flic_decode_end,
1099 .decode = flic_decode_frame,
1100 .capabilities = AV_CODEC_CAP_DR1,