在一个DPCM系统中,有两个因素需要设计:预测器和量化器。理想情况下,预测器和量化器应进行联合优化。实际中,采用一种次优的设计方法:分别进行线性预测器和量化器的优化设计。
DPCM编码部分代码
/* DPCM encoding */ /* rebuild & diffrential file */ FILE* rebuildFile = NULL; FILE* diffFile = NULL; if ((rebuildFile = fopen(argv[3], "wb+")) == NULL) { printf("yuv rebuild file failed!"); exit(0); } if ((diffFile = fopen(argv[4], "wb+")) == NULL) { printf("yuv differential file failed!"); exit(0); } /* buffer */ unsigned char* inBuf = NULL; unsigned char* dBuf = NULL; unsigned char* reBuf = NULL; inBuf = (unsigned char *)malloc(height*width); dBuf = (unsigned char *)malloc(height*width); reBuf = (unsigned char *)malloc(height*width); inBuf = yBuff; // only have to free either one of them /* quantify setting */ const long Qbit = 1; long scale = 512 / (1 << Qbit); /* avoid overflow */ long temp; /* differentiate & quantify & rebuild */ for(i=0;i<height;i++) { for(j=0;j<width;j++) { if(!j) { *(dBuf + i*width + j) = ((*(inBuf + i*width + j) - 128 + 255) / scale) ; temp = (((*(dBuf + i*width + j) - (255 / scale)) * scale) + 128) ; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; *(reBuf + i*width + j) = temp; } else { *(dBuf + i*width + j) = ((*(inBuf + i*width + j) - *(reBuf + i*width + j - 1) + 255) / scale) ; temp = (((*(dBuf + i*width + j) - (255 / scale)) * scale) + *(reBuf + i*width + j - 1)); if (temp > 255) temp = 255; else if (temp < 0) temp = 0; *(reBuf + i*width + j) = temp ; } *(dBuf + i*width + j) = *(dBuf + i*width + j) * scale / 2; } } fwrite(reBuf, 1, width * height, rebuildFile); fwrite(dBuf, 1, width * height, diffFile);对前三个正常图像,经DPCM的压缩比较不进行DPCM的压缩比更大,对后两个随机噪声和高频图像,经DPCM的压缩比反而更小,因为它们像素间几乎没有关联,所以DPCM对像素间有关联的正常图像可以进行数据压缩。
对于降低量化比特数后的预测误差图像,采用如下操作使其范围均衡化到0~255的范围,方便观察,若不进行均衡化,4比特以下的预测误差图像像素值很低,将是一片漆黑(颤抖吧)。
*(dBuf + i*width + j) = *(dBuf + i*width + j) * scale / 2;另外在试验中还需要注意,重建图像的像素值有可能溢出(实际是在4bit以下量化是发生溢出)。所以需要对重建图像像素值上下限进行限制,即代码中引入的temp变量。