clone.c 14.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * clone.c : Clone video plugin for vlc
 *****************************************************************************
4
 * Copyright (C) 2002, 2003 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * 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.
13
 *
14 15 16 17 18 19 20
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

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

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_vout.h>
35 36 37

#include "filter_common.h"

Gildas Bazin's avatar
 
Gildas Bazin committed
38 39
#define VOUTSEPARATOR ','

40 41 42 43 44 45 46 47 48 49 50 51
/*****************************************************************************
 * 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 * );
static void Render    ( vout_thread_t *, picture_t * );

static void RemoveAllVout  ( vout_thread_t *p_vout );

52 53 54
static int  SendEvents( vlc_object_t *, char const *,
                        vlc_value_t, vlc_value_t, void * );

55 56 57
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
58
#define COUNT_TEXT N_("Number of clones")
59
#define COUNT_LONGTEXT N_("Number of video windows in which to "\
60
    "clone the video.")
61

62 63 64
#define VOUTLIST_TEXT N_("Video output modules")
#define VOUTLIST_LONGTEXT N_("You can use specific video output modules " \
        "for the clones. Use a comma-separated list of modules." )
Gildas Bazin's avatar
 
Gildas Bazin committed
65

66 67
#define CFG_PREFIX "clone-"

68
vlc_module_begin();
69
    set_description( N_("Clone video filter") );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
70
    set_capability( "video filter", 0 );
71
    set_shortname( N_("Clone" ));
72 73 74
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );

75 76
    add_integer( CFG_PREFIX "count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT, false );
    add_string ( CFG_PREFIX "vout-list", NULL, NULL, VOUTLIST_TEXT, VOUTLIST_LONGTEXT, true );
Clément Stenac's avatar
 
Clément Stenac committed
77

78 79 80 81
    add_shortcut( "clone" );
    set_callbacks( Create, Destroy );
vlc_module_end();

82
static const char *const ppsz_filter_options[] = {
83 84 85
    "count", "vout-list", NULL
};

86 87 88 89 90 91 92 93 94
/*****************************************************************************
 * vout_sys_t: Clone video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the Clone specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    int    i_clones;
Gildas Bazin's avatar
 
Gildas Bazin committed
95 96 97 98 99 100

    /* list of vout modules to use. "default" will launch a default
     * module. If specified, overrides the setting in i_clones (which it
     * sets to the list length) */
    char **ppsz_vout_list;

101 102 103
    vout_thread_t **pp_vout;
};

104 105 106 107 108 109 110 111 112 113 114 115 116
/*****************************************************************************
 * Control: control facility for the vout (forwards to child vout)
 *****************************************************************************/
static int Control( vout_thread_t *p_vout, int i_query, va_list args )
{
    int i_vout;
    for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
    {
        vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ], i_query, args );
    }
    return VLC_SUCCESS;
}

