1 /*****************************************************************************
2 * scene.c : scene video filter (based on modules/video_output/image.c)
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
7 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
38 #include "vlc_filter.h"
39 #include "filter_picture.h"
40 #include "vlc_image.h"
41 #include "vlc_strings.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Create ( vlc_object_t * );
47 static void Destroy ( vlc_object_t * );
49 static picture_t *Filter( filter_t *, picture_t * );
51 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic );
52 static void SavePicture( filter_t *, picture_t * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define FORMAT_TEXT N_( "Image format" )
58 #define FORMAT_LONGTEXT N_( "Format of the output images (png, jpeg, ...)." )
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 " \
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 " \
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." )
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." )
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 " \
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." )
89 #define CFG_PREFIX "scene-"
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 )
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 )
112 /* Snapshot method */
113 add_integer( CFG_PREFIX "ratio", 50, NULL,
114 RATIO_TEXT, RATIO_LONGTEXT, false )
116 set_callbacks( Create, Destroy )
119 static const char *const ppsz_vfilter_options[] = {
120 "format", "width", "height", "ratio", "prefix", "path", "replace", NULL
123 typedef struct scene_t {
125 video_format_t format;
128 /*****************************************************************************
129 * filter_sys_t: private data
130 *****************************************************************************/
133 image_handler_t *p_image;
139 vlc_fourcc_t i_format;
142 int32_t i_ratio; /* save every n-th frame */
143 int32_t i_frames; /* frames count */
147 /*****************************************************************************
148 * Create: initialize and set pf_video_filter()
149 *****************************************************************************/
150 static int Create( vlc_object_t *p_this )
152 filter_t *p_filter = (filter_t *)p_this;
153 filter_sys_t *p_sys = NULL;
155 config_ChainParse( p_filter, CFG_PREFIX, ppsz_vfilter_options,
158 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
159 if( p_filter->p_sys == NULL )
161 memset( p_sys, 0, sizeof(filter_sys_t) );
163 p_sys->p_scene = malloc( sizeof( scene_t ) );
164 if( !p_sys->p_scene )
169 memset( p_sys->p_scene, 0, sizeof(scene_t) );
171 p_sys->p_image = image_HandlerCreate( p_this );
172 if( !p_sys->p_image )
174 msg_Err( p_this, "Couldn't get handle to image conversion routines." );
175 free( p_sys->p_scene );
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 )
184 msg_Err( p_filter, "Could not find FOURCC for image type '%s'",
186 image_HandlerDelete( p_sys->p_image );
187 free( p_sys->p_scene );
188 free( p_sys->psz_format );
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 )
201 i_ret = asprintf( &p_sys->psz_path, "%s", config_GetHomeDir());
203 p_sys->psz_path = NULL;
206 p_filter->pf_video_filter = Filter;
211 /*****************************************************************************
212 * Destroy: destroy video filter method
213 *****************************************************************************/
214 static void Destroy( vlc_object_t *p_this )
216 filter_t *p_filter = (filter_t *)p_this;
217 filter_sys_t *p_sys = (filter_sys_t *) p_filter->p_sys;
219 image_HandlerDelete( p_sys->p_image );
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 );
230 /*****************************************************************************
231 * Filter: Apply filtering logic to picture.
232 *****************************************************************************/
233 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
235 /* TODO: think of some funky algorithm to detect scene changes. */
236 SnapshotRatio( p_filter, p_pic );
240 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic )
242 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
244 if( !p_sys || !p_pic ) return;
246 if( p_sys->i_frames % p_sys->i_ratio != 0 )
255 if( p_sys->p_scene->p_pic )
256 picture_Release( p_sys->p_scene->p_pic );
258 if( (p_sys->i_width <= 0) && (p_sys->i_height > 0) )
260 p_sys->i_width = (p_pic->format.i_width * p_sys->i_height) / p_pic->format.i_height;
262 else if( (p_sys->i_height <= 0) && (p_sys->i_width > 0) )
264 p_sys->i_height = (p_pic->format.i_height * p_sys->i_width) / p_pic->format.i_width;
266 else if( (p_sys->i_width <= 0) && (p_sys->i_height <= 0) )
268 p_sys->i_width = p_pic->format.i_width;
269 p_sys->i_height = p_pic->format.i_height;
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 )
276 picture_Copy( p_sys->p_scene->p_pic, p_pic );
277 SavePicture( p_filter, p_sys->p_scene->p_pic );
282 /*****************************************************************************
283 * Save Picture to disk
284 *****************************************************************************/
285 static void SavePicture( filter_t *p_filter, picture_t *p_pic )
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;
293 memset( &fmt_in, 0, sizeof(video_format_t) );
294 memset( &fmt_out, 0, sizeof(video_format_t) );
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;
304 * Save the snapshot to a temporary file and
305 * switch it to the real name afterwards.
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,
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 );
318 msg_Err( p_filter, "could not create snapshot %s", psz_filename );
321 path_sanitize( psz_filename );
323 i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
326 msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
329 path_sanitize( psz_temp );
332 i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
334 if( i_ret != VLC_SUCCESS )
336 msg_Err( p_filter, "could not create snapshot %s", psz_temp );
340 /* switch to the final destination */
341 i_ret = rename( psz_temp, psz_filename );
344 msg_Err( p_filter, "could not rename snapshot %s %m", psz_filename );
351 free( psz_filename );