Ejemplo n.º 1
0
static int md_detect_motion(void)
{
    int idx, tick, rv;
    int val, cy, cv, cu;

    register int col, row, x, y;

    if(!md_running())
    {
        return 0;
    }

    tick = get_tick_count();
    rv = 1;

#ifdef OPT_MD_DEBUG
    if(motion_detector.comp_calls_cnt < MD_REC_CALLS_CNT)
    {
        motion_detector.comp_calls[motion_detector.comp_calls_cnt]=tick;
    }
    motion_detector.comp_calls_cnt++;
#endif

    if(motion_detector.start_time + motion_detector.timeout < tick )
    {
        md_save_calls_history();
        motion_detector.running = 0;
        return 0;
    }

    if(motion_detector.last_measure_time + motion_detector.measure_interval > tick)
    {
        // wait for the next time
        return 1;
    }

    motion_detector.last_measure_time = tick;

    unsigned char* img = vid_get_viewport_live_fb();
    if(img==NULL)
    {
        img = vid_get_viewport_fb();
    }

#ifdef OPT_MD_DEBUG
    if(motion_detector.comp_calls_cnt==50 && (motion_detector.parameters & MD_MAKE_RAM_DUMP_FILE) != 0 )
    {
        mx_dump_memory((char*)img);
    }
#endif

	img += vid_get_viewport_image_offset();		// offset into viewport for when image size != viewport size (e.g. 16:9 image on 4:3 LCD)

	int vp_h = vid_get_viewport_height();
    int vp_w = vid_get_viewport_width();
	int vp_bw = vid_get_viewport_byte_width() * vid_get_viewport_yscale();

	int x_step = motion_detector.pixels_step * 3;
	int y_step = motion_detector.pixels_step * vp_bw;

    for (idx=0, row=0; row < motion_detector.rows; row++)
    {
        // Calc img y start and end offsets (use same height for all cells so 'points' is consistent)
        int y_start = ((row * vp_h) / motion_detector.rows) * vp_bw;
        int y_end = y_start + ((vp_h / motion_detector.rows) * vp_bw);

        for (col=0; col < motion_detector.columns; col++, idx++)
        {
            int in_clipping_region=0;

            if (col+1 >= motion_detector.clipping_region_column1 &&
                col+1 <= motion_detector.clipping_region_column2 &&
                row+1 >= motion_detector.clipping_region_row1 &&
                row+1 <= motion_detector.clipping_region_row2)
            {
                in_clipping_region=1;
            }

            int curr = 0;
            int diff = 0;

            if (
                (motion_detector.clipping_region_mode==MD_REGION_NONE) ||
                (motion_detector.clipping_region_mode==MD_REGION_EXCLUDE && in_clipping_region==0) ||
                (motion_detector.clipping_region_mode==MD_REGION_INCLUDE && in_clipping_region==1)
               )
            {
                // Calc img x start and end offsets (use same width for all cells so 'points' is consistent)
                int x_start = ((col * vp_w) / motion_detector.columns) * 3;
                int x_end = x_start + ((vp_w / motion_detector.columns) * 3);

                int points = 0;

                for (y=y_start; y<y_end; y+=y_step)
                {
                    for (x=x_start; x<x_end; x+=x_step)
                    {
                        // ARRAY of UYVYYY values
                        // 6 bytes - 4 pixels

                        if (motion_detector.pixel_measure_mode == MD_MEASURE_MODE_Y)
                        {
                            val = img[y + x + 1];				                        //Y
                        }
                        else
                        {
                            // Calc offset to UYV component
                            int uvx = x;
                            if (uvx & 1) uvx -= 3;

                            switch(motion_detector.pixel_measure_mode)
                            {
                            case MD_MEASURE_MODE_U:
                                val = (signed char)img[y + uvx];		                //U
                                break;

                            case MD_MEASURE_MODE_V:
                                val = (signed char)img[y + uvx + 2];	                //V
                                break;

                            case MD_MEASURE_MODE_R:
                                cy = img[y + x + 1];
                                cv = (signed char)img[y + uvx + 2];
                                val = clip(((cy<<12)           + cv*5743 + 2048)>>12); // R
                                break;

                            case MD_MEASURE_MODE_G:
                                cy = img[y + x + 1];
                                cu = (signed char)img[y + uvx];
                                cv = (signed char)img[y + uvx + 2];
                                val = clip(((cy<<12) - cu*1411 - cv*2925 + 2048)>>12); // G
                                break;

                            case MD_MEASURE_MODE_B:
                                cy = img[y + x + 1];
                                cu = (signed char)img[y + uvx];
                                val = clip(((cy<<12) + cu*7258           + 2048)>>12); // B
                                break;

                            default:
                                val = 0;    // Stop compiler warning
                                break;
                            }
                        }

                        curr += val;
                        points++;
                    }
                }
                motion_detector.points = points ;
                diff = (curr - motion_detector.prev[idx]) / points;
                if (diff < 0) diff = -diff;
                if ((diff > motion_detector.threshold) &&
                    (motion_detector.start_time+motion_detector.msecs_before_trigger < tick))
                {
                    motion_detector.detected_cells++;
                }
            }

            motion_detector.diff[idx] = diff;
            motion_detector.prev[idx] = curr;
	}
    }
Ejemplo n.º 2
0
Archivo: lib.c Proyecto: c10ud/CHDK
void *vid_get_viewport_fb_d()
{
    int x=(*(int*)0x71AB0); // @ffd0f29c
    return (x) ? (void*)x : vid_get_viewport_fb();
}
Ejemplo n.º 3
0
void histogram_process()
{
    static unsigned char *img;
    int i, hi, c;
    int y, v, u;
	static int x;
    static int viewport_size;
    unsigned int histo_fill[5];

	    switch (histogram_stage) {
        case 0:
            img=((mode_get()&MODE_MASK) == MODE_PLAY)?vid_get_viewport_fb_d():((kbd_is_key_pressed(KEY_SHOOT_HALF))?vid_get_viewport_fb():vid_get_viewport_live_fb());
      	
        	if (img==NULL){
	    	  img = vid_get_viewport_fb();
		    }
			img += vid_get_viewport_image_offset();		// offset into viewport for when image size != viewport size (e.g. 16:9 image on 4:3 LCD)
            viewport_size = vid_get_viewport_height() * vid_get_viewport_buffer_width();
            for (c=0; c<5; ++c) {
                for (i=0; i<HISTO_WIDTH; ++i) {
                    histogram_proc[c][i]=0;
                }
                histo_max[c] = histo_max_center[c] = 0;
            }

            histogram_stage=1;
            break;
        
        case 1:
        case 2:
        case 3:
			x = 0;	// count how many blocks we have done on the current row (to skip unused buffer space at end of each row)
            for (i=(histogram_stage-1)*6; i<viewport_size*3; i+=HISTO_STEP_SIZE*6) {
                y = img[i+1];
                u = *(signed char*)(&img[i]);
                if (u&0x00000080) u|=0xFFFFFF00;
                v = *(signed char*)(&img[i+2]);
                if (v&0x00000080) v|=0xFFFFFF00;

                hi = y*HISTO_WIDTH/256; // Y
                ++histogram_proc[HISTO_Y][hi];
                hi = clip(((y<<12)          + v*5743 + 2048)/4096)*HISTO_WIDTH/256; // R
                ++histogram_proc[HISTO_R][hi];
                hi = clip(((y<<12) - u*1411 - v*2925 + 2048)/4096)*HISTO_WIDTH/256; // G
                ++histogram_proc[HISTO_G][hi];
                hi = clip(((y<<12) + u*7258          + 2048)/4096)*HISTO_WIDTH/256; // B
                ++histogram_proc[HISTO_B][hi];

				// Handle case where viewport memory buffer is wider than the actual buffer.
				x += HISTO_STEP_SIZE * 2;	// viewport width is measured in blocks of three bytes each even though the data is stored in six byte chunks !
				if (x == vid_get_viewport_width())
				{
					i += vid_get_viewport_row_offset();
					x = 0;
				}
			}

            ++histogram_stage;
            break;

        case 4:
            for (i=0; i<HISTO_WIDTH; ++i) { // G
                histogram_proc[HISTO_RGB][i]=histogram_proc[HISTO_R][i]+histogram_proc[HISTO_G][i]+histogram_proc[HISTO_B][i];
            }
            for (c=0; c<5; ++c) { // calculate maximums
                for (i=0; i<HISTO_WIDTH; ++i) {
                    if (histo_max[c]<histogram_proc[c][i])
                        histo_max[c]=histogram_proc[c][i];
                    if (histo_max_center[c]<histogram_proc[c][i] && i>=conf.histo_ignore_boundary && i<HISTO_WIDTH-conf.histo_ignore_boundary)
                        histo_max_center[c]=histogram_proc[c][i];
                }
                
                if (histo_max[c] > 0) {
                    histo_max_invw[c] = ((float)HISTO_HEIGHT)/histogram_transform((float)histo_max[c]);
                } else {
                    histo_max_invw[c] = 0.0f;
                }

                if (histo_max_center[c] > 0) {
                    histo_max_center_invw[c] = ((float)HISTO_HEIGHT)/histogram_transform((float)histo_max_center[c]);
                } else {
                    histo_max_center_invw[c] = 0.0f;
                }
            }

            if (histo_max[HISTO_RGB] > 0) { // over- / under- expos
                under_exposed = (histogram_proc[HISTO_RGB][0]*8
                                +histogram_proc[HISTO_RGB][1]*4
                                +histogram_proc[HISTO_RGB][2]) > exposition_thresh;

                over_exposed = (histogram_proc[HISTO_RGB][HISTO_WIDTH-3]
                               +histogram_proc[HISTO_RGB][HISTO_WIDTH-2]*4
                               +histogram_proc[HISTO_RGB][HISTO_WIDTH-1]*8) > exposition_thresh;
            } else {
                over_exposed = 0;
                under_exposed = 1;
            }

            histogram_stage=5;
            state_expos_recalculated = 1;
            break;

        case 5:
            for (c=0; c<5; ++c) {
                histo_fill[c]=0;
                for (i=0; i<HISTO_WIDTH; ++i) {
                    histogram[c][i] = (histogram_transform((float)histogram_proc[c][i]))*histo_max_center_invw[c];
                    if (histogram[c][i] > HISTO_HEIGHT)
                        histogram[c][i] = HISTO_HEIGHT;
                    histo_fill[c]+=histogram[c][i];
                }
            }

            if (conf.histo_auto_ajust) {
                histo_magnification = histo_fill[histo_main]*1000/(HISTO_HEIGHT*HISTO_WIDTH);
                if (histo_magnification<200) { // try to ajust if average level is less than 20%
                    histo_magnification=200*1000/histo_magnification;
                    for (c=0; c<5; ++c) {
                        for (i=0;i<HISTO_WIDTH;i++) {
                            histogram[c][i] = (histogram_transform((float)histogram_proc[c][i]))*histo_max_center_invw[c]*histo_magnification/1000;
                            if (histogram[c][i] > HISTO_HEIGHT)
                                histogram[c][i] = HISTO_HEIGHT;
                        }
                    }
                } else 
                    histo_magnification=0;
            } else {
                histo_magnification=0;
            }

            histogram_stage=0;
            break;

        case HISTOGRAM_IDLE_STAGE:
            break;
    }

}
Ejemplo n.º 4
0
// Sobel edge detector
static int calc_edge_overlay()
{
    int shutter_fullpress = kbd_is_key_pressed(KEY_SHOOT_FULL);

    const int bPlayMode = (mode_get() & MODE_MASK) == MODE_PLAY;
    const unsigned char* img = bPlayMode ? vid_get_viewport_fb_d() :  vid_get_viewport_fb();
    const unsigned char*  ptrh1 = NULL;    // previous pixel line
    const unsigned char*  ptrh2 = NULL;    // current pixel line
    const unsigned char*  ptrh3 = NULL;    // next pixel line
    unsigned char*  smptr = NULL;    // pointer to line in smbuf
    int x, y, xdiv3;
    int conv1, conv2;

    const int y_min = viewport_yoffset + EDGE_HMARGIN+ slice   *slice_height;
    const int y_max = viewport_yoffset + EDGE_HMARGIN+(slice+1)*slice_height;
    const int x_min = viewport_xoffset*3 + 6;
    const int x_max = (viewport_width + viewport_xoffset - 2) * 3;

    xoffset = 0;
    yoffset = 0;

    // Reserve buffers
    ensure_allocate_imagebuffer();
    if( !is_buffer_ready() ) return 0;

    // In every 6 bytes the Y of four pixels are described in the
    // viewport (UYVYYY format). For edge detection we only
    // consider the second in the current and the first
    // in the next pixel.

    // Clear all edges in the current slice
    int compressed_slice = edgebuf->ptrLen / EDGE_SLICES;
    memset(edgebuf->ptr + slice*compressed_slice, 0, compressed_slice);

    if (conf.edge_overlay_filter)
    {
        // Prefill smbuf with three lines of avergae-filtered data.
        // This looks much more complex then it actually is.
        // We really are just summing up nine pixels in a 3x3 box
        // and averaging the current pixel based on them. And
        // we do it 4 bytes at a time because of the UYVYYY format.
        for (y = -1; y <= 1; ++y)
        {
            shutter_fullpress |= kbd_is_key_pressed(KEY_SHOOT_FULL);

            ptrh1 = img + (y_min+y-1) * viewport_byte_width;
            smptr = smbuf + (y+1) * viewport_byte_width;

            average_filter_row(ptrh1, smptr, x_min, x_max);
        }
    }

    for (y = y_min; y < y_max; ++y)
    {
        shutter_fullpress |= kbd_is_key_pressed(KEY_SHOOT_FULL);

        if (conf.edge_overlay_filter)
        {
            // We need to shift up our smbuf one line,
            // and fill in the last line (which now empty)
            // with average-filtered data from img.
            // By storing only three lines of smoothed picture
            // in memory, we save memory.

            // Shift
            memcpy(smbuf, smbuf+viewport_byte_width, viewport_byte_width*2);

            // Filter new line
            ptrh1 = img + y * viewport_byte_width;
            smptr = smbuf + 2 * viewport_byte_width;
            average_filter_row(ptrh1, smptr, x_min, x_max);

            ptrh1 = smbuf;
        }
        else
        {
            ptrh1 = img + (y-1) * viewport_byte_width;
        }
        ptrh2 = ptrh1 + viewport_byte_width;
        ptrh3 = ptrh2 + viewport_byte_width;

        // Now we do sobel on the current line

        for (x = x_min, xdiv3 = x_min/3; x < x_max; x += 6, xdiv3 += 2)
        {
            // convolve vert (second Y)
            conv1 = *(ptrh1 + x + 1) * ( 1) +
                    *(ptrh1 + x + 4) * (-1) +

                    *(ptrh2 + x + 1) * ( 2) +
                    *(ptrh2 + x + 4) * (-2) +

                    *(ptrh3 + x + 1) * ( 1) +
                    *(ptrh3 + x + 4) * (-1);
            if  (conv1 < 0)     // abs()
                conv1 = -conv1;

            // convolve vert (first Y of next pixel)
            conv2 = *(ptrh1 + x + 1) * ( 1) +
                    *(ptrh1 + x + 3) * ( 2) +
                    *(ptrh1 + x + 4) * ( 1) +

                    *(ptrh3 + x + 1) * (-1) +
                    *(ptrh3 + x + 3) * (-2) +
                    *(ptrh3 + x + 4) * (-1);
            if  (conv2 < 0)     // abs()
                conv2 = -conv2;

            if (conv1 + conv2 > conf.edge_overlay_thresh)
            {
                bv_set(edgebuf, (y-viewport_yoffset-EDGE_HMARGIN)*viewport_width + xdiv3, 1);
            }

            // Do it once again for the next 'pixel'

            // convolve vert (second Y)
            conv1 = *(ptrh1 + x + 5) * ( 1) +
                    *(ptrh1 + x + 9) * (-1) +

                    *(ptrh2 + x + 5) * ( 2) +
                    *(ptrh2 + x + 9) * (-2) +

                    *(ptrh3 + x + 5) * ( 1) +
                    *(ptrh3 + x + 9) * (-1);
            if  (conv1 < 0)     // abs()
                conv1 = -conv1;

            // convolve vert (first Y of next pixel)
            conv2 = *(ptrh1 + x + 5) * ( 1) +
                    *(ptrh1 + x + 7) * ( 2) +
                    *(ptrh1 + x + 9) * ( 1) +

                    *(ptrh3 + x + 5) * (-1) +
                    *(ptrh3 + x + 7) * (-2) +
                    *(ptrh3 + x + 9) * (-1);
            if  (conv2 < 0)     // abs()
                conv2 = -conv2;

            if (conv1 + conv2 > conf.edge_overlay_thresh)
            {
                bv_set(edgebuf, (y-viewport_yoffset-EDGE_HMARGIN)*viewport_width + xdiv3+1, 1);
            }
        }   // for x
    }   // for y


//  For an even more improved edge overlay, enabling the following lines will
//  post-filter the results of the edge detection, removing false edge 'dots'
//  from the display. However, the speed hit is large. In the developer's opinion
//  this code is not needed, but if you want that additional quality and do not
//  care so much about performance, you can enable it.
//
//    if (conf.edge_overlay_filter)
//    {
//        // Here we do basic filtering on the detected edges.
//        // If a pixel is marked as edge but just a few of its
//        // neighbors are also edges, then we assume that the
//        // current pixel is just noise and delete the mark.
//
//        bit_vector_t* bv_tmp = bv_create(edgebuf->nElem, edgebuf->nBits);
//        if (bv_tmp != NULL)
//        {
//            memset(bv_tmp->ptr, 0, bv_tmp->ptrLen);
//
//            for (y = 1; y < viewport_height-1; ++y)
//            {
//                shutter_fullpress |= kbd_is_key_pressed(KEY_SHOOT_FULL);
//
//                for (x=12; x<(viewport_width - 4); ++x)
//                {
//                    int bEdge = bv_get(edgebuf, y*viewport_width + x);
//                    if (bEdge)
//                    {
//                        // Count the number of neighbor edges
//                        int sum =
//                            bv_get(edgebuf, (y-1)*viewport_width + (x-1)) +
//                            bv_get(edgebuf, (y-1)*viewport_width + (x)) +
//                            bv_get(edgebuf, (y-1)*viewport_width + (x+1)) +
//
//                            bv_get(edgebuf, (y)*viewport_width + (x-1)) +
////              bv_get(&edgebuf, (y)*viewport_width + (x)) + //  we only inspect the neighbors
//                            bv_get(edgebuf, (y)*viewport_width + (x+1)) +
//
//                            bv_get(edgebuf, (y+1)*viewport_width + (x-1)) +
//                            bv_get(edgebuf, (y+1)*viewport_width + (x)) +
//                            bv_get(edgebuf, (y+1)*viewport_width + (x+1));
//
//                        if (!conf.edge_overlay_show)
//                        {
//                            if (sum >= 5)    // if we have at least 5 neighboring edges
//                                bv_set(bv_tmp, y*viewport_width + x, 1);   // keep the edge
//                            // else
//                            // there is no need to delete because the buffer is already zeroed
//                        }
//                    }
//                }   // for x
//            }   // for y
//
//            // Swap the filtered edge buffer for the real one
//            bit_vector_t* swap_tmp = edgebuf;
//            edgebuf = bv_tmp;
//            bv_free(swap_tmp);
//        }   // NULL-check
//    }   // if filtering

    return shutter_fullpress;
}
Ejemplo n.º 5
0
int md_detect_motion(void){
	int *tmp;
	unsigned char * img;
    int vp_w, vp_h, idx, tmp2, tick, in_clipping_region, x_step, y_step, x_end, y_end;
	int val;
    int cy,cv,cu;

	register int col, row, x, y;

	if(!md_running()){ 
		return 0;
	}

	tick=get_tick_count();
#ifdef OPT_MD_DEBUG
	if(motion_detector->comp_calls_cnt < MD_REC_CALLS_CNT) {
		motion_detector->comp_calls[motion_detector->comp_calls_cnt]=tick;
	}
	motion_detector->comp_calls_cnt++;
#endif

	if(motion_detector->start_time + motion_detector->timeout < tick ) {
		md_save_calls_history();
		motion_detector->running = 0;
		return 0;
	}

	if(motion_detector->last_measure_time + motion_detector->measure_interval > tick){
		// wait for the next time
		return 1;
	}

	motion_detector->last_measure_time=tick;

// swap pointers so we don't need to copy last data array into Previous one
	tmp=motion_detector->curr;
	motion_detector->curr=motion_detector->prev;
	motion_detector->prev=tmp;

	 img = vid_get_viewport_live_fb();
		if(img==NULL){
			img = vid_get_viewport_fb();
        }

#ifdef OPT_MD_DEBUG
	if(motion_detector->comp_calls_cnt==50 && (motion_detector->parameters & MD_MAKE_RAM_DUMP_FILE) != 0 ){
		mx_dump_memory((char*)img);
	}
#endif

	vp_h = vid_get_viewport_height();
	vp_w = vid_get_viewport_buffer_width();
	img += vid_get_viewport_image_offset();		// offset into viewport for when image size != viewport size (e.g. 16:9 image on 4:3 LCD)

	x_step=vid_get_viewport_width()/motion_detector->columns;
	y_step=vp_h/motion_detector->rows;

	for (idx=0, row=0; row < motion_detector->rows; row++)
	{
		for (col=0; col < motion_detector->columns; col++, idx++)
		{
			// clear cur and points, previously down in it's own loop
			// might be able to avoid clearing all, since some are overwritten below
			motion_detector->points[idx] = 0;
			motion_detector->curr[idx] = 0;

			in_clipping_region=0;

			if (col+1 >= motion_detector->clipping_region_column1 &&
				col+1 <= motion_detector->clipping_region_column2 &&
				row+1 >= motion_detector->clipping_region_row1 &&
				row+1 <= motion_detector->clipping_region_row2)
			{
				in_clipping_region=1;
			}

			if (
				(motion_detector->clipping_region_mode==MD_REGION_NONE) ||
				(motion_detector->clipping_region_mode==MD_REGION_EXCLUDE && in_clipping_region==0) ||
				(motion_detector->clipping_region_mode==MD_REGION_INCLUDE && in_clipping_region==1)
			   )
			{
				x_end=(col+1)*x_step;
				y_end=(row+1)*y_step*vp_w;
				for(y=row*y_step*vp_w; y<y_end; y+=motion_detector->pixels_step*vp_w){
					for(x=col*x_step; x<x_end; x+=motion_detector->pixels_step){

						// ARRAY of UYVYYY values
						// 6 bytes - 4 pixels

						switch(motion_detector->pixel_measure_mode){
						default:
						case MD_MEASURE_MODE_Y:
							val = img[(y+x)*3 + 1];				//Y
							break;
						case MD_MEASURE_MODE_U:
							val = img[(y+(x&0xFFFFFFFE))*3];		//U
							break;
						case MD_MEASURE_MODE_V:
							val = img[(y+(x&0xFFFFFFFE))*3 + 2];	//V
							break;

						case MD_MEASURE_MODE_R:
							cy=img[(y+x)*3 + 1];
							cv=img[(y+(x&0xFFFFFFFE))*3 + 2];
							val = clip(((cy<<12)           + cv*5743 + 2048)>>12); // R
							break;

						case MD_MEASURE_MODE_G:
							cy=img[(y+x)*3 + 1];
							cu=img[(y+(x&0xFFFFFFFE))*3];
							cv=img[(y+(x&0xFFFFFFFE))*3 + 2];
							val = clip(((cy<<12) - cu*1411 - cv*2925 + 2048)>>12); // G
							break;

						case MD_MEASURE_MODE_B:
							cy=img[(y+x)*3 + 1];
							cu=img[(y+(x&0xFFFFFFFE))*3];
							val = clip(((cy<<12) + cu*7258           + 2048)>>12); // B
							break;
						}

						motion_detector->curr[ idx ] += val;
						motion_detector->points[ idx ]++;
					}
				}
			}
		}
	}