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 or jpg)." )
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 static const char *const psz_format_list[] = { "png", "jpeg" };
90 static const char *const psz_format_list_text[] = { "PNG", "JPEG" };
92 #define CFG_PREFIX "scene-"
95 set_shortname( N_( "Scene filter" ) )
96 set_description( N_( "Scene video filter" ) )
97 set_category( CAT_VIDEO )
98 set_subcategory( SUBCAT_VIDEO_VOUT )
99 set_capability( "video filter2", 0 )
101 /* General options */
102 add_string( CFG_PREFIX "format", "png", NULL,
103 FORMAT_TEXT, FORMAT_LONGTEXT, false )
104 change_string_list( psz_format_list, psz_format_list_text, 0 );
105 add_integer( CFG_PREFIX "width", 288, NULL,
106 WIDTH_TEXT, WIDTH_LONGTEXT, true )
107 add_integer( CFG_PREFIX "height", 160, NULL,
108 HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
109 add_string( CFG_PREFIX "prefix", "scene", NULL,
110 PREFIX_TEXT, PREFIX_LONGTEXT, false )
111 add_string( CFG_PREFIX "path", NULL, NULL,
112 PATH_TEXT, PATH_LONGTEXT, false )
113 add_bool( CFG_PREFIX "replace", false, NULL,
114 REPLACE_TEXT, REPLACE_LONGTEXT, false )
116 /* Snapshot method */
117 add_integer( CFG_PREFIX "ratio", 50, NULL,
118 RATIO_TEXT, RATIO_LONGTEXT, false )
120 set_callbacks( Create, Destroy )
123 static const char *const ppsz_vfilter_options[] = {
124 "format", "width", "height", "ratio", "prefix", "path", "replace", NULL
127 typedef struct scene_t {
129 video_format_t format;
132 /*****************************************************************************
133 * filter_sys_t: private data
134 *****************************************************************************/
137 image_handler_t *p_image;
145 int32_t i_ratio; /* save every n-th frame */
146 int32_t i_frames; /* frames count */
150 /*****************************************************************************
151 * Create: initialize and set pf_video_filter()
152 *****************************************************************************/
153 static int Create( vlc_object_t *p_this )
155 filter_t *p_filter = (filter_t *)p_this;
156 filter_sys_t *p_sys = NULL;
158 config_ChainParse( p_filter, CFG_PREFIX, ppsz_vfilter_options,
161 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
162 if( p_filter->p_sys == NULL )
164 memset( p_sys, 0, sizeof(filter_sys_t) );
166 p_sys->p_scene = malloc( sizeof( scene_t ) );
167 if( !p_sys->p_scene )
172 memset( p_sys->p_scene, 0, sizeof(scene_t) );
174 p_sys->p_image = image_HandlerCreate( p_this );
175 if( !p_sys->p_image )
177 msg_Err( p_this, "Couldn't get handle to image conversion routines." );
178 free( p_sys->p_scene );
183 p_sys->psz_prefix = var_CreateGetString( p_this, CFG_PREFIX "prefix" );
184 p_sys->psz_path = var_GetNonEmptyString( p_this, CFG_PREFIX "path" );
185 if( p_sys->psz_path == NULL )
188 i_ret = asprintf( &p_sys->psz_path, "%s", config_GetHomeDir());
190 p_sys->psz_path = NULL;
192 p_sys->psz_format = var_CreateGetString( p_this, CFG_PREFIX "format" );
193 p_sys->i_width = var_CreateGetInteger( p_this, CFG_PREFIX "width" );
194 p_sys->i_height = var_CreateGetInteger( p_this, CFG_PREFIX "height" );
195 p_sys->i_ratio = var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
196 p_sys->b_replace = var_CreateGetBool( p_this, CFG_PREFIX "replace" );
198 p_filter->pf_video_filter = Filter;
203 /*****************************************************************************
204 * Destroy: destroy video filter method
205 *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
208 filter_t *p_filter = (filter_t *)p_this;
209 filter_sys_t *p_sys = (filter_sys_t *) p_filter->p_sys;
211 image_HandlerDelete( p_sys->p_image );
213 if( p_sys->p_scene && p_sys->p_scene->p_pic )
214 picture_Release( p_sys->p_scene->p_pic );
215 free( p_sys->p_scene );
216 free( p_sys->psz_format );
217 free( p_sys->psz_prefix );
218 free( p_sys->psz_path );
222 /*****************************************************************************
223 * Filter: Apply filtering logic to picture.
224 *****************************************************************************/
225 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
227 /* TODO: think of some funky algorithm to detect scene changes. */
228 SnapshotRatio( p_filter, p_pic );
232 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic )
234 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
236 if( !p_sys || !p_pic ) return;
238 if( p_sys->i_frames % p_sys->i_ratio != 0 )
247 if( p_sys->p_scene->p_pic )
248 picture_Release( p_sys->p_scene->p_pic );
249 p_sys->p_scene->p_pic = picture_New( p_pic->format.i_chroma,
250 p_pic->format.i_width, p_pic->format.i_height,
251 p_pic->format.i_sar_num );
252 if( p_sys->p_scene->p_pic )
254 picture_Copy( p_sys->p_scene->p_pic, p_pic );
255 SavePicture( p_filter, p_sys->p_scene->p_pic );
260 /*****************************************************************************
261 * Save Picture to disk
262 *****************************************************************************/
263 static void SavePicture( filter_t *p_filter, picture_t *p_pic )
265 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
266 video_format_t fmt_in, fmt_out;
267 char *psz_filename = NULL;
268 char *psz_temp = NULL;
271 memset( &fmt_in, 0, sizeof(video_format_t) );
272 memset( &fmt_out, 0, sizeof(video_format_t) );
274 /* Save snapshot psz_format to a memory zone */
275 fmt_in = p_pic->format;
276 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
277 fmt_out.i_width = p_sys->i_width;
278 fmt_out.i_height = p_sys->i_height;
279 if( strlen( p_sys->psz_format ) == 3 )
280 fmt_out.i_chroma = VLC_FOURCC('p','n','g',' ');
282 fmt_out.i_chroma = VLC_FOURCC('j','p','e','g');
284 if( (fmt_out.i_width == 0) && (fmt_out.i_height > 0) )
286 fmt_out.i_width = (fmt_in.i_width * fmt_out.i_height) / fmt_in.i_height;
288 else if( (fmt_out.i_height == 0) && (fmt_out.i_width > 0) )
290 fmt_out.i_height = (fmt_in.i_height * fmt_out.i_width) / fmt_in.i_width;
292 else if( (fmt_out.i_width == 0) && (fmt_out.i_height == 0) )
294 fmt_out.i_width = fmt_in.i_width;
295 fmt_out.i_height = fmt_in.i_height;
299 * Save the snapshot to a temporary file and
300 * switch it to the real name afterwards.
302 if( p_sys->b_replace )
303 i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s.%s",
304 p_sys->psz_path, p_sys->psz_prefix,
307 i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
308 p_sys->psz_path, p_sys->psz_prefix,
309 p_sys->i_frames, p_sys->psz_format );
313 msg_Err( p_filter, "could not create snapshot %s", psz_filename );
316 path_sanitize( psz_filename );
318 i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
321 msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
324 path_sanitize( psz_temp );
327 i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
329 if( i_ret != VLC_SUCCESS )
331 msg_Err( p_filter, "could not create snapshot %s", psz_temp );
335 /* switch to the final destination */
336 i_ret = rename( psz_temp, psz_filename );
339 msg_Err( p_filter, "could not rename snapshot %s %m", psz_filename );
346 free( psz_filename );