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