c JPEG编码,但有错误

news2024/12/22 20:33:47


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>  //v4l2 头文件
#include <string.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <math.h>
#define PI 3.1415926

#define  pic_width   80
#define  pic_heigth  48


#define filename  "/home/wjs/Pictures/sample.yuv"
#define file_out  "/home/wjs/Pictures/"           //输出文件目录

static unsigned char o_bit[900000] = {};
static int to = 0;

int  main(void) {
	
	
	//-------JPEG通用量化表--------------------------------
	unsigned char lhb0[0x45] = {0xff, 0xdb, 0, 0x43, 0,
	                            16, 11, 10, 16, 24, 40, 51, 61,
	                            12, 12, 14, 19, 26, 58, 60, 55,
	                            14, 13, 16, 24, 40, 57, 69, 56,
	                            14, 17, 22, 29, 51, 87, 80, 62,
	                            18, 22, 37, 56, 68, 109, 103, 77,
	                            24, 35, 55, 64, 81, 104, 113, 92,
	                            49, 64, 78, 87, 103, 121, 120, 101,
	                            72, 92, 95, 98, 112, 100, 103, 99
		
	
		};

	unsigned char lhb1[0x45] = {0xff, 0xdb, 0, 0x43, 1,
	                            17, 18, 24, 47, 99, 99, 99, 99,     //17,18,24,47
	                            18, 21, 26, 66, 99, 99, 99, 99,     //18,21,26,66,
	                            24, 26, 56, 99, 99, 99, 99, 99,      //24,26,56
	                            47, 66, 99, 99, 99, 99, 99, 99,      //47,66,
	                            99, 99, 99, 99, 99, 99, 99, 99,
	                            99, 99, 99, 99, 99, 99, 99, 99,
	                            99, 99, 99, 99, 99, 99, 99, 99,
	                            99, 99, 99, 99, 99, 99, 99, 99
	
	};
	
	//-------JPEG通用霍夫曼表------------------------------

	unsigned char hfm0[] = {0xff, 0xc4, 0, 0x1f, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
	                        1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
	                       };
	unsigned char hfm1[] = { 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17,
	                         5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240,
	                         36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69,
	                         70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118,
	                         119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162,
	                         163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198,
	                         199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233,
	                         234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250
	                       };

	unsigned char hfm2[] = { 255, 196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
	                         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
	                       };
	unsigned char hfm3[] = {255, 196, 0, 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2,
	                        119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177,
	                        193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41,
	                        42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101,
	                        102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137,
	                        138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179,
	                        180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214,
	                        215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249,
	                        250
	                       };

	//------ali--------------------------------------
	int ali(int i, int out[2]) {      //out[1]=len  out[0]=int

		int o = -1;    //如果输出负数无意义
		char len = -1; //如果输出负数无意义

		if (i == 0) {
			len = 0;
			o = 0;
		}
		if (i == -1) {
			len = 1;
			o = 0;
		}
		if (i == 1) {
			len = 1;
			o = 1;
		}

		if ((i >= 2) && (i <= 32767)) {     //二进制位数0-16位
			for (int a = 0; a < 16; a++) {
				if ((i >= pow(2, a)) && (i < pow(2, (a + 1)))) {
					len = a + 1;
					o = i;
				}
			}
		}
		if ((i >= -32767) && (i <= -2)) {
			for (int a = 0; a < 16; a++) {
				if ((i <= -pow(2, a)) && (i > -pow(2, (a + 1)))) {
					len = a + 1;
					o = i + pow(2, (a + 1)) - 1;
				}
			}
		}

		out[1] = len;
		out[0] = o;
		return 0;
	}
	//-------------Y_DC--------------------------------
	int hfm_ydc(unsigned char  i, int out[2]) { //out[1]=len  out[0]=int

		if (i == 0) {
			out[1] = 2;
			out[0] = 0b00;
		}
		if (i == 1) {
			out[1] = 3;
			out[0] = 0b010;
		}
		if (i == 2) {
			out[1] = 3;
			out[0] = 0b011;
		}
		if (i == 3) {
			out[1] = 3;
			out[0] = 0b100;
		}
		if (i == 4) {
			out[1] = 3;
			out[0] = 0b101;
		}
		if (i == 5) {
			out[1] = 3;
			out[0] = 0b110;
		}
		if (i == 6) {
			out[1] = 4;
			out[0] = 0b1110;
		}
		if (i == 7) {
			out[1] = 5;
			out[0] = 0b11110;
		}
		if (i == 8) {
			out[1] = 6;
			out[0] = 0b111110;
		}
		if (i == 9) {
			out[1] = 7;
			out[0] = 0b1111110;
		}
		if (i == 10) {
			out[1] = 8;
			out[0] = 0b11111110;
		}
		if (i == 11) {
			out[1] = 9;
			out[0] = 0b111111110;
		}
		return 0;
	}
	//-------------UV_DC-----------------------------------
	int hfm_uvdc(unsigned char i, int out[2]) { //out[1]=len  out[0]=int
		if (i == 0) {
			out[1] = 2;
			out[0] = 0;
		}
		if (i == 1) {
			out[1] = 2;
			out[0] = 0b01;
		}
		if (i == 2) {
			out[1] = 2;
			out[0] = 0b10;
		}
		if (i == 3) {
			out[1] = 3;
			out[0] = 0b110;
		}
		if (i == 4) {
			out[1] = 4;
			out[0] = 0b1110;
		}
		if (i == 5) {
			out[1] = 5;
			out[0] = 0b11110;
		}
		if (i == 6) {
			out[1] = 6;
			out[0] = 0b111110;
		}
		if (i == 7) {
			out[1] = 7;
			out[0] = 0b1111110;
		}
		if (i == 8) {
			out[1] = 8;
			out[0] = 0b11111110;
		}
		if (i == 9) {
			out[1] = 9;
			out[0] = 0b111111110;
		}
		if (i == 10) {
			out[1] = 10;
			out[0] = 0b1111111110;
		}
		if (i == 11) {
			out[1] = 11;
			out[0] = 0b11111111110;
		}
		return 0;
	}
	//---------霍夫曼编码Y_AC-----------------------------------
	int hfm_yac(unsigned char i_0, unsigned char i_len, unsigned int out[2]) {

		//	unsigned char i_0=0xf;                      //out[1]=len  out[0]=int
		//	unsigned char i_len=0xa;

		unsigned int len;
		unsigned int o;

		unsigned char zj = i_0 * 16 + i_len; //合成一个字节

		unsigned char ws[16] = {};
		memcpy(ws, &hfm1[5], 16);

		unsigned char zh[162] = {};
		memcpy(zh, &hfm1[21], 162);

		int cx_ws, cx_b;
		unsigned char hfm[17][0x7d] = {};
		int t = 0;
		for (int a = 0; a < 16; a++) {     //把要编码的162个数按位数分为16个数组,二位一组....16位一组
			if (ws[a] == 0) {
				continue;
			}

			for (int b = 0; b < ws[a]; b++) {
				hfm[a + 1][b] = zh[t];
				t++;
			}
		}
		for (int a = 0; a < 16; a++) {       //查询输入数的位数和在所在组的顺序,组内顺序从0开始
			if (ws[a] == 0) {
				continue;
			}

			for (int b = 0; b < ws[a]; b++) {
				if (	hfm[a + 1][b] == zj) {
					cx_ws = a + 1;     //得到输入数二进制位数,根据此数到相依位数的数组查询
					cx_b = b;          //输入数在按位数分组的数组中所在的位置,从0开始
					break;
				}
			}
		}

		int o_js = 0;     //每一组的第一个数的值
		if (cx_ws == 2) {
			o_js = 0b00;
		}
		if (cx_ws == 3) {
			o_js = 0b100;
		}
		if (cx_ws == 4) {
			o_js = 0b1010;
		}
		if (cx_ws == 5) {
			o_js = 0b11010;
		}
		if (cx_ws == 6) {
			o_js = 0b111010;
		}
		if (cx_ws == 7) {
			o_js = 0b1111000;
		}
		if (cx_ws == 8) {
			o_js = 0b11111000;
		}
		if (cx_ws == 9) {
			o_js = 0b111110110;
		}
		if (cx_ws == 10) {
			o_js = 0b1111110110;
		}
		if (cx_ws == 11) {
			o_js = 0b11111110110;
		}
		if (cx_ws == 12) {
			o_js = 0b111111110100;
		}
		if (cx_ws == 15) {
			o_js = 0b111111111000000;
		}
		if (cx_ws == 16) {
			o_js = 0b1111111110000010;
		}
		len = cx_ws;
		o = o_js + cx_b;
		out[1] = len;
		out[0] = o;
		return 0;
	}

	//---------UV_AC---------------------------------

	int hfm_uvac(unsigned char i_0, unsigned char i_len, unsigned int out[2]) {

		//	unsigned char i_0=0xf;                         //out[1]=len  out[0]=int
		//	unsigned char i_len=0xa;

		unsigned int len;
		unsigned int o;

		unsigned char zj = i_0 * 16 + i_len; //合成一个字节

		unsigned char ws[16] = {};
		memcpy(ws, &hfm3[5], 16);

		unsigned  char zh[162] = {};
		memcpy(zh, &hfm3[21], 162);

		int cx_ws, cx_b;
		unsigned char hfm[17][0x7d] = {};
		int t = 0;
		for (int a = 0; a < 16; a++) {     //把要编码的162个数按位数分为16个数组,二位一组....16位一组
			if (ws[a] == 0) {
				continue;
			}

			for (int b = 0; b < ws[a]; b++) {
				hfm[a + 1][b] = zh[t];
				t++;
			}
		}
		for (int a = 0; a < 16; a++) {       //查询输入数的位数和在所在组的顺序,组内顺序从0开始
			if (ws[a] == 0) {
				continue;
			}

			for (int b = 0; b < ws[a]; b++) {
				if (	hfm[a + 1][b] == zj) {
					cx_ws = a + 1;     //得到输入数二进制位数,根据此数到相依位数的数组查询
					cx_b = b;          //输入数在按位数分组的数组中所在的位置,从0开始
					break;
				}
			}
		}

		int o_js = 0;     //每一组的第一个数的值
		if (cx_ws == 2) {
			o_js = 0b00;
		}
		if (cx_ws == 3) {
			o_js = 0b100;
		}
		if (cx_ws == 4) {
			o_js = 0b1010;
		}
		if (cx_ws == 5) {
			o_js = 0b11000;
		}
		if (cx_ws == 6) {
			o_js = 0b111000;
		}
		if (cx_ws == 7) {
			o_js = 0b1111000;
		}
		if (cx_ws == 8) {
			o_js = 0b11110110;
		}
		if (cx_ws == 9) {
			o_js = 0b111110100;
		}
		if (cx_ws == 10) {
			o_js = 0b1111110110;
		}
		if (cx_ws == 11) {
			o_js = 0b11111110110;
		}
		if (cx_ws == 12) {
			o_js = 0b111111110100;
		}
		if (cx_ws == 14) {
			o_js = 0b11111111100000;
		}
		if (cx_ws == 15) {
			o_js = 0b111111111000010;
		}
		if (cx_ws == 16) {
			o_js = 0b1111111110001000;
		}
		len = cx_ws;
		o = o_js + cx_b;
		out[1] = len;
		out[0] = o;
		return 0;
	}


//-----------FDCT()函数------------------------------------
	int fdct( unsigned char i[8][8], int o[8][8] ) {  //i 为输入   o 为参数传入的输出转换后的数据


		double s;
		double au;
		double av;

		for (int u = 0; u < 8; u++) {
			for (int v = 0; v < 8; v++) {
				for (int y = 0; y < 8; y++) {
					for (int x = 0; x < 8; x++) {
						s = s + (1.0 / 4) * (i[y][x] - 128) * cos((2 * y + 1) * u * PI / 16) * cos((2 * x + 1) * v * PI / 16);
					}
				}

				if (u == 0) {
					au = 1.0 / sqrt(2);
				} else {
					au = 1.0;
				}
				if (v == 0) {
					av = 1.0 / sqrt(2);
				} else {
					av = 1.0;
				}

				s = s * au * av;   //-30.1856
				int s1 = round(s * 100); //-3019
				s = s1 / 100.0;    //-30.19

				o[u][v] = s;       //double 转为char 类型
				s = 0;
			}
		}

		return 0;
	}
//-----------规范RLC格式---------------------
	int zl(int len, int (*i)[2], int (*o)[2]) {
		int t = 0;                      //如果中间有一次超过15个0,o的下标要加一,因为增加了(15,0)
		for (int a = 0; a < len; a++) {

			if ((a < len) && (i[a][1] >= 16) && (i[a][0] != 0)) {
				o[a + t][0] = 0;
				o[a + t][1] = 15;
				o[a + 1 + t][0] = i[a][0];
				o[a + 1 + t][1] = i[a][1] - 15;
				t++;
			}
			if ((a < len) && (i[a][1] < 16)) {
				memcpy(&(o[a + t][0]), &(i[a][0]), 8); //一行为单位复制
			}
			if ((a == len) && (i[a][0] == 0)) {
				o[a + t][0] = 0;
				o[a + t][1] = 0;
				break;
			}

		}

		return len + t;
	}

//-----------去0-----------------------------
	int q0(int i[64], int (*o)[2]) {

		int t = 0;         //输出数组序号
		int z = 0;         //计算连续的0
		o[0][1] = 0;
		o[0][0] = i[0];
		t = 1;
		for (int a = 1; a < 64; a++) {                               //  a
			if ((i[a] == 0) && (i[a + 1] == 0) && ((a + 1) < 63)) { //000001
				z++;

			}                                                        // a
			if ((i[a] == 0) && (i[a + 1] == 0) && ((a + 1) == 63)) { //0000结束
				z++;             //本次的0
				o[t][0] = 0;
				o[t][1] = z + 1; //加a+1的0
				break;           //判断完成

			}                                               //  a
			if ((i[a] == 0) && (i[a + 1] != 0)) {           //000100
				z++;                  //加上本次的一个0
				o[t][0] = i[a + 1];
				o[t][1] = z;
				z = 0;           //清0,计算下次的连续0
				t++;
				a = a + 1;

			}
			if ((i[a] != 0) && (a == 0)) { //第一个数非0
				o[t][0] = i[a];
				o[t][1] = 0;
				t++;

			}
			if ((a > 0) && (i[a] != 0) && (i[a - 1] != 0)) { //防止第3种重复读取,这种是读取连续的非0
				o[t][0] = i[a];
				o[t][1] = 0;
				t++;

			}

		}

		return t + 1;
	}

//--------Z 排序--------------------------------

	int zz(int (*i)[8], int o[64]) {
		int zb[64] = {0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6,
		              7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47,
		              55, 62, 63
		             };
		int *p = (int *)i;
		for (int a = 0; a < 64; a++) {

			o[a] = p[zb[a]];
		}

		return 0;
	}
// -----量化函数---------------
	int lh(int (*i)[8], unsigned char (*lhb)[8], int (*o)[8]) {             //?
		for (int a = 0; a < 8; a++) {
			for (int b = 0; b < 8; b++) {
				o[a][b] = round((i[a][b]) / (lhb[a][b]));
			}
		}

		return 0;
	}


//---------编码start-------------------------------------

//---------读量化表-------------------------------
	char lh00[64] = {};                 //提取量化表
	char lh10[64] = {};

	memcpy(lh00, &lhb0[5], 64); //使用自定义量化表
	memcpy(lh10, &lhb1[5], 64);


//-----------读yuv ----------------------------------------------------------------
	FILE *f = fopen(filename, "rb");  //如用mmap 必须以读写方式打开文件
	if (f == NULL) {
		puts("filename error");
		exit(-1);
	}
	fseek(f, 0, SEEK_END);
	int len_file = ftell(f);
	fseek(f, 0, SEEK_SET);

	int fd = fileno(f);
	unsigned char *mp = mmap(NULL, len_file, PROT_READ, MAP_SHARED, fd, 0); //必须要读,写

                                                               //480-32
	unsigned char yfl[pic_heigth * pic_width] = {};
	unsigned char ufl[pic_heigth * pic_width ] = {};
	unsigned char  vfl[pic_heigth * pic_width ] = {};

	for (int a = 0; a < pic_heigth * pic_width; a++) {
		yfl[a] = mp[a];
	}
	for (int a = 0; a < pic_heigth * pic_width / 4; a++) {
		ufl[a] = mp[a + pic_heigth * pic_width];
	}
	for (int a = 0; a < pic_heigth * pic_width / 4; a++) {
		vfl[a] = mp[a + pic_heigth * pic_width * 5 / 4];
	}



	
	//-------Y 分割8×8----------------------------


	int yp = 0;              //输出数组序号
	unsigned char y64[pic_width * pic_heigth / 64][64] = {};   //输出数组

	for (int y = 0; y < pic_heigth; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width; x = x + 8) { //提取左上角点的水平数据
			int n = 0;           //每一个数组下标取值 0-15

			for (int a = 0; a < 8; a++) {
				for (int b = 0; b < 8; b++) {
					y64[yp][n] = yfl[pic_width * (a + y) + (b + x)];
					n++;
				}
			}
			yp++;
		}
	}

	//-------U分割8×8----------------------------


	int up = 0;              //输出数组序号
	unsigned char u64[pic_width * pic_heigth /256][64] = {}; //输出数组

	for (int y = 0; y < pic_heigth/2; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width/2; x = x + 8) { //提取左上角点的水平数据
			int n = 0;           //每一个数组下标取值 0-15

			for (int a = 0; a < 8; a=a+1) {
				for (int b = 0; b < 8; b=b+1) {
					u64[up][n] = ufl[(pic_width/2) * (a + y) + (b + x)];
				
					n++;
				}
			}
			up++;
		}
	}


