]> git.sesse.net Git - vlc/blob - modules/video_filter/opencv_wrapper.c
video_filter_colorthres: fix a typo.
[vlc] / modules / video_filter / opencv_wrapper.c
1 /*****************************************************************************
2  * opencv_wrapper.c : OpenCV wrapper video filter
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  *
6  * Authors: Dugal Harris <dugalh@protoclea.co.za>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #include <cxcore.h>
28 #include <cv.h>
29 #include <highgui.h>
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_vout.h>
38
39 #include <math.h>
40 #include <time.h>
41
42 #include <vlc_filter.h>
43 #include "filter_common.h"
44 #include <vlc_charset.h>
45 #include <vlc_image.h>
46 #include <vlc_input.h>
47 #include <vlc_playlist.h>
48
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Create    ( vlc_object_t * );
54 static void Destroy   ( vlc_object_t * );
55
56 static int  Init      ( vout_thread_t * );
57 static void End       ( vout_thread_t * );
58 static void Render    ( vout_thread_t *, picture_t * );
59
60 static void ReleaseImages( vout_thread_t *p_vout );
61 static void VlcPictureToIplImage( vout_thread_t *p_vout, picture_t *p_in );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66
67 static const char *const chroma_list[] = { "input", "I420", "RGB32"};
68 static const char *const chroma_list_text[] = { N_("Use input chroma unaltered"),
69   N_("I420 - first plane is greyscale"), N_("RGB32")};
70
71 static const char *const output_list[] = { "none", "input", "processed"};
72 static const char *const output_list_text[] = { N_("Don't display any video"),
73   N_("Display the input video"), N_("Display the processed video")};
74
75 static const char *const verbosity_list[] = { "error", "warning", "debug"};
76 static const char *const verbosity_list_text[] = { N_("Show only errors"),
77   N_("Show errors and warnings"), N_("Show everything including debug messages")};
78
79 vlc_module_begin ()
80     set_description( N_("OpenCV video filter wrapper") )
81     set_shortname( N_("OpenCV" ))
82     set_category( CAT_VIDEO )
83     set_subcategory( SUBCAT_VIDEO_VFILTER )
84     set_capability( "video filter", 0 )
85     add_shortcut( "opencv_wrapper" )
86     set_callbacks( Create, Destroy )
87     add_float_with_range( "opencv-scale", 1.0, 0.1, 2.0, NULL,
88                           N_("Scale factor (0.1-2.0)"),
89                           N_("Ammount by which to scale the picture before sending it to the internal OpenCV filter"),
90                           false )
91     add_string( "opencv-chroma", "input", NULL,
92                           N_("OpenCV filter chroma"),
93                           N_("Chroma to convert picture to before sending it to the internal OpenCV filter"), false);
94         change_string_list( chroma_list, chroma_list_text, 0);
95     add_string( "opencv-output", "input", NULL,
96                           N_("Wrapper filter output"),
97                           N_("Determines what (if any) video is displayed by the wrapper filter"), false);
98         change_string_list( output_list, output_list_text, 0);
99     add_string( "opencv-verbosity", "error", NULL,
100                           N_("Wrapper filter verbosity"),
101                           N_("Determines wrapper filter verbosity level"), false);
102         change_string_list( verbosity_list, verbosity_list_text, 0);
103     add_string( "opencv-filter-name", "none", NULL,
104                           N_("OpenCV internal filter name"),
105                           N_("Name of internal OpenCV plugin filter to use"), false);
106 vlc_module_end ()
107
108
109 /*****************************************************************************
110  * wrapper_output_t: what video is output
111  *****************************************************************************/
112 enum wrapper_output_t
113 {
114    NONE,    //not working yet
115    VINPUT,
116    PROCESSED
117 };
118
119 /*****************************************************************************
120  * internal_chroma_t: what chroma is sent to the internal opencv filter
121  *****************************************************************************/
122 enum internal_chroma_t
123 {
124    CINPUT,
125    GREY,
126    RGB
127 };
128
129 /*****************************************************************************
130  * verbosity_t:
131  *****************************************************************************/
132 enum verbosity_t
133 {
134    VERB_ERROR,
135    VERB_WARN,
136    VERB_DEBUG
137 };
138
139 /*****************************************************************************
140  * vout_sys_t: opencv_wrapper video output method descriptor
141  *****************************************************************************
142  * This structure is part of the video output thread descriptor.
143  * It describes the opencv_wrapper specific properties of an output thread.
144  *****************************************************************************/
145 struct vout_sys_t
146 {
147     vout_thread_t *p_vout;
148
149     image_handler_t *p_image;
150
151     int i_cv_image_size;
152
153     picture_t *p_proc_image;
154     picture_t *p_to_be_freed;
155
156     float f_scale;
157
158     int i_wrapper_output;
159     int i_internal_chroma;
160     int i_verbosity;
161
162     IplImage *p_cv_image[VOUT_MAX_PLANES];
163
164     filter_t *p_opencv;
165     char* psz_inner_name;
166
167     picture_t hacked_pic;
168 };
169
170 /*****************************************************************************
171  * Control: control facility for the vout (forwards to child vout)
172  *****************************************************************************/
173 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
174 {
175     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
176 }
177
178 /*****************************************************************************
179  * Create: allocates opencv_wrapper video thread output method
180  *****************************************************************************
181  * This function allocates and initializes a opencv_wrapper vout method.
182  *****************************************************************************/
183 static int Create( vlc_object_t *p_this )
184 {
185     vout_thread_t *p_vout = (vout_thread_t *)p_this;
186     char *psz_chroma, *psz_output, *psz_verbosity;
187     int i = 0;
188
189     /* Allocate structure */
190     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
191     if( p_vout->p_sys == NULL )
192         return VLC_ENOMEM;
193
194     /* Init structure */
195     p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
196     for (i = 0; i < VOUT_MAX_PLANES; i++)
197         p_vout->p_sys->p_cv_image[i] = NULL;
198     p_vout->p_sys->p_proc_image = NULL;
199     p_vout->p_sys->p_to_be_freed = NULL;
200     p_vout->p_sys->i_cv_image_size = 0;
201
202     p_vout->pf_init = Init;
203     p_vout->pf_end = End;
204     p_vout->pf_manage = NULL;
205     p_vout->pf_render = Render;
206     p_vout->pf_display = NULL;
207     p_vout->pf_control = Control;
208
209     /* Retrieve and apply config */
210     psz_chroma = var_InheritString( p_vout, "opencv-chroma" );
211     if( psz_chroma == NULL )
212     {
213         msg_Err( p_vout, "configuration variable %s empty, using 'grey'",
214                          "opencv-chroma" );
215         p_vout->p_sys->i_internal_chroma = GREY;
216     }
217     else
218     {
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;
225         else
226         {
227             msg_Err( p_vout, "no valid opencv-chroma provided, using 'grey'" );
228             p_vout->p_sys->i_internal_chroma = GREY;
229         }
230     }
231     free( psz_chroma);
232
233     psz_output = var_InheritString( p_vout, "opencv-output" );
234     if( psz_output == NULL )
235     {
236         msg_Err( p_vout, "configuration variable %s empty, using 'input'",
237                          "opencv-output" );
238         p_vout->p_sys->i_wrapper_output = VINPUT;
239     }
240     else
241     {
242         if( !strcmp( psz_output, "none" ) )
243             p_vout->p_sys->i_wrapper_output = NONE;
244         else if( !strcmp( psz_output, "input" ) )
245             p_vout->p_sys->i_wrapper_output = VINPUT;
246         else if( !strcmp( psz_output, "processed" ) )
247             p_vout->p_sys->i_wrapper_output = PROCESSED;
248         else
249         {
250             msg_Err( p_vout, "no valid opencv-output provided, using 'input'" );
251             p_vout->p_sys->i_wrapper_output = VINPUT;
252         }
253     }
254     free( psz_output);
255
256     psz_verbosity = var_InheritString( p_vout, "opencv-verbosity" );
257     if( psz_verbosity == NULL )
258     {
259         msg_Err( p_vout, "configuration variable %s empty, using 'input'",
260                          "opencv-verbosity" );
261         p_vout->p_sys->i_verbosity = VERB_ERROR;
262     }
263     else
264     {
265         if( !strcmp( psz_verbosity, "error" ) )
266             p_vout->p_sys->i_verbosity = VERB_ERROR;
267         else if( !strcmp( psz_verbosity, "warning" ) )
268             p_vout->p_sys->i_verbosity = VERB_WARN;
269         else if( !strcmp( psz_verbosity, "debug" ) )
270             p_vout->p_sys->i_verbosity = VERB_DEBUG;
271         else
272         {
273             msg_Err( p_vout, "no valid opencv-verbosity provided, using 'error'" );
274             p_vout->p_sys->i_verbosity = VERB_ERROR;
275         }
276     }
277     free( psz_verbosity);
278
279     p_vout->p_sys->psz_inner_name =
280         var_InheritString( p_vout, "opencv-filter-name" );
281     p_vout->p_sys->f_scale =
282         var_InheritFloat( p_vout, "opencv-scale" );
283
284     if (p_vout->p_sys->i_verbosity > VERB_WARN)
285         msg_Info(p_vout, "Configuration: opencv-scale: %f, opencv-chroma: %d, "
286             "opencv-output: %d, opencv-verbosity %d, opencv-filter %s",
287             p_vout->p_sys->f_scale,
288             p_vout->p_sys->i_internal_chroma,
289             p_vout->p_sys->i_wrapper_output,
290             p_vout->p_sys->i_verbosity,
291             p_vout->p_sys->psz_inner_name);
292
293     return VLC_SUCCESS;
294 }
295
296 /*****************************************************************************
297  * Init: initialize opencv_wrapper video thread output method
298  *****************************************************************************/
299 static int Init( vout_thread_t *p_vout )
300 {
301     video_format_t fmt;
302     vout_sys_t *p_sys = p_vout->p_sys;
303     I_OUTPUTPICTURES = 0;
304
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
312
313     fmt = p_vout->fmt_out;
314     if (p_sys->i_wrapper_output == PROCESSED)   //set to processed video format
315     {
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;
322
323         if (p_sys->i_internal_chroma == GREY)
324             fmt.i_chroma = VLC_CODEC_I420;
325         else if (p_sys->i_internal_chroma == RGB)
326             fmt.i_chroma = VLC_CODEC_RGB32;
327     }
328
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 );
333
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, NULL, false );
337
338     if( !p_sys->p_opencv->p_module )
339     {
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_release( p_sys->p_opencv );
343         p_sys->p_opencv = NULL;
344     }
345
346     /* Try to open the real video output */
347     if (p_sys->i_verbosity > VERB_WARN)
348         msg_Dbg( p_vout, "spawning the real video output" );
349
350     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
351
352     /* Everything failed */
353     if( p_vout->p_sys->p_vout == NULL )
354     {
355         msg_Err( p_vout, "can't open vout, aborting" );
356         return VLC_EGENERIC;
357     }
358
359     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
360
361     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, NULL );
362
363     return VLC_SUCCESS;
364 }
365
366 /*****************************************************************************
367  * End: terminate opencv_wrapper video thread output method
368  *****************************************************************************/
369 static void End( vout_thread_t *p_vout )
370 {
371     vout_sys_t *p_sys = p_vout->p_sys;
372
373     vout_filter_DelChild( p_vout, p_sys->p_vout, NULL );
374     vout_CloseAndRelease( p_sys->p_vout );
375
376     vout_filter_ReleaseDirectBuffers( p_vout );
377
378     if( p_sys->p_opencv )
379     {
380         //release the internal opencv filter
381         if( p_sys->p_opencv->p_module )
382             module_unneed( p_sys->p_opencv, p_sys->p_opencv->p_module );
383         vlc_object_release( p_sys->p_opencv );
384         p_sys->p_opencv = NULL;
385     }
386 }
387
388 /*****************************************************************************
389  * Destroy: destroy opencv_wrapper video thread output method
390  *****************************************************************************
391  * Terminate an output method created by opencv_wrapperCreateOutputMethod
392  *****************************************************************************/
393 static void Destroy( vlc_object_t *p_this )
394 {
395     vout_thread_t *p_vout = (vout_thread_t *)p_this;
396
397     ReleaseImages(p_vout);
398
399     if( p_vout->p_sys->p_image )
400         image_HandlerDelete( p_vout->p_sys->p_image );
401
402     free( p_vout->p_sys );
403 }
404
405 /*****************************************************************************
406  * ReleaseImages: Release OpenCV images in vout_sys_t.
407  *****************************************************************************/
408 static void ReleaseImages(vout_thread_t *p_vout)
409 {
410     int i = 0;
411     if (p_vout->p_sys->p_cv_image)
412     {
413         for (i = 0; i < VOUT_MAX_PLANES; i++)
414         {
415             if (p_vout->p_sys->p_cv_image[i])
416                 cvReleaseImageHeader(&(p_vout->p_sys->p_cv_image[i]));
417             p_vout->p_sys->p_cv_image[i] = NULL;
418         }
419     }
420     p_vout->p_sys->i_cv_image_size = 0;
421
422     /* Release temp picture_t if it exists */
423     if (p_vout->p_sys->p_to_be_freed)
424     {
425         picture_Release( p_vout->p_sys->p_to_be_freed );
426         p_vout->p_sys->p_to_be_freed = NULL;
427     }
428     if (p_vout->p_sys->i_verbosity > VERB_WARN)
429         msg_Dbg( p_vout, "images released" );
430 }
431
432 /*****************************************************************************
433  * VlcPictureToIplImage: Convert picture_t to IplImage
434  *****************************************************************************
435  * Converts given picture_t into IplImage(s) according to module config.
436  * IplImage(s) are stored in vout_sys_t.
437  *****************************************************************************/
438 static void VlcPictureToIplImage( vout_thread_t *p_vout, picture_t *p_in )
439 {
440     int planes = p_in->i_planes;    //num input video planes
441     // input video size
442     CvSize sz = cvSize(abs(p_in->format.i_width), abs(p_in->format.i_height));
443     video_format_t fmt_out;
444     clock_t start, finish;  //performance measures
445     double  duration;
446     int i = 0;
447     vout_sys_t* p_sys = p_vout->p_sys;
448
449     memset( &fmt_out, 0, sizeof(video_format_t) );
450
451     start = clock();
452
453     //do scale / color conversion according to p_sys config
454     if ((p_sys->f_scale != 1) || (p_sys->i_internal_chroma != CINPUT))
455     {
456         fmt_out = p_in->format;
457
458         //calc the scaled video size
459         fmt_out.i_width = p_in->format.i_width * p_sys->f_scale;
460         fmt_out.i_height = p_in->format.i_height * p_sys->f_scale;
461
462         if (p_sys->i_internal_chroma == RGB)
463         {
464             //rgb2 gives 3 separate planes, this gives 1 interleaved plane
465             //rv24 gives is about 20% faster but gives r&b the wrong way round
466             //and I cant think of an easy way to fix this
467             fmt_out.i_chroma = VLC_CODEC_RGB32;
468         }
469         else if (p_sys->i_internal_chroma == GREY)
470         {
471             //take the I (gray) plane (video seems to commonly be in this fmt so usually the
472             //conversion does nothing)
473             fmt_out.i_chroma = VLC_CODEC_I420;
474         }
475
476         //convert from the input image
477         p_sys->p_proc_image = image_Convert( p_sys->p_image, p_in,
478                                      &(p_in->format), &fmt_out );
479
480         if (!p_sys->p_proc_image)
481         {
482             msg_Err(p_vout, "can't convert (unsupported formats?), aborting...");
483             return;
484         }
485
486         p_sys->p_to_be_freed = p_sys->p_proc_image;    //remember this so we can free it later
487
488     }
489     else    //((p_sys->f_scale != 1) || (p_sys->i_internal_chroma != CINPUT))
490     {
491         //use the input image without conversion
492         p_sys->p_proc_image = p_in;
493     }
494
495     //Convert to the IplImage array that is to be processed.
496     //If there are multiple planes in p_sys->p_proc_image, then 1 IplImage
497     //is created for each plane.
498     planes = p_sys->p_proc_image->i_planes;
499     p_sys->i_cv_image_size = planes;
500     for ( i = 0; i < planes; i++ )
501     {
502         sz = cvSize(abs(p_sys->p_proc_image->p[i].i_visible_pitch /
503             p_sys->p_proc_image->p[i].i_pixel_pitch),
504             abs(p_sys->p_proc_image->p[i].i_visible_lines));
505
506         p_sys->p_cv_image[i] = cvCreateImageHeader(sz, IPL_DEPTH_8U,
507             p_sys->p_proc_image->p[i].i_pixel_pitch);
508
509         cvSetData( p_sys->p_cv_image[i],
510             (char*)(p_sys->p_proc_image->p[i].p_pixels), p_sys->p_proc_image->p[i].i_pitch );
511     }
512
513     //Hack the above opencv image array into a picture_t so that it can be sent to
514     //another video filter
515     p_sys->hacked_pic.p_data_orig = p_sys->p_cv_image;
516     p_sys->hacked_pic.i_planes = planes;
517     p_sys->hacked_pic.format.i_chroma = fmt_out.i_chroma;
518
519     //calculate duration of conversion
520     finish = clock();
521     duration = (double)(finish - start) / CLOCKS_PER_SEC;
522     if (p_sys->i_verbosity > VERB_WARN)
523         msg_Dbg( p_vout, "VlcPictureToIplImageRgb took %2.4f seconds", duration );
524 }
525
526 /*****************************************************************************
527  * Render: displays previously rendered output
528  *****************************************************************************
529  * This function send the currently rendered image to the internal opencv
530  * filter for processing.
531  *****************************************************************************/
532 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
533 {
534     picture_t *p_outpic = NULL;
535     clock_t start, finish;
536     double  duration;
537
538     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
539               == NULL )
540     {
541         if( !vlc_object_alive (p_vout) || p_vout->b_error )
542         {   return; }
543         msleep( VOUT_OUTMEM_SLEEP );
544     }
545
546     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
547
548     start = clock();
549
550     if (p_vout->p_sys->i_wrapper_output == VINPUT)  //output = input video
551     {
552         //This copy is a bit unfortunate but image_Convert can't write into an existing image so it is better to copy the
553         //(say) 16bit YUV image here than a 32bit RGB image somehwere else.
554         //It is also not that expensive in time.
555         picture_Copy( p_outpic, p_pic );
556         VlcPictureToIplImage( p_vout, p_pic);
557         //pass the image to the internal opencv filter for processing
558         if ((p_vout->p_sys->p_opencv) && (p_vout->p_sys->p_opencv->p_module))
559             p_vout->p_sys->p_opencv->pf_video_filter( p_vout->p_sys->p_opencv, &(p_vout->p_sys->hacked_pic));
560     }
561     else    //output = processed video (NONE option not working yet)
562     {
563         VlcPictureToIplImage( p_vout, p_pic);
564         //pass the image to the internal opencv filter for processing
565         if ((p_vout->p_sys->p_opencv) && (p_vout->p_sys->p_opencv->p_module))
566             p_vout->p_sys->p_opencv->pf_video_filter( p_vout->p_sys->p_opencv, &(p_vout->p_sys->hacked_pic));
567         //copy the processed image into the output image
568         if ((p_vout->p_sys->p_proc_image) && (p_vout->p_sys->p_proc_image->p_data))
569             picture_Copy( p_outpic, p_vout->p_sys->p_proc_image );
570     }
571
572     //calculate duration
573     finish = clock();
574     duration = (double)(finish - start) / CLOCKS_PER_SEC;
575     if (p_vout->p_sys->i_verbosity > VERB_WARN)
576         msg_Dbg( p_vout, "Render took %2.4f seconds", duration );
577
578     ReleaseImages(p_vout);
579     p_outpic->date  = p_pic->date;
580     
581     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
582     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
583 }
584