2 * FLI/FLC Animation Video Decoder
3 * Copyright (C) 2003, 2004 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Autodesk Animator FLI/FLC Video Decoder
24 * by Mike Melanson (melanson@pcisys.net)
25 * for more information on the .fli/.flc file format and all of its many
27 * http://www.compuphase.com/flic.htm
29 * This decoder outputs PAL8 colorspace data. To use this decoder, be
30 * sure that your demuxer sends the FLI file header to the decoder via
31 * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
32 * large. The only exception is for FLI files from the game "Magic Carpet",
33 * in which the header is only 12 bytes.
45 #define FLI_256_COLOR 4
54 typedef struct FlicDecodeContext {
55 AVCodecContext *avctx;
58 unsigned int palette[256];
60 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
63 static int flic_decode_init(AVCodecContext *avctx)
65 FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
66 unsigned char *fli_header = (unsigned char *)avctx->extradata;
69 avctx->pix_fmt = PIX_FMT_PAL8;
70 avctx->has_b_frames = 0;
72 if (s->avctx->extradata_size == 12) {
73 /* special case for magic carpet FLIs */
75 } else if (s->avctx->extradata_size == 128) {
76 s->fli_type = LE_16(&fli_header[4]);
78 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
82 s->frame.data[0] = NULL;
88 static int flic_decode_frame(AVCodecContext *avctx,
89 void *data, int *data_size,
90 uint8_t *buf, int buf_size)
92 FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
95 int stream_ptr_after_color_chunk;
98 unsigned char palette_idx1;
99 unsigned char palette_idx2;
101 unsigned int frame_size;
104 unsigned int chunk_size;
112 unsigned char r, g, b;
115 int compressed_lines;
117 signed short line_packets;
119 signed char byte_run;
122 unsigned char *pixels;
124 s->frame.reference = 1;
125 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
126 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
127 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
131 pixels = s->frame.data[0];
133 frame_size = LE_32(&buf[stream_ptr]);
134 stream_ptr += 6; /* skip the magic number */
135 num_chunks = LE_16(&buf[stream_ptr]);
136 stream_ptr += 10; /* skip padding */
140 /* iterate through the chunks */
141 while ((frame_size > 0) && (num_chunks > 0)) {
142 chunk_size = LE_32(&buf[stream_ptr]);
144 chunk_type = LE_16(&buf[stream_ptr]);
147 switch (chunk_type) {
150 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
153 /* check special case: If this file is from the Magic Carpet
154 * game and uses 6-bit colors even though it reports 256-color
155 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
157 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != 0xAF13))
161 /* set up the palette */
162 color_packets = LE_16(&buf[stream_ptr]);
165 for (i = 0; i < color_packets; i++) {
166 /* first byte is how many colors to skip */
167 palette_ptr += buf[stream_ptr++];
169 /* next byte indicates how many entries to change */
170 color_changes = buf[stream_ptr++];
172 /* if there are 0 color changes, there are actually 256 */
173 if (color_changes == 0)
176 for (j = 0; j < color_changes; j++) {
178 /* wrap around, for good measure */
179 if (palette_ptr >= 256)
182 r = buf[stream_ptr++] << color_shift;
183 g = buf[stream_ptr++] << color_shift;
184 b = buf[stream_ptr++] << color_shift;
185 s->palette[palette_ptr++] = (r << 16) | (g << 8) | b;
189 /* color chunks sometimes have weird 16-bit alignment issues;
190 * therefore, take the hardline approach and set the stream_ptr
191 * to the value calculated w.r.t. the size specified by the color
193 stream_ptr = stream_ptr_after_color_chunk;
199 compressed_lines = LE_16(&buf[stream_ptr]);
201 while (compressed_lines > 0) {
202 line_packets = LE_16(&buf[stream_ptr]);
204 if (line_packets < 0) {
205 line_packets = -line_packets;
206 y_ptr += line_packets * s->frame.linesize[0];
210 pixel_countdown = s->avctx->width;
211 for (i = 0; i < line_packets; i++) {
212 /* account for the skip bytes */
213 pixel_skip = buf[stream_ptr++];
214 pixel_ptr += pixel_skip;
215 pixel_countdown -= pixel_skip;
216 byte_run = buf[stream_ptr++];
218 byte_run = -byte_run;
219 palette_idx1 = buf[stream_ptr++];
220 palette_idx2 = buf[stream_ptr++];
221 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
222 pixels[pixel_ptr++] = palette_idx1;
223 pixels[pixel_ptr++] = palette_idx2;
226 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
227 palette_idx1 = buf[stream_ptr++];
228 pixels[pixel_ptr++] = palette_idx1;
233 y_ptr += s->frame.linesize[0];
239 /* line compressed */
240 starting_line = LE_16(&buf[stream_ptr]);
243 y_ptr += starting_line * s->frame.linesize[0];
245 compressed_lines = LE_16(&buf[stream_ptr]);
247 while (compressed_lines > 0) {
249 pixel_countdown = s->avctx->width;
250 line_packets = buf[stream_ptr++];
251 if (line_packets > 0) {
252 for (i = 0; i < line_packets; i++) {
253 /* account for the skip bytes */
254 pixel_skip = buf[stream_ptr++];
255 pixel_ptr += pixel_skip;
256 pixel_countdown -= pixel_skip;
257 byte_run = buf[stream_ptr++];
259 for (j = 0; j < byte_run; j++, pixel_countdown--) {
260 palette_idx1 = buf[stream_ptr++];
261 pixels[pixel_ptr++] = palette_idx1;
264 byte_run = -byte_run;
265 palette_idx1 = buf[stream_ptr++];
266 for (j = 0; j < byte_run; j++, pixel_countdown--) {
267 pixels[pixel_ptr++] = palette_idx1;
273 y_ptr += s->frame.linesize[0];
279 /* set the whole frame to color 0 (which is usually black) */
281 s->frame.linesize[0] * s->avctx->height);
285 /* Byte run compression: This chunk type only occurs in the first
286 * FLI frame and it will update the entire frame. */
288 for (lines = 0; lines < s->avctx->height; lines++) {
290 /* disregard the line packets; instead, iterate through all
293 pixel_countdown = s->avctx->width;
294 while (pixel_countdown > 0) {
295 byte_run = buf[stream_ptr++];
297 palette_idx1 = buf[stream_ptr++];
298 for (j = 0; j < byte_run; j++) {
299 pixels[pixel_ptr++] = palette_idx1;
301 if (pixel_countdown < 0)
302 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
305 } else { /* copy bytes if byte_run < 0 */
306 byte_run = -byte_run;
307 for (j = 0; j < byte_run; j++) {
308 palette_idx1 = buf[stream_ptr++];
309 pixels[pixel_ptr++] = palette_idx1;
311 if (pixel_countdown < 0)
312 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
318 y_ptr += s->frame.linesize[0];
323 /* copy the chunk (uncompressed frame) */
324 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
325 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
326 "bigger than image, skipping chunk\n", chunk_size - 6);
327 stream_ptr += chunk_size - 6;
329 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
330 y_ptr += s->frame.linesize[0]) {
331 memcpy(&pixels[y_ptr], &buf[stream_ptr],
333 stream_ptr += s->avctx->width;
339 /* some sort of a thumbnail? disregard this chunk... */
340 stream_ptr += chunk_size - 6;
344 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
348 frame_size -= chunk_size;
352 /* by the end of the chunk, the stream ptr should equal the frame
353 * size (minus 1, possibly); if it doesn't, issue a warning */
354 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
355 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
356 "and final chunk ptr = %d\n", buf_size, stream_ptr);
358 /* make the palette available on the way out */
359 // if (s->new_palette) {
361 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
362 s->frame.palette_has_changed = 1;
366 *data_size=sizeof(AVFrame);
367 *(AVFrame*)data = s->frame;
372 static int flic_decode_end(AVCodecContext *avctx)
374 FlicDecodeContext *s = avctx->priv_data;
376 if (s->frame.data[0])
377 avctx->release_buffer(avctx, &s->frame);
382 AVCodec flic_decoder = {
386 sizeof(FlicDecodeContext),