一:实验原理
1.JPEG编码原理
JPEG 是Joint Photographic Experts Group(联合图像专家小组)的缩写,是第一个国际图像压缩标准。
.jpeg/.jpg是最常用的图像文件格式,是一种有损压缩格式。JPEG
编码框图如下图所示:
(1)零偏置(level offset)
对于灰度级是2n的像素,通过减去2n-1,将无符号的整数值变成有符号数;
对于n=8,即将0~255的值域,通过减去128,转换为值域在-128~127之间的值。这样做的目的是: 使像素的绝对值出现3位10进制的概率大大减少。
(2)8x8 DCT变换
DCT变换是指
对每个单独的彩色图像分量,把整个分量图像分成8×8的图像块,再以8x8的图像块为一个单位进行量化和编码处理。我们可以利用DCT变换去相关的特性,去除冗余信息,提高编码效率。
(3)量化
我们可以通过量化减少数据的编码位数,提高编码效率;
因为人眼对亮度信号比对色差信号更敏感,因此使用了两种量化表:亮度量化值和色差量化值;
根据人眼的视觉特性
(对低频敏感,对高频不太敏感)对低频分量采取较细的量化,对高频分量采取较粗的量化。
(4)DC系数差分编码
8×8图像块经过DCT变换之后得到的DC直流系数有两个特点:系数的数值比较大和相邻8×8图像块的DC系数值变化不大:冗余;
根据这个特点, JPEG算法使用了差分脉冲调制编码(DPCM)技术,对相邻图像块之间量化DC系数的差值DIFF进行编码:
再对DIFF进行Huffman编码。
(5)AC系数的之字形扫描
由于经DCT变换后,系数大多数集中在左上角,即低频分量区,因此采用Z字形按频率的高低顺序读出,可以出现很多连零的机会。可以使用游程编码。尤其在最后,如果都是零,给出EOB (End of Block)即可。zigzag扫描如下图:
(6)AC系数游程编码
在经过之字形扫描排序后的AC系数,存在很多连0。为了进一步提高编码效率,因此对AC系数进行游程编码(RLC)处理之后,再进一步进行Huffman编码。
(7)将进行处理之后的AC和DC系数,送到Huffman编码器分别进行Huffman编码
JPEG中共采用了四张Huffman码表:亮度DC、亮度AC、色度DC、色度AC,即分别对图像的亮度和色度,直流和交流数据进行编码处理。
2.JPEG文件格式分析
(1)segment的组织形式
EG 在文件中以 Segment 的形式组织,它具有以下特点:
均以 0xFF 开始,后跟 1 byte 的 Marker 和 2 byte 的 Segment length(包含表示Length 本身所占用的 2 byte,不含“ 0xFF” + “
Marker” 所占用的 2 byte);
采用 Motorola 序(相对于 Intel 序),即保存时高位在前,低位在后;
Data 部分中, 0xFF 后若为 0x00,则跳过此字节不予处理;
(2)文件格式分析
下面,我们通过对一个具体的JPEG文件格式的分析,来了解相关marker:
FFD8:SOI, Start of Image,图像开始
所有的JPEG文件都必须以SOI开始。
FFE0:Application,应用程序保留标记 0
length: 16 byte (2 byte)----00 10
标识符: JFIF (5 byte)----4A 46 49 46 00
Version: 0101 (2 byte)----01 01
Units: 01 (1 byte) X and Y are dots per inch----01
Xdensity: 96 (2 bytes) Horizontal pixel density(水平方向点密度)----00 60
Ydensity:96 (2 bytes) Vertical pixel density(垂直方向点密度)---- 00 60
缩略图水平像素数目: 00 (1 byte)----00
缩略图垂直像素数目: 00 (1 byte)----00
缩略图 24bitRGB 点数目: 缩略图水平像素数目 * 缩略图垂直像素数目 = 00
FFDB:DQT, Define Quantization Table,定义量化表
length: 67 byte (2 byte)----00 43
QT information - precision: 00 (Higher 4 bit) (8 bit)
QT information - index: 00 (Lower 4 bit)----00
qt_table:从08一直到下一个FFDB之前,为量化表的64个量化值
下一个
FFDB定义了第二个量化表,这里不再赘述。
FFC0
: SOF0 , Start of Frame, 基线离散余弦变换
length: 17 byte (2 byte)----0
0 11
图像精度(每个数据样本的位数) : 8----08
Image Height: 900 (2 byte)----03 84
Image Width: 1440 (2 byte)----05 A0
颜色分量数: 03( YCrCb) (1 byte)----03
颜色分量 ID: 01 (1 byte) (Y)----01
SampRate_Y_H: 10 (Higher 4 bit)
SampRate_Y_V: 10 (Lower 4 bit)----22
Y QtTableID: 00 (1 byte)----00
颜色分量 ID: 02 (1 byte) (U)----02
SampRate_U_H: 01 (Higher 4 bit)
SampRate_U_V: 01 (Lower 4 bit)----11
U QtTableID: 01 (1 byte)----01
颜色分量 ID: 03 (1 byte) (V)----03
SampRate_V_H: 01 (Higher 4 bit)
SampRate_V_V: 01 (Lower 4 bit)----11
V QtTableID: 01 (1 byte)----01
FFC4: DHT, Define Huffman Table,定义 Huffman 树表
length: 31byte (2 byte)----00 1F
Huffman 表类型: 0 (Higher 4 bit) (DC)
Huffman 表 ID: 0 (Lower 4 bit) (0 号表)----00
HuffmanTableIndex: 0
code_len_table:16bytes
剩下的是各码字对应的权值。
后面的三个FFC4分别为AC0号表,DC1号表,AC1号表,这里不再赘述。
FFDA: SOS, Start of Scan,扫描开始
length: 12 byte (2 byte)----00 0C
颜色分量数:3(1byte)----03
颜色分量 ID: 1 (1 byte) (Y)----01
Y Dc HuffmanTreeIndex: 0 (Higher 4 bit)
Y Ac HuffmanTreeIndex: 0(Lower 4 bit)----00
颜色分量 ID: 2 (1 byte) (U or V)----02
UV Dc HuffmanTreeIndex: 1 (Higher 4 bit)
UV Ac HuffmanTreeIndex: 1 (Lower 4 bit)----11
颜色分量 ID: 3 (1 byte) (U or V)----03
UV Dc HuffmanTreeIndex: 1 (Higher 4 bit)
UV Ac HuffmanTreeIndex: 1(Lower 4 bit)----11
谱选择开始 1 byte---- 固定值 0x00
谱选择结束 1 byte---- 固定值 0x3F
谱选择 1 byte---- 在基本 JPEG 中总为 00
二:JPEG解码流程
JPEG解码流程为编码的逆过程,具体流程如下所示:
1 .读取文件
2. 解析 Segment Marker
2.1 解析 SOI
2.2 解析 APP0
检查标识“ JFIF”及版本
得到一些参数
2.3 解析 DQT
得到量化表长度(可能包含多张量化表)
得到量化表的精度
得到及检查量化表的序号(只能是 0 —— 3)
得到量化表内容( 64 个数据)
2.4 解析 SOF0
得到每个 sample 的比特数、长宽、颜色分量数
得到每个颜色分量的 ID、水平采样因子、垂直采样因子、使用的量化表序号(与 DQT 中序号对应)
2.5 解析 DHT
得到 Huffman 表的类型( AC、 DC)、序号
依据数据重建 Huffman 表
2.6 解析 SOS
得到解析每个颜色分量的 DC、 AC 值所使用的 Huffman 表序号(与 DHT中序号对应)
3.依据每个分量的水平、垂直采样因子计算 MCU 的大小,并得到每个 MCU 中 8*8宏块的个数
4 .对每个 MCU 解码(依照各分量水平、垂直采样因子对 MCU 中每个分量宏块解码)
4.1 对每个宏块进行 Huffman 解码,得到 DCT 系数
4.2 对每个宏块的 DCT 系数进行 IDCT,得到 Y、 Cb、 Cr
4.3 遇到 Segment Marker RST 时,清空之前的 DC DCT 系数
5 .解析到 EOI,解码结束
6.将 Y、 Cb、 Cr 转化为需要的色彩空间并保存。
三:关键代码分析
JPEG解码程序工程文件目录如下:
该JPEG解码系统,是按照分层设计的思想组织。因此这里我们按照调试的顺序,通过数据流的走向对整个系统的各个模块进行分析。因此为了更好地描述整个系统,我们在tinyjpeg_internal文件中定义了三个结构体:
struct huffman_table(Huffman码表结构体)
- struct huffman_table
- {
-
-
- short int lookup[HUFFMAN_HASH_SIZE];
-
- unsigned char code_size[HUFFMAN_HASH_SIZE];
-
-
-
- uint16_t slowtable[16-HUFFMAN_HASH_NBITS][256];
- };
struct component (8*8宏块结构体)
- <span style="font-weight: normal;">struct component
-
- {
- unsigned int Hfactor;
- unsigned int Vfactor;
- float *Q_table;
- struct huffman_table *AC_table;
- struct huffman_table *DC_table;
- short int previous_DC; //前一个块的DC系数
- short int DCT[64]; //该块的DCT系数,其中DCT[0]为该块直流,其他为交流
- #if SANITY_CHECK
- unsigned int cid;
- #endif
- };</span>
struct jdec_private(文件解码信息结构体)
- struct jdec_private(文件解码信息结构体)
-
- {
-
- uint8_t *components[COMPONENTS];
- unsigned int width, height; //图像的宽高
- unsigned int flags;
-
-
- const unsigned char *stream_begin, *stream_end;
- unsigned int stream_length;
-
- const unsigned char *stream; //指向当前文件流的指针
- unsigned int reservoir, nbits_in_reservoir;
-
- struct component component_infos[COMPONENTS];
- float Q_tables[COMPONENTS][64];
- struct huffman_table HTDC[HUFFMAN_TABLES];
- struct huffman_table HTAC[HUFFMAN_TABLES];
- int default_huffman_table_initialized;
- int restart_interval;
- int restarts_to_go;
- int last_rst_marker_seen; //固定增长
-
-
- uint8_t Y[64*4], Cr[64], Cb[64];
-
- jmp_buf jump_state;
-
- uint8_t *plane[COMPONENTS];
-
- };
1.读取文件
在命令行中,我们设置了输入的JPEG文件,输出的文件和输出格式:
在主函数main函数中,我们打开对输入输出文件,并解析了输出格式:
- int main(int argc, char *argv[])
- {
- int output_format = TINYJPEG_FMT_YUV420P;
- char *output_filename, *input_filename;
- clock_t start_time, finish_time;
- unsigned int duration;
- int current_argument;
- int benchmark_mode = 0;
-
- #if TRACE//TRACE=1,则中间代码会编译,TRACE=0,则会忽略
- p_trace=fopen(TRACEFILE,"w");
- if (p_trace==NULL)
- {
- printf("trace file open error!");
- }
- #endif
- if (argc < 3)
- usage();
-
- current_argument = 1;
- while (1)
- {
- if (strcmp(argv[current_argument], "--benchmark")==0)
- benchmark_mode = 1;
- else
- break;
- current_argument++;
- }
-
- if (argc < current_argument+2)
- usage();
-
- input_filename = argv[current_argument];
- if (strcmp(argv[current_argument+1],"yuv420p")==0)
- output_format = TINYJPEG_FMT_YUV420P;
- else if (strcmp(argv[current_argument+1],"rgb24")==0)
- output_format = TINYJPEG_FMT_RGB24;
- else if (strcmp(argv[current_argument+1],"bgr24")==0)
- output_format = TINYJPEG_FMT_BGR24;
- else if (strcmp(argv[current_argument+1],"grey")==0)
- output_format = TINYJPEG_FMT_GREY;
-
- else if (strcmp(argv[current_argument+1],"yuvone")==0)
- output_format =TINYJPEG_FMT_YUV420one;
-
- else
- exitmessage("Bad format: need to be one of yuv420p, rgb24, bgr24, grey,yuvone\n");
- output_filename = argv[current_argument+2];
其中通过在系统各处添加trace,通过初始化trace的值,可以在需要的时候输出一些中间结果。
根据数据走向,整个JPEG解码过程,都是由下面的covert_one函数实现:
- if (benchmark_mode)
- load_multiple_times(input_filename, output_filename, output_format);
- else
- convert_one_image(input_filename, output_filename, output_format);
在convert_one_image函数中,我们进行了以下解码处理。
- int convert_one_image(const char *infilename, const char *outfilename, int output_format)
- {
- FILE *fp;
- unsigned int length_of_file;
- unsigned int width, height;
- unsigned char *buf;
- struct jdec_private *jdec;
- unsigned char *components[3];
-
-
- fp = fopen(infilename, "rb");
- if (fp == NULL)
- exitmessage("Cannot open filename\n");
- length_of_file = filesize(fp);
- buf = (unsigned char *)malloc(length_of_file + 4);
- if (buf == NULL)
- exitmessage("Not enough memory for loading file\n");
- fread(buf, length_of_file, 1, fp);
- fclose(fp);
-
-
- jdec = tinyjpeg_init();
- if (jdec == NULL)
- exitmessage("Not enough memory to alloc the structure need for decompressing\n");
-
- if (<strong><span style="color:#ff0000;">tinyjpeg_parse_header(jdec, buf, length_of_file</span></strong>)<0)
- exitmessage(tinyjpeg_get_errorstring(jdec));
-
-
- tinyjpeg_get_size(jdec, &width, &height);
-
- snprintf(error_string, sizeof(error_string),"Decoding JPEG image...\n");
- if (<strong><span style="color:#ff0000;">tinyjpeg_decode</span></strong>(jdec, output_format) < 0)
- exitmessage(tinyjpeg_get_errorstring(jdec));
-
-
-
-
-
-
- tinyjpeg_get_components(jdec, components);
-
-
- switch (output_format)
- {
- case TINYJPEG_FMT_RGB24:
- case TINYJPEG_FMT_BGR24:
- write_tga(outfilename, output_format, width, height, components);
- break;
- case TINYJPEG_FMT_YUV420P:
- write_yuv(outfilename, width, height, components);
- break;
- case TINYJPEG_FMT_GREY:
- write_pgm(outfilename, width, height, components);
- break;
- }
-
-
- tinyjpeg_free(jdec);
-
-
- free(buf);
- return 0;
任务一:将输出文件保存为可供YUVViewer观看的YUV文件。
1.在命令行将输出格式设为要求的格式(yuv),格式名称自定义。
2.在main函数中,条件判断属于哪一格式输出。
- if (strcmp(argv[current_argument+1],"yuv420p")==0)
- output_format = TINYJPEG_FMT_YUV420P;
- else if (strcmp(argv[current_argument+1],"rgb24")==0)
- output_format = TINYJPEG_FMT_RGB24;
- else if (strcmp(argv[current_argument+1],"bgr24")==0)
- output_format = TINYJPEG_FMT_BGR24;
- else if (strcmp(argv[current_argument+1],"grey")==0)
- output_format = TINYJPEG_FMT_GREY;
3.在convert_one_image函数中,进行格式判断,并调用相应的输出函数。
-
- switch (output_format)
- {
- case TINYJPEG_FMT_RGB24:
- case TINYJPEG_FMT_BGR24:
- write_tga(outfilename, output_format, width, height, components);
- break;
- case TINYJPEG_FMT_YUV420P:
- write_yuv(outfilename, width, height, components);
- break;
- case TINYJPEG_FMT_GREY:
- write_pgm(outfilename, width, height, components);
- break;
- }
4.这里输出为yuv,调用write_yuv函数
- static void write_yuv(const char *filename, int width, int height, unsigned char **components)
- {
- FILE *F;
- char temp[1024];
-
- snprintf(temp, 1024, "%s.yuv", filename);
- F = fopen(temp, "ab");
- fwrite(components[0], width, height, F);
- fclose(F);
- snprintf(temp, 1024, "%s.yuv", filename);
- F = fopen(temp, "ab");
- fwrite(components[1], width*height/4, 1, F);
- fclose(F);
- snprintf(temp, 1024, "%s.yuv", filename);
- F = fopen(temp, "ab");
- fwrite(components[2], width*height/4, 1, F);
- fclose(F);
- printf("ok");
- }
2.解析 Segment Marker(tinyjpeg_parse_header中)
tinyjpeg_parse_header函数:
- int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
- {
- int ret;
-
-
- if ((buf[0] != 0xFF) || (buf[1] != SOI))
- snprintf(error_string, sizeof(error_string),"Not a JPG file ?\n");
-
- priv->stream_begin = buf+2;
- priv->stream_length = size-2;
- priv->stream_end = priv->stream_begin + priv->stream_length;
-
- ret = <strong><span style="color:#ff0000;">parse_JFIF</span></strong>(priv, priv->stream_begin);
-
- return ret;
- }
其中的parse_JFIF函数的作用便是解析文件头:
- static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
- {
- int chuck_len;
- int marker;
- int sos_marker_found = 0;
- int dht_marker_found = 0;
- const unsigned char *next_chunck;
-
-
-
- while (!sos_marker_found)
- {
- if (*stream++ != 0xff)
- goto bogus_jpeg_format;
-
- while (*stream == 0xff)
- stream++;
-
- marker = *stream++;
- chuck_len = be16_to_cpu(stream);
- next_chunck = stream + chuck_len;
- switch (marker)
- {
- case SOF:
- if (<strong><span style="color:#ff0000;">parse_SOF</span></strong>(priv, stream) < 0)
- return -1;
- break;
- case DQT:
- if (<strong><span style="color:#ff0000;">parse_DQT</span></strong>(priv, stream) < 0)
- return -1;
- break;
- case SOS:
- if (<span style="color:#ff0000;"><strong>parse_SOS</strong></span>(priv, stream) < 0)
- return -1;
- sos_marker_found = 1;
- break;
- case DHT:
- if (<span style="color:#ff0000;"><strong>parse_DHT</strong></span>(priv, stream) < 0)
- return -1;
- dht_marker_found = 1;
- break;
- case DRI:
- if (parse_DRI(priv, stream) < 0)
- return -1;
- break;
- default:
- #if TRACE
- fprintf(p_trace,"> Unknown marker %2.2x\n", marker);
- fflush(p_trace);
- #endif
- break;
- }
-
- stream = next_chunck;
- }
-
- if (!dht_marker_found) {
- #if TRACE
- fprintf(p_trace,"No Huffman table loaded, using the default one\n");
- fflush(p_trace);
- #endif
- build_default_huffman_tables(priv);
- }
-
- #ifdef SANITY_CHECK
- if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
- || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
- snprintf(error_string, sizeof(error_string),"Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
- if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
- || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
- snprintf(error_string, sizeof(error_string),"Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
- if ( (priv->component_infos[cCb].Hfactor!=1)
- || (priv->component_infos[cCr].Hfactor!=1)
- || (priv->component_infos[cCb].Vfactor!=1)
- || (priv->component_infos[cCr].Vfactor!=1))
- snprintf(error_string, sizeof(error_string),"Sampling other than 1x1 for Cr and Cb is not supported");
- #endif
-
- return 0;
- bogus_jpeg_format:
- #if TRACE
- fprintf(p_trace,"Bogus jpeg format\n");
- fflush(p_trace);
- #endif
- return -1;
- }
parse SOI:
- if ((buf[0] != 0xFF) || (buf[1] != SOI))
- snprintf(error_string, sizeof(error_string),"Not a JPG file ?\n");
parse_DQT函数:定义量化表
得到量化表长度(可能包含多张量化表)
得到量化表的精度
得到及检查量化表的序号(只能是 0 —— 3)
得到量化表内容( 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;
-
- 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];
- build_quantization_table(table, stream);
- stream += 64;
- }
build_quantization_table函数:
- 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++) {
- *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
- }
- }
- }
Zigzag数组:
- static const unsigned char zigzag[64] =
- {
- 0, 1, 5, 6, 14, 15, 27, 28,
- 2, 4, 7, 13, 16, 26, 29, 42,
- 3, 8, 12, 17, 25, 30, 41, 43,
- 9, 11, 18, 24, 31, 40, 44, 53,
- 10, 19, 23, 32, 39, 45, 52, 54,
- 20, 22, 33, 38, 46, 51, 55, 60,
- 21, 34, 37, 47, 50, 56, 59, 61,
- 35, 36, 48, 49, 57, 58, 62, 63
- };
任务二:输出量化表到TXT文件:
在解析量化表之后,我们可以中间输出量化表到txt文件中:即在parse_DQT函数的后面添加如下代码:
-
-
- qfile=fopen("q_file.txt","ab");
- fputs("量化表",qfile);
- fputc(10,qfile);
- for( i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- fprintf(qfile,"%f",*table);
- table++;
- }
- fputc(10,qfile);
-
- }
-
- fclose(qfile);
-
parse_SOF函数:
得到每个 sample 的比特数、长宽、颜色分量数;得到每个颜色分量的 ID、水平采样因子、垂直采样因子、使用的量化表序号(与 DQT 中序号对应)。
- 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++;
- 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;
- c->Hfactor = sampling_factor>>4;
- 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;
- }
解析DHT的函数 parse_DHT:得到 Huffman 表的类型( AC、 DC)、序号
- static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
- {
- unsigned int count, i;
- unsigned char huff_bits[17];
- int length, index;
-
- FILE *hufftable;
- hufftable=fopen("huffmantable_file.txt","ab");
-
-
- length = be16_to_cpu(stream) - 2;
- stream += 2;
- #if TRACE
- fprintf(p_trace,"> DHT marker (length=%d)\n", length);
- fflush(p_trace);
- #endif
-
- while (length>0) {
- index = *stream++;
-
-
- huff_bits[0] = 0;
- count = 0;
- for (i=1; i<17; i++) {
- 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);
-
- fprintf(hufftable,"Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
- fflush(hufftable);
-
-
- #endif
- #endif
-
- if (index & 0xf0 )
- {
- 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;
- }
build_huffman_table:依据数据重建 Huffman 表
- 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;
-
- FILE *hufftable;
- hufftable=fopen("huffmantable_file.txt","ab");
-
-
-
-
-
-
- hz = huffsize;
- for (i=1; i<=16; i++)
- {
- for (j=1; j<=bits[i]; j++)
- *hz++ = i;
- }
- *hz = 0;
-
- memset(table->lookup, 0xff, sizeof(table->lookup));
- for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
- table->slowtable[i][0] = 0;
-
-
-
-
- code = 0;
- hc = huffcode;
- hz = huffsize;
- nbits = *hz;
- while (*hz)
- {
- while (*hz == nbits)
- {
- *hc++ = code++;
- hz++;
- }
- code <<= 1;
- nbits++;
- }
-
-
-
-
- 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);
-
- fprintf(hufftable,"val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
- fflush(hufftable);
-
-
- #endif
-
- table->code_size[val] = code_size;
- if (code_size <= HUFFMAN_HASH_NBITS)
- {
-
-
-
-
- int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
- code <<= HUFFMAN_HASH_NBITS - code_size;
- while ( repeat-- )
- table->lookup[code++] = val;
-
- }
- else
- {
-
- 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;
-
- }
-
- }
-
- }
任务三:输出Huffman码表到txt文件:
在解析重建Huffman表之后,我们可以中间输出Huffman表到txt文件中:即在trace中添加如下代码:
- #if TRACE
- fprintf(p_trace,"Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
- fflush(p_trace);
-
- fprintf(hufftable,"Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
- fflush(hufftable);
-
-
- #endif
parse_SOS函数:
得到解析每个颜色分量的 DC、 AC 值所使用的 Huffman 表序号(与 DHT中序号对应)
- 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++) {
- cid = *stream++;
- table = *stream++;
- #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
- 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;
- }
分析完文件头的各个marker后,我们开始对熵编码数据进行解码,这部分在tinyjpeg_decode函数中实现:
3.依据每个分量的水平、垂直采样因子计算 MCU 的大小,并得到每个 MCU 中 8*8宏块的个数
- xstride_by_mcu = ystride_by_mcu = 8;
- 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) {
- 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 == 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
- } else {
- 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
- }
4.对每个 MCU 解码(依照各分量水平、垂直采样因子对 MCU 中每个分量宏块解码)
对每个宏块进行 Huffman 解码,得到 DCT 系数
对每个宏块的 DCT 系数进行 IDCT,得到 Y、 Cb、 Cr
遇到 Segment Marker RST 时,清空之前的 DC DCT 系数
-
-
-
- static void decode_MCU_1x1_3planes(struct jdec_private *priv)
- {
-
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y, 8);
-
-
- process_Huffman_data_unit(priv, cCb);
- IDCT(&priv->component_infos[cCb], priv->Cb, 8);
-
-
- process_Huffman_data_unit(priv, cCr);
- IDCT(&priv->component_infos[cCr], priv->Cr, 8);
- }
-
-
-
-
- static void decode_MCU_1x1_1plane(struct jdec_private *priv)
- {
-
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y, 8);
-
-
- process_Huffman_data_unit(priv, cCb);
- IDCT(&priv->component_infos[cCb], priv->Cb, 8);
-
-
- process_Huffman_data_unit(priv, cCr);
- IDCT(&priv->component_infos[cCr], priv->Cr, 8);
- }
-
-
-
-
-
-
-
-
- static void decode_MCU_2x1_3planes(struct jdec_private *priv)
- {
-
- 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);
-
-
- process_Huffman_data_unit(priv, cCb);
- IDCT(&priv->component_infos[cCb], priv->Cb, 8);
-
-
- process_Huffman_data_unit(priv, cCr);
- IDCT(&priv->component_infos[cCr], priv->Cr, 8);
- }
-
-
-
-
-
-
-
- static void decode_MCU_2x1_1plane(struct jdec_private *priv)
- {
-
- 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);
-
-
- process_Huffman_data_unit(priv, cCb);
-
-
- process_Huffman_data_unit(priv, cCr);
- }
-
-
-
-
-
-
-
-
-
-
- static void decode_MCU_2x2_3planes(struct jdec_private *priv)
- {
-
- 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);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
-
-
- process_Huffman_data_unit(priv, cCb);
- IDCT(&priv->component_infos[cCb], priv->Cb, 8);
-
-
- process_Huffman_data_unit(priv, cCr);
- IDCT(&priv->component_infos[cCr], priv->Cr, 8);
- }
-
-
-
-
-
-
-
-
-
- static void decode_MCU_2x2_1plane(struct jdec_private *priv)
- {
-
- 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);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
-
-
- process_Huffman_data_unit(priv, cCb);
-
-
- process_Huffman_data_unit(priv, cCr);
- }
-
-
-
-
-
-
-
-
-
- static void decode_MCU_1x2_3planes(struct jdec_private *priv)
- {
-
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y, 8);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64, 8);
-
-
- process_Huffman_data_unit(priv, cCb);
- IDCT(&priv->component_infos[cCb], priv->Cb, 8);
-
-
- process_Huffman_data_unit(priv, cCr);
- IDCT(&priv->component_infos[cCr], priv->Cr, 8);
- }
-
-
-
-
-
-
-
-
-
- static void decode_MCU_1x2_1plane(struct jdec_private *priv)
- {
-
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y, 8);
- process_Huffman_data_unit(priv, cY);
- IDCT(&priv->component_infos[cY], priv->Y+64, 8);
-
-
- process_Huffman_data_unit(priv, cCb);
-
-
- process_Huffman_data_unit(priv, cCr);
- }
process_Huffman_data_unit函数:对一个8*8的彩色分量单元进行解码:
- 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];
-
-
-
- memset(DCT, 0, sizeof(DCT));
-
-
- huff_code = get_next_huffman_code(priv, c->DC_table);
-
- if (huff_code) {
- get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
- DCT[0] += c->previous_DC;
- c->previous_DC = DCT[0];
- } else {
- DCT[0] = c->previous_DC;
- }
-
-
-
- j = 1;
- while (j<64)
- {
- huff_code = get_next_huffman_code(priv, c->AC_table);
-
-
- size_val = huff_code & 0xF;
- count_0 = huff_code >> 4;
- if (size_val == 0)
- {
- if (count_0 == 0)
- break;
- else if (count_0 == 0xF)
- j += 16;
- }
- else
- {
- j += count_0;
- 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]];
-
-
-
- }
5.解完所有 MCU,解码结束
- for (y=0; y < priv->height/ystride_by_mcu; y++)
- {
-
- priv->plane[0] = priv->components[0] + (y * bytes_per_blocklines[0]);
- priv->plane[1] = priv->components[1] + (y * bytes_per_blocklines[1]);
- priv->plane[2] = priv->components[2] + (y * bytes_per_blocklines[2]);
- for (x=0; x < priv->width; x+=xstride_by_mcu)
- {
- decode_MCU(priv);
- convert_to_pixfmt(priv);
- priv->plane[0] += bytes_per_mcu[0];
- priv->plane[1] += bytes_per_mcu[1];
- priv->plane[2] += bytes_per_mcu[2];
- if (priv->restarts_to_go>0)
- {
- priv->restarts_to_go--;
- if (priv->restarts_to_go == 0)
- {
- priv->stream -= (priv->nbits_in_reservoir/8);
- resync(priv);
- if (find_next_rst_marker(priv) < 0)
- return -1;
- }
- }
- }
- }
任务四:输出DC图像,AC图像并经过huffman统计其概率分布
1.在结构体中创建两个int指针和两个char型指针;int型便于计算,char型用于输出
-
- int *dcimg,*acimg;
- unsigned char *dcimg_ch,*acimg_ch;
-
2.定义两个文件指针分别打开两个yuv文件:DC图像和AC图像
由于DCT变换的能量守恒和要求DC系数和AC系数均为有效的数据(0~255),我们定义了用于后面作归一化处理的4个变量:acmax,dcmax,acmin,dcmin。
-
- FILE *DCimg=fopen("DCimage.yuv","wb");
- FILE *ACimg=fopen("ACimage.yuv","wb");
- static int ida=0;
- int acmax, dcmax, acmin, dcmin;
-
3.为创建的指针分配空间:(注意数据类型)
-
- priv->dcimg = (int *)malloc(sizeof(int)*priv->width * priv->height/64);
- priv->acimg = (int*)malloc(sizeof(int)*priv->width * priv->height / 64);
- priv->dcimg_ch = (unsigned char *)malloc(sizeof(unsigned char)*priv->width * priv->height/64);
- priv->acimg_ch = (unsigned char*)malloc(sizeof(unsigned char)*priv->width * priv->height / 64);
-
-
4.每解一个8*8的块,将DC(DCT[0])和AC(这里是DCT[1])写入内存
-
- if(ida<priv->width*priv->height/64)
- {
- priv->dcimg[ida] = priv->component_infos[cY].DCT[0];
- priv->acimg[ida] = priv->component_infos[cY].DCT[1];
- ida++;
- }
-
5.归一化处理
根据DCT的能量守恒特性,反DCT之后的DC取值最大可达到8*256,且反差分编码后数据可能为负数。因此需要对得到的DC系数进行归一化处理,使其值分布在[0,255]之间,
AC系数亦是如此。
-
-
-
- acmax = priv->acimg[0];
- acmin = priv->acimg[0];
- dcmax = priv->dcimg[0];
- dcmin = priv->dcimg[0];
- for (ida = 0; ida < priv->width*priv->height / 64; ida++)
- {
- if (priv->acimg[ida] >=acmax)
- acmax = priv->acimg[ida];
- if (priv->dcimg[ida] >= dcmax)
- dcmax = priv->dcimg[ida];
- if (priv->acimg[ida] <= acmin)
- acmin = priv->acimg[ida];
- if (priv->dcimg[ida] <=dcmin)
- dcmin = priv->dcimg[ida];
- }
- for (ida = 0; ida < priv->width*priv->height / 64; ida++)
- {
- priv->acimg_ch[ida] = (unsigned char)(255 *(priv->acimg[ida]-acmin)/ (acmax - acmin));
- }
-
- for (ida= 0; ida < priv->width*priv->height / 64; ida++)
- {
- priv->dcimg_ch[ida] =(unsigned char)(255 *(priv->dcimg[ida]-dcmin)/ (dcmax - dcmin));
- }
-
-
- fwrite(priv->dcimg_ch, 1, priv->width*priv->height / 64, DCimg);
- fwrite(priv->acimg_ch, 1, priv->width*priv->height / 64, ACimg);
-
-
6.再将输出的DC图像和AC图像经过Huffman编码器,得到其概率分布函数。Huffman编码器的程序前面实验已经给出,这里不再赘述。
四:实验结果
1.任务一实验结果:
2.任务二实验结果:
3.任务三实验结果:
4.任务四实验结果:
输出的DC和AC图像;
DC和AC图像的概率分布: