45 uint32_t i, j, sumQp = 0;
46 uint32_t mbWidth, mbHeight, numMbs;
50 int importanceLevelCentre = p_param->
roi_demo_mode == 1 ? 40 : 10;
51 int importanceLevelRest = p_param->
roi_demo_mode == 1 ? 10 : 40;
52 int32_t width, height;
63 uint32_t roiMapBlockUnitSize = 64;
64 uint32_t max_cu_size = 64;
68 roiMapBlockUnitSize = 16;
76 width = (width / 8) * 8;
77 height = (height / 8) * 8;
81 ((width + max_cu_size - 1) & (~(max_cu_size - 1))) /
84 ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
86 numMbs = mbWidth * mbHeight;
91 uint32_t entryPerMb = (roiMapBlockUnitSize / 8) * (roiMapBlockUnitSize / 8);
93 for (i = 0; i < numMbs; i++)
95 if ((i % mbWidth > mbWidth / 3) && (i % mbWidth < mbWidth * 2 / 3))
100 for (j = 0; j < entryPerMb; j++)
113 bIsCenter ? importanceLevelCentre : importanceLevelRest;
119 (sumQp + (numMbs >> 1)) / numMbs;
141 "Error %s(): no mem for reconf BR aux_data\n",
145 *((int32_t *)aux_data->data) =
164 int32_t intraprd = *((int32_t *)aux_data->data) =
167 "xcoder_send_frame: frame #%lu reconf "
213 "Error %s(): no mem for reconf LTR aux_data\n",
224 "%s(): frame #%lu reconf LTR "
225 "use_cur_src_as_long_term_pic %u use_long_term_ref "
242 "Error %s(): no mem for reconf max&min QP aux_data\n",
269 *((int32_t *)aux_data->data) =
273 *((int32_t *)aux_data->data));
292 *((int32_t *)aux_data->data) =
296 *((int32_t *)aux_data->data));
310 "Error %s(): no mem for reconf framerate aux_data\n",
321 "%s(): frame #%lu reconfig framerate (%d/%d) by frame aux data\n",
337 "Error %s(): no mem for reconf maxFrameSize aux_data\n",
341 *((int32_t *)aux_data->data) =
358 "Error %s(): no mem for reconf crf aux_data\n",
362 *((int32_t *)aux_data->data) =
379 "Error %s(): no mem for reconf crf aux_data\n",
385 *((
float *)aux_data->data) = crf;
387 "%s(): frame #%lu reconfig float type crf %f by frame "
388 "aux data\n", __func__, p_enc_ctx->
frame_num, crf);
401 "Error %s(): no mem for reconf vbfMaxRate aux_data\n",
405 *((int32_t *)aux_data->data) =
411 "Error %s(): no mem for reconf vbvBufferSize aux_data\n",
415 *((int32_t *)aux_data->data) =
418 "%s(): frame #%lu reconfig vbfMaxRate %d vbvBufferSize "
419 "%d by frame aux data\n",
431 if (maxFrameSizeRatio < 1) {
440 "Error %s(): no mem for reconf maxFrameSizeRatio aux_data\n",
445 int32_t bitrate, framerate_num, framerate_denom;
446 uint32_t min_maxFrameSize, maxFrameSize;
456 framerate_num = (int32_t) api_param->
fps_number;
460 min_maxFrameSize = ((uint32_t)bitrate / framerate_num * framerate_denom) / 8;
463 *((int32_t *)aux_data->data) = maxFrameSize;
465 "xcoder_send_frame: frame #%lu reconf "
466 "maxFrameSizeRatio %d maxFrameSize %d\n",
467 p_enc_ctx->
frame_num, maxFrameSizeRatio, maxFrameSize);
479 "Error %s(): no mem for reconf sliceArg aux_data\n",
483 *((int16_t *)aux_data->data) =
486 "xcoder_send_frame: frame #%lu reconf "
524 "xcoder_send_frame: frame #%lu API reconfig intraPeriod %d\n",
553 "colorDescPresent %d colorPrimaries %d "
554 "colorTrc %d colorSpace %d aspectRatioWidth %d "
555 "aspectRatioHeight %d videoFullRange %d\n",
596 "%s(): frame %llu minQpI %d maxQpI %d maxDeltaQp %d minQpPB %d maxQpPB %d\n",
638 "%s(): frame #%lu API reconfig framerate (%d/%d)\n",
688 ni_log(
NI_LOG_DEBUG,
"%s(): frame #%lu API reconfig vbvMaxRate %d vbvBufferSize %d\n",
702 "xcoder_send_frame: frame #%lu reconf maxFrameSizeRatio %d\n",
714 "xcoder_send_frame: frame #%lu API reconfig sliceArg %d\n",
737 int input_video_width,
int input_video_height,
749 "for previous session to end\n");
785 p_in_frame->
pts = p_ctx->
pts[enc_id];
809 uint64_t abs_time_ns;
828 "Error: failed ni_device_session_write() for encoder\n");
858 "encoder_send_data: session_run_state change to %d\n",
863 ++p_ctx->
pts[enc_id];
883 int input_video_width,
int input_video_height)
886 int data_len_to_send;
913 #ifdef ENCODER_FLUSH_INJECT
954 p_in_frame->
pts = p_ctx->
pts[enc_id];
962 bool alignment_2pass_wa = (
970 dst_stride, dst_height_aligned);
972 if (alignment_2pass_wa && !is_hwframe) {
977 dst_height_aligned[1] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
981 dst_height_aligned[2] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
1018 p_enc_ctx, p_in_frame, &(p_dec_out_data->
data.
frame),
1019 p_enc_ctx->
codec_format, should_send_sei_with_frame, mdcv_data,
1020 cll_data, cc_data, udu_data, hdrp_data);
1039 p_in_frame, input_video_width, input_video_height, dst_stride,
1042 if (!p_in_frame->
p_data[0])
1052 if (!p_in_frame->
p_data[3])
1060 "p_dst alloc linesize = %d/%d/%d src height=%d "
1061 "dst height aligned = %d/%d/%d force_key_frame=%d, "
1063 " sei_size=%u (hdr_content_light_level %u hdr_mastering_display_"
1064 "color_vol %u hdr10+ %u hrd %d) reconf_size=%u roi_size=%u "
1065 "force_pic_qp=%u udu_sei_size=%u "
1066 "use_cur_src_as_long_term_pic %u use_long_term_ref %u\n",
1067 dst_stride[0], dst_stride[1], dst_stride[2], input_video_height,
1068 dst_height_aligned[0], dst_height_aligned[1], dst_height_aligned[2],
1084 src_height[1] = src_height[2] = src_height[0] / 2;
1091 src_stride[2] = src_stride[1];
1107 (uint8_t **)(p_in_frame->
p_data), p_src, input_video_width,
1110 ->cfg_enc_params.conf_win_right,
1111 dst_stride, dst_height_aligned, src_stride, src_height);
1120 cc_data, udu_data, hdrp_data, is_hwframe,
1134 data_len_to_send = (int)(p_in_frame->
data_len[0] + p_in_frame->
data_len[1] +
1152 }
else if (oneSent == 0)
1173 ++p_ctx->
pts[enc_id];
1190 int input_video_width,
int input_video_height,
int eos)
1220 p_in_frame->
pts = p_ctx->
pts[enc_id];
1235 "Error: failed ni_device_session_write() for encoder\n");
1258 ++p_ctx->
pts[enc_id];
1274 bool check_zerocopy)
1277 bool isrgba =
false;
1287 p_enc_ctx->
hw_id = iXcoderGUID;
1329 int linesize_aligned = width;
1339 linesize_aligned = ((width + 1) / 2) * 2;
1341 (linesize_aligned - width) / 2 * 2;
1346 int height_aligned = height;
1356 height_aligned = ((height + 1) / 2) * 2;
1358 (height_aligned - height) / 2 * 2;
1376 src_stride[1] = src_stride[2] = 0;
1380 src_stride[1] = isnv12frame ? src_stride[0] : src_stride[0] / 2;
1381 src_stride[2] = isnv12frame ? 0 : src_stride[0] / 2;
1385 p_enc_params, width, height,
1386 (
const int *)src_stride,
true);
1395 ni_log(
NI_LOG_INFO,
"Encoder device %d session open successful.\n", iXcoderGUID);
1423 int new_stride, ori_stride;
1424 bool bIsSmallPicture =
false;
1427 int new_width, new_height;
1452 src_stride[1] = is_semiplanar ? src_stride[0] : src_stride[0] / 2;
1453 src_stride[2] = is_semiplanar ? 0 : src_stride[0] / 2;
1457 p_api_param, new_width, new_height,
1478 "lookaheadDepth %d\n",
1482 bIsSmallPicture =
true;
1488 bIsSmallPicture =
true;
1497 bIsSmallPicture =
true;
1503 "due to crop width x height\n");
1504 bIsSmallPicture =
true;
1508 "pix fmt: %d->%d bIsSmallPicture %d codec %d\n",
1509 ori_stride, p_enc_ctx->
ori_height, new_stride, new_height,
1510 p_enc_ctx->
ori_pix_fmt, new_pix_fmt, bIsSmallPicture,
1514 if (ori_stride*p_enc_ctx->
ori_height < new_stride*new_height ||
1519 ni_log(
NI_LOG_INFO,
"XCoder encode sequence change by close / re-open session\n");
1522 p_enc_ctx->
hw_id, p_api_param, new_width,
1523 new_height, new_pix_fmt,
true);
1526 ni_log(
NI_LOG_ERROR,
"Failed to Re-open Encoder Session upon Sequence Change (status = %d)\n", ret);
1541 "resolution change: AV1 Picture Width not aligned to %d - picture will be cropped\n",
1546 "resolution change: AV1 Picture Height not aligned to %d - picture will be cropped\n",
1549 ni_log(
NI_LOG_INFO,
"XCoder encode sequence change by re-config session (fast path)\n");
1563 uint32_t frame_denom, FILE *p_file)
1568 uint8_t start_header[32] = {
1569 0x44, 0x4b, 0x49, 0x46,
1572 0x41, 0x56, 0x30, 0x31,
1575 0x1e, 0x00, 0x00, 0x00,
1576 0x01, 0x00, 0x00, 0x00,
1577 0x00, 0x00, 0x00, 0x00,
1578 0x00, 0x00, 0x00, 0x00
1581 if (width && height)
1583 start_header[12] = width & 0xff;
1584 start_header[13] = ((width >> 8) & 0xff);
1585 start_header[14] = height & 0xff;
1586 start_header[15] = ((height >> 8) & 0xff);
1589 if (frame_num && frame_denom)
1591 start_header[16] = frame_num & 0xff;
1592 start_header[17] = ((frame_num >> 8) & 0xff);
1593 start_header[18] = ((frame_num >> 16) & 0xff);
1594 start_header[19] = ((frame_num >> 24) & 0xff);
1595 start_header[20] = frame_denom & 0xff;
1596 start_header[21] = ((frame_denom >> 8) & 0xff);
1597 start_header[22] = ((frame_denom >> 16) & 0xff);
1598 start_header[23] = ((frame_denom >> 24) & 0xff);
1601 if (fwrite(start_header,
sizeof(start_header), 1, p_file) != 1)
1625 uint32_t pkt_size = p_out_pkt->
data_len - meta_size;
1639 uint8_t ivf_frame_header[12] = {((pkt_size & 0xff)),
1640 ((pkt_size >> 8) & 0xff),
1641 ((pkt_size >> 16) & 0xff),
1642 ((pkt_size >> 24) & 0xff),
1644 ((pts >> 8) & 0xff),
1645 ((pts >> 16) & 0xff),
1646 ((pts >> 24) & 0xff),
1647 0x00, 0x00, 0x00, 0x00};
1648 if (fwrite(ivf_frame_header, 12, 1, p_file) != 1)
1671 if (fwrite((uint8_t *)p_out_pkt->
av1_p_data[i] + meta_size,
1672 p_out_pkt->
av1_data_len[i] - meta_size, 1, p_file) != 1)
1680 if (fwrite((uint8_t *)p_out_pkt->
p_data + meta_size,
1681 p_out_pkt->
data_len - meta_size, 1, p_file) != 1)
1704 uint8_t frame_cnt[4] = {
1705 (muxed_num_packets & 0xff),
1706 ((muxed_num_packets >> 8) & 0xff),
1707 ((muxed_num_packets >> 16) & 0xff),
1708 ((muxed_num_packets >> 24) & 0xff)};
1709 fseek(p_file, 24, SEEK_SET);
1710 if (fwrite(frame_cnt, 4, 1, p_file) != 1)
1754 "encode session id invalid, the session should be closed\n");
1773 if (rx_size > meta_size)
1786 p_out_pkt->
p_data = NULL;
1804 p_out_pkt->
p_data = NULL;
1817 "pts %lld dts %lld\n", p_out_pkt->
pts, p_out_pkt->
dts);
1840 (fwrite((uint8_t *)p_out_pkt->
p_data + meta_size,
1841 p_out_pkt->
data_len - meta_size, 1, p_file) != 1))
1859 pts = p_out_pkt->
pts;
1867 }
else if (rx_size != 0)
1870 rx_size, meta_size);
1872 }
else if (end_flag)
1879 "change completed, return SEQ_CHANGE_DONE and will reopen "
1880 "or reconfig codec!\n");
1978 int bit_depth_factor;
1987 bit_depth_factor = 1;
1992 bit_depth_factor = 2;
1996 bit_depth_factor = 1;
2003 ni_log(
NI_LOG_ERROR,
"Failed to send Sequence Change to Encoder Session (status = %d)\n", ret);
2024 int output_total,
char p_enc_conf_params[][2048],
2025 char p_enc_conf_gop[][2048],
2026 ni_frame_t *p_ni_frame,
int width,
int height,
2027 int fps_num,
int fps_den,
int bitrate,
2029 int aspect_ratio_idc,
int xcoder_guid,
2031 bool check_zerocopy)
2039 int video_full_range_flag = 0;
2041 if (p_ni_frame != NULL)
2076 for (i = 0; i < output_total; i++)
2091 fps_den, bitrate, width, height,
2092 enc_ctx_list[i].codec_format);
2104 &p_api_param_list[i], &enc_ctx_list[i]))
2111 &p_api_param_list[i], &enc_ctx_list[i]))
2121 "in the session context!\n", i);
2127 if (color_prim != p_api_param_list[i].color_primaries &&
2131 "%d to overwrite source %d\n",
2132 i, p_api_param_list[i].color_primaries, color_prim);
2137 if (color_trc != p_api_param_list[i].color_transfer_characteristic &&
2141 "overwrite source %d\n", i,
2142 p_api_param_list[i].color_transfer_characteristic, color_trc);
2147 if (color_space != p_api_param_list[i].color_space &&
2151 "to overwrite source %d\n",
2152 i, p_api_param_list[i].color_space, color_space);
2157 if (p_api_param_list[i].video_full_range_flag >= 0)
2160 "flag %d\n", i, p_api_param_list[i].video_full_range_flag);
2167 sar_num = ni_h264_pixel_aspect_list[aspect_ratio_idc].num;
2168 sar_den = ni_h264_pixel_aspect_list[aspect_ratio_idc].den;
2169 }
else if (p_api_param_list[i].sar_denom)
2171 sar_num = p_api_param_list[i].
sar_num;
2172 sar_den = p_api_param_list[i].
sar_denom;
2176 if (p_surface != NULL)
2203 &p_api_param_list[i], width, height,
2204 pix_fmt, check_zerocopy);
2223 int output_total,
char p_enc_conf_params[][2048],
2224 char p_enc_conf_gop[][2048],
2225 ni_frame_t *p_ni_frame,
int width[],
int height[],
2226 int fps_num,
int fps_den,
int bitrate,
2228 int aspect_ratio_idc,
int xcoder_guid,
2230 bool check_zerocopy)
2238 int video_full_range_flag = 0;
2240 if (p_ni_frame != NULL)
2275 for (i = 0; i < output_total; i++)
2290 fps_den, bitrate, width[i], height[i],
2291 enc_ctx_list[i].codec_format);
2303 &p_api_param_list[i], &enc_ctx_list[i]))
2310 &p_api_param_list[i], &enc_ctx_list[i]))
2320 "in the session context!\n", i);
2326 if (color_prim != p_api_param_list[i].color_primaries &&
2330 "%d to overwrite source %d\n",
2331 i, p_api_param_list[i].color_primaries, color_prim);
2336 if (color_trc != p_api_param_list[i].color_transfer_characteristic &&
2340 "overwrite source %d\n", i,
2341 p_api_param_list[i].color_transfer_characteristic, color_trc);
2346 if (color_space != p_api_param_list[i].color_space &&
2350 "to overwrite source %d\n",
2351 i, p_api_param_list[i].color_space, color_space);
2356 if (p_api_param_list[i].video_full_range_flag >= 0)
2359 "flag %d\n", i, p_api_param_list[i].video_full_range_flag);
2366 sar_num = ni_h264_pixel_aspect_list[aspect_ratio_idc].num;
2367 sar_den = ni_h264_pixel_aspect_list[aspect_ratio_idc].den;
2368 }
else if (p_api_param_list[i].sar_denom)
2370 sar_num = p_api_param_list[i].
sar_num;
2371 sar_den = p_api_param_list[i].
sar_denom;
2375 if (p_surface[i] != NULL)
2402 &p_api_param_list[i], width[i], height[i],
2403 pix_fmt[i], check_zerocopy);
2422 int output_total, FILE **pfs_list)
2424 int i, recycle_index;
2429 for (i = 0; i < output_total; i++)
2433 p_out_pkt = &pkt[i];
2440 height, pfs_list[i], in_frame);
2444 enc_ctx_list[i].
hw_action && recycle_index > 0 &&
2454 "number_of_packets_list %u recycle_index %u\n", i,
2477 if (recv_fin_flag < 0)
2487 return recv_fin_flag;
2495 int output_total, FILE **pfs_list)
2497 int i, recycle_index;
2502 for (i = 0; i < output_total; i++)
2506 p_out_pkt = &pkt[i];
2513 height[i], pfs_list[i], in_frame);
2517 enc_ctx_list[i].
hw_action && recycle_index > 0 &&
2527 "number_of_packets_list %u recycle_index %u\n", i,
2550 if (recv_fin_flag < 0)
2560 return recv_fin_flag;
2573 for (i = 0; i < output_total; i++)
2575 ni_log(
NI_LOG_ERROR,
"Encoder %d closing, Got: Packets=%u FPS=%.2f Total bytes %llu\n",
2613 p_dec_frame = &frame_list->
frames[frame_list->
head];
2625 if (p_enc_ctx_list[i].hw_action)
2641 }
else if (p_enc_ctx_list[0].hw_action && !p_ctx->
enc_eos_sent[i])
2651 }
else if (p_enc_ctx_list[0].hw_action)
2676 return (
void *)(int64_t)ret;
2686 int end_of_all_streams = 0;
2697 p_enc_recv_param->
p_file);
2698 for (i = 0; ret >= 0 && i < p_enc_recv_param->
output_total; i++)
2703 end_of_all_streams = 0;
2708 end_of_all_streams = 1;
2716 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
2737 return (
void *)(int64_t)ret;
2748 return q->
size == 0;
2819 int has_b_frame = 0;
2827 if (dtsOffset < dts_offset)
2829 dtsOffset = dts_offset;
2840 if (has_b_frame && !dtsOffset)
2846 for (
int i = 0; i < dtsOffset; ++i)