利用c 原生头文件完成JPEG全流程编码

骄傲一下,经过一个多月的努力,终于完成jpeg的全套编码。经验证此程序可以把摄像头yuv信号转为JPG图片。现在的程序还不完美,只能对长和宽尺寸是16倍数的信号转码。而且转码速度太慢,一帧1280×720的图片要2秒多。此程序只能对yuv420p编码。

此程序有很多重复的代码,主要是为了清晰好查错,如三分量的提取,处理等阶段可以用一个函数形式来完成,减少代码量。

说实话此程序没有实用价值,因为压缩时间太长,不可能用于摄像头的信号转换,查网络说是浮点运算太多,要用数学的方法把浮点运算转为乘法和加法的运算,所以不想再折腾了,转而去学ffmpeg。想借用它的库函数完成摄像头的信号压缩转码。目标是搞一个高速摄像头。

写了这么多博文主要是为了留存,以后想再搞又可以用上。还有把学习的思路也备份一下。

9cfc435483224fc08c19dfa9a05ca322.jpeg

下面是全套程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>   //如果直接读文件到数组,也可不用此头文件
#include <math.h>
#define PI 3.1415926#define  pic_width   656
#define  pic_heigth  480#define filename  "/home/wjs/Pictures/sample.yuv"
#define file_out  "/home/wjs/Pictures/wz.jpg"           //输出文件目录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  //           1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};unsigned char lhb1[0x45] = {0xff, 0xdb, 0, 0x43, 1,17, 18, 24, 47, 99, 99, 99, 99,     //17,18,24,4718, 21, 26, 66, 99, 99, 99, 99,     //18,21,26,66,24, 26, 56, 99, 99, 99, 99, 99,      //24,26,5647, 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//      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};//-------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]=intint 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]=intif (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]=intif (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.1856int s1 = round(s * 100); //-3019s = s1 / 100.0;    //-30.19o[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;         //计算连续的0o[0][1] = 0;o[0][0] = i[0];t = 1;for (int a = 1; a < 64; a++) {                               //  aif ((i[a] == 0) && (i[a + 1] == 0) && ((a + 1) < 63)) { //000001z++;}                                                        // aif ((i[a] == 0) && (i[a + 1] == 0) && ((a + 1) == 63)) { //0000结束z++;             //本次的0o[t][0] = 0;o[t][1] = z + 1; //加a+1的0break;           //判断完成}                                               //  aif ((i[a] == 0) && (i[a + 1] != 0)) {           //000100z++;                  //加上本次的一个0o[t][0] = i[a + 1];o[t][1] = z;z = 0;           //清0,计算下次的连续0t++;a = a + 1;}if ((i[a] != 0) && (a == 0)) { //第一个数非0o[t][0] = i[a];o[t][1] = 0;t++;}if ((a > 0) && (i[a] != 0) && (i[a - 1] != 0)) { //防止第3种重复读取,这种是读取连续的非0o[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); //必须要读,写//---------------------------------------------------------------------------------unsigned char yfl[pic_heigth * pic_width] = {};unsigned char ufl[pic_heigth * pic_width / 4 ] = {};unsigned char  vfl[pic_heigth * pic_width / 4 ] = {};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-63for (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-63for (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-15for (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]=intint q = y_zl[0][0] - ysc; //DC 差值ali(q, ydc);           //处理Y_DCint ydc_hfm[2] = {};   //ydc_hfm[1]=lenhfm_ydc(ydc[1], ydc_hfm);int ls = (ydc_hfm[0] << ydc[1]) | ydc[0]; //组合成一intfor (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]=intint q = u_zl[0][0] - usc; //DC 差值ali(q, udc);           //处理Y_DCint udc_hfm[2] = {};   //ydc_hfm[1]=lenhfm_uvdc(udc[1], udc_hfm);int ls = (udc_hfm[0] << udc[1]) | udc[0]; //组合成一intfor (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]=intint q = v_zl[0][0] - vsc; //DC 差值ali(q, vdc);           //处理Y_DCint vdc_hfm[2] = {};   //ydc_hfm[1]=lenhfm_uvdc(vdc[1], vdc_hfm);int ls = (vdc_hfm[0] << vdc[1]) | vdc[0]; //组合成一intfor (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;//---------Y 排序----------------------------------   //第一个MCU y01 y02 y10 y11 u01 v01int yw[pic_heigth*pic_width/64]={};int wn=0;for(int y=0;y<pic_heigth/8;y=y+2){for(int x=0;x<pic_width/8;x=x+2){for(int b=0;b<2;b++){for(int a=0;a<2;a++){yw[wn]=(y+b)*pic_width/8+x+a;wn++;}}}}//---------MCU---------------------------------for (int a = 0; a < pic_width * pic_heigth / 256; a++) {    //MCU=4Y+1U+1Vfor (int b = 0; b < 4; b++) {unsigned char y1[8][8] = {};memcpy(y1, &(y64[yw[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;FILE *fz = fopen(file_out, "w+b");fwrite(jp, k, 1, fz);fclose(fz);puts("编码over");return 0;
}

 

 

 

 

 

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java生成四位数随机验证码

引言&#xff1a; 我们生活中登录的时候都要输入验证码&#xff0c;这些验证码是为了增加注册或者登录难度&#xff0c;减少被人用脚本疯狂登录注册导致的一系列危害&#xff0c;减少数据库的一些压力。 毕竟那些用脚本生成的账号都是垃圾账号 本次实践&#xff1a;生成这样的…

Docker Consul详解与部署示例

目录 Consul构成 Docker Consul 概述 Raft算法 服务注册与发现 健康检查 Key/Value存储 多数据中心 部署模式 consul-template守护进程 registrator容器 consul服务部署&#xff08;192.168.41.31&#xff09; 环境准备 搭建Consul服务 查看集群信息 registrato…

开源协议概览

身为程序员&#xff0c;我们不可避免的要和开源项目打交道&#xff0c;不管是我们自己做了些开源项目&#xff0c;还是使用开源项目&#xff0c;对各种开源协议的了解是必要的。 OSI(Open Source Initiative) OSI&#xff0c;开发源代码组织&#xff0c;是一个旨在推动开源软件…

【河海大学论文LaTeX+VSCode全指南】

河海大学论文LaTeXVSCode全指南 前言一、 LaTeX \LaTeX{} LATE​X的安装二、VScode的安装三、VScode的配置四、验证五、优化 前言 LaTeX \LaTeX{} LATE​X在论文写作方面具有传统Word无法比拟的优点&#xff0c;VScode作为一个轻量化的全功能文本编辑器&#xff0c;由于其极强的…

ZYNQ 7020 PL feature 解读

1. 组成 CLB, RAM, DSP, IO block,XADC, PCI-E, etc 2. CLK Each device in the Zynq-7000 family has up to 8 clock management tiles (CMTs), each consisting of one mixed-mode clock manager (MMCM) and one phase-locked loop (PLL). See Table 5. 2.1, Clock Distri…

记录下载安装rabbitmq(Linux) 并整合springboot--详细版(全)

下载rabbitmq&#xff08;Linux&#xff09;&#xff1a; erlang压缩包&#xff1a; https://share.weiyun.com/TGhfV8eZ rabbitMq-server压缩包&#xff1a; https://share.weiyun.com/ZXbUwWHD &#xff08;因为RabbitMQ采用 Erlang 实现的工业级的消息队列(MQ)服务器&#…

网络安全技术新手入门:利用永恒之蓝获取靶机控制权限

目录 前言 一、搜索永恒之蓝可用模块 二、使用攻击模块 三、配置攻击模块 四、攻击 五、总结 前言 相关法律声明&#xff1a;《中华人民共和国网络安全法》第二十七条 任何个人和组织不得从事非法侵入他人网络、干扰他人网络正常功能、窃取网络数据等危害网络安全的活动&…

element-ui表单验证同时用change与blur一起验证

项目场景&#xff1a; 提示&#xff1a;这里简述项目相关背景&#xff1a; 当审批时不通过审批意见要必须输入&#xff0c; 1&#xff1a;如果用change验证的话删除所有内容时报错是massage的提示&#xff0c;但是在失去焦点的时候报错就成了英文&#xff0c;如下图&#xf…

Qt 国产嵌入式操作系统实现文字转语音功能(TTS)

1.简介 本示例使用的CPU&#xff1a;rk3588。 操作系统&#xff1a;kylin V10 架构&#xff1a;aarch64 在Windows端&#xff0c;我们很容易想到使用Qt自带的类QTextToSpeech来实现文字转语音功能&#xff0c;Qt版本得在5.11.0以上才支持。但是在嵌入式平台&#xff0c;尤其…

Matlab数学建模算法之模拟退火算法(SA)详解

&#x1f517; 运行环境&#xff1a;Matlab &#x1f6a9; 撰写作者&#xff1a;左手の明天 &#x1f947; 精选专栏&#xff1a;《python》 &#x1f525; 推荐专栏&#xff1a;《算法研究》 &#x1f510;#### 防伪水印——左手の明天 ####&#x1f510; &#x1f497; 大家…

STM32之OLED显示

一、模块介绍 1、常见的显示设备 LED、数码管、点阵、LCD屏(1602/12864)、OLED屏(消费电子) 2、OLED屏的概述 OLED&#xff0c;即有机发光二极管&#xff08;Organic Light-Emitting Diode&#xff09;&#xff0c;又称为有机电激光显示&#xff08;Organic Electroluminesenc…

机器学习算法 - 马尔可夫链

马尔可夫链&#xff08;Markov Chain&#xff09;可以说是机器学习和人工智能的基石&#xff0c;在强化学习、自然语言处理、金融领域、天气预测、语音识别方面都有着极其广泛的应用 > The future is independent of the past given the present 未来独立于过去&#xff…

Linux实操学习

Linux常用操作 一、帮助命令1. man1.1 基本语法1.2 快捷键1.3 注意事项 2. help2.1 基本语法2.2 注意事项 3. 常用快捷键 二、文件目录类1. 常规操作1.1 pwd1.2 cd1.3 ls 2. 文件夹操作2.1 mkdir2.2 rmdir 3. 文件操作3.1 touch3.2 cp3.3 rm3.4 mv 4. 文件查看4.1 cat4.2 more4…

四、任意文件读取漏洞

一、介绍 解释&#xff1a;任意文件读取漏洞就其本身来说就是&#xff0c;攻击者绕过网站防御者设置的防御&#xff0c;读取到了正常使用者不应该读取到的内容。网站开发者使用不同的语言&#xff0c;任意文件读取漏洞利用方式就不同。 二、不同开发语言的不同漏洞点 1.PHP …

Kali Linux保姆级教程|零基础从入门到精通,看完这一篇就够了!(附工具包)

作为一名从事网络安全的技术人员&#xff0c;不懂Kali Linux的话&#xff0c;连脚本小子都算不上。 Kali Linux预装了数百种享誉盛名的渗透工具&#xff0c;使你可以更轻松地测试、破解以及进行与数字取证相关的任何其他工作。 今天给大家分享一套Kali Linux资料合集&#xf…

2024年高校建设大数据实验室建设的意义

数据挖掘与大数据分析是以计算机基础为基础&#xff0c;以挖掘算法为核心&#xff0c;紧密面向行业应用的一门综合性学科。其主要技术涉及概率论与数理统计、数据挖掘、算法与数据结构、计算机网络、并行计算等多个专业方向&#xff0c;因此该学科对于实验室具有较高的专业要求…

构建未来教育:在线培训系统开发的技术探讨

随着远程学习的崛起和数字化教育的普及&#xff0c;在线培训系统的开发成为了现代教育的核心。本文将深入讨论在线培训系统的关键技术要点&#xff0c;涵盖前后端开发、数据库管理、以及安全性和身份验证等关键方面。 前端开发&#xff1a;提供交互性与用户友好体验 在构建在…

HTML--JavaScript--引入方式

啊哈~~~基础三剑看到第三剑&#xff0c;JavaScript HTML用于控制网页结构 CSS用于控制网页的外观 JavaScript用于控制网页的行为 JavaScript引入方式 引入的三种方式&#xff1a; 外部JavaScript 内部JavaScript 元素事件JavaScript 引入外部JavaScript 一般情况下网页最好…

【数据结构】常见八大排序算法总结

目录 前言 1.直接插入排序 2.希尔排序 3.选择排序 4.堆排序 5.冒泡排序 6.快速排序 6.1Hoare版本 6.2挖坑法 6.3前后指针法 6.4快速排序的递归实现 6.5快速排序的非递归实现 7.归并排序 8.计数排序&#xff08;非比较排序&#xff09; 9.补充:基数排序 10.总结…

【Java】十年老司机转开发语言,新小白从学习路线图开始

欢迎来到《小5讲堂》 大家好&#xff0c;我是全栈小5。 这是《Java》序列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对知识点的理解和掌握…