1 /*
2  * Copyright 2023 Shenzhen Kaihong DID Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 		http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "command_parse.h"
16 #include <getopt.h>
17 #include <iostream>
18 enum class MyOptIndex {
19     OPT_INDEX_UNKONWN = 0,
20     OPT_INDEX_BUFFER_HANDLE,
21     OPT_INDEX_DMA_BUFFER,
22     OPT_INDEX_HEVC,
23     OPT_INDEX_HELP,
24     OPT_INDEX_HEIGHT = 'h',
25     OPT_INDEX_INPUT = 'i',
26     OPT_INDEX_OUTPUT = 'o',
27     OPT_INDEX_WIDTH = 'w'
28 };
29 
30 static struct option g_longOptions[] = {
31     {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
32     {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
33     {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
34     {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
35     {"nocopy", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_BUFFER_HANDLE)},
36     {"dma", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_DMA_BUFFER)},
37     {"HEVC", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEVC)},
38     {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
39     {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}
40 };
41 
Parse(int argc,char * argv[],CommandOpt & opt)42 bool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
43 {
44     while (1) {
45         int optionIndex = 0;
46 
47         int c = getopt_long(argc, argv, "i:o:w:h:", g_longOptions, &optionIndex);
48         if (c == -1) {
49             break;
50         }
51         MyOptIndex index = (MyOptIndex)c;
52         switch (index) {
53             case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
54                 opt.useBufferHandle = true;
55                 break;
56             case MyOptIndex::OPT_INDEX_DMA_BUFFER:
57                 opt.useDMABuffer = true;
58                 break;
59             case MyOptIndex::OPT_INDEX_HEVC:
60                 opt.codec = codecMime::HEVC;
61                 break;
62             case MyOptIndex::OPT_INDEX_HELP:
63                 ShowUsage();
64                 break;
65             case MyOptIndex::OPT_INDEX_INPUT:
66                 opt.fileInput = optarg;
67                 break;
68             case MyOptIndex::OPT_INDEX_OUTPUT:
69                 opt.fileOutput = optarg;
70                 break;
71             case MyOptIndex::OPT_INDEX_WIDTH:
72                 opt.width = atoi(optarg);
73                 break;
74             case MyOptIndex::OPT_INDEX_HEIGHT:
75                 opt.height = atoi(optarg);
76                 break;
77             default:
78                 ShowUsage();
79                 break;
80         }
81     }
82     if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
83         return false;
84     }
85     return true;
86 }
87 
ShowUsage()88 void CommandParse::ShowUsage()
89 {
90     std::cout << "Options:" << std::endl;
91     std::cout << " -w, --width=width          The video width." << std::endl;
92     std::cout << " -h, --height=height        The video height." << std::endl;
93     std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
94     std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
95     std::cout << " --HEVC                     HEVC decode or HEVC encode, AVC for default." << std::endl;
96     std::cout << " --nocopy                   Support BufferHandle." << std::endl;
97     std::cout << " --dma                      Support dma Buffer." << std::endl;
98     std::cout << " --help                     The help info." << std::endl;
99 }
100