43 int type,
char *
data,
size_t bit_len)
47 VABufferID param_buffer, data_buffer;
49 VAEncPackedHeaderParameterBuffer params = {
51 .bit_length = bit_len,
52 .has_emulation_bytes = 1,
60 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
61 VAEncPackedHeaderParameterBufferType,
62 sizeof(params), 1, ¶ms, ¶m_buffer);
63 if (vas != VA_STATUS_SUCCESS) {
65 "for packed header (type %d): %d (%s).\n",
66 type, vas, vaErrorStr(vas));
71 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
72 VAEncPackedHeaderDataBufferType,
73 (bit_len + 7) / 8, 1,
data, &data_buffer);
74 if (vas != VA_STATUS_SUCCESS) {
76 "for packed header (type %d): %d (%s).\n",
77 type, vas, vaErrorStr(vas));
83 "(%zu bits).\n",
type, param_buffer, data_buffer, bit_len);
101 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
103 if (vas != VA_STATUS_SUCCESS) {
105 "(type %d): %d (%s).\n",
type, vas, vaErrorStr(vas));
124 VAEncMiscParameterBuffer
header = {
127 size_t buffer_size =
sizeof(
header) +
len;
134 VAEncMiscParameterBufferType,
140 #if VA_CHECK_VERSION(1, 9, 0)
158 #if VA_CHECK_VERSION(1, 9, 0)
160 vas = vaSyncBuffer(
ctx->hwctx->display,
162 VA_TIMEOUT_INFINITE);
163 if (vas != VA_STATUS_SUCCESS) {
165 "%d (%s).\n", vas, vaErrorStr(vas));
172 if (vas != VA_STATUS_SUCCESS) {
174 "%d (%s).\n", vas, vaErrorStr(vas));
196 rounding =
ctx->slice_block_rows -
ctx->nb_slices *
ctx->slice_size;
204 for (
i = 0;
i < rounding;
i++)
207 for (
i = 0;
i < (rounding + 1) / 2;
i++)
209 for (
i = 0;
i < rounding / 2;
i++)
212 }
else if (rounding < 0) {
248 for (
i = 0;
i <
ctx->tile_cols;
i++) {
249 for (j = 0; j <
ctx->tile_rows; j++) {
255 ctx->row_bd[j] *
ctx->slice_block_cols;
259 "width:%2d height:%2d (%d blocks).\n",
index,
ctx->col_bd[
i],
328 if (
ctx->codec->picture_params_size > 0) {
333 ctx->codec->picture_params_size);
342 VAEncSequenceParameterBufferType,
343 ctx->codec_sequence_params,
344 ctx->codec->sequence_params_size);
350 for (
i = 0;
i <
ctx->nb_global_params;
i++) {
352 ctx->global_params_type[
i],
353 ctx->global_params[
i],
354 ctx->global_params_size[
i]);
360 if (
ctx->codec->init_picture_params) {
361 err =
ctx->codec->init_picture_params(avctx, base_pic);
364 "parameters: %d.\n", err);
368 VAEncPictureParameterBufferType,
370 ctx->codec->picture_params_size);
375 #if VA_CHECK_VERSION(1, 5, 0)
376 if (
ctx->max_frame_size) {
378 VAEncMiscParameterTypeMaxFrameSize,
380 sizeof(
ctx->mfs_params));
387 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
388 ctx->codec->write_sequence_header) {
389 bit_len = 8 *
sizeof(
data);
390 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
393 "header: %d.\n", err);
397 ctx->codec->sequence_header_type,
404 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
405 ctx->codec->write_picture_header) {
406 bit_len = 8 *
sizeof(
data);
407 err =
ctx->codec->write_picture_header(avctx, base_pic,
data, &bit_len);
410 "header: %d.\n", err);
414 ctx->codec->picture_header_type,
420 if (
ctx->codec->write_extra_buffer) {
424 err =
ctx->codec->write_extra_buffer(avctx, base_pic,
i, &
type,
430 "buffer %d: %d.\n",
i, err);
441 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
442 ctx->codec->write_extra_header) {
445 bit_len = 8 *
sizeof(
data);
446 err =
ctx->codec->write_extra_header(avctx, base_pic,
i, &
type,
452 "header %d: %d.\n",
i, err);
472 if (
ctx->tile_rows &&
ctx->tile_cols)
481 if (
ctx->codec->slice_params_size > 0) {
489 if (
ctx->codec->init_slice_params) {
490 err =
ctx->codec->init_slice_params(avctx, base_pic, slice);
493 "parameters: %d.\n", err);
498 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
499 ctx->codec->write_slice_header) {
500 bit_len = 8 *
sizeof(
data);
501 err =
ctx->codec->write_slice_header(avctx, pic, slice,
505 "header: %d.\n", err);
509 ctx->codec->slice_header_type,
515 if (
ctx->codec->init_slice_params) {
517 VAEncSliceParameterBufferType,
519 ctx->codec->slice_params_size);
525 #if VA_CHECK_VERSION(1, 0, 0)
531 VAEncMiscParameterBufferROI param_roi;
536 av_assert0(roi_size && sd->size % roi_size == 0);
537 nb_roi = sd->size / roi_size;
538 if (nb_roi >
ctx->roi_max_regions) {
541 "supported by driver (%d > %d).\n",
542 nb_roi,
ctx->roi_max_regions);
545 nb_roi =
ctx->roi_max_regions;
554 for (
i = 0;
i < nb_roi;
i++) {
562 pic->
roi[
i] = (VAEncROI) {
573 param_roi = (VAEncMiscParameterBufferROI) {
575 .max_delta_qp = INT8_MAX,
576 .min_delta_qp = INT8_MIN,
578 .roi_flags.bits.roi_value_is_qp_delta = 1,
582 VAEncMiscParameterTypeROI,
590 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
592 if (vas != VA_STATUS_SUCCESS) {
594 "%d (%s).\n", vas, vaErrorStr(vas));
596 goto fail_with_picture;
599 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
601 if (vas != VA_STATUS_SUCCESS) {
603 "%d (%s).\n", vas, vaErrorStr(vas));
605 goto fail_with_picture;
608 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
609 if (vas != VA_STATUS_SUCCESS) {
611 "%d (%s).\n", vas, vaErrorStr(vas));
615 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
622 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
625 vas = vaDestroyBuffer(
ctx->hwctx->display,
627 if (vas != VA_STATUS_SUCCESS) {
629 "param buffer %#x: %d (%s).\n",
639 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
660 VACodedBufferSegment *buf_list, *buf;
665 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
667 if (vas != VA_STATUS_SUCCESS) {
669 "%d (%s).\n", vas, vaErrorStr(vas));
674 for (buf = buf_list; buf; buf = buf->next)
677 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
678 if (vas != VA_STATUS_SUCCESS) {
680 "%d (%s).\n", vas, vaErrorStr(vas));
689 VABufferID buf_id, uint8_t **
dst)
692 VACodedBufferSegment *buf_list, *buf;
696 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
698 if (vas != VA_STATUS_SUCCESS) {
700 "%d (%s).\n", vas, vaErrorStr(vas));
705 for (buf = buf_list; buf; buf = buf->next) {
707 "(status %08x).\n", buf->size, buf->status);
709 memcpy(*
dst, buf->buf, buf->size);
713 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
714 if (vas != VA_STATUS_SUCCESS) {
716 "%d (%s).\n", vas, vaErrorStr(vas));
728 VABufferID output_buffer_prev;
733 if (
ctx->coded_buffer_ref) {
734 output_buffer_prev = *
ctx->coded_buffer_ref;
751 if (
ctx->coded_buffer_ref) {
825 "%"PRId64
"/%"PRId64
".\n",
841 if (
ctx->codec->picture_priv_data_size > 0) {
885 ctx->global_params_type[
ctx->nb_global_params] =
type;
887 ctx->global_params_size[
ctx->nb_global_params] =
size;
889 ++
ctx->nb_global_params;
902 {
"YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
903 {
"YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
904 {
"YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
905 #if VA_CHECK_VERSION(1, 2, 0)
906 {
"YUV420_12", VA_RT_FORMAT_YUV420_12, 12, 3, 1, 1 },
907 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
908 {
"YUV422_12", VA_RT_FORMAT_YUV422_12, 12, 3, 1, 0 },
909 {
"YUV444_10", VA_RT_FORMAT_YUV444_10, 10, 3, 0, 0 },
910 {
"YUV444_12", VA_RT_FORMAT_YUV444_12, 12, 3, 0, 0 },
912 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
913 {
"XYUV", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
914 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
915 #if VA_CHECK_VERSION(0, 38, 1)
916 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
921 VAEntrypointEncSlice,
922 VAEntrypointEncPicture,
923 #if VA_CHECK_VERSION(0, 39, 2)
924 VAEntrypointEncSliceLP,
928 #if VA_CHECK_VERSION(0, 39, 2)
929 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
930 VAEntrypointEncSliceLP,
939 VAProfile *va_profiles =
NULL;
940 VAEntrypoint *va_entrypoints =
NULL;
942 const VAEntrypoint *usable_entrypoints;
945 VAConfigAttrib rt_format_attr;
947 const char *profile_string, *entrypoint_string;
948 int i, j, n, depth, err;
951 if (
ctx->low_power) {
952 #if VA_CHECK_VERSION(0, 39, 2)
953 usable_entrypoints = vaapi_encode_entrypoints_low_power;
956 "supported with this VAAPI version.\n");
969 depth =
desc->comp[0].depth;
970 for (
i = 1;
i <
desc->nb_components;
i++) {
971 if (
desc->comp[
i].depth != depth) {
980 n = vaMaxNumProfiles(
ctx->hwctx->display);
986 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
987 if (vas != VA_STATUS_SUCCESS) {
989 vas, vaErrorStr(vas));
995 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1001 if (
desc->nb_components > 1 &&
1009 #if VA_CHECK_VERSION(1, 0, 0)
1010 profile_string = vaProfileStr(
profile->va_profile);
1012 profile_string =
"(no profile names)";
1015 for (j = 0; j < n; j++) {
1016 if (va_profiles[j] ==
profile->va_profile)
1021 "is not supported by driver.\n", profile_string,
1029 if (!
ctx->profile) {
1038 profile_string,
ctx->va_profile);
1040 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1042 if (!va_entrypoints) {
1046 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1047 va_entrypoints, &n);
1048 if (vas != VA_STATUS_SUCCESS) {
1050 "profile %s (%d): %d (%s).\n", profile_string,
1051 ctx->va_profile, vas, vaErrorStr(vas));
1056 for (
i = 0;
i < n;
i++) {
1057 for (j = 0; usable_entrypoints[j]; j++) {
1058 if (va_entrypoints[
i] == usable_entrypoints[j])
1061 if (usable_entrypoints[j])
1066 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1071 ctx->va_entrypoint = va_entrypoints[
i];
1072 #if VA_CHECK_VERSION(1, 0, 0)
1073 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1075 entrypoint_string =
"(no entrypoint names)";
1078 entrypoint_string,
ctx->va_entrypoint);
1082 if (rt_format->
depth == depth &&
1090 "found for profile %s (%d) entrypoint %s (%d).\n",
1091 profile_string,
ctx->va_profile,
1092 entrypoint_string,
ctx->va_entrypoint);
1097 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1098 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1099 ctx->va_profile,
ctx->va_entrypoint,
1100 &rt_format_attr, 1);
1101 if (vas != VA_STATUS_SUCCESS) {
1103 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1108 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1110 "supported by driver: assuming surface RT format %s "
1111 "is valid.\n", rt_format->
name);
1112 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1114 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1115 rt_format->
name, profile_string,
ctx->va_profile,
1116 entrypoint_string,
ctx->va_entrypoint);
1121 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1122 ctx->config_attributes[
ctx->nb_config_attributes++] =
1124 .type = VAConfigAttribRTFormat,
1125 .value = rt_format->
value,
1143 #if VA_CHECK_VERSION(1, 1, 0)
1148 #if VA_CHECK_VERSION(1, 3, 0)
1160 uint32_t supported_va_rc_modes;
1163 int rc_target_percentage;
1167 int64_t hrd_initial_buffer_fullness;
1169 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1171 char supported_rc_modes_string[64];
1173 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1174 ctx->va_profile,
ctx->va_entrypoint,
1176 if (vas != VA_STATUS_SUCCESS) {
1178 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1181 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1183 "supported rate control modes: assuming CQP only.\n");
1184 supported_va_rc_modes = VA_RC_CQP;
1185 strcpy(supported_rc_modes_string,
"unknown");
1187 char *str = supported_rc_modes_string;
1188 size_t len =
sizeof(supported_rc_modes_string);
1191 supported_va_rc_modes = rc_attr.value;
1193 #if VA_CHECK_VERSION(0, 39, 2)
1194 if (!(supported_va_rc_modes & VA_RC_MB)) {
1201 "or above, which can support BLBRC.\n");
1207 if (supported_va_rc_modes &
rc_mode->va_mode) {
1223 supported_rc_modes_string);
1238 #define TRY_RC_MODE(mode, fail) do { \
1239 rc_mode = &vaapi_encode_rc_modes[mode]; \
1240 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1242 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1243 "RC mode (supported modes: %s).\n", rc_mode->name, \
1244 supported_rc_modes_string); \
1245 return AVERROR(EINVAL); \
1247 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1248 "RC mode.\n", rc_mode->name); \
1251 goto rc_mode_found; \
1255 if (
ctx->explicit_rc_mode)
1258 if (
ctx->explicit_qp)
1288 "RC mode compatible with selected options "
1289 "(supported modes: %s).\n", supported_rc_modes_string);
1306 rc_bits_per_second = avctx->
bit_rate;
1312 rc_target_percentage = 100;
1319 }
else if (
rc_mode->maxrate) {
1323 "bitrate (%"PRId64
") must not be greater than "
1324 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1329 rc_target_percentage = (avctx->
bit_rate * 100) /
1336 rc_bits_per_second = 2 * avctx->
bit_rate;
1337 rc_target_percentage = 50;
1342 "in %s RC mode.\n",
rc_mode->name);
1344 rc_bits_per_second = avctx->
bit_rate;
1345 rc_target_percentage = 100;
1348 rc_bits_per_second = 0;
1349 rc_target_percentage = 100;
1353 if (
ctx->explicit_qp) {
1354 rc_quality =
ctx->explicit_qp;
1361 rc_quality =
ctx->codec->default_quality;
1363 "using default (%d).\n", rc_quality);
1379 "must have initial buffer size (%d) <= "
1380 "buffer size (%"PRId64
").\n",
1386 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1389 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1393 "in %s RC mode.\n",
rc_mode->name);
1396 hrd_buffer_size = 0;
1397 hrd_initial_buffer_fullness = 0;
1401 rc_window_size = 1000;
1405 if (rc_bits_per_second > UINT32_MAX ||
1406 hrd_buffer_size > UINT32_MAX ||
1407 hrd_initial_buffer_fullness > UINT32_MAX) {
1409 "greater are not supported by VAAPI.\n");
1414 ctx->rc_quality = rc_quality;
1416 ctx->va_bit_rate = rc_bits_per_second;
1420 if (
ctx->blbrc &&
ctx->va_rc_mode == VA_RC_CQP)
1424 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1427 ctx->config_attributes[
ctx->nb_config_attributes++] =
1429 .type = VAConfigAttribRateControl,
1430 #if VA_CHECK_VERSION(0, 39, 2)
1431 .value =
ctx->blbrc ?
ctx->va_rc_mode | VA_RC_MB :
ctx->va_rc_mode,
1433 .value =
ctx->va_rc_mode,
1441 if (
rc_mode->va_mode != VA_RC_CQP) {
1444 "converging in %d frames with %d%% accuracy.\n",
1445 rc_bits_per_second, rc_window_size,
1446 rc_target_percentage);
1447 }
else if (
rc_mode->bitrate) {
1449 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
1450 rc_bits_per_second, rc_window_size);
1453 ctx->rc_params = (VAEncMiscParameterRateControl) {
1454 .bits_per_second = rc_bits_per_second,
1455 .target_percentage = rc_target_percentage,
1456 .window_size = rc_window_size,
1458 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
1459 .basic_unit_size = 0,
1460 #
if VA_CHECK_VERSION(1, 1, 0)
1461 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
1462 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
1464 #
if VA_CHECK_VERSION(1, 3, 0)
1465 .quality_factor = rc_quality,
1467 #if VA_CHECK_VERSION(0, 39, 2)
1468 .rc_flags.bits.mb_rate_control =
ctx->blbrc ? 1 : 2,
1472 VAEncMiscParameterTypeRateControl,
1474 sizeof(
ctx->rc_params));
1479 "initial fullness %"PRId64
" bits.\n",
1480 hrd_buffer_size, hrd_initial_buffer_fullness);
1482 ctx->hrd_params = (VAEncMiscParameterHRD) {
1483 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1484 .buffer_size = hrd_buffer_size,
1487 VAEncMiscParameterTypeHRD,
1489 sizeof(
ctx->hrd_params));
1500 fr_num, fr_den, (
double)fr_num / fr_den);
1502 ctx->fr_params = (VAEncMiscParameterFrameRate) {
1503 .framerate = (
unsigned int)fr_den << 16 | fr_num,
1505 #if VA_CHECK_VERSION(0, 40, 0)
1507 VAEncMiscParameterTypeFrameRate,
1509 sizeof(
ctx->fr_params));
1517 #if VA_CHECK_VERSION(1, 5, 0)
1519 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
1522 if (
ctx->va_rc_mode == VA_RC_CQP) {
1523 ctx->max_frame_size = 0;
1529 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1533 if (vas != VA_STATUS_SUCCESS) {
1534 ctx->max_frame_size = 0;
1536 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1540 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1541 ctx->max_frame_size = 0;
1543 "is not supported.\n");
1546 VAConfigAttribValMaxFrameSize attr_mfs;
1547 attr_mfs.value = attr.value;
1549 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
1550 ctx->max_frame_size = 0;
1552 "max frame size which has not been implemented in FFmpeg.\n");
1556 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
1557 .max_frame_size =
ctx->max_frame_size * 8,
1561 ctx->max_frame_size);
1565 "this VAAPI version.\n");
1577 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
1578 uint32_t ref_l0, ref_l1;
1579 int prediction_pre_only, err;
1581 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1585 if (vas != VA_STATUS_SUCCESS) {
1587 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1591 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1592 ref_l0 = ref_l1 = 0;
1594 ref_l0 = attr.value & 0xffff;
1595 ref_l1 = attr.value >> 16 & 0xffff;
1599 prediction_pre_only = 0;
1601 #if VA_CHECK_VERSION(1, 9, 0)
1604 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
1605 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1609 if (vas != VA_STATUS_SUCCESS) {
1611 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1613 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1615 "prediction constraints.\n");
1617 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
1618 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
1620 "direction attribute.\n");
1624 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
1625 if (ref_l0 > 0 && ref_l1 > 0) {
1626 prediction_pre_only = 1;
1628 "lists for B-frames.\n");
1632 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
1633 if (ref_l0 > 0 && ref_l1 > 0) {
1636 "replacing them with B-frames.\n");
1644 ctx->codec->
flags, prediction_pre_only);
1652 uint32_t slice_structure)
1662 if (avctx->
slices >
ctx->slice_block_rows) {
1664 "configured number of slices (%d < %d); using "
1665 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
1666 req_slices =
ctx->slice_block_rows;
1668 req_slices = avctx->
slices;
1670 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
1671 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
1672 ctx->nb_slices = req_slices;
1673 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
1674 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
1676 for (k = 1;; k *= 2) {
1677 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
1680 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
1681 ctx->slice_size = k;
1682 #if VA_CHECK_VERSION(1, 0, 0)
1683 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
1684 ctx->nb_slices =
ctx->slice_block_rows;
1685 ctx->slice_size = 1;
1689 "slice structure modes (%#x).\n", slice_structure);
1697 uint32_t slice_structure)
1702 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
1703 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
1704 ctx->tile_cols == 1))) {
1706 "current tile requirement.\n", slice_structure);
1710 if (
ctx->tile_rows >
ctx->slice_block_rows ||
1711 ctx->tile_cols >
ctx->slice_block_cols) {
1713 "for configured number of tile (%d x %d); ",
1714 ctx->slice_block_rows,
ctx->slice_block_cols,
1715 ctx->tile_rows,
ctx->tile_cols);
1716 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
1717 ctx->slice_block_rows :
ctx->tile_rows;
1718 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
1719 ctx->slice_block_cols :
ctx->tile_cols;
1721 ctx->tile_rows,
ctx->tile_cols);
1724 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
1729 if (avctx->
slices != req_tiles)
1731 "mismatches with configured number of tile (%d != %d); "
1732 "using requested tile number for slice.\n",
1733 avctx->
slices, req_tiles);
1735 ctx->nb_slices = req_tiles;
1739 for (
i = 0;
i <
ctx->tile_cols;
i++) {
1740 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
1741 i *
ctx->slice_block_cols /
ctx->tile_cols;
1742 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
1745 for (
i = 0;
i <
ctx->tile_rows;
i++) {
1746 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
1747 i *
ctx->slice_block_rows /
ctx->tile_rows;
1748 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
1752 ctx->tile_rows,
ctx->tile_cols);
1761 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
1762 { VAConfigAttribEncSliceStructure },
1763 #if VA_CHECK_VERSION(1, 1, 0)
1764 { VAConfigAttribEncTileSupport },
1768 uint32_t max_slices, slice_structure;
1774 "but this codec does not support controlling slices.\n");
1786 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
1788 ctx->slice_size =
ctx->slice_block_rows;
1792 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1796 if (vas != VA_STATUS_SUCCESS) {
1798 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
1801 max_slices = attr[0].value;
1802 slice_structure = attr[1].value;
1803 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
1804 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
1806 "pictures as multiple slices.\n.");
1810 if (
ctx->tile_rows &&
ctx->tile_cols) {
1811 #if VA_CHECK_VERSION(1, 1, 0)
1812 uint32_t tile_support = attr[2].value;
1813 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
1815 "pictures as multiple tiles.\n.");
1820 "not supported with this VAAPI version.\n");
1825 if (
ctx->tile_rows &&
ctx->tile_cols)
1834 "%d (from %d) due to driver constraints on slice "
1835 "structure.\n",
ctx->nb_slices, avctx->
slices);
1837 if (
ctx->nb_slices > max_slices) {
1839 "encoding with %d slices (max %"PRIu32
").\n",
1840 ctx->nb_slices, max_slices);
1853 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
1855 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1859 if (vas != VA_STATUS_SUCCESS) {
1861 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1865 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1866 if (
ctx->desired_packed_headers) {
1868 "packed headers (wanted %#x).\n",
1869 ctx->desired_packed_headers);
1872 "packed headers (none wanted).\n");
1874 ctx->va_packed_headers = 0;
1876 if (
ctx->desired_packed_headers & ~attr.value) {
1878 "wanted packed headers (wanted %#x, found %#x).\n",
1879 ctx->desired_packed_headers, attr.value);
1882 "available (wanted %#x, found %#x).\n",
1883 ctx->desired_packed_headers, attr.value);
1885 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
1888 if (
ctx->va_packed_headers) {
1889 ctx->config_attributes[
ctx->nb_config_attributes++] =
1891 .type = VAConfigAttribEncPackedHeaders,
1892 .value =
ctx->va_packed_headers,
1896 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
1897 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
1900 "sequence headers, but a global header is requested.\n");
1902 "this may result in a stream which is not usable for some "
1903 "purposes (e.g. not muxable to some containers).\n");
1911 #if VA_CHECK_VERSION(0, 36, 0)
1914 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
1917 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1921 if (vas != VA_STATUS_SUCCESS) {
1923 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1927 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1930 "supported: will use default quality level.\n");
1935 "valid range is 0-%d, using %d.\n",
1936 attr.value, attr.value);
1940 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
1944 VAEncMiscParameterTypeQualityLevel,
1945 &
ctx->quality_params,
1946 sizeof(
ctx->quality_params));
1950 "not supported with this VAAPI version.\n");
1958 #if VA_CHECK_VERSION(1, 0, 0)
1962 VAConfigAttrib attr = { VAConfigAttribEncROI };
1964 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1968 if (vas != VA_STATUS_SUCCESS) {
1970 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1974 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1977 VAConfigAttribValEncROI roi = {
1978 .value = attr.value,
1981 ctx->roi_max_regions = roi.bits.num_roi_regions;
1983 (
ctx->va_rc_mode == VA_RC_CQP ||
1984 roi.bits.roi_rc_qp_delta_support);
1995 VABufferID *buffer_id_ref = obj;
1996 VABufferID buffer_id = *buffer_id_ref;
1998 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2008 VABufferID *buffer_id = obj;
2015 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2016 VAEncCodedBufferType,
2018 (1 << 16), 1, 0, buffer_id);
2019 if (vas != VA_STATUS_SUCCESS) {
2021 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2064 "frame context: %d.\n", err);
2103 ctx->va_config = VA_INVALID_ID;
2104 ctx->va_context = VA_INVALID_ID;
2114 if (
ctx->codec->get_encoder_caps) {
2115 err =
ctx->codec->get_encoder_caps(avctx);
2154 if (
ctx->max_frame_size) {
2160 vas = vaCreateConfig(
ctx->hwctx->display,
2161 ctx->va_profile,
ctx->va_entrypoint,
2162 ctx->config_attributes,
ctx->nb_config_attributes,
2164 if (vas != VA_STATUS_SUCCESS) {
2166 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2176 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2182 if (vas != VA_STATUS_SUCCESS) {
2184 "context: %d (%s).\n", vas, vaErrorStr(vas));
2189 ctx->output_buffer_pool =
2193 if (!
ctx->output_buffer_pool) {
2198 if (
ctx->codec->configure) {
2199 err =
ctx->codec->configure(avctx);
2207 if (
ctx->codec->sequence_params_size > 0) {
2208 ctx->codec_sequence_params =
2210 if (!
ctx->codec_sequence_params) {
2215 if (
ctx->codec->picture_params_size > 0) {
2216 ctx->codec_picture_params =
2218 if (!
ctx->codec_picture_params) {
2224 if (
ctx->codec->init_sequence_params) {
2225 err =
ctx->codec->init_sequence_params(avctx);
2228 "failed: %d.\n", err);
2233 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2234 ctx->codec->write_sequence_header &&
2237 size_t bit_len = 8 *
sizeof(
data);
2239 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2242 "for extradata: %d.\n", err);
2256 #if VA_CHECK_VERSION(1, 9, 0)
2258 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2259 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2283 if (!base_ctx->
frame)
2286 for (pic = base_ctx->
pic_start; pic; pic = next) {
2293 if (
ctx->va_context != VA_INVALID_ID) {
2295 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2296 ctx->va_context = VA_INVALID_ID;
2299 if (
ctx->va_config != VA_INVALID_ID) {
2301 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2302 ctx->va_config = VA_INVALID_ID;