- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/05_jpeg_encode/jpeg_encode_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 "jpeg_encode.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 <<
    00053         "\njpeg-encode <in-file> <in-width> <in-height> <out-file> [OPTIONS]\n\n"
    00054         "OPTIONS:\n"
    00055         "\t-h,--help            Prints this text\n"
    00056         "\t--dbg-level <level>  Sets the debug level [Values 0-3]\n\n"
    00057         "\t--encode-fd          Uses FD as input to encoder [DEFAULT]\n"
    00058         "\t--encode-buffer      Uses buffer as input to encoder\n\n"
    00059         "\t-f <pixfmt>          Color format of input to encoder (works only for --encode-fd) [1=YUV420(Default), 2=NV12]\n\n"
    00060         "\t-crop <left> <top> <width> <height>  Cropping rectangle for JPEG encoder\n\n";
    00061 }
    00062
    00063 static int32_t
    00064 get_dbg_level(char *arg)
    00065 {
    00066     int32_t log_level = atoi(arg);
    00067     if (log_level < 0)
    00068     {
    00069         cout<<"log level too small, set to 0"<<endl;
    00070         return 0;
    00071     }
    00072
    00073     if (log_level > 3)
    00074     {
    00075         cout<<"log level too high, set to 3"<<endl;
    00076         return 3;
    00077     }
    00078
    00079     return log_level;
    00080 }
    00081
    00082 int
    00083 parse_csv_args(context_t * ctx, int argc, char *argv[])
    00084 {
    00085     char **argp = argv;
    00086     char *arg = *(++argp);
    00087
    00088     if (argc == 1 || (arg && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))))
    00089     {
    00090         print_help();
    00091         exit(EXIT_SUCCESS);
    00092     }
    00093
    00094     CSV_PARSE_CHECK_ERROR(argc < 5, "Insufficient arguments");
    00095
    00096     ctx->in_file_path = strdup(*argp);
    00097     CSV_PARSE_CHECK_ERROR(!ctx->in_file_path, "Input file not specified");
    00098
    00099     ctx->in_width = atoi(*(++argp));
    00100     CSV_PARSE_CHECK_ERROR(ctx->in_width == 0, "Input width should be > 0");
    00101
    00102     ctx->in_height = atoi(*(++argp));
    00103     CSV_PARSE_CHECK_ERROR(ctx->in_height == 0, "Input height should be > 0");
    00104
    00105     ctx->out_file_path = strdup(*(++argp));
    00106     CSV_PARSE_CHECK_ERROR(!ctx->out_file_path, "Output file not specified");
    00107
    00108     while ((arg = *(++argp)))
    00109     {
    00110         if (!strcmp(arg, "--dbg-level"))
    00111         {
    00112             argp++;
    00113             CHECK_OPTION_VALUE(argp);
    00114             log_level = get_dbg_level(*argp);
    00115         }
    00116         else if (!strcmp(arg, "--encode-fd"))
    00117         {
    00118             ctx->use_fd = true;
    00119         }
    00120         else if (!strcmp(arg, "--encode-buffer"))
    00121         {
    00122             ctx->use_fd = false;
    00123         }
    00124         else if (!strcmp(arg, "-f"))
    00125         {
    00126             argp++;
    00127             CHECK_OPTION_VALUE(argp);
    00128             switch(atoi(*argp))
    00129             {
    00130                 case 1:
    00131                     ctx->in_pixfmt = V4L2_PIX_FMT_YUV420M;
    00132                     break;
    00133                 case 2:
    00134                     ctx->in_pixfmt = V4L2_PIX_FMT_NV12M;
    00135                     break;
    00136                 default:
    00137                     CSV_PARSE_CHECK_ERROR(true, "Unsupported value for -f");
    00138             }
    00139         }
    00140         else if (!strcmp(arg, "-crop"))
    00141         {
    00142             argp++;
    00143             CHECK_OPTION_VALUE(argp);
    00144             ctx->crop_left = atoi(*argp);
    00145             argp++;
    00146             CHECK_OPTION_VALUE(argp);
    00147             ctx->crop_top = atoi(*argp);
    00148             argp++;
    00149             CHECK_OPTION_VALUE(argp);
    00150             ctx->crop_width = atoi(*argp);
    00151             argp++;
    00152             CHECK_OPTION_VALUE(argp);
    00153             ctx->crop_height = atoi(*argp);
    00154             CSV_PARSE_CHECK_ERROR(ctx->crop_width == 0 || ctx->crop_height == 0,
    00155                     "Crop height/width should be greater than zero");
    00156         }
    00157         else if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
    00158         {
    00159             print_help();
    00160             exit(EXIT_SUCCESS);
    00161         }
    00162         else
    00163         {
    00164             CSV_PARSE_CHECK_ERROR(ctx->out_file_path, "Unknown option " << arg);
    00165         }
    00166         CSV_PARSE_CHECK_ERROR(
    00167                 ctx->in_pixfmt == V4L2_PIX_FMT_NV12M && !ctx->use_fd,
    00168                 "--encode-buffer is not supported with NV12 format");
    00169     }
    00170
    00171     return 0;
    00172
    00173 error:
    00174     print_help();
    00175     return -1;
    00176 }