利用libpng儲存YUV NV21格式的圖片及優化
阿新 • • 發佈:2018-12-22
將YUV格式轉換為RGB格式後,利用libpng儲存RGB為png即可,關於YUV格式的詳細情況和與RGB的轉換關係在網上找找就可以了。目前的做法耗時比較多。
704*576 480ms
bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
FILE *fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
std::string path;
GenFileName(path, file_path_, mLocal_) ;
fp = fopen(path.c_str(), "wb");
if (fp){
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if(png_ptr == NULL || info_ptr == NULL){
fclose(fp);
return false;
}
mFileName_ = path;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
uint8_t * data = new uint8_t [32 * width];
int nv_start = width * height ;
uint32_t rgb_index = 0;
int r, g, b, nv_index,y, ru, rv;
for(uint32_t i = 0; i < height ; ++i){
for(uint32_t j = 0; j < width; ++j){
nv_index = i / 2 * width + j - j % 2;
y = yuv_data[rgb_index];
ru = yuv_data[nv_start + nv_index] - 128;
rv = yuv_data[nv_start + nv_index + 1] - 128;
r = y + 1.4 * rv; //r
g = y - 0.34 * ru - 0.71 * rv; //g
b = y + 1.77 * ru; //b
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0) r = 0;
if(g < 0) g = 0;
if(b < 0) b = 0;
data[j * 4 + 0] = b;
data[j * 4 + 1] = g;
data[j * 4 + 2] = r;
data[j * 4 + 3] = 255;
++rgb_index;
}
png_write_row(png_ptr,data);
}
delete data;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
return false;
}
改成整形計算:370ms左右
bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
FILE *fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
std::string path;
GenFileName(path, file_path_, mLocal_);
fp = fopen(path.c_str(), "wb");
if (fp){
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if(png_ptr == NULL || info_ptr == NULL){
fclose(fp);
return false;
}
mFileName_ = path;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
uint8_t * data = new uint8_t [width << 2];
memset(data,255,width << 2);
int nv_start = width * height ;
uint32_t rgb_index = 0,pos;
int r, g, b, nv_index,y, ru, rv;
for(uint32_t i = 0; i < height ; ++i){
for(uint32_t j = 0; j < width; ++j){
nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
y = yuv_data[rgb_index];
ru = yuv_data[nv_index] - 128;
rv = yuv_data[nv_index + 1] - 128;
r = y + 140 * rv / 100; //r
g = y - 34 * ru / 100 - 71 * rv / 100; //g
b = y + 177 * ru / 100; //b
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0 ) r = 0;
if(g < 0 ) g = 0;
if(b < 0 ) b = 0;
pos = j << 2;
data[pos ] = b;
data[pos + 1] = g;
data[pos + 2] = r;
++rgb_index;
}
png_write_row(png_ptr,data);
}
delete data;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
return false;
}
去掉除法,改成移位,沒多大改善:
bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
FILE *fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
std::string path;
GenFileName(path, file_path_, mLocal_);
fp = fopen(path.c_str(), "wb");
if (fp){
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if(png_ptr == NULL || info_ptr == NULL){
fclose(fp);
return false;
}
mFileName_ = path;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
uint8_t * data = new uint8_t [width << 2];
memset(data,255,width << 2);
int nv_start = width * height ;
uint32_t rgb_index = 0,pos;
int r, g, b, nv_index,y, ru, rv;
for(uint32_t i = 0; i < height ; ++i){
for(uint32_t j = 0; j < width; ++j){
nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
y = yuv_data[rgb_index];
ru = yuv_data[nv_index] - 128;
rv = yuv_data[nv_index + 1] - 128;
r = y + ((103 * rv) >> 8); //r
g = y - ((88 * ru) >> 8) - ((183 * rv) >> 8); //g
b = y + ((198 * ru) >> 8); //b
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0 ) r = 0;
if(g < 0 ) g = 0;
if(b < 0 ) b = 0;
pos = j << 2;
data[pos ] = b;
data[pos + 1] = g;
data[pos + 2] = r;
++rgb_index;
}
png_write_row(png_ptr,data);
}
delete data;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
return false;
}
部分查表,多消耗1kb記憶體,沒啥改善:
bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
const static int Table_fv1[256]={ -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114,