Sunday 15 May 2011

PSNR calclator of 2 YUV files using c++ segmentation fault -



PSNR calclator of 2 YUV files using c++ segmentation fault -

iam trying write code in c++ calculate psnr of 2 yuv video files should work segmentation fault error .. help here highly appreciated here code

#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "base.h" void makeaframe( yuvframe* f, int width, int height ) { f->lum.width = width; f->lum.height = height; f->cb .width = width/2; f->cb .height = height/2; f->cr .width = width/2; f->cr .height = height/2; } void readcolorcomponent( colorcomponent* cc, file* file ) { unsigned int size = cc->width*cc->height; unsigned int rsize; rsize = fread( cc->data, sizeof(unsigned char), size, file ); } double psnr( colorcomponent& rec, colorcomponent& org) { unsigned char* porg = org.data; unsigned char* prec = rec.data; double ssd = 0; int diff; ( int r = 0; r < rec.height; r++ ) { for( int c = 0; c < rec.width; c++ ) { diff = prec[c] - porg[c]; ssd += (double)( diff * diff ); } prec += rec.width; porg += org.width; } if( ssd == 0.0 ) { homecoming 99.99; } homecoming ( 10.0 * log10( (double)rec.width * (double)rec.height * 65025.0 / ssd ) ); } void getpsnr( double& psnry, double& psnru, double& psnrv, yuvframe& rcframeorg, yuvframe& rcframerec ) { psnry = psnr( rcframerec.lum, rcframeorg.lum ); psnru = psnr( rcframerec.cb, rcframeorg.cb ); psnrv = psnr( rcframerec.cr, rcframeorg.cr ); } void readframe( yuvframe* f, file* file ) { readcolorcomponent( &f->lum, file ); readcolorcomponent( &f->cb, file ); readcolorcomponent( &f->cr, file ); } int main(int argc, char *argv[]) { int acc = 10000; #define out "%d,%04d" int stream = 0; unsigned int width = 0; unsigned int height = 0; unsigned int temporal_stages = 0; unsigned int skip_at_start = 0; double fps = 0.0; file* org_file = 0; file* rec_file = 0; file* str_file = 0; char* prefix_string = 0; unsigned int index, skip, skip_between, sequence_length; int py, pu, pv, br; double bitrate = 0.0; double psnry, psnru, psnrv; yuvframe corgframe, crecframe; double averagepsnr_y = 0.0; double averagepsnr_u = 0.0; double averagepsnr_v = 0.0; int currarg = 5; int rpsnr = 0; width = 300; height = 240; org_file = fopen("foreman_qcif.yuv","rb"); rec_file = fopen("foreman_qcif2.yuv","rb"); temporal_stages=2; fseek( rec_file, 0, seek_end ); fseek( org_file, 0, seek_end ); size_t rsize = ftell( rec_file ); size_t osize = ftell( org_file ); fseek( rec_file, 0, seek_set ); fseek( org_file, 0, seek_set ); if (rsize < osize) { sequence_length = (unsigned int)((double)rsize/(double)((width*height*3)/2)); } else { sequence_length = (unsigned int)((double)osize/(double)((width*height*3)/2)); skip_between = ( 1 << temporal_stages ) - 1; } //here seek create frames out of images makeaframe( &corgframe, width, height ); makeaframe( &crecframe, width, height ); //looping on frames for( skip = skip_at_start, index = 0; index < sequence_length; index++, skip = skip_between ) { fseek( org_file, skip*width*height*3/2, seek_cur); readframe ( &corgframe, org_file ); readframe ( &crecframe, rec_file ); getpsnr ( psnry, psnru, psnrv, corgframe, crecframe); averagepsnr_y += psnry; averagepsnr_u += psnru; averagepsnr_v += psnrv; py = (int)floor( acc * psnry + 0.5 ); pu = (int)floor( acc * psnru + 0.5 ); pv = (int)floor( acc * psnrv + 0.5 ); fprintf(stdout,"%d\t"out"\t"out"\t"out"\n",index,py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); } fprintf(stdout,"\n"); py = (int)floor( acc * averagepsnr_y / (double)sequence_length + 0.5 ); pu = (int)floor( acc * averagepsnr_u / (double)sequence_length + 0.5 ); pv = (int)floor( acc * averagepsnr_v / (double)sequence_length + 0.5 ); br = (int)floor( acc * bitrate + 0.5 ); if( stream ) { if( prefix_string ) { fprintf(stderr,"%s\t"out"\t"out"\t"out"\t"out"\n",prefix_string,br/acc,br%acc,py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); fprintf(stdout,"%s\t"out"\t"out"\t"out"\t"out"\n",prefix_string,br/acc,br%acc,py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); } else { fprintf(stderr,out"\t"out"\t"out"\t"out"\n",br/acc,br%acc,py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); fprintf(stdout,out"\t"out"\t"out"\t"out"\n",br/acc,br%acc,py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); } } else { fprintf(stderr,"total\t"out"\t"out"\t"out"\n",py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); fprintf(stdout,"total\t"out"\t"out"\t"out"\n",py/acc,py%acc,pu/acc,pu%acc,pv/acc,pv%acc); } fprintf(stdout, "\n"); fclose ( org_file ); fclose ( rec_file ); if( stream ) { fclose ( str_file ); } homecoming (rpsnr*py); }

and base.h contains structures

typedef struct { int width; int height; unsigned char* data; } colorcomponent; typedef struct { colorcomponent lum; colorcomponent cb; colorcomponent cr; } yuvframe;

well, segmentation error arises trying access memory addresses not assigned ... w hot-fix avoid error add together function code

void foo( colorcomponent* cc ) { if( ! ( cc->data = new unsigned char[cc->width * cc->height])) { fprintf(stderr, "\nerror: memory allocation failed!\n\n"); exit(-1); } }

and alter these lines

void makeaframe( yuvframe* f, int width, int height ) { f->lum.width = width; f->lum.height = height; f->cb .width = width/2; f->cb .height = height/2; f->cr .width = width/2; f->cr .height = height/2; }

to

void makeaframe( yuvframe* f, int width, int height ) { f->lum.width = width; f->lum.height = height; foo( &f->lum ); f->cb .width = width/2; f->cb .height = height/2; foo( &f->cb ); f->cr .width = width/2; f->cr .height = height/2; foo( &f->cr ); }

things should work now

c++

No comments:

Post a Comment