video_output.c 82.9 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3 4
 * video_output.c : video output thread
 * This module describes the programming interface for video output threads.
 * It includes functions allowing to open a new thread, send pictures to a
5 6 7 8 9 10 11 12 13 14
 * thread, and destroy a previously oppened video output thread.
 *****************************************************************************
 * Copyright (C) 2000 VideoLAN
 *
 * Authors:
 *
 * 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.
15
 * 
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21 22 23
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

26
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
27
 * Preamble
28
 *****************************************************************************/
29 30
#include "defs.h"

31 32 33 34
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
35

Michel Kaempf's avatar
Michel Kaempf committed
36
#include "config.h"
37 38
#include "common.h"
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
39
#include "mtime.h"
40
#include "plugins.h"
Michel Kaempf's avatar
Michel Kaempf committed
41 42
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
43
#include "video_text.h"
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
44
#include "video_spu.h"
45
#include "video_yuv.h"
46

Vincent Seguin's avatar
Vincent Seguin committed
47
#include "intf_msg.h"
48
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
49

50
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
51
 * Local prototypes
52
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
53 54
static int      BinaryLog         ( u32 i );
static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
55 56 57 58 59
static int      InitThread        ( vout_thread_t *p_vout );
static void     RunThread         ( vout_thread_t *p_vout );
static void     ErrorThread       ( vout_thread_t *p_vout );
static void     EndThread         ( vout_thread_t *p_vout );
static void     DestroyThread     ( vout_thread_t *p_vout, int i_status );
60
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
61 62 63 64 65 66 67 68 69 70 71 72
                                    int i_h_align, int i_v_align,
                                    unsigned char *psz_text );
static void     SetBufferArea     ( vout_thread_t *p_vout, int i_x, int i_y,
                                    int i_w, int i_h );
static void     SetBufferPicture  ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderPicture     ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
static void     RenderSubPicture  ( vout_thread_t *p_vout,
                                    subpicture_t *p_subpic );
static void     RenderInterface   ( vout_thread_t *p_vout );
static int      RenderIdle        ( vout_thread_t *p_vout );
static void     RenderInfo        ( vout_thread_t *p_vout );
73
static void     Synchronize       ( vout_thread_t *p_vout, s64 i_delay );
74 75 76 77
static int      Manage            ( vout_thread_t *p_vout );
static int      Align             ( vout_thread_t *p_vout, int *pi_x,
                                    int *pi_y, int i_width, int i_height,
                                    int i_h_align, int i_v_align );
78 79
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
80

81
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
82
 * vout_CreateThread: creates a new video output thread
83
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
84 85 86
 * This function creates a new video output thread, and returns a pointer
 * to its description. On error, it returns NULL.
 * If pi_status is NULL, then the function will block until the thread is ready.
87
 * If not, it will be updated using one of the THREAD_* constants.
88
 *****************************************************************************/
89 90
vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
                          int i_width, int i_height, int *pi_status, int i_method )
Michel Kaempf's avatar
Michel Kaempf committed
91
{
92 93 94
    vout_thread_t * p_vout;                             /* thread descriptor */
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
95
    char *          psz_method;
Michel Kaempf's avatar
Michel Kaempf committed
96

97
    /* Allocate descriptor */
98
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
99
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
100
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
101
    {
102
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
Michel Kaempf's avatar
Michel Kaempf committed
103 104
        return( NULL );
    }
105

106
    /* Request an interface plugin */
107 108
    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );

109
    if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
110
    {
111
        intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
112 113
        free( p_vout );
        return( NULL );
114
    }
115 116

    /* Get plugins */
Sam Hocevar's avatar
Sam Hocevar committed
117 118 119 120 121 122 123 124 125 126 127 128
    p_vout->p_sys_create = 
        GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
    p_vout->p_sys_init =
        GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
    p_vout->p_sys_end =
        GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
    p_vout->p_sys_destroy =
        GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
    p_vout->p_sys_manage =
        GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
    p_vout->p_sys_display =
        GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
129

130
    /* Initialize thread properties - thread id and locks will be initialized
131
     * later */
132
    p_vout->b_die               = 0;
133
    p_vout->b_error             = 0;
134 135
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
136
    *p_vout->pi_status          = THREAD_CREATE;
137

138
    /* Initialize some fields used by the system-dependant method - these fields will
139
     * probably be modified by the method, and are only preferences */