//-------V分割8×8----------------------------


	int vp = 0;              //输出数组序号
	unsigned char v64[pic_width * pic_heigth /256][64] = {}; //输出数组

	for (int y = 0; y < pic_heigth/2 ; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width/2 ; x = x + 8) { //提取左上角点的水平数据
			int n = 0;           //每一个数组下标取值 0-15
 
			for (int a = 0; a < 8; a=a+1) {
				for (int b = 0; b < 8; b=b+1) {
					v64[vp][n] = vfl[(pic_width/2) * (a + y) + (b + x)];
					
					n++;
				}
			}
			vp++;
		}
	}

	//------Y处理函数---------------------------------
   

	int yy(int ysc, unsigned char lh0[8][8],unsigned char y[8][8]) {


		int y_fdct[8][8] = {};
		fdct(y, y_fdct);
		

		int y_lh[8][8] = {};
		lh(y_fdct, lh0, y_lh);

		int y_z[64] = {};
		zz(y_lh, y_z);

		int y_0[64][2] = {};
		int len_y_0 = q0(y_z, y_0);
		int y_zl[64][2] = {};

		int len_y_zl = zl(len_y_0, y_0, y_zl);

		//--------Y-DC----------------------------------
		int ydc[2] = {};       //ydc[1]=len  ydc[0]=int
		int q = y_zl[0][0] - ysc; //DC 差值
		ali(q, ydc);           //处理Y_DC
		int ydc_hfm[2] = {};   //ydc_hfm[1]=len
		hfm_ydc(ydc[1], ydc_hfm);

		int ls = (ydc_hfm[0] << ydc[1]) | ydc[0]; //组合成一int

		for (int a = (ydc[1] + ydc_hfm[1]); a > 0; a--) {
			o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
			to++;
		}
		//-------Y-AC-------------------------------
		for (int a = 1; a < len_y_zl; a++) {
			int yac[2] = {};
			ali(y_zl[a][0], yac);              //取AC 值
			unsigned int yac_h[2] = {};
			hfm_yac(y_zl[a][1], yac[1], yac_h); //0的个数+系数位数
			int ls = (yac_h[0] << yac[1]) | yac[0];
			for (int a = (yac[1] + yac_h[1]); a > 0; a--) {
				o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
				to++;
			}
		}
		return y_zl[0][0];                     //返回本次的Y_DC值
	}

	unsigned char (*lh0)[8] = (unsigned char (*)[8])lh00;
	unsigned char (*lh1)[8] = (unsigned char (*)[8])lh10;    //UV 量化表转成8×8
	


	//------UV处理函数---------------------------------


	int uu(int usc, unsigned char lh1[8][8],unsigned char u[8][8]) {


		int u_fdct[8][8] = {};
		fdct(u, u_fdct);

		int u_lh[8][8] = {};
		lh(u_fdct, lh1, u_lh);

		int u_z[64] = {};
		zz(u_lh, u_z);

		int u_0[64][2] = {};
		int len_u_0 = q0(u_z, u_0);
		int u_zl[64][2] = {};

		int len_u_zl = zl(len_u_0, u_0, u_zl);

		//--------UV-DC----------------------------------
		int udc[2] = {};       //ydc[1]=len  ydc[0]=int
		int q = u_zl[0][0] - usc; //DC 差值
		ali(q, udc);           //处理Y_DC
		int udc_hfm[2] = {};   //ydc_hfm[1]=len
		hfm_uvdc(udc[1], udc_hfm);

		int ls = (udc_hfm[0] << udc[1]) | udc[0]; //组合成一int

		for (int a = (udc[1] + udc_hfm[1]); a > 0; a--) {
			o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
			to++;
		}
		//-------UV-AC-------------------------------
		for (int a = 1; a < len_u_zl; a++) {
			int uac[2] = {};
			ali(u_zl[a][0], uac);              //取AC 值
			unsigned int uac_h[2] = {};
			hfm_uvac(u_zl[a][1], uac[1], uac_h); //0的个数+系数位数
			int ls = (uac_h[0] << uac[1]) | uac[0];
			for (int a = (uac[1] + uac_h[1]); a > 0; a--) {
				o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
				to++;
			}
		}
		return u_zl[0][0];                     //返回本次的Y_DC值
	}
	
	
	
	int vv(int vsc, unsigned char lh1[8][8],unsigned char v[8][8]) {
		
		
		int v_fdct[8][8] = {};
		fdct(v, v_fdct);
		
		int v_lh[8][8] = {};
		lh(v_fdct, lh1, v_lh);
		
		int v_z[64] = {};
		zz(v_lh, v_z);
		
		int v_0[64][2] = {};
		int len_v_0 = q0(v_z, v_0);
		int v_zl[64][2] = {};
		
		int len_v_zl = zl(len_v_0, v_0, v_zl);
		
		//--------UV-DC----------------------------------
		int vdc[2] = {};       //ydc[1]=len  ydc[0]=int
		int q = v_zl[0][0] - vsc; //DC 差值
		ali(q, vdc);           //处理Y_DC
		int vdc_hfm[2] = {};   //ydc_hfm[1]=len
		hfm_uvdc(vdc[1], vdc_hfm);
		
		int ls = (vdc_hfm[0] << vdc[1]) | vdc[0]; //组合成一int
		
		for (int a = (vdc[1] + vdc_hfm[1]); a > 0; a--) {
			o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
			to++;
		}
		//-------UV-AC-------------------------------
		for (int a = 1; a < len_v_zl; a++) {
			int vac[2] = {};
			ali(v_zl[a][0], vac);              //取AC 值
			unsigned int vac_h[2] = {};
			hfm_uvac(v_zl[a][1], vac[1], vac_h); //0的个数+系数位数
			int ls = (vac_h[0] << vac[1]) | vac[0];
			for (int a = (vac[1] + vac_h[1]); a > 0; a--) {
				o_bit[to] = (ls & (int)pow(2, (a - 1))) >> (a - 1); //取ls 每一位数
				to++;
			}
		}
		return v_zl[0][0];                     //返回本次的Y_DC值
	}
