• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device 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_adapter_parse.h"
16 #include <getopt.h>
17 
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     };
29 }
30 
Parse(int argc,char * argv[],CommandOpt & opt)31 bool CommandAdapterParse::Parse(int argc, char *argv[], CommandOpt &opt)
32 {
33     while (1) {
34         int optionIndex = 0;
35         static struct option long_options[] = {
36             {"width",  required_argument, 0,    (int)MyOptIndex::OPT_INDEX_WIDTH  },
37             {"height", required_argument, 0,    (int)MyOptIndex::OPT_INDEX_HEIGHT },
38             {"in",     required_argument, 0,    (int)MyOptIndex::OPT_INDEX_INPUT  },
39             {"out",    required_argument, 0,    (int)MyOptIndex::OPT_INDEX_OUTPUT },
40             {"help",   no_argument,       NULL, (int)MyOptIndex::OPT_INDEX_HELP   },
41             {0,        0,                 0,    (int)MyOptIndex::OPT_INDEX_UNKONWN}
42         };
43         int c = getopt_long(argc, argv, "i:o:w:h:", long_options, &optionIndex);
44         if (c == -1) {
45             break;
46         }
47         MyOptIndex index = (MyOptIndex)c;
48         switch (index) {
49             case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
50                 opt.useBuffer = true;
51                 break;
52             case MyOptIndex::OPT_INDEX_HEVC:
53                 opt.codec = codecMime::HEVC;
54                 break;
55             case MyOptIndex::OPT_INDEX_HELP:
56                 ShowUsage();
57                 break;
58             case MyOptIndex::OPT_INDEX_INPUT:
59                 opt.fileInput = optarg;
60                 break;
61             case MyOptIndex::OPT_INDEX_OUTPUT:
62                 opt.fileOutput = optarg;
63                 break;
64             case MyOptIndex::OPT_INDEX_WIDTH:
65                 opt.width = atoi(optarg);
66                 break;
67             case MyOptIndex::OPT_INDEX_HEIGHT:
68                 opt.height = atoi(optarg);
69                 break;
70             default:
71                 ShowUsage();
72                 break;
73         }
74     }
75     if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
76         return false;
77     }
78     return true;
79 }
80 
ShowUsage()81 void CommandAdapterParse::ShowUsage()
82 {
83     std::cout << "Options:" << std::endl;
84     std::cout << " -w, --width=width          The video width." << std::endl;
85     std::cout << " -h, --height=height        The video height." << std::endl;
86     std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
87     std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
88     std::cout << " --help                     The help info." << std::endl;
89 }
90