1 /*
2 * Copyright (c) 2023 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
16 #include "command_parser.h"
17 #include <getopt.h>
18 #include <iostream>
19 #include <cinttypes>
20
21 namespace OHOS::MediaAVCodec {
22 using namespace std;
23
24 enum ShortOption {
25 OPT_UNKONWN = 0,
26 OPT_HELP,
27 OPT_INPUT = 'i',
28 OPT_WIDTH = 'w',
29 OPT_HEIGHT = 'h',
30 OPT_TEST_CODECBASE_API = UINT8_MAX + 1,
31 OPT_IS_ENCODER,
32 OPT_REPEAT_CNT,
33 OPT_INPUT_FRAME_CNT,
34 OPT_PROTOCOL,
35 OPT_PIXEL_FMT,
36 OPT_FRAME_RATE,
37 OPT_TIME_OUT,
38 OPT_IS_BUFFER_MODE,
39 // encoder only
40 OPT_COLOR_RANGE,
41 OPT_COLOR_PRIMARY,
42 OPT_COLOR_TRANSFER,
43 OPT_COLOR_MATRIX,
44 OPT_I_FRAME_INTERVAL,
45 OPT_IDR_FRAME,
46 OPT_PROFILE,
47 OPT_BITRATE_MODE,
48 OPT_BITRATE,
49 OPT_QUALITY,
50 // decoder only
51 OPT_ROTATION,
52 OPT_FLUSH_CNT
53 };
54
55 static struct option g_longOptions[] = {
56 {"help", no_argument, nullptr, OPT_HELP},
57 {"in", required_argument, nullptr, OPT_INPUT},
58 {"width", required_argument, nullptr, OPT_WIDTH},
59 {"height", required_argument, nullptr, OPT_HEIGHT},
60 {"testCodecBase", required_argument, nullptr, OPT_TEST_CODECBASE_API},
61 {"isEncoder", required_argument, nullptr, OPT_IS_ENCODER},
62 {"repeatCnt", required_argument, nullptr, OPT_REPEAT_CNT},
63 {"inputCnt", required_argument, nullptr, OPT_INPUT_FRAME_CNT},
64 {"protocol", required_argument, nullptr, OPT_PROTOCOL},
65 {"pixelFmt", required_argument, nullptr, OPT_PIXEL_FMT},
66 {"frameRate", required_argument, nullptr, OPT_FRAME_RATE},
67 {"timeout", required_argument, nullptr, OPT_TIME_OUT},
68 {"isBufferMode", required_argument, nullptr, OPT_IS_BUFFER_MODE},
69 {"colorRange", required_argument, nullptr, OPT_COLOR_RANGE},
70 {"colorPrimary", required_argument, nullptr, OPT_COLOR_PRIMARY},
71 {"colorTransfer", required_argument, nullptr, OPT_COLOR_TRANSFER},
72 {"colorMatrix", required_argument, nullptr, OPT_COLOR_MATRIX},
73 {"iFrameInterval", required_argument, nullptr, OPT_I_FRAME_INTERVAL},
74 {"IDRFrame", required_argument, nullptr, OPT_IDR_FRAME},
75 {"profile", required_argument, nullptr, OPT_PROFILE},
76 {"bitRateMode", required_argument, nullptr, OPT_BITRATE_MODE},
77 {"bitRate", required_argument, nullptr, OPT_BITRATE},
78 {"quality", required_argument, nullptr, OPT_QUALITY},
79 {"rotation", required_argument, nullptr, OPT_ROTATION},
80 {"flushCnt", required_argument, nullptr, OPT_FLUSH_CNT},
81 {nullptr, no_argument, nullptr, OPT_UNKONWN},
82 };
83
ShowUsage()84 void ShowUsage()
85 {
86 std::cout << "HCodec Test Options:" << std::endl;
87 std::cout << " --help help info." << std::endl;
88 std::cout << " -i, --in file name for input file." << std::endl;
89 std::cout << " -w, --width video width." << std::endl;
90 std::cout << " -h, --height video height." << std::endl;
91 std::cout << " --testCodecBase 1 is test codecbase api, 0 is test capi" << std::endl;
92 std::cout << " --isEncoder 1 is test encoder, 0 is test decoder" << std::endl;
93 std::cout << " --repeatCnt repeat test, default is 1" << std::endl;
94 std::cout << " --inputCnt input frame count to encode/decode" << std::endl;
95 std::cout << " --protocol video protocol. 0 is H264, 1 is H265" << std::endl;
96 std::cout << " --pixelFmt video pixel fmt. 1 is I420, 2 is NV12, 3 is NV21, 5 is RGBA" << std::endl;
97 std::cout << " --frameRate video frame rate." << std::endl;
98 std::cout << " --timeout thread timeout(ms). -1 means wait forever" << std::endl;
99 std::cout << " --isBufferMode 0 is surface mode, 1 is buffer mode." << std::endl;
100 std::cout << " --colorRange color range. 1 is full range, 0 is limited range." << std::endl;
101 std::cout << " --colorPrimary color primary. see H.273 standard." << std::endl;
102 std::cout << " --colorTransfer color transfer characteristic. see H.273 standard." << std::endl;
103 std::cout << " --colorMatrix color matrix coefficient. see H.273 standard." << std::endl;
104 std::cout << " --iFrameInterval <0 means only one I frame, =0 means all intra" << std::endl;
105 std::cout << " >0 means I frame interval in milliseconds" << std::endl;
106 std::cout << " --IDRFrame >0 means that the frame is set to an IDR frame." << std::endl;
107 std::cout << " --profile video profile, for 264: 0(baseline), 1(constrained baseline), " << std::endl;
108 std::cout << " 2(constrained high), 3(extended), 4(high), 8(main)" << std::endl;
109 std::cout << " for 265: 0(main)" << std::endl;
110 std::cout << " --bitRateMode bit rate mode for encoder. 0(CBR), 1(VBR), 2(CQ)" << std::endl;
111 std::cout << " --bitRate target encode bit rate (bps)" << std::endl;
112 std::cout << " --quality target encode quality" << std::endl;
113 std::cout << " --rotation rotation angle after decode, eg. 0/90/180/270" << std::endl;
114 std::cout << " --flushCnt total flush count during decoding" << std::endl;
115 }
116
Parse(int argc,char * argv[])117 CommandOpt Parse(int argc, char *argv[])
118 {
119 CommandOpt opt;
120 int c;
121 while ((c = getopt_long(argc, argv, "i:w:h:", g_longOptions, nullptr)) != -1) {
122 switch (c) {
123 case OPT_HELP:
124 ShowUsage();
125 break;
126 case OPT_INPUT:
127 opt.inputFile = string(optarg);
128 break;
129 case OPT_WIDTH:
130 opt.dispW = stol(optarg);
131 break;
132 case OPT_HEIGHT:
133 opt.dispH = stol(optarg);
134 break;
135 case OPT_TEST_CODECBASE_API:
136 opt.testCodecBaseApi = stol(optarg);
137 break;
138 case OPT_IS_ENCODER:
139 opt.isEncoder = stol(optarg);
140 break;
141 case OPT_REPEAT_CNT:
142 opt.repeatCnt = stol(optarg);
143 break;
144 case OPT_INPUT_FRAME_CNT:
145 opt.inputCnt = stol(optarg);
146 break;
147 case OPT_PROTOCOL:
148 opt.protocol = static_cast<CodeType>(stol(optarg));
149 break;
150 case OPT_PIXEL_FMT:
151 opt.pixFmt = static_cast<VideoPixelFormat>(stol(optarg));
152 break;
153 case OPT_FRAME_RATE:
154 opt.frameRate = stol(optarg);
155 break;
156 case OPT_TIME_OUT:
157 opt.timeout = stol(optarg);
158 break;
159 case OPT_IS_BUFFER_MODE:
160 opt.isBufferMode = stol(optarg);
161 break;
162 case OPT_COLOR_RANGE:
163 opt.rangeFlag = stol(optarg);
164 break;
165 case OPT_COLOR_PRIMARY:
166 opt.primary = static_cast<ColorPrimary>(stol(optarg));
167 break;
168 case OPT_COLOR_TRANSFER:
169 opt.transfer = static_cast<TransferCharacteristic>(stol(optarg));
170 break;
171 case OPT_COLOR_MATRIX:
172 opt.matrix = static_cast<MatrixCoefficient>(stol(optarg));
173 break;
174 case OPT_I_FRAME_INTERVAL:
175 opt.iFrameInterval = stol(optarg);
176 break;
177 case OPT_IDR_FRAME:
178 opt.numIdrFrame = stol(optarg);
179 break;
180 case OPT_PROFILE:
181 opt.profile = stol(optarg);
182 break;
183 case OPT_BITRATE_MODE:
184 opt.rateMode = static_cast<VideoEncodeBitrateMode>(stol(optarg));
185 break;
186 case OPT_BITRATE:
187 opt.bitRate = stol(optarg);
188 break;
189 case OPT_QUALITY:
190 opt.quality = stol(optarg);
191 break;
192 case OPT_ROTATION:
193 opt.rotation = static_cast<VideoRotation>(stol(optarg));
194 break;
195 case OPT_FLUSH_CNT:
196 opt.flushCnt = stol(optarg);
197 break;
198 default:
199 break;
200 }
201 }
202 return opt;
203 }
204
Print() const205 void CommandOpt::Print() const
206 {
207 printf("-----------------------------\n");
208 printf("test %s, %s, repeat %u times\n",
209 testCodecBaseApi ? "codecbase api" : "capi",
210 isEncoder ? "encoder" : "decoder",
211 repeatCnt);
212 printf("read inputFile %s up to %u frames\n", inputFile.c_str(), inputCnt);
213 printf("%u x %u @ %u fps\n", dispW, dispH, frameRate);
214 printf("protocol = %s, pixFmt = %d\n", (protocol == H264) ? "264" : "265", pixFmt);
215 printf("%s mode\n", isBufferMode ? "buffer" : "surface");
216 printf("timeout = %d\n", timeout);
217 printf("range %d, primary %d, transfer %d, matrix %d\n", rangeFlag, primary, transfer, matrix);
218 printf("I frame interval %d\n", iFrameInterval);
219 printf("profile %d\n", profile);
220 printf("bit rate mode %d, bit rate %" PRId64 ", quality %u\n", rateMode, bitRate, quality);
221 printf("rotation angle %u\n", rotation);
222 printf("flush cnt %d\n", flushCnt);
223 printf("Set NO.%u frame as the IDR Frame\n", numIdrFrame);
224 printf("-----------------------------\n");
225 }
226 }