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 mtx_lock(&drv
->mutex
);
54 context
= handle_table_get(drv
->htab
, context_id
);
56 mtx_unlock(&drv
->mutex
);
57 return VA_STATUS_ERROR_INVALID_CONTEXT
;
60 if (u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_MPEG12
) {
61 context
->desc
.mpeg12
.intra_matrix
= NULL
;
62 context
->desc
.mpeg12
.non_intra_matrix
= NULL
;
65 surf
= handle_table_get(drv
->htab
, render_target
);
66 mtx_unlock(&drv
->mutex
);
67 if (!surf
|| !surf
->buffer
)
68 return VA_STATUS_ERROR_INVALID_SURFACE
;
70 context
->target_id
= render_target
;
71 surf
->ctx
= context_id
;
72 context
->target
= surf
->buffer
;
73 context
->mjpeg
.sampling_factor
= 0;
75 if (!context
->decoder
) {
78 if (context
->templat
.profile
== PIPE_VIDEO_PROFILE_UNKNOWN
&&
79 context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8A8_UNORM
&&
80 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8A8_UNORM
&&
81 context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8X8_UNORM
&&
82 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8X8_UNORM
&&
83 context
->target
->buffer_format
!= PIPE_FORMAT_NV12
&&
84 context
->target
->buffer_format
!= PIPE_FORMAT_P016
)
85 return VA_STATUS_ERROR_UNIMPLEMENTED
;
87 return VA_STATUS_SUCCESS
;
90 if (context
->decoder
->entrypoint
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
)
91 context
->needs_begin_frame
= true;
93 return VA_STATUS_SUCCESS
;
97 vlVaGetReferenceFrame(vlVaDriver
*drv
, VASurfaceID surface_id
,
98 struct pipe_video_buffer
**ref_frame
)
100 vlVaSurface
*surf
= handle_table_get(drv
->htab
, surface_id
);
102 *ref_frame
= surf
->buffer
;
108 handlePictureParameterBuffer(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
110 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
112 switch (u_reduce_video_profile(context
->templat
.profile
)) {
113 case PIPE_VIDEO_FORMAT_MPEG12
:
114 vlVaHandlePictureParameterBufferMPEG12(drv
, context
, buf
);
117 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
118 vlVaHandlePictureParameterBufferH264(drv
, context
, buf
);
121 case PIPE_VIDEO_FORMAT_VC1
:
122 vlVaHandlePictureParameterBufferVC1(drv
, context
, buf
);
125 case PIPE_VIDEO_FORMAT_MPEG4
:
126 vlVaHandlePictureParameterBufferMPEG4(drv
, context
, buf
);
129 case PIPE_VIDEO_FORMAT_HEVC
:
130 vlVaHandlePictureParameterBufferHEVC(drv
, context
, buf
);
133 case PIPE_VIDEO_FORMAT_JPEG
:
134 vlVaHandlePictureParameterBufferMJPEG(drv
, context
, buf
);
141 /* Create the decoder once max_references is known. */
142 if (!context
->decoder
) {
143 enum pipe_video_format format
=
144 u_reduce_video_profile(context
->templat
.profile
);
146 if (!context
->target
)
147 return VA_STATUS_ERROR_INVALID_CONTEXT
;
149 if (context
->templat
.max_references
== 0 &&
150 format
!= PIPE_VIDEO_FORMAT_JPEG
)
151 return VA_STATUS_ERROR_INVALID_BUFFER
;
153 if (format
== PIPE_VIDEO_FORMAT_MPEG4_AVC
)
154 context
->templat
.level
= u_get_h264_level(context
->templat
.width
,
155 context
->templat
.height
, &context
->templat
.max_references
);
157 context
->decoder
= drv
->pipe
->create_video_codec(drv
->pipe
,
160 if (!context
->decoder
)
161 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
163 context
->needs_begin_frame
= true;
170 handleIQMatrixBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
172 switch (u_reduce_video_profile(context
->templat
.profile
)) {
173 case PIPE_VIDEO_FORMAT_MPEG12
:
174 vlVaHandleIQMatrixBufferMPEG12(context
, buf
);
177 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
178 vlVaHandleIQMatrixBufferH264(context
, buf
);
181 case PIPE_VIDEO_FORMAT_MPEG4
:
182 vlVaHandleIQMatrixBufferMPEG4(context
, buf
);
185 case PIPE_VIDEO_FORMAT_HEVC
:
186 vlVaHandleIQMatrixBufferHEVC(context
, buf
);
189 case PIPE_VIDEO_FORMAT_JPEG
:
190 vlVaHandleIQMatrixBufferMJPEG(context
, buf
);
199 handleSliceParameterBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
201 switch (u_reduce_video_profile(context
->templat
.profile
)) {
202 case PIPE_VIDEO_FORMAT_MPEG12
:
203 vlVaHandleSliceParameterBufferMPEG12(context
, buf
);
206 case PIPE_VIDEO_FORMAT_VC1
:
207 vlVaHandleSliceParameterBufferVC1(context
, buf
);
210 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
211 vlVaHandleSliceParameterBufferH264(context
, buf
);
214 case PIPE_VIDEO_FORMAT_MPEG4
:
215 vlVaHandleSliceParameterBufferMPEG4(context
, buf
);
218 case PIPE_VIDEO_FORMAT_HEVC
:
219 vlVaHandleSliceParameterBufferHEVC(context
, buf
);
222 case PIPE_VIDEO_FORMAT_JPEG
:
223 vlVaHandleSliceParameterBufferMJPEG(context
, buf
);
232 bufHasStartcode(vlVaBuffer
*buf
, unsigned int code
, unsigned int bits
)
234 struct vl_vlc vlc
= {0};
237 /* search the first 64 bytes for a startcode */
238 vl_vlc_init(&vlc
, 1, (const void * const*)&buf
->data
, &buf
->size
);
239 for (i
= 0; i
< 64 && vl_vlc_bits_left(&vlc
) >= bits
; ++i
) {
240 if (vl_vlc_peekbits(&vlc
, bits
) == code
)
242 vl_vlc_eatbits(&vlc
, 8);
243 vl_vlc_fillbits(&vlc
);
250 handleVASliceDataBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
252 enum pipe_video_format format
;
253 unsigned num_buffers
= 0;
254 void * const *buffers
[2];
256 static const uint8_t start_code_h264
[] = { 0x00, 0x00, 0x01 };
257 static const uint8_t start_code_h265
[] = { 0x00, 0x00, 0x01 };
258 static const uint8_t start_code_vc1
[] = { 0x00, 0x00, 0x01, 0x0d };
260 format
= u_reduce_video_profile(context
->templat
.profile
);
262 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
263 if (bufHasStartcode(buf
, 0x000001, 24))
266 buffers
[num_buffers
] = (void *const)&start_code_h264
;
267 sizes
[num_buffers
++] = sizeof(start_code_h264
);
269 case PIPE_VIDEO_FORMAT_HEVC
:
270 if (bufHasStartcode(buf
, 0x000001, 24))
273 buffers
[num_buffers
] = (void *const)&start_code_h265
;
274 sizes
[num_buffers
++] = sizeof(start_code_h265
);
276 case PIPE_VIDEO_FORMAT_VC1
:
277 if (bufHasStartcode(buf
, 0x0000010d, 32) ||
278 bufHasStartcode(buf
, 0x0000010c, 32) ||
279 bufHasStartcode(buf
, 0x0000010b, 32))
282 if (context
->decoder
->profile
== PIPE_VIDEO_PROFILE_VC1_ADVANCED
) {
283 buffers
[num_buffers
] = (void *const)&start_code_vc1
;
284 sizes
[num_buffers
++] = sizeof(start_code_vc1
);
287 case PIPE_VIDEO_FORMAT_MPEG4
:
288 if (bufHasStartcode(buf
, 0x000001, 24))
291 vlVaDecoderFixMPEG4Startcode(context
);
292 buffers
[num_buffers
] = (void *)context
->mpeg4
.start_code
;
293 sizes
[num_buffers
++] = context
->mpeg4
.start_code_size
;
295 case PIPE_VIDEO_FORMAT_JPEG
:
301 buffers
[num_buffers
] = buf
->data
;
302 sizes
[num_buffers
] = buf
->size
;
305 if (context
->needs_begin_frame
) {
306 context
->decoder
->begin_frame(context
->decoder
, context
->target
,
307 &context
->desc
.base
);
308 context
->needs_begin_frame
= false;
310 context
->decoder
->decode_bitstream(context
->decoder
, context
->target
, &context
->desc
.base
,
311 num_buffers
, (const void * const*)buffers
, sizes
);
315 handleVAEncMiscParameterTypeRateControl(vlVaContext
*context
, VAEncMiscParameterBuffer
*misc
)
317 VAStatus status
= VA_STATUS_SUCCESS
;
319 switch (u_reduce_video_profile(context
->templat
.profile
)) {
320 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
321 status
= vlVaHandleVAEncMiscParameterTypeRateControlH264(context
, misc
);
324 case PIPE_VIDEO_FORMAT_HEVC
:
325 status
= vlVaHandleVAEncMiscParameterTypeRateControlHEVC(context
, misc
);
336 handleVAEncMiscParameterTypeFrameRate(vlVaContext
*context
, VAEncMiscParameterBuffer
*misc
)
338 VAStatus status
= VA_STATUS_SUCCESS
;
340 switch (u_reduce_video_profile(context
->templat
.profile
)) {
341 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
342 status
= vlVaHandleVAEncMiscParameterTypeFrameRateH264(context
, misc
);
345 case PIPE_VIDEO_FORMAT_HEVC
:
346 status
= vlVaHandleVAEncMiscParameterTypeFrameRateHEVC(context
, misc
);
357 handleVAEncSequenceParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
359 VAStatus status
= VA_STATUS_SUCCESS
;
361 switch (u_reduce_video_profile(context
->templat
.profile
)) {
362 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
363 status
= vlVaHandleVAEncSequenceParameterBufferTypeH264(drv
, context
, buf
);
366 case PIPE_VIDEO_FORMAT_HEVC
:
367 status
= vlVaHandleVAEncSequenceParameterBufferTypeHEVC(drv
, context
, buf
);
378 handleVAEncMiscParameterBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
380 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
381 VAEncMiscParameterBuffer
*misc
;
384 switch (misc
->type
) {
385 case VAEncMiscParameterTypeRateControl
:
386 vaStatus
= handleVAEncMiscParameterTypeRateControl(context
, misc
);
389 case VAEncMiscParameterTypeFrameRate
:
390 vaStatus
= handleVAEncMiscParameterTypeFrameRate(context
, misc
);
401 handleVAEncPictureParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
403 VAStatus status
= VA_STATUS_SUCCESS
;
405 switch (u_reduce_video_profile(context
->templat
.profile
)) {
406 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
407 status
= vlVaHandleVAEncPictureParameterBufferTypeH264(drv
, context
, buf
);
410 case PIPE_VIDEO_FORMAT_HEVC
:
411 status
= vlVaHandleVAEncPictureParameterBufferTypeHEVC(drv
, context
, buf
);
422 handleVAEncSliceParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
424 VAStatus status
= VA_STATUS_SUCCESS
;
426 switch (u_reduce_video_profile(context
->templat
.profile
)) {
427 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
428 status
= vlVaHandleVAEncSliceParameterBufferTypeH264(drv
, context
, buf
);
431 case PIPE_VIDEO_FORMAT_HEVC
:
432 status
= vlVaHandleVAEncSliceParameterBufferTypeHEVC(drv
, context
, buf
);
443 vlVaRenderPicture(VADriverContextP ctx
, VAContextID context_id
, VABufferID
*buffers
, int num_buffers
)
446 vlVaContext
*context
;
447 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
452 return VA_STATUS_ERROR_INVALID_CONTEXT
;
454 drv
= VL_VA_DRIVER(ctx
);
456 return VA_STATUS_ERROR_INVALID_CONTEXT
;
458 mtx_lock(&drv
->mutex
);
459 context
= handle_table_get(drv
->htab
, context_id
);
461 mtx_unlock(&drv
->mutex
);
462 return VA_STATUS_ERROR_INVALID_CONTEXT
;
465 for (i
= 0; i
< num_buffers
; ++i
) {
466 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, buffers
[i
]);
468 mtx_unlock(&drv
->mutex
);
469 return VA_STATUS_ERROR_INVALID_BUFFER
;
473 case VAPictureParameterBufferType
:
474 vaStatus
= handlePictureParameterBuffer(drv
, context
, buf
);
477 case VAIQMatrixBufferType
:
478 handleIQMatrixBuffer(context
, buf
);
481 case VASliceParameterBufferType
:
482 handleSliceParameterBuffer(context
, buf
);
485 case VASliceDataBufferType
:
486 handleVASliceDataBufferType(context
, buf
);
488 case VAProcPipelineParameterBufferType
:
489 vaStatus
= vlVaHandleVAProcPipelineParameterBufferType(drv
, context
, buf
);
492 case VAEncSequenceParameterBufferType
:
493 vaStatus
= handleVAEncSequenceParameterBufferType(drv
, context
, buf
);
496 case VAEncMiscParameterBufferType
:
497 vaStatus
= handleVAEncMiscParameterBufferType(context
, buf
);
500 case VAEncPictureParameterBufferType
:
501 vaStatus
= handleVAEncPictureParameterBufferType(drv
, context
, buf
);
504 case VAEncSliceParameterBufferType
:
505 vaStatus
= handleVAEncSliceParameterBufferType(drv
, context
, buf
);
508 case VAHuffmanTableBufferType
:
509 vlVaHandleHuffmanTableBufferType(context
, buf
);
516 mtx_unlock(&drv
->mutex
);
522 vlVaEndPicture(VADriverContextP ctx
, VAContextID context_id
)
525 vlVaContext
*context
;
526 vlVaBuffer
*coded_buf
;
529 struct pipe_screen
*screen
;
531 bool realloc
= false;
532 enum pipe_format format
;
535 return VA_STATUS_ERROR_INVALID_CONTEXT
;
537 drv
= VL_VA_DRIVER(ctx
);
539 return VA_STATUS_ERROR_INVALID_CONTEXT
;
541 mtx_lock(&drv
->mutex
);
542 context
= handle_table_get(drv
->htab
, context_id
);
543 mtx_unlock(&drv
->mutex
);
545 return VA_STATUS_ERROR_INVALID_CONTEXT
;
547 if (!context
->decoder
) {
548 if (context
->templat
.profile
!= PIPE_VIDEO_PROFILE_UNKNOWN
)
549 return VA_STATUS_ERROR_INVALID_CONTEXT
;
552 return VA_STATUS_SUCCESS
;
555 mtx_lock(&drv
->mutex
);
556 surf
= handle_table_get(drv
->htab
, context
->target_id
);
557 context
->mpeg4
.frame_num
++;
559 screen
= context
->decoder
->context
->screen
;
560 supported
= screen
->get_video_param(screen
, context
->decoder
->profile
,
561 context
->decoder
->entrypoint
,
562 surf
->buffer
->interlaced
?
563 PIPE_VIDEO_CAP_SUPPORTS_INTERLACED
:
564 PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE
);
567 surf
->templat
.interlaced
= screen
->get_video_param(screen
,
568 context
->decoder
->profile
,
569 context
->decoder
->entrypoint
,
570 PIPE_VIDEO_CAP_PREFERS_INTERLACED
);
574 format
= screen
->get_video_param(screen
, context
->decoder
->profile
,
575 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
576 PIPE_VIDEO_CAP_PREFERED_FORMAT
);
578 if (surf
->buffer
->buffer_format
!= format
&&
579 surf
->buffer
->buffer_format
== PIPE_FORMAT_NV12
) {
580 /* check originally as NV12 only */
581 surf
->templat
.buffer_format
= format
;
585 if (u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_JPEG
&&
586 surf
->buffer
->buffer_format
== PIPE_FORMAT_NV12
) {
587 if (context
->mjpeg
.sampling_factor
== 0x211111 ||
588 context
->mjpeg
.sampling_factor
== 0x221212) {
589 surf
->templat
.buffer_format
= PIPE_FORMAT_YUYV
;
591 } else if (context
->mjpeg
.sampling_factor
!= 0x221111) {
592 /* Not NV12 either */
593 mtx_unlock(&drv
->mutex
);
594 return VA_STATUS_ERROR_INVALID_SURFACE
;
599 struct pipe_video_buffer
*old_buf
= surf
->buffer
;
601 if (vlVaHandleSurfaceAllocate(drv
, surf
, &surf
->templat
) != VA_STATUS_SUCCESS
) {
602 mtx_unlock(&drv
->mutex
);
603 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
606 if (context
->decoder
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
607 if (old_buf
->interlaced
) {
608 struct u_rect src_rect
, dst_rect
;
610 dst_rect
.x0
= src_rect
.x0
= 0;
611 dst_rect
.y0
= src_rect
.y0
= 0;
612 dst_rect
.x1
= src_rect
.x1
= surf
->templat
.width
;
613 dst_rect
.y1
= src_rect
.y1
= surf
->templat
.height
;
614 vl_compositor_yuv_deint_full(&drv
->cstate
, &drv
->compositor
,
615 old_buf
, surf
->buffer
,
616 &src_rect
, &dst_rect
, VL_COMPOSITOR_WEAVE
);
618 /* Can't convert from progressive to interlaced yet */
619 mtx_unlock(&drv
->mutex
);
620 return VA_STATUS_ERROR_INVALID_SURFACE
;
624 old_buf
->destroy(old_buf
);
625 context
->target
= surf
->buffer
;
628 if (context
->decoder
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
629 coded_buf
= context
->coded_buf
;
630 if (u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
631 getEncParamPresetH264(context
);
632 context
->desc
.h264enc
.frame_num_cnt
++;
633 } else if (u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_HEVC
)
634 getEncParamPresetH265(context
);
635 context
->decoder
->begin_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
636 context
->decoder
->encode_bitstream(context
->decoder
, context
->target
,
637 coded_buf
->derived_surface
.resource
, &feedback
);
638 surf
->feedback
= feedback
;
639 surf
->coded_buf
= coded_buf
;
642 context
->decoder
->end_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
643 if (context
->decoder
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
&&
644 u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
645 int idr_period
= context
->desc
.h264enc
.gop_size
/ context
->gop_coeff
;
646 int p_remain_in_idr
= idr_period
- context
->desc
.h264enc
.frame_num
;
647 surf
->frame_num_cnt
= context
->desc
.h264enc
.frame_num_cnt
;
648 surf
->force_flushed
= false;
649 if (context
->first_single_submitted
) {
650 context
->decoder
->flush(context
->decoder
);
651 context
->first_single_submitted
= false;
652 surf
->force_flushed
= true;
654 if (p_remain_in_idr
== 1) {
655 if ((context
->desc
.h264enc
.frame_num_cnt
% 2) != 0) {
656 context
->decoder
->flush(context
->decoder
);
657 context
->first_single_submitted
= true;
660 context
->first_single_submitted
= false;
661 surf
->force_flushed
= true;
663 } else if (context
->decoder
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
&&
664 u_reduce_video_profile(context
->templat
.profile
) == PIPE_VIDEO_FORMAT_HEVC
)
665 context
->desc
.h265enc
.frame_num
++;
666 mtx_unlock(&drv
->mutex
);
667 return VA_STATUS_SUCCESS
;