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