38 #elif __linux__ || __APPLE__
46 "Video transcoding demo application directly using Netint Libxcoder version %s\n"
47 "Usage: ni_xcoder_transcode_filter [options]\n"
50 "-h | --help Show this message.\n"
51 "-v | --version Print version info.\n"
52 "-i | --input (Required) Input file path.\n"
53 "-o | --output (Required) Output file path.\n"
54 " Can be specified multiple (max %d) times\n"
55 " to run multiple encoding instances simultaneously.\n"
56 "-m | --dec-codec (Required) Decoder codec format. Must match the codec of input file.\n"
57 " [a|avc, h|hevc, v|vp9]\n"
58 "-n | --enc-codec (Required) Encoder codec format.\n"
59 " [a|avc, h|hevc, x|av1] (x is in ivf container format)\n"
60 "-l | --loglevel Set loglevel of this application and libxcoder API.\n"
61 " [none, fatal, error, info, debug, trace]\n"
63 "-c | --card Set card index to use.\n"
64 " See `ni_rsrc_mon` for info of cards on system.\n"
66 "-r | --repeat To loop input X times. Must be a positive integer\n"
68 "-d | --decoder-params Decoding params. See \"Decoding Parameters\" chapter in\n"
69 " QuadraIntegration&ProgrammingGuide*.pdf for help.\n"
71 "-e | --encoder-params Encoding params. See \"Encoding Parameters\" chapter in\n"
72 " QuadraIntegration&ProgrammingGuide*.pdf for help.\n"
73 " Can be specified multiple (max %d) times,\n"
74 " must match the number of -o specified.\n"
76 "-g | --encoder-gop Custom GOP for encoding. See \"Custom Gop Structure\" chapter in\n"
77 " QuadraIntegration&ProgrammingGuide*.pdf for help.\n"
78 " gopPresetIdx must be set to 0 to be in effect.\n"
80 "-u | --user-data-sei-passthru (No argument) Enable user data unregistered SEI passthrough when specified\n"
81 "-f | --vf Video filter params. The only supported filters in this demo are:\n"
82 " ni_quadra_scale - supported params [width, height, format]\n"
83 " e.g. ni_quadra_scale=width=1280:height=720:format=yuv420p\n"
84 " ni_quadra_drawbox - supported params [x, y, width, height]\n"
85 " e.g. ni_quadra_drawbox=x=300:y=150:width=600:height=400\n"
90 int main(
int argc,
char *argv[])
97 int o_index = 0, e_index = 0, g_index = 0;
99 int input_width = 0, input_height = 0, output_width = 0, output_height = 0, bit_depth = 8;
100 int dec_codec_format = -1, enc_codec_format = -1;
104 char dec_conf_params[2048] = {0};
105 char filter_conf_params[2048] = {0};
108 int user_data_sei_passthru = 0;
122 void *p_stream_info = NULL;
123 int fps_num = 30, fps_den = 1, bitrate = 200000;
132 int encoder_opened = 0, end_of_all_streams = 0;
139 const char *opt_string =
"hvi:o:m:n:l:c:r:d:e:g:uf:";
140 static struct option long_options[] = {
153 {
"user-data-sei-passthru",
no_argument, NULL,
'u'},
158 while ((opt =
getopt_long(argc, argv, opt_string, long_options, &opt_index)) != -1)
171 strcpy(in_filename,
optarg);
181 for (i = 0; i < o_index; i++)
183 if (0 == strcmp(out_filename[i],
optarg))
191 strcpy(out_filename[o_index],
optarg);
196 for (i = 0; i < strlen(
optarg); i++)
200 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
204 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
208 else if (strcmp(
optarg,
"v") == 0 || strcmp(
optarg,
"vp9") == 0)
215 "Must be one of [a|avc, h|hevc, v|vp9]\n",
optarg);
222 for (i = 0; i < strlen(
optarg); i++)
226 if (strcmp(
optarg,
"a") == 0 || strcmp(
optarg,
"avc") == 0)
230 else if (strcmp(
optarg,
"h") == 0 || strcmp(
optarg,
"hevc") == 0)
234 else if (strcmp(
optarg,
"x") == 0 || strcmp(
optarg,
"av1") == 0)
241 "Must be one of [a|avc, h|hevc, x|av1]\n",
optarg);
255 "Must be one of [none, fatal, error, info, debug, trace]\n",
optarg);
261 xcoderGUID = (int)strtol(
optarg, &n, 10);
262 if (n ==
optarg || *n !=
'\0' || xcoderGUID < 0)
265 "Must be a non-negative integer\n",
optarg);
275 "Must be a positive integer\n",
optarg);
281 strcpy(dec_conf_params,
optarg);
290 strcpy(enc_conf_params[e_index],
optarg);
300 strcpy(enc_gop_params[g_index],
optarg);
304 user_data_sei_passthru = 1;
307 strcpy(filter_conf_params,
optarg);
316 if (!in_filename[0]) {
328 if (dec_codec_format == -1) {
334 if (enc_codec_format == -1) {
347 output_total = o_index;
348 for (i = 0; i < output_total; i++)
350 if (strcmp(out_filename[i],
"null") != 0 &&
351 strcmp(out_filename[i],
"/dev/null") != 0)
353 output_fp[i] = fopen(out_filename[i],
"wb");
364 ni_log(
NI_LOG_INFO,
"Note: Requested NULL output for index %d, no output file will be generated\n", i);
369 if (!p_dec_api_param)
377 if (!p_enc_api_param)
390 "ERROR: Failed to probe input file as H.264, file format not supported!\n");
395 input_width = avc_sps.
width;
396 input_height = avc_sps.
height;
398 bit_depth, input_width, input_height);
405 "ERROR: Failed to probe input file as H.265, file format not supported!\n");
409 input_width = (int)(hevc_sps.
width -
412 input_height = (int)(hevc_sps.
height -
415 ni_log(
NI_LOG_INFO,
"Using probed H.265 source info: %d bits, resolution %dx%d\n",
416 bit_depth, input_width, input_height);
423 "ERROR: Failed to probe input file as VP9, file format not supported!\n");
426 bit_depth = vp9_info.
profile ? 10 : 8;
427 input_width = vp9_info.
width;
428 input_height = vp9_info.
height;
430 "Using probed VP9 source info: %d bits, resolution %dx%d, timebase %u/%u\n",
431 bit_depth, input_width, input_height,
476 for (i = 0; i < output_total; i++)
486 for (i = 0; i < output_total; i++)
492 p_stream_info = &avc_sps;
494 p_stream_info = &hevc_sps;
497 p_stream_info = &vp9_info;
502 output_width = input_width;
503 output_height = input_height;
505 if (filter_conf_params[0])
508 ni_log(
NI_LOG_ERROR,
"Error: filters are only supported when hwframe is enabled (out=hw)\n");
515 ni_log(
NI_LOG_ERROR,
"Error: failed to parse filter parameters: %s\n", filter_conf_params);
528 output_width = scale_params.
width;
529 output_height = scale_params.
height;
531 else if (drawbox_params.
enabled)
559 ni_log(
NI_LOG_INFO,
"Starting to transcode: HWFrames %d, video resolution %dx%d -> %dx%d\n",
560 dec_ctx.
hw_action, input_width, input_height, output_width, output_height);
565 while (!end_of_all_streams &&
571 send_rc =
decoder_send_data(&ctx, &dec_ctx, &in_pkt, input_width, input_height, p_stream_info);
581 input_height, NULL, 0, &rx_size);
582 frame_to_enc = &out_frame;
600 if (p_enc_api_param[0].low_delay_mode <= 0 && encoder_opened)
619 frame_to_enc = &filter_out_frame;
621 else if (drawbox_params.
enabled)
628 frame_to_enc = &filter_out_frame;
638 else if (drawbox_params.
enabled)
643 ret =
encoder_open(enc_ctx, p_enc_api_param, output_total, enc_conf_params,
644 enc_gop_params, &out_frame.
data.
frame, output_width, output_height,
645 fps_num, fps_den, bitrate, enc_codec_format, enc_pix_fmt,
656 for (i = 0; i < output_total; i++)
660 send_rc =
encoder_send_data2(&ctx, &enc_ctx[i], frame_to_enc, &enc_in_frame, output_width, output_height);
698 output_width, output_height, output_total, output_fp);
699 for (i = 0; receive_rc >= 0 && i < output_total; i++)
704 end_of_all_streams = 0;
709 end_of_all_streams = 1;
715 ni_log(
NI_LOG_INFO,
"Decoder stats: received %u frames, fps %.2f, total bytes %u\n",
719 for (i = 0; i < output_total; i++)
721 ni_log(
NI_LOG_INFO,
"Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
744 for (i = 0; i < output_total; i++)
750 for (i = 0; i < output_total; i++)
753 if (output_fp[i] != NULL)
755 fclose(output_fp[i]);
771 free(p_dec_api_param);
772 free(p_enc_api_param);