video_output.c 51.9 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * video_output.c : video output thread
3
 *
Michel Kaempf's avatar
Michel Kaempf committed
4 5
 * This module describes the programming interface for video output threads.
 * It includes functions allowing to open a new thread, send pictures to a
6 7
 * thread, and destroy a previously oppened video output thread.
 *****************************************************************************
8
 * Copyright (C) 2000-2007 the VideoLAN team
Gildas Bazin's avatar
Gildas Bazin committed
9
 * $Id$
10
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
11
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12
 *          Gildas Bazin <gbazin@videolan.org>
13
 *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
14 15 16 17 18
 *
 * 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.
Sam Hocevar's avatar
Sam Hocevar committed
19
 *
20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
24
 *
25 26
 * 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
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
29

30
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
31
 * Preamble
32
 *****************************************************************************/
33 34 35 36
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

37
#include <vlc_common.h>
38

39
#include <stdlib.h>                                                /* free() */
40
#include <string.h>
41
#include <assert.h>
Vincent Seguin's avatar
Vincent Seguin committed
42

Clément Stenac's avatar
Clément Stenac committed
43
#include <vlc_vout.h>
44

Clément Stenac's avatar
Clément Stenac committed
45 46 47
#include <vlc_filter.h>
#include <vlc_osd.h>

48 49
#include <libvlc.h>
#include <vlc_input.h>
50
#include "vout_pictures.h"
Laurent Aimar's avatar
Laurent Aimar committed
51
#include "vout_internal.h"
52

53
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
54
 * Local prototypes
55
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
56 57
static void     *Thread(void *);
static void     vout_Destructor(vlc_object_t *);
58

Gildas Bazin's avatar
 
Gildas Bazin committed
59
/* Object variables callbacks */
60 61
static int FilterCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
62 63
static int VideoFilter2Callback( vlc_object_t *, char const *,
                                 vlc_value_t, vlc_value_t, void * );
Laurent Aimar's avatar
Laurent Aimar committed
64 65

/* */
66 67 68 69 70
static void PostProcessEnable( vout_thread_t * );
static void PostProcessDisable( vout_thread_t * );
static void PostProcessSetFilterQuality( vout_thread_t *p_vout );
static int  PostProcessCallback( vlc_object_t *, char const *,
                                 vlc_value_t, vlc_value_t, void * );
Laurent Aimar's avatar
Laurent Aimar committed
71 72
/* */
static void DeinterlaceEnable( vout_thread_t * );
73
static void DeinterlaceNeeded( vout_thread_t *, bool );
Gildas Bazin's avatar
 
Gildas Bazin committed
74

75 76 77
/* Display media title in OSD */
static void DisplayTitleOnOSD( vout_thread_t *p_vout );

Laurent Aimar's avatar
Laurent Aimar committed
78 79 80 81 82 83 84
/* */
static void PrintVideoFormat(vout_thread_t *, const char *, const video_format_t *);

/* Maximum delay between 2 displayed pictures.
 * XXX it is needed for now but should be removed in the long term.
 */
#define VOUT_REDISPLAY_DELAY (INT64_C(80000))
85

Laurent Aimar's avatar
Laurent Aimar committed
86 87 88 89
/**
 * Late pictures having a delay higher than this value are thrashed.
 */
#define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
90 91

/* Better be in advance when awakening than late... */
Laurent Aimar's avatar
Laurent Aimar committed
92
#define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
93

94 95 96 97 98
/*****************************************************************************
 * Video Filter2 functions
 *****************************************************************************/
static picture_t *video_new_buffer_filter( filter_t *p_filter )
{
99
    vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
100
    return picture_pool_Get(p_vout->p->private_pool);
101 102 103 104
}

static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
{
Laurent Aimar's avatar
Laurent Aimar committed
105
    VLC_UNUSED(p_filter);
106
    picture_Release(p_pic);
107 108 109 110
}

static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
{
111 112
    p_filter->pf_video_buffer_new = video_new_buffer_filter;
    p_filter->pf_video_buffer_del = video_del_buffer_filter;
113 114
    p_filter->p_owner = p_data; /* p_vout */
    return VLC_SUCCESS;
115 116
}

117
#undef vout_Request
118
/*****************************************************************************
119 120 121 122 123
 * vout_Request: find a video output thread, create one, or destroy one.
 *****************************************************************************
 * This function looks for a video output thread matching the current
 * properties. If not found, it spawns a new one.
 *****************************************************************************/
124 125
vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
                             video_format_t *p_fmt )
126
{
127
    if( !p_fmt )
128
    {
129 130 131
        /* Video output is no longer used.
         * TODO: support for reusing video outputs with proper _thread-safe_
         * reference handling. */
132
        if( p_vout )
133
            vout_CloseAndRelease( p_vout );
134 135 136 137 138 139
        return NULL;
    }

    /* If a video output was provided, lock it, otherwise look for one. */
    if( p_vout )
    {
140
        vlc_object_hold( p_vout );
141 142
    }

143
    /* TODO: find a suitable unused video output */
144 145 146 147

    /* If we now have a video output, check it has the right properties */
    if( p_vout )
    {
148
        vlc_mutex_lock( &p_vout->p->change_lock );
149

150
        /* We don't directly check for the "vout-filter" variable for obvious
Gildas Bazin's avatar
 
Gildas Bazin committed
151
         * performance reasons. */
152
        if( p_vout->p->b_filter_change )
Gildas Bazin's avatar
 
Gildas Bazin committed
153
        {
154
            char *psz_filter_chain = var_GetString( p_vout, "vout-filter" );
Gildas Bazin's avatar
 
Gildas Bazin committed
155

156
            if( psz_filter_chain && !*psz_filter_chain )
Gildas Bazin's avatar
 
Gildas Bazin committed
157 158 159 160
            {
                free( psz_filter_chain );
                psz_filter_chain = NULL;
            }
161
            if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
Gildas Bazin's avatar
 
Gildas Bazin committed
162
            {
163 164
                free( p_vout->p->psz_filter_chain );
                p_vout->p->psz_filter_chain = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
165 166
            }

167
            if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
Gildas Bazin's avatar
 
Gildas Bazin committed
168
            {
169
                p_vout->p->b_filter_change = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
170 171
            }

172
            free( psz_filter_chain );
Gildas Bazin's avatar
 
Gildas Bazin committed
173 174
        }

175 176
#warning "FIXME: Check RGB masks in vout_Request"
        /* FIXME: check RGB masks */
177
        if( p_vout->fmt_render.i_chroma != vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma ) ||
178 179
            p_vout->fmt_render.i_width != p_fmt->i_width ||
            p_vout->fmt_render.i_height != p_fmt->i_height ||
180
            p_vout->p->b_filter_change )
181
        {
182
            vlc_mutex_unlock( &p_vout->p->change_lock );
183

184
            /* We are not interested in this format, close this vout */
185
            vout_CloseAndRelease( p_vout );
186
            vlc_object_release( p_vout );
187 188 189 190 191
            p_vout = NULL;
        }
        else
        {
            /* This video output is cool! Hijack it. */
Laurent Aimar's avatar
Laurent Aimar committed
192 193 194 195 196 197
            /* Correct aspect ratio on change
             * FIXME factorize this code with other aspect ration related code */
            unsigned int i_sar_num;
            unsigned int i_sar_den;
            vlc_ureduce( &i_sar_num, &i_sar_den,
                         p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
198
#if 0
Laurent Aimar's avatar
Laurent Aimar committed
199 200 201 202 203 204
            /* What's that, it does not seems to be used correcly everywhere */
            if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
            {
                i_sar_num *= p_vout->i_par_den;
                i_sar_den *= p_vout->i_par_num;
            }
205 206
#endif

Laurent Aimar's avatar
Laurent Aimar committed
207 208 209 210 211 212 213 214 215
            if( i_sar_num > 0 && i_sar_den > 0 &&
                ( i_sar_num != p_vout->fmt_render.i_sar_num ||
                  i_sar_den != p_vout->fmt_render.i_sar_den ) )
            {
                p_vout->fmt_in.i_sar_num = i_sar_num;
                p_vout->fmt_in.i_sar_den = i_sar_den;

                p_vout->fmt_render.i_sar_num = i_sar_num;
                p_vout->fmt_render.i_sar_den = i_sar_den;
216
                p_vout->p->i_changes |= VOUT_ASPECT_CHANGE;
217
            }
218
            vlc_mutex_unlock( &p_vout->p->change_lock );
219 220 221 222 223 224 225 226

            vlc_object_release( p_vout );
        }

        if( p_vout )
        {
            msg_Dbg( p_this, "reusing provided vout" );

227
            spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
228
            vlc_object_detach( p_vout );
229

230
            vlc_object_attach( p_vout, p_this );
231
            spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
232 233 234 235 236 237 238
        }
    }

    if( !p_vout )
    {
        msg_Dbg( p_this, "no usable vout present, spawning one" );

239
        p_vout = vout_Create( p_this, p_fmt );
240 241 242 243 244 245 246
    }

    return p_vout;
}

/*****************************************************************************
 * vout_Create: creates a new video output thread
247
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
248 249
 * This function creates a new video output thread, and returns a pointer
 * to its description. On error, it returns NULL.
250
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
251
vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
Michel Kaempf's avatar
Michel Kaempf committed
252
{
253 254
    vout_thread_t  *p_vout;                            /* thread descriptor */
    vlc_value_t     text;