140
    p_vout->i_changes           = 0;
141 142
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
143
    p_vout->i_bytes_per_line    = i_width * 2;
144 145
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
146
    p_vout->f_gamma             = VOUT_GAMMA;
147

Sam Hocevar's avatar
Sam Hocevar committed
148 149
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
                                                       VOUT_GRAYSCALE_DEFAULT );
150
    p_vout->b_info              = 0;
151 152
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
153 154

    p_vout->p_set_palette       = SetPalette;
155

Sam Hocevar's avatar
Sam Hocevar committed
156 157 158
    intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
159

160 161
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
162
    p_vout->last_display_date   = 0;
163 164
    p_vout->last_idle_date      = 0;

165 166
#ifdef STATS
    /* Initialize statistics fields */
167 168 169
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
170 171 172 173

    /* Initialize buffer index */
    p_vout->i_buffer_index      = 0;

174
    /* Initialize pictures and subpictures - translation tables and functions
175
     * will be initialized later in InitThread */
176 177
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
178 179
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
180 181 182
    }
    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
    {
183 184
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
185
    }
186
    p_vout->i_pictures = 0;
187 188 189

    /* Initialize synchronization informations */
    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
190

191 192
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
193
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
194
    {
195
        TrashPlugin( p_vout->vout_plugin );
196 197
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
198
    }
Vincent Seguin's avatar
Vincent Seguin committed
199
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
200
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
201 202 203 204 205 206 207 208
                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
                p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );

    /* Calculate shifts from system-updated masks */
    MaskToShift( &p_vout->i_red_lshift,   &p_vout->i_red_rshift,   p_vout->i_red_mask );
    MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
    MaskToShift( &p_vout->i_blue_lshift,  &p_vout->i_blue_rshift,  p_vout->i_blue_mask );

209
    /* Set some useful colors */
210
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
211 212
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
213
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
214

215 216
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
217 218 219 220 221
    p_vout->p_default_font      = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
    if( p_vout->p_default_font == NULL )
    {
        p_vout->p_default_font  = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
    }
Vincent Seguin's avatar
Vincent Seguin committed
222 223
    if( p_vout->p_default_font == NULL )
    {
224
        p_vout->p_sys_destroy( p_vout );
225
        TrashPlugin( p_vout->vout_plugin );
226 227 228
        free( p_vout );
        return( NULL );
    }
229 230 231 232 233
    p_vout->p_large_font        = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
    if( p_vout->p_large_font == NULL )
    {
        p_vout->p_large_font    = vout_LoadFont( "share/" VOUT_LARGE_FONT );
    }
Vincent Seguin's avatar
Vincent Seguin committed
234 235
    if( p_vout->p_large_font == NULL )
    {
236
        vout_UnloadFont( p_vout->p_default_font );
237
        p_vout->p_sys_destroy( p_vout );
238
        TrashPlugin( p_vout->vout_plugin );
239 240 241
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
242

Michel Kaempf's avatar
Michel Kaempf committed
243
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
244
    vlc_mutex_init( &p_vout->picture_lock );
245 246 247
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
    vlc_mutex_lock( &p_vout->change_lock );
Vincent Seguin's avatar
Vincent Seguin committed
248
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
249
    {
250
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
251
        vout_UnloadFont( p_vout->p_default_font );
252
        vout_UnloadFont( p_vout->p_large_font );
253
        p_vout->p_sys_destroy( p_vout );
254
        TrashPlugin( p_vout->vout_plugin );
Michel Kaempf's avatar
Michel Kaempf committed
255 256
        free( p_vout );
        return( NULL );
257
    }
Michel Kaempf's avatar
Michel Kaempf committed
258

259
    intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
Vincent Seguin's avatar
Vincent Seguin committed
260
             p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
261

Michel Kaempf's avatar
Michel Kaempf committed
262 263 264 265
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
266
        {
Michel Kaempf's avatar
Michel Kaempf committed
267
            msleep( THREAD_SLEEP );
268
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
269 270 271
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
272 273
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
274 275 276 277
    }
    return( p_vout );
}

278
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
279
 * vout_DestroyThread: destroys a previously created thread
280 281
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
282
 * The function will request a destruction of the specified thread. If pi_error
283
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
284
 * update using one of the THREAD_* constants.
