45 int type,
char *
data,
size_t bit_len)
49 VABufferID param_buffer, data_buffer;
51 VAEncPackedHeaderParameterBuffer params = {
53 .bit_length = bit_len,
54 .has_emulation_bytes = 1,
62 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
63 VAEncPackedHeaderParameterBufferType,
64 sizeof(params), 1, ¶ms, ¶m_buffer);
65 if (vas != VA_STATUS_SUCCESS) {
67 "for packed header (type %d): %d (%s).\n",
68 type, vas, vaErrorStr(vas));
73 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
74 VAEncPackedHeaderDataBufferType,
75 (bit_len + 7) / 8, 1,
data, &data_buffer);
76 if (vas != VA_STATUS_SUCCESS) {
78 "for packed header (type %d): %d (%s).\n",
79 type, vas, vaErrorStr(vas));
85 "(%zu bits).\n",
type, param_buffer, data_buffer, bit_len);
103 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
105 if (vas != VA_STATUS_SUCCESS) {
107 "(type %d): %d (%s).\n",
type, vas, vaErrorStr(vas));
126 VAEncMiscParameterBuffer
header = {
129 size_t buffer_size =
sizeof(
header) +
len;
136 VAEncMiscParameterBufferType,
157 #if VA_CHECK_VERSION(1, 9, 0)
158 if (
ctx->has_sync_buffer_func) {
159 vas = vaSyncBuffer(
ctx->hwctx->display,
161 VA_TIMEOUT_INFINITE);
162 if (vas != VA_STATUS_SUCCESS) {
164 "%d (%s).\n", vas, vaErrorStr(vas));
171 if (vas != VA_STATUS_SUCCESS) {
173 "%d (%s).\n", vas, vaErrorStr(vas));
195 rounding =
ctx->slice_block_rows -
ctx->nb_slices *
ctx->slice_size;
203 for (
i = 0;
i < rounding;
i++)
206 for (
i = 0;
i < (rounding + 1) / 2;
i++)
208 for (
i = 0;
i < rounding / 2;
i++)
211 }
else if (rounding < 0) {
247 for (
i = 0;
i <
ctx->tile_cols;
i++) {
248 for (j = 0; j <
ctx->tile_rows; j++) {
254 ctx->row_bd[j] *
ctx->slice_block_cols;
258 "width:%2d height:%2d (%d blocks).\n",
index,
ctx->col_bd[
i],
336 if (
ctx->codec->picture_params_size > 0) {
341 ctx->codec->picture_params_size);
350 VAEncSequenceParameterBufferType,
351 ctx->codec_sequence_params,
352 ctx->codec->sequence_params_size);
358 for (
i = 0;
i <
ctx->nb_global_params;
i++) {
360 ctx->global_params_type[
i],
361 ctx->global_params[
i],
362 ctx->global_params_size[
i]);
368 if (
ctx->codec->init_picture_params) {
369 err =
ctx->codec->init_picture_params(avctx, pic);
372 "parameters: %d.\n", err);
376 VAEncPictureParameterBufferType,
378 ctx->codec->picture_params_size);
383 #if VA_CHECK_VERSION(1, 5, 0)
384 if (
ctx->max_frame_size) {
386 VAEncMiscParameterTypeMaxFrameSize,
388 sizeof(
ctx->mfs_params));
395 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
396 ctx->codec->write_sequence_header) {
397 bit_len = 8 *
sizeof(
data);
398 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
401 "header: %d.\n", err);
405 ctx->codec->sequence_header_type,
412 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
413 ctx->codec->write_picture_header) {
414 bit_len = 8 *
sizeof(
data);
415 err =
ctx->codec->write_picture_header(avctx, pic,
data, &bit_len);
418 "header: %d.\n", err);
422 ctx->codec->picture_header_type,
428 if (
ctx->codec->write_extra_buffer) {
432 err =
ctx->codec->write_extra_buffer(avctx, pic,
i, &
type,
438 "buffer %d: %d.\n",
i, err);
449 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
450 ctx->codec->write_extra_header) {
453 bit_len = 8 *
sizeof(
data);
454 err =
ctx->codec->write_extra_header(avctx, pic,
i, &
type,
460 "header %d: %d.\n",
i, err);
480 if (
ctx->tile_rows &&
ctx->tile_cols)
489 if (
ctx->codec->slice_params_size > 0) {
497 if (
ctx->codec->init_slice_params) {
498 err =
ctx->codec->init_slice_params(avctx, pic, slice);
501 "parameters: %d.\n", err);
506 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
507 ctx->codec->write_slice_header) {
508 bit_len = 8 *
sizeof(
data);
509 err =
ctx->codec->write_slice_header(avctx, pic, slice,
513 "header: %d.\n", err);
517 ctx->codec->slice_header_type,
523 if (
ctx->codec->init_slice_params) {
525 VAEncSliceParameterBufferType,
527 ctx->codec->slice_params_size);
533 #if VA_CHECK_VERSION(1, 0, 0)
536 if (sd &&
ctx->roi_allowed) {
539 VAEncMiscParameterBufferROI param_roi;
544 av_assert0(roi_size && sd->size % roi_size == 0);
545 nb_roi = sd->size / roi_size;
546 if (nb_roi >
ctx->roi_max_regions) {
547 if (!
ctx->roi_warned) {
549 "supported by driver (%d > %d).\n",
550 nb_roi,
ctx->roi_max_regions);
553 nb_roi =
ctx->roi_max_regions;
562 for (
i = 0;
i < nb_roi;
i++) {
570 pic->
roi[
i] = (VAEncROI) {
581 param_roi = (VAEncMiscParameterBufferROI) {
583 .max_delta_qp = INT8_MAX,
584 .min_delta_qp = INT8_MIN,
586 .roi_flags.bits.roi_value_is_qp_delta = 1,
590 VAEncMiscParameterTypeROI,
598 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
600 if (vas != VA_STATUS_SUCCESS) {
602 "%d (%s).\n", vas, vaErrorStr(vas));
604 goto fail_with_picture;
607 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
609 if (vas != VA_STATUS_SUCCESS) {
611 "%d (%s).\n", vas, vaErrorStr(vas));
613 goto fail_with_picture;
616 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
617 if (vas != VA_STATUS_SUCCESS) {
619 "%d (%s).\n", vas, vaErrorStr(vas));
623 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
630 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
633 vas = vaDestroyBuffer(
ctx->hwctx->display,
635 if (vas != VA_STATUS_SUCCESS) {
637 "param buffer %#x: %d (%s).\n",
649 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
693 if (
ctx->output_delay == 0) {
702 (3 *
ctx->output_delay +
ctx->async_depth)];
711 VACodedBufferSegment *buf_list, *buf;
716 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
718 if (vas != VA_STATUS_SUCCESS) {
720 "%d (%s).\n", vas, vaErrorStr(vas));
725 for (buf = buf_list; buf; buf = buf->next)
728 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
729 if (vas != VA_STATUS_SUCCESS) {
731 "%d (%s).\n", vas, vaErrorStr(vas));
740 VABufferID buf_id, uint8_t **dst)
743 VACodedBufferSegment *buf_list, *buf;
747 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
749 if (vas != VA_STATUS_SUCCESS) {
751 "%d (%s).\n", vas, vaErrorStr(vas));
756 for (buf = buf_list; buf; buf = buf->next) {
758 "(status %08x).\n", buf->size, buf->status);
760 memcpy(*dst, buf->buf, buf->size);
764 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
765 if (vas != VA_STATUS_SUCCESS) {
767 "%d (%s).\n", vas, vaErrorStr(vas));
779 VABufferID output_buffer_prev;
784 if (
ctx->coded_buffer_ref) {
785 output_buffer_prev = *
ctx->coded_buffer_ref;
802 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) {
941 int is_ref,
int in_dpb,
int prev)
1024 if (current_depth ==
ctx->max_b_depth || start->
next->
next == end) {
1025 for (pic = start->
next; pic; pic = pic->
next) {
1044 for (pic = start->
next; pic != end; pic = pic->
next)
1046 for (pic = start->
next,
i = 1; 2 * i < len; pic = pic->next,
i++);
1063 current_depth + 1, &next);
1068 current_depth + 1, last);
1082 for (
i = 0;
i <
ctx->nb_next_prev;
i++) {
1083 --
ctx->next_prev[
i]->ref_count[0];
1086 ctx->next_prev[0] = pic;
1088 ctx->nb_next_prev = 1;
1094 ctx->next_prev[
ctx->nb_next_prev++] = pic;
1097 --
ctx->next_prev[0]->ref_count[0];
1099 ctx->next_prev[
i] =
ctx->next_prev[
i + 1];
1100 ctx->next_prev[
i] = pic;
1110 int i, b_counter, closed_gop_end;
1115 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1137 "encode next.\n", pic->
b_depth);
1146 closed_gop_end =
ctx->closed_gop ||
1147 ctx->idr_counter ==
ctx->gop_per_idr;
1148 for (pic =
ctx->pic_start; pic; pic = next) {
1158 if (b_counter ==
ctx->b_per_p)
1162 if (
ctx->gop_counter + b_counter + closed_gop_end >=
ctx->gop_size)
1166 if (next && next->force_idr)
1172 if (!pic &&
ctx->end_of_stream) {
1183 "need more input for reference pictures.\n");
1186 if (
ctx->input_order <=
ctx->decode_delay && !
ctx->end_of_stream) {
1188 "need more input for timestamps.\n");
1196 ctx->idr_counter = 1;
1197 ctx->gop_counter = 1;
1199 }
else if (
ctx->gop_counter + b_counter >=
ctx->gop_size) {
1200 if (
ctx->idr_counter ==
ctx->gop_per_idr) {
1204 ctx->idr_counter = 1;
1211 ctx->gop_counter = 1;
1214 if (
ctx->gop_counter + b_counter + closed_gop_end ==
ctx->gop_size) {
1223 ctx->gop_counter += 1 + b_counter;
1234 for (
i = 0;
i <
ctx->nb_next_prev;
i++)
1246 if (b_counter > 0) {
1265 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1271 for (pic =
ctx->pic_start; pic; pic = pic->
next) {
1278 for (pic =
ctx->pic_start; pic; pic = next) {
1285 ctx->pic_start = next;
1300 if ((
frame->crop_top ||
frame->crop_bottom ||
1301 frame->crop_left ||
frame->crop_right) && !
ctx->crop_warned) {
1303 "frames ignored due to lack of API support.\n");
1304 ctx->crop_warned = 1;
1307 if (!
ctx->roi_allowed) {
1311 if (sd && !
ctx->roi_warned) {
1313 "frames ignored due to lack of driver support.\n");
1314 ctx->roi_warned = 1;
1362 if (
ctx->input_order == 0)
1363 ctx->first_pts = pic->
pts;
1364 if (
ctx->input_order ==
ctx->decode_delay)
1365 ctx->dts_pts_diff = pic->
pts -
ctx->first_pts;
1366 if (
ctx->output_delay > 0)
1367 ctx->ts_ring[
ctx->input_order %
1368 (3 *
ctx->output_delay +
ctx->async_depth)] = pic->
pts;
1373 if (
ctx->pic_start) {
1374 ctx->pic_end->next = pic;
1377 ctx->pic_start = pic;
1382 ctx->end_of_stream = 1;
1386 if (
ctx->input_order <
ctx->decode_delay)
1387 ctx->dts_pts_diff =
ctx->pic_end->pts -
ctx->first_pts;
1406 if (
ctx->tail_pkt->size) {
1410 else if (!
tmp->next) {
1428 if (!
ctx->pic_start) {
1429 if (
ctx->end_of_stream)
1435 if (
ctx->has_sync_buffer_func) {
1503 ctx->global_params_type[
ctx->nb_global_params] =
type;
1505 ctx->global_params_size[
ctx->nb_global_params] =
size;
1507 ++
ctx->nb_global_params;
1520 {
"YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
1521 {
"YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
1522 {
"YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
1523 #if VA_CHECK_VERSION(1, 2, 0)
1524 {
"YUV420_12", VA_RT_FORMAT_YUV420_12, 12, 3, 1, 1 },
1525 {
"YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1526 {
"YUV422_12", VA_RT_FORMAT_YUV422_12, 12, 3, 1, 0 },
1527 {
"YUV444_10", VA_RT_FORMAT_YUV444_10, 10, 3, 0, 0 },
1528 {
"YUV444_12", VA_RT_FORMAT_YUV444_12, 12, 3, 0, 0 },
1530 {
"YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1531 {
"XYUV", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1532 {
"YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1533 #if VA_CHECK_VERSION(0, 38, 1)
1534 {
"YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1539 VAEntrypointEncSlice,
1540 VAEntrypointEncPicture,
1541 #if VA_CHECK_VERSION(0, 39, 2)
1542 VAEntrypointEncSliceLP,
1546 #if VA_CHECK_VERSION(0, 39, 2)
1547 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1548 VAEntrypointEncSliceLP,
1556 VAProfile *va_profiles =
NULL;
1557 VAEntrypoint *va_entrypoints =
NULL;
1559 const VAEntrypoint *usable_entrypoints;
1562 VAConfigAttrib rt_format_attr;
1564 const char *profile_string, *entrypoint_string;
1565 int i, j, n, depth, err;
1568 if (
ctx->low_power) {
1569 #if VA_CHECK_VERSION(0, 39, 2)
1570 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1573 "supported with this VAAPI version.\n");
1583 ctx->input_frames->sw_format);
1586 depth =
desc->comp[0].depth;
1587 for (
i = 1;
i <
desc->nb_components;
i++) {
1588 if (
desc->comp[
i].depth != depth) {
1597 n = vaMaxNumProfiles(
ctx->hwctx->display);
1603 vas = vaQueryConfigProfiles(
ctx->hwctx->display, va_profiles, &n);
1604 if (vas != VA_STATUS_SUCCESS) {
1606 vas, vaErrorStr(vas));
1612 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
1615 if (depth !=
profile->depth ||
1618 if (
desc->nb_components > 1 &&
1626 #if VA_CHECK_VERSION(1, 0, 0)
1627 profile_string = vaProfileStr(
profile->va_profile);
1629 profile_string =
"(no profile names)";
1632 for (j = 0; j < n; j++) {
1633 if (va_profiles[j] ==
profile->va_profile)
1638 "is not supported by driver.\n", profile_string,
1646 if (!
ctx->profile) {
1655 profile_string,
ctx->va_profile);
1657 n = vaMaxNumEntrypoints(
ctx->hwctx->display);
1659 if (!va_entrypoints) {
1663 vas = vaQueryConfigEntrypoints(
ctx->hwctx->display,
ctx->va_profile,
1664 va_entrypoints, &n);
1665 if (vas != VA_STATUS_SUCCESS) {
1667 "profile %s (%d): %d (%s).\n", profile_string,
1668 ctx->va_profile, vas, vaErrorStr(vas));
1673 for (
i = 0;
i < n;
i++) {
1674 for (j = 0; usable_entrypoints[j]; j++) {
1675 if (va_entrypoints[
i] == usable_entrypoints[j])
1678 if (usable_entrypoints[j])
1683 "for profile %s (%d).\n", profile_string,
ctx->va_profile);
1688 ctx->va_entrypoint = va_entrypoints[
i];
1689 #if VA_CHECK_VERSION(1, 0, 0)
1690 entrypoint_string = vaEntrypointStr(
ctx->va_entrypoint);
1692 entrypoint_string =
"(no entrypoint names)";
1695 entrypoint_string,
ctx->va_entrypoint);
1699 if (rt_format->
depth == depth &&
1707 "found for profile %s (%d) entrypoint %s (%d).\n",
1708 profile_string,
ctx->va_profile,
1709 entrypoint_string,
ctx->va_entrypoint);
1714 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1715 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1716 ctx->va_profile,
ctx->va_entrypoint,
1717 &rt_format_attr, 1);
1718 if (vas != VA_STATUS_SUCCESS) {
1720 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1725 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1727 "supported by driver: assuming surface RT format %s "
1728 "is valid.\n", rt_format->
name);
1729 }
else if (!(rt_format_attr.value & rt_format->
value)) {
1731 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1732 rt_format->
name, profile_string,
ctx->va_profile,
1733 entrypoint_string,
ctx->va_entrypoint);
1738 "format %s (%#x).\n", rt_format->
name, rt_format->
value);
1739 ctx->config_attributes[
ctx->nb_config_attributes++] =
1741 .type = VAConfigAttribRTFormat,
1742 .value = rt_format->
value,
1760 #if VA_CHECK_VERSION(1, 1, 0)
1765 #if VA_CHECK_VERSION(1, 3, 0)
1777 uint32_t supported_va_rc_modes;
1779 int64_t rc_bits_per_second;
1780 int rc_target_percentage;
1783 int64_t hrd_buffer_size;
1784 int64_t hrd_initial_buffer_fullness;
1786 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1788 char supported_rc_modes_string[64];
1790 vas = vaGetConfigAttributes(
ctx->hwctx->display,
1791 ctx->va_profile,
ctx->va_entrypoint,
1793 if (vas != VA_STATUS_SUCCESS) {
1795 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1798 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1800 "supported rate control modes: assuming CQP only.\n");
1801 supported_va_rc_modes = VA_RC_CQP;
1802 strcpy(supported_rc_modes_string,
"unknown");
1804 char *str = supported_rc_modes_string;
1805 size_t len =
sizeof(supported_rc_modes_string);
1808 supported_va_rc_modes = rc_attr.value;
1810 #if VA_CHECK_VERSION(0, 39, 2)
1811 if (!(supported_va_rc_modes & VA_RC_MB)) {
1818 "or above, which can support BLBRC.\n");
1824 if (supported_va_rc_modes &
rc_mode->va_mode) {
1840 supported_rc_modes_string);
1855 #define TRY_RC_MODE(mode, fail) do { \
1856 rc_mode = &vaapi_encode_rc_modes[mode]; \
1857 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1859 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1860 "RC mode (supported modes: %s).\n", rc_mode->name, \
1861 supported_rc_modes_string); \
1862 return AVERROR(EINVAL); \
1864 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1865 "RC mode.\n", rc_mode->name); \
1868 goto rc_mode_found; \
1872 if (
ctx->explicit_rc_mode)
1875 if (
ctx->explicit_qp)
1905 "RC mode compatible with selected options "
1906 "(supported modes: %s).\n", supported_rc_modes_string);
1923 rc_bits_per_second = avctx->
bit_rate;
1929 rc_target_percentage = 100;
1936 }
else if (
rc_mode->maxrate) {
1940 "bitrate (%"PRId64
") must not be greater than "
1941 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1946 rc_target_percentage = (avctx->
bit_rate * 100) /
1953 rc_bits_per_second = 2 * avctx->
bit_rate;
1954 rc_target_percentage = 50;
1959 "in %s RC mode.\n",
rc_mode->name);
1961 rc_bits_per_second = avctx->
bit_rate;
1962 rc_target_percentage = 100;
1965 rc_bits_per_second = 0;
1966 rc_target_percentage = 100;
1970 if (
ctx->explicit_qp) {
1971 rc_quality =
ctx->explicit_qp;
1978 rc_quality =
ctx->codec->default_quality;
1980 "using default (%d).\n", rc_quality);
1996 "must have initial buffer size (%d) <= "
1997 "buffer size (%"PRId64
").\n",
2003 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
2006 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
2010 "in %s RC mode.\n",
rc_mode->name);
2013 hrd_buffer_size = 0;
2014 hrd_initial_buffer_fullness = 0;
2018 rc_window_size = 1000;
2022 if (rc_bits_per_second > UINT32_MAX ||
2023 hrd_buffer_size > UINT32_MAX ||
2024 hrd_initial_buffer_fullness > UINT32_MAX) {
2026 "greater are not supported by VAAPI.\n");
2031 ctx->rc_quality = rc_quality;
2033 ctx->va_bit_rate = rc_bits_per_second;
2037 if (
ctx->blbrc &&
ctx->va_rc_mode == VA_RC_CQP)
2041 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2044 ctx->config_attributes[
ctx->nb_config_attributes++] =
2046 .type = VAConfigAttribRateControl,
2047 #if VA_CHECK_VERSION(0, 39, 2)
2048 .value =
ctx->blbrc ?
ctx->va_rc_mode | VA_RC_MB :
ctx->va_rc_mode,
2050 .value =
ctx->va_rc_mode,
2058 if (
rc_mode->va_mode != VA_RC_CQP) {
2061 "converging in %d frames with %d%% accuracy.\n",
2062 rc_bits_per_second, rc_window_size,
2063 rc_target_percentage);
2064 }
else if (
rc_mode->bitrate) {
2066 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
2067 rc_bits_per_second, rc_window_size);
2070 ctx->rc_params = (VAEncMiscParameterRateControl) {
2071 .bits_per_second = rc_bits_per_second,
2072 .target_percentage = rc_target_percentage,
2073 .window_size = rc_window_size,
2075 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
2076 .basic_unit_size = 0,
2077 #
if VA_CHECK_VERSION(1, 1, 0)
2078 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
2079 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
2081 #
if VA_CHECK_VERSION(1, 3, 0)
2082 .quality_factor = rc_quality,
2084 #if VA_CHECK_VERSION(0, 39, 2)
2085 .rc_flags.bits.mb_rate_control =
ctx->blbrc ? 1 : 2,
2089 VAEncMiscParameterTypeRateControl,
2091 sizeof(
ctx->rc_params));
2096 "initial fullness %"PRId64
" bits.\n",
2097 hrd_buffer_size, hrd_initial_buffer_fullness);
2099 ctx->hrd_params = (VAEncMiscParameterHRD) {
2100 .initial_buffer_fullness = hrd_initial_buffer_fullness,
2101 .buffer_size = hrd_buffer_size,
2104 VAEncMiscParameterTypeHRD,
2106 sizeof(
ctx->hrd_params));
2117 fr_num, fr_den, (
double)fr_num / fr_den);
2119 ctx->fr_params = (VAEncMiscParameterFrameRate) {
2120 .framerate = (
unsigned int)fr_den << 16 | fr_num,
2122 #if VA_CHECK_VERSION(0, 40, 0)
2124 VAEncMiscParameterTypeFrameRate,
2126 sizeof(
ctx->fr_params));
2134 #if VA_CHECK_VERSION(1, 5, 0)
2136 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
2139 if (
ctx->va_rc_mode == VA_RC_CQP) {
2140 ctx->max_frame_size = 0;
2146 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2150 if (vas != VA_STATUS_SUCCESS) {
2151 ctx->max_frame_size = 0;
2153 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2157 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2158 ctx->max_frame_size = 0;
2160 "is not supported.\n");
2163 VAConfigAttribValMaxFrameSize attr_mfs;
2164 attr_mfs.value = attr.value;
2166 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
2167 ctx->max_frame_size = 0;
2169 "max frame size which has not been implemented in FFmpeg.\n");
2173 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
2174 .max_frame_size =
ctx->max_frame_size * 8,
2178 ctx->max_frame_size);
2182 "this VAAPI version.\n");
2193 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
2194 uint32_t ref_l0, ref_l1;
2195 int prediction_pre_only;
2197 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2201 if (vas != VA_STATUS_SUCCESS) {
2203 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2207 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2208 ref_l0 = ref_l1 = 0;
2210 ref_l0 = attr.value & 0xffff;
2211 ref_l1 = attr.value >> 16 & 0xffff;
2215 prediction_pre_only = 0;
2217 #if VA_CHECK_VERSION(1, 9, 0)
2220 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
2221 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2225 if (vas != VA_STATUS_SUCCESS) {
2227 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2229 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2231 "prediction constraints.\n");
2233 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
2234 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
2236 "direction attribute.\n");
2240 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
2241 if (ref_l0 > 0 && ref_l1 > 0) {
2242 prediction_pre_only = 1;
2244 "lists for B-frames.\n");
2248 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
2249 if (ref_l0 > 0 && ref_l1 > 0) {
2252 "replacing them with B-frames.\n");
2263 }
else if (ref_l0 < 1) {
2265 "reference frames.\n");
2268 ref_l1 < 1 || avctx->max_b_frames < 1 ||
2269 prediction_pre_only) {
2272 "(supported references: %d / %d).\n",
2276 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2278 ctx->p_per_i = INT_MAX;
2283 "(supported references: %d / %d).\n",
2287 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2289 ctx->p_per_i = INT_MAX;
2295 ctx->max_b_depth = 1;
2301 ctx->gop_per_idr =
ctx->idr_interval + 1;
2303 ctx->closed_gop = 1;
2304 ctx->gop_per_idr = 1;
2311 uint32_t slice_structure)
2321 if (avctx->
slices >
ctx->slice_block_rows) {
2323 "configured number of slices (%d < %d); using "
2324 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2325 req_slices =
ctx->slice_block_rows;
2327 req_slices = avctx->
slices;
2329 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2330 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2331 ctx->nb_slices = req_slices;
2332 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2333 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2335 for (k = 1;; k *= 2) {
2336 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2339 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2340 ctx->slice_size = k;
2341 #if VA_CHECK_VERSION(1, 0, 0)
2342 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2343 ctx->nb_slices =
ctx->slice_block_rows;
2344 ctx->slice_size = 1;
2348 "slice structure modes (%#x).\n", slice_structure);
2356 uint32_t slice_structure)
2361 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2362 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2363 ctx->tile_cols == 1))) {
2365 "current tile requirement.\n", slice_structure);
2369 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2370 ctx->tile_cols >
ctx->slice_block_cols) {
2372 "for configured number of tile (%d x %d); ",
2373 ctx->slice_block_rows,
ctx->slice_block_cols,
2374 ctx->tile_rows,
ctx->tile_cols);
2375 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2376 ctx->slice_block_rows :
ctx->tile_rows;
2377 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2378 ctx->slice_block_cols :
ctx->tile_cols;
2380 ctx->tile_rows,
ctx->tile_cols);
2383 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2388 if (avctx->
slices != req_tiles)
2390 "mismatches with configured number of tile (%d != %d); "
2391 "using requested tile number for slice.\n",
2392 avctx->
slices, req_tiles);
2394 ctx->nb_slices = req_tiles;
2398 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2399 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2400 i *
ctx->slice_block_cols /
ctx->tile_cols;
2401 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2404 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2405 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2406 i *
ctx->slice_block_rows /
ctx->tile_rows;
2407 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2411 ctx->tile_rows,
ctx->tile_cols);
2419 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2420 { VAConfigAttribEncSliceStructure },
2421 #if VA_CHECK_VERSION(1, 1, 0)
2422 { VAConfigAttribEncTileSupport },
2426 uint32_t max_slices, slice_structure;
2432 "but this codec does not support controlling slices.\n");
2439 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2440 ctx->slice_block_height;
2441 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2442 ctx->slice_block_width;
2444 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2446 ctx->slice_size =
ctx->slice_block_rows;
2450 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2454 if (vas != VA_STATUS_SUCCESS) {
2456 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2459 max_slices = attr[0].value;
2460 slice_structure = attr[1].value;
2461 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2462 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2464 "pictures as multiple slices.\n.");
2468 if (
ctx->tile_rows &&
ctx->tile_cols) {
2469 #if VA_CHECK_VERSION(1, 1, 0)
2470 uint32_t tile_support = attr[2].value;
2471 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2473 "pictures as multiple tiles.\n.");
2478 "not supported with this VAAPI version.\n");
2483 if (
ctx->tile_rows &&
ctx->tile_cols)
2492 "%d (from %d) due to driver constraints on slice "
2493 "structure.\n",
ctx->nb_slices, avctx->
slices);
2495 if (
ctx->nb_slices > max_slices) {
2497 "encoding with %d slices (max %"PRIu32
").\n",
2498 ctx->nb_slices, max_slices);
2511 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2513 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2517 if (vas != VA_STATUS_SUCCESS) {
2519 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2523 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2524 if (
ctx->desired_packed_headers) {
2526 "packed headers (wanted %#x).\n",
2527 ctx->desired_packed_headers);
2530 "packed headers (none wanted).\n");
2532 ctx->va_packed_headers = 0;
2534 if (
ctx->desired_packed_headers & ~attr.value) {
2536 "wanted packed headers (wanted %#x, found %#x).\n",
2537 ctx->desired_packed_headers, attr.value);
2540 "available (wanted %#x, found %#x).\n",
2541 ctx->desired_packed_headers, attr.value);
2543 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2546 if (
ctx->va_packed_headers) {
2547 ctx->config_attributes[
ctx->nb_config_attributes++] =
2549 .type = VAConfigAttribEncPackedHeaders,
2550 .value =
ctx->va_packed_headers,
2554 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2555 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2558 "sequence headers, but a global header is requested.\n");
2560 "this may result in a stream which is not usable for some "
2561 "purposes (e.g. not muxable to some containers).\n");
2569 #if VA_CHECK_VERSION(0, 36, 0)
2572 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2575 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2579 if (vas != VA_STATUS_SUCCESS) {
2581 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2585 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2588 "supported: will use default quality level.\n");
2593 "valid range is 0-%d, using %d.\n",
2594 attr.value, attr.value);
2598 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2602 VAEncMiscParameterTypeQualityLevel,
2603 &
ctx->quality_params,
2604 sizeof(
ctx->quality_params));
2608 "not supported with this VAAPI version.\n");
2616 #if VA_CHECK_VERSION(1, 0, 0)
2619 VAConfigAttrib attr = { VAConfigAttribEncROI };
2621 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2625 if (vas != VA_STATUS_SUCCESS) {
2627 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2631 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2632 ctx->roi_allowed = 0;
2634 VAConfigAttribValEncROI roi = {
2635 .value = attr.value,
2638 ctx->roi_max_regions = roi.bits.num_roi_regions;
2639 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2640 (
ctx->va_rc_mode == VA_RC_CQP ||
2641 roi.bits.roi_rc_qp_delta_support);
2652 VABufferID *buffer_id_ref = obj;
2653 VABufferID buffer_id = *buffer_id_ref;
2655 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2664 VABufferID *buffer_id = obj;
2671 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2672 VAEncCodedBufferType,
2673 3 *
ctx->surface_width *
ctx->surface_height +
2674 (1 << 16), 1, 0, buffer_id);
2675 if (vas != VA_STATUS_SUCCESS) {
2677 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2714 if (
ctx->input_frames->sw_format ==
2716 recon_format =
ctx->input_frames->sw_format;
2727 recon_format =
ctx->input_frames->sw_format;
2737 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2738 ctx->surface_width,
ctx->surface_height,
2749 if (!
ctx->recon_frames_ref) {
2756 ctx->recon_frames->sw_format = recon_format;
2757 ctx->recon_frames->width =
ctx->surface_width;
2758 ctx->recon_frames->height =
ctx->surface_height;
2763 "frame context: %d.\n", err);
2781 ctx->va_config = VA_INVALID_ID;
2782 ctx->va_context = VA_INVALID_ID;
2793 "required to associate the encoding device.\n");
2798 if (!
ctx->input_frames_ref) {
2805 if (!
ctx->device_ref) {
2810 ctx->hwctx =
ctx->device->hwctx;
2813 if (!
ctx->tail_pkt) {
2822 if (
ctx->codec->get_encoder_caps) {
2823 err =
ctx->codec->get_encoder_caps(avctx);
2831 ctx->slice_block_width = 16;
2832 ctx->slice_block_height = 16;
2862 if (
ctx->max_frame_size) {
2868 vas = vaCreateConfig(
ctx->hwctx->display,
2869 ctx->va_profile,
ctx->va_entrypoint,
2870 ctx->config_attributes,
ctx->nb_config_attributes,
2872 if (vas != VA_STATUS_SUCCESS) {
2874 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2883 recon_hwctx =
ctx->recon_frames->hwctx;
2884 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2885 ctx->surface_width,
ctx->surface_height,
2890 if (vas != VA_STATUS_SUCCESS) {
2892 "context: %d (%s).\n", vas, vaErrorStr(vas));
2897 ctx->output_buffer_pool =
2901 if (!
ctx->output_buffer_pool) {
2906 if (
ctx->codec->configure) {
2907 err =
ctx->codec->configure(avctx);
2912 ctx->output_delay =
ctx->b_per_p;
2913 ctx->decode_delay =
ctx->max_b_depth;
2915 if (
ctx->codec->sequence_params_size > 0) {
2916 ctx->codec_sequence_params =
2918 if (!
ctx->codec_sequence_params) {
2923 if (
ctx->codec->picture_params_size > 0) {
2924 ctx->codec_picture_params =
2926 if (!
ctx->codec_picture_params) {
2932 if (
ctx->codec->init_sequence_params) {
2933 err =
ctx->codec->init_sequence_params(avctx);
2936 "failed: %d.\n", err);
2941 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2942 ctx->codec->write_sequence_header &&
2945 size_t bit_len = 8 *
sizeof(
data);
2947 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2950 "for extradata: %d.\n", err);
2964 #if VA_CHECK_VERSION(1, 9, 0)
2966 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2967 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2968 ctx->has_sync_buffer_func = 1;
2972 if (!
ctx->encode_fifo)
2993 for (pic =
ctx->pic_start; pic; pic = next) {
3000 if (
ctx->va_context != VA_INVALID_ID) {
3001 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
3002 ctx->va_context = VA_INVALID_ID;
3005 if (
ctx->va_config != VA_INVALID_ID) {
3006 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
3007 ctx->va_config = VA_INVALID_ID;