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"
39 #include <sys/types.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
52 * LIBVA_FOOL_ENCODE=<framename>:
53 * . if set, encode does nothing, but fill in the coded buffer from the content of files with
54 * name framename.0,framename.1,..., framename.N, framename.0,..., framename.N,...repeatly
55 * Use file name to determine h264 or vp8
56 * LIBVA_FOOL_JPEG=<framename>:fill the content of filename to codedbuf for jpeg encoding
58 * . if set, do nothing for vaPutSurface
66 #define FOOL_BUFID_MAGIC 0x12345600
67 #define FOOL_BUFID_MASK 0xffffff00
70 int enabled; /* fool_codec is global, and it is for concurent encode/decode */
71 char *fn_enc;/* file pattern with codedbuf content for encode */
72 char *segbuf_enc; /* the segment buffer of coded buffer, load frome fn_enc */
75 char *fn_jpg;/* file name of JPEG fool with codedbuf content */
76 char *segbuf_jpg; /* the segment buffer of coded buffer, load frome fn_jpg */
78 VAEntrypoint entrypoint; /* current entrypoint */
80 /* all buffers with same type share one malloc-ed memory
81 * bufferID = (buffer numbers with the same type << 8) || type
82 * the malloc-ed memory can be find by fool_buf[bufferID & 0xff]
83 * the size is ignored here
85 char *fool_buf[VABufferTypeMax]; /* memory of fool buffers */
86 unsigned int fool_buf_size[VABufferTypeMax]; /* size of memory of fool buffers */
87 unsigned int fool_buf_element[VABufferTypeMax]; /* element count of created buffers */
88 unsigned int fool_buf_count[VABufferTypeMax]; /* count of created buffers */
92 #define FOOL_CTX(dpy) ((struct fool_context *)((VADisplayContextP)dpy)->vafool)
94 #define DPY2FOOLCTX(dpy) \
95 struct fool_context *fool_ctx = FOOL_CTX(dpy); \
96 if (fool_ctx == NULL) \
97 return 0; /* no fool for the context */ \
99 #define DPY2FOOLCTX_CHK(dpy) \
100 struct fool_context *fool_ctx = FOOL_CTX(dpy); \
101 if ((fool_ctx == NULL) || (fool_ctx->enabled == 0)) \
102 return 0; /* no fool for the context */ \
104 /* Prototype declarations (functions defined in va.c) */
106 void va_errorMessage(const char *msg, ...);
107 void va_infoMessage(const char *msg, ...);
109 int va_parseConfig(char *env, char *env_value);
111 void va_FoolInit(VADisplay dpy)
113 char env_value[1024];
115 struct fool_context *fool_ctx = calloc(sizeof(struct fool_context), 1);
117 if (fool_ctx == NULL)
120 if (va_parseConfig("LIBVA_FOOL_POSTP", NULL) == 0) {
122 va_infoMessage("LIBVA_FOOL_POSTP is on, dummy vaPutSurface\n");
125 if (va_parseConfig("LIBVA_FOOL_DECODE", NULL) == 0) {
126 fool_codec |= VA_FOOL_FLAG_DECODE;
127 va_infoMessage("LIBVA_FOOL_DECODE is on, dummy decode\n");
129 if (va_parseConfig("LIBVA_FOOL_ENCODE", &env_value[0]) == 0) {
130 fool_codec |= VA_FOOL_FLAG_ENCODE;
131 fool_ctx->fn_enc = strdup(env_value);
132 va_infoMessage("LIBVA_FOOL_ENCODE is on, load encode data from file with patten %s\n",
135 if (va_parseConfig("LIBVA_FOOL_JPEG", &env_value[0]) == 0) {
136 fool_codec |= VA_FOOL_FLAG_JPEG;
137 fool_ctx->fn_jpg = strdup(env_value);
138 va_infoMessage("LIBVA_FOOL_JPEG is on, load encode data from file with patten %s\n",
142 ((VADisplayContextP)dpy)->vafool = fool_ctx;
146 int va_FoolEnd(VADisplay dpy)
151 for (i = 0; i < VABufferTypeMax; i++) {/* free memory */
152 if (fool_ctx->fool_buf[i])
153 free(fool_ctx->fool_buf[i]);
155 if (fool_ctx->segbuf_enc)
156 free(fool_ctx->segbuf_enc);
157 if (fool_ctx->segbuf_jpg)
158 free(fool_ctx->segbuf_jpg);
159 if (fool_ctx->fn_enc)
160 free(fool_ctx->fn_enc);
161 if (fool_ctx->fn_jpg)
162 free(fool_ctx->fn_jpg);
165 ((VADisplayContextP)dpy)->vafool = NULL;
170 int va_FoolCreateConfig(
173 VAEntrypoint entrypoint,
174 VAConfigAttrib *attrib_list,
176 VAConfigID *config_id /* out */
181 fool_ctx->entrypoint = entrypoint;
184 * check fool_codec to align with current context
185 * e.g. fool_codec = decode then for encode, the
186 * vaBegin/vaRender/vaEnd also run into fool path
187 * which is not desired
189 if (((fool_codec & VA_FOOL_FLAG_DECODE) && (entrypoint == VAEntrypointVLD)) ||
190 ((fool_codec & VA_FOOL_FLAG_JPEG) && (entrypoint == VAEntrypointEncPicture)))
191 fool_ctx->enabled = 1;
192 else if ((fool_codec & VA_FOOL_FLAG_ENCODE) && (entrypoint == VAEntrypointEncSlice)) {
193 /* H264 is desired */
194 if (((profile == VAProfileH264Baseline ||
195 profile == VAProfileH264Main ||
196 profile == VAProfileH264High ||
197 profile == VAProfileH264ConstrainedBaseline)) &&
198 strstr(fool_ctx->fn_enc, "h264"))
199 fool_ctx->enabled = 1;
202 if ((profile == VAProfileVP8Version0_3) &&
203 strstr(fool_ctx->fn_enc, "vp8"))
204 fool_ctx->enabled = 1;
206 if (fool_ctx->enabled)
207 va_infoMessage("FOOL is enabled for this context\n");
209 va_infoMessage("FOOL is not enabled for this context\n");
212 return 0; /* continue */
216 VAStatus va_FoolCreateBuffer(
218 VAContextID context, /* in */
219 VABufferType type, /* in */
220 unsigned int size, /* in */
221 unsigned int num_elements, /* in */
223 VABufferID *buf_id /* out */
226 unsigned int new_size = size * num_elements;
227 unsigned int old_size;
228 DPY2FOOLCTX_CHK(dpy);
230 old_size = fool_ctx->fool_buf_size[type] * fool_ctx->fool_buf_element[type];
232 if (old_size < new_size)
233 fool_ctx->fool_buf[type] = realloc(fool_ctx->fool_buf[type], new_size);
235 fool_ctx->fool_buf_size[type] = size;
236 fool_ctx->fool_buf_element[type] = num_elements;
237 fool_ctx->fool_buf_count[type]++;
238 /* because we ignore the vaRenderPicture,
239 * all buffers with same type share same real memory
240 * bufferID = (magic number) | type
242 *buf_id = FOOL_BUFID_MAGIC | type;
244 return 1; /* don't call into driver */
247 VAStatus va_FoolBufferInfo(
249 VABufferID buf_id, /* in */
250 VABufferType *type, /* out */
251 unsigned int *size, /* out */
252 unsigned int *num_elements /* out */
255 DPY2FOOLCTX_CHK(dpy);
257 *type = buf_id & 0xff;
258 *size = fool_ctx->fool_buf_size[*type];
259 *num_elements = fool_ctx->fool_buf_element[*type];;
261 return 1; /* fool is valid */
264 static int va_FoolFillCodedBufEnc(struct fool_context *fool_ctx)
266 char file_name[1024];
267 struct stat file_stat = {0};
268 VACodedBufferSegment *codedbuf;
271 /* try file_name.file_count, if fail, try file_name.file_count-- */
272 for (i=0; i<=1; i++) {
273 snprintf(file_name, 1024, "%s.%d",
275 fool_ctx->file_count);
277 if ((fd = open(file_name, O_RDONLY)) != -1) {
278 fstat(fd, &file_stat);
279 fool_ctx->file_count++; /* open next file */
281 } else /* fall back to the first file file */
282 fool_ctx->file_count = 0;
285 fool_ctx->segbuf_enc = realloc(fool_ctx->segbuf_enc, file_stat.st_size);
286 read(fd, fool_ctx->segbuf_enc, file_stat.st_size);
289 va_errorMessage("Open file %s failed:%s\n", file_name, strerror(errno));
291 codedbuf = (VACodedBufferSegment *)fool_ctx->fool_buf[VAEncCodedBufferType];
292 codedbuf->size = file_stat.st_size;
293 codedbuf->bit_offset = 0;
294 codedbuf->status = 0;
295 codedbuf->reserved = 0;
296 codedbuf->buf = fool_ctx->segbuf_enc;
297 codedbuf->next = NULL;
302 static int va_FoolFillCodedBufJPG(struct fool_context *fool_ctx)
304 struct stat file_stat = {0};
305 VACodedBufferSegment *codedbuf;
308 if ((fd = open(fool_ctx->fn_jpg, O_RDONLY)) != -1) {
309 fstat(fd, &file_stat);
310 fool_ctx->segbuf_jpg = realloc(fool_ctx->segbuf_jpg, file_stat.st_size);
311 read(fd, fool_ctx->segbuf_jpg, file_stat.st_size);
314 va_errorMessage("Open file %s failed:%s\n", fool_ctx->fn_jpg, strerror(errno));
316 codedbuf = (VACodedBufferSegment *)fool_ctx->fool_buf[VAEncCodedBufferType];
317 codedbuf->size = file_stat.st_size;
318 codedbuf->bit_offset = 0;
319 codedbuf->status = 0;
320 codedbuf->reserved = 0;
321 codedbuf->buf = fool_ctx->segbuf_jpg;
322 codedbuf->next = NULL;
328 static int va_FoolFillCodedBuf(struct fool_context *fool_ctx)
330 if (fool_ctx->entrypoint == VAEntrypointEncSlice)
331 va_FoolFillCodedBufEnc(fool_ctx);
332 else if (fool_ctx->entrypoint == VAEntrypointEncPicture)
333 va_FoolFillCodedBufJPG(fool_ctx);
339 VAStatus va_FoolMapBuffer(
341 VABufferID buf_id, /* in */
342 void **pbuf /* out */
345 unsigned int buftype;
346 DPY2FOOLCTX_CHK(dpy);
348 buftype = buf_id & 0xff;
349 *pbuf = fool_ctx->fool_buf[buftype];
351 /* it is coded buffer, fill coded segment from file */
352 if (*pbuf && (buftype == VAEncCodedBufferType))
353 va_FoolFillCodedBuf(fool_ctx);
355 return 1; /* fool is valid */
358 VAStatus va_FoolCheckContinuity(VADisplay dpy)
360 DPY2FOOLCTX_CHK(dpy);
362 return 1; /* fool is valid */