From a814574a1029b1f40b1727f1ba4130facb6cf338 Mon Sep 17 00:00:00 2001 From: Yeonji Date: Thu, 13 Sep 2018 01:34:19 +0800 Subject: [PATCH] First view of Bad Apple!!! --- Makefile | 2 +- src/bmp.c | 2 +- src/image_edge_dect.c | 4 +- src/main.c | 37 +++++---- src/path.c | 174 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 190 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 58c1d4c..39c80fc 100644 --- a/Makefile +++ b/Makefile @@ -44,4 +44,4 @@ clean: rm -f ${DIR_BIN}/${TARGET} run: - @time ${BIN_TARGET} \ No newline at end of file + @${BIN_TARGET} diff --git a/src/bmp.c b/src/bmp.c index 727c194..d7a1819 100644 --- a/src/bmp.c +++ b/src/bmp.c @@ -23,7 +23,7 @@ SOFTWARE. #include #include #include -#include +#include diff --git a/src/image_edge_dect.c b/src/image_edge_dect.c index e25c74f..92a5e81 100644 --- a/src/image_edge_dect.c +++ b/src/image_edge_dect.c @@ -46,12 +46,12 @@ int image_edge_dect(image_t * img) conv_tmp += image_pixel(img, x0 + 1, y0)[0]; conv_tmp += image_pixel(img, x0 + 1, y0 + 1)[0]; - conv_tmp += image_pixel(img, x0 + 1, y0 + 1)[0]; + conv_tmp += image_pixel(img, x0 + 1, y0 - 1)[0]; conv_tmp += image_pixel(img, x0, y0 + 1)[0]; conv_tmp -= image_pixel(img, x0 - 1, y0)[0]; conv_tmp -= image_pixel(img, x0 - 1, y0 - 1)[0]; - conv_tmp -= image_pixel(img, x0 - 1, y0 - 1)[0]; + conv_tmp -= image_pixel(img, x0 - 1, y0 + 1)[0]; conv_tmp -= image_pixel(img, x0, y0 - 1)[0]; conv_tmp = (float)fabs(conv_tmp / 8.0); diff --git a/src/main.c b/src/main.c index 6d36b43..6cf24fd 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,7 @@ SOFTWARE. #include #include #include - -#include +#include int frame_cnt = 0; int frame_index = 0; @@ -54,7 +53,7 @@ int main(void) create_tmp_dir(); - transform_video("./test-y.mp4"); + transform_video("./input.mp4"); frame_cnt = get_frame_cnt(); @@ -63,10 +62,7 @@ int main(void) sa.sa_handler = &show_proc; sa.sa_flags = SA_RESTART; sigaction(SIGALRM, &sa, NULL); - alarm(1); - - ProfilerStart("./test.prof"); - ProfilerRegisterThread(); + alarm(3); wav = wave_new(2, 48000, (48000 / 24) * frame_cnt); @@ -88,9 +84,11 @@ int main(void) image_binary(img); - sprintf(path, "./tmp/frames_proc/v-%05d.bmp", frame_index); + // sprintf(path, "./tmp/frames_proc/v-%05d.bmp", frame_index); - bmp_save(img, path); + // bmp_save(img, path); + + gen_path(img, wav, frame_index - 1); image_free(img); @@ -100,17 +98,18 @@ int main(void) fprintf(stderr, "\nProcess end.\n"); - ProfilerStop(); - alarm(0); + fprintf(stderr, "\nSaving.\n"); + wave_save(wav, "./out.wav"); wave_free(wav); - /* - system("rm -rf ./tmp"); - */ + fprintf(stderr, "\nCleaning.\n"); + + remove_tmp_dir(); + return 0; } @@ -125,15 +124,15 @@ void show_proc(int signal) static int time_m = 0; static int time_h = 0; - int fps; + float fps; float kbps; float speed; - fps = frame_index - priv_index; + fps = (frame_index - priv_index) / 3.0; kbps = (fps * frame_s)/1000.0; priv_index = frame_index; speed = (float)fps / (float)24; - time_s ++; + time_s += 3; if (time_s == 60) { time_s = 0; @@ -145,9 +144,9 @@ void show_proc(int signal) time_h ++; } - fprintf(stderr, "\rframe= %d fps= %d time=%02d:%02d:%02ds bitrate=%.1fkbits/s speed=%.2fx SIG=%d",\ + fprintf(stderr, "\rframe= %d fps= %.1f time=%02d:%02d:%02ds bitrate=%.1fkbits/s speed=%.2fx SIG=%d",\ frame_index, fps, time_h, time_m, time_s, kbps, speed, signal); fflush(stderr); - alarm(1); + alarm(3); } diff --git a/src/path.c b/src/path.c index 0cac004..ee1228c 100644 --- a/src/path.c +++ b/src/path.c @@ -19,25 +19,187 @@ SOFTWARE. ********************************************************************************/ #include +#include +#include +#include +#include +#include + +extern inline float * image_pixel(image_t * img, int32_t x, int32_t y); int gen_path(image_t * img, wave_t * wav, int frame_no) { + float * point; + + static int graph_size = 0; + + /* store point on origin graph */ + /* using static to avoid too much malloc */ static int * origin_x_pos = NULL; static int * origin_y_pos = NULL; - static int * origin_dist = NULL; - static int * origin_avail = NULL; - static int * sorted_x = NULL; - static int * sorted_y = NULL; + static int * origin_dist = NULL; /* store distence from last point */ + static int * origin_avail = NULL; /* judge if is avaliable */ - int graph_size = 0; + /* origin point count */ int point_cnt; + /* scan point */ + int point_index; + + /* avail point */ + int point_avail; + + float point_distant; + + /* store sorted path */ + static int * sorted_x = NULL; + static int * sorted_y = NULL; + int sorted_length = 0; + + static int * wave_x = NULL; + static int * wave_y = NULL; int wav_length = (48000 / 24); + int wav_index; + int wav_bias; + float adjust_rate; + float zoom_rate; + + /* store the last point */ + /* using static to make frames continuity */ static int last_x = 0; static int last_y = 0; + + int dx,dy; + + int dist_min; + int dist_min_index; + + /* scan graph */ + int scan_x; + int scan_y; + + + /* initlize some static variable */ + if (graph_size == 0) { + graph_size = img->width * img->height; + + origin_x_pos = malloc(graph_size * sizeof(int)); + origin_y_pos = malloc(graph_size * sizeof(int)); + origin_dist = malloc(graph_size * sizeof(int)); + origin_avail = malloc(graph_size * sizeof(int)); + + sorted_x = malloc(graph_size * sizeof(int)); + sorted_y = malloc(graph_size * sizeof(int)); + + wave_x = malloc(wav_length * sizeof(int)); + wave_y = malloc(wav_length * sizeof(int)); + } + + /* reset avail points */ + for (point_cnt = graph_size - 1; point_cnt >= 0; point_cnt --) { + origin_avail[point_cnt] = 0; + } + + point_cnt = 0; + + /* find all points */ + for (scan_x = 1; scan_x < img->width; scan_x++) { + for (scan_y = 1; scan_y < img->width; scan_y++) { + point = image_pixel(img, scan_x, scan_y); + if (*point > 128){ + origin_x_pos[point_cnt] = scan_x; + origin_y_pos[point_cnt] = scan_y; + origin_dist[point_cnt] = 65536; + origin_avail[point_cnt] = 1; + + //printf("(%d,%d)\n", origin_x_pos[point_cnt], origin_y_pos[point_cnt]); + point_cnt ++; + + } + } + } + + /* parse every point */ + for (point_avail = point_cnt; point_avail > 0; point_avail--) { + + /* caculate distance of every available point */ + for (point_index = 0; point_index < point_cnt; point_index++) { + + if ( origin_avail[point_index] == 1 ) { + + dx = abs((origin_x_pos[point_index] - last_x)); + dy = abs((origin_y_pos[point_index] - last_y)); + + point_distant = sqrt((float)dx * (float)dx + (float)dy * (float)dy); + + origin_dist[point_index] = (int)point_distant; + } + } + + /* find the nearist point */ + + dist_min = img->width + img->height; + dist_min_index = 0; + + for (point_index = 0; point_index < point_cnt; point_index++) { + + if ( origin_avail[point_index] == 1 ) { + + if ( origin_dist[point_index] < dist_min ) { + dist_min = origin_dist[point_index]; + dist_min_index = point_index; + } + + } + } + + /* add to sorted array and remove from available */ + sorted_x[sorted_length] = origin_x_pos[dist_min_index]; + sorted_y[sorted_length] = origin_y_pos[dist_min_index]; + + origin_avail[dist_min_index] = 0; + + last_x = origin_x_pos[dist_min_index]; + last_y = origin_y_pos[dist_min_index]; + + sorted_length ++; + + } + + + adjust_rate = (float)sorted_length / (float)wav_length; + zoom_rate = 65535 / (float)(img->width); + + wav_bias = frame_no * (48000 / 24); + + for (wav_index = 0; wav_index < wav_length; wav_index++) { + point_index = wav_index * adjust_rate; + wav->data[0][wav_bias] = zoom_rate * (sorted_x[point_index] - (img->width)/2); + wav->data[1][wav_bias] = zoom_rate * (sorted_y[point_index] - (img->height)/2); + + wav_bias ++; + } + + return 0; } -int main(void) { +int __attribute__((weak)) main(void) +{ + image_t * img; + wave_t * wav; + int frame; + + img = bmp_read("./tmp/frames_proc/v-00530.bmp"); + + wav = wave_new(2, 48000, (48000 / 24) * 20); + + for (frame = 0; frame < 20; frame++) { + printf("Frame_no: %d\n",frame); + gen_path(img, wav, frame); + } + + wave_save(wav, "./out.wav"); + return 0; }