Michel Kaempf's avatar
Michel Kaempf committed
255

256

257 258 259 260
    config_chain_t *p_cfg;
    char *psz_parser;
    char *psz_name;

261
    if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
262
        return NULL;
263
    const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
264 265 266 267 268 269

    vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
                 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
    if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
        return NULL;

270
    /* Allocate descriptor */
271 272 273
    static const char typename[] = "video output";
    p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
                                typename );
274
    if( p_vout == NULL )
275
        return NULL;
276

277
    /* */
Laurent Aimar's avatar
Laurent Aimar committed
278
    p_vout->p = calloc( 1, sizeof(*p_vout->p) );
279 280 281 282 283 284
    if( !p_vout->p )
    {
        vlc_object_release( p_vout );
        return NULL;
    }

285
    /* */
286 287
    p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
    p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
288

289 290
    p_vout->fmt_render.i_chroma = 
    p_vout->fmt_in.i_chroma     = i_chroma;
291 292 293
    video_format_FixRgb( &p_vout->fmt_render );
    video_format_FixRgb( &p_vout->fmt_in );

Gildas Bazin's avatar
 
Gildas Bazin committed
294
    /* Initialize misc stuff */
295 296
    p_vout->p->i_changes    = 0;
    p_vout->p->b_fullscreen = 0;
297
    vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
298
    vout_statistic_Init( &p_vout->p->statistic );
299 300 301
    p_vout->p->b_filter_change = 0;
    p_vout->p->i_par_num =
    p_vout->p->i_par_den = 1;
302
    p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
Laurent Aimar's avatar
Laurent Aimar committed
303
    p_vout->p->b_picture_empty = false;
Laurent Aimar's avatar
Laurent Aimar committed
304
    p_vout->p->displayed.date = VLC_TS_INVALID;
305
    p_vout->p->displayed.decoded = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
306
    p_vout->p->displayed.timestamp = VLC_TS_INVALID;
307 308
    p_vout->p->displayed.qtype = QTYPE_NONE;
    p_vout->p->displayed.is_interlaced = false;
Laurent Aimar's avatar
Laurent Aimar committed
309

310
    p_vout->p->step.is_requested = false;
Laurent Aimar's avatar
Laurent Aimar committed
311 312 313 314 315
    p_vout->p->step.last         = VLC_TS_INVALID;
    p_vout->p->step.timestamp    = VLC_TS_INVALID;

    p_vout->p->pause.is_on  = false;
    p_vout->p->pause.date   = VLC_TS_INVALID;
316 317 318

    p_vout->p->decoder_fifo = picture_fifo_New();
    p_vout->p->decoder_pool = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
319

320 321
    vlc_mouse_Init( &p_vout->p->mouse );

322
    vout_snapshot_Init( &p_vout->p->snapshot );
323

Gildas Bazin's avatar
 
Gildas Bazin committed
324
    /* Initialize locks */
325
    vlc_mutex_init( &p_vout->p->picture_lock );
326
    vlc_cond_init( &p_vout->p->picture_wait );
327
    vlc_mutex_init( &p_vout->p->change_lock );