//----------------------------------------------------------------
	int udiff = 0;
	int vdiff = 0;
	int ydiff = 0;
	
	for (int a = 0; a < pic_width * pic_heigth /256; a++) {
	
		for(int b=0;b<4;b++){
		   unsigned char y1[8][8] = {};
		   memcpy(y1, &(y64[a*4+b][0]), 64);
	    	ydiff = yy(ydiff,lh0, y1);
		}
	
		unsigned char u1[8][8] = {};
		memcpy(u1, &(u64[a][0]), 64);	
		udiff = uu(udiff,lh1, u1);
		unsigned char v1[8][8] = {};
		memcpy(v1, &(v64[a][0]), 64);
		vdiff = vv(vdiff,lh1, v1);
	 
	}

	//-------生成jpeg文件-----------------------------
	unsigned char h1 = pic_heigth / 256;
	unsigned char h2 = pic_heigth % 256;
	unsigned char l1 = pic_width / 256;
	unsigned char l2 = pic_width % 256;

	unsigned char zhen[] = {255, 192, 0, 17, 8, h1, h2, l1, l2, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1 }; //帧头
	unsigned char sos[] = {255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0 };   //比特流开始

	unsigned char jp[900000] = {};

	jp[0] = 0xff;                    //jpeg开始
	jp[1] = 0xd8;
	int k = 2;
	memcpy(&jp[k], lhb0, (2 + 0x43));   //量化表
	k = k + 2 + 0x43;
	memcpy(&jp[k], lhb1, (2 + 0x43));
	k = k + 2 + 0x43;
	memcpy(&jp[k], zhen, (2 + 0x11));   //帧全局
	k = k + 2 + 0x11;
	memcpy(&jp[k], hfm0, (2 + 0x1f));   //霍夫曼表
	k = k + 2 + 0x1f;
	memcpy(&jp[k], hfm1, (2 + 0xb5));
	k = k + 2 + 0xb5;
	memcpy(&jp[k], hfm2, (2 + 0x1f));
	k = k + 2 + 0x1f;
	memcpy(&jp[k], hfm3, (2 + 0xb5));
	k = k + 2 + 0xb5;
	memcpy(&jp[k], sos, (2 + 0xc));      //比特流开始
	k = k + 2 + 0xc;

	for (int a = 0; a < 8 - (to % 8); a++) {
		o_bit[to + a] = 1;        //比特流凑成8的倍数,不够补1
	}
	to = to + 8 - to % 8;

	for (int a = 0; a < to; a = a + 8) { //比特流转成字节
		unsigned char zz = o_bit[a] * 128 + o_bit[a + 1] * 64 + o_bit[a + 2] * 32 + o_bit[a + 3] * 16 + o_bit[a + 4] * 8 + o_bit[a + 5] * 4 + o_bit[a + 6] * 2 + o_bit[a + 7];
		if (zz == 0xff) {
			jp[k] = zz;
			jp[k = k + 1] = 0;      //0xff 后面跟0

		} else {
			jp[k] = zz;
		}
		k = k + 1;
	}

	jp[k = k + 1] = 0xff;          //jpeg 结束
	jp[k = k + 1] = 0xd9;

	chdir(file_out);
	FILE *fz = fopen("zz.jpeg", "w+b");
	fwrite(jp, k, 1, fz);
	fclose(fz);
	puts("over");
