da9ca5aa6c9617dd34a9539c85f06f4291eef41f
[mesa.git] / src / gallium / state_trackers / va / picture.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_video_codec.h"
30
31 #include "util/u_handle_table.h"
32 #include "util/u_video.h"
33
34 #include "vl/vl_vlc.h"
35 #include "vl/vl_winsys.h"
36
37 #include "va_private.h"
38
39 VAStatus
40 vlVaBeginPicture(VADriverContextP ctx, VAContextID context_id, VASurfaceID render_target)
41 {
42 vlVaDriver *drv;
43 vlVaContext *context;
44 vlVaSurface *surf;
45
46 if (!ctx)
47 return VA_STATUS_ERROR_INVALID_CONTEXT;
48
49 drv = VL_VA_DRIVER(ctx);
50 if (!drv)
51 return VA_STATUS_ERROR_INVALID_CONTEXT;
52
53 context = handle_table_get(drv->htab, context_id);
54 if (!context)
55 return VA_STATUS_ERROR_INVALID_CONTEXT;
56
57 surf = handle_table_get(drv->htab, render_target);
58 if (!surf || !surf->buffer)
59 return VA_STATUS_ERROR_INVALID_SURFACE;
60
61 context->target = surf->buffer;
62
63 if (!context->decoder) {
64 /* VPP */
65 if (context->templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN &&
66 ((context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
67 context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
68 context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
69 context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM) ||
70 context->target->interlaced))
71 return VA_STATUS_ERROR_UNIMPLEMENTED;
72
73 return VA_STATUS_SUCCESS;
74 }
75
76 context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
77
78 return VA_STATUS_SUCCESS;
79 }
80
81 void
82 vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
83 struct pipe_video_buffer **ref_frame)
84 {
85 vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
86 if (surf)
87 *ref_frame = surf->buffer;
88 else
89 *ref_frame = NULL;
90 }
91
92 static VAStatus
93 handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
94 {
95 VAStatus vaStatus = VA_STATUS_SUCCESS;
96
97 switch (u_reduce_video_profile(context->templat.profile)) {
98 case PIPE_VIDEO_FORMAT_MPEG12:
99 vlVaHandlePictureParameterBufferMPEG12(drv, context, buf);
100 break;
101
102 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
103 vlVaHandlePictureParameterBufferH264(drv, context, buf);
104 break;
105
106 case PIPE_VIDEO_FORMAT_VC1:
107 vlVaHandlePictureParameterBufferVC1(drv, context, buf);
108 break;
109
110 case PIPE_VIDEO_FORMAT_MPEG4:
111 vlVaHandlePictureParameterBufferMPEG4(drv, context, buf);
112 break;
113
114 case PIPE_VIDEO_FORMAT_HEVC:
115 vlVaHandlePictureParameterBufferHEVC(drv, context, buf);
116 break;
117
118 default:
119 break;
120 }
121
122 /* Create the decoder once max_references is known. */
123 if (!context->decoder) {
124 if (!context->target)
125 return VA_STATUS_ERROR_INVALID_CONTEXT;
126
127 if (context->templat.max_references == 0)
128 return VA_STATUS_ERROR_INVALID_BUFFER;
129
130 if (u_reduce_video_profile(context->templat.profile) ==
131 PIPE_VIDEO_FORMAT_MPEG4_AVC)
132 context->templat.level = u_get_h264_level(context->templat.width,
133 context->templat.height, &context->templat.max_references);
134
135 context->decoder = drv->pipe->create_video_codec(drv->pipe,
136 &context->templat);
137
138 if (!context->decoder)
139 return VA_STATUS_ERROR_ALLOCATION_FAILED;
140
141 context->decoder->begin_frame(context->decoder, context->target,
142 &context->desc.base);
143 }
144
145 return vaStatus;
146 }
147
148 static void
149 handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
150 {
151 switch (u_reduce_video_profile(context->templat.profile)) {
152 case PIPE_VIDEO_FORMAT_MPEG12:
153 vlVaHandleIQMatrixBufferMPEG12(context, buf);
154 break;
155
156 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
157 vlVaHandleIQMatrixBufferH264(context, buf);
158 break;
159
160 case PIPE_VIDEO_FORMAT_MPEG4:
161 vlVaHandleIQMatrixBufferMPEG4(context, buf);
162 break;
163
164 case PIPE_VIDEO_FORMAT_HEVC:
165 vlVaHandleIQMatrixBufferHEVC(context, buf);
166 break;
167
168 default:
169 break;
170 }
171 }
172
173 static void
174 handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
175 {
176 switch (u_reduce_video_profile(context->templat.profile)) {
177 case PIPE_VIDEO_FORMAT_MPEG12:
178 vlVaHandleSliceParameterBufferMPEG12(context, buf);
179 break;
180
181 case PIPE_VIDEO_FORMAT_VC1:
182 vlVaHandleSliceParameterBufferVC1(context, buf);
183 break;
184
185 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
186 vlVaHandleSliceParameterBufferH264(context, buf);
187 break;
188
189 case PIPE_VIDEO_FORMAT_MPEG4:
190 vlVaHandleSliceParameterBufferMPEG4(context, buf);
191 break;
192
193 case PIPE_VIDEO_FORMAT_HEVC:
194 vlVaHandleSliceParameterBufferHEVC(context, buf);
195 break;
196
197 default:
198 break;
199 }
200 }
201
202 static unsigned int
203 bufHasStartcode(vlVaBuffer *buf, unsigned int code, unsigned int bits)
204 {
205 struct vl_vlc vlc = {0};
206 int i;
207
208 /* search the first 64 bytes for a startcode */
209 vl_vlc_init(&vlc, 1, (const void * const*)&buf->data, &buf->size);
210 for (i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= bits; ++i) {
211 if (vl_vlc_peekbits(&vlc, bits) == code)
212 return 1;
213 vl_vlc_eatbits(&vlc, 8);
214 vl_vlc_fillbits(&vlc);
215 }
216
217 return 0;
218 }
219
220 static void
221 handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
222 {
223 enum pipe_video_format format;
224 unsigned num_buffers = 0;
225 void * const *buffers[2];
226 unsigned sizes[2];
227 static const uint8_t start_code_h264[] = { 0x00, 0x00, 0x01 };
228 static const uint8_t start_code_h265[] = { 0x00, 0x00, 0x01 };
229 static const uint8_t start_code_vc1[] = { 0x00, 0x00, 0x01, 0x0d };
230
231 format = u_reduce_video_profile(context->templat.profile);
232 switch (format) {
233 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
234 if (bufHasStartcode(buf, 0x000001, 24))
235 break;
236
237 buffers[num_buffers] = (void *const)&start_code_h264;
238 sizes[num_buffers++] = sizeof(start_code_h264);
239 break;
240 case PIPE_VIDEO_FORMAT_HEVC:
241 if (bufHasStartcode(buf, 0x000001, 24))
242 break;
243
244 buffers[num_buffers] = (void *const)&start_code_h265;
245 sizes[num_buffers++] = sizeof(start_code_h265);
246 break;
247 case PIPE_VIDEO_FORMAT_VC1:
248 if (bufHasStartcode(buf, 0x0000010d, 32) ||
249 bufHasStartcode(buf, 0x0000010c, 32) ||
250 bufHasStartcode(buf, 0x0000010b, 32))
251 break;
252
253 if (context->decoder->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED) {
254 buffers[num_buffers] = (void *const)&start_code_vc1;
255 sizes[num_buffers++] = sizeof(start_code_vc1);
256 }
257 break;
258 case PIPE_VIDEO_FORMAT_MPEG4:
259 if (bufHasStartcode(buf, 0x000001, 24))
260 break;
261
262 vlVaDecoderFixMPEG4Startcode(context);
263 buffers[num_buffers] = (void *)context->mpeg4.start_code;
264 sizes[num_buffers++] = context->mpeg4.start_code_size;
265 default:
266 break;
267 }
268
269 buffers[num_buffers] = buf->data;
270 sizes[num_buffers] = buf->size;
271 ++num_buffers;
272 context->decoder->decode_bitstream(context->decoder, context->target, &context->desc.base,
273 num_buffers, (const void * const*)buffers, sizes);
274 }
275
276 VAStatus
277 vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buffers, int num_buffers)
278 {
279 vlVaDriver *drv;
280 vlVaContext *context;
281 VAStatus vaStatus = VA_STATUS_SUCCESS;
282
283 unsigned i;
284
285 if (!ctx)
286 return VA_STATUS_ERROR_INVALID_CONTEXT;
287
288 drv = VL_VA_DRIVER(ctx);
289 if (!drv)
290 return VA_STATUS_ERROR_INVALID_CONTEXT;
291
292 context = handle_table_get(drv->htab, context_id);
293 if (!context)
294 return VA_STATUS_ERROR_INVALID_CONTEXT;
295
296 for (i = 0; i < num_buffers; ++i) {
297 vlVaBuffer *buf = handle_table_get(drv->htab, buffers[i]);
298 if (!buf)
299 return VA_STATUS_ERROR_INVALID_BUFFER;
300
301 switch (buf->type) {
302 case VAPictureParameterBufferType:
303 vaStatus = handlePictureParameterBuffer(drv, context, buf);
304 break;
305
306 case VAIQMatrixBufferType:
307 handleIQMatrixBuffer(context, buf);
308 break;
309
310 case VASliceParameterBufferType:
311 handleSliceParameterBuffer(context, buf);
312 break;
313
314 case VASliceDataBufferType:
315 handleVASliceDataBufferType(context, buf);
316 break;
317 case VAProcPipelineParameterBufferType:
318 vaStatus = vlVaHandleVAProcPipelineParameterBufferType(drv, context, buf);
319 break;
320
321 default:
322 break;
323 }
324 }
325
326 return vaStatus;
327 }
328
329 VAStatus
330 vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
331 {
332 vlVaDriver *drv;
333 vlVaContext *context;
334
335 if (!ctx)
336 return VA_STATUS_ERROR_INVALID_CONTEXT;
337
338 drv = VL_VA_DRIVER(ctx);
339 if (!drv)
340 return VA_STATUS_ERROR_INVALID_CONTEXT;
341
342 context = handle_table_get(drv->htab, context_id);
343 if (!context)
344 return VA_STATUS_ERROR_INVALID_CONTEXT;
345
346 if (!context->decoder) {
347 if (context->templat.profile != PIPE_VIDEO_PROFILE_UNKNOWN)
348 return VA_STATUS_ERROR_INVALID_CONTEXT;
349
350 /* VPP */
351 return VA_STATUS_SUCCESS;
352 }
353
354 context->mpeg4.frame_num++;
355 context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
356
357 return VA_STATUS_SUCCESS;
358 }