OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2008-2009 Intel Corporation. All Rights Reserved. |
| 3 * |
| 4 * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 * copy of this software and associated documentation files (the |
| 6 * "Software"), to deal in the Software without restriction, including |
| 7 * without limitation the rights to use, copy, modify, merge, publish, |
| 8 * distribute, sub license, and/or sell copies of the Software, and to |
| 9 * permit persons to whom the Software is furnished to do so, subject to |
| 10 * the following conditions: |
| 11 * |
| 12 * The above copyright notice and this permission notice (including the |
| 13 * next paragraph) shall be included in all copies or substantial portions |
| 14 * of the Software. |
| 15 * |
| 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. |
| 19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR |
| 20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 */ |
| 24 #include "loadsurface_yuv.h" |
| 25 |
| 26 static int scale_2dimage(unsigned char *src_img, int src_imgw, int src_imgh, |
| 27 unsigned char *dst_img, int dst_imgw, int dst_imgh) |
| 28 { |
| 29 int row=0, col=0; |
| 30 |
| 31 for (row=0; row<dst_imgh; row++) { |
| 32 for (col=0; col<dst_imgw; col++) { |
| 33 *(dst_img + row * dst_imgw + col) = *(src_img + (row * src_imgh/dst_
imgh) * src_imgw + col * src_imgw/dst_imgw); |
| 34 } |
| 35 } |
| 36 |
| 37 return 0; |
| 38 } |
| 39 |
| 40 |
| 41 static int YUV_blend_with_pic(int width, int height, |
| 42 unsigned char *Y_start, int Y_pitch, |
| 43 unsigned char *U_start, int U_pitch, |
| 44 unsigned char *V_start, int V_pitch, |
| 45 int UV_interleave, int fixed_alpha) |
| 46 { |
| 47 /* PIC YUV format */ |
| 48 unsigned char *pic_y_old = yuvga_pic; |
| 49 unsigned char *pic_u_old = pic_y_old + 640*480; |
| 50 unsigned char *pic_v_old = pic_u_old + 640*480/4; |
| 51 unsigned char *pic_y, *pic_u, *pic_v; |
| 52 |
| 53 int alpha_values[] = {100,90,80,70,60,50,40,30,20,30,40,50,60,70,80,90}; |
| 54 |
| 55 static int alpha_idx = 0; |
| 56 int alpha; |
| 57 int allocated = 0; |
| 58 |
| 59 int row, col; |
| 60 |
| 61 if (fixed_alpha == 0) { |
| 62 alpha = alpha_values[alpha_idx % 16 ]; |
| 63 alpha_idx ++; |
| 64 } else |
| 65 alpha = fixed_alpha; |
| 66 |
| 67 //alpha = 0; |
| 68 |
| 69 pic_y = pic_y_old; |
| 70 pic_u = pic_u_old; |
| 71 pic_v = pic_v_old; |
| 72 |
| 73 if (width != 640 || height != 480) { /* need to scale the pic */ |
| 74 pic_y = (unsigned char *)malloc(width * height); |
| 75 pic_u = (unsigned char *)malloc(width * height/4); |
| 76 pic_v = (unsigned char *)malloc(width * height/4); |
| 77 |
| 78 allocated = 1; |
| 79 |
| 80 scale_2dimage(pic_y_old, 640, 480, |
| 81 pic_y, width, height); |
| 82 scale_2dimage(pic_u_old, 320, 240, |
| 83 pic_u, width/2, height/2); |
| 84 scale_2dimage(pic_v_old, 320, 240, |
| 85 pic_v, width/2, height/2); |
| 86 } |
| 87 |
| 88 /* begin blend */ |
| 89 |
| 90 /* Y plane */ |
| 91 for (row=0; row<height; row++) |
| 92 for (col=0; col<width; col++) { |
| 93 unsigned char *p = Y_start + row * Y_pitch + col; |
| 94 unsigned char *q = pic_y + row * width + col; |
| 95 |
| 96 *p = *p * (100 - alpha) / 100 + *q * alpha/100; |
| 97 } |
| 98 |
| 99 if (UV_interleave == 0) { |
| 100 for (row=0; row<height/2; row++) |
| 101 for (col=0; col<width/2; col++) { |
| 102 unsigned char *p = U_start + row * U_pitch + col; |
| 103 unsigned char *q = pic_u + row * width/2 + col; |
| 104 |
| 105 *p = *p * (100 - alpha) / 100 + *q * alpha/100; |
| 106 } |
| 107 |
| 108 for (row=0; row<height/2; row++) |
| 109 for (col=0; col<width/2; col++) { |
| 110 unsigned char *p = V_start + row * V_pitch + col; |
| 111 unsigned char *q = pic_v + row * width/2 + col; |
| 112 |
| 113 *p = *p * (100 - alpha) / 100 + *q * alpha/100; |
| 114 } |
| 115 } else { /* NV12 */ |
| 116 for (row=0; row<height/2; row++) |
| 117 for (col=0; col<width/2; col++) { |
| 118 unsigned char *pU = U_start + row * U_pitch + col*2; |
| 119 unsigned char *qU = pic_u + row * width/2 + col; |
| 120 unsigned char *pV = pU + 1; |
| 121 unsigned char *qV = pic_v + row * width/2 + col; |
| 122 |
| 123 *pU = *pU * (100 - alpha) / 100 + *qU * alpha/100; |
| 124 *pV = *pV * (100 - alpha) / 100 + *qV * alpha/100; |
| 125 } |
| 126 } |
| 127 |
| 128 |
| 129 if (allocated) { |
| 130 free(pic_y); |
| 131 free(pic_u); |
| 132 free(pic_v); |
| 133 } |
| 134 |
| 135 return 0; |
| 136 } |
| 137 |
| 138 static int yuvgen_planar(int width, int height, |
| 139 unsigned char *Y_start, int Y_pitch, |
| 140 unsigned char *U_start, int U_pitch, |
| 141 unsigned char *V_start, int V_pitch, |
| 142 int UV_interleave, int box_width, int row_shift, |
| 143 int field) |
| 144 { |
| 145 int row, alpha; |
| 146 |
| 147 /* copy Y plane */ |
| 148 for (row=0;row<height;row++) { |
| 149 unsigned char *Y_row = Y_start + row * Y_pitch; |
| 150 int jj, xpos, ypos; |
| 151 |
| 152 ypos = (row / box_width) & 0x1; |
| 153 |
| 154 /* fill garbage data into the other field */ |
| 155 if (((field == VA_TOP_FIELD) && (row &1)) |
| 156 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) { |
| 157 memset(Y_row, 0xff, width); |
| 158 continue; |
| 159 } |
| 160 |
| 161 for (jj=0; jj<width; jj++) { |
| 162 xpos = ((row_shift + jj) / box_width) & 0x1; |
| 163 |
| 164 if ((xpos == 0) && (ypos == 0)) |
| 165 Y_row[jj] = 0xeb; |
| 166 if ((xpos == 1) && (ypos == 1)) |
| 167 Y_row[jj] = 0xeb; |
| 168 |
| 169 if ((xpos == 1) && (ypos == 0)) |
| 170 Y_row[jj] = 0x10; |
| 171 if ((xpos == 0) && (ypos == 1)) |
| 172 Y_row[jj] = 0x10; |
| 173 } |
| 174 } |
| 175 |
| 176 /* copy UV data */ |
| 177 for( row =0; row < height/2; row++) { |
| 178 unsigned short value = 0x80; |
| 179 |
| 180 /* fill garbage data into the other field */ |
| 181 if (((field == VA_TOP_FIELD) && (row &1)) |
| 182 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) { |
| 183 value = 0xff; |
| 184 } |
| 185 |
| 186 if (UV_interleave) { |
| 187 unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch)
; |
| 188 |
| 189 memset(UV_row, value, width); |
| 190 } else { |
| 191 unsigned char *U_row = U_start + row * U_pitch; |
| 192 unsigned char *V_row = V_start + row * V_pitch; |
| 193 |
| 194 memset (U_row,value,width/2); |
| 195 memset (V_row,value,width/2); |
| 196 } |
| 197 } |
| 198 |
| 199 if (getenv("AUTO_NOUV")) |
| 200 return 0; |
| 201 |
| 202 if (getenv("AUTO_ALPHA")) |
| 203 alpha = 0; |
| 204 else |
| 205 alpha = 70; |
| 206 |
| 207 YUV_blend_with_pic(width,height, |
| 208 Y_start, Y_pitch, |
| 209 U_start, U_pitch, |
| 210 V_start, V_pitch, |
| 211 UV_interleave, alpha); |
| 212 |
| 213 return 0; |
| 214 } |
| 215 |
| 216 static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id, |
| 217 int box_width, int row_shift, |
| 218 int field) |
| 219 { |
| 220 VAImage surface_image; |
| 221 void *surface_p=NULL, *U_start,*V_start; |
| 222 VAStatus va_status; |
| 223 |
| 224 va_status = vaDeriveImage(va_dpy,surface_id,&surface_image); |
| 225 CHECK_VASTATUS(va_status,"vaDeriveImage"); |
| 226 |
| 227 vaMapBuffer(va_dpy,surface_image.buf,&surface_p); |
| 228 assert(VA_STATUS_SUCCESS == va_status); |
| 229 |
| 230 U_start = (char *)surface_p + surface_image.offsets[1]; |
| 231 V_start = (char *)surface_p + surface_image.offsets[2]; |
| 232 |
| 233 /* assume surface is planar format */ |
| 234 yuvgen_planar(surface_image.width, surface_image.height, |
| 235 (unsigned char *)surface_p, surface_image.pitches[0], |
| 236 (unsigned char *)U_start, surface_image.pitches[1], |
| 237 (unsigned char *)V_start, surface_image.pitches[2], |
| 238 (surface_image.format.fourcc==VA_FOURCC_NV12), |
| 239 box_width, row_shift, field); |
| 240 |
| 241 vaUnmapBuffer(va_dpy,surface_image.buf); |
| 242 |
| 243 vaDestroyImage(va_dpy,surface_image.image_id); |
| 244 |
| 245 return 0; |
| 246 } |
OLD | NEW |