1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <string.h>
18
19 #include "mpp_log.h"
20 #include "mpp_frame.h"
21 #include "mpp_common.h"
22 #include "mpp_err.h"
23
24 #include "utils.h"
25 #include "rga_api.h"
26
27 #define MAX_NAME_LENGTH 256
28
29 typedef struct RgaTestCmd_t {
30 RK_U32 src_w;
31 RK_U32 src_h;
32 RK_U32 dst_w;
33 RK_U32 dst_h;
34 MppFrameFormat src_fmt;
35 MppFrameFormat dst_fmt;
36 char input_file[MAX_NAME_LENGTH];
37 char output_file[MAX_NAME_LENGTH];
38 RK_U32 have_input;
39 RK_U32 have_output;
40 } RgaTestCmd;
41
usage()42 void usage()
43 {
44 mpp_log("usage:./rga_test -i input -o output_file"
45 " -w src_w -h src_h -f src_fmt -dst_w dst_w"
46 " -dst_h dst_h -dst_fmt dst_fmt\n");
47 }
48
rga_test_parse_options(int argc,char ** argv,RgaTestCmd * cmd)49 static RK_S32 rga_test_parse_options(int argc, char **argv, RgaTestCmd *cmd)
50 {
51 const char *opt;
52 const char *next;
53 RK_S32 optindex = 1;
54 RK_S32 handleoptions = 1;
55 RK_S32 err = MPP_NOK;
56
57 if (argc < 2 || cmd == NULL) {
58 err = 1;
59 return err;
60 }
61
62 while (optindex < argc) {
63 opt = (const char *) argv[optindex++];
64 next = (const char *) argv[optindex];
65
66 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
67 if (opt[1] == '-') {
68 if (opt[2] != '\0') {
69 opt++;
70 } else {
71 handleoptions = 0;
72 break;
73 }
74 }
75
76 opt++;
77
78 switch (*opt) {
79 case 'i' :
80 if (next) {
81 strncpy(cmd->input_file, next, MAX_NAME_LENGTH - 1);
82 cmd->input_file[strlen(next)] = '\0';
83 cmd->have_input = 1;
84 } else {
85 mpp_err("input file is invalid\n");
86 goto PARSE_ERR;
87 }
88 break;
89 case 'o' :
90 if (next) {
91 strncpy(cmd->output_file, next, MAX_NAME_LENGTH - 1);
92 cmd->output_file[strlen(next)] = '\0';
93 cmd->have_output = 1;
94 } else {
95 mpp_err("output file is invalid\n");
96 goto PARSE_ERR;
97 }
98 break;
99 case 'w' :
100 if (next) {
101 cmd->src_w = atoi(next);
102 } else {
103 mpp_err("src width is invalid\n");
104 goto PARSE_ERR;
105 }
106 break;
107 case 'h' :
108 if (next) {
109 cmd->src_h = atoi(next);
110 } else {
111 mpp_err("src height is invalid\n");
112 goto PARSE_ERR;
113 }
114 break;
115 case 'f' :
116 if (next) {
117 cmd->src_fmt = (MppFrameFormat) atoi(next);
118 err = ((cmd->src_fmt >= MPP_FMT_YUV_BUTT && cmd->src_fmt < MPP_FRAME_FMT_RGB) ||
119 cmd->src_fmt >= MPP_FMT_RGB_BUTT);
120 } else {
121 mpp_err("src fmt is invalid\n");
122 goto PARSE_ERR;
123 }
124 break;
125 case 'd' :
126 if ((*(opt + 1) != '\0') && !strncmp(opt, "dst_w", 5)) {
127 cmd->dst_w = atoi(next);
128 } else if ((*(opt + 1) != '\0') && !strncmp(opt, "dst_h", 5)) {
129 cmd->dst_h = atoi(next);
130 } else if ((*(opt + 1) != '\0') && !strncmp(opt, "dst_fmt", 5)) {
131 cmd->dst_fmt = (MppFrameFormat) atoi(next);
132 } else {
133 mpp_err("dst parameters is invalid\n");
134 goto PARSE_ERR;
135 }
136 break;
137 }
138 }
139 }
140 PARSE_ERR:
141 return err;
142 }
143
main(int argc,char ** argv)144 int main(int argc, char **argv)
145 {
146 mpp_log("rga test unit\n");
147
148 MPP_RET ret = MPP_NOK;
149 RgaTestCmd cmd;
150 MppBuffer src_buf = NULL;
151 MppBuffer dst_buf = NULL;
152 MppFrame src_frm = NULL;
153 MppFrame dst_frm = NULL;
154 FILE *fin = NULL;
155 FILE *fout = NULL;
156 RgaCtx ctx = NULL;
157 RK_U32 frame_count = 0;
158
159 void *ptr;
160 RK_U32 src_w;
161 RK_U32 src_h;
162 RK_U32 dst_w;
163 RK_U32 dst_h;
164 RK_U32 src_size;
165 RK_U32 dst_size;
166
167 memset(&cmd, 0, sizeof(cmd));
168 if (rga_test_parse_options(argc, argv, &cmd)) {
169 usage();
170 goto END;
171 }
172
173 mpp_log("src w:%d h:%d fmt:%d dst w:%d h:%d fmt:%d input:%s output:%s\n",
174 cmd.src_w, cmd.src_h, cmd.src_fmt,
175 cmd.dst_w, cmd.dst_h, cmd.dst_fmt,
176 cmd.input_file, cmd.output_file);
177
178 src_w = cmd.src_w;
179 src_h = cmd.src_h;
180 dst_w = cmd.dst_w;
181 dst_h = cmd.dst_h;
182 src_size = src_w * src_h * 4;
183 dst_size = dst_w * dst_h * 4;
184
185 if (cmd.have_input) {
186 fin = fopen(cmd.input_file, "r");
187 if (!fin) {
188 mpp_log("open input file %s failed\n", cmd.input_file);
189 goto END;
190 }
191 }
192
193 if (cmd.have_output) {
194 fout = fopen(cmd.output_file, "w+");
195 if (!fout) {
196 mpp_log("open output file %s failed\n", cmd.output_file);
197 goto END;
198 }
199 }
200
201 ret = mpp_buffer_get(NULL, &src_buf, src_size);
202 if (ret) {
203 mpp_err("failed to get src buffer %d with size %d\n", ret, src_size);
204 goto END;
205 }
206
207 ret = mpp_buffer_get(NULL, &dst_buf, dst_size);
208 if (ret) {
209 mpp_err("failed to get dst buffer %d with size %d\n", ret, dst_size);
210 goto END;
211 }
212
213 ret = mpp_frame_init(&src_frm);
214 if (ret) {
215 mpp_err("failed to init src frame\n");
216 goto END;
217 }
218
219 ret = mpp_frame_init(&dst_frm);
220 if (ret) {
221 mpp_err("failed to init dst frame\n");
222 goto END;
223 }
224
225 ptr = mpp_buffer_get_ptr(src_buf);
226 if (cmd.have_input) {
227 ret = read_image((RK_U8 *)ptr, fin, cmd.src_w, cmd.src_h,
228 cmd.src_w, cmd.src_h, cmd.dst_fmt);
229 if (ret) {
230 mpp_err("failed to read input file ret:%d\n", ret);
231 goto END;
232 }
233 } else {
234 ret = fill_image((RK_U8 *)ptr, cmd.src_w, cmd.src_h,
235 cmd.src_w, cmd.src_h, cmd.src_fmt, frame_count);
236 if (ret) {
237 mpp_err("failed to fill input buffer ret:%d\n", ret);
238 goto END;
239 }
240 }
241
242 mpp_frame_set_buffer(src_frm, src_buf);
243 mpp_frame_set_width(src_frm, src_w);
244 mpp_frame_set_height(src_frm, src_h);
245 mpp_frame_set_hor_stride(src_frm, MPP_ALIGN(src_w, 16));
246 mpp_frame_set_ver_stride(src_frm, MPP_ALIGN(src_h, 16));
247 mpp_frame_set_fmt(src_frm, cmd.src_fmt);
248
249 mpp_frame_set_buffer(dst_frm, dst_buf);
250 mpp_frame_set_width(dst_frm, dst_w);
251 mpp_frame_set_height(dst_frm, dst_h);
252 mpp_frame_set_hor_stride(dst_frm, MPP_ALIGN(dst_w, 16));
253 mpp_frame_set_ver_stride(dst_frm, MPP_ALIGN(dst_h, 16));
254 mpp_frame_set_fmt(dst_frm, cmd.dst_fmt);
255
256 ret = rga_init(&ctx);
257 if (ret) {
258 mpp_err("init rga context failed %d\n", ret);
259 goto END;
260 }
261
262 // start copy process
263 ret = rga_control(ctx, RGA_CMD_INIT, NULL);
264 if (ret) {
265 mpp_err("rga cmd init failed %d\n", ret);
266 goto END;
267 }
268
269 ret = rga_control(ctx, RGA_CMD_SET_SRC, src_frm);
270 if (ret) {
271 mpp_err("rga cmd setup source failed %d\n", ret);
272 goto END;
273 }
274
275 ret = rga_control(ctx, RGA_CMD_SET_DST, dst_frm);
276 if (ret) {
277 mpp_err("rga cmd setup destination failed %d\n", ret);
278 goto END;
279 }
280
281 ret = rga_control(ctx, RGA_CMD_RUN_SYNC, NULL);
282 if (ret) {
283 mpp_err("rga cmd process copy failed %d\n", ret);
284 goto END;
285 }
286
287 // start field duplicate process
288 mpp_frame_set_buffer(src_frm, dst_buf);
289 mpp_frame_set_width(src_frm, dst_w);
290 mpp_frame_set_height(src_frm, dst_h / 2);
291 mpp_frame_set_hor_stride(src_frm, MPP_ALIGN(dst_w, 16) * 2);
292 mpp_frame_set_ver_stride(src_frm, MPP_ALIGN(src_h, 16) / 2);
293 mpp_frame_set_fmt(src_frm, cmd.dst_fmt);
294
295 ret = rga_control(ctx, RGA_CMD_INIT, NULL);
296 if (ret) {
297 mpp_err("rga cmd init failed %d\n", ret);
298 goto END;
299 }
300
301 ret = rga_control(ctx, RGA_CMD_SET_SRC, src_frm);
302 if (ret) {
303 mpp_err("rga cmd setup source failed %d\n", ret);
304 goto END;
305 }
306
307 ret = rga_control(ctx, RGA_CMD_SET_DST, dst_frm);
308 if (ret) {
309 mpp_err("rga cmd setup destination failed %d\n", ret);
310 goto END;
311 }
312
313 ret = rga_control(ctx, RGA_CMD_RUN_SYNC, NULL);
314 if (ret) {
315 mpp_err("rga cmd process copy failed %d\n", ret);
316 goto END;
317 }
318
319 ret = rga_deinit(ctx);
320 if (ret) {
321 mpp_err("deinit rga context failed %d\n", ret);
322 goto END;
323 }
324
325 if (cmd.have_output)
326 dump_mpp_frame_to_file(dst_frm, fout);
327
328 END:
329 if (src_frm)
330 mpp_frame_deinit(&src_frm);
331
332 if (dst_frm)
333 mpp_frame_deinit(&dst_frm);
334
335 if (src_buf)
336 mpp_buffer_put(src_buf);
337
338 if (dst_buf)
339 mpp_buffer_put(dst_buf);
340
341 if (fin)
342 fclose(fin);
343
344 if (fout)
345 fclose(fout);
346
347 mpp_log("rga test exit ret %d\n", ret);
348 return 0;
349 }
350