1 /*
2 * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
3 * Authors:
4 * YuQiaowei <cerf.yu@rock-chips.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <cstddef>
23 #include <cmath>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28
29 #include "RgaUtils.h"
30
get_cur_us()31 int64_t get_cur_us() {
32 struct timeval tv;
33 gettimeofday(&tv,NULL);
34 return tv.tv_sec * 1000000 + tv.tv_usec;
35 }
36
get_cur_ms()37 int64_t get_cur_ms() {
38 struct timeval tv;
39 gettimeofday(&tv,NULL);
40 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
41 }
42
draw_rgba(char * buffer,int width,int height)43 void draw_rgba(char *buffer, int width, int height) {
44 for (int i = 0; i < height; i++) {
45 for (int j = 0; j < width/4; j++) {
46 buffer[(i*width*4) + j*4 + 0] = 0xff; //R
47 buffer[(i*width*4) + j*4 + 1] = 0x00; //G
48 buffer[(i*width*4) + j*4 + 2] = 0x00; //B
49 buffer[(i*width*4) + j*4 + 3] = 0xff; //A
50 }
51 for (int j = width/4; j < width/4*2; j++) {
52 buffer[(i*width*4) + j*4 + 0] = 0x00;
53 buffer[(i*width*4) + j*4 + 1] = 0xff;
54 buffer[(i*width*4) + j*4 + 2] = 0x00;
55 buffer[(i*width*4) + j*4 + 3] = 0xff;
56 }
57 for (int j = width/4*2; j < width/4*3; j++) {
58 buffer[(i*width*4) + j*4 + 0] = 0x00;
59 buffer[(i*width*4) + j*4 + 1] = 0x00;
60 buffer[(i*width*4) + j*4 + 2] = 0xff;
61 buffer[(i*width*4) + j*4 + 3] = 0xff;
62 }
63 for (int j = width/4*3; j < width; j++) {
64 buffer[(i*width*4) + j*4 + 0] = 0xff;
65 buffer[(i*width*4) + j*4 + 1] = 0xff;
66 buffer[(i*width*4) + j*4 + 2] = 0xff;
67 buffer[(i*width*4) + j*4 + 3] = 0xff;
68 }
69 }
70 }
71
draw_YUV420(char * buffer,int width,int height)72 void draw_YUV420(char *buffer, int width, int height) {
73 /* Y channel */
74 memset(buffer, 0xa8, width * height / 2);
75 memset(buffer + width * height / 2, 0x54, width * height / 2);
76 /* UV channel */
77 memset(buffer + width * height, 0x80, width * height / 4);
78 memset(buffer + (int)(width * height * 1.25), 0x30, width * height / 4);
79 }
80
draw_YUV422(char * buffer,int width,int height)81 void draw_YUV422(char *buffer, int width, int height) {
82 /* Y channel */
83 memset(buffer, 0xa8, width * height / 2);
84 memset(buffer + width * height / 2, 0x54, width * height / 2);
85 /* UV channel */
86 memset(buffer + width * height, 0x80, width * height / 2);
87 memset(buffer + (int)(width * height * 1.5), 0x30, width * height / 2);
88 }
89
draw_gray256(char * buffer,int width,int height)90 void draw_gray256(char *buffer, int width, int height) {
91 for (int i = 0; i < height; i++) {
92 for (int j = 0; j < width/4; j++) {
93 buffer[(i*width*4) + j*4] = 0xa8;
94 }
95 for (int j = width/4; j < width/4*2; j++) {
96 buffer[(i*width*4) + j*4] = 0x80;
97 }
98 for (int j = width/4*2; j < width/4*3; j++) {
99 buffer[(i*width*4) + j*4] = 0x54;
100 }
101 for (int j = width/4*3; j < width; j++) {
102 buffer[(i*width*4) + j*4] = 0x30;
103 }
104 }
105 }
106
read_image_from_fbc_file(void * buf,const char * path,int sw,int sh,int fmt,int index)107 int read_image_from_fbc_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
108 int size;
109 char filePath[100];
110 const char *inputFbcFilePath = "%s/in%dw%d-h%d-%s-fbc.bin";
111
112 snprintf(filePath, 100, inputFbcFilePath,
113 path, index, sw, sh, translate_format_str(fmt));
114
115 FILE *file = fopen(filePath, "rb");
116 if (!file) {
117 fprintf(stderr, "Could not open %s\n", filePath);
118 return -EINVAL;
119 }
120
121 size = sw * sh * get_bpp_from_format(fmt) * 1.5;
122
123 fread(buf, size, 1, file);
124
125 fclose(file);
126
127 return 0;
128 }
129
read_image_from_file(void * buf,const char * path,int sw,int sh,int fmt,int index)130 int read_image_from_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
131 int size;
132 char filePath[100];
133 const char *inputFilePath = "%s/in%dw%d-h%d-%s.bin";
134
135 snprintf(filePath, 100, inputFilePath,
136 path, index, sw, sh, translate_format_str(fmt));
137
138 FILE *file = fopen(filePath, "rb");
139 if (!file) {
140 fprintf(stderr, "Could not open %s\n", filePath);
141 return -EINVAL;
142 }
143
144 size = sw * sh * get_bpp_from_format(fmt);
145
146 fread(buf, size, 1, file);
147
148 fclose(file);
149
150 return 0;
151 }
152
write_image_to_fbc_file(void * buf,const char * path,int sw,int sh,int fmt,int index)153 int write_image_to_fbc_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
154 int size;
155 char filePath[100];
156 const char *outputFbcFilePath = "%s/out%dw%d-h%d-%s-fbc.bin";
157
158 snprintf(filePath, 100, outputFbcFilePath,
159 path, index, sw, sh, translate_format_str(fmt));
160
161 FILE *file = fopen(filePath, "wb+");
162 if (!file) {
163 fprintf(stderr, "Could not open %s\n", filePath);
164 return false;
165 } else {
166 fprintf(stderr, "open %s and write ok\n", filePath);
167 }
168
169 size = sw * sh * get_bpp_from_format(fmt) * 1.5;
170
171 fwrite(buf, size, 1, file);
172
173 fclose(file);
174
175 return 0;
176 }
177
write_image_to_file(void * buf,const char * path,int sw,int sh,int fmt,int index)178 int write_image_to_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
179 int size;
180 char filePath[100];
181 const char *outputFilePath = "%s/out%dw%d-h%d-%s.bin";
182
183 snprintf(filePath, 100, outputFilePath,
184 path, index, sw, sh, translate_format_str(fmt));
185
186 FILE *file = fopen(filePath, "wb+");
187 if (!file) {
188 fprintf(stderr, "Could not open %s\n", filePath);
189 return false;
190 } else {
191 fprintf(stderr, "open %s and write ok\n", filePath);
192 }
193
194 size = sw * sh * get_bpp_from_format(fmt);
195
196 fwrite(buf, size, 1, file);
197
198 fclose(file);
199
200 return 0;
201 }
202