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