- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/02_video_dec_cuda/videodec_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
    00033 #include "videodec.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_dec_cuda <in-file> <in-format> [options]\n\n"
    00053             "Supported formats:\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--disable-rendering  Disable rendering\n"
    00060             "\t--fullscreen         Fullscreen playback [Default = disabled]\n"
    00061             "\t-ww <width>          Window width in pixels [Default = video-width]\n"
    00062             "\t-wh <height>         Window height in pixels [Default = video-height]\n"
    00063             "\t-wx <x-offset>       Horizontal window offset [Default = 0]\n"
    00064             "\t-wy <y-offset>       Vertical window offset [Default = 0]\n\n"
    00065             "\t-fps <fps>           Display rate in frames per second [Default = 30]\n\n"
    00066             "\t-o <out-file>        Write to output file\n\n"
    00067             "\t-f <out_pixfmt>      1 NV12, 2 I420 [Default = 1]\n\n"
    00068             "\t--input-nalu         Input to the decoder will be nal units\n"
    00069             "\t--input-chunks       Input to the decoder will be a chunk of bytes [Default]\n"
    00070             "\t--bbox-file          bbox file path\n\n";
    00071 }
    00072
    00073 static uint32_t
    00074 get_decoder_type(char *arg)
    00075 {
    00076     if (!strcmp(arg, "H264"))
    00077         return V4L2_PIX_FMT_H264;
    00078     if (!strcmp(arg, "H265"))
    00079         return V4L2_PIX_FMT_H265;
    00080     return 0;
    00081 }
    00082
    00083 static int32_t
    00084 get_dbg_level(char *arg)
    00085 {
    00086     int32_t log_level = atoi(arg);
    00087     if (log_level < 0)
    00088     {
    00089         cout<<"log level too small, set to 0"<<endl;
    00090         return 0;
    00091     }
    00092
    00093     if (log_level > 3)
    00094     {
    00095         cout<<"log level too high, set to 3"<<endl;
    00096         return 3;
    00097     }
    00098
    00099     return log_level;
    00100 }
    00101
    00102 int
    00103 parse_csv_args(context_t * ctx, int argc, char *argv[])
    00104 {
    00105     char **argp = argv;
    00106     char *arg = *(++argp);
    00107
    00108     if (argc == 1 || (arg && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))))
    00109     {
    00110         print_help();
    00111         exit(EXIT_SUCCESS);
    00112     }
    00113
    00114     CSV_PARSE_CHECK_ERROR(argc < 3, "Insufficient arguments");
    00115
    00116     ctx->in_file_path = strdup(*argp);
    00117     CSV_PARSE_CHECK_ERROR(!ctx->in_file_path, "Input file not specified");
    00118
    00119     ctx->decoder_pixfmt = get_decoder_type(*(++argp));
    00120     CSV_PARSE_CHECK_ERROR(ctx->decoder_pixfmt == 0,
    00121                           "Incorrect input format");
    00122
    00123     while ((arg = *(++argp)))
    00124     {
    00125         if (!strcmp(arg, "-o"))
    00126         {
    00127             argp++;
    00128             CHECK_OPTION_VALUE(argp);
    00129             ctx->out_file_path = strdup(*argp);
    00130             CSV_PARSE_CHECK_ERROR(!ctx->out_file_path,
    00131                                   "Output file not specified");
    00132         }
    00133         else if (!strcmp(arg, "-f"))
    00134         {
    00135             argp++;
    00136             CHECK_OPTION_VALUE(argp);
    00137             ctx->out_pixfmt = atoi(*argp);
    00138             CSV_PARSE_CHECK_ERROR((ctx->out_pixfmt < 1 || ctx->out_pixfmt > 2),
    00139                                     "out_pixfmt shoud be 1(NV12), 2(I420)");
    00140         }
    00141         else if (!strcmp(arg, "--disable-rendering"))
    00142         {
    00143             ctx->disable_rendering = true;
    00144         }
    00145         else if (!strcmp(arg, "--fullscreen"))
    00146         {
    00147             ctx->fullscreen = true;
    00148         }
    00149         else if (!strcmp(arg, "-wh"))
    00150         {
    00151             argp++;
    00152             CHECK_OPTION_VALUE(argp);
    00153             ctx->window_height = atoi(*argp);
    00154             CSV_PARSE_CHECK_ERROR(ctx->window_height == 0,
    00155                                   "Window height should be > 0");
    00156         }
    00157         else if (!strcmp(arg, "-ww"))
    00158         {
    00159             argp++;
    00160             CHECK_OPTION_VALUE(argp);
    00161             ctx->window_width = atoi(*argp);
    00162             CSV_PARSE_CHECK_ERROR(ctx->window_width == 0,
    00163                                   "Window width should be > 0");
    00164         }
    00165         else if (!strcmp(arg, "-wx"))
    00166         {
    00167             argp++;
    00168             CHECK_OPTION_VALUE(argp);
    00169             ctx->window_x = atoi(*argp);
    00170         }
    00171         else if (!strcmp(arg, "-wy"))
    00172         {
    00173             argp++;
    00174             CHECK_OPTION_VALUE(argp);
    00175             ctx->window_y = atoi(*argp);
    00176         }
    00177         else if (!strcmp(arg, "-fps"))
    00178         {
    00179             argp++;
    00180             CHECK_OPTION_VALUE(argp);
    00181             ctx->fps = atof(*argp);
    00182             CSV_PARSE_CHECK_ERROR(ctx->fps == 0, "FPS should be > 0");
    00183         }
    00184         else if (!strcmp(arg, "--input-nalu"))
    00185         {
    00186             ctx->input_nalu = true;
    00187         }
    00188         else if (!strcmp(arg, "--input-chunks"))
    00189         {
    00190             ctx->input_nalu = false;
    00191         }
    00192         else if (!strcmp(arg, "--bbox-file"))
    00193         {
    00194             argp++;
    00195             ctx->enable_osd = true;
    00196             ctx->osd_file_path = strdup(*argp);
    00197         }
    00198         else if (!strcmp(arg, "--dbg-level"))
    00199         {
    00200             argp++;
    00201             CHECK_OPTION_VALUE(argp);
    00202             log_level = get_dbg_level(*argp);
    00203         }
    00204         else if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
    00205         {
    00206             print_help();
    00207             exit(EXIT_SUCCESS);
    00208         }
    00209         else
    00210         {
    00211             CSV_PARSE_CHECK_ERROR(ctx->out_file_path, "Unknown option " << arg);
    00212         }
    00213     }
    00214
    00215     return 0;
    00216
    00217 error:
    00218     print_help();
    00219     return -1;
    00220 }