JPEG原理分析及JPEG解码器的调试

JPEG原理分析及JPEG解码器的调试

实验目的

掌握JPEG编解码系统的基本原理。初步掌握复杂的数据压缩算法实现,并能根据理论分析需要实现所对应数据的输出。

实验内容

基本思路

在这里插入图片描述

SOI & EOI

SOI ,Start of Image, 图像开始
标记代码 2字节 固定值0xFFD8
EOI,End of Image, 图像结束 2字节
标记代码 2字节 固定值0xFFD9

在这里插入图片描述

APP0 应用程序保留标记0

标记代码 2字节 固定值0xFFE0
包含9个具体字段: ① 数据长度 2字节 ①~⑨9个字段的总长度 ② 标识符 5字节 固定值0x4A46494600,即字符串“JFIF0”
③ 版本号 2字节 一般是0x0102,表示JFIF的版本号1.2
④ X和Y的密度单位 1字节 只有三个值可选 0:无单位;1:点数/英寸;2:点数/厘米
⑤ X方向像素密度 2字节 取值范围未知
⑥ Y方向像素密度 2
字节 取值范围未知
⑦ 缩略图水平像素数目 1字节 取值范围未知
⑧ 缩略图垂直像素数目 1字节 取值范围未知
⑨ 缩略图RGB位图 长度可能是3的倍数 缩略图RGB位图数据

在这里插入图片描述

DQT 定义量化表

标记代码 2字节 固定值0xFFDB
包含9个具体字段: ① 数据长度 2字节 字段①和多个字段②的总长度 ② 量化表 数据长度-2字节
a) 精度及量化表ID 1字节
高4位:精度,只有两个可选值 0:8位;1:16位 低4位:量化表ID,取值范围为0~3
b) 表项 (64×(精度+1))字节
例如8位精度的量化表,其表项长度为64×(0+1)=64字节
本标记段中,字段②可以重复出现,表示多个量化表,但最多只能出现4次
在这里插入图片描述
DHT,定义哈夫曼表

标记代码 2字节 固定值0xFFC4
包含2个具体字段:
① 数据长度 2字节
② huffman表 数据长度-2字节
表ID和表类型 1字节
高4位:类型,只有两个值可选
0:DC直流;1:AC交流 低4位:哈夫曼表ID,
注意,DC表和AC表分开编码
不同位数的码字数量 16字节
编码内容 16个不同位数的码字数量之和(字节)本标记段中,字段②可以重复出现(一般4次),也可以只出现1次。

SOS 扫描开始

标记代码 2字节 固定值0xFFDA
包含2个具体字段: ①数据长度 2字节 ①~④两个字段的总长度 ②颜色分量数 1字节 应该和SOF中的字段⑤的值相同,即: 1:灰度图是;3: YCrCb或YIQ;4:CMYK。
③颜色分量信息 a) 颜色分量ID 1字节 b) 直流/交流系数表号 1字节
高4位:直流分量使用的哈夫曼树编号 低4位:交流分量使用的哈夫曼树编号
④ 压缩图像数据 a)谱选择开始 1字节 固定值0x00 b)谱选择结束 1字节 固定值0x3F c)谱选择 1字节 在基本JPEG中总为00

SOF0 帧图像开始

标记代码 2字节 固定值0xFFC0
包含9个具体字段:
① 数据长度 2字节 ①~⑥六个字段的总长度
② 精度 1字节 每个数据样本的位数 通常是8位,一般软件都不支持 12位和16位
③ 图像高度 2字节 图像高度(单位:像素)
④ 图像宽度 2字节 图像宽度(单位:像素)
⑤ 颜色分量数 1字节 只有3个数值可选 1:灰度图;3:YCrCb或YIQ;4:CMYK 而JFIF中使用YCrCb,故这里颜色分量数恒为3
⑥颜色分量信息 颜色分量数×3字节(通常为9字节) a)颜色分量ID 1字节
b)水平/垂直采样因子 1字节
高4位:水平采样因子 低4位:垂直采样因子 c) 量化表 1字节 当前分量使用的量化表的ID

JPEG格式二进制可转换后对应找到相应固定模式和数据

在这里插入图片描述
在这里插入图片描述

代码实现

3个结构体

huffman_table.h
component.h
jdec_private.h

