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);
311 video_height[s_index] = tmp_height;
313 if ((*n !=
'x') || (video_width[s_index] <= 0 || video_height[s_index] <= 0))
316 "Must be in format [WIDTHxHEIGHT] (e.g. 1920x1080)\n",
optarg);
329 ni_strcpy(enc_conf_params[e_index],
sizeof(enc_conf_params[e_index]),
optarg);
339 ni_strcpy(enc_gop_params[g_index],
sizeof(enc_gop_params[g_index]),
optarg);
369 ni_log(
NI_LOG_ERROR,
"Error: Missing output filepath (or filename) argument (-o | --output)\n");
374 if (s_index != i_index) {
375 ni_log(
NI_LOG_ERROR,
"Error: Number of input resolution specified does not match number of input files\n");
380 if (enc_codec_format == -1) {
390 if (f_index != o_index)
392 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);
398 input_total = i_index;
407 output_total = o_index;
418 ni_log(
NI_LOG_ERROR,
"Error: -k | --readframerate option is not supported in hwupload mode\n");
422 if (hwupload && input_total > 1)
424 ni_log(
NI_LOG_ERROR,
"Error: multiple input (sequence change) is not supported in hwupload mode\n");
452 for (i = 0; i < input_total; i++)
454 if (!in_filename[i][0])
462 ni_fopen(&(input_fp[i]), in_filename[i],
"rb");
476 for (i = 0; i < output_total; i++)
478 if (strcmp(out_filename[i],
"null") != 0 &&
479 strcmp(out_filename[i],
"/dev/null") != 0)
484 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
486 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
489 ni_fopen(&(output_fp[i]), out_filename[i],
"wb");
504 ni_log(
NI_LOG_INFO,
"Note: Requested NULL output for index %d, no output file will be generated\n", i);
511 for (i = 0; i < output_total; i++)
513 if (bitstream_features_log_file[i][0] != 0)
515 bitstream_features_fp[i] = NULL;
516 ni_fopen(&bitstream_features_fp[i], bitstream_features_log_file[i],
"w");
517 if (!bitstream_features_fp[i])
519 ni_log(
NI_LOG_ERROR,
"Error: Failed to open bitstream features log file %s\n", bitstream_features_log_file[i]);
523 ni_log(
NI_LOG_INFO,
"Opened bitstream features log file: %s (for output %d)\n", bitstream_features_log_file[i], i);
529 if (!p_enc_api_param)
536 for (i = 0; i < output_total; i++)
546 for (i = 0; i < output_total; i++)
570 ni_log(
NI_LOG_INFO,
"Starting hwupload + encoding mode: video resolution %dx%d\n",
571 video_width[0], video_height[0]);
580 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
581 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
582 if (p_hwframe == NULL)
588 ret =
encoder_open(&ctx, &enc_ctx[0], p_enc_api_param, output_total, enc_conf_params, enc_gop_params,
589 NULL, video_width[0], video_height[0], 30, 1, 200000, enc_codec_format,
591 0, xcoderGUID, p_hwframe, 0,
false);
603 video_width[0], video_height[0], output_total, output_fp);
613 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
618 while (!end_of_all_streams &&
622 if (first_frame_uploaded && !eos)
624 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
625 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
629 p_in_frame = &in_frame;
631 }
else if (p_hwframe == NULL)
640 for (i = 0; i < output_total; i++)
643 send_rc =
encoder_send_data3(&ctx, &enc_ctx[i], p_in_frame, video_width[0], video_height[0], eos);
644 first_frame_uploaded = 1;
649 end_of_all_streams = 1;
667 if (end_of_all_streams)
671 for (i = 0; i < output_total; i++)
677 video_width[0], video_height[0], output_total, output_fp);
681 for (i = 0; i < output_total; i++)
683 if (out_packet[i].data.
packet.
data_len > 0 && bitstream_features_fp[i] != NULL)
689 for (i = 0; receive_rc >= 0 && i < output_total; i++)
695 output_fp[i] != NULL)
697 fclose(output_fp[i]);
700 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
702 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
712 end_of_all_streams = 0;
717 end_of_all_streams = 1;
723 for (i = 0; i < output_total; i++)
725 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
743 ret =
encoder_open(&ctx, enc_ctx, p_enc_api_param, output_total,
744 enc_conf_params, enc_gop_params, NULL, video_width[0],
745 video_height[0], 30, 1, 200000,
746 enc_codec_format, pix_fmt, 0, xcoderGUID, NULL,
760 video_width[0], video_height[0], output_total, output_fp);
770 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
776 ni_log(
NI_LOG_INFO,
"Starting to encode: video resolution %dx%d\n", video_width[0], video_height[0]);
778 while (!end_of_all_streams &&
783 video_width[i_index], video_height[i_index],
784 pix_fmt, sw_pix_fmt, &eos,
785 enc_ctx[0].session_run_state);
795 video_width[i_index], video_height[i_index],
796 sw_pix_fmt, 0, enc_codec_format);
803 for (i = 0; i < output_total; i++)
809 video_width[i_index], video_height[i_index], eos);
814 &in_frame, eos ? NULL : yuv_buf,
815 video_width[i_index], video_height[i_index],
816 i_index == input_total - 1);
833 for (i = 0; i < output_total; i++)
839 video_width[0], video_height[0], output_total, output_fp);
843 for (i = 0; i < output_total; i++)
845 if (out_packet[i].data.
packet.
data_len > 0 && bitstream_features_fp[i] != NULL)
851 for (i = 0; receive_rc >= 0 && i < output_total; i++)
857 output_fp[i] != NULL)
859 fclose(output_fp[i]);
862 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
864 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
874 end_of_all_streams = 0;
879 end_of_all_streams = 1;
885 for (i = 0; i < output_total; i++)
887 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
898 for (i = 0; i < output_total; i++)
901 output_fp[i] != NULL)
903 fseek(output_fp[i], 0, SEEK_END);
904 if (ftell(output_fp[i]) == 0)
906 fclose(output_fp[i]);
908 remove(jpeg_filename[i]);
917 for (i = 0; i < output_total; i++)
924 for (i = 0; i < output_total; i++)
937 for (i = 0; i < output_total; i++)
948 for (i = 0; i < output_total; i++)
951 if (output_fp[i] != NULL)
953 fclose(output_fp[i]);
957 for (i = 0; i < input_total; i++)
968 for (i = 0; i < output_total; i++)
970 if (bitstream_features_fp[i] != NULL)
972 fclose(bitstream_features_fp[i]);
973 bitstream_features_fp[i] = NULL;
979 free(p_enc_api_param);