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 int UV_interleave, 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 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;
96 *p = *p * (100 - alpha) / 100 + *q * alpha/100;
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;
105 *p = *p * (100 - alpha) / 100 + *q * alpha/100;
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;
113 *p = *p * (100 - alpha) / 100 + *q * alpha/100;
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;
123 *pU = *pU * (100 - alpha) / 100 + *qU * alpha/100;
124 *pV = *pV * (100 - alpha) / 100 + *qV * alpha/100;
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,
148 for (row=0;row<height;row++) {
149 unsigned char *Y_row = Y_start + row * Y_pitch;
152 ypos = (row / box_width) & 0x1;
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);
161 for (jj=0; jj<width; jj++) {
162 xpos = ((row_shift + jj) / box_width) & 0x1;
164 if ((xpos == 0) && (ypos == 0))
166 if ((xpos == 1) && (ypos == 1))
169 if ((xpos == 1) && (ypos == 0))
171 if ((xpos == 0) && (ypos == 1))
177 for( row =0; row < height/2; row++) {
178 unsigned short value = 0x80;
180 /* fill garbage data into the other field */
181 if (((field == VA_TOP_FIELD) && (row &1))
182 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
187 unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);
189 memset(UV_row, value, width);
191 unsigned char *U_row = U_start + row * U_pitch;
192 unsigned char *V_row = V_start + row * V_pitch;
194 memset (U_row,value,width/2);
195 memset (V_row,value,width/2);
199 if (getenv("AUTO_NOUV"))
202 if (getenv("AUTO_ALPHA"))
207 YUV_blend_with_pic(width,height,
211 UV_interleave, alpha);
216 static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id,
217 int box_width, int row_shift,
220 VAImage surface_image;
221 void *surface_p=NULL, *U_start,*V_start;
223 unsigned int pitches[3];
225 va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
226 CHECK_VASTATUS(va_status,"vaDeriveImage");
228 vaMapBuffer(va_dpy,surface_image.buf,&surface_p);
229 assert(VA_STATUS_SUCCESS == va_status);
231 pitches[0] = surface_image.pitches[0];
232 switch (surface_image.format.fourcc) {
234 U_start = (char *)surface_p + surface_image.offsets[1];
235 V_start = U_start + 1;
236 pitches[1] = surface_image.pitches[1];
237 pitches[2] = surface_image.pitches[1];
240 U_start = (char *)surface_p + surface_image.offsets[1];
241 V_start = (char *)surface_p + surface_image.offsets[2];
242 pitches[1] = surface_image.pitches[1];
243 pitches[2] = surface_image.pitches[2];
246 U_start = (char *)surface_p + surface_image.offsets[2];
247 V_start = (char *)surface_p + surface_image.offsets[1];
248 pitches[1] = surface_image.pitches[2];
249 pitches[2] = surface_image.pitches[1];
255 /* assume surface is planar format */
256 yuvgen_planar(surface_image.width, surface_image.height,
257 (unsigned char *)surface_p, pitches[0],
258 (unsigned char *)U_start, pitches[1],
259 (unsigned char *)V_start, pitches[2],
260 (surface_image.format.fourcc==VA_FOURCC_NV12),
261 box_width, row_shift, field);
263 vaUnmapBuffer(va_dpy,surface_image.buf);
265 vaDestroyImage(va_dpy,surface_image.image_id);