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 {
"YUV420_12", VA_RT_FORMAT_YUV420_12, 12, 3, 1, 1 },
1309 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1310 {
"YUV422_12", VA_RT_FORMAT_YUV422_12, 12, 3, 1, 0 },
1311 {
"YUV444_10", VA_RT_FORMAT_YUV444_10, 10, 3, 0, 0 },
1312 {
"YUV444_12", VA_RT_FORMAT_YUV444_12, 12, 3, 0, 0 },
1314 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1315 {
"XYUV", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1316 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1317 #if VA_CHECK_VERSION(0, 38, 1)
1318 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1323 VAEntrypointEncSlice,
1324 VAEntrypointEncPicture,
1325 #if VA_CHECK_VERSION(0, 39, 2)
1326 VAEntrypointEncSliceLP,
1330 #if VA_CHECK_VERSION(0, 39, 2)
1331 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1332 VAEntrypointEncSliceLP,
1340 VAProfile *va_profiles =
NULL;
1341 VAEntrypoint *va_entrypoints =
NULL;
1343 const VAEntrypoint *usable_entrypoints;
1346 VAConfigAttrib rt_format_attr;
1348 const char *profile_string, *entrypoint_string;
1349 int i, j, n, depth, err;
1352 if (
ctx->low_power) {
1353 #if VA_CHECK_VERSION(0, 39, 2)
1354 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1357 "supported with this VAAPI version.\n");
1367 ctx->input_frames->sw_format);
1370 depth =
desc->comp[0].depth;
1371 for (
i = 1;
i <
desc->nb_components;
i++) {
1372 if (
desc->comp[
i].depth != depth) {
1381 n = vaMaxNumProfiles(
ctx->hwctx->display);
1387 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
1388 if (vas != VA_STATUS_SUCCESS) {
1390 vas, vaErrorStr(vas));
1396 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1399 if (depth !=
profile->depth ||
1402 if (
desc->nb_components > 1 &&
1410 #if VA_CHECK_VERSION(1, 0, 0)
1411 profile_string = vaProfileStr(
profile->va_profile);
1413 profile_string =
"(no profile names)";
1416 for (j = 0; j < n; j++) {
1417 if (va_profiles[j] ==
profile->va_profile)
1422 "is not supported by driver.\n", profile_string,
1430 if (!
ctx->profile) {
1439 profile_string,
ctx->va_profile);
1441 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1443 if (!va_entrypoints) {
1447 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1448 va_entrypoints, &n);
1449 if (vas != VA_STATUS_SUCCESS) {
1451 "profile %s (%d): %d (%s).\n", profile_string,
1452 ctx->va_profile, vas, vaErrorStr(vas));
1457 for (
i = 0;
i < n;
i++) {
1458 for (j = 0; usable_entrypoints[j]; j++) {
1459 if (va_entrypoints[
i] == usable_entrypoints[j])
1462 if (usable_entrypoints[j])
1467 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1472 ctx->va_entrypoint = va_entrypoints[
i];
1473 #if VA_CHECK_VERSION(1, 0, 0)
1474 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1476 entrypoint_string =
"(no entrypoint names)";
1479 entrypoint_string,
ctx->va_entrypoint);
1483 if (rt_format->
depth == depth &&
1491 "found for profile %s (%d) entrypoint %s (%d).\n",
1492 profile_string,
ctx->va_profile,
1493 entrypoint_string,
ctx->va_entrypoint);
1498 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1499 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1500 ctx->va_profile,
ctx->va_entrypoint,
1501 &rt_format_attr, 1);
1502 if (vas != VA_STATUS_SUCCESS) {
1504 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1509 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1511 "supported by driver: assuming surface RT format %s "
1512 "is valid.\n", rt_format->
name);
1513 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1515 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1516 rt_format->
name, profile_string,
ctx->va_profile,
1517 entrypoint_string,
ctx->va_entrypoint);
1522 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1523 ctx->config_attributes[
ctx->nb_config_attributes++] =
1525 .type = VAConfigAttribRTFormat,
1526 .value = rt_format->
value,
1544 #if VA_CHECK_VERSION(1, 1, 0)
1549 #if VA_CHECK_VERSION(1, 3, 0)
1561 uint32_t supported_va_rc_modes;
1563 int64_t rc_bits_per_second;
1564 int rc_target_percentage;
1567 int64_t hrd_buffer_size;
1568 int64_t hrd_initial_buffer_fullness;
1570 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1572 char supported_rc_modes_string[64];
1574 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1575 ctx->va_profile,
ctx->va_entrypoint,
1577 if (vas != VA_STATUS_SUCCESS) {
1579 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1582 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1584 "supported rate control modes: assuming CQP only.\n");
1585 supported_va_rc_modes = VA_RC_CQP;
1586 strcpy(supported_rc_modes_string,
"unknown");
1588 char *
str = supported_rc_modes_string;
1589 size_t len =
sizeof(supported_rc_modes_string);
1592 supported_va_rc_modes = rc_attr.value;
1595 if (supported_va_rc_modes &
rc_mode->va_mode) {
1611 supported_rc_modes_string);
1626 #define TRY_RC_MODE(mode, fail) do { \
1627 rc_mode = &vaapi_encode_rc_modes[mode]; \
1628 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1630 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1631 "RC mode (supported modes: %s).\n", rc_mode->name, \
1632 supported_rc_modes_string); \
1633 return AVERROR(EINVAL); \
1635 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1636 "RC mode.\n", rc_mode->name); \
1639 goto rc_mode_found; \
1643 if (
ctx->explicit_rc_mode)
1646 if (
ctx->explicit_qp)
1676 "RC mode compatible with selected options "
1677 "(supported modes: %s).\n", supported_rc_modes_string);
1694 rc_bits_per_second = avctx->
bit_rate;
1700 rc_target_percentage = 100;
1707 }
else if (
rc_mode->maxrate) {
1711 "bitrate (%"PRId64
") must not be greater than "
1712 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1717 rc_target_percentage = (avctx->
bit_rate * 100) /
1724 rc_bits_per_second = 2 * avctx->
bit_rate;
1725 rc_target_percentage = 50;
1730 "in %s RC mode.\n",
rc_mode->name);
1732 rc_bits_per_second = avctx->
bit_rate;
1733 rc_target_percentage = 100;
1736 rc_bits_per_second = 0;
1737 rc_target_percentage = 100;
1741 if (
ctx->explicit_qp) {
1742 rc_quality =
ctx->explicit_qp;
1746 rc_quality =
ctx->codec->default_quality;
1748 "using default (%d).\n", rc_quality);
1764 "must have initial buffer size (%d) <= "
1765 "buffer size (%"PRId64
").\n",
1771 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1774 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1778 "in %s RC mode.\n",
rc_mode->name);
1781 hrd_buffer_size = 0;
1782 hrd_initial_buffer_fullness = 0;
1786 rc_window_size = 1000;
1790 if (rc_bits_per_second > UINT32_MAX ||
1791 hrd_buffer_size > UINT32_MAX ||
1792 hrd_initial_buffer_fullness > UINT32_MAX) {
1794 "greater are not supported by VAAPI.\n");
1799 ctx->rc_quality = rc_quality;
1801 ctx->va_bit_rate = rc_bits_per_second;
1804 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1807 ctx->config_attributes[
ctx->nb_config_attributes++] =
1809 .type = VAConfigAttribRateControl,
1810 .value =
ctx->va_rc_mode,
1817 if (
rc_mode->va_mode != VA_RC_CQP) {
1820 "converging in %d frames with %d%% accuracy.\n",
1821 rc_bits_per_second, rc_window_size,
1822 rc_target_percentage);
1823 }
else if (
rc_mode->bitrate) {
1825 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
1826 rc_bits_per_second, rc_window_size);
1829 ctx->rc_params = (VAEncMiscParameterRateControl) {
1830 .bits_per_second = rc_bits_per_second,
1831 .target_percentage = rc_target_percentage,
1832 .window_size = rc_window_size,
1834 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
1835 .basic_unit_size = 0,
1836 #
if VA_CHECK_VERSION(1, 1, 0)
1837 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
1838 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
1840 #
if VA_CHECK_VERSION(1, 3, 0)
1841 .quality_factor = rc_quality,
1845 VAEncMiscParameterTypeRateControl,
1847 sizeof(
ctx->rc_params));
1852 "initial fullness %"PRId64
" bits.\n",
1853 hrd_buffer_size, hrd_initial_buffer_fullness);
1855 ctx->hrd_params = (VAEncMiscParameterHRD) {
1856 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1857 .buffer_size = hrd_buffer_size,
1860 VAEncMiscParameterTypeHRD,
1862 sizeof(
ctx->hrd_params));
1873 fr_num, fr_den, (
double)fr_num / fr_den);
1875 ctx->fr_params = (VAEncMiscParameterFrameRate) {
1876 .framerate = (
unsigned int)fr_den << 16 | fr_num,
1878 #if VA_CHECK_VERSION(0, 40, 0)
1880 VAEncMiscParameterTypeFrameRate,
1882 sizeof(
ctx->fr_params));
1890 #if VA_CHECK_VERSION(1, 5, 0)
1892 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
1895 if (
ctx->va_rc_mode == VA_RC_CQP) {
1896 ctx->max_frame_size = 0;
1902 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1906 if (vas != VA_STATUS_SUCCESS) {
1907 ctx->max_frame_size = 0;
1909 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1913 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1914 ctx->max_frame_size = 0;
1916 "is not supported.\n");
1919 VAConfigAttribValMaxFrameSize attr_mfs;
1920 attr_mfs.value = attr.value;
1922 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
1923 ctx->max_frame_size = 0;
1925 "max frame size which has not been implemented in FFmpeg.\n");
1929 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
1930 .max_frame_size =
ctx->max_frame_size * 8,
1934 ctx->max_frame_size);
1938 "this VAAPI version.\n");
1949 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
1950 uint32_t ref_l0, ref_l1;
1951 int prediction_pre_only;
1953 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1957 if (vas != VA_STATUS_SUCCESS) {
1959 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1963 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1964 ref_l0 = ref_l1 = 0;
1966 ref_l0 = attr.value & 0xffff;
1967 ref_l1 = attr.value >> 16 & 0xffff;
1971 prediction_pre_only = 0;
1973 #if VA_CHECK_VERSION(1, 9, 0)
1976 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
1977 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1981 if (vas != VA_STATUS_SUCCESS) {
1983 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1985 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1987 "prediction constraints.\n");
1989 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
1990 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
1992 "direction attribute.\n");
1996 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
1997 if (ref_l0 > 0 && ref_l1 > 0) {
1998 prediction_pre_only = 1;
2000 "lists for B-frames.\n");
2004 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
2005 if (ref_l0 > 0 && ref_l1 > 0) {
2008 "replacing them with B-frames.\n");
2019 }
else if (ref_l0 < 1) {
2021 "reference frames.\n");
2024 ref_l1 < 1 || avctx->max_b_frames < 1 ||
2025 prediction_pre_only) {
2028 "(supported references: %d / %d).\n",
2032 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2034 ctx->p_per_i = INT_MAX;
2039 "(supported references: %d / %d).\n",
2043 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2045 ctx->p_per_i = INT_MAX;
2051 ctx->max_b_depth = 1;
2057 ctx->gop_per_idr =
ctx->idr_interval + 1;
2059 ctx->closed_gop = 1;
2060 ctx->gop_per_idr = 1;
2067 uint32_t slice_structure)
2077 if (avctx->
slices >
ctx->slice_block_rows) {
2079 "configured number of slices (%d < %d); using "
2080 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2081 req_slices =
ctx->slice_block_rows;
2083 req_slices = avctx->
slices;
2085 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2086 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2087 ctx->nb_slices = req_slices;
2088 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2089 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2091 for (k = 1;; k *= 2) {
2092 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2095 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2096 ctx->slice_size = k;
2097 #if VA_CHECK_VERSION(1, 0, 0)
2098 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2099 ctx->nb_slices =
ctx->slice_block_rows;
2100 ctx->slice_size = 1;
2104 "slice structure modes (%#x).\n", slice_structure);
2112 uint32_t slice_structure)
2117 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2118 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2119 ctx->tile_cols == 1))) {
2121 "current tile requirement.\n", slice_structure);
2125 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2126 ctx->tile_cols >
ctx->slice_block_cols) {
2128 "for configured number of tile (%d x %d); ",
2129 ctx->slice_block_rows,
ctx->slice_block_cols,
2130 ctx->tile_rows,
ctx->tile_cols);
2131 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2132 ctx->slice_block_rows :
ctx->tile_rows;
2133 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2134 ctx->slice_block_cols :
ctx->tile_cols;
2136 ctx->tile_rows,
ctx->tile_cols);
2139 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2144 if (avctx->
slices != req_tiles)
2146 "mismatches with configured number of tile (%d != %d); "
2147 "using requested tile number for slice.\n",
2148 avctx->
slices, req_tiles);
2150 ctx->nb_slices = req_tiles;
2154 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2155 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2156 i *
ctx->slice_block_cols /
ctx->tile_cols;
2157 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2160 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2161 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2162 i *
ctx->slice_block_rows /
ctx->tile_rows;
2163 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2167 ctx->tile_rows,
ctx->tile_cols);
2175 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2176 { VAConfigAttribEncSliceStructure },
2177 #if VA_CHECK_VERSION(1, 1, 0)
2178 { VAConfigAttribEncTileSupport },
2182 uint32_t max_slices, slice_structure;
2188 "but this codec does not support controlling slices.\n");
2195 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2196 ctx->slice_block_height;
2197 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2198 ctx->slice_block_width;
2200 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2202 ctx->slice_size =
ctx->slice_block_rows;
2206 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2210 if (vas != VA_STATUS_SUCCESS) {
2212 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2215 max_slices = attr[0].value;
2216 slice_structure = attr[1].value;
2217 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2218 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2220 "pictures as multiple slices.\n.");
2224 if (
ctx->tile_rows &&
ctx->tile_cols) {
2225 #if VA_CHECK_VERSION(1, 1, 0)
2226 uint32_t tile_support = attr[2].value;
2227 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2229 "pictures as multiple tiles.\n.");
2234 "not supported with this VAAPI version.\n");
2239 if (
ctx->tile_rows &&
ctx->tile_cols)
2248 "%d (from %d) due to driver constraints on slice "
2249 "structure.\n",
ctx->nb_slices, avctx->
slices);
2251 if (
ctx->nb_slices > max_slices) {
2253 "encoding with %d slices (max %"PRIu32
").\n",
2254 ctx->nb_slices, max_slices);
2267 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2269 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2273 if (vas != VA_STATUS_SUCCESS) {
2275 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2279 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2280 if (
ctx->desired_packed_headers) {
2282 "packed headers (wanted %#x).\n",
2283 ctx->desired_packed_headers);
2286 "packed headers (none wanted).\n");
2288 ctx->va_packed_headers = 0;
2290 if (
ctx->desired_packed_headers & ~attr.value) {
2292 "wanted packed headers (wanted %#x, found %#x).\n",
2293 ctx->desired_packed_headers, attr.value);
2296 "available (wanted %#x, found %#x).\n",
2297 ctx->desired_packed_headers, attr.value);
2299 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2302 if (
ctx->va_packed_headers) {
2303 ctx->config_attributes[
ctx->nb_config_attributes++] =
2305 .type = VAConfigAttribEncPackedHeaders,
2306 .value =
ctx->va_packed_headers,
2310 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2311 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2314 "sequence headers, but a global header is requested.\n");
2316 "this may result in a stream which is not usable for some "
2317 "purposes (e.g. not muxable to some containers).\n");
2325 #if VA_CHECK_VERSION(0, 36, 0)
2328 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2331 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2335 if (vas != VA_STATUS_SUCCESS) {
2337 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2341 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2344 "supported: will use default quality level.\n");
2349 "valid range is 0-%d, using %d.\n",
2350 attr.value, attr.value);
2354 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2358 VAEncMiscParameterTypeQualityLevel,
2359 &
ctx->quality_params,
2360 sizeof(
ctx->quality_params));
2364 "not supported with this VAAPI version.\n");
2372 #if VA_CHECK_VERSION(1, 0, 0)
2375 VAConfigAttrib attr = { VAConfigAttribEncROI };
2377 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2381 if (vas != VA_STATUS_SUCCESS) {
2383 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2387 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2388 ctx->roi_allowed = 0;
2390 VAConfigAttribValEncROI roi = {
2391 .value = attr.value,
2394 ctx->roi_max_regions = roi.bits.num_roi_regions;
2395 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2396 (
ctx->va_rc_mode == VA_RC_CQP ||
2397 roi.bits.roi_rc_qp_delta_support);
2408 VABufferID buffer_id;
2410 buffer_id = (VABufferID)(uintptr_t)
data;
2412 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2422 VABufferID buffer_id;
2430 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2431 VAEncCodedBufferType,
2432 3 *
ctx->surface_width *
ctx->surface_height +
2433 (1 << 16), 1, 0, &buffer_id);
2434 if (vas != VA_STATUS_SUCCESS) {
2436 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2447 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2482 if (
ctx->input_frames->sw_format ==
2484 recon_format =
ctx->input_frames->sw_format;
2495 recon_format =
ctx->input_frames->sw_format;
2505 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2506 ctx->surface_width,
ctx->surface_height,
2517 if (!
ctx->recon_frames_ref) {
2524 ctx->recon_frames->sw_format = recon_format;
2525 ctx->recon_frames->width =
ctx->surface_width;
2526 ctx->recon_frames->height =
ctx->surface_height;
2531 "frame context: %d.\n", err);
2549 ctx->va_config = VA_INVALID_ID;
2550 ctx->va_context = VA_INVALID_ID;
2561 "required to associate the encoding device.\n");
2566 if (!
ctx->input_frames_ref) {
2573 if (!
ctx->device_ref) {
2578 ctx->hwctx =
ctx->device->hwctx;
2584 if (
ctx->codec->get_encoder_caps) {
2585 err =
ctx->codec->get_encoder_caps(avctx);
2593 ctx->slice_block_width = 16;
2594 ctx->slice_block_height = 16;
2624 if (
ctx->max_frame_size) {
2630 vas = vaCreateConfig(
ctx->hwctx->display,
2631 ctx->va_profile,
ctx->va_entrypoint,
2632 ctx->config_attributes,
ctx->nb_config_attributes,
2634 if (vas != VA_STATUS_SUCCESS) {
2636 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2645 recon_hwctx =
ctx->recon_frames->hwctx;
2646 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2647 ctx->surface_width,
ctx->surface_height,
2652 if (vas != VA_STATUS_SUCCESS) {
2654 "context: %d (%s).\n", vas, vaErrorStr(vas));
2659 ctx->output_buffer_pool =
2662 if (!
ctx->output_buffer_pool) {
2667 if (
ctx->codec->configure) {
2668 err =
ctx->codec->configure(avctx);
2673 ctx->output_delay =
ctx->b_per_p;
2674 ctx->decode_delay =
ctx->max_b_depth;
2676 if (
ctx->codec->sequence_params_size > 0) {
2677 ctx->codec_sequence_params =
2679 if (!
ctx->codec_sequence_params) {
2684 if (
ctx->codec->picture_params_size > 0) {
2685 ctx->codec_picture_params =
2687 if (!
ctx->codec_picture_params) {
2693 if (
ctx->codec->init_sequence_params) {
2694 err =
ctx->codec->init_sequence_params(avctx);
2697 "failed: %d.\n", err);
2702 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2703 ctx->codec->write_sequence_header &&
2706 size_t bit_len = 8 *
sizeof(
data);
2708 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2711 "for extradata: %d.\n", err);
2725 #if VA_CHECK_VERSION(1, 9, 0)
2727 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2728 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2729 ctx->has_sync_buffer_func = 1;
2733 if (!
ctx->encode_fifo)
2754 for (pic =
ctx->pic_start; pic; pic = next) {
2761 if (
ctx->va_context != VA_INVALID_ID) {
2762 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2763 ctx->va_context = VA_INVALID_ID;
2766 if (
ctx->va_config != VA_INVALID_ID) {
2767 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2768 ctx->va_config = VA_INVALID_ID;