// Huffman码表结构体
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];
};
// 分量结构体:代表当前正在处理的某个分量的8x8块
struct component 
{
  unsigned int Hfactor;//水平采样因子
  unsigned int Vfactor;//垂直采样因子
  float *Q_table;		/* Pointer to the quantisation table to use */
  struct huffman_table *AC_table;//指向该宏块交流系数的Huffman码表
  struct huffman_table *DC_table;//指向该宏块直交流系数的Huffman码表
  short int previous_DC;	/* Previous DC coefficient *///前一个块的DC系数
  short int DCT[64];		/* DCT coef *///该块的DCT系数,其中DCT[0]为该块直流,其他为交流
#if SANITY_CHECK
  unsigned int cid;
#endif
};
// 文件解码信息结构体
struct jdec_private
{
  /* Public variables */
  //分别指向YUV分量结构体的指针数组
  uint8_t *components[COMPONENTS];
  // 图像的宽高
  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;

主要代码

#include <stdio.h>
#ifndef __JPEGDEC_H__
#define __JPEGDEC_H__
#define TINYJPEG_FLAGS_MJPEG_TABLE	(1<<1)
#ifdef __cplusplus
extern "C" {
#endif
#define  snprintf _snprintf
// 通过定义TRACE的值为0或1来打开和关闭TRACE
#define TRACE 1
#define  TRACEFILE "trace_jpeg.txt"
static const unsigned char zigzag[64];
struct jdec_private;
enum tinyjpeg_fmt {
   TINYJPEG_FMT_GREY = 1,
   TINYJPEG_FMT_BGR24,
   TINYJPEG_FMT_RGB24,
   TINYJPEG_FMT_YUV420P,
};
FILE *p_trace;//add by nxn
static char error_string[256];//add by nxn
struct jdec_private *tinyjpeg_init(void);
void tinyjpeg_free(struct jdec_private *priv);
int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size);
int tinyjpeg_decode(struct jdec_private *priv, int pixel_format);
const char *tinyjpeg_get_errorstring(struct jdec_private *priv);
void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height);
int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components);
int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents);
int tinyjpeg_set_flags(struct jdec_private *priv, int flags);
static void outdcac(struct jdec_private *priv, FILE *DC_image, FILE *AC_image);
static void getdcac(struct jdec_private *priv);
#ifdef __cplusplus
}
#endif
#endif

int main(int argc, char *argv[])
{
// 初始化输出文件格式为YUV420P
  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;
// 通过在tinyjpeg.h文件中设置TRACE的值为1以及#if TRACE … #endif打开打开TRACE文件,TRACE是用来跟踪和记录解码过程,存放解码过程中得到的数据
#if TRACE
  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
    exitmessage("Bad format: need to be one of yuv420p, rgb24, bgr24, grey\n");
  output_filename = argv[current_argument+2];

  start_time = clock();

  if (benchmark_mode)
    load_multiple_times(input_filename, output_filename, output_format);
  else
    convert_one_image(input_filename, output_filename, output_format);

  finish_time = clock();
  duration = finish_time - start_time;
  snprintf(error_string, sizeof(error_string),"Decoding finished in %u ticks\n", duration);
#if TRACE
  fclose(p_trace);
#endif
  return 0;
}
convert_one_image:加载一幅jpeg图像,解压缩后存出结果
int convert_one_image(const char *infilename, const char *outfilename, int output_format)
{
  // 定义文件指针
  FILE *fp;
  // 保存文件大小
  unsigned int length_of_file;
  // 保存图像宽高
  unsigned int width, height;
  // 用来存储JPEG文件中的数据的缓冲区
  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");
  // 解析 Segment Marker 
  if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
    exitmessage(tinyjpeg_get_errorstring(jdec));

  /* Get the size of the image */
  tinyjpeg_get_size(jdec, &width, &height);

  snprintf(error_string, sizeof(error_string),"Decoding JPEG image...\n");
  // 文件解码
  if (tinyjpeg_decode(jdec, output_format) < 0)
    exitmessage(tinyjpeg_get_errorstring(jdec));
  /* 
   * Get address for each plane (not only max 3 planes is supported), and
   * depending of the output mode, only some components will be filled 
   * RGB: 1 plane, YUV420P: 3 planes, GREY: 1 plane
   */
  tinyjpeg_get_components(jdec, components);
 
  /* Save it */
  // 按要求输出文件
  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);
  /* else called just free(jdec); */
  free(buf);
  return 0;
}
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, "wb");
  fwrite(components[0], width, height, F);
  fwrite(components[1], width*height / 4, 1, F);
  fwrite(components[2], width*height / 4, 1, F);
  fclose(F);
}

