00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 #include <cstdlib>
00031 #include <cstring>
00032 #include <linux/v4l2-controls.h>
00033 #include "video_cuda_enc.h"
00034
00035 #define CHECK_OPTION_VALUE(argp) if(!*argp || (*argp)[0] == '-') \
00036 { \
00037 cerr << "Error: value not specified for option " << arg << endl; \
00038 goto error; \
00039 }
00040
00041 #define CSV_PARSE_CHECK_ERROR(condition, str) \
00042 if (condition) {\
00043 cerr << "Error: " << str << endl; \
00044 goto error; \
00045 }
00046
00047 using namespace std;
00048
00049 static void
00050 print_help(void)
00051 {
00052 cerr << "\nvideo_cuda_enc <in-file> <in-width> <in-height> <encoder-type> <out-file> [OPTIONS]\n\n"
00053 "Encoder Types:\n"
00054 "\tH264\n"
00055 "\tH265\n\n"
00056 "OPTIONS:\n"
00057 "\t-h,--help Prints this text\n"
00058 "\t--dbg-level <level> Sets the debug level [Values 0-3]\n\n"
00059 "\t-br <bitrate> Bitrate [Default = 4000000]\n"
00060 "\t-fps <num> <den> Encoding fps in num/den [Default = 30/1]\n\n";
00061 }
00062
00063 static int32_t
00064 get_encoder_type(char *arg)
00065 {
00066 if (!strcmp(arg, "H264"))
00067 return V4L2_PIX_FMT_H264;
00068 if (!strcmp(arg, "H265"))
00069 return V4L2_PIX_FMT_H265;
00070 return 0;
00071 }
00072
00073 static int32_t
00074 get_dbg_level(char *arg)
00075 {
00076 int32_t log_level = atoi(arg);
00077
00078 if (log_level < 0)
00079 {
00080 cout << "Warning: invalid log level input, defaulting to setting 0" << endl;
00081 return 0;
00082 }
00083
00084 if (log_level > 3)
00085 {
00086 cout << "Warning: invalid log level input, defaulting to setting 3" << endl;
00087 return 3;
00088 }
00089
00090 return log_level;
00091 }
00092
00093 int
00094 parse_csv_args(context_t * ctx, int argc, char *argv[])
00095 {
00096 char **argp = argv;
00097 char *arg = *(++argp);
00098
00099 if (argc == 1 || (arg && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))))
00100 {
00101 print_help();
00102 exit(EXIT_SUCCESS);
00103 }
00104
00105 CSV_PARSE_CHECK_ERROR(argc < 6, "Insufficient arguments");
00106
00107 ctx->in_file_path = strdup(*argp);
00108 CSV_PARSE_CHECK_ERROR(!ctx->in_file_path, "Input file not specified");
00109
00110 ctx->width = atoi(*(++argp));
00111 CSV_PARSE_CHECK_ERROR(ctx->width == 0, "Input width should be > 0");
00112
00113 ctx->height = atoi(*(++argp));
00114 CSV_PARSE_CHECK_ERROR(ctx->height == 0, "Input height should be > 0");
00115
00116 ctx->encoder_pixfmt = get_encoder_type(*(++argp));
00117 CSV_PARSE_CHECK_ERROR(ctx->encoder_pixfmt == 0,
00118 "Incorrect encoder type");
00119
00120 ctx->out_file_path = strdup(*(++argp));
00121 CSV_PARSE_CHECK_ERROR(!ctx->out_file_path, "Output file not specified");
00122
00123 while ((arg = *(++argp)))
00124 {
00125 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
00126 {
00127 print_help();
00128 exit(EXIT_SUCCESS);
00129 }
00130 else if (!strcmp(arg, "--dbg-level"))
00131 {
00132 argp++;
00133 CHECK_OPTION_VALUE(argp);
00134 log_level = get_dbg_level(*argp);
00135 }
00136 else if (!strcmp(arg, "-fps"))
00137 {
00138 argp++;
00139 CHECK_OPTION_VALUE(argp);
00140 ctx->fps_n = atoi(*argp);
00141 argp++;
00142 CHECK_OPTION_VALUE(argp);
00143 ctx->fps_d = atoi(*argp);
00144 CSV_PARSE_CHECK_ERROR(ctx->fps_d == 0, "fps den should be > 0");
00145 }
00146 else if (!strcmp(arg, "-br"))
00147 {
00148 argp++;
00149 CHECK_OPTION_VALUE(argp);
00150 ctx->bitrate = atoi(*argp);
00151 CSV_PARSE_CHECK_ERROR(ctx->bitrate == 0, "bit rate should be > 0");
00152 }
00153 else
00154 {
00155 CSV_PARSE_CHECK_ERROR(ctx->out_file_path,
00156 "Unknown option " << arg);
00157 }
00158 }
00159
00160 return 0;
00161
00162 error:
00163 print_help();
00164 return -1;
00165 }