opencv: update motioncells to GstOpencvVideoFilter

Update motioncells to inherit from GstOpencvVideoFilter instead of from
GstElement. This means less code and more uniformity with other OpenCV
elements.
This commit is contained in:
Luis de Bethencourt 2015-12-18 12:28:23 +00:00
parent f9464ce354
commit 40d0c1aec0
2 changed files with 235 additions and 247 deletions

View file

@ -141,7 +141,7 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
G_DEFINE_TYPE (GstMotioncells, gst_motion_cells, GST_TYPE_ELEMENT);
G_DEFINE_TYPE (GstMotioncells, gst_motion_cells, GST_TYPE_OPENCV_VIDEO_FILTER);
static void gst_motion_cells_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
@ -150,8 +150,8 @@ static void gst_motion_cells_get_property (GObject * object, guint prop_id,
static gboolean gst_motion_cells_handle_sink_event (GstPad * pad,
GstObject * parent, GstEvent * event);
static GstFlowReturn gst_motion_cells_chain (GstPad * pad, GstObject * parent,
GstBuffer * buf);
static GstFlowReturn gst_motion_cells_transform_ip (GstOpencvVideoFilter *
filter, GstBuffer * buf, IplImage * img);
static void gst_motioncells_update_motion_cells (GstMotioncells * filter);
static void gst_motioncells_update_motion_masks (GstMotioncells * filter);
@ -176,10 +176,6 @@ gst_motion_cells_finalize (GObject * obj)
GFREE (filter->motioncellsidx);
}
if (filter->cvImage) {
cvReleaseImage (&filter->cvImage);
}
GFREE (filter->motioncellscolor);
GFREE (filter->prev_datafile);
GFREE (filter->cur_datafile);
@ -194,22 +190,28 @@ static void
gst_motion_cells_class_init (GstMotioncellsClass * klass)
{
GObjectClass *gobject_class;
GstOpencvVideoFilterClass *gstopencvbasefilter_class;
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gobject_class = (GObjectClass *) klass;
gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_motion_cells_finalize);
gobject_class->set_property = gst_motion_cells_set_property;
gobject_class->get_property = gst_motion_cells_get_property;
gstopencvbasefilter_class->cv_trans_ip_func = gst_motion_cells_transform_ip;
g_object_class_install_property (gobject_class, PROP_GRID_X,
g_param_spec_int ("gridx", "Number of Horizontal Grids",
"You can give number of horizontal grid cells.", GRID_MIN, GRID_MAX,
GRID_DEF, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
GRID_DEF,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (gobject_class, PROP_GRID_Y,
g_param_spec_int ("gridy", "Number of Vertical Grids",
"You can give number of vertical grid cells.", GRID_MIN, GRID_MAX,
GRID_DEF, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
GRID_DEF,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (gobject_class, PROP_SENSITIVITY,
g_param_spec_double ("sensitivity", "Motion Sensitivity",
"You can tunning the element motion sensitivity.", SENSITIVITY_MIN,
@ -315,19 +317,8 @@ gst_motion_cells_class_init (GstMotioncellsClass * klass)
static void
gst_motion_cells_init (GstMotioncells * filter)
{
filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
GST_PAD_SET_PROXY_CAPS (filter->sinkpad);
gst_pad_set_event_function (filter->sinkpad,
gst_pad_set_event_function (GST_BASE_TRANSFORM_SINK_PAD (filter),
GST_DEBUG_FUNCPTR (gst_motion_cells_handle_sink_event));
gst_pad_set_chain_function (filter->sinkpad,
GST_DEBUG_FUNCPTR (gst_motion_cells_chain));
filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
GST_PAD_SET_PROXY_CAPS (filter->srcpad);
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
filter->display = TRUE;
filter->calculate_motion = TRUE;
@ -385,6 +376,8 @@ gst_motion_cells_init (GstMotioncells * filter)
filter->datafileidx = 0;
filter->id = motion_cells_init ();
gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
TRUE);
}
static void
@ -821,11 +814,6 @@ gst_motion_cells_handle_sink_event (GstPad * pad, GstObject * parent,
filter->height = info.height;
filter->framerate = (double) info.fps_n / (double) info.fps_d;
if (filter->cvImage)
cvReleaseImage (&filter->cvImage);
filter->cvImage =
cvCreateImage (cvSize (filter->width, filter->height), IPL_DEPTH_8U,
3);
break;
}
default:
@ -835,19 +823,17 @@ gst_motion_cells_handle_sink_event (GstPad * pad, GstObject * parent,
res = gst_pad_event_default (pad, parent, event);
return res;
}
/* chain function
* this function does the actual processing
*/
static GstFlowReturn
gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
gst_motion_cells_transform_ip (GstOpencvVideoFilter * base, GstBuffer * buf,
IplImage * img)
{
GstMotioncells *filter;
GstMapInfo info;
filter = gst_motion_cells (parent);
GstMotioncells *filter = gst_motion_cells (base);
GST_OBJECT_LOCK (filter);
if (filter->calculate_motion) {
double sensitivity;
@ -866,12 +852,11 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
motioncellidx *motioncellsidx;
buf = gst_buffer_make_writable (buf);
if (gst_buffer_map (buf, &info, GST_MAP_WRITE)) {
filter->cvImage->imageData = (char *) info.data;
if (filter->firstframe) {
setPrevFrame (filter->cvImage, filter->id);
setPrevFrame (img, filter->id);
filter->firstframe = FALSE;
}
minimum_motion_frames = filter->minimum_motion_frames;
postnomotion = filter->postnomotion;
sensitivity = filter->sensitivity;
@ -919,6 +904,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
filter->changed_gridy = FALSE;
filter->changed_startime = FALSE;
}
datafile = g_strdup (filter->cur_datafile);
filter->cur_buff_timestamp = (GST_BUFFER_TIMESTAMP (buf) / GST_MSECOND);
filter->starttime +=
@ -928,13 +914,12 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
filter->diff_timestamp =
(gint64) (GST_BUFFER_TIMESTAMP (buf) / GST_MSECOND);
changed_datafile = filter->changed_datafile;
motionmaskcells_count = filter->motionmaskcells_count;
motionmaskcellsidx =
g_new0 (motioncellidx, filter->motionmaskcells_count);
motionmaskcellsidx = g_new0 (motioncellidx, filter->motionmaskcells_count);
for (i = 0; i < filter->motionmaskcells_count; i++) {
motionmaskcellsidx[i].lineidx = filter->motionmaskcellsidx[i].lineidx;
motionmaskcellsidx[i].columnidx =
filter->motionmaskcellsidx[i].columnidx;
motionmaskcellsidx[i].columnidx = filter->motionmaskcellsidx[i].columnidx;
}
motioncells_count = filter->motioncells_count;
motioncellsidx = g_new0 (motioncellidx, filter->motioncells_count);
@ -942,10 +927,11 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
motioncellsidx[i].lineidx = filter->motioncellsidx[i].lineidx;
motioncellsidx[i].columnidx = filter->motioncellsidx[i].columnidx;
}
useAlpha = filter->usealpha;
thickness = filter->thickness;
success =
perform_detection_motion_cells (filter->cvImage, sensitivity,
perform_detection_motion_cells (img, sensitivity,
framerate, gridx, gridy,
(gint64) (GST_BUFFER_TIMESTAMP (buf) / GST_MSECOND) -
filter->diff_timestamp, display, useAlpha, motionmaskcoord_count,
@ -958,6 +944,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
int initerrorcode;
GstStructure *s;
GstMessage *m;
initfailedreason = getInitDataFileFailed (filter->id);
initerrorcode = getInitErrorCode (filter->id);
s = gst_structure_new ("motion", "init_error_code", G_TYPE_INT,
@ -971,6 +958,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
int saveerrorcode;
GstStructure *s;
GstMessage *m;
savefailedreason = getSaveDataFileFailed (filter->id);
saveerrorcode = getSaveErrorCode (filter->id);
s = gst_structure_new ("motion", "save_error_code", G_TYPE_INT,
@ -981,7 +969,6 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
}
if (success == -2) {
GST_LOG_OBJECT (filter, "frame dropped");
gst_buffer_unmap (buf, &info);
filter->prev_buff_timestamp = filter->cur_buff_timestamp;
//free
GFREE (datafile);
@ -989,8 +976,9 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
GFREE (motionmaskcellsidx);
GFREE (motioncellsidx);
GST_OBJECT_UNLOCK (filter);
return gst_pad_push (filter->srcpad, buf);
return GST_FLOW_OK;
}
filter->changed_datafile = getChangedDataFile (filter->id);
motioncellsidxcnt = getMotionCellsIdxCnt (filter->id);
numberOfCells = filter->gridx * filter->gridy;
@ -1003,6 +991,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
motiondetect = (motioncellsnumber >= mincellsOfInterestNumber) ? 1 : 0;
if ((motioncellsidxcnt > 0) && (motiondetect == 1)) {
char *detectedmotioncells;
filter->last_motion_timestamp = GST_BUFFER_TIMESTAMP (buf);
detectedmotioncells = getMotionCellsIdx (filter->id);
if (detectedmotioncells) {
@ -1011,6 +1000,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
&& (filter->consecutive_motion >= minimum_motion_frames)) {
GstStructure *s;
GstMessage *m;
GST_DEBUG_OBJECT (filter, "motion started, post msg on the bus");
filter->previous_motion = TRUE;
filter->motion_begin_timestamp = GST_BUFFER_TIMESTAMP (buf);
@ -1022,6 +1012,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
} else if (filter->postallmotion) {
GstStructure *s;
GstMessage *m;
GST_DEBUG_OBJECT (filter, "motion, post msg on the bus");
filter->motion_timestamp = GST_BUFFER_TIMESTAMP (buf);
s = gst_structure_new ("motion", "motion_cells_indices",
@ -1033,6 +1024,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
} else {
GstStructure *s;
GstMessage *m;
s = gst_structure_new ("motion", "motion_cells_indices",
G_TYPE_STRING, "error", NULL);
m = gst_message_new_element (GST_OBJECT (filter), s);
@ -1047,6 +1039,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
if (filter->previous_motion) {
GstStructure *s;
GstMessage *m;
GST_DEBUG_OBJECT (filter, "motion finished, post msg on the bus");
filter->previous_motion = FALSE;
s = gst_structure_new ("motion", "motion_finished", G_TYPE_UINT64,
@ -1067,6 +1060,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
filter->postnomotion) {
GstStructure *s;
GstMessage *m;
filter->last_nomotion_notified = GST_BUFFER_TIMESTAMP (buf);
s = gst_structure_new ("motion", "no_motion", G_TYPE_UINT64,
filter->last_motion_timestamp, NULL);
@ -1075,8 +1069,8 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
}
}
}
gst_buffer_unmap (buf, &info);
filter->prev_buff_timestamp = filter->cur_buff_timestamp;
//free
GFREE (datafile);
GFREE (motionmaskcoords);
@ -1086,10 +1080,7 @@ gst_motion_cells_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
GST_WARNING_OBJECT (filter, "error mapping input buffer");
GST_OBJECT_UNLOCK (filter);
}
} else {
GST_OBJECT_UNLOCK (filter);
}
return gst_pad_push (filter->srcpad, buf);
return GST_FLOW_OK;
}
/* entry point to initialize the plug-in

View file

@ -45,8 +45,7 @@
#ifndef __GST_MOTIONCELLS_H__
#define __GST_MOTIONCELLS_H__
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gstopencvvideofilter.h>
#include <opencv2/core/core_c.h>
#include "motioncells_wrapper.h"
@ -67,8 +66,7 @@ typedef struct _GstMotioncellsClass GstMotioncellsClass;
struct _GstMotioncells
{
GstElement element;
GstPad *sinkpad, *srcpad;
GstOpencvVideoFilter element;
GstState state;
gboolean display, calculate_motion, firstgridx, firstgridy, changed_gridx,
changed_gridy, changed_startime;
@ -78,7 +76,6 @@ struct _GstMotioncells
gchar *prev_datafile, *cur_datafile, *basename_datafile, *datafile_extension;
gint prevgridx, gridx, prevgridy, gridy, id;
gdouble sensitivity, threshold;
IplImage *cvImage;
motionmaskcoordrect *motionmaskcoords;
cellscolor *motioncellscolor;
motioncellidx *motioncellsidx, *motionmaskcellsidx;
@ -97,7 +94,7 @@ struct _GstMotioncells
struct _GstMotioncellsClass
{
GstElementClass parent_class;
GstOpencvVideoFilterClass parent_class;
};
GType gst_motion_cells_get_type (void);