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 * LIBVA_FOOL_JPEG=<framename>:fill the content of filename to codedbuf for jpeg encoding
57 * . if set, do nothing for vaPutSurface
65 #define FOOL_CONTEXT_MAX 4
67 #define FOOL_BUFID_MAGIC 0x12345600
68 #define FOOL_BUFID_MASK 0xffffff00
69 /* per context settings */
70 static struct _fool_context {
71 VADisplay dpy; /* should use context as the key */
73 char *fn_enc;/* file pattern with codedbuf content for encode */
74 char *segbuf_enc; /* the segment buffer of coded buffer, load frome fn_enc */
77 char *fn_jpg;/* file name of JPEG fool with codedbuf content */
78 char *segbuf_jpg; /* the segment buffer of coded buffer, load frome fn_jpg */
80 VAEntrypoint entrypoint; /* current entrypoint */
82 /* all buffers with same type share one malloc-ed memory
83 * bufferID = (buffer numbers with the same type << 8) || type
84 * the malloc-ed memory can be find by fool_buf[bufferID & 0xff]
85 * the size is ignored here
87 char *fool_buf[VABufferTypeMax]; /* memory of fool buffers */
88 unsigned int fool_buf_size[VABufferTypeMax]; /* size of memory of fool buffers */
89 unsigned int fool_buf_element[VABufferTypeMax]; /* element count of created buffers */
90 unsigned int fool_buf_count[VABufferTypeMax]; /* count of created buffers */
92 } fool_context[FOOL_CONTEXT_MAX]; /* trace five context at the same time */
94 #define DPY2INDEX(dpy) \
97 for (idx = 0; idx < FOOL_CONTEXT_MAX; idx++) \
98 if (fool_context[idx].dpy == dpy) \
101 if (idx == FOOL_CONTEXT_MAX) \
102 return 0; /* let driver go */
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];
116 for (fool_index = 0; fool_index < FOOL_CONTEXT_MAX; fool_index++)
117 if (fool_context[fool_index].dpy == 0)
120 if (fool_index == FOOL_CONTEXT_MAX)
123 memset(&fool_context[fool_index], 0, sizeof(struct _fool_context));
124 if (va_parseConfig("LIBVA_FOOL_POSTP", NULL) == 0) {
126 va_infoMessage("LIBVA_FOOL_POSTP is on, dummy vaPutSurface\n");
129 if (va_parseConfig("LIBVA_FOOL_DECODE", NULL) == 0) {
130 fool_codec |= VA_FOOL_FLAG_DECODE;
131 va_infoMessage("LIBVA_FOOL_DECODE is on, dummy decode\n");
133 if (va_parseConfig("LIBVA_FOOL_ENCODE", &env_value[0]) == 0) {
134 fool_codec |= VA_FOOL_FLAG_ENCODE;
135 fool_context[fool_index].fn_enc = strdup(env_value);
136 va_infoMessage("LIBVA_FOOL_ENCODE is on, load encode data from file with patten %s\n",
137 fool_context[fool_index].fn_enc);
139 if (va_parseConfig("LIBVA_FOOL_JPEG", &env_value[0]) == 0) {
140 fool_codec |= VA_FOOL_FLAG_JPEG;
141 fool_context[fool_index].fn_jpg = strdup(env_value);
142 va_infoMessage("LIBVA_FOOL_JPEG is on, load encode data from file with patten %s\n",
143 fool_context[fool_index].fn_jpg);
147 fool_context[fool_index].dpy = dpy;
151 int va_FoolEnd(VADisplay dpy)
156 for (i = 0; i < VABufferTypeMax; i++) {/* free memory */
157 if (fool_context[idx].fool_buf[i])
158 free(fool_context[idx].fool_buf[i]);
160 if (fool_context[idx].segbuf_enc)
161 free(fool_context[idx].segbuf_enc);
162 if (fool_context[idx].segbuf_jpg)
163 free(fool_context[idx].segbuf_jpg);
164 if (fool_context[idx].fn_enc)
165 free(fool_context[idx].fn_enc);
166 if (fool_context[idx].fn_jpg)
167 free(fool_context[idx].fn_jpg);
169 memset(&fool_context[idx], 0, sizeof(struct _fool_context));
175 int va_FoolCreateConfig(
178 VAEntrypoint entrypoint,
179 VAConfigAttrib *attrib_list,
181 VAConfigID *config_id /* out */
186 fool_context[idx].entrypoint = entrypoint;
189 * check fool_codec to align with current context
190 * e.g. fool_codec = decode then for encode, the
191 * vaBegin/vaRender/vaEnd also run into fool path
192 * which is not desired
194 if (((fool_codec & VA_FOOL_FLAG_DECODE) && (entrypoint == VAEntrypointVLD)) ||
195 ((fool_codec & VA_FOOL_FLAG_ENCODE) && (entrypoint == VAEntrypointEncSlice)) ||
196 ((fool_codec & VA_FOOL_FLAG_JPEG) && (entrypoint == VAEntrypointEncPicture)))
197 ; /* the fool_codec is meaningful */
201 return 0; /* driver continue */
205 VAStatus va_FoolCreateBuffer(
207 VAContextID context, /* in */
208 VABufferType type, /* in */
209 unsigned int size, /* in */
210 unsigned int num_elements, /* in */
212 VABufferID *buf_id /* out */
215 unsigned int new_size = size * num_elements;
216 unsigned int old_size;
219 old_size = fool_context[idx].fool_buf_size[type] * fool_context[idx].fool_buf_element[type];
221 if (old_size < new_size)
222 fool_context[idx].fool_buf[type] = realloc(fool_context[idx].fool_buf[type], new_size);
224 fool_context[idx].fool_buf_size[type] = size;
225 fool_context[idx].fool_buf_element[type] = num_elements;
226 fool_context[idx].fool_buf_count[type]++;
227 /* because we ignore the vaRenderPicture,
228 * all buffers with same type share same real memory
229 * bufferID = (magic number) | type
231 *buf_id = FOOL_BUFID_MAGIC | type;
233 return 1; /* don't call into driver */
236 VAStatus va_FoolBufferInfo(
238 VABufferID buf_id, /* in */
239 VABufferType *type, /* out */
240 unsigned int *size, /* out */
241 unsigned int *num_elements /* out */
244 unsigned int magic = buf_id & FOOL_BUFID_MASK;
247 if (magic != FOOL_BUFID_MAGIC)
250 *type = buf_id & 0xff;
251 *size = fool_context[idx].fool_buf_size[*type];
252 *num_elements = fool_context[idx].fool_buf_element[*type];;
254 return 1; /* don't call into driver */
257 static int va_FoolFillCodedBufEnc(int idx)
259 char file_name[1024];
260 struct stat file_stat = {0};
261 VACodedBufferSegment *codedbuf;
264 /* try file_name.file_count, if fail, try file_name.file_count-- */
265 for (i=0; i<=1; i++) {
266 snprintf(file_name, 1024, "%s.%d",
267 fool_context[idx].fn_enc,
268 fool_context[idx].file_count);
270 if ((fd = open(file_name, O_RDONLY)) != -1) {
271 fstat(fd, &file_stat);
272 fool_context[idx].file_count++; /* open next file */
274 } else /* fall back to the first file file */
275 fool_context[idx].file_count = 0;
278 fool_context[idx].segbuf_enc = realloc(fool_context[idx].segbuf_enc, file_stat.st_size);
279 read(fd, fool_context[idx].segbuf_enc, file_stat.st_size);
282 va_errorMessage("Open file %s failed:%s\n", file_name, strerror(errno));
284 codedbuf = (VACodedBufferSegment *)fool_context[idx].fool_buf[VAEncCodedBufferType];
285 codedbuf->size = file_stat.st_size;
286 codedbuf->bit_offset = 0;
287 codedbuf->status = 0;
288 codedbuf->reserved = 0;
289 codedbuf->buf = fool_context[idx].segbuf_enc;
290 codedbuf->next = NULL;
296 static int va_FoolFillCodedBufJPG(int idx)
298 struct stat file_stat = {0};
299 VACodedBufferSegment *codedbuf;
302 if ((fd = open(fool_context[idx].fn_jpg, O_RDONLY)) != -1) {
303 fstat(fd, &file_stat);
304 fool_context[idx].segbuf_jpg = realloc(fool_context[idx].segbuf_jpg, file_stat.st_size);
305 read(fd, fool_context[idx].segbuf_jpg, file_stat.st_size);
308 va_errorMessage("Open file %s failed:%s\n", fool_context[idx].fn_jpg, strerror(errno));
310 codedbuf = (VACodedBufferSegment *)fool_context[idx].fool_buf[VAEncCodedBufferType];
311 codedbuf->size = file_stat.st_size;
312 codedbuf->bit_offset = 0;
313 codedbuf->status = 0;
314 codedbuf->reserved = 0;
315 codedbuf->buf = fool_context[idx].segbuf_jpg;
316 codedbuf->next = NULL;
322 static int va_FoolFillCodedBuf(int idx)
324 if (fool_context[idx].entrypoint == VAEntrypointEncSlice)
325 va_FoolFillCodedBufEnc(idx);
326 else if (fool_context[idx].entrypoint == VAEntrypointEncPicture)
327 va_FoolFillCodedBufJPG(idx);
333 VAStatus va_FoolMapBuffer(
335 VABufferID buf_id, /* in */
336 void **pbuf /* out */
339 unsigned int buftype = buf_id & 0xff;
340 unsigned int magic = buf_id & FOOL_BUFID_MASK;
343 if (magic != FOOL_BUFID_MAGIC || buftype >= VABufferTypeMax || !pbuf)
346 /* buf_id is the buffer type */
347 *pbuf = fool_context[idx].fool_buf[buftype];
349 /* it is coded buffer, fill the fake segment buf from file */
350 if (*pbuf && (buftype == VAEncCodedBufferType))
351 va_FoolFillCodedBuf(idx);
353 return 1; /* don't call into driver */
356 VAStatus va_FoolUnmapBuffer(
358 VABufferID buf_id /* in */
361 unsigned int magic = buf_id & FOOL_BUFID_MASK;
363 if (magic != FOOL_BUFID_MAGIC)