Skip to content

Commit

Permalink
First view of Bad Apple!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonzi committed Sep 12, 2018
1 parent fc61e7f commit a814574
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ clean:
rm -f ${DIR_BIN}/${TARGET}

run:
@time ${BIN_TARGET}
@${BIN_TARGET}
2 changes: 1 addition & 1 deletion src/bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SOFTWARE.
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <image.h>
#include <bmp.h>



Expand Down
4 changes: 2 additions & 2 deletions src/image_edge_dect.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 18 additions & 19 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ SOFTWARE.
#include <bmp.h>
#include <utils.h>
#include <wave.h>

#include <gperftools/profiler.h>
#include <path.h>

int frame_cnt = 0;
int frame_index = 0;
Expand All @@ -54,7 +53,7 @@ int main(void)

create_tmp_dir();

transform_video("./test-y.mp4");
transform_video("./input.mp4");

frame_cnt = get_frame_cnt();

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);
}
174 changes: 168 additions & 6 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,187 @@ SOFTWARE.
********************************************************************************/

#include <path.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <image.h>
#include <bmp.h>

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;
}

0 comments on commit a814574

Please sign in to comment.