285
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
286
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
287 288
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
289 290

    /* Set status */
291
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
292
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
293 294
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
295 296 297 298
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
299
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
300 301 302 303
    {
        do
        {
            msleep( THREAD_SLEEP );
304 305
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
306 307 308
    }
}

309
/*****************************************************************************
310
 * vout_DisplaySubPicture: display a subpicture unit
311 312
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
313
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
314
 * the output thread if is reserved.
315
 *****************************************************************************/
316
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
317 318
{
#ifdef DEBUG_VIDEO
319 320
    char        psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
    char        psz_end_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
Vincent Seguin's avatar
Vincent Seguin committed
321 322 323 324
#endif

#ifdef DEBUG
    /* Check if status is valid */
325
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
326
    {
327 328 329
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
330 331 332
#endif

    /* Remove reservation flag */
333
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
334 335

#ifdef DEBUG_VIDEO
336
    /* Send subpicture informations */
337 338 339 340
    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
                p_subpic, p_subpic->i_type,
                mstrtime( psz_begin_date, p_subpic->begin_date ),
                mstrtime( psz_end_date, p_subpic->end_date ) );
Vincent Seguin's avatar
Vincent Seguin committed
341 342 343
#endif
}

344
/*****************************************************************************
345
 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
346 347
 *****************************************************************************
 * This function create a reserved subpicture in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
348
 * A null pointer is returned if the function fails. This method provides an
349
 * already allocated zone of memory in the spu data fields. It needs locking
350 351 352
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
353
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
354
{
355 356 357
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
358 359 360 361

    /* Get lock */
    vlc_mutex_lock( &p_vout->subpicture_lock );

362 363
    /*
     * Look for an empty place
364
     */
