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