//---------------------------------------------------
	return 0;
}










生成的图片始终有错位,已排除了8×8分割等,感觉在Z排序之前都没问题。始终找不到原因。现在的办法是再完成jpeg的解码,对比数据看能发现问题不。下图是错位的显示,原始yuv图片是显示纯红色条,编码生成的jpeg是下图这样,换了几个看图软件都是一样的,问题出在生成的jpeg文件。看错误的图片已生成成了红条,但又多出了一个白条和一个黑条,不知道是哪个环节出错了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1368978.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

3. SPSS数据文件的基本加工和处理

如何获取SPSS自带的案例数据文件&#xff1f; 首先找到SPSS的安装目录&#xff0c;然后找到Samples文件夹 可以看到有不同语言版本&#xff0c;选择简体中文 就能看到很多.sav文件 数据文件的整理 个案排序 单值排序 例&#xff1a;对于下面的数据集&#xff0c;将工资按…

拼多多API:从数据中挖掘商业价值的力量

随着大数据时代的来临&#xff0c;数据已经成为企业决策和创新的基石。拼多多API作为电商领域的重要接口&#xff0c;为企业提供了从数据中挖掘商业价值的机会。通过拼多多API&#xff0c;企业可以获取丰富的用户数据、商品数据和交易数据&#xff0c;从而深入了解市场需求、优…

