📄 main.c
字号:
motion->MV_th2 = mval.mv_th2;
motion->sad_th2 = mval.sad_th2;
motion->delta_dev_th2 = mval.dev_th2;
motion->md_interval = mval.md_interval;
motion->motion_dection_enable=mval.enable;
}
#define abs(X) (((X)>0)?(X):-(X))
void motion_dection_1mv(MACROBLOCK_INFO *mb_array, MOTION_INFO *pEnc_motion, Faraday_ENC_FRAME *pEnc_frame, unsigned int mbwidth, int mbheight)
{
unsigned int x_pos, y_pos;
unsigned int sad16=0;
unsigned int dev=0, post_dev=0;
MACROBLOCK_INFO *mb, *post_mb;
if( pEnc_frame->frameindex>=pEnc_motion->md_interval) {
for(y_pos=0 ; y_pos<mbheight; y_pos++ ) {
for ( x_pos=0 ; x_pos<mbwidth ; x_pos++ ) {
mb=mb_array+(y_pos*mbwidth + x_pos);
post_mb = mb_array+(y_pos*mbwidth + x_pos-1);
post_dev = post_mb->dev;
dev=mb->dev;
//if( pEnc_frame->frameindex>=pEnc_motion->md_interval) {
sad16=mb->sad16;
if((x_pos>=pEnc_motion->range_mb_x0_LU)
&&(y_pos>=pEnc_motion->range_mb_y0_LU)
&&(x_pos<pEnc_motion->range_mb_x0_RD)
&&(y_pos<pEnc_motion->range_mb_y0_RD)) {
if (((mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x+mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x)>pEnc_motion->MV_th0)
&&(sad16>pEnc_motion->sad_th0)) {
pEnc_frame->active0++;
}
}
if((x_pos>=pEnc_motion->range_mb_x1_LU)
&&(y_pos>=pEnc_motion->range_mb_y1_LU)
&&(x_pos<pEnc_motion->range_mb_x1_RD)
&&(y_pos<pEnc_motion->range_mb_y1_RD)) {
if (((mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x+mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x)>pEnc_motion->MV_th1)
&&(sad16>pEnc_motion->sad_th1)){
pEnc_frame->active1 ++;
}
}
if((x_pos>=pEnc_motion->range_mb_x2_LU)
&&(y_pos>=pEnc_motion->range_mb_y2_LU)
&&(x_pos<pEnc_motion->range_mb_x2_RD)
&&(y_pos<pEnc_motion->range_mb_y2_RD)) {
if (((mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x+mb->mvs[3].vec.s16x*mb->mvs[3].vec.s16x)>pEnc_motion->MV_th2)
&&(sad16>pEnc_motion->sad_th2)){
pEnc_frame->active2 ++;
}
}
}
}
}
}
#define FMJPEG_ENCODER_DEV "/dev/mjenc" //major:10 minior:61
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int fmjpeg_encoder_sj(int width,int height,char *data0,char *data1,char *data2, char *outfile)
{
FILE *fout;
FJPEG_ENC_PARAM enc_param;
unsigned int y_image_size,u_image_size,v_image_size;
unsigned int max_h_samp,max_v_samp;
int fjpeg_enc_fd=0;
int err, buf_size;
char *outbuf;
if(fjpeg_enc_fd==0)
fjpeg_enc_fd=open(FMJPEG_ENCODER_DEV,O_RDWR);
if(fjpeg_enc_fd==0)
{
printf("Fail to open %s\n",FMJPEG_ENCODER_DEV);
fflush(stdout);
return -1;
}
buf_size = width*height + width*height/2;
outbuf=malloc(sizeof(char)*buf_size);
if(!outbuf)
{
printf("output buf allocate fail\n");
fflush(stdout);
}
fout=fopen(outfile,"wb");
if(!fout)
{
printf("open outfile err\n");
fflush(stdout);
}
// to set each component's sampling factor (horizontally and vertically)
// set Y component's sampling factor
enc_param.rgComponentInfo[0].m_u8HSamplingFrequency=2;
enc_param.rgComponentInfo[0].m_u8VSamplingFrequency=2;
// set U component's sampling factor
enc_param.rgComponentInfo[1].m_u8HSamplingFrequency=1;
enc_param.rgComponentInfo[1].m_u8VSamplingFrequency=1;
// set V component's sampling factor
enc_param.rgComponentInfo[2].m_u8HSamplingFrequency=1;
enc_param.rgComponentInfo[2].m_u8VSamplingFrequency=1;
#define IMAGE_COMP 3
// to describe the YUV format through the following encoding parameters
enc_param.u8NumComponents = IMAGE_COMP; // the input image has 3 components 'YUV'
enc_param.u32ImageQuality=40; // we set image quality to 90 (0~100)
enc_param.u32RestartInterval=10; // we set restart interval to 5
enc_param.u32ImageWidth=width; // set image width
enc_param.u32ImageHeight=height; // set image height
enc_param.u8JPGPIC = 1;
enc_param.u32ImageMotionDetection=0;
enc_param.u8MP4Alarm = 1;
enc_param.roi_enable = 0;
enc_param.roi_left_x = 120;
enc_param.roi_left_y = 120;
enc_param.roi_right_x = 440;
enc_param.roi_right_y = 360;
// get the maximum horizontal sampling factor
max_h_samp=MAX(enc_param.rgComponentInfo[0].m_u8HSamplingFrequency,
MAX(enc_param.rgComponentInfo[1].m_u8HSamplingFrequency,
enc_param.rgComponentInfo[2].m_u8HSamplingFrequency));
// get the maximum horizontal sampling factor
max_v_samp=MAX(enc_param.rgComponentInfo[0].m_u8VSamplingFrequency,
MAX(enc_param.rgComponentInfo[1].m_u8VSamplingFrequency,
enc_param.rgComponentInfo[2].m_u8VSamplingFrequency));
// calculate each component size according to its maximum sampling factor
// and individual sampling factor
y_image_size=(((enc_param.rgComponentInfo[0].m_u8HSamplingFrequency*enc_param.u32ImageWidth)/max_h_samp)*
((enc_param.rgComponentInfo[0].m_u8VSamplingFrequency*enc_param.u32ImageHeight)/max_v_samp));
u_image_size=(((enc_param.rgComponentInfo[1].m_u8HSamplingFrequency*enc_param.u32ImageWidth)/max_h_samp)*
((enc_param.rgComponentInfo[1].m_u8VSamplingFrequency*enc_param.u32ImageHeight)/max_v_samp));
v_image_size=(((enc_param.rgComponentInfo[2].m_u8HSamplingFrequency*enc_param.u32ImageWidth)/max_h_samp)*
((enc_param.rgComponentInfo[2].m_u8VSamplingFrequency*enc_param.u32ImageHeight)/max_v_samp));
enc_param.pu8YUVAddr[0]=data0;
enc_param.pu8YUVAddr[1]=data1;
enc_param.pu8YUVAddr[2]=data2;
// to create the jpeg encoder object
err=ioctl(fjpeg_enc_fd,FMJPEG_IOCTL_ENCODE_CREATE,&enc_param );
if(err<0)
{
close(fjpeg_enc_fd);
printf("Error to set FMJPEG_IOCTL_ENCODE_CREATE\n");
fflush(stdout);
return -1;
}
// to begin to encode the input image
enc_param.pu8BitstreamAddr = outbuf;
err=ioctl(fjpeg_enc_fd,FMJPEG_IOCTL_ENCODE_ONE,&enc_param);
if(err<0)
{
close(fjpeg_enc_fd);
printf("Error to set FMJPEG_IOCTL_ENCODE_ONE\n");
fflush(stdout);
return -1;
}
fflush(stdout);
fwrite(outbuf, enc_param.bitstream_size, 1, fout);
fclose(fout);
// release the JPEG encoder object
err=ioctl(fjpeg_enc_fd,FMJPEG_IOCTL_ENCODE_DESTROY,&enc_param);
if(err<0)
{
close(fjpeg_enc_fd);
printf("Error to set FMJPEG_IOCTL_ENCODE_DESTROY\n");
fflush(stdout);
return -1;
}
if(outbuf)
free(outbuf);
return enc_param.bitstream_size;
}
void do_Static_JPEG(char *filename, char *dataY, char *dataU, char *dataV, int w, int h)
{
char oname[100];//,copycmd[50];
char wi[10],hi[10];//,quant[10];
// int len;
time_t *timep;
struct tm *stime;
char year[8],month[4],date[4],hour[4],minute[4],second[4];
timep=malloc(sizeof(time_t));
stime=malloc(sizeof(struct tm));
time(timep);
stime=localtime(timep);
sprintf(year,"%d",stime->tm_year+1900);
if(stime->tm_mon+1 > 9)
sprintf(month,"%d",stime->tm_mon+1);
else
sprintf(month,"0%d",stime->tm_mon+1);
if(stime->tm_mday > 9)
sprintf(date,"%d",stime->tm_mday);
else
sprintf(date,"0%d",stime->tm_mday);
if(stime->tm_hour > 9)
sprintf(hour,"%d",stime->tm_hour);
else
sprintf(hour,"0%d",stime->tm_hour);
if(stime->tm_min > 9)
sprintf(minute,"%d",stime->tm_min);
else
sprintf(minute,"0%d",stime->tm_min);
if(stime->tm_sec > 9)
sprintf(second,"%d",stime->tm_sec);
else
sprintf(second,"0%d",stime->tm_sec);
sprintf(oname,"/%s-%s-%s-%s-%s-%s-%s.jpg",filename,year,month,date,hour,minute,second);
sprintf(wi,"%d",w);
sprintf(hi,"%d",h);
if(fmjpeg_encoder_sj(w,h,dataY,dataU,dataV,oname) < 0 )
printf("error to encode\n");
fprintf(stderr,"Encode a jpeg %s\n",oname);
{
FILE *tmpfd;
tmpfd=fopen("/tmp/alarm1.tmp","w");
fprintf(tmpfd,"%s",oname);
fclose(tmpfd);
}
}
void do_alarm_picture(char *filename,pid_t alarm_pid,int w,int h,char *data0,char *data1,char *data2)
{
pid_t gpid;
if((gpid=fork())<0)
printf("fork error");
else if(gpid==0)
{
fprintf(stderr,"do_static_JPEG %s\n",filename);
do_Static_JPEG(filename,data0,data1,data2,w,h);
exit(0);
}
if(waitpid(gpid,NULL,0)<0)
printf("wait error");
}
void do_alarm_video(char *filename,pid_t alarm_pid,int w,int h,char *data0,char *data1,char *data2)
{
pid_t gpid;
if((gpid=fork())<0)
printf("fork error");
else if(gpid==0)
{
execlp("ffly","ffly","alarm","start",(char *)0);
//system("ffly alarm start");
exit(0);
}
if(waitpid(gpid,NULL,0)<0)
printf("wait error");
}
int read_motion_config(const char *filename)
{
FILE *fp;
char line[1024];
const char *p;
char cmd[64];
char arg[1024];
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("Can't read motion config %s\n",filename);
return -1;
}
memset(&mval,0,sizeof(mval));
for(;;)
{
if (fgets(line, sizeof(line), fp) == NULL)
break;
p = line;
while (isspace(*p))
p++;
if (*p == '\0' || *p == '#')
continue;
get_arg(cmd, sizeof(cmd), &p);
if (!strcasecmp(cmd, "pid"))
{
get_arg(arg, sizeof(arg), &p);
mval.pid = atoi(arg);
}
else if (!strcasecmp(cmd, "enable"))
{
get_arg(arg, sizeof(arg), &p);
mval.enable = atoi(arg);
}
else if (!strcasecmp(cmd, "md_interval"))
{
get_arg(arg, sizeof(arg), &p);
mval.md_interval = atoi(arg);
}
else if (!strcasecmp(cmd, "x0_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.x0_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "y0_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.y0_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "x0_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.x0_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "y0_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.y0_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "x1_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.x1_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "y1_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.y1_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "x1_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.x1_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "y1_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.y1_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "x2_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.x2_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "y2_LU"))
{
get_arg(arg, sizeof(arg), &p);
mval.y2_LU = atoi(arg);
}
else if (!strcasecmp(cmd, "x2_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.x2_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "y2_RD"))
{
get_arg(arg, sizeof(arg), &p);
mval.y2_RD = atoi(arg);
}
else if (!strcasecmp(cmd, "mv_th0"))
{
get_arg(arg, sizeof(arg), &p);
mval.mv_th0 = atoi(arg);
}
else if (!strcasecmp(cmd, "sad_th0"))
{
get_arg(arg, sizeof(arg), &p);
mval.sad_th0 = atoi(arg);
}
else if (!strcasecmp(cmd, "dev_th0"))
{
get_arg(arg, sizeof(arg), &p);
mval.dev_th0 = atoi(arg);
}
else if (!strcasecmp(cmd, "alarm_idx0"))
{
get_arg(arg, sizeof(arg), &p);
mval.alarm_idx0 = atoi(arg);
}
else if (!strcasecmp(cmd, "mv_th1"))
{
get_arg(arg, sizeof(arg), &p);
mval.mv_th1 = atoi(arg);
}
else if (!strcasecmp(cmd, "sad_th1"))
{
get_arg(arg, sizeof(arg), &p);
mval.sad_th1 = atoi(arg);
}
else if (!strcasecmp(cmd, "dev_th1"))
{
get_arg(arg, sizeof(arg), &p);
mval.dev_th1 = atoi(arg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -