1 /*****************************************************************************
2 * opencv_wrapper.c : OpenCV wrapper video filter
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
6 * Authors: Dugal Harris <dugalh@protoclea.co.za>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
38 #include "vlc_filter.h"
39 #include "filter_common.h"
40 #include <vlc_charset.h>
41 #include "vlc_image.h"
42 #include "vlc_input.h"
43 #include "vlc_playlist.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Create ( vlc_object_t * );
50 static void Destroy ( vlc_object_t * );
52 static int Init ( vout_thread_t * );
53 static void End ( vout_thread_t * );
54 static void Render ( vout_thread_t *, picture_t * );
56 static int SendEvents( vlc_object_t *, char const *,
57 vlc_value_t, vlc_value_t, void * );
58 static void ReleaseImages( vout_thread_t *p_vout );
59 static void VlcPictureToIplImage( vout_thread_t *p_vout, picture_t *p_in );
61 /*****************************************************************************
63 *****************************************************************************/
65 static char *chroma_list[] = { "input", "I420", "RGB32"};
66 static char *chroma_list_text[] = { N_("Use input chroma unaltered"),
67 N_("I420 - first plane is greyscale"), N_("RGB32")};
69 static char *output_list[] = { "none", "input", "processed"};
70 static char *output_list_text[] = { N_("Don't display any video"),
71 N_("Display the input video"), N_("Display the processed video")};
73 static char *verbosity_list[] = { "error", "warning", "debug"};
74 static char *verbosity_list_text[] = { N_("Show only errors"),
75 N_("Show errors and warnings"), N_("Show everything including debug messages")};
78 set_description( _("OpenCV video filter wrapper") );
79 set_shortname( _("OpenCV" ));
80 set_category( CAT_VIDEO );
81 set_subcategory( SUBCAT_VIDEO_VFILTER );
82 set_capability( "video filter", 0 );
83 add_shortcut( "opencv_wrapper" );
84 set_callbacks( Create, Destroy );
85 add_float_with_range( "opencv-scale", 1.0, 0.1, 2.0, NULL,
86 N_("Scale factor (0.1-2.0)"),
87 N_("Ammount by which to scale the picture before sending it to the internal OpenCV filter"),
89 add_string( "opencv-chroma", "input", NULL,
90 N_("OpenCV filter chroma"),
91 N_("Chroma to convert picture to before sending it to the internal OpenCV filter"), VLC_FALSE);
92 change_string_list( chroma_list, chroma_list_text, 0);
93 add_string( "opencv-output", "input", NULL,
94 N_("Wrapper filter output"),
95 N_("Determines what (if any) video is displayed by the wrapper filter"), VLC_FALSE);
96 change_string_list( output_list, output_list_text, 0);
97 add_string( "opencv-verbosity", "error", NULL,
98 N_("Wrapper filter verbosity"),
99 N_("Determines wrapper filter verbosity level"), VLC_FALSE);
100 change_string_list( verbosity_list, verbosity_list_text, 0);
101 add_string( "opencv-filter-name", "none", NULL,
102 N_("OpenCV internal filter name"),
103 N_("Name of internal OpenCV plugin filter to use"), VLC_FALSE);
107 /*****************************************************************************
108 * wrapper_output_t: what video is output
109 *****************************************************************************/
110 enum wrapper_output_t
112 NONE, //not working yet
117 /*****************************************************************************
118 * internal_chroma_t: what chroma is sent to the internal opencv filter
119 *****************************************************************************/
120 enum internal_chroma_t
127 /*****************************************************************************
129 *****************************************************************************/
137 /*****************************************************************************
138 * vout_sys_t: opencv_wrapper video output method descriptor
139 *****************************************************************************
140 * This structure is part of the video output thread descriptor.
141 * It describes the opencv_wrapper specific properties of an output thread.
142 *****************************************************************************/
145 vout_thread_t *p_vout;
147 image_handler_t *p_image;
151 picture_t *p_proc_image;
152 picture_t *p_to_be_freed;
156 int i_wrapper_output;
157 int i_internal_chroma;
160 IplImage *p_cv_image[VOUT_MAX_PLANES];
163 char* psz_inner_name;
165 picture_t hacked_pic;
168 /*****************************************************************************
169 * Control: control facility for the vout (forwards to child vout)
170 *****************************************************************************/
171 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
173 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
176 /*****************************************************************************
177 * Create: allocates opencv_wrapper video thread output method
178 *****************************************************************************
179 * This function allocates and initializes a opencv_wrapper vout method.
180 *****************************************************************************/
181 static int Create( vlc_object_t *p_this )
183 vout_thread_t *p_vout = (vout_thread_t *)p_this;
184 char *psz_chroma, *psz_output, *psz_verbosity;
187 /* Allocate structure */
188 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
189 if( p_vout->p_sys == NULL )
191 msg_Err( p_vout, "out of memory" );
196 p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
197 for (i = 0; i < VOUT_MAX_PLANES; i++)
198 p_vout->p_sys->p_cv_image[i] = NULL;
199 p_vout->p_sys->p_proc_image = NULL;
200 p_vout->p_sys->p_to_be_freed = NULL;
201 p_vout->p_sys->i_cv_image_size = 0;
203 p_vout->pf_init = Init;
204 p_vout->pf_end = End;
205 p_vout->pf_manage = NULL;
206 p_vout->pf_render = Render;
207 p_vout->pf_display = NULL;
208 p_vout->pf_control = Control;
210 /* Retrieve and apply config */
211 if( !(psz_chroma = config_GetPsz( p_vout, "opencv-chroma" )) )
213 msg_Err( p_vout, "configuration variable %s empty, using 'grey'",
215 p_vout->p_sys->i_internal_chroma = GREY;
219 if( !strcmp( psz_chroma, "input" ) )
220 p_vout->p_sys->i_internal_chroma = CINPUT;
221 else if( !strcmp( psz_chroma, "I420" ) )
222 p_vout->p_sys->i_internal_chroma = GREY;
223 else if( !strcmp( psz_chroma, "RGB32" ) )
224 p_vout->p_sys->i_internal_chroma = RGB;
227 msg_Err( p_vout, "no valid opencv-chroma provided, using 'grey'" );
228 p_vout->p_sys->i_internal_chroma = GREY;
233 if( !(psz_output = config_GetPsz( p_vout, "opencv-output" )) )
235 msg_Err( p_vout, "configuration variable %s empty, using 'input'",
237 p_vout->p_sys->i_wrapper_output = VINPUT;
241 if( !strcmp( psz_output, "none" ) )
242 p_vout->p_sys->i_wrapper_output = NONE;
243 else if( !strcmp( psz_output, "input" ) )
244 p_vout->p_sys->i_wrapper_output = VINPUT;
245 else if( !strcmp( psz_output, "processed" ) )
246 p_vout->p_sys->i_wrapper_output = PROCESSED;
249 msg_Err( p_vout, "no valid opencv-output provided, using 'input'" );
250 p_vout->p_sys->i_wrapper_output = VINPUT;
255 if( !(psz_verbosity = config_GetPsz( p_vout, "opencv-verbosity" )) )
257 msg_Err( p_vout, "configuration variable %s empty, using 'input'",
258 "opencv-verbosity" );
259 p_vout->p_sys->i_verbosity = VERB_ERROR;
263 if( !strcmp( psz_verbosity, "error" ) )
264 p_vout->p_sys->i_verbosity = VERB_ERROR;
265 else if( !strcmp( psz_verbosity, "warning" ) )
266 p_vout->p_sys->i_verbosity = VERB_WARN;
267 else if( !strcmp( psz_verbosity, "debug" ) )
268 p_vout->p_sys->i_verbosity = VERB_DEBUG;
271 msg_Err( p_vout, "no valid opencv-verbosity provided, using 'error'" );
272 p_vout->p_sys->i_verbosity = VERB_ERROR;
275 free( psz_verbosity);
277 p_vout->p_sys->psz_inner_name = config_GetPsz( p_vout, "opencv-filter-name" );
279 p_vout->p_sys->f_scale =
280 config_GetFloat( p_vout, "opencv-scale" );
282 if (p_vout->p_sys->i_verbosity > VERB_WARN)
283 msg_Info(p_vout, "Configuration: opencv-scale: %f, opencv-chroma: %d, "
284 "opencv-output: %d, opencv-verbosity %d, opencv-filter %s",
285 p_vout->p_sys->f_scale,
286 p_vout->p_sys->i_internal_chroma,
287 p_vout->p_sys->i_wrapper_output,
288 p_vout->p_sys->i_verbosity,
289 p_vout->p_sys->psz_inner_name);
294 /*****************************************************************************
295 * Init: initialize opencv_wrapper video thread output method
296 *****************************************************************************/
297 static int Init( vout_thread_t *p_vout )
302 vout_sys_t *p_sys = p_vout->p_sys;
303 I_OUTPUTPICTURES = 0;
305 /* Initialize the output video format */
306 memset( &fmt, 0, sizeof(video_format_t) );
307 p_vout->output.i_chroma = p_vout->render.i_chroma;
308 p_vout->output.i_width = p_vout->render.i_width;
309 p_vout->output.i_height = p_vout->render.i_height;
310 p_vout->output.i_aspect = p_vout->render.i_aspect;
311 p_vout->fmt_out = p_vout->fmt_in; //set to input video format
313 fmt = p_vout->fmt_out;
314 if (p_sys->i_wrapper_output == PROCESSED) //set to processed video format
316 fmt.i_width = fmt.i_width * p_sys->f_scale;
317 fmt.i_height = fmt.i_height * p_sys->f_scale;
318 fmt.i_visible_width = fmt.i_visible_width * p_sys->f_scale;
319 fmt.i_visible_height = fmt.i_visible_height * p_sys->f_scale;
320 fmt.i_x_offset = fmt.i_x_offset * p_sys->f_scale;
321 fmt.i_y_offset = fmt.i_y_offset * p_sys->f_scale;
323 if (p_sys->i_internal_chroma == GREY)
324 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
325 else if (p_sys->i_internal_chroma == RGB)
326 fmt.i_chroma = VLC_FOURCC('R','V','3','2');
329 /* Load the internal opencv filter */
330 /* We don't need to set up video formats for this filter as it not actually using a picture_t */
331 p_sys->p_opencv = vlc_object_create( p_vout, sizeof(filter_t) );
332 vlc_object_attach( p_sys->p_opencv, p_vout );
334 if (p_vout->p_sys->psz_inner_name)
335 p_sys->p_opencv->p_module =
336 module_Need( p_sys->p_opencv, p_sys->psz_inner_name, 0, 0 );
338 if( !p_sys->p_opencv->p_module )
340 msg_Err( p_vout, "can't open internal opencv filter: %s", p_vout->p_sys->psz_inner_name );
341 p_vout->p_sys->psz_inner_name = NULL;
342 vlc_object_detach( p_sys->p_opencv );
343 vlc_object_destroy( p_sys->p_opencv );
344 p_sys->p_opencv = NULL;
347 /* Try to open the real video output */
348 if (p_sys->i_verbosity > VERB_WARN)
349 msg_Dbg( p_vout, "spawning the real video output" );
351 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
353 /* Everything failed */
354 if( p_vout->p_sys->p_vout == NULL )
356 msg_Err( p_vout, "can't open vout, aborting" );
360 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
362 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
364 ADD_PARENT_CALLBACKS( SendEventsToChild );
369 /*****************************************************************************
370 * End: terminate opencv_wrapper video thread output method
371 *****************************************************************************/
372 static void End( vout_thread_t *p_vout )
376 /* Free the fake output buffers we allocated */
377 for( i_index = I_OUTPUTPICTURES ; i_index ; )
380 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
383 if ( p_vout->p_sys->p_opencv )
385 //release the internal opencv filter
386 if( p_vout->p_sys->p_opencv->p_module )
387 module_Unneed( p_vout->p_sys->p_opencv, p_vout->p_sys->p_opencv->p_module );
388 vlc_object_detach( p_vout->p_sys->p_opencv );
389 vlc_object_destroy( p_vout->p_sys->p_opencv );
390 p_vout->p_sys->p_opencv = NULL;
395 /*****************************************************************************
396 * Destroy: destroy opencv_wrapper video thread output method
397 *****************************************************************************
398 * Terminate an output method created by opencv_wrapperCreateOutputMethod
399 *****************************************************************************/
400 static void Destroy( vlc_object_t *p_this )
402 vout_thread_t *p_vout = (vout_thread_t *)p_this;
404 if( p_vout->p_sys->p_vout )
406 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
407 vlc_object_detach( p_vout->p_sys->p_vout );
408 vout_Destroy( p_vout->p_sys->p_vout );
411 DEL_PARENT_CALLBACKS( SendEventsToChild );
413 ReleaseImages(p_vout);
415 if( p_vout->p_sys->p_image )
416 image_HandlerDelete( p_vout->p_sys->p_image );
418 free( p_vout->p_sys );
421 /*****************************************************************************
422 * ReleaseImages: Release OpenCV images in vout_sys_t.
423 *****************************************************************************/
424 static void ReleaseImages(vout_thread_t *p_vout)
427 if (p_vout->p_sys->p_cv_image)
429 for (i = 0; i < VOUT_MAX_PLANES; i++)
431 if (p_vout->p_sys->p_cv_image[i])
432 cvReleaseImageHeader(&(p_vout->p_sys->p_cv_image[i]));
433 p_vout->p_sys->p_cv_image[i] = NULL;
436 p_vout->p_sys->i_cv_image_size = 0;
438 /* Release temp picture_t if it exists */
439 if (p_vout->p_sys->p_to_be_freed)
441 p_vout->p_sys->p_to_be_freed->pf_release( p_vout->p_sys->p_to_be_freed );
442 p_vout->p_sys->p_to_be_freed = NULL;
444 if (p_vout->p_sys->i_verbosity > VERB_WARN)
445 msg_Dbg( p_vout, "images released" );
448 /*****************************************************************************
449 * VlcPictureToIplImage: Convert picture_t to IplImage
450 *****************************************************************************
451 * Converts given picture_t into IplImage(s) according to module config.
452 * IplImage(s) are stored in vout_sys_t.
453 *****************************************************************************/
454 static void VlcPictureToIplImage( vout_thread_t *p_vout, picture_t *p_in )
456 int planes = p_in->i_planes; //num input video planes
458 CvSize sz = cvSize(abs(p_in->format.i_width), abs(p_in->format.i_height));
459 video_format_t fmt_out;
460 clock_t start, finish; //performance measures
463 vout_sys_t* p_sys = p_vout->p_sys;
465 memset( &fmt_out, 0, sizeof(video_format_t) );
469 //do scale / color conversion according to p_sys config
470 if ((p_sys->f_scale != 1) || (p_sys->i_internal_chroma != CINPUT))
472 fmt_out = p_in->format;
474 //calc the scaled video size
475 fmt_out.i_width = p_in->format.i_width * p_sys->f_scale;
476 fmt_out.i_height = p_in->format.i_height * p_sys->f_scale;
478 if (p_sys->i_internal_chroma == RGB)
480 //rgb2 gives 3 separate planes, this gives 1 interleaved plane
481 //rv24 gives is about 20% faster but gives r&b the wrong way round
482 //and I cant think of an easy way to fix this
483 fmt_out.i_chroma = VLC_FOURCC('R','V','3','2');
485 else if (p_sys->i_internal_chroma == GREY)
487 //take the I (gray) plane (video seems to commonly be in this fmt so usually the
488 //conversion does nothing)
489 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
492 //convert from the input image
493 p_sys->p_proc_image = image_Convert( p_sys->p_image, p_in,
494 &(p_in->format), &fmt_out );
496 if (!p_sys->p_proc_image)
498 msg_Err(p_vout, "can't convert (unsupported formats?), aborting...");
502 p_sys->p_to_be_freed = p_sys->p_proc_image; //remember this so we can free it later
505 else //((p_sys->f_scale != 1) || (p_sys->i_internal_chroma != CINPUT))
507 //use the input image without conversion
508 p_sys->p_proc_image = p_in;
511 //Convert to the IplImage array that is to be processed.
512 //If there are multiple planes in p_sys->p_proc_image, then 1 IplImage
513 //is created for each plane.
514 planes = p_sys->p_proc_image->i_planes;
515 p_sys->i_cv_image_size = planes;
516 for ( i = 0; i < planes; i++ )
518 sz = cvSize(abs(p_sys->p_proc_image->p[i].i_visible_pitch /
519 p_sys->p_proc_image->p[i].i_pixel_pitch),
520 abs(p_sys->p_proc_image->p[i].i_visible_lines));
522 p_sys->p_cv_image[i] = cvCreateImageHeader(sz, IPL_DEPTH_8U,
523 p_sys->p_proc_image->p[i].i_pixel_pitch);
525 cvSetData( p_sys->p_cv_image[i],
526 (char*)(p_sys->p_proc_image->p[i].p_pixels), p_sys->p_proc_image->p[i].i_pitch );
529 //Hack the above opencv image array into a picture_t so that it can be sent to
530 //another video filter
531 p_sys->hacked_pic.p_data_orig = p_sys->p_cv_image;
532 p_sys->hacked_pic.i_planes = planes;
533 p_sys->hacked_pic.format.i_chroma = fmt_out.i_chroma;
535 //calculate duration of conversion
537 duration = (double)(finish - start) / CLOCKS_PER_SEC;
538 if (p_sys->i_verbosity > VERB_WARN)
539 msg_Dbg( p_vout, "VlcPictureToIplImageRgb took %2.4f seconds\n", duration );
542 /*****************************************************************************
543 * Render: displays previously rendered output
544 *****************************************************************************
545 * This function send the currently rendered image to the internal opencv
546 * filter for processing.
547 *****************************************************************************/
548 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
550 picture_t *p_outpic = NULL;
551 clock_t start, finish;
554 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
557 if( p_vout->b_die || p_vout->b_error )
559 msleep( VOUT_OUTMEM_SLEEP );
562 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
566 if (p_vout->p_sys->i_wrapper_output == VINPUT) //output = input video
568 //This copy is a bit unfortunate but image_Convert can't write into an existing image so it is better to copy the
569 //(say) 16bit YUV image here than a 32bit RGB image somehwere else.
570 //It is also not that expensive in time.
571 vout_CopyPicture( p_vout, p_outpic, p_pic );
572 VlcPictureToIplImage( p_vout, p_pic);
573 //pass the image to the internal opencv filter for processing
574 if ((p_vout->p_sys->p_opencv) && (p_vout->p_sys->p_opencv->p_module))
575 p_vout->p_sys->p_opencv->pf_video_filter( p_vout->p_sys->p_opencv, &(p_vout->p_sys->hacked_pic));
577 else //output = processed video (NONE option not working yet)
579 VlcPictureToIplImage( p_vout, p_pic);
580 //pass the image to the internal opencv filter for processing
581 if ((p_vout->p_sys->p_opencv) && (p_vout->p_sys->p_opencv->p_module))
582 p_vout->p_sys->p_opencv->pf_video_filter( p_vout->p_sys->p_opencv, &(p_vout->p_sys->hacked_pic));
583 //copy the processed image into the output image
584 if ((p_vout->p_sys->p_proc_image) && (p_vout->p_sys->p_proc_image->p_data))
585 vout_CopyPicture( p_vout, p_outpic, p_vout->p_sys->p_proc_image );
590 duration = (double)(finish - start) / CLOCKS_PER_SEC;
591 if (p_vout->p_sys->i_verbosity > VERB_WARN)
592 msg_Dbg( p_vout, "Render took %2.4f seconds\n", duration );
594 ReleaseImages(p_vout);
595 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
597 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
598 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
601 /*****************************************************************************
602 * SendEvents: forward mouse and keyboard events to the parent p_vout
603 *****************************************************************************/
604 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
605 vlc_value_t oldval, vlc_value_t newval, void *p_data )
607 var_Set( (vlc_object_t *)p_data, psz_var, newval );
612 /*****************************************************************************
613 * SendEventsToChild: forward events to the child/children vout
614 *****************************************************************************/
615 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
616 vlc_value_t oldval, vlc_value_t newval, void *p_data )
618 vout_thread_t *p_vout = (vout_thread_t *)p_this;
619 var_Set( p_vout->p_sys->p_vout, psz_var, newval );