1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include "pipe/p_video_codec.h"
31 #include "util/u_handle_table.h"
32 #include "util/u_video.h"
34 #include "vl/vl_vlc.h"
35 #include "vl/vl_winsys.h"
37 #include "va_private.h"
40 vlVaBeginPicture(VADriverContextP ctx
, VAContextID context_id
, VASurfaceID render_target
)
47 return VA_STATUS_ERROR_INVALID_CONTEXT
;
49 drv
= VL_VA_DRIVER(ctx
);
51 return VA_STATUS_ERROR_INVALID_CONTEXT
;
53 pipe_mutex_lock(drv
->mutex
);
54 context
= handle_table_get(drv
->htab
, context_id
);
56 pipe_mutex_unlock(drv
->mutex
);
57 return VA_STATUS_ERROR_INVALID_CONTEXT
;
60 surf
= handle_table_get(drv
->htab
, render_target
);
61 pipe_mutex_unlock(drv
->mutex
);
62 if (!surf
|| !surf
->buffer
)
63 return VA_STATUS_ERROR_INVALID_SURFACE
;
65 context
->target
= surf
->buffer
;
67 if (!context
->decoder
) {
70 if (context
->templat
.profile
== PIPE_VIDEO_PROFILE_UNKNOWN
&&
71 ((context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8A8_UNORM
&&
72 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8A8_UNORM
&&
73 context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8X8_UNORM
&&
74 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8X8_UNORM
) ||
75 context
->target
->interlaced
))
76 return VA_STATUS_ERROR_UNIMPLEMENTED
;
78 return VA_STATUS_SUCCESS
;
81 context
->decoder
->begin_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
83 return VA_STATUS_SUCCESS
;
87 vlVaGetReferenceFrame(vlVaDriver
*drv
, VASurfaceID surface_id
,
88 struct pipe_video_buffer
**ref_frame
)
90 vlVaSurface
*surf
= handle_table_get(drv
->htab
, surface_id
);
92 *ref_frame
= surf
->buffer
;
98 handlePictureParameterBuffer(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
100 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
102 switch (u_reduce_video_profile(context
->templat
.profile
)) {
103 case PIPE_VIDEO_FORMAT_MPEG12
:
104 vlVaHandlePictureParameterBufferMPEG12(drv
, context
, buf
);
107 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
108 vlVaHandlePictureParameterBufferH264(drv
, context
, buf
);
111 case PIPE_VIDEO_FORMAT_VC1
:
112 vlVaHandlePictureParameterBufferVC1(drv
, context
, buf
);
115 case PIPE_VIDEO_FORMAT_MPEG4
:
116 vlVaHandlePictureParameterBufferMPEG4(drv
, context
, buf
);
119 case PIPE_VIDEO_FORMAT_HEVC
:
120 vlVaHandlePictureParameterBufferHEVC(drv
, context
, buf
);
127 /* Create the decoder once max_references is known. */
128 if (!context
->decoder
) {
129 if (!context
->target
)
130 return VA_STATUS_ERROR_INVALID_CONTEXT
;
132 if (context
->templat
.max_references
== 0)
133 return VA_STATUS_ERROR_INVALID_BUFFER
;
135 if (u_reduce_video_profile(context
->templat
.profile
) ==
136 PIPE_VIDEO_FORMAT_MPEG4_AVC
)
137 context
->templat
.level
= u_get_h264_level(context
->templat
.width
,
138 context
->templat
.height
, &context
->templat
.max_references
);
140 context
->decoder
= drv
->pipe
->create_video_codec(drv
->pipe
,
143 if (!context
->decoder
)
144 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
146 context
->decoder
->begin_frame(context
->decoder
, context
->target
,
147 &context
->desc
.base
);
154 handleIQMatrixBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
156 switch (u_reduce_video_profile(context
->templat
.profile
)) {
157 case PIPE_VIDEO_FORMAT_MPEG12
:
158 vlVaHandleIQMatrixBufferMPEG12(context
, buf
);
161 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
162 vlVaHandleIQMatrixBufferH264(context
, buf
);
165 case PIPE_VIDEO_FORMAT_MPEG4
:
166 vlVaHandleIQMatrixBufferMPEG4(context
, buf
);
169 case PIPE_VIDEO_FORMAT_HEVC
:
170 vlVaHandleIQMatrixBufferHEVC(context
, buf
);
179 handleSliceParameterBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
181 switch (u_reduce_video_profile(context
->templat
.profile
)) {
182 case PIPE_VIDEO_FORMAT_MPEG12
:
183 vlVaHandleSliceParameterBufferMPEG12(context
, buf
);
186 case PIPE_VIDEO_FORMAT_VC1
:
187 vlVaHandleSliceParameterBufferVC1(context
, buf
);
190 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
191 vlVaHandleSliceParameterBufferH264(context
, buf
);
194 case PIPE_VIDEO_FORMAT_MPEG4
:
195 vlVaHandleSliceParameterBufferMPEG4(context
, buf
);
198 case PIPE_VIDEO_FORMAT_HEVC
:
199 vlVaHandleSliceParameterBufferHEVC(context
, buf
);
208 bufHasStartcode(vlVaBuffer
*buf
, unsigned int code
, unsigned int bits
)
210 struct vl_vlc vlc
= {0};
213 /* search the first 64 bytes for a startcode */
214 vl_vlc_init(&vlc
, 1, (const void * const*)&buf
->data
, &buf
->size
);
215 for (i
= 0; i
< 64 && vl_vlc_bits_left(&vlc
) >= bits
; ++i
) {
216 if (vl_vlc_peekbits(&vlc
, bits
) == code
)
218 vl_vlc_eatbits(&vlc
, 8);
219 vl_vlc_fillbits(&vlc
);
226 handleVASliceDataBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
228 enum pipe_video_format format
;
229 unsigned num_buffers
= 0;
230 void * const *buffers
[2];
232 static const uint8_t start_code_h264
[] = { 0x00, 0x00, 0x01 };
233 static const uint8_t start_code_h265
[] = { 0x00, 0x00, 0x01 };
234 static const uint8_t start_code_vc1
[] = { 0x00, 0x00, 0x01, 0x0d };
236 format
= u_reduce_video_profile(context
->templat
.profile
);
238 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
239 if (bufHasStartcode(buf
, 0x000001, 24))
242 buffers
[num_buffers
] = (void *const)&start_code_h264
;
243 sizes
[num_buffers
++] = sizeof(start_code_h264
);
245 case PIPE_VIDEO_FORMAT_HEVC
:
246 if (bufHasStartcode(buf
, 0x000001, 24))
249 buffers
[num_buffers
] = (void *const)&start_code_h265
;
250 sizes
[num_buffers
++] = sizeof(start_code_h265
);
252 case PIPE_VIDEO_FORMAT_VC1
:
253 if (bufHasStartcode(buf
, 0x0000010d, 32) ||
254 bufHasStartcode(buf
, 0x0000010c, 32) ||
255 bufHasStartcode(buf
, 0x0000010b, 32))
258 if (context
->decoder
->profile
== PIPE_VIDEO_PROFILE_VC1_ADVANCED
) {
259 buffers
[num_buffers
] = (void *const)&start_code_vc1
;
260 sizes
[num_buffers
++] = sizeof(start_code_vc1
);
263 case PIPE_VIDEO_FORMAT_MPEG4
:
264 if (bufHasStartcode(buf
, 0x000001, 24))
267 vlVaDecoderFixMPEG4Startcode(context
);
268 buffers
[num_buffers
] = (void *)context
->mpeg4
.start_code
;
269 sizes
[num_buffers
++] = context
->mpeg4
.start_code_size
;
274 buffers
[num_buffers
] = buf
->data
;
275 sizes
[num_buffers
] = buf
->size
;
277 context
->decoder
->decode_bitstream(context
->decoder
, context
->target
, &context
->desc
.base
,
278 num_buffers
, (const void * const*)buffers
, sizes
);
282 vlVaRenderPicture(VADriverContextP ctx
, VAContextID context_id
, VABufferID
*buffers
, int num_buffers
)
285 vlVaContext
*context
;
286 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
291 return VA_STATUS_ERROR_INVALID_CONTEXT
;
293 drv
= VL_VA_DRIVER(ctx
);
295 return VA_STATUS_ERROR_INVALID_CONTEXT
;
297 pipe_mutex_lock(drv
->mutex
);
298 context
= handle_table_get(drv
->htab
, context_id
);
300 pipe_mutex_unlock(drv
->mutex
);
301 return VA_STATUS_ERROR_INVALID_CONTEXT
;
304 for (i
= 0; i
< num_buffers
; ++i
) {
305 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, buffers
[i
]);
307 pipe_mutex_unlock(drv
->mutex
);
308 return VA_STATUS_ERROR_INVALID_BUFFER
;
312 case VAPictureParameterBufferType
:
313 vaStatus
= handlePictureParameterBuffer(drv
, context
, buf
);
316 case VAIQMatrixBufferType
:
317 handleIQMatrixBuffer(context
, buf
);
320 case VASliceParameterBufferType
:
321 handleSliceParameterBuffer(context
, buf
);
324 case VASliceDataBufferType
:
325 handleVASliceDataBufferType(context
, buf
);
327 case VAProcPipelineParameterBufferType
:
328 vaStatus
= vlVaHandleVAProcPipelineParameterBufferType(drv
, context
, buf
);
335 pipe_mutex_unlock(drv
->mutex
);
341 vlVaEndPicture(VADriverContextP ctx
, VAContextID context_id
)
344 vlVaContext
*context
;
347 return VA_STATUS_ERROR_INVALID_CONTEXT
;
349 drv
= VL_VA_DRIVER(ctx
);
351 return VA_STATUS_ERROR_INVALID_CONTEXT
;
353 pipe_mutex_lock(drv
->mutex
);
354 context
= handle_table_get(drv
->htab
, context_id
);
355 pipe_mutex_unlock(drv
->mutex
);
357 return VA_STATUS_ERROR_INVALID_CONTEXT
;
359 if (!context
->decoder
) {
360 if (context
->templat
.profile
!= PIPE_VIDEO_PROFILE_UNKNOWN
)
361 return VA_STATUS_ERROR_INVALID_CONTEXT
;
364 return VA_STATUS_SUCCESS
;
367 context
->mpeg4
.frame_num
++;
368 context
->decoder
->end_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
370 return VA_STATUS_SUCCESS
;