• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 namespace {
19 enum class MyOptIndex {
20     OPT_INDEX_UNKONWN = 0,
21     OPT_INDEX_BUFFER_HANDLE,
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     OPT_INDEX_COLOR = 'c'
29 };
30 
31 static struct option g_longOptions[] = {
32     {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
33     {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
34     {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
35     {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
36     {"color", optional_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_COLOR)},
37     {"nocopy", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_BUFFER_HANDLE)},
38     {"HEVC", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEVC)},
39     {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
40     {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}};
41 }  // namespace
42 
Parse(int argc,char * argv[],CommandOpt & opt)43 bool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
44 {
45     while (1) {
46         int optionIndex = 0;
47         int c = getopt_long(argc, argv, "c::i:o:w:h:", g_longOptions, &optionIndex);
48         if (c == -1) {
49             break;
50         }
51         MyOptIndex index = static_cast<MyOptIndex>(c);
52         switch (index) {
53             case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
54                 opt.useBuffer = true;
55                 break;
56             case MyOptIndex::OPT_INDEX_HEVC:
57                 opt.codec = codecMime::HEVC;
58                 break;
59             case MyOptIndex::OPT_INDEX_HELP:
60                 ShowUsage();
61                 break;
62             case MyOptIndex::OPT_INDEX_INPUT:
63                 opt.fileInput = optarg;
64                 break;
65             case MyOptIndex::OPT_INDEX_OUTPUT:
66                 opt.fileOutput = optarg;
67                 break;
68             case MyOptIndex::OPT_INDEX_WIDTH:
69                 opt.width = atoi(optarg);
70                 break;
71             case MyOptIndex::OPT_INDEX_HEIGHT:
72                 opt.height = atoi(optarg);
73                 break;
74             case MyOptIndex::OPT_INDEX_COLOR:
75                 if (optarg) {
76                     opt.colorForamt = static_cast<ColorFormat>(atoi(optarg));
77                 }
78                 break;
79             default:
80                 ShowUsage();
81                 break;
82         }
83     }
84     if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
85         return false;
86     }
87     return true;
88 }
89 
ShowUsage()90 void CommandParse::ShowUsage()
91 {
92     std::cout << "Options:" << std::endl;
93     std::cout << " -w, --width=width          The video width." << std::endl;
94     std::cout << " -h, --height=height        The video height." << std::endl;
95     std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
96     std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
97     std::cout << " -cN, --color=N             The color format in the file. 0 is YUV420SP, 1 is RGBA888, 2 is BGRA888, "
98                  "the default is 0."
99               << std::endl;
100     std::cout << " --HEVC                     HEVC decode or HEVC encode, AVC for default." << std::endl;
101     std::cout << " --nocopy                   Support BufferHandle." << std::endl;
102     std::cout << " --help                     The help info." << std::endl;
103 }
104