JPEG解码实验
一、实验原理(JPEG文件格式)
JPEG编码原理
![截屏2020-05-31下午3.24.33](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnvzpbvjj319o0lincc.jpg)
1、Segment的组织形式
JPEG 在文件中以 Segment 的形式组织,它具有以下特点:
- 均以 0xFF 开始,后跟 1 byte的Marker和2byte的Segment length(包含表示Length本身所占用的2byte,不含“0xFF” + “Marker” 所占用的2byte)
- 采用 Motorola 序(相对于Intel 序),即保存时高位在前,低位在后
- Data 部分中,0xFF 后若为 0x00,则跳过此字节不予处理
2、DQT(定义量化表)
Define Quantization Table,定义量化表。
![截屏2020-05-30下午5.44.50](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfamb0ioa1j30y2042404.jpg)
- 量化表标记代码:0xFFDB
- 量化表长度:0x43,指包含Segment length的2byte在内共有67byte
- 精度及量化表ID:低位0为量化表的ID索引值,其取值范围为 0~3,所以说最多可能有四张量化表(亮度量化表,色度量化表,可能会有 R、G、B 各一张量化表)。高位0为量化精度,有两个可选值,0:8 位;1:16 位,这里是 0,代表量化精度为 8bit,即用一个字节来表示一个量化系数。
- 数据:64字节的量化系数,(一个字节对应一个量化系数,对于 8x8 的宏块来说,共 8x8=64 个量化系数)
JPEG标准中采用线性均匀量化器,对64个DCT系数除以量化步长并四舍五入取整,量化步长由量化表决定。量化表为8x8矩阵,与DCT变换系数一一对应,亮度分量和彩色分量使用不同的量化表。
量化后系数的读取顺序按Z字形排列。
3、SOF0
- 得到每个sample的比特数、长宽、颜色分量数
- 得到每个颜色分量的ID、水平采样因子、垂直采样因子、使用的量化表序号(与DQT中序号对应)
![截屏2020-05-30下午7.20.56](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfap2wo3o6j30pg02075z.jpg)
- 数据长度:长度本身占 2byte,length=17
- 图像精度 :1byte, 这里是 08,即精度为一个字节。
- 图像高度:2 byte, 以像素为单位,lines=1024。
- 图像宽度:2 byte, 以像素为单位,samples_per_lines=1024。
- 颜色分量数:一个字节,这里是 03,代表有三个分量,YCrCb。
- 颜色分量信息:每个分量有三个字节,第一个为分量的 ID,01:Y 、02:U、 03:V;第二个字节高位为水平采样因子,低位为垂直采样因子,这里三个分量的采样率相同,所以采样格式为 4:4:4;第三个字节代表这个分量对应的量化表 ID,可以看出,Y 对应的量化表 ID 索引值为 00,而 UV 对应的量化表 ID都为 01,即它们共用一张量化表。所以,除了标记外,总共的长度就为 2+1+5+3*3=17
4、DHT(定义Huffman树表)
![截屏2020-05-30下午8.52.38](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfarqcitu3j30mi0243zu.jpg)
-
数据长度:2 byte,这里是0x1D,length=29
-
Huffman表ID号和类型:1 byte,高 4 位为表的类型,0:DC 直流;1:AC交流。
0x00表示DC直流0号表;
0x01表示DC直流1号表;
0x10表示AC交流0号表;
0x11表示AC交流1号表 -
不同长度 Huffman 的码字数量:固定为 16 个字节,每个字节代表从长度为 1到长度为16的码字的个数。
-
每个符号码字对应的权值:由前一部分数据知道,此哈夫曼树有0+3+1+1+1+1+1+1+1=10个叶子结点,即本字段应该有10个字节。这段数据表示10个叶子结点按从小到大排列,其权值依次为04、 05、 06、 03、 02、 01、 00、09、 07、 08 。
对于直流系数,权值就是解码时再需要读入的bit位数。这个再次读入的位数通过查表得到真正的码值。注意:直流系数是差分编码的!
对于交流系数,权值的高4位表示当前数值前面有多少个连续的零,低4位表示该交流分量数值的二进制位数,也就是接下来需要读入的位数。
5、SOS
得到解析每个颜色分量的DC、AC值所使用的Huffman表序号(与DHT中序号对应)
![截屏2020-05-31上午12.33.59](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfay4m6my8j30ps01e3ze.jpg)
- 数据长度:2 byte,这里是0x0C,length=12
- 颜色分量数:1 byte,0x03三个分量,与SOF中的颜色分量相同
- 颜色分量信息:每个分量对应 3 个字节,第一个字节是颜色分量 ID,1,2,3对应 YUV,第二个字节高位为直流分量使用的哈夫曼树编号,这里 Y 的直流分量用的是 DC 的第 0 张表,低四位代表交流分量使用的哈夫曼树编号,这里 Y的交流分量用的是 AC 的第 0 张表,而两个色度信号的直流分量都用的是 DC的第 1 张表,交流分量用的是 AC 的第 1 张表.
二、实验步骤
1、程序设计整体框架
![jpeg解码](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbi9pnvoqj30u012g40g.jpg)
2、结构体设计
本实验设计三个关键结构体,具体分析如下。
(1)struct huffman_table
快速查找,加快程序运行速度。
// lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
// code_size will be used to known how many bits this symbol is encoded.
// slowtable will be used when the first lookup didn't give the result.
struct huffman_table
{
/* Fast look up table, using HUFFMAN_HASH_NBITS bits we can have directly the symbol,
* if the symbol is <0, then we need to look into the tree table */
short int lookup[HUFFMAN_HASH_SIZE];
/* code size: give the number of bits of a symbol is encoded */
unsigned char code_size[HUFFMAN_HASH_SIZE];
/* some place to store value that is not encoded in the lookup table
* FIXME: Calculate if 256 value is enough to store all values
*/
uint16_t slowtable[16-HUFFMAN_HASH_NBITS][256];
};
lookup[]为快速查找表,用来加快查表速度。如果失败,则使用slowtable[]做慢速查找。
codesize为码字长度。
(2)struct component
存储一个8x8宏块的Y/U/V分量。
struct component
{
unsigned int Hfactor; /* 水平方向采样信息 */
unsigned int Vfactor; /* 垂直方向采样信息 */
float *Q_table; /* Pointer to the quantisation table to use */
struct huffman_table *AC_table;
struct huffman_table *DC_table;
short int previous_DC; /* Previous DC coefficient 前一个DC系数*/
short int DCT[64]; /* DCT coef 8x8宏块内的DCT系数*/
#if SANITY_CHECK
unsigned int cid;
#endif
};
假设水平采样因子:垂直采样因子= 2 :2,水平方向每两个分量采样一次,垂直方向每两个分量采样一次。
(3)struct jdec_private
存储整个jpeg文件信息,其中嵌套了以上两种结构体。
struct jdec_private
{
/* Public variables */
uint8_t *components[COMPONENTS]; /* #define COMPONENTS 3 */
unsigned int width, height; /* Size of the image */
unsigned int flags;
/* Private variables */
const unsigned char *stream_begin, *stream_end;
unsigned int stream_length;
const unsigned char *stream; /* Pointer to the current stream */
unsigned int reservoir, nbits_in_reservoir;
struct component component_infos[COMPONENTS];
float Q_tables[COMPONENTS][64]; /* quantization tables */
struct huffman_table HTDC[HUFFMAN_TABLES]; /* DC huffman tables */
struct huffman_table HTAC[HUFFMAN_TABLES]; /* AC huffman tables */
int default_huffman_table_initialized;
int restart_interval;
int restarts_to_go; /* MCUs left in this restart interval */
int last_rst_marker_seen; /* Rst marker is incremented each time */
/* Temp space used after the IDCT to store each components */
uint8_t Y[64*4], Cr[64], Cb[64];
jmp_buf jump_state;
/* Internal Pointer use for colorspace conversion, do not modify it !!! */
uint8_t *plane[COMPONENTS];
};
3、TRACE的目的和含义
提供一组方法和属性,帮助跟踪代码的执行。可以使用Trace类中的属性和方法来检测发布版本。 借助检测功能,可以监视应用程序在真实设置中运行的运行状况。
#define TRACE 1//add by nxn
利用宏定义,当TRACE的值为1时,TRACE开启。当TRACE的值为0时,TRACE关闭。
具体使用例子如下:
#if TRACE
p_trace=fopen(TRACEFILE,"w");
if (p_trace==NULL)
{
printf("trace file open error!");
}
#endif
TRACE=1时,执行内部语句,打开TRACEFILE。
程序执行完毕后,打开trace_jpeg.txt即可查看追踪内容。
4、关键位置代码
(1)解析DQT
/* 解析量化表 */
/* 得到量化表长度(可能含多张量化表) */
/* 得到量化表精度 */
/* 得到及检查量化表的序号(只能是0-3) */
/* 得到量化表的内容(8x8=64) */
static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
{
int qi;
float *table;
const unsigned char *dqt_block_end;
#if TRACE
fprintf(p_trace,"> DQT marker\n");
fflush(p_trace);
#endif
dqt_block_end = stream + be16_to_cpu(stream); /* 指向量化表结尾 */
stream += 2; /* Skip length */
while (stream < dqt_block_end)
{
qi = *stream++;
#if SANITY_CHECK
if (qi>>4)
snprintf(error_string, sizeof(error_string),"16 bits quantization table is not supported\n");
if (qi>4)
snprintf(error_string, sizeof(error_string),"No more 4 quantization table is supported (got %d)\n", qi);
#endif
table = priv->Q_tables[qi]; /* table指向第qi个量化表 */
build_quantization_table(table, stream); /* 创建量化表 */
stream += 64;
}
#if TRACE
fprintf(p_trace,"< DQT marker\n");
fflush(p_trace);
#endif
return 0;
}
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
{
/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
* For float AA&N IDCT method, divisors are equal to quantization
* coefficients scaled by scalefactor[row]*scalefactor[col], where
* scalefactor[0] = 1
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
* We apply a further scale factor of 8.
* What's actually stored is 1/divisor so that the inner loop can
* use a multiplication rather than a division.
*/
int i, j;
static const double aanscalefactor[8] = {
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
};
const unsigned char *zz = zigzag;
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
// 量化后系数的读取顺序按Z字形排列
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
}
}
(2)解析SOF0
/* 解析SOF */
static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
{
int i, width, height, nr_components, cid, sampling_factor;
int Q_table;
struct component *c;
#if TRACE
fprintf(p_trace,"> SOF marker\n");
fflush(p_trace);
#endif
print_SOF(stream);
height = be16_to_cpu(stream+3); // 获取当前图片的高度
width = be16_to_cpu(stream+5); // 获取当前图片的宽度
nr_components = stream[7]; // 颜色分量数
#if SANITY_CHECK // 检验图像是否符合要求
if (stream[2] != 8)
snprintf(error_string, sizeof(error_string),"Precision other than 8 is not supported\n");
if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT)
snprintf(error_string, sizeof(error_string),"Width and Height (%dx%d) seems suspicious\n", width, height);
if (nr_components != 3)
snprintf(error_string, sizeof(error_string),"We only support YUV images\n");
if (height%16)
snprintf(error_string, sizeof(error_string),"Height need to be a multiple of 16 (current height is %d)\n", height);
if (width%16)
snprintf(error_string, sizeof(error_string),"Width need to be a multiple of 16 (current Width is %d)\n", width);
#endif
stream += 8; // 将指针移至颜色分量信息处
for (i=0; i<nr_components; i++) {
cid = *stream++; // 分量ID
sampling_factor = *stream++; // 水平竖直采样因子
Q_table = *stream++; // 量化表序号
c = &priv->component_infos[i];
#if SANITY_CHECK
c->cid = cid;
if (Q_table >= COMPONENTS)
snprintf(error_string, sizeof(error_string),"Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1);
#endif
c->Vfactor = sampling_factor&0xf; // 取低4位赋值
c->Hfactor = sampling_factor>>4; // 取高8位赋值
c->Q_table = priv->Q_tables[Q_table]; // 量化表序号赋值
#if TRACE
fprintf(p_trace,"Component:%d factor:%dx%d Quantization table:%d\n",
cid, c->Hfactor, c->Hfactor, Q_table );
fflush(p_trace);
#endif
}
priv->width = width; // 赋宽度
priv->height = height; // 赋高度
#if TRACE
fprintf(p_trace,"< SOF marker\n");
fflush(p_trace);
#endif
return 0;
}
(3)解析DHT
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int count, i;
unsigned char huff_bits[17];
int length, index;
length = be16_to_cpu(stream) - 2; // 读入数据长度
stream += 2; /* Skip length */
#if TRACE
fprintf(p_trace,"> DHT marker (length=%d)\n", length);
fflush(p_trace);
#endif
while (length>0) { // 是否还有表
index = *stream++;
/* We need to calculate the number of bytes 'vals' will takes */
huff_bits[0] = 0;
count = 0;
for (i=1; i<17; i++) { /* 计算Huffman树共有多少叶子节点 */
huff_bits[i] = *stream++;
count += huff_bits[i];
}
#if SANITY_CHECK /* 检查数据格式 */
if (count >= HUFFMAN_BITS_SIZE)
snprintf(error_string, sizeof(error_string),"No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
if ( (index &0xf) >= HUFFMAN_TABLES)
snprintf(error_string, sizeof(error_string),"No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
#if TRACE
fprintf(p_trace,"Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
fflush(p_trace);
#endif
#endif
if (index & 0xf0 ) // 判断建立AC表orDC表
/* HTAC[index&0xf]确定为第几张表 */
build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
else
build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
length -= 1;
length -= 16;
length -= count;
stream += count;
}
#if TRACE
fprintf(p_trace,"< DHT marker\n");
fflush(p_trace);
#endif
return 0;
}
/*
* Takes two array of bits, and build the huffman table for size, and code
*
* lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
* code_size will be used to known how many bits this symbol is encoded.
* slowtable will be used when the first lookup didn't give the result.
*/
static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
{
unsigned int i, j, code, code_size, val, nbits;
unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
int next_free_entry;
/*
* Build a temp array
* huffsize[X] => numbers of bits to write vals[X]
*/
hz = huffsize;
for (i=1; i<=16; i++)
{
for (j=1; j<=bits[i]; j++)
*hz++ = i; // 按码字顺序保存每个码字的长度
}
*hz = 0;
/* 将某一块内存中的内容全部设置为0xff */
memset(table->lookup, 0xff, sizeof(table->lookup));
for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
table->slowtable[i][0] = 0;
/* Build a temp array
* huffcode[X] => code used to write vals[X]
*/
code = 0;
hc = huffcode; // Huffcode数组里按码字顺序保存每个码字
hz = huffsize; // huffsize数组中按码字顺序保存每个码字的长度
nbits = *hz; // 码字长度
while (*hz)
{
/* 码长相同+1 码长增加码字左移一位 */
while (*hz == nbits)
{
*hc++ = code++;
hz++;
}
code <<= 1;
nbits++;
}
/*
* Build the lookup table, and the slowtable if needed.
*/
next_free_entry = -1;
for (i=0; huffsize[i]; i++)
{
val = vals[i]; // 权值
code = huffcode[i]; // 码长
code_size = huffsize[i]; // 码字长度
#if TRACE
fprintf(p_trace,"val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
fflush(p_trace);
#endif
table->code_size[val] = code_size; // 建立权值和码长的关系
if (code_size <= HUFFMAN_HASH_NBITS)
{
/*
* Good: val can be put in the lookup table, so fill all value of this
* column with value val
*/
int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
code <<= HUFFMAN_HASH_NBITS - code_size;
while ( repeat-- )
table->lookup[code++] = val; // 建立查找表和权值的关系
}
else
{
/* Perhaps sorting the array will be an optimization */
uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
while(slowtable[0])
slowtable+=2;
slowtable[0] = code;
slowtable[1] = val;
slowtable[2] = 0;
/* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */
}
}
}
(4)解析SOS
static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int i, cid, table;
unsigned int nr_components = stream[2]; // 得到颜色分量数
#if TRACE
fprintf(p_trace,"> SOS marker\n");
fflush(p_trace);
#endif
#if SANITY_CHECK
if (nr_components != 3)
snprintf(error_string, sizeof(error_string),"We only support YCbCr image\n");
#endif
stream += 3;
for (i=0;i<nr_components;i++) { // 依次处理三个component
cid = *stream++; // 得到ID
table = *stream++; // Huffman表序号
#if SANITY_CHECK
if ((table&0xf)>=4)
snprintf(error_string, sizeof(error_string),"We do not support more than 2 AC Huffman table\n");
if ((table>>4)>=4)
snprintf(error_string, sizeof(error_string),"We do not support more than 2 DC Huffman table\n");
if (cid != priv->component_infos[i].cid)
snprintf(error_string, sizeof(error_string),"SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n",
i, cid, i, priv->component_infos[i].cid);
#if TRACE
fprintf(p_trace,"ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
fflush(p_trace);
#endif
#endif
/* 指向所用的Huffman表 */
priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
}
priv->stream = stream+3;
#if TRACE
fprintf(p_trace,"< SOS marker\n");
fflush(p_trace);
#endif
return 0;
}
(5)依据每个分量的水平、垂直采样因子计算MCU的大小,并得到每个MCU中8x8宏块的个数
xstride_by_mcu = ystride_by_mcu = 8; // 初始化为444情况
/* 水平垂直采样因子都为1 */
if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
decode_MCU = decode_mcu_table[0];
convert_to_pixfmt = colorspace_array_conv[0];
#if TRACE
fprintf(p_trace,"Use decode 1x1 sampling\n");
fflush(p_trace);
#endif
} else if (priv->component_infos[cY].Hfactor == 1) { /* 水平方向采样因子为1 */
decode_MCU = decode_mcu_table[1];
convert_to_pixfmt = colorspace_array_conv[1];
ystride_by_mcu = 16;
#if TRACE
fprintf(p_trace,"Use decode 1x2 sampling (not supported)\n");
fflush(p_trace);
#endif
} else if (priv->component_infos[cY].Vfactor == 1) { /* 垂直方向采样因子为1 */
decode_MCU = decode_mcu_table[2];
convert_to_pixfmt = colorspace_array_conv[2];
xstride_by_mcu = 16;
#if TRACE
fprintf(p_trace,"Use decode 2x1 sampling\n");
fflush(p_trace);
#endif
} else { /* 水平垂直采样因子都为2 */
decode_MCU = decode_mcu_table[3];
convert_to_pixfmt = colorspace_array_conv[3];
xstride_by_mcu = 16;
ystride_by_mcu = 16;
#if TRACE
fprintf(p_trace,"Use decode 2x2 sampling\n");
fflush(p_trace);
#endif
}
(6)对每个MCU解码
- 对每个宏块进行Huffman解码,得到DCT系数
- 对每个宏块的 DCT 系数进行 IDCT,得到 Y、Cb、Cr
- 遇到 Segment Marker RST 时,清空之前的 DC DCT 系数
基本框架
/*
* Decode a 1x1 directly in 1 color
*/
static void decode_MCU_1x1_1plane(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY); // 以8x8宏块为单位进行Huffman解码
IDCT(&priv->component_infos[cY], priv->Y, 8); // 对得到的DCT系数进行IDCT
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
若MCU为其他格式,则:
1x2 2x1
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
2x2
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
进行Huffman解码
/**
*
* Decode a single block that contains the DCT coefficients.
* The table coefficients is already dezigzaged at the end of the operation.
*
*/
static void process_Huffman_data_unit(struct jdec_private *priv, int component)
{
unsigned char j;
unsigned int huff_code;
unsigned char size_val, count_0;
struct component *c = &priv->component_infos[component];
short int DCT[64]; // 存放DCT系数
/* Initialize the DCT coef table */
memset(DCT, 0, sizeof(DCT));
/* DC coefficient decoding */
huff_code = get_next_huffman_code(priv, c->DC_table); // 找到下一个完整Huffman码字
//trace("+ %x\n", huff_code);
if (huff_code) {
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]); //查表的DC DCT系数
DCT[0] += c->previous_DC; // DC系数采用差分编码,恢复原值
c->previous_DC = DCT[0];
} else {
DCT[0] = c->previous_DC;
}
/* AC coefficient decoding */
j = 1;
while (j<64)
{
huff_code = get_next_huffman_code(priv, c->AC_table); // 找到下一个完整Huffman码字
//trace("- %x\n", huff_code);
size_val = huff_code & 0xF; // 取出低8位:权值
count_0 = huff_code >> 4; // 取出高8位:数值前有多少个连0
if (size_val == 0)
{ /* RLE */
if (count_0 == 0)
break; /* EOB found, go out */
else if (count_0 == 0xF)
j += 16; /* skip 16 zeros */
}
else
{
j += count_0; /* skip count_0 zeroes */
if (__unlikely(j >= 64))
{
snprintf(error_string, sizeof(error_string), "Bad huffman data (buffer overflow)");
break;
}
// 译码
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
j++;
}
}
for (j = 0; j < 64; j++)
c->DCT[j] = DCT[zigzag[j]]; // z字形扫描顺序转换为矩阵顺序
}
5、将输出文件保存为YUV文件
补充后代码如下:
static void write_yuv(const char *filename, int width, int height, unsigned char **components)
{
FILE *F;
char temp[1024];
/* 将YUV写入同一个文件 */
FILE* output;
output = fopen("output.yuv", "wb");
fwrite(components[0], width, height, output);
fwrite(components[1], width * height / 4, 1, output);
fwrite(components[2], width * height / 4, 1, output);
fclose(output);
snprintf(temp, 1024, "%s.Y", filename);
F = fopen(temp, "wb");
fwrite(components[0], width, height, F);
fclose(F);
snprintf(temp, 1024, "%s.U", filename);
F = fopen(temp, "wb");
fwrite(components[1], width*height/4, 1, F);
fclose(F);
snprintf(temp, 1024, "%s.V", filename);
F = fopen(temp, "wb");
fwrite(components[2], width*height/4, 1, F);
fclose(F);
}
使用YUVViewer观看到的YUV文件(与原图一致)
![截屏2020-05-31下午1.29.06](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbkj7wemqj311a0u04qp.jpg)
6、输出量化矩阵和HUFFMAN码表
以txt文件形式输出。
输出量化矩阵
补充后代码如下:
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
{
int i, j;
static const double aanscalefactor[8] = {
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
};
const unsigned char *zz = zigzag;
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
fprintf(DQT_File, "%d\t", ref_table[*zz]);
fflush(DQT_File);
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
}
fprintf(DQT_File, "\n");
fflush(DQT_File);
}
}
结果:
![截屏2020-05-31下午1.15.52](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbk5dobloj30vw0pyabt.jpg)
输出Huffman码表
补充后代码如下
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int count, i;
unsigned char huff_bits[17];
int length, index;
length = be16_to_cpu(stream) - 2; // 读入数据长度
stream += 2; /* Skip length */
#if TRACE
fprintf(p_trace,"> DHT marker (length=%d)\n", length);
fflush(p_trace);
#endif
fprintf(DHT_File, "> DHT marker (length=%d)\n", length);
fflush(DHT_File);
while (length>0) {
index = *stream++;
/* We need to calculate the number of bytes 'vals' will takes */
huff_bits[0] = 0;
count = 0;
for (i=1; i<17; i++) { /* 计算Huffman树共有多少叶子节点 */
huff_bits[i] = *stream++;
count += huff_bits[i];
}
#if SANITY_CHECK /* 检查数据格式 */
if (count >= HUFFMAN_BITS_SIZE)
snprintf(error_string, sizeof(error_string),"No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
if ( (index &0xf) >= HUFFMAN_TABLES)
snprintf(error_string, sizeof(error_string),"No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
#if TRACE
fprintf(p_trace,"Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
fflush(p_trace);
#endif
#endif
fprintf(DHT_File, "Huffman table %s[%d] length=%d\n", (index & 0xf0) ? "AC" : "DC", index & 0xf, count);
fflush(DHT_File);
if (index & 0xf0 ) // 判断建立AC表orDC表
build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
else
build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
length -= 1;
length -= 16;
length -= count;
stream += count;
}
fprintf(DHT_File, "< DHT marker\n");
fflush(DHT_File);
#if TRACE
fprintf(p_trace,"< DHT marker\n");
fflush(p_trace);
#endif
return 0;
}
static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
{
unsigned int i, j, code, code_size, val, nbits;
unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
int next_free_entry;
/*
* Build a temp array
* huffsize[X] => numbers of bits to write vals[X]
*/
hz = huffsize;
for (i=1; i<=16; i++)
{
for (j=1; j<=bits[i]; j++)
*hz++ = i; // 按码字顺序保存每个码字的长度
}
*hz = 0;
/* 将某一块内存中的内容全部设置为0xff */
memset(table->lookup, 0xff, sizeof(table->lookup));
for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
table->slowtable[i][0] = 0;
/* Build a temp array
* huffcode[X] => code used to write vals[X]
*/
code = 0;
hc = huffcode; // Huffcode数组里按码字顺序保存每个码字
hz = huffsize; // huffsize数组中按码字顺序保存每个码字的长度
nbits = *hz; // 码字长度
while (*hz)
{
/* 码长相同+1 码长增加码字左移一位 */
while (*hz == nbits)
{
*hc++ = code++;
hz++;
}
code <<= 1;
nbits++;
}
/*
* Build the lookup table, and the slowtable if needed.
*/
next_free_entry = -1;
for (i=0; huffsize[i]; i++)
{
val = vals[i]; // 权值
code = huffcode[i]; // 码长
code_size = huffsize[i]; // 码字长度
#if TRACE
fprintf(p_trace,"val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
fflush(p_trace);
#endif
fprintf(DHT_File, "val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
fflush(DHT_File);
table->code_size[val] = code_size;
if (code_size <= HUFFMAN_HASH_NBITS)
{
/*
* Good: val can be put in the lookup table, so fill all value of this
* column with value val
*/
int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
code <<= HUFFMAN_HASH_NBITS - code_size;
while ( repeat-- )
table->lookup[code++] = val;
}
else
{
/* Perhaps sorting the array will be an optimization */
uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
while(slowtable[0])
slowtable+=2;
slowtable[0] = code;
slowtable[1] = val;
slowtable[2] = 0;
/* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */
}
}
}
结果:
![截屏2020-05-31下午1.14.27](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbk3y22jfj30io0wkq6g.jpg)
7、输出DC图像并统计概率分布
由于MCU为8x8宏块,故DC值的个数为原图像大小的1/64。
在 convert_one_image() 函数中补充:
/* 输出DC AC图像 */
/* 写入图像的UV分量 全部置为128 */
unsigned char temp = 128;
for (int i = 0; i < width*height/32; i++) {
fwrite(&temp, 1, 1, DC_File);
fwrite(&temp, 1, 1, AC_File);
}
在 tinyjpeg_decode() 函数中的循环中添加:
/* 输出DC AC图像 */
unsigned char DC_Value = 0;
unsigned char AC_Value = 0;
/* 读出DC AC系数并映射至0~255范围 */
DC_Value = (unsigned char)((priv->component_infos->DCT[0] + 512.0) / 4 + 0.5);
AC_Value = (unsigned char)(priv->component_infos->DCT[1] + 128);
fwrite(&DC_Value, 1, 1, DC_File);
fwrite(&AC_Value, 1, 1, AC_File);
/* 计算概率分布 */
count_DC[DC_Value]++;
count_AC[AC_Value]++;
在 tinyjpeg_decode() 函数末尾添加:
/* 计算频率分布 */
for (int i = 0; i < 256; i++) {
Freq_DC[i] = count_DC[i] / (priv->width * priv->height / 64);
Freq_AC[i] = count_AC[i] / (priv->width * priv->height / 64);
}
/* 输出至文件 */
fprintf(freq_DC_File, "symbol\tfreq\n");
fprintf(freq_AC_File, "symbol\tfreq\n");
for (int i = 0; i < 256; i++) {
fprintf(freq_DC_File, "%d\t%lf\n", i, Freq_DC[i]);
fprintf(freq_AC_File, "%d\t%lf\n", i, Freq_AC[i]);
}
结果
- DC值图像
![截屏2020-05-31下午3.09.37](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnfpj7o5j307a08ut9a.jpg)
- DC值概率分布
![截屏2020-05-31下午3.11.05](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnh8kys5j30dm0bw3z7.jpg)
![image-20200531151335869](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnjsvru9j30k00c0jta.jpg)
8、输出某一个AC值图像并统计其概率分布
修改代码见上文
- AC值图像
![截屏2020-05-31下午3.15.41](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnm0t4cfj3078090jrv.jpg)
- AC值概率分布
![截屏2020-05-31下午3.16.45](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnn58lycj30fa0d6mxz.jpg)
![image-20200531151848339](https://tva1.sinaimg.cn/large/007S8ZIlgy1gfbnp7mmylj30k00c00u3.jpg)