34 static const uint8_t default_scaling_list_inter[] = {
35 16, 16, 16, 16, 17, 18, 20, 24, 16, 16, 16, 17, 18, 20, 24, 25,
36 16, 16, 17, 18, 20, 24, 25, 28, 16, 17, 18, 20, 24, 25, 28, 33,
37 17, 18, 20, 24, 25, 28, 33, 41, 18, 20, 24, 25, 28, 33, 41, 54,
38 20, 24, 25, 28, 33, 41, 54, 71, 24, 25, 28, 33, 41, 54, 71, 91};
40 static const uint8_t default_scaling_list_intra[] = {
41 16, 16, 16, 16, 17, 18, 21, 24, 16, 16, 16, 16, 17, 19, 22, 25,
42 16, 16, 17, 18, 20, 22, 25, 29, 16, 16, 18, 21, 24, 27, 31, 36,
43 17, 17, 20, 24, 30, 35, 41, 47, 18, 19, 22, 27, 35, 44, 54, 65,
44 21, 22, 25, 31, 41, 54, 70, 88, 24, 25, 29, 36, 47, 65, 88, 115};
46 static const uint8_t default_scaling4[2][16] = {
47 {6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42},
48 {10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34}};
50 static const uint8_t default_scaling8[2][64] = {
51 {6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29,
52 13, 16, 18, 23, 25, 27, 29, 31, 16, 18, 23, 25, 27, 29, 31, 33,
53 18, 23, 25, 27, 29, 31, 33, 36, 23, 25, 27, 29, 31, 33, 36, 38,
54 25, 27, 29, 31, 33, 36, 38, 40, 27, 29, 31, 33, 36, 38, 40, 42},
55 {9, 13, 15, 17, 19, 21, 22, 24, 13, 13, 17, 19, 21, 22, 24, 25,
56 15, 17, 19, 21, 22, 24, 25, 27, 17, 19, 21, 22, 24, 25, 27, 28,
57 19, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32,
58 22, 24, 25, 27, 28, 30, 32, 33, 24, 25, 27, 28, 30, 32, 33, 35}};
60 static const uint8_t ni_zigzag_direct[64] = {
61 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
62 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
63 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
64 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63};
66 static const uint8_t ni_zigzag_scan[16 + 1] = {
67 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 2 + 0 * 4,
68 3 + 0 * 4, 2 + 1 * 4, 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4,
69 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
73 {0, 1}, {1, 1}, {12, 11}, {10, 11}, {16, 11}, {40, 33},
74 {24, 11}, {20, 11}, {32, 11}, {80, 33}, {18, 11}, {15, 11},
75 {64, 33}, {160, 99}, {4, 3}, {3, 2}, {2, 1},
78 static const uint8_t hevc_sub_width_c[] = {1, 2, 2, 1};
80 static const uint8_t hevc_sub_height_c[] = {1, 2, 1, 1};
82 static const uint8_t hevc_diag_scan4x4_x[16] = {
83 0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 1, 2, 3, 2, 3, 3,
86 static const uint8_t hevc_diag_scan4x4_y[16] = {
87 0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 3, 2, 3,
90 static const uint8_t hevc_diag_scan8x8_x[64] = {
91 0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0,
92 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 2,
93 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 4, 5, 6, 7, 5, 6, 7, 6, 7, 7,
96 static const uint8_t hevc_diag_scan8x8_y[64] = {
97 0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 6,
98 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 7,
99 6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 7, 6, 5, 4, 7, 6, 5, 7, 6, 7,
187 for (i = 0; i < cpb_count; i++)
205 int ret = -1, aspect_ratio_info_present_flag;
206 unsigned int aspect_ratio_idc;
209 if (aspect_ratio_info_present_flag)
218 sps->
sar = ni_h264_pixel_aspect_list[aspect_ratio_idc];
276 if (!num_units_in_tick || !time_scale)
280 num_units_in_tick, time_scale);
339 const uint8_t *jvt_list,
const uint8_t *fallback_list)
341 int i, last = 8, next = 8;
342 const uint8_t *scan = (size == 16 ? ni_zigzag_scan : ni_zigzag_direct);
347 memcpy(factors, fallback_list, size *
sizeof(uint8_t));
350 for (i = 0; i < size; i++)
355 if (v < -128 || v > 127)
360 next = (last + v) & 0xff;
364 memcpy(factors, jvt_list, size *
sizeof(uint8_t));
367 last = (factors[scan[i]] = next ? next : last);
375 uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64])
378 const uint8_t *fallback[4] = {default_scaling4[0], default_scaling4[1],
379 default_scaling8[0], default_scaling8[1]};
385 default_scaling4[0], fallback[0]);
387 default_scaling4[0], scaling_matrix4[0]);
389 default_scaling4[0], scaling_matrix4[1]);
391 default_scaling4[1], fallback[1]);
393 default_scaling4[1], scaling_matrix4[3]);
395 default_scaling4[1], scaling_matrix4[4]);
397 default_scaling8[0], fallback[2]);
399 default_scaling8[1], fallback[3]);
403 default_scaling8[0], scaling_matrix8[0]);
405 default_scaling8[1], scaling_matrix8[3]);
407 default_scaling8[0], scaling_matrix8[1]);
409 default_scaling8[1], scaling_matrix8[4]);
424 int profile_idc, level_idc, constraint_set_flags = 0;
426 int i, log2_max_frame_num_minus4;
455 if (100 == profile_idc || 110 == profile_idc || 122 == profile_idc ||
456 244 == profile_idc || 44 == profile_idc || 83 == profile_idc ||
457 86 == profile_idc || 118 == profile_idc || 128 == profile_idc ||
458 138 == profile_idc || 139 == profile_idc || 134 == profile_idc ||
459 135 == profile_idc || 144 == profile_idc )
516 log2_max_frame_num_minus4);
587 int step_x = 1 << hsub;
624 int *sei_type,
int *is_interlaced)
640 unsigned type = 0, tmp;
651 }
while (tmp == 0xFF);
653 *sei_type = (int)type;
663 }
while (tmp == 0xFF);
676 if (cpb_dpb_delays_present_flag)
688 "parse_sei pic_timing SEI invalid pic_struct: "
720 uint32_t nal_size, ep3_removed = 0, vcl_nal_count = 0;
721 int nal_type = -1, sei_type = -1;
722 int sps_parsed = 0, is_interlaced = 0;
732 while ((!sps_parsed || !is_interlaced) && vcl_nal_count < 100 &&
739 while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
755 nal_size -= ep3_removed;
759 if (vcl_nal_count > 0)
762 "Warning: %s has %d slice NAL units ahead of SPS!\n",
763 __func__, vcl_nal_count);
787 "probe_h264_stream_info interlaced NOT supported!\n");
795 vcl_nal_count, sps_parsed ?
"Yes" :
"No",
796 is_interlaced ?
"Yes" :
"No");
797 if (sps_parsed && !is_interlaced)
803 "probing failed.\n");
806 static const char csp[4][5] = {
"Gray",
"420",
"422",
"444"};
808 "H.264 stream probed %d VCL NAL units, sps:%u "
809 "profile:%d/%d poc %d ref:%d %dx%d [SAR: %d:%d] %s %s "
810 "%" PRId32
"/%" PRId32
" %d bits max_reord:%d max_dec_buf:"
832 int32_t *frame_num,
unsigned int *first_mb_in_slice)
835 uint8_t *p_buf = buf;
836 unsigned int slice_type, pps_id;
839 while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
865 slice_type, *first_mb_in_slice);
872 "pps_id %u size %d first_mb %u\n",
873 slice_type, *frame_num, pps_id, size_bytes,
927 *nal_type = (p_ctx->
file_cache[i] & 0x7E) >> 1;
953 int subpic_params_present)
957 for (i = 0; i < nb_cpb; i++)
962 if (subpic_params_present)
982 for (i = 0; i < 32; i++)
995 #define check_profile_idc(idc) \
996 ptl->profile_idc == (idc) || ptl->profile_compatibility_flag[idc]
1036 #undef check_profile_idc
1046 8 + (8 * 2 * (max_num_sub_layers - 1 > 0)))
1054 for (i = 0; i < max_num_sub_layers - 1; i++)
1060 if (max_num_sub_layers - 1 > 0)
1061 for (i = max_num_sub_layers - 1; i < 8; i++)
1063 for (i = 0; i < max_num_sub_layers - 1; i++)
1077 "Not enough data for sublayer %i level_idc\n", i);
1089 int nal_params_present = 0, vcl_params_present = 0;
1090 int subpic_params_present = 0;
1093 if (common_inf_present)
1098 if (nal_params_present || vcl_params_present)
1102 if (subpic_params_present)
1116 if (subpic_params_present)
1127 for (i = 0; i < max_sublayers; i++)
1130 unsigned int nb_cpb = 1;
1144 if (nb_cpb < 1 || nb_cpb > 32)
1151 if (nal_params_present)
1153 if (vcl_params_present)
1163 for (matrixId = 0; matrixId < 6; matrixId++)
1166 memset(sl->
sl[0][matrixId], 16, 16);
1167 sl->
sl_dc[0][matrixId] = 16;
1168 sl->
sl_dc[1][matrixId] = 16;
1170 memcpy(sl->
sl[1][0], default_scaling_list_intra, 64);
1171 memcpy(sl->
sl[1][1], default_scaling_list_intra, 64);
1172 memcpy(sl->
sl[1][2], default_scaling_list_intra, 64);
1173 memcpy(sl->
sl[1][3], default_scaling_list_inter, 64);
1174 memcpy(sl->
sl[1][4], default_scaling_list_inter, 64);
1175 memcpy(sl->
sl[1][5], default_scaling_list_inter, 64);
1176 memcpy(sl->
sl[2][0], default_scaling_list_intra, 64);
1177 memcpy(sl->
sl[2][1], default_scaling_list_intra, 64);
1178 memcpy(sl->
sl[2][2], default_scaling_list_intra, 64);
1179 memcpy(sl->
sl[2][3], default_scaling_list_inter, 64);
1180 memcpy(sl->
sl[2][4], default_scaling_list_inter, 64);
1181 memcpy(sl->
sl[2][5], default_scaling_list_inter, 64);
1182 memcpy(sl->
sl[3][0], default_scaling_list_intra, 64);
1183 memcpy(sl->
sl[3][1], default_scaling_list_intra, 64);
1184 memcpy(sl->
sl[3][2], default_scaling_list_intra, 64);
1185 memcpy(sl->
sl[3][3], default_scaling_list_inter, 64);
1186 memcpy(sl->
sl[3][4], default_scaling_list_inter, 64);
1187 memcpy(sl->
sl[3][5], default_scaling_list_inter, 64);
1192 uint8_t scaling_list_pred_mode_flag;
1193 int32_t scaling_list_dc_coef[2][6];
1194 int size_id, matrix_id, pos;
1197 for (size_id = 0; size_id < 4; size_id++)
1198 for (matrix_id = 0; matrix_id < 6;
1199 matrix_id += ((size_id == 3) ? 3 : 1))
1202 if (!scaling_list_pred_mode_flag)
1210 delta *= (size_id == 3) ? 3 : 1;
1211 if (matrix_id < delta)
1214 "Invalid delta in scaling list data: %d.\n",
1219 memcpy(sl->
sl[size_id][matrix_id],
1220 sl->
sl[size_id][matrix_id - delta],
1221 size_id > 0 ? 64 : 16);
1223 sl->
sl_dc[size_id - 2][matrix_id] =
1224 sl->
sl_dc[size_id - 2][matrix_id - delta];
1228 int32_t next_coef, coef_num;
1229 int32_t scaling_list_delta_coef;
1232 coef_num = 1 << (4 + (size_id << 1));
1237 scaling_list_dc_coef[size_id - 2][matrix_id] =
1239 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
1240 sl->
sl_dc[size_id - 2][matrix_id] = next_coef;
1242 for (i = 0; i < coef_num; i++)
1246 4 * hevc_diag_scan4x4_y[i] + hevc_diag_scan4x4_x[i];
1249 8 * hevc_diag_scan8x8_y[i] + hevc_diag_scan8x8_x[i];
1253 (next_coef + 256U + scaling_list_delta_coef) % 256;
1254 sl->
sl[size_id][matrix_id][pos] = next_coef;
1261 for (i = 0; i < 64; i++)
1263 sl->
sl[3][1][i] = sl->
sl[2][1][i];
1264 sl->
sl[3][2][i] = sl->
sl[2][2][i];
1265 sl->
sl[3][4][i] = sl->
sl[2][4][i];
1266 sl->
sl[3][5][i] = sl->
sl[2][5][i];
1280 uint8_t rps_predict = 0;
1294 int32_t abs_delta_rps;
1295 uint8_t use_delta_flag = 0;
1296 uint8_t delta_rps_sign;
1298 if (is_slice_header)
1304 "Invalid value of delta_idx in slice header RPS: %d > "
1316 if (abs_delta_rps < 1 || abs_delta_rps > 32768)
1322 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
1330 if (used || use_delta_flag)
1332 if (i < rps_ridx->num_delta_pocs)
1333 delta_poc = delta_rps + rps_ridx->
delta_poc[i];
1335 delta_poc = delta_rps;
1345 if (k >= (
sizeof(rps->
used) /
sizeof(rps->
used[0])))
1360 used = rps->
used[i];
1361 for (k = i - 1; k >= 0; k--)
1364 if (delta_poc < tmp)
1369 rps->
used[k] = used;
1382 used = rps->
used[i];
1386 rps->
used[k] = used;
1392 int prev, nb_positive_pics;
1410 if (delta_poc < 1 || delta_poc > 32768)
1421 for (i = 0; i < nb_positive_pics; i++)
1424 if (delta_poc < 1 || delta_poc > 32768)
1442 VUI backup_vui, *vui = &sps->
vui;
1444 int sar_present, alt = 0;
1450 if (sar_idx < (
sizeof(vui_sar) /
sizeof(vui_sar[0])))
1451 vui->
sar = vui_sar[sar_idx];
1452 else if (sar_idx == 255)
1497 "Invalid format, only support yuv420p\n");
1516 memcpy(&br_backup, br,
sizeof(br_backup));
1517 memcpy(&backup_vui, vui,
sizeof(backup_vui));
1529 if (apply_defdispwin)
1532 "discarding vui default display window, "
1533 "original values are l:%u r:%u t:%u b:%u\n",
1554 "Strange VUI timing information, retrying...\n");
1555 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1556 memcpy(br, &br_backup,
sizeof(br_backup));
1581 "Strange VUI bitstream restriction information, retrying"
1582 " from timing information...\n");
1583 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1584 memcpy(br, &br_backup,
sizeof(br_backup));
1602 "Overread in VUI, retrying from timing information...\n");
1603 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1604 memcpy(br, &br_backup,
sizeof(br_backup));
1615 int log2_diff_max_min_transform_block_size;
1616 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
1681 "Luma bit depth (%d) is different from chroma bit depth (%d), "
1682 "this is unsupported.\n",
1691 "only support 8bit/10bit yuv420p, bit_depth %d, "
1692 "chroma_format_idc %d\n",
1706 "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1730 if (!sublayer_ordering_info)
1732 for (i = 0; i < start; i++)
1760 "Invalid value %d for log2_diff_max_min_coding_block_size",
1772 if (log2_diff_max_min_transform_block_size < 0 ||
1773 log2_diff_max_min_transform_block_size > 30)
1776 "Invalid value %d for log2_diff_max_min_transform_block_size",
1777 log2_diff_max_min_transform_block_size);
1812 "PCM bit depth (%d, %d) is greater than normal bit depth "
1828 for (i = 0; i < (int)sps->
nb_st_rps; i++)
1881 "extended_precision_processing_flag not yet implemented\n");
1888 "high_precision_offsets_enabled_flag not yet "
1898 "cabac_bypass_alignment_enabled_flag not yet "
1913 memset(ow, 0,
sizeof(*ow));
1932 "log2_ctb_size %d differs from the bounds of any known profile\n",
1964 "max_transform_hierarchy_depth_inter out of range: %d\n",
1972 "max_transform_hierarchy_depth_intra out of range: %d\n",
1997 uint8_t *buf = NULL;
1999 uint32_t nal_size, ep3_removed = 0, vcl_nal_count = 0;
2006 "Error probe_h265_stream_info: allocate stream buf failed\n");
2012 while ((!sps_parsed) && vcl_nal_count < 100 &&
2018 while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
2027 "Error probe_h265_stream_info NAL has no header\n");
2034 nal_size -= ep3_removed;
2039 if (vcl_nal_count > 0)
2042 "Warning: %s has %d slice NAL units ahead of SPS!\n",
2043 __func__, vcl_nal_count);
2049 "probe_h265_stream_info: parse_sps error\n");
2053 }
else if (nal_type < 32)
2107 memcpy(p_dst, &p_ctx->
file_cache[i], data_size);
2112 memcpy(p_dst, &p_ctx->
file_cache[i], data_size);
2168 if ((profile != 0) && (profile != 2))
2172 "Only support profile0(yuv420,8bit) and profile2(yuv420, 10bit)\n");
2183 uint8_t *buf = NULL;
2185 if (NULL == (buf = calloc(1, 64)))
2188 "Error probe_vp9_stream_info: allocate stream buf failed\n");
2193 uint32_t size_bytes = 64;
2230 int input_video_width,
int input_video_height,
2234 uint8_t *tmp_buf_ptr = tmp_buf;
2236 uint32_t frame_pkt_size = 0, nal_size;
2239 uint32_t send_size = 0;
2241 int32_t frame_num = -1, curr_frame_num;
2242 unsigned int first_mb_in_slice = 0;
2265 frame_pkt_size += nal_size;
2266 tmp_buf_ptr += nal_size;
2268 nal_type, nal_size);
2274 nal_size, sps, &curr_frame_num,
2275 &first_mb_in_slice))
2277 if (-1 == frame_num)
2280 frame_num = curr_frame_num;
2281 }
else if (curr_frame_num != frame_num ||
2282 0 == first_mb_in_slice)
2287 frame_pkt_size -= nal_size;
2295 "decoder_send_data: parse_slice_header error "
2296 "NAL type %d size %u, continue\n",
2297 nal_type, nal_size);
2299 }
else if (-1 != frame_num)
2303 frame_pkt_size -= nal_size;
2312 frame_pkt_size += nal_size;
2313 tmp_buf_ptr += nal_size;
2315 nal_type, nal_size);
2317 if (nal_type >= 0 && nal_type <= 23)
2320 __func__, nal_type, nal_size);
2328 frame_pkt_size += packet_size;
2341 send_size = frame_pkt_size + p_dec_ctx->
prev_size;
2418 int output_video_width,
int output_video_height,
2419 FILE *p_file,
int write_to_file,
2424 bool b_is_hwframe = p_dec_ctx->
hw_action;
2435 "===> decoder_receive_data hwframe %d pixel_format %d <===\n",
2451 output_video_height;
2474 &(p_out_data->
data.
frame), width, height,
2504 }
else if (rx_size > 0)
2512 if (p_file && write_to_file) {
2521 output_video_width, output_video_height, p_dec_ctx->
pixel_format,
2527 output_video_width, output_video_height, p_dec_ctx->
pixel_format, p_out_frame);
2551 *p_rx_size = rx_size;
2589 p_dec_ctx->
hw_id = iXcoderGUID;
2607 ni_log(
NI_LOG_INFO,
"Decoder device %d session open successful.\n", iXcoderGUID);
2675 return (
void *)(long)retval;
2708 p_out_frame = &frame_list->
frames[frame_list->
tail];
2711 p_ctx, p_dec_ctx, p_out_frame, p_dec_recv_param->
input_width,
2724 "Error: decoder_receive_thread break in transcode mode!\n");
2756 else if (drawbox_params->
enabled)
2761 p_dec_recv_param->
p_fmt_ctx, p_ni_frame, &filter_out_frame, drawbox_params,
2778 ni_log(
NI_LOG_INFO,
"Decoder stats: received %u frames, fps %.2f, total bytes %u\n",
2795 return (
void *)(long)retval;