【leetcode】字符串中的第一个唯一字符

题目描述 给定一个字符串 s &#xff0c;找到 它的第一个不重复的字符&#xff0c;并返回它的索引 。如果不存在&#xff0c;则返回 -1 。 用例 示例 1&#xff1a; 输入: s “leetcode” 输出: 0 示例 2: 输入: s “loveleetcode” 输出: 2 示例 3: 输入: s “aabb”…

Flink CDC使用

Flink 环境准备 Flink 版本对应的CDC版本 两个jar包上传到flink bin目录下 flink-sql-connector-mysql-cdc mysql-connector-java 重启Flink集群

跟我学java|Stream流式编程——Stream 的中间操作

书接上回&#xff0c;继续研究。 过滤操作&#xff08;filter&#xff09; 过滤操作&#xff08;filter&#xff09;是 Stream API 中的一种常用操作方法&#xff0c;它接受一个 Predicate 函数作为参数&#xff0c;用于过滤 Stream 中的元素。只有满足 Predicate 条件的元素会…

搭建Windows版Redis集群

redis集群 Redis单机版安装 链接: Redis官网下载地址 下载完成后解压至指定目录 打开一个 cmd 窗口 使用 cd 命令切换目录到 E:\Redis\Redis 运行&#xff1a; redis-server.exe redis.windows.confRedis集群的安装 1.构建集群节点目录 创建一个redis-cluster目录用于存放…

