]> git.sesse.net Git - vlc/blob - modules/video_filter/scene.c
Remove most stray semi-colons in module descriptions
[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 or jpg)." )
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 static const char *const psz_format_list[] = { "png", "jpeg" };
90 static const char *const psz_format_list_text[] = { "PNG", "JPEG" };
91
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_category( CAT_VIDEO )
98     set_subcategory( SUBCAT_VIDEO_VOUT )
99     set_capability( "video filter2", 0 )
100
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 );
115
116     /* Snapshot method */
117     add_integer( CFG_PREFIX "ratio", 50, NULL,
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 *p_scene;
139
140     char *psz_path;
141     char *psz_prefix;
142     char *psz_format;
143     int32_t i_width;
144     int32_t i_height;
145     int32_t i_ratio;  /* save every n-th frame */
146     int32_t i_frames; /* frames count */
147     bool  b_replace;
148 };
149
150 /*****************************************************************************
151  * Create: initialize and set pf_video_filter()
152  *****************************************************************************/
153 static int Create( vlc_object_t *p_this )
154 {
155     filter_t *p_filter = (filter_t *)p_this;
156     filter_sys_t *p_sys = NULL;
157
158     config_ChainParse( p_filter, CFG_PREFIX, ppsz_vfilter_options,
159                        p_filter->p_cfg );
160
161     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
162     if( p_filter->p_sys == NULL )
163         return VLC_ENOMEM;
164     memset( p_sys, 0, sizeof(filter_sys_t) );
165
166     p_sys->p_scene = malloc( sizeof( scene_t ) );
167     if( !p_sys->p_scene )
168     {
169         free( p_sys );
170         return VLC_ENOMEM;
171     }
172     memset( p_sys->p_scene, 0, sizeof(scene_t) );
173
174     p_sys->p_image = image_HandlerCreate( p_this );
175     if( !p_sys->p_image )
176     {
177         msg_Err( p_this, "Couldn't get handle to image conversion routines." );
178         free( p_sys->p_scene );
179         free( p_sys );
180         return VLC_EGENERIC;
181     }
182
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 )
186     {
187         int i_ret;
188         i_ret = asprintf( &p_sys->psz_path, "%s", config_GetHomeDir());
189         if( i_ret == -1 )
190             p_sys->psz_path = NULL;
191     }
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" );
197
198     p_filter->pf_video_filter = Filter;
199
200     return VLC_SUCCESS;
201 }
202
203 /*****************************************************************************
204  * Destroy: destroy video filter method
205  *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
207 {
208     filter_t *p_filter = (filter_t *)p_this;
209     filter_sys_t *p_sys = (filter_sys_t *) p_filter->p_sys;
210
211     image_HandlerDelete( p_sys->p_image );
212
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 );
219     free( p_sys );
220 }
221
222 /*****************************************************************************
223  * Filter: Apply filtering logic to picture.
224  *****************************************************************************/
225 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
226 {
227     /* TODO: think of some funky algorithm to detect scene changes. */
228     SnapshotRatio( p_filter, p_pic );
229     return p_pic;
230 }
231
232 static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic )
233 {
234     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
235
236     if( !p_sys || !p_pic ) return;
237
238     if( p_sys->i_frames % p_sys->i_ratio != 0 )
239     {
240         p_sys->i_frames++;
241         return;
242     }
243     p_sys->i_frames++;
244
245     if( p_sys->p_scene )
246     {
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 )
253         {
254             picture_Copy( p_sys->p_scene->p_pic, p_pic );
255             SavePicture( p_filter, p_sys->p_scene->p_pic );
256         }
257     }
258 }
259
260 /*****************************************************************************
261  * Save Picture to disk
262  *****************************************************************************/
263 static void SavePicture( filter_t *p_filter, picture_t *p_pic )
264 {
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;
269     int i_ret;
270
271     memset( &fmt_in, 0, sizeof(video_format_t) );
272     memset( &fmt_out, 0, sizeof(video_format_t) );
273
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',' ');
281     else
282         fmt_out.i_chroma = VLC_FOURCC('j','p','e','g');
283
284     if( (fmt_out.i_width == 0) && (fmt_out.i_height > 0) )
285     {
286         fmt_out.i_width = (fmt_in.i_width * fmt_out.i_height) / fmt_in.i_height;
287     }
288     else if( (fmt_out.i_height == 0) && (fmt_out.i_width > 0) )
289     {
290         fmt_out.i_height = (fmt_in.i_height * fmt_out.i_width) / fmt_in.i_width;
291     }
292     else if( (fmt_out.i_width == 0) && (fmt_out.i_height == 0) )
293     {
294         fmt_out.i_width = fmt_in.i_width;
295         fmt_out.i_height = fmt_in.i_height;
296     }
297
298     /*
299      * Save the snapshot to a temporary file and
300      * switch it to the real name afterwards.
301      */
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,
305                           p_sys->psz_format );
306     else
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 );
310
311     if( i_ret == -1 )
312     {
313         msg_Err( p_filter, "could not create snapshot %s", psz_filename );
314         goto error;
315     }
316     path_sanitize( psz_filename );
317
318     i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
319     if( i_ret == -1 )
320     {
321         msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
322         goto error;
323     }
324     path_sanitize( psz_temp );
325
326     /* Save the image */
327     i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
328                             psz_temp );
329     if( i_ret != VLC_SUCCESS )
330     {
331         msg_Err( p_filter, "could not create snapshot %s", psz_temp );
332     }
333     else
334     {
335         /* switch to the final destination */
336         i_ret = rename( psz_temp, psz_filename );
337         if( i_ret == -1 )
338         {
339             msg_Err( p_filter, "could not rename snapshot %s %m", psz_filename );
340             goto error;
341         }
342     }
343
344 error:
345     free( psz_temp );
346     free( psz_filename );
347 }