tinyjpeg.c
tinyjpeg_parse_header函数:解析 Segment Marker
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");
  // 如果是0xFFD8,则将文件流的开始向后移两个字节
  priv->stream_begin = buf+2;
  // 将文件的长度也减小两个字节
  priv->stream_length = size-2;
  // 定位到文件的最后
  priv->stream_end = priv->stream_begin + priv->stream_length;
  // 调用parse_JFIF函数解析文件头
  ret = parse_JFIF(priv, priv->stream_begin);

  return ret;
}
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;

  /* Parse marker */
  // 循环一直读到扫描开始,即编码数据块
  while (!sos_marker_found)
   {
     if (*stream++ != 0xff)
       goto bogus_jpeg_format;
     /* Skip any padding ff byte (this is normal) */
     while (*stream == 0xff)
       stream++;

     marker = *stream++;
     chuck_len = be16_to_cpu(stream);
     next_chunck = stream + chuck_len;
     switch (marker)
      {
       case SOF:
	 if (parse_SOF(priv, stream) < 0)
	   return -1;
	 break;
       case DQT:
	 if (parse_DQT(priv, stream) < 0)
	   return -1;
	 break;
       case SOS:
	 if (parse_SOS(priv, stream) < 0)
	   return -1;
	 sos_marker_found = 1;
	 break;
       case DHT:
	 if (parse_DHT(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_DQT函数:解析DQT,得到量化表长度(可能包含多张量化表)、量化表的精度,得到及检查量化表的序号(只能是 0-3),得到量化表内容(64 个数据)
任务三:以txt文件输出所有的量化矩阵
// 解析DQT
static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
{
  int qi;
  //int count = 0;
  // 定义指向量化表的指针
  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];
	 // 得到量化表内容
	 // 数据:64 字节的量化系数,(一个字节对应一个量化系数,对于 8*8 的宏块来说,共 8*8=64 个量化系数) 
     build_quantization_table(table, stream);
	 //count++;
	 /* 添加:以之字形扫描顺序输出量化矩阵*/
	 /* a:以“追加”方式打开文件
	    如果文件不存在,那么创建一个新文件
		如果文件存在,那么将写入的数据追加到文件的末尾(文件原有的内容保留)*/
	 FILE *fp = fopen("table.txt", "a");
	 if (fp == NULL) 
	 {
		 printf("Fail to open file!\n");
		 exit(0);  //退出程序(结束程序)
	 }

	 fprintf(fp, "量化表\n");
	 for (int i = 0; i < 64; i++)
	 {
		 if (i != 0 && i % 8 == 0)
			 fprintf(fp, "\n");
		 fprintf(fp, "%f\t   ", table[i]);
		 
	 }
	 printf(fp, "\n");
     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)
{

  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;
  // 以 zig-zag 序存储 
  for (i=0; i<8; i++) {
     for (j=0; j<8; j++) {
       *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
     }
   }

}
// 之字形扫描顺序
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
};
parse_SOF函数:解析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);
  // 得到颜色分量数(JFIF中使用YCrCb,故这里颜色分量数恒为3) 
  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++) {
	 // 该颜色分量的ID
     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;
}
parse_DHT函数:解析DHT,得到 Huffman 表的类型(AC、DC)、序号, 依据数据重建 Huffman 表
任务四:以txt文件输出所有的HUFFMAN码表
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
{
  unsigned int count, i;
  // 定义存储码长从1至16的码字个数的数组
  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++) 
	 {
		// 各码长的码字个数分别赋值
    	     huff_bits[i] = *stream++;
		// 总码字数
		// count个字节对应的就是每个字符对应的权值
		// 这些权值的含义即为DC系数经DPCM编码后幅度值的位长
	    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
	 /* 添加:以txt文件输出所有的HUFFMAN码表 */
	 FILE *fp = fopen("huffman.txt", "a");
	 if (fp == NULL)
	 {
		 printf("Fail to open file!\n");
		 exit(0);  //退出程序(结束程序)
	 }
	 if (index & 0xf0)
	 // (index&0xf)获得Huffman表序号 
	 {
		 fprintf(fp, "huffman_table	AC%d号表\n", (index & 0xf));
		 build_huffman_table(huff_bits, stream, &priv->HTAC[index & 0xf]);
	 }
	 // 高四位为0是DC表 
	 else
	 {
		 fprintf(fp, "huffman_table	DC%d号表\n", (index & 0xf));
		 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;
}
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;
  // 码长为1-16
  for (i=1; i<=16; i++)
   {
	 // 码长为1-16的码字个数
     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;
  // 第一个码字必定为0
  code = 0;
  hc = huffcode;
  hz = huffsize;
  nbits = *hz;
  while (*hz)
   {
     while (*hz == nbits)
     {
		*hc++ = code++;
		hz++;
     }
     code <<= 1;
     nbits++;
   }

  /*
   * Build the lookup table, and the slowtable if needed.
   */
   /* 添加:以txt文件输出所有的HUFFMAN码表 */
  FILE *fp = fopen("huffman.txt", "a");
  if (fp == NULL)
  {
	  printf("Fail to open file!\n");
	  exit(0);  //退出程序(结束程序)
  }
  next_free_entry = -1;
  for (i=0; huffsize[i]; i++)
   {
	 // 每个码字的权值,即解码时需要再读入的bit位数
     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(fp, "val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
	 fflush(fp);
    #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 */
      }
   } 
}
parse_SOS函数:解析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++) {
	 // 得到ID
     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
	 // Huffman表序号,高四位:DC 低四位:AC 
     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;
}
Tinyjpeg_decode函数:依据每个分量的水平、垂直采样因子计算 MCU 的大小,并得到每个 MCU 中 8*8 宏块的个数
任务五:输出DC图像和某一个AC值图像并分别统计其概率分布(使用第三个实验中的Huffman编码器)
// 定义两个文件指针分别打开两个yuv文件:DC图像和AC图像
// add
  FILE *DC_image = fopen("DCimage.yuv", "wb");
  FILE *AC_image = fopen("ACimage.yuv", "wb");
  // 为创建的指针分配空间
  // 每个8x8的块只有一个直流分量
  priv->dc = (int *)malloc(sizeof(int) * (priv->width * priv->height) / 64);
  // 因为只输出某一AC值图像,所以AC和DC所占空间相同
  priv->ac = (int *)malloc(sizeof(int) * (priv->width * priv->height) / 64);
  
  //add
