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);
76
printf("Failed to allocate memory for pic_y\n");
80
pic_u = (unsigned char *)malloc(width * height/4);
82
printf("Failed to allocate memory for pic_u\n");
87
pic_v = (unsigned char *)malloc(width * height/4);
89
printf("Failed to allocate memory for pic_v\n");
96
memset(pic_y, 0, width * height);
97
memset(pic_u, 0, width * height /4);
98
memset(pic_v, 0, width * height /4);
100
scale_2dimage(pic_y_old, 640, 480,
101
pic_y, width, height);
102
scale_2dimage(pic_u_old, 320, 240,
103
pic_u, width/2, height/2);
104
scale_2dimage(pic_v_old, 320, 240,
105
pic_v, width/2, height/2);
111
int Y_pixel_stride = 1;
112
if (fourcc == VA_FOURCC_YUY2)
115
for (row=0; row<height; row++) {
116
unsigned char *p = Y_start + row * Y_pitch;
117
unsigned char *q = pic_y + row * width;
118
for (col=0; col<width; col++, q++) {
119
*p = *p * (100 - alpha) / 100 + *q * alpha/100;
125
int U_pixel_stride = 0, V_pixel_stride = 0;
126
int v_factor_to_nv12 = 1;
129
U_pixel_stride = V_pixel_stride = 1;
132
U_pixel_stride = V_pixel_stride = 2;
135
U_pixel_stride = V_pixel_stride = 4;
136
v_factor_to_nv12 = 2;
141
for (row=0; row<height/2*v_factor_to_nv12; row++) {
142
unsigned char *pU = U_start + row * U_pitch;
143
unsigned char *pV = V_start + row * V_pitch;
144
unsigned char *qU = pic_u + row/v_factor_to_nv12 * width/2;
145
unsigned char *qV = pic_v + row/v_factor_to_nv12 * width/2;
147
for (col=0; col<width/2; col++, qU++, qV++) {
148
*pU = *pU * (100 - alpha) / 100 + *qU * alpha/100;
149
*pV = *pV * (100 - alpha) / 100 + *qV * alpha/100;
151
pU += U_pixel_stride;
152
pV += V_pixel_stride;
165
static int yuvgen_planar(int width, int height,
166
unsigned char *Y_start, int Y_pitch,
167
unsigned char *U_start, int U_pitch,
168
unsigned char *V_start, int V_pitch,
169
unsigned int fourcc, int box_width, int row_shift,
173
unsigned char uv_value = 0x80;
177
if (fourcc == VA_FOURCC_YUY2) y_factor = 2;
178
for (row=0;row<height;row++) {
179
unsigned char *Y_row = Y_start + row * Y_pitch;
182
ypos = (row / box_width) & 0x1;
184
/* fill garbage data into the other field */
185
if (((field == VA_TOP_FIELD) && (row &1))
186
|| ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
187
memset(Y_row, 0xff, width);
191
for (jj=0; jj<width; jj++) {
192
xpos = ((row_shift + jj) / box_width) & 0x1;
194
Y_row[jj*y_factor] = 0xeb;
196
Y_row[jj*y_factor] = 0x10;
198
if (fourcc == VA_FOURCC_YUY2) {
199
Y_row[jj*y_factor+1] = uv_value; // it is for UV
205
for( row =0; row < height/2; row++) {
207
/* fill garbage data into the other field */
208
if (((field == VA_TOP_FIELD) && (row &1))
209
|| ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
213
unsigned char *U_row = U_start + row * U_pitch;
214
unsigned char *V_row = V_start + row * V_pitch;
217
memset(U_row, uv_value, width);
220
memset (U_row,uv_value,width/2);
221
memset (V_row,uv_value,width/2);
224
// see above. it is set with Y update.
227
printf("unsupported fourcc in loadsurface.h\n");
232
if (getenv("AUTO_NOUV"))
235
if (getenv("AUTO_ALPHA"))
240
YUV_blend_with_pic(width,height,
249
static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id,
250
int box_width, int row_shift,
253
VAImage surface_image;
254
void *surface_p=NULL, *U_start = NULL,*V_start = NULL;
256
unsigned int pitches[3]={0,0,0};
258
va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
259
CHECK_VASTATUS(va_status,"vaDeriveImage");
261
vaMapBuffer(va_dpy,surface_image.buf,&surface_p);
262
assert(VA_STATUS_SUCCESS == va_status);
264
pitches[0] = surface_image.pitches[0];
265
switch (surface_image.format.fourcc) {
267
U_start = (char *)surface_p + surface_image.offsets[1];
268
V_start = (char *)U_start + 1;
269
pitches[1] = surface_image.pitches[1];
270
pitches[2] = surface_image.pitches[1];
273
U_start = (char *)surface_p + surface_image.offsets[1];
274
V_start = (char *)surface_p + surface_image.offsets[2];
275
pitches[1] = surface_image.pitches[1];
276
pitches[2] = surface_image.pitches[2];
279
U_start = (char *)surface_p + surface_image.offsets[2];
280
V_start = (char *)surface_p + surface_image.offsets[1];
281
pitches[1] = surface_image.pitches[2];
282
pitches[2] = surface_image.pitches[1];
285
U_start = (char *)surface_p + 1;
286
V_start = (char *)surface_p + 3;
287
pitches[1] = surface_image.pitches[0];
288
pitches[2] = surface_image.pitches[0];
294
/* assume surface is planar format */
295
yuvgen_planar(surface_image.width, surface_image.height,
296
(unsigned char *)surface_p, pitches[0],
297
(unsigned char *)U_start, pitches[1],
298
(unsigned char *)V_start, pitches[2],
299
surface_image.format.fourcc,
300
box_width, row_shift, field);
302
vaUnmapBuffer(va_dpy,surface_image.buf);
304
vaDestroyImage(va_dpy,surface_image.image_id);
310
* Upload YUV data from memory into a surface
311
* if src_fourcc == NV12, assume the buffer pointed by src_U
312
* is UV interleaved (src_V is ignored)
314
static int upload_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
315
int src_fourcc, int src_width, int src_height,
316
unsigned char *src_Y, unsigned char *src_U, unsigned char *src_V)
318
VAImage surface_image;
319
unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL;
320
int Y_pitch=0, U_pitch=0, row;
323
va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
324
CHECK_VASTATUS(va_status,"vaDeriveImage");
326
vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
327
assert(VA_STATUS_SUCCESS == va_status);
330
Y_pitch = surface_image.pitches[0];
331
switch (surface_image.format.fourcc) {
333
U_start = (unsigned char *)surface_p + surface_image.offsets[1];
334
U_pitch = surface_image.pitches[1];
337
U_start = (unsigned char *)surface_p + surface_image.offsets[1];
338
U_pitch = surface_image.pitches[1];
341
U_start = (unsigned char *)surface_p + surface_image.offsets[2];
342
U_pitch = surface_image.pitches[2];
345
U_start = surface_p + 1;
346
U_pitch = surface_image.pitches[0];
353
for (row=0;row<src_height;row++) {
354
unsigned char *Y_row = Y_start + row * Y_pitch;
355
memcpy(Y_row, src_Y + row*src_width, src_width);
358
for (row =0; row < src_height/2; row++) {
359
unsigned char *U_row = U_start + row * U_pitch;
360
unsigned char *u_ptr = NULL, *v_ptr=NULL;
362
switch (surface_image.format.fourcc) {
364
if (src_fourcc == VA_FOURCC_NV12) {
365
memcpy(U_row, src_U + row * src_width, src_width);
367
} else if (src_fourcc == VA_FOURCC_IYUV) {
368
u_ptr = src_U + row * (src_width/2);
369
v_ptr = src_V + row * (src_width/2);
370
} else if (src_fourcc == VA_FOURCC_YV12) {
371
v_ptr = src_U + row * (src_width/2);
372
u_ptr = src_V + row * (src_width/2);
374
if ((src_fourcc == VA_FOURCC_IYUV) ||
375
(src_fourcc == VA_FOURCC_YV12)) {
376
for(j = 0; j < src_width/2; j++) {
377
U_row[2*j] = u_ptr[j];
378
U_row[2*j+1] = v_ptr[j];
386
printf("unsupported fourcc in load_surface_yuv\n");
391
vaUnmapBuffer(va_dpy,surface_image.buf);
393
vaDestroyImage(va_dpy,surface_image.image_id);
399
* Download YUV data from a surface into memory
400
* Some hardward doesn't have a aperture for linear access of
401
* tiled surface, thus use vaGetImage to expect the implemnetion
402
* to do tile to linear convert
404
* if dst_fourcc == NV12, assume the buffer pointed by dst_U
405
* is UV interleaved (src_V is ignored)
407
static int download_surface_yuv(VADisplay va_dpy, VASurfaceID surface_id,
408
int dst_fourcc, int dst_width, int dst_height,
409
unsigned char *dst_Y, unsigned char *dst_U, unsigned char *dst_V)
411
VAImage surface_image;
412
unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL;
413
int Y_pitch=0, U_pitch=0, row;
416
va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
417
CHECK_VASTATUS(va_status,"vaDeriveImage");
419
vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
420
assert(VA_STATUS_SUCCESS == va_status);
423
Y_pitch = surface_image.pitches[0];
424
switch (surface_image.format.fourcc) {
426
U_start = (unsigned char *)surface_p + surface_image.offsets[1];
427
U_pitch = surface_image.pitches[1];
430
U_start = (unsigned char *)surface_p + surface_image.offsets[1];
431
U_pitch = surface_image.pitches[1];
434
U_start = (unsigned char *)surface_p + surface_image.offsets[2];
435
U_pitch = surface_image.pitches[2];
438
U_start = surface_p + 1;
439
U_pitch = surface_image.pitches[0];
446
for (row=0;row<dst_height;row++) {
447
unsigned char *Y_row = Y_start + row * Y_pitch;
448
memcpy(dst_Y + row*dst_width, Y_row, dst_width);
451
for (row =0; row < dst_height/2; row++) {
452
unsigned char *U_row = U_start + row * U_pitch;
453
unsigned char *u_ptr = NULL, *v_ptr = NULL;
455
switch (surface_image.format.fourcc) {
457
if (dst_fourcc == VA_FOURCC_NV12) {
458
memcpy(dst_U + row * dst_width, U_row, dst_width);
460
} else if (dst_fourcc == VA_FOURCC_IYUV) {
461
u_ptr = dst_U + row * (dst_width/2);
462
v_ptr = dst_V + row * (dst_width/2);
463
} else if (dst_fourcc == VA_FOURCC_YV12) {
464
v_ptr = dst_U + row * (dst_width/2);
465
u_ptr = dst_V + row * (dst_width/2);
467
if ((dst_fourcc == VA_FOURCC_IYUV) ||
468
(dst_fourcc == VA_FOURCC_YV12)) {
469
for(j = 0; j < dst_width/2; j++) {
470
u_ptr[j] = U_row[2*j];
471
v_ptr[j] = U_row[2*j+1];
479
printf("unsupported fourcc in load_surface_yuv\n");
484
vaUnmapBuffer(va_dpy,surface_image.buf);
486
vaDestroyImage(va_dpy,surface_image.image_id);