]> git.sesse.net Git - ffmpeg/blob - libavcodec/flicvideo.c
avcodec/flicvideo: add support for 24bit flic files
[ffmpeg] / 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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
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_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %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 != 0 &&
88         avctx->extradata_size != 12 &&
89         avctx->extradata_size != 128 &&
90         avctx->extradata_size != 256 &&
91         avctx->extradata_size != 904 &&
92         avctx->extradata_size != 1024) {
93         av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
94         return AVERROR_INVALIDDATA;
95     }
96
97     s->avctx = avctx;
98
99     if (s->avctx->extradata_size == 12) {
100         /* special case for magic carpet FLIs */
101         s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
102         depth = 8;
103     } else if (avctx->extradata_size == 1024) {
104         uint8_t *ptr = avctx->extradata;
105         int i;
106
107         for (i = 0; i < 256; i++) {
108             s->palette[i] = AV_RL32(ptr);
109             ptr += 4;
110         }
111         depth = 8;
112         /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
113     } else if (avctx->extradata_size == 0 ||
114                avctx->extradata_size == 256 ||
115         /* see FFmpeg ticket #1234 */
116                avctx->extradata_size == 904) {
117         s->fli_type = FLI_TYPE_CODE;
118         depth = 8;
119     } else {
120         s->fli_type = AV_RL16(&fli_header[4]);
121         depth = AV_RL16(&fli_header[12]);
122     }
123
124     if (depth == 0) {
125         depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
126     }
127
128     if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
129         depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
130     }
131
132     switch (depth) {
133         case 8  : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
134         case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
135         case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
136         case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
137         default :
138                   av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
139                   return AVERROR_INVALIDDATA;
140     }
141
142     s->frame = av_frame_alloc();
143     if (!s->frame)
144         return AVERROR(ENOMEM);
145
146     s->new_palette = 0;
147
148     return 0;
149 }
150
151 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
152                                   void *data, int *got_frame,
153                                   const uint8_t *buf, int buf_size)
154 {
155     FlicDecodeContext *s = avctx->priv_data;
156
157     GetByteContext g2;
158     int pixel_ptr;
159     int palette_ptr;
160     unsigned char palette_idx1;
161     unsigned char palette_idx2;
162
163     unsigned int frame_size;
164     int num_chunks;
165
166     unsigned int chunk_size;
167     int chunk_type;
168
169     int i, j, ret;
170
171     int color_packets;
172     int color_changes;
173     int color_shift;
174     unsigned char r, g, b;
175
176     int lines;
177     int compressed_lines;
178     int starting_line;
179     signed short line_packets;
180     int y_ptr;
181     int byte_run;
182     int pixel_skip;
183     int pixel_countdown;
184     unsigned char *pixels;
185     unsigned int pixel_limit;
186
187     bytestream2_init(&g2, buf, buf_size);
188
189     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
190         return ret;
191
192     pixels = s->frame->data[0];
193     pixel_limit = s->avctx->height * s->frame->linesize[0];
194     if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
195         return AVERROR_INVALIDDATA;
196     frame_size = bytestream2_get_le32(&g2);
197     if (frame_size > buf_size)
198         frame_size = buf_size;
199     bytestream2_skip(&g2, 2); /* skip the magic number */
200     num_chunks = bytestream2_get_le16(&g2);
201     bytestream2_skip(&g2, 8);  /* skip padding */
202
203     frame_size -= 16;
204
205     /* iterate through the chunks */
206     while ((frame_size >= 6) && (num_chunks > 0) &&
207             bytestream2_get_bytes_left(&g2) >= 4) {
208         int stream_ptr_after_chunk;
209         chunk_size = bytestream2_get_le32(&g2);
210         if (chunk_size > frame_size) {
211             av_log(avctx, AV_LOG_WARNING,
212                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
213             chunk_size = frame_size;
214         }
215         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
216
217         chunk_type = bytestream2_get_le16(&g2);
218
219         switch (chunk_type) {
220         case FLI_256_COLOR:
221         case FLI_COLOR:
222             /* check special case: If this file is from the Magic Carpet
223              * game and uses 6-bit colors even though it reports 256-color
224              * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
225              * initialization) */
226             if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
227                 color_shift = 0;
228             else
229                 color_shift = 2;
230             /* set up the palette */
231             color_packets = bytestream2_get_le16(&g2);
232             palette_ptr = 0;
233             for (i = 0; i < color_packets; i++) {
234                 /* first byte is how many colors to skip */
235                 palette_ptr += bytestream2_get_byte(&g2);
236
237                 /* next byte indicates how many entries to change */
238                 color_changes = bytestream2_get_byte(&g2);
239
240                 /* if there are 0 color changes, there are actually 256 */
241                 if (color_changes == 0)
242                     color_changes = 256;
243
244                 if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
245                     break;
246
247                 for (j = 0; j < color_changes; j++) {
248                     unsigned int entry;
249
250                     /* wrap around, for good measure */
251                     if ((unsigned)palette_ptr >= 256)
252                         palette_ptr = 0;
253
254                     r = bytestream2_get_byte(&g2) << color_shift;
255                     g = bytestream2_get_byte(&g2) << color_shift;
256                     b = bytestream2_get_byte(&g2) << color_shift;
257                     entry = 0xFFU << 24 | r << 16 | g << 8 | b;
258                     if (color_shift == 2)
259                         entry |= entry >> 6 & 0x30303;
260                     if (s->palette[palette_ptr] != entry)
261                         s->new_palette = 1;
262                     s->palette[palette_ptr++] = entry;
263                 }
264             }
265             break;
266
267         case FLI_DELTA:
268             y_ptr = 0;
269             compressed_lines = bytestream2_get_le16(&g2);
270             while (compressed_lines > 0) {
271                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
272                     break;
273                 line_packets = bytestream2_get_le16(&g2);
274                 if ((line_packets & 0xC000) == 0xC000) {
275                     // line skip opcode
276                     line_packets = -line_packets;
277                     y_ptr += line_packets * s->frame->linesize[0];
278                 } else if ((line_packets & 0xC000) == 0x4000) {
279                     av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
280                 } else if ((line_packets & 0xC000) == 0x8000) {
281                     // "last byte" opcode
282                     pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
283                     CHECK_PIXEL_PTR(0);
284                     pixels[pixel_ptr] = line_packets & 0xff;
285                 } else {
286                     compressed_lines--;
287                     pixel_ptr = y_ptr;
288                     CHECK_PIXEL_PTR(0);
289                     pixel_countdown = s->avctx->width;
290                     for (i = 0; i < line_packets; i++) {
291                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
292                             break;
293                         /* account for the skip bytes */
294                         pixel_skip = bytestream2_get_byte(&g2);
295                         pixel_ptr += pixel_skip;
296                         pixel_countdown -= pixel_skip;
297                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
298                         if (byte_run < 0) {
299                             byte_run = -byte_run;
300                             palette_idx1 = bytestream2_get_byte(&g2);
301                             palette_idx2 = bytestream2_get_byte(&g2);
302                             CHECK_PIXEL_PTR(byte_run * 2);
303                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
304                                 pixels[pixel_ptr++] = palette_idx1;
305                                 pixels[pixel_ptr++] = palette_idx2;
306                             }
307                         } else {
308                             CHECK_PIXEL_PTR(byte_run * 2);
309                             if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
310                                 break;
311                             for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
312                                 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
313                             }
314                         }
315                     }
316
317                     y_ptr += s->frame->linesize[0];
318                 }
319             }
320             break;
321
322         case FLI_LC:
323             /* line compressed */
324             starting_line = bytestream2_get_le16(&g2);
325             y_ptr = 0;
326             y_ptr += starting_line * s->frame->linesize[0];
327
328             compressed_lines = bytestream2_get_le16(&g2);
329             while (compressed_lines > 0) {
330                 pixel_ptr = y_ptr;
331                 CHECK_PIXEL_PTR(0);
332                 pixel_countdown = s->avctx->width;
333                 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
334                     break;
335                 line_packets = bytestream2_get_byte(&g2);
336                 if (line_packets > 0) {
337                     for (i = 0; i < line_packets; i++) {
338                         /* account for the skip bytes */
339                         if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
340                             break;
341                         pixel_skip = bytestream2_get_byte(&g2);
342                         pixel_ptr += pixel_skip;
343                         pixel_countdown -= pixel_skip;
344                         byte_run = sign_extend(bytestream2_get_byte(&g2),8);
345                         if (byte_run > 0) {
346                             CHECK_PIXEL_PTR(byte_run);
347                             if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
348                                 break;
349                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
350                                 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
351                             }
352                         } else if (byte_run < 0) {
353                             byte_run = -byte_run;
354                             palette_idx1 = bytestream2_get_byte(&g2);
355                             CHECK_PIXEL_PTR(byte_run);
356                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
357                                 pixels[pixel_ptr++] = palette_idx1;
358                             }
359                         }
360                     }
361                 }
362
363                 y_ptr += s->frame->linesize[0];
364                 compressed_lines--;
365             }
366             break;
367
368         case FLI_BLACK:
369             /* set the whole frame to color 0 (which is usually black) */
370             memset(pixels, 0,
371                 s->frame->linesize[0] * s->avctx->height);
372             break;
373
374         case FLI_BRUN:
375             /* Byte run compression: This chunk type only occurs in the first
376              * FLI frame and it will update the entire frame. */
377             y_ptr = 0;
378             for (lines = 0; lines < s->avctx->height; lines++) {
379                 pixel_ptr = y_ptr;
380                 /* disregard the line packets; instead, iterate through all
381                  * pixels on a row */
382                  bytestream2_skip(&g2, 1);
383                 pixel_countdown = s->avctx->width;
384                 while (pixel_countdown > 0) {
385                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
386                         break;
387                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
388                     if (!byte_run) {
389                         av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
390                         return AVERROR_INVALIDDATA;
391                     }
392
393                     if (byte_run > 0) {
394                         palette_idx1 = bytestream2_get_byte(&g2);
395                         CHECK_PIXEL_PTR(byte_run);
396                         for (j = 0; j < byte_run; j++) {
397                             pixels[pixel_ptr++] = palette_idx1;
398                             pixel_countdown--;
399                             if (pixel_countdown < 0)
400                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
401                                        pixel_countdown, lines);
402                         }
403                     } else {  /* copy bytes if byte_run < 0 */
404                         byte_run = -byte_run;
405                         CHECK_PIXEL_PTR(byte_run);
406                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
407                             break;
408                         for (j = 0; j < byte_run; j++) {
409                             pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
410                             pixel_countdown--;
411                             if (pixel_countdown < 0)
412                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
413                                        pixel_countdown, lines);
414                         }
415                     }
416                 }
417
418                 y_ptr += s->frame->linesize[0];
419             }
420             break;
421
422         case FLI_COPY:
423             /* copy the chunk (uncompressed frame) */
424             if (chunk_size - 6 != FFALIGN(s->avctx->width, 4) * s->avctx->height) {
425                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
426                        "has incorrect size, skipping chunk\n", chunk_size - 6);
427                 bytestream2_skip(&g2, chunk_size - 6);
428             } else {
429                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
430                      y_ptr += s->frame->linesize[0]) {
431                     bytestream2_get_buffer(&g2, &pixels[y_ptr],
432                                            s->avctx->width);
433                     if (s->avctx->width & 3)
434                         bytestream2_skip(&g2, 4 - (s->avctx->width & 3));
435                 }
436             }
437             break;
438
439         case FLI_MINI:
440             /* some sort of a thumbnail? disregard this chunk... */
441             break;
442
443         default:
444             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
445             break;
446         }
447
448         if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
449             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
450
451         frame_size -= chunk_size;
452         num_chunks--;
453     }
454
455     /* by the end of the chunk, the stream ptr should equal the frame
456      * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
457     if (bytestream2_get_bytes_left(&g2) > 2)
458         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
459                "and final chunk ptr = %d\n", buf_size,
460                buf_size - bytestream2_get_bytes_left(&g2));
461
462     /* make the palette available on the way out */
463     memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
464     if (s->new_palette) {
465         s->frame->palette_has_changed = 1;
466         s->new_palette = 0;
467     }
468
469     if ((ret = av_frame_ref(data, s->frame)) < 0)
470         return ret;
471
472     *got_frame = 1;
473
474     return buf_size;
475 }
476
477 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
478                                       void *data, int *got_frame,
479                                       const uint8_t *buf, int buf_size)
480 {
481     /* Note, the only difference between the 15Bpp and 16Bpp */
482     /* Format is the pixel format, the packets are processed the same. */
483     FlicDecodeContext *s = avctx->priv_data;
484
485     GetByteContext g2;
486     int pixel_ptr;
487     unsigned char palette_idx1;
488
489     unsigned int frame_size;
490     int num_chunks;
491
492     unsigned int chunk_size;
493     int chunk_type;
494
495     int i, j, ret;
496
497     int lines;
498     int compressed_lines;
499     signed short line_packets;
500     int y_ptr;
501     int byte_run;
502     int pixel_skip;
503     int pixel_countdown;
504     unsigned char *pixels;
505     int pixel;
506     unsigned int pixel_limit;
507
508     bytestream2_init(&g2, buf, buf_size);
509
510     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
511         return ret;
512
513     pixels = s->frame->data[0];
514     pixel_limit = s->avctx->height * s->frame->linesize[0];
515
516     frame_size = bytestream2_get_le32(&g2);
517     bytestream2_skip(&g2, 2);  /* skip the magic number */
518     num_chunks = bytestream2_get_le16(&g2);
519     bytestream2_skip(&g2, 8);  /* skip padding */
520     if (frame_size > buf_size)
521         frame_size = buf_size;
522
523     frame_size -= 16;
524
525     /* iterate through the chunks */
526     while ((frame_size > 0) && (num_chunks > 0) &&
527             bytestream2_get_bytes_left(&g2) >= 4) {
528         int stream_ptr_after_chunk;
529         chunk_size = bytestream2_get_le32(&g2);
530         if (chunk_size > frame_size) {
531             av_log(avctx, AV_LOG_WARNING,
532                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
533             chunk_size = frame_size;
534         }
535         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
536
537         chunk_type = bytestream2_get_le16(&g2);
538
539
540         switch (chunk_type) {
541         case FLI_256_COLOR:
542         case FLI_COLOR:
543             /* For some reason, it seems that non-palettized flics do
544              * include one of these chunks in their first frame.
545              * Why I do not know, it seems rather extraneous. */
546             ff_dlog(avctx,
547                     "Unexpected Palette chunk %d in non-palettized FLC\n",
548                     chunk_type);
549             bytestream2_skip(&g2, chunk_size - 6);
550             break;
551
552         case FLI_DELTA:
553         case FLI_DTA_LC:
554             y_ptr = 0;
555             compressed_lines = bytestream2_get_le16(&g2);
556             while (compressed_lines > 0) {
557                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
558                     break;
559                 line_packets = bytestream2_get_le16(&g2);
560                 if (line_packets < 0) {
561                     line_packets = -line_packets;
562                     y_ptr += line_packets * s->frame->linesize[0];
563                 } else {
564                     compressed_lines--;
565                     pixel_ptr = y_ptr;
566                     CHECK_PIXEL_PTR(0);
567                     pixel_countdown = s->avctx->width;
568                     for (i = 0; i < line_packets; i++) {
569                         /* account for the skip bytes */
570                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
571                             break;
572                         pixel_skip = bytestream2_get_byte(&g2);
573                         pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
574                         pixel_countdown -= pixel_skip;
575                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
576                         if (byte_run < 0) {
577                             byte_run = -byte_run;
578                             pixel    = bytestream2_get_le16(&g2);
579                             CHECK_PIXEL_PTR(2 * byte_run);
580                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
581                                 *((signed short*)(&pixels[pixel_ptr])) = pixel;
582                                 pixel_ptr += 2;
583                             }
584                         } else {
585                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
586                                 break;
587                             CHECK_PIXEL_PTR(2 * byte_run);
588                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
589                                 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
590                                 pixel_ptr += 2;
591                             }
592                         }
593                     }
594
595                     y_ptr += s->frame->linesize[0];
596                 }
597             }
598             break;
599
600         case FLI_LC:
601             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
602             bytestream2_skip(&g2, chunk_size - 6);
603             break;
604
605         case FLI_BLACK:
606             /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
607             memset(pixels, 0x0000,
608                    s->frame->linesize[0] * s->avctx->height);
609             break;
610
611         case FLI_BRUN:
612             y_ptr = 0;
613             for (lines = 0; lines < s->avctx->height; lines++) {
614                 pixel_ptr = y_ptr;
615                 /* disregard the line packets; instead, iterate through all
616                  * pixels on a row */
617                 bytestream2_skip(&g2, 1);
618                 pixel_countdown = (s->avctx->width * 2);
619
620                 while (pixel_countdown > 0) {
621                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
622                         break;
623                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
624                     if (byte_run > 0) {
625                         palette_idx1 = bytestream2_get_byte(&g2);
626                         CHECK_PIXEL_PTR(byte_run);
627                         for (j = 0; j < byte_run; j++) {
628                             pixels[pixel_ptr++] = palette_idx1;
629                             pixel_countdown--;
630                             if (pixel_countdown < 0)
631                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
632                                        pixel_countdown, lines);
633                         }
634                     } else {  /* copy bytes if byte_run < 0 */
635                         byte_run = -byte_run;
636                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
637                             break;
638                         CHECK_PIXEL_PTR(byte_run);
639                         for (j = 0; j < byte_run; j++) {
640                             palette_idx1 = bytestream2_get_byte(&g2);
641                             pixels[pixel_ptr++] = palette_idx1;
642                             pixel_countdown--;
643                             if (pixel_countdown < 0)
644                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
645                                        pixel_countdown, lines);
646                         }
647                     }
648                 }
649
650                 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
651                  * This does not give us any good opportunity to perform word endian conversion
652                  * during decompression. So if it is required (i.e., this is not a LE target, we do
653                  * a second pass over the line here, swapping the bytes.
654                  */
655 #if HAVE_BIGENDIAN
656                 pixel_ptr = y_ptr;
657                 pixel_countdown = s->avctx->width;
658                 while (pixel_countdown > 0) {
659                     *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
660                     pixel_ptr += 2;
661                 }
662 #endif
663                 y_ptr += s->frame->linesize[0];
664             }
665             break;
666
667         case FLI_DTA_BRUN:
668             y_ptr = 0;
669             for (lines = 0; lines < s->avctx->height; lines++) {
670                 pixel_ptr = y_ptr;
671                 /* disregard the line packets; instead, iterate through all
672                  * pixels on a row */
673                 bytestream2_skip(&g2, 1);
674                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
675
676                 while (pixel_countdown > 0) {
677                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
678                         break;
679                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
680                     if (byte_run > 0) {
681                         pixel    = bytestream2_get_le16(&g2);
682                         CHECK_PIXEL_PTR(2 * byte_run);
683                         for (j = 0; j < byte_run; j++) {
684                             *((signed short*)(&pixels[pixel_ptr])) = pixel;
685                             pixel_ptr += 2;
686                             pixel_countdown--;
687                             if (pixel_countdown < 0)
688                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
689                                        pixel_countdown);
690                         }
691                     } else {  /* copy pixels if byte_run < 0 */
692                         byte_run = -byte_run;
693                         if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
694                             break;
695                         CHECK_PIXEL_PTR(2 * byte_run);
696                         for (j = 0; j < byte_run; j++) {
697                             *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
698                             pixel_ptr  += 2;
699                             pixel_countdown--;
700                             if (pixel_countdown < 0)
701                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
702                                        pixel_countdown);
703                         }
704                     }
705                 }
706
707                 y_ptr += s->frame->linesize[0];
708             }
709             break;
710
711         case FLI_COPY:
712         case FLI_DTA_COPY:
713             /* copy the chunk (uncompressed frame) */
714             if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
715                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
716                        "bigger than image, skipping chunk\n", chunk_size - 6);
717                 bytestream2_skip(&g2, chunk_size - 6);
718             } else {
719
720                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
721                      y_ptr += s->frame->linesize[0]) {
722
723                     pixel_countdown = s->avctx->width;
724                     pixel_ptr = 0;
725                     while (pixel_countdown > 0) {
726                       *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
727                       pixel_ptr += 2;
728                       pixel_countdown--;
729                     }
730                     if (s->avctx->width & 1)
731                         bytestream2_skip(&g2, 2);
732                 }
733             }
734             break;
735
736         case FLI_MINI:
737             /* some sort of a thumbnail? disregard this chunk... */
738             bytestream2_skip(&g2, chunk_size - 6);
739             break;
740
741         default:
742             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
743             break;
744         }
745
746         frame_size -= chunk_size;
747         num_chunks--;
748     }
749
750     /* by the end of the chunk, the stream ptr should equal the frame
751      * size (minus 1, possibly); if it doesn't, issue a warning */
752     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
753         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
754                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
755
756     if ((ret = av_frame_ref(data, s->frame)) < 0)
757         return ret;
758
759     *got_frame = 1;
760
761     return buf_size;
762 }
763
764 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
765                                    void *data, int *got_frame,
766                                    const uint8_t *buf, int buf_size)
767 {
768     FlicDecodeContext *s = avctx->priv_data;
769
770     GetByteContext g2;
771     int pixel_ptr;
772     unsigned char palette_idx1;
773
774     unsigned int frame_size;
775     int num_chunks;
776
777     unsigned int chunk_size;
778     int chunk_type;
779
780     int i, j, ret;
781
782     int lines;
783     int compressed_lines;
784     signed short line_packets;
785     int y_ptr;
786     int byte_run;
787     int pixel_skip;
788     int pixel_countdown;
789     unsigned char *pixels;
790     int pixel;
791     unsigned int pixel_limit;
792
793     bytestream2_init(&g2, buf, buf_size);
794
795     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
796         return ret;
797
798     pixels = s->frame->data[0];
799     pixel_limit = s->avctx->height * s->frame->linesize[0];
800
801     frame_size = bytestream2_get_le32(&g2);
802     bytestream2_skip(&g2, 2);  /* skip the magic number */
803     num_chunks = bytestream2_get_le16(&g2);
804     bytestream2_skip(&g2, 8);  /* skip padding */
805     if (frame_size > buf_size)
806         frame_size = buf_size;
807
808     frame_size -= 16;
809
810     /* iterate through the chunks */
811     while ((frame_size > 0) && (num_chunks > 0) &&
812             bytestream2_get_bytes_left(&g2) >= 4) {
813         int stream_ptr_after_chunk;
814         chunk_size = bytestream2_get_le32(&g2);
815         if (chunk_size > frame_size) {
816             av_log(avctx, AV_LOG_WARNING,
817                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
818             chunk_size = frame_size;
819         }
820         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
821
822         chunk_type = bytestream2_get_le16(&g2);
823
824
825         switch (chunk_type) {
826         case FLI_256_COLOR:
827         case FLI_COLOR:
828             /* For some reason, it seems that non-palettized flics do
829              * include one of these chunks in their first frame.
830              * Why I do not know, it seems rather extraneous. */
831             ff_dlog(avctx,
832                     "Unexpected Palette chunk %d in non-palettized FLC\n",
833                     chunk_type);
834             bytestream2_skip(&g2, chunk_size - 6);
835             break;
836
837         case FLI_DELTA:
838         case FLI_DTA_LC:
839             y_ptr = 0;
840             compressed_lines = bytestream2_get_le16(&g2);
841             while (compressed_lines > 0) {
842                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
843                     break;
844                 line_packets = bytestream2_get_le16(&g2);
845                 if (line_packets < 0) {
846                     line_packets = -line_packets;
847                     y_ptr += line_packets * s->frame->linesize[0];
848                 } else {
849                     compressed_lines--;
850                     pixel_ptr = y_ptr;
851                     CHECK_PIXEL_PTR(0);
852                     pixel_countdown = s->avctx->width;
853                     for (i = 0; i < line_packets; i++) {
854                         /* account for the skip bytes */
855                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
856                             break;
857                         pixel_skip = bytestream2_get_byte(&g2);
858                         pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
859                         pixel_countdown -= pixel_skip;
860                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
861                         if (byte_run < 0) {
862                             byte_run = -byte_run;
863                             pixel    = bytestream2_get_le24(&g2);
864                             CHECK_PIXEL_PTR(3 * byte_run);
865                             for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
866                                 AV_WL24(&pixels[pixel_ptr], pixel);
867                                 pixel_ptr += 3;
868                             }
869                         } else {
870                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
871                                 break;
872                             CHECK_PIXEL_PTR(2 * byte_run);
873                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
874                                 pixel = bytestream2_get_le24(&g2);
875                                 AV_WL24(&pixels[pixel_ptr], pixel);
876                                 pixel_ptr += 3;
877                             }
878                         }
879                     }
880
881                     y_ptr += s->frame->linesize[0];
882                 }
883             }
884             break;
885
886         case FLI_LC:
887             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
888             bytestream2_skip(&g2, chunk_size - 6);
889             break;
890
891         case FLI_BLACK:
892             /* set the whole frame to 0x00 which is black for 24 bit mode. */
893             memset(pixels, 0x00,
894                    s->frame->linesize[0] * s->avctx->height);
895             break;
896
897         case FLI_BRUN:
898             y_ptr = 0;
899             for (lines = 0; lines < s->avctx->height; lines++) {
900                 pixel_ptr = y_ptr;
901                 /* disregard the line packets; instead, iterate through all
902                  * pixels on a row */
903                 bytestream2_skip(&g2, 1);
904                 pixel_countdown = (s->avctx->width * 3);
905
906                 while (pixel_countdown > 0) {
907                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
908                         break;
909                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
910                     if (byte_run > 0) {
911                         palette_idx1 = bytestream2_get_byte(&g2);
912                         CHECK_PIXEL_PTR(byte_run);
913                         for (j = 0; j < byte_run; j++) {
914                             pixels[pixel_ptr++] = palette_idx1;
915                             pixel_countdown--;
916                             if (pixel_countdown < 0)
917                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
918                                        pixel_countdown, lines);
919                         }
920                     } else {  /* copy bytes if byte_run < 0 */
921                         byte_run = -byte_run;
922                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
923                             break;
924                         CHECK_PIXEL_PTR(byte_run);
925                         for (j = 0; j < byte_run; j++) {
926                             palette_idx1 = bytestream2_get_byte(&g2);
927                             pixels[pixel_ptr++] = palette_idx1;
928                             pixel_countdown--;
929                             if (pixel_countdown < 0)
930                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
931                                        pixel_countdown, lines);
932                         }
933                     }
934                 }
935
936                 y_ptr += s->frame->linesize[0];
937             }
938             break;
939
940         case FLI_DTA_BRUN:
941             y_ptr = 0;
942             for (lines = 0; lines < s->avctx->height; lines++) {
943                 pixel_ptr = y_ptr;
944                 /* disregard the line packets; instead, iterate through all
945                  * pixels on a row */
946                 bytestream2_skip(&g2, 1);
947                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
948
949                 while (pixel_countdown > 0) {
950                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
951                         break;
952                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
953                     if (byte_run > 0) {
954                         pixel = bytestream2_get_le24(&g2);
955                         CHECK_PIXEL_PTR(3 * byte_run);
956                         for (j = 0; j < byte_run; j++) {
957                             AV_WL24(pixels + pixel_ptr, pixel);
958                             pixel_ptr += 3;
959                             pixel_countdown--;
960                             if (pixel_countdown < 0)
961                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
962                                        pixel_countdown);
963                         }
964                     } else {  /* copy pixels if byte_run < 0 */
965                         byte_run = -byte_run;
966                         if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
967                             break;
968                         CHECK_PIXEL_PTR(3 * byte_run);
969                         for (j = 0; j < byte_run; j++) {
970                             pixel = bytestream2_get_le24(&g2);
971                             AV_WL24(pixels + pixel_ptr, pixel);
972                             pixel_ptr  += 3;
973                             pixel_countdown--;
974                             if (pixel_countdown < 0)
975                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
976                                        pixel_countdown);
977                         }
978                     }
979                 }
980
981                 y_ptr += s->frame->linesize[0];
982             }
983             break;
984
985         case FLI_COPY:
986         case FLI_DTA_COPY:
987             /* copy the chunk (uncompressed frame) */
988             if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
989                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
990                        "bigger than image, skipping chunk\n", chunk_size - 6);
991                 bytestream2_skip(&g2, chunk_size - 6);
992             } else {
993                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
994                      y_ptr += s->frame->linesize[0]) {
995
996                     pixel_countdown = s->avctx->width;
997                     pixel_ptr = 0;
998                     while (pixel_countdown > 0) {
999                         pixel = bytestream2_get_le24(&g2);
1000                         AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
1001                         pixel_ptr += 3;
1002                         pixel_countdown--;
1003                     }
1004                     if (s->avctx->width & 1)
1005                         bytestream2_skip(&g2, 3);
1006                 }
1007             }
1008             break;
1009
1010         case FLI_MINI:
1011             /* some sort of a thumbnail? disregard this chunk... */
1012             bytestream2_skip(&g2, chunk_size - 6);
1013             break;
1014
1015         default:
1016             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
1017             break;
1018         }
1019
1020         frame_size -= chunk_size;
1021         num_chunks--;
1022     }
1023
1024     /* by the end of the chunk, the stream ptr should equal the frame
1025      * size (minus 1, possibly); if it doesn't, issue a warning */
1026     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
1027         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
1028                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
1029
1030     if ((ret = av_frame_ref(data, s->frame)) < 0)
1031         return ret;
1032
1033     *got_frame = 1;
1034
1035     return buf_size;
1036 }
1037
1038 static int flic_decode_frame(AVCodecContext *avctx,
1039                              void *data, int *got_frame,
1040                              AVPacket *avpkt)
1041 {
1042     const uint8_t *buf = avpkt->data;
1043     int buf_size = avpkt->size;
1044     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1045         return flic_decode_frame_8BPP(avctx, data, got_frame,
1046                                       buf, buf_size);
1047     } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
1048                (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
1049         return flic_decode_frame_15_16BPP(avctx, data, got_frame,
1050                                           buf, buf_size);
1051     } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
1052         return flic_decode_frame_24BPP(avctx, data, got_frame,
1053                                        buf, buf_size);
1054     }
1055
1056     /* Should not get  here, ever as the pix_fmt is processed */
1057     /* in flic_decode_init and the above if should deal with */
1058     /* the finite set of possibilities allowable by here. */
1059     /* But in case we do, just error out. */
1060     av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
1061     return AVERROR_BUG;
1062 }
1063
1064
1065 static av_cold int flic_decode_end(AVCodecContext *avctx)
1066 {
1067     FlicDecodeContext *s = avctx->priv_data;
1068
1069     av_frame_free(&s->frame);
1070
1071     return 0;
1072 }
1073
1074 AVCodec ff_flic_decoder = {
1075     .name           = "flic",
1076     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
1077     .type           = AVMEDIA_TYPE_VIDEO,
1078     .id             = AV_CODEC_ID_FLIC,
1079     .priv_data_size = sizeof(FlicDecodeContext),
1080     .init           = flic_decode_init,
1081     .close          = flic_decode_end,
1082     .decode         = flic_decode_frame,
1083     .capabilities   = AV_CODEC_CAP_DR1,
1084 };