328
    vlc_mutex_init( &p_vout->p->vfilter_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
329

330 331
    /* Mouse coordinates */
    var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
332 333
    var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
    var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
334 335
    /* Mouse object (area of interest in a video filter) */
    var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
336

Gildas Bazin's avatar
 
Gildas Bazin committed
337 338 339
    /* Attach the new object now so we can use var inheritance below */
    vlc_object_attach( p_vout, p_parent );

340
    /* Initialize subpicture unit */
341
    p_vout->p->p_spu = spu_Create( p_vout );
342

343
    /* */
344
    spu_Init( p_vout->p->p_spu );
345

346
    spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
347

348 349
    /* Take care of some "interface/control" related initialisations */
    vout_IntfInit( p_vout );
Gildas Bazin's avatar
 
Gildas Bazin committed
350

Gildas Bazin's avatar
 
Gildas Bazin committed
351 352
    /* If the parent is not a VOUT object, that means we are at the start of
     * the video output pipe */
353
    if( vlc_internals( p_parent )->i_object_type != VLC_OBJECT_VOUT )
Sam Hocevar's avatar
 
Sam Hocevar committed
354
    {
355
        /* Look for the default filter configuration */
356
        p_vout->p->psz_filter_chain =
357
            var_CreateGetStringCommand( p_vout, "vout-filter" );
358 359

        /* Apply video filter2 objects on the first vout */
360
        p_vout->p->psz_vf2 =
361
            var_CreateGetStringCommand( p_vout, "video-filter" );
362 363

        p_vout->p->b_first_vout = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
364 365 366 367
    }
    else
    {
        /* continue the parent's filter chain */
368
        char *psz_tmp;
Gildas Bazin's avatar
 
Gildas Bazin committed
369

370
        /* Ugly hack to jump to our configuration chain */
371 372 373 374
        p_vout->p->psz_filter_chain
            = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
        p_vout->p->psz_filter_chain
            = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
375 376
        config_ChainDestroy( p_cfg );
        free( psz_tmp );
377 378

        /* Create a video filter2 var ... but don't inherit values */
379 380
        var_Create( p_vout, "video-filter",
                    VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
381
        p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
382 383 384

        /* */
        p_vout->p->b_first_vout = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
385 386
    }

387
    var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
388
    p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
389
        false, video_filter_buffer_allocation_init, NULL, p_vout );
390

Gildas Bazin's avatar
 
Gildas Bazin committed
391
    /* Choose the video output module */
392
    if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
Gildas Bazin's avatar
 
Gildas Bazin committed
393
    {
394
        psz_parser = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
395 396 397
    }
    else
    {
398
        psz_parser = strdup( p_vout->p->psz_filter_chain );
Laurent Aimar's avatar
Laurent Aimar committed
399
        p_vout->p->title.show = false;
Sam Hocevar's avatar
 
Sam Hocevar committed
400 401
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
402
    /* Create the vout thread */
403
    char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
Antoine Cellerier's avatar
Antoine Cellerier committed
404
    free( psz_parser );
405
    free( psz_tmp );
406
    p_vout->p->p_cfg = p_cfg;
Laurent Aimar's avatar
Laurent Aimar committed
407 408 409 410 411 412 413 414 415 416

    /* Create a few object variables for interface interaction */
    var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    text.psz_string = _("Filters");
    var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
    var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );

    /* */
    DeinterlaceEnable( p_vout );

417
    if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
418 419 420 421 422 423 424
    {
        char *psz_tmp;
        if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
            psz_tmp = strdup( "" );
        free( psz_name );
        psz_name = psz_tmp;
    }
425
    p_vout->p->psz_module_name = psz_name;
Laurent Aimar's avatar
Laurent Aimar committed
426 427

    /* */
428
    vlc_object_set_destructor( p_vout, vout_Destructor );
Gildas Bazin's avatar
 
Gildas Bazin committed
429

Laurent Aimar's avatar
Laurent Aimar committed
430
    /* */
431
    vlc_cond_init( &p_vout->p->change_wait );
Laurent Aimar's avatar
Laurent Aimar committed
432
    if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
433
                   VLC_THREAD_PRIORITY_OUTPUT ) )
Michel Kaempf's avatar
Michel Kaempf committed
434
    {
435 436 437
        spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
        spu_Destroy( p_vout->p->p_spu );
        p_vout->p->p_spu = NULL;
438
        vlc_object_release( p_vout );
439 440 441
        return NULL;
    }

442
    vlc_mutex_lock( &p_vout->p->change_lock );
443 444 445 446
    while( !p_vout->p->b_ready )
    {   /* We are (ab)using the same condition in opposite directions for
         * b_ready and b_done. This works because of the strict ordering. */
        assert( !p_vout->p->b_done );
447
        vlc_cond_wait( &p_vout->p->change_wait, &p_vout->p->change_lock );
448
    }
449
    vlc_mutex_unlock( &p_vout->p->change_lock );
450

451
    if( p_vout->p->b_error )
452 453
    {
        msg_Err( p_vout, "video output creation failed" );
454
        vout_CloseAndRelease( p_vout );
455
        return NULL;
456
    }
Michel Kaempf's avatar
Michel Kaempf committed
457

458
    return p_vout;
Michel Kaempf's avatar
Michel Kaempf committed
459 460
}

461
/*****************************************************************************
462
 * vout_Close: Close a vout created by vout_Create.
463
 *****************************************************************************
464
 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
Rémi Denis-Courmont's avatar
Typo  
Rémi Denis-Courmont committed
465
 * You should NEVER call it on vout not obtained through vout_Create
466
 * (like with vout_Request or vlc_object_find.)
467
 * You can use vout_CloseAndRelease() as a convenience method.
468
 *****************************************************************************/
469
void vout_Close( vout_thread_t *p_vout )
470 471 472
{
    assert( p_vout );

473
    vlc_mutex_lock( &p_vout->p->change_lock );
474 475
    p_vout->p->b_done = true;
    vlc_cond_signal( &p_vout->p->change_wait );
476
    vlc_mutex_unlock( &p_vout->p->change_lock );
477

478
    vout_snapshot_End( &p_vout->p->snapshot );
479

480
    vlc_join( p_vout->p->thread, NULL );
481 482 483
}

