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