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