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