/* */
484 485
static void vout_Destructor( vlc_object_t * p_this )
{
486
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
487

488
    /* Make sure the vout was stopped first */
489
    //assert( !p_vout->p_module );
490

491 492
    free( p_vout->p->psz_module_name );

Laurent Aimar's avatar
Laurent Aimar committed
493
    /* */
494 495
    if( p_vout->p->p_spu )
        spu_Destroy( p_vout->p->p_spu );
Laurent Aimar's avatar
Laurent Aimar committed
496

497 498 499 500 501 502
    vout_chrono_Clean( &p_vout->p->render );

    if( p_vout->p->decoder_fifo )
        picture_fifo_Delete( p_vout->p->decoder_fifo );
    assert( !p_vout->p->decoder_pool );

503
    /* Destroy the locks */
504
    vlc_cond_destroy( &p_vout->p->change_wait );
505
    vlc_cond_destroy( &p_vout->p->picture_wait );
506 507
    vlc_mutex_destroy( &p_vout->p->picture_lock );
    vlc_mutex_destroy( &p_vout->p->change_lock );
508
    vlc_mutex_destroy( &p_vout->p->vfilter_lock );
509

510 511 512
    /* */
    vout_statistic_Clean( &p_vout->p->statistic );

513
    /* */
514
    vout_snapshot_Clean( &p_vout->p->snapshot );
515 516

    /* */
517
    free( p_vout->p->psz_filter_chain );
Laurent Aimar's avatar
Laurent Aimar committed
518
    free( p_vout->p->title.value );
Gildas Bazin's avatar
 
Gildas Bazin committed
519

520
    config_ChainDestroy( p_vout->p->p_cfg );
521

522 523
    free( p_vout->p );

Michel Kaempf's avatar
Michel Kaempf committed
524 525
}

Laurent Aimar's avatar
Laurent Aimar committed
526 527 528
/* */
void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
{
529
    vlc_mutex_lock( &p_vout->p->change_lock );
Laurent Aimar's avatar
Laurent Aimar committed
530

Laurent Aimar's avatar
Laurent Aimar committed
531
    assert( !p_vout->p->pause.is_on || !b_paused );
Laurent Aimar's avatar
Laurent Aimar committed
532

533
    vlc_mutex_lock( &p_vout->p->picture_lock );
Laurent Aimar's avatar
Laurent Aimar committed
534

Laurent Aimar's avatar
Laurent Aimar committed
535
    if( p_vout->p->pause.is_on )
Laurent Aimar's avatar
Laurent Aimar committed
536
    {
Laurent Aimar's avatar
Laurent Aimar committed
537
        const mtime_t i_duration = i_date - p_vout->p->pause.date;
Laurent Aimar's avatar
Laurent Aimar committed
538

539 540 541 542 543 544 545
        if (p_vout->p->step.timestamp > VLC_TS_INVALID)
            p_vout->p->step.timestamp += i_duration;
        if (!b_paused)
            p_vout->p->step.last = p_vout->p->step.timestamp;
        picture_fifo_OffsetDate( p_vout->p->decoder_fifo, i_duration );
        if (p_vout->p->displayed.decoded)
            p_vout->p->displayed.decoded->date += i_duration;
Laurent Aimar's avatar
Laurent Aimar committed
546

547
        vlc_cond_signal( &p_vout->p->picture_wait );
548
        vlc_mutex_unlock( &p_vout->p->picture_lock );
549

550
        spu_OffsetSubtitleDate( p_vout->p->p_spu, i_duration );
Laurent Aimar's avatar
Laurent Aimar committed
551
    }
Laurent Aimar's avatar
Laurent Aimar committed
552 553
    else
    {
554 555
        if (b_paused)
            p_vout->p->step.last = p_vout->p->step.timestamp;
556
        vlc_mutex_unlock( &p_vout->p->picture_lock );
Laurent Aimar's avatar
Laurent Aimar committed
557
    }
Laurent Aimar's avatar
Laurent Aimar committed
558 559
    p_vout->p->pause.is_on = b_paused;
    p_vout->p->pause.date  = i_date;
Laurent Aimar's avatar
Laurent Aimar committed
560

561
    vlc_mutex_unlock( &p_vout->p->change_lock );
Laurent Aimar's avatar
Laurent Aimar committed
562
}
563

564 565
void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
{
566 567
    vout_statistic_GetReset( &p_vout->p->statistic,
                             pi_displayed, pi_lost );
568
}
569

570
static void Flush(vout_thread_t *vout, mtime_t date, bool below)
Laurent Aimar's avatar
Laurent Aimar committed
571
{
572 573 574 575 576 577 578 579 580
    vlc_assert_locked(&vout->p->picture_lock);
    vout->p->step.timestamp = VLC_TS_INVALID;
    vout->p->step.last      = VLC_TS_INVALID;

    picture_t *last = vout->p->displayed.decoded;
    if (last &&
        (( below  && last->date <= date) ||
          (!below && last->date >= date))) {
        vout->p->step.is_requested = true;
Laurent Aimar's avatar
Laurent Aimar committed
581
    }
582
    picture_fifo_Flush( vout->p->decoder_fifo, date, below );
Laurent Aimar's avatar
Laurent Aimar committed
583
}
584

