OSDN Git Service

h264encode: fix build warning on Android
[android-x86/hardware-intel-common-libva.git] / test / loadsurface.h
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             unsigned int fourcc, 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     int Y_pixel_stride = 1;
92     if (fourcc == VA_FOURCC_YUY2) 
93         Y_pixel_stride = 2;
94          
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;
100             p += Y_pixel_stride;
101         }
102     }
103
104     /* U/V plane */
105     int U_pixel_stride = 0, V_pixel_stride = 0;
106     int v_factor_to_nv12 = 1;
107     switch (fourcc) {
108     case VA_FOURCC_YV12:
109         U_pixel_stride = V_pixel_stride = 1;
110         break;
111     case VA_FOURCC_NV12:
112         U_pixel_stride = V_pixel_stride = 2;
113         break;
114     case VA_FOURCC_YUY2:
115         U_pixel_stride = V_pixel_stride = 4;
116         v_factor_to_nv12 = 2;
117         break;
118     default:
119         break;
120     }
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;
126             
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;
130
131             pU += U_pixel_stride;
132             pV += V_pixel_stride;
133         }
134     }
135         
136     
137     if (allocated) {
138         free(pic_y);
139         free(pic_u);
140         free(pic_v);
141     }
142     
143     return 0;
144 }
145
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,
151                          int field)
152 {
153     int row, alpha;
154     unsigned char uv_value = 0x80;
155
156     /* copy Y plane */
157     int y_factor = 1;
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;
161         int jj, xpos, ypos;
162
163         ypos = (row / box_width) & 0x1;
164
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);
169             continue;
170         }
171         
172         for (jj=0; jj<width; jj++) {
173             xpos = ((row_shift + jj) / box_width) & 0x1;
174             if (xpos == ypos)
175                 Y_row[jj*y_factor] = 0xeb;
176             else 
177                 Y_row[jj*y_factor] = 0x10;
178
179             if (fourcc == VA_FOURCC_YUY2) {
180                 Y_row[jj*y_factor+1] = uv_value; // it is for UV
181                     }
182         }
183     }
184   
185     /* copy UV data */
186     for( row =0; row < height/2; row++) {
187
188         /* fill garbage data into the other field */
189         if (((field == VA_TOP_FIELD) && (row &1))
190             || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
191             uv_value = 0xff;
192         }
193
194         unsigned char *U_row = U_start + row * U_pitch;
195         unsigned char *V_row = V_start + row * V_pitch;
196         switch (fourcc) {
197         case VA_FOURCC_NV12:
198             memset(U_row, uv_value, width);
199                         break;
200         case VA_FOURCC_YV12:
201             memset (U_row,uv_value,width/2);
202             memset (V_row,uv_value,width/2);
203                         break;
204         case VA_FOURCC_YUY2:
205             // see above. it is set with Y update.
206             break;
207         default:
208             printf("unsupported fourcc in loadsurface.h\n");
209             assert(0);
210         }
211     }
212
213     if (getenv("AUTO_NOUV"))
214         return 0;
215
216     if (getenv("AUTO_ALPHA"))
217         alpha = 0;
218     else
219         alpha = 70;
220     
221     YUV_blend_with_pic(width,height,
222                        Y_start, Y_pitch,
223                        U_start, U_pitch,
224                        V_start, V_pitch,
225                        fourcc, alpha);
226     
227     return 0;
228 }
229
230 static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id,
231                           int box_width, int row_shift,
232                           int field)
233 {
234     VAImage surface_image;
235     void *surface_p=NULL, *U_start = NULL,*V_start = NULL;
236     VAStatus va_status;
237     unsigned int pitches[3]={0,0,0};
238     
239     va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
240     CHECK_VASTATUS(va_status,"vaDeriveImage");
241
242     vaMapBuffer(va_dpy,surface_image.buf,&surface_p);
243     assert(VA_STATUS_SUCCESS == va_status);
244
245     pitches[0] = surface_image.pitches[0];
246     switch (surface_image.format.fourcc) {
247     case VA_FOURCC_NV12:
248         U_start = (char *)surface_p + surface_image.offsets[1];
249         V_start = (char *)U_start + 1;
250         pitches[1] = surface_image.pitches[1];
251         pitches[2] = surface_image.pitches[1];
252         break;
253     case VA_FOURCC_IYUV:
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];
258         break;
259     case VA_FOURCC_YV12:
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];
264         break;
265     case VA_FOURCC_YUY2:
266         U_start = (char *)surface_p + 1;
267         V_start = (char *)surface_p + 3;
268         pitches[1] = surface_image.pitches[0];
269         pitches[2] = surface_image.pitches[0];
270         break;
271     default:
272         assert(0);
273     }
274
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);
282         
283     vaUnmapBuffer(va_dpy,surface_image.buf);
284
285     vaDestroyImage(va_dpy,surface_image.image_id);
286
287     return 0;
288 }