19 #include "config_components.h"
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,
155 #if VA_CHECK_VERSION(1, 9, 0)
156 if (
ctx->has_sync_buffer_func) {
157 vas = vaSyncBuffer(
ctx->hwctx->display,
159 VA_TIMEOUT_INFINITE);
160 if (vas != VA_STATUS_SUCCESS) {
162 "%d (%s).\n", vas, vaErrorStr(vas));
169 if (vas != VA_STATUS_SUCCESS) {
171 "%d (%s).\n", vas, vaErrorStr(vas));
193 rounding =
ctx->slice_block_rows -
ctx->nb_slices *
ctx->slice_size;
201 for (
i = 0;
i < rounding;
i++)
204 for (
i = 0;
i < (rounding + 1) / 2;
i++)
206 for (
i = 0;
i < rounding / 2;
i++)
209 }
else if (rounding < 0) {
245 for (
i = 0;
i <
ctx->tile_cols;
i++) {
246 for (j = 0; j <
ctx->tile_rows; j++) {
252 ctx->row_bd[j] *
ctx->slice_block_cols;
256 "width:%2d height:%2d (%d blocks).\n",
index,
ctx->col_bd[
i],
321 if (
ctx->codec->picture_params_size > 0) {
326 ctx->codec->picture_params_size);
335 VAEncSequenceParameterBufferType,
336 ctx->codec_sequence_params,
337 ctx->codec->sequence_params_size);
343 for (
i = 0;
i <
ctx->nb_global_params;
i++) {
345 ctx->global_params_type[
i],
346 ctx->global_params[
i],
347 ctx->global_params_size[
i]);
353 if (
ctx->codec->init_picture_params) {
354 err =
ctx->codec->init_picture_params(avctx, pic);
357 "parameters: %d.\n", err);
361 VAEncPictureParameterBufferType,
363 ctx->codec->picture_params_size);
369 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
370 ctx->codec->write_sequence_header) {
371 bit_len = 8 *
sizeof(
data);
372 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
375 "header: %d.\n", err);
379 ctx->codec->sequence_header_type,
386 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
387 ctx->codec->write_picture_header) {
388 bit_len = 8 *
sizeof(
data);
389 err =
ctx->codec->write_picture_header(avctx, pic,
data, &bit_len);
392 "header: %d.\n", err);
396 ctx->codec->picture_header_type,
402 if (
ctx->codec->write_extra_buffer) {
406 err =
ctx->codec->write_extra_buffer(avctx, pic,
i, &
type,
412 "buffer %d: %d.\n",
i, err);
423 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
424 ctx->codec->write_extra_header) {
427 bit_len = 8 *
sizeof(
data);
428 err =
ctx->codec->write_extra_header(avctx, pic,
i, &
type,
434 "header %d: %d.\n",
i, err);
454 if (
ctx->tile_rows &&
ctx->tile_cols)
463 if (
ctx->codec->slice_params_size > 0) {
471 if (
ctx->codec->init_slice_params) {
472 err =
ctx->codec->init_slice_params(avctx, pic, slice);
475 "parameters: %d.\n", err);
480 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
481 ctx->codec->write_slice_header) {
482 bit_len = 8 *
sizeof(
data);
483 err =
ctx->codec->write_slice_header(avctx, pic, slice,
487 "header: %d.\n", err);
491 ctx->codec->slice_header_type,
497 if (
ctx->codec->init_slice_params) {
499 VAEncSliceParameterBufferType,
501 ctx->codec->slice_params_size);
507 #if VA_CHECK_VERSION(1, 0, 0)
510 if (sd &&
ctx->roi_allowed) {
513 VAEncMiscParameterBufferROI param_roi;
518 av_assert0(roi_size && sd->size % roi_size == 0);
519 nb_roi = sd->size / roi_size;
520 if (nb_roi >
ctx->roi_max_regions) {
521 if (!
ctx->roi_warned) {
523 "supported by driver (%d > %d).\n",
524 nb_roi,
ctx->roi_max_regions);
527 nb_roi =
ctx->roi_max_regions;
536 for (
i = 0;
i < nb_roi;
i++) {
544 pic->
roi[
i] = (VAEncROI) {
555 param_roi = (VAEncMiscParameterBufferROI) {
557 .max_delta_qp = INT8_MAX,
558 .min_delta_qp = INT8_MIN,
560 .roi_flags.bits.roi_value_is_qp_delta = 1,
564 VAEncMiscParameterTypeROI,
572 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
574 if (vas != VA_STATUS_SUCCESS) {
576 "%d (%s).\n", vas, vaErrorStr(vas));
578 goto fail_with_picture;
581 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
583 if (vas != VA_STATUS_SUCCESS) {
585 "%d (%s).\n", vas, vaErrorStr(vas));
587 goto fail_with_picture;
590 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
591 if (vas != VA_STATUS_SUCCESS) {
593 "%d (%s).\n", vas, vaErrorStr(vas));
597 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
604 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
607 vas = vaDestroyBuffer(
ctx->hwctx->display,
609 if (vas != VA_STATUS_SUCCESS) {
611 "param buffer %#x: %d (%s).\n",
623 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
646 VACodedBufferSegment *buf_list, *buf;
659 if (vas != VA_STATUS_SUCCESS) {
661 "%d (%s).\n", vas, vaErrorStr(vas));
666 for (buf = buf_list; buf; buf = buf->next)
667 total_size += buf->size;
675 for (buf = buf_list; buf; buf = buf->next) {
677 "(status %08x).\n", buf->size, buf->status);
679 memcpy(ptr, buf->buf, buf->size);
689 if (vas != VA_STATUS_SUCCESS) {
691 "%d (%s).\n", vas, vaErrorStr(vas));
718 "%"PRId64
"/%"PRId64
".\n",
737 if (
ctx->codec->picture_priv_data_size > 0) {
786 int is_ref,
int in_dpb,
int prev)
859 if (current_depth ==
ctx->max_b_depth || start->
next->
next == end) {
860 for (pic = start->
next; pic; pic = pic->
next) {
879 for (pic = start->
next; pic != end; pic = pic->
next)
881 for (pic = start->
next,
i = 1; 2 * i < len; pic = pic->next,
i++);
898 current_depth + 1, &next);
903 current_depth + 1, last);
912 int i, b_counter, closed_gop_end;
917 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
932 "encode next.\n", pic->
b_depth);
941 closed_gop_end =
ctx->closed_gop ||
942 ctx->idr_counter ==
ctx->gop_per_idr;
943 for (pic =
ctx->pic_start; pic; pic = next) {
953 if (b_counter ==
ctx->b_per_p)
957 if (
ctx->gop_counter + b_counter + closed_gop_end >=
ctx->gop_size)
961 if (next && next->force_idr)
967 if (!pic &&
ctx->end_of_stream) {
978 "need more input for reference pictures.\n");
981 if (
ctx->input_order <=
ctx->decode_delay && !
ctx->end_of_stream) {
983 "need more input for timestamps.\n");
991 ctx->idr_counter = 1;
992 ctx->gop_counter = 1;
994 }
else if (
ctx->gop_counter + b_counter >=
ctx->gop_size) {
995 if (
ctx->idr_counter ==
ctx->gop_per_idr) {
999 ctx->idr_counter = 1;
1006 ctx->gop_counter = 1;
1009 if (
ctx->gop_counter + b_counter + closed_gop_end ==
ctx->gop_size) {
1018 ctx->gop_counter += 1 + b_counter;
1031 --
ctx->next_prev->ref_count[0];
1033 if (b_counter > 0) {
1037 ctx->next_prev = pic;
1039 ++
ctx->next_prev->ref_count[0];
1051 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1057 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1064 for (pic =
ctx->pic_start; pic; pic = next) {
1071 ctx->pic_start = next;
1086 if ((
frame->crop_top ||
frame->crop_bottom ||
1087 frame->crop_left ||
frame->crop_right) && !
ctx->crop_warned) {
1089 "frames ignored due to lack of API support.\n");
1090 ctx->crop_warned = 1;
1093 if (!
ctx->roi_allowed) {
1097 if (sd && !
ctx->roi_warned) {
1099 "frames ignored due to lack of driver support.\n");
1100 ctx->roi_warned = 1;
1139 if (
ctx->input_order == 0)
1140 ctx->first_pts = pic->
pts;
1141 if (
ctx->input_order ==
ctx->decode_delay)
1142 ctx->dts_pts_diff = pic->
pts -
ctx->first_pts;
1143 if (
ctx->output_delay > 0)
1144 ctx->ts_ring[
ctx->input_order %
1145 (3 *
ctx->output_delay +
ctx->async_depth)] = pic->
pts;
1150 if (
ctx->pic_start) {
1151 ctx->pic_end->next = pic;
1154 ctx->pic_start = pic;
1159 ctx->end_of_stream = 1;
1163 if (
ctx->input_order <
ctx->decode_delay)
1164 ctx->dts_pts_diff =
ctx->pic_end->pts -
ctx->first_pts;
1192 if (!
ctx->pic_start) {
1193 if (
ctx->end_of_stream)
1199 if (
ctx->has_sync_buffer_func) {
1248 if (
ctx->output_delay == 0) {
1257 (3 *
ctx->output_delay +
ctx->async_depth)];
1276 ctx->global_params_type[
ctx->nb_global_params] =
type;
1278 ctx->global_params_size[
ctx->nb_global_params] =
size;
1280 ++
ctx->nb_global_params;
1293 {
"YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
1294 {
"YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
1295 {
"YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
1296 #if VA_CHECK_VERSION(1, 2, 0)
1297 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1299 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1300 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1301 #if VA_CHECK_VERSION(0, 38, 1)
1302 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1307 VAEntrypointEncSlice,
1308 VAEntrypointEncPicture,
1309 #if VA_CHECK_VERSION(0, 39, 2)
1310 VAEntrypointEncSliceLP,
1314 #if VA_CHECK_VERSION(0, 39, 2)
1315 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1316 VAEntrypointEncSliceLP,
1324 VAProfile *va_profiles =
NULL;
1325 VAEntrypoint *va_entrypoints =
NULL;
1327 const VAEntrypoint *usable_entrypoints;
1330 VAConfigAttrib rt_format_attr;
1332 const char *profile_string, *entrypoint_string;
1333 int i, j, n, depth, err;
1336 if (
ctx->low_power) {
1337 #if VA_CHECK_VERSION(0, 39, 2)
1338 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1341 "supported with this VAAPI version.\n");
1351 ctx->input_frames->sw_format);
1354 depth =
desc->comp[0].depth;
1355 for (
i = 1;
i <
desc->nb_components;
i++) {
1356 if (
desc->comp[
i].depth != depth) {
1365 n = vaMaxNumProfiles(
ctx->hwctx->display);
1371 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
1372 if (vas != VA_STATUS_SUCCESS) {
1374 vas, vaErrorStr(vas));
1380 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1383 if (depth !=
profile->depth ||
1386 if (
desc->nb_components > 1 &&
1394 #if VA_CHECK_VERSION(1, 0, 0)
1395 profile_string = vaProfileStr(
profile->va_profile);
1397 profile_string =
"(no profile names)";
1400 for (j = 0; j < n; j++) {
1401 if (va_profiles[j] ==
profile->va_profile)
1406 "is not supported by driver.\n", profile_string,
1414 if (!
ctx->profile) {
1423 profile_string,
ctx->va_profile);
1425 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1427 if (!va_entrypoints) {
1431 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1432 va_entrypoints, &n);
1433 if (vas != VA_STATUS_SUCCESS) {
1435 "profile %s (%d): %d (%s).\n", profile_string,
1436 ctx->va_profile, vas, vaErrorStr(vas));
1441 for (
i = 0;
i < n;
i++) {
1442 for (j = 0; usable_entrypoints[j]; j++) {
1443 if (va_entrypoints[
i] == usable_entrypoints[j])
1446 if (usable_entrypoints[j])
1451 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1456 ctx->va_entrypoint = va_entrypoints[
i];
1457 #if VA_CHECK_VERSION(1, 0, 0)
1458 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1460 entrypoint_string =
"(no entrypoint names)";
1463 entrypoint_string,
ctx->va_entrypoint);
1467 if (rt_format->
depth == depth &&
1475 "found for profile %s (%d) entrypoint %s (%d).\n",
1476 profile_string,
ctx->va_profile,
1477 entrypoint_string,
ctx->va_entrypoint);
1482 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1483 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1484 ctx->va_profile,
ctx->va_entrypoint,
1485 &rt_format_attr, 1);
1486 if (vas != VA_STATUS_SUCCESS) {
1488 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1493 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1495 "supported by driver: assuming surface RT format %s "
1496 "is valid.\n", rt_format->
name);
1497 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1499 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1500 rt_format->
name, profile_string,
ctx->va_profile,
1501 entrypoint_string,
ctx->va_entrypoint);
1506 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1507 ctx->config_attributes[
ctx->nb_config_attributes++] =
1509 .type = VAConfigAttribRTFormat,
1510 .value = rt_format->
value,
1528 #if VA_CHECK_VERSION(1, 1, 0)
1533 #if VA_CHECK_VERSION(1, 3, 0)
1545 uint32_t supported_va_rc_modes;
1547 int64_t rc_bits_per_second;
1548 int rc_target_percentage;
1551 int64_t hrd_buffer_size;
1552 int64_t hrd_initial_buffer_fullness;
1554 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1556 char supported_rc_modes_string[64];
1558 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1559 ctx->va_profile,
ctx->va_entrypoint,
1561 if (vas != VA_STATUS_SUCCESS) {
1563 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1566 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1568 "supported rate control modes: assuming CQP only.\n");
1569 supported_va_rc_modes = VA_RC_CQP;
1570 strcpy(supported_rc_modes_string,
"unknown");
1572 char *
str = supported_rc_modes_string;
1573 size_t len =
sizeof(supported_rc_modes_string);
1576 supported_va_rc_modes = rc_attr.value;
1579 if (supported_va_rc_modes &
rc_mode->va_mode) {
1595 supported_rc_modes_string);
1610 #define TRY_RC_MODE(mode, fail) do { \
1611 rc_mode = &vaapi_encode_rc_modes[mode]; \
1612 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1614 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1615 "RC mode (supported modes: %s).\n", rc_mode->name, \
1616 supported_rc_modes_string); \
1617 return AVERROR(EINVAL); \
1619 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1620 "RC mode.\n", rc_mode->name); \
1623 goto rc_mode_found; \
1627 if (
ctx->explicit_rc_mode)
1630 if (
ctx->explicit_qp)
1660 "RC mode compatible with selected options "
1661 "(supported modes: %s).\n", supported_rc_modes_string);
1678 rc_bits_per_second = avctx->
bit_rate;
1684 rc_target_percentage = 100;
1691 }
else if (
rc_mode->maxrate) {
1695 "bitrate (%"PRId64
") must not be greater than "
1696 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1701 rc_target_percentage = (avctx->
bit_rate * 100) /
1708 rc_bits_per_second = 2 * avctx->
bit_rate;
1709 rc_target_percentage = 50;
1714 "in %s RC mode.\n",
rc_mode->name);
1716 rc_bits_per_second = avctx->
bit_rate;
1717 rc_target_percentage = 100;
1720 rc_bits_per_second = 0;
1721 rc_target_percentage = 100;
1725 if (
ctx->explicit_qp) {
1726 rc_quality =
ctx->explicit_qp;
1730 rc_quality =
ctx->codec->default_quality;
1732 "using default (%d).\n", rc_quality);
1748 "must have initial buffer size (%d) <= "
1749 "buffer size (%"PRId64
").\n",
1755 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1758 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1762 "in %s RC mode.\n",
rc_mode->name);
1765 hrd_buffer_size = 0;
1766 hrd_initial_buffer_fullness = 0;
1770 rc_window_size = 1000;
1774 if (rc_bits_per_second > UINT32_MAX ||
1775 hrd_buffer_size > UINT32_MAX ||
1776 hrd_initial_buffer_fullness > UINT32_MAX) {
1778 "greater are not supported by VAAPI.\n");
1783 ctx->rc_quality = rc_quality;
1785 ctx->va_bit_rate = rc_bits_per_second;
1788 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1791 ctx->config_attributes[
ctx->nb_config_attributes++] =
1793 .type = VAConfigAttribRateControl,
1794 .value =
ctx->va_rc_mode,
1801 if (
rc_mode->va_mode != VA_RC_CQP) {
1804 "converging in %d frames with %d%% accuracy.\n",
1805 rc_bits_per_second, rc_window_size,
1806 rc_target_percentage);
1807 }
else if (
rc_mode->bitrate) {
1809 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
1810 rc_bits_per_second, rc_window_size);
1813 ctx->rc_params = (VAEncMiscParameterRateControl) {
1814 .bits_per_second = rc_bits_per_second,
1815 .target_percentage = rc_target_percentage,
1816 .window_size = rc_window_size,
1818 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
1819 .basic_unit_size = 0,
1820 #
if VA_CHECK_VERSION(1, 1, 0)
1821 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
1822 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
1824 #
if VA_CHECK_VERSION(1, 3, 0)
1825 .quality_factor = rc_quality,
1829 VAEncMiscParameterTypeRateControl,
1831 sizeof(
ctx->rc_params));
1836 "initial fullness %"PRId64
" bits.\n",
1837 hrd_buffer_size, hrd_initial_buffer_fullness);
1839 ctx->hrd_params = (VAEncMiscParameterHRD) {
1840 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1841 .buffer_size = hrd_buffer_size,
1844 VAEncMiscParameterTypeHRD,
1846 sizeof(
ctx->hrd_params));
1857 fr_num, fr_den, (
double)fr_num / fr_den);
1859 ctx->fr_params = (VAEncMiscParameterFrameRate) {
1860 .framerate = (
unsigned int)fr_den << 16 | fr_num,
1862 #if VA_CHECK_VERSION(0, 40, 0)
1864 VAEncMiscParameterTypeFrameRate,
1866 sizeof(
ctx->fr_params));
1876 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
1877 uint32_t ref_l0, ref_l1;
1878 int prediction_pre_only;
1880 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1884 if (vas != VA_STATUS_SUCCESS) {
1886 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1890 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1891 ref_l0 = ref_l1 = 0;
1893 ref_l0 = attr.value & 0xffff;
1894 ref_l1 = attr.value >> 16 & 0xffff;
1898 prediction_pre_only = 0;
1900 #if VA_CHECK_VERSION(1, 9, 0)
1903 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
1904 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1908 if (vas != VA_STATUS_SUCCESS) {
1910 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1912 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1914 "prediction constraints.\n");
1916 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
1917 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
1919 "direction attribute.\n");
1923 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
1924 if (ref_l0 > 0 && ref_l1 > 0) {
1925 prediction_pre_only = 1;
1927 "lists for B-frames.\n");
1931 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
1932 if (ref_l0 > 0 && ref_l1 > 0) {
1935 "replacing them with B-frames.\n");
1946 }
else if (ref_l0 < 1) {
1948 "reference frames.\n");
1951 ref_l1 < 1 || avctx->max_b_frames < 1 ||
1952 prediction_pre_only) {
1955 "(supported references: %d / %d).\n",
1959 "(supported references: %d / %d).\n", ref_l0, ref_l1);
1961 ctx->p_per_i = INT_MAX;
1966 "(supported references: %d / %d).\n",
1970 "(supported references: %d / %d).\n", ref_l0, ref_l1);
1972 ctx->p_per_i = INT_MAX;
1978 ctx->max_b_depth = 1;
1984 ctx->gop_per_idr =
ctx->idr_interval + 1;
1986 ctx->closed_gop = 1;
1987 ctx->gop_per_idr = 1;
1994 uint32_t slice_structure)
2004 if (avctx->
slices >
ctx->slice_block_rows) {
2006 "configured number of slices (%d < %d); using "
2007 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2008 req_slices =
ctx->slice_block_rows;
2010 req_slices = avctx->
slices;
2012 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2013 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2014 ctx->nb_slices = req_slices;
2015 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2016 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2018 for (k = 1;; k *= 2) {
2019 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2022 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2023 ctx->slice_size = k;
2024 #if VA_CHECK_VERSION(1, 0, 0)
2025 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2026 ctx->nb_slices =
ctx->slice_block_rows;
2027 ctx->slice_size = 1;
2031 "slice structure modes (%#x).\n", slice_structure);
2039 uint32_t slice_structure)
2044 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2045 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2046 ctx->tile_cols == 1))) {
2048 "current tile requirement.\n", slice_structure);
2052 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2053 ctx->tile_cols >
ctx->slice_block_cols) {
2055 "for configured number of tile (%d x %d); ",
2056 ctx->slice_block_rows,
ctx->slice_block_cols,
2057 ctx->tile_rows,
ctx->tile_cols);
2058 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2059 ctx->slice_block_rows :
ctx->tile_rows;
2060 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2061 ctx->slice_block_cols :
ctx->tile_cols;
2063 ctx->tile_rows,
ctx->tile_cols);
2066 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2071 if (avctx->
slices != req_tiles)
2073 "mismatches with configured number of tile (%d != %d); "
2074 "using requested tile number for slice.\n",
2075 avctx->
slices, req_tiles);
2077 ctx->nb_slices = req_tiles;
2081 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2082 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2083 i *
ctx->slice_block_cols /
ctx->tile_cols;
2084 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2087 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2088 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2089 i *
ctx->slice_block_rows /
ctx->tile_rows;
2090 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2094 ctx->tile_rows,
ctx->tile_cols);
2102 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2103 { VAConfigAttribEncSliceStructure },
2104 #if VA_CHECK_VERSION(1, 1, 0)
2105 { VAConfigAttribEncTileSupport },
2109 uint32_t max_slices, slice_structure;
2115 "but this codec does not support controlling slices.\n");
2122 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2123 ctx->slice_block_height;
2124 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2125 ctx->slice_block_width;
2127 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2129 ctx->slice_size =
ctx->slice_block_rows;
2133 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2137 if (vas != VA_STATUS_SUCCESS) {
2139 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2142 max_slices = attr[0].value;
2143 slice_structure = attr[1].value;
2144 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2145 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2147 "pictures as multiple slices.\n.");
2151 if (
ctx->tile_rows &&
ctx->tile_cols) {
2152 #if VA_CHECK_VERSION(1, 1, 0)
2153 uint32_t tile_support = attr[2].value;
2154 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2156 "pictures as multiple tiles.\n.");
2161 "not supported with this VAAPI version.\n");
2166 if (
ctx->tile_rows &&
ctx->tile_cols)
2175 "%d (from %d) due to driver constraints on slice "
2176 "structure.\n",
ctx->nb_slices, avctx->
slices);
2178 if (
ctx->nb_slices > max_slices) {
2180 "encoding with %d slices (max %"PRIu32
").\n",
2181 ctx->nb_slices, max_slices);
2194 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2196 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2200 if (vas != VA_STATUS_SUCCESS) {
2202 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2206 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2207 if (
ctx->desired_packed_headers) {
2209 "packed headers (wanted %#x).\n",
2210 ctx->desired_packed_headers);
2213 "packed headers (none wanted).\n");
2215 ctx->va_packed_headers = 0;
2217 if (
ctx->desired_packed_headers & ~attr.value) {
2219 "wanted packed headers (wanted %#x, found %#x).\n",
2220 ctx->desired_packed_headers, attr.value);
2223 "available (wanted %#x, found %#x).\n",
2224 ctx->desired_packed_headers, attr.value);
2226 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2229 if (
ctx->va_packed_headers) {
2230 ctx->config_attributes[
ctx->nb_config_attributes++] =
2232 .type = VAConfigAttribEncPackedHeaders,
2233 .value =
ctx->va_packed_headers,
2237 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2238 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2241 "sequence headers, but a global header is requested.\n");
2243 "this may result in a stream which is not usable for some "
2244 "purposes (e.g. not muxable to some containers).\n");
2252 #if VA_CHECK_VERSION(0, 36, 0)
2255 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2258 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2262 if (vas != VA_STATUS_SUCCESS) {
2264 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2268 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2271 "supported: will use default quality level.\n");
2276 "valid range is 0-%d, using %d.\n",
2277 attr.value, attr.value);
2281 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2285 VAEncMiscParameterTypeQualityLevel,
2286 &
ctx->quality_params,
2287 sizeof(
ctx->quality_params));
2291 "not supported with this VAAPI version.\n");
2299 #if VA_CHECK_VERSION(1, 0, 0)
2302 VAConfigAttrib attr = { VAConfigAttribEncROI };
2304 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2308 if (vas != VA_STATUS_SUCCESS) {
2310 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2314 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2315 ctx->roi_allowed = 0;
2317 VAConfigAttribValEncROI roi = {
2318 .value = attr.value,
2321 ctx->roi_max_regions = roi.bits.num_roi_regions;
2322 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2323 (
ctx->va_rc_mode == VA_RC_CQP ||
2324 roi.bits.roi_rc_qp_delta_support);
2335 VABufferID buffer_id;
2337 buffer_id = (VABufferID)(uintptr_t)
data;
2339 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2349 VABufferID buffer_id;
2357 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2358 VAEncCodedBufferType,
2359 3 *
ctx->surface_width *
ctx->surface_height +
2360 (1 << 16), 1, 0, &buffer_id);
2361 if (vas != VA_STATUS_SUCCESS) {
2363 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2374 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2409 if (
ctx->input_frames->sw_format ==
2411 recon_format =
ctx->input_frames->sw_format;
2422 recon_format =
ctx->input_frames->sw_format;
2432 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2433 ctx->surface_width,
ctx->surface_height,
2444 if (!
ctx->recon_frames_ref) {
2451 ctx->recon_frames->sw_format = recon_format;
2452 ctx->recon_frames->width =
ctx->surface_width;
2453 ctx->recon_frames->height =
ctx->surface_height;
2458 "frame context: %d.\n", err);
2476 ctx->va_config = VA_INVALID_ID;
2477 ctx->va_context = VA_INVALID_ID;
2488 "required to associate the encoding device.\n");
2493 if (!
ctx->input_frames_ref) {
2500 if (!
ctx->device_ref) {
2505 ctx->hwctx =
ctx->device->hwctx;
2511 if (
ctx->codec->get_encoder_caps) {
2512 err =
ctx->codec->get_encoder_caps(avctx);
2520 ctx->slice_block_width = 16;
2521 ctx->slice_block_height = 16;
2551 vas = vaCreateConfig(
ctx->hwctx->display,
2552 ctx->va_profile,
ctx->va_entrypoint,
2553 ctx->config_attributes,
ctx->nb_config_attributes,
2555 if (vas != VA_STATUS_SUCCESS) {
2557 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2566 recon_hwctx =
ctx->recon_frames->hwctx;
2567 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2568 ctx->surface_width,
ctx->surface_height,
2573 if (vas != VA_STATUS_SUCCESS) {
2575 "context: %d (%s).\n", vas, vaErrorStr(vas));
2580 ctx->output_buffer_pool =
2583 if (!
ctx->output_buffer_pool) {
2588 if (
ctx->codec->configure) {
2589 err =
ctx->codec->configure(avctx);
2594 ctx->output_delay =
ctx->b_per_p;
2595 ctx->decode_delay =
ctx->max_b_depth;
2597 if (
ctx->codec->sequence_params_size > 0) {
2598 ctx->codec_sequence_params =
2600 if (!
ctx->codec_sequence_params) {
2605 if (
ctx->codec->picture_params_size > 0) {
2606 ctx->codec_picture_params =
2608 if (!
ctx->codec_picture_params) {
2614 if (
ctx->codec->init_sequence_params) {
2615 err =
ctx->codec->init_sequence_params(avctx);
2618 "failed: %d.\n", err);
2623 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2624 ctx->codec->write_sequence_header &&
2627 size_t bit_len = 8 *
sizeof(
data);
2629 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2632 "for extradata: %d.\n", err);
2646 #if VA_CHECK_VERSION(1, 9, 0)
2648 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2649 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2650 ctx->has_sync_buffer_func = 1;
2654 if (!
ctx->encode_fifo)
2675 for (pic =
ctx->pic_start; pic; pic = next) {
2682 if (
ctx->va_context != VA_INVALID_ID) {
2683 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2684 ctx->va_context = VA_INVALID_ID;
2687 if (
ctx->va_config != VA_INVALID_ID) {
2688 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2689 ctx->va_config = VA_INVALID_ID;