365
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
366
    {
367 368 369 370 371 372
        if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
        {
            /* Subpicture is marked for destruction, but is still allocated */
            if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
                (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
            {
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
373 374 375 376
                /* Memory size do match or is smaller : memory will not be
                 * reallocated, and function can end immediately - this is
                 * the best possible case, since no memory allocation needs
                 * to be done */
377
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
378
#ifdef DEBUG_VIDEO
379 380
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
381
#endif
382 383 384 385 386 387 388 389 390 391
                vlc_mutex_unlock( &p_vout->subpicture_lock );
                return( &p_vout->p_subpicture[i_subpic] );
            }
            else if( p_destroyed_subpic == NULL )
            {
                /* Memory size do not match, but subpicture index will be kept in
                 * case no other place are left */
                p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
            }
        }
392
        else if( (p_free_subpic == NULL) &&
393 394
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
395
            /* Subpicture is empty and ready for allocation */
396 397 398 399 400 401
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

    /* If no free subpicture is available, use a destroyed subpicture */
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
402
    {
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
403 404
        /* No free subpicture or matching destroyed subpicture has been
         * found, but a destroyed subpicture is still avalaible */
405
        free( p_destroyed_subpic->p_data );
406 407 408 409 410 411 412 413 414 415 416
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
417 418
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
419
            break;
420 421 422
        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
            p_free_subpic->p_data = malloc( i_size );
            break;
423 424 425
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
426 427 428
            p_free_subpic->p_data   =  NULL;
            break;
#endif
429 430 431
        }

        if( p_free_subpic->p_data != NULL )
432
        {           /* Copy subpicture informations, set some default values */
433 434
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
435
            p_free_subpic->i_size                      = i_size;
436
            p_free_subpic->i_x                         = 0;
437
            p_free_subpic->i_y                         = 0;
438 439
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
440 441
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
442 443 444 445
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
446 447 448 449
            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
            p_free_subpic->i_status =  FREE_SUBPICTURE;
            p_free_subpic =            NULL;
            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
450
        }
451

452
#ifdef DEBUG_VIDEO
453
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
454 455 456 457
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
458

459 460 461 462
    /* No free or destroyed subpicture could be found */
    intf_DbgMsg( "warning: heap is full\n" );
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
463 464
}

465
/*****************************************************************************
466
 * vout_DestroySubPicture: remove a subpicture from the heap
467
 *****************************************************************************
468
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
469
 * It is meant to be used when the construction of a picture aborted.
470
 * This function does not need locking since reserved subpictures are ignored
471
 * by the output thread.
472
 *****************************************************************************/
473
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
474 475
{
#ifdef DEBUG
476 477
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
478
   {
479 480 481
       intf_DbgMsg("error: subpicture %p has invalid status %d\n",
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
482 483
#endif

484
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
485 486

#ifdef DEBUG_VIDEO
487
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
488 489 490
#endif
}

491
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
492
 * vout_DisplayPicture: display a picture
493
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
494
 * Remove the reservation flag of a picture, which will cause it to be ready for
495
 * display. The picture won't be displayed until vout_DatePicture has been
496
 * called.
497
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
498
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
499
{
500 501
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
502
    {
503
    case RESERVED_PICTURE:
504
        p_pic->i_status = RESERVED_DISP_PICTURE;
505
        break;
506 507
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
508
        break;
509
#ifdef DEBUG
510 511 512
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
513
#endif
514
    }
Michel Kaempf's avatar
Michel Kaempf committed
515

516
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
517
    intf_DbgMsg("picture %p\n", p_pic);
518
#endif
519
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
520 521
}

522
/*****************************************************************************
523
 * vout_DatePicture: date a picture
524
 *****************************************************************************
525
 * Remove the reservation flag of a picture, which will cause it to be ready for
526
 * display. The picture won't be displayed until vout_DisplayPicture has been
527
 * called.
528
 *****************************************************************************/
529 530
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
531
#ifdef DEBUG_VIDEO
532
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
533 534
#endif

535
    vlc_mutex_lock( &p_vout->picture_lock );
536
    p_pic->date = date;
537 538
    switch( p_pic->i_status )
    {
539
    case RESERVED_PICTURE:
540
        p_pic->i_status = RESERVED_DATED_PICTURE;
541
        break;
542 543
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
544
        break;
545
#ifdef DEBUG
546 547 548
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
549 550 551 552
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
553
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
554
#endif
555
    vlc_mutex_unlock( &p_vout->picture_lock );
556 557
}

558
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
559
 * vout_CreatePicture: allocate a picture in the video output heap.
560 561
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
562
 * A null pointer is returned if the function fails. This method provides an
563
 * already allocated zone of memory in the picture data fields. It needs locking
564 565 566
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
567
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
568
{
569 570 571 572
    int         i_picture;                                  /* picture index */
    int         i_chroma_width = 0;                          /* chroma width */
    picture_t * p_free_picture = NULL;                 /* first free picture */
    picture_t * p_destroyed_picture = NULL;       /* first destroyed picture */
Michel Kaempf's avatar
Michel Kaempf committed
573

Vincent Seguin's avatar
Vincent Seguin committed
574
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
575
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
576

577 578
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
579
     */
580
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
581
    {
582 583 584
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
585
             * that if width and type are the same for two pictures, chroma_width
586
             * should also be the same */
587 588 589 590 591
            if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
                (p_vout->p_picture[i_picture].i_height         == i_height) &&
                (p_vout->p_picture[i_picture].i_width          == i_width) )
            {
                /* Memory size do match : memory will not be reallocated, and function
Vincent Seguin's avatar
Vincent Seguin committed
592 593
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
594
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
595
                p_vout->i_pictures++;
596
#ifdef DEBUG_VIDEO
597 598
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
599
#endif
600 601 602 603 604 605 606 607 608 609
                vlc_mutex_unlock( &p_vout->picture_lock );
                return( &p_vout->p_picture[i_picture] );
            }
            else if( p_destroyed_picture == NULL )
            {
                /* Memory size do not match, but picture index will be kept in
                 * case no other place are left */
                p_destroyed_picture = &p_vout->p_picture[i_picture];
            }
        }
610
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
611 612
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
613
            /* Picture is empty and ready for allocation */
614
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
615
        }
Michel Kaempf's avatar
Michel Kaempf committed
616 617
    }

Vincent Seguin's avatar
Vincent Seguin committed
618 619
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
620
    {
621 622
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
623 624
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
625 626
    }

Vincent Seguin's avatar
Vincent Seguin committed
627 628 629 630 631 632 633
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
634
        {
635 636
        case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
            i_chroma_width = i_width / 2;
Vincent Seguin's avatar
Vincent Seguin committed
637
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
638
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
639 640
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
641
            break;
642 643
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
644 645
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
646 647
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
648
            break;
649 650
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
651 652
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
653 654
            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
655
            break;
Vincent Seguin's avatar
Vincent Seguin committed
656 657
#ifdef DEBUG
        default:
658
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
659 660 661
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
662 663
        }

Vincent Seguin's avatar
Vincent Seguin committed
664
        if( p_free_picture->p_data != NULL )
665
        {
666
            /* Copy picture informations, set some default values */
667 668
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
669
            p_free_picture->i_matrix_coefficients       = 1;
670 671
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
672
            p_free_picture->i_chroma_width              = i_chroma_width;
673
            p_free_picture->i_display_horizontal_offset = 0;
674
            p_free_picture->i_display_vertical_offset   = 0;
675 676
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
677 678
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
679
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
680 681 682 683
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
684 685 686 687
            p_free_picture->i_type   =  EMPTY_PICTURE;
            p_free_picture->i_status =  FREE_PICTURE;
            p_free_picture =            NULL;
            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
688
        }
689

690
#ifdef DEBUG_VIDEO
691
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
692
#endif
Vincent Seguin's avatar
Vincent Seguin committed
693
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
694
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
695
    }
696

697
    /* No free or destroyed picture could be found */
698
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
699
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
700
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
701 702
}