117 118 119 120 121 122 123 124
/*****************************************************************************
 * Create: allocates Clone video thread output method
 *****************************************************************************
 * This function allocates and initializes a Clone vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
125
    char *psz_clonelist;
126 127 128 129

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
130
        return VLC_ENOMEM;
131 132 133 134 135 136

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = NULL;
    p_vout->pf_render = Render;
    p_vout->pf_display = NULL;
137
    p_vout->pf_control = Control;
138

139 140 141 142 143
    config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
                       p_vout->p_cfg );

    psz_clonelist = var_CreateGetNonEmptyString( p_vout,
                                                 CFG_PREFIX "vout-list" );
Gildas Bazin's avatar
 
Gildas Bazin committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    if( psz_clonelist )
    {
        int i_dummy;
        char *psz_token;

        /* Count the number of defined vout */
        p_vout->p_sys->i_clones = 1;
        i_dummy = 0;
        while( psz_clonelist[i_dummy] != 0 )
        {
            if( psz_clonelist[i_dummy] == VOUTSEPARATOR )
                p_vout->p_sys->i_clones++;
            i_dummy++;
        }

        p_vout->p_sys->ppsz_vout_list = malloc( p_vout->p_sys->i_clones
                                                * sizeof(char *) );
        if( !p_vout->p_sys->ppsz_vout_list )
        {
163
            free( psz_clonelist );
Gildas Bazin's avatar
 
Gildas Bazin committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            free( p_vout->p_sys );
            return VLC_ENOMEM;
        }

        /* Tokenize the list */
        i_dummy = 0;
        psz_token = psz_clonelist;
        while( psz_token && *psz_token )
        {
           char *psz_module;
           psz_module = psz_token;
           psz_token = strchr( psz_module, VOUTSEPARATOR );
           if( psz_token )
           {
               *psz_token = '\0';
               psz_token++;
           }
           p_vout->p_sys->ppsz_vout_list[i_dummy] = strdup( psz_module );
           i_dummy++;
        }

        free( psz_clonelist );
    }
    else
    {
        /* No list was specified. We will use the default vout, and get
         * the number of clones from clone-count */
191 192
        p_vout->p_sys->i_clones =
            var_CreateGetInteger( p_vout, CFG_PREFIX "count" );
Gildas Bazin's avatar
 
Gildas Bazin committed
193 194
        p_vout->p_sys->ppsz_vout_list = NULL;
    }
195 196 197 198 199 200 201 202 203 204

    p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );

    msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );

    p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
                                     sizeof(vout_thread_t *) );
    if( p_vout->p_sys->pp_vout == NULL )
    {
        free( p_vout->p_sys );
205
        return VLC_ENOMEM;
206 207
    }

208
    return VLC_SUCCESS;
209 210 211 212 213 214 215 216 217
}

/*****************************************************************************
 * Init: initialize Clone video thread output method
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int   i_index, i_vout;
    picture_t *p_pic;
Gildas Bazin's avatar
 
Gildas Bazin committed
218
    char *psz_default_vout;
219
    video_format_t fmt;
220

221
    I_OUTPUTPICTURES = 0;
222
    memset( &fmt, 0, sizeof(video_format_t) );
223 224 225 226 227 228

    /* Initialize the output structure */
    p_vout->output.i_chroma = p_vout->render.i_chroma;
    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->render.i_aspect;
229 230
    p_vout->fmt_out = p_vout->fmt_in;
    fmt = p_vout->fmt_out;
231

232 233 234
    /* Try to open the real video output */
    msg_Dbg( p_vout, "spawning the real video outputs" );

Gildas Bazin's avatar
 
Gildas Bazin committed
235 236 237
    /* Save the default vout */
    psz_default_vout = config_GetPsz( p_vout, "vout" );

238 239
    for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
240
        if( p_vout->p_sys->ppsz_vout_list == NULL
Gildas Bazin's avatar
 
Gildas Bazin committed
241 242 243 244
            || ( !strncmp( p_vout->p_sys->ppsz_vout_list[i_vout],
                           "default", 8 ) ) )
        {
            p_vout->p_sys->pp_vout[i_vout] =
245
                vout_Create( p_vout, &fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
246 247 248 249 250 251 252
        }
        else
        {
            /* create the appropriate vout instead of the default one */
            config_PutPsz( p_vout, "vout",
                           p_vout->p_sys->ppsz_vout_list[i_vout] );
            p_vout->p_sys->pp_vout[i_vout] =
253
                vout_Create( p_vout, &fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
254 255 256 257 258

            /* Reset the default value */
            config_PutPsz( p_vout, "vout", psz_default_vout );
        }

259 260 261
        if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
        {
            msg_Err( p_vout, "failed to clone %i vout threads",
Gildas Bazin's avatar
 
Gildas Bazin committed
262
                     p_vout->p_sys->i_clones );
263
            p_vout->p_sys->i_clones = i_vout;
264
            free( psz_default_vout );
265
            RemoveAllVout( p_vout );
266
            return VLC_EGENERIC;
267
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
268

269
        ADD_CALLBACKS( p_vout->p_sys->pp_vout[ i_vout ], SendEvents );
270 271
    }

272
    free( psz_default_vout );
273 274
    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );

Gildas Bazin's avatar
 
Gildas Bazin committed
275 276
    ADD_PARENT_CALLBACKS( SendEventsToChild );

277
    return VLC_SUCCESS;
278 279 280 281 282 283 284 285 286
}