计算机网络 - 路由器查表过程模拟 C++(2024)

1.题目描述 参考计算机网络教材 140 页 4.3 节内容&#xff0c;编程模拟路由器查找路由表的过程&#xff0c;用&#xff08;目的地址 掩码 下一跳&#xff09; 的 IP 路由表以及目的地址作为输入&#xff0c;为目的地址查找路由表&#xff0c;找出正确的下一跳并输出结果。 1.…

ATA-P系列功率放大器在压电叠堆中的作用是什么

功率放大器在压电叠堆中的作用是为压电叠堆提供足够的电能&#xff0c;使其产生强大的机械振动。以下为您详细介绍一下。 压电叠堆是一种利用压电效应产生振动的器件。通过在叠堆上施加电压&#xff0c;叠堆内部的压电材料会产生机械应变&#xff0c;从而引起叠堆的振动。这种振…

C++的一些书籍整理(个人学习)

UNIX环境高级编程&#xff08;第三版&#xff09; UNXI网络编程卷1 网络编程的笔记 收藏 我会了 一堆书 这个仓 数据库连接池原理介绍常用连接池介绍

7.3 CONSTANT MEMORY AND CACHING

掩模数组M在卷积中的使用方式有三个有趣的属性。首先&#xff0c;M阵列的大小通常很小。大多数卷积掩模在每个维度上都少于10个元素。即使在3D卷积的情况下&#xff0c;掩码通常也只包含少于1000个元素。其次&#xff0c;在内核执行过程中&#xff0c;M的内容不会改变。第三&am…

