1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <assert.h>
6
7 #include "move_detection.h"
8
9 void rga_ds(unsigned char* pin, unsigned char* pout, int w, int h, int w_ds, int h_ds);
10 void down_sampling(unsigned char* im, unsigned char* img_down_sam, int height, int width);
11 void down_sampling1(unsigned char* im, unsigned char* img_down_sam, int height, int width);
12
down_sampling(unsigned char * im,unsigned char * img_down_sam,int height,int width)13 void down_sampling(unsigned char* im, unsigned char* img_down_sam, int height, int width)
14 {
15 int i, j;
16
17 height = height / 3;
18 width = width / 3;
19
20 for (i = 0; i < height; i++)
21 {
22 for (j = 0; j < width; j++)
23 {
24 img_down_sam[i*width + j] = (im[(i * 3 + 0)*width * 3 + j * 3 + 0] + im[(i * 3 + 0)*width * 3 + j * 3 + 1] + im[(i * 3 + 0)*width * 3 + j * 3 + 2] + im[(i * 3 + 1)*width * 3 + j * 3 + 0] + im[(i * 3 + 1)*width * 3 + j * 3 + 1] + im[(i * 3 + 1)*width * 3 + j * 3 + 2] + im[(i * 3 + 2)*width * 3 + j * 3 + 0] + im[(i * 3 + 2)*width * 3 + j * 3 + 1] + im[(i * 3 + 2)*width * 3 + j * 3 + 2]) / 9;
25 //img_down_sam[i*width + j] = ( im[(i * 3 + 0)*width * 3 + j * 3 + 1] +
26 //im[(i * 3 + 1)*width * 3 + j * 3 + 0] + im[(i * 3 + 1)*width * 3 + j * 3 + 1] + im[(i * 3 + 1)*width * 3 + j * 3 + 2] +
27 //im[(i * 3 + 2)*width * 3 + j * 3 + 1]) / 5;
28 }
29 }
30 }
down_sampling1(unsigned char * im,unsigned char * img_down_sam,int height,int width)31 void down_sampling1(unsigned char* im, unsigned char* img_down_sam, int height, int width)
32 {
33 int i, j;
34
35 height = height / 2;
36 width = width / 2;
37
38 for (i = 0; i < height; i++)
39 {
40 for (j = 0; j < width; j++)
41 {
42 img_down_sam[i*width + j] = im[(i * 2 + 1)*width * 2 + j * 2 + 1];
43 }
44 }
45 }
46
47 #define TIME_DEBUG(FUNC) {\
48 struct timeval tv1, tv2;\
49 gettimeofday(&tv1, NULL);\
50 FUNC; \
51 gettimeofday(&tv2, NULL);\
52 printf("elapse %ld ms\n", (tv2.tv_sec - tv1.tv_sec) * 1000 + (tv2.tv_usec - tv1.tv_usec) / 1000);\
53 }
54
55 #define HW_MAIN 1
56
57
58 #if HW_MAIN
59
60 const char yuv_input[200] = "/data/314_1080p_10000kbps_h265_300frames.yuv";
61 const int yuv_w = 1920;
62 const int yuv_h = 1080;
63 int is_single_ref = 0;
64
hw_simu_top()65 int hw_simu_top()
66 {
67 unsigned char datain[yuv_w * yuv_h];
68 unsigned char datain_uv[yuv_w * yuv_h / 2];
69 INFO_LIST info_list[4096];
70 ROI_INFO roi_in;
71
72 FILE *fidin = fopen(yuv_input, "rb");
73 //FILE *fod_debug3 = fopen("info_list.txt", "wb");
74 int sz;
75 int frame;
76 int i;
77 int frame_size_y, frame_size_uv;
78 ROI_INFO* p_roi_in = (ROI_INFO*)&roi_in;
79 int width, height, width_ds, height_ds;
80 struct md_ctx *md_ctx;
81 void *img_down_sam;
82
83 width = yuv_w;
84 height = yuv_h;
85 width_ds = yuv_w/3;
86 height_ds = yuv_h/3;
87 roi_in.flag = 1;
88 roi_in.is_move = 0;
89 roi_in.up_left[0] = 0;//y
90 roi_in.up_left[1] = 0;//x
91 roi_in.down_right[0] = yuv_h-1;//y
92 roi_in.down_right[1] = yuv_w-1;//x
93
94 fseek(fidin, 0L, SEEK_END);
95 sz = ftell(fidin);
96 fseek(fidin, 0L, SEEK_SET);
97 frame = sz / (width * height * 3 / 2);
98 frame_size_y = width*height;
99 frame_size_uv = frame_size_y/2;
100
101 md_ctx = move_detection_init(width, height, width_ds, height_ds, is_single_ref);
102 img_down_sam = (unsigned char *)malloc(width_ds * height_ds);
103
104 fread(datain, sizeof(unsigned char), frame_size_y, fidin);
105 fread(datain_uv, sizeof(unsigned char), frame_size_uv, fidin);
106
107 rga_ds(datain, img_down_sam, width, height, width_ds, height_ds);
108 memset(info_list, 0, sizeof(info_list));
109 move_detection(md_ctx, img_down_sam, p_roi_in, info_list);
110 //fwrite(info_list, sizeof(INFO_LIST), 4096, fod_debug3);
111
112 TIME_DEBUG(usleep(1000*1000));
113 int retry_count = 100;
114 retry:
115 printf("Test: is_single_ref %d, raw %d x %d, ds %d x %d\n", is_single_ref, width, height, width_ds, height_ds);
116 for (i = 1; i < frame - 1; i++)
117 {
118 fread(datain, sizeof(unsigned char), frame_size_y, fidin);
119 fread(datain_uv, sizeof(unsigned char), frame_size_uv, fidin);
120
121 rga_ds(datain, img_down_sam, width, height, width_ds, height_ds);
122 memset(info_list, 0, sizeof(info_list));
123 TIME_DEBUG(move_detection(md_ctx, img_down_sam, p_roi_in, info_list));
124 //fwrite(info_list, sizeof(INFO_LIST), 4096, fod_debug3);
125 }
126 if (retry_count-- > 0) {
127 retry_count = 1000;
128 fseek(fidin, 0L, SEEK_SET);
129 goto retry;
130 }
131
132 move_detection_deinit(md_ctx);
133 fclose(fidin);
134 //fclose(fod_debug3);
135
136 return 0;
137 }
138
139 #endif
140
141
rga_ds(unsigned char * pin,unsigned char * pout,int w,int h,int w_ds,int h_ds)142 void rga_ds(unsigned char* pin, unsigned char* pout, int w, int h, int w_ds, int h_ds)
143 {
144 int i, j;
145
146 if(3*w_ds != w)
147 assert(0);
148 if(3*h_ds != h)
149 assert(0);
150
151 for (i = 0; i < h_ds; i++)
152 {
153 for (j = 0; j < w_ds; j++)
154 {
155 pout[i*w_ds + j] = (pin[(i * 3 + 0)*w_ds * 3 + j * 3 + 0] + pin[(i * 3 + 0)*w_ds * 3 + j * 3 + 1] + pin[(i * 3 + 0)*w_ds * 3 + j * 3 + 2] +
156 pin[(i * 3 + 1)*w_ds * 3 + j * 3 + 0] + pin[(i * 3 + 1)*w_ds * 3 + j * 3 + 1] + pin[(i * 3 + 1)*w_ds * 3 + j * 3 + 2] +
157 pin[(i * 3 + 2)*w_ds * 3 + j * 3 + 0] + pin[(i * 3 + 2)*w_ds * 3 + j * 3 + 1] + pin[(i * 3 + 2)*w_ds * 3 + j * 3 + 2]) / 9;
158 }
159 }
160 }
161
162 #include<pthread.h>
thr_fn(void * arg)163 void *thr_fn(void *arg)
164 {
165 printf("new thread\n");
166 hw_simu_top();
167 }
168
169 //C:\Users\rock\Desktop\ipc\1080\isp_test_2.yuv 1080 1920 isp_test_2.yuv C:\Users\rock\Desktop\rk/960x540\isp_test_2.yuv
main(int argc,char ** argv)170 int main(int argc, char** argv) {
171 int threads = 1;
172 pthread_t *tid;
173
174 if (argc != 3) {
175 printf("example: \n \
176 test_md 0 2: test multi ref with 2 thread\n \
177 test_md 1 2: test single ref with 2 thread\n");
178 return -1;
179 }
180 is_single_ref = atoi(argv[1]);
181 threads = atoi(argv[2]);
182 threads = (threads < 1) ? 1 : threads;
183
184 tid = malloc(sizeof(pthread_t) * threads);
185
186 for (int i = 0; i < threads; i++) {
187 int err = pthread_create(&tid[i], NULL, thr_fn, NULL);
188 if (err != 0) {
189 printf("can't create thread: %s\n", strerror(err));
190 return -1;
191 }
192 }
193
194 for (int i = 0; i < threads; i++)
195 pthread_join(tid[i],NULL);
196
197 free(tid);
198 return 0;
199 }
200