FFmpeg
Loading...
Searching...
No Matches
flicvideo.c
Go to the documentation of this file.
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 <string.h>
38
40#include "avcodec.h"
41#include "bytestream.h"
42#include "codec_internal.h"
43#include "decode.h"
44#include "mathops.h"
45
46#define FLI_256_COLOR 4
47#define FLI_DELTA 7
48#define FLI_COLOR 11
49#define FLI_LC 12
50#define FLI_BLACK 13
51#define FLI_BRUN 15
52#define FLI_COPY 16
53#define FLI_MINI 18
54#define FLI_DTA_BRUN 25
55#define FLI_DTA_COPY 26
56#define FLI_DTA_LC 27
57
58#define FLI_TYPE_CODE (0xAF11)
59#define FLC_FLX_TYPE_CODE (0xAF12)
60#define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
61#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
62
63static inline int check_pixel_ptr(ptrdiff_t ptr, int n,
64 ptrdiff_t lo, ptrdiff_t hi)
65{
66 if (ptr < lo || ptr + n > hi)
68 return 0;
69}
70
71#define CHECK_PIXEL_PTR(n) \
72{ \
73 ret = check_pixel_ptr(pixel_ptr, (n), pixel_lo, pixel_hi); \
74 if (ret < 0) \
75 return ret; \
76}
77
78#define CHECK_Y_PTR() \
79{ \
80 ret = check_pixel_ptr(y_ptr, 0, pixel_lo, pixel_hi); \
81 if (ret < 0) \
82 return ret; \
83}
84
85static void pixel_range(const AVFrame *frame, ptrdiff_t *lo, ptrdiff_t *hi)
86{
87 ptrdiff_t linesize = frame->linesize[0];
88
89 if (linesize > 0) {
90 *lo = 0;
91 *hi = frame->height * linesize;
92 } else {
93 *lo = (frame->height - 1) * linesize;
94 *hi = -linesize;
95 }
96}
97
98typedef struct FlicDecodeContext {
101
102 unsigned int palette[256];
104 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
106
108{
109 FlicDecodeContext *s = avctx->priv_data;
110 uint8_t *fli_header = avctx->extradata;
111 int depth;
112
113 if (avctx->extradata_size != 0 &&
114 avctx->extradata_size != 12 &&
115 avctx->extradata_size != 128 &&
116 avctx->extradata_size != 256 &&
117 avctx->extradata_size != 904 &&
118 avctx->extradata_size != 1024) {
119 av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
120 return AVERROR_INVALIDDATA;
121 }
122
123 s->avctx = avctx;
124
125 if (s->avctx->extradata_size == 12) {
126 /* special case for magic carpet FLIs */
128 depth = 8;
129 } else if (avctx->extradata_size == 1024) {
130 uint8_t *ptr = avctx->extradata;
131 int i;
132
133 for (i = 0; i < 256; i++) {
134 s->palette[i] = AV_RL32(ptr);
135 ptr += 4;
136 }
137 depth = 8;
138 /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
139 } else if (avctx->extradata_size == 0 ||
140 avctx->extradata_size == 256 ||
141 /* see FFmpeg ticket #1234 */
142 avctx->extradata_size == 904) {
143 s->fli_type = FLI_TYPE_CODE;
144 depth = 8;
145 } else {
146 s->fli_type = AV_RL16(&fli_header[4]);
147 depth = AV_RL16(&fli_header[12]);
148 }
149
150 if (depth == 0) {
151 depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
152 }
153
154 if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
155 depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
156 }
157
158 switch (depth) {
159 case 1 : avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; break;
160 case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
161 case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
162 case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
163 case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
164 default :
165 av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
166 return AVERROR_INVALIDDATA;
167 }
168
169 s->frame = av_frame_alloc();
170 if (!s->frame)
171 return AVERROR(ENOMEM);
172
173 s->new_palette = 0;
174
175 return 0;
176}
177
179 AVFrame *rframe, int *got_frame,
180 const uint8_t *buf, int buf_size)
181{
182 FlicDecodeContext *s = avctx->priv_data;
183
185 ptrdiff_t pixel_ptr;
186
187 unsigned int frame_size;
188 int num_chunks;
189
190 unsigned int chunk_size;
191 int chunk_type;
192
193 int i, j, ret;
194
195 int lines;
196 int compressed_lines;
197 int starting_line;
198 int line_packets;
199 ptrdiff_t y_ptr;
200 int byte_run;
201 int pixel_skip;
202 int pixel_countdown;
203 unsigned char *pixels;
204 ptrdiff_t pixel_lo, pixel_hi;
205
206 bytestream2_init(&g2, buf, buf_size);
207
208 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
209 return ret;
210
211 pixels = s->frame->data[0];
212 pixel_range(s->frame, &pixel_lo, &pixel_hi);
213 if (buf_size < 16 || buf_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
214 return AVERROR_INVALIDDATA;
215 frame_size = bytestream2_get_le32(&g2);
216 if (frame_size > buf_size)
217 frame_size = buf_size;
218 bytestream2_skip(&g2, 2); /* skip the magic number */
219 num_chunks = bytestream2_get_le16(&g2);
220 bytestream2_skip(&g2, 8); /* skip padding */
221
222 if (frame_size < 16)
223 return AVERROR_INVALIDDATA;
224
225 frame_size -= 16;
226
227 /* iterate through the chunks */
228 while ((frame_size >= 6) && (num_chunks > 0) &&
229 bytestream2_get_bytes_left(&g2) >= 4) {
230 int stream_ptr_after_chunk;
231 chunk_size = bytestream2_get_le32(&g2);
232 if (chunk_size > frame_size) {
233 av_log(avctx, AV_LOG_WARNING,
234 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
235 chunk_size = frame_size;
236 }
237 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
238
239 chunk_type = bytestream2_get_le16(&g2);
240
241 switch (chunk_type) {
242 case FLI_BRUN:
243 /* Byte run compression: This chunk type only occurs in the first
244 * FLI frame and it will update the entire frame. */
245 y_ptr = 0;
246 for (lines = 0; lines < s->avctx->height; lines++) {
247 pixel_ptr = y_ptr;
248 /* disregard the line packets; instead, iterate through all
249 * pixels on a row */
250 bytestream2_skip(&g2, 1);
251 pixel_countdown = (s->avctx->width + 7) >> 3;
252 while (pixel_countdown > 0) {
253 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
254 break;
255 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
256 if (!byte_run) {
257 av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
258 return AVERROR_INVALIDDATA;
259 }
260
261 if (byte_run > 0) {
262 int value = bytestream2_get_byte(&g2);
263 CHECK_PIXEL_PTR(byte_run);
264 for (j = 0; j < byte_run; j++) {
265 pixels[pixel_ptr++] = value;
266 pixel_countdown--;
267 if (pixel_countdown < 0)
268 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
269 pixel_countdown, lines);
270 }
271 } else { /* copy bytes if byte_run < 0 */
272 byte_run = -byte_run;
273 CHECK_PIXEL_PTR(byte_run);
274 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
275 break;
276 for (j = 0; j < byte_run; j++) {
277 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
278 pixel_countdown--;
279 if (pixel_countdown < 0)
280 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
281 pixel_countdown, lines);
282 }
283 }
284 }
285
286 y_ptr += s->frame->linesize[0];
287 }
288 break;
289
290 case FLI_LC:
291 /* line compressed */
292 starting_line = bytestream2_get_le16(&g2);
293 if (starting_line >= s->avctx->height)
294 return AVERROR_INVALIDDATA;
295 y_ptr = 0;
296 y_ptr += starting_line * s->frame->linesize[0];
297
298 compressed_lines = bytestream2_get_le16(&g2);
299 while (compressed_lines > 0) {
300 pixel_ptr = y_ptr;
302 pixel_countdown = (s->avctx->width + 7) >> 3;
303 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
304 break;
305 line_packets = bytestream2_get_byte(&g2);
306 if (line_packets > 0) {
307 for (i = 0; i < line_packets; i++) {
308 /* account for the skip bytes */
309 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
310 break;
311 pixel_skip = bytestream2_get_byte(&g2);
312 pixel_ptr += pixel_skip;
313 pixel_countdown -= pixel_skip;
314 byte_run = sign_extend(bytestream2_get_byte(&g2),8);
315 if (byte_run > 0) {
316 CHECK_PIXEL_PTR(byte_run);
317 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
318 break;
319 for (j = 0; j < byte_run; j++, pixel_countdown--) {
320 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
321 }
322 } else if (byte_run < 0) {
323 int value = bytestream2_get_byte(&g2);
324 byte_run = -byte_run;
325 CHECK_PIXEL_PTR(byte_run);
326 for (j = 0; j < byte_run; j++, pixel_countdown--) {
327 pixels[pixel_ptr++] = value;
328 }
329 }
330 }
331 }
332
333 y_ptr += s->frame->linesize[0];
334 compressed_lines--;
335 }
336 break;
337
338 default:
339 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
340 break;
341 }
342
343 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
344 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
345 } else {
346 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
347 break;
348 }
349
350 frame_size -= chunk_size;
351 num_chunks--;
352 }
353
354 /* by the end of the chunk, the stream ptr should equal the frame
355 * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
356 if (bytestream2_get_bytes_left(&g2) > 2)
357 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
358 "and final chunk ptr = %d\n", buf_size,
359 buf_size - bytestream2_get_bytes_left(&g2));
360
361 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
362 return ret;
363
364 *got_frame = 1;
365
366 return buf_size;
367}
368
370 AVFrame *rframe, int *got_frame,
371 const uint8_t *buf, int buf_size)
372{
373 FlicDecodeContext *s = avctx->priv_data;
374
376 ptrdiff_t pixel_ptr;
377 int palette_ptr;
378 unsigned char palette_idx1;
379 unsigned char palette_idx2;
380
381 unsigned int frame_size;
382 int num_chunks;
383
384 unsigned int chunk_size;
385 int chunk_type;
386
387 int i, j, ret;
388
389 int color_packets;
390 int color_changes;
391 int color_shift;
392 unsigned char r, g, b;
393
394 int lines;
395 int compressed_lines;
396 int starting_line;
397 int line_packets;
398 ptrdiff_t y_ptr;
399 int byte_run;
400 int pixel_skip;
401 int pixel_countdown;
402 unsigned char *pixels;
403 ptrdiff_t pixel_lo, pixel_hi;
404
405 bytestream2_init(&g2, buf, buf_size);
406
407 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
408 return ret;
409
410 pixels = s->frame->data[0];
411 pixel_range(s->frame, &pixel_lo, &pixel_hi);
412 if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
413 return AVERROR_INVALIDDATA;
414 frame_size = bytestream2_get_le32(&g2);
415 if (frame_size > buf_size)
416 frame_size = buf_size;
417 bytestream2_skip(&g2, 2); /* skip the magic number */
418 num_chunks = bytestream2_get_le16(&g2);
419 bytestream2_skip(&g2, 8); /* skip padding */
420
421 if (frame_size < 16)
422 return AVERROR_INVALIDDATA;
423
424 frame_size -= 16;
425
426 /* iterate through the chunks */
427 while ((frame_size >= 6) && (num_chunks > 0) &&
428 bytestream2_get_bytes_left(&g2) >= 4) {
429 int stream_ptr_after_chunk;
430 chunk_size = bytestream2_get_le32(&g2);
431 if (chunk_size > frame_size) {
432 av_log(avctx, AV_LOG_WARNING,
433 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
434 chunk_size = frame_size;
435 }
436 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
437
438 chunk_type = bytestream2_get_le16(&g2);
439
440 switch (chunk_type) {
441 case FLI_256_COLOR:
442 case FLI_COLOR:
443 /* check special case: If this file is from the Magic Carpet
444 * game and uses 6-bit colors even though it reports 256-color
445 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
446 * initialization) */
447 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
448 color_shift = 0;
449 else
450 color_shift = 2;
451 /* set up the palette */
452 color_packets = bytestream2_get_le16(&g2);
453 palette_ptr = 0;
454 for (i = 0; i < color_packets; i++) {
455 /* first byte is how many colors to skip */
456 palette_ptr += bytestream2_get_byte(&g2);
457
458 /* next byte indicates how many entries to change */
459 color_changes = bytestream2_get_byte(&g2);
460
461 /* if there are 0 color changes, there are actually 256 */
462 if (color_changes == 0)
463 color_changes = 256;
464
465 if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
466 break;
467
468 for (j = 0; j < color_changes; j++) {
469 unsigned int entry;
470
471 /* wrap around, for good measure */
472 if ((unsigned)palette_ptr >= 256)
473 palette_ptr = 0;
474
475 r = bytestream2_get_byte(&g2) << color_shift;
476 g = bytestream2_get_byte(&g2) << color_shift;
477 b = bytestream2_get_byte(&g2) << color_shift;
478 entry = 0xFFU << 24 | r << 16 | g << 8 | b;
479 if (color_shift == 2)
480 entry |= entry >> 6 & 0x30303;
481 if (s->palette[palette_ptr] != entry)
482 s->new_palette = 1;
483 s->palette[palette_ptr++] = entry;
484 }
485 }
486 break;
487
488 case FLI_DELTA:
489 y_ptr = 0;
490 compressed_lines = bytestream2_get_le16(&g2);
491 while (compressed_lines > 0) {
492 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
493 break;
495 line_packets = sign_extend(bytestream2_get_le16(&g2), 16);
496 if ((line_packets & 0xC000) == 0xC000) {
497 // line skip opcode
498 line_packets = -line_packets;
499 if (line_packets > s->avctx->height)
500 return AVERROR_INVALIDDATA;
501 y_ptr += line_packets * s->frame->linesize[0];
502 } else if ((line_packets & 0xC000) == 0x4000) {
503 av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
504 } else if ((line_packets & 0xC000) == 0x8000) {
505 // "last byte" opcode
506 pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
508 pixels[pixel_ptr] = line_packets & 0xff;
509 } else {
510 compressed_lines--;
511 pixel_ptr = y_ptr;
513 pixel_countdown = s->avctx->width;
514 for (i = 0; i < line_packets; i++) {
515 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
516 break;
517 /* account for the skip bytes */
518 pixel_skip = bytestream2_get_byte(&g2);
519 pixel_ptr += pixel_skip;
520 pixel_countdown -= pixel_skip;
521 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
522 if (byte_run < 0) {
523 byte_run = -byte_run;
524 palette_idx1 = bytestream2_get_byte(&g2);
525 palette_idx2 = bytestream2_get_byte(&g2);
526 CHECK_PIXEL_PTR(byte_run * 2);
527 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
528 pixels[pixel_ptr++] = palette_idx1;
529 pixels[pixel_ptr++] = palette_idx2;
530 }
531 } else {
532 CHECK_PIXEL_PTR(byte_run * 2);
533 if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
534 break;
535 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
536 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
537 }
538 }
539 }
540
541 y_ptr += s->frame->linesize[0];
542 }
543 }
544 break;
545
546 case FLI_LC:
547 /* line compressed */
548 starting_line = bytestream2_get_le16(&g2);
549 if (starting_line >= s->avctx->height)
550 return AVERROR_INVALIDDATA;
551 y_ptr = 0;
552 y_ptr += starting_line * s->frame->linesize[0];
553
554 compressed_lines = bytestream2_get_le16(&g2);
555 while (compressed_lines > 0) {
556 pixel_ptr = y_ptr;
558 pixel_countdown = s->avctx->width;
559 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
560 break;
561 line_packets = bytestream2_get_byte(&g2);
562 if (line_packets > 0) {
563 for (i = 0; i < line_packets; i++) {
564 /* account for the skip bytes */
565 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
566 break;
567 pixel_skip = bytestream2_get_byte(&g2);
568 pixel_ptr += pixel_skip;
569 pixel_countdown -= pixel_skip;
570 byte_run = sign_extend(bytestream2_get_byte(&g2),8);
571 if (byte_run > 0) {
572 CHECK_PIXEL_PTR(byte_run);
573 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
574 break;
575 for (j = 0; j < byte_run; j++, pixel_countdown--) {
576 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
577 }
578 } else if (byte_run < 0) {
579 byte_run = -byte_run;
580 palette_idx1 = bytestream2_get_byte(&g2);
581 CHECK_PIXEL_PTR(byte_run);
582 for (j = 0; j < byte_run; j++, pixel_countdown--) {
583 pixels[pixel_ptr++] = palette_idx1;
584 }
585 }
586 }
587 }
588
589 y_ptr += s->frame->linesize[0];
590 compressed_lines--;
591 }
592 break;
593
594 case FLI_BLACK:
595 /* set the whole frame to color 0 (which is usually black) */
596 for (int y = 0; y < s->avctx->height; y++)
597 memset(pixels + y * s->frame->linesize[0], 0, s->avctx->width);
598 break;
599
600 case FLI_BRUN:
601 /* Byte run compression: This chunk type only occurs in the first
602 * FLI frame and it will update the entire frame. */
603 y_ptr = 0;
604 for (lines = 0; lines < s->avctx->height; lines++) {
605 pixel_ptr = y_ptr;
606 /* disregard the line packets; instead, iterate through all
607 * pixels on a row */
608 bytestream2_skip(&g2, 1);
609 pixel_countdown = s->avctx->width;
610 while (pixel_countdown > 0) {
611 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
612 break;
613 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
614 if (!byte_run) {
615 av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
616 return AVERROR_INVALIDDATA;
617 }
618
619 if (byte_run > 0) {
620 palette_idx1 = bytestream2_get_byte(&g2);
621 CHECK_PIXEL_PTR(byte_run);
622 for (j = 0; j < byte_run; j++) {
623 pixels[pixel_ptr++] = palette_idx1;
624 pixel_countdown--;
625 if (pixel_countdown < 0)
626 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
627 pixel_countdown, lines);
628 }
629 } else { /* copy bytes if byte_run < 0 */
630 byte_run = -byte_run;
631 CHECK_PIXEL_PTR(byte_run);
632 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
633 break;
634 for (j = 0; j < byte_run; j++) {
635 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
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 y_ptr += s->frame->linesize[0];
645 }
646 break;
647
648 case FLI_COPY:
649 /* copy the chunk (uncompressed frame) */
650 if (chunk_size - 6 != FFALIGN(s->avctx->width, 4) * s->avctx->height) {
651 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
652 "has incorrect size, skipping chunk\n", chunk_size - 6);
653 bytestream2_skip(&g2, chunk_size - 6);
654 } else {
655 for (y_ptr = 0; check_pixel_ptr(y_ptr, s->avctx->width, pixel_lo, pixel_hi) == 0;
656 y_ptr += s->frame->linesize[0]) {
657 bytestream2_get_buffer(&g2, &pixels[y_ptr],
658 s->avctx->width);
659 if (s->avctx->width & 3)
660 bytestream2_skip(&g2, 4 - (s->avctx->width & 3));
661 }
662 }
663 break;
664
665 case FLI_MINI:
666 /* some sort of a thumbnail? disregard this chunk... */
667 break;
668
669 default:
670 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
671 break;
672 }
673
674 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
675 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
676 } else {
677 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
678 break;
679 }
680
681 frame_size -= chunk_size;
682 num_chunks--;
683 }
684
685 /* by the end of the chunk, the stream ptr should equal the frame
686 * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
687 if (bytestream2_get_bytes_left(&g2) > 2)
688 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
689 "and final chunk ptr = %d\n", buf_size,
690 buf_size - bytestream2_get_bytes_left(&g2));
691
692 /* make the palette available on the way out */
693 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
694 if (s->new_palette) {
695 s->new_palette = 0;
696 }
697
698 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
699 return ret;
700
701 *got_frame = 1;
702
703 return buf_size;
704}
705
707 AVFrame *rframe, int *got_frame,
708 const uint8_t *buf, int buf_size)
709{
710 /* Note, the only difference between the 15Bpp and 16Bpp */
711 /* Format is the pixel format, the packets are processed the same. */
712 FlicDecodeContext *s = avctx->priv_data;
713
715 ptrdiff_t pixel_ptr;
716 unsigned char palette_idx1;
717
718 unsigned int frame_size;
719 int num_chunks;
720
721 unsigned int chunk_size;
722 int chunk_type;
723
724 int i, j, ret;
725
726 int lines;
727 int compressed_lines;
728 int line_packets;
729 ptrdiff_t y_ptr;
730 int byte_run;
731 int pixel_skip;
732 int pixel_countdown;
733 unsigned char *pixels;
734 int pixel;
735 ptrdiff_t pixel_lo, pixel_hi;
736
737 bytestream2_init(&g2, buf, buf_size);
738
739 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
740 return ret;
741
742 pixels = s->frame->data[0];
743 pixel_range(s->frame, &pixel_lo, &pixel_hi);
744
745 frame_size = bytestream2_get_le32(&g2);
746 bytestream2_skip(&g2, 2); /* skip the magic number */
747 num_chunks = bytestream2_get_le16(&g2);
748 bytestream2_skip(&g2, 8); /* skip padding */
749 if (frame_size > buf_size)
750 frame_size = buf_size;
751
752 if (frame_size < 16)
753 return AVERROR_INVALIDDATA;
754 frame_size -= 16;
755
756 /* iterate through the chunks */
757 while ((frame_size > 0) && (num_chunks > 0) &&
758 bytestream2_get_bytes_left(&g2) >= 4) {
759 int stream_ptr_after_chunk;
760 chunk_size = bytestream2_get_le32(&g2);
761 if (chunk_size > frame_size) {
762 av_log(avctx, AV_LOG_WARNING,
763 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
764 chunk_size = frame_size;
765 }
766 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
767
768 chunk_type = bytestream2_get_le16(&g2);
769
770
771 switch (chunk_type) {
772 case FLI_256_COLOR:
773 case FLI_COLOR:
774 /* For some reason, it seems that non-palettized flics do
775 * include one of these chunks in their first frame.
776 * Why I do not know, it seems rather extraneous. */
777 ff_dlog(avctx,
778 "Unexpected Palette chunk %d in non-palettized FLC\n",
779 chunk_type);
780 bytestream2_skip(&g2, chunk_size - 6);
781 break;
782
783 case FLI_DELTA:
784 case FLI_DTA_LC:
785 y_ptr = 0;
786 compressed_lines = bytestream2_get_le16(&g2);
787 while (compressed_lines > 0) {
788 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
789 break;
791 line_packets = sign_extend(bytestream2_get_le16(&g2), 16);
792 if (line_packets < 0) {
793 line_packets = -line_packets;
794 if (line_packets > s->avctx->height)
795 return AVERROR_INVALIDDATA;
796 y_ptr += line_packets * s->frame->linesize[0];
797 } else {
798 compressed_lines--;
799 pixel_ptr = y_ptr;
801 pixel_countdown = s->avctx->width;
802 for (i = 0; i < line_packets; i++) {
803 /* account for the skip bytes */
804 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
805 break;
806 pixel_skip = bytestream2_get_byte(&g2);
807 pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
808 pixel_countdown -= pixel_skip;
809 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
810 if (byte_run < 0) {
811 byte_run = -byte_run;
812 pixel = bytestream2_get_le16(&g2);
813 CHECK_PIXEL_PTR(2 * byte_run);
814 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
815 *((signed short*)(&pixels[pixel_ptr])) = pixel;
816 pixel_ptr += 2;
817 }
818 } else {
819 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
820 break;
821 CHECK_PIXEL_PTR(2 * byte_run);
822 for (j = 0; j < byte_run; j++, pixel_countdown--) {
823 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
824 pixel_ptr += 2;
825 }
826 }
827 }
828
829 y_ptr += s->frame->linesize[0];
830 }
831 }
832 break;
833
834 case FLI_LC:
835 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
836 bytestream2_skip(&g2, chunk_size - 6);
837 break;
838
839 case FLI_BLACK:
840 /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
841 for (int y = 0; y < s->avctx->height; y++)
842 memset(pixels + y * s->frame->linesize[0], 0, s->avctx->width * 2);
843 break;
844
845 case FLI_BRUN:
846 y_ptr = 0;
847 for (lines = 0; lines < s->avctx->height; lines++) {
848 pixel_ptr = y_ptr;
849 /* disregard the line packets; instead, iterate through all
850 * pixels on a row */
851 bytestream2_skip(&g2, 1);
852 pixel_countdown = (s->avctx->width * 2);
853
854 while (pixel_countdown > 0) {
855 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
856 break;
857 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
858 if (byte_run > 0) {
859 palette_idx1 = bytestream2_get_byte(&g2);
860 CHECK_PIXEL_PTR(byte_run);
861 for (j = 0; j < byte_run; j++) {
862 pixels[pixel_ptr++ ^ HAVE_BIGENDIAN] = palette_idx1;
863 pixel_countdown--;
864 if (pixel_countdown < 0)
865 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
866 pixel_countdown, lines);
867 }
868 } else { /* copy bytes if byte_run < 0 */
869 byte_run = -byte_run;
870 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
871 break;
872 CHECK_PIXEL_PTR(byte_run);
873 for (j = 0; j < byte_run; j++) {
874 palette_idx1 = bytestream2_get_byte(&g2);
875 pixels[pixel_ptr++ ^ HAVE_BIGENDIAN] = palette_idx1;
876 pixel_countdown--;
877 if (pixel_countdown < 0)
878 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
879 pixel_countdown, lines);
880 }
881 }
882 }
883 y_ptr += s->frame->linesize[0];
884 }
885 break;
886
887 case FLI_DTA_BRUN:
888 y_ptr = 0;
889 for (lines = 0; lines < s->avctx->height; lines++) {
890 pixel_ptr = y_ptr;
891 /* disregard the line packets; instead, iterate through all
892 * pixels on a row */
893 bytestream2_skip(&g2, 1);
894 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
895
896 while (pixel_countdown > 0) {
897 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
898 break;
899 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
900 if (byte_run > 0) {
901 pixel = bytestream2_get_le16(&g2);
902 CHECK_PIXEL_PTR(2 * byte_run);
903 for (j = 0; j < byte_run; j++) {
904 *((signed short*)(&pixels[pixel_ptr])) = pixel;
905 pixel_ptr += 2;
906 pixel_countdown--;
907 if (pixel_countdown < 0)
908 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
909 pixel_countdown);
910 }
911 } else { /* copy pixels if byte_run < 0 */
912 byte_run = -byte_run;
913 if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
914 break;
915 CHECK_PIXEL_PTR(2 * byte_run);
916 for (j = 0; j < byte_run; j++) {
917 *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
918 pixel_ptr += 2;
919 pixel_countdown--;
920 if (pixel_countdown < 0)
921 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
922 pixel_countdown);
923 }
924 }
925 }
926
927 y_ptr += s->frame->linesize[0];
928 }
929 break;
930
931 case FLI_COPY:
932 case FLI_DTA_COPY:
933 /* copy the chunk (uncompressed frame) */
934 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
935 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
936 "bigger than image, skipping chunk\n", chunk_size - 6);
937 bytestream2_skip(&g2, chunk_size - 6);
938 } else {
939
940 if (bytestream2_get_bytes_left(&g2) < 2 * s->avctx->width * s->avctx->height )
941 return AVERROR_INVALIDDATA;
942 for (y_ptr = 0; check_pixel_ptr(y_ptr, 2*s->avctx->width, pixel_lo, pixel_hi) == 0;
943 y_ptr += s->frame->linesize[0]) {
944
945 pixel_countdown = s->avctx->width;
946 pixel_ptr = 0;
947 while (pixel_countdown > 0) {
948 *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
949 pixel_ptr += 2;
950 pixel_countdown--;
951 }
952 if (s->avctx->width & 1)
953 bytestream2_skip(&g2, 2);
954 }
955 }
956 break;
957
958 case FLI_MINI:
959 /* some sort of a thumbnail? disregard this chunk... */
960 bytestream2_skip(&g2, chunk_size - 6);
961 break;
962
963 default:
964 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
965 break;
966 }
967
968 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
969 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
970 } else {
971 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
972 break;
973 }
974
975 frame_size -= chunk_size;
976 num_chunks--;
977 }
978
979 /* by the end of the chunk, the stream ptr should equal the frame
980 * size (minus 1, possibly); if it doesn't, issue a warning */
981 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
982 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
983 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
984
985 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
986 return ret;
987
988 *got_frame = 1;
989
990 return buf_size;
991}
992
994 AVFrame *rframe, int *got_frame,
995 const uint8_t *buf, int buf_size)
996{
997 FlicDecodeContext *s = avctx->priv_data;
998
1000 ptrdiff_t pixel_ptr;
1001 unsigned char palette_idx1;
1002
1003 unsigned int frame_size;
1004 int num_chunks;
1005
1006 unsigned int chunk_size;
1007 int chunk_type;
1008
1009 int i, j, ret;
1010
1011 int lines;
1012 int compressed_lines;
1013 int line_packets;
1014 ptrdiff_t y_ptr;
1015 int byte_run;
1016 int pixel_skip;
1017 int pixel_countdown;
1018 unsigned char *pixels;
1019 int pixel;
1020 ptrdiff_t pixel_lo, pixel_hi;
1021
1022 bytestream2_init(&g2, buf, buf_size);
1023
1024 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
1025 return ret;
1026
1027 pixels = s->frame->data[0];
1028 pixel_range(s->frame, &pixel_lo, &pixel_hi);
1029
1030 frame_size = bytestream2_get_le32(&g2);
1031 bytestream2_skip(&g2, 2); /* skip the magic number */
1032 num_chunks = bytestream2_get_le16(&g2);
1033 bytestream2_skip(&g2, 8); /* skip padding */
1034 if (frame_size > buf_size)
1035 frame_size = buf_size;
1036
1037 if (frame_size < 16)
1038 return AVERROR_INVALIDDATA;
1039 frame_size -= 16;
1040
1041 /* iterate through the chunks */
1042 while ((frame_size > 0) && (num_chunks > 0) &&
1043 bytestream2_get_bytes_left(&g2) >= 4) {
1044 int stream_ptr_after_chunk;
1045 chunk_size = bytestream2_get_le32(&g2);
1046 if (chunk_size > frame_size) {
1047 av_log(avctx, AV_LOG_WARNING,
1048 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
1049 chunk_size = frame_size;
1050 }
1051 stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
1052
1053 chunk_type = bytestream2_get_le16(&g2);
1054
1055
1056 switch (chunk_type) {
1057 case FLI_256_COLOR:
1058 case FLI_COLOR:
1059 /* For some reason, it seems that non-palettized flics do
1060 * include one of these chunks in their first frame.
1061 * Why I do not know, it seems rather extraneous. */
1062 ff_dlog(avctx,
1063 "Unexpected Palette chunk %d in non-palettized FLC\n",
1064 chunk_type);
1065 bytestream2_skip(&g2, chunk_size - 6);
1066 break;
1067
1068 case FLI_DELTA:
1069 case FLI_DTA_LC:
1070 y_ptr = 0;
1071 compressed_lines = bytestream2_get_le16(&g2);
1072 while (compressed_lines > 0) {
1073 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
1074 break;
1075 CHECK_Y_PTR()
1076 line_packets = sign_extend(bytestream2_get_le16(&g2), 16);
1077 if (line_packets < 0) {
1078 line_packets = -line_packets;
1079 if (line_packets > s->avctx->height)
1080 return AVERROR_INVALIDDATA;
1081 y_ptr += line_packets * s->frame->linesize[0];
1082 } else {
1083 compressed_lines--;
1084 pixel_ptr = y_ptr;
1085 CHECK_PIXEL_PTR(0);
1086 pixel_countdown = s->avctx->width;
1087 for (i = 0; i < line_packets; i++) {
1088 /* account for the skip bytes */
1089 if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
1090 break;
1091 pixel_skip = bytestream2_get_byte(&g2);
1092 pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
1093 pixel_countdown -= pixel_skip;
1094 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
1095 if (byte_run < 0) {
1096 byte_run = -byte_run;
1097 pixel = bytestream2_get_le24(&g2);
1098 CHECK_PIXEL_PTR(3 * byte_run);
1099 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
1100 AV_WL24(&pixels[pixel_ptr], pixel);
1101 pixel_ptr += 3;
1102 }
1103 } else {
1104 if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
1105 break;
1106 CHECK_PIXEL_PTR(3 * byte_run);
1107 for (j = 0; j < byte_run; j++, pixel_countdown--) {
1108 pixel = bytestream2_get_le24(&g2);
1109 AV_WL24(&pixels[pixel_ptr], pixel);
1110 pixel_ptr += 3;
1111 }
1112 }
1113 }
1114
1115 y_ptr += s->frame->linesize[0];
1116 }
1117 }
1118 break;
1119
1120 case FLI_LC:
1121 av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
1122 bytestream2_skip(&g2, chunk_size - 6);
1123 break;
1124
1125 case FLI_BLACK:
1126 /* set the whole frame to 0x00 which is black for 24 bit mode. */
1127 for (int y = 0; y < s->avctx->height; y++)
1128 memset(pixels + y * s->frame->linesize[0], 0, s->avctx->width * 3);
1129 break;
1130
1131 case FLI_BRUN:
1132 y_ptr = 0;
1133 for (lines = 0; lines < s->avctx->height; lines++) {
1134 pixel_ptr = y_ptr;
1135 /* disregard the line packets; instead, iterate through all
1136 * pixels on a row */
1137 bytestream2_skip(&g2, 1);
1138 pixel_countdown = (s->avctx->width * 3);
1139
1140 while (pixel_countdown > 0) {
1141 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
1142 break;
1143 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
1144 if (byte_run > 0) {
1145 palette_idx1 = bytestream2_get_byte(&g2);
1146 CHECK_PIXEL_PTR(byte_run);
1147 for (j = 0; j < byte_run; j++) {
1148 pixels[pixel_ptr++] = palette_idx1;
1149 pixel_countdown--;
1150 if (pixel_countdown < 0)
1151 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
1152 pixel_countdown, lines);
1153 }
1154 } else { /* copy bytes if byte_run < 0 */
1155 byte_run = -byte_run;
1156 if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
1157 break;
1158 CHECK_PIXEL_PTR(byte_run);
1159 for (j = 0; j < byte_run; j++) {
1160 palette_idx1 = bytestream2_get_byte(&g2);
1161 pixels[pixel_ptr++] = palette_idx1;
1162 pixel_countdown--;
1163 if (pixel_countdown < 0)
1164 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
1165 pixel_countdown, lines);
1166 }
1167 }
1168 }
1169
1170 y_ptr += s->frame->linesize[0];
1171 }
1172 break;
1173
1174 case FLI_DTA_BRUN:
1175 y_ptr = 0;
1176 for (lines = 0; lines < s->avctx->height; lines++) {
1177 pixel_ptr = y_ptr;
1178 /* disregard the line packets; instead, iterate through all
1179 * pixels on a row */
1180 bytestream2_skip(&g2, 1);
1181 pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
1182
1183 while (pixel_countdown > 0) {
1184 if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
1185 break;
1186 byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
1187 if (byte_run > 0) {
1188 pixel = bytestream2_get_le24(&g2);
1189 CHECK_PIXEL_PTR(3 * byte_run);
1190 for (j = 0; j < byte_run; j++) {
1191 AV_WL24(pixels + pixel_ptr, pixel);
1192 pixel_ptr += 3;
1193 pixel_countdown--;
1194 if (pixel_countdown < 0)
1195 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
1196 pixel_countdown);
1197 }
1198 } else { /* copy pixels if byte_run < 0 */
1199 byte_run = -byte_run;
1200 if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
1201 break;
1202 CHECK_PIXEL_PTR(3 * byte_run);
1203 for (j = 0; j < byte_run; j++) {
1204 pixel = bytestream2_get_le24(&g2);
1205 AV_WL24(pixels + pixel_ptr, pixel);
1206 pixel_ptr += 3;
1207 pixel_countdown--;
1208 if (pixel_countdown < 0)
1209 av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
1210 pixel_countdown);
1211 }
1212 }
1213 }
1214
1215 y_ptr += s->frame->linesize[0];
1216 }
1217 break;
1218
1219 case FLI_COPY:
1220 case FLI_DTA_COPY:
1221 /* copy the chunk (uncompressed frame) */
1222 if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
1223 av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
1224 "bigger than image, skipping chunk\n", chunk_size - 6);
1225 bytestream2_skip(&g2, chunk_size - 6);
1226 } else {
1227 for (y_ptr = 0; check_pixel_ptr(y_ptr, 3*s->avctx->width, pixel_lo, pixel_hi) == 0;
1228 y_ptr += s->frame->linesize[0]) {
1229
1230 bytestream2_get_buffer(&g2, pixels + y_ptr, 3*s->avctx->width);
1231 if (s->avctx->width & 1)
1232 bytestream2_skip(&g2, 3);
1233 }
1234 }
1235 break;
1236
1237 case FLI_MINI:
1238 /* some sort of a thumbnail? disregard this chunk... */
1239 bytestream2_skip(&g2, chunk_size - 6);
1240 break;
1241
1242 default:
1243 av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
1244 break;
1245 }
1246
1247 if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
1248 bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
1249 } else {
1250 av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
1251 break;
1252 }
1253
1254 frame_size -= chunk_size;
1255 num_chunks--;
1256 }
1257
1258 /* by the end of the chunk, the stream ptr should equal the frame
1259 * size (minus 1, possibly); if it doesn't, issue a warning */
1260 if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
1261 av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
1262 "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
1263
1264 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
1265 return ret;
1266
1267 *got_frame = 1;
1268
1269 return buf_size;
1270}
1271
1273 int *got_frame, AVPacket *avpkt)
1274{
1275 const uint8_t *buf = avpkt->data;
1276 int buf_size = avpkt->size;
1277 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK) {
1278 return flic_decode_frame_1BPP(avctx, frame, got_frame,
1279 buf, buf_size);
1280 } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1281 return flic_decode_frame_8BPP(avctx, frame, got_frame,
1282 buf, buf_size);
1283 } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
1284 (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
1285 return flic_decode_frame_15_16BPP(avctx, frame, got_frame,
1286 buf, buf_size);
1287 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
1288 return flic_decode_frame_24BPP(avctx, frame, got_frame,
1289 buf, buf_size);
1290 }
1291
1292 /* Should not get here, ever as the pix_fmt is processed */
1293 /* in flic_decode_init and the above if should deal with */
1294 /* the finite set of possibilities allowable by here. */
1295 /* But in case we do, just error out. */
1296 av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
1297 return AVERROR_BUG;
1298}
1299
1300
1302{
1303 FlicDecodeContext *s = avctx->priv_data;
1304
1305 av_frame_free(&s->frame);
1306
1307 return 0;
1308}
1309
1311 .p.name = "flic",
1312 CODEC_LONG_NAME("Autodesk Animator Flic video"),
1313 .p.type = AVMEDIA_TYPE_VIDEO,
1314 .p.id = AV_CODEC_ID_FLIC,
1315 .priv_data_size = sizeof(FlicDecodeContext),
1319 .p.capabilities = AV_CODEC_CAP_DR1,
1320};
const FFCodec ff_flic_decoder
Definition flicvideo.c:1310
#define entry
Libavcodec external API header.
#define pixel
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1943
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
static av_cold int flic_decode_init(AVCodecContext *avctx)
Definition flicvideo.c:107
#define FLI_COPY
Definition flicvideo.c:52
#define FLC_FLX_TYPE_CODE
Definition flicvideo.c:59
static int flic_decode_frame_24BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
Definition flicvideo.c:993
#define FLI_BLACK
Definition flicvideo.c:50
#define FLI_MINI
Definition flicvideo.c:53
static int flic_decode_frame_8BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
Definition flicvideo.c:369
#define FLI_256_COLOR
Definition flicvideo.c:46
#define FLI_DTA_COPY
Definition flicvideo.c:55
static int flic_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition flicvideo.c:1272
static av_cold int flic_decode_end(AVCodecContext *avctx)
Definition flicvideo.c:1301
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
Definition flicvideo.c:61
#define FLI_BRUN
Definition flicvideo.c:51
#define FLI_DTA_BRUN
Definition flicvideo.c:54
static void pixel_range(const AVFrame *frame, ptrdiff_t *lo, ptrdiff_t *hi)
Definition flicvideo.c:85
#define FLI_LC
Definition flicvideo.c:49
#define CHECK_Y_PTR()
Definition flicvideo.c:78
static int check_pixel_ptr(ptrdiff_t ptr, int n, ptrdiff_t lo, ptrdiff_t hi)
Definition flicvideo.c:63
#define FLI_DELTA
Definition flicvideo.c:47
static int flic_decode_frame_1BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
Definition flicvideo.c:178
#define FLI_DTA_LC
Definition flicvideo.c:56
static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
Definition flicvideo.c:706
#define FLI_TYPE_CODE
Definition flicvideo.c:58
#define CHECK_PIXEL_PTR(n)
Definition flicvideo.c:71
#define FLI_COLOR
Definition flicvideo.c:48
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_FLIC
Definition codec_id.h:100
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RL32(p)
#define AV_WL24(p, d)
#define AV_RL16(p)
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
#define AV_PIX_FMT_RGB565
Definition pixfmt.h:535
#define AVPALETTE_SIZE
Definition pixfmt.h:32
#define AV_PIX_FMT_RGB555
Definition pixfmt.h:536
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
unsigned int palette[256]
Definition flicvideo.c:102
AVCodecContext * avctx
Definition flicvideo.c:99
AVFrame * frame
Definition flicvideo.c:100
#define ff_dlog(a,...)
#define av_log(a,...)
const char * g
Definition vf_curves.c:128