- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/03_video_cuda_enc/video_cuda_enc_csvparser.cpp

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
    00003  *
    00004  * Redistribution and use in source and binary forms, with or without
    00005  * modification, are permitted provided that the following conditions
    00006  * are met:
    00007  *  * Redistributions of source code must retain the above copyright
    00008  *    notice, this list of conditions and the following disclaimer.
    00009  *  * Redistributions in binary form must reproduce the above copyright
    00010  *    notice, this list of conditions and the following disclaimer in the
    00011  *    documentation and/or other materials provided with the distribution.
    00012  *  * Neither the name of NVIDIA CORPORATION nor the names of its
    00013  *    contributors may be used to endorse or promote products derived
    00014  *    from this software without specific prior written permission.
    00015  *
    00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
    00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    00019  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00020  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00022  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00023  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    00024  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00026  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    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 }