]> git.sesse.net Git - ffmpeg/blob - libavcodec/flicvideo.c
Merge remote-tracking branch 'qatar/master'
[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 != 904 &&
90         avctx->extradata_size != 1024) {
91         av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
92         return AVERROR_INVALIDDATA;
93     }
94
95     s->avctx = avctx;
96
97     if (s->avctx->extradata_size == 12) {
98         /* special case for magic carpet FLIs */
99         s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
100         depth = 8;
101     } else if (avctx->extradata_size == 1024) {
102         uint8_t *ptr = avctx->extradata;
103         int i;
104
105         for (i = 0; i < 256; i++) {
106             s->palette[i] = AV_RL32(ptr);
107             ptr += 4;
108         }
109         depth = 8;
110         /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
111     } else if (avctx->extradata_size == 0 ||
112         /* see FFmpeg ticket #1234 */
113                avctx->extradata_size == 904) {
114         s->fli_type = FLI_TYPE_CODE;
115         depth = 8;
116     } else {
117         s->fli_type = AV_RL16(&fli_header[4]);
118         depth = AV_RL16(&fli_header[12]);
119     }
120
121     if (depth == 0) {
122         depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
123     }
124
125     if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
126         depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
127     }
128
129     switch (depth) {
130         case 8  : avctx->pix_fmt = PIX_FMT_PAL8; break;
131         case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
132         case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
133         case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
134                   av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
135                   return -1;
136         default :
137                   av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
138                   return -1;
139     }
140
141     avcodec_get_frame_defaults(&s->frame);
142     s->frame.data[0] = NULL;
143     s->new_palette = 0;
144
145     return 0;
146 }
147
148 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
149                                   void *data, int *data_size,
150                                   const uint8_t *buf, int buf_size)
151 {
152     FlicDecodeContext *s = avctx->priv_data;
153
154     GetByteContext g2;
155     int pixel_ptr;
156     int palette_ptr;
157     unsigned char palette_idx1;
158     unsigned char palette_idx2;
159
160     unsigned int frame_size;
161     int num_chunks;
162
163     unsigned int chunk_size;
164     int chunk_type;
165
166     int i, j;
167
168     int color_packets;
169     int color_changes;
170     int color_shift;
171     unsigned char r, g, b;
172
173     int lines;
174     int compressed_lines;
175     int starting_line;
176     signed short line_packets;
177     int y_ptr;
178     int byte_run;
179     int pixel_skip;
180     int pixel_countdown;
181     unsigned char *pixels;
182     unsigned int pixel_limit;
183
184     bytestream2_init(&g2, buf, buf_size);
185
186     s->frame.reference = 3;
187     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
188     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
189         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
190         return -1;
191     }
192
193     pixels = s->frame.data[0];
194     pixel_limit = s->avctx->height * s->frame.linesize[0];
195     if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
196         return AVERROR_INVALIDDATA;
197     frame_size = bytestream2_get_le32(&g2);
198     if (frame_size > buf_size)
199         frame_size = buf_size;
200     bytestream2_skip(&g2, 2); /* skip the magic number */
201     num_chunks = bytestream2_get_le16(&g2);
202     bytestream2_skip(&g2, 8);  /* skip padding */
203
204     frame_size -= 16;
205
206     /* iterate through the chunks */
207     while ((frame_size >= 6) && (num_chunks > 0)) {
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 = 0xFF << 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 > 0) {
389                         palette_idx1 = bytestream2_get_byte(&g2);
390                         CHECK_PIXEL_PTR(byte_run);
391                         for (j = 0; j < byte_run; j++) {
392                             pixels[pixel_ptr++] = palette_idx1;
393                             pixel_countdown--;
394                             if (pixel_countdown < 0)
395                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
396                                        pixel_countdown, lines);
397                         }
398                     } else {  /* copy bytes if byte_run < 0 */
399                         byte_run = -byte_run;
400                         CHECK_PIXEL_PTR(byte_run);
401                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
402                             break;
403                         for (j = 0; j < byte_run; j++) {
404                             pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
405                             pixel_countdown--;
406                             if (pixel_countdown < 0)
407                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
408                                        pixel_countdown, lines);
409                         }
410                     }
411                 }
412
413                 y_ptr += s->frame.linesize[0];
414             }
415             break;
416
417         case FLI_COPY:
418             /* copy the chunk (uncompressed frame) */
419             if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
420                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
421                        "has incorrect size, skipping chunk\n", chunk_size - 6);
422                 bytestream2_skip(&g2, chunk_size - 6);
423             } else {
424                 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
425                      y_ptr += s->frame.linesize[0]) {
426                     bytestream2_get_buffer(&g2, &pixels[y_ptr],
427                                            s->avctx->width);
428                 }
429             }
430             break;
431
432         case FLI_MINI:
433             /* some sort of a thumbnail? disregard this chunk... */
434             break;
435
436         default:
437             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
438             break;
439         }
440
441         if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
442             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
443
444         frame_size -= chunk_size;
445         num_chunks--;
446     }
447
448     /* by the end of the chunk, the stream ptr should equal the frame
449      * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
450     if (bytestream2_get_bytes_left(&g2) > 2)
451         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
452                "and final chunk ptr = %d\n", buf_size,
453                buf_size - bytestream2_get_bytes_left(&g2));
454
455     /* make the palette available on the way out */
456     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
457     if (s->new_palette) {
458         s->frame.palette_has_changed = 1;
459         s->new_palette = 0;
460     }
461
462     *data_size=sizeof(AVFrame);
463     *(AVFrame*)data = s->frame;
464
465     return buf_size;
466 }
467
468 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
469                                       void *data, int *data_size,
470                                       const uint8_t *buf, int buf_size)
471 {
472     /* Note, the only difference between the 15Bpp and 16Bpp */
473     /* Format is the pixel format, the packets are processed the same. */
474     FlicDecodeContext *s = avctx->priv_data;
475
476     GetByteContext g2;
477     int pixel_ptr;
478     unsigned char palette_idx1;
479
480     unsigned int frame_size;
481     int num_chunks;
482
483     unsigned int chunk_size;
484     int chunk_type;
485
486     int i, j;
487
488     int lines;
489     int compressed_lines;
490     signed short line_packets;
491     int y_ptr;
492     int byte_run;
493     int pixel_skip;
494     int pixel_countdown;
495     unsigned char *pixels;
496     int pixel;
497     unsigned int pixel_limit;
498
499     bytestream2_init(&g2, buf, buf_size);
500
501     s->frame.reference = 3;
502     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
503     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
504         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
505         return -1;
506     }
507
508     pixels = s->frame.data[0];
509     pixel_limit = s->avctx->height * s->frame.linesize[0];
510
511     frame_size = bytestream2_get_le32(&g2);
512     bytestream2_skip(&g2, 2);  /* skip the magic number */
513     num_chunks = bytestream2_get_le16(&g2);
514     bytestream2_skip(&g2, 8);  /* skip padding */
515     if (frame_size > buf_size)
516         frame_size = buf_size;
517
518     frame_size -= 16;
519
520     /* iterate through the chunks */
521     while ((frame_size > 0) && (num_chunks > 0)) {
522         int stream_ptr_after_chunk;
523         chunk_size = bytestream2_get_le32(&g2);
524         if (chunk_size > frame_size) {
525             av_log(avctx, AV_LOG_WARNING,
526                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
527             chunk_size = frame_size;
528         }
529         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
530
531         chunk_type = bytestream2_get_le16(&g2);
532
533
534         switch (chunk_type) {
535         case FLI_256_COLOR:
536         case FLI_COLOR:
537             /* For some reason, it seems that non-palettized flics do
538              * include one of these chunks in their first frame.
539              * Why I do not know, it seems rather extraneous. */
540 /*            av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
541             bytestream2_skip(&g2, chunk_size - 6);
542             break;
543
544         case FLI_DELTA:
545         case FLI_DTA_LC:
546             y_ptr = 0;
547             compressed_lines = bytestream2_get_le16(&g2);
548             while (compressed_lines > 0) {
549                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
550                     break;
551                 line_packets = bytestream2_get_le16(&g2);
552                 if (line_packets < 0) {
553                     line_packets = -line_packets;
554                     y_ptr += line_packets * s->frame.linesize[0];
555                 } else {
556                     compressed_lines--;
557                     pixel_ptr = y_ptr;
558                     CHECK_PIXEL_PTR(0);
559                     pixel_countdown = s->avctx->width;
560                     for (i = 0; i < line_packets; i++) {
561                         /* account for the skip bytes */
562                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
563                             break;
564                         pixel_skip = bytestream2_get_byte(&g2);
565                         pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
566                         pixel_countdown -= pixel_skip;
567                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
568                         if (byte_run < 0) {
569                             byte_run = -byte_run;
570                             pixel    = bytestream2_get_le16(&g2);
571                             CHECK_PIXEL_PTR(2 * byte_run);
572                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
573                                 *((signed short*)(&pixels[pixel_ptr])) = pixel;
574                                 pixel_ptr += 2;
575                             }
576                         } else {
577                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
578                                 break;
579                             CHECK_PIXEL_PTR(2 * byte_run);
580                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
581                                 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
582                                 pixel_ptr += 2;
583                             }
584                         }
585                     }
586
587                     y_ptr += s->frame.linesize[0];
588                 }
589             }
590             break;
591
592         case FLI_LC:
593             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
594             bytestream2_skip(&g2, chunk_size - 6);
595             break;
596
597         case FLI_BLACK:
598             /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
599             memset(pixels, 0x0000,
600                    s->frame.linesize[0] * s->avctx->height);
601             break;
602
603         case FLI_BRUN:
604             y_ptr = 0;
605             for (lines = 0; lines < s->avctx->height; lines++) {
606                 pixel_ptr = y_ptr;
607                 /* disregard the line packets; instead, iterate through all
608                  * pixels on a row */
609                 bytestream2_skip(&g2, 1);
610                 pixel_countdown = (s->avctx->width * 2);
611
612                 while (pixel_countdown > 0) {
613                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
614                         break;
615                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
616                     if (byte_run > 0) {
617                         palette_idx1 = bytestream2_get_byte(&g2);
618                         CHECK_PIXEL_PTR(byte_run);
619                         for (j = 0; j < byte_run; j++) {
620                             pixels[pixel_ptr++] = palette_idx1;
621                             pixel_countdown--;
622                             if (pixel_countdown < 0)
623                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
624                                        pixel_countdown, lines);
625                         }
626                     } else {  /* copy bytes if byte_run < 0 */
627                         byte_run = -byte_run;
628                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
629                             break;
630                         CHECK_PIXEL_PTR(byte_run);
631                         for (j = 0; j < byte_run; j++) {
632                             palette_idx1 = bytestream2_get_byte(&g2);
633                             pixels[pixel_ptr++] = palette_idx1;
634                             pixel_countdown--;
635                             if (pixel_countdown < 0)
636                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
637                                        pixel_countdown, lines);
638                         }
639                     }
640                 }
641
642                 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
643                  * This does not give us any good oportunity to perform word endian conversion
644                  * during decompression. So if it is required (i.e., this is not a LE target, we do
645                  * a second pass over the line here, swapping the bytes.
646                  */
647 #if HAVE_BIGENDIAN
648                 pixel_ptr = y_ptr;
649                 pixel_countdown = s->avctx->width;
650                 while (pixel_countdown > 0) {
651                     *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
652                     pixel_ptr += 2;
653                 }
654 #endif
655                 y_ptr += s->frame.linesize[0];
656             }
657             break;
658
659         case FLI_DTA_BRUN:
660             y_ptr = 0;
661             for (lines = 0; lines < s->avctx->height; lines++) {
662                 pixel_ptr = y_ptr;
663                 /* disregard the line packets; instead, iterate through all
664                  * pixels on a row */
665                 bytestream2_skip(&g2, 1);
666                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
667
668                 while (pixel_countdown > 0) {
669                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
670                         break;
671                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
672                     if (byte_run > 0) {
673                         pixel    = bytestream2_get_le16(&g2);
674                         CHECK_PIXEL_PTR(2 * byte_run);
675                         for (j = 0; j < byte_run; j++) {
676                             *((signed short*)(&pixels[pixel_ptr])) = pixel;
677                             pixel_ptr += 2;
678                             pixel_countdown--;
679                             if (pixel_countdown < 0)
680                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
681                                        pixel_countdown);
682                         }
683                     } else {  /* copy pixels if byte_run < 0 */
684                         byte_run = -byte_run;
685                         if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
686                             break;
687                         CHECK_PIXEL_PTR(2 * byte_run);
688                         for (j = 0; j < byte_run; j++) {
689                             *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
690                             pixel_ptr  += 2;
691                             pixel_countdown--;
692                             if (pixel_countdown < 0)
693                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
694                                        pixel_countdown);
695                         }
696                     }
697                 }
698
699                 y_ptr += s->frame.linesize[0];
700             }
701             break;
702
703         case FLI_COPY:
704         case FLI_DTA_COPY:
705             /* copy the chunk (uncompressed frame) */
706             if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
707                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
708                        "bigger than image, skipping chunk\n", chunk_size - 6);
709                 bytestream2_skip(&g2, chunk_size - 6);
710             } else {
711
712                 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
713                      y_ptr += s->frame.linesize[0]) {
714
715                     pixel_countdown = s->avctx->width;
716                     pixel_ptr = 0;
717                     while (pixel_countdown > 0) {
718                       *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
719                       pixel_ptr += 2;
720                       pixel_countdown--;
721                     }
722                 }
723             }
724             break;
725
726         case FLI_MINI:
727             /* some sort of a thumbnail? disregard this chunk... */
728             bytestream2_skip(&g2, chunk_size - 6);
729             break;
730
731         default:
732             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
733             break;
734         }
735
736         frame_size -= chunk_size;
737         num_chunks--;
738     }
739
740     /* by the end of the chunk, the stream ptr should equal the frame
741      * size (minus 1, possibly); if it doesn't, issue a warning */
742     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
743         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
744                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
745
746
747     *data_size=sizeof(AVFrame);
748     *(AVFrame*)data = s->frame;
749
750     return buf_size;
751 }
752
753 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
754                                    void *data, int *data_size,
755                                    const uint8_t *buf, int buf_size)
756 {
757   av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
758   return -1;
759 }
760
761 static int flic_decode_frame(AVCodecContext *avctx,
762                              void *data, int *data_size,
763                              AVPacket *avpkt)
764 {
765     const uint8_t *buf = avpkt->data;
766     int buf_size = avpkt->size;
767     if (avctx->pix_fmt == PIX_FMT_PAL8) {
768       return flic_decode_frame_8BPP(avctx, data, data_size,
769                                     buf, buf_size);
770     }
771     else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
772              (avctx->pix_fmt == PIX_FMT_RGB565)) {
773       return flic_decode_frame_15_16BPP(avctx, data, data_size,
774                                         buf, buf_size);
775     }
776     else if (avctx->pix_fmt == PIX_FMT_BGR24) {
777       return flic_decode_frame_24BPP(avctx, data, data_size,
778                                      buf, buf_size);
779     }
780
781     /* Should not get  here, ever as the pix_fmt is processed */
782     /* in flic_decode_init and the above if should deal with */
783     /* the finite set of possibilites allowable by here. */
784     /* But in case we do, just error out. */
785     av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
786     return -1;
787 }
788
789
790 static av_cold int flic_decode_end(AVCodecContext *avctx)
791 {
792     FlicDecodeContext *s = avctx->priv_data;
793
794     if (s->frame.data[0])
795         avctx->release_buffer(avctx, &s->frame);
796
797     return 0;
798 }
799
800 AVCodec ff_flic_decoder = {
801     .name           = "flic",
802     .type           = AVMEDIA_TYPE_VIDEO,
803     .id             = CODEC_ID_FLIC,
804     .priv_data_size = sizeof(FlicDecodeContext),
805     .init           = flic_decode_init,
806     .close          = flic_decode_end,
807     .decode         = flic_decode_frame,
808     .capabilities   = CODEC_CAP_DR1,
809     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
810 };