]> git.sesse.net Git - vlc/blob - modules/video_filter/scene.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / video_filter / scene.c
1 /*****************************************************************************
2  * scene.c : scene video filter (based on modules/video_output/image.c)
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include <vlc_block.h>
37
38 #include "vlc_filter.h"
39 #include "filter_picture.h"
40 #include "vlc_image.h"
41 #include "vlc_strings.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create      ( vlc_object_t * );
47 static void Destroy     ( vlc_object_t * );
48
49 static picture_t *Filter( filter_t *, picture_t * );
50
51 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic );
52 static void SavePicture( filter_t *, picture_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define FORMAT_TEXT N_( "Image format" )
58 #define FORMAT_LONGTEXT N_( "Format of the output images (png, jpeg, ...)." )
59
60 #define WIDTH_TEXT N_( "Image width" )
61 #define WIDTH_LONGTEXT N_( "You can enforce the image width. By default " \
62                             "(-1) VLC will adapt to the video " \
63                             "characteristics.")
64
65 #define HEIGHT_TEXT N_( "Image height" )
66 #define HEIGHT_LONGTEXT N_( "You can enforce the image height. By default " \
67                             "(-1) VLC will adapt to the video " \
68                             "characteristics.")
69
70 #define RATIO_TEXT N_( "Recording ratio" )
71 #define RATIO_LONGTEXT N_( "Ratio of images to record. "\
72                            "3 means that one image out of three is recorded." )
73
74 #define PREFIX_TEXT N_( "Filename prefix" )
75 #define PREFIX_LONGTEXT N_( "Prefix of the output images filenames. Output " \
76                             "filenames will have the \"prefixNUMBER.format\" "\
77                             "form if replace is not true." )
78
79 #define PATH_TEXT N_( "Directory path prefix" )
80 #define PATH_LONGTEXT N_( "Directory path where images files should be saved." \
81                           "If not set, then images will be automatically saved in " \
82                           "users homedir." )
83
84 #define REPLACE_TEXT N_( "Always write to the same file" )
85 #define REPLACE_LONGTEXT N_( "Always write to the same file instead of " \
86                             "creating one file per image. In this case, " \
87                              "the number is not appended to the filename." )
88
89 #define CFG_PREFIX "scene-"
90
91 vlc_module_begin ()
92     set_shortname( N_( "Scene filter" ) )
93     set_description( N_( "Scene video filter" ) )
94     set_category( CAT_VIDEO )
95     set_subcategory( SUBCAT_VIDEO_VOUT )
96     set_capability( "video filter2", 0 )
97
98     /* General options */
99     add_string(  CFG_PREFIX "format", "png", NULL,
100                  FORMAT_TEXT, FORMAT_LONGTEXT, false )
101     add_integer( CFG_PREFIX "width", -1, NULL,
102                  WIDTH_TEXT, WIDTH_LONGTEXT, true )
103     add_integer( CFG_PREFIX "height", -1, NULL,
104                  HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
105     add_string(  CFG_PREFIX "prefix", "scene", NULL,
106                  PREFIX_TEXT, PREFIX_LONGTEXT, false )
107     add_string(  CFG_PREFIX "path", NULL, NULL,
108                  PATH_TEXT, PATH_LONGTEXT, false )
109     add_bool(    CFG_PREFIX "replace", false, NULL,
110                  REPLACE_TEXT, REPLACE_LONGTEXT, false )
111
112     /* Snapshot method */
113     add_integer( CFG_PREFIX "ratio", 50, NULL,
114                  RATIO_TEXT, RATIO_LONGTEXT, false )
115
116     set_callbacks( Create, Destroy )
117 vlc_module_end ()
118
119 static const char *const ppsz_vfilter_options[] = {
120     "format", "width", "height", "ratio", "prefix", "path", "replace", NULL
121 };
122
123 typedef struct scene_t {
124     picture_t       *p_pic;
125     video_format_t  format;
126 } scene_t;
127
128 /*****************************************************************************
129  * filter_sys_t: private data
130  *****************************************************************************/
131 struct filter_sys_t
132 {
133     image_handler_t *p_image;
134     scene_t *p_scene;
135
136     char *psz_path;
137     char *psz_prefix;
138     char *psz_format;
139     vlc_fourcc_t i_format;
140     int32_t i_width;
141     int32_t i_height;
142     int32_t i_ratio;  /* save every n-th frame */
143     int32_t i_frames; /* frames count */
144     bool  b_replace;
145 };
146
147 /*****************************************************************************
148  * Create: initialize and set pf_video_filter()
149  *****************************************************************************/
150 static int Create( vlc_object_t *p_this )
151 {
152     filter_t *p_filter = (filter_t *)p_this;
153     filter_sys_t *p_sys = NULL;
154
155     config_ChainParse( p_filter, CFG_PREFIX, ppsz_vfilter_options,
156                        p_filter->p_cfg );
157
158     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
159     if( p_filter->p_sys == NULL )
160         return VLC_ENOMEM;
161     memset( p_sys, 0, sizeof(filter_sys_t) );
162
163     p_sys->p_scene = malloc( sizeof( scene_t ) );
164     if( !p_sys->p_scene )
165     {
166         free( p_sys );
167         return VLC_ENOMEM;
168     }
169     memset( p_sys->p_scene, 0, sizeof(scene_t) );
170
171     p_sys->p_image = image_HandlerCreate( p_this );
172     if( !p_sys->p_image )
173     {
174         msg_Err( p_this, "Couldn't get handle to image conversion routines." );
175         free( p_sys->p_scene );
176         free( p_sys );
177         return VLC_EGENERIC;
178     }
179
180     p_sys->psz_format = var_CreateGetString( p_this, CFG_PREFIX "format" );
181     p_sys->i_format = image_Type2Fourcc( p_sys->psz_format );
182     if( !p_sys->i_format )
183     {
184         msg_Err( p_filter, "Could not find FOURCC for image type '%s'",
185                  p_sys->psz_format );
186         image_HandlerDelete( p_sys->p_image );
187         free( p_sys->p_scene );
188         free( p_sys->psz_format );
189         free( p_sys );
190         return VLC_EGENERIC;
191     }
192     p_sys->i_width = var_CreateGetInteger( p_this, CFG_PREFIX "width" );
193     p_sys->i_height = var_CreateGetInteger( p_this, CFG_PREFIX "height" );
194     p_sys->i_ratio = var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
195     p_sys->b_replace = var_CreateGetBool( p_this, CFG_PREFIX "replace" );
196     p_sys->psz_prefix = var_CreateGetString( p_this, CFG_PREFIX "prefix" );
197     p_sys->psz_path = var_GetNonEmptyString( p_this, CFG_PREFIX "path" );
198     if( p_sys->psz_path == NULL )
199     {
200         int i_ret;
201         i_ret = asprintf( &p_sys->psz_path, "%s", config_GetHomeDir());
202         if( i_ret == -1 )
203             p_sys->psz_path = NULL;
204     }
205
206     p_filter->pf_video_filter = Filter;
207
208     return VLC_SUCCESS;
209 }
210
211 /*****************************************************************************
212  * Destroy: destroy video filter method
213  *****************************************************************************/
214 static void Destroy( vlc_object_t *p_this )
215 {
216     filter_t *p_filter = (filter_t *)p_this;
217     filter_sys_t *p_sys = (filter_sys_t *) p_filter->p_sys;
218
219     image_HandlerDelete( p_sys->p_image );
220
221     if( p_sys->p_scene && p_sys->p_scene->p_pic )
222     picture_Release( p_sys->p_scene->p_pic );
223     free( p_sys->p_scene );
224     free( p_sys->psz_format );
225     free( p_sys->psz_prefix );
226     free( p_sys->psz_path );
227     free( p_sys );
228 }
229
230 /*****************************************************************************
231  * Filter: Apply filtering logic to picture.
232  *****************************************************************************/
233 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
234 {
235     /* TODO: think of some funky algorithm to detect scene changes. */
236     SnapshotRatio( p_filter, p_pic );
237     return p_pic;
238 }
239
240 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic )
241 {
242     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
243
244     if( !p_sys || !p_pic ) return;
245
246     if( p_sys->i_frames % p_sys->i_ratio != 0 )
247     {
248         p_sys->i_frames++;
249         return;
250     }
251     p_sys->i_frames++;
252
253     if( p_sys->p_scene )
254     {
255         if( p_sys->p_scene->p_pic )
256             picture_Release( p_sys->p_scene->p_pic );
257
258         if( (p_sys->i_width <= 0) && (p_sys->i_height > 0) )
259         {
260             p_sys->i_width = (p_pic->format.i_width * p_sys->i_height) / p_pic->format.i_height;
261         }
262         else if( (p_sys->i_height <= 0) && (p_sys->i_width > 0) )
263         {
264             p_sys->i_height = (p_pic->format.i_height * p_sys->i_width) / p_pic->format.i_width;
265         }
266         else if( (p_sys->i_width <= 0) && (p_sys->i_height <= 0) )
267         {
268             p_sys->i_width = p_pic->format.i_width;
269             p_sys->i_height = p_pic->format.i_height;
270         }
271         p_sys->p_scene->p_pic = picture_New( p_pic->format.i_chroma,
272            p_pic->format.i_width, p_pic->format.i_height,
273            p_pic->format.i_sar_num );
274         if( p_sys->p_scene->p_pic )
275         {
276             picture_Copy( p_sys->p_scene->p_pic, p_pic );
277             SavePicture( p_filter, p_sys->p_scene->p_pic );
278         }
279     }
280 }
281
282 /*****************************************************************************
283  * Save Picture to disk
284  *****************************************************************************/
285 static void SavePicture( filter_t *p_filter, picture_t *p_pic )
286 {
287     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
288     video_format_t fmt_in, fmt_out;
289     char *psz_filename = NULL;
290     char *psz_temp = NULL;
291     int i_ret;
292
293     memset( &fmt_in, 0, sizeof(video_format_t) );
294     memset( &fmt_out, 0, sizeof(video_format_t) );
295
296     /* Save snapshot psz_format to a memory zone */
297     fmt_in = p_pic->format;
298     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
299     fmt_out.i_width = p_sys->i_width;
300     fmt_out.i_height = p_sys->i_height;
301     fmt_out.i_chroma = p_sys->i_format;
302
303     /*
304      * Save the snapshot to a temporary file and
305      * switch it to the real name afterwards.
306      */
307     if( p_sys->b_replace )
308         i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s.%s",
309                           p_sys->psz_path, p_sys->psz_prefix,
310                           p_sys->psz_format );
311     else
312         i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
313                           p_sys->psz_path, p_sys->psz_prefix,
314                           p_sys->i_frames, p_sys->psz_format );
315
316     if( i_ret == -1 )
317     {
318         msg_Err( p_filter, "could not create snapshot %s", psz_filename );
319         goto error;
320     }
321     path_sanitize( psz_filename );
322
323     i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
324     if( i_ret == -1 )
325     {
326         msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
327         goto error;
328     }
329     path_sanitize( psz_temp );
330
331     /* Save the image */
332     i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
333                             psz_temp );
334     if( i_ret != VLC_SUCCESS )
335     {
336         msg_Err( p_filter, "could not create snapshot %s", psz_temp );
337     }
338     else
339     {
340         /* switch to the final destination */
341         i_ret = rename( psz_temp, psz_filename );
342         if( i_ret == -1 )
343         {
344             msg_Err( p_filter, "could not rename snapshot %s %m", psz_filename );
345             goto error;
346         }
347     }
348
349 error:
350     free( psz_temp );
351     free( psz_filename );
352 }