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)))
1058 for (i = 0; i < max_num_sub_layers - 1; i++)
1064 if (max_num_sub_layers - 1 > 0)
1065 for (i = max_num_sub_layers - 1; i < 8; i++)
1067 for (i = 0; i < max_num_sub_layers - 1; i++)
1081 "Not enough data for sublayer %i level_idc\n", i);
1093 int nal_params_present = 0, vcl_params_present = 0;
1094 int subpic_params_present = 0;
1097 if (common_inf_present)
1102 if (nal_params_present || vcl_params_present)
1106 if (subpic_params_present)
1120 if (subpic_params_present)
1131 for (i = 0; i < max_sublayers; i++)
1134 unsigned int nb_cpb = 1;
1148 if (nb_cpb < 1 || nb_cpb > 32)
1155 if (nal_params_present)
1157 if (vcl_params_present)
1167 for (matrixId = 0; matrixId < 6; matrixId++)
1170 memset(sl->
sl[0][matrixId], 16, 16);
1171 sl->
sl_dc[0][matrixId] = 16;
1172 sl->
sl_dc[1][matrixId] = 16;
1174 memcpy(sl->
sl[1][0], default_scaling_list_intra, 64);
1175 memcpy(sl->
sl[1][1], default_scaling_list_intra, 64);
1176 memcpy(sl->
sl[1][2], default_scaling_list_intra, 64);
1177 memcpy(sl->
sl[1][3], default_scaling_list_inter, 64);
1178 memcpy(sl->
sl[1][4], default_scaling_list_inter, 64);
1179 memcpy(sl->
sl[1][5], default_scaling_list_inter, 64);
1180 memcpy(sl->
sl[2][0], default_scaling_list_intra, 64);
1181 memcpy(sl->
sl[2][1], default_scaling_list_intra, 64);
1182 memcpy(sl->
sl[2][2], default_scaling_list_intra, 64);
1183 memcpy(sl->
sl[2][3], default_scaling_list_inter, 64);
1184 memcpy(sl->
sl[2][4], default_scaling_list_inter, 64);
1185 memcpy(sl->
sl[2][5], default_scaling_list_inter, 64);
1186 memcpy(sl->
sl[3][0], default_scaling_list_intra, 64);
1187 memcpy(sl->
sl[3][1], default_scaling_list_intra, 64);
1188 memcpy(sl->
sl[3][2], default_scaling_list_intra, 64);
1189 memcpy(sl->
sl[3][3], default_scaling_list_inter, 64);
1190 memcpy(sl->
sl[3][4], default_scaling_list_inter, 64);
1191 memcpy(sl->
sl[3][5], default_scaling_list_inter, 64);
1196 uint8_t scaling_list_pred_mode_flag;
1197 int32_t scaling_list_dc_coef[2][6];
1198 int size_id, matrix_id, pos;
1201 for (size_id = 0; size_id < 4; size_id++)
1202 for (matrix_id = 0; matrix_id < 6;
1203 matrix_id += ((size_id == 3) ? 3 : 1))
1206 if (!scaling_list_pred_mode_flag)
1214 delta *= (size_id == 3) ? 3 : 1;
1215 if (matrix_id < delta)
1218 "Invalid delta in scaling list data: %d.\n",
1223 memcpy(sl->
sl[size_id][matrix_id],
1224 sl->
sl[size_id][matrix_id - delta],
1225 size_id > 0 ? 64 : 16);
1227 sl->
sl_dc[size_id - 2][matrix_id] =
1228 sl->
sl_dc[size_id - 2][matrix_id - delta];
1232 int32_t next_coef, coef_num;
1233 int32_t scaling_list_delta_coef;
1236 coef_num = 1 << (4 + (size_id << 1));
1241 scaling_list_dc_coef[size_id - 2][matrix_id] =
1243 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
1244 sl->
sl_dc[size_id - 2][matrix_id] = next_coef;
1246 for (i = 0; i < coef_num; i++)
1250 4 * hevc_diag_scan4x4_y[i] + hevc_diag_scan4x4_x[i];
1253 8 * hevc_diag_scan8x8_y[i] + hevc_diag_scan8x8_x[i];
1257 (next_coef + 256U + scaling_list_delta_coef) % 256;
1258 sl->
sl[size_id][matrix_id][pos] = next_coef;
1265 for (i = 0; i < 64; i++)
1267 sl->
sl[3][1][i] = sl->
sl[2][1][i];
1268 sl->
sl[3][2][i] = sl->
sl[2][2][i];
1269 sl->
sl[3][4][i] = sl->
sl[2][4][i];
1270 sl->
sl[3][5][i] = sl->
sl[2][5][i];
1284 uint8_t rps_predict = 0;
1298 int32_t abs_delta_rps;
1299 uint8_t use_delta_flag = 0;
1300 uint8_t delta_rps_sign;
1302 if (is_slice_header)
1308 "Invalid value of delta_idx in slice header RPS: %d > "
1320 if (abs_delta_rps < 1 || abs_delta_rps > 32768)
1326 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
1334 if (used || use_delta_flag)
1336 if (i < rps_ridx->num_delta_pocs)
1337 delta_poc = delta_rps + rps_ridx->
delta_poc[i];
1339 delta_poc = delta_rps;
1349 if (k >= (
sizeof(rps->
used) /
sizeof(rps->
used[0])))
1364 used = rps->
used[i];
1365 for (k = i - 1; k >= 0; k--)
1368 if (delta_poc < tmp)
1373 rps->
used[k] = used;
1386 used = rps->
used[i];
1390 rps->
used[k] = used;
1396 int prev, nb_positive_pics;
1414 if (delta_poc < 1 || delta_poc > 32768)
1425 for (i = 0; i < nb_positive_pics; i++)
1428 if (delta_poc < 1 || delta_poc > 32768)
1446 VUI backup_vui, *vui = &sps->
vui;
1448 int sar_present, alt = 0;
1454 if (sar_idx < (
sizeof(vui_sar) /
sizeof(vui_sar[0])))
1455 vui->
sar = vui_sar[sar_idx];
1456 else if (sar_idx == 255)
1501 "Invalid format, only support yuv420p\n");
1520 memcpy(&br_backup, br,
sizeof(br_backup));
1521 memcpy(&backup_vui, vui,
sizeof(backup_vui));
1533 if (apply_defdispwin)
1536 "discarding vui default display window, "
1537 "original values are l:%u r:%u t:%u b:%u\n",
1558 "Strange VUI timing information, retrying...\n");
1559 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1560 memcpy(br, &br_backup,
sizeof(br_backup));
1585 "Strange VUI bitstream restriction information, retrying"
1586 " from timing information...\n");
1587 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1588 memcpy(br, &br_backup,
sizeof(br_backup));
1606 "Overread in VUI, retrying from timing information...\n");
1607 memcpy(vui, &backup_vui,
sizeof(backup_vui));
1608 memcpy(br, &br_backup,
sizeof(br_backup));
1619 int log2_diff_max_min_transform_block_size;
1620 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
1685 "Luma bit depth (%d) is different from chroma bit depth (%d), "
1686 "this is unsupported.\n",
1695 "only support 8bit/10bit yuv420p, bit_depth %d, "
1696 "chroma_format_idc %d\n",
1710 "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1734 if (!sublayer_ordering_info)
1736 for (i = 0; i < start; i++)
1764 "Invalid value %d for log2_diff_max_min_coding_block_size",
1776 if (log2_diff_max_min_transform_block_size < 0 ||
1777 log2_diff_max_min_transform_block_size > 30)
1780 "Invalid value %d for log2_diff_max_min_transform_block_size",
1781 log2_diff_max_min_transform_block_size);
1816 "PCM bit depth (%d, %d) is greater than normal bit depth "
1832 for (i = 0; i < (int)sps->
nb_st_rps; i++)
1885 "extended_precision_processing_flag not yet implemented\n");
1892 "high_precision_offsets_enabled_flag not yet "
1902 "cabac_bypass_alignment_enabled_flag not yet "
1917 memset(ow, 0,
sizeof(*ow));
1936 "log2_ctb_size %d differs from the bounds of any known profile\n",
1968 "max_transform_hierarchy_depth_inter out of range: %d\n",
1976 "max_transform_hierarchy_depth_intra out of range: %d\n",
2001 uint8_t *buf = NULL;
2003 uint32_t nal_size, ep3_removed = 0, vcl_nal_count = 0;
2010 "Error probe_h265_stream_info: allocate stream buf failed\n");
2016 while ((!sps_parsed) && vcl_nal_count < 100 &&
2022 while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
2031 "Error probe_h265_stream_info NAL has no header\n");
2038 nal_size -= ep3_removed;
2043 if (vcl_nal_count > 0)
2046 "Warning: %s has %d slice NAL units ahead of SPS!\n",
2047 __func__, vcl_nal_count);
2053 "probe_h265_stream_info: parse_sps error\n");
2057 }
else if (nal_type < 32)
2111 memcpy(p_dst, &p_ctx->
file_cache[i], data_size);
2116 memcpy(p_dst, &p_ctx->
file_cache[i], data_size);
2172 if ((profile != 0) && (profile != 2))
2176 "Only support profile0(yuv420,8bit) and profile2(yuv420, 10bit)\n");
2187 uint8_t *buf = NULL;
2189 if (NULL == (buf = calloc(1, 64)))
2192 "Error probe_vp9_stream_info: allocate stream buf failed\n");
2197 uint32_t size_bytes = 64;
2234 int input_video_width,
int input_video_height,
2238 uint8_t *tmp_buf_ptr = tmp_buf;
2240 uint32_t frame_pkt_size = 0, nal_size;
2243 uint32_t send_size = 0;
2245 int32_t frame_num = -1, curr_frame_num;
2246 unsigned int first_mb_in_slice = 0;
2269 frame_pkt_size += nal_size;
2270 tmp_buf_ptr += nal_size;
2272 nal_type, nal_size);
2278 nal_size, sps, &curr_frame_num,
2279 &first_mb_in_slice))
2281 if (-1 == frame_num)
2284 frame_num = curr_frame_num;
2285 }
else if (curr_frame_num != frame_num ||
2286 0 == first_mb_in_slice)
2291 frame_pkt_size -= nal_size;
2299 "decoder_send_data: parse_slice_header error "
2300 "NAL type %d size %u, continue\n",
2301 nal_type, nal_size);
2303 }
else if (-1 != frame_num)
2307 frame_pkt_size -= nal_size;
2316 frame_pkt_size += nal_size;
2317 tmp_buf_ptr += nal_size;
2319 nal_type, nal_size);
2321 if (nal_type >= 0 && nal_type <= 23)
2324 __func__, nal_type, nal_size);
2332 frame_pkt_size += packet_size;
2345 send_size = frame_pkt_size + p_dec_ctx->
prev_size;
2422 int output_video_width,
int output_video_height,
2423 FILE *p_file,
int write_to_file,
2428 bool b_is_hwframe = p_dec_ctx->
hw_action;
2439 "===> decoder_receive_data hwframe %d pixel_format %d <===\n",
2455 output_video_height;
2478 &(p_out_data->
data.
frame), width, height,
2508 }
else if (rx_size > 0)
2516 if (p_file && write_to_file) {
2525 output_video_width, output_video_height, p_dec_ctx->
pixel_format,
2531 output_video_width, output_video_height, p_dec_ctx->
pixel_format, p_out_frame);
2555 *p_rx_size = rx_size;
2593 p_dec_ctx->
hw_id = iXcoderGUID;
2611 ni_log(
NI_LOG_INFO,
"Decoder device %d session open successful.\n", iXcoderGUID);
2679 return (
void *)(int64_t)retval;
2712 p_out_frame = &frame_list->
frames[frame_list->
tail];
2715 p_ctx, p_dec_ctx, p_out_frame, p_dec_recv_param->
input_width,
2728 "Error: decoder_receive_thread break in transcode mode!\n");
2760 else if (drawbox_params->
enabled)
2765 p_dec_recv_param->
p_fmt_ctx, p_ni_frame, &filter_out_frame, drawbox_params,
2782 ni_log(
NI_LOG_INFO,
"Decoder stats: received %u frames, fps %.2f, total bytes %u\n",
2799 return (
void *)(int64_t)retval;