移动端对大批量图片加载的优化方法(三)

移动端对大批量图片加载的优化方法&#xff08;三&#xff09;Flutter 本篇主要从Flutter开发中可以使用到的对大批量图片加载的优化方法进行整理。 1.合适的图片格式 详情请参考移动端对大批量图片加载的优化方法&#xff08;一&#xff09;。 2.缓存机制 在Flutter中&am…

2023年山东省职业院校技能大赛高职组信息安全管理与评估—内存取证解析

内存取证 1、从内存中获取到用户admin的密码并且破解密码,以Flag{admin,password} 形式提交(密码为 6 位); volatility -f 1.vmem imageinfo 操作系统我们一般取第一个就可以了

vulhub中的Apache SSI 远程命令执行漏洞

Apache SSI 远程命令执行漏洞 1.cd到ssi-rce cd /opt/vulhub/httpd/ssi-rce/ 2.执行docker-compose up -d docker-compose up -d 3.查看靶场是否开启成功 dooker ps 拉取成功了 4.访问url 这里已经执行成功了&#xff0c;注意这里需要加入/upload.php 5.写入一句话木马 &…

Qt实现简单的分割窗口

最近在学习一些关于Qt的新知识&#xff0c;今天来讲述下我学习到的窗口分割&#xff0c;如果有不正确的&#xff0c;大家可以指正哦~ 首先&#xff0c;先看一下实现之后的简单效果吧&#xff01;省的说的天花乱坠&#xff0c;大家却不知道说的是哪个部分。 功能实现 整体demo…

