#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef unsigned char uint8_t;
typedef struct{
uint8_t *y;
uint8_t *u;
uint8_t *v;
}image_t;
#define MALLC_AND_CHECK(p, size) p = malloc(size);\
if (p == NULL)\
{\
printf("malloc "#p" error\n");\
goto error_out;\
}
#define CHECK_AND_FREE(p) if (p != NULL)\
{\
free(p);\
}
double calc_mse(image_t *img_org, image_t *img_dec, int width, int height)
{
int i, j;
double sum;
uint8_t *py_org, *py_dec;
uint8_t *pu_org, *pu_dec;
uint8_t *pv_org, *pv_dec;
int diff_val;
py_org = img_org->y;
py_dec = img_dec->y;
pu_org = img_org->u;
pu_dec = img_dec->u;
pv_org = img_org->v;
pv_dec = img_dec->v;
sum = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
diff_val = *py_org - *py_dec;
sum += diff_val * diff_val;
py_org++;
py_dec++;
}
}
for (i = 0; i < height/2; i++)
{
for (j = 0; j < width/2; j++)
{
diff_val = *pu_org - *pu_dec;
sum += diff_val * diff_val;
diff_val = *pv_org - *pv_dec;
sum += diff_val * diff_val;
pu_org++;
pu_dec++;
pv_org++;
pv_dec++;
}
}
sum = sum / (3 * width * height / 2);
return sum;
}
double psnr(double mse)
{
return 10 * log10f(255.0 * 255.0 / mse);
}
int main(int argc, char **argv)
{
image_t *img_org;
image_t *img_dec;
int width, height;
FILE *fp_org;
FILE *fp_dec;
int i;
int ret_val;
int frame_cnt = 0;
double mse;
double psnr_sum = 0;
if (argc < 7)
{
printf("Not enough parameters!\n");
printf("\tUseage: calc_psnr -w 352 -h 288 infile1 infile2\n");
printf("\t-w: width\n");
printf("\t-h: height\n");
return -1;
}
width = 0;
height = 0;
fp_org = NULL;
fp_dec = NULL;
for (i = 1; i < 7; i++)
{
if (strcmp(argv[i], "-w") == 0)
{
width = atoi(argv[i+1]);
i++;
continue;
}
else if (strcmp(argv[i], "-h") == 0)
{
height = atoi(argv[i+1]);
i++;
continue;
}
else if (fp_org == NULL)
{
fp_org = fopen(argv[i], "rb");
if (fp_org == NULL)
{
printf("can't open file %s\n", argv[i]);
goto error_out;
}
}
else if (fp_dec == NULL)
{
fp_dec = fopen(argv[i], "rb");
if (fp_dec == NULL)
{
printf("can't open file %s.\n", argv[i]);
goto error_out;
}
}
}
if (width < 0 || width > 2048)
{
printf("illeagle width!\n");
goto error_out;
}
if (height < 0 || height > 2048)
{
printf("illedgle height!\n");
goto error_out;
}
MALLC_AND_CHECK(img_org, sizeof(image_t));
MALLC_AND_CHECK(img_dec, sizeof(image_t));
MALLC_AND_CHECK(img_org->y, width*height);
MALLC_AND_CHECK(img_org->u, width*height/4);
MALLC_AND_CHECK(img_org->v, width*height/4);
MALLC_AND_CHECK(img_dec->y, width*height);
MALLC_AND_CHECK(img_dec->u, width*height/4);
MALLC_AND_CHECK(img_dec->v, width*height/4);
while (1)
{
ret_val = fread(img_org->y, 1, width * height, fp_org);
if (ret_val != width * height)
{
break;
}
ret_val = fread(img_org->u, 1, width * height / 4, fp_org);
if (ret_val != width * height / 4)
{
break;
}
ret_val = fread(img_org->v, 1, width * height /4, fp_org);
if (ret_val != width * height / 4)
{
break;
}
ret_val = fread(img_dec->y, 1, width * height, fp_dec);
if (ret_val != width * height)
{
break;
}
ret_val = fread(img_dec->u, 1, width * height / 4, fp_dec);
if (ret_val != width * height / 4)
{
break;
}
ret_val = fread(img_dec->v, 1, width * height /4, fp_dec);
if (ret_val != width * height / 4)
{
break;
}
frame_cnt++;
mse = calc_mse(img_org, img_dec, width, height);
psnr_sum += psnr(mse);
}
printf("frame_cnt = %d.\n", frame_cnt);
printf("average psnr = %f\n", psnr_sum/frame_cnt);
error_out:
if (fp_org != NULL)
{
fclose(fp_org);
}
if (fp_dec != NULL)
{
fclose(fp_dec);
}
CHECK_AND_FREE(img_org->y);
CHECK_AND_FREE(img_org->u);
CHECK_AND_FREE(img_org->v);
CHECK_AND_FREE(img_dec->y);
CHECK_AND_FREE(img_dec->u);
CHECK_AND_FREE(img_dec->v);
CHECK_AND_FREE(img_org);
CHECK_AND_FREE(img_dec);
return 0;
}
PSNR.zip_PSNR_视频质量_视频质量评估
版权申诉
6 浏览量
2022-09-19
17:50:01
上传
评论
收藏 1KB ZIP 举报
刘良运
- 粉丝: 66
- 资源: 1万+
最新资源
- Flume进阶-自定义拦截器jar包
- Dubins曲线算法讲解和在运动规划中的使用.pdf
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.dta
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.xlsx
- Reeds+Shepp曲线算法讲解和实现.pdf
- 毕业设计基于SpringBoot+MyBatisPlus+MySQL+Vue的外卖配送信息系统源代码+数据库
- 词向量(Word Embeddings)是自然语言处理(NLP)领域的一种重要技术.txt
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