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 context
= handle_table_get(drv
->htab
, context_id
);
55 return VA_STATUS_ERROR_INVALID_CONTEXT
;
57 surf
= handle_table_get(drv
->htab
, render_target
);
58 if (!surf
|| !surf
->buffer
)
59 return VA_STATUS_ERROR_INVALID_SURFACE
;
61 context
->target
= surf
->buffer
;
63 if (!context
->decoder
) {
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
;
73 return VA_STATUS_SUCCESS
;
76 context
->decoder
->begin_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
78 return VA_STATUS_SUCCESS
;
82 vlVaGetReferenceFrame(vlVaDriver
*drv
, VASurfaceID surface_id
,
83 struct pipe_video_buffer
**ref_frame
)
85 vlVaSurface
*surf
= handle_table_get(drv
->htab
, surface_id
);
87 *ref_frame
= surf
->buffer
;
93 handlePictureParameterBuffer(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
95 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
97 switch (u_reduce_video_profile(context
->templat
.profile
)) {
98 case PIPE_VIDEO_FORMAT_MPEG12
:
99 vlVaHandlePictureParameterBufferMPEG12(drv
, context
, buf
);
102 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
103 vlVaHandlePictureParameterBufferH264(drv
, context
, buf
);
106 case PIPE_VIDEO_FORMAT_VC1
:
107 vlVaHandlePictureParameterBufferVC1(drv
, context
, buf
);
110 case PIPE_VIDEO_FORMAT_MPEG4
:
111 vlVaHandlePictureParameterBufferMPEG4(drv
, context
, buf
);
114 case PIPE_VIDEO_FORMAT_HEVC
:
115 vlVaHandlePictureParameterBufferHEVC(drv
, context
, buf
);
122 /* Create the decoder once max_references is known. */
123 if (!context
->decoder
) {
124 if (!context
->target
)
125 return VA_STATUS_ERROR_INVALID_CONTEXT
;
127 if (context
->templat
.max_references
== 0)
128 return VA_STATUS_ERROR_INVALID_BUFFER
;
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
);
135 context
->decoder
= drv
->pipe
->create_video_codec(drv
->pipe
,
138 if (!context
->decoder
)
139 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
141 context
->decoder
->begin_frame(context
->decoder
, context
->target
,
142 &context
->desc
.base
);
149 handleIQMatrixBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
151 switch (u_reduce_video_profile(context
->templat
.profile
)) {
152 case PIPE_VIDEO_FORMAT_MPEG12
:
153 vlVaHandleIQMatrixBufferMPEG12(context
, buf
);
156 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
157 vlVaHandleIQMatrixBufferH264(context
, buf
);
160 case PIPE_VIDEO_FORMAT_MPEG4
:
161 vlVaHandleIQMatrixBufferMPEG4(context
, buf
);
164 case PIPE_VIDEO_FORMAT_HEVC
:
165 vlVaHandleIQMatrixBufferHEVC(context
, buf
);
174 handleSliceParameterBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
176 switch (u_reduce_video_profile(context
->templat
.profile
)) {
177 case PIPE_VIDEO_FORMAT_MPEG12
:
178 vlVaHandleSliceParameterBufferMPEG12(context
, buf
);
181 case PIPE_VIDEO_FORMAT_VC1
:
182 vlVaHandleSliceParameterBufferVC1(context
, buf
);
185 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
186 vlVaHandleSliceParameterBufferH264(context
, buf
);
189 case PIPE_VIDEO_FORMAT_MPEG4
:
190 vlVaHandleSliceParameterBufferMPEG4(context
, buf
);
193 case PIPE_VIDEO_FORMAT_HEVC
:
194 vlVaHandleSliceParameterBufferHEVC(context
, buf
);
203 bufHasStartcode(vlVaBuffer
*buf
, unsigned int code
, unsigned int bits
)
205 struct vl_vlc vlc
= {0};
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
)
213 vl_vlc_eatbits(&vlc
, 8);
214 vl_vlc_fillbits(&vlc
);
221 handleVASliceDataBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
223 enum pipe_video_format format
;
224 unsigned num_buffers
= 0;
225 void * const *buffers
[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 };
231 format
= u_reduce_video_profile(context
->templat
.profile
);
233 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
234 if (bufHasStartcode(buf
, 0x000001, 24))
237 buffers
[num_buffers
] = (void *const)&start_code_h264
;
238 sizes
[num_buffers
++] = sizeof(start_code_h264
);
240 case PIPE_VIDEO_FORMAT_HEVC
:
241 if (bufHasStartcode(buf
, 0x000001, 24))
244 buffers
[num_buffers
] = (void *const)&start_code_h265
;
245 sizes
[num_buffers
++] = sizeof(start_code_h265
);
247 case PIPE_VIDEO_FORMAT_VC1
:
248 if (bufHasStartcode(buf
, 0x0000010d, 32) ||
249 bufHasStartcode(buf
, 0x0000010c, 32) ||
250 bufHasStartcode(buf
, 0x0000010b, 32))
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
);
258 case PIPE_VIDEO_FORMAT_MPEG4
:
259 if (bufHasStartcode(buf
, 0x000001, 24))
262 vlVaDecoderFixMPEG4Startcode(context
);
263 buffers
[num_buffers
] = (void *)context
->mpeg4
.start_code
;
264 sizes
[num_buffers
++] = context
->mpeg4
.start_code_size
;
269 buffers
[num_buffers
] = buf
->data
;
270 sizes
[num_buffers
] = buf
->size
;
272 context
->decoder
->decode_bitstream(context
->decoder
, context
->target
, &context
->desc
.base
,
273 num_buffers
, (const void * const*)buffers
, sizes
);
277 vlVaRenderPicture(VADriverContextP ctx
, VAContextID context_id
, VABufferID
*buffers
, int num_buffers
)
280 vlVaContext
*context
;
281 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
286 return VA_STATUS_ERROR_INVALID_CONTEXT
;
288 drv
= VL_VA_DRIVER(ctx
);
290 return VA_STATUS_ERROR_INVALID_CONTEXT
;
292 context
= handle_table_get(drv
->htab
, context_id
);
294 return VA_STATUS_ERROR_INVALID_CONTEXT
;
296 for (i
= 0; i
< num_buffers
; ++i
) {
297 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, buffers
[i
]);
299 return VA_STATUS_ERROR_INVALID_BUFFER
;
302 case VAPictureParameterBufferType
:
303 vaStatus
= handlePictureParameterBuffer(drv
, context
, buf
);
306 case VAIQMatrixBufferType
:
307 handleIQMatrixBuffer(context
, buf
);
310 case VASliceParameterBufferType
:
311 handleSliceParameterBuffer(context
, buf
);
314 case VASliceDataBufferType
:
315 handleVASliceDataBufferType(context
, buf
);
317 case VAProcPipelineParameterBufferType
:
318 vaStatus
= vlVaHandleVAProcPipelineParameterBufferType(drv
, context
, buf
);
330 vlVaEndPicture(VADriverContextP ctx
, VAContextID context_id
)
333 vlVaContext
*context
;
336 return VA_STATUS_ERROR_INVALID_CONTEXT
;
338 drv
= VL_VA_DRIVER(ctx
);
340 return VA_STATUS_ERROR_INVALID_CONTEXT
;
342 context
= handle_table_get(drv
->htab
, context_id
);
344 return VA_STATUS_ERROR_INVALID_CONTEXT
;
346 if (!context
->decoder
) {
347 if (context
->templat
.profile
!= PIPE_VIDEO_PROFILE_UNKNOWN
)
348 return VA_STATUS_ERROR_INVALID_CONTEXT
;
351 return VA_STATUS_SUCCESS
;
354 context
->mpeg4
.frame_num
++;
355 context
->decoder
->end_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
357 return VA_STATUS_SUCCESS
;