2 * Copyright (c) 2008-2009 Intel Corporation. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
24 #include "loadsurface_yuv.h"
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)
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);
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 unsigned int fourcc, int fixed_alpha)
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;
53 int alpha_values[] = {100,90,80,70,60,50,40,30,20,30,40,50,60,70,80,90};
55 static int alpha_idx = 0;
61 if (fixed_alpha == 0) {
62 alpha = alpha_values[alpha_idx % 16 ];
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);
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);
91 int Y_pixel_stride = 1;
92 if (fourcc == VA_FOURCC_YUY2)
95 for (row=0; row<height; row++) {
96 unsigned char *p = Y_start + row * Y_pitch;
97 unsigned char *q = pic_y + row * width;
98 for (col=0; col<width; col++, q++) {
99 *p = *p * (100 - alpha) / 100 + *q * alpha/100;
105 int U_pixel_stride, V_pixel_stride;
106 int v_factor_to_nv12 = 1;
109 U_pixel_stride = V_pixel_stride = 1;
112 U_pixel_stride = V_pixel_stride = 2;
115 U_pixel_stride = V_pixel_stride = 4;
116 v_factor_to_nv12 = 2;
121 for (row=0; row<height/2*v_factor_to_nv12; row++) {
122 unsigned char *pU = U_start + row * U_pitch;
123 unsigned char *pV = V_start + row * V_pitch;
124 unsigned char *qU = pic_u + row/v_factor_to_nv12 * width/2;
125 unsigned char *qV = pic_v + row/v_factor_to_nv12 * width/2;
127 for (col=0; col<width/2; col++, qU++, qV++) {
128 *pU = *pU * (100 - alpha) / 100 + *qU * alpha/100;
129 *pV = *pV * (100 - alpha) / 100 + *qV * alpha/100;
131 pU += U_pixel_stride;
132 pV += V_pixel_stride;
146 static int yuvgen_planar(int width, int height,
147 unsigned char *Y_start, int Y_pitch,
148 unsigned char *U_start, int U_pitch,
149 unsigned char *V_start, int V_pitch,
150 unsigned int fourcc, int box_width, int row_shift,
154 unsigned char uv_value = 0x80;
158 if (fourcc == VA_FOURCC_YUY2) y_factor = 2;
159 for (row=0;row<height;row++) {
160 unsigned char *Y_row = Y_start + row * Y_pitch;
163 ypos = (row / box_width) & 0x1;
165 /* fill garbage data into the other field */
166 if (((field == VA_TOP_FIELD) && (row &1))
167 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
168 memset(Y_row, 0xff, width);
172 for (jj=0; jj<width; jj++) {
173 xpos = ((row_shift + jj) / box_width) & 0x1;
175 Y_row[jj*y_factor] = 0xeb;
177 Y_row[jj*y_factor] = 0x10;
179 if (fourcc == VA_FOURCC_YUY2) {
180 Y_row[jj*y_factor+1] = uv_value; // it is for UV
186 for( row =0; row < height/2; row++) {
188 /* fill garbage data into the other field */
189 if (((field == VA_TOP_FIELD) && (row &1))
190 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
194 unsigned char *U_row = U_start + row * U_pitch;
195 unsigned char *V_row = V_start + row * V_pitch;
198 memset(U_row, uv_value, width);
201 memset (U_row,uv_value,width/2);
202 memset (V_row,uv_value,width/2);
205 // see above. it is set with Y update.
208 printf("unsupported fourcc in loadsurface.h\n");
213 if (getenv("AUTO_NOUV"))
216 if (getenv("AUTO_ALPHA"))
221 YUV_blend_with_pic(width,height,
230 static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id,
231 int box_width, int row_shift,
234 VAImage surface_image;
235 void *surface_p=NULL, *U_start,*V_start;
237 unsigned int pitches[3];
239 va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
240 CHECK_VASTATUS(va_status,"vaDeriveImage");
242 vaMapBuffer(va_dpy,surface_image.buf,&surface_p);
243 assert(VA_STATUS_SUCCESS == va_status);
245 pitches[0] = surface_image.pitches[0];
246 switch (surface_image.format.fourcc) {
248 U_start = (char *)surface_p + surface_image.offsets[1];
249 V_start = U_start + 1;
250 pitches[1] = surface_image.pitches[1];
251 pitches[2] = surface_image.pitches[1];
254 U_start = (char *)surface_p + surface_image.offsets[1];
255 V_start = (char *)surface_p + surface_image.offsets[2];
256 pitches[1] = surface_image.pitches[1];
257 pitches[2] = surface_image.pitches[2];
260 U_start = (char *)surface_p + surface_image.offsets[2];
261 V_start = (char *)surface_p + surface_image.offsets[1];
262 pitches[1] = surface_image.pitches[2];
263 pitches[2] = surface_image.pitches[1];
266 U_start = surface_p + 1;
267 V_start = surface_p + 3;
268 pitches[1] = surface_image.pitches[0];
269 pitches[2] = surface_image.pitches[0];
275 /* assume surface is planar format */
276 yuvgen_planar(surface_image.width, surface_image.height,
277 (unsigned char *)surface_p, pitches[0],
278 (unsigned char *)U_start, pitches[1],
279 (unsigned char *)V_start, pitches[2],
280 surface_image.format.fourcc,
281 box_width, row_shift, field);
283 vaUnmapBuffer(va_dpy,surface_image.buf);
285 vaDestroyImage(va_dpy,surface_image.image_id);