2 * Copyright (c) 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.
27 #include "va_backend.h"
37 #include <sys/types.h>
42 #include "va_fool_264.h"
46 * Do dummy decode/encode, ignore the input data
47 * In order to debug memory leak or low performance issues, we need to isolate driver problems
48 * We export env "VA_FOOL", with which, we can do fake decode/encode:
51 * . if set, decode does nothing, but fill in some YUV data
53 * . if set, encode does nothing, but fill in a hard-coded 720P clip into coded buffer.
54 * . VA CONTEXT/CONFIG/SURFACE will call into drivers, but VA Buffer creation is done in here
55 * . Bypass all ~SvaBeginPic/vaRenderPic/vaEndPic~T
57 * . if set, do nothing for vaPutSurface
63 /* LIBVA_FOOL_DECODE/LIBVA_FOOL_ENCODE/LIBVA_FOOL_POSTP */
64 static int fool_decode = 0;
65 static int fool_encode = 0;
68 #define FOOL_CONTEXT_MAX 4
69 /* per context settings */
70 static struct _fool_context {
71 VADisplay dpy; /* should use context as the key */
73 VAProfile fool_profile; /* current profile for buffers */
74 VAEntrypoint fool_entrypoint; /* current entrypoint */
76 FILE *fool_fp_codedclip; /* load a clip from disk for fooling encode*/
78 /* all buffers with same type share one malloc-ed memory
79 * bufferID = (buffer numbers with the same type << 8) || type
80 * the malloc-ed memory can be find by fool_buf[bufferID & 0xff]
81 * the size is ignored here
83 char *fool_buf[VABufferTypeMax]; /* memory of fool buffers */
84 unsigned int fool_buf_size[VABufferTypeMax]; /* size of memory of fool buffers */
85 unsigned int fool_buf_count[VABufferTypeMax]; /* count of created buffers */
87 } fool_context[FOOL_CONTEXT_MAX] = { {0} }; /* trace five context at the same time */
89 #define FOOL_DECODE(idx) (fool_decode && (fool_context[idx].fool_entrypoint == VAEntrypointVLD))
90 #define FOOL_ENCODE(idx) \
92 && (fool_context[idx].fool_entrypoint == VAEntrypointEncSlice) \
93 && (fool_context[idx].fool_profile >= VAProfileH264Baseline) \
94 && (fool_context[idx].fool_profile <= VAProfileH264High))
98 #define DPY2INDEX(dpy) \
101 for (idx = 0; idx < FOOL_CONTEXT_MAX; idx++) \
102 if (fool_context[idx].dpy == dpy) \
105 if (idx == FOOL_CONTEXT_MAX) \
106 return 0; /* let driver go */ \
108 /* Prototype declarations (functions defined in va.c) */
110 void va_errorMessage(const char *msg, ...);
111 void va_infoMessage(const char *msg, ...);
113 int va_parseConfig(char *env, char *env_value);
115 VAStatus vaBufferInfo(
117 VAContextID context, /* in */
118 VABufferID buf_id, /* in */
119 VABufferType *type, /* out */
120 unsigned int *size, /* out */
121 unsigned int *num_elements /* out */
124 VAStatus vaLockSurface(VADisplay dpy,
126 unsigned int *fourcc, /* following are output argument */
127 unsigned int *luma_stride,
128 unsigned int *chroma_u_stride,
129 unsigned int *chroma_v_stride,
130 unsigned int *luma_offset,
131 unsigned int *chroma_u_offset,
132 unsigned int *chroma_v_offset,
133 unsigned int *buffer_name,
137 VAStatus vaUnlockSurface(VADisplay dpy,
142 void va_FoolInit(VADisplay dpy)
144 char env_value[1024];
147 for (fool_index = 0; fool_index < FOOL_CONTEXT_MAX; fool_index++)
148 if (fool_context[fool_index].dpy == 0)
151 if (fool_index == FOOL_CONTEXT_MAX)
154 if (va_parseConfig("LIBVA_FOOL_POSTP", NULL) == 0) {
156 va_infoMessage("LIBVA_FOOL_POSTP is on, dummy vaPutSurface\n");
160 if (va_parseConfig("LIBVA_FOOL_DECODE", NULL) == 0) {
162 va_infoMessage("LIBVA_FOOL_DECODE is on, dummy decode\n");
166 if (va_parseConfig("LIBVA_FOOL_ENCODE", &env_value[0]) == 0) {
167 FILE *tmp = fopen(env_value, "r");
170 fool_context[fool_index].fool_fp_codedclip = tmp;
174 va_infoMessage("LIBVA_FOOL_ENCODE is on, dummy encode\n");
177 if (fool_encode || fool_decode)
178 fool_context[fool_index].dpy = dpy;
182 int va_FoolEnd(VADisplay dpy)
188 for (i = 0; i < VABufferTypeMax; i++) /* free memory */
189 if (fool_context[idx].fool_buf[i])
190 free(fool_context[idx].fool_buf[i]);
192 memset(&fool_context[idx], sizeof(struct _fool_context), 0);
196 int va_FoolCodedBuf(VADisplay dpy)
203 int va_FoolCreateConfig(
206 VAEntrypoint entrypoint,
207 VAConfigAttrib *attrib_list,
209 VAConfigID *config_id /* out */
214 /* call into driver level to allocate real context/surface/buffers, etc */
215 fool_context[idx].fool_profile = profile;
216 fool_context[idx].fool_entrypoint = entrypoint;
220 static int yuvgen_planar(
221 int width, int height,
222 unsigned char *Y_start, int Y_pitch,
223 unsigned char *U_start, int U_pitch,
224 unsigned char *V_start, int V_pitch,
225 int UV_interleave, int box_width, int row_shift,
232 for (row=0;row<height;row++) {
233 unsigned char *Y_row = Y_start + row * Y_pitch;
236 ypos = (row / box_width) & 0x1;
238 /* fill garbage data into the other field */
239 if (((field == VA_TOP_FIELD) && (row &1))
240 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
241 memset(Y_row, 0xff, width);
245 for (jj=0; jj<width; jj++) {
246 xpos = ((row_shift + jj) / box_width) & 0x1;
248 if ((xpos == 0) && (ypos == 0))
250 if ((xpos == 1) && (ypos == 1))
253 if ((xpos == 1) && (ypos == 0))
255 if ((xpos == 0) && (ypos == 1))
261 for( row =0; row < height/2; row++) {
262 unsigned short value = 0x80;
264 /* fill garbage data into the other field */
265 if (((field == VA_TOP_FIELD) && (row &1))
266 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
271 unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);
273 memset(UV_row, value, width);
275 unsigned char *U_row = U_start + row * U_pitch;
276 unsigned char *V_row = V_start + row * V_pitch;
278 memset (U_row,value,width/2);
279 memset (V_row,value,width/2);
287 int va_FoolCreateSurfaces(
293 VASurfaceID *surfaces /* out */
297 unsigned int fourcc; /* following are output argument */
298 unsigned int luma_stride;
299 unsigned int chroma_u_stride;
300 unsigned int chroma_v_stride;
301 unsigned int luma_offset;
302 unsigned int chroma_u_offset;
303 unsigned int chroma_v_offset;
304 unsigned int buffer_name;
306 unsigned char *Y_data, *U_data, *V_data;
308 int box_width = num_surfaces/2;
314 if (FOOL_DECODE(idx)) {
315 /* call into driver level to allocate real context/surface/buffers, etc
316 * fill in the YUV data, will be overwrite if it is encode context
318 for (i = 0; i < num_surfaces; i++) {
319 /* fool decoder: fill with auto-generated YUV data */
320 va_status = vaLockSurface(dpy, surfaces[i], &fourcc,
321 &luma_stride, &chroma_u_stride, &chroma_v_stride,
322 &luma_offset, &chroma_u_offset, &chroma_v_offset,
323 &buffer_name, &buffer);
325 if (va_status != VA_STATUS_SUCCESS)
329 vaUnlockSurface(dpy, surfaces[i]);
335 /* UV should be same for NV12 */
336 U_data = buffer + chroma_u_offset;
337 V_data = buffer + chroma_v_offset;
339 yuvgen_planar(width, height,
341 U_data, chroma_v_stride,
342 V_data, chroma_v_stride,
343 (fourcc==VA_FOURCC_NV12),
344 box_width, row_shift, 0);
346 vaUnlockSurface(dpy, surfaces[i]);
349 if (row_shift==(2*box_width))
352 return 0; /* the return value is ignored */
354 return 0; /* the return value is ignored */
357 VAStatus va_FoolCreateBuffer (
359 VAContextID context, /* in */
360 VABufferType type, /* in */
361 unsigned int size, /* in */
362 unsigned int num_elements, /* in */
364 VABufferID *buf_id /* out */
369 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) { /* fool buffer creation */
370 int new_size = size * num_elements;
372 if (type == VAEncCodedBufferType) /* only a VACodedBufferSegment */
373 new_size = sizeof(VACodedBufferSegment);
375 if (fool_context[idx].fool_buf_size[type] == 0)
376 fool_context[idx].fool_buf[type] = calloc(1, new_size);
377 else if (fool_context[idx].fool_buf_size[type] <= new_size)
378 fool_context[idx].fool_buf[type] = realloc(fool_context[idx].fool_buf, new_size);
380 if (fool_context[idx].fool_buf[type] == NULL) {
382 return 0; /* let driver go */
385 /* because we ignore the vaRenderPicture,
386 * all buffers with same type share same real memory
387 * bufferID = (buffer count << 8) | type
389 fool_context[idx].fool_buf_count[type]++;
390 *buf_id = (fool_context[idx].fool_buf_count[type] << 8) | type;
392 return 1; /* don't call into driver */
395 return 0; /* let driver go ... */
399 VAStatus va_FoolMapBuffer (
401 VABufferID buf_id, /* in */
402 void **pbuf /* out */
407 unsigned int num_elements;
410 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) { /* fool buffer creation */
411 unsigned int buf_idx = buf_id & 0xff;
414 vaBufferInfo(dpy, fool_context[idx].context, buf_id, &type, &size, &num_elements);
415 if (type == VAImageBufferType && FOOL_ENCODE(idx))
418 /* buf_id is the buffer type */
419 if (fool_context[idx].fool_buf[buf_idx] != NULL)
420 *pbuf = fool_context[idx].fool_buf[buf_idx];
424 /* expect APP to MapBuffer when get the the coded data */
425 if (*pbuf && (buf_idx == VAEncCodedBufferType)) { /* it is coded buffer */
426 /* should read from a clip, here we use the hardcoded h264_720p_nal */
427 memcpy(*pbuf, &h264_720p_nal[h264_720p_nal_idx], sizeof(VACodedBufferSegment));
429 if (h264_720p_nal_idx == H264_720P_NAL_NUMBER)
430 h264_720p_nal_idx = 0; /* reset to 0 */
433 return 1; /* don't call into driver */
436 return 0; /* let driver go ... */
440 int va_FoolBeginPicture(
443 VASurfaceID render_target
448 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx))
450 if (fool_context[idx].context == 0)
451 fool_context[idx].context = context;
452 return 1; /* don't call into driver level */
455 return 0; /* let driver go ... */
458 int va_FoolRenderPicture(
467 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx))
468 return 1; /* don't call into driver level */
470 return 0; /* let driver go ... */
474 int va_FoolEndPicture(
481 /* don't call into driver level */
483 /* do real fooling operation here */
485 /* only support H264 encoding currently */
486 if (FOOL_ENCODE(idx)) {
487 /* expect vaMapBuffer will handle it
488 * or else, need to save the codedbuf ID,
489 * and fool encode it here
491 /* va_FoolCodedBuf(dpy); */
492 return 1; /* don't call into driver level */
495 if (FOOL_DECODE(idx))
496 return 1; /* don't call into driver level */
498 return 0; /* let driver go ... */
501 int va_FoolSyncSurface(
503 VASurfaceID render_target)
506 /*Fill in black and white squares. */
507 if (FOOL_DECODE(idx) || FOOL_DECODE(idx))
516 VAStatus va_FoolUnmapBuffer (
518 VABufferID buf_id /* in */
523 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) { /* fool buffer creation */
529 VAStatus va_FoolQuerySubpictureFormats (
531 VAImageFormat *format_list,
533 unsigned int *num_formats
538 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) {