1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Shenzhen Kaihong DID Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci * 		http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include "command_parse.h"
16094332d3Sopenharmony_ci#include <getopt.h>
17094332d3Sopenharmony_ci#include <iostream>
18094332d3Sopenharmony_cinamespace {
19094332d3Sopenharmony_cistatic struct option g_longOptions[] = {
20094332d3Sopenharmony_ci    {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
21094332d3Sopenharmony_ci    {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
22094332d3Sopenharmony_ci    {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
23094332d3Sopenharmony_ci    {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
24094332d3Sopenharmony_ci    {"color", optional_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_COLOR)},
25094332d3Sopenharmony_ci    {"nocopy", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_BUFFER_HANDLE)},
26094332d3Sopenharmony_ci    {"HEVC", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEVC)},
27094332d3Sopenharmony_ci    {"MPEG4", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_MPEG4)},
28094332d3Sopenharmony_ci    {"VP9", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_VP9)},
29094332d3Sopenharmony_ci    {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
30094332d3Sopenharmony_ci    {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}};
31094332d3Sopenharmony_ci}  // namespace
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_civoid CommandParse::ParseCodingType(const MyOptIndex index, CommandOpt &opt)
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    MyOptIndex optIndex = static_cast<MyOptIndex>(index);
36094332d3Sopenharmony_ci    switch (optIndex) {
37094332d3Sopenharmony_ci        case MyOptIndex::OPT_INDEX_HEVC:
38094332d3Sopenharmony_ci            opt.codec = CodecMime::HEVC;
39094332d3Sopenharmony_ci            break;
40094332d3Sopenharmony_ci        case MyOptIndex::OPT_INDEX_VP9:
41094332d3Sopenharmony_ci            opt.codec = CodecMime::VP9;
42094332d3Sopenharmony_ci            break;
43094332d3Sopenharmony_ci        case MyOptIndex::OPT_INDEX_MPEG4:
44094332d3Sopenharmony_ci            opt.codec = CodecMime::MPEG4;
45094332d3Sopenharmony_ci            break;
46094332d3Sopenharmony_ci        default:
47094332d3Sopenharmony_ci            ShowUsage();
48094332d3Sopenharmony_ci            break;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci}
51094332d3Sopenharmony_cibool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
52094332d3Sopenharmony_ci{
53094332d3Sopenharmony_ci    while (1) {
54094332d3Sopenharmony_ci        int optionIndex = 0;
55094332d3Sopenharmony_ci        int c = getopt_long(argc, argv, "c::i:o:w:h:", g_longOptions, &optionIndex);
56094332d3Sopenharmony_ci        if (c == -1) {
57094332d3Sopenharmony_ci            break;
58094332d3Sopenharmony_ci        }
59094332d3Sopenharmony_ci        MyOptIndex index = static_cast<MyOptIndex>(c);
60094332d3Sopenharmony_ci        switch (index) {
61094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
62094332d3Sopenharmony_ci                opt.useBuffer = true;
63094332d3Sopenharmony_ci                break;
64094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HELP:
65094332d3Sopenharmony_ci                ShowUsage();
66094332d3Sopenharmony_ci                break;
67094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_INPUT:
68094332d3Sopenharmony_ci                opt.fileInput = optarg;
69094332d3Sopenharmony_ci                break;
70094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_OUTPUT:
71094332d3Sopenharmony_ci                opt.fileOutput = optarg;
72094332d3Sopenharmony_ci                break;
73094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_WIDTH:
74094332d3Sopenharmony_ci                opt.width = std::stoi(optarg);
75094332d3Sopenharmony_ci                break;
76094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HEIGHT:
77094332d3Sopenharmony_ci                opt.height = std::stoi(optarg);
78094332d3Sopenharmony_ci                break;
79094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_COLOR:
80094332d3Sopenharmony_ci                if (optarg) {
81094332d3Sopenharmony_ci                    opt.colorForamt = static_cast<ColorFormat>(std::stoi(optarg));
82094332d3Sopenharmony_ci                }
83094332d3Sopenharmony_ci                break;
84094332d3Sopenharmony_ci            default:
85094332d3Sopenharmony_ci                ParseCodingType(index, opt);
86094332d3Sopenharmony_ci                break;
87094332d3Sopenharmony_ci        }
88094332d3Sopenharmony_ci    }
89094332d3Sopenharmony_ci    if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
90094332d3Sopenharmony_ci        return false;
91094332d3Sopenharmony_ci    }
92094332d3Sopenharmony_ci    return true;
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_civoid CommandParse::ShowUsage()
96094332d3Sopenharmony_ci{
97094332d3Sopenharmony_ci    std::cout << "Options:" << std::endl;
98094332d3Sopenharmony_ci    std::cout << " -w, --width=width          The video width." << std::endl;
99094332d3Sopenharmony_ci    std::cout << " -h, --height=height        The video height." << std::endl;
100094332d3Sopenharmony_ci    std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
101094332d3Sopenharmony_ci    std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
102094332d3Sopenharmony_ci    std::cout << " -cN, --color=N             The color format in the file. 0 is YUV420SP, 1 is RGBA888, 2 is BGRA888, "
103094332d3Sopenharmony_ci                 "the default is 0."
104094332d3Sopenharmony_ci              << std::endl;
105094332d3Sopenharmony_ci    std::cout << " --HEVC                     HEVC decode or encode, AVC for default." << std::endl;
106094332d3Sopenharmony_ci    std::cout << " --MPEG4                    MPEG4 decode or encode, AVC for default." << std::endl;
107094332d3Sopenharmony_ci    std::cout << " --VP9                      VP9 decode or encode, AVC for default." << std::endl;
108094332d3Sopenharmony_ci    std::cout << " --nocopy                   Support BufferHandle." << std::endl;
109094332d3Sopenharmony_ci    std::cout << " --help                     The help info." << std::endl;
110094332d3Sopenharmony_ci}
111