]> git.sesse.net Git - ffmpeg/blob - libavcodec/flicvideo.c
cavsdec: check dimensions being valid.
[ffmpeg] / libavcodec / flicvideo.c
1 /*
2  * FLI/FLC Animation Video Decoder
3  * Copyright (C) 2003, 2004 the ffmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Autodesk Animator FLI/FLC Video Decoder
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the .fli/.flc file format and all of its many
27  * variations, visit:
28  *   http://www.compuphase.com/flic.htm
29  *
30  * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
31  * colorspace data, depending on the FLC. To use this decoder, be
32  * sure that your demuxer sends the FLI file header to the decoder via
33  * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
34  * large. The only exception is for FLI files from the game "Magic Carpet",
35  * in which the header is only 12 bytes.
36  */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "libavutil/intreadwrite.h"
43 #include "avcodec.h"
44 #include "bytestream.h"
45 #include "mathops.h"
46
47 #define FLI_256_COLOR 4
48 #define FLI_DELTA     7
49 #define FLI_COLOR     11
50 #define FLI_LC        12
51 #define FLI_BLACK     13
52 #define FLI_BRUN      15
53 #define FLI_COPY      16
54 #define FLI_MINI      18
55 #define FLI_DTA_BRUN  25
56 #define FLI_DTA_COPY  26
57 #define FLI_DTA_LC    27
58
59 #define FLI_TYPE_CODE     (0xAF11)
60 #define FLC_FLX_TYPE_CODE (0xAF12)
61 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
62 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
63
64 #define CHECK_PIXEL_PTR(n) \
65     if (pixel_ptr + n > pixel_limit) { \
66         av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
67         pixel_ptr + n, pixel_limit); \
68         return AVERROR_INVALIDDATA; \
69     } \
70
71 typedef struct FlicDecodeContext {
72     AVCodecContext *avctx;
73     AVFrame frame;
74
75     unsigned int palette[256];
76     int new_palette;
77     int fli_type;  /* either 0xAF11 or 0xAF12, affects palette resolution */
78 } FlicDecodeContext;
79
80 static av_cold int flic_decode_init(AVCodecContext *avctx)
81 {
82     FlicDecodeContext *s = avctx->priv_data;
83     unsigned char *fli_header = (unsigned char *)avctx->extradata;
84     int depth;
85
86     if (avctx->extradata_size != 0 &&
87         avctx->extradata_size != 12 &&
88         avctx->extradata_size != 128 &&
89         avctx->extradata_size != 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 = PIX_FMT_PAL8; break;
133         case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
134         case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
135         case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
136                   av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
137                   return -1;
138         default :
139                   av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
140                   return -1;
141     }
142
143     avcodec_get_frame_defaults(&s->frame);
144     s->frame.data[0] = NULL;
145     s->new_palette = 0;
146
147     return 0;
148 }
149
150 static int flic_decode_frame_8BPP(AVCodecContext *avctx,
151                                   void *data, int *data_size,
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;
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     s->frame.reference = 3;
189     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
190     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
191         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
192         return -1;
193     }
194
195     pixels = s->frame.data[0];
196     pixel_limit = s->avctx->height * s->frame.linesize[0];
197     if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
198         return AVERROR_INVALIDDATA;
199     frame_size = bytestream2_get_le32(&g2);
200     if (frame_size > buf_size)
201         frame_size = buf_size;
202     bytestream2_skip(&g2, 2); /* skip the magic number */
203     num_chunks = bytestream2_get_le16(&g2);
204     bytestream2_skip(&g2, 8);  /* skip padding */
205
206     frame_size -= 16;
207
208     /* iterate through the chunks */
209     while ((frame_size >= 6) && (num_chunks > 0)) {
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 = 0xFF << 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 > 0) {
391                         palette_idx1 = bytestream2_get_byte(&g2);
392                         CHECK_PIXEL_PTR(byte_run);
393                         for (j = 0; j < byte_run; j++) {
394                             pixels[pixel_ptr++] = palette_idx1;
395                             pixel_countdown--;
396                             if (pixel_countdown < 0)
397                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
398                                        pixel_countdown, lines);
399                         }
400                     } else {  /* copy bytes if byte_run < 0 */
401                         byte_run = -byte_run;
402                         CHECK_PIXEL_PTR(byte_run);
403                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
404                             break;
405                         for (j = 0; j < byte_run; j++) {
406                             pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
407                             pixel_countdown--;
408                             if (pixel_countdown < 0)
409                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
410                                        pixel_countdown, lines);
411                         }
412                     }
413                 }
414
415                 y_ptr += s->frame.linesize[0];
416             }
417             break;
418
419         case FLI_COPY:
420             /* copy the chunk (uncompressed frame) */
421             if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
422                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
423                        "has incorrect size, skipping chunk\n", chunk_size - 6);
424                 bytestream2_skip(&g2, chunk_size - 6);
425             } else {
426                 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
427                      y_ptr += s->frame.linesize[0]) {
428                     bytestream2_get_buffer(&g2, &pixels[y_ptr],
429                                            s->avctx->width);
430                 }
431             }
432             break;
433
434         case FLI_MINI:
435             /* some sort of a thumbnail? disregard this chunk... */
436             break;
437
438         default:
439             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
440             break;
441         }
442
443         if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
444             bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
445
446         frame_size -= chunk_size;
447         num_chunks--;
448     }
449
450     /* by the end of the chunk, the stream ptr should equal the frame
451      * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
452     if (bytestream2_get_bytes_left(&g2) > 2)
453         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
454                "and final chunk ptr = %d\n", buf_size,
455                buf_size - bytestream2_get_bytes_left(&g2));
456
457     /* make the palette available on the way out */
458     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
459     if (s->new_palette) {
460         s->frame.palette_has_changed = 1;
461         s->new_palette = 0;
462     }
463
464     *data_size=sizeof(AVFrame);
465     *(AVFrame*)data = s->frame;
466
467     return buf_size;
468 }
469
470 static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
471                                       void *data, int *data_size,
472                                       const uint8_t *buf, int buf_size)
473 {
474     /* Note, the only difference between the 15Bpp and 16Bpp */
475     /* Format is the pixel format, the packets are processed the same. */
476     FlicDecodeContext *s = avctx->priv_data;
477
478     GetByteContext g2;
479     int pixel_ptr;
480     unsigned char palette_idx1;
481
482     unsigned int frame_size;
483     int num_chunks;
484
485     unsigned int chunk_size;
486     int chunk_type;
487
488     int i, j;
489
490     int lines;
491     int compressed_lines;
492     signed short line_packets;
493     int y_ptr;
494     int byte_run;
495     int pixel_skip;
496     int pixel_countdown;
497     unsigned char *pixels;
498     int pixel;
499     unsigned int pixel_limit;
500
501     bytestream2_init(&g2, buf, buf_size);
502
503     s->frame.reference = 3;
504     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
505     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
506         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
507         return -1;
508     }
509
510     pixels = s->frame.data[0];
511     pixel_limit = s->avctx->height * s->frame.linesize[0];
512
513     frame_size = bytestream2_get_le32(&g2);
514     bytestream2_skip(&g2, 2);  /* skip the magic number */
515     num_chunks = bytestream2_get_le16(&g2);
516     bytestream2_skip(&g2, 8);  /* skip padding */
517     if (frame_size > buf_size)
518         frame_size = buf_size;
519
520     frame_size -= 16;
521
522     /* iterate through the chunks */
523     while ((frame_size > 0) && (num_chunks > 0)) {
524         int stream_ptr_after_chunk;
525         chunk_size = bytestream2_get_le32(&g2);
526         if (chunk_size > frame_size) {
527             av_log(avctx, AV_LOG_WARNING,
528                    "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
529             chunk_size = frame_size;
530         }
531         stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
532
533         chunk_type = bytestream2_get_le16(&g2);
534
535
536         switch (chunk_type) {
537         case FLI_256_COLOR:
538         case FLI_COLOR:
539             /* For some reason, it seems that non-palettized flics do
540              * include one of these chunks in their first frame.
541              * Why I do not know, it seems rather extraneous. */
542 /*            av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
543             bytestream2_skip(&g2, chunk_size - 6);
544             break;
545
546         case FLI_DELTA:
547         case FLI_DTA_LC:
548             y_ptr = 0;
549             compressed_lines = bytestream2_get_le16(&g2);
550             while (compressed_lines > 0) {
551                 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
552                     break;
553                 line_packets = bytestream2_get_le16(&g2);
554                 if (line_packets < 0) {
555                     line_packets = -line_packets;
556                     y_ptr += line_packets * s->frame.linesize[0];
557                 } else {
558                     compressed_lines--;
559                     pixel_ptr = y_ptr;
560                     CHECK_PIXEL_PTR(0);
561                     pixel_countdown = s->avctx->width;
562                     for (i = 0; i < line_packets; i++) {
563                         /* account for the skip bytes */
564                         if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
565                             break;
566                         pixel_skip = bytestream2_get_byte(&g2);
567                         pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
568                         pixel_countdown -= pixel_skip;
569                         byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
570                         if (byte_run < 0) {
571                             byte_run = -byte_run;
572                             pixel    = bytestream2_get_le16(&g2);
573                             CHECK_PIXEL_PTR(2 * byte_run);
574                             for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
575                                 *((signed short*)(&pixels[pixel_ptr])) = pixel;
576                                 pixel_ptr += 2;
577                             }
578                         } else {
579                             if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
580                                 break;
581                             CHECK_PIXEL_PTR(2 * byte_run);
582                             for (j = 0; j < byte_run; j++, pixel_countdown--) {
583                                 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
584                                 pixel_ptr += 2;
585                             }
586                         }
587                     }
588
589                     y_ptr += s->frame.linesize[0];
590                 }
591             }
592             break;
593
594         case FLI_LC:
595             av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
596             bytestream2_skip(&g2, chunk_size - 6);
597             break;
598
599         case FLI_BLACK:
600             /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
601             memset(pixels, 0x0000,
602                    s->frame.linesize[0] * s->avctx->height);
603             break;
604
605         case FLI_BRUN:
606             y_ptr = 0;
607             for (lines = 0; lines < s->avctx->height; lines++) {
608                 pixel_ptr = y_ptr;
609                 /* disregard the line packets; instead, iterate through all
610                  * pixels on a row */
611                 bytestream2_skip(&g2, 1);
612                 pixel_countdown = (s->avctx->width * 2);
613
614                 while (pixel_countdown > 0) {
615                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
616                         break;
617                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
618                     if (byte_run > 0) {
619                         palette_idx1 = bytestream2_get_byte(&g2);
620                         CHECK_PIXEL_PTR(byte_run);
621                         for (j = 0; j < byte_run; j++) {
622                             pixels[pixel_ptr++] = palette_idx1;
623                             pixel_countdown--;
624                             if (pixel_countdown < 0)
625                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
626                                        pixel_countdown, lines);
627                         }
628                     } else {  /* copy bytes if byte_run < 0 */
629                         byte_run = -byte_run;
630                         if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
631                             break;
632                         CHECK_PIXEL_PTR(byte_run);
633                         for (j = 0; j < byte_run; j++) {
634                             palette_idx1 = bytestream2_get_byte(&g2);
635                             pixels[pixel_ptr++] = palette_idx1;
636                             pixel_countdown--;
637                             if (pixel_countdown < 0)
638                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
639                                        pixel_countdown, lines);
640                         }
641                     }
642                 }
643
644                 /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
645                  * This does not give us any good oportunity to perform word endian conversion
646                  * during decompression. So if it is required (i.e., this is not a LE target, we do
647                  * a second pass over the line here, swapping the bytes.
648                  */
649 #if HAVE_BIGENDIAN
650                 pixel_ptr = y_ptr;
651                 pixel_countdown = s->avctx->width;
652                 while (pixel_countdown > 0) {
653                     *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
654                     pixel_ptr += 2;
655                 }
656 #endif
657                 y_ptr += s->frame.linesize[0];
658             }
659             break;
660
661         case FLI_DTA_BRUN:
662             y_ptr = 0;
663             for (lines = 0; lines < s->avctx->height; lines++) {
664                 pixel_ptr = y_ptr;
665                 /* disregard the line packets; instead, iterate through all
666                  * pixels on a row */
667                 bytestream2_skip(&g2, 1);
668                 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
669
670                 while (pixel_countdown > 0) {
671                     if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
672                         break;
673                     byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
674                     if (byte_run > 0) {
675                         pixel    = bytestream2_get_le16(&g2);
676                         CHECK_PIXEL_PTR(2 * byte_run);
677                         for (j = 0; j < byte_run; j++) {
678                             *((signed short*)(&pixels[pixel_ptr])) = pixel;
679                             pixel_ptr += 2;
680                             pixel_countdown--;
681                             if (pixel_countdown < 0)
682                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
683                                        pixel_countdown);
684                         }
685                     } else {  /* copy pixels if byte_run < 0 */
686                         byte_run = -byte_run;
687                         if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
688                             break;
689                         CHECK_PIXEL_PTR(2 * byte_run);
690                         for (j = 0; j < byte_run; j++) {
691                             *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
692                             pixel_ptr  += 2;
693                             pixel_countdown--;
694                             if (pixel_countdown < 0)
695                                 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
696                                        pixel_countdown);
697                         }
698                     }
699                 }
700
701                 y_ptr += s->frame.linesize[0];
702             }
703             break;
704
705         case FLI_COPY:
706         case FLI_DTA_COPY:
707             /* copy the chunk (uncompressed frame) */
708             if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
709                 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
710                        "bigger than image, skipping chunk\n", chunk_size - 6);
711                 bytestream2_skip(&g2, chunk_size - 6);
712             } else {
713
714                 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
715                      y_ptr += s->frame.linesize[0]) {
716
717                     pixel_countdown = s->avctx->width;
718                     pixel_ptr = 0;
719                     while (pixel_countdown > 0) {
720                       *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
721                       pixel_ptr += 2;
722                       pixel_countdown--;
723                     }
724                 }
725             }
726             break;
727
728         case FLI_MINI:
729             /* some sort of a thumbnail? disregard this chunk... */
730             bytestream2_skip(&g2, chunk_size - 6);
731             break;
732
733         default:
734             av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
735             break;
736         }
737
738         frame_size -= chunk_size;
739         num_chunks--;
740     }
741
742     /* by the end of the chunk, the stream ptr should equal the frame
743      * size (minus 1, possibly); if it doesn't, issue a warning */
744     if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
745         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
746                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
747
748
749     *data_size=sizeof(AVFrame);
750     *(AVFrame*)data = s->frame;
751
752     return buf_size;
753 }
754
755 static int flic_decode_frame_24BPP(AVCodecContext *avctx,
756                                    void *data, int *data_size,
757                                    const uint8_t *buf, int buf_size)
758 {
759   av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
760   return -1;
761 }
762
763 static int flic_decode_frame(AVCodecContext *avctx,
764                              void *data, int *data_size,
765                              AVPacket *avpkt)
766 {
767     const uint8_t *buf = avpkt->data;
768     int buf_size = avpkt->size;
769     if (avctx->pix_fmt == PIX_FMT_PAL8) {
770       return flic_decode_frame_8BPP(avctx, data, data_size,
771                                     buf, buf_size);
772     }
773     else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
774              (avctx->pix_fmt == PIX_FMT_RGB565)) {
775       return flic_decode_frame_15_16BPP(avctx, data, data_size,
776                                         buf, buf_size);
777     }
778     else if (avctx->pix_fmt == PIX_FMT_BGR24) {
779       return flic_decode_frame_24BPP(avctx, data, data_size,
780                                      buf, buf_size);
781     }
782
783     /* Should not get  here, ever as the pix_fmt is processed */
784     /* in flic_decode_init and the above if should deal with */
785     /* the finite set of possibilites allowable by here. */
786     /* But in case we do, just error out. */
787     av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
788     return -1;
789 }
790
791
792 static av_cold int flic_decode_end(AVCodecContext *avctx)
793 {
794     FlicDecodeContext *s = avctx->priv_data;
795
796     if (s->frame.data[0])
797         avctx->release_buffer(avctx, &s->frame);
798
799     return 0;
800 }
801
802 AVCodec ff_flic_decoder = {
803     .name           = "flic",
804     .type           = AVMEDIA_TYPE_VIDEO,
805     .id             = AV_CODEC_ID_FLIC,
806     .priv_data_size = sizeof(FlicDecodeContext),
807     .init           = flic_decode_init,
808     .close          = flic_decode_end,
809     .decode         = flic_decode_frame,
810     .capabilities   = CODEC_CAP_DR1,
811     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
812 };