阿里云RDMA通信库XRDMA论文详解

RDMA(remote direct memory access)即远端直接内存访问&#xff0c;是一种高性能网络通信技术&#xff0c;具有高带宽、低延迟、无CPU消耗等优点。RDMA相比TCP在性能方面有明显的优势&#xff0c;但在编程复杂度上RDMA verbs却比TCP socket复杂一个数量级。 开源社区和各大云厂…

如何在iOS手机上查看应用日志

引言 在开发iOS应用过程中&#xff0c;查看应用日志是非常重要的一项工作。通过查看日志&#xff0c;我们可以了解应用程序运行时的状态和错误信息&#xff0c;帮助我们进行调试和排查问题。本文将介绍两种方法来查看iOS手机上的应用日志&#xff0c;并提供相应的操作步骤。 …

Python学习之路——文件部分【书接上回】

一、书接上回 上个博客我说过&#xff0c;为什么最开始的时候一定要将文件内的中文的逗号替换为英文的逗号&#xff0c;接下来&#xff0c;请看&#xff08;其实想一想&#xff0c;感觉没必要&#xff0c;不过也是好的&#xff0c;总要练练手的嘛&#xff09; def func03(st…

Spring——基于注解的AOP配置

基于注解的AOP配置 1.创建工程 1.1.pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"…

基于DNA的密码学和隐写术综述

摘要 本文全面调研了不同的脱氧核糖核酸(DNA)-基于密码学和隐写术技术。基于DNA的密码学是一个新兴领域,利用DNA分子的大规模并行性和巨大的存储容量来编码和解码信息。近年来,由于其相对传统密码学方法的潜在优势,如高存储容量、低错误率和对环境因素的抗性,该领域引起…

怎么把图片表格转成word表格?教你简单转换

怎么把图片表格转成word表格&#xff1f;在我们的日常工作中&#xff0c;经常会遇到需要将图片表格转换成Word表格的情况。这样不仅可以方便我们编辑、整理数据&#xff0c;还能提高工作效率。那么&#xff0c;如何将图片表格快速、准确地转换成Word表格呢&#xff1f;下面就为…