202int main(
int argc,
char *argv[])
209 int o_index = 0, e_index = 0, g_index = 0, f_index = 0;
210 int output_total = 0;
211 int input_width = 0, input_height = 0, bit_depth = 8;
214 int dec_codec_format = -1, enc_codec_format = -1;
218 char dec_conf_params[2048] = {0};
222 int user_data_sei_passthru = 0;
236 void *p_stream_info = NULL;
237 int fps_num = 30, fps_den = 1, bitrate = 200000;
247 int encoder_opened = 0, end_of_all_streams = 0;
254 int hw_frame_ref_flag = 0;
255 int recycle_hw_frame_for_scaler = 0;
259 const char *opt_string =
"hvi:o:m:n:l:c:r:d:e:g:uf:";
260 static struct option long_options[] = {
273 {
"user-data-sei-passthru",
no_argument, NULL,
'u'},
278 while ((opt =
getopt_long(argc, argv, opt_string, long_options, &opt_index)) != -1)
301 for (i = 0; i < o_index; i++)
303 if (0 == strcmp(out_filename[i],
optarg))
316 for (i = 0; i < strlen(
optarg); i++)
320 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
324 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
328 else if (strcmp(
optarg,
"v") == 0 || strcmp(
optarg,
"vp9") == 0)
335 "Must be one of [a|avc, h|hevc, v|vp9]\n",
optarg);
342 for (i = 0; i < strlen(
optarg); i++)
346 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
350 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
354 else if (strcmp(
optarg,
"x") == 0 || strcmp(
optarg,
"av1") == 0)
361 "Must be one of [a|avc, h|hevc, x|av1]\n",
optarg);
375 "Must be one of [none, fatal, error, info, debug, trace]\n",
optarg);
381 xcoderGUID = (int)strtol(
optarg, &n, 10);
382 if (n ==
optarg || *n !=
'\0' || xcoderGUID < 0)
385 "Must be a non-negative integer\n",
optarg);
395 "Must be a positive integer\n",
optarg);
410 ni_strcpy(enc_conf_params[e_index],
sizeof(enc_conf_params[e_index]),
optarg);
420 ni_strcpy(enc_gop_params[g_index],
sizeof(enc_gop_params[g_index]),
optarg);
424 user_data_sei_passthru = 1;
433 ni_strcpy(filter_conf_params[f_index],
sizeof(filter_conf_params[f_index]),
optarg);
443 if (!in_filename[0]) {
455 if (dec_codec_format == -1) {
461 if (enc_codec_format == -1) {
474 output_total = o_index;
475 for (i = 0; i < output_total; i++)
477 if (strcmp(out_filename[i],
"null") != 0 &&
478 strcmp(out_filename[i],
"/dev/null") != 0)
481 ni_fopen(&(output_fp[i]), out_filename[i],
"wb");
492 ni_log(
NI_LOG_INFO,
"Note: Requested NULL output for index %d, no output file will be generated\n", i);
495 frame_to_enc[i] = NULL;
497 p_hwframe2[i] = NULL;
501 if (!p_dec_api_param)
509 if (!p_enc_api_param)
522 "ERROR: Failed to probe input file as H.264, file format not supported!\n");
527 input_width = avc_sps.
width;
528 input_height = avc_sps.
height;
530 bit_depth, input_width, input_height);
537 "ERROR: Failed to probe input file as H.265, file format not supported!\n");
541 input_width = (int)(hevc_sps.
width -
544 input_height = (int)(hevc_sps.
height -
547 ni_log(
NI_LOG_INFO,
"Using probed H.265 source info: %d bits, resolution %dx%d\n",
548 bit_depth, input_width, input_height);
555 "ERROR: Failed to probe input file as VP9, file format not supported!\n");
558 bit_depth = vp9_info.
profile ? 10 : 8;
559 input_width = vp9_info.
width;
560 input_height = vp9_info.
height;
562 "Using probed VP9 source info: %d bits, resolution %dx%d, timebase %u/%u\n",
563 bit_depth, input_width, input_height,
608 for (i = 0; i < output_total; i++)
618 for (i = 0; i < output_total; i++)
621 output_width[i] = input_width;
622 output_height[i] = input_height;
626 p_stream_info = &avc_sps;
628 p_stream_info = &hevc_sps;
631 p_stream_info = &vp9_info;
636 for (i = 0; i < output_total; i++)
638 if (filter_conf_params[i][0])
641 ni_log(
NI_LOG_ERROR,
"Error: filters are only supported when hwframe is enabled (out=hw)\n");
646 ret =
retrieve_filter_params(filter_conf_params[i], &scale_params[i], &drawbox_params, &ai_pre_params[i]);
649 ni_log(
NI_LOG_ERROR,
"Error: failed to parse filter parameters: %s\n", filter_conf_params[i]);
654 if (scale_params[i].enabled)
662 output_width[i] = scale_params[i].
width;
663 output_height[i] = scale_params[i].
height;
665 else if (ai_pre_params[i].enabled)
667 if (!ai_pre_params[i].nb_file[0])
669 ni_log(
NI_LOG_ERROR,
"Error: ni_quadra_ai_pre requires an 'nb' (network binary) param\n");
682 if (ai_pre_params[i].out_width > 0)
683 output_width[i] = ai_pre_params[i].
out_width;
684 if (ai_pre_params[i].out_height > 0)
685 output_height[i] = ai_pre_params[i].
out_height;
687 else if ((drawbox_params.
enabled))
717 for (i = 0; i < output_total; i++)
720 ni_log(
NI_LOG_ERROR,
"Error: Mixing scale and drawbox filter is not supported in this demo\n");
724 ni_log(
NI_LOG_ERROR,
"Error: Mixing ai_pre and drawbox filter is not supported in this demo\n");
727 ni_log(
NI_LOG_INFO,
"(Task %d) Starting to transcode: HWFrames %d, video resolution %dx%d -> %dx%d\n",
728 i, dec_ctx.
hw_action, input_width, input_height, output_width[i], output_height[i]);
734 while (!end_of_all_streams &&
740 send_rc =
decoder_send_data(&ctx, &dec_ctx, &in_pkt, input_width, input_height, p_stream_info);
750 input_height, NULL, 0, &rx_size);
752 for (i = 0; i < output_total; i++)
754 frame_to_enc[i] = &out_frame;
774 if (p_enc_api_param[0].low_delay_mode <= 0 && encoder_opened)
788 recycle_hw_frame_for_scaler = 1;
789 for (i = 0; i < output_total; i++)
791 if (scale_params[i].enabled)
793 if(!hw_frame_ref_flag)
795 hw_frame_ref_flag = 1;
801 frame_to_enc[i] = &filter_out_frame[i];
803 else if (ai_pre_params[i].enabled)
807 if(!hw_frame_ref_flag)
809 hw_frame_ref_flag = 1;
815 if (!ai_pre_params[i].initialized)
828 ret = ni_create_network(&ai_network[i]);
838 if ((ai_network[i].netw != p_hwframe->
ui16width
843 "Model not match input, model resolution=%dx%d, input resolution=%dx%d\n",
844 ai_network[i].netw, ai_network[i].neth,
850 if ((ai_network[i].net_out_w != ai_pre_params[i].out_width
851 && ai_network[i].net_out_h != ai_pre_params[i].out_height)
852 && ai_network[i].net_out_w !=
NI_VPU_ALIGN128(ai_pre_params[i].out_width))
855 "Model not match output, model resolution=%dx%d, output resolution=%dx%d\n",
856 ai_network[i].net_out_w, ai_network[i].net_out_h,
857 ai_pre_params[i].out_width, ai_pre_params[i].out_height);
865 &filter_out_frame[i], &ai_pre_params[i], ai_out_gc620);
872 frame_to_enc[i] = &filter_out_frame[i];
874 else if (drawbox_params.
enabled)
876 if(!hw_frame_ref_flag)
878 hw_frame_ref_flag = 1;
884 for (i = 0; i < output_total; i++)
886 frame_to_enc[i] = &filter_out_frame[0];
891 if (!scale_params[i].enabled && !ai_pre_params[i].enabled)
892 recycle_hw_frame_for_scaler = 0;
901 if((hw_frame_ref_flag) && (recycle_hw_frame_for_scaler))
903 hw_frame_ref_flag = 0;
909 for (i = 0; i < output_total; i++)
914 if (scale_params[i].enabled)
916 else if (ai_pre_params[i].enabled)
918 else if (drawbox_params.
enabled)
924 ret =
encoder_open2(&ctx, enc_ctx, p_enc_api_param, output_total, enc_conf_params,
925 enc_gop_params, &out_frame.
data.
frame, output_width, output_height,
926 fps_num, fps_den, bitrate, enc_codec_format, enc_pix_fmt,
937 output_width, output_height, output_total, output_fp);
947 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
955 for (i = 0; i < output_total; i++)
959 send_rc =
encoder_send_data2(&ctx, &enc_ctx[i], frame_to_enc[i], &enc_in_frame, output_width[i], output_height[i]);
1000 output_width, output_height, output_total, output_fp);
1002 for (i = 0; receive_rc >= 0 && i < output_total; i++)
1007 end_of_all_streams = 0;
1012 end_of_all_streams = 1;
1018 ni_log(
NI_LOG_INFO,
"Decoder stats: received %u frames, fps %.2f, total bytes %u\n",
1022 for (i = 0; i < output_total; i++)
1024 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
1037 for (i = 0; i < output_total; i++)
1039 if (ai_pre_params[i].enabled)
1042 ni_destroy_network(&ai_network[i]);
1048 for (i = 0; i < output_total; i++)
1051 if (scale_params[i].enabled)
1055 if (ai_pre_params[i].enabled)
1070 for (i = 0; i < output_total; i++)
1074 if (scale_params[i].enabled)
1079 if (ai_pre_params[i].enabled)
1106 for (i = 0; i < output_total; i++)
1112 for (i = 0; i < output_total; i++)
1118 for (i = 0; i < output_total; i++)
1121 if (output_fp[i] != NULL)
1123 fclose(output_fp[i]);
1125 if (scale_params[i].enabled)
1129 if (ai_pre_params[i].enabled)
1150 free(p_dec_api_param);
1151 free(p_enc_api_param);