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);
368 #if VA_CHECK_VERSION(1, 5, 0)
369 if (
ctx->max_frame_size) {
371 VAEncMiscParameterTypeMaxFrameSize,
373 sizeof(
ctx->mfs_params));
380 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
381 ctx->codec->write_sequence_header) {
382 bit_len = 8 *
sizeof(
data);
383 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
386 "header: %d.\n", err);
390 ctx->codec->sequence_header_type,
397 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
398 ctx->codec->write_picture_header) {
399 bit_len = 8 *
sizeof(
data);
400 err =
ctx->codec->write_picture_header(avctx, pic,
data, &bit_len);
403 "header: %d.\n", err);
407 ctx->codec->picture_header_type,
413 if (
ctx->codec->write_extra_buffer) {
417 err =
ctx->codec->write_extra_buffer(avctx, pic,
i, &
type,
423 "buffer %d: %d.\n",
i, err);
434 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
435 ctx->codec->write_extra_header) {
438 bit_len = 8 *
sizeof(
data);
439 err =
ctx->codec->write_extra_header(avctx, pic,
i, &
type,
445 "header %d: %d.\n",
i, err);
465 if (
ctx->tile_rows &&
ctx->tile_cols)
474 if (
ctx->codec->slice_params_size > 0) {
482 if (
ctx->codec->init_slice_params) {
483 err =
ctx->codec->init_slice_params(avctx, pic, slice);
486 "parameters: %d.\n", err);
491 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
492 ctx->codec->write_slice_header) {
493 bit_len = 8 *
sizeof(
data);
494 err =
ctx->codec->write_slice_header(avctx, pic, slice,
498 "header: %d.\n", err);
502 ctx->codec->slice_header_type,
508 if (
ctx->codec->init_slice_params) {
510 VAEncSliceParameterBufferType,
512 ctx->codec->slice_params_size);
518 #if VA_CHECK_VERSION(1, 0, 0)
521 if (sd &&
ctx->roi_allowed) {
524 VAEncMiscParameterBufferROI param_roi;
529 av_assert0(roi_size && sd->size % roi_size == 0);
530 nb_roi = sd->size / roi_size;
531 if (nb_roi >
ctx->roi_max_regions) {
532 if (!
ctx->roi_warned) {
534 "supported by driver (%d > %d).\n",
535 nb_roi,
ctx->roi_max_regions);
538 nb_roi =
ctx->roi_max_regions;
547 for (
i = 0;
i < nb_roi;
i++) {
555 pic->
roi[
i] = (VAEncROI) {
566 param_roi = (VAEncMiscParameterBufferROI) {
568 .max_delta_qp = INT8_MAX,
569 .min_delta_qp = INT8_MIN,
571 .roi_flags.bits.roi_value_is_qp_delta = 1,
575 VAEncMiscParameterTypeROI,
583 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
585 if (vas != VA_STATUS_SUCCESS) {
587 "%d (%s).\n", vas, vaErrorStr(vas));
589 goto fail_with_picture;
592 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
594 if (vas != VA_STATUS_SUCCESS) {
596 "%d (%s).\n", vas, vaErrorStr(vas));
598 goto fail_with_picture;
601 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
602 if (vas != VA_STATUS_SUCCESS) {
604 "%d (%s).\n", vas, vaErrorStr(vas));
608 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
615 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
618 vas = vaDestroyBuffer(
ctx->hwctx->display,
620 if (vas != VA_STATUS_SUCCESS) {
622 "param buffer %#x: %d (%s).\n",
634 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
657 VACodedBufferSegment *buf_list, *buf;
670 if (vas != VA_STATUS_SUCCESS) {
672 "%d (%s).\n", vas, vaErrorStr(vas));
677 for (buf = buf_list; buf; buf = buf->next)
678 total_size += buf->size;
686 for (buf = buf_list; buf; buf = buf->next) {
688 "(status %08x).\n", buf->size, buf->status);
690 memcpy(ptr, buf->buf, buf->size);
700 if (vas != VA_STATUS_SUCCESS) {
702 "%d (%s).\n", vas, vaErrorStr(vas));
729 "%"PRId64
"/%"PRId64
".\n",
748 if (
ctx->codec->picture_priv_data_size > 0) {
797 int is_ref,
int in_dpb,
int prev)
870 if (current_depth ==
ctx->max_b_depth || start->
next->
next == end) {
871 for (pic = start->
next; pic; pic = pic->
next) {
890 for (pic = start->
next; pic != end; pic = pic->
next)
892 for (pic = start->
next,
i = 1; 2 * i < len; pic = pic->next,
i++);
909 current_depth + 1, &next);
914 current_depth + 1, last);
923 int i, b_counter, closed_gop_end;
928 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
943 "encode next.\n", pic->
b_depth);
952 closed_gop_end =
ctx->closed_gop ||
953 ctx->idr_counter ==
ctx->gop_per_idr;
954 for (pic =
ctx->pic_start; pic; pic = next) {
964 if (b_counter ==
ctx->b_per_p)
968 if (
ctx->gop_counter + b_counter + closed_gop_end >=
ctx->gop_size)
972 if (next && next->force_idr)
978 if (!pic &&
ctx->end_of_stream) {
989 "need more input for reference pictures.\n");
992 if (
ctx->input_order <=
ctx->decode_delay && !
ctx->end_of_stream) {
994 "need more input for timestamps.\n");
1002 ctx->idr_counter = 1;
1003 ctx->gop_counter = 1;
1005 }
else if (
ctx->gop_counter + b_counter >=
ctx->gop_size) {
1006 if (
ctx->idr_counter ==
ctx->gop_per_idr) {
1010 ctx->idr_counter = 1;
1017 ctx->gop_counter = 1;
1020 if (
ctx->gop_counter + b_counter + closed_gop_end ==
ctx->gop_size) {
1029 ctx->gop_counter += 1 + b_counter;
1042 --
ctx->next_prev->ref_count[0];
1044 if (b_counter > 0) {
1048 ctx->next_prev = pic;
1050 ++
ctx->next_prev->ref_count[0];
1062 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1068 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1075 for (pic =
ctx->pic_start; pic; pic = next) {
1082 ctx->pic_start = next;
1097 if ((
frame->crop_top ||
frame->crop_bottom ||
1098 frame->crop_left ||
frame->crop_right) && !
ctx->crop_warned) {
1100 "frames ignored due to lack of API support.\n");
1101 ctx->crop_warned = 1;
1104 if (!
ctx->roi_allowed) {
1108 if (sd && !
ctx->roi_warned) {
1110 "frames ignored due to lack of driver support.\n");
1111 ctx->roi_warned = 1;
1150 if (
ctx->input_order == 0)
1151 ctx->first_pts = pic->
pts;
1152 if (
ctx->input_order ==
ctx->decode_delay)
1153 ctx->dts_pts_diff = pic->
pts -
ctx->first_pts;
1154 if (
ctx->output_delay > 0)
1155 ctx->ts_ring[
ctx->input_order %
1156 (3 *
ctx->output_delay +
ctx->async_depth)] = pic->
pts;
1161 if (
ctx->pic_start) {
1162 ctx->pic_end->next = pic;
1165 ctx->pic_start = pic;
1170 ctx->end_of_stream = 1;
1174 if (
ctx->input_order <
ctx->decode_delay)
1175 ctx->dts_pts_diff =
ctx->pic_end->pts -
ctx->first_pts;
1203 if (!
ctx->pic_start) {
1204 if (
ctx->end_of_stream)
1210 if (
ctx->has_sync_buffer_func) {
1259 if (
ctx->output_delay == 0) {
1268 (3 *
ctx->output_delay +
ctx->async_depth)];
1287 ctx->global_params_type[
ctx->nb_global_params] =
type;
1289 ctx->global_params_size[
ctx->nb_global_params] =
size;
1291 ++
ctx->nb_global_params;
1304 {
"YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
1305 {
"YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
1306 {
"YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
1307 #if VA_CHECK_VERSION(1, 2, 0)
1308 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1310 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1311 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1312 #if VA_CHECK_VERSION(0, 38, 1)
1313 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1318 VAEntrypointEncSlice,
1319 VAEntrypointEncPicture,
1320 #if VA_CHECK_VERSION(0, 39, 2)
1321 VAEntrypointEncSliceLP,
1325 #if VA_CHECK_VERSION(0, 39, 2)
1326 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1327 VAEntrypointEncSliceLP,
1335 VAProfile *va_profiles =
NULL;
1336 VAEntrypoint *va_entrypoints =
NULL;
1338 const VAEntrypoint *usable_entrypoints;
1341 VAConfigAttrib rt_format_attr;
1343 const char *profile_string, *entrypoint_string;
1344 int i, j, n, depth, err;
1347 if (
ctx->low_power) {
1348 #if VA_CHECK_VERSION(0, 39, 2)
1349 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1352 "supported with this VAAPI version.\n");
1362 ctx->input_frames->sw_format);
1365 depth =
desc->comp[0].depth;
1366 for (
i = 1;
i <
desc->nb_components;
i++) {
1367 if (
desc->comp[
i].depth != depth) {
1376 n = vaMaxNumProfiles(
ctx->hwctx->display);
1382 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
1383 if (vas != VA_STATUS_SUCCESS) {
1385 vas, vaErrorStr(vas));
1391 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1394 if (depth !=
profile->depth ||
1397 if (
desc->nb_components > 1 &&
1405 #if VA_CHECK_VERSION(1, 0, 0)
1406 profile_string = vaProfileStr(
profile->va_profile);
1408 profile_string =
"(no profile names)";
1411 for (j = 0; j < n; j++) {
1412 if (va_profiles[j] ==
profile->va_profile)
1417 "is not supported by driver.\n", profile_string,
1425 if (!
ctx->profile) {
1434 profile_string,
ctx->va_profile);
1436 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1438 if (!va_entrypoints) {
1442 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1443 va_entrypoints, &n);
1444 if (vas != VA_STATUS_SUCCESS) {
1446 "profile %s (%d): %d (%s).\n", profile_string,
1447 ctx->va_profile, vas, vaErrorStr(vas));
1452 for (
i = 0;
i < n;
i++) {
1453 for (j = 0; usable_entrypoints[j]; j++) {
1454 if (va_entrypoints[
i] == usable_entrypoints[j])
1457 if (usable_entrypoints[j])
1462 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1467 ctx->va_entrypoint = va_entrypoints[
i];
1468 #if VA_CHECK_VERSION(1, 0, 0)
1469 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1471 entrypoint_string =
"(no entrypoint names)";
1474 entrypoint_string,
ctx->va_entrypoint);
1478 if (rt_format->
depth == depth &&
1486 "found for profile %s (%d) entrypoint %s (%d).\n",
1487 profile_string,
ctx->va_profile,
1488 entrypoint_string,
ctx->va_entrypoint);
1493 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1494 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1495 ctx->va_profile,
ctx->va_entrypoint,
1496 &rt_format_attr, 1);
1497 if (vas != VA_STATUS_SUCCESS) {
1499 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1504 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1506 "supported by driver: assuming surface RT format %s "
1507 "is valid.\n", rt_format->
name);
1508 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1510 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1511 rt_format->
name, profile_string,
ctx->va_profile,
1512 entrypoint_string,
ctx->va_entrypoint);
1517 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1518 ctx->config_attributes[
ctx->nb_config_attributes++] =
1520 .type = VAConfigAttribRTFormat,
1521 .value = rt_format->
value,
1539 #if VA_CHECK_VERSION(1, 1, 0)
1544 #if VA_CHECK_VERSION(1, 3, 0)
1556 uint32_t supported_va_rc_modes;
1558 int64_t rc_bits_per_second;
1559 int rc_target_percentage;
1562 int64_t hrd_buffer_size;
1563 int64_t hrd_initial_buffer_fullness;
1565 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1567 char supported_rc_modes_string[64];
1569 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1570 ctx->va_profile,
ctx->va_entrypoint,
1572 if (vas != VA_STATUS_SUCCESS) {
1574 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1577 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1579 "supported rate control modes: assuming CQP only.\n");
1580 supported_va_rc_modes = VA_RC_CQP;
1581 strcpy(supported_rc_modes_string,
"unknown");
1583 char *
str = supported_rc_modes_string;
1584 size_t len =
sizeof(supported_rc_modes_string);
1587 supported_va_rc_modes = rc_attr.value;
1590 if (supported_va_rc_modes &
rc_mode->va_mode) {
1606 supported_rc_modes_string);
1621 #define TRY_RC_MODE(mode, fail) do { \
1622 rc_mode = &vaapi_encode_rc_modes[mode]; \
1623 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1625 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1626 "RC mode (supported modes: %s).\n", rc_mode->name, \
1627 supported_rc_modes_string); \
1628 return AVERROR(EINVAL); \
1630 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1631 "RC mode.\n", rc_mode->name); \
1634 goto rc_mode_found; \
1638 if (
ctx->explicit_rc_mode)
1641 if (
ctx->explicit_qp)
1671 "RC mode compatible with selected options "
1672 "(supported modes: %s).\n", supported_rc_modes_string);
1689 rc_bits_per_second = avctx->
bit_rate;
1695 rc_target_percentage = 100;
1702 }
else if (
rc_mode->maxrate) {
1706 "bitrate (%"PRId64
") must not be greater than "
1707 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1712 rc_target_percentage = (avctx->
bit_rate * 100) /
1719 rc_bits_per_second = 2 * avctx->
bit_rate;
1720 rc_target_percentage = 50;
1725 "in %s RC mode.\n",
rc_mode->name);
1727 rc_bits_per_second = avctx->
bit_rate;
1728 rc_target_percentage = 100;
1731 rc_bits_per_second = 0;
1732 rc_target_percentage = 100;
1736 if (
ctx->explicit_qp) {
1737 rc_quality =
ctx->explicit_qp;
1741 rc_quality =
ctx->codec->default_quality;
1743 "using default (%d).\n", rc_quality);
1759 "must have initial buffer size (%d) <= "
1760 "buffer size (%"PRId64
").\n",
1766 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1769 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1773 "in %s RC mode.\n",
rc_mode->name);
1776 hrd_buffer_size = 0;
1777 hrd_initial_buffer_fullness = 0;
1781 rc_window_size = 1000;
1785 if (rc_bits_per_second > UINT32_MAX ||
1786 hrd_buffer_size > UINT32_MAX ||
1787 hrd_initial_buffer_fullness > UINT32_MAX) {
1789 "greater are not supported by VAAPI.\n");
1794 ctx->rc_quality = rc_quality;
1796 ctx->va_bit_rate = rc_bits_per_second;
1799 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1802 ctx->config_attributes[
ctx->nb_config_attributes++] =
1804 .type = VAConfigAttribRateControl,
1805 .value =
ctx->va_rc_mode,
1812 if (
rc_mode->va_mode != VA_RC_CQP) {
1815 "converging in %d frames with %d%% accuracy.\n",
1816 rc_bits_per_second, rc_window_size,
1817 rc_target_percentage);
1818 }
else if (
rc_mode->bitrate) {
1820 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
1821 rc_bits_per_second, rc_window_size);
1824 ctx->rc_params = (VAEncMiscParameterRateControl) {
1825 .bits_per_second = rc_bits_per_second,
1826 .target_percentage = rc_target_percentage,
1827 .window_size = rc_window_size,
1829 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
1830 .basic_unit_size = 0,
1831 #
if VA_CHECK_VERSION(1, 1, 0)
1832 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
1833 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
1835 #
if VA_CHECK_VERSION(1, 3, 0)
1836 .quality_factor = rc_quality,
1840 VAEncMiscParameterTypeRateControl,
1842 sizeof(
ctx->rc_params));
1847 "initial fullness %"PRId64
" bits.\n",
1848 hrd_buffer_size, hrd_initial_buffer_fullness);
1850 ctx->hrd_params = (VAEncMiscParameterHRD) {
1851 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1852 .buffer_size = hrd_buffer_size,
1855 VAEncMiscParameterTypeHRD,
1857 sizeof(
ctx->hrd_params));
1868 fr_num, fr_den, (
double)fr_num / fr_den);
1870 ctx->fr_params = (VAEncMiscParameterFrameRate) {
1871 .framerate = (
unsigned int)fr_den << 16 | fr_num,
1873 #if VA_CHECK_VERSION(0, 40, 0)
1875 VAEncMiscParameterTypeFrameRate,
1877 sizeof(
ctx->fr_params));
1885 #if VA_CHECK_VERSION(1, 5, 0)
1887 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
1890 if (
ctx->va_rc_mode == VA_RC_CQP) {
1891 ctx->max_frame_size = 0;
1897 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1901 if (vas != VA_STATUS_SUCCESS) {
1902 ctx->max_frame_size = 0;
1904 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1908 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1909 ctx->max_frame_size = 0;
1911 "is not supported.\n");
1914 VAConfigAttribValMaxFrameSize attr_mfs;
1915 attr_mfs.value = attr.value;
1917 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
1918 ctx->max_frame_size = 0;
1920 "max frame size which has not been implemented in FFmpeg.\n");
1924 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
1925 .max_frame_size =
ctx->max_frame_size * 8,
1929 ctx->max_frame_size);
1933 "this VAAPI version.\n");
1944 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
1945 uint32_t ref_l0, ref_l1;
1946 int prediction_pre_only;
1948 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1952 if (vas != VA_STATUS_SUCCESS) {
1954 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1958 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1959 ref_l0 = ref_l1 = 0;
1961 ref_l0 = attr.value & 0xffff;
1962 ref_l1 = attr.value >> 16 & 0xffff;
1966 prediction_pre_only = 0;
1968 #if VA_CHECK_VERSION(1, 9, 0)
1971 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
1972 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1976 if (vas != VA_STATUS_SUCCESS) {
1978 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1980 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1982 "prediction constraints.\n");
1984 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
1985 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
1987 "direction attribute.\n");
1991 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
1992 if (ref_l0 > 0 && ref_l1 > 0) {
1993 prediction_pre_only = 1;
1995 "lists for B-frames.\n");
1999 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
2000 if (ref_l0 > 0 && ref_l1 > 0) {
2003 "replacing them with B-frames.\n");
2014 }
else if (ref_l0 < 1) {
2016 "reference frames.\n");
2019 ref_l1 < 1 || avctx->max_b_frames < 1 ||
2020 prediction_pre_only) {
2023 "(supported references: %d / %d).\n",
2027 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2029 ctx->p_per_i = INT_MAX;
2034 "(supported references: %d / %d).\n",
2038 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2040 ctx->p_per_i = INT_MAX;
2046 ctx->max_b_depth = 1;
2052 ctx->gop_per_idr =
ctx->idr_interval + 1;
2054 ctx->closed_gop = 1;
2055 ctx->gop_per_idr = 1;
2062 uint32_t slice_structure)
2072 if (avctx->
slices >
ctx->slice_block_rows) {
2074 "configured number of slices (%d < %d); using "
2075 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2076 req_slices =
ctx->slice_block_rows;
2078 req_slices = avctx->
slices;
2080 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2081 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2082 ctx->nb_slices = req_slices;
2083 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2084 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2086 for (k = 1;; k *= 2) {
2087 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2090 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2091 ctx->slice_size = k;
2092 #if VA_CHECK_VERSION(1, 0, 0)
2093 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2094 ctx->nb_slices =
ctx->slice_block_rows;
2095 ctx->slice_size = 1;
2099 "slice structure modes (%#x).\n", slice_structure);
2107 uint32_t slice_structure)
2112 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2113 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2114 ctx->tile_cols == 1))) {
2116 "current tile requirement.\n", slice_structure);
2120 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2121 ctx->tile_cols >
ctx->slice_block_cols) {
2123 "for configured number of tile (%d x %d); ",
2124 ctx->slice_block_rows,
ctx->slice_block_cols,
2125 ctx->tile_rows,
ctx->tile_cols);
2126 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2127 ctx->slice_block_rows :
ctx->tile_rows;
2128 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2129 ctx->slice_block_cols :
ctx->tile_cols;
2131 ctx->tile_rows,
ctx->tile_cols);
2134 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2139 if (avctx->
slices != req_tiles)
2141 "mismatches with configured number of tile (%d != %d); "
2142 "using requested tile number for slice.\n",
2143 avctx->
slices, req_tiles);
2145 ctx->nb_slices = req_tiles;
2149 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2150 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2151 i *
ctx->slice_block_cols /
ctx->tile_cols;
2152 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2155 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2156 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2157 i *
ctx->slice_block_rows /
ctx->tile_rows;
2158 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2162 ctx->tile_rows,
ctx->tile_cols);
2170 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2171 { VAConfigAttribEncSliceStructure },
2172 #if VA_CHECK_VERSION(1, 1, 0)
2173 { VAConfigAttribEncTileSupport },
2177 uint32_t max_slices, slice_structure;
2183 "but this codec does not support controlling slices.\n");
2190 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2191 ctx->slice_block_height;
2192 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2193 ctx->slice_block_width;
2195 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2197 ctx->slice_size =
ctx->slice_block_rows;
2201 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2205 if (vas != VA_STATUS_SUCCESS) {
2207 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2210 max_slices = attr[0].value;
2211 slice_structure = attr[1].value;
2212 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2213 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2215 "pictures as multiple slices.\n.");
2219 if (
ctx->tile_rows &&
ctx->tile_cols) {
2220 #if VA_CHECK_VERSION(1, 1, 0)
2221 uint32_t tile_support = attr[2].value;
2222 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2224 "pictures as multiple tiles.\n.");
2229 "not supported with this VAAPI version.\n");
2234 if (
ctx->tile_rows &&
ctx->tile_cols)
2243 "%d (from %d) due to driver constraints on slice "
2244 "structure.\n",
ctx->nb_slices, avctx->
slices);
2246 if (
ctx->nb_slices > max_slices) {
2248 "encoding with %d slices (max %"PRIu32
").\n",
2249 ctx->nb_slices, max_slices);
2262 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2264 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2268 if (vas != VA_STATUS_SUCCESS) {
2270 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2274 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2275 if (
ctx->desired_packed_headers) {
2277 "packed headers (wanted %#x).\n",
2278 ctx->desired_packed_headers);
2281 "packed headers (none wanted).\n");
2283 ctx->va_packed_headers = 0;
2285 if (
ctx->desired_packed_headers & ~attr.value) {
2287 "wanted packed headers (wanted %#x, found %#x).\n",
2288 ctx->desired_packed_headers, attr.value);
2291 "available (wanted %#x, found %#x).\n",
2292 ctx->desired_packed_headers, attr.value);
2294 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2297 if (
ctx->va_packed_headers) {
2298 ctx->config_attributes[
ctx->nb_config_attributes++] =
2300 .type = VAConfigAttribEncPackedHeaders,
2301 .value =
ctx->va_packed_headers,
2305 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2306 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2309 "sequence headers, but a global header is requested.\n");
2311 "this may result in a stream which is not usable for some "
2312 "purposes (e.g. not muxable to some containers).\n");
2320 #if VA_CHECK_VERSION(0, 36, 0)
2323 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2326 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2330 if (vas != VA_STATUS_SUCCESS) {
2332 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2336 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2339 "supported: will use default quality level.\n");
2344 "valid range is 0-%d, using %d.\n",
2345 attr.value, attr.value);
2349 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2353 VAEncMiscParameterTypeQualityLevel,
2354 &
ctx->quality_params,
2355 sizeof(
ctx->quality_params));
2359 "not supported with this VAAPI version.\n");
2367 #if VA_CHECK_VERSION(1, 0, 0)
2370 VAConfigAttrib attr = { VAConfigAttribEncROI };
2372 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2376 if (vas != VA_STATUS_SUCCESS) {
2378 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2382 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2383 ctx->roi_allowed = 0;
2385 VAConfigAttribValEncROI roi = {
2386 .value = attr.value,
2389 ctx->roi_max_regions = roi.bits.num_roi_regions;
2390 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2391 (
ctx->va_rc_mode == VA_RC_CQP ||
2392 roi.bits.roi_rc_qp_delta_support);
2403 VABufferID buffer_id;
2405 buffer_id = (VABufferID)(uintptr_t)
data;
2407 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2417 VABufferID buffer_id;
2425 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2426 VAEncCodedBufferType,
2427 3 *
ctx->surface_width *
ctx->surface_height +
2428 (1 << 16), 1, 0, &buffer_id);
2429 if (vas != VA_STATUS_SUCCESS) {
2431 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2442 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2477 if (
ctx->input_frames->sw_format ==
2479 recon_format =
ctx->input_frames->sw_format;
2490 recon_format =
ctx->input_frames->sw_format;
2500 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2501 ctx->surface_width,
ctx->surface_height,
2512 if (!
ctx->recon_frames_ref) {
2519 ctx->recon_frames->sw_format = recon_format;
2520 ctx->recon_frames->width =
ctx->surface_width;
2521 ctx->recon_frames->height =
ctx->surface_height;
2526 "frame context: %d.\n", err);
2544 ctx->va_config = VA_INVALID_ID;
2545 ctx->va_context = VA_INVALID_ID;
2556 "required to associate the encoding device.\n");
2561 if (!
ctx->input_frames_ref) {
2568 if (!
ctx->device_ref) {
2573 ctx->hwctx =
ctx->device->hwctx;
2579 if (
ctx->codec->get_encoder_caps) {
2580 err =
ctx->codec->get_encoder_caps(avctx);
2588 ctx->slice_block_width = 16;
2589 ctx->slice_block_height = 16;
2619 if (
ctx->max_frame_size) {
2625 vas = vaCreateConfig(
ctx->hwctx->display,
2626 ctx->va_profile,
ctx->va_entrypoint,
2627 ctx->config_attributes,
ctx->nb_config_attributes,
2629 if (vas != VA_STATUS_SUCCESS) {
2631 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2640 recon_hwctx =
ctx->recon_frames->hwctx;
2641 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2642 ctx->surface_width,
ctx->surface_height,
2647 if (vas != VA_STATUS_SUCCESS) {
2649 "context: %d (%s).\n", vas, vaErrorStr(vas));
2654 ctx->output_buffer_pool =
2657 if (!
ctx->output_buffer_pool) {
2662 if (
ctx->codec->configure) {
2663 err =
ctx->codec->configure(avctx);
2668 ctx->output_delay =
ctx->b_per_p;
2669 ctx->decode_delay =
ctx->max_b_depth;
2671 if (
ctx->codec->sequence_params_size > 0) {
2672 ctx->codec_sequence_params =
2674 if (!
ctx->codec_sequence_params) {
2679 if (
ctx->codec->picture_params_size > 0) {
2680 ctx->codec_picture_params =
2682 if (!
ctx->codec_picture_params) {
2688 if (
ctx->codec->init_sequence_params) {
2689 err =
ctx->codec->init_sequence_params(avctx);
2692 "failed: %d.\n", err);
2697 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2698 ctx->codec->write_sequence_header &&
2701 size_t bit_len = 8 *
sizeof(
data);
2703 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2706 "for extradata: %d.\n", err);
2720 #if VA_CHECK_VERSION(1, 9, 0)
2722 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2723 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2724 ctx->has_sync_buffer_func = 1;
2728 if (!
ctx->encode_fifo)
2749 for (pic =
ctx->pic_start; pic; pic = next) {
2756 if (
ctx->va_context != VA_INVALID_ID) {
2757 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2758 ctx->va_context = VA_INVALID_ID;
2761 if (
ctx->va_config != VA_INVALID_ID) {
2762 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2763 ctx->va_config = VA_INVALID_ID;