44 int type,
char *
data,
size_t bit_len)
48 VABufferID param_buffer, data_buffer;
50 VAEncPackedHeaderParameterBuffer params = {
52 .bit_length = bit_len,
53 .has_emulation_bytes = 1,
61 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
62 VAEncPackedHeaderParameterBufferType,
63 sizeof(params), 1, ¶ms, ¶m_buffer);
64 if (vas != VA_STATUS_SUCCESS) {
66 "for packed header (type %d): %d (%s).\n",
67 type, vas, vaErrorStr(vas));
72 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
73 VAEncPackedHeaderDataBufferType,
74 (bit_len + 7) / 8, 1,
data, &data_buffer);
75 if (vas != VA_STATUS_SUCCESS) {
77 "for packed header (type %d): %d (%s).\n",
78 type, vas, vaErrorStr(vas));
84 "(%zu bits).\n",
type, param_buffer, data_buffer, bit_len);
102 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
104 if (vas != VA_STATUS_SUCCESS) {
106 "(type %d): %d (%s).\n",
type, vas, vaErrorStr(vas));
125 VAEncMiscParameterBuffer
header = {
128 size_t buffer_size =
sizeof(
header) +
len;
135 VAEncMiscParameterBufferType,
156 #if VA_CHECK_VERSION(1, 9, 0)
157 if (
ctx->has_sync_buffer_func) {
158 vas = vaSyncBuffer(
ctx->hwctx->display,
160 VA_TIMEOUT_INFINITE);
161 if (vas != VA_STATUS_SUCCESS) {
163 "%d (%s).\n", vas, vaErrorStr(vas));
170 if (vas != VA_STATUS_SUCCESS) {
172 "%d (%s).\n", vas, vaErrorStr(vas));
194 rounding =
ctx->slice_block_rows -
ctx->nb_slices *
ctx->slice_size;
202 for (
i = 0;
i < rounding;
i++)
205 for (
i = 0;
i < (rounding + 1) / 2;
i++)
207 for (
i = 0;
i < rounding / 2;
i++)
210 }
else if (rounding < 0) {
246 for (
i = 0;
i <
ctx->tile_cols;
i++) {
247 for (j = 0; j <
ctx->tile_rows; j++) {
253 ctx->row_bd[j] *
ctx->slice_block_cols;
257 "width:%2d height:%2d (%d blocks).\n",
index,
ctx->col_bd[
i],
335 if (
ctx->codec->picture_params_size > 0) {
340 ctx->codec->picture_params_size);
349 VAEncSequenceParameterBufferType,
350 ctx->codec_sequence_params,
351 ctx->codec->sequence_params_size);
357 for (
i = 0;
i <
ctx->nb_global_params;
i++) {
359 ctx->global_params_type[
i],
360 ctx->global_params[
i],
361 ctx->global_params_size[
i]);
367 if (
ctx->codec->init_picture_params) {
368 err =
ctx->codec->init_picture_params(avctx, pic);
371 "parameters: %d.\n", err);
375 VAEncPictureParameterBufferType,
377 ctx->codec->picture_params_size);
382 #if VA_CHECK_VERSION(1, 5, 0)
383 if (
ctx->max_frame_size) {
385 VAEncMiscParameterTypeMaxFrameSize,
387 sizeof(
ctx->mfs_params));
394 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
395 ctx->codec->write_sequence_header) {
396 bit_len = 8 *
sizeof(
data);
397 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
400 "header: %d.\n", err);
404 ctx->codec->sequence_header_type,
411 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
412 ctx->codec->write_picture_header) {
413 bit_len = 8 *
sizeof(
data);
414 err =
ctx->codec->write_picture_header(avctx, pic,
data, &bit_len);
417 "header: %d.\n", err);
421 ctx->codec->picture_header_type,
427 if (
ctx->codec->write_extra_buffer) {
431 err =
ctx->codec->write_extra_buffer(avctx, pic,
i, &
type,
437 "buffer %d: %d.\n",
i, err);
448 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
449 ctx->codec->write_extra_header) {
452 bit_len = 8 *
sizeof(
data);
453 err =
ctx->codec->write_extra_header(avctx, pic,
i, &
type,
459 "header %d: %d.\n",
i, err);
479 if (
ctx->tile_rows &&
ctx->tile_cols)
488 if (
ctx->codec->slice_params_size > 0) {
496 if (
ctx->codec->init_slice_params) {
497 err =
ctx->codec->init_slice_params(avctx, pic, slice);
500 "parameters: %d.\n", err);
505 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
506 ctx->codec->write_slice_header) {
507 bit_len = 8 *
sizeof(
data);
508 err =
ctx->codec->write_slice_header(avctx, pic, slice,
512 "header: %d.\n", err);
516 ctx->codec->slice_header_type,
522 if (
ctx->codec->init_slice_params) {
524 VAEncSliceParameterBufferType,
526 ctx->codec->slice_params_size);
532 #if VA_CHECK_VERSION(1, 0, 0)
535 if (sd &&
ctx->roi_allowed) {
538 VAEncMiscParameterBufferROI param_roi;
543 av_assert0(roi_size && sd->size % roi_size == 0);
544 nb_roi = sd->size / roi_size;
545 if (nb_roi >
ctx->roi_max_regions) {
546 if (!
ctx->roi_warned) {
548 "supported by driver (%d > %d).\n",
549 nb_roi,
ctx->roi_max_regions);
552 nb_roi =
ctx->roi_max_regions;
561 for (
i = 0;
i < nb_roi;
i++) {
569 pic->
roi[
i] = (VAEncROI) {
580 param_roi = (VAEncMiscParameterBufferROI) {
582 .max_delta_qp = INT8_MAX,
583 .min_delta_qp = INT8_MIN,
585 .roi_flags.bits.roi_value_is_qp_delta = 1,
589 VAEncMiscParameterTypeROI,
597 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
599 if (vas != VA_STATUS_SUCCESS) {
601 "%d (%s).\n", vas, vaErrorStr(vas));
603 goto fail_with_picture;
606 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
608 if (vas != VA_STATUS_SUCCESS) {
610 "%d (%s).\n", vas, vaErrorStr(vas));
612 goto fail_with_picture;
615 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
616 if (vas != VA_STATUS_SUCCESS) {
618 "%d (%s).\n", vas, vaErrorStr(vas));
622 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
629 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
632 vas = vaDestroyBuffer(
ctx->hwctx->display,
634 if (vas != VA_STATUS_SUCCESS) {
636 "param buffer %#x: %d (%s).\n",
648 vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
692 if (
ctx->output_delay == 0) {
701 (3 *
ctx->output_delay +
ctx->async_depth)];
710 VACodedBufferSegment *buf_list, *buf;
715 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
717 if (vas != VA_STATUS_SUCCESS) {
719 "%d (%s).\n", vas, vaErrorStr(vas));
724 for (buf = buf_list; buf; buf = buf->next)
727 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
728 if (vas != VA_STATUS_SUCCESS) {
730 "%d (%s).\n", vas, vaErrorStr(vas));
739 VABufferID buf_id, uint8_t **dst)
742 VACodedBufferSegment *buf_list, *buf;
746 vas = vaMapBuffer(
ctx->hwctx->display, buf_id,
748 if (vas != VA_STATUS_SUCCESS) {
750 "%d (%s).\n", vas, vaErrorStr(vas));
755 for (buf = buf_list; buf; buf = buf->next) {
757 "(status %08x).\n", buf->size, buf->status);
759 memcpy(*dst, buf->buf, buf->size);
763 vas = vaUnmapBuffer(
ctx->hwctx->display, buf_id);
764 if (vas != VA_STATUS_SUCCESS) {
766 "%d (%s).\n", vas, vaErrorStr(vas));
778 VABufferID output_buffer_prev;
783 if (
ctx->coded_buffer_ref) {
784 output_buffer_prev = *
ctx->coded_buffer_ref;
801 if (
ctx->coded_buffer_ref) {
835 if (
ctx->tail_pkt->size) {
845 pkt_ptr =
ctx->tail_pkt;
871 "%"PRId64
"/%"PRId64
".\n",
890 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;
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;
1811 if (supported_va_rc_modes &
rc_mode->va_mode) {
1827 supported_rc_modes_string);
1842 #define TRY_RC_MODE(mode, fail) do { \
1843 rc_mode = &vaapi_encode_rc_modes[mode]; \
1844 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1846 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1847 "RC mode (supported modes: %s).\n", rc_mode->name, \
1848 supported_rc_modes_string); \
1849 return AVERROR(EINVAL); \
1851 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1852 "RC mode.\n", rc_mode->name); \
1855 goto rc_mode_found; \
1859 if (
ctx->explicit_rc_mode)
1862 if (
ctx->explicit_qp)
1892 "RC mode compatible with selected options "
1893 "(supported modes: %s).\n", supported_rc_modes_string);
1910 rc_bits_per_second = avctx->
bit_rate;
1916 rc_target_percentage = 100;
1923 }
else if (
rc_mode->maxrate) {
1927 "bitrate (%"PRId64
") must not be greater than "
1928 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1933 rc_target_percentage = (avctx->
bit_rate * 100) /
1940 rc_bits_per_second = 2 * avctx->
bit_rate;
1941 rc_target_percentage = 50;
1946 "in %s RC mode.\n",
rc_mode->name);
1948 rc_bits_per_second = avctx->
bit_rate;
1949 rc_target_percentage = 100;
1952 rc_bits_per_second = 0;
1953 rc_target_percentage = 100;
1957 if (
ctx->explicit_qp) {
1958 rc_quality =
ctx->explicit_qp;
1962 rc_quality =
ctx->codec->default_quality;
1964 "using default (%d).\n", rc_quality);
1980 "must have initial buffer size (%d) <= "
1981 "buffer size (%"PRId64
").\n",
1987 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1990 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1994 "in %s RC mode.\n",
rc_mode->name);
1997 hrd_buffer_size = 0;
1998 hrd_initial_buffer_fullness = 0;
2002 rc_window_size = 1000;
2006 if (rc_bits_per_second > UINT32_MAX ||
2007 hrd_buffer_size > UINT32_MAX ||
2008 hrd_initial_buffer_fullness > UINT32_MAX) {
2010 "greater are not supported by VAAPI.\n");
2015 ctx->rc_quality = rc_quality;
2017 ctx->va_bit_rate = rc_bits_per_second;
2020 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2023 ctx->config_attributes[
ctx->nb_config_attributes++] =
2025 .type = VAConfigAttribRateControl,
2026 .value =
ctx->va_rc_mode,
2033 if (
rc_mode->va_mode != VA_RC_CQP) {
2036 "converging in %d frames with %d%% accuracy.\n",
2037 rc_bits_per_second, rc_window_size,
2038 rc_target_percentage);
2039 }
else if (
rc_mode->bitrate) {
2041 "%"PRId64
" bps over %d ms.\n", rc_target_percentage,
2042 rc_bits_per_second, rc_window_size);
2045 ctx->rc_params = (VAEncMiscParameterRateControl) {
2046 .bits_per_second = rc_bits_per_second,
2047 .target_percentage = rc_target_percentage,
2048 .window_size = rc_window_size,
2050 .min_qp = (avctx->
qmin > 0 ? avctx->
qmin : 0),
2051 .basic_unit_size = 0,
2052 #
if VA_CHECK_VERSION(1, 1, 0)
2053 .ICQ_quality_factor =
av_clip(rc_quality, 1, 51),
2054 .max_qp = (avctx->
qmax > 0 ? avctx->
qmax : 0),
2056 #
if VA_CHECK_VERSION(1, 3, 0)
2057 .quality_factor = rc_quality,
2061 VAEncMiscParameterTypeRateControl,
2063 sizeof(
ctx->rc_params));
2068 "initial fullness %"PRId64
" bits.\n",
2069 hrd_buffer_size, hrd_initial_buffer_fullness);
2071 ctx->hrd_params = (VAEncMiscParameterHRD) {
2072 .initial_buffer_fullness = hrd_initial_buffer_fullness,
2073 .buffer_size = hrd_buffer_size,
2076 VAEncMiscParameterTypeHRD,
2078 sizeof(
ctx->hrd_params));
2089 fr_num, fr_den, (
double)fr_num / fr_den);
2091 ctx->fr_params = (VAEncMiscParameterFrameRate) {
2092 .framerate = (
unsigned int)fr_den << 16 | fr_num,
2094 #if VA_CHECK_VERSION(0, 40, 0)
2096 VAEncMiscParameterTypeFrameRate,
2098 sizeof(
ctx->fr_params));
2106 #if VA_CHECK_VERSION(1, 5, 0)
2108 VAConfigAttrib attr = { VAConfigAttribMaxFrameSize };
2111 if (
ctx->va_rc_mode == VA_RC_CQP) {
2112 ctx->max_frame_size = 0;
2118 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2122 if (vas != VA_STATUS_SUCCESS) {
2123 ctx->max_frame_size = 0;
2125 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2129 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2130 ctx->max_frame_size = 0;
2132 "is not supported.\n");
2135 VAConfigAttribValMaxFrameSize attr_mfs;
2136 attr_mfs.value = attr.value;
2138 if (!attr_mfs.bits.max_frame_size && attr_mfs.bits.multiple_pass) {
2139 ctx->max_frame_size = 0;
2141 "max frame size which has not been implemented in FFmpeg.\n");
2145 ctx->mfs_params = (VAEncMiscParameterBufferMaxFrameSize){
2146 .max_frame_size =
ctx->max_frame_size * 8,
2150 ctx->max_frame_size);
2154 "this VAAPI version.\n");
2165 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
2166 uint32_t ref_l0, ref_l1;
2167 int prediction_pre_only;
2169 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2173 if (vas != VA_STATUS_SUCCESS) {
2175 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2179 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2180 ref_l0 = ref_l1 = 0;
2182 ref_l0 = attr.value & 0xffff;
2183 ref_l1 = attr.value >> 16 & 0xffff;
2187 prediction_pre_only = 0;
2189 #if VA_CHECK_VERSION(1, 9, 0)
2192 attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection };
2193 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2197 if (vas != VA_STATUS_SUCCESS) {
2199 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2201 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2203 "prediction constraints.\n");
2205 if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) ||
2206 ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) {
2208 "direction attribute.\n");
2212 if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) {
2213 if (ref_l0 > 0 && ref_l1 > 0) {
2214 prediction_pre_only = 1;
2216 "lists for B-frames.\n");
2220 if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) {
2221 if (ref_l0 > 0 && ref_l1 > 0) {
2224 "replacing them with B-frames.\n");
2235 }
else if (ref_l0 < 1) {
2237 "reference frames.\n");
2240 ref_l1 < 1 || avctx->max_b_frames < 1 ||
2241 prediction_pre_only) {
2244 "(supported references: %d / %d).\n",
2248 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2250 ctx->p_per_i = INT_MAX;
2255 "(supported references: %d / %d).\n",
2259 "(supported references: %d / %d).\n", ref_l0, ref_l1);
2261 ctx->p_per_i = INT_MAX;
2267 ctx->max_b_depth = 1;
2273 ctx->gop_per_idr =
ctx->idr_interval + 1;
2275 ctx->closed_gop = 1;
2276 ctx->gop_per_idr = 1;
2283 uint32_t slice_structure)
2293 if (avctx->
slices >
ctx->slice_block_rows) {
2295 "configured number of slices (%d < %d); using "
2296 "maximum.\n",
ctx->slice_block_rows, avctx->
slices);
2297 req_slices =
ctx->slice_block_rows;
2299 req_slices = avctx->
slices;
2301 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
2302 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
2303 ctx->nb_slices = req_slices;
2304 ctx->slice_size =
ctx->slice_block_rows /
ctx->nb_slices;
2305 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
2307 for (k = 1;; k *= 2) {
2308 if (2 * k * (req_slices - 1) + 1 >=
ctx->slice_block_rows)
2311 ctx->nb_slices = (
ctx->slice_block_rows + k - 1) / k;
2312 ctx->slice_size = k;
2313 #if VA_CHECK_VERSION(1, 0, 0)
2314 }
else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
2315 ctx->nb_slices =
ctx->slice_block_rows;
2316 ctx->slice_size = 1;
2320 "slice structure modes (%#x).\n", slice_structure);
2328 uint32_t slice_structure)
2333 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
2334 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
2335 ctx->tile_cols == 1))) {
2337 "current tile requirement.\n", slice_structure);
2341 if (
ctx->tile_rows >
ctx->slice_block_rows ||
2342 ctx->tile_cols >
ctx->slice_block_cols) {
2344 "for configured number of tile (%d x %d); ",
2345 ctx->slice_block_rows,
ctx->slice_block_cols,
2346 ctx->tile_rows,
ctx->tile_cols);
2347 ctx->tile_rows =
ctx->tile_rows >
ctx->slice_block_rows ?
2348 ctx->slice_block_rows :
ctx->tile_rows;
2349 ctx->tile_cols =
ctx->tile_cols >
ctx->slice_block_cols ?
2350 ctx->slice_block_cols :
ctx->tile_cols;
2352 ctx->tile_rows,
ctx->tile_cols);
2355 req_tiles =
ctx->tile_rows *
ctx->tile_cols;
2360 if (avctx->
slices != req_tiles)
2362 "mismatches with configured number of tile (%d != %d); "
2363 "using requested tile number for slice.\n",
2364 avctx->
slices, req_tiles);
2366 ctx->nb_slices = req_tiles;
2370 for (
i = 0;
i <
ctx->tile_cols;
i++) {
2371 ctx->col_width[
i] = (
i + 1 ) *
ctx->slice_block_cols /
ctx->tile_cols -
2372 i *
ctx->slice_block_cols /
ctx->tile_cols;
2373 ctx->col_bd[
i + 1] =
ctx->col_bd[
i] +
ctx->col_width[
i];
2376 for (
i = 0;
i <
ctx->tile_rows;
i++) {
2377 ctx->row_height[
i] = (
i + 1 ) *
ctx->slice_block_rows /
ctx->tile_rows -
2378 i *
ctx->slice_block_rows /
ctx->tile_rows;
2379 ctx->row_bd[
i + 1] =
ctx->row_bd[
i] +
ctx->row_height[
i];
2383 ctx->tile_rows,
ctx->tile_cols);
2391 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
2392 { VAConfigAttribEncSliceStructure },
2393 #if VA_CHECK_VERSION(1, 1, 0)
2394 { VAConfigAttribEncTileSupport },
2398 uint32_t max_slices, slice_structure;
2404 "but this codec does not support controlling slices.\n");
2411 ctx->slice_block_rows = (avctx->
height +
ctx->slice_block_height - 1) /
2412 ctx->slice_block_height;
2413 ctx->slice_block_cols = (avctx->
width +
ctx->slice_block_width - 1) /
2414 ctx->slice_block_width;
2416 if (avctx->
slices <= 1 && !
ctx->tile_rows && !
ctx->tile_cols) {
2418 ctx->slice_size =
ctx->slice_block_rows;
2422 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2426 if (vas != VA_STATUS_SUCCESS) {
2428 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2431 max_slices = attr[0].value;
2432 slice_structure = attr[1].value;
2433 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2434 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2436 "pictures as multiple slices.\n.");
2440 if (
ctx->tile_rows &&
ctx->tile_cols) {
2441 #if VA_CHECK_VERSION(1, 1, 0)
2442 uint32_t tile_support = attr[2].value;
2443 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2445 "pictures as multiple tiles.\n.");
2450 "not supported with this VAAPI version.\n");
2455 if (
ctx->tile_rows &&
ctx->tile_cols)
2464 "%d (from %d) due to driver constraints on slice "
2465 "structure.\n",
ctx->nb_slices, avctx->
slices);
2467 if (
ctx->nb_slices > max_slices) {
2469 "encoding with %d slices (max %"PRIu32
").\n",
2470 ctx->nb_slices, max_slices);
2483 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2485 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2489 if (vas != VA_STATUS_SUCCESS) {
2491 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2495 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2496 if (
ctx->desired_packed_headers) {
2498 "packed headers (wanted %#x).\n",
2499 ctx->desired_packed_headers);
2502 "packed headers (none wanted).\n");
2504 ctx->va_packed_headers = 0;
2506 if (
ctx->desired_packed_headers & ~attr.value) {
2508 "wanted packed headers (wanted %#x, found %#x).\n",
2509 ctx->desired_packed_headers, attr.value);
2512 "available (wanted %#x, found %#x).\n",
2513 ctx->desired_packed_headers, attr.value);
2515 ctx->va_packed_headers =
ctx->desired_packed_headers & attr.value;
2518 if (
ctx->va_packed_headers) {
2519 ctx->config_attributes[
ctx->nb_config_attributes++] =
2521 .type = VAConfigAttribEncPackedHeaders,
2522 .value =
ctx->va_packed_headers,
2526 if ( (
ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2527 !(
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2530 "sequence headers, but a global header is requested.\n");
2532 "this may result in a stream which is not usable for some "
2533 "purposes (e.g. not muxable to some containers).\n");
2541 #if VA_CHECK_VERSION(0, 36, 0)
2544 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2547 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2551 if (vas != VA_STATUS_SUCCESS) {
2553 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2557 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2560 "supported: will use default quality level.\n");
2565 "valid range is 0-%d, using %d.\n",
2566 attr.value, attr.value);
2570 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2574 VAEncMiscParameterTypeQualityLevel,
2575 &
ctx->quality_params,
2576 sizeof(
ctx->quality_params));
2580 "not supported with this VAAPI version.\n");
2588 #if VA_CHECK_VERSION(1, 0, 0)
2591 VAConfigAttrib attr = { VAConfigAttribEncROI };
2593 vas = vaGetConfigAttributes(
ctx->hwctx->display,
2597 if (vas != VA_STATUS_SUCCESS) {
2599 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2603 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2604 ctx->roi_allowed = 0;
2606 VAConfigAttribValEncROI roi = {
2607 .value = attr.value,
2610 ctx->roi_max_regions = roi.bits.num_roi_regions;
2611 ctx->roi_allowed =
ctx->roi_max_regions > 0 &&
2612 (
ctx->va_rc_mode == VA_RC_CQP ||
2613 roi.bits.roi_rc_qp_delta_support);
2624 VABufferID *buffer_id_ref = obj;
2625 VABufferID buffer_id = *buffer_id_ref;
2627 vaDestroyBuffer(
ctx->hwctx->display, buffer_id);
2636 VABufferID *buffer_id = obj;
2643 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
2644 VAEncCodedBufferType,
2645 3 *
ctx->surface_width *
ctx->surface_height +
2646 (1 << 16), 1, 0, buffer_id);
2647 if (vas != VA_STATUS_SUCCESS) {
2649 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2686 if (
ctx->input_frames->sw_format ==
2688 recon_format =
ctx->input_frames->sw_format;
2699 recon_format =
ctx->input_frames->sw_format;
2709 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2710 ctx->surface_width,
ctx->surface_height,
2721 if (!
ctx->recon_frames_ref) {
2728 ctx->recon_frames->sw_format = recon_format;
2729 ctx->recon_frames->width =
ctx->surface_width;
2730 ctx->recon_frames->height =
ctx->surface_height;
2735 "frame context: %d.\n", err);
2753 ctx->va_config = VA_INVALID_ID;
2754 ctx->va_context = VA_INVALID_ID;
2765 "required to associate the encoding device.\n");
2770 if (!
ctx->input_frames_ref) {
2777 if (!
ctx->device_ref) {
2782 ctx->hwctx =
ctx->device->hwctx;
2785 if (!
ctx->tail_pkt) {
2794 if (
ctx->codec->get_encoder_caps) {
2795 err =
ctx->codec->get_encoder_caps(avctx);
2803 ctx->slice_block_width = 16;
2804 ctx->slice_block_height = 16;
2834 if (
ctx->max_frame_size) {
2840 vas = vaCreateConfig(
ctx->hwctx->display,
2841 ctx->va_profile,
ctx->va_entrypoint,
2842 ctx->config_attributes,
ctx->nb_config_attributes,
2844 if (vas != VA_STATUS_SUCCESS) {
2846 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2855 recon_hwctx =
ctx->recon_frames->hwctx;
2856 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
2857 ctx->surface_width,
ctx->surface_height,
2862 if (vas != VA_STATUS_SUCCESS) {
2864 "context: %d (%s).\n", vas, vaErrorStr(vas));
2869 ctx->output_buffer_pool =
2873 if (!
ctx->output_buffer_pool) {
2878 if (
ctx->codec->configure) {
2879 err =
ctx->codec->configure(avctx);
2884 ctx->output_delay =
ctx->b_per_p;
2885 ctx->decode_delay =
ctx->max_b_depth;
2887 if (
ctx->codec->sequence_params_size > 0) {
2888 ctx->codec_sequence_params =
2890 if (!
ctx->codec_sequence_params) {
2895 if (
ctx->codec->picture_params_size > 0) {
2896 ctx->codec_picture_params =
2898 if (!
ctx->codec_picture_params) {
2904 if (
ctx->codec->init_sequence_params) {
2905 err =
ctx->codec->init_sequence_params(avctx);
2908 "failed: %d.\n", err);
2913 if (
ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2914 ctx->codec->write_sequence_header &&
2917 size_t bit_len = 8 *
sizeof(
data);
2919 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
2922 "for extradata: %d.\n", err);
2936 #if VA_CHECK_VERSION(1, 9, 0)
2938 vas = vaSyncBuffer(
ctx->hwctx->display, VA_INVALID_ID, 0);
2939 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
2940 ctx->has_sync_buffer_func = 1;
2944 if (!
ctx->encode_fifo)
2965 for (pic =
ctx->pic_start; pic; pic = next) {
2972 if (
ctx->va_context != VA_INVALID_ID) {
2973 vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
2974 ctx->va_context = VA_INVALID_ID;
2977 if (
ctx->va_config != VA_INVALID_ID) {
2978 vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
2979 ctx->va_config = VA_INVALID_ID;