95int main(
int argc,
char *argv[])
105 int i_index = 0, s_index = 0, o_index = 0, e_index = 0, g_index = 0, f_index = 0;
106 int input_total = 0, output_total = 0;
107 int enc_codec_format = -1;
119 int first_frame_uploaded = 0;
120 void *yuv_buf = NULL;
132 int end_of_all_streams = 0, read_size = 0, eos = 0;
139 const char *opt_string =
"hvi:o:m:l:c:r:k:p:s:e:g:ub:";
140 static struct option long_options[] = {
159 while ((opt =
getopt_long(argc, argv, opt_string, long_options, &opt_index)) != -1)
189 for (i = 0; i < o_index; i++)
191 if (0 == strcmp(out_filename[i],
optarg))
204 for (i = 0; i < strlen(
optarg); i++)
208 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
212 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
216 else if (strcmp(
optarg,
"j") == 0 || strcmp(
optarg,
"jpeg") == 0)
220 else if (strcmp(
optarg,
"x") == 0 || strcmp(
optarg,
"av1") == 0)
224 else if (strcmp(
optarg,
"o") == 0 || strcmp(
optarg,
"obu") == 0)
232 "Must be one of [a|avc, h|hevc, x|av1, o|obu]\n",
optarg);
246 "Must be one of [none, fatal, error, info, debug, trace]\n",
optarg);
252 xcoderGUID = (int)strtol(
optarg, &n, 10);
253 if (n ==
optarg || *n !=
'\0' || xcoderGUID < 0)
256 "Must be a non-negative integer\n",
optarg);
266 "Must be a positive integer\n",
optarg);
276 "Must be a non-negative integer\n",
optarg);
285 if (!strcmp(
optarg,
"yuv444p"))
292 "Must be one of [yuv420p, yuv420p10le, nv12, p010le, rgba, gbra, argb, abgr, bgr0, yuv444p]\n",
306 video_width[s_index] = (int)strtol(
optarg, &n, 10);
307 video_height[s_index] =
atoi(n + 1);
308 if ((*n !=
'x') || (video_width[s_index] <= 0 || video_height[s_index] <= 0))
311 "Must be in format [WIDTHxHEIGHT] (e.g. 1920x1080)\n",
optarg);
324 ni_strcpy(enc_conf_params[e_index],
sizeof(enc_conf_params[e_index]),
optarg);
334 ni_strcpy(enc_gop_params[g_index],
sizeof(enc_gop_params[g_index]),
optarg);
364 ni_log(
NI_LOG_ERROR,
"Error: Missing output filepath (or filename) argument (-o | --output)\n");
369 if (s_index != i_index) {
370 ni_log(
NI_LOG_ERROR,
"Error: Number of input resolution specified does not match number of input files\n");
375 if (enc_codec_format == -1) {
385 if (f_index != o_index)
387 ni_log(
NI_LOG_ERROR,
"Error: Number of bitstream features log files (%d) must match number of output files (%d)\n", f_index, o_index);
393 input_total = i_index;
402 output_total = o_index;
413 ni_log(
NI_LOG_ERROR,
"Error: -k | --readframerate option is not supported in hwupload mode\n");
417 if (hwupload && input_total > 1)
419 ni_log(
NI_LOG_ERROR,
"Error: multiple input (sequence change) is not supported in hwupload mode\n");
447 for (i = 0; i < input_total; i++)
449 if (!in_filename[i][0])
457 ni_fopen(&(input_fp[i]), in_filename[i],
"rb");
471 for (i = 0; i < output_total; i++)
473 if (strcmp(out_filename[i],
"null") != 0 &&
474 strcmp(out_filename[i],
"/dev/null") != 0)
479 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
481 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
484 ni_fopen(&(output_fp[i]), out_filename[i],
"wb");
499 ni_log(
NI_LOG_INFO,
"Note: Requested NULL output for index %d, no output file will be generated\n", i);
506 for (i = 0; i < output_total; i++)
508 if (bitstream_features_log_file[i][0] != 0)
510 bitstream_features_fp[i] = NULL;
511 ni_fopen(&bitstream_features_fp[i], bitstream_features_log_file[i],
"w");
512 if (!bitstream_features_fp[i])
514 ni_log(
NI_LOG_ERROR,
"Error: Failed to open bitstream features log file %s\n", bitstream_features_log_file[i]);
518 ni_log(
NI_LOG_INFO,
"Opened bitstream features log file: %s (for output %d)\n", bitstream_features_log_file[i], i);
524 if (!p_enc_api_param)
531 for (i = 0; i < output_total; i++)
541 for (i = 0; i < output_total; i++)
565 ni_log(
NI_LOG_INFO,
"Starting hwupload + encoding mode: video resolution %dx%d\n",
566 video_width[0], video_height[0]);
575 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
576 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
577 if (p_hwframe == NULL)
583 ret =
encoder_open(&ctx, &enc_ctx[0], p_enc_api_param, output_total, enc_conf_params, enc_gop_params,
584 NULL, video_width[0], video_height[0], 30, 1, 200000, enc_codec_format,
586 0, xcoderGUID, p_hwframe, 0,
false);
598 video_width[0], video_height[0], output_total, output_fp);
608 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
613 while (!end_of_all_streams &&
617 if (first_frame_uploaded && !eos)
619 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
620 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
624 p_in_frame = &in_frame;
626 }
else if (p_hwframe == NULL)
635 for (i = 0; i < output_total; i++)
638 send_rc =
encoder_send_data3(&ctx, &enc_ctx[i], p_in_frame, video_width[0], video_height[0], eos);
639 first_frame_uploaded = 1;
644 end_of_all_streams = 1;
662 if (end_of_all_streams)
666 for (i = 0; i < output_total; i++)
672 video_width[0], video_height[0], output_total, output_fp);
676 for (i = 0; i < output_total; i++)
678 if (out_packet[i].data.
packet.
data_len > 0 && bitstream_features_fp[i] != NULL)
684 for (i = 0; receive_rc >= 0 && i < output_total; i++)
690 output_fp[i] != NULL)
692 fclose(output_fp[i]);
695 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
697 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
707 end_of_all_streams = 0;
712 end_of_all_streams = 1;
718 for (i = 0; i < output_total; i++)
720 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
738 ret =
encoder_open(&ctx, enc_ctx, p_enc_api_param, output_total,
739 enc_conf_params, enc_gop_params, NULL, video_width[0],
740 video_height[0], 30, 1, 200000,
741 enc_codec_format, pix_fmt, 0, xcoderGUID, NULL,
755 video_width[0], video_height[0], output_total, output_fp);
765 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
771 ni_log(
NI_LOG_INFO,
"Starting to encode: video resolution %dx%d\n", video_width[0], video_height[0]);
773 while (!end_of_all_streams &&
778 video_width[i_index], video_height[i_index],
779 pix_fmt, sw_pix_fmt, &eos,
780 enc_ctx[0].session_run_state);
790 video_width[i_index], video_height[i_index],
791 sw_pix_fmt, 0, enc_codec_format);
798 for (i = 0; i < output_total; i++)
804 video_width[i_index], video_height[i_index], eos);
809 &in_frame, eos ? NULL : yuv_buf,
810 video_width[i_index], video_height[i_index],
811 i_index == input_total - 1);
828 for (i = 0; i < output_total; i++)
834 video_width[0], video_height[0], output_total, output_fp);
838 for (i = 0; i < output_total; i++)
840 if (out_packet[i].data.
packet.
data_len > 0 && bitstream_features_fp[i] != NULL)
846 for (i = 0; receive_rc >= 0 && i < output_total; i++)
852 output_fp[i] != NULL)
854 fclose(output_fp[i]);
857 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
859 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
869 end_of_all_streams = 0;
874 end_of_all_streams = 1;
880 for (i = 0; i < output_total; i++)
882 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
893 for (i = 0; i < output_total; i++)
896 output_fp[i] != NULL)
898 fseek(output_fp[i], 0, SEEK_END);
899 if (ftell(output_fp[i]) == 0)
901 fclose(output_fp[i]);
903 remove(jpeg_filename[i]);
912 for (i = 0; i < output_total; i++)
919 for (i = 0; i < output_total; i++)
932 for (i = 0; i < output_total; i++)
943 for (i = 0; i < output_total; i++)
946 if (output_fp[i] != NULL)
948 fclose(output_fp[i]);
952 for (i = 0; i < input_total; i++)
963 for (i = 0; i < output_total; i++)
965 if (bitstream_features_fp[i] != NULL)
967 fclose(bitstream_features_fp[i]);
968 bitstream_features_fp[i] = NULL;
974 free(p_enc_api_param);