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_id
= render_target
;
66 surf
->ctx
= context_id
;
67 context
->target
= surf
->buffer
;
69 if (!context
->decoder
) {
72 if (context
->templat
.profile
== PIPE_VIDEO_PROFILE_UNKNOWN
&&
73 context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8A8_UNORM
&&
74 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8A8_UNORM
&&
75 context
->target
->buffer_format
!= PIPE_FORMAT_B8G8R8X8_UNORM
&&
76 context
->target
->buffer_format
!= PIPE_FORMAT_R8G8B8X8_UNORM
&&
77 context
->target
->buffer_format
!= PIPE_FORMAT_NV12
)
78 return VA_STATUS_ERROR_UNIMPLEMENTED
;
80 return VA_STATUS_SUCCESS
;
83 if (context
->decoder
->entrypoint
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
)
84 context
->decoder
->begin_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
86 return VA_STATUS_SUCCESS
;
90 vlVaGetReferenceFrame(vlVaDriver
*drv
, VASurfaceID surface_id
,
91 struct pipe_video_buffer
**ref_frame
)
93 vlVaSurface
*surf
= handle_table_get(drv
->htab
, surface_id
);
95 *ref_frame
= surf
->buffer
;
101 getEncParamPreset(vlVaContext
*context
)
103 //motion estimation preset
104 context
->desc
.h264enc
.motion_est
.motion_est_quarter_pixel
= 0x00000001;
105 context
->desc
.h264enc
.motion_est
.lsmvert
= 0x00000002;
106 context
->desc
.h264enc
.motion_est
.enc_disable_sub_mode
= 0x00000078;
107 context
->desc
.h264enc
.motion_est
.enc_en_ime_overw_dis_subm
= 0x00000001;
108 context
->desc
.h264enc
.motion_est
.enc_ime_overw_dis_subm_no
= 0x00000001;
109 context
->desc
.h264enc
.motion_est
.enc_ime2_search_range_x
= 0x00000004;
110 context
->desc
.h264enc
.motion_est
.enc_ime2_search_range_y
= 0x00000004;
113 context
->desc
.h264enc
.pic_ctrl
.enc_cabac_enable
= 0x00000001;
114 context
->desc
.h264enc
.pic_ctrl
.enc_constraint_set_flags
= 0x00000040;
117 context
->desc
.h264enc
.rate_ctrl
.vbv_buffer_size
= 20000000;
118 context
->desc
.h264enc
.rate_ctrl
.vbv_buf_lv
= 48;
119 context
->desc
.h264enc
.rate_ctrl
.fill_data_enable
= 1;
120 context
->desc
.h264enc
.rate_ctrl
.enforce_hrd
= 1;
121 context
->desc
.h264enc
.enable_vui
= false;
122 if (context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
== 0)
123 context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
= 30;
124 context
->desc
.h264enc
.rate_ctrl
.target_bits_picture
=
125 context
->desc
.h264enc
.rate_ctrl
.target_bitrate
/ context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
;
126 context
->desc
.h264enc
.rate_ctrl
.peak_bits_picture_integer
=
127 context
->desc
.h264enc
.rate_ctrl
.peak_bitrate
/ context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
;
128 context
->desc
.h264enc
.rate_ctrl
.peak_bits_picture_fraction
= 0;
130 context
->desc
.h264enc
.ref_pic_mode
= 0x00000201;
134 handlePictureParameterBuffer(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
136 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
138 switch (u_reduce_video_profile(context
->templat
.profile
)) {
139 case PIPE_VIDEO_FORMAT_MPEG12
:
140 vlVaHandlePictureParameterBufferMPEG12(drv
, context
, buf
);
143 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
144 vlVaHandlePictureParameterBufferH264(drv
, context
, buf
);
147 case PIPE_VIDEO_FORMAT_VC1
:
148 vlVaHandlePictureParameterBufferVC1(drv
, context
, buf
);
151 case PIPE_VIDEO_FORMAT_MPEG4
:
152 vlVaHandlePictureParameterBufferMPEG4(drv
, context
, buf
);
155 case PIPE_VIDEO_FORMAT_HEVC
:
156 vlVaHandlePictureParameterBufferHEVC(drv
, context
, buf
);
163 /* Create the decoder once max_references is known. */
164 if (!context
->decoder
) {
165 if (!context
->target
)
166 return VA_STATUS_ERROR_INVALID_CONTEXT
;
168 if (context
->templat
.max_references
== 0)
169 return VA_STATUS_ERROR_INVALID_BUFFER
;
171 if (u_reduce_video_profile(context
->templat
.profile
) ==
172 PIPE_VIDEO_FORMAT_MPEG4_AVC
)
173 context
->templat
.level
= u_get_h264_level(context
->templat
.width
,
174 context
->templat
.height
, &context
->templat
.max_references
);
176 context
->decoder
= drv
->pipe
->create_video_codec(drv
->pipe
,
179 if (!context
->decoder
)
180 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
182 context
->decoder
->begin_frame(context
->decoder
, context
->target
,
183 &context
->desc
.base
);
190 handleIQMatrixBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
192 switch (u_reduce_video_profile(context
->templat
.profile
)) {
193 case PIPE_VIDEO_FORMAT_MPEG12
:
194 vlVaHandleIQMatrixBufferMPEG12(context
, buf
);
197 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
198 vlVaHandleIQMatrixBufferH264(context
, buf
);
201 case PIPE_VIDEO_FORMAT_MPEG4
:
202 vlVaHandleIQMatrixBufferMPEG4(context
, buf
);
205 case PIPE_VIDEO_FORMAT_HEVC
:
206 vlVaHandleIQMatrixBufferHEVC(context
, buf
);
215 handleSliceParameterBuffer(vlVaContext
*context
, vlVaBuffer
*buf
)
217 switch (u_reduce_video_profile(context
->templat
.profile
)) {
218 case PIPE_VIDEO_FORMAT_MPEG12
:
219 vlVaHandleSliceParameterBufferMPEG12(context
, buf
);
222 case PIPE_VIDEO_FORMAT_VC1
:
223 vlVaHandleSliceParameterBufferVC1(context
, buf
);
226 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
227 vlVaHandleSliceParameterBufferH264(context
, buf
);
230 case PIPE_VIDEO_FORMAT_MPEG4
:
231 vlVaHandleSliceParameterBufferMPEG4(context
, buf
);
234 case PIPE_VIDEO_FORMAT_HEVC
:
235 vlVaHandleSliceParameterBufferHEVC(context
, buf
);
244 bufHasStartcode(vlVaBuffer
*buf
, unsigned int code
, unsigned int bits
)
246 struct vl_vlc vlc
= {0};
249 /* search the first 64 bytes for a startcode */
250 vl_vlc_init(&vlc
, 1, (const void * const*)&buf
->data
, &buf
->size
);
251 for (i
= 0; i
< 64 && vl_vlc_bits_left(&vlc
) >= bits
; ++i
) {
252 if (vl_vlc_peekbits(&vlc
, bits
) == code
)
254 vl_vlc_eatbits(&vlc
, 8);
255 vl_vlc_fillbits(&vlc
);
262 handleVASliceDataBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
264 enum pipe_video_format format
;
265 unsigned num_buffers
= 0;
266 void * const *buffers
[2];
268 static const uint8_t start_code_h264
[] = { 0x00, 0x00, 0x01 };
269 static const uint8_t start_code_h265
[] = { 0x00, 0x00, 0x01 };
270 static const uint8_t start_code_vc1
[] = { 0x00, 0x00, 0x01, 0x0d };
272 format
= u_reduce_video_profile(context
->templat
.profile
);
274 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
275 if (bufHasStartcode(buf
, 0x000001, 24))
278 buffers
[num_buffers
] = (void *const)&start_code_h264
;
279 sizes
[num_buffers
++] = sizeof(start_code_h264
);
281 case PIPE_VIDEO_FORMAT_HEVC
:
282 if (bufHasStartcode(buf
, 0x000001, 24))
285 buffers
[num_buffers
] = (void *const)&start_code_h265
;
286 sizes
[num_buffers
++] = sizeof(start_code_h265
);
288 case PIPE_VIDEO_FORMAT_VC1
:
289 if (bufHasStartcode(buf
, 0x0000010d, 32) ||
290 bufHasStartcode(buf
, 0x0000010c, 32) ||
291 bufHasStartcode(buf
, 0x0000010b, 32))
294 if (context
->decoder
->profile
== PIPE_VIDEO_PROFILE_VC1_ADVANCED
) {
295 buffers
[num_buffers
] = (void *const)&start_code_vc1
;
296 sizes
[num_buffers
++] = sizeof(start_code_vc1
);
299 case PIPE_VIDEO_FORMAT_MPEG4
:
300 if (bufHasStartcode(buf
, 0x000001, 24))
303 vlVaDecoderFixMPEG4Startcode(context
);
304 buffers
[num_buffers
] = (void *)context
->mpeg4
.start_code
;
305 sizes
[num_buffers
++] = context
->mpeg4
.start_code_size
;
310 buffers
[num_buffers
] = buf
->data
;
311 sizes
[num_buffers
] = buf
->size
;
313 context
->decoder
->decode_bitstream(context
->decoder
, context
->target
, &context
->desc
.base
,
314 num_buffers
, (const void * const*)buffers
, sizes
);
318 handleVAEncMiscParameterTypeRateControl(vlVaContext
*context
, VAEncMiscParameterBuffer
*misc
)
320 VAEncMiscParameterRateControl
*rc
= (VAEncMiscParameterRateControl
*)misc
->data
;
321 if (context
->desc
.h264enc
.rate_ctrl
.rate_ctrl_method
==
322 PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT
)
323 context
->desc
.h264enc
.rate_ctrl
.target_bitrate
= rc
->bits_per_second
;
325 context
->desc
.h264enc
.rate_ctrl
.target_bitrate
= rc
->bits_per_second
* (rc
->target_percentage
/ 100.0);
326 context
->desc
.h264enc
.rate_ctrl
.peak_bitrate
= rc
->bits_per_second
;
327 if (context
->desc
.h264enc
.rate_ctrl
.target_bitrate
< 2000000)
328 context
->desc
.h264enc
.rate_ctrl
.vbv_buffer_size
= MIN2((context
->desc
.h264enc
.rate_ctrl
.target_bitrate
* 2.75), 2000000);
330 context
->desc
.h264enc
.rate_ctrl
.vbv_buffer_size
= context
->desc
.h264enc
.rate_ctrl
.target_bitrate
;
332 return VA_STATUS_SUCCESS
;
336 handleVAEncMiscParameterTypeFrameRate(vlVaContext
*context
, VAEncMiscParameterBuffer
*misc
)
338 VAEncMiscParameterFrameRate
*fr
= (VAEncMiscParameterFrameRate
*)misc
->data
;
339 context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
= fr
->framerate
;
340 return VA_STATUS_SUCCESS
;
344 handleVAEncSequenceParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
346 VAEncSequenceParameterBufferH264
*h264
= (VAEncSequenceParameterBufferH264
*)buf
->data
;
347 if (!context
->decoder
) {
348 context
->templat
.max_references
= h264
->max_num_ref_frames
;
349 context
->templat
.level
= h264
->level_idc
;
350 context
->decoder
= drv
->pipe
->create_video_codec(drv
->pipe
, &context
->templat
);
351 if (!context
->decoder
)
352 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
354 context
->desc
.h264enc
.gop_size
= h264
->intra_idr_period
;
355 context
->desc
.h264enc
.rate_ctrl
.frame_rate_num
= h264
->time_scale
/ 2;
356 context
->desc
.h264enc
.rate_ctrl
.frame_rate_den
= 1;
357 return VA_STATUS_SUCCESS
;
361 handleVAEncMiscParameterBufferType(vlVaContext
*context
, vlVaBuffer
*buf
)
363 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
364 VAEncMiscParameterBuffer
*misc
;
367 switch (misc
->type
) {
368 case VAEncMiscParameterTypeRateControl
:
369 vaStatus
= handleVAEncMiscParameterTypeRateControl(context
, misc
);
372 case VAEncMiscParameterTypeFrameRate
:
373 vaStatus
= handleVAEncMiscParameterTypeFrameRate(context
, misc
);
384 handleVAEncPictureParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
386 VAEncPictureParameterBufferH264
*h264
;
387 vlVaBuffer
*coded_buf
;
390 context
->desc
.h264enc
.frame_num
= h264
->frame_num
;
391 context
->desc
.h264enc
.not_referenced
= false;
392 context
->desc
.h264enc
.is_idr
= (h264
->pic_fields
.bits
.idr_pic_flag
== 1);
393 context
->desc
.h264enc
.pic_order_cnt
= h264
->CurrPic
.TopFieldOrderCnt
/ 2;
394 if (context
->desc
.h264enc
.is_idr
)
395 context
->desc
.h264enc
.i_remain
= 1;
397 context
->desc
.h264enc
.i_remain
= 0;
399 context
->desc
.h264enc
.p_remain
= context
->desc
.h264enc
.gop_size
- context
->desc
.h264enc
.gop_cnt
- context
->desc
.h264enc
.i_remain
;
401 coded_buf
= handle_table_get(drv
->htab
, h264
->coded_buf
);
402 if (!coded_buf
->derived_surface
.resource
)
403 coded_buf
->derived_surface
.resource
= pipe_buffer_create(drv
->pipe
->screen
, PIPE_BIND_VERTEX_BUFFER
,
404 PIPE_USAGE_STREAM
, coded_buf
->size
);
405 context
->coded_buf
= coded_buf
;
407 context
->desc
.h264enc
.frame_idx
[h264
->CurrPic
.picture_id
] = h264
->frame_num
;
408 if (context
->desc
.h264enc
.is_idr
)
409 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_IDR
;
411 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_P
;
413 context
->desc
.h264enc
.quant_i_frames
= h264
->pic_init_qp
;
414 context
->desc
.h264enc
.quant_b_frames
= h264
->pic_init_qp
;
415 context
->desc
.h264enc
.quant_p_frames
= h264
->pic_init_qp
;
416 context
->desc
.h264enc
.frame_num_cnt
++;
417 context
->desc
.h264enc
.gop_cnt
++;
418 if (context
->desc
.h264enc
.gop_cnt
== context
->desc
.h264enc
.gop_size
)
419 context
->desc
.h264enc
.gop_cnt
= 0;
421 return VA_STATUS_SUCCESS
;
425 handleVAEncSliceParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
427 VAEncSliceParameterBufferH264
*h264
;
430 context
->desc
.h264enc
.ref_idx_l0
= VA_INVALID_ID
;
431 context
->desc
.h264enc
.ref_idx_l1
= VA_INVALID_ID
;
433 for (int i
= 0; i
< 32; i
++) {
434 if (h264
->RefPicList0
[i
].picture_id
!= VA_INVALID_ID
) {
435 if (context
->desc
.h264enc
.ref_idx_l0
== VA_INVALID_ID
)
436 context
->desc
.h264enc
.ref_idx_l0
= context
->desc
.h264enc
.frame_idx
[h264
->RefPicList0
[i
].picture_id
];
438 if (h264
->RefPicList1
[i
].picture_id
!= VA_INVALID_ID
&& h264
->slice_type
== 1) {
439 if (context
->desc
.h264enc
.ref_idx_l1
== VA_INVALID_ID
)
440 context
->desc
.h264enc
.ref_idx_l1
= context
->desc
.h264enc
.frame_idx
[h264
->RefPicList1
[i
].picture_id
];
444 if (h264
->slice_type
== 1)
445 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_B
;
446 else if (h264
->slice_type
== 0)
447 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_P
;
448 else if (h264
->slice_type
== 2) {
449 if (context
->desc
.h264enc
.is_idr
){
450 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_IDR
;
451 context
->desc
.h264enc
.idr_pic_id
++;
453 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_I
;
455 context
->desc
.h264enc
.picture_type
= PIPE_H264_ENC_PICTURE_TYPE_SKIP
;
457 return VA_STATUS_SUCCESS
;
461 vlVaRenderPicture(VADriverContextP ctx
, VAContextID context_id
, VABufferID
*buffers
, int num_buffers
)
464 vlVaContext
*context
;
465 VAStatus vaStatus
= VA_STATUS_SUCCESS
;
470 return VA_STATUS_ERROR_INVALID_CONTEXT
;
472 drv
= VL_VA_DRIVER(ctx
);
474 return VA_STATUS_ERROR_INVALID_CONTEXT
;
476 pipe_mutex_lock(drv
->mutex
);
477 context
= handle_table_get(drv
->htab
, context_id
);
479 pipe_mutex_unlock(drv
->mutex
);
480 return VA_STATUS_ERROR_INVALID_CONTEXT
;
483 for (i
= 0; i
< num_buffers
; ++i
) {
484 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, buffers
[i
]);
486 pipe_mutex_unlock(drv
->mutex
);
487 return VA_STATUS_ERROR_INVALID_BUFFER
;
491 case VAPictureParameterBufferType
:
492 vaStatus
= handlePictureParameterBuffer(drv
, context
, buf
);
495 case VAIQMatrixBufferType
:
496 handleIQMatrixBuffer(context
, buf
);
499 case VASliceParameterBufferType
:
500 handleSliceParameterBuffer(context
, buf
);
503 case VASliceDataBufferType
:
504 handleVASliceDataBufferType(context
, buf
);
506 case VAProcPipelineParameterBufferType
:
507 vaStatus
= vlVaHandleVAProcPipelineParameterBufferType(drv
, context
, buf
);
510 case VAEncSequenceParameterBufferType
:
511 vaStatus
= handleVAEncSequenceParameterBufferType(drv
, context
, buf
);
514 case VAEncMiscParameterBufferType
:
515 vaStatus
= handleVAEncMiscParameterBufferType(context
, buf
);
518 case VAEncPictureParameterBufferType
:
519 vaStatus
= handleVAEncPictureParameterBufferType(drv
, context
, buf
);
522 case VAEncSliceParameterBufferType
:
523 vaStatus
= handleVAEncSliceParameterBufferType(drv
, context
, buf
);
530 pipe_mutex_unlock(drv
->mutex
);
536 vlVaEndPicture(VADriverContextP ctx
, VAContextID context_id
)
539 vlVaContext
*context
;
540 vlVaBuffer
*coded_buf
;
545 return VA_STATUS_ERROR_INVALID_CONTEXT
;
547 drv
= VL_VA_DRIVER(ctx
);
549 return VA_STATUS_ERROR_INVALID_CONTEXT
;
551 pipe_mutex_lock(drv
->mutex
);
552 context
= handle_table_get(drv
->htab
, context_id
);
553 pipe_mutex_unlock(drv
->mutex
);
555 return VA_STATUS_ERROR_INVALID_CONTEXT
;
557 if (!context
->decoder
) {
558 if (context
->templat
.profile
!= PIPE_VIDEO_PROFILE_UNKNOWN
)
559 return VA_STATUS_ERROR_INVALID_CONTEXT
;
562 return VA_STATUS_SUCCESS
;
565 pipe_mutex_lock(drv
->mutex
);
566 surf
= handle_table_get(drv
->htab
, context
->target_id
);
567 context
->mpeg4
.frame_num
++;
569 if (context
->decoder
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
570 coded_buf
= context
->coded_buf
;
571 getEncParamPreset(context
);
572 context
->decoder
->begin_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
573 context
->decoder
->encode_bitstream(context
->decoder
, context
->target
,
574 coded_buf
->derived_surface
.resource
, &feedback
);
575 surf
->frame_num_cnt
= context
->desc
.h264enc
.frame_num_cnt
;
576 surf
->feedback
= feedback
;
577 surf
->coded_buf
= coded_buf
;
580 context
->decoder
->end_frame(context
->decoder
, context
->target
, &context
->desc
.base
);
581 pipe_mutex_unlock(drv
->mutex
);
582 return VA_STATUS_SUCCESS
;