[checksum]: use sum and xor for data/frame check

Change-Id: Ie3bd2f2819d8387a5b2486cf0a096d271220d819
Signed-off-by: Ding Wei <leo.ding@rock-chips.com>
This commit is contained in:
Ding Wei
2018-01-19 15:01:38 +08:00
parent 1a795b05aa
commit 53a02bf26f
2 changed files with 114 additions and 33 deletions

View File

@@ -74,53 +74,117 @@ void dump_mpp_frame_to_file(MppFrame frame, FILE *fp)
} }
} }
void calc_frm_checksum(MppFrame frame, RK_U8 *sum) void calc_data_crc(RK_U8 *dat, RK_U32 len, DataCrc *crc)
{ {
RK_U32 checksum = 0; RK_U32 i = 0;
RK_U8 xor_mask; RK_U8 *dat8 = NULL;
RK_U32 y, x; RK_U32 *dat32 = NULL;
RK_U32 sum = 0, xor = 0;
/*calc sum */
dat8 = dat;
for (i = 0; i < len; i++)
sum += dat8[i];
/*calc xor */
dat32 = (RK_U32 *)dat;
for (i = 0; i < len / 4; i++)
xor ^= dat32[i];
if (len % 4) {
RK_U32 val = 0;
dat8 = (RK_U8 *)&val;
for (i = (len / 4) * 4; i < len; i++)
dat8[i] = dat[i];
xor ^= val;
}
crc->len = len;
crc->sum = sum;
crc->xor = xor;
}
void write_data_crc(FILE *fp, DataCrc *crc)
{
if (fp) {
fprintf(fp, "%d, %08x, %08x\n", crc->len, crc->sum, crc->xor);
fflush(fp);
}
}
void read_data_crc(FILE *fp, DataCrc *crc)
{
if (fp) {
RK_S32 ret = 0;
ret = fscanf(fp, "%d, %08x, %08x\n", &crc->len, &crc->sum, &crc->xor);
if (ret == EOF)
mpp_err_f("unexpected EOF found\n");
}
}
void calc_frm_crc(MppFrame frame, FrmCrc *crc)
{
RK_U32 y = 0, x = 0;
RK_U8 *dat8 = NULL;
RK_U32 *dat32 = NULL;
RK_U32 sum = 0, xor = 0;
RK_U32 width = mpp_frame_get_width(frame); RK_U32 width = mpp_frame_get_width(frame);
RK_U32 height = mpp_frame_get_height(frame); RK_U32 height = mpp_frame_get_height(frame);
RK_U32 stride = mpp_frame_get_hor_stride(frame); RK_U32 stride = mpp_frame_get_hor_stride(frame);
RK_U8 *buf = (RK_U8 *)mpp_buffer_get_ptr(mpp_frame_get_buffer(frame)); RK_U8 *buf = (RK_U8 *)mpp_buffer_get_ptr(mpp_frame_get_buffer(frame));
/* luma */
dat8 = buf;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
sum += dat8[y * stride + x];
for (y = 0; y < height; y++) { for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) { dat32 = (RK_U32 *)&dat8[y * stride];
xor_mask = (x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8); for (x = 0; x < width / 4; x++)
checksum = (checksum + ((buf[y * stride + x] & 0xff) ^ xor_mask)) & 0xffffffff; xor ^= dat32[x];
}
} }
crc->luma.len = height * width;
crc->luma.sum = sum;
crc->luma.xor = xor;
sum[0] = (checksum >> 24) & 0xff; /* chroma */
sum[1] = (checksum >> 16) & 0xff; dat8 = buf + height * stride;
sum[2] = (checksum >> 8) & 0xff; for (y = 0; y < height / 2; y++)
sum[3] = checksum & 0xff; for (x = 0; x < width; x++)
sum += dat8[y * stride + x];
for (y = 0; y < height / 2; y++) {
dat32 = (RK_U32 *)&dat8[y * stride];
for (x = 0; x < width / 4; x++)
xor ^= dat32[x];
}
crc->chroma.len = height * width / 2;
crc->chroma.sum = sum;
crc->chroma.xor = xor;
} }
void write_checksum(FILE *fp, RK_U8 *sum) void write_frm_crc(FILE *fp, FrmCrc *crc)
{ {
RK_S32 i; if (fp) {
fprintf(fp, "%d, %08x, %08x, %d, %08x, %08x\n",
for (i = 0; i < 16; i++) crc->luma.len, crc->luma.sum, crc->luma.xor,
fprintf(fp, "%02hhx", sum[i]); crc->chroma.len, crc->chroma.sum, crc->chroma.xor);
fflush(fp);
fprintf(fp, "\n"); }
} }
void read_checksum(FILE *fp, RK_U8 *sum) void read_frm_crc(FILE *fp, FrmCrc *crc)
{ {
RK_S32 i; if (fp) {
int ret; RK_S32 ret = 0;
ret = fscanf(fp, "%d, %08x, %08x, %d, %08x, %08x\n",
for (i = 0; i < 16; i++) { &crc->luma.len, &crc->luma.sum, &crc->luma.xor,
ret = fscanf(fp, "%02hhx", sum + i); &crc->chroma.len, &crc->chroma.sum, &crc->chroma.xor);
if (ret == EOF) { if (ret == EOF)
mpp_err_f("unexpected EOF found\n"); mpp_err_f("unexpected EOF found\n");
break;
}
} }
ret = fscanf(fp, "\n");
} }

View File

@@ -26,6 +26,18 @@ typedef struct OptionInfo_t {
const char* help; const char* help;
} OptionInfo; } OptionInfo;
typedef struct data_crc_t {
RK_U32 len;
RK_U32 sum;
RK_U32 xor;
} DataCrc;
typedef struct frame_crc_t {
DataCrc luma;
DataCrc chroma;
} FrmCrc;
#define show_options(opt) \ #define show_options(opt) \
do { \ do { \
_show_options(sizeof(opt)/sizeof(OptionInfo), opt); \ _show_options(sizeof(opt)/sizeof(OptionInfo), opt); \
@@ -37,9 +49,14 @@ extern "C" {
void _show_options(int count, OptionInfo *options); void _show_options(int count, OptionInfo *options);
void dump_mpp_frame_to_file(MppFrame frame, FILE *fp); void dump_mpp_frame_to_file(MppFrame frame, FILE *fp);
void calc_frm_checksum(MppFrame frame, RK_U8 *sum);
void write_checksum(FILE *fp, RK_U8 *sum); void calc_data_crc(RK_U8 *dat, RK_U32 len, DataCrc *crc);
void read_checksum(FILE *fp, RK_U8 *sum); void write_data_crc(FILE *fp, DataCrc *crc);
void read_data_crc(FILE *fp, DataCrc *crc);
void calc_frm_crc(MppFrame frame, FrmCrc *crc);
void write_frm_crc(FILE *fp, FrmCrc *crc);
void read_frm_crc(FILE *fp, FrmCrc *crc);
#ifdef __cplusplus #ifdef __cplusplus
} }