/*****************************************************************************
 * End: terminate Clone video thread output method
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
    int i_index;

287 288
    DEL_PARENT_CALLBACKS( SendEventsToChild );

289 290 291 292 293 294
    /* Free the fake output buffers we allocated */
    for( i_index = I_OUTPUTPICTURES ; i_index ; )
    {
        i_index--;
        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
    }
295 296

    RemoveAllVout( p_vout );
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}

/*****************************************************************************
 * Destroy: destroy Clone video thread output method
 *****************************************************************************
 * Terminate an output method created by CloneCreateOutputMethod
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    free( p_vout->p_sys->pp_vout );
    free( p_vout->p_sys );
}

/*****************************************************************************
 * Render: displays previously rendered output
 *****************************************************************************
 * This function send the currently rendered image to Clone image, waits
 * until it is displayed and switch the two rendering buffers, preparing next
 * frame.
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    picture_t *p_outpic = NULL;
    int i_vout, i_plane;

    for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
    {
        while( ( p_outpic =
            vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
               ) == NULL )
        {
330
            if( !vlc_object_alive (p_vout) || p_vout->b_error )
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            {
                vout_DestroyPicture(
                    p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
                return;
            }

            msleep( VOUT_OUTMEM_SLEEP );
        }

        vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
                          p_outpic, p_pic->date );
        vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );

        for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
        {
346
            uint8_t *p_in, *p_in_end, *p_out;
347 348
            int i_in_pitch = p_pic->p[i_plane].i_pitch;
            const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
349
            const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
350 351 352 353

            p_in = p_pic->p[i_plane].p_pixels;
            p_out = p_outpic->p[i_plane].p_pixels;

354 355 356
            if( i_in_pitch == i_copy_pitch
                 && i_out_pitch == i_copy_pitch )
            {
357
                vlc_memcpy( p_out, p_in, i_in_pitch
358
                                     * p_outpic->p[i_plane].i_visible_lines );
359 360
            }
            else
361
            {
362 363
                p_in_end = p_in + i_in_pitch *
                    p_outpic->p[i_plane].i_visible_lines;
364 365 366

                while( p_in < p_in_end )
                {
367
                    vlc_memcpy( p_out, p_in, i_copy_pitch );
368 369 370
                    p_in += i_in_pitch;
                    p_out += i_out_pitch;
                }
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
            }
        }

        vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
        vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
    }
}

/*****************************************************************************
 * RemoveAllVout: destroy all the child video output threads
 *****************************************************************************/
static void RemoveAllVout( vout_thread_t *p_vout )
{
    while( p_vout->p_sys->i_clones )
    {
         --p_vout->p_sys->i_clones;
387 388
         DEL_CALLBACKS( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones],
                        SendEvents );
389
         vout_CloseAndRelease( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
390 391 392
    }
}

393 394 395 396 397 398
/*****************************************************************************
 * SendEvents: forward mouse and keyboard events to the parent p_vout
 *****************************************************************************/
static int SendEvents( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
399
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
400 401 402 403
    var_Set( (vlc_object_t *)p_data, psz_var, newval );

    return VLC_SUCCESS;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
404 405 406 407 408 409 410

/*****************************************************************************
 * SendEventsToChild: forward events to the child/children vout
 *****************************************************************************/
static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
411
    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
Gildas Bazin's avatar
 
Gildas Bazin committed
412 413 414 415 416 417 418 419 420 421 422 423
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    int i_vout;

    for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
    {
        var_Set( p_vout->p_sys->pp_vout[ i_vout ], psz_var, newval );

        if( !strcmp( psz_var, "fullscreen" ) ) break;
    }

    return VLC_SUCCESS;
}