image.c 12 KB
Newer Older
1
2
3
/*****************************************************************************
 * image.c : image video output
 *****************************************************************************
4
 * Copyright (C) 2004-2006 the VideoLAN team
dionoea's avatar
dionoea committed
5
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
30
31
#include <vlc_vout.h>
#include <vlc_interface.h>
32
33

#include "vlc_image.h"
34
#include "vlc_strings.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t *p_vout );
static void Display   ( vout_thread_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FORMAT_TEXT N_( "Image format" )
50
#define FORMAT_LONGTEXT N_( "Format of the output images (png or jpg)." )
51

52
53
54
55
56
57
58
59
60
61
#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.")

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

#define PREFIX_TEXT N_( "Filename prefix" )
67
68
69
#define PREFIX_LONGTEXT N_( "Prefix of the output images filenames. Output " \
                            "filenames will have the \"prefixNUMBER.format\" "\
                            "form." )
70

71
#define REPLACE_TEXT N_( "Always write to the same file" )
72
73
74
#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." )
75

hartman's avatar
hartman committed
76
77
static const char *psz_format_list[] = { "png", "jpeg" };
static const char *psz_format_list_text[] = { "PNG", "JPEG" };
78

79
80
#define CFG_PREFIX "image-out-"

81
vlc_module_begin( );
zorglub's avatar
zorglub committed
82
    set_shortname( _( "Image file" ) );
83
84
85
86
87
    set_description( _( "Image video output" ) );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VOUT );
    set_capability( "video output", 0 );

88
89
    add_string(  CFG_PREFIX "format", "png", NULL,
                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
90
    change_string_list( psz_format_list, psz_format_list_text, 0 );
91
92
93
94
95
96
97
98
99
100
101
102
    add_integer( CFG_PREFIX "width", -1, NULL,
                 WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
        add_deprecated( "image-width", VLC_FALSE ); /* since 0.9.0 */
    add_integer( CFG_PREFIX "height", -1, NULL,
                 HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_TRUE );
        add_deprecated( "image-height", VLC_FALSE ); /* since 0.9.0 */
    add_integer( CFG_PREFIX "ratio", 3, NULL,
                 RATIO_TEXT, RATIO_LONGTEXT, VLC_FALSE );
    add_string(  CFG_PREFIX "prefix", "img", NULL,
                 PREFIX_TEXT, PREFIX_LONGTEXT, VLC_FALSE );
    add_bool(    CFG_PREFIX "replace", 0, NULL,
                 REPLACE_TEXT, REPLACE_LONGTEXT, VLC_FALSE );
103
104
105
    set_callbacks( Create, Destroy );
vlc_module_end();

106
107
108
109
static const char *ppsz_vout_options[] = {
    "format", "width", "height", "ratio", "prefix", "replace", NULL
};

110
111
112
113
114
/*****************************************************************************
 * vout_sys_t: video output descriptor
 *****************************************************************************/
struct vout_sys_t
{
115
116
117
    char        *psz_prefix;          /* Prefix */
    char        *psz_format;          /* Format */
    int         i_ratio;         /* Image ratio */
118

119
120
121
    int         i_width;         /* Image width */
    int         i_height;       /* Image height */

122
123
    int         i_current;     /* Current image */
    int         i_frames;   /* Number of frames */
124

125
126
    vlc_bool_t  b_replace;

127
128
129
    vlc_bool_t b_time;
    vlc_bool_t b_meta;

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    image_handler_t *p_image;
};

/*****************************************************************************
 * Create: allocates video thread
 *****************************************************************************
 * This function allocates and initializes a vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = ( vout_thread_t * )p_this;

    /* Allocate instance and initialize some members */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( ! p_vout->p_sys )
        return VLC_ENOMEM;

147
148
149
    config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
                       p_vout->p_cfg );

150
    p_vout->p_sys->psz_prefix =
151
            var_CreateGetString( p_this, CFG_PREFIX "prefix" );
152
153
154
155
    p_vout->p_sys->b_time = strchr( p_vout->p_sys->psz_prefix, '%' )
                            ? VLC_TRUE : VLC_FALSE;
    p_vout->p_sys->b_meta = strchr( p_vout->p_sys->psz_prefix, '$' )
                            ? VLC_TRUE : VLC_FALSE;
156
    p_vout->p_sys->psz_format =
157
            var_CreateGetString( p_this, CFG_PREFIX "format" );
158
    p_vout->p_sys->i_width =
159
            var_CreateGetInteger( p_this, CFG_PREFIX "width" );
160
    p_vout->p_sys->i_height =
161
            var_CreateGetInteger( p_this, CFG_PREFIX "height" );
162
    p_vout->p_sys->i_ratio =
163
            var_CreateGetInteger( p_this, CFG_PREFIX "ratio" );
164
    p_vout->p_sys->b_replace =
165
            var_CreateGetBool( p_this, CFG_PREFIX "replace" );
166
167
168
169
170
171
    p_vout->p_sys->i_current = 0;
    p_vout->p_sys->p_image = image_HandlerCreate( p_vout );

    if( !p_vout->p_sys->p_image )
    {
        msg_Err( p_this, "unable to create image handler") ;
zorglub's avatar
zorglub committed
172
173
        FREENULL( p_vout->p_sys->psz_prefix );
        FREENULL( p_vout->p_sys );
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        return VLC_EGENERIC;
    }

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = NULL;
    p_vout->pf_render = Display;
    p_vout->pf_display = NULL;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Init: initialize video thread
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;

    /* Initialize the output structure */
195
    p_vout->output.i_chroma = p_vout->render.i_chroma;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    p_vout->output.pf_setpalette = NULL;
    p_vout->output.i_width = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->output.i_width
                               * VOUT_ASPECT_FACTOR / p_vout->output.i_height;

    p_vout->output.i_rmask = 0xff0000;
    p_vout->output.i_gmask = 0x00ff00;
    p_vout->output.i_bmask = 0x0000ff;

    /* Try to initialize 1 direct buffer */
    p_pic = NULL;

    /* Find an empty picture slot */
    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
    {
        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
        {
            p_pic = p_vout->p_picture + i_index;
            break;
        }
    }

    /* Allocate the picture */
    if( p_pic == NULL )
    {
        return VLC_EGENERIC;
    }

    vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
                          p_vout->output.i_width, p_vout->output.i_height,
                          p_vout->output.i_aspect );

    if( p_pic->i_planes == 0 )
    {
        return VLC_EGENERIC;
    }

    p_pic->i_status = DESTROYED_PICTURE;
    p_pic->i_type   = DIRECT_PICTURE;

    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
    I_OUTPUTPICTURES++;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Destroy: destroy video thread
 *****************************************************************************
 * Terminate an output method created by Create
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
249
    int i_index;
250
251
    vout_thread_t *p_vout = ( vout_thread_t * )p_this;

252
253
254
255
256
    for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
    {
        free( PP_OUTPUTPICTURE[ i_index ]->p_data );
    }

257
258
    /* Destroy structure */
    image_HandlerDelete( p_vout->p_sys->p_image );
zorglub's avatar
zorglub committed
259
260
261
    FREENULL( p_vout->p_sys->psz_prefix );
    FREENULL( p_vout->p_sys->psz_format );
    FREENULL( p_vout->p_sys );
262
263
264
265
266
267
268
269
270
}

/*****************************************************************************
 * Display: displays previously rendered output
 *****************************************************************************
 * This function copies the rendered picture into our circular buffer.
 *****************************************************************************/
static void Display( vout_thread_t *p_vout, picture_t *p_pic )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
271
    video_format_t fmt_in, fmt_out;
272
273

    char *psz_filename;
274
275
    char *psz_prefix;
    char *psz_tmp;
276

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
278
279
    memset (&fmt_in, 0, sizeof (fmt_in));
    memset (&fmt_out, 0, sizeof (fmt_out));

280
281
282
283
284
285
286
    if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
    {
        p_vout->p_sys->i_frames++;
        return;
    }
    p_vout->p_sys->i_frames++;

287
    fmt_in.i_chroma = p_vout->render.i_chroma;
288
289
290
291
292
293
294
    fmt_in.i_width = p_vout->render.i_width;
    fmt_in.i_height = p_vout->render.i_height;

    fmt_out.i_width = p_vout->p_sys->i_width > 0 ? p_vout->p_sys->i_width :
                                                   p_vout->render.i_width;
    fmt_out.i_height = p_vout->p_sys->i_height > 0 ? p_vout->p_sys->i_height :
                                                     p_vout->render.i_height;
295

296
    if( p_vout->p_sys->b_time )
297
    {
298
        psz_tmp = str_format_time( p_vout->p_sys->psz_prefix );
299
300
        path_sanitize( psz_tmp );
    }
301
302
303
304
305
    else
        psz_tmp = p_vout->p_sys->psz_prefix;
    if( p_vout->p_sys->b_meta )
    {
        psz_prefix = str_format_meta( p_vout, psz_tmp );
306
        path_sanitize( psz_prefix );
307
308
309
310
311
312
313
        if( p_vout->p_sys->b_time )
            free( psz_tmp );
    }
    else
        psz_prefix = psz_tmp;
    psz_filename = (char *)malloc( 10 + strlen( psz_prefix )
                                      + strlen( p_vout->p_sys->psz_format ) );
314
315
    if( p_vout->p_sys->b_replace )
    {
316
317
        sprintf( psz_filename, "%s.%s", psz_prefix,
                                        p_vout->p_sys->psz_format );
318
319
320
    }
    else
    {
321
        sprintf( psz_filename, "%s%.6i.%s", psz_prefix,
322
323
324
                                            p_vout->p_sys->i_current,
                                            p_vout->p_sys->psz_format );
    }
325
326
    if( p_vout->p_sys->b_time || p_vout->p_sys->b_meta )
        free( psz_prefix );
327
328
329
    image_WriteUrl( p_vout->p_sys->p_image, p_pic,
                    &fmt_in, &fmt_out, psz_filename ) ;
    free( psz_filename );
330
331
332

    p_vout->p_sys->i_current++;

333
334
335
336
337
338
339
    return;
}


static void End( vout_thread_t *p_vout )
{
}