]> git.sesse.net Git - ffmpeg/blob - libavcodec/flicvideo.c
Merge commit '00b775dda2b3f78ae60ff3278d3b3d6545883a83'
[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/BGR24. To use this decoder, be
31  * sure that your demuxer sends the FLI file header to the decoder via
32  * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
33  * large. The only exception is for FLI files from the game "Magic Carpet",
34  * in which the header is only 12 bytes.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "libavutil/intreadwrite.h"
42 #include "avcodec.h"
43 #include "bytestream.h"
44 #include "internal.h"
45 #include "mathops.h"
46
47 #define FLI_256_COLOR 4
48 #define FLI_DELTA     7
49 #define FLI_COLOR     11
50 #define FLI_LC        12
51 #define FLI_BLACK     13
52 #define FLI_BRUN      15
53 #define FLI_COPY      16
54 #define FLI_MINI      18
55 #define FLI_DTA_BRUN  25
56 #define FLI_DTA_COPY  26
57 #define FLI_DTA_LC    27
58
59 #define FLI_TYPE_CODE     (0xAF11)
60 #define FLC_FLX_TYPE_CODE (0xAF12)
61 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
62 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
63
64 #define CHECK_PIXEL_PTR(n) \
65     if (pixel_ptr + n > pixel_limit) { \
66         av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
67         pixel_ptr + n, pixel_limit); \
68         return AVERROR_INVALIDDATA; \
69     } \
70
71 typedef struct FlicDecodeContext {
72     AVCodecContext *avctx;
73     AVFrame *frame;
74
75     unsigned int palette[256];
76     int new_palette;
77     int fli_type;  /* either 0xAF11 or 0xAF12, affects palette resolution */
78 } FlicDecodeContext;
79
80 static av_cold int flic_decode_init(AVCodecContext *avctx)
81 {
82     FlicDecodeContext *s = avctx->priv_data;
83     unsigned char *fli_header = (unsigned char *)avctx->extradata;
84     int depth;
85
86     if (avctx->extradata_size != 0 &&
87         avctx->extradata_size != 12 &&
88         avctx->extradata_size != 128 &&
89         avctx->extradata_size != 256 &&
90         avctx->extradata_size != 904 &&
91         avctx->extradata_size != 1024) {
92         av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
93         return AVERROR_INVALIDDATA;
94     }
95
96     s->avctx = avctx;
97
98     if (s->avctx->extradata_size == 12) {
99         /* special case for magic carpet FLIs */
100         s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
101         depth = 8;
102     } else if (avctx->extradata_size == 1024) {
103         uint8_t *ptr = avctx->extradata;
104         int i;
105
106         for (i = 0; i < 256; i++) {
107             s->palette[i] = AV_RL32(ptr);
108             ptr += 4;
109         }
110         depth = 8;
111         /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
112     } else if (avctx->extradata_size == 0 ||
113                avctx->extradata_size == 256 ||
114         /* see FFmpeg ticket #1234 */
115                avctx->extradata_size == 904) {
116         s->fli_type = FLI_TYPE_CODE;
117         depth = 8;
118     } else {
119         s->fli_type = AV_RL16(&fli_header[4]);
120         depth = AV_RL16(&fli_header[12]);
121     }
122
123     if (depth == 0) {
124         depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
125     }
126
127     if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
128         depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
129     }
130
131     switch (depth) {
132         case 8  : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
133         case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
134         case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
135         case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
136         default :
137                   av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
138                   return AVERROR_INVALIDDATA;
139     }
140
141     s->frame = av_frame_alloc();
142     if (!s->frame)
143         return AVERROR(ENOMEM);
144
145     s->new_palette = 0;
146
147     return 0;
148 }
149
150 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
151                                   void *data, int *got_frame,
152                                   const uint8_t *buf, int buf_size)
153 {
154     FlicDecodeContext *s = avctx->priv_data;
155
156     GetByteContext g2;
157     int pixel_ptr;
158     int palette_ptr;
159     unsigned char palette_idx1;
160     unsigned char palette_idx2;
161
162     unsigned int frame_size;
163     int num_chunks;
164
165     unsigned int chunk_size;
166     int chunk_type;
167
168     int i, j, ret;
169
170     int color_packets;
171     int color_changes;
172     int color_shift;
173     unsigned char r, g, b;
174
175     int lines;
176     int compressed_lines;
177     int starting_line;
178     signed short line_packets;
179     int y_ptr;
180     int byte_run;
181     int pixel_skip;
182     int pixel_countdown;
183     unsigned char *pixels;
184     unsigned int pixel_limit;
185
186     bytestream2_init(&g2, buf, buf_size);
187
188     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
189         return ret;
190
191     pixels = s->frame->data[0];
192     pixel_limit = s->avctx->height * s->frame->linesize[0];
193     if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
194         return AVERROR_INVALIDDATA;
195     frame_size = bytestream2_get_le32(&g2);
196     if (frame_size > buf_size)
197         frame_size = buf_size;
198     bytestream2_skip(&g2, 2); /* skip the magic number */
199     num_chunks = bytestream2_get_le16(&g2);
200     bytestream2_skip(&g2, 8);  /* skip padding */
201
202     if (frame_size < 16)
203         return AVERROR_INVALIDDATA;
204
205     frame_size -= 16;
206
207     /* iterate through the chunks */
208     while ((frame_size >= 6) && (num_chunks > 0) &&
209             bytestream2_get_bytes_left(&g2) >= 4) {
210         int stream_ptr_after_chunk;
211         chunk_size = bytestream2_get_le32(&g2);
212         if (chunk_size > frame_size) {
213             av_log(avctx, AV_LOG_WARNING,
214                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
215             chunk_size = frame_size;
216         }
217         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
218
219         chunk_type = bytestream2_get_le16(&g2);
220
221         switch (chunk_type) {
222         case FLI_256_COLOR:
223         case FLI_COLOR:
224             /* check special case: If this file is from the Magic Carpet
225              * game and uses 6-bit colors even though it reports 256-color
226              * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
227              * initialization) */
228             if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
229                 color_shift = 0;
230             else
231                 color_shift = 2;
232             /* set up the palette */
233             color_packets = bytestream2_get_le16(&g2);
234             palette_ptr = 0;
235             for (i = 0; i < color_packets; i++) {
236                 /* first byte is how many colors to skip */
237                 palette_ptr += bytestream2_get_byte(&g2);
238
239                 /* next byte indicates how many entries to change */
240                 color_changes = bytestream2_get_byte(&g2);
241
242                 /* if there are 0 color changes, there are actually 256 */
243                 if (color_changes == 0)
244                     color_changes = 256;
245
246                 if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
247                     break;
248
249                 for (j = 0; j < color_changes; j++) {
250                     unsigned int entry;
251
252                     /* wrap around, for good measure */
253                     if ((unsigned)palette_ptr >= 256)
254                         palette_ptr = 0;
255
256                     r = bytestream2_get_byte(&g2) << color_shift;
257                     g = bytestream2_get_byte(&g2) << color_shift;
258                     b = bytestream2_get_byte(&g2) << color_shift;
259                     entry = 0xFFU << 24 | r << 16 | g << 8 | b;
260                     if (color_shift == 2)
261                         entry |= entry >> 6 & 0x30303;
262                     if (s->palette[palette_ptr] != entry)
263                         s->new_palette = 1;
264                     s->palette[palette_ptr++] = entry;
265                 }
266             }
267             break;
268
269         case FLI_DELTA:
270             y_ptr = 0;
271             compressed_lines = bytestream2_get_le16(&g2);
272             while (compressed_lines > 0) {
273                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
274                     break;
275                 line_packets = bytestream2_get_le16(&g2);
276                 if ((line_packets & 0xC000) == 0xC000) {
277                     // line skip opcode
278                     line_packets = -line_packets;
279                     y_ptr += line_packets * s->frame->linesize[0];
280                 } else if ((line_packets & 0xC000) == 0x4000) {
281                     av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
282                 } else if ((line_packets & 0xC000) == 0x8000) {
283                     // "last byte" opcode
284                     pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
285                     CHECK_PIXEL_PTR(0);
286                     pixels[pixel_ptr] = line_packets & 0xff;
287                 } else {
288                     compressed_lines--;
289                     pixel_ptr = y_ptr;
290                     CHECK_PIXEL_PTR(0);
291                     pixel_countdown = s->avctx->width;
292                     for (i = 0; i < line_packets; i++) {
293                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
294                             break;
295                         /* account for the skip bytes */
296                         pixel_skip = bytestream2_get_byte(&g2);
297                         pixel_ptr += pixel_skip;
298                         pixel_countdown -= pixel_skip;
299                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
300                         if (byte_run < 0) {
301                             byte_run = -byte_run;
302                             palette_idx1 = bytestream2_get_byte(&g2);
303                             palette_idx2 = bytestream2_get_byte(&g2);
304                             CHECK_PIXEL_PTR(byte_run * 2);
305                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
306                                 pixels[pixel_ptr++] = palette_idx1;
307                                 pixels[pixel_ptr++] = palette_idx2;
308                             }
309                         } else {
310                             CHECK_PIXEL_PTR(byte_run * 2);
311                             if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
312                                 break;
313                             for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
314                                 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
315                             }
316                         }
317                     }
318
319                     y_ptr += s->frame->linesize[0];
320                 }
321             }
322             break;
323
324         case FLI_LC:
325             /* line compressed */
326             starting_line = bytestream2_get_le16(&g2);
327             y_ptr = 0;
328             y_ptr += starting_line * s->frame->linesize[0];
329
330             compressed_lines = bytestream2_get_le16(&g2);
331             while (compressed_lines > 0) {
332                 pixel_ptr = y_ptr;
333                 CHECK_PIXEL_PTR(0);
334                 pixel_countdown = s->avctx->width;
335                 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
336                     break;
337                 line_packets = bytestream2_get_byte(&g2);
338                 if (line_packets > 0) {
339                     for (i = 0; i < line_packets; i++) {
340                         /* account for the skip bytes */
341                         if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
342                             break;
343                         pixel_skip = bytestream2_get_byte(&g2);
344                         pixel_ptr += pixel_skip;
345                         pixel_countdown -= pixel_skip;
346                         byte_run = sign_extend(bytestream2_get_byte(&g2),8);
347                         if (byte_run > 0) {
348                             CHECK_PIXEL_PTR(byte_run);
349                             if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
350                                 break;
351                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
352                                 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
353                             }
354                         } else if (byte_run < 0) {
355                             byte_run = -byte_run;
356                             palette_idx1 = bytestream2_get_byte(&g2);
357                             CHECK_PIXEL_PTR(byte_run);
358                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
359                                 pixels[pixel_ptr++] = palette_idx1;
360                             }
361                         }
362                     }
363                 }
364
365                 y_ptr += s->frame->linesize[0];
366                 compressed_lines--;
367             }
368             break;
369
370         case FLI_BLACK:
371             /* set the whole frame to color 0 (which is usually black) */
372             memset(pixels, 0,
373                 s->frame->linesize[0] * s->avctx->height);
374             break;
375
376         case FLI_BRUN:
377             /* Byte run compression: This chunk type only occurs in the first
378              * FLI frame and it will update the entire frame. */
379             y_ptr = 0;
380             for (lines = 0; lines < s->avctx->height; lines++) {
381                 pixel_ptr = y_ptr;
382                 /* disregard the line packets; instead, iterate through all
383                  * pixels on a row */
384                  bytestream2_skip(&g2, 1);
385                 pixel_countdown = s->avctx->width;
386                 while (pixel_countdown > 0) {
387                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
388                         break;
389                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
390                     if (!byte_run) {
391                         av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
392                         return AVERROR_INVALIDDATA;
393                     }
394
395                     if (byte_run > 0) {
396                         palette_idx1 = bytestream2_get_byte(&g2);
397                         CHECK_PIXEL_PTR(byte_run);
398                         for (j = 0; j < byte_run; j++) {
399                             pixels[pixel_ptr++] = palette_idx1;
400                             pixel_countdown--;
401                             if (pixel_countdown < 0)
402                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
403                                        pixel_countdown, lines);
404                         }
405                     } else {  /* copy bytes if byte_run < 0 */
406                         byte_run = -byte_run;
407                         CHECK_PIXEL_PTR(byte_run);
408                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
409                             break;
410                         for (j = 0; j < byte_run; j++) {
411                             pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
412                             pixel_countdown--;
413                             if (pixel_countdown < 0)
414                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
415                                        pixel_countdown, lines);
416                         }
417                     }
418                 }
419
420                 y_ptr += s->frame->linesize[0];
421             }
422             break;
423
424         case FLI_COPY:
425             /* copy the chunk (uncompressed frame) */
426             if (chunk_size - 6 != FFALIGN(s->avctx->width, 4) * s->avctx->height) {
427                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
428                        "has incorrect size, skipping chunk\n", chunk_size - 6);
429                 bytestream2_skip(&g2, chunk_size - 6);
430             } else {
431                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
432                      y_ptr += s->frame->linesize[0]) {
433                     bytestream2_get_buffer(&g2, &pixels[y_ptr],
434                                            s->avctx->width);
435                     if (s->avctx->width & 3)
436                         bytestream2_skip(&g2, 4 - (s->avctx->width & 3));
437                 }
438             }
439             break;
440
441         case FLI_MINI:
442             /* some sort of a thumbnail? disregard this chunk... */
443             break;
444
445         default:
446             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
447             break;
448         }
449
450         if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
451             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
452         } else {
453             av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
454             break;
455         }
456
457         frame_size -= chunk_size;
458         num_chunks--;
459     }
460
461     /* by the end of the chunk, the stream ptr should equal the frame
462      * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
463     if (bytestream2_get_bytes_left(&g2) > 2)
464         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
465                "and final chunk ptr = %d\n", buf_size,
466                buf_size - bytestream2_get_bytes_left(&g2));
467
468     /* make the palette available on the way out */
469     memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
470     if (s->new_palette) {
471         s->frame->palette_has_changed = 1;
472         s->new_palette = 0;
473     }
474
475     if ((ret = av_frame_ref(data, s->frame)) < 0)
476         return ret;
477
478     *got_frame = 1;
479
480     return buf_size;
481 }
482
483 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
484                                       void *data, int *got_frame,
485                                       const uint8_t *buf, int buf_size)
486 {
487     /* Note, the only difference between the 15Bpp and 16Bpp */
488     /* Format is the pixel format, the packets are processed the same. */
489     FlicDecodeContext *s = avctx->priv_data;
490
491     GetByteContext g2;
492     int pixel_ptr;
493     unsigned char palette_idx1;
494
495     unsigned int frame_size;
496     int num_chunks;
497
498     unsigned int chunk_size;
499     int chunk_type;
500
501     int i, j, ret;
502
503     int lines;
504     int compressed_lines;
505     signed short line_packets;
506     int y_ptr;
507     int byte_run;
508     int pixel_skip;
509     int pixel_countdown;
510     unsigned char *pixels;
511     int pixel;
512     unsigned int pixel_limit;
513
514     bytestream2_init(&g2, buf, buf_size);
515
516     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
517         return ret;
518
519     pixels = s->frame->data[0];
520     pixel_limit = s->avctx->height * s->frame->linesize[0];
521
522     frame_size = bytestream2_get_le32(&g2);
523     bytestream2_skip(&g2, 2);  /* skip the magic number */
524     num_chunks = bytestream2_get_le16(&g2);
525     bytestream2_skip(&g2, 8);  /* skip padding */
526     if (frame_size > buf_size)
527         frame_size = buf_size;
528
529     if (frame_size < 16)
530         return AVERROR_INVALIDDATA;
531     frame_size -= 16;
532
533     /* iterate through the chunks */
534     while ((frame_size > 0) && (num_chunks > 0) &&
535             bytestream2_get_bytes_left(&g2) >= 4) {
536         int stream_ptr_after_chunk;
537         chunk_size = bytestream2_get_le32(&g2);
538         if (chunk_size > frame_size) {
539             av_log(avctx, AV_LOG_WARNING,
540                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
541             chunk_size = frame_size;
542         }
543         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
544
545         chunk_type = bytestream2_get_le16(&g2);
546
547
548         switch (chunk_type) {
549         case FLI_256_COLOR:
550         case FLI_COLOR:
551             /* For some reason, it seems that non-palettized flics do
552              * include one of these chunks in their first frame.
553              * Why I do not know, it seems rather extraneous. */
554             ff_dlog(avctx,
555                     "Unexpected Palette chunk %d in non-palettized FLC\n",
556                     chunk_type);
557             bytestream2_skip(&g2, chunk_size - 6);
558             break;
559
560         case FLI_DELTA:
561         case FLI_DTA_LC:
562             y_ptr = 0;
563             compressed_lines = bytestream2_get_le16(&g2);
564             while (compressed_lines > 0) {
565                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
566                     break;
567                 line_packets = bytestream2_get_le16(&g2);
568                 if (line_packets < 0) {
569                     line_packets = -line_packets;
570                     y_ptr += line_packets * s->frame->linesize[0];
571                 } else {
572                     compressed_lines--;
573                     pixel_ptr = y_ptr;
574                     CHECK_PIXEL_PTR(0);
575                     pixel_countdown = s->avctx->width;
576                     for (i = 0; i < line_packets; i++) {
577                         /* account for the skip bytes */
578                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
579                             break;
580                         pixel_skip = bytestream2_get_byte(&g2);
581                         pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
582                         pixel_countdown -= pixel_skip;
583                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
584                         if (byte_run < 0) {
585                             byte_run = -byte_run;
586                             pixel    = bytestream2_get_le16(&g2);
587                             CHECK_PIXEL_PTR(2 * byte_run);
588                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
589                                 *((signed short*)(&pixels[pixel_ptr])) = pixel;
590                                 pixel_ptr += 2;
591                             }
592                         } else {
593                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
594                                 break;
595                             CHECK_PIXEL_PTR(2 * byte_run);
596                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
597                                 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
598                                 pixel_ptr += 2;
599                             }
600                         }
601                     }
602
603                     y_ptr += s->frame->linesize[0];
604                 }
605             }
606             break;
607
608         case FLI_LC:
609             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
610             bytestream2_skip(&g2, chunk_size - 6);
611             break;
612
613         case FLI_BLACK:
614             /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
615             memset(pixels, 0x0000,
616                    s->frame->linesize[0] * s->avctx->height);
617             break;
618
619         case FLI_BRUN:
620             y_ptr = 0;
621             for (lines = 0; lines < s->avctx->height; lines++) {
622                 pixel_ptr = y_ptr;
623                 /* disregard the line packets; instead, iterate through all
624                  * pixels on a row */
625                 bytestream2_skip(&g2, 1);
626                 pixel_countdown = (s->avctx->width * 2);
627
628                 while (pixel_countdown > 0) {
629                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
630                         break;
631                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
632                     if (byte_run > 0) {
633                         palette_idx1 = bytestream2_get_byte(&g2);
634                         CHECK_PIXEL_PTR(byte_run);
635                         for (j = 0; j < byte_run; j++) {
636                             pixels[pixel_ptr++] = palette_idx1;
637                             pixel_countdown--;
638                             if (pixel_countdown < 0)
639                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
640                                        pixel_countdown, lines);
641                         }
642                     } else {  /* copy bytes if byte_run < 0 */
643                         byte_run = -byte_run;
644                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
645                             break;
646                         CHECK_PIXEL_PTR(byte_run);
647                         for (j = 0; j < byte_run; j++) {
648                             palette_idx1 = bytestream2_get_byte(&g2);
649                             pixels[pixel_ptr++] = palette_idx1;
650                             pixel_countdown--;
651                             if (pixel_countdown < 0)
652                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
653                                        pixel_countdown, lines);
654                         }
655                     }
656                 }
657
658                 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
659                  * This does not give us any good opportunity to perform word endian conversion
660                  * during decompression. So if it is required (i.e., this is not a LE target, we do
661                  * a second pass over the line here, swapping the bytes.
662                  */
663 #if HAVE_BIGENDIAN
664                 pixel_ptr = y_ptr;
665                 pixel_countdown = s->avctx->width;
666                 while (pixel_countdown > 0) {
667                     *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
668                     pixel_ptr += 2;
669                 }
670 #endif
671                 y_ptr += s->frame->linesize[0];
672             }
673             break;
674
675         case FLI_DTA_BRUN:
676             y_ptr = 0;
677             for (lines = 0; lines < s->avctx->height; lines++) {
678                 pixel_ptr = y_ptr;
679                 /* disregard the line packets; instead, iterate through all
680                  * pixels on a row */
681                 bytestream2_skip(&g2, 1);
682                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
683
684                 while (pixel_countdown > 0) {
685                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
686                         break;
687                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
688                     if (byte_run > 0) {
689                         pixel    = bytestream2_get_le16(&g2);
690                         CHECK_PIXEL_PTR(2 * byte_run);
691                         for (j = 0; j < byte_run; j++) {
692                             *((signed short*)(&pixels[pixel_ptr])) = pixel;
693                             pixel_ptr += 2;
694                             pixel_countdown--;
695                             if (pixel_countdown < 0)
696                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
697                                        pixel_countdown);
698                         }
699                     } else {  /* copy pixels if byte_run < 0 */
700                         byte_run = -byte_run;
701                         if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
702                             break;
703                         CHECK_PIXEL_PTR(2 * byte_run);
704                         for (j = 0; j < byte_run; j++) {
705                             *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
706                             pixel_ptr  += 2;
707                             pixel_countdown--;
708                             if (pixel_countdown < 0)
709                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
710                                        pixel_countdown);
711                         }
712                     }
713                 }
714
715                 y_ptr += s->frame->linesize[0];
716             }
717             break;
718
719         case FLI_COPY:
720         case FLI_DTA_COPY:
721             /* copy the chunk (uncompressed frame) */
722             if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
723                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
724                        "bigger than image, skipping chunk\n", chunk_size - 6);
725                 bytestream2_skip(&g2, chunk_size - 6);
726             } else {
727
728                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
729                      y_ptr += s->frame->linesize[0]) {
730
731                     pixel_countdown = s->avctx->width;
732                     pixel_ptr = 0;
733                     while (pixel_countdown > 0) {
734                       *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
735                       pixel_ptr += 2;
736                       pixel_countdown--;
737                     }
738                     if (s->avctx->width & 1)
739                         bytestream2_skip(&g2, 2);
740                 }
741             }
742             break;
743
744         case FLI_MINI:
745             /* some sort of a thumbnail? disregard this chunk... */
746             bytestream2_skip(&g2, chunk_size - 6);
747             break;
748
749         default:
750             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
751             break;
752         }
753
754         if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
755             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
756         } else {
757             av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
758             break;
759         }
760
761         frame_size -= chunk_size;
762         num_chunks--;
763     }
764
765     /* by the end of the chunk, the stream ptr should equal the frame
766      * size (minus 1, possibly); if it doesn't, issue a warning */
767     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
768         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
769                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
770
771     if ((ret = av_frame_ref(data, s->frame)) < 0)
772         return ret;
773
774     *got_frame = 1;
775
776     return buf_size;
777 }
778
779 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
780                                    void *data, int *got_frame,
781                                    const uint8_t *buf, int buf_size)
782 {
783     FlicDecodeContext *s = avctx->priv_data;
784
785     GetByteContext g2;
786     int pixel_ptr;
787     unsigned char palette_idx1;
788
789     unsigned int frame_size;
790     int num_chunks;
791
792     unsigned int chunk_size;
793     int chunk_type;
794
795     int i, j, ret;
796
797     int lines;
798     int compressed_lines;
799     signed short line_packets;
800     int y_ptr;
801     int byte_run;
802     int pixel_skip;
803     int pixel_countdown;
804     unsigned char *pixels;
805     int pixel;
806     unsigned int pixel_limit;
807
808     bytestream2_init(&g2, buf, buf_size);
809
810     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
811         return ret;
812
813     pixels = s->frame->data[0];
814     pixel_limit = s->avctx->height * s->frame->linesize[0];
815
816     frame_size = bytestream2_get_le32(&g2);
817     bytestream2_skip(&g2, 2);  /* skip the magic number */
818     num_chunks = bytestream2_get_le16(&g2);
819     bytestream2_skip(&g2, 8);  /* skip padding */
820     if (frame_size > buf_size)
821         frame_size = buf_size;
822
823     if (frame_size < 16)
824         return AVERROR_INVALIDDATA;
825     frame_size -= 16;
826
827     /* iterate through the chunks */
828     while ((frame_size > 0) && (num_chunks > 0) &&
829             bytestream2_get_bytes_left(&g2) >= 4) {
830         int stream_ptr_after_chunk;
831         chunk_size = bytestream2_get_le32(&g2);
832         if (chunk_size > frame_size) {
833             av_log(avctx, AV_LOG_WARNING,
834                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
835             chunk_size = frame_size;
836         }
837         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
838
839         chunk_type = bytestream2_get_le16(&g2);
840
841
842         switch (chunk_type) {
843         case FLI_256_COLOR:
844         case FLI_COLOR:
845             /* For some reason, it seems that non-palettized flics do
846              * include one of these chunks in their first frame.
847              * Why I do not know, it seems rather extraneous. */
848             ff_dlog(avctx,
849                     "Unexpected Palette chunk %d in non-palettized FLC\n",
850                     chunk_type);
851             bytestream2_skip(&g2, chunk_size - 6);
852             break;
853
854         case FLI_DELTA:
855         case FLI_DTA_LC:
856             y_ptr = 0;
857             compressed_lines = bytestream2_get_le16(&g2);
858             while (compressed_lines > 0) {
859                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
860                     break;
861                 line_packets = bytestream2_get_le16(&g2);
862                 if (line_packets < 0) {
863                     line_packets = -line_packets;
864                     y_ptr += line_packets * s->frame->linesize[0];
865                 } else {
866                     compressed_lines--;
867                     pixel_ptr = y_ptr;
868                     CHECK_PIXEL_PTR(0);
869                     pixel_countdown = s->avctx->width;
870                     for (i = 0; i < line_packets; i++) {
871                         /* account for the skip bytes */
872                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
873                             break;
874                         pixel_skip = bytestream2_get_byte(&g2);
875                         pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
876                         pixel_countdown -= pixel_skip;
877                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
878                         if (byte_run < 0) {
879                             byte_run = -byte_run;
880                             pixel    = bytestream2_get_le24(&g2);
881                             CHECK_PIXEL_PTR(3 * byte_run);
882                             for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
883                                 AV_WL24(&pixels[pixel_ptr], pixel);
884                                 pixel_ptr += 3;
885                             }
886                         } else {
887                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
888                                 break;
889                             CHECK_PIXEL_PTR(2 * byte_run);
890                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
891                                 pixel = bytestream2_get_le24(&g2);
892                                 AV_WL24(&pixels[pixel_ptr], pixel);
893                                 pixel_ptr += 3;
894                             }
895                         }
896                     }
897
898                     y_ptr += s->frame->linesize[0];
899                 }
900             }
901             break;
902
903         case FLI_LC:
904             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
905             bytestream2_skip(&g2, chunk_size - 6);
906             break;
907
908         case FLI_BLACK:
909             /* set the whole frame to 0x00 which is black for 24 bit mode. */
910             memset(pixels, 0x00,
911                    s->frame->linesize[0] * s->avctx->height);
912             break;
913
914         case FLI_BRUN:
915             y_ptr = 0;
916             for (lines = 0; lines < s->avctx->height; lines++) {
917                 pixel_ptr = y_ptr;
918                 /* disregard the line packets; instead, iterate through all
919                  * pixels on a row */
920                 bytestream2_skip(&g2, 1);
921                 pixel_countdown = (s->avctx->width * 3);
922
923                 while (pixel_countdown > 0) {
924                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
925                         break;
926                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
927                     if (byte_run > 0) {
928                         palette_idx1 = bytestream2_get_byte(&g2);
929                         CHECK_PIXEL_PTR(byte_run);
930                         for (j = 0; j < byte_run; j++) {
931                             pixels[pixel_ptr++] = palette_idx1;
932                             pixel_countdown--;
933                             if (pixel_countdown < 0)
934                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
935                                        pixel_countdown, lines);
936                         }
937                     } else {  /* copy bytes if byte_run < 0 */
938                         byte_run = -byte_run;
939                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
940                             break;
941                         CHECK_PIXEL_PTR(byte_run);
942                         for (j = 0; j < byte_run; j++) {
943                             palette_idx1 = bytestream2_get_byte(&g2);
944                             pixels[pixel_ptr++] = palette_idx1;
945                             pixel_countdown--;
946                             if (pixel_countdown < 0)
947                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
948                                        pixel_countdown, lines);
949                         }
950                     }
951                 }
952
953                 y_ptr += s->frame->linesize[0];
954             }
955             break;
956
957         case FLI_DTA_BRUN:
958             y_ptr = 0;
959             for (lines = 0; lines < s->avctx->height; lines++) {
960                 pixel_ptr = y_ptr;
961                 /* disregard the line packets; instead, iterate through all
962                  * pixels on a row */
963                 bytestream2_skip(&g2, 1);
964                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
965
966                 while (pixel_countdown > 0) {
967                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
968                         break;
969                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
970                     if (byte_run > 0) {
971                         pixel = bytestream2_get_le24(&g2);
972                         CHECK_PIXEL_PTR(3 * byte_run);
973                         for (j = 0; j < byte_run; j++) {
974                             AV_WL24(pixels + pixel_ptr, pixel);
975                             pixel_ptr += 3;
976                             pixel_countdown--;
977                             if (pixel_countdown < 0)
978                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
979                                        pixel_countdown);
980                         }
981                     } else {  /* copy pixels if byte_run < 0 */
982                         byte_run = -byte_run;
983                         if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
984                             break;
985                         CHECK_PIXEL_PTR(3 * byte_run);
986                         for (j = 0; j < byte_run; j++) {
987                             pixel = bytestream2_get_le24(&g2);
988                             AV_WL24(pixels + pixel_ptr, pixel);
989                             pixel_ptr  += 3;
990                             pixel_countdown--;
991                             if (pixel_countdown < 0)
992                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
993                                        pixel_countdown);
994                         }
995                     }
996                 }
997
998                 y_ptr += s->frame->linesize[0];
999             }
1000             break;
1001
1002         case FLI_COPY:
1003         case FLI_DTA_COPY:
1004             /* copy the chunk (uncompressed frame) */
1005             if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
1006                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
1007                        "bigger than image, skipping chunk\n", chunk_size - 6);
1008                 bytestream2_skip(&g2, chunk_size - 6);
1009             } else {
1010                 for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
1011                      y_ptr += s->frame->linesize[0]) {
1012
1013                     pixel_countdown = s->avctx->width;
1014                     pixel_ptr = 0;
1015                     while (pixel_countdown > 0) {
1016                         pixel = bytestream2_get_le24(&g2);
1017                         AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
1018                         pixel_ptr += 3;
1019                         pixel_countdown--;
1020                     }
1021                     if (s->avctx->width & 1)
1022                         bytestream2_skip(&g2, 3);
1023                 }
1024             }
1025             break;
1026
1027         case FLI_MINI:
1028             /* some sort of a thumbnail? disregard this chunk... */
1029             bytestream2_skip(&g2, chunk_size - 6);
1030             break;
1031
1032         default:
1033             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
1034             break;
1035         }
1036
1037         if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
1038             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
1039         } else {
1040             av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
1041             break;
1042         }
1043
1044         frame_size -= chunk_size;
1045         num_chunks--;
1046     }
1047
1048     /* by the end of the chunk, the stream ptr should equal the frame
1049      * size (minus 1, possibly); if it doesn't, issue a warning */
1050     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
1051         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
1052                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
1053
1054     if ((ret = av_frame_ref(data, s->frame)) < 0)
1055         return ret;
1056
1057     *got_frame = 1;
1058
1059     return buf_size;
1060 }
1061
1062 static int flic_decode_frame(AVCodecContext *avctx,
1063                              void *data, int *got_frame,
1064                              AVPacket *avpkt)
1065 {
1066     const uint8_t *buf = avpkt->data;
1067     int buf_size = avpkt->size;
1068     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1069         return flic_decode_frame_8BPP(avctx, data, got_frame,
1070                                       buf, buf_size);
1071     } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
1072                (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
1073         return flic_decode_frame_15_16BPP(avctx, data, got_frame,
1074                                           buf, buf_size);
1075     } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
1076         return flic_decode_frame_24BPP(avctx, data, got_frame,
1077                                        buf, buf_size);
1078     }
1079
1080     /* Should not get  here, ever as the pix_fmt is processed */
1081     /* in flic_decode_init and the above if should deal with */
1082     /* the finite set of possibilities allowable by here. */
1083     /* But in case we do, just error out. */
1084     av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
1085     return AVERROR_BUG;
1086 }
1087
1088
1089 static av_cold int flic_decode_end(AVCodecContext *avctx)
1090 {
1091     FlicDecodeContext *s = avctx->priv_data;
1092
1093     av_frame_free(&s->frame);
1094
1095     return 0;
1096 }
1097
1098 AVCodec ff_flic_decoder = {
1099     .name           = "flic",
1100     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
1101     .type           = AVMEDIA_TYPE_VIDEO,
1102     .id             = AV_CODEC_ID_FLIC,
1103     .priv_data_size = sizeof(FlicDecodeContext),
1104     .init           = flic_decode_init,
1105     .close          = flic_decode_end,
1106     .decode         = flic_decode_frame,
1107     .capabilities   = AV_CODEC_CAP_DR1,
1108 };