场景:
安卓摄像头获取画面的处理。
内容来自:
https://code.google.com/p/libyuv
源代码下载:
http://files.cnblogs.com/files/wzswzd/libyuv-master.zip
相关的代码如下:
#define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
#define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
#define UB -128 /* max(-128, round(-2.018 * 64)) */
#define UG 25 /* round(0.391 * 64) */
#define VG 52 /* round(0.813 * 64) */
#define VR -102 /* round(-1.596 * 64) */
#define BB (UB * 128 + YGB)
#define BG (UG * 128 + VG * 128 + YGB)
#define BR (VR * 128 + YGB)
typedef unsigned char sd_uint8;
typedef unsigned long sd_uint32;
typedef signed long sd_int32;
sd_int32 clamp0(sd_int32 v) { return ((-(v) >> 31) & (v)); }
sd_int32 clamp255(sd_int32 v) { return (((255 - (v)) >> 31) | (v)) & 255; }
sd_uint32 Clamp(sd_int32 val) { int v = clamp0(val); return (sd_uint32)(clamp255(v)); }
void YuvPixel(
sd_uint8 y,
sd_uint8 u,
sd_uint8 v,
sd_uint8* b,
sd_uint8* g,
sd_uint8* r
){
sd_uint32 y1 = (sd_uint32)(y * 0x0101 * YG) >> 16;
*r = Clamp((sd_int32)(-(u * UB) + y1 + BB) >> 6);
*g = Clamp((sd_int32)(-(v * VG + u * UG) + y1 + BG) >> 6);
*b = Clamp((sd_int32)(-(v * VR) + y1 + BR) >> 6);
}
void NV21ToARGBRow_C(
const sd_uint8* src_y,
const sd_uint8* src_vu,
sd_uint8* rgb_buf,
int width
){
int x;
for ( x = 0; x < width - 1; x += 2 ) {
YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
rgb_buf[3] = 255;
YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6);
rgb_buf[7] = 255;
src_y += 2;
src_vu += 2;
rgb_buf += 8;
}
if ( width & 1 ) {
YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
rgb_buf[3] = 255;
}
}
int NV21ToARGB(
const sd_uint8* src_y,
int src_stride_y,
const sd_uint8* src_uv,
int src_stride_uv,
sd_uint8* dst_argb,
int dst_stride_argb,
int width,
int height
){
int y;
if ( !src_y || !src_uv || !dst_argb || width <= 0 || height == 0 ) { return -1; }
for ( y = 0; y < height; ++y ) {
NV21ToARGBRow_C(src_y, src_uv, dst_argb, width);
dst_argb += dst_stride_argb;
src_y += src_stride_y;
if ( y & 1 ) {
src_uv += src_stride_uv;
}
}
return 0;
}
int ConvertToARGB(
const sd_uint8* sample, // 原图的地址
sd_uint8* crop_argb, // RGBA的地址
int argb_stride, // should be width * 4 or width must be 1 right
int crop_x, // 裁剪的x坐标
int crop_y, // 裁剪的y坐标
int src_width, // 原图的宽度
int src_height, // 原图的高度
int crop_width, // 裁剪的宽度
int crop_height // 裁剪的高度
){
const sd_uint8* src;
const sd_uint8* src_uv;
int aligned_src_width = (src_width + 1) & ~1;
src = sample + (src_width * crop_y + crop_x);
src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x;
// Call NV12 but with u and v parameters swapped.
return NV21ToARGB(src, src_width, src_uv, aligned_src_width, crop_argb, argb_stride, crop_width, crop_height);
}
网友评论