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)
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; \
83 unsigned char *fli_header = (
unsigned char *)avctx->
extradata;
106 for (i = 0; i < 256; i++) {
151 void *
data,
int *got_frame,
159 unsigned char palette_idx1;
160 unsigned char palette_idx2;
165 unsigned int chunk_size;
173 unsigned char r,
g,
b;
176 int compressed_lines;
178 signed short line_packets;
184 unsigned int pixel_limit;
195 frame_size = bytestream2_get_le32(&g2);
196 if (frame_size > buf_size)
197 frame_size = buf_size;
199 num_chunks = bytestream2_get_le16(&g2);
208 while ((frame_size >= 6) && (num_chunks > 0) &&
210 int stream_ptr_after_chunk;
211 chunk_size = bytestream2_get_le32(&g2);
212 if (chunk_size > frame_size) {
214 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
219 chunk_type = bytestream2_get_le16(&g2);
221 switch (chunk_type) {
233 color_packets = bytestream2_get_le16(&g2);
235 for (i = 0; i < color_packets; i++) {
237 palette_ptr += bytestream2_get_byte(&g2);
240 color_changes = bytestream2_get_byte(&g2);
243 if (color_changes == 0)
249 for (j = 0; j < color_changes; j++) {
253 if ((
unsigned)palette_ptr >= 256)
256 r = bytestream2_get_byte(&g2) << color_shift;
257 g = bytestream2_get_byte(&g2) << color_shift;
258 b = bytestream2_get_byte(&g2) << color_shift;
259 entry = 0xFF
U << 24 | r << 16 | g << 8 |
b;
260 if (color_shift == 2)
261 entry |= entry >> 6 & 0x30303;
262 if (s->
palette[palette_ptr] != entry)
264 s->
palette[palette_ptr++] = entry;
271 compressed_lines = bytestream2_get_le16(&g2);
272 while (compressed_lines > 0) {
275 if (y_ptr > pixel_limit)
277 line_packets = bytestream2_get_le16(&g2);
278 if ((line_packets & 0xC000) == 0xC000) {
280 line_packets = -line_packets;
284 }
else if ((line_packets & 0xC000) == 0x4000) {
286 }
else if ((line_packets & 0xC000) == 0x8000) {
290 pixels[pixel_ptr] = line_packets & 0xff;
296 for (i = 0; i < line_packets; i++) {
300 pixel_skip = bytestream2_get_byte(&g2);
301 pixel_ptr += pixel_skip;
302 pixel_countdown -= pixel_skip;
303 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
305 byte_run = -byte_run;
306 palette_idx1 = bytestream2_get_byte(&g2);
307 palette_idx2 = bytestream2_get_byte(&g2);
309 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
310 pixels[pixel_ptr++] = palette_idx1;
311 pixels[pixel_ptr++] = palette_idx2;
317 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
318 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
330 starting_line = bytestream2_get_le16(&g2);
336 compressed_lines = bytestream2_get_le16(&g2);
337 while (compressed_lines > 0) {
343 line_packets = bytestream2_get_byte(&g2);
344 if (line_packets > 0) {
345 for (i = 0; i < line_packets; i++) {
349 pixel_skip = bytestream2_get_byte(&g2);
350 pixel_ptr += pixel_skip;
351 pixel_countdown -= pixel_skip;
352 byte_run =
sign_extend(bytestream2_get_byte(&g2),8);
357 for (j = 0; j < byte_run; j++, pixel_countdown--) {
358 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
360 }
else if (byte_run < 0) {
361 byte_run = -byte_run;
362 palette_idx1 = bytestream2_get_byte(&g2);
364 for (j = 0; j < byte_run; j++, pixel_countdown--) {
365 pixels[pixel_ptr++] = palette_idx1;
386 for (lines = 0; lines < s->
avctx->
height; lines++) {
392 while (pixel_countdown > 0) {
395 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
402 palette_idx1 = bytestream2_get_byte(&g2);
404 for (j = 0; j < byte_run; j++) {
405 pixels[pixel_ptr++] = palette_idx1;
407 if (pixel_countdown < 0)
409 pixel_countdown, lines);
412 byte_run = -byte_run;
416 for (j = 0; j < byte_run; j++) {
417 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
419 if (pixel_countdown < 0)
421 pixel_countdown, lines);
434 "has incorrect size, skipping chunk\n", chunk_size - 6);
459 frame_size -= chunk_size;
467 "and final chunk ptr = %d\n", buf_size,
486 void *
data,
int *got_frame,
495 unsigned char palette_idx1;
500 unsigned int chunk_size;
506 int compressed_lines;
507 signed short line_packets;
514 unsigned int pixel_limit;
524 frame_size = bytestream2_get_le32(&g2);
526 num_chunks = bytestream2_get_le16(&g2);
528 if (frame_size > buf_size)
529 frame_size = buf_size;
536 while ((frame_size > 0) && (num_chunks > 0) &&
538 int stream_ptr_after_chunk;
539 chunk_size = bytestream2_get_le32(&g2);
540 if (chunk_size > frame_size) {
542 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
547 chunk_type = bytestream2_get_le16(&g2);
550 switch (chunk_type) {
557 "Unexpected Palette chunk %d in non-palettized FLC\n",
565 compressed_lines = bytestream2_get_le16(&g2);
566 while (compressed_lines > 0) {
569 if (y_ptr > pixel_limit)
571 line_packets = bytestream2_get_le16(&g2);
572 if (line_packets < 0) {
573 line_packets = -line_packets;
582 for (i = 0; i < line_packets; i++) {
586 pixel_skip = bytestream2_get_byte(&g2);
587 pixel_ptr += (pixel_skip*2);
588 pixel_countdown -= pixel_skip;
589 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
591 byte_run = -byte_run;
592 pixel = bytestream2_get_le16(&g2);
594 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
595 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
602 for (j = 0; j < byte_run; j++, pixel_countdown--) {
603 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
621 memset(pixels, 0x0000,
627 for (lines = 0; lines < s->
avctx->
height; lines++) {
634 while (pixel_countdown > 0) {
637 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
639 palette_idx1 = bytestream2_get_byte(&g2);
641 for (j = 0; j < byte_run; j++) {
642 pixels[pixel_ptr++] = palette_idx1;
644 if (pixel_countdown < 0)
646 pixel_countdown, lines);
649 byte_run = -byte_run;
653 for (j = 0; j < byte_run; j++) {
654 palette_idx1 = bytestream2_get_byte(&g2);
655 pixels[pixel_ptr++] = palette_idx1;
657 if (pixel_countdown < 0)
659 pixel_countdown, lines);
672 while (pixel_countdown > 0) {
673 *((
signed short*)(&pixels[pixel_ptr])) =
AV_RL16(&buf[pixel_ptr]);
683 for (lines = 0; lines < s->
avctx->
height; lines++) {
690 while (pixel_countdown > 0) {
693 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
695 pixel = bytestream2_get_le16(&g2);
697 for (j = 0; j < byte_run; j++) {
698 *((
signed short*)(&pixels[pixel_ptr])) = pixel;
701 if (pixel_countdown < 0)
706 byte_run = -byte_run;
710 for (j = 0; j < byte_run; j++) {
711 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
714 if (pixel_countdown < 0)
730 "bigger than image, skipping chunk\n", chunk_size - 6);
739 while (pixel_countdown > 0) {
740 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
760 frame_size -= chunk_size;
779 void *
data,
int *got_frame,
786 unsigned char palette_idx1;
791 unsigned int chunk_size;
797 int compressed_lines;
798 signed short line_packets;
805 unsigned int pixel_limit;
815 frame_size = bytestream2_get_le32(&g2);
817 num_chunks = bytestream2_get_le16(&g2);
819 if (frame_size > buf_size)
820 frame_size = buf_size;
827 while ((frame_size > 0) && (num_chunks > 0) &&
829 int stream_ptr_after_chunk;
830 chunk_size = bytestream2_get_le32(&g2);
831 if (chunk_size > frame_size) {
833 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
838 chunk_type = bytestream2_get_le16(&g2);
841 switch (chunk_type) {
848 "Unexpected Palette chunk %d in non-palettized FLC\n",
856 compressed_lines = bytestream2_get_le16(&g2);
857 while (compressed_lines > 0) {
860 if (y_ptr > pixel_limit)
862 line_packets = bytestream2_get_le16(&g2);
863 if (line_packets < 0) {
864 line_packets = -line_packets;
873 for (i = 0; i < line_packets; i++) {
877 pixel_skip = bytestream2_get_byte(&g2);
878 pixel_ptr += (pixel_skip*3);
879 pixel_countdown -= pixel_skip;
880 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
882 byte_run = -byte_run;
883 pixel = bytestream2_get_le24(&g2);
885 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
886 AV_WL24(&pixels[pixel_ptr], pixel);
893 for (j = 0; j < byte_run; j++, pixel_countdown--) {
894 pixel = bytestream2_get_le24(&g2);
895 AV_WL24(&pixels[pixel_ptr], pixel);
919 for (lines = 0; lines < s->
avctx->
height; lines++) {
926 while (pixel_countdown > 0) {
929 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
931 palette_idx1 = bytestream2_get_byte(&g2);
933 for (j = 0; j < byte_run; j++) {
934 pixels[pixel_ptr++] = palette_idx1;
936 if (pixel_countdown < 0)
938 pixel_countdown, lines);
941 byte_run = -byte_run;
945 for (j = 0; j < byte_run; j++) {
946 palette_idx1 = bytestream2_get_byte(&g2);
947 pixels[pixel_ptr++] = palette_idx1;
949 if (pixel_countdown < 0)
951 pixel_countdown, lines);
962 for (lines = 0; lines < s->
avctx->
height; lines++) {
969 while (pixel_countdown > 0) {
972 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
974 pixel = bytestream2_get_le24(&g2);
976 for (j = 0; j < byte_run; j++) {
977 AV_WL24(pixels + pixel_ptr, pixel);
980 if (pixel_countdown < 0)
985 byte_run = -byte_run;
989 for (j = 0; j < byte_run; j++) {
990 pixel = bytestream2_get_le24(&g2);
991 AV_WL24(pixels + pixel_ptr, pixel);
994 if (pixel_countdown < 0)
1010 "bigger than image, skipping chunk\n", chunk_size - 6);
1018 while (pixel_countdown > 0) {
1019 pixel = bytestream2_get_le24(&g2);
1020 AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
1040 frame_size -= chunk_size;
1059 void *
data,
int *got_frame,
1063 int buf_size = avpkt->
size;
1080 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
#define CHECK_PIXEL_PTR(n)
static av_cold int init(AVCodecContext *avctx)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, 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)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
static int flic_decode_frame_8BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
8 bits with AV_PIX_FMT_RGB32 palette
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int flic_decode_frame_24BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
const char * name
Name of the codec implementation.
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
int width
picture width / height.
#define FLC_FLX_TYPE_CODE
packed RGB 8:8:8, 24bpp, BGRBGR...
static av_always_inline int bytestream2_tell(GetByteContext *g)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
static int flic_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static av_cold int flic_decode_end(AVCodecContext *avctx)
int palette_has_changed
Tell user application that palette has changed from previous frame.
static av_const int sign_extend(int val, unsigned bits)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
#define AV_PIX_FMT_RGB555
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
unsigned int palette[256]
#define AV_PIX_FMT_RGB565
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
This structure stores compressed data.
static av_cold int flic_decode_init(AVCodecContext *avctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.