35 #define _POSIX_C_SOURCE 200809L
38 #include <sys/types.h>
48 #define MAX_YUV_FRAME_SIZE (7680 * 4320 * 3 / 2)
49 #define MAX_ABGR_FRAME_SIZE (7680 * 4320 * 4)
51 #define FILE_NAME_LEN 256
88 fprintf(stderr,
"Error: unrecognized argument for %s, \"%s\"\n", arg_name,
104 uint8_t *tmp_dst = p_dst;
106 "read_next_chunk_from_file:p_dst %p len %u totalSize %llu left %llu\n",
108 int to_copy = to_read;
109 unsigned long tmpFileSize = to_read;
117 lseek(fd, 0, SEEK_SET);
128 int one_read_size = read(fd, tmp_dst, to_copy);
129 if (one_read_size == -1)
131 fprintf(stderr,
"Error: reading file, quit! left-to-read %lu\n",
133 fprintf(stderr,
"Error: input file read error\n");
155 if (stat(filename, &info) < 0)
157 fprintf(stderr,
"Can't stat %s\n", filename);
162 if (info.st_size <= 0)
164 fprintf(stderr,
"File %s is empty\n", filename);
168 *bytes_read = info.st_size;
192 fprintf(stderr,
"Recycle failed\n");
219 uint8_t **p_yuv420p_frame,
ni_frame_t *p_in_frame,
220 int input_video_width,
int input_video_height,
221 unsigned long *bytes_sent,
int *input_exhausted)
242 frame_size = input_video_height * input_video_width * 3 / 2;
249 *input_exhausted = 1;
261 "dst height aligned = %d/%d/%d \n",
262 dst_stride[0], dst_stride[1], dst_stride[2],
263 input_video_height, dst_height[0], dst_height[1],
267 src_stride[1] = src_stride[2] = src_stride[0] / 2;
269 src_height[0] = input_video_height;
270 src_height[1] = src_height[0] / 2;
271 src_height[2] = src_height[1];
274 p_src[1] = tmp_buf + src_stride[0] * src_height[0];
275 p_src[2] = p_src[1] + src_stride[1] * src_height[1];
277 alignedh = (input_video_height + 1) & ~1;
279 Ysize = dst_stride[0] * alignedh;
280 Usize = dst_stride[1] * alignedh / 2;
281 Vsize = dst_stride[2] * alignedh / 2;
283 total_size = Ysize + Usize + Vsize;
284 total_size = ((total_size + 4095) & ~4095) + 4096;
286 if (*p_yuv420p_frame == NULL)
290 fprintf(stderr,
"Can't alloc memory\n");
294 *p_yuv420p_frame = p_buffer;
297 p_dst[0] = *p_yuv420p_frame;
298 p_dst[1] = *p_yuv420p_frame + Ysize;
299 p_dst[2] = *p_yuv420p_frame + Ysize + Usize;
303 0, 0, dst_stride, dst_height, src_stride, src_height);
308 fprintf(stderr,
"Error: failed ni_uploader_p2p_test_send()\n");
313 *bytes_sent = total_size;
337 uint8_t **p_rgba_frame,
ni_frame_t *p_in_frame,
338 int input_video_width,
int input_video_height,
339 unsigned long *bytes_sent,
int *input_exhausted)
343 uint8_t *p_src,*p_dst;
354 frame_size = input_video_width * input_video_height * 4;
361 *input_exhausted = 1;
368 linewidth = input_video_width * 4;
373 if (*p_rgba_frame == NULL)
377 fprintf(stderr,
"Can't alloc memory\n");
381 *p_rgba_frame = p_buffer;
385 p_dst = *p_rgba_frame;
387 for (row = 0; row < input_video_height; row++)
389 memcpy(p_dst, p_src, linewidth);
397 fprintf(stderr,
"Error: failed ni_uploader_p2p_test_send()\n");
401 *bytes_sent = total_size;
420 int input_video_height,
ni_frame_t p2p_frame[])
429 p_in_frame = &p2p_frame[i];
438 p_in_frame, input_video_width, input_video_height,
441 fprintf(stderr,
"Error: could not allocate hw frame buffer!");
451 fprintf(stderr,
"Error: failed ni_device_session_acquire()\n");
485 static int started = 0;
525 "Error: failed ni_device_session_write() for encoder\n");
566 unsigned long long *total_bytes_received,
575 static int received_stream_header = 0;
595 fprintf(stderr,
"Error: malloc packet failed, ret = %d!\n", rc);
603 if (!received_stream_header)
611 if (fwrite((uint8_t *)p_out_pkt->
p_data + meta_size,
612 p_out_pkt->
data_len - meta_size, 1, p_file) != 1)
614 fprintf(stderr,
"Error: writing data %u bytes error!\n",
616 fprintf(stderr,
"Error: ferror rc = %d\n", ferror(p_file));
620 *total_bytes_received += rx_size;
622 received_stream_header = 1;
625 fprintf(stderr,
"Error: reading header %d\n", rc);
636 printf(
"[R] Got:%d Packets= %u fps=%u Total bytes %llu\n",
638 *total_bytes_received);
657 ni_log2(p_enc_ctx,
NI_LOG_DEBUG,
"encoder_receive_data: received data size=%d\n", rx_size);
659 if (rx_size > meta_size)
661 if (fwrite((uint8_t *)p_out_pkt->
p_data + meta_size,
662 p_out_pkt->
data_len - meta_size, 1, p_file) != 1)
664 fprintf(stderr,
"Error: writing data %u bytes error!\n",
666 fprintf(stderr,
"Error: ferror rc = %d\n", ferror(p_file));
669 *total_bytes_received += rx_size - meta_size;
673 }
else if (rx_size != 0)
675 fprintf(stderr,
"Error: received %d bytes, <= metadata size %d!\n",
678 }
else if (!end_flag &&
693 printf(
"[R] Got:%d Packets= %u fps=%u Total bytes %llu\n", rx_size,
695 *total_bytes_received);
700 printf(
"Encoder Receiving done\n");
702 }
else if (0 == rx_size)
747 p_enc_ctx->
hw_id = iXcoderGUID;
759 fprintf(stderr,
"Error: encoder open session failure\n");
762 printf(
"Encoder device %d session open successful\n", iXcoderGUID);
779 int width,
int height)
791 p_upl_ctx->
hw_id = *iXcoderGUID;
803 fprintf(stderr,
"Error: uploader_open_session failure!\n");
807 printf(
"Uploader device %d session opened successfully\n",
809 *iXcoderGUID = p_upl_ctx->
hw_id;
816 fprintf(stderr,
"Error: Can't create frame pool\n");
820 printf(
"Uploader device %d configured successfully\n", *iXcoderGUID);
835 printf(
"Video encoder/P2P application directly using Netint "
836 "Libxcoder release v%s\n"
837 "Usage: xcoderp2p [options]\n"
840 "--------------------------------------------------------------------------------\n"
841 " -h | --help Show help.\n"
842 " -v | --version Print version info.\n"
843 " -l | --loglevel Set loglevel of libxcoder API.\n"
844 " [none, fatal, error, info, debug, trace]\n"
846 " -c | --card Set card index to use.\n"
847 " See `ni_rsrc_mon` for cards on system.\n"
849 " -i | --input Input file path.\n"
850 " -r | --repeat (Positive integer) to Repeat input X times "
852 " test. (Default: 1)\n"
853 " -s | --size Resolution of input file in format "
855 " (eg. '1920x1080')\n"
856 " -m | --mode Input to output codec processing mode in "
858 " INTYPE2OUTTYPE. [p2a, p2h, r2a, r2h]\n"
859 " Type notation: p=P2P, a=AVC, h=HEVC, r=ABGR\n"
860 " -o | --output Output file path.\n",
879 char *output_filename,
int *iXcoderGUID,
int *arg_width,
880 int *arg_height,
int *dst_codec_format)
883 char mode_description[128];
890 static const char *opt_string =
"hvl:c:i:s:m:o:r:";
891 static const struct option long_options[] = {
904 while ((opt =
getopt_long(argc, argv, opt_string, long_options,
913 printf(
"Release ver: %s\n"
930 strcpy(xcoderGUID,
optarg);
931 *iXcoderGUID = (int)strtol(
optarg, &n, 10);
937 strcpy(input_filename,
optarg);
940 *arg_width = (int)strtol(
optarg, &n, 10);
941 *arg_height =
atoi(n + 1);
942 if ((*n !=
'x') || (!*arg_width || !*arg_height))
946 if (!(strlen(
optarg) == 3))
950 for (i = 0; i < strlen(
optarg); i++)
953 if (strcmp(
optarg,
"p2a") != 0 && strcmp(
optarg,
"p2h") != 0 &&
954 strcmp(
optarg,
"r2a") != 0 && strcmp(
optarg,
"r2h") != 0)
958 sprintf(mode_description,
"P2P + Encoding");
965 strcat(mode_description,
" to AVC");
971 strcat(mode_description,
" to HEVC");
973 printf(
"%s...\n", mode_description);
977 strcpy(output_filename,
optarg);
991 if (!input_filename[0])
993 printf(
"Error: missing argument for -i | --input\n");
997 if (!output_filename[0])
999 printf(
"Error: missing argument for -o | --output\n");
1008 unsigned long total_bytes_sent;
1009 unsigned long long total_bytes_received;
1010 int input_video_width;
1011 int input_video_height;
1012 int iXcoderGUID = 0;
1015 int input_exhausted = 0;
1016 int num_post_recycled = 0;
1017 int dst_codec_format = 0;
1021 int need_to_resend = 0;
1022 int render_index = 0;
1023 int encode_index = -1;
1024 FILE *p_file = NULL;
1030 int input_file_fd = -1;
1032 parse_arguments(argc, argv, input_filename, output_filename, &iXcoderGUID,
1033 &arg_width, &arg_height, &dst_codec_format);
1044 if (strcmp(output_filename,
"null") != 0)
1046 p_file = fopen(output_filename,
"wb");
1049 fprintf(stderr,
"Error: cannot open %s\n", output_filename);
1054 printf(
"SUCCESS: Opened output file: %s\n", output_filename);
1058 fprintf(stderr,
"Error: init encoder context error\n");
1064 fprintf(stderr,
"Error: init uploader context error\n");
1068 total_bytes_received = 0;
1069 total_bytes_sent = 0;
1074 printf(
"User video resolution: %dx%d\n", arg_width, arg_height);
1076 if (arg_width == 0 || arg_height == 0)
1078 input_video_width = 1280;
1079 input_video_height = 720;
1082 input_video_width = arg_width;
1083 input_video_height = arg_height;
1091 printf(
"P2P Encoding resolution: %dx%d\n", input_video_width,
1092 input_video_height);
1105 fprintf(stderr,
"Error: encoder init default set up error\n");
1115 fprintf(stderr,
"Error: can't set low delay mode %d\n", ret);
1125 fprintf(stderr,
"Error: can't set gop preset %d\n", ret);
1142 &api_param, arg_width, arg_height,
1143 &p2p_frame[render_index]);
1151 input_file_fd = open(input_filename, O_RDONLY | O_BINARY);
1153 input_file_fd = open(input_filename, O_RDONLY);
1156 if (input_file_fd < 0)
1158 fprintf(stderr,
"Error: can not open input file %s\n", input_filename);
1167 &p2p_frame[render_index], input_video_width, input_video_height,
1168 &total_bytes_sent, &input_exhausted))
1170 fprintf(stderr,
"Error: upload frame error\n");
1178 &upl_ctx, input_file_fd, &
g_yuv_frame[render_index],
1179 &p2p_frame[render_index], input_video_width, input_video_height,
1180 &total_bytes_sent, &input_exhausted))
1182 fprintf(stderr,
"Error: upload frame error\n");
1183 close(input_file_fd);
1194 encode_index = render_index;
1198 input_exhausted, &need_to_resend);
1207 render_index = !render_index;
1210 if (!input_exhausted && need_to_resend == 0)
1216 &p2p_frame[render_index], input_video_width,
1217 input_video_height, &total_bytes_sent, &input_exhausted))
1219 fprintf(stderr,
"Error: upload frame error\n");
1220 close(input_file_fd);
1227 &upl_ctx, input_file_fd, &
g_yuv_frame[render_index],
1228 &p2p_frame[render_index], input_video_width,
1229 input_video_height, &total_bytes_sent, &input_exhausted))
1231 fprintf(stderr,
"Error: upload frame error\n");
1232 close(input_file_fd);
1240 &enc_ctx, &out_packet, p_file, &total_bytes_received, print_time);
1255 timeDiff = (timeDiff > 0) ? timeDiff : 1;
1257 printf(
"[R] Got: Packets= %u fps=%u Total bytes %llu\n",
1259 total_bytes_received);
1281 close(input_file_fd);
1288 printf(
"All done\n");