// 初始化为 4:4:4 时的情况,即MCU的宽和高均为8x8
  xstride_by_mcu = ystride_by_mcu = 8;
  // 如果Y分量的垂直和水平采样因子相等,则每个MCU就包括1个Y分量
  if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
	  // 4:4:4   
	  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];
	 // 一个 MCU 的高为 16 
     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) {
	 // 如果水平采样因子为2,垂直为2,则每个MCU包含4个Y分量
     decode_MCU = decode_mcu_table[3];
     convert_to_pixfmt = colorspace_array_conv[3];
	 // 一个 MCU 的宽为 16 
     xstride_by_mcu = 16;
	 // 一个 MCU 的高为 16 
     ystride_by_mcu = 16;
#if TRACE 
	 fprintf(p_trace,"Use decode 2x2 sampling\n");
	 fflush(p_trace);
#endif
  } else {
	 // 如果水平采样因子为2,垂直为1,则每个MCU包含2个Y分量
     decode_MCU = decode_mcu_table[2];
     convert_to_pixfmt = colorspace_array_conv[2];
	 // 一个 MCU 的宽为 16 
     xstride_by_mcu = 16;
#if TRACE
     fprintf(p_trace,"Use decode 2x1 sampling\n");
	 fflush(p_trace);
#endif
  }

  resync(priv);

  /* Don't forget to that block can be either 8 or 16 lines */
  bytes_per_blocklines[0] *= ystride_by_mcu;
  bytes_per_blocklines[1] *= ystride_by_mcu;
  bytes_per_blocklines[2] *= ystride_by_mcu;

  bytes_per_mcu[0] *= xstride_by_mcu/8;
  bytes_per_mcu[1] *= xstride_by_mcu/8;
  bytes_per_mcu[2] *= xstride_by_mcu/8;
  
   /* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
   // 行循环 
  for (y=0; y < priv->height/ystride_by_mcu; y++)
   {
     //trace("Decoding row %d\n", 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)
     {
// 解码(Huffman 解码 + IDCT) 
// 每解码一个8x8的宏块,调用getdcac函数将DC(DCT[0])和AC(这里是DCT[1])写入内存
	getdcac(priv);
	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);
		  // 清空 preDC(所有颜色分量) 
	      resync(priv);
		  // 查找 RST 标记 
	      if (find_next_rst_marker(priv) < 0)
		return -1;
	    }
	 }
      }
   }
  
#if TRACE
  fprintf(p_trace,"Input file size: %d\n", priv->stream_length+2);
  fprintf(p_trace,"Input bytes actually read: %d\n", priv->stream - priv->stream_begin + 2);
  fflush(p_trace);
#endif
   //反DCT之后的DC取值最大可达到8*256,
   
 outdcac(priv, DC_image, AC_image);
  
  return 0;
}
static void getdcac(struct jdec_private *priv)
{
	static int i = 0;
	if (i < priv->width * priv->height / 64)
	{
		priv->dc[i] = priv->component_infos[0].DCT[0];
		priv->ac[i] = priv->component_infos[0].DCT[1];
	}
	i++;
}
static void outdcac(struct jdec_private *priv,FILE *DC_image, FILE *AC_image)
{
	priv->outdc = (unsigned char*)malloc(sizeof(unsigned char) * (priv->width * priv->height) / 64);
	priv->outac = (unsigned char*)malloc(sizeof(unsigned char) * (priv->width * priv->height) / 64);
	// 添加
	int dcmax = priv->dc[0];
	int acmax = priv->ac[0];
	int dcmin = priv->dc[0];
	int acmin = priv->ac[0];

	for (int j = 0; j < priv->width * priv->height / 64; j++)
	{
		if (priv->dc[j] > dcmax)
			dcmax = priv->dc[j];
		if (priv->dc[j] < dcmin)
			dcmin = priv->dc[j];
		if (priv->ac[j] > acmax)
			acmax = priv->ac[j];
		if (priv->ac[j] < acmin)
			acmin = priv->ac[j];
	}
     }
	// 归一化处理
	for (int k = 0; k < priv->width * priv->height / 64; k++)
	{
		priv->outdc[k] = (unsigned char)(255 * (priv->dc[k] - dcmin) / (dcmax - dcmin));
		priv->outac[k] = (unsigned char)(255 * (priv->ac[k] - acmin) / (acmax - acmin));
	}

	fwrite(priv->outdc, 1, priv->width * priv->height / 64, DC_image);
	fwrite(priv->outac, 1, priv->width * priv->height / 64, AC_image);
}
//对每个 MCU 解码(依照各分量水平、垂直采样因子对 MCU 中每个分量宏块解码) 
//1. 对每个宏块进行 Huffman 解码,得到 DCT 系数 
//2. 对每个宏块的 DCT 系数进行 IDCT,得到 Y、Cb、Cr 
//3. 遇到 Segment Marker RST 时,清空之前的 DC DCT 系数
// 4:4:4
static void decode_MCU_1x1_3planes(struct jdec_private *priv)
{
  // Y
  // 以 8x8 宏块为单位进行 Huffman 解码 
  process_Huffman_data_unit(priv, cY);
  // 对得到的 DCT 系数进行 IDCT   
  IDCT(&priv->component_infos[cY], priv->Y, 8);
  
  // 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);
}

/*
 * Decode a 1x1 directly in 1 color
 */
