00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "rle.h"
00023 #include "libavutil/common.h"
00024
00034 static int count_pixels(const uint8_t *start, int len, int bpp, int same)
00035 {
00036 const uint8_t *pos;
00037 int count = 1;
00038
00039 for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
00040 if(same != !memcmp(pos-bpp, pos, bpp)) {
00041 if(!same) {
00042
00043
00044 if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
00045 continue;
00046
00047
00048
00049 count --;
00050 }
00051 break;
00052 }
00053 }
00054
00055 return count;
00056 }
00057
00058 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
00059 int add_rep, int xor_rep, int add_raw, int xor_raw)
00060 {
00061 int count, x;
00062 uint8_t *out = outbuf;
00063
00064 for(x = 0; x < w; x += count) {
00065
00066 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
00067 if(out + bpp + 1 > outbuf + out_size) return -1;
00068 *out++ = (count ^ xor_rep) + add_rep;
00069 memcpy(out, ptr, bpp);
00070 out += bpp;
00071 } else {
00072
00073 count = count_pixels(ptr, w-x, bpp, 0);
00074 if(out + bpp*count >= outbuf + out_size) return -1;
00075 *out++ = (count ^ xor_raw) + add_raw;
00076
00077 memcpy(out, ptr, bpp * count);
00078 out += bpp * count;
00079 }
00080
00081 ptr += count * bpp;
00082 }
00083
00084 return out - outbuf;
00085 }