YUV420转化为RGB565如何修改
网上找的一篇将YUV420SP的转化为RGB565的代码 ,请问大家怎么修改为YUV420->RGB565的呢?我修改的有错误?
static public void decodeYUV420P(byte[] rgbBuf, byte[] yuv420p,int width, int height) { final int frameSize = width * height; if (rgbBuf == null) throw new NullPointerException("buffer 'rgbBuf' is null"); if (rgbBuf.length < frameSize * 3) throw new IllegalArgumentException("buffer 'rgbBuf' size " + rgbBuf.length + " < minimum " + frameSize * 3); if (yuv420p == null) throw new NullPointerException("buffer 'yuv420p' is null"); if (yuv420p.length < frameSize * 3 / 2) throw new IllegalArgumentException("buffer 'yuv420p' size " + yuv420p.length + " < minimum " + frameSize * 3 / 2); int i = 0, y = 0; int uvp = 0, u = 0, v = 0; int y1192 = 0, r = 0, g = 0, b = 0; for (int j = 0, yp = 0; j < height; j++) { uvp = frameSize + (j >> 1) * width; u = 0; v = 0; for (i = 0; i < width; i++, yp++) { y = (0xff & ((int) yuv420p[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420p[uvp++]) - 128; u = (0xff & yuv420p[uvp++]) - 128; } y1192 = 1192 * y; r = (y1192 + 1634 * v); g = (y1192 - 833 * v - 400 * u); b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgbBuf[yp * 3] = (byte) (r >> 10); rgbBuf[yp * 3 + 1] = (byte) (g >> 10); rgbBuf[yp * 3 + 2] = (byte) (b >> 10); } } }
int *colortab=NULL;int *u_b_tab=NULL;int *u_g_tab=NULL;int *v_g_tab=NULL;int *v_r_tab=NULL;short *tmp_pic=NULL;unsigned int *rgb_2_pix=NULL;unsigned int *r_2_pix=NULL;unsigned int *g_2_pix=NULL;unsigned int *b_2_pix=NULL; void DeleteYUVTab(){ av_free(tmp_pic); av_free(colortab); av_free(rgb_2_pix);}void CreateYUVTab_16(){ int i; int u, v; tmp_pic = (short*)av_malloc(320*480*2); // 最大320x240,16bits colortab = (int *)av_malloc(4*256*sizeof(int)); u_b_tab = &colortab[0*256]; u_g_tab = &colortab[1*256]; v_g_tab = &colortab[2*256]; v_r_tab = &colortab[3*256]; for (i=0; i<256; i++) { u = v = (i-128); u_b_tab[i] = (int) ( 1.772 * u); u_g_tab[i] = (int) ( 0.34414 * u); v_g_tab[i] = (int) ( 0.71414 * v); v_r_tab[i] = (int) ( 1.402 * v); } rgb_2_pix = (unsigned int *)av_malloc(3*768*sizeof(unsigned int)); r_2_pix = &rgb_2_pix[0*768]; g_2_pix = &rgb_2_pix[1*768]; b_2_pix = &rgb_2_pix[2*768]; for(i=0; i<256; i++) { r_2_pix[i] = 0; g_2_pix[i] = 0; b_2_pix[i] = 0; } for(i=0; i<256; i++) { r_2_pix[i+256] = (i & 0xF8) << 8; g_2_pix[i+256] = (i & 0xFC) << 3; b_2_pix[i+256] = (i ) >> 3; } for(i=0; i<256; i++) { r_2_pix[i+512] = 0xF8 << 8; g_2_pix[i+512] = 0xFC << 3; b_2_pix[i+512] = 0x1F; } r_2_pix += 256; g_2_pix += 256; b_2_pix += 256;}void DisplayYUV_16(unsigned int *pdst1, unsigned char *y, unsigned char *u, unsigned char *v, int width, int height, int src_ystride, int src_uvstride, int dst_ystride){ int i, j; int r, g, b, rgb; int yy, ub, ug, vg, vr; unsigned char* yoff; unsigned char* uoff; unsigned char* voff; unsigned int* pdst=pdst1; int width2 = width/2; int height2 = height/2; if(width2>iWidth/2) { width2=iWidth/2; y+=(width-iWidth)/4*2; u+=(width-iWidth)/4; v+=(width-iWidth)/4; } if(height2>iHeight) height2=iHeight; for(j=0; j<height2; j++) // 一次2x2共四个像素 { yoff = y + j * 2 * src_ystride; uoff = u + j * src_uvstride; voff = v + j * src_uvstride; for(i=0; i<width2; i++) { yy = *(yoff+(i<<1)); ub = u_b_tab[*(uoff+i)]; ug = u_g_tab[*(uoff+i)]; vg = v_g_tab[*(voff+i)]; vr = v_r_tab[*(voff+i)]; b = yy + ub; g = yy - ug - vg; r = yy + vr; rgb = r_2_pix[r] + g_2_pix[g] + b_2_pix[b]; yy = *(yoff+(i<<1)+1); b = yy + ub; g = yy - ug - vg; r = yy + vr; pdst[(j*dst_ystride+i)] = (rgb)+((r_2_pix[r] + g_2_pix[g] + b_2_pix[b])<<16); yy = *(yoff+(i<<1)+src_ystride); b = yy + ub; g = yy - ug - vg; r = yy + vr; rgb = r_2_pix[r] + g_2_pix[g] + b_2_pix[b]; yy = *(yoff+(i<<1)+src_ystride+1); b = yy + ub; g = yy - ug - vg; r = yy + vr; pdst [((2*j+1)*dst_ystride+i*2)>>1] = (rgb)+((r_2_pix[r] + g_2_pix[g] + b_2_pix[b])<<16); } }}