...
 
Commits (2)
......@@ -6,6 +6,7 @@ set (EXAMPLE_IMAGES
lena.tiff
small.mp4
grb_2.mp4
test1.mp4
)
......
......@@ -17,6 +17,7 @@ if (BUILD_TESTING)
new_test (kdictionary kdictionary)
new_test (kimage kimage)
#new_test (kvideo_encode_decode kvideo)
new_test (kvideo_color_motion kvideo_color_motion)
new_test (kimage_get_neighborhood kimage_get_neighborhood)
new_test (kimage_get_patch kimage_get_patch)
new_test (khistogram khistogram)
......
#include <stdlib.h>
#include <stdio.h>
// #include <math.h>
#include "kpixel.h"
#include "kvalue.h"
#include "kimage.h"
#include "kvideo.h"
#include "kerror.h"
#include "kmodule.h"
#include "kobject.h"
#include "kfile.h"
#include "internal/kvideo.h"
static const AVMotionVector* motion_vectors (KObject* kobj, int* num_mvs)
{
KVideo* kvid = to_kvideo (kobj);
AVFrameSideData* sd = av_frame_get_side_data(kvid->pFrame, AV_FRAME_DATA_MOTION_VECTORS);
if (!sd) {
*num_mvs = 0;
return NULL;
}
const AVMotionVector* mvs = (const AVMotionVector *)sd->data;
*num_mvs = sd->size / sizeof(*mvs);
return mvs;
}
static void clean_slate (KObject* img)
{
unsigned int i, j, c;
unsigned char* pixels = kimage_raw_pixels (img);
unsigned int width = kimage_width (img);
unsigned int height = kimage_height (img);
for (j=0; j<height; j++) {
for (i=0; i<width; i++) {
for (c=0; c<3; c++) {
pixels[width*height*c + width*j + i] = 0;
}
}
}
}
static KObject* draw_frame (KObject* img, unsigned int offset_x, unsigned int offset_y)
{
unsigned int i, j, row, col, x, y;
unsigned char* pixels = kimage_raw_pixels (img);
unsigned int width = kimage_width (img);
unsigned int height = kimage_height (img);
unsigned int color = 0xFF000000;
unsigned int box_dim = 30;
clean_slate (img);
for (row=0; row<10; row++) {
for (col=0; col<10; col++, color >>= 1) {
for (j=box_dim*(row); j<box_dim*(row+1); j++) {
for (i=box_dim*(col); i<box_dim*(col+1); i++) {
x = (i + offset_x) % width;
y = (j + offset_y) % height;
pixels[width*height*0 + width*y + x] = (0xFF0000 & color) >> 16;
pixels[width*height*1 + width*y + x] = (0x00FF00 & color) >> 8;
pixels[width*height*2 + width*y + x] = (0x0000FF & color);
}
}
if (!color)
color = 0xFF000000;
}
}
return img;
}
//compares two images. If identical, returns 0
//else, returns the mean square difference of pixel values?
static double check (KObject* img, KObject* img2)
{
//check if dimensions, nchan equal
if ( kimage_nchannels(img) != kimage_nchannels(img2)){
printf("nchannels FAIL");
return -1;
}
if ( kimage_width(img) != kimage_width(img2)){
printf("width FAIL");
return -1;
}
if ( kimage_height(img) != kimage_height(img2)){
printf("width FAIL");
return -1;
}
int i, j, c;
double sum = 0;
unsigned int xdim = kimage_dim (img, 0);
unsigned int ydim = kimage_dim (img, 1);
KObject* px = kvalue_new ();
KObject* px2 = kvalue_new ();
for (c=0; c<kimage_nchannels(img); c++)
for (j=0; j<ydim; j++)
for (i=0; i<xdim; i++) {
kimage_value (img, px, c, KCOORDS(i, j));
kimage_value (img2, px2, c, KCOORDS(i, j));
// if (kvalue_get_int (px) != kvalue_get_int (px2)){
// printf("FAIL: %d vs %d\n",kvalue_get_int (px),kvalue_get_int (px2) );
// }
int diff = kvalue_get_int (px) - kvalue_get_int (px2);
sum += diff*diff;
}
kobject_release (px);
kobject_release (px2);
unsigned int totpixels = kimage_nchannels(img)*ydim*xdim;
return sum/totpixels;
}
int main (int argc, char** argv)
{
unsigned int width = 1920;
unsigned int height = 1080;
unsigned int nframe;
unsigned int MOV_DELTA = 5;
KObject* vid;
KObject* correct_img;
KObject* v_img;
KErrorCode kcode;
double sum;
char* filename = "../examples/images/test1.mp4";
kmodule_depend ("tiff");
correct_img = kimage_new (char_px, 3, KDIMS(width, height));
//open video, read in a frame
vid = kfile_read(filename);
if (vid ==NULL){
fprintf(stderr, "Error while trying to open saved:%s\n",filename);
exit(EXIT_FAILURE);
}
//Compare the I frame
kcode = kvideo_frame_next (vid);
if (kcode){
exit(EXIT_FAILURE);
}
nframe = 0;
correct_img = draw_frame (correct_img, MOV_DELTA*nframe, MOV_DELTA*nframe);
v_img = kvideo_frame_to_kimage_decoded(vid);
sum = check (correct_img,v_img);
printf("sum of squares i-frame result: %f\n", sum);
if (sum > 1){
exit(EXIT_FAILURE);
}
kobject_release (v_img);
//Compare the P frame
//first, find the next p-frame
while (kvideo_frame_type(vid) != 'P') {
kcode = kvideo_frame_next (vid);
if (kcode){
exit(EXIT_FAILURE);
}
nframe++;
}
nframe--;
correct_img = draw_frame (correct_img, MOV_DELTA*nframe, MOV_DELTA*nframe);
v_img = kvideo_frame_to_kimage_decoded(vid);
sum = check (correct_img,v_img);
printf("sum of squares p-frame result: %f\n", sum);
if (sum > 21){
exit(EXIT_FAILURE);
}
//compare the P-frame motion vectors:
//expected motion: -5, -5
// print motion vectors
int num_mvs;
int i;
const AVMotionVector* mvs = motion_vectors (vid, &num_mvs);
int del = 15;
if (mvs == NULL){
exit(EXIT_FAILURE);
}
for (i = 0; i < num_mvs; i++) {
const AVMotionVector *mv = &mvs[i];
int xmag = mv->dst_x-mv->src_x;
int ymag = mv->dst_y-mv->src_y;
if (xmag ==0 || ymag ==0)
continue;
if (xmag ==del || ymag ==del)
continue;
if (xmag ==del+1 || ymag ==del+1)
continue;
if (xmag ==del-1 || ymag ==del-1)
continue;
if (xmag ==del+2 || ymag ==del+2)
continue;
if (xmag ==del-2 || ymag ==del-2)
continue;
exit(EXIT_FAILURE);
printf("weird mv found:\n");
printf("%d,%d\n",xmag,ymag);
}
kobject_release (v_img);
kobject_release (correct_img);
kobject_release (vid);
printf("SUCCESS\n");
return 0;
}
......@@ -214,7 +214,7 @@ int fps, int width, int height, enum KVideoCodec codec)
// ----------------------
int buffer_size_alignment = 32; //1 = no alignment?
int buffer_size_alignment = 1; //1 = no alignment?
rgbframe = av_frame_alloc();
......@@ -328,7 +328,7 @@ int write_vid(char* filename, KObject* img, unsigned int width, unsigned int hei
int ret = 0;
int fps = 30;
int seconds = 4;
int seconds = 2;
int num_pics = fps*seconds;
av_register_all();
......@@ -396,8 +396,12 @@ int write_vid(char* filename, KObject* img, unsigned int width, unsigned int hei
printf("freeing objects...\n");
sws_freeContext(swCtx);
av_freep(rgbframe->data);
av_frame_free(&rgbframe);
av_freep(convertedframe->data);
av_frame_free(&convertedframe);
av_free (enc_ctx);
//free_stream(&out_stream);
......@@ -490,6 +494,7 @@ int main (int argc, char** argv)
KObject* vid;
KObject* img;
KObject* img2;
KObject* img3;
char* filename = "test.avi"; //video output
int ret;
......@@ -501,7 +506,7 @@ int main (int argc, char** argv)
// first argument is image filepath, second is video filepath
//load image
kmodule_depend ("png");
// kmodule_depend ("png");
generate_2d_image(&img, width,height, channels);
......@@ -555,14 +560,14 @@ int main (int argc, char** argv)
ftype = kvideo_frame_type(vid);
printf("Obtained %c-frame\n",ftype);
img2 = kvideo_frame_to_kimage_decoded(vid);
sum = check (img,img2);
img3 = kvideo_frame_to_kimage_decoded(vid);
sum = check (img,img3);
printf("sum of squares result: %u\n", sum);
if (sum != 58667091){
exit(EXIT_FAILURE);
}
kobject_release (img3);
kobject_release (img2);
kobject_release (img);
kobject_release (vid);
......