31 #include <arpa/inet.h>
79 static const int32_t LT_GOP_PRESET_I_1[6] = {
SLICE_TYPE_I, 1, 0, 0, 0, 0};
80 static const int32_t LT_GOP_PRESET_P_1[6] = {
SLICE_TYPE_MP, 1, 1, 0, 0, -1};
81 static const int32_t LT_GOP_PRESET_B_1[6] = {
SLICE_TYPE_B, 1, 1, 0, 0, -1};
83 static const int32_t LT_GOP_PRESET_BP_2[12] = {
84 SLICE_TYPE_MP, 2, 1, 0, 0, -2,
SLICE_TYPE_B, 1, 3, 0, 0, 2,
87 static const int32_t LT_GOP_PRESET_BBBP_4[24] = {
88 SLICE_TYPE_MP, 4, 1, 0, 0, -4,
SLICE_TYPE_B, 2, 3, 0, 0, 4,
89 SLICE_TYPE_B, 1, 5, 0, 0, 2,
SLICE_TYPE_B, 3, 5, 0, 2, 4,
92 static const int32_t LT_GOP_PRESET_LP_4[24] = {
93 SLICE_TYPE_MP, 1, 5, 0, 0, -4,
SLICE_TYPE_MP, 2, 3, 0, 1, 0,
94 SLICE_TYPE_MP, 3, 5, 0, 2, 0,
SLICE_TYPE_MP, 4, 1, 0, 3, 0,
96 static const int32_t LT_GOP_PRESET_LD_4[24] = {
97 SLICE_TYPE_B, 1, 5, 0, 0, -4,
SLICE_TYPE_B, 2, 3, 0, 1, 0,
98 SLICE_TYPE_B, 3, 5, 0, 2, 0,
SLICE_TYPE_B, 4, 1, 0, 3, 0,
102 static const int32_t LT_GOP_PRESET_RA_8[48] = {
103 SLICE_TYPE_B, 8, 1, 0, 0, -8,
SLICE_TYPE_B, 4, 3, 0, 0, 8,
104 SLICE_TYPE_B, 2, 5, 0, 0, 4,
SLICE_TYPE_B, 1, 8, 0, 0, 2,
105 SLICE_TYPE_B, 3, 8, 0, 2, 4,
SLICE_TYPE_B, 6, 5, 0, 4, 8,
106 SLICE_TYPE_B, 5, 8, 0, 4, 6,
SLICE_TYPE_B, 7, 8, 0, 6, 8,
109 static const int32_t LT_GOP_PRESET_SP_1[6] = {
SLICE_TYPE_P, 1, 1, 0, 0, -1};
111 static const int32_t LT_GOP_PRESET_BSP_2[12] = {
112 SLICE_TYPE_P, 2, 1, 0, 0, -2,
SLICE_TYPE_B, 1, 3, 0, 0, 2,
114 static const int32_t LT_GOP_PRESET_BBBSP_4[24] = {
115 SLICE_TYPE_P, 4, 1, 0, 0, -4,
SLICE_TYPE_B, 2, 3, 0, 0, 4,
116 SLICE_TYPE_B, 1, 5, 0, 0, 2,
SLICE_TYPE_B, 3, 5, 0, 2, 4,
118 static const int32_t LT_GOP_PRESET_LSP_4[24] = {
119 SLICE_TYPE_P, 1, 5, 0, 0, -4,
SLICE_TYPE_P, 2, 3, 0, 1, 0,
120 SLICE_TYPE_P, 3, 5, 0, 2, 0,
SLICE_TYPE_P, 4, 1, 0, 3, 0,
123 static const int32_t LT_GOP_PRESET_BBP_3[18] = {
124 SLICE_TYPE_MP, 3, 1, 0, 0, -3,
SLICE_TYPE_B, 1, 3, 0, 0, 3,
128 static const int32_t LT_GOP_PRESET_BBSP_3[18] = {
129 SLICE_TYPE_P, 3, 1, 0, 0, 0,
SLICE_TYPE_B, 1, 3, 0, 0, 3,
133 static const int32_t LT_GOP_PRESET_BBBBBBBP_8[48] = {
134 SLICE_TYPE_MP, 8, 1, 0, 0, -8,
SLICE_TYPE_B, 4, 3, 0, 0, 8,
135 SLICE_TYPE_B, 2, 5, 0, 0, 4,
SLICE_TYPE_B, 1, 8, 0, 0, 2,
136 SLICE_TYPE_B, 3, 8, 0, 2, 4,
SLICE_TYPE_B, 6, 5, 0, 4, 8,
137 SLICE_TYPE_B, 5, 8, 0, 4, 6,
SLICE_TYPE_B, 7, 8, 0, 6, 8,
140 static const int32_t LT_GOP_PRESET_BBBBBBBSP_8[48] = {
141 SLICE_TYPE_P, 8, 1, 0, 0, 0,
SLICE_TYPE_B, 4, 3, 0, 0, 8,
142 SLICE_TYPE_B, 2, 5, 0, 0, 4,
SLICE_TYPE_B, 1, 8, 0, 0, 2,
143 SLICE_TYPE_B, 3, 8, 0, 2, 4,
SLICE_TYPE_B, 6, 5, 0, 4, 8,
144 SLICE_TYPE_B, 5, 8, 0, 4, 6,
SLICE_TYPE_B, 7, 8, 0, 6, 8,
153 LT_GOP_PRESET_BBBP_4,
160 LT_GOP_PRESET_BBBSP_4,
164 LT_GOP_PRESET_BBSP_3,
165 LT_GOP_PRESET_BBBBBBBP_8,
166 LT_GOP_PRESET_BBBBBBBSP_8,
172 #define SAMPLE_SPS_MAX_SUB_LAYERS_MINUS1 0
173 #define MAX_VPS_MAX_SUB_LAYERS 16
174 #define MAX_CPB_COUNT 16
175 #define MAX_DURATION 0.5
239 uint8_t *sei_buf = NULL;
241 int start_offset = 0;
250 sei_buf = (uint8_t *)frame->
p_data[start_offset] +
257 "unregisted SEI!\n");
287 "mastering display color SEI!\n");
296 *)((uint8_t *)frame->
p_data[start_offset] +
302 ntohs(pColourVolume->display_primaries[2][0]);
305 ntohs(pColourVolume->display_primaries[2][1]);
308 ntohs(pColourVolume->display_primaries[0][0]);
311 ntohs(pColourVolume->display_primaries[0][1]);
314 ntohs(pColourVolume->display_primaries[1][0]);
317 ntohs(pColourVolume->display_primaries[1][1]);
325 ntohl(pColourVolume->min_display_mastering_luminance);
328 ntohl(pColourVolume->max_display_mastering_luminance);
346 "content light level SEI !\n");
353 *)((uint8_t *)frame->
p_data[start_offset] +
356 clm->
max_cll = ntohs(pLightLevel->max_content_light_level);
357 clm->
max_fall = ntohs(pLightLevel->max_pic_average_light_level);
373 int w, i, j, i_limit, j_limit;
378 sei_buf = (uint8_t *)frame->
p_data[start_offset] +
437 "hdr10+ targeted_system_display_maximum_luminance "
441 "hdr10+ targeted_system_display_actual_peak_lumi"
456 "hdr10+ num_rows_targeted_system_display_actual"
458 "num_cols_targeted_system_display_actual_"
459 "peak_luminance %d x %d\n",
462 i_limit = i_limit > 25 ? 25 : i_limit;
463 j_limit = j_limit > 25 ? 25 : j_limit;
464 for (i = 0; i < i_limit; i++)
465 for (j = 0; j < j_limit; j++)
474 "hdr10+ targeted_system_display_actual_peak"
475 "_luminance[%d][%d] %d\n",
485 for (i = 0; i < 3; i++)
503 "hdr10+ num_distribution_maxrgb_percentiles[%d] %d\n",
506 i_limit = i_limit > 15 ? 15 : i_limit;
507 for (i = 0; i < i_limit; i++)
516 "hdr10+ distribution_maxrgb_percentage[%d][%d] "
521 "hdr10+ distribution_maxrgb_percentile[%d][%d] "
539 "hdr10+ mastering_display_actual_peak_luminance_flag %u\n",
550 "hdr10+ num_rows_mastering_display_actual_peak_"
552 "num_cols_mastering_display_actual_peak_luminance "
556 i_limit = i_limit > 25 ? 25 : i_limit;
557 j_limit = j_limit > 25 ? 25 : j_limit;
558 for (i = 0; i < i_limit; i++)
559 for (j = 0; j < j_limit; j++)
566 "hdr10+ mastering_display_actual_peak_lumi"
567 "nance[%d][%d] %d\n",
570 ->mastering_display_actual_peak_luminance[i]
599 "hdr10+ num_bezier_curve_anchors[%d] %u\n", w,
608 "hdr10+ bezier_curve_anchors[%d][%d] %d\n", w,
616 "hdr10+ color_saturation_mapping_flag[%d] %u\n", w,
624 "hdr10+ color_saturation_weight[%d] %d\n", w,
653 frame->
color_trc = p_dec_ext_meta->color_trc;
658 "ni_dec_retrieve_aux_data VUI "
659 "aspect_ratio_idc %u "
660 "sar_width %u sar_height %u "
661 "video_full_range_flag %d "
662 "color-pri %u color-trc %u color-space %u "
663 "vui_num_units_in_tick %u "
664 "vui_time_scale %u\n",
675 sei_buf = (uint8_t *)frame->
p_data[start_offset] +
680 "writting VUI color-trc value %u\n",
692 int height,
int intra_qp)
701 uint8_t isAbsQp_flag = 0;
710 width = (width / 8) * 8;
711 height = (height / 8) * 8;
716 uint32_t roiMapBlockUnitSize =
718 uint32_t mbWidth = ((width + max_cu_size - 1) & (~(max_cu_size - 1))) /
720 uint32_t mbHeight = ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
722 uint32_t numMbs = mbWidth * mbHeight;
723 uint32_t subMbWidth = roiMapBlockUnitSize / 8;
724 uint32_t subMbHeight = subMbWidth;
725 uint32_t subNumMbs = subMbWidth * subMbHeight;
729 uint32_t block_size = ((width + max_cu_size - 1) & (~(max_cu_size - 1))) *
730 ((height + max_cu_size - 1) & (~(max_cu_size - 1))) / (8 * 8);
733 uint32_t customMapSize = ((block_size + 63) & (~63));
745 memset(p_enc_ctx->
roi_map, 0, customMapSize);
749 for (r = nb_roi - 1; r >= 0; r--)
758 if (api_params->cfg_enc_params.customize_roi_qp_level) {
763 set_qp = (int32_t)((
float)roi->
qoffset.
num * 1.0f /
766 isAbsQp_flag = api_params->cfg_enc_params.customize_roi_qp_level;
771 set_qp = (int32_t)((
float)roi->
qoffset.
num * 1.0f /
780 "set_roi_map: left %d right %d top %d bottom %d num %d den"
786 for (j = 0; j < mbHeight; j++)
788 for (i = 0; i < mbWidth; i++)
790 k = j * (int)mbWidth + i;
792 for (m = 0; m < subNumMbs; m++)
794 if (((
int)(i % mbWidth) >=
795 (
int)((roi->
left + roiMapBlockUnitSize - 1) /
796 roiMapBlockUnitSize) -
798 ((int)(i % mbWidth) <=
799 (int)((roi->
right + roiMapBlockUnitSize - 1) /
800 roiMapBlockUnitSize) -
802 ((int)(j % mbHeight) >=
803 (int)((roi->
top + roiMapBlockUnitSize - 1) /
804 roiMapBlockUnitSize) -
806 ((int)(j % mbHeight) <=
807 (int)((roi->
bottom + roiMapBlockUnitSize - 1) /
808 roiMapBlockUnitSize) -
813 p_enc_ctx->
roi_map[k * subNumMbs + m]
826 p_enc_ctx->
roi_len = customMapSize;
860 int should_send_sei_with_frame, uint8_t *mdcv_data,
861 uint8_t *cll_data, uint8_t *cc_data,
862 uint8_t *udu_data, uint8_t *hdrp_data)
891 intraprd = *((int32_t *)aux_data->
data);
892 if (intraprd < 0 || intraprd > 1024)
913 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): NOT allowed to reconfig intraPeriod %d in intra_mb_refresh_mode %d or gop_preset_index %d\n",
922 should_send_sei_with_frame = 1;
928 intraprd - (p_enc_ctx->
frame_num % intraprd);
938 ni_log(
NI_LOG_INFO,
"%s(): set intraPeriod %d on frame %u, update intra offset %u\n", __func__,
951 8 + 6 * 2 + 2 * 2 + 2 * 4 + 1;
975 uint32_t uint32_t_tmp = htonl(
979 uint32_t_tmp = htonl(
998 dst[0] = dst[1] = dst[2] = 0;
1025 uint16_t dp00 = 0, dp01 = 0, dp10 = 0, dp11 = 0, dp20 = 0, dp21 = 0,
1030 dp00 = (uint16_t)lrint(chroma_den *
1033 dp01 = (uint16_t)lrint(chroma_den *
1036 dp10 = (uint16_t)lrint(chroma_den *
1039 dp11 = (uint16_t)lrint(chroma_den *
1042 dp20 = (uint16_t)lrint(chroma_den *
1045 dp21 = (uint16_t)lrint(chroma_den *
1049 wpx = (uint16_t)lrint(chroma_den * ni_q2d(p_src->
white_point[0]));
1051 wpy = (uint16_t)lrint(chroma_den * ni_q2d(p_src->
white_point[1]));
1055 "mastering display color volume, primaries "
1056 "%u/%u/%u/%u/%u/%u white_point_x/y %u/%u max/min_lumi %u/%u\n",
1057 (uint16_t)dp00, (uint16_t)dp01, (uint16_t)dp10, (uint16_t)dp11,
1058 (uint16_t)dp20, (uint16_t)dp21, (uint16_t)wpx, (uint16_t)wpy,
1062 dst += 6 * 2 + 2 * 2;
1090 uint16_t max_content_light_level =
1092 uint16_t max_pic_average_light_level =
1102 &max_pic_average_light_level,
sizeof(uint16_t));
1113 should_send_sei_with_frame)
1116 dst[0] = dst[1] = dst[2] = 0;
1153 int cc_size = aux_data->
size;
1160 memcpy(cc_data_emu_prevent, aux_data->
data, cc_size);
1161 int cc_size_emu_prevent = cc_size +
1163 if (cc_size_emu_prevent != cc_size)
1166 "emulation prevention bytes added: %d\n",
1167 cc_size_emu_prevent - cc_size);
1184 memcpy(dst, cc_data_emu_prevent, cc_size_emu_prevent);
1185 dst += cc_size_emu_prevent;
1199 memcpy(dst, cc_data_emu_prevent, cc_size_emu_prevent);
1200 dst += cc_size_emu_prevent;
1257 ui_tmp = lrint(10000 *
1266 "hdr10+ targeted_system_display_actual_peak_luminance_"
1281 "hdr10+ num_rows_targeted_system_display_actual_peak_luminance "
1282 "x num_cols_targeted_system_display_actual_peak_luminance %u x "
1302 "luminance[%d][%d] %u\n",
1309 for (i = 0; i < 3; i++)
1311 ui_tmp = lrint(100000 * ni_q2d(hdrp->
params[w].
maxscl[i]));
1322 "hdr10+ num_distribution_maxrgb_percentiles[%d] %d\n", w,
1335 "hdr10+ distribution_maxrgb_percentage[%d][%d] %u\n", w, i,
1338 "hdr10+ distribution_maxrgb_percentile[%d][%d] %u\n", w, i,
1351 "hdr10+ mastering_display_actual_peak_luminance_flag %u\n",
1360 "hdr10+ num_rows_mastering_display_actual_peak_luminance x "
1361 "num_cols_mastering_display_actual_peak_luminance %u x %u\n",
1380 "mastering_display_actual_peak_luminance[%d][%d] %u\n",
1437 int emu_bytes_inserted;
1450 emu_bytes_inserted =
1452 dst += hdr10p_num_bytes + emu_bytes_inserted;
1457 (uint32_t)hdr10p_num_bytes + emu_bytes_inserted +
1470 emu_bytes_inserted =
1472 dst += hdr10p_num_bytes + emu_bytes_inserted;
1477 (uint8_t)hdr10p_num_bytes + emu_bytes_inserted +
1483 "ni_enc_prep_aux_data: codec %d not supported for HDR10+ "
1501 int udu_sei_size = aux_data->
size;
1502 int ext_udu_sei_size, sei_len;
1504 uint8_t *sei_data = malloc(udu_sei_size * 3 / 2);
1507 memcpy(sei_data, (uint8_t *)aux_data->
data, udu_sei_size);
1508 int emu_bytes_inserted =
1511 ext_udu_sei_size = udu_sei_size + emu_bytes_inserted;
1518 6 + ((udu_sei_size + 0xFE) / 0xFF) + ext_udu_sei_size + 1;
1524 7 + ((udu_sei_size + 0xFE) / 0xFF) + ext_udu_sei_size + 1;
1531 "ni_enc_prep_aux_data sei total length %u + sei_len %d "
1532 "exceeds maximum sei size %u, discarding it !\n",
1537 int payload_size = udu_sei_size;
1556 while (payload_size > 0)
1559 (payload_size > 0xFF ? 0xFF : (uint8_t)payload_size);
1560 payload_size -= 0xFF;
1564 memcpy(dst, sei_data, ext_udu_sei_size);
1565 dst += ext_udu_sei_size;
1586 int is_new_rois = 1;
1588 uint32_t self_size = 0;
1592 if (!self_size || aux_data->
size % self_size)
1595 "aux_data size %d self_size %u\n",
1596 aux_data->
size, self_size);
1599 int nb_roi = aux_data->
size / (int)self_size;
1627 if (set_roi_map(p_enc_ctx, codec_format, aux_data, nb_roi,
1651 if (aux_data && p_enc_ctx->
roi_map)
1669 "aux_data %d ctx->roi_map %d frame->roi_len %u ctx->roi_len %u\n",
1670 api_params->
cacheRoi, aux_data != NULL,
1683 "%s(): frame aux data LTR use_cur_src_as_ltr %u "
1696 "%s(): frame API set LTR use_cur_src_as_ltr %u "
1717 "%s(): frame aux data qp info max/min I qp <%d %d> maxDeltaQp <%d> max/min PB qp <%d %d>",
1734 int32_t bitrate = -1;
1738 bitrate = *((int32_t *)aux_data->
data);
1760 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s: bitrate %d updated for reconfig\n", __func__, bitrate);
1783 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): invalid colorDescPresent in aux data %d\n",
1819 vui = p_enc_ctx->
vui;
1828 "colorDescPresent %d colorPrimaries %d "
1829 "colorTrc %d colorSpace %d aspectRatioWidth %d "
1830 "aspectRatioHeight %d videoFullRange %d\n",
1854 int32_t ltr_interval = -1;
1859 ltr_interval = *((int32_t *)aux_data->
data);
1872 if (ltr_interval > 0)
1890 if ((framerate_num <= 0) || (framerate_denom <= 0))
1893 "ERROR: %s(): invalid framerate in aux data (%d/%d)\n",
1894 __func__, framerate_num, framerate_denom);
1897 if ((framerate_num % framerate_denom) != 0)
1899 uint32_t numUnitsInTick = 1000;
1900 framerate_num = framerate_num / framerate_denom;
1901 framerate_denom = numUnitsInTick + 1;
1903 framerate_num *= numUnitsInTick;
1906 framerate_num = framerate_num / framerate_denom;
1907 framerate_denom = 1;
1909 if (((framerate_num + framerate_denom - 1) / framerate_denom) >
1913 "ERROR: %s(): invalid framerate in aux data (%d/%d)\n",
1921 __func__, framerate_num, framerate_denom);
1948 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s: framerate num %d denom %d updated for reconfig\n",
1954 int32_t frame_num = -1;
1959 frame_num = *((int32_t *)aux_data->
data);
1961 __func__, frame_num);
1983 should_send_sei_with_frame)
2002 int32_t max_frame_size = 0;
2006 max_frame_size = *((int32_t *)aux_data->
data);
2007 uint32_t maxFrameSize = (uint32_t)max_frame_size / 2000;
2008 uint32_t min_maxFrameSize;
2014 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): max_frame_size %d is valid only when lowDelay mode is enabled\n",
2015 __func__, max_frame_size);
2020 int32_t tmp_bitrate, tmp_framerate_num, tmp_framerate_denom;
2030 tmp_framerate_num = (int32_t)api_params->
fps_number;
2034 min_maxFrameSize = (((uint32_t)tmp_bitrate / tmp_framerate_num * tmp_framerate_denom) / 8) / 2000;
2036 if (maxFrameSize < min_maxFrameSize)
2038 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): max_frame_size %d is too small (invalid)\n",
2039 __func__, max_frame_size);
2053 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set max_frame_size %d\n", __func__, max_frame_size);
2056 if (max_frame_size > 0)
2071 crf = (float)(*((int32_t *)aux_data->
data));
2077 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): reconfigure crf value %d is valid only in CRF mode\n",
2083 if (crf < 0 || crf > 51)
2085 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): crf value %d is invalid (valid range in [0..51])\n",
2094 crf = *((
float *)aux_data->
data);
2100 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): reconfigure crf value %f is valid only in CRF mode\n",
2106 if (crf < 0 || crf > 51)
2108 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): crf value %f is invalid (valid range in [0..51])\n",
2135 int32_t vbvMaxRate = 0;
2139 vbvMaxRate = *((int32_t *)aux_data->
data);
2148 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_vbv_max_rate %d\n", __func__, vbvMaxRate);
2160 int32_t vbvBufferSize = 0;
2164 vbvBufferSize = *((int32_t *)aux_data->
data);
2165 if ((vbvBufferSize < 10 && vbvBufferSize != 0) || vbvBufferSize > 3000)
2167 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"%s(): invalid frame aux data vbvBufferSize %d\n", __func__,
2182 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_vbv_max_rate %d\n", __func__, vbvBufferSize);
2194 int16_t sliceArg = 0;
2198 sliceArg = *((int16_t *)aux_data->
data);
2202 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"%s():not support to reconfig slice_arg when slice_mode disable.\n",
2213 int max_num_ctu_mb_row = (api_params->
source_height + ctu_mb_size - 1) / ctu_mb_size;
2214 if (sliceArg < 1 || sliceArg > max_num_ctu_mb_row)
2231 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_slice_arg %d\n", __func__, sliceArg);
2237 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s(): slice arg > NI_MAX_SLICE_SIZE, change from %d to %d\n",
2269 const uint8_t *mdcv_data,
const uint8_t *cll_data,
2270 const uint8_t *cc_data,
const uint8_t *udu_data,
2271 const uint8_t *hdrp_data,
int is_hwframe,
2278 if (is_hwframe != 0 && is_hwframe != 1 && is_semiplanar != 0 &&
2282 "ni_enc_copy_aux_data: error, illegal hwframe or nv12frame\n");
2299 uint8_t *dst = (uint8_t *)p_enc_frame->
p_data[2 + is_hwframe] +
2300 p_enc_frame->
data_len[2 + is_hwframe] + frame_metadata_size;
2304 dst = (uint8_t *)p_enc_frame->
p_data[2 - is_semiplanar] +
2305 p_enc_frame->
data_len[2 - is_semiplanar] +
2306 frame_metadata_size;
2313 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"ni_enc_copy_aux_data: p_metadata_buffer %p frame_metadata_size %u dst %p\n",
2350 memset(dst, 0, p_enc_ctx->
roi_len);
2365 memcpy(dst, mdcv_data,
2376 memcpy(dst, cll_data,
2384 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"ni_enc_copy_aux_data: preferred characteristics size: %u to %p\n",
2387 dst[0] = dst[1] = dst[2] = 0;
2415 memcpy(dst, cc_data, p_enc_frame->
sei_cc_len);
2456 uint8_t *dst = timecode_data;
2458 uint32_t header_len, payload_len, total_len;
2459 int emu_bytes_inserted, is_hwframe, is_semiplanar;
2461 if (!p_enc_ctx || !p_enc_frame || !p_timecode) {
2467 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s insertion of timecode in SEI is only supported on H264 and H265 encoder\n", __func__);
2558 dst[6] = payload_len;
2560 dst[7] = payload_len;
2566 dst += payload_len + emu_bytes_inserted;
2570 total_len = header_len + payload_len + emu_bytes_inserted + 1;
2575 dst = (uint8_t *)p_enc_frame->
p_data[2 + is_hwframe] +
2579 dst = (uint8_t *)p_enc_frame->
p_data[2 - is_semiplanar] +
2590 memcpy(dst, timecode_data, total_len);
2625 ni_frame_t *p_enc_frame, uint8_t *p_yuv_buffer)
2629 bool need_copy =
true;
2632 bool alignment_2pass_wa =
false;
2635 if (!p_ctx || !p_enc_frame)
2652 __func__, p_yuv_buffer);
2670 "%s: resolution change %dx%d -> %dx%d "
2671 "or pixel format change %d -> %d\n",
2704 int should_send_sei_with_frame =
2716 should_send_sei_with_frame, mdcv_data, cll_data,
2717 cc_data, udu_data, hdrp_data);
2736 is_semiplanar, dst_stride, dst_height_aligned);
2747 src_height[0] = frame_height;
2748 p_src[0] = p_yuv_buffer;
2763 src_stride[1] = is_semiplanar ? src_stride[0] : src_stride[0] / 2;
2764 src_stride[2] = is_semiplanar ? 0 : src_stride[0] / 2;
2766 src_height[1] = src_height[0] / 2;
2767 src_height[2] = is_semiplanar ? 0 : src_height[1];
2769 p_src[1] = p_src[0] + src_stride[0] * src_height[0];
2770 p_src[2] = p_src[1] + src_stride[1] * src_height[1];
2773 alignment_2pass_wa = (
2780 if (alignment_2pass_wa && !is_hwframe) {
2781 if (is_semiplanar) {
2785 dst_height_aligned[1] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
2789 dst_height_aligned[2] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
2795 p_param, frame_width, frame_height,
2801 p_enc_frame, frame_width,
2802 frame_height, (
const int *)src_stride, (
const uint8_t **)p_src,
2818 dst_height_aligned[0], dst_stride,
2828 "%s: src_stride %d dst_stride "
2829 "%d dst_height_aligned %d src_height %d need_copy %d\n",
2830 __func__, src_stride[0],
2831 dst_stride[0], dst_height_aligned[0],
2832 src_height[0], need_copy);
2840 dst_height_aligned, src_stride, src_height);
2845 "p_dst alloc linesize = %d/%d/%d src height=%d "
2846 "dst height aligned = %d/%d/%d force_key_frame=%d, "
2848 " sei_size=%u (hdr_content_light_level %u hdr_mastering_display_"
2849 "color_vol %u hdr10+ %u hrd %d) reconf_size=%u roi_size=%u "
2850 "force_pic_qp=%u udu_sei_size=%u "
2851 "use_cur_src_as_long_term_pic %u use_long_term_ref %u\n",
2852 dst_stride[0], dst_stride[1], dst_stride[2], frame_height,
2853 dst_height_aligned[0], dst_height_aligned[1], dst_height_aligned[2],
2865 mdcv_data, cll_data, cc_data, udu_data, hdrp_data,
2875 p_enc_frame->p_buffer = NULL;
2876 p_enc_frame->buffer_size = 0;
2879 p_enc_frame->data_len[i] = 0;
2880 p_enc_frame->p_data[i] = NULL;
2903 for (i = 0; i < 3; i++) {
2904 uint32_t tmp = *state << 8;
2905 *state = tmp + *(p++);
2906 if (tmp == 0x100 || p == end)
2915 else if (p[-3] | (p[-1] - 1))
2923 p = ((p) < (end) ? (p - 4) : (end - 4));
2925 (((uint32_t) (p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3]);
2944 ni_packet_t *p_packet, uint8_t sei_type,
int vcl_found)
2971 ni_log(
NI_LOG_INFO,
"number of custom sei in current frame is out of limit(%d).\n",
2975 sei_data = &p_custom_sei->
data[0];
2985 while (index < pkt_size && pkt_data[index] == 0xff)
2987 sei_size += pkt_data[index++];
2990 if (index >= pkt_size)
2995 sei_size += pkt_data[index++];
3004 udata = &pkt_data[index];
3007 sei_data[0] = sei_type;
3013 for (i = 0, len = 0; (i < pkt_size - index) && len < sei_size; i++, len++)
3017 if (i >= 2 && udata[i - 2] == 0 && udata[i - 1] == 0 && udata[i] == 3)
3022 sei_data[len] = udata[i];
3025 if (len != sei_size)
3028 "required size:%d, actual size:%d.\n", sei_size, len);
3032 p_custom_sei->
type = sei_type;
3033 p_custom_sei->
size = sei_size;
3039 __FUNCTION__, sei_size, sei_type);
3065 uint8_t *data,
int size,
ni_packet_t *p_packet,
int low_delay,
3066 int codec_format,
int pkt_nal_bitmap,
int custom_sei_type,
3067 int *svct_skip_next_packet,
int *is_lone_sei_pkt)
3069 int pkt_sei_alone = 1;
3077 const uint8_t *ptr = data;
3078 const uint8_t *end = data + size;
3081 uint8_t sei_type = 0;
3085 uint32_t temporal_id;
3090 "ni_dec_packet_parse(): already find the header of streams.\n");
3105 if (0 == nalu_count)
3117 nalu_type = (int)stc & 0x1f;
3120 pkt_sei_alone = (pkt_sei_alone && 6 == nalu_type);
3140 "ni_dec_packet_parse(): Detect SPS, PPS and IDR, "
3141 "enable decoder low delay mode.\n");
3152 custom_sei_type == sei_type ||
3162 ptr + 1 - data, p_packet,
3163 sei_type, vcl_found);
3169 }
else if (nalu_type >= 1 &&
3173 }
else if (14 == nalu_type)
3180 if (temporal_id > svct_layer)
3184 *svct_skip_next_packet = 1;
3186 "ni_dec_packet_parse(): temporal_id %d"
3187 " is bigger than decoded layer %d, skip the next "
3189 temporal_id, svct_layer);
3194 nalu_type = (int)(stc >> 1) & 0x3F;
3197 pkt_sei_alone = (pkt_sei_alone &&
3219 if (pkt_nal_bitmap ==
3225 "ni_dec_packet_parse(): Detect VPS, SPS, PPS and "
3226 "IDR enable decoder low delay mode.\n");
3233 if (39 == nalu_type ||
3236 sei_type = *(ptr + 1);
3238 custom_sei_type == sei_type ||
3248 sei_type, vcl_found);
3254 }
else if (nalu_type >= 0 &&
3261 ni_log2(p_session_ctx,
NI_LOG_DEBUG,
"%s() wrong codec %d !\n", __FUNCTION__, codec_format);
3269 *is_lone_sei_pkt = pkt_sei_alone;
3289 int raw_width,
int raw_height,
int ni_fmt,
int nb_planes)
3291 int i, j, h, tenBit = 0;
3292 int vpad[3], hpad[3], src_height[3], src_width[3], src_stride[3];
3293 uint8_t *src_line, *dst_line, *sample, *dest, YUVsample;
3300 src_width[0] =
NIALIGN(raw_width, 2);
3301 src_width[1] =
NIALIGN(raw_width, 2) / 2;
3302 src_width[2] =
NIALIGN(raw_width, 2) / 2;
3305 src_height[0] =
NIALIGN(raw_height, 2);
3306 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3307 src_height[2] =
NIALIGN(raw_height, 2) / 2;
3310 src_stride[0] =
NIALIGN(src_width[0], 128);
3311 src_stride[1] =
NIALIGN(src_width[1], 128);
3312 src_stride[2] =
NIALIGN(src_width[2], 128);
3317 hpad[0] = dst_stride[0] - src_width[0];
3318 hpad[1] = dst_stride[1] - src_width[1];
3319 hpad[2] = dst_stride[2] - src_width[2];
3330 src_width[0] =
NIALIGN(raw_width, 2);
3331 src_width[1] =
NIALIGN(raw_width, 2) / 2;
3332 src_width[2] =
NIALIGN(raw_width, 2) / 2;
3335 src_height[0] =
NIALIGN(raw_height, 2);
3336 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3337 src_height[2] =
NIALIGN(raw_height, 2) / 2;
3340 src_stride[0] =
NIALIGN(src_width[0] * 2, 128);
3341 src_stride[1] =
NIALIGN(src_width[1] * 2, 128);
3342 src_stride[2] =
NIALIGN(src_width[2] * 2, 128);
3347 hpad[0] = dst_stride[0] - src_width[0] * 2;
3348 hpad[1] = dst_stride[1] - src_width[1] * 2;
3349 hpad[2] = dst_stride[2] - src_width[2] * 2;
3360 src_width[0] =
NIALIGN(raw_width, 2);
3361 src_width[1] =
NIALIGN(raw_width, 2);
3365 src_height[0] =
NIALIGN(raw_height, 2);
3366 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3370 src_stride[0] =
NIALIGN(src_width[0], 128);
3371 src_stride[1] =
NIALIGN(src_width[1], 128);
3377 hpad[0] = dst_stride[0] - src_width[0];
3378 hpad[1] = dst_stride[1] - src_width[1];
3390 src_width[0] =
NIALIGN(raw_width, 2);
3391 src_width[1] =
NIALIGN(raw_width, 2);
3395 src_height[0] =
NIALIGN(raw_height, 2);
3396 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3400 src_stride[0] =
NIALIGN(src_width[0] * 2, 128);
3401 src_stride[1] =
NIALIGN(src_width[1] * 2, 128);
3407 hpad[0] = dst_stride[0] - src_width[0] * 2;
3408 hpad[1] = dst_stride[1] - src_width[1] * 2;
3425 dst_line = dst->
p_data[i];
3426 src_line = src->
p_data[i];
3428 for (h = 0; i < 3 && h < src_height[i]; h++)
3430 memcpy(dst_line, src_line, src_width[i] * (tenBit + 1));
3435 lastidx = src_width[i];
3439 sample = &src_line[(lastidx - 1) * 2];
3440 dest = &dst_line[lastidx * 2];
3443 for (j = 0; j < hpad[i] / 2; j++)
3445 memcpy(dest, sample, 2);
3450 YUVsample = dst_line[lastidx - 1];
3451 memset(&dst_line[lastidx], YUVsample, hpad[i]);
3455 src_line += src_stride[i];
3456 dst_line += dst_stride[i];
3460 src_line = dst_line - dst_stride[i];
3462 for (h = 0; h < vpad[i]; h++)
3464 memcpy(dst_line, src_line, dst_stride[i]);
3465 dst_line += dst_stride[i];
3487 int sei_size, size, len;
3488 int ret = 0, i = 0, j = 0;
3489 uint8_t *p_src_sei_data, *p_dst_sei_data;
3492 if (!p_session_ctx || !p_param || !ppu_config)
3503 ni_log2(p_session_ctx,
NI_LOG_ERROR,
"%s: not supported on device with FW API version < 6rx\n", __func__);
3511 "h264 and h265 decoder\n", __func__);
3516 if (p_dec_input_param->
hwframes != 1)
3519 "So can't reconfig ppu\n", __func__);
3526 "So can't reconfig ppu1\n", __func__);
3533 "So can't reconfig ppu2\n", __func__);
3546 "out of range\n", __func__, ppu_config->
ppu_w[i], ppu_config->
ppu_h[i]);
3550 if ((ppu_config->
ppu_w[i] & 1) || (ppu_config->
ppu_h[i] & 1))
3553 __func__, ppu_config->
ppu_w[i], ppu_config->
ppu_h[i]);
3560 if (p_dst_custom_sei == NULL)
3563 "to allocate memory for custom sei data\n", __func__);
3572 p_src_sei_data = (uint8_t *)ppu_config;
3573 p_dst_sei_data = &p_dst_custom_sei->
data[0];
3577 p_dst_sei_data[size++] = 0x00;
3578 p_dst_sei_data[size++] = 0x00;
3579 p_dst_sei_data[size++] = 0x00;
3580 p_dst_sei_data[size++] = 0x01;
3584 p_dst_sei_data[size++] = 0x06;
3588 p_dst_sei_data[size++] = 0x4e;
3589 p_dst_sei_data[size++] = 0x01;
3599 p_dst_sei_data[size++] = len > 0xff ? 0xff : len;
3606 if (j >= 2 && !p_dst_sei_data[size - 2] && !p_dst_sei_data[size - 1] && p_src_sei_data[j] <= 0x03)
3609 p_dst_sei_data[size++] = 0x03;
3611 p_dst_sei_data[size++] = p_src_sei_data[j];
3617 "idx=%u, size=%d\n", __func__,
3619 free(p_dst_custom_sei);
3624 p_dst_sei_data[size++] = 0x80;
3627 p_dst_sei_data, size);
3629 free(p_dst_custom_sei);