46#define FLI_256_COLOR 4
54#define FLI_DTA_BRUN 25
55#define FLI_DTA_COPY 26
58#define FLI_TYPE_CODE (0xAF11)
59#define FLC_FLX_TYPE_CODE (0xAF12)
60#define FLC_DTA_TYPE_CODE (0xAF44)
61#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
64 ptrdiff_t lo, ptrdiff_t hi)
66 if (ptr < lo || ptr + n > hi)
71#define CHECK_PIXEL_PTR(n) \
73 ret = check_pixel_ptr(pixel_ptr, (n), pixel_lo, pixel_hi); \
78#define CHECK_Y_PTR() \
80 ret = check_pixel_ptr(y_ptr, 0, pixel_lo, pixel_hi); \
87 ptrdiff_t linesize =
frame->linesize[0];
91 *hi =
frame->height * linesize;
93 *lo = (
frame->height - 1) * linesize;
125 if (
s->avctx->extradata_size == 12) {
133 for (
i = 0;
i < 256;
i++) {
146 s->fli_type =
AV_RL16(&fli_header[4]);
147 depth =
AV_RL16(&fli_header[12]);
165 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
179 AVFrame *rframe,
int *got_frame,
180 const uint8_t *buf,
int buf_size)
190 unsigned int chunk_size;
196 int compressed_lines;
203 unsigned char *pixels;
204 ptrdiff_t pixel_lo, pixel_hi;
211 pixels =
s->frame->data[0];
219 num_chunks = bytestream2_get_le16(&g2);
228 while ((
frame_size >= 6) && (num_chunks > 0) &&
230 int stream_ptr_after_chunk;
231 chunk_size = bytestream2_get_le32(&g2);
234 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
239 chunk_type = bytestream2_get_le16(&g2);
241 switch (chunk_type) {
246 for (lines = 0; lines <
s->avctx->height; lines++) {
251 pixel_countdown = (
s->avctx->width + 7) >> 3;
252 while (pixel_countdown > 0) {
255 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
262 int value = bytestream2_get_byte(&g2);
264 for (j = 0; j < byte_run; j++) {
265 pixels[pixel_ptr++] =
value;
267 if (pixel_countdown < 0)
269 pixel_countdown, lines);
272 byte_run = -byte_run;
276 for (j = 0; j < byte_run; j++) {
277 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
279 if (pixel_countdown < 0)
281 pixel_countdown, lines);
286 y_ptr +=
s->frame->linesize[0];
292 starting_line = bytestream2_get_le16(&g2);
293 if (starting_line >=
s->avctx->height)
296 y_ptr += starting_line *
s->frame->linesize[0];
298 compressed_lines = bytestream2_get_le16(&g2);
299 while (compressed_lines > 0) {
302 pixel_countdown = (
s->avctx->width + 7) >> 3;
305 line_packets = bytestream2_get_byte(&g2);
306 if (line_packets > 0) {
307 for (
i = 0;
i < line_packets;
i++) {
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);
319 for (j = 0; j < byte_run; j++, pixel_countdown--) {
320 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
322 }
else if (byte_run < 0) {
323 int value = bytestream2_get_byte(&g2);
324 byte_run = -byte_run;
326 for (j = 0; j < byte_run; j++, pixel_countdown--) {
327 pixels[pixel_ptr++] =
value;
333 y_ptr +=
s->frame->linesize[0];
358 "and final chunk ptr = %d\n", buf_size,
370 AVFrame *rframe,
int *got_frame,
371 const uint8_t *buf,
int buf_size)
378 unsigned char palette_idx1;
379 unsigned char palette_idx2;
384 unsigned int chunk_size;
392 unsigned char r,
g,
b;
395 int compressed_lines;
402 unsigned char *pixels;
403 ptrdiff_t pixel_lo, pixel_hi;
410 pixels =
s->frame->data[0];
418 num_chunks = bytestream2_get_le16(&g2);
427 while ((
frame_size >= 6) && (num_chunks > 0) &&
429 int stream_ptr_after_chunk;
430 chunk_size = bytestream2_get_le32(&g2);
433 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
438 chunk_type = bytestream2_get_le16(&g2);
440 switch (chunk_type) {
452 color_packets = bytestream2_get_le16(&g2);
454 for (
i = 0;
i < color_packets;
i++) {
456 palette_ptr += bytestream2_get_byte(&g2);
459 color_changes = bytestream2_get_byte(&g2);
462 if (color_changes == 0)
468 for (j = 0; j < color_changes; j++) {
472 if ((
unsigned)palette_ptr >= 256)
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)
481 if (
s->palette[palette_ptr] !=
entry)
483 s->palette[palette_ptr++] =
entry;
490 compressed_lines = bytestream2_get_le16(&g2);
491 while (compressed_lines > 0) {
495 line_packets =
sign_extend(bytestream2_get_le16(&g2), 16);
496 if ((line_packets & 0xC000) == 0xC000) {
498 line_packets = -line_packets;
499 if (line_packets >
s->avctx->height)
501 y_ptr += line_packets *
s->frame->linesize[0];
502 }
else if ((line_packets & 0xC000) == 0x4000) {
504 }
else if ((line_packets & 0xC000) == 0x8000) {
506 pixel_ptr= y_ptr +
s->frame->linesize[0] - 1;
508 pixels[pixel_ptr] = line_packets & 0xff;
513 pixel_countdown =
s->avctx->width;
514 for (
i = 0;
i < line_packets;
i++) {
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);
523 byte_run = -byte_run;
524 palette_idx1 = bytestream2_get_byte(&g2);
525 palette_idx2 = bytestream2_get_byte(&g2);
527 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
528 pixels[pixel_ptr++] = palette_idx1;
529 pixels[pixel_ptr++] = palette_idx2;
535 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
536 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
541 y_ptr +=
s->frame->linesize[0];
548 starting_line = bytestream2_get_le16(&g2);
549 if (starting_line >=
s->avctx->height)
552 y_ptr += starting_line *
s->frame->linesize[0];
554 compressed_lines = bytestream2_get_le16(&g2);
555 while (compressed_lines > 0) {
558 pixel_countdown =
s->avctx->width;
561 line_packets = bytestream2_get_byte(&g2);
562 if (line_packets > 0) {
563 for (
i = 0;
i < line_packets;
i++) {
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);
575 for (j = 0; j < byte_run; j++, pixel_countdown--) {
576 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
578 }
else if (byte_run < 0) {
579 byte_run = -byte_run;
580 palette_idx1 = bytestream2_get_byte(&g2);
582 for (j = 0; j < byte_run; j++, pixel_countdown--) {
583 pixels[pixel_ptr++] = palette_idx1;
589 y_ptr +=
s->frame->linesize[0];
596 for (
int y = 0; y <
s->avctx->height; y++)
597 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width);
604 for (lines = 0; lines <
s->avctx->height; lines++) {
609 pixel_countdown =
s->avctx->width;
610 while (pixel_countdown > 0) {
613 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
620 palette_idx1 = bytestream2_get_byte(&g2);
622 for (j = 0; j < byte_run; j++) {
623 pixels[pixel_ptr++] = palette_idx1;
625 if (pixel_countdown < 0)
627 pixel_countdown, lines);
630 byte_run = -byte_run;
634 for (j = 0; j < byte_run; j++) {
635 pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
637 if (pixel_countdown < 0)
639 pixel_countdown, lines);
644 y_ptr +=
s->frame->linesize[0];
650 if (chunk_size - 6 !=
FFALIGN(
s->avctx->width, 4) *
s->avctx->height) {
652 "has incorrect size, skipping chunk\n", chunk_size - 6);
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]) {
659 if (
s->avctx->width & 3)
689 "and final chunk ptr = %d\n", buf_size,
694 if (
s->new_palette) {
707 AVFrame *rframe,
int *got_frame,
708 const uint8_t *buf,
int buf_size)
716 unsigned char palette_idx1;
721 unsigned int chunk_size;
727 int compressed_lines;
733 unsigned char *pixels;
735 ptrdiff_t pixel_lo, pixel_hi;
742 pixels =
s->frame->data[0];
747 num_chunks = bytestream2_get_le16(&g2);
757 while ((
frame_size > 0) && (num_chunks > 0) &&
759 int stream_ptr_after_chunk;
760 chunk_size = bytestream2_get_le32(&g2);
763 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
768 chunk_type = bytestream2_get_le16(&g2);
771 switch (chunk_type) {
778 "Unexpected Palette chunk %d in non-palettized FLC\n",
786 compressed_lines = bytestream2_get_le16(&g2);
787 while (compressed_lines > 0) {
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)
796 y_ptr += line_packets *
s->frame->linesize[0];
801 pixel_countdown =
s->avctx->width;
802 for (
i = 0;
i < line_packets;
i++) {
806 pixel_skip = bytestream2_get_byte(&g2);
807 pixel_ptr += (pixel_skip*2);
808 pixel_countdown -= pixel_skip;
809 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
811 byte_run = -byte_run;
812 pixel = bytestream2_get_le16(&g2);
814 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
815 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
822 for (j = 0; j < byte_run; j++, pixel_countdown--) {
823 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
829 y_ptr +=
s->frame->linesize[0];
841 for (
int y = 0; y <
s->avctx->height; y++)
842 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width * 2);
847 for (lines = 0; lines <
s->avctx->height; lines++) {
852 pixel_countdown = (
s->avctx->width * 2);
854 while (pixel_countdown > 0) {
857 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
859 palette_idx1 = bytestream2_get_byte(&g2);
861 for (j = 0; j < byte_run; j++) {
862 pixels[pixel_ptr++ ^ HAVE_BIGENDIAN] = palette_idx1;
864 if (pixel_countdown < 0)
866 pixel_countdown, lines);
869 byte_run = -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;
877 if (pixel_countdown < 0)
879 pixel_countdown, lines);
883 y_ptr +=
s->frame->linesize[0];
889 for (lines = 0; lines <
s->avctx->height; lines++) {
894 pixel_countdown =
s->avctx->width;
896 while (pixel_countdown > 0) {
899 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
901 pixel = bytestream2_get_le16(&g2);
903 for (j = 0; j < byte_run; j++) {
904 *((
signed short*)(&pixels[pixel_ptr])) =
pixel;
907 if (pixel_countdown < 0)
912 byte_run = -byte_run;
916 for (j = 0; j < byte_run; j++) {
917 *((
signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
920 if (pixel_countdown < 0)
927 y_ptr +=
s->frame->linesize[0];
934 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*2) {
936 "bigger than image, skipping chunk\n", chunk_size - 6);
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]) {
945 pixel_countdown =
s->avctx->width;
947 while (pixel_countdown > 0) {
948 *((
signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
952 if (
s->avctx->width & 1)
994 AVFrame *rframe,
int *got_frame,
995 const uint8_t *buf,
int buf_size)
1000 ptrdiff_t pixel_ptr;
1001 unsigned char palette_idx1;
1006 unsigned int chunk_size;
1012 int compressed_lines;
1017 int pixel_countdown;
1018 unsigned char *pixels;
1020 ptrdiff_t pixel_lo, pixel_hi;
1027 pixels =
s->frame->data[0];
1032 num_chunks = bytestream2_get_le16(&g2);
1042 while ((
frame_size > 0) && (num_chunks > 0) &&
1044 int stream_ptr_after_chunk;
1045 chunk_size = bytestream2_get_le32(&g2);
1048 "Invalid chunk_size = %u > frame_size = %u\n", chunk_size,
frame_size);
1053 chunk_type = bytestream2_get_le16(&g2);
1056 switch (chunk_type) {
1063 "Unexpected Palette chunk %d in non-palettized FLC\n",
1071 compressed_lines = bytestream2_get_le16(&g2);
1072 while (compressed_lines > 0) {
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)
1081 y_ptr += line_packets *
s->frame->linesize[0];
1086 pixel_countdown =
s->avctx->width;
1087 for (
i = 0;
i < line_packets;
i++) {
1091 pixel_skip = bytestream2_get_byte(&g2);
1092 pixel_ptr += (pixel_skip*3);
1093 pixel_countdown -= pixel_skip;
1094 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
1096 byte_run = -byte_run;
1097 pixel = bytestream2_get_le24(&g2);
1099 for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
1107 for (j = 0; j < byte_run; j++, pixel_countdown--) {
1108 pixel = bytestream2_get_le24(&g2);
1115 y_ptr +=
s->frame->linesize[0];
1127 for (
int y = 0; y <
s->avctx->height; y++)
1128 memset(pixels + y *
s->frame->linesize[0], 0,
s->avctx->width * 3);
1133 for (lines = 0; lines <
s->avctx->height; lines++) {
1138 pixel_countdown = (
s->avctx->width * 3);
1140 while (pixel_countdown > 0) {
1143 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
1145 palette_idx1 = bytestream2_get_byte(&g2);
1147 for (j = 0; j < byte_run; j++) {
1148 pixels[pixel_ptr++] = palette_idx1;
1150 if (pixel_countdown < 0)
1152 pixel_countdown, lines);
1155 byte_run = -byte_run;
1159 for (j = 0; j < byte_run; j++) {
1160 palette_idx1 = bytestream2_get_byte(&g2);
1161 pixels[pixel_ptr++] = palette_idx1;
1163 if (pixel_countdown < 0)
1165 pixel_countdown, lines);
1170 y_ptr +=
s->frame->linesize[0];
1176 for (lines = 0; lines <
s->avctx->height; lines++) {
1181 pixel_countdown =
s->avctx->width;
1183 while (pixel_countdown > 0) {
1186 byte_run =
sign_extend(bytestream2_get_byte(&g2), 8);
1188 pixel = bytestream2_get_le24(&g2);
1190 for (j = 0; j < byte_run; j++) {
1194 if (pixel_countdown < 0)
1199 byte_run = -byte_run;
1203 for (j = 0; j < byte_run; j++) {
1204 pixel = bytestream2_get_le24(&g2);
1208 if (pixel_countdown < 0)
1215 y_ptr +=
s->frame->linesize[0];
1222 if (chunk_size - 6 > (
unsigned int)(
FFALIGN(
s->avctx->width, 2) *
s->avctx->height)*3) {
1224 "bigger than image, skipping chunk\n", chunk_size - 6);
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]) {
1231 if (
s->avctx->width & 1)
1275 const uint8_t *buf = avpkt->
data;
1276 int buf_size = avpkt->
size;
1296 av_log(avctx,
AV_LOG_ERROR,
"Unknown FLC format, my science cannot explain how this happened.\n");
const FFCodec ff_flic_decoder
Libavcodec external API header.
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(const 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(const GetByteContext *g)
#define i(width, name, range_min, range_max)
#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.
int(* init)(AVBSFContext *ctx)
static av_cold int flic_decode_init(AVCodecContext *avctx)
#define FLC_FLX_TYPE_CODE
static int flic_decode_frame_24BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
static int flic_decode_frame_8BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
static int flic_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static av_cold int flic_decode_end(AVCodecContext *avctx)
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
static void pixel_range(const AVFrame *frame, ptrdiff_t *lo, ptrdiff_t *hi)
static int check_pixel_ptr(ptrdiff_t ptr, int n, ptrdiff_t lo, ptrdiff_t hi)
static int flic_decode_frame_1BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const uint8_t *buf, int buf_size)
#define CHECK_PIXEL_PTR(n)
static const uint8_t frame_size[4]
#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.
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
static av_const int sign_extend(int val, unsigned bits)
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
@ 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
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
unsigned int palette[256]