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