scene.c 11.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * scene.c : scene video filter (based on modules/video_output/image.c)
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2004-2008 VLC authors and VideoLAN
5 6 7 8 9
 * $Id$
 *
 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <limits.h>
34
#include <errno.h>
35

36 37
#include <vlc_common.h>
#include <vlc_plugin.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
38
#include <vlc_filter.h>
39
#include <vlc_picture.h>
40
#include "filter_picture.h"
Rémi Duraffort's avatar
Rémi Duraffort committed
41 42
#include <vlc_image.h>
#include <vlc_strings.h>
43
#include <vlc_fs.h>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create      ( vlc_object_t * );
static void Destroy     ( vlc_object_t * );

static picture_t *Filter( filter_t *, picture_t * );

static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic );
static void SavePicture( filter_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FORMAT_TEXT N_( "Image format" )
60
#define FORMAT_LONGTEXT N_( "Format of the output images (png, jpeg, ...)." )
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

#define WIDTH_TEXT N_( "Image width" )
#define WIDTH_LONGTEXT N_( "You can enforce the image width. By default " \
                            "(-1) VLC will adapt to the video " \
                            "characteristics.")

#define HEIGHT_TEXT N_( "Image height" )
#define HEIGHT_LONGTEXT N_( "You can enforce the image height. By default " \
                            "(-1) VLC will adapt to the video " \
                            "characteristics.")

#define RATIO_TEXT N_( "Recording ratio" )
#define RATIO_LONGTEXT N_( "Ratio of images to record. "\
                           "3 means that one image out of three is recorded." )

#define PREFIX_TEXT N_( "Filename prefix" )
#define PREFIX_LONGTEXT N_( "Prefix of the output images filenames. Output " \
                            "filenames will have the \"prefixNUMBER.format\" "\
                            "form if replace is not true." )

#define PATH_TEXT N_( "Directory path prefix" )
Christoph Miebach's avatar
Christoph Miebach committed
82
#define PATH_LONGTEXT N_( "Directory path where images files should be saved. " \
83 84 85 86 87 88 89 90
                          "If not set, then images will be automatically saved in " \
                          "users homedir." )

#define REPLACE_TEXT N_( "Always write to the same file" )
#define REPLACE_LONGTEXT N_( "Always write to the same file instead of " \
                            "creating one file per image. In this case, " \
                             "the number is not appended to the filename." )

91
#define SCENE_HELP N_("Send your video to picture files")
92 93
#define CFG_PREFIX "scene-"

94 95 96
vlc_module_begin ()
    set_shortname( N_( "Scene filter" ) )
    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 filter", 0 )
101 102

    /* General options */
103
    add_string(  CFG_PREFIX "format", "png",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104
                 FORMAT_TEXT, FORMAT_LONGTEXT, false )
105
    add_integer( CFG_PREFIX "width", -1,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
106
                 WIDTH_TEXT, WIDTH_LONGTEXT, true )
107
    add_integer( CFG_PREFIX "height", -1,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
108
                 HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
109
    add_string(  CFG_PREFIX "prefix", "scene",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
110
                 PREFIX_TEXT, PREFIX_LONGTEXT, false )
111
    add_string(  CFG_PREFIX "path", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
112
                 PATH_TEXT, PATH_LONGTEXT, false )
113
    add_bool(    CFG_PREFIX "replace", false,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
114
                 REPLACE_TEXT, REPLACE_LONGTEXT, false )
115 116

    /* Snapshot method */
117 118
    add_integer_with_range( CFG_PREFIX "ratio", 50, 1, INT_MAX,
                            RATIO_TEXT, RATIO_LONGTEXT, false )
119

120 121
    set_callbacks( Create, Destroy )
vlc_module_end ()
122 123 124 125 126 127

static const char *const ppsz_vfilter_options[] = {
    "format", "width", "height", "ratio", "prefix", "path", "replace", NULL
};

typedef struct scene_t {
128 129
    picture_t       *p_pic;
    video_format_t  format;
130 131 132 133 134
} scene_t;

/*****************************************************************************
 * filter_sys_t: private data
 *****************************************************************************/
135
typedef struct
136 137
{
    image_handler_t *p_image;
138
    scene_t scene;
139 140 141 142

    char *psz_path;
    char *psz_prefix;
    char *psz_format;
143
    vlc_fourcc_t i_format;
144 145 146 147 148
    int32_t i_width;
    int32_t i_height;
    int32_t i_ratio;  /* save every n-th frame */
    int32_t i_frames; /* frames count */
    bool  b_replace;
149
} filter_sys_t;
150 151 152 153 154 155 156

/*****************************************************************************
 * Create: initialize and set pf_video_filter()
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
157
    filter_sys_t *p_sys;
158

159 160 161 162 163
    const vlc_chroma_description_t *p_chroma =
        vlc_fourcc_GetChromaDescription( p_filter->fmt_in.video.i_chroma );
    if( p_chroma == NULL || p_chroma->plane_count == 0 )
        return VLC_EGENERIC;

164 165 166
    config_ChainParse( p_filter, CFG_PREFIX, ppsz_vfilter_options,
                       p_filter->p_cfg );

167
    p_filter->p_sys = p_sys = calloc( 1, sizeof( filter_sys_t ) );
168 169 170 171 172 173 174 175 176 177 178
    if( p_filter->p_sys == NULL )
        return VLC_ENOMEM;

    p_sys->p_image = image_HandlerCreate( p_this );
    if( !p_sys->p_image )
    {
        msg_Err( p_this, "Couldn't get handle to image conversion routines." );
        free( p_sys );
        return VLC_EGENERIC;
    }

179 180 181 182 183 184 185 186 187 188 189 190 191 192
    p_sys->psz_format = var_CreateGetString( p_this, CFG_PREFIX "format" );
    p_sys->i_format = image_Type2Fourcc( p_sys->psz_format );
    if( !p_sys->i_format )
    {
        msg_Err( p_filter, "Could not find FOURCC for image type '%s'",
                 p_sys->psz_format );
        image_HandlerDelete( p_sys->p_image );
        free( p_sys->psz_format );
        free( p_sys );
        return VLC_EGENERIC;
    }
    p_sys->i_width = var_CreateGetInteger( p_this, CFG_PREFIX "width" );
    p_sys->i_height = var_CreateGetInteger( p_this, CFG_PREFIX "height" );
    p_sys->i_ratio = var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
193 194
    if( p_sys->i_ratio <= 0)
        p_sys->i_ratio = 1;
195
    p_sys->b_replace = var_CreateGetBool( p_this, CFG_PREFIX "replace" );
196 197 198
    p_sys->psz_prefix = var_CreateGetString( p_this, CFG_PREFIX "prefix" );
    p_sys->psz_path = var_GetNonEmptyString( p_this, CFG_PREFIX "path" );
    if( p_sys->psz_path == NULL )
199
        p_sys->psz_path = config_GetUserDir( VLC_PICTURES_DIR );
200 201 202 203 204 205 206 207 208 209 210 211

    p_filter->pf_video_filter = Filter;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Destroy: destroy video filter method
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
212
    filter_sys_t *p_sys = p_filter->p_sys;
213 214 215

    image_HandlerDelete( p_sys->p_image );

216 217
    if( p_sys->scene.p_pic )
        picture_Release( p_sys->scene.p_pic );
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    free( p_sys->psz_format );
    free( p_sys->psz_prefix );
    free( p_sys->psz_path );
    free( p_sys );
}

/*****************************************************************************
 * Filter: Apply filtering logic to picture.
 *****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    /* TODO: think of some funky algorithm to detect scene changes. */
    SnapshotRatio( p_filter, p_pic );
    return p_pic;
}

