OSDN Git Service

Add the fourcc of I010 format
[android-x86/hardware-intel-common-libva.git] / test / loadsurface.h
index 119db97..82cf61c 100755 (executable)
@@ -72,10 +72,30 @@ static int YUV_blend_with_pic(int width, int height,
     
     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);
@@ -133,7 +153,6 @@ static int YUV_blend_with_pic(int width, int height,
         }
     }
         
-    
     if (allocated) {
         free(pic_y);
         free(pic_u);
@@ -297,8 +316,8 @@ static int upload_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
                               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, *V_start=NULL;
-    int Y_pitch=0, U_pitch=0, V_pitch=0, row;
+    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);
@@ -312,27 +331,19 @@ static int upload_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
     switch (surface_image.format.fourcc) {
     case VA_FOURCC_NV12:
         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
-        V_start = U_start + 1;
         U_pitch = surface_image.pitches[1];
-        V_pitch = surface_image.pitches[1];
         break;
     case VA_FOURCC_IYUV:
         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
-        V_start = (unsigned char *)surface_p + surface_image.offsets[2];
         U_pitch = surface_image.pitches[1];
-        V_pitch = surface_image.pitches[2];
         break;
     case VA_FOURCC_YV12:
         U_start = (unsigned char *)surface_p + surface_image.offsets[2];
-        V_start = (unsigned char *)surface_p + surface_image.offsets[1];
         U_pitch = surface_image.pitches[2];
-        V_pitch = surface_image.pitches[1];
         break;
     case VA_FOURCC_YUY2:
         U_start = surface_p + 1;
-        V_start = surface_p + 3;
         U_pitch = surface_image.pitches[0];
-        V_pitch = surface_image.pitches[0];
         break;
     default:
         assert(0);
@@ -360,9 +371,12 @@ static int upload_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
                 v_ptr = src_U + row * (src_width/2);
                 u_ptr = src_V + row * (src_width/2);
             }
-            for(j = 0; j < src_width/2; j++) {
-                U_row[2*j] = u_ptr[j];
-                U_row[2*j+1] = v_ptr[j];
+            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:
@@ -395,8 +409,8 @@ static int download_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
                                 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,*V_start=NULL;
-    int Y_pitch=0, U_pitch=0, V_pitch=0, row;
+    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);
@@ -410,27 +424,19 @@ static int download_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
     switch (surface_image.format.fourcc) {
     case VA_FOURCC_NV12:
         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
-        V_start = U_start + 1;
         U_pitch = surface_image.pitches[1];
-        V_pitch = surface_image.pitches[1];
         break;
     case VA_FOURCC_IYUV:
         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
-        V_start = (unsigned char *)surface_p + surface_image.offsets[2];
         U_pitch = surface_image.pitches[1];
-        V_pitch = surface_image.pitches[2];
         break;
     case VA_FOURCC_YV12:
         U_start = (unsigned char *)surface_p + surface_image.offsets[2];
-        V_start = (unsigned char *)surface_p + surface_image.offsets[1];
         U_pitch = surface_image.pitches[2];
-        V_pitch = surface_image.pitches[1];
         break;
     case VA_FOURCC_YUY2:
         U_start = surface_p + 1;
-        V_start = surface_p + 3;
         U_pitch = surface_image.pitches[0];
-        V_pitch = surface_image.pitches[0];
         break;
     default:
         assert(0);
@@ -458,9 +464,12 @@ static int download_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
                 v_ptr = dst_U + row * (dst_width/2);
                 u_ptr = dst_V + row * (dst_width/2);
             }
-            for(j = 0; j < dst_width/2; j++) {
-                u_ptr[j] = U_row[2*j];
-                v_ptr[j] = U_row[2*j+1];
+            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: