libxcoder 5.6.0
Loading...
Searching...
No Matches
ni_xcoder_multithread_transcode.c
Go to the documentation of this file.
1/*******************************************************************************
2 *
3 * Copyright (C) 2022 NETINT Technologies
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 *
20 ******************************************************************************/
21
22/*!*****************************************************************************
23 * \file ni_xcoder_multithread_transcode.c
24 *
25 * \brief Multi-threaded video transcoding and filtering demo application
26 * directly using Netint Libxcoder API
27 ******************************************************************************/
28
29#include "ni_generic_utils.h"
30#include "ni_decode_utils.h"
31#include "ni_encode_utils.h"
32#include "ni_filter_utils.h"
33#include "ni_log.h"
34#include "ni_util.h"
35
36#ifdef _WIN32
37#include "ni_getopt.h"
38#elif __linux__ || __APPLE__
39#include <getopt.h>
40#include <unistd.h>
41#endif
42
43static void print_usage(void)
44{
46 "Multi-threaded video transcoding demo application directly using Netint Libxcoder version %s\n"
47 "Usage: ni_xcoder_multithread_transcode [options]\n"
48 "\n"
49 "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"
62 " (Default: info)\n"
63 "-c | --card Set card index to use.\n"
64 " See `ni_rsrc_mon` for info of cards on system.\n"
65 " (Default: 0)\n"
66 "-r | --repeat To loop input X times. Must be a positive integer\n"
67 " (Default: 1)\n"
68 "-d | --decoder-params Decoding params. See \"Decoding Parameters\" chapter in\n"
69 " QuadraIntegration&ProgrammingGuide*.pdf for help.\n"
70 " (Default: \"\")\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"
75 " (Default: \"\")\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"
79 " (Default: \"\")\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"
86 " (Default: \"\")\n"
88}
89
90int main(int argc, char *argv[])
91{
92 int i, ret = 0;
93 ni_demo_context_t ctx = {0};
94 char in_filename[FILE_NAME_LEN] = {0};
95 char out_filename[MAX_OUTPUT_FILES][FILE_NAME_LEN] = {0};
96 FILE *output_fp[MAX_OUTPUT_FILES] = {0};
97 int o_index = 0, e_index = 0, g_index = 0;
98 int output_total = 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;
101 ni_log_level_t log_level;
102 int xcoderGUID = 0;
103 char *n;
104 char dec_conf_params[2048] = {0};
105 char filter_conf_params[2048] = {0};
106 char enc_conf_params[MAX_OUTPUT_FILES][2048] = {0};
107 char enc_gop_params[MAX_OUTPUT_FILES][2048] = {0};
108 int user_data_sei_passthru = 0;
109 ni_h264_sps_t avc_sps = {0};
110 ni_h265_sps_t hevc_sps = {0};
111 ni_vp9_header_info_t vp9_info = {0};
112 ni_xcoder_params_t *p_dec_api_param = NULL;
113 ni_xcoder_params_t *p_enc_api_param = NULL;
114 ni_session_context_t dec_ctx = {0};
116 ni_session_context_t sca_ctx = {0};
117 ni_session_context_t crop_ctx = {0};
118 ni_session_context_t pad_ctx = {0};
119 ni_session_context_t ovly_ctx = {0};
120 ni_session_context_t fmt_ctx = {0};
121 void *p_stream_info = NULL;
122 int fps_num = 30, fps_den = 1, bitrate = 200000;
123 ni_frame_t *p_ni_frame = NULL;
124 niFrameSurface1_t *p_hwframe = NULL;
125 ni_scale_params_t scale_params = {0};
126 ni_drawbox_params_t drawbox_params = {0};
127 ni_test_frame_list_t frame_list = {0};
128 ni_pthread_t dec_send_tid, dec_recv_tid, enc_send_tid, enc_recv_tid;
133 ni_pix_fmt_t enc_pix_fmt = NI_PIX_FMT_YUV420P;
134
135 int opt;
136 int opt_index;
137 const char *opt_string = "hvi:o:m:n:l:c:r:d:e:g:uf:";
138 static struct option long_options[] = {
139 {"help", no_argument, NULL, 'h'},
140 {"version", no_argument, NULL, 'v'},
141 {"input", required_argument, NULL, 'i'},
142 {"output", required_argument, NULL, 'o'},
143 {"dec-codec", required_argument, NULL, 'n'},
144 {"enc-codec", required_argument, NULL, 'm'},
145 {"loglevel", required_argument, NULL, 'l'},
146 {"card", required_argument, NULL, 'c'},
147 {"repeat", required_argument, NULL, 'r'},
148 {"decoder-params", required_argument, NULL, 'd'},
149 {"encoder-params", required_argument, NULL, 'e'},
150 {"encoder-gop", required_argument, NULL, 'g'},
151 {"user-data-sei-passthru", no_argument, NULL, 'u'},
152 {"vf", required_argument, NULL, 'f'},
153 {NULL, 0, NULL, 0},
154 };
155
156 while ((opt = getopt_long(argc, argv, opt_string, long_options, &opt_index)) != -1)
157 {
158 switch (opt)
159 {
160 case 'h':
161 print_usage();
162 ret = 0;
163 goto end;
164 case 'v':
166 ret = 0;
167 goto end;
168 case 'i':
169 ni_strcpy(in_filename, FILE_NAME_LEN, optarg);
170 break;
171 case 'o':
172 if (o_index == MAX_OUTPUT_FILES)
173 {
174 ni_log(NI_LOG_ERROR, "Error: number of output files cannot exceed %d\n", MAX_OUTPUT_FILES);
175 ret = -1;
176 goto end;
177 }
178
179 for (i = 0; i < o_index; i++)
180 {
181 if (0 == strcmp(out_filename[i], optarg))
182 {
183 ni_log(NI_LOG_ERROR, "Error: output file names must be unique: %s\n", optarg);
184 ret = -1;
185 goto end;
186 }
187 }
188
189 ni_strcpy(out_filename[o_index], FILE_NAME_LEN, optarg);
190 o_index++;
191 break;
192 case 'm':
193 // Accept both upper and lower case
194 for (i = 0; i < strlen(optarg); i++)
195 {
196 optarg[i] = (char)tolower((unsigned char)optarg[i]);
197 }
198 if (strcmp(optarg, "a") == 0 || strcmp(optarg, "avc") == 0)
199 {
200 dec_codec_format = NI_CODEC_FORMAT_H264;
201 }
202 else if (strcmp(optarg, "h") == 0 || strcmp(optarg, "hevc") == 0)
203 {
204 dec_codec_format = NI_CODEC_FORMAT_H265;
205 }
206 else if (strcmp(optarg, "v") == 0 || strcmp(optarg, "vp9") == 0)
207 {
208 dec_codec_format = NI_CODEC_FORMAT_VP9;
209 }
210 else
211 {
212 ni_log(NI_LOG_ERROR, "Error: Invalid value \"%s\" for -m | --dec-codec option\n"
213 "Must be one of [a|avc, h|hevc, v|vp9]\n", optarg);
214 ret = -1;
215 goto end;
216 }
217 break;
218 case 'n':
219 // Accept both upper and lower case
220 for (i = 0; i < strlen(optarg); i++)
221 {
222 optarg[i] = (char)tolower((unsigned char)optarg[i]);
223 }
224 if (strcmp(optarg, "a") == 0 || strcmp(optarg, "avc") == 0)
225 {
226 enc_codec_format = NI_CODEC_FORMAT_H264;
227 }
228 else if (strcmp(optarg, "h") == 0 || strcmp(optarg, "hevc") == 0)
229 {
230 enc_codec_format = NI_CODEC_FORMAT_H265;
231 }
232 else if (strcmp(optarg, "x") == 0 || strcmp(optarg, "av1") == 0)
233 {
234 enc_codec_format = NI_CODEC_FORMAT_AV1;
235 }
236 else
237 {
238 ni_log(NI_LOG_ERROR, "Error: Invalid value \"%s\" for -n | --enc-codec option\n"
239 "Must be one of [a|avc, h|hevc, x|av1]\n", optarg);
240 ret = -1;
241 goto end;
242 }
243 break;
244 case 'l':
245 log_level = arg_to_ni_log_level(optarg);
246 if (log_level != NI_LOG_INVALID)
247 {
248 ni_log_set_level(log_level);
249 }
250 else
251 {
252 ni_log(NI_LOG_ERROR, "Error: Invalid value \"%s\" for -l | --loglevel option\n"
253 "Must be one of [none, fatal, error, info, debug, trace]\n", optarg);
254 ret = -1;
255 goto end;
256 }
257 break;
258 case 'c':
259 xcoderGUID = (int)strtol(optarg, &n, 10);
260 if (n == optarg || *n != '\0' || xcoderGUID < 0)
261 {
262 ni_log(NI_LOG_ERROR, "Error: Invalid value \"%s\" for -c | --card option\n"
263 "Must be a non-negative integer\n", optarg);
264 ret = -1;
265 goto end;
266 }
267 break;
268 case 'r':
269 ctx.loops_left = strtol(optarg, &n, 10);
270 if (n == optarg || *n != '\0' || !(ctx.loops_left >= 1))
271 {
272 ni_log(NI_LOG_ERROR, "Error: Invalid value \"%s\" for -r | --repeat option\n"
273 "Must be a positive integer\n", optarg);
274 ret = -1;
275 goto end;
276 }
277 break;
278 case 'd':
279 ni_strcpy(dec_conf_params, sizeof(dec_conf_params), optarg);
280 break;
281 case 'e':
282 if (e_index == MAX_OUTPUT_FILES)
283 {
284 ni_log(NI_LOG_ERROR, "Error: number of encoder config cannot exceed %d\n", MAX_OUTPUT_FILES);
285 ret = -1;
286 goto end;
287 }
288 ni_strcpy(enc_conf_params[e_index], sizeof(enc_conf_params[e_index]), optarg);
289 e_index++;
290 break;
291 case 'g':
292 if (g_index == MAX_OUTPUT_FILES)
293 {
294 ni_log(NI_LOG_ERROR, "Error: number of encoder gop settings cannot exceed %d\n", MAX_OUTPUT_FILES);
295 ret = -1;
296 goto end;
297 }
298 ni_strcpy(enc_gop_params[g_index], sizeof(enc_gop_params[g_index]), optarg);
299 g_index++;
300 break;
301 case 'u':
302 user_data_sei_passthru = 1;
303 break;
304 case 'f':
305 ni_strcpy(filter_conf_params, sizeof(filter_conf_params), optarg);
306 break;
307 default:
308 print_usage();
309 ret = -1;
310 goto end;
311 }
312 }
313
314 if (!in_filename[0]) {
315 ni_log(NI_LOG_ERROR, "Error: Missing input file argument (-i | --input)\n");
316 ret = -1;
317 goto end;
318 }
319
320 if (o_index == 0) {
321 ni_log(NI_LOG_ERROR, "Error: Missing output file argument (-o | --output)\n");
322 ret = -1;
323 goto end;
324 }
325
326 if (dec_codec_format == -1) {
327 ni_log(NI_LOG_ERROR, "Error: Missing decoder codec argument (-m | --dec-codec)\n");
328 ret = -1;
329 goto end;
330 }
331
332 if (enc_codec_format == -1) {
333 ni_log(NI_LOG_ERROR, "Error: Missing encoder codec argument (-n | --enc-codec)\n");
334 ret = -1;
335 goto end;
336 }
337
338 ret = read_and_cache_file(&ctx, in_filename);
339 if (ret)
340 {
341 ni_log(NI_LOG_ERROR, "Error: Read input file failure\n");
342 goto end;
343 }
344
345 output_total = o_index;
346 for (i = 0; i < output_total; i++)
347 {
348 if (strcmp(out_filename[i], "null") != 0 &&
349 strcmp(out_filename[i], "/dev/null") != 0)
350 {
351 output_fp[i] = NULL;
352 ni_fopen(&(output_fp[i]), out_filename[i], "wb");
353 if (!output_fp[i])
354 {
355 ni_log(NI_LOG_ERROR, "Error: Failed to open %s\n", out_filename[i]);
356 ret = -1;
357 goto end;
358 }
359 ni_log(NI_LOG_INFO, "Opened output file: %s\n", out_filename[i]);
360 } else
361 {
362 output_fp[i] = NULL;
363 ni_log(NI_LOG_INFO, "Note: Requested NULL output for index %d, no output file will be generated\n", i);
364 }
365 }
366
367 p_dec_api_param = malloc(sizeof(ni_xcoder_params_t));
368 if (!p_dec_api_param)
369 {
370 ni_log(NI_LOG_ERROR, "Error: failed to allocate p_dec_api_param\n");
371 ret = -1;
372 goto end;
373 }
374
375 p_enc_api_param = calloc(output_total, sizeof(ni_xcoder_params_t));
376 if (!p_enc_api_param)
377 {
378 ni_log(NI_LOG_ERROR, "Error: Failed to allocate memory for p_enc_api_param\n");
379 ret = -1;
380 goto end;
381 }
382
383 if (dec_codec_format == NI_CODEC_FORMAT_H264)
384 {
385 ret = probe_h264_stream_info(&ctx, &avc_sps);
386 if (ret)
387 {
389 "ERROR: Failed to probe input file as H.264, file format not supported!\n");
390 goto end;
391 }
392
393 bit_depth = avc_sps.bit_depth_luma;
394 input_width = avc_sps.width;
395 input_height = avc_sps.height;
396 ni_log(NI_LOG_DEBUG, "Using probed H.264 source info: %d bits, resolution %dx%d\n",
397 bit_depth, input_width, input_height);
398 } else if (dec_codec_format == NI_CODEC_FORMAT_H265)
399 {
400 ret = probe_h265_stream_info(&ctx, &hevc_sps);
401 if (ret)
402 {
404 "ERROR: Failed to probe input file as H.265, file format not supported!\n");
405 goto end;
406 }
407 bit_depth = hevc_sps.bit_depth_chroma;
408 input_width = (int)(hevc_sps.width -
409 (hevc_sps.pic_conf_win.left_offset +
410 hevc_sps.pic_conf_win.right_offset));
411 input_height = (int)(hevc_sps.height -
412 (hevc_sps.pic_conf_win.top_offset +
413 hevc_sps.pic_conf_win.bottom_offset));
414 ni_log(NI_LOG_INFO, "Using probed H.265 source info: %d bits, resolution %dx%d\n",
415 bit_depth, input_width, input_height);
416 } else if (dec_codec_format == NI_CODEC_FORMAT_VP9)
417 {
418 ret = probe_vp9_stream_info(&ctx, &vp9_info);
419 if (ret)
420 {
422 "ERROR: Failed to probe input file as VP9, file format not supported!\n");
423 goto end;
424 }
425 bit_depth = vp9_info.profile ? 10 : 8;
426 input_width = vp9_info.width;
427 input_height = vp9_info.height;
429 "Using probed VP9 source info: %d bits, resolution %dx%d, timebase %u/%u\n",
430 bit_depth, input_width, input_height,
431 vp9_info.timebase.den, vp9_info.timebase.num);
432 }
433
434 // set up decoder config params with some hard-coded values
435 ret = ni_decoder_init_default_params(p_dec_api_param, 25, 1, 200000, input_width, input_height);
436 if (ret)
437 {
438 ni_log(NI_LOG_ERROR, "Error: Failed to init default decoder config\n");
439 goto end;
440 }
441
442 ret = ni_device_session_context_init(&dec_ctx);
443 if (ret)
444 {
445 ni_log(NI_LOG_ERROR, "Error: Failed to init decoder context\n");
446 goto end;
447 }
448
449 dec_ctx.codec_format = dec_codec_format;
450 dec_ctx.src_bit_depth = bit_depth;
451 dec_ctx.bit_depth_factor = 1;
452 if (10 == dec_ctx.src_bit_depth)
453 {
454 dec_ctx.bit_depth_factor = 2;
455 }
456 if ((NI_CODEC_FORMAT_H264 == dec_codec_format) || (NI_CODEC_FORMAT_H265 == dec_codec_format))
457 dec_ctx.enable_user_data_sei_passthru = user_data_sei_passthru;
458
459 // check and set ni_decoder_params from --xcoder-params
460 ret = ni_retrieve_decoder_params(dec_conf_params, p_dec_api_param, &dec_ctx);
461 if (ret)
462 {
463 ni_log(NI_LOG_ERROR, "Error: decoder config params parsing error\n");
464 goto end;
465 }
466
467 // Decode, use all the parameters specified by user
468 ret = decoder_open_session(&dec_ctx, xcoderGUID, p_dec_api_param);
469 if (ret)
470 {
471 ni_log(NI_LOG_ERROR, "Error: Failed to open decoder session\n");
472 goto end;
473 }
474
475 for (i = 0; i < output_total; i++)
476 {
477 if (ni_device_session_context_init(&enc_ctx[i]) < 0)
478 {
479 ni_log(NI_LOG_ERROR, "Error: init encoder %d context error\n", i);
480 ret = -1;
481 goto end;
482 }
483 }
484
485 for (i = 0; i < output_total; i++)
486 {
487 enc_ctx[i].codec_format = enc_codec_format;
488 }
489
490 if (dec_codec_format == NI_CODEC_FORMAT_H264)
491 p_stream_info = &avc_sps;
492 else if (dec_codec_format == NI_CODEC_FORMAT_H265)
493 p_stream_info = &hevc_sps;
494 else if (dec_codec_format == NI_CODEC_FORMAT_VP9)
495 {
496 p_stream_info = &vp9_info;
497 fps_num = vp9_info.timebase.den;
498 fps_den = vp9_info.timebase.num;
499 }
500
501 output_width = input_width;
502 output_height = input_height;
503
504 if (filter_conf_params[0])
505 {
506 if (!dec_ctx.hw_action) {
507 ni_log(NI_LOG_ERROR, "Error: filters are only supported when hwframe is enabled (out=hw)\n");
508 ret = -1;
509 goto end;
510 }
511 ret = retrieve_filter_params(filter_conf_params, &scale_params, &drawbox_params);
512 if (ret)
513 {
514 ni_log(NI_LOG_ERROR, "Error: failed to parse filter parameters: %s\n", filter_conf_params);
515 goto end;
516 }
517 }
518
519 if (scale_params.enabled)
520 {
521 ret = ni_device_session_context_init(&sca_ctx);
522 if (ret)
523 {
524 ni_log(NI_LOG_ERROR, "Error: Failed to init scale context\n");
525 goto end;
526 }
527 output_width = scale_params.width;
528 output_height = scale_params.height;
529 }
530 else if (drawbox_params.enabled)
531 {
532 ret = ni_device_session_context_init(&crop_ctx);
533 if (ret)
534 {
535 ni_log(NI_LOG_ERROR, "Error: Failed to init crop context\n");
536 goto end;
537 }
538 ret = ni_device_session_context_init(&pad_ctx);
539 if (ret)
540 {
541 ni_log(NI_LOG_ERROR, "Error: Failed to init pad context\n");
542 goto end;
543 }
544 ret = ni_device_session_context_init(&ovly_ctx);
545 if (ret)
546 {
547 ni_log(NI_LOG_ERROR, "Error: Failed to init overlay context\n");
548 goto end;
549 }
550 ret = ni_device_session_context_init(&fmt_ctx);
551 if (ret)
552 {
553 ni_log(NI_LOG_ERROR, "Error: Failed to init format context\n");
554 goto end;
555 }
556 }
557
558 ni_log(NI_LOG_INFO, "Starting to transcode: HWFrames %d, video resolution %dx%d -> %dx%d\n",
559 dec_ctx.hw_action, input_width, input_height, output_width, output_height);
560
562
563 // init and create decoding thread
564 dec_send_param.p_ctx = &ctx;
565 dec_send_param.p_dec_ctx = &dec_ctx;
566 dec_send_param.input_width = input_width;
567 dec_send_param.input_height = input_height;
568 dec_send_param.p_stream_info = p_stream_info;
569 dec_send_param.frame_list = &frame_list;
570
571 dec_recv_param.p_ctx = &ctx;
572 dec_recv_param.p_dec_ctx = &dec_ctx;
573 dec_recv_param.p_sca_ctx = &sca_ctx;
574 dec_recv_param.p_crop_ctx = &crop_ctx;
575 dec_recv_param.p_pad_ctx = &pad_ctx;
576 dec_recv_param.p_ovly_ctx = &ovly_ctx;
577 dec_recv_param.p_fmt_ctx = &fmt_ctx;
578 dec_recv_param.xcoderGUID = xcoderGUID;
579 dec_recv_param.input_width = input_width;
580 dec_recv_param.input_height = input_height;
581 dec_recv_param.frame_list = &frame_list;
582 dec_recv_param.scale_params = &scale_params;
583 dec_recv_param.drawbox_params = &drawbox_params;
584
585 if (ni_pthread_create(&dec_send_tid, NULL, decoder_send_thread,
587 {
589 "Error: create decoder send thread failed in transcode "
590 "mode\n");
591 return -1;
592 }
593 if (ni_pthread_create(&dec_recv_tid, NULL, decoder_receive_thread,
595 {
597 "Error: create decoder receive thread failed in "
598 "transcode mode\n");
599 return -1;
600 }
601
602 // polling the first received decoded frame
603 while (frame_list_is_empty(&frame_list) && !ctx.end_all_threads)
604 {
605 ni_usleep(100);
606 }
607
608 if (!ctx.end_all_threads)
609 {
610 p_ni_frame = &frame_list.frames[frame_list.head].data.frame;
611 p_hwframe = dec_ctx.hw_action == NI_CODEC_HW_ENABLE ?
612 (niFrameSurface1_t *)p_ni_frame->p_data[3] : NULL;
613
614 if (scale_params.enabled)
615 enc_pix_fmt = gc620_to_ni_pix_fmt(scale_params.format);
616 else if (drawbox_params.enabled)
617 enc_pix_fmt = NI_PIX_FMT_YUV420P;
618 else
619 enc_pix_fmt = dec_ctx.pixel_format;
620
621 ret = encoder_open(&ctx, enc_ctx, p_enc_api_param, output_total, enc_conf_params, enc_gop_params,
622 p_ni_frame, output_width, output_height, fps_num, fps_den, bitrate,
623 enc_codec_format, enc_pix_fmt, p_ni_frame->aspect_ratio_idc,
624 xcoderGUID, p_hwframe, 1, false);
625 if (ret != 0)
626 {
627 goto end;
628 }
629
630 // init and create encoding thread
631 enc_send_param.p_ctx = &ctx;
632 enc_send_param.p_enc_ctx = enc_ctx;
633 enc_send_param.output_width = output_width;
634 enc_send_param.output_height = output_height;
635 enc_send_param.output_total = output_total;
636 enc_send_param.frame_list = &frame_list;
637
638 enc_recv_param.p_ctx = &ctx;
639 enc_recv_param.p_enc_ctx = enc_ctx;
640 enc_recv_param.output_width = output_width;
641 enc_recv_param.output_height = output_height;
642 enc_recv_param.p_file = output_fp;
643 enc_recv_param.output_total = output_total;
644 enc_recv_param.frame_list = &frame_list;
645
646 if (ni_pthread_create(&enc_send_tid, NULL, encoder_send_thread,
648 {
649 ni_log(NI_LOG_ERROR, "Error: create encoder send thread failed "
650 "in transcode mode\n");
651 return -1;
652 }
653 if (ni_pthread_create(&enc_recv_tid, NULL, encoder_receive_thread,
655 {
656 ni_log(NI_LOG_ERROR, "Error: create encoder recieve thread "
657 "failed in transcode mode\n");
658 return -1;
659 }
660
661 ni_pthread_join(enc_send_tid, NULL);
662 ni_pthread_join(enc_recv_tid, NULL);
663 }
664 ni_pthread_join(dec_send_tid, NULL);
665 ni_pthread_join(dec_recv_tid, NULL);
666
667 decoder_stat_report_and_close(&ctx, &dec_ctx);
668 encoder_stat_report_and_close(&ctx, enc_ctx, output_total);
669
670end:
671 hwframe_list_release(&frame_list);
672
674 for (i = 0; i < output_total; i++)
675 {
677 if (output_fp[i] != NULL)
678 {
679 fclose(output_fp[i]);
680 }
681 }
682 if (scale_params.enabled)
683 {
685 }
686 if (drawbox_params.enabled)
687 {
692 }
693
694 free(ctx.file_cache);
695
696 for(i = 0; i < MAX_OUTPUT_FILES; ++i)
697 {
698 free(ctx.enc_pts_queue[i]);
699 ctx.enc_pts_queue[i] = NULL;
700 }
701
702 free(p_dec_api_param);
703 free(p_enc_api_param);
704
705 return ret;
706}
int main()
Definition client.cpp:51
void decoder_stat_report_and_close(ni_demo_context_t *p_ctx, ni_session_context_t *p_dec_ctx)
int probe_h264_stream_info(ni_demo_context_t *p_ctx, ni_h264_sps_t *sps)
void * decoder_send_thread(void *args)
int decoder_open_session(ni_session_context_t *p_dec_ctx, int iXcoderGUID, ni_xcoder_params_t *p_dec_params)
decoder session open
int probe_h265_stream_info(ni_demo_context_t *p_ctx, ni_h265_sps_t *sps)
void * decoder_receive_thread(void *args)
int probe_vp9_stream_info(ni_demo_context_t *p_ctx, ni_vp9_header_info_t *vp9_info)
#define NI_XCODER_REVISION
Definition ni_defs.h:98
ni_retcode_t ni_device_session_context_init(ni_session_context_t *p_ctx)
Initialize already allocated session context to a known state.
ni_retcode_t ni_decoder_init_default_params(ni_xcoder_params_t *p_param, int fps_num, int fps_denom, long bit_rate, int width, int height)
Initialize default decoder parameters.
void ni_device_session_context_clear(ni_session_context_t *p_ctx)
Clear already allocated session context.
@ NI_CODEC_HW_ENABLE
@ NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_VP9
ni_pix_fmt_t
@ NI_PIX_FMT_YUV420P
int encoder_open(ni_demo_context_t *p_ctx, ni_session_context_t *enc_ctx_list, ni_xcoder_params_t *p_api_param_list, int output_total, char p_enc_conf_params[][2048], char p_enc_conf_gop[][2048], ni_frame_t *p_ni_frame, int width, int height, int fps_num, int fps_den, int bitrate, int codec_format, ni_pix_fmt_t pix_fmt, int aspect_ratio_idc, int xcoder_guid, niFrameSurface1_t *p_surface, int multi_thread, bool check_zerocopy)
void * encoder_receive_thread(void *args)
void encoder_stat_report_and_close(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx_list, int output_total)
void * encoder_send_thread(void *args)
int retrieve_filter_params(char filter_params[], ni_scale_params_t *scale_params, ni_drawbox_params_t *drawbox_params)
ni_pix_fmt_t gc620_to_ni_pix_fmt(int pix_fmt)
bool frame_list_is_empty(ni_test_frame_list_t *list)
int read_and_cache_file(ni_demo_context_t *ctx, char *filename)
void hwframe_list_release(ni_test_frame_list_t *list)
void print_version(void)
#define MAX_OUTPUT_FILES
#define FILE_NAME_LEN
char * optarg
Definition ni_getopt.c:33
int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition ni_getopt.c:99
Implementation of getopt() and getopt_long() for Windows environment.
#define no_argument
Definition ni_getopt.h:85
#define required_argument
Definition ni_getopt.h:86
ni_log_level_t arg_to_ni_log_level(const char *arg_str)
Convert terminal arg string to ni_log_level_t.
Definition ni_log.c:262
void ni_log_set_level(ni_log_level_t level)
Set ni_log_level.
Definition ni_log.c:202
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition ni_log.c:183
Logging definitions.
ni_log_level_t
Definition ni_log.h:58
@ NI_LOG_DEBUG
Definition ni_log.h:64
@ NI_LOG_ERROR
Definition ni_log.h:62
@ NI_LOG_INFO
Definition ni_log.h:63
@ NI_LOG_INVALID
Definition ni_log.h:59
int ni_pthread_create(ni_pthread_t *thread, const ni_pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
create a new thread
Definition ni_util.c:4750
ni_retcode_t ni_fopen(FILE **fp, const char *filename, const char *mode)
Definition ni_util.c:983
ni_retcode_t ni_strcpy(char *dest, size_t dmax, const char *src)
Definition ni_util.c:453
int ni_retrieve_decoder_params(char xcoderParams[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
retrieve decoder config parameter values from –decoder-params
Definition ni_util.c:4560
void ni_usleep(int64_t usec)
Definition ni_util.c:362
uint64_t ni_gettime_ns(void)
Definition ni_util.c:2622
int ni_pthread_join(ni_pthread_t thread, void **value_ptr)
join with a terminated thread
Definition ni_util.c:4777
Utility definitions.
ni_pts_queue * enc_pts_queue[MAX_OUTPUT_FILES]
uint8_t aspect_ratio_idc
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
int bit_depth_luma
bit_depth_luma_minus8 + 8
ni_h265_window_t pic_conf_win
unsigned int left_offset
unsigned int bottom_offset
unsigned int right_offset
unsigned int top_offset
union _ni_session_data_io::@19 data
ni_session_data_io_t frames[NI_MAX_BUFFERED_FRAME]
struct _ni_vp9_header_info::@3 timebase
ni_demo_context_t * p_ctx
ni_drawbox_params_t * drawbox_params
ni_session_context_t * p_sca_ctx
ni_scale_params_t * scale_params
ni_session_context_t * p_pad_ctx
ni_session_context_t * p_dec_ctx
ni_session_context_t * p_ovly_ctx
ni_session_context_t * p_crop_ctx
ni_test_frame_list_t * frame_list
ni_session_context_t * p_fmt_ctx
ni_demo_context_t * p_ctx
ni_session_context_t * p_dec_ctx
ni_test_frame_list_t * frame_list
ni_session_context_t * p_enc_ctx
ni_demo_context_t * p_ctx
ni_test_frame_list_t * frame_list
ni_session_context_t * p_enc_ctx
ni_demo_context_t * p_ctx
ni_test_frame_list_t * frame_list