static void SnapshotRatio( filter_t *p_filter, picture_t *p_pic )
{
    filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;

238
    if( !p_pic ) return;
239 240 241 242 243 244 245 246

    if( p_sys->i_frames % p_sys->i_ratio != 0 )
    {
        p_sys->i_frames++;
        return;
    }
    p_sys->i_frames++;

247 248 249 250
    if( p_sys->scene.p_pic )
        picture_Release( p_sys->scene.p_pic );

    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 257 258 259 260 261
        p_sys->i_height = (p_pic->format.i_height * p_sys->i_width) / p_pic->format.i_width;
    }
    else if( (p_sys->i_width <= 0) && (p_sys->i_height <= 0) )
    {
        p_sys->i_width = p_pic->format.i_width;
        p_sys->i_height = p_pic->format.i_height;
262
    }
263

264 265 266 267 268 269
    p_sys->scene.p_pic = picture_NewFromFormat( &p_pic->format );
    if( p_sys->scene.p_pic )
    {
        picture_Copy( p_sys->scene.p_pic, p_pic );
        SavePicture( p_filter, p_sys->scene.p_pic );
    }
270 271 272 273 274 275 276 277 278 279
}

/*****************************************************************************
 * Save Picture to disk
 *****************************************************************************/
static void SavePicture( filter_t *p_filter, picture_t *p_pic )
{
    filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
    video_format_t fmt_in, fmt_out;
    char *psz_filename = NULL;
280
    char *psz_temp = NULL;
281 282 283 284 285 286 287 288 289
    int i_ret;

    memset( &fmt_out, 0, sizeof(video_format_t) );

    /* Save snapshot psz_format to a memory zone */
    fmt_in = p_pic->format;
    fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
    fmt_out.i_width = p_sys->i_width;
    fmt_out.i_height = p_sys->i_height;
290
    fmt_out.i_chroma = p_sys->i_format;
291

292 293 294 295
    /*
     * Save the snapshot to a temporary file and
     * switch it to the real name afterwards.
     */
296 297 298 299 300 301 302 303 304 305 306 307
    if( p_sys->b_replace )
        i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s.%s",
                          p_sys->psz_path, p_sys->psz_prefix,
                          p_sys->psz_format );
    else
        i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
                          p_sys->psz_path, p_sys->psz_prefix,
                          p_sys->i_frames, p_sys->psz_format );

    if( i_ret == -1 )
    {
        msg_Err( p_filter, "could not create snapshot %s", psz_filename );
308
        goto error;
309 310
    }

311 312 313 314 315 316 317
    i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
    if( i_ret == -1 )
    {
        msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
        goto error;
    }

318 319
    /* Save the image */
    i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
320
                            psz_temp );
321 322
    if( i_ret != VLC_SUCCESS )
    {
323
        msg_Err( p_filter, "could not create snapshot %s", psz_temp );
324
    }
325 326 327
    else
    {
        /* switch to the final destination */
328
#if defined (_WIN32) || defined(__OS2__)
329 330
        vlc_unlink( psz_filename );
#endif
331
        i_ret = vlc_rename( psz_temp, psz_filename );
332 333
        if( i_ret == -1 )
        {
334 335
            msg_Err( p_filter, "could not rename snapshot %s: %s",
                     psz_filename, vlc_strerror_c(errno) );
336 337 338 339 340 341
            goto error;
        }
    }

error:
    free( psz_temp );
342 343
    free( psz_filename );
}