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