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
00033 static int count_pixels(const uint8_t *start, int len, int bpp, int same)
00034 {
00035 const uint8_t *pos;
00036 int count = 1;
00037
00038 for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
00039 if(same != !memcmp(pos-bpp, pos, bpp)) {
00040 if(!same) {
00041
00042
00043 if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
00044 continue;
00045
00046
00047
00048 count --;
00049 }
00050 break;
00051 }
00052 }
00053
00054 return count;
00055 }
00056
00057 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
00058 int add_rep, int xor_rep, int add_raw, int xor_raw)
00059 {
00060 int count, x;
00061 uint8_t *out = outbuf;
00062
00063 for(x = 0; x < w; x += count) {
00064
00065 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
00066 if(out + bpp + 1 > outbuf + out_size) return -1;
00067 *out++ = (count ^ xor_rep) + add_rep;
00068 memcpy(out, ptr, bpp);
00069 out += bpp;
00070 } else {
00071
00072 count = count_pixels(ptr, w-x, bpp, 0);
00073 if(out + bpp*count >= outbuf + out_size) return -1;
00074 *out++ = (count ^ xor_raw) + add_raw;
00075
00076 memcpy(out, ptr, bpp * count);
00077 out += bpp * count;
00078 }
00079
00080 ptr += count * bpp;
00081 }
00082
00083 return out - outbuf;
00084 }