47 #define FLI_256_COLOR 4
55 #define FLI_DTA_BRUN 25
56 #define FLI_DTA_COPY 26
59 #define FLI_TYPE_CODE (0xAF11)
60 #define FLC_FLX_TYPE_CODE (0xAF12)
61 #define FLC_DTA_TYPE_CODE (0xAF44)
62 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
65 ptrdiff_t limit,
int direction)
67 if (( direction && ptr + n > limit) ||
68 (!direction && ptr + n < limit))
73 #define CHECK_PIXEL_PTR(n) \
75 ret = check_pixel_ptr(pixel_ptr, (n), pixel_limit, direction); \
80 #define CHECK_Y_PTR() \
82 ret = check_pixel_ptr(y_ptr, 0, pixel_limit, direction); \
99 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
114 if (
s->avctx->extradata_size == 12) {
122 for (
i = 0;
i < 256;
i++) {
135 s->fli_type =
AV_RL16(&fli_header[4]);
136 depth =
AV_RL16(&fli_header[12]);
153 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
167 void *
data,
int *got_frame,
168 const uint8_t *buf,
int buf_size)
175 unsigned char palette_idx1;
176 unsigned char palette_idx2;
181 unsigned int chunk_size;
184 int i, j, ret, direction;
189 unsigned char r,
g,
b;
192 int compressed_lines;
199 unsigned char *pixels;
200 ptrdiff_t pixel_limit;
207 direction =
s->frame->linesize[0] > 0;
208 pixels =
s->frame->data[0];
209 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
216 num_chunks = bytestream2_get_le16(&g2);
225 while ((
frame_size >= 6) && (num_chunks > 0) &&
227 int stream_ptr_after_chunk;
228 chunk_size = bytestream2_get_le32(&g2);
231 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
236 chunk_type = bytestream2_get_le16(&g2);
238 switch (chunk_type) {
250 color_packets = bytestream2_get_le16(&g2);
252 for (
i = 0;
i < color_packets;
i++) {
254 palette_ptr += bytestream2_get_byte(&g2);
257 color_changes = bytestream2_get_byte(&g2);
260 if (color_changes == 0)
266 for (j = 0; j < color_changes; j++) {
270 if ((
unsigned)palette_ptr >= 256)
273 r = bytestream2_get_byte(&g2) << color_shift;
274 g = bytestream2_get_byte(&g2) << color_shift;
275 b = bytestream2_get_byte(&g2) << color_shift;
276 entry = 0xFFU << 24 |
r << 16 |
g << 8 |
b;
277 if (color_shift == 2)
278 entry |= entry >> 6 & 0x30303;
279 if (
s->palette[palette_ptr] != entry)
281 s->palette[palette_ptr++] = entry;
288 compressed_lines = bytestream2_get_le16(&g2);
289 while (compressed_lines > 0) {
293 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
294 if ((line_packets & 0xC000) == 0xC000) {
296 line_packets = -line_packets;
297 if (line_packets >
s->avctx->height)
299 y_ptr += line_packets *
s->frame->linesize[0];
300 }
else if ((line_packets & 0xC000) == 0x4000) {
302 }
else if ((line_packets & 0xC000) == 0x8000) {
304 pixel_ptr= y_ptr +
s->frame->linesize[0] - 1;
306 pixels[pixel_ptr] = line_packets & 0xff;
311 pixel_countdown =
s->avctx->width;
312 for (
i = 0;
i < line_packets;
i++) {
316 pixel_skip = bytestream2_get_byte(&g2);
317 pixel_ptr += pixel_skip;
318 pixel_countdown -= pixel_skip;
319 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
321 byte_run = -byte_run;
322 palette_idx1 = bytestream2_get_byte(&g2);
323 palette_idx2 = bytestream2_get_byte(&g2);
325 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
326 pixels[pixel_ptr++] = palette_idx1;
327 pixels[pixel_ptr++] = palette_idx2;
333 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
334 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
339 y_ptr +=
s->frame->linesize[0];
346 starting_line = bytestream2_get_le16(&g2);
347 if (starting_line >=
s->avctx->height)
350 y_ptr += starting_line *
s->frame->linesize[0];
352 compressed_lines = bytestream2_get_le16(&g2);
353 while (compressed_lines > 0) {
356 pixel_countdown =
s->avctx->width;
359 line_packets = bytestream2_get_byte(&g2);
360 if (line_packets > 0) {
361 for (
i = 0;
i < line_packets;
i++) {
365 pixel_skip = bytestream2_get_byte(&g2);
366 pixel_ptr += pixel_skip;
367 pixel_countdown -= pixel_skip;
368 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
373 for (j = 0; j < byte_run; j++, pixel_countdown--) {
374 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
376 }
else if (byte_run < 0) {
377 byte_run = -byte_run;
378 palette_idx1 = bytestream2_get_byte(&g2);
380 for (j = 0; j < byte_run; j++, pixel_countdown--) {
381 pixels[pixel_ptr++] = palette_idx1;
387 y_ptr +=
s->frame->linesize[0];
394 for (
int y = 0; y <
s->avctx->height; y++)
395 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width);
402 for (lines = 0; lines <
s->avctx->height; lines++) {
407 pixel_countdown =
s->avctx->width;
408 while (pixel_countdown > 0) {
411 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
418 palette_idx1 = bytestream2_get_byte(&g2);
420 for (j = 0; j < byte_run; j++) {
421 pixels[pixel_ptr++] = palette_idx1;
423 if (pixel_countdown < 0)
425 pixel_countdown, lines);
428 byte_run = -byte_run;
432 for (j = 0; j < byte_run; j++) {
433 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
435 if (pixel_countdown < 0)
437 pixel_countdown, lines);
442 y_ptr +=
s->frame->linesize[0];
448 if (chunk_size - 6 !=
FFALIGN(
s->avctx->width, 4) *
s->avctx->height) {
450 "has incorrect size, skipping chunk\n", chunk_size - 6);
453 for (y_ptr = 0;
check_pixel_ptr(y_ptr, 0, pixel_limit, direction) == 0;
454 y_ptr +=
s->frame->linesize[0]) {
457 if (
s->avctx->width & 3)
487 "and final chunk ptr = %d\n", buf_size,
492 if (
s->new_palette) {
493 s->frame->palette_has_changed = 1;
506 void *
data,
int *got_frame,
507 const uint8_t *buf,
int buf_size)
515 unsigned char palette_idx1;
520 unsigned int chunk_size;
523 int i, j, ret, direction;
526 int compressed_lines;
532 unsigned char *pixels;
534 ptrdiff_t pixel_limit;
541 direction =
s->frame->linesize[0] > 0;
542 pixels =
s->frame->data[0];
543 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
547 num_chunks = bytestream2_get_le16(&g2);
557 while ((
frame_size > 0) && (num_chunks > 0) &&
559 int stream_ptr_after_chunk;
560 chunk_size = bytestream2_get_le32(&g2);
563 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
568 chunk_type = bytestream2_get_le16(&g2);
571 switch (chunk_type) {
578 "Unexpected Palette chunk %d in non-palettized FLC\n",
586 compressed_lines = bytestream2_get_le16(&g2);
587 while (compressed_lines > 0) {
591 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
592 if (line_packets < 0) {
593 line_packets = -line_packets;
594 if (line_packets >
s->avctx->height)
596 y_ptr += line_packets *
s->frame->linesize[0];
601 pixel_countdown =
s->avctx->width;
602 for (
i = 0;
i < line_packets;
i++) {
606 pixel_skip = bytestream2_get_byte(&g2);
607 pixel_ptr += (pixel_skip*2);
608 pixel_countdown -= pixel_skip;
609 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
611 byte_run = -byte_run;
612 pixel = bytestream2_get_le16(&g2);
614 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
615 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
622 for (j = 0; j < byte_run; j++, pixel_countdown--) {
623 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
629 y_ptr +=
s->frame->linesize[0];
641 for (
int y = 0; y <
s->avctx->height; y++)
642 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width * 2);
647 for (lines = 0; lines <
s->avctx->height; lines++) {
652 pixel_countdown = (
s->avctx->width * 2);
654 while (pixel_countdown > 0) {
657 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
659 palette_idx1 = bytestream2_get_byte(&g2);
661 for (j = 0; j < byte_run; j++) {
664 if (pixel_countdown < 0)
666 pixel_countdown, lines);
669 byte_run = -byte_run;
673 for (j = 0; j < byte_run; j++) {
674 palette_idx1 = bytestream2_get_byte(&g2);
677 if (pixel_countdown < 0)
679 pixel_countdown, lines);
683 y_ptr +=
s->frame->linesize[0];
689 for (lines = 0; lines <
s->avctx->height; lines++) {
694 pixel_countdown =
s->avctx->width;
696 while (pixel_countdown > 0) {
699 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
701 pixel = bytestream2_get_le16(&g2);
703 for (j = 0; j < byte_run; j++) {
704 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
707 if (pixel_countdown < 0)
712 byte_run = -byte_run;
716 for (j = 0; j < byte_run; j++) {
717 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
720 if (pixel_countdown < 0)
727 y_ptr +=
s->frame->linesize[0];
734 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*2) {
736 "bigger than image, skipping chunk\n", chunk_size - 6);
742 for (y_ptr = 0;
check_pixel_ptr(y_ptr, 0, pixel_limit, direction) == 0;
743 y_ptr +=
s->frame->linesize[0]) {
745 pixel_countdown =
s->avctx->width;
747 while (pixel_countdown > 0) {
748 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
752 if (
s->avctx->width & 1)
794 void *
data,
int *got_frame,
795 const uint8_t *buf,
int buf_size)
801 unsigned char palette_idx1;
806 unsigned int chunk_size;
809 int i, j, ret, direction;
812 int compressed_lines;
818 unsigned char *pixels;
820 ptrdiff_t pixel_limit;
827 direction =
s->frame->linesize[0] > 0;
828 pixels =
s->frame->data[0];
829 pixel_limit =
s->avctx->height *
s->frame->linesize[0];
833 num_chunks = bytestream2_get_le16(&g2);
843 while ((
frame_size > 0) && (num_chunks > 0) &&
845 int stream_ptr_after_chunk;
846 chunk_size = bytestream2_get_le32(&g2);
849 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
854 chunk_type = bytestream2_get_le16(&g2);
857 switch (chunk_type) {
864 "Unexpected Palette chunk %d in non-palettized FLC\n",
872 compressed_lines = bytestream2_get_le16(&g2);
873 while (compressed_lines > 0) {
877 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
878 if (line_packets < 0) {
879 line_packets = -line_packets;
880 if (line_packets >
s->avctx->height)
882 y_ptr += line_packets *
s->frame->linesize[0];
887 pixel_countdown =
s->avctx->width;
888 for (
i = 0;
i < line_packets;
i++) {
892 pixel_skip = bytestream2_get_byte(&g2);
893 pixel_ptr += (pixel_skip*3);
894 pixel_countdown -= pixel_skip;
895 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
897 byte_run = -byte_run;
898 pixel = bytestream2_get_le24(&g2);
900 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
908 for (j = 0; j < byte_run; j++, pixel_countdown--) {
909 pixel = bytestream2_get_le24(&g2);
916 y_ptr +=
s->frame->linesize[0];
928 for (
int y = 0; y <
s->avctx->height; y++)
929 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width * 3);
934 for (lines = 0; lines <
s->avctx->height; lines++) {
939 pixel_countdown = (
s->avctx->width * 3);
941 while (pixel_countdown > 0) {
944 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
946 palette_idx1 = bytestream2_get_byte(&g2);
948 for (j = 0; j < byte_run; j++) {
949 pixels[pixel_ptr++] = palette_idx1;
951 if (pixel_countdown < 0)
953 pixel_countdown, lines);
956 byte_run = -byte_run;
960 for (j = 0; j < byte_run; j++) {
961 palette_idx1 = bytestream2_get_byte(&g2);
962 pixels[pixel_ptr++] = palette_idx1;
964 if (pixel_countdown < 0)
966 pixel_countdown, lines);
971 y_ptr +=
s->frame->linesize[0];
977 for (lines = 0; lines <
s->avctx->height; lines++) {
982 pixel_countdown =
s->avctx->width;
984 while (pixel_countdown > 0) {
987 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
989 pixel = bytestream2_get_le24(&g2);
991 for (j = 0; j < byte_run; j++) {
995 if (pixel_countdown < 0)
1000 byte_run = -byte_run;
1004 for (j = 0; j < byte_run; j++) {
1005 pixel = bytestream2_get_le24(&g2);
1009 if (pixel_countdown < 0)
1016 y_ptr +=
s->frame->linesize[0];
1023 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*3) {
1025 "bigger than image, skipping chunk\n", chunk_size - 6);
1028 for (y_ptr = 0;
check_pixel_ptr(y_ptr, 0, pixel_limit, direction) == 0;
1029 y_ptr +=
s->frame->linesize[0]) {
1032 if (
s->avctx->width & 1)
1074 void *
data,
int *got_frame,
1078 int buf_size = avpkt->
size;
1095 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_tell(GetByteContext *g)
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.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static av_cold int flic_decode_init(AVCodecContext *avctx)
#define FLC_FLX_TYPE_CODE
static int check_pixel_ptr(ptrdiff_t ptr, int n, ptrdiff_t limit, int direction)
static av_cold int flic_decode_end(AVCodecContext *avctx)
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
static int flic_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static int flic_decode_frame_8BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
static int flic_decode_frame_24BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
#define CHECK_PIXEL_PTR(n)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_const int sign_extend(int val, unsigned bits)
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
#define AV_PIX_FMT_RGB565
#define AV_PIX_FMT_RGB555
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
unsigned int palette[256]