YUV转rgb888
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static long int crv_tab[256];
static long int cbu_tab[256];
static long int cgu_tab[256];
static long int cgv_tab[256];
static long int tab_76309[256];
static unsigned char clp[1024];
void init_yuv420p_table()
{
long int crv, cbu, cgu, cgv;
int i, ind;
static int init = 0;
if (init == 1)
return;
crv = 104597;
cbu = 132201;
cgu = 25675;
cgv = 53279;
for (i = 0; i < 256; i++)
{
crv_tab[i] = (i - 128) * crv;
cbu_tab[i] = (i - 128) * cbu;
cgu_tab[i] = (i - 128) * cgu;
cgv_tab[i] = (i - 128) * cgv;
tab_76309[i] = 76309 * (i - 16);
}
for (i = 0; i < 384; i++)
clp[i] = 0;
ind = 384;
for (i = 0; i < 256; i++)
clp[ind++] = i;
ind = 640;
for (i = 0; i < 384; i++)
clp[ind++] = 255;
init = 1;
}
void planar420_to_rgb24(uint8_t *y_buffer, uint8_t *u_buffer, uint8_t *v_buffer,uint8_t *rgbbuffer, int width, int height)
{
#if 1
int y1, y2, u, v;
unsigned char *py1, *py2;
int i, j, c1, c2, c3, c4;
unsigned char *d1, *d2;
py1 = y_buffer;
py2 = py1 + width;
d1 = rgbbuffer;
d2 = d1 + 3 * width;
init_yuv420p_table();
for (j = 0; j < height; j += 2)
{
for (i = 0; i < width; i += 2)
{
u = *u_buffer++;
v = *v_buffer++;
c1 = crv_tab[v];
c2 = cgu_tab[u];
c3 = cgv_tab[v];
c4 = cbu_tab[u];
y1 = tab_76309[*py1++];
*d1++ = clp[384 + ((y1 + c1) >> 16)];
*d1++ = clp[384 + ((y1 - c2 - c3) >> 16)];
*d1++ = clp[384 + ((y1 + c4) >> 16)];
y2 = tab_76309[*py2++];
*d2++ = clp[384 + ((y2 + c1) >> 16)];
*d2++ = clp[384 + ((y2 - c2 - c3) >> 16)];
*d2++ = clp[384 + ((y2 + c4) >> 16)];
y1 = tab_76309[*py1++];
*d1++ = clp[384 + ((y1 + c1) >> 16)];
*d1++ = clp[384 + ((y1 - c2 - c3) >> 16)];
*d1++ = clp[384 + ((y1 + c4) >> 16)];
y2 = tab_76309[*py2++];
*d2++ = clp[384 + ((y2 + c1) >> 16)];
*d2++ = clp[384 + ((y2 - c2 - c3) >> 16)];
*d2++ = clp[384 + ((y2 + c4) >> 16)];
}
d1 += 3 * width;
d2 += 3 * width;
py1 += width;
py2 += width;
}
#endif
}
#define OUT_WIDTH (1920)
#define OUT_HEIGHT (1080)
int main()
{
uint8_t *rgbbuffer = (uint8_t*)malloc(OUT_WIDTH*OUT_HEIGHT*3);
uint8_t *y_buffer = (uint8_t*)malloc(OUT_WIDTH*OUT_HEIGHT);
uint8_t *u_buffer = (uint8_t*)malloc(OUT_WIDTH*OUT_HEIGHT/4);
uint8_t *v_buffer = (uint8_t*)malloc(OUT_WIDTH*OUT_HEIGHT/4);
int len = 0;
FILE *y_fp = fopen( "./y_buffer.yuv", "r+" );
if(y_fp == NULL){
perror("fopen:");
return 0;
}
len = fread( y_buffer, 1, OUT_WIDTH*OUT_HEIGHT, y_fp );
printf("%s@%d ====> len %d\n", __func__, __LINE__, len);
fclose(y_fp);
FILE *u_fp = fopen( "./u_buffer.yuv", "r+" );
if(u_fp == NULL){
perror("fopen:");
return 0;
}
len = fread( u_buffer, 1, OUT_WIDTH*OUT_HEIGHT/4, u_fp );
printf("%s@%d ====> len %d\n", __func__, __LINE__, len);
fclose(u_fp);
FILE *v_fp = fopen( "./v_buffer.yuv", "r" );
if(v_fp == NULL){
perror("fopen:");
return 0;
}
len = fread( v_buffer, 1, OUT_WIDTH*OUT_HEIGHT/4, v_fp );
printf("%s@%d ====> len %d\n", __func__, __LINE__, len);
fclose(v_fp);
int y_fd = open( "./ybuffer.yuv", O_RDONLY );
if(y_fd < 0){
perror("fopen:");
return 0;
}
read(y_fd, y_buffer, OUT_WIDTH*OUT_HEIGHT);
close(y_fd);
int u_fd = open( "./ubuffer.yuv", O_RDONLY );
if(u_fd < 0){
perror("fopen:");
return 0;
}
read(u_fd, u_buffer, OUT_WIDTH*OUT_HEIGHT/4);
close(u_fd);
int v_fd = open( "./vbuffer.yuv", O_RDONLY );
if(v_fd < 0){
perror("fopen:");
return 0;
}
read(v_fd, v_buffer, OUT_WIDTH*OUT_HEIGHT/4);
close(v_fd);
planar420_to_rgb24( y_buffer, u_buffer, v_buffer,rgbbuffer, OUT_WIDTH, OUT_HEIGHT );
FILE *rgb_fp = fopen( "./rgb.yuv", "wb+" );
if(rgb_fp == NULL){
perror("fopen:");
return 0;
}
fwrite( rgbbuffer, 1, OUT_WIDTH*OUT_HEIGHT*3, rgb_fp );
fclose(rgb_fp);
free(rgbbuffer);
free(y_buffer);
free(u_buffer);
free(v_buffer);
return 0;
}
NV12转RGB888
#include color_convert.h
#include <imp/imp_common.h>
static long int crv_tab[256];
static long int cbu_tab[256];
static long int cgu_tab[256];
static long int cgv_tab[256];
static long int tab_76309[256];
static unsigned char clp[1024];
void init_yuv420p_table()
{
long int crv, cbu, cgu, cgv;
int i, ind;
static int init = 0;
if (init == 1)
return;
crv = 104597;
cbu = 132201;
cgu = 25675;
cgv = 53279;
for (i = 0; i < 256; i++)
{
crv_tab[i] = (i - 128) * crv;
cbu_tab[i] = (i - 128) * cbu;
cgu_tab[i] = (i - 128) * cgu;
cgv_tab[i] = (i - 128) * cgv;
tab_76309[i] = 76309 * (i - 16);
}
for (i = 0; i < 384; i++)
clp[i] = 0;
ind = 384;
for (i = 0; i < 256; i++)
clp[ind++] = i;
ind = 640;
for (i = 0; i < 384; i++)
clp[ind++] = 255;
init = 1;
}
#define AV_PIX_FMT_NV12 PIX_FMT_NV12
#define AV_PIX_FMT_NV21 PIX_FMT_NV21
void NV12_or_NV21_to_rgb24(int frame_type, unsigned char *yuvbuffer, unsigned char *rgbbuffer, int width, int height)
{
int y1, y2, u, v;
unsigned char *py1, *py2;
int i, j, c1, c2, c3, c4;
unsigned char *d1, *d2;
unsigned char *src_u;
static int init_yuv420p = 0;
src_u = yuvbuffer + width * height;
py1 = yuvbuffer;
py2 = py1 + width;
d1 = rgbbuffer;
d2 = d1 + 3 * width;
for (j = 0; j < height; j += 2)
{
for (i = 0; i < width; i += 2)
{
if (frame_type == AV_PIX_FMT_NV12)
{
u = *src_u++;
v = *src_u++;
}
if (frame_type == AV_PIX_FMT_NV21)
{
v = *src_u++;
u = *src_u++;
}
c1 = crv_tab[v];
c2 = cgu_tab[u];
c3 = cgv_tab[v];
c4 = cbu_tab[u];
y1 = tab_76309[*py1++];
*d1++ = clp[384 + ((y1 + c1) >> 16)];
*d1++ = clp[384 + ((y1 - c2 - c3) >> 16)];
*d1++ = clp[384 + ((y1 + c4) >> 16)];
y2 = tab_76309[*py2++];
*d2++ = clp[384 + ((y2 + c1) >> 16)];
*d2++ = clp[384 + ((y2 - c2 - c3) >> 16)];
*d2++ = clp[384 + ((y2 + c4) >> 16)];
y1 = tab_76309[*py1++];
*d1++ = clp[384 + ((y1 + c1) >> 16)];
*d1++ = clp[384 + ((y1 - c2 - c3) >> 16)];
*d1++ = clp[384 + ((y1 + c4) >> 16)];
y2 = tab_76309[*py2++];
*d2++ = clp[384 + ((y2 + c1) >> 16)];
*d2++ = clp[384 + ((y2 - c2 - c3) >> 16)];
*d2++ = clp[384 + ((y2 + c4) >> 16)];
}
d1 += 3 * width;
d2 += 3 * width;
py1 += width;
py2 += width;
}
}