static void decode_MCU_1x1_1plane(struct jdec_private *priv)
{
  // Y
  process_Huffman_data_unit(priv, cY);
  IDCT(&priv->component_infos[cY], priv->Y, 8);
  
  // 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);
}


static void decode_MCU_2x1_3planes(struct jdec_private *priv)
{
  // 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);

  // 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);
}


static void decode_MCU_2x1_1plane(struct jdec_private *priv)
{
  // 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);

  // Cb
  process_Huffman_data_unit(priv, cCb);

  // Cr
  process_Huffman_data_unit(priv, cCr);
}


static void decode_MCU_2x2_3planes(struct jdec_private *priv)
{
  // 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);
  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);

  // 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);
}


static void decode_MCU_2x2_1plane(struct jdec_private *priv)
{
  // 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);
  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);

  // Cb
  process_Huffman_data_unit(priv, cCb);

  // Cr
  process_Huffman_data_unit(priv, cCr);
}


static void decode_MCU_1x2_3planes(struct jdec_private *priv)
{
  // Y
  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);

  // 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);
}


static void decode_MCU_1x2_1plane(struct jdec_private *priv)
{
  // Y
  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);

  // Cb
  process_Huffman_data_unit(priv, cCb);

  // Cr
  process_Huffman_data_unit(priv, cCr);
}

//process_Huffman_data_unit函数:以 8x8 宏块为单位进行 Huffman 解码
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);
  //trace("+ %x\n", huff_code);
  if (huff_code) {
	 // 查表的 DC DCT 系数(残值)
     get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
	 // DC系数采用差分编码,恢复原值 
	 DCT[0] += c->previous_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);
     //trace("- %x\n", huff_code);
	 // Amplitude 幅度 
     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]];
}


输出结果

test.jpg文件

在这里插入图片描述

输出AC、DC图像

在这里插入图片描述
输出矩阵形式:

在这里插入图片描述
AC、DC转化txt数据后概率分布图

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值