585
void vout_Flush(vout_thread_t *vout, mtime_t date)
586
{
587
    vlc_mutex_lock(&vout->p->picture_lock);
588

589
    Flush(vout, date, false);
590

591 592 593
    vlc_cond_signal(&vout->p->picture_wait);
    vlc_mutex_unlock(&vout->p->picture_lock);
}
594

595
void vout_Reset(vout_thread_t *vout)
596 597
{
    vlc_mutex_lock(&vout->p->picture_lock);
598

599 600 601
    Flush(vout, INT64_MAX, true);
    if (vout->p->decoder_pool)
        picture_pool_NonEmpty(vout->p->decoder_pool, true);
602 603 604
    vout->p->pause.is_on = false;
    vout->p->pause.date  = mdate();

605 606 607
    vlc_cond_signal( &vout->p->picture_wait );
    vlc_mutex_unlock(&vout->p->picture_lock);
}
608

609
void vout_FixLeaks( vout_thread_t *vout )
610 611 612 613 614 615
{
    vlc_mutex_lock(&vout->p->picture_lock);

    picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
    if (!picture) {
        picture = picture_pool_Get(vout->p->decoder_pool);
616
    }
617 618 619 620 621 622

    if (picture) {
        picture_Release(picture);
        /* Not all pictures has been displayed yet or some are
         * free */
        vlc_mutex_unlock(&vout->p->picture_lock);
623 624 625
        return;
    }

626 627 628
    /* There is no reason that no pictures are available, force one
     * from the pool, becarefull with it though */
    msg_Err(vout, "pictures leaked, trying to workaround");
Laurent Aimar's avatar
Laurent Aimar committed
629

630 631
    /* */
    picture_pool_NonEmpty(vout->p->decoder_pool, false);
Laurent Aimar's avatar
Laurent Aimar committed
632

633 634
    vlc_cond_signal(&vout->p->picture_wait);
    vlc_mutex_unlock(&vout->p->picture_lock);
635
}
636
void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
Laurent Aimar's avatar
Laurent Aimar committed
637
{
638
    vlc_mutex_lock(&vout->p->picture_lock);
Laurent Aimar's avatar
Laurent Aimar committed
639

640 641
    vout->p->b_picture_empty = false;
    vout->p->step.is_requested = true;
Laurent Aimar's avatar
Laurent Aimar committed
642

643 644
    /* FIXME I highly doubt that it can works with only one cond_t FIXME */
    vlc_cond_signal(&vout->p->picture_wait);
Laurent Aimar's avatar
Laurent Aimar committed
645

646 647
    while (vout->p->step.is_requested && !vout->p->b_picture_empty)
        vlc_cond_wait(&vout->p->picture_wait, &vout->p->picture_lock);
Laurent Aimar's avatar
Laurent Aimar committed
648

649 650 651 652 653 654 655
    if (vout->p->step.last > VLC_TS_INVALID &&
        vout->p->step.timestamp > vout->p->step.last) {
        *duration = vout->p->step.timestamp - vout->p->step.last;
        vout->p->step.last = vout->p->step.timestamp;
    } else {
        *duration = 0;
    }
Laurent Aimar's avatar
Laurent Aimar committed
656 657

    /* TODO advance subpicture by the duration ... */
658
    vlc_mutex_unlock(&vout->p->picture_lock);
Laurent Aimar's avatar
Laurent Aimar committed
659
}
660

661 662 663 664
void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
{
    assert( psz_title );

665
    if( !var_InheritBool( p_vout, "osd" ) )
666 667
        return;

668
    vlc_mutex_lock( &p_vout->p->change_lock );
Laurent Aimar's avatar
Laurent Aimar committed
669 670
    free( p_vout->p->title.value );
    p_vout->p->title.value = strdup( psz_title );
671
    vlc_mutex_unlock( &p_vout->p->change_lock );
672
}
673

674 675
spu_t *vout_GetSpu( vout_thread_t *p_vout )
{
676
    return p_vout->p->p_spu;
677 678
}

679
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
680
 * InitThread: initialize video output thread
681
 *****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
682
 * This function is called from Thread and performs the second step of the
Sam Hocevar's avatar
 
Sam Hocevar committed
683 684
 * initialization. It returns 0 on success. Note that the thread's flag are not
 * modified inside this function.
685
 * XXX You have to enter it with change_lock taken.
686
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
687
static int ThreadInit(vout_thread_t *vout)
Vincent Seguin's avatar
Vincent Seguin committed
688
{
Sam Hocevar's avatar
 
Sam Hocevar committed
689
    /* Initialize output method, it allocates direct buffers for us */
Laurent Aimar's avatar
Laurent Aimar committed
690
    if (vout_InitWrapper(vout))
691
        return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
692
    assert(vout->p->decoder_pool);
Sam Hocevar's avatar
 
Sam Hocevar committed
693

Laurent Aimar's avatar
Laurent Aimar committed
694
    vout->p->displayed.decoded = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
695

basos g's avatar
basos g committed
696 697
    /* print some usefull debug info about different vout formats
     */
Laurent Aimar's avatar
Laurent Aimar committed
698 699 700
    PrintVideoFormat(vout, "pic render", &vout->fmt_render);
    PrintVideoFormat(vout, "pic in",     &vout->fmt_in);
    PrintVideoFormat(vout, "pic out",    &vout->fmt_out);
Laurent Aimar's avatar
Laurent Aimar committed
701

Laurent Aimar's avatar
Laurent Aimar committed
702 703 704
    assert(vout->fmt_out.i_width  == vout->fmt_render.i_width &&
           vout->fmt_out.i_height == vout->fmt_render.i_height &&
           vout->fmt_out.i_chroma == vout->fmt_render.i_chroma);
705
    return VLC_SUCCESS;
Vincent Seguin's avatar
Vincent Seguin committed
706 707
}

Laurent Aimar's avatar
Laurent Aimar committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
/*****************************************************************************
 * CleanThread: clean up after InitThread
 *****************************************************************************
 * This function is called after a sucessful
 * initialization. It frees all resources allocated by InitThread.
 * XXX You have to enter it with change_lock taken.
 *****************************************************************************/
static void ThreadClean(vout_thread_t *vout)
{
    /* Destroy translation tables */
    if (!vout->p->b_error) {
        picture_fifo_Flush(vout->p->decoder_fifo, INT64_MAX, false);
        vout_EndWrapper(vout);
    }
}

724 725
static int ThreadDisplayPicture(vout_thread_t *vout,
                                bool now, mtime_t *deadline)
Vincent Seguin's avatar
Vincent Seguin committed
726
{
727 728 729 730 731
    int displayed_count = 0;
    int lost_count = 0;

    for (;;) {
        const mtime_t date = mdate();
Laurent Aimar's avatar
Laurent Aimar committed
732
        const bool is_paused = vout->p->pause.is_on;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
        bool redisplay = is_paused && !now;
        bool is_forced;

        /* FIXME/XXX we must redisplay the last decoded picture (because
         * of potential vout updated, or filters update or SPU update)
         * For now a high update period is needed but it coulmd be removed
         * if and only if:
         * - vout module emits events from theselves.
         * - *and* SPU is modified to emit an event or a deadline when needed.
         *
         * So it will be done latter.
         */
        if (!redisplay) {
            picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
            if (peek) {
                is_forced = peek->b_force || is_paused || now;
                *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
                picture_Release(peek);
            } else {
                redisplay = true;
753 754
            }
        }
755 756
        if (redisplay) {
             /* FIXME a better way for this delay is needed */
Laurent Aimar's avatar
Laurent Aimar committed
757
            const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
758 759 760
            if (date_update > date || !vout->p->displayed.decoded) {
                *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
                break;
Laurent Aimar's avatar
Laurent Aimar committed
761
            }
762 763 764
            /* */
            is_forced = true;
            *deadline = date - vout_chrono_GetHigh(&vout->p->render);
Laurent Aimar's avatar
Laurent Aimar committed
765
        }
Laurent Aimar's avatar
Laurent Aimar committed
766 767
        if (*deadline > VOUT_MWAIT_TOLERANCE)
            *deadline -= VOUT_MWAIT_TOLERANCE;
Laurent Aimar's avatar
Laurent Aimar committed
768

Laurent Aimar's avatar
Laurent Aimar committed
769
        /* If we are too early and can wait, do it */
770 771
        if (date < *deadline && !now)
            break;
772

773 774 775 776 777 778 779 780 781 782 783 784
        picture_t *decoded;
        if (redisplay) {
            decoded = vout->p->displayed.decoded;
            vout->p->displayed.decoded = NULL;
        } else {
            decoded = picture_fifo_Pop(vout->p->decoder_fifo);
            assert(decoded);
            if (!is_forced && !vout->p->is_late_dropped) {
                const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
                const mtime_t late = predicted - decoded->date;
                if (late > 0) {
                    msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
Laurent Aimar's avatar
Laurent Aimar committed
785
                    if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
786 787 788 789 790
                        msg_Warn(vout, "rejected picture because of render time");
                        /* TODO */
                        picture_Release(decoded);
                        lost_count++;
                        break;
Laurent Aimar's avatar
Laurent Aimar committed
791 792 793
                    }
                }
            }
794 795 796

            vout->p->displayed.is_interlaced = !decoded->b_progressive;
            vout->p->displayed.qtype         = decoded->i_qtype;
797
        }
Laurent Aimar's avatar
Laurent Aimar committed
798
        vout->p->displayed.timestamp = decoded->date;
799 800

        /* */
801 802 803 804
        if (vout->p->displayed.decoded)
            picture_Release(vout->p->displayed.decoded);
        picture_Hold(decoded);
        vout->p->displayed.decoded = decoded;
805

806 807
        /* */
        vout_chrono_Start(&vout->p->render);
808

809 810 811 812 813 814 815 816
        picture_t *filtered = NULL;
        if (decoded) {
            vlc_mutex_lock(&vout->p->vfilter_lock);
            filtered = filter_chain_VideoFilter(vout->p->p_vf2_chain, decoded);
            //assert(filtered == decoded); // TODO implement
            vlc_mutex_unlock(&vout->p->vfilter_lock);
            if (!filtered)
                continue;
817
        }
818

Sam Hocevar's avatar
 
Sam Hocevar committed
819 820 821
        /*
         * Check for subpictures to display
         */
822 823
        const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
        mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
Laurent Aimar's avatar
Laurent Aimar committed
824 825
        if (vout->p->pause.is_on)
            spu_render_time = vout->p->pause.date;
826
        else
827
            spu_render_time = filtered->date > 1 ? filtered->date : mdate();
828

829 830 831
        subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
                                                   spu_render_time,
                                                   do_snapshot);
Sam Hocevar's avatar
 
Sam Hocevar committed
832 833
        /*
         * Perform rendering
834 835 836 837
         *
         * We have to:
         * - be sure to end up with a direct buffer.
         * - blend subtitles, and in a fast access buffer
Sam Hocevar's avatar
 
Sam Hocevar committed
838
         */
839 840 841 842 843 844 845 846 847 848
        picture_t *direct = NULL;
        if (filtered &&
            (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
            picture_t *render;
            if (vout->p->is_decoder_pool_slow)
                render = picture_NewFromFormat(&vout->fmt_out);
            else if (vout->p->decoder_pool != vout->p->display_pool)
                render = picture_pool_Get(vout->p->display_pool);
            else
                render = picture_pool_Get(vout->p->private_pool);
Sam Hocevar's avatar
 
Sam Hocevar committed
849

850 851
            if (render) {
                picture_Copy(render, filtered);
852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
                spu_RenderSubpictures(vout->p->p_spu,
                                      render, &vout->fmt_out,
                                      subpic, &vout->fmt_in, spu_render_time);
            }
            if (vout->p->is_decoder_pool_slow) {
                direct = picture_pool_Get(vout->p->display_pool);
                if (direct)
                    picture_Copy(direct, render);
                picture_Release(render);

            } else {
                direct = render;
            }
            picture_Release(filtered);
            filtered = NULL;
        } else {
            direct = filtered;
Sam Hocevar's avatar
 
Sam Hocevar committed
870 871
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
872
        /*
873
         * Take a snapshot if requested
Sam Hocevar's avatar
 
Sam Hocevar committed
874
         */
875 876
        if (direct && do_snapshot)
            vout_snapshot_Set(&vout->p->snapshot, &vout->fmt_out, direct);
877

878 879 880
        /* Render the direct buffer returned by vout_RenderPicture */
        if (direct) {
            vout_RenderWrapper(vout, direct);
Sam Hocevar's avatar
 
Sam Hocevar committed
881

882 883
            vout_chrono_Stop(&vout->p->render);
#if 0
Gildas Bazin's avatar
 
Gildas Bazin committed
884
            {
885 886 887 888
            static int i = 0;
            if (((i++)%10) == 0)
                msg_Info(vout, "render: avg %d ms var %d ms",
                         (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
Gildas Bazin's avatar
 
Gildas Bazin committed
889
            }
890
#endif
891
        }
892

893 894 895
        /* Wait the real date (for rendering jitter) */
        if (!is_forced)
            mwait(decoded->date);
Sam Hocevar's avatar
 
Sam Hocevar committed
896

897
        /* Display the direct buffer returned by vout_RenderPicture */
Laurent Aimar's avatar
Laurent Aimar committed
898
        vout->p->displayed.date = mdate();
899 900
        if (direct)
            vout_DisplayWrapper(vout, direct);
Vincent Seguin's avatar
Vincent Seguin committed
901

902 903 904
        displayed_count++;
        break;
    }
Vincent Seguin's avatar
Vincent Seguin committed
905

906 907 908 909 910
    vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
    if (displayed_count <= 0)
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}
911

912
/**********************