#include "loadsurface_yuv.h"
static int scale_2dimage(unsigned char *src_img, int src_imgw, int src_imgh,
- unsigned char *dst_img, int dst_imgw, int dst_imgh)
+ unsigned char *dst_img, int dst_imgw, int dst_imgh)
{
int row=0, col=0;
static int YUV_blend_with_pic(int width, int height,
- unsigned char *Y_start, int Y_pitch,
- unsigned char *U_start, int U_pitch,
- unsigned char *V_start, int V_pitch,
- int UV_interleave, int fixed_alpha)
+ unsigned char *Y_start, int Y_pitch,
+ unsigned char *U_start, int U_pitch,
+ unsigned char *V_start, int V_pitch,
+ unsigned int fourcc, int fixed_alpha)
{
/* PIC YUV format */
unsigned char *pic_y_old = yuvga_pic;
if (width != 640 || height != 480) { /* need to scale the pic */
pic_y = (unsigned char *)malloc(width * height);
+ if(pic_y == NULL) {
+ printf("Failed to allocate memory for pic_y\n");
+ return -1;
+ }
+
pic_u = (unsigned char *)malloc(width * height/4);
- pic_v = (unsigned char *)malloc(width * height/4);
+ if(pic_u == NULL) {
+ printf("Failed to allocate memory for pic_u\n");
+ free(pic_y);
+ return -1;
+ }
+ pic_v = (unsigned char *)malloc(width * height/4);
+ if(pic_v == NULL) {
+ printf("Failed to allocate memory for pic_v\n");
+ free(pic_y);
+ free(pic_u);
+ return -1;
+ }
allocated = 1;
+
+ memset(pic_y, 0, width * height);
+ memset(pic_u, 0, width * height /4);
+ memset(pic_v, 0, width * height /4);
scale_2dimage(pic_y_old, 640, 480,
pic_y, width, height);
/* begin blend */
/* Y plane */
- for (row=0; row<height; row++)
- for (col=0; col<width; col++) {
- unsigned char *p = Y_start + row * Y_pitch + col;
- unsigned char *q = pic_y + row * width + col;
-
+ int Y_pixel_stride = 1;
+ if (fourcc == VA_FOURCC_YUY2)
+ Y_pixel_stride = 2;
+
+ for (row=0; row<height; row++) {
+ unsigned char *p = Y_start + row * Y_pitch;
+ unsigned char *q = pic_y + row * width;
+ for (col=0; col<width; col++, q++) {
*p = *p * (100 - alpha) / 100 + *q * alpha/100;
+ p += Y_pixel_stride;
}
+ }
- if (UV_interleave == 0) {
- for (row=0; row<height/2; row++)
- for (col=0; col<width/2; col++) {
- unsigned char *p = U_start + row * U_pitch + col;
- unsigned char *q = pic_u + row * width/2 + col;
-
- *p = *p * (100 - alpha) / 100 + *q * alpha/100;
- }
-
- for (row=0; row<height/2; row++)
- for (col=0; col<width/2; col++) {
- unsigned char *p = V_start + row * V_pitch + col;
- unsigned char *q = pic_v + row * width/2 + col;
-
- *p = *p * (100 - alpha) / 100 + *q * alpha/100;
- }
- } else { /* NV12 */
- for (row=0; row<height/2; row++)
- for (col=0; col<width/2; col++) {
- unsigned char *pU = U_start + row * U_pitch + col*2;
- unsigned char *qU = pic_u + row * width/2 + col;
- unsigned char *pV = pU + 1;
- unsigned char *qV = pic_v + row * width/2 + col;
+ /* U/V plane */
+ int U_pixel_stride = 0, V_pixel_stride = 0;
+ int v_factor_to_nv12 = 1;
+ switch (fourcc) {
+ case VA_FOURCC_YV12:
+ U_pixel_stride = V_pixel_stride = 1;
+ break;
+ case VA_FOURCC_NV12:
+ U_pixel_stride = V_pixel_stride = 2;
+ break;
+ case VA_FOURCC_YUY2:
+ U_pixel_stride = V_pixel_stride = 4;
+ v_factor_to_nv12 = 2;
+ break;
+ default:
+ break;
+ }
+ for (row=0; row<height/2*v_factor_to_nv12; row++) {
+ unsigned char *pU = U_start + row * U_pitch;
+ unsigned char *pV = V_start + row * V_pitch;
+ unsigned char *qU = pic_u + row/v_factor_to_nv12 * width/2;
+ unsigned char *qV = pic_v + row/v_factor_to_nv12 * width/2;
- *pU = *pU * (100 - alpha) / 100 + *qU * alpha/100;
- *pV = *pV * (100 - alpha) / 100 + *qV * alpha/100;
- }
+ for (col=0; col<width/2; col++, qU++, qV++) {
+ *pU = *pU * (100 - alpha) / 100 + *qU * alpha/100;
+ *pV = *pV * (100 - alpha) / 100 + *qV * alpha/100;
+
+ pU += U_pixel_stride;
+ pV += V_pixel_stride;
+ }
}
-
if (allocated) {
free(pic_y);
free(pic_u);
unsigned char *Y_start, int Y_pitch,
unsigned char *U_start, int U_pitch,
unsigned char *V_start, int V_pitch,
- int UV_interleave, int box_width, int row_shift,
+ unsigned int fourcc, int box_width, int row_shift,
int field)
{
int row, alpha;
+ unsigned char uv_value = 0x80;
/* copy Y plane */
+ int y_factor = 1;
+ if (fourcc == VA_FOURCC_YUY2) y_factor = 2;
for (row=0;row<height;row++) {
unsigned char *Y_row = Y_start + row * Y_pitch;
int jj, xpos, ypos;
for (jj=0; jj<width; jj++) {
xpos = ((row_shift + jj) / box_width) & 0x1;
-
- if ((xpos == 0) && (ypos == 0))
- Y_row[jj] = 0xeb;
- if ((xpos == 1) && (ypos == 1))
- Y_row[jj] = 0xeb;
-
- if ((xpos == 1) && (ypos == 0))
- Y_row[jj] = 0x10;
- if ((xpos == 0) && (ypos == 1))
- Y_row[jj] = 0x10;
+ if (xpos == ypos)
+ Y_row[jj*y_factor] = 0xeb;
+ else
+ Y_row[jj*y_factor] = 0x10;
+
+ if (fourcc == VA_FOURCC_YUY2) {
+ Y_row[jj*y_factor+1] = uv_value; // it is for UV
+ }
}
}
/* copy UV data */
for( row =0; row < height/2; row++) {
- unsigned short value = 0x80;
/* fill garbage data into the other field */
if (((field == VA_TOP_FIELD) && (row &1))
|| ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
- value = 0xff;
+ uv_value = 0xff;
}
- if (UV_interleave) {
- unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);
-
- memset(UV_row, value, width);
- } else {
- unsigned char *U_row = U_start + row * U_pitch;
- unsigned char *V_row = V_start + row * V_pitch;
-
- memset (U_row,value,width/2);
- memset (V_row,value,width/2);
+ unsigned char *U_row = U_start + row * U_pitch;
+ unsigned char *V_row = V_start + row * V_pitch;
+ switch (fourcc) {
+ case VA_FOURCC_NV12:
+ memset(U_row, uv_value, width);
+ break;
+ case VA_FOURCC_YV12:
+ memset (U_row,uv_value,width/2);
+ memset (V_row,uv_value,width/2);
+ break;
+ case VA_FOURCC_YUY2:
+ // see above. it is set with Y update.
+ break;
+ default:
+ printf("unsupported fourcc in loadsurface.h\n");
+ assert(0);
}
}
Y_start, Y_pitch,
U_start, U_pitch,
V_start, V_pitch,
- UV_interleave, alpha);
+ fourcc, alpha);
return 0;
}
int field)
{
VAImage surface_image;
- void *surface_p=NULL, *U_start,*V_start;
+ void *surface_p=NULL, *U_start = NULL,*V_start = NULL;
VAStatus va_status;
- unsigned int pitches[3];
+ unsigned int pitches[3]={0,0,0};
va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
CHECK_VASTATUS(va_status,"vaDeriveImage");
switch (surface_image.format.fourcc) {
case VA_FOURCC_NV12:
U_start = (char *)surface_p + surface_image.offsets[1];
- V_start = U_start + 1;
+ V_start = (char *)U_start + 1;
pitches[1] = surface_image.pitches[1];
pitches[2] = surface_image.pitches[1];
break;
pitches[1] = surface_image.pitches[2];
pitches[2] = surface_image.pitches[1];
break;
+ case VA_FOURCC_YUY2:
+ U_start = (char *)surface_p + 1;
+ V_start = (char *)surface_p + 3;
+ pitches[1] = surface_image.pitches[0];
+ pitches[2] = surface_image.pitches[0];
+ break;
default:
assert(0);
}
(unsigned char *)surface_p, pitches[0],
(unsigned char *)U_start, pitches[1],
(unsigned char *)V_start, pitches[2],
- (surface_image.format.fourcc==VA_FOURCC_NV12),
+ surface_image.format.fourcc,
box_width, row_shift, field);
vaUnmapBuffer(va_dpy,surface_image.buf);
return 0;
}
+
+/*
+ * Upload YUV data from memory into a surface
+ * if src_fourcc == NV12, assume the buffer pointed by src_U
+ * is UV interleaved (src_V is ignored)
+ */
+static int upload_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
+ int src_fourcc, int src_width, int src_height,
+ unsigned char *src_Y, unsigned char *src_U, unsigned char *src_V)
+{
+ VAImage surface_image;
+ unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL;
+ int Y_pitch=0, U_pitch=0, row;
+ VAStatus va_status;
+
+ va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
+ CHECK_VASTATUS(va_status,"vaDeriveImage");
+
+ vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
+ assert(VA_STATUS_SUCCESS == va_status);
+
+ Y_start = surface_p;
+ Y_pitch = surface_image.pitches[0];
+ switch (surface_image.format.fourcc) {
+ case VA_FOURCC_NV12:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[1];
+ U_pitch = surface_image.pitches[1];
+ break;
+ case VA_FOURCC_IYUV:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[1];
+ U_pitch = surface_image.pitches[1];
+ break;
+ case VA_FOURCC_YV12:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[2];
+ U_pitch = surface_image.pitches[2];
+ break;
+ case VA_FOURCC_YUY2:
+ U_start = surface_p + 1;
+ U_pitch = surface_image.pitches[0];
+ break;
+ default:
+ assert(0);
+ }
+
+ /* copy Y plane */
+ for (row=0;row<src_height;row++) {
+ unsigned char *Y_row = Y_start + row * Y_pitch;
+ memcpy(Y_row, src_Y + row*src_width, src_width);
+ }
+
+ for (row =0; row < src_height/2; row++) {
+ unsigned char *U_row = U_start + row * U_pitch;
+ unsigned char *u_ptr = NULL, *v_ptr=NULL;
+ int j;
+ switch (surface_image.format.fourcc) {
+ case VA_FOURCC_NV12:
+ if (src_fourcc == VA_FOURCC_NV12) {
+ memcpy(U_row, src_U + row * src_width, src_width);
+ break;
+ } else if (src_fourcc == VA_FOURCC_IYUV) {
+ u_ptr = src_U + row * (src_width/2);
+ v_ptr = src_V + row * (src_width/2);
+ } else if (src_fourcc == VA_FOURCC_YV12) {
+ v_ptr = src_U + row * (src_width/2);
+ u_ptr = src_V + row * (src_width/2);
+ }
+ if ((src_fourcc == VA_FOURCC_IYUV) ||
+ (src_fourcc == VA_FOURCC_YV12)) {
+ for(j = 0; j < src_width/2; j++) {
+ U_row[2*j] = u_ptr[j];
+ U_row[2*j+1] = v_ptr[j];
+ }
+ }
+ break;
+ case VA_FOURCC_IYUV:
+ case VA_FOURCC_YV12:
+ case VA_FOURCC_YUY2:
+ default:
+ printf("unsupported fourcc in load_surface_yuv\n");
+ assert(0);
+ }
+ }
+
+ vaUnmapBuffer(va_dpy,surface_image.buf);
+
+ vaDestroyImage(va_dpy,surface_image.image_id);
+
+ return 0;
+}
+
+/*
+ * Download YUV data from a surface into memory
+ * Some hardward doesn't have a aperture for linear access of
+ * tiled surface, thus use vaGetImage to expect the implemnetion
+ * to do tile to linear convert
+ *
+ * if dst_fourcc == NV12, assume the buffer pointed by dst_U
+ * is UV interleaved (src_V is ignored)
+ */
+static int download_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
+ int dst_fourcc, int dst_width, int dst_height,
+ unsigned char *dst_Y, unsigned char *dst_U, unsigned char *dst_V)
+{
+ VAImage surface_image;
+ unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL;
+ int Y_pitch=0, U_pitch=0, row;
+ VAStatus va_status;
+
+ va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
+ CHECK_VASTATUS(va_status,"vaDeriveImage");
+
+ vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
+ assert(VA_STATUS_SUCCESS == va_status);
+
+ Y_start = surface_p;
+ Y_pitch = surface_image.pitches[0];
+ switch (surface_image.format.fourcc) {
+ case VA_FOURCC_NV12:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[1];
+ U_pitch = surface_image.pitches[1];
+ break;
+ case VA_FOURCC_IYUV:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[1];
+ U_pitch = surface_image.pitches[1];
+ break;
+ case VA_FOURCC_YV12:
+ U_start = (unsigned char *)surface_p + surface_image.offsets[2];
+ U_pitch = surface_image.pitches[2];
+ break;
+ case VA_FOURCC_YUY2:
+ U_start = surface_p + 1;
+ U_pitch = surface_image.pitches[0];
+ break;
+ default:
+ assert(0);
+ }
+
+ /* copy Y plane */
+ for (row=0;row<dst_height;row++) {
+ unsigned char *Y_row = Y_start + row * Y_pitch;
+ memcpy(dst_Y + row*dst_width, Y_row, dst_width);
+ }
+
+ for (row =0; row < dst_height/2; row++) {
+ unsigned char *U_row = U_start + row * U_pitch;
+ unsigned char *u_ptr = NULL, *v_ptr = NULL;
+ int j;
+ switch (surface_image.format.fourcc) {
+ case VA_FOURCC_NV12:
+ if (dst_fourcc == VA_FOURCC_NV12) {
+ memcpy(dst_U + row * dst_width, U_row, dst_width);
+ break;
+ } else if (dst_fourcc == VA_FOURCC_IYUV) {
+ u_ptr = dst_U + row * (dst_width/2);
+ v_ptr = dst_V + row * (dst_width/2);
+ } else if (dst_fourcc == VA_FOURCC_YV12) {
+ v_ptr = dst_U + row * (dst_width/2);
+ u_ptr = dst_V + row * (dst_width/2);
+ }
+ if ((dst_fourcc == VA_FOURCC_IYUV) ||
+ (dst_fourcc == VA_FOURCC_YV12)) {
+ for(j = 0; j < dst_width/2; j++) {
+ u_ptr[j] = U_row[2*j];
+ v_ptr[j] = U_row[2*j+1];
+ }
+ }
+ break;
+ case VA_FOURCC_IYUV:
+ case VA_FOURCC_YV12:
+ case VA_FOURCC_YUY2:
+ default:
+ printf("unsupported fourcc in load_surface_yuv\n");
+ assert(0);
+ }
+ }
+
+ vaUnmapBuffer(va_dpy,surface_image.buf);
+
+ vaDestroyImage(va_dpy,surface_image.image_id);
+
+ return 0;
+}