86int main(
int argc,
char *argv[])
96 int i_index = 0, s_index = 0, o_index = 0, e_index = 0, g_index = 0;
97 int input_total = 0, output_total = 0;
98 int enc_codec_format = -1;
110 int first_frame_uploaded = 0;
111 void *yuv_buf = NULL;
123 int end_of_all_streams = 0, read_size = 0, eos = 0;
128 const char *opt_string =
"hvi:o:m:l:c:r:k:p:s:e:g:u";
129 static struct option long_options[] = {
147 while ((opt =
getopt_long(argc, argv, opt_string, long_options, &opt_index)) != -1)
177 for (i = 0; i < o_index; i++)
179 if (0 == strcmp(out_filename[i],
optarg))
192 for (i = 0; i < strlen(
optarg); i++)
196 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
200 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
204 else if (strcmp(
optarg,
"j") == 0 || strcmp(
optarg,
"jpeg") == 0)
208 else if (strcmp(
optarg,
"x") == 0 || strcmp(
optarg,
"av1") == 0)
212 else if (strcmp(
optarg,
"o") == 0 || strcmp(
optarg,
"obu") == 0)
220 "Must be one of [a|avc, h|hevc, x|av1, o|obu]\n",
optarg);
234 "Must be one of [none, fatal, error, info, debug, trace]\n",
optarg);
240 xcoderGUID = (int)strtol(
optarg, &n, 10);
241 if (n ==
optarg || *n !=
'\0' || xcoderGUID < 0)
244 "Must be a non-negative integer\n",
optarg);
254 "Must be a positive integer\n",
optarg);
264 "Must be a non-negative integer\n",
optarg);
273 if (!strcmp(
optarg,
"yuv444p"))
280 "Must be one of [yuv420p, yuv420p10le, nv12, p010le, rgba, gbra, argb, abgr, bgr0, yuv444p]\n",
294 video_width[s_index] = (int)strtol(
optarg, &n, 10);
295 video_height[s_index] =
atoi(n + 1);
296 if ((*n !=
'x') || (video_width[s_index] <= 0 || video_height[s_index] <= 0))
299 "Must be in format [WIDTHxHEIGHT] (e.g. 1920x1080)\n",
optarg);
312 ni_strcpy(enc_conf_params[e_index],
sizeof(enc_conf_params[e_index]),
optarg);
322 ni_strcpy(enc_gop_params[g_index],
sizeof(enc_gop_params[g_index]),
optarg);
342 ni_log(
NI_LOG_ERROR,
"Error: Missing output filepath (or filename) argument (-o | --output)\n");
347 if (s_index != i_index) {
348 ni_log(
NI_LOG_ERROR,
"Error: Number of input resolution specified does not match number of input files\n");
353 if (enc_codec_format == -1) {
359 input_total = i_index;
368 output_total = o_index;
379 ni_log(
NI_LOG_ERROR,
"Error: -k | --readframerate option is not supported in hwupload mode\n");
383 if (hwupload && input_total > 1)
385 ni_log(
NI_LOG_ERROR,
"Error: multiple input (sequence change) is not supported in hwupload mode\n");
413 for (i = 0; i < input_total; i++)
415 if (!in_filename[i][0])
423 ni_fopen(&(input_fp[i]), in_filename[i],
"rb");
437 for (i = 0; i < output_total; i++)
439 if (strcmp(out_filename[i],
"null") != 0 &&
440 strcmp(out_filename[i],
"/dev/null") != 0)
445 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
447 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
450 ni_fopen(&(output_fp[i]), out_filename[i],
"wb");
465 ni_log(
NI_LOG_INFO,
"Note: Requested NULL output for index %d, no output file will be generated\n", i);
470 if (!p_enc_api_param)
477 for (i = 0; i < output_total; i++)
487 for (i = 0; i < output_total; i++)
511 ni_log(
NI_LOG_INFO,
"Starting hwupload + encoding mode: video resolution %dx%d\n",
512 video_width[0], video_height[0]);
521 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
522 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
523 if (p_hwframe == NULL)
529 ret =
encoder_open(&ctx, &enc_ctx[0], p_enc_api_param, output_total, enc_conf_params, enc_gop_params,
530 NULL, video_width[0], video_height[0], 30, 1, 200000, enc_codec_format,
532 0, xcoderGUID, p_hwframe, 0,
false);
544 video_width[0], video_height[0], output_total, output_fp);
554 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
559 while (!end_of_all_streams &&
563 if (first_frame_uploaded && !eos)
565 p_hwframe =
hwupload_frame(&ctx, &upl_ctx, &sca_ctx, &swin_frame, &in_frame, &scale_frame, pix_fmt,
566 video_width[0], video_height[0], input_fp[0], yuv_buf, &eos);
570 p_in_frame = &in_frame;
572 }
else if (p_hwframe == NULL)
581 for (i = 0; i < output_total; i++)
584 send_rc =
encoder_send_data3(&ctx, &enc_ctx[i], p_in_frame, video_width[0], video_height[0], eos);
585 first_frame_uploaded = 1;
590 end_of_all_streams = 1;
608 if (end_of_all_streams)
612 for (i = 0; i < output_total; i++)
618 video_width[0], video_height[0], output_total, output_fp);
619 for (i = 0; receive_rc >= 0 && i < output_total; i++)
625 output_fp[i] != NULL)
627 fclose(output_fp[i]);
630 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
632 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
642 end_of_all_streams = 0;
647 end_of_all_streams = 1;
653 for (i = 0; i < output_total; i++)
655 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
673 ret =
encoder_open(&ctx, enc_ctx, p_enc_api_param, output_total,
674 enc_conf_params, enc_gop_params, NULL, video_width[0],
675 video_height[0], 30, 1, 200000,
676 enc_codec_format, pix_fmt, 0, xcoderGUID, NULL,
690 video_width[0], video_height[0], output_total, output_fp);
700 ni_log(
NI_LOG_ERROR,
"Failed to get encoded sequence header packet, retcode %d\n", receive_rc);
706 ni_log(
NI_LOG_INFO,
"Starting to encode: video resolution %dx%d\n", video_width[0], video_height[0]);
708 while (!end_of_all_streams &&
713 video_width[i_index], video_height[i_index],
714 pix_fmt, sw_pix_fmt, &eos,
715 enc_ctx[0].session_run_state);
725 video_width[i_index], video_height[i_index],
726 sw_pix_fmt, 0, enc_codec_format);
733 for (i = 0; i < output_total; i++)
739 video_width[i_index], video_height[i_index], eos);
744 &in_frame, eos ? NULL : yuv_buf,
745 video_width[i_index], video_height[i_index],
746 i_index == input_total - 1);
763 for (i = 0; i < output_total; i++)
769 video_width[0], video_height[0], output_total, output_fp);
770 for (i = 0; receive_rc >= 0 && i < output_total; i++)
776 output_fp[i] != NULL)
778 fclose(output_fp[i]);
781 snprintf(jpeg_filename[i],
sizeof(jpeg_filename[i]),
"%s/%" PRIu64
".png",
783 ni_fopen(&(output_fp[i]), jpeg_filename[i],
"wb");
793 end_of_all_streams = 0;
798 end_of_all_streams = 1;
804 for (i = 0; i < output_total; i++)
806 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
817 for (i = 0; i < output_total; i++)
820 output_fp[i] != NULL)
822 fseek(output_fp[i], 0, SEEK_END);
823 if (ftell(output_fp[i]) == 0)
825 fclose(output_fp[i]);
827 remove(jpeg_filename[i]);
837 for (i = 0; i < output_total; i++)
848 for (i = 0; i < output_total; i++)
851 if (output_fp[i] != NULL)
853 fclose(output_fp[i]);
857 for (i = 0; i < input_total; i++)
866 free(p_enc_api_param);