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