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