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],
334 if (
ctx->codec->picture_params_size > 0) {
339 ctx->codec->picture_params_size);
348 VAEncSequenceParameterBufferType,
349 ctx->codec_sequence_params,
350 ctx->codec->sequence_params_size);
356 for (
i = 0;
i <
ctx->nb_global_params;
i++) {
358 ctx->global_params_type[
i],
359 ctx->global_params[
i],
360 ctx->global_params_size[
i]);
366 if (
ctx->codec->init_picture_params) {
367 err =
ctx->codec->init_picture_params(avctx, pic);
370 "parameters: %d.\n", err);
374 VAEncPictureParameterBufferType,
376 ctx->codec->picture_params_size);
381 #if VA_CHECK_VERSION(1, 5, 0)
382 if (
ctx->max_frame_size) {
384 VAEncMiscParameterTypeMaxFrameSize,
386 sizeof(
ctx->mfs_params));
393 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
394 ctx->codec->write_sequence_header) {
395 bit_len = 8 *
sizeof(
data);
396 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
399 "header: %d.\n", err);
403 ctx->codec->sequence_header_type,
410 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
411 ctx->codec->write_picture_header) {
412 bit_len = 8 *
sizeof(
data);
413 err =
ctx->codec->write_picture_header(avctx, pic,
data, &bit_len);
416 "header: %d.\n", err);
420 ctx->codec->picture_header_type,
426 if (
ctx->codec->write_extra_buffer) {
430 err =
ctx->codec->write_extra_buffer(avctx, pic,
i, &
type,
436 "buffer %d: %d.\n",
i, err);
447 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
448 ctx->codec->write_extra_header) {
451 bit_len = 8 *
sizeof(
data);
452 err =
ctx->codec->write_extra_header(avctx, pic,
i, &
type,
458 "header %d: %d.\n",
i, err);
478 if (
ctx->tile_rows &&
ctx->tile_cols)
487 if (
ctx->codec->slice_params_size > 0) {
495 if (
ctx->codec->init_slice_params) {
496 err =
ctx->codec->init_slice_params(avctx, pic, slice);
499 "parameters: %d.\n", err);
504 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
505 ctx->codec->write_slice_header) {
506 bit_len = 8 *
sizeof(
data);
507 err =
ctx->codec->write_slice_header(avctx, pic, slice,
511 "header: %d.\n", err);
515 ctx->codec->slice_header_type,
521 if (
ctx->codec->init_slice_params) {
523 VAEncSliceParameterBufferType,
525 ctx->codec->slice_params_size);
531 #if VA_CHECK_VERSION(1, 0, 0)
534 if (sd &&
ctx->roi_allowed) {
537 VAEncMiscParameterBufferROI param_roi;
542 av_assert0(roi_size && sd->size % roi_size == 0);
543 nb_roi = sd->size / roi_size;
544 if (nb_roi >
ctx->roi_max_regions) {
545 if (!
ctx->roi_warned) {
547 "supported by driver (%d > %d).\n",
548 nb_roi,
ctx->roi_max_regions);
551 nb_roi =
ctx->roi_max_regions;
560 for (
i = 0;
i < nb_roi;
i++) {
568 pic->
roi[
i] = (VAEncROI) {
579 param_roi = (VAEncMiscParameterBufferROI) {
581 .max_delta_qp = INT8_MAX,
582 .min_delta_qp = INT8_MIN,
584 .roi_flags.bits.roi_value_is_qp_delta = 1,
588 VAEncMiscParameterTypeROI,
596 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
598 if (vas != VA_STATUS_SUCCESS) {
600 "%d (%s).\n", vas, vaErrorStr(vas));
602 goto fail_with_picture;
605 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
607 if (vas != VA_STATUS_SUCCESS) {
609 "%d (%s).\n", vas, vaErrorStr(vas));
611 goto fail_with_picture;
614 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
615 if (vas != VA_STATUS_SUCCESS) {
617 "%d (%s).\n", vas, vaErrorStr(vas));
621 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
628 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
631 vas = vaDestroyBuffer(
ctx->hwctx->display,
633 if (vas != VA_STATUS_SUCCESS) {
635 "param buffer %#x: %d (%s).\n",
647 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
691 if (
ctx->output_delay == 0) {
700 (3 *
ctx->output_delay +
ctx->async_depth)];
709 VACodedBufferSegment *buf_list, *buf;
714 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
716 if (vas != VA_STATUS_SUCCESS) {
718 "%d (%s).\n", vas, vaErrorStr(vas));
723 for (buf = buf_list; buf; buf = buf->next)
726 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
727 if (vas != VA_STATUS_SUCCESS) {
729 "%d (%s).\n", vas, vaErrorStr(vas));
738 VABufferID buf_id, uint8_t **dst)
741 VACodedBufferSegment *buf_list, *buf;
745 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
747 if (vas != VA_STATUS_SUCCESS) {
749 "%d (%s).\n", vas, vaErrorStr(vas));
754 for (buf = buf_list; buf; buf = buf->next) {
756 "(status %08x).\n", buf->size, buf->status);
758 memcpy(*dst, buf->buf, buf->size);
762 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
763 if (vas != VA_STATUS_SUCCESS) {
765 "%d (%s).\n", vas, vaErrorStr(vas));
777 VABufferID output_buffer_prev;
782 if (
ctx->coded_buffer_ref) {
783 output_buffer_prev = (VABufferID)(uintptr_t)
ctx->coded_buffer_ref->data;
800 if (
ctx->coded_buffer_ref) {
811 if (
ctx->coded_buffer_ref) {
836 if (
ctx->tail_pkt->size) {
846 pkt_ptr =
ctx->tail_pkt;
872 "%"PRId64
"/%"PRId64
".\n",
891 if (
ctx->codec->picture_priv_data_size > 0) {
942 int is_ref,
int in_dpb,
int prev)
1025 if (current_depth ==
ctx->max_b_depth || start->
next->
next == end) {
1026 for (pic = start->
next; pic; pic = pic->
next) {
1045 for (pic = start->
next; pic != end; pic = pic->
next)
1047 for (pic = start->
next,
i = 1; 2 * i < len; pic = pic->next,
i++);
1064 current_depth + 1, &next);
1069 current_depth + 1, last);
1083 for (
i = 0;
i <
ctx->nb_next_prev;
i++) {
1084 --
ctx->next_prev[
i]->ref_count[0];
1087 ctx->next_prev[0] = pic;
1089 ctx->nb_next_prev = 1;
1095 ctx->next_prev[
ctx->nb_next_prev++] = pic;
1098 --
ctx->next_prev[0]->ref_count[0];
1100 ctx->next_prev[
i] =
ctx->next_prev[
i + 1];
1101 ctx->next_prev[
i] = pic;
1111 int i, b_counter, closed_gop_end;
1116 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1138 "encode next.\n", pic->
b_depth);
1147 closed_gop_end =
ctx->closed_gop ||
1148 ctx->idr_counter ==
ctx->gop_per_idr;
1149 for (pic =
ctx->pic_start; pic; pic = next) {
1159 if (b_counter ==
ctx->b_per_p)
1163 if (
ctx->gop_counter + b_counter + closed_gop_end >=
ctx->gop_size)
1167 if (next && next->force_idr)
1173 if (!pic &&
ctx->end_of_stream) {
1184 "need more input for reference pictures.\n");
1187 if (
ctx->input_order <=
ctx->decode_delay && !
ctx->end_of_stream) {
1189 "need more input for timestamps.\n");
1197 ctx->idr_counter = 1;
1198 ctx->gop_counter = 1;
1200 }
else if (
ctx->gop_counter + b_counter >=
ctx->gop_size) {
1201 if (
ctx->idr_counter ==
ctx->gop_per_idr) {
1205 ctx->idr_counter = 1;
1212 ctx->gop_counter = 1;
1215 if (
ctx->gop_counter + b_counter + closed_gop_end ==
ctx->gop_size) {
1224 ctx->gop_counter += 1 + b_counter;
1235 for (
i = 0;
i <
ctx->nb_next_prev;
i++)
1247 if (b_counter > 0) {
1266 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1272 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1279 for (pic =
ctx->pic_start; pic; pic = next) {
1286 ctx->pic_start = next;
1304 "frames ignored due to lack of API support.\n");
1305 ctx->crop_warned = 1;
1308 if (!
ctx->roi_allowed) {
1312 if (sd && !
ctx->roi_warned) {
1314 "frames ignored due to lack of driver support.\n");
1315 ctx->roi_warned = 1;
1363 if (
ctx->input_order == 0)
1364 ctx->first_pts = pic->
pts;
1365 if (
ctx->input_order ==
ctx->decode_delay)
1366 ctx->dts_pts_diff = pic->
pts -
ctx->first_pts;
1367 if (
ctx->output_delay > 0)
1368 ctx->ts_ring[
ctx->input_order %
1369 (3 *
ctx->output_delay +
ctx->async_depth)] = pic->
pts;
1374 if (
ctx->pic_start) {
1375 ctx->pic_end->next = pic;
1378 ctx->pic_start = pic;
1383 ctx->end_of_stream = 1;
1387 if (
ctx->input_order <
ctx->decode_delay)
1388 ctx->dts_pts_diff =
ctx->pic_end->pts -
ctx->first_pts;
1407 if (
ctx->tail_pkt->size) {
1411 else if (!
tmp->next) {
1429 if (!
ctx->pic_start) {
1430 if (
ctx->end_of_stream)
1436 if (
ctx->has_sync_buffer_func) {
1504 ctx->global_params_type[
ctx->nb_global_params] =
type;
1506 ctx->global_params_size[
ctx->nb_global_params] =
size;
1508 ++
ctx->nb_global_params;
1521 {
"YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
1522 {
"YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
1523 {
"YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
1524 #if VA_CHECK_VERSION(1, 2, 0)
1525 {
"YUV420_12", VA_RT_FORMAT_YUV420_12, 12, 3, 1, 1 },
1526 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1527 {
"YUV422_12", VA_RT_FORMAT_YUV422_12, 12, 3, 1, 0 },
1528 {
"YUV444_10", VA_RT_FORMAT_YUV444_10, 10, 3, 0, 0 },
1529 {
"YUV444_12", VA_RT_FORMAT_YUV444_12, 12, 3, 0, 0 },
1531 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1532 {
"XYUV", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1533 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1534 #if VA_CHECK_VERSION(0, 38, 1)
1535 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1540 VAEntrypointEncSlice,
1541 VAEntrypointEncPicture,
1542 #if VA_CHECK_VERSION(0, 39, 2)
1543 VAEntrypointEncSliceLP,
1547 #if VA_CHECK_VERSION(0, 39, 2)
1548 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1549 VAEntrypointEncSliceLP,
1557 VAProfile *va_profiles =
NULL;
1558 VAEntrypoint *va_entrypoints =
NULL;
1560 const VAEntrypoint *usable_entrypoints;
1563 VAConfigAttrib rt_format_attr;
1565 const char *profile_string, *entrypoint_string;
1566 int i, j, n, depth, err;
1569 if (
ctx->low_power) {
1570 #if VA_CHECK_VERSION(0, 39, 2)
1571 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1574 "supported with this VAAPI version.\n");
1584 ctx->input_frames->sw_format);
1587 depth =
desc->comp[0].depth;
1588 for (
i = 1;
i <
desc->nb_components;
i++) {
1589 if (
desc->comp[
i].depth != depth) {
1598 n = vaMaxNumProfiles(
ctx->hwctx->display);
1604 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
1605 if (vas != VA_STATUS_SUCCESS) {
1607 vas, vaErrorStr(vas));
1613 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1616 if (depth !=
profile->depth ||
1619 if (
desc->nb_components > 1 &&
1627 #if VA_CHECK_VERSION(1, 0, 0)
1628 profile_string = vaProfileStr(
profile->va_profile);
1630 profile_string =
"(no profile names)";
1633 for (j = 0; j < n; j++) {
1634 if (va_profiles[j] ==
profile->va_profile)
1639 "is not supported by driver.\n", profile_string,
1647 if (!
ctx->profile) {
1656 profile_string,
ctx->va_profile);
1658 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1660 if (!va_entrypoints) {
1664 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1665 va_entrypoints, &n);
1666 if (vas != VA_STATUS_SUCCESS) {
1668 "profile %s (%d): %d (%s).\n", profile_string,
1669 ctx->va_profile, vas, vaErrorStr(vas));
1674 for (
i = 0;
i < n;
i++) {
1675 for (j = 0; usable_entrypoints[j]; j++) {
1676 if (va_entrypoints[
i] == usable_entrypoints[j])
1679 if (usable_entrypoints[j])
1684 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1689 ctx->va_entrypoint = va_entrypoints[
i];
1690 #if VA_CHECK_VERSION(1, 0, 0)
1691 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1693 entrypoint_string =
"(no entrypoint names)";
1696 entrypoint_string,
ctx->va_entrypoint);
1700 if (rt_format->
depth == depth &&
1708 "found for profile %s (%d) entrypoint %s (%d).\n",
1709 profile_string,
ctx->va_profile,
1710 entrypoint_string,
ctx->va_entrypoint);
1715 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1716 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1717 ctx->va_profile,
ctx->va_entrypoint,
1718 &rt_format_attr, 1);
1719 if (vas != VA_STATUS_SUCCESS) {
1721 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1726 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1728 "supported by driver: assuming surface RT format %s "
1729 "is valid.\n", rt_format->
name);
1730 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1732 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1733 rt_format->
name, profile_string,
ctx->va_profile,
1734 entrypoint_string,
ctx->va_entrypoint);
1739 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1740 ctx->config_attributes[
ctx->nb_config_attributes++] =
1742 .type = VAConfigAttribRTFormat,
1743 .value = rt_format->
value,
1761 #if VA_CHECK_VERSION(1, 1, 0)
1766 #if VA_CHECK_VERSION(1, 3, 0)
1778 uint32_t supported_va_rc_modes;
1780 int64_t rc_bits_per_second;
1781 int rc_target_percentage;
1784 int64_t hrd_buffer_size;
1785 int64_t hrd_initial_buffer_fullness;
1787 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1789 char supported_rc_modes_string[64];
1791 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1792 ctx->va_profile,
ctx->va_entrypoint,
1794 if (vas != VA_STATUS_SUCCESS) {
1796 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1799 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1801 "supported rate control modes: assuming CQP only.\n");
1802 supported_va_rc_modes = VA_RC_CQP;
1803 strcpy(supported_rc_modes_string,
"unknown");
1805 char *str = supported_rc_modes_string;
1806 size_t len =
sizeof(supported_rc_modes_string);
1809 supported_va_rc_modes = rc_attr.value;
1812 if (supported_va_rc_modes &
rc_mode->va_mode) {
1828 supported_rc_modes_string);
1843 #define TRY_RC_MODE(mode, fail) do { \
1844 rc_mode = &vaapi_encode_rc_modes[mode]; \
1845 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1847 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1848 "RC mode (supported modes: %s).\n", rc_mode->name, \
1849 supported_rc_modes_string); \
1850 return AVERROR(EINVAL); \
1852 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1853 "RC mode.\n", rc_mode->name); \
1856 goto rc_mode_found; \
1860 if (
ctx->explicit_rc_mode)
1863 if (
ctx->explicit_qp)
1893 "RC mode compatible with selected options "
1894 "(supported modes: %s).\n", supported_rc_modes_string);
1911 rc_bits_per_second = avctx->
bit_rate;
1917 rc_target_percentage = 100;
1924 }
else if (
rc_mode->maxrate) {
1928 "bitrate (%"PRId64
") must not be greater than "
1929 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1934 rc_target_percentage = (avctx->
bit_rate * 100) /
1941 rc_bits_per_second = 2 * avctx->
bit_rate;
1942 rc_target_percentage = 50;
1947 "in %s RC mode.\n",
rc_mode->name);
1949 rc_bits_per_second = avctx->
bit_rate;
1950 rc_target_percentage = 100;
1953 rc_bits_per_second = 0;
1954 rc_target_percentage = 100;
1958 if (
ctx->explicit_qp) {
1959 rc_quality =
ctx->explicit_qp;
1963 rc_quality =
ctx->codec->default_quality;
1965 "using default (%d).\n", rc_quality);
1981 "must have initial buffer size (%d) <= "
1982 "buffer size (%"PRId64
").\n",
1988 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1991 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1995 "in %s RC mode.\n",
rc_mode->name);
1998 hrd_buffer_size = 0;
1999 hrd_initial_buffer_fullness = 0;
2003 rc_window_size = 1000;
2007 if (rc_bits_per_second > UINT32_MAX ||
2008 hrd_buffer_size > UINT32_MAX ||
2009 hrd_initial_buffer_fullness > UINT32_MAX) {
2011 "greater are not supported by VAAPI.\n");
2016 ctx->rc_quality = rc_quality;
2018 ctx->va_bit_rate = rc_bits_per_second;
2021 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2024 ctx->config_attributes[
ctx->nb_config_attributes++] =
2026 .type = VAConfigAttribRateControl,
2027 .value =
ctx->va_rc_mode,
2034 if (
rc_mode->va_mode != VA_RC_CQP) {
2037 "converging in %d frames with %d%% accuracy.\n",
2038 rc_bits_per_second, rc_window_size,
2039 rc_target_percentage);
2040 }
else if (
rc_mode->bitrate) {
2042 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
2043 rc_bits_per_second, rc_window_size);
2046 ctx->rc_params = (VAEncMiscParameterRateControl) {
2047 .bits_per_second = rc_bits_per_second,
2048 .target_percentage = rc_target_percentage,
2049 .window_size = rc_window_size,
2051 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
2052 .basic_unit_size = 0,
2053 #
if VA_CHECK_VERSION(1, 1, 0)
2054 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
2055 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
2057 #
if VA_CHECK_VERSION(1, 3, 0)
2058 .quality_factor = rc_quality,
2062 VAEncMiscParameterTypeRateControl,
2064 sizeof(
ctx->rc_params));
2069 "initial fullness %"PRId64
" bits.\n",
2070 hrd_buffer_size, hrd_initial_buffer_fullness);
2072 ctx->hrd_params = (VAEncMiscParameterHRD) {
2073 .initial_buffer_fullness = hrd_initial_buffer_fullness,
2074 .buffer_size = hrd_buffer_size,
2077 VAEncMiscParameterTypeHRD,
2079 sizeof(
ctx->hrd_params));
2090 fr_num, fr_den, (
double)fr_num / fr_den);
2092 ctx->fr_params = (VAEncMiscParameterFrameRate) {
2093 .framerate = (
unsigned int)fr_den << 16 | fr_num,
2095 #if VA_CHECK_VERSION(0, 40, 0)
2097 VAEncMiscParameterTypeFrameRate,
2099 sizeof(
ctx->fr_params));
2107 #if VA_CHECK_VERSION(1, 5, 0)
2109 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
2112 if (
ctx->va_rc_mode == VA_RC_CQP) {
2113 ctx->max_frame_size = 0;
2119 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2123 if (vas != VA_STATUS_SUCCESS) {
2124 ctx->max_frame_size = 0;
2126 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2130 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2131 ctx->max_frame_size = 0;
2133 "is not supported.\n");
2136 VAConfigAttribValMaxFrameSize attr_mfs;
2137 attr_mfs.value = attr.value;
2139 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
2140 ctx->max_frame_size = 0;
2142 "max frame size which has not been implemented in FFmpeg.\n");
2146 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
2147 .max_frame_size =
ctx->max_frame_size * 8,
2151 ctx->max_frame_size);
2155 "this VAAPI version.\n");
2166 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
2167 uint32_t ref_l0, ref_l1;
2168 int prediction_pre_only;
2170 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2174 if (vas != VA_STATUS_SUCCESS) {
2176 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2180 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2181 ref_l0 = ref_l1 = 0;
2183 ref_l0 = attr.value & 0xffff;
2184 ref_l1 = attr.value >> 16 & 0xffff;
2188 prediction_pre_only = 0;
2190 #if VA_CHECK_VERSION(1, 9, 0)
2193 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
2194 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2198 if (vas != VA_STATUS_SUCCESS) {
2200 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2202 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2204 "prediction constraints.\n");
2206 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
2207 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
2209 "direction attribute.\n");
2213 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
2214 if (ref_l0 > 0 && ref_l1 > 0) {
2215 prediction_pre_only = 1;
2217 "lists for B-frames.\n");
2221 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
2222 if (ref_l0 > 0 && ref_l1 > 0) {
2225 "replacing them with B-frames.\n");
2236 }
else if (ref_l0 < 1) {
2238 "reference frames.\n");
2241 ref_l1 < 1 || avctx->max_b_frames < 1 ||
2242 prediction_pre_only) {
2245 "(supported references: %d / %d).\n",
2249 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2251 ctx->p_per_i = INT_MAX;
2256 "(supported references: %d / %d).\n",
2260 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2262 ctx->p_per_i = INT_MAX;
2268 ctx->max_b_depth = 1;
2274 ctx->gop_per_idr =
ctx->idr_interval + 1;
2276 ctx->closed_gop = 1;
2277 ctx->gop_per_idr = 1;
2284 uint32_t slice_structure)
2294 if (avctx->
slices >
ctx->slice_block_rows) {
2296 "configured number of slices (%d < %d); using "
2297 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2298 req_slices =
ctx->slice_block_rows;
2300 req_slices = avctx->
slices;
2302 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2303 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2304 ctx->nb_slices = req_slices;
2305 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2306 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2308 for (k = 1;; k *= 2) {
2309 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2312 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2313 ctx->slice_size = k;
2314 #if VA_CHECK_VERSION(1, 0, 0)
2315 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2316 ctx->nb_slices =
ctx->slice_block_rows;
2317 ctx->slice_size = 1;
2321 "slice structure modes (%#x).\n", slice_structure);
2329 uint32_t slice_structure)
2334 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2335 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2336 ctx->tile_cols == 1))) {
2338 "current tile requirement.\n", slice_structure);
2342 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2343 ctx->tile_cols >
ctx->slice_block_cols) {
2345 "for configured number of tile (%d x %d); ",
2346 ctx->slice_block_rows,
ctx->slice_block_cols,
2347 ctx->tile_rows,
ctx->tile_cols);
2348 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2349 ctx->slice_block_rows :
ctx->tile_rows;
2350 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2351 ctx->slice_block_cols :
ctx->tile_cols;
2353 ctx->tile_rows,
ctx->tile_cols);
2356 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2361 if (avctx->
slices != req_tiles)
2363 "mismatches with configured number of tile (%d != %d); "
2364 "using requested tile number for slice.\n",
2365 avctx->
slices, req_tiles);
2367 ctx->nb_slices = req_tiles;
2371 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2372 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2373 i *
ctx->slice_block_cols /
ctx->tile_cols;
2374 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2377 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2378 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2379 i *
ctx->slice_block_rows /
ctx->tile_rows;
2380 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2384 ctx->tile_rows,
ctx->tile_cols);
2392 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2393 { VAConfigAttribEncSliceStructure },
2394 #if VA_CHECK_VERSION(1, 1, 0)
2395 { VAConfigAttribEncTileSupport },
2399 uint32_t max_slices, slice_structure;
2405 "but this codec does not support controlling slices.\n");
2412 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2413 ctx->slice_block_height;
2414 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2415 ctx->slice_block_width;
2417 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2419 ctx->slice_size =
ctx->slice_block_rows;
2423 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2427 if (vas != VA_STATUS_SUCCESS) {
2429 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2432 max_slices = attr[0].value;
2433 slice_structure = attr[1].value;
2434 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2435 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2437 "pictures as multiple slices.\n.");
2441 if (
ctx->tile_rows &&
ctx->tile_cols) {
2442 #if VA_CHECK_VERSION(1, 1, 0)
2443 uint32_t tile_support = attr[2].value;
2444 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2446 "pictures as multiple tiles.\n.");
2451 "not supported with this VAAPI version.\n");
2456 if (
ctx->tile_rows &&
ctx->tile_cols)
2465 "%d (from %d) due to driver constraints on slice "
2466 "structure.\n",
ctx->nb_slices, avctx->
slices);
2468 if (
ctx->nb_slices > max_slices) {
2470 "encoding with %d slices (max %"PRIu32
").\n",
2471 ctx->nb_slices, max_slices);
2484 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2486 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2490 if (vas != VA_STATUS_SUCCESS) {
2492 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2496 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2497 if (
ctx->desired_packed_headers) {
2499 "packed headers (wanted %#x).\n",
2500 ctx->desired_packed_headers);
2503 "packed headers (none wanted).\n");
2505 ctx->va_packed_headers = 0;
2507 if (
ctx->desired_packed_headers & ~attr.value) {
2509 "wanted packed headers (wanted %#x, found %#x).\n",
2510 ctx->desired_packed_headers, attr.value);
2513 "available (wanted %#x, found %#x).\n",
2514 ctx->desired_packed_headers, attr.value);
2516 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2519 if (
ctx->va_packed_headers) {
2520 ctx->config_attributes[
ctx->nb_config_attributes++] =
2522 .type = VAConfigAttribEncPackedHeaders,
2523 .value =
ctx->va_packed_headers,
2527 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2528 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2531 "sequence headers, but a global header is requested.\n");
2533 "this may result in a stream which is not usable for some "
2534 "purposes (e.g. not muxable to some containers).\n");
2542 #if VA_CHECK_VERSION(0, 36, 0)
2545 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2548 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2552 if (vas != VA_STATUS_SUCCESS) {
2554 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2558 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2561 "supported: will use default quality level.\n");
2566 "valid range is 0-%d, using %d.\n",
2567 attr.value, attr.value);
2571 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2575 VAEncMiscParameterTypeQualityLevel,
2576 &
ctx->quality_params,
2577 sizeof(
ctx->quality_params));
2581 "not supported with this VAAPI version.\n");
2589 #if VA_CHECK_VERSION(1, 0, 0)
2592 VAConfigAttrib attr = { VAConfigAttribEncROI };
2594 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2598 if (vas != VA_STATUS_SUCCESS) {
2600 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2604 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2605 ctx->roi_allowed = 0;
2607 VAConfigAttribValEncROI roi = {
2608 .value = attr.value,
2611 ctx->roi_max_regions = roi.bits.num_roi_regions;
2612 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2613 (
ctx->va_rc_mode == VA_RC_CQP ||
2614 roi.bits.roi_rc_qp_delta_support);
2625 VABufferID buffer_id;
2627 buffer_id = (VABufferID)(uintptr_t)
data;
2629 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2639 VABufferID buffer_id;
2647 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2648 VAEncCodedBufferType,
2649 3 *
ctx->surface_width *
ctx->surface_height +
2650 (1 << 16), 1, 0, &buffer_id);
2651 if (vas != VA_STATUS_SUCCESS) {
2653 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2664 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2699 if (
ctx->input_frames->sw_format ==
2701 recon_format =
ctx->input_frames->sw_format;
2712 recon_format =
ctx->input_frames->sw_format;
2722 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2723 ctx->surface_width,
ctx->surface_height,
2734 if (!
ctx->recon_frames_ref) {
2741 ctx->recon_frames->sw_format = recon_format;
2742 ctx->recon_frames->width =
ctx->surface_width;
2743 ctx->recon_frames->height =
ctx->surface_height;
2748 "frame context: %d.\n", err);
2766 ctx->va_config = VA_INVALID_ID;
2767 ctx->va_context = VA_INVALID_ID;
2778 "required to associate the encoding device.\n");
2783 if (!
ctx->input_frames_ref) {
2790 if (!
ctx->device_ref) {
2795 ctx->hwctx =
ctx->device->hwctx;
2798 if (!
ctx->tail_pkt) {
2807 if (
ctx->codec->get_encoder_caps) {
2808 err =
ctx->codec->get_encoder_caps(avctx);
2816 ctx->slice_block_width = 16;
2817 ctx->slice_block_height = 16;
2847 if (
ctx->max_frame_size) {
2853 vas = vaCreateConfig(
ctx->hwctx->display,
2854 ctx->va_profile,
ctx->va_entrypoint,
2855 ctx->config_attributes,
ctx->nb_config_attributes,
2857 if (vas != VA_STATUS_SUCCESS) {
2859 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2868 recon_hwctx =
ctx->recon_frames->hwctx;
2869 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2870 ctx->surface_width,
ctx->surface_height,
2875 if (vas != VA_STATUS_SUCCESS) {
2877 "context: %d (%s).\n", vas, vaErrorStr(vas));
2882 ctx->output_buffer_pool =
2885 if (!
ctx->output_buffer_pool) {
2890 if (
ctx->codec->configure) {
2891 err =
ctx->codec->configure(avctx);
2896 ctx->output_delay =
ctx->b_per_p;
2897 ctx->decode_delay =
ctx->max_b_depth;
2899 if (
ctx->codec->sequence_params_size > 0) {
2900 ctx->codec_sequence_params =
2902 if (!
ctx->codec_sequence_params) {
2907 if (
ctx->codec->picture_params_size > 0) {
2908 ctx->codec_picture_params =
2910 if (!
ctx->codec_picture_params) {
2916 if (
ctx->codec->init_sequence_params) {
2917 err =
ctx->codec->init_sequence_params(avctx);
2920 "failed: %d.\n", err);
2925 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2926 ctx->codec->write_sequence_header &&
2929 size_t bit_len = 8 *
sizeof(
data);
2931 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2934 "for extradata: %d.\n", err);
2948 #if VA_CHECK_VERSION(1, 9, 0)
2950 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2951 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2952 ctx->has_sync_buffer_func = 1;
2956 if (!
ctx->encode_fifo)
2977 for (pic =
ctx->pic_start; pic; pic = next) {
2984 if (
ctx->va_context != VA_INVALID_ID) {
2985 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2986 ctx->va_context = VA_INVALID_ID;
2989 if (
ctx->va_config != VA_INVALID_ID) {
2990 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2991 ctx->va_config = VA_INVALID_ID;