703
/*****************************************************************************
704
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
705
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
706
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
707 708
 * picture. It is meant to be used when the construction of a picture aborted.
 * Note that the picture will be destroyed even if it is linked !
709
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
710
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
711
{
712
   vlc_mutex_lock( &p_vout->picture_lock );
713

714
#ifdef DEBUG
715
   /* Check if picture status is valid */
716
   if( (p_pic->i_status != RESERVED_PICTURE) &&
717 718
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
719
   {
720 721
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
722 723
#endif

724
   p_pic->i_status = DESTROYED_PICTURE;
725
   p_vout->i_pictures--;
726 727

#ifdef DEBUG_VIDEO
728
   intf_DbgMsg("picture %p\n", p_pic);
729
#endif
730
   vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
731 732
}

733
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
734
 * vout_LinkPicture: increment reference counter of a picture
735
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
736
 * This function increment the reference counter of a picture in the video
737
 * heap. It needs a lock since several producer threads can access the picture.
738
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
739 740
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
741
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
742
    p_pic->i_refcount++;
743 744

#ifdef DEBUG_VIDEO
745
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
746
#endif
747 748

    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
749 750
}

751
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
752
 * vout_UnlinkPicture: decrement reference counter of a picture
753
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
754
 * This function decrement the reference counter of a picture in the video heap.
755
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
756 757
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
758
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
759
    p_pic->i_refcount--;
760 761 762 763 764

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
765 766
        p_pic->i_refcount = 0;
    }
767 768
#endif

Vincent Seguin's avatar
Vincent Seguin committed
769
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
770
    {
771
        p_pic->i_status = DESTROYED_PICTURE;
772
        p_vout->i_pictures--;
Michel Kaempf's avatar
Michel Kaempf committed
773
    }
774 775

#ifdef DEBUG_VIDEO
776
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
777
#endif
778 779

    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
780 781
}

782
/*****************************************************************************
783
 * vout_SetBuffers: set buffers adresses
784 785
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
786
 * adresses.
787
 *****************************************************************************/
788
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
789 790
{
    /* No picture previously */
791 792 793 794 795 796 797 798
    p_vout->p_buffer[0].i_pic_x =         0;
    p_vout->p_buffer[0].i_pic_y =         0;
    p_vout->p_buffer[0].i_pic_width =     0;
    p_vout->p_buffer[0].i_pic_height =    0;
    p_vout->p_buffer[1].i_pic_x =         0;
    p_vout->p_buffer[1].i_pic_y =         0;
    p_vout->p_buffer[1].i_pic_width =     0;
    p_vout->p_buffer[1].i_pic_height =    0;
799 800

    /* The first area covers all the screen */
801 802 803 804 805 806 807 808
    p_vout->p_buffer[0].i_areas =                 1;
    p_vout->p_buffer[0].pi_area_begin[0] =        0;
    p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
    p_vout->p_buffer[1].i_areas =                 1;
    p_vout->p_buffer[1].pi_area_begin[0] =        0;
    p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;

    /* Set adresses */
809 810
    p_vout->p_buffer[0].p_data = p_buf1;
    p_vout->p_buffer[1].p_data = p_buf2;