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"
38 #include <sys/types.h>
44 * Do dummy decode/encode, ignore the input data
45 * In order to debug memory leak or low performance issues, we need to isolate driver problems
46 * We export env "VA_FOOL", with which, we can do fake decode/encode:
49 * . if set, decode does nothing, but fill in some YUV data
50 * LIBVA_FOOL_ENCODE=<clip name>:
51 * . if set, encode does nothing, but fill in the coded buffer from a H264 clip.
52 * . VA CONTEXT/CONFIG/SURFACE will call into drivers, but VA Buffer creation is done in here
53 * . Bypass all "vaBeginPic/vaRenderPic/vaEndPic"
55 * . if set, do nothing for vaPutSurface
61 /* LIBVA_FOOL_DECODE/LIBVA_FOOL_ENCODE/LIBVA_FOOL_POSTP */
68 #define NAL_BUF_SIZE 65536 // maximum NAL unit size
69 #define RING_BUF_SIZE 8192 // input ring buffer size, MUST be a power of two!
73 #define FOOL_CONTEXT_MAX 4
74 /* per context settings */
75 static struct _fool_context {
76 VADisplay dpy; /* should use context as the key */
78 VAProfile fool_profile; /* current profile for buffers */
79 VAEntrypoint fool_entrypoint; /* current entrypoint */
81 FILE *fool_fp_codedclip; /* load a clip from disk for fooling encode*/
84 /* all buffers with same type share one malloc-ed memory
85 * bufferID = (buffer numbers with the same type << 8) || type
86 * the malloc-ed memory can be find by fool_buf[bufferID & 0xff]
87 * the size is ignored here
89 char *fool_buf[VABufferTypeMax]; /* memory of fool buffers */
90 unsigned int fool_buf_size[VABufferTypeMax]; /* size of memory of fool buffers */
91 unsigned int fool_buf_count[VABufferTypeMax]; /* count of created buffers */
93 } fool_context[FOOL_CONTEXT_MAX] = { {0} }; /* trace five context at the same time */
95 #define FOOL_DECODE(idx) (fool_decode && (fool_context[idx].fool_entrypoint == VAEntrypointVLD))
96 #define FOOL_ENCODE(idx) \
98 && (fool_context[idx].fool_entrypoint == VAEntrypointEncSlice) \
99 && (fool_context[idx].fool_profile >= VAProfileH264Baseline) \
100 && (fool_context[idx].fool_profile <= VAProfileH264High))
104 #define DPY2INDEX(dpy) \
107 for (idx = 0; idx < FOOL_CONTEXT_MAX; idx++) \
108 if (fool_context[idx].dpy == dpy) \
111 if (idx == FOOL_CONTEXT_MAX) \
112 return 0; /* let driver go */
115 /* Prototype declarations (functions defined in va.c) */
117 void va_errorMessage(const char *msg, ...);
118 void va_infoMessage(const char *msg, ...);
120 int va_parseConfig(char *env, char *env_value);
122 VAStatus vaBufferInfo(
124 VAContextID context, /* in */
125 VABufferID buf_id, /* in */
126 VABufferType *type, /* out */
127 unsigned int *size, /* out */
128 unsigned int *num_elements /* out */
131 VAStatus vaLockSurface(VADisplay dpy,
133 unsigned int *fourcc, /* following are output argument */
134 unsigned int *luma_stride,
135 unsigned int *chroma_u_stride,
136 unsigned int *chroma_v_stride,
137 unsigned int *luma_offset,
138 unsigned int *chroma_u_offset,
139 unsigned int *chroma_v_offset,
140 unsigned int *buffer_name,
144 VAStatus vaUnlockSurface(VADisplay dpy,
149 void va_FoolInit(VADisplay dpy)
151 char env_value[1024];
154 for (fool_index = 0; fool_index < FOOL_CONTEXT_MAX; fool_index++)
155 if (fool_context[fool_index].dpy == 0)
158 if (fool_index == FOOL_CONTEXT_MAX)
161 if (va_parseConfig("LIBVA_FOOL_POSTP", NULL) == 0) {
163 va_infoMessage("LIBVA_FOOL_POSTP is on, dummy vaPutSurface\n");
167 if (va_parseConfig("LIBVA_FOOL_DECODE", NULL) == 0) {
169 va_infoMessage("LIBVA_FOOL_DECODE is on, dummy decode\n");
173 if (va_parseConfig("LIBVA_FOOL_ENCODE", &env_value[0]) == 0) {
174 fool_context[fool_index].fool_fp_codedclip = fopen(env_value, "r");
176 if (fool_context[fool_index].fool_fp_codedclip) {
181 if (fool_encode) /* malloc the buffer for fake clip */
182 fool_context[fool_index].frame_buf = malloc(MAX_FRAME*SLICE_NUM*NAL_BUF_SIZE*sizeof(char));
184 if (fool_context[fool_index].frame_buf == NULL)
188 va_infoMessage("LIBVA_FOOL_ENCODE is on, dummy encode\n");
191 if (fool_encode || fool_decode)
192 fool_context[fool_index].dpy = dpy;
196 int va_FoolEnd(VADisplay dpy)
202 for (i = 0; i < VABufferTypeMax; i++) {/* free memory */
203 if (fool_context[idx].fool_buf[i])
204 free(fool_context[idx].fool_buf[i]);
206 if (fool_context[idx].fool_fp_codedclip)
207 fclose(fool_context[idx].fool_fp_codedclip);
209 if (fool_context[idx].frame_buf)
210 free(fool_context[idx].frame_buf);
212 memset(&fool_context[idx], sizeof(struct _fool_context), 0);
216 int va_FoolCodedBuf(VADisplay dpy)
223 int va_FoolCreateConfig(
226 VAEntrypoint entrypoint,
227 VAConfigAttrib *attrib_list,
229 VAConfigID *config_id /* out */
234 /* call into driver level to allocate real context/surface/buffers, etc */
235 fool_context[idx].fool_profile = profile;
236 fool_context[idx].fool_entrypoint = entrypoint;
240 static int yuvgen_planar(
241 int width, int height,
242 unsigned char *Y_start, int Y_pitch,
243 unsigned char *U_start, int U_pitch,
244 unsigned char *V_start, int V_pitch,
245 int UV_interleave, int box_width, int row_shift,
252 for (row=0;row<height;row++) {
253 unsigned char *Y_row = Y_start + row * Y_pitch;
256 ypos = (row / box_width) & 0x1;
258 /* fill garbage data into the other field */
259 if (((field == VA_TOP_FIELD) && (row &1))
260 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
261 memset(Y_row, 0xff, width);
265 for (jj=0; jj<width; jj++) {
266 xpos = ((row_shift + jj) / box_width) & 0x1;
268 if ((xpos == 0) && (ypos == 0))
270 if ((xpos == 1) && (ypos == 1))
273 if ((xpos == 1) && (ypos == 0))
275 if ((xpos == 0) && (ypos == 1))
281 for( row =0; row < height/2; row++) {
282 unsigned short value = 0x80;
284 /* fill garbage data into the other field */
285 if (((field == VA_TOP_FIELD) && (row &1))
286 || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
291 unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);
293 memset(UV_row, value, width);
295 unsigned char *U_row = U_start + row * U_pitch;
296 unsigned char *V_row = V_start + row * V_pitch;
298 memset (U_row,value,width/2);
299 memset (V_row,value,width/2);
307 int va_FoolCreateSurfaces(
313 VASurfaceID *surfaces /* out */
317 unsigned int fourcc; /* following are output argument */
318 unsigned int luma_stride;
319 unsigned int chroma_u_stride;
320 unsigned int chroma_v_stride;
321 unsigned int luma_offset;
322 unsigned int chroma_u_offset;
323 unsigned int chroma_v_offset;
324 unsigned int buffer_name;
326 unsigned char *Y_data, *U_data, *V_data;
328 int box_width = num_surfaces/2;
334 if (FOOL_DECODE(idx)) {
335 /* call into driver level to allocate real context/surface/buffers, etc
336 * fill in the YUV data, will be overwrite if it is encode context
338 for (i = 0; i < num_surfaces; i++) {
339 /* fool decoder: fill with auto-generated YUV data */
340 va_status = vaLockSurface(dpy, surfaces[i], &fourcc,
341 &luma_stride, &chroma_u_stride, &chroma_v_stride,
342 &luma_offset, &chroma_u_offset, &chroma_v_offset,
343 &buffer_name, &buffer);
345 if (va_status != VA_STATUS_SUCCESS)
349 vaUnlockSurface(dpy, surfaces[i]);
355 /* UV should be same for NV12 */
356 U_data = buffer + chroma_u_offset;
357 V_data = buffer + chroma_v_offset;
359 yuvgen_planar(width, height,
361 U_data, chroma_v_stride,
362 V_data, chroma_v_stride,
363 (fourcc==VA_FOURCC_NV12),
364 box_width, row_shift, 0);
366 vaUnlockSurface(dpy, surfaces[i]);
369 if (row_shift==(2*box_width))
372 return 0; /* the return value is ignored */
374 return 0; /* the return value is ignored */
377 VAStatus va_FoolCreateBuffer (
379 VAContextID context, /* in */
380 VABufferType type, /* in */
381 unsigned int size, /* in */
382 unsigned int num_elements, /* in */
384 VABufferID *buf_id /* out */
389 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) { /* fool buffer creation */
390 int new_size = size * num_elements;
392 if (type == VAEncCodedBufferType) /* only a VACodedBufferSegment */
393 new_size = sizeof(VACodedBufferSegment);
395 if (fool_context[idx].fool_buf_size[type] == 0)
396 fool_context[idx].fool_buf[type] = calloc(1, new_size);
397 else if (fool_context[idx].fool_buf_size[type] <= new_size)
398 fool_context[idx].fool_buf[type] = realloc(fool_context[idx].fool_buf, new_size);
400 if (fool_context[idx].fool_buf[type] == NULL) {
402 return 0; /* let driver go */
405 /* because we ignore the vaRenderPicture,
406 * all buffers with same type share same real memory
407 * bufferID = (buffer count << 8) | type
409 fool_context[idx].fool_buf_count[type]++;
410 *buf_id = (fool_context[idx].fool_buf_count[type] << 8) | type;
412 return 1; /* don't call into driver */
415 return 0; /* let driver go ... */
418 VAStatus va_FoolMapBuffer (
420 VABufferID buf_id, /* in */
421 void **pbuf /* out */
426 unsigned int num_elements;
429 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) { /* fool buffer creation */
430 unsigned int buf_idx = buf_id & 0xff;
433 vaBufferInfo(dpy, fool_context[idx].context, buf_id, &type, &size, &num_elements);
434 if (type == VAImageBufferType && FOOL_ENCODE(idx))
437 /* buf_id is the buffer type */
438 if (fool_context[idx].fool_buf[buf_idx] != NULL)
439 *pbuf = fool_context[idx].fool_buf[buf_idx];
443 /* expect APP to MapBuffer when get the the coded data */
444 if (*pbuf && (buf_idx == VAEncCodedBufferType)) { /* it is coded buffer */
445 /* read from a clip */
446 va_FoolGetFrame(fool_context[idx].fool_fp_codedclip,
447 fool_context[idx].frame_buf);
448 *pbuf = fool_context[idx].frame_buf;
450 return 1; /* don't call into driver */
453 return 0; /* let driver go ... */
457 int va_FoolBeginPicture(
460 VASurfaceID render_target
465 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) {
466 if (fool_context[idx].context == 0)
467 fool_context[idx].context = context;
468 return 1; /* don't call into driver level */
471 return 0; /* let driver go ... */
474 int va_FoolRenderPicture(
483 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx))
484 return 1; /* don't call into driver level */
486 return 0; /* let driver go ... */
490 int va_FoolEndPicture(
497 /* don't call into driver level */
499 /* do real fooling operation here */
501 /* only support H264 encoding currently */
502 if (FOOL_ENCODE(idx)) {
503 /* expect vaMapBuffer will handle it
504 * or else, need to save the codedbuf ID,
505 * and fool encode it here
507 /* va_FoolCodedBuf(dpy); */
508 return 1; /* don't call into driver level */
511 if (FOOL_DECODE(idx))
512 return 1; /* don't call into driver level */
514 return 0; /* let driver go ... */
517 int va_FoolSyncSurface(
519 VASurfaceID render_target
524 /*Fill in black and white squares. */
525 if (FOOL_DECODE(idx) || FOOL_DECODE(idx))
532 VAStatus va_FoolUnmapBuffer(
534 VABufferID buf_id /* in */
539 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx))
540 return 1; /* fool buffer creation */
545 VAStatus va_FoolQuerySubpictureFormats(
547 VAImageFormat *format_list,
549 unsigned int *num_formats
554 if (FOOL_ENCODE(idx) || FOOL_DECODE(idx)) {