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;
861 int should_send_sei_with_frame, uint8_t *mdcv_data,
862 uint8_t *cll_data, uint8_t *cc_data,
863 uint8_t *udu_data, uint8_t *hdrp_data)
892 intraprd = *((int32_t *)aux_data->
data);
893 if (intraprd < 0 || intraprd > 1024)
914 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",
923 should_send_sei_with_frame = 1;
929 intraprd - (p_enc_ctx->
frame_num % intraprd);
939 ni_log(
NI_LOG_INFO,
"%s(): set intraPeriod %d on frame %u, update intra offset %u\n", __func__,
952 8 + 6 * 2 + 2 * 2 + 2 * 4 + 1;
976 uint32_t uint32_t_tmp = htonl(
980 uint32_t_tmp = htonl(
999 dst[0] = dst[1] = dst[2] = 0;
1026 uint16_t dp00 = 0, dp01 = 0, dp10 = 0, dp11 = 0, dp20 = 0, dp21 = 0,
1031 dp00 = (uint16_t)lrint(chroma_den *
1034 dp01 = (uint16_t)lrint(chroma_den *
1037 dp10 = (uint16_t)lrint(chroma_den *
1040 dp11 = (uint16_t)lrint(chroma_den *
1043 dp20 = (uint16_t)lrint(chroma_den *
1046 dp21 = (uint16_t)lrint(chroma_den *
1050 wpx = (uint16_t)lrint(chroma_den * ni_q2d(p_src->
white_point[0]));
1052 wpy = (uint16_t)lrint(chroma_den * ni_q2d(p_src->
white_point[1]));
1056 "mastering display color volume, primaries "
1057 "%u/%u/%u/%u/%u/%u white_point_x/y %u/%u max/min_lumi %u/%u\n",
1058 (uint16_t)dp00, (uint16_t)dp01, (uint16_t)dp10, (uint16_t)dp11,
1059 (uint16_t)dp20, (uint16_t)dp21, (uint16_t)wpx, (uint16_t)wpy,
1063 dst += 6 * 2 + 2 * 2;
1091 uint16_t max_content_light_level =
1093 uint16_t max_pic_average_light_level =
1103 &max_pic_average_light_level,
sizeof(uint16_t));
1114 should_send_sei_with_frame)
1117 dst[0] = dst[1] = dst[2] = 0;
1154 int cc_size = aux_data->
size;
1161 memcpy(cc_data_emu_prevent, aux_data->
data, cc_size);
1162 int cc_size_emu_prevent = cc_size +
1164 if (cc_size_emu_prevent != cc_size)
1167 "emulation prevention bytes added: %d\n",
1168 cc_size_emu_prevent - cc_size);
1185 memcpy(dst, cc_data_emu_prevent, cc_size_emu_prevent);
1186 dst += cc_size_emu_prevent;
1200 memcpy(dst, cc_data_emu_prevent, cc_size_emu_prevent);
1201 dst += cc_size_emu_prevent;
1258 ui_tmp = lrint(10000 *
1267 "hdr10+ targeted_system_display_actual_peak_luminance_"
1282 "hdr10+ num_rows_targeted_system_display_actual_peak_luminance "
1283 "x num_cols_targeted_system_display_actual_peak_luminance %u x "
1303 "luminance[%d][%d] %u\n",
1310 for (i = 0; i < 3; i++)
1312 ui_tmp = lrint(100000 * ni_q2d(hdrp->
params[w].
maxscl[i]));
1323 "hdr10+ num_distribution_maxrgb_percentiles[%d] %d\n", w,
1336 "hdr10+ distribution_maxrgb_percentage[%d][%d] %u\n", w, i,
1339 "hdr10+ distribution_maxrgb_percentile[%d][%d] %u\n", w, i,
1352 "hdr10+ mastering_display_actual_peak_luminance_flag %u\n",
1361 "hdr10+ num_rows_mastering_display_actual_peak_luminance x "
1362 "num_cols_mastering_display_actual_peak_luminance %u x %u\n",
1381 "mastering_display_actual_peak_luminance[%d][%d] %u\n",
1438 int emu_bytes_inserted;
1451 emu_bytes_inserted =
1453 dst += hdr10p_num_bytes + emu_bytes_inserted;
1458 (uint32_t)hdr10p_num_bytes + emu_bytes_inserted +
1471 emu_bytes_inserted =
1473 dst += hdr10p_num_bytes + emu_bytes_inserted;
1478 (uint8_t)hdr10p_num_bytes + emu_bytes_inserted +
1484 "ni_enc_prep_aux_data: codec %d not supported for HDR10+ "
1502 int udu_sei_size = aux_data->
size;
1503 int ext_udu_sei_size, sei_len;
1505 uint8_t *sei_data = malloc(udu_sei_size * 3 / 2);
1508 memcpy(sei_data, (uint8_t *)aux_data->
data, udu_sei_size);
1509 int emu_bytes_inserted =
1512 ext_udu_sei_size = udu_sei_size + emu_bytes_inserted;
1519 6 + ((udu_sei_size + 0xFE) / 0xFF) + ext_udu_sei_size + 1;
1525 7 + ((udu_sei_size + 0xFE) / 0xFF) + ext_udu_sei_size + 1;
1532 "ni_enc_prep_aux_data sei total length %u + sei_len %d "
1533 "exceeds maximum sei size %u, discarding it !\n",
1538 int payload_size = udu_sei_size;
1557 while (payload_size > 0)
1560 (payload_size > 0xFF ? 0xFF : (uint8_t)payload_size);
1561 payload_size -= 0xFF;
1565 memcpy(dst, sei_data, ext_udu_sei_size);
1566 dst += ext_udu_sei_size;
1587 int is_new_rois = 1;
1589 uint32_t self_size = 0;
1593 if (!self_size || aux_data->
size % self_size)
1596 "aux_data size %d self_size %u\n",
1597 aux_data->
size, self_size);
1600 int nb_roi = aux_data->
size / (int)self_size;
1628 if (set_roi_map(p_enc_ctx, codec_format, aux_data, nb_roi,
1652 if (aux_data && p_enc_ctx->
roi_map)
1670 "aux_data %d ctx->roi_map %d frame->roi_len %u ctx->roi_len %u\n",
1671 api_params->
cacheRoi, aux_data != NULL,
1684 "%s(): frame aux data LTR use_cur_src_as_ltr %u "
1697 "%s(): frame API set LTR use_cur_src_as_ltr %u "
1718 "%s(): frame aux data qp info max/min I qp <%d %d> maxDeltaQp <%d> max/min PB qp <%d %d>",
1735 int32_t bitrate = -1;
1739 bitrate = *((int32_t *)aux_data->
data);
1761 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s: bitrate %d updated for reconfig\n", __func__, bitrate);
1784 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): invalid colorDescPresent in aux data %d\n",
1820 vui = p_enc_ctx->
vui;
1829 "colorDescPresent %d colorPrimaries %d "
1830 "colorTrc %d colorSpace %d aspectRatioWidth %d "
1831 "aspectRatioHeight %d videoFullRange %d\n",
1855 int32_t ltr_interval = -1;
1860 ltr_interval = *((int32_t *)aux_data->
data);
1873 if (ltr_interval > 0)
1891 if ((framerate_num <= 0) || (framerate_denom <= 0))
1894 "ERROR: %s(): invalid framerate in aux data (%d/%d)\n",
1895 __func__, framerate_num, framerate_denom);
1898 if ((framerate_num % framerate_denom) != 0)
1900 uint32_t numUnitsInTick = 1000;
1901 framerate_num = framerate_num / framerate_denom;
1902 framerate_denom = numUnitsInTick + 1;
1904 framerate_num *= numUnitsInTick;
1907 framerate_num = framerate_num / framerate_denom;
1908 framerate_denom = 1;
1910 if (((framerate_num + framerate_denom - 1) / framerate_denom) >
1914 "ERROR: %s(): invalid framerate in aux data (%d/%d)\n",
1922 __func__, framerate_num, framerate_denom);
1949 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s: framerate num %d denom %d updated for reconfig\n",
1955 int32_t frame_num = -1;
1960 frame_num = *((int32_t *)aux_data->
data);
1962 __func__, frame_num);
1984 should_send_sei_with_frame)
2003 int32_t max_frame_size = 0;
2007 max_frame_size = *((int32_t *)aux_data->
data);
2008 uint32_t maxFrameSize = (uint32_t)max_frame_size / 2000;
2009 uint32_t min_maxFrameSize;
2015 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): max_frame_size %d is valid only when lowDelay mode is enabled\n",
2016 __func__, max_frame_size);
2021 int32_t tmp_bitrate, tmp_framerate_num, tmp_framerate_denom;
2031 tmp_framerate_num = (int32_t)api_params->
fps_number;
2035 min_maxFrameSize = (((uint32_t)tmp_bitrate / tmp_framerate_num * tmp_framerate_denom) / 8) / 2000;
2037 if (maxFrameSize < min_maxFrameSize)
2039 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): max_frame_size %d is too small (invalid)\n",
2040 __func__, max_frame_size);
2054 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set max_frame_size %d\n", __func__, max_frame_size);
2057 if (max_frame_size > 0)
2072 crf = (float)(*((int32_t *)aux_data->
data));
2078 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): reconfigure crf value %d is valid only in CRF mode\n",
2084 if (crf < 0 || crf > 51)
2086 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): crf value %d is invalid (valid range in [0..51])\n",
2095 crf = *((
float *)aux_data->
data);
2101 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): reconfigure crf value %f is valid only in CRF mode\n",
2107 if (crf < 0 || crf > 51)
2109 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"ERROR: %s(): crf value %f is invalid (valid range in [0..51])\n",
2136 int32_t vbvMaxRate = 0;
2140 vbvMaxRate = *((int32_t *)aux_data->
data);
2149 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_vbv_max_rate %d\n", __func__, vbvMaxRate);
2161 int32_t vbvBufferSize = 0;
2165 vbvBufferSize = *((int32_t *)aux_data->
data);
2166 if ((vbvBufferSize < 10 && vbvBufferSize != 0) || vbvBufferSize > 3000)
2168 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"%s(): invalid frame aux data vbvBufferSize %d\n", __func__,
2183 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_vbv_max_rate %d\n", __func__, vbvBufferSize);
2195 int16_t sliceArg = 0;
2199 sliceArg = *((int16_t *)aux_data->
data);
2203 ni_log2(p_enc_ctx,
NI_LOG_ERROR,
"%s():not support to reconfig slice_arg when slice_mode disable.\n",
2214 int max_num_ctu_mb_row = (api_params->
source_height + ctu_mb_size - 1) / ctu_mb_size;
2215 if (sliceArg < 1 || sliceArg > max_num_ctu_mb_row)
2232 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"%s(): API set reconfig_slice_arg %d\n", __func__, sliceArg);
2238 ni_log2(p_enc_ctx,
NI_LOG_INFO,
"%s(): slice arg > NI_MAX_SLICE_SIZE, change from %d to %d\n",
2270 const uint8_t *mdcv_data,
const uint8_t *cll_data,
2271 const uint8_t *cc_data,
const uint8_t *udu_data,
2272 const uint8_t *hdrp_data,
int is_hwframe,
2279 if (is_hwframe != 0 && is_hwframe != 1 && is_semiplanar != 0 &&
2283 "ni_enc_copy_aux_data: error, illegal hwframe or nv12frame\n");
2300 uint8_t *dst = (uint8_t *)p_enc_frame->
p_data[2 + is_hwframe] +
2301 p_enc_frame->
data_len[2 + is_hwframe] + frame_metadata_size;
2305 dst = (uint8_t *)p_enc_frame->
p_data[2 - is_semiplanar] +
2306 p_enc_frame->
data_len[2 - is_semiplanar] +
2307 frame_metadata_size;
2314 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"ni_enc_copy_aux_data: p_metadata_buffer %p frame_metadata_size %u dst %p\n",
2351 memset(dst, 0, p_enc_ctx->
roi_len);
2366 memcpy(dst, mdcv_data,
2377 memcpy(dst, cll_data,
2385 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"ni_enc_copy_aux_data: preferred characteristics size: %u to %p\n",
2388 dst[0] = dst[1] = dst[2] = 0;
2416 memcpy(dst, cc_data, p_enc_frame->
sei_cc_len);
2457 uint8_t *dst = timecode_data;
2459 uint32_t header_len, payload_len, total_len;
2460 int emu_bytes_inserted, is_hwframe, is_semiplanar;
2462 if (!p_enc_ctx || !p_enc_frame || !p_timecode) {
2468 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__);
2559 dst[6] = payload_len;
2561 dst[7] = payload_len;
2567 dst += payload_len + emu_bytes_inserted;
2571 total_len = header_len + payload_len + emu_bytes_inserted + 1;
2576 dst = (uint8_t *)p_enc_frame->
p_data[2 + is_hwframe] +
2580 dst = (uint8_t *)p_enc_frame->
p_data[2 - is_semiplanar] +
2591 memcpy(dst, timecode_data, total_len);
2626 ni_frame_t *p_enc_frame, uint8_t *p_yuv_buffer)
2630 bool need_copy =
true;
2633 bool alignment_2pass_wa =
false;
2636 if (!p_ctx || !p_enc_frame)
2653 __func__, p_yuv_buffer);
2671 "%s: resolution change %dx%d -> %dx%d "
2672 "or pixel format change %d -> %d\n",
2705 int should_send_sei_with_frame =
2717 should_send_sei_with_frame, mdcv_data, cll_data,
2718 cc_data, udu_data, hdrp_data);
2737 is_semiplanar, dst_stride, dst_height_aligned);
2748 src_height[0] = frame_height;
2749 p_src[0] = p_yuv_buffer;
2764 src_stride[1] = is_semiplanar ? src_stride[0] : src_stride[0] / 2;
2765 src_stride[2] = is_semiplanar ? 0 : src_stride[0] / 2;
2767 src_height[1] = src_height[0] / 2;
2768 src_height[2] = is_semiplanar ? 0 : src_height[1];
2770 p_src[1] = p_src[0] + src_stride[0] * src_height[0];
2771 p_src[2] = p_src[1] + src_stride[1] * src_height[1];
2774 alignment_2pass_wa = (
2781 if (alignment_2pass_wa && !is_hwframe) {
2782 if (is_semiplanar) {
2786 dst_height_aligned[1] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
2790 dst_height_aligned[2] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
2796 p_param, frame_width, frame_height,
2802 p_enc_frame, frame_width,
2803 frame_height, (
const int *)src_stride, (
const uint8_t **)p_src,
2819 dst_height_aligned[0], dst_stride,
2829 "%s: src_stride %d dst_stride "
2830 "%d dst_height_aligned %d src_height %d need_copy %d\n",
2831 __func__, src_stride[0],
2832 dst_stride[0], dst_height_aligned[0],
2833 src_height[0], need_copy);
2841 dst_height_aligned, src_stride, src_height);
2846 "p_dst alloc linesize = %d/%d/%d src height=%d "
2847 "dst height aligned = %d/%d/%d force_key_frame=%d, "
2849 " sei_size=%u (hdr_content_light_level %u hdr_mastering_display_"
2850 "color_vol %u hdr10+ %u hrd %d) reconf_size=%u roi_size=%u "
2851 "force_pic_qp=%u udu_sei_size=%u "
2852 "use_cur_src_as_long_term_pic %u use_long_term_ref %u\n",
2853 dst_stride[0], dst_stride[1], dst_stride[2], frame_height,
2854 dst_height_aligned[0], dst_height_aligned[1], dst_height_aligned[2],
2866 mdcv_data, cll_data, cc_data, udu_data, hdrp_data,
2876 p_enc_frame->p_buffer = NULL;
2877 p_enc_frame->buffer_size = 0;
2880 p_enc_frame->data_len[i] = 0;
2881 p_enc_frame->p_data[i] = NULL;
2904 for (i = 0; i < 3; i++) {
2905 uint32_t tmp = *state << 8;
2906 *state = tmp + *(p++);
2907 if (tmp == 0x100 || p == end)
2916 else if (p[-3] | (p[-1] - 1))
2924 p = ((p) < (end) ? (p - 4) : (end - 4));
2926 (((uint32_t) (p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3]);
2945 ni_packet_t *p_packet, uint8_t sei_type,
int vcl_found)
2972 ni_log(
NI_LOG_INFO,
"number of custom sei in current frame is out of limit(%d).\n",
2976 sei_data = &p_custom_sei->
data[0];
2986 while (index < pkt_size && pkt_data[index] == 0xff)
2988 sei_size += pkt_data[index++];
2991 if (index >= pkt_size)
2996 sei_size += pkt_data[index++];
3005 udata = &pkt_data[index];
3008 sei_data[0] = sei_type;
3014 for (i = 0, len = 0; (i < pkt_size - index) && len < sei_size; i++, len++)
3018 if (i >= 2 && udata[i - 2] == 0 && udata[i - 1] == 0 && udata[i] == 3)
3023 sei_data[len] = udata[i];
3026 if (len != sei_size)
3029 "required size:%d, actual size:%d.\n", sei_size, len);
3033 p_custom_sei->
type = sei_type;
3034 p_custom_sei->
size = sei_size;
3040 __FUNCTION__, sei_size, sei_type);
3066 uint8_t *data,
int size,
ni_packet_t *p_packet,
int low_delay,
3067 int codec_format,
int pkt_nal_bitmap,
int custom_sei_type,
3068 int *svct_skip_next_packet,
int *is_lone_sei_pkt)
3070 int pkt_sei_alone = 1;
3078 const uint8_t *ptr = data;
3079 const uint8_t *end = data + size;
3082 uint8_t sei_type = 0;
3086 uint32_t temporal_id;
3091 "ni_dec_packet_parse(): already find the header of streams.\n");
3106 if (0 == nalu_count)
3118 nalu_type = (int)stc & 0x1f;
3121 pkt_sei_alone = (pkt_sei_alone && 6 == nalu_type);
3141 "ni_dec_packet_parse(): Detect SPS, PPS and IDR, "
3142 "enable decoder low delay mode.\n");
3153 custom_sei_type == sei_type ||
3163 ptr + 1 - data, p_packet,
3164 sei_type, vcl_found);
3170 }
else if (nalu_type >= 1 &&
3174 }
else if (14 == nalu_type)
3181 if (temporal_id > svct_layer)
3185 *svct_skip_next_packet = 1;
3187 "ni_dec_packet_parse(): temporal_id %d"
3188 " is bigger than decoded layer %d, skip the next "
3190 temporal_id, svct_layer);
3195 nalu_type = (int)(stc >> 1) & 0x3F;
3198 pkt_sei_alone = (pkt_sei_alone &&
3220 if (pkt_nal_bitmap ==
3226 "ni_dec_packet_parse(): Detect VPS, SPS, PPS and "
3227 "IDR enable decoder low delay mode.\n");
3234 if (39 == nalu_type ||
3237 sei_type = *(ptr + 1);
3239 custom_sei_type == sei_type ||
3249 sei_type, vcl_found);
3255 }
else if (nalu_type >= 0 &&
3262 ni_log2(p_session_ctx,
NI_LOG_DEBUG,
"%s() wrong codec %d !\n", __FUNCTION__, codec_format);
3270 *is_lone_sei_pkt = pkt_sei_alone;
3290 int raw_width,
int raw_height,
int ni_fmt,
int nb_planes)
3292 int i, j, h, tenBit = 0;
3293 int vpad[3], hpad[3], src_height[3], src_width[3], src_stride[3];
3294 uint8_t *src_line, *dst_line, *sample, *dest, YUVsample;
3301 src_width[0] =
NIALIGN(raw_width, 2);
3302 src_width[1] =
NIALIGN(raw_width, 2) / 2;
3303 src_width[2] =
NIALIGN(raw_width, 2) / 2;
3306 src_height[0] =
NIALIGN(raw_height, 2);
3307 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3308 src_height[2] =
NIALIGN(raw_height, 2) / 2;
3311 src_stride[0] =
NIALIGN(src_width[0], 128);
3312 src_stride[1] =
NIALIGN(src_width[1], 128);
3313 src_stride[2] =
NIALIGN(src_width[2], 128);
3318 hpad[0] = dst_stride[0] - src_width[0];
3319 hpad[1] = dst_stride[1] - src_width[1];
3320 hpad[2] = dst_stride[2] - src_width[2];
3331 src_width[0] =
NIALIGN(raw_width, 2);
3332 src_width[1] =
NIALIGN(raw_width, 2) / 2;
3333 src_width[2] =
NIALIGN(raw_width, 2) / 2;
3336 src_height[0] =
NIALIGN(raw_height, 2);
3337 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3338 src_height[2] =
NIALIGN(raw_height, 2) / 2;
3341 src_stride[0] =
NIALIGN(src_width[0] * 2, 128);
3342 src_stride[1] =
NIALIGN(src_width[1] * 2, 128);
3343 src_stride[2] =
NIALIGN(src_width[2] * 2, 128);
3348 hpad[0] = dst_stride[0] - src_width[0] * 2;
3349 hpad[1] = dst_stride[1] - src_width[1] * 2;
3350 hpad[2] = dst_stride[2] - src_width[2] * 2;
3361 src_width[0] =
NIALIGN(raw_width, 2);
3362 src_width[1] =
NIALIGN(raw_width, 2);
3366 src_height[0] =
NIALIGN(raw_height, 2);
3367 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3371 src_stride[0] =
NIALIGN(src_width[0], 128);
3372 src_stride[1] =
NIALIGN(src_width[1], 128);
3378 hpad[0] = dst_stride[0] - src_width[0];
3379 hpad[1] = dst_stride[1] - src_width[1];
3391 src_width[0] =
NIALIGN(raw_width, 2);
3392 src_width[1] =
NIALIGN(raw_width, 2);
3396 src_height[0] =
NIALIGN(raw_height, 2);
3397 src_height[1] =
NIALIGN(raw_height, 2) / 2;
3401 src_stride[0] =
NIALIGN(src_width[0] * 2, 128);
3402 src_stride[1] =
NIALIGN(src_width[1] * 2, 128);
3408 hpad[0] = dst_stride[0] - src_width[0] * 2;
3409 hpad[1] = dst_stride[1] - src_width[1] * 2;
3426 dst_line = dst->
p_data[i];
3427 src_line = src->
p_data[i];
3429 for (h = 0; i < 3 && h < src_height[i]; h++)
3431 memcpy(dst_line, src_line, src_width[i] * (tenBit + 1));
3436 lastidx = src_width[i];
3440 sample = &src_line[(lastidx - 1) * 2];
3441 dest = &dst_line[lastidx * 2];
3444 for (j = 0; j < hpad[i] / 2; j++)
3446 memcpy(dest, sample, 2);
3451 YUVsample = dst_line[lastidx - 1];
3452 memset(&dst_line[lastidx], YUVsample, hpad[i]);
3456 src_line += src_stride[i];
3457 dst_line += dst_stride[i];
3461 src_line = dst_line - dst_stride[i];
3463 for (h = 0; h < vpad[i]; h++)
3465 memcpy(dst_line, src_line, dst_stride[i]);
3466 dst_line += dst_stride[i];
3488 int sei_size, size, len;
3489 int ret = 0, i = 0, j = 0;
3490 uint8_t *p_src_sei_data, *p_dst_sei_data;
3493 if (!p_session_ctx || !p_param || !ppu_config)
3504 ni_log2(p_session_ctx,
NI_LOG_ERROR,
"%s: not supported on device with FW API version < 6rx\n", __func__);
3512 "h264 and h265 decoder\n", __func__);
3517 if (p_dec_input_param->
hwframes != 1)
3520 "So can't reconfig ppu\n", __func__);
3527 "So can't reconfig ppu1\n", __func__);
3534 "So can't reconfig ppu2\n", __func__);
3547 "out of range\n", __func__, ppu_config->
ppu_w[i], ppu_config->
ppu_h[i]);
3551 if ((ppu_config->
ppu_w[i] & 1) || (ppu_config->
ppu_h[i] & 1))
3554 __func__, ppu_config->
ppu_w[i], ppu_config->
ppu_h[i]);
3561 if (p_dst_custom_sei == NULL)
3564 "to allocate memory for custom sei data\n", __func__);
3573 p_src_sei_data = (uint8_t *)ppu_config;
3574 p_dst_sei_data = &p_dst_custom_sei->
data[0];
3578 p_dst_sei_data[size++] = 0x00;
3579 p_dst_sei_data[size++] = 0x00;
3580 p_dst_sei_data[size++] = 0x00;
3581 p_dst_sei_data[size++] = 0x01;
3585 p_dst_sei_data[size++] = 0x06;
3589 p_dst_sei_data[size++] = 0x4e;
3590 p_dst_sei_data[size++] = 0x01;
3600 p_dst_sei_data[size++] = len > 0xff ? 0xff : len;
3607 if (j >= 2 && !p_dst_sei_data[size - 2] && !p_dst_sei_data[size - 1] && p_src_sei_data[j] <= 0x03)
3610 p_dst_sei_data[size++] = 0x03;
3612 p_dst_sei_data[size++] = p_src_sei_data[j];
3618 "idx=%u, size=%d\n", __func__,
3620 free(p_dst_custom_sei);
3625 p_dst_sei_data[size++] = 0x80;
3628 p_dst_sei_data, size);
3630 free(p_dst_custom_sei);