Update to VA API 0.26
[platform/upstream/libva.git] / test / test_11.c
1 /*
2  * Copyright (c) 2007 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 TEST_DESCRIPTION        "Map and unmap buffers"
26
27 #include "test_common.c"
28
29 VAConfigID config;
30 VAContextID context;
31 VASurfaceID *surfaces;
32 int total_surfaces;
33
34 void pre()
35 {
36     test_init();
37
38     va_status = vaCreateConfig(va_dpy, VAProfileMPEG2Main, VAEntrypointVLD, NULL, 0, &config);
39     ASSERT( VA_STATUS_SUCCESS == va_status );
40     status("vaCreateConfig returns %08x\n", config);
41
42     int width = 352;
43     int height = 288;
44     int surface_count = 4;
45     total_surfaces = surface_count;
46     
47     surfaces = malloc(total_surfaces * sizeof(VASurfaceID));
48
49     // TODO: Don't assume VA_RT_FORMAT_YUV420 is supported / needed for each config
50     va_status = vaCreateSurfaces(va_dpy, width, height, VA_RT_FORMAT_YUV420, total_surfaces, surfaces);
51     ASSERT( VA_STATUS_SUCCESS == va_status );
52     
53     status("vaCreateContext with config %08x\n", config);
54     int flags = 0;
55     va_status = vaCreateContext( va_dpy, config, width, height, flags, surfaces, surface_count, &context );
56     ASSERT( VA_STATUS_SUCCESS == va_status );
57 }
58
59 void test_unique_buffers(VABufferID *buffer_list, int buffer_count)
60 {
61     int i,j;
62     
63     for(i = 0; i < buffer_count; i++)
64     {
65         for(j = 0; j < i; j++)
66         {
67             ASSERT(buffer_list[i] != buffer_list[j]);
68         }
69     }
70 }
71
72 VABufferType buffer_types[] =
73 {
74   VAPictureParameterBufferType,
75   VAIQMatrixBufferType,
76   VABitPlaneBufferType,
77   VASliceGroupMapBufferType,
78   VASliceParameterBufferType,
79   VASliceDataBufferType,
80   VAMacroblockParameterBufferType,
81   VAResidualDataBufferType,
82   VADeblockingParameterBufferType,
83   VAImageBufferType
84 };
85
86 unsigned int buffer_sizes[] =
87 {
88   sizeof(VAPictureParameterBufferMPEG4), 
89   sizeof(VAIQMatrixBufferH264),
90   32*1024,
91   48*1024,
92   sizeof(VASliceParameterBufferMPEG2),
93   128*1024,
94   sizeof(VAMacroblockParameterBufferMPEG2),
95   32*1024,
96   15*1024,
97   32*1024,
98 };
99
100
101 #define NUM_BUFFER_TYPES        (sizeof(buffer_types) / sizeof(VABufferType))
102
103 #define DEAD_BUFFER_ID  ((VABufferID) 0x1234ffff)
104
105 void test()
106 {
107     VABufferID buffer_ids[NUM_BUFFER_TYPES+1];
108     uint32_t *input_data[NUM_BUFFER_TYPES];
109     int i, j;
110     memset(buffer_ids, 0xff, sizeof(buffer_ids));
111     for(i=0; i < NUM_BUFFER_TYPES; i++)
112     {
113         uint32_t *data;
114
115         input_data[i] = malloc(buffer_sizes[i]+4);
116         ASSERT(input_data[i]);
117         
118         /* Generate input data */
119         for(j = buffer_sizes[i] / 4; j--;)
120         {
121             input_data[i][j] = random();
122         }
123         
124         /* Copy to secondary buffer */
125         data = malloc(buffer_sizes[i]);
126         ASSERT(data);
127         memcpy(data, input_data[i], buffer_sizes[i]);
128
129         /* Create buffer and fill with data */
130         va_status = vaCreateBuffer(va_dpy, context, buffer_types[i], buffer_sizes[i], 1, data, &buffer_ids[i]);
131         ASSERT( VA_STATUS_SUCCESS == va_status );
132         status("vaCreateBuffer created buffer %08x of type %d\n", buffer_ids[i], buffer_types[i]);
133         
134         /* Wipe secondary buffer */
135         memset(data, 0, buffer_sizes[i]);
136         free(data);
137     }
138
139     for(i=0; i < NUM_BUFFER_TYPES; i++)
140     {
141         void *data = NULL;
142         /* Fetch VA Buffer */
143         va_status = vaMapBuffer(va_dpy, buffer_ids[i], &data);
144         ASSERT( VA_STATUS_SUCCESS == va_status );
145         status("vaMapBuffer mapped buffer %08x\n", buffer_ids[i]);
146
147         /* Compare data */        
148         ASSERT( memcmp(input_data[i], data, buffer_sizes[i]) == 0 );
149     }
150     
151     for(i=0; i < NUM_BUFFER_TYPES; i++)
152     {
153         va_status = vaUnmapBuffer(va_dpy, buffer_ids[i]);
154         ASSERT( VA_STATUS_SUCCESS == va_status );
155
156         va_status = vaDestroyBuffer(va_dpy, buffer_ids[i]);
157         ASSERT( VA_STATUS_SUCCESS == va_status );
158         
159         free(input_data[i]);
160     }
161 }
162
163
164
165 void post()
166 {
167     status("vaDestroyContext for context %08x\n", context);
168     va_status = vaDestroyContext( va_dpy, context );
169     ASSERT( VA_STATUS_SUCCESS == va_status );
170
171     status("vaDestroyConfig for config %08x\n", config);
172     va_status = vaDestroyConfig( va_dpy, config );
173     ASSERT( VA_STATUS_SUCCESS == va_status );
174     
175     va_status = vaDestroySurfaces(va_dpy, surfaces, total_surfaces);
176     ASSERT( VA_STATUS_SUCCESS == va_status );
177     
178     free(surfaces);
179
180     test_terminate();
181 }