- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/backend/v4l2_backend_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 #ifdef ENABLE_GIE
    00033 #include "gie_inference.h"
    00034 #endif
    00035 #include "v4l2_backend_test.h"
    00036
    00037 #define CHECK_OPTION_VALUE(argp) if(!*argp || (*argp)[0] == '-') \
    00038                                 { \
    00039                                     cerr << "Error: value not specified for option " << arg << endl; \
    00040                                     goto error; \
    00041                                 }
    00042
    00043 #define CSV_PARSE_CHECK_ERROR(condition, str) \
    00044     if (condition) {\
    00045     cerr << "Error: " << str << endl; \
    00046     goto error; \
    00047     }
    00048
    00049 using namespace std;
    00050
    00051 void
    00052 print_help(void)
    00053 {
    00054     cerr << "\nbackend <channel-num> <in-file1> <in-file2>... <in-format> [options]\n\n"
    00055             "Channel-num:\n"
    00056             "\t1-4, Number of file arguments should exactly match the number of channels specified\n\n"
    00057             "Supported formats:\n"
    00058             "\tH264\n"
    00059             "\tH265\n\n"
    00060             "OPTIONS:\n"
    00061             "\t-h,--help            Prints this text\n"
    00062             "\t-fps <fps>           Display rate in frames per second [Default = 30]\n\n"
    00063             "\t--s                  Give a statistic of each channel\n"
    00064             "\t-run-opt <0-3>       0[default], 1 parser only, 2 parser+decoder,  3 parser+decoder+VIC\n"
    00065             "\t--input-nalu         Input to the decoder will be nal units[Default]\n"
    00066             "\t--input-chunks       Input to the decoder will be a chunk of bytes\n\n"
    00067 #ifdef ENABLE_GIE
    00068             "\t--gie-deployfile     set deploy file name\n"
    00069             "\t--gie-modelfile      set model file name\n"
    00070             "\t--gie-proc-interval  set process interval, 1 frame will be process every gie-proc-interval\n"
    00071             "\t--gie-forcefp32      0 to use fp16 (if supported), 1 to use fp32\n"
    00072             "\t--gie-dumpresult     1 to dump result, 0[default] otherwise\n"
    00073             "\t--gie-enable-perf    1[default] to enable perf measurement, 0 otherwise\n"
    00074 #endif
    00075             << endl;
    00076 }
    00077
    00078 static uint32_t
    00079 get_decoder_type(char *arg)
    00080 {
    00081     if (!strcmp(arg, "H264"))
    00082         return V4L2_PIX_FMT_H264;
    00083     if (!strcmp(arg, "H265"))
    00084         return V4L2_PIX_FMT_H265;
    00085     return 0;
    00086 }
    00087
    00088 int
    00089 parse_csv_args(context_t * ctx,
    00090 #ifdef ENABLE_GIE
    00091             GIE_Context *gie_ctx,
    00092 #endif
    00093             int argc, char *argv[])
    00094 {
    00095     char **argp = argv;
    00096     char *arg = *(++argp);
    00097
    00098     ctx->decoder_pixfmt = get_decoder_type(*argp);
    00099     CSV_PARSE_CHECK_ERROR(ctx->decoder_pixfmt == 0,
    00100                           "Incorrect input format");
    00101
    00102     while ((arg = *(++argp)))
    00103     {
    00104         if (!strcmp(arg, "-o"))
    00105         {
    00106             argp++;
    00107             CHECK_OPTION_VALUE(argp);
    00108             ctx->out_file_path = strdup(*argp);
    00109             CSV_PARSE_CHECK_ERROR(!ctx->out_file_path,
    00110                                   "Output file not specified");
    00111         }
    00112         else if (!strcmp(arg, "--disable-dpb"))
    00113         {
    00114             ctx->disable_dpb = true;
    00115         }
    00116         else if (!strcmp(arg, "--fullscreen"))
    00117         {
    00118             ctx->fullscreen = true;
    00119         }
    00120         else if (!strcmp(arg, "-wh"))
    00121         {
    00122             argp++;
    00123             CHECK_OPTION_VALUE(argp);
    00124             ctx->window_height = atoi(*argp);
    00125             CSV_PARSE_CHECK_ERROR(ctx->window_height == 0,
    00126                                   "Window height should be > 0");
    00127         }
    00128         else if (!strcmp(arg, "-ww"))
    00129         {
    00130             argp++;
    00131             CHECK_OPTION_VALUE(argp);
    00132             ctx->window_width = atoi(*argp);
    00133             CSV_PARSE_CHECK_ERROR(ctx->window_width == 0,
    00134                                   "Window width should be > 0");
    00135         }
    00136         else if (!strcmp(arg, "-wx"))
    00137         {
    00138             argp++;
    00139             CHECK_OPTION_VALUE(argp);
    00140             ctx->window_x = atoi(*argp);
    00141         }
    00142         else if (!strcmp(arg, "-wy"))
    00143         {
    00144             argp++;
    00145             CHECK_OPTION_VALUE(argp);
    00146             ctx->window_y = atoi(*argp);
    00147         }
    00148         else if (!strcmp(arg, "-fps"))
    00149         {
    00150             argp++;
    00151             CHECK_OPTION_VALUE(argp);
    00152             ctx->fps = atof(*argp);
    00153             CSV_PARSE_CHECK_ERROR(ctx->fps == 0, "FPS should be > 0");
    00154         }
    00155         else if (!strcmp(arg, "-run-opt"))
    00156         {
    00157             argp++;
    00158             ctx->cpu_occupation_option = atoi(*argp);
    00159             CSV_PARSE_CHECK_ERROR(ctx->cpu_occupation_option < 0 ||
    00160                                   ctx->cpu_occupation_option >3,
    00161                                   "parameter error:run-opt should be 0-3");
    00162         }
    00163         else if (!strcmp(arg, "--s"))
    00164         {
    00165             ctx->do_stat = true;
    00166         }
    00167         else if (!strcmp(arg, "--input-nalu"))
    00168         {
    00169             ctx->input_nalu = true;
    00170         }
    00171         else if (!strcmp(arg, "--input-chunks"))
    00172         {
    00173             ctx->input_nalu = false;
    00174         }
    00175         else if (!strcmp(arg, "--dbg-level"))
    00176         {
    00177             argp++;
    00178             CHECK_OPTION_VALUE(argp);
    00179             log_level = atoi(*argp);
    00180         }
    00181         else if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
    00182         {
    00183             print_help();
    00184             exit(EXIT_SUCCESS);
    00185         }
    00186 #ifdef ENABLE_GIE
    00187         else if (!strcmp(arg, "--gie-deployfile"))
    00188         {
    00189             argp++;
    00190             /* This parameter has been parsed in global_cfg,
    00191                but need to skip if found here */
    00192             continue;
    00193         }
    00194         else if (!strcmp(arg, "--gie-modelfile"))
    00195         {
    00196             argp++;
    00197             /* This parameter has been parsed in global_cfg,
    00198                but need to skip if found here */
    00199             continue;
    00200         }
    00201         else if (!strcmp(arg, "--gie-forcefp32"))
    00202         {
    00203             argp++;
    00204             gie_ctx->setForcedFp32((bool)atoi(*argp));
    00205         }
    00206         else if (!strcmp(arg, "--gie-proc-interval"))
    00207         {
    00208             argp++;
    00209             gie_ctx->setFilterNum(atoi(*argp));
    00210         }
    00211         else if (!strcmp(arg, "--gie-dumpresult"))
    00212         {
    00213             if (*(argp + 1) != NULL &&
    00214                 (strcmp(*(argp + 1), "0") == 0 ||
    00215                 strcmp(*(argp + 1), "1") == 0))
    00216             {
    00217                 argp++;
    00218                 gie_ctx->setDumpResult((bool)atoi(*argp));
    00219             }
    00220         }
    00221         else if (!strcmp(arg, "--gie-enable-perf"))
    00222         {
    00223             if (*(argp + 1) != NULL &&
    00224                 (strcmp(*(argp + 1), "0") == 0 ||
    00225                 strcmp(*(argp + 1), "1") == 0))
    00226             {
    00227                 argp++;
    00228                 gie_ctx->setGieProfilerEnabled((bool)atoi(*argp));
    00229             }
    00230         }
    00231 #endif
    00232         else
    00233         {
    00234             CSV_PARSE_CHECK_ERROR(ctx->out_file_path,
    00235                                     "Unknown option " << arg);
    00236         }
    00237     }
    00238
    00239     return 0;
    00240
    00241 error:
    00242     print_help();
    00243     return -1;
    00244 }
    00245
    00246 void
    00247 parse_global(global_cfg* cfg, int argc, char ***argv)
    00248 {
    00249     char **argp = *argv;
    00250     char *arg = *(++argp);
    00251     if (argc == 1 || (arg && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))))
    00252     {
    00253         print_help();
    00254         exit(1);
    00255     }
    00256     cfg->channel_num = atoi(*argp);
    00257     CSV_PARSE_CHECK_ERROR(cfg->channel_num < 1 ||  cfg->channel_num > 4,
    00258                     "channel should be between 1 and 4, program will exit");
    00259
    00260     for (int i = 0; i < cfg->channel_num; i++)
    00261     {
    00262         if (*(argp + 1) != NULL && strcmp(*(argp + 1), "H264") != 0 &&
    00263             strcmp(*(argp + 1), "H265") != 0)
    00264         {
    00265             cfg->in_file_path[i] = strdup(*(++argp));
    00266         }
    00267         else
    00268         {
    00269             cout << "Not enough number of files provided" << endl;
    00270             print_help();
    00271             exit(1);
    00272         }
    00273     }
    00274
    00275     *argv = argp;
    00276
    00277 #ifdef ENABLE_GIE
    00278     // seek for GIE deploy & caffemodel
    00279     while ((arg = *(++argp)))
    00280     {
    00281         if (!strcmp(arg, "--gie-deployfile"))
    00282         {
    00283             argp++;
    00284             cfg->deployfile = *argp;
    00285         }
    00286         else if (!strcmp(arg, "--gie-modelfile"))
    00287         {
    00288             argp++;
    00289             cfg->modelfile = *argp;
    00290         }
    00291     }
    00292 #endif
    00293     return;
    00294
    00295 error:
    00296     exit(EXIT_SUCCESS);
    00297 }