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