8e8a81325894c017e0b90a148d2779bdfbdc1632
[libav.git] / libavcodec / flicvideo.c
1 /*
2 * FLI/FLC Animation Video Decoder
3 * Copyright (C) 2003, 2004 the ffmpeg project
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
42 #include "libavutil/intreadwrite.h"
43 #include "avcodec.h"
44
45 #define FLI_256_COLOR 4
46 #define FLI_DELTA 7
47 #define FLI_COLOR 11
48 #define FLI_LC 12
49 #define FLI_BLACK 13
50 #define FLI_BRUN 15
51 #define FLI_COPY 16
52 #define FLI_MINI 18
53 #define FLI_DTA_BRUN 25
54 #define FLI_DTA_COPY 26
55 #define FLI_DTA_LC 27
56
57 #define FLI_TYPE_CODE (0xAF11)
58 #define FLC_FLX_TYPE_CODE (0xAF12)
59 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
60 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
61
62 #define CHECK_PIXEL_PTR(n) \
63 if (pixel_ptr + n > pixel_limit) { \
64 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
65 pixel_ptr + n, pixel_limit); \
66 return -1; \
67 } \
68
69 typedef struct FlicDecodeContext {
70 AVCodecContext *avctx;
71 AVFrame frame;
72
73 unsigned int palette[256];
74 int new_palette;
75 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
76 } FlicDecodeContext;
77
78 static av_cold int flic_decode_init(AVCodecContext *avctx)
79 {
80 FlicDecodeContext *s = avctx->priv_data;
81 unsigned char *fli_header = (unsigned char *)avctx->extradata;
82 int depth;
83
84 if (avctx->extradata_size != 12 &&
85 avctx->extradata_size != 128) {
86 av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
87 return AVERROR_INVALIDDATA;
88 }
89
90 s->avctx = avctx;
91
92 s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
93
94 depth = 0;
95 if (s->avctx->extradata_size == 12) {
96 /* special case for magic carpet FLIs */
97 s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
98 depth = 8;
99 } else {
100 depth = AV_RL16(&fli_header[12]);
101 }
102
103 if (depth == 0) {
104 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
105 }
106
107 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
108 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
109 }
110
111 switch (depth) {
112 case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
113 case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
114 case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
115 case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
116 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
117 return -1;
118 default :
119 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
120 return -1;
121 }
122
123 s->frame.data[0] = NULL;
124 s->new_palette = 0;
125
126 return 0;
127 }
128
129 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
130 void *data, int *data_size,
131 const uint8_t *buf, int buf_size)
132 {
133 FlicDecodeContext *s = avctx->priv_data;
134
135 int stream_ptr = 0;
136 int stream_ptr_after_color_chunk;
137 int pixel_ptr;
138 int palette_ptr;
139 unsigned char palette_idx1;
140 unsigned char palette_idx2;
141
142 unsigned int frame_size;
143 int num_chunks;
144
145 unsigned int chunk_size;
146 int chunk_type;
147
148 int i, j;
149
150 int color_packets;
151 int color_changes;
152 int color_shift;
153 unsigned char r, g, b;
154
155 int lines;
156 int compressed_lines;
157 int starting_line;
158 signed short line_packets;
159 int y_ptr;
160 int byte_run;
161 int pixel_skip;
162 int pixel_countdown;
163 unsigned char *pixels;
164 unsigned int pixel_limit;
165
166 s->frame.reference = 1;
167 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
168 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
169 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
170 return -1;
171 }
172
173 pixels = s->frame.data[0];
174 pixel_limit = s->avctx->height * s->frame.linesize[0];
175
176 frame_size = AV_RL32(&buf[stream_ptr]);
177 stream_ptr += 6; /* skip the magic number */
178 num_chunks = AV_RL16(&buf[stream_ptr]);
179 stream_ptr += 10; /* skip padding */
180
181 frame_size -= 16;
182
183 /* iterate through the chunks */
184 while ((frame_size > 0) && (num_chunks > 0)) {
185 chunk_size = AV_RL32(&buf[stream_ptr]);
186 stream_ptr += 4;
187 chunk_type = AV_RL16(&buf[stream_ptr]);
188 stream_ptr += 2;
189
190 switch (chunk_type) {
191 case FLI_256_COLOR:
192 case FLI_COLOR:
193 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
194
195 /* check special case: If this file is from the Magic Carpet
196 * game and uses 6-bit colors even though it reports 256-color
197 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
198 * initialization) */
199 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
200 color_shift = 0;
201 else
202 color_shift = 2;
203 /* set up the palette */
204 color_packets = AV_RL16(&buf[stream_ptr]);
205 stream_ptr += 2;
206 palette_ptr = 0;
207 for (i = 0; i < color_packets; i++) {
208 /* first byte is how many colors to skip */
209 palette_ptr += buf[stream_ptr++];
210
211 /* next byte indicates how many entries to change */
212 color_changes = buf[stream_ptr++];
213
214 /* if there are 0 color changes, there are actually 256 */
215 if (color_changes == 0)
216 color_changes = 256;
217
218 for (j = 0; j < color_changes; j++) {
219 unsigned int entry;
220
221 /* wrap around, for good measure */
222 if ((unsigned)palette_ptr >= 256)
223 palette_ptr = 0;
224
225 r = buf[stream_ptr++] << color_shift;
226 g = buf[stream_ptr++] << color_shift;
227 b = buf[stream_ptr++] << color_shift;
228 entry = (r << 16) | (g << 8) | b;
229 if (s->palette[palette_ptr] != entry)
230 s->new_palette = 1;
231 s->palette[palette_ptr++] = entry;
232 }
233 }
234
235 /* color chunks sometimes have weird 16-bit alignment issues;
236 * therefore, take the hardline approach and set the stream_ptr
237 * to the value calculated w.r.t. the size specified by the color
238 * chunk header */
239 stream_ptr = stream_ptr_after_color_chunk;
240
241 break;
242
243 case FLI_DELTA:
244 y_ptr = 0;
245 compressed_lines = AV_RL16(&buf[stream_ptr]);
246 stream_ptr += 2;
247 while (compressed_lines > 0) {
248 line_packets = AV_RL16(&buf[stream_ptr]);
249 stream_ptr += 2;
250 if ((line_packets & 0xC000) == 0xC000) {
251 // line skip opcode
252 line_packets = -line_packets;
253 y_ptr += line_packets * s->frame.linesize[0];
254 } else if ((line_packets & 0xC000) == 0x4000) {
255 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
256 } else if ((line_packets & 0xC000) == 0x8000) {
257 // "last byte" opcode
258 pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
259 CHECK_PIXEL_PTR(0);
260 pixels[pixel_ptr] = line_packets & 0xff;
261 } else {
262 compressed_lines--;
263 pixel_ptr = y_ptr;
264 CHECK_PIXEL_PTR(0);
265 pixel_countdown = s->avctx->width;
266 for (i = 0; i < line_packets; i++) {
267 /* account for the skip bytes */
268 pixel_skip = buf[stream_ptr++];
269 pixel_ptr += pixel_skip;
270 pixel_countdown -= pixel_skip;
271 byte_run = (signed char)(buf[stream_ptr++]);
272 if (byte_run < 0) {
273 byte_run = -byte_run;
274 palette_idx1 = buf[stream_ptr++];
275 palette_idx2 = buf[stream_ptr++];
276 CHECK_PIXEL_PTR(byte_run * 2);
277 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
278 pixels[pixel_ptr++] = palette_idx1;
279 pixels[pixel_ptr++] = palette_idx2;
280 }
281 } else {
282 CHECK_PIXEL_PTR(byte_run * 2);
283 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
284 palette_idx1 = buf[stream_ptr++];
285 pixels[pixel_ptr++] = palette_idx1;
286 }
287 }
288 }
289
290 y_ptr += s->frame.linesize[0];
291 }
292 }
293 break;
294
295 case FLI_LC:
296 /* line compressed */
297 starting_line = AV_RL16(&buf[stream_ptr]);
298 stream_ptr += 2;
299 y_ptr = 0;
300 y_ptr += starting_line * s->frame.linesize[0];
301
302 compressed_lines = AV_RL16(&buf[stream_ptr]);
303 stream_ptr += 2;
304 while (compressed_lines > 0) {
305 pixel_ptr = y_ptr;
306 CHECK_PIXEL_PTR(0);
307 pixel_countdown = s->avctx->width;
308 line_packets = buf[stream_ptr++];
309 if (line_packets > 0) {
310 for (i = 0; i < line_packets; i++) {
311 /* account for the skip bytes */
312 pixel_skip = buf[stream_ptr++];
313 pixel_ptr += pixel_skip;
314 pixel_countdown -= pixel_skip;
315 byte_run = (signed char)(buf[stream_ptr++]);
316 if (byte_run > 0) {
317 CHECK_PIXEL_PTR(byte_run);
318 for (j = 0; j < byte_run; j++, pixel_countdown--) {
319 palette_idx1 = buf[stream_ptr++];
320 pixels[pixel_ptr++] = palette_idx1;
321 }
322 } else if (byte_run < 0) {
323 byte_run = -byte_run;
324 palette_idx1 = buf[stream_ptr++];
325 CHECK_PIXEL_PTR(byte_run);
326 for (j = 0; j < byte_run; j++, pixel_countdown--) {
327 pixels[pixel_ptr++] = palette_idx1;
328 }
329 }
330 }
331 }
332
333 y_ptr += s->frame.linesize[0];
334 compressed_lines--;
335 }
336 break;
337
338 case FLI_BLACK:
339 /* set the whole frame to color 0 (which is usually black) */
340 memset(pixels, 0,
341 s->frame.linesize[0] * s->avctx->height);
342 break;
343
344 case FLI_BRUN:
345 /* Byte run compression: This chunk type only occurs in the first
346 * FLI frame and it will update the entire frame. */
347 y_ptr = 0;
348 for (lines = 0; lines < s->avctx->height; lines++) {
349 pixel_ptr = y_ptr;
350 /* disregard the line packets; instead, iterate through all
351 * pixels on a row */
352 stream_ptr++;
353 pixel_countdown = s->avctx->width;
354 while (pixel_countdown > 0) {
355 byte_run = (signed char)(buf[stream_ptr++]);
356 if (byte_run > 0) {
357 palette_idx1 = buf[stream_ptr++];
358 CHECK_PIXEL_PTR(byte_run);
359 for (j = 0; j < byte_run; j++) {
360 pixels[pixel_ptr++] = palette_idx1;
361 pixel_countdown--;
362 if (pixel_countdown < 0)
363 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
364 pixel_countdown, lines);
365 }
366 } else { /* copy bytes if byte_run < 0 */
367 byte_run = -byte_run;
368 CHECK_PIXEL_PTR(byte_run);
369 for (j = 0; j < byte_run; j++) {
370 palette_idx1 = buf[stream_ptr++];
371 pixels[pixel_ptr++] = palette_idx1;
372 pixel_countdown--;
373 if (pixel_countdown < 0)
374 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
375 pixel_countdown, lines);
376 }
377 }
378 }
379
380 y_ptr += s->frame.linesize[0];
381 }
382 break;
383
384 case FLI_COPY:
385 /* copy the chunk (uncompressed frame) */
386 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
387 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
388 "bigger than image, skipping chunk\n", chunk_size - 6);
389 stream_ptr += chunk_size - 6;
390 } else {
391 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
392 y_ptr += s->frame.linesize[0]) {
393 memcpy(&pixels[y_ptr], &buf[stream_ptr],
394 s->avctx->width);
395 stream_ptr += s->avctx->width;
396 }
397 }
398 break;
399
400 case FLI_MINI:
401 /* some sort of a thumbnail? disregard this chunk... */
402 stream_ptr += chunk_size - 6;
403 break;
404
405 default:
406 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
407 break;
408 }
409
410 frame_size -= chunk_size;
411 num_chunks--;
412 }
413
414 /* by the end of the chunk, the stream ptr should equal the frame
415 * size (minus 1, possibly); if it doesn't, issue a warning */
416 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
417 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
418 "and final chunk ptr = %d\n", buf_size, stream_ptr);
419
420 /* make the palette available on the way out */
421 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
422 if (s->new_palette) {
423 s->frame.palette_has_changed = 1;
424 s->new_palette = 0;
425 }
426
427 *data_size=sizeof(AVFrame);
428 *(AVFrame*)data = s->frame;
429
430 return buf_size;
431 }
432
433 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
434 void *data, int *data_size,
435 const uint8_t *buf, int buf_size)
436 {
437 /* Note, the only difference between the 15Bpp and 16Bpp */
438 /* Format is the pixel format, the packets are processed the same. */
439 FlicDecodeContext *s = avctx->priv_data;
440
441 int stream_ptr = 0;
442 int pixel_ptr;
443 unsigned char palette_idx1;
444
445 unsigned int frame_size;
446 int num_chunks;
447
448 unsigned int chunk_size;
449 int chunk_type;
450
451 int i, j;
452
453 int lines;
454 int compressed_lines;
455 signed short line_packets;
456 int y_ptr;
457 int byte_run;
458 int pixel_skip;
459 int pixel_countdown;
460 unsigned char *pixels;
461 int pixel;
462 unsigned int pixel_limit;
463
464 s->frame.reference = 1;
465 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
466 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
467 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
468 return -1;
469 }
470
471 pixels = s->frame.data[0];
472 pixel_limit = s->avctx->height * s->frame.linesize[0];
473
474 frame_size = AV_RL32(&buf[stream_ptr]);
475 stream_ptr += 6; /* skip the magic number */
476 num_chunks = AV_RL16(&buf[stream_ptr]);
477 stream_ptr += 10; /* skip padding */
478
479 frame_size -= 16;
480
481 /* iterate through the chunks */
482 while ((frame_size > 0) && (num_chunks > 0)) {
483 chunk_size = AV_RL32(&buf[stream_ptr]);
484 stream_ptr += 4;
485 chunk_type = AV_RL16(&buf[stream_ptr]);
486 stream_ptr += 2;
487
488 switch (chunk_type) {
489 case FLI_256_COLOR:
490 case FLI_COLOR:
491 /* For some reason, it seems that non-palettized flics do
492 * include one of these chunks in their first frame.
493 * Why I do not know, it seems rather extraneous. */
494 /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
495 stream_ptr = stream_ptr + chunk_size - 6;
496 break;
497
498 case FLI_DELTA:
499 case FLI_DTA_LC:
500 y_ptr = 0;
501 compressed_lines = AV_RL16(&buf[stream_ptr]);
502 stream_ptr += 2;
503 while (compressed_lines > 0) {
504 line_packets = AV_RL16(&buf[stream_ptr]);
505 stream_ptr += 2;
506 if (line_packets < 0) {
507 line_packets = -line_packets;
508 y_ptr += line_packets * s->frame.linesize[0];
509 } else {
510 compressed_lines--;
511 pixel_ptr = y_ptr;
512 CHECK_PIXEL_PTR(0);
513 pixel_countdown = s->avctx->width;
514 for (i = 0; i < line_packets; i++) {
515 /* account for the skip bytes */
516 pixel_skip = buf[stream_ptr++];
517 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
518 pixel_countdown -= pixel_skip;
519 byte_run = (signed char)(buf[stream_ptr++]);
520 if (byte_run < 0) {
521 byte_run = -byte_run;
522 pixel = AV_RL16(&buf[stream_ptr]);
523 stream_ptr += 2;
524 CHECK_PIXEL_PTR(2 * byte_run);
525 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
526 *((signed short*)(&pixels[pixel_ptr])) = pixel;
527 pixel_ptr += 2;
528 }
529 } else {
530 CHECK_PIXEL_PTR(2 * byte_run);
531 for (j = 0; j < byte_run; j++, pixel_countdown--) {
532 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
533 stream_ptr += 2;
534 pixel_ptr += 2;
535 }
536 }
537 }
538
539 y_ptr += s->frame.linesize[0];
540 }
541 }
542 break;
543
544 case FLI_LC:
545 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
546 stream_ptr = stream_ptr + chunk_size - 6;
547 break;
548
549 case FLI_BLACK:
550 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
551 memset(pixels, 0x0000,
552 s->frame.linesize[0] * s->avctx->height);
553 break;
554
555 case FLI_BRUN:
556 y_ptr = 0;
557 for (lines = 0; lines < s->avctx->height; lines++) {
558 pixel_ptr = y_ptr;
559 /* disregard the line packets; instead, iterate through all
560 * pixels on a row */
561 stream_ptr++;
562 pixel_countdown = (s->avctx->width * 2);
563
564 while (pixel_countdown > 0) {
565 byte_run = (signed char)(buf[stream_ptr++]);
566 if (byte_run > 0) {
567 palette_idx1 = buf[stream_ptr++];
568 CHECK_PIXEL_PTR(byte_run);
569 for (j = 0; j < byte_run; j++) {
570 pixels[pixel_ptr++] = palette_idx1;
571 pixel_countdown--;
572 if (pixel_countdown < 0)
573 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
574 pixel_countdown, lines);
575 }
576 } else { /* copy bytes if byte_run < 0 */
577 byte_run = -byte_run;
578 CHECK_PIXEL_PTR(byte_run);
579 for (j = 0; j < byte_run; j++) {
580 palette_idx1 = buf[stream_ptr++];
581 pixels[pixel_ptr++] = palette_idx1;
582 pixel_countdown--;
583 if (pixel_countdown < 0)
584 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
585 pixel_countdown, lines);
586 }
587 }
588 }
589
590 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
591 * This does not give us any good oportunity to perform word endian conversion
592 * during decompression. So if it is required (i.e., this is not a LE target, we do
593 * a second pass over the line here, swapping the bytes.
594 */
595 #if HAVE_BIGENDIAN
596 pixel_ptr = y_ptr;
597 pixel_countdown = s->avctx->width;
598 while (pixel_countdown > 0) {
599 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
600 pixel_ptr += 2;
601 }
602 #endif
603 y_ptr += s->frame.linesize[0];
604 }
605 break;
606
607 case FLI_DTA_BRUN:
608 y_ptr = 0;
609 for (lines = 0; lines < s->avctx->height; lines++) {
610 pixel_ptr = y_ptr;
611 /* disregard the line packets; instead, iterate through all
612 * pixels on a row */
613 stream_ptr++;
614 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
615
616 while (pixel_countdown > 0) {
617 byte_run = (signed char)(buf[stream_ptr++]);
618 if (byte_run > 0) {
619 pixel = AV_RL16(&buf[stream_ptr]);
620 stream_ptr += 2;
621 CHECK_PIXEL_PTR(2 * byte_run);
622 for (j = 0; j < byte_run; j++) {
623 *((signed short*)(&pixels[pixel_ptr])) = pixel;
624 pixel_ptr += 2;
625 pixel_countdown--;
626 if (pixel_countdown < 0)
627 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
628 pixel_countdown);
629 }
630 } else { /* copy pixels if byte_run < 0 */
631 byte_run = -byte_run;
632 CHECK_PIXEL_PTR(2 * byte_run);
633 for (j = 0; j < byte_run; j++) {
634 *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[stream_ptr]);
635 stream_ptr += 2;
636 pixel_ptr += 2;
637 pixel_countdown--;
638 if (pixel_countdown < 0)
639 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
640 pixel_countdown);
641 }
642 }
643 }
644
645 y_ptr += s->frame.linesize[0];
646 }
647 break;
648
649 case FLI_COPY:
650 case FLI_DTA_COPY:
651 /* copy the chunk (uncompressed frame) */
652 if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
653 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
654 "bigger than image, skipping chunk\n", chunk_size - 6);
655 stream_ptr += chunk_size - 6;
656 } else {
657
658 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
659 y_ptr += s->frame.linesize[0]) {
660
661 pixel_countdown = s->avctx->width;
662 pixel_ptr = 0;
663 while (pixel_countdown > 0) {
664 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = AV_RL16(&buf[stream_ptr+pixel_ptr]);
665 pixel_ptr += 2;
666 pixel_countdown--;
667 }
668 stream_ptr += s->avctx->width*2;
669 }
670 }
671 break;
672
673 case FLI_MINI:
674 /* some sort of a thumbnail? disregard this chunk... */
675 stream_ptr += chunk_size - 6;
676 break;
677
678 default:
679 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
680 break;
681 }
682
683 frame_size -= chunk_size;
684 num_chunks--;
685 }
686
687 /* by the end of the chunk, the stream ptr should equal the frame
688 * size (minus 1, possibly); if it doesn't, issue a warning */
689 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
690 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
691 "and final chunk ptr = %d\n", buf_size, stream_ptr);
692
693
694 *data_size=sizeof(AVFrame);
695 *(AVFrame*)data = s->frame;
696
697 return buf_size;
698 }
699
700 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
701 void *data, int *data_size,
702 const uint8_t *buf, int buf_size)
703 {
704 av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
705 return -1;
706 }
707
708 static int flic_decode_frame(AVCodecContext *avctx,
709 void *data, int *data_size,
710 AVPacket *avpkt)
711 {
712 const uint8_t *buf = avpkt->data;
713 int buf_size = avpkt->size;
714 if (avctx->pix_fmt == PIX_FMT_PAL8) {
715 return flic_decode_frame_8BPP(avctx, data, data_size,
716 buf, buf_size);
717 }
718 else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
719 (avctx->pix_fmt == PIX_FMT_RGB565)) {
720 return flic_decode_frame_15_16BPP(avctx, data, data_size,
721 buf, buf_size);
722 }
723 else if (avctx->pix_fmt == PIX_FMT_BGR24) {
724 return flic_decode_frame_24BPP(avctx, data, data_size,
725 buf, buf_size);
726 }
727
728 /* Should not get here, ever as the pix_fmt is processed */
729 /* in flic_decode_init and the above if should deal with */
730 /* the finite set of possibilites allowable by here. */
731 /* But in case we do, just error out. */
732 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
733 return -1;
734 }
735
736
737 static av_cold int flic_decode_end(AVCodecContext *avctx)
738 {
739 FlicDecodeContext *s = avctx->priv_data;
740
741 if (s->frame.data[0])
742 avctx->release_buffer(avctx, &s->frame);
743
744 return 0;
745 }
746
747 AVCodec ff_flic_decoder = {
748 .name = "flic",
749 .type = AVMEDIA_TYPE_VIDEO,
750 .id = CODEC_ID_FLIC,
751 .priv_data_size = sizeof(FlicDecodeContext),
752 .init = flic_decode_init,
753 .close = flic_decode_end,
754 .decode = flic_decode_frame,
755 .capabilities = CODEC_CAP_DR1,
756 .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
757 };