OSDN Git Service

Detect short reads when filling fool buffers
[android-x86/hardware-intel-common-libva.git] / va / va_fool.c
1 /*
2  * Copyright (c) 2009 Intel Corporation. All Rights Reserved.
3  *
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:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
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.
23  */
24
25 #define _GNU_SOURCE 1
26 #include "sysdeps.h"
27 #include "va.h"
28 #include "va_backend.h"
29 #include "va_internal.h"
30 #include "va_trace.h"
31 #include "va_fool.h"
32
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <dlfcn.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <time.h>
45 #include <fcntl.h>
46
47 /*
48  * Do dummy decode/encode, ignore the input data
49  * In order to debug memory leak or low performance issues, we need to isolate driver problems
50  * We export env "VA_FOOL", with which, we can do fake decode/encode:
51  *
52  * LIBVA_FOOL_DECODE:
53  * . if set, decode does nothing
54  * LIBVA_FOOL_ENCODE=<framename>:
55  * . if set, encode does nothing, but fill in the coded buffer from the content of files with
56  *   name framename.0,framename.1,..., framename.N, framename.0,..., framename.N,...repeatly
57  *   Use file name to determine h264 or vp8
58  * LIBVA_FOOL_JPEG=<framename>:fill the content of filename to codedbuf for jpeg encoding
59  * LIBVA_FOOL_POSTP:
60  * . if set, do nothing for vaPutSurface
61  */
62
63
64 /* global settings */
65 int va_fool_codec = 0;
66 int va_fool_postp  = 0;
67
68 #define FOOL_BUFID_MAGIC   0x12345600
69 #define FOOL_BUFID_MASK    0xffffff00
70
71 struct fool_context {
72     int enabled; /* va_fool_codec is global, and it is for concurent encode/decode */
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 */
75     int file_count;
76
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 */
79
80     VAEntrypoint entrypoint; /* current entrypoint */
81     
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
86      */
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 */
91     VAContextID context;
92 };
93
94 #define FOOL_CTX(dpy) ((struct fool_context *)((VADisplayContextP)dpy)->vafool)
95
96 #define DPY2FOOLCTX(dpy)                                 \
97     struct fool_context *fool_ctx = FOOL_CTX(dpy);       \
98     if (fool_ctx == NULL)                                \
99         return 0; /* no fool for the context */          \
100
101 #define DPY2FOOLCTX_CHK(dpy)                             \
102     struct fool_context *fool_ctx = FOOL_CTX(dpy);       \
103     if ((fool_ctx == NULL) || (fool_ctx->enabled == 0))  \
104         return 0; /* no fool for the context */          \
105
106
107 void va_FoolInit(VADisplay dpy)
108 {
109     char env_value[1024];
110
111     struct fool_context *fool_ctx = calloc(sizeof(struct fool_context), 1);
112     
113     if (fool_ctx == NULL)
114         return;
115     
116     if (va_parseConfig("LIBVA_FOOL_POSTP", NULL) == 0) {
117         va_fool_postp = 1;
118         va_infoMessage(dpy, "LIBVA_FOOL_POSTP is on, dummy vaPutSurface\n");
119     }
120     
121     if (va_parseConfig("LIBVA_FOOL_DECODE", NULL) == 0) {
122         va_fool_codec  |= VA_FOOL_FLAG_DECODE;
123         va_infoMessage(dpy, "LIBVA_FOOL_DECODE is on, dummy decode\n");
124     }
125     if (va_parseConfig("LIBVA_FOOL_ENCODE", &env_value[0]) == 0) {
126         va_fool_codec  |= VA_FOOL_FLAG_ENCODE;
127         fool_ctx->fn_enc = strdup(env_value);
128         va_infoMessage(dpy, "LIBVA_FOOL_ENCODE is on, load encode data from file with patten %s\n",
129                        fool_ctx->fn_enc);
130     }
131     if (va_parseConfig("LIBVA_FOOL_JPEG", &env_value[0]) == 0) {
132         va_fool_codec  |= VA_FOOL_FLAG_JPEG;
133         fool_ctx->fn_jpg = strdup(env_value);
134         va_infoMessage(dpy, "LIBVA_FOOL_JPEG is on, load encode data from file with patten %s\n",
135                        fool_ctx->fn_jpg);
136     }
137     
138     ((VADisplayContextP)dpy)->vafool = fool_ctx;
139 }
140
141
142 int va_FoolEnd(VADisplay dpy)
143 {
144     int i;
145     DPY2FOOLCTX(dpy);
146
147     for (i = 0; i < VABufferTypeMax; i++) {/* free memory */
148         if (fool_ctx->fool_buf[i])
149             free(fool_ctx->fool_buf[i]);
150     }
151     if (fool_ctx->segbuf_enc)
152         free(fool_ctx->segbuf_enc);
153     if (fool_ctx->segbuf_jpg)
154         free(fool_ctx->segbuf_jpg);
155     if (fool_ctx->fn_enc)
156         free(fool_ctx->fn_enc);
157     if (fool_ctx->fn_jpg)
158         free(fool_ctx->fn_jpg);
159
160     free(fool_ctx);
161     ((VADisplayContextP)dpy)->vafool = NULL;
162     
163     return 0;
164 }
165
166 int va_FoolCreateConfig(
167         VADisplay dpy,
168         VAProfile profile, 
169         VAEntrypoint entrypoint, 
170         VAConfigAttrib *attrib_list,
171         int num_attribs,
172         VAConfigID *config_id /* out */
173 )
174 {
175     DPY2FOOLCTX(dpy);
176
177     fool_ctx->entrypoint = entrypoint;
178     
179     /*
180      * check va_fool_codec to align with current context
181      * e.g. va_fool_codec = decode then for encode, the
182      * vaBegin/vaRender/vaEnd also run into fool path
183      * which is not desired
184      */
185     if (((va_fool_codec & VA_FOOL_FLAG_DECODE) && (entrypoint == VAEntrypointVLD)) ||
186         ((va_fool_codec & VA_FOOL_FLAG_JPEG) && (entrypoint == VAEntrypointEncPicture)))
187         fool_ctx->enabled = 1;
188     else if ((va_fool_codec & VA_FOOL_FLAG_ENCODE) && (entrypoint == VAEntrypointEncSlice)) {
189         /* H264 is desired */
190         if (((profile == VAProfileH264Baseline ||
191               profile == VAProfileH264Main ||
192               profile == VAProfileH264High ||
193               profile == VAProfileH264ConstrainedBaseline)) &&
194             strstr(fool_ctx->fn_enc, "h264"))
195             fool_ctx->enabled = 1;
196
197         /* vp8 is desired */
198         if ((profile == VAProfileVP8Version0_3) &&
199             strstr(fool_ctx->fn_enc, "vp8"))
200             fool_ctx->enabled = 1;
201     }
202     if (fool_ctx->enabled)
203         va_infoMessage(dpy, "FOOL is enabled for this context\n");
204     else
205         va_infoMessage(dpy, "FOOL is not enabled for this context\n");
206
207     
208     return 0; /* continue */
209 }
210
211
212 VAStatus va_FoolCreateBuffer(
213     VADisplay dpy,
214     VAContextID context,        /* in */
215     VABufferType type,          /* in */
216     unsigned int size,          /* in */
217     unsigned int num_elements,  /* in */
218     void *data,                 /* in */
219     VABufferID *buf_id          /* out */
220 )
221 {
222     unsigned int new_size = size * num_elements;
223     unsigned int old_size;
224     DPY2FOOLCTX_CHK(dpy);
225
226     old_size = fool_ctx->fool_buf_size[type] * fool_ctx->fool_buf_element[type];
227
228     if (old_size < new_size)
229         fool_ctx->fool_buf[type] = realloc(fool_ctx->fool_buf[type], new_size);
230     
231     fool_ctx->fool_buf_size[type] = size;
232     fool_ctx->fool_buf_element[type] = num_elements;
233     fool_ctx->fool_buf_count[type]++;
234     /* because we ignore the vaRenderPicture, 
235      * all buffers with same type share same real memory
236      * bufferID = (magic number) | type
237      */
238     *buf_id = FOOL_BUFID_MAGIC | type;
239
240     return 1; /* don't call into driver */
241 }
242
243 VAStatus va_FoolBufferInfo(
244     VADisplay dpy,
245     VABufferID buf_id,  /* in */
246     VABufferType *type, /* out */
247     unsigned int *size,         /* out */
248     unsigned int *num_elements /* out */
249 )
250 {
251     unsigned int magic;
252     
253     DPY2FOOLCTX_CHK(dpy);
254
255     magic = buf_id & FOOL_BUFID_MASK;
256     if (magic != FOOL_BUFID_MAGIC)
257         return 0; /* could be VAImageBufferType from vaDeriveImage */
258     
259     *type = buf_id & 0xff;
260     *size = fool_ctx->fool_buf_size[*type];
261     *num_elements = fool_ctx->fool_buf_element[*type];;
262     
263     return 1; /* fool is valid */
264 }
265
266 static int va_FoolFillCodedBufEnc(struct fool_context *fool_ctx)
267 {
268     char file_name[1024];
269     struct stat file_stat = {0};
270     VACodedBufferSegment *codedbuf;
271     int i, fd = -1;
272     ssize_t ret;
273
274     /* try file_name.file_count, if fail, try file_name.file_count-- */
275     for (i=0; i<=1; i++) {
276         snprintf(file_name, 1024, "%s.%d",
277                  fool_ctx->fn_enc,
278                  fool_ctx->file_count);
279
280         if ((fd = open(file_name, O_RDONLY)) != -1) {
281             fstat(fd, &file_stat);
282             fool_ctx->file_count++; /* open next file */
283             break;
284         } else /* fall back to the first file file */
285             fool_ctx->file_count = 0;
286     }
287     if (fd != -1) {
288         fool_ctx->segbuf_enc = realloc(fool_ctx->segbuf_enc, file_stat.st_size);
289         ret = read(fd, fool_ctx->segbuf_enc, file_stat.st_size);
290         if (ret < file_stat.st_size)
291             va_errorMessage("Reading file %s failed.\n", file_name);
292         close(fd);
293     } else
294         va_errorMessage("Open file %s failed:%s\n", file_name, strerror(errno));
295
296     codedbuf = (VACodedBufferSegment *)fool_ctx->fool_buf[VAEncCodedBufferType];
297     codedbuf->size = file_stat.st_size;
298     codedbuf->bit_offset = 0;
299     codedbuf->status = 0;
300     codedbuf->reserved = 0;
301     codedbuf->buf = fool_ctx->segbuf_enc;
302     codedbuf->next = NULL;
303
304     return 0;
305 }
306
307 static int va_FoolFillCodedBufJPG(struct fool_context *fool_ctx)
308 {
309     struct stat file_stat = {0};
310     VACodedBufferSegment *codedbuf;
311     int fd = -1;
312     ssize_t ret;
313
314     if ((fd = open(fool_ctx->fn_jpg, O_RDONLY)) != -1) {
315         fstat(fd, &file_stat);
316         fool_ctx->segbuf_jpg = realloc(fool_ctx->segbuf_jpg, file_stat.st_size);
317         ret = read(fd, fool_ctx->segbuf_jpg, file_stat.st_size);
318         if (ret < file_stat.st_size)
319             va_errorMessage("Reading file %s failed.\n", fool_ctx->fn_jpg);
320         close(fd);
321     } else
322         va_errorMessage("Open file %s failed:%s\n", fool_ctx->fn_jpg, strerror(errno));
323
324     codedbuf = (VACodedBufferSegment *)fool_ctx->fool_buf[VAEncCodedBufferType];
325     codedbuf->size = file_stat.st_size;
326     codedbuf->bit_offset = 0;
327     codedbuf->status = 0;
328     codedbuf->reserved = 0;
329     codedbuf->buf = fool_ctx->segbuf_jpg;
330     codedbuf->next = NULL;
331
332     return 0;
333 }
334
335
336 static int va_FoolFillCodedBuf(struct fool_context *fool_ctx)
337 {
338     if (fool_ctx->entrypoint == VAEntrypointEncSlice)
339         va_FoolFillCodedBufEnc(fool_ctx);
340     else if (fool_ctx->entrypoint == VAEntrypointEncPicture)
341         va_FoolFillCodedBufJPG(fool_ctx);
342         
343     return 0;
344 }
345
346
347 VAStatus va_FoolMapBuffer(
348     VADisplay dpy,
349     VABufferID buf_id,  /* in */
350     void **pbuf         /* out */
351 )
352 {
353     unsigned int magic, buftype;
354     DPY2FOOLCTX_CHK(dpy);
355
356     magic = buf_id & FOOL_BUFID_MASK;
357     if (magic != FOOL_BUFID_MAGIC)
358         return 0; /* could be VAImageBufferType from vaDeriveImage */
359     
360     buftype = buf_id & 0xff;
361     *pbuf = fool_ctx->fool_buf[buftype];
362
363     /* it is coded buffer, fill coded segment from file */
364     if (*pbuf && (buftype == VAEncCodedBufferType))
365         va_FoolFillCodedBuf(fool_ctx);
366     
367     return 1; /* fool is valid */
368 }
369
370 VAStatus va_FoolCheckContinuity(VADisplay dpy)
371 {
372     DPY2FOOLCTX_CHK(dpy);
373
374     return 1; /* fool is valid */
375 }
376