video_output.c 77.8 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * video_output.c : video output thread
3
 * (c)2000 VideoLAN
4
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
5 6 7
 * This module describes the programming interface for video output threads.
 * It includes functions allowing to open a new thread, send pictures to a
 * thread, and destroy a previously oppenned video output thread.
8
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
9

10
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
11
 * Preamble
12
 *****************************************************************************/
13
#include <errno.h>
Michel Kaempf's avatar
Michel Kaempf committed
14 15 16
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Vincent Seguin's avatar
Vincent Seguin committed
17

Michel Kaempf's avatar
Michel Kaempf committed
18 19 20
#include "common.h"
#include "config.h"
#include "mtime.h"
21
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
22 23
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
24
#include "video_text.h"
Vincent Seguin's avatar
Vincent Seguin committed
25
#include "video_sys.h"
26
#include "video_yuv.h"
Vincent Seguin's avatar
Vincent Seguin committed
27
#include "intf_msg.h"
28
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
29

30
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
31
 * Local prototypes
32
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
33 34
static int      BinaryLog         ( u32 i );
static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
35 36 37 38 39
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 );
40
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                                    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 );
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 );
57 58
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
59

60
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
61
 * vout_CreateThread: creates a new video output thread
62
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
63 64 65
 * 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.
66
 * If not, it will be updated using one of the THREAD_* constants.
67
 *****************************************************************************/
68 69
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
70
{
71 72 73
    vout_thread_t * p_vout;                             /* thread descriptor */
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
Michel Kaempf's avatar
Michel Kaempf committed
74

75
    /* Allocate descriptor */
76
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
77
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
78
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
79
    {
80
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Michel Kaempf's avatar
Michel Kaempf committed
81 82
        return( NULL );
    }
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    /* Sets method-specific functions */
    switch( i_method )
    {
        case VOUT_DUMMY_METHOD:
            p_vout->p_sys_create =    vout_DummySysCreate;
            p_vout->p_sys_init =      vout_DummySysInit;
            p_vout->p_sys_end =       vout_DummySysEnd;
            p_vout->p_sys_destroy =   vout_DummySysDestroy;
            p_vout->p_sys_manage =    vout_DummySysManage;
            p_vout->p_sys_display =   vout_DummySysDisplay;
            break;
#ifdef VIDEO_X11
        case VOUT_X11_METHOD:
            p_vout->p_sys_create =    vout_X11SysCreate;
            p_vout->p_sys_init =      vout_X11SysInit;
            p_vout->p_sys_end =       vout_X11SysEnd;
            p_vout->p_sys_destroy =   vout_X11SysDestroy;
            p_vout->p_sys_manage =    vout_X11SysManage;
            p_vout->p_sys_display =   vout_X11SysDisplay;
            break;
#endif
#ifdef VIDEO_FB
        case VOUT_FB_METHOD:
            p_vout->p_sys_create =    vout_FBSysCreate;
            p_vout->p_sys_init =      vout_FBSysInit;
            p_vout->p_sys_end =       vout_FBSysEnd;
            p_vout->p_sys_destroy =   vout_FBSysDestroy;
            p_vout->p_sys_manage =    vout_FBSysManage;
            p_vout->p_sys_display =   vout_FBSysDisplay;
            break;
#endif
#ifdef VIDEO_GLIDE
        case VOUT_GLIDE_METHOD:
            p_vout->p_sys_create =    vout_GlideSysCreate;
            p_vout->p_sys_init =      vout_GlideSysInit;
            p_vout->p_sys_end =       vout_GlideSysEnd;
            p_vout->p_sys_destroy =   vout_GlideSysDestroy;
            p_vout->p_sys_manage =    vout_GlideSysManage;
            p_vout->p_sys_display =   vout_GlideSysDisplay;
            break;
#endif
#ifdef VIDEO_DGA
        case VOUT_DGA_METHOD:
            p_vout->p_sys_create =    vout_DGASysCreate;
            p_vout->p_sys_init =      vout_DGASysInit;
            p_vout->p_sys_end =       vout_DGASysEnd;
            p_vout->p_sys_destroy =   vout_DGASysDestroy;
            p_vout->p_sys_manage =    vout_DGASysManage;
            p_vout->p_sys_display =   vout_DGASysDisplay;
            break;
#endif
#ifdef VIDEO_GGI
        case VOUT_GGI_METHOD:
            p_vout->p_sys_create =    vout_GGISysCreate;
            p_vout->p_sys_init =      vout_GGISysInit;
            p_vout->p_sys_end =       vout_GGISysEnd;
            p_vout->p_sys_destroy =   vout_GGISysDestroy;
            p_vout->p_sys_manage =    vout_GGISysManage;
            p_vout->p_sys_display =   vout_GGISysDisplay;
            break;
#endif
#ifdef VIDEO_BEOS
        case VOUT_BEOS_METHOD:
147 148 149 150 151 152
            p_vout->p_sys_create =    vout_BSysCreate;
            p_vout->p_sys_init =      vout_BSysInit;
            p_vout->p_sys_end =       vout_BSysEnd;
            p_vout->p_sys_destroy =   vout_BSysDestroy;
            p_vout->p_sys_manage =    vout_BSysManage;
            p_vout->p_sys_display =   vout_BSysDisplay;
153 154 155 156 157 158 159 160
            break;
#endif
        default:
            intf_ErrMsg( "error: video output method not available\n" );
            free( p_vout );
            return( NULL );
    }

161
    /* Initialize thread properties - thread id and locks will be initialized
162
     * later */
163
    p_vout->b_die               = 0;
164
    p_vout->b_error             = 0;
165 166
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
167
    *p_vout->pi_status          = THREAD_CREATE;
168

169
    /* Initialize some fields used by the system-dependant method - these fields will
170
     * probably be modified by the method, and are only preferences */
171
    p_vout->i_changes           = 0;
172 173
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
174
    p_vout->i_bytes_per_line    = i_width * 2;
175 176
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
177
    p_vout->f_gamma             = VOUT_GAMMA;
178

Vincent Seguin's avatar
Vincent Seguin committed
179
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
180
    p_vout->b_info              = 0;
181 182
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
183 184

    p_vout->p_set_palette       = SetPalette;
185

Vincent Seguin's avatar
Vincent Seguin committed
186
    intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
187
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
188
                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
189

190 191
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
192
    p_vout->last_display_date   = 0;
193 194
    p_vout->last_idle_date      = 0;

195 196
#ifdef STATS
    /* Initialize statistics fields */
197 198 199
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
200 201 202 203

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

204
    /* Initialize pictures and subpictures - translation tables and functions
205
     * will be initialized later in InitThread */
206 207
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
208 209 210 211
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
212
    }
213

214 215
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
216
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
217
    {
Vincent Seguin's avatar
Vincent Seguin committed
218 219
      free( p_vout );
      return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
220
    }
Vincent Seguin's avatar
Vincent Seguin committed
221
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
222
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
223 224 225 226 227 228 229 230
                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 );

231
    /* Set some useful colors */
232
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
233 234
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
235
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
236

237 238
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
239
    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
240 241
    if( p_vout->p_default_font == NULL )
    {
242
        p_vout->p_sys_destroy( p_vout );
243 244 245 246
        free( p_vout );
        return( NULL );
    }
    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
247 248
    if( p_vout->p_large_font == NULL )
    {
249
        vout_UnloadFont( p_vout->p_default_font );
250
        p_vout->p_sys_destroy( p_vout );
251 252 253
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
254

Michel Kaempf's avatar
Michel Kaempf committed
255
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
256
    vlc_mutex_init( &p_vout->picture_lock );
257 258 259
    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
260
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
261
    {
262
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
263
        vout_UnloadFont( p_vout->p_default_font );
264
        vout_UnloadFont( p_vout->p_large_font );
265
        p_vout->p_sys_destroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
266 267
        free( p_vout );
        return( NULL );
268
    }
Michel Kaempf's avatar
Michel Kaempf committed
269

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

Michel Kaempf's avatar
Michel Kaempf committed
273 274 275 276
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
277
        {
Michel Kaempf's avatar
Michel Kaempf committed
278
            msleep( THREAD_SLEEP );
279
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
280 281 282
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
283 284
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
285 286 287 288
    }
    return( p_vout );
}

289
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
290
 * vout_DestroyThread: destroys a previously created thread
291 292
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
293
 * The function will request a destruction of the specified thread. If pi_error
294
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
295
 * update using one of the THREAD_* constants.
296
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
297
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
298 299
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
300 301

    /* Set status */
302
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
303
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
304 305
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
306 307 308 309
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
310
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
311 312 313 314
    {
        do
        {
            msleep( THREAD_SLEEP );
315 316
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
317 318 319
    }
}

320
/*****************************************************************************
321
 * vout_DisplaySubPicture: display a subpicture unit
322 323
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
324
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
325
 * the output thread if is reserved.
326
 *****************************************************************************/
327
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
328 329
{
#ifdef DEBUG_VIDEO
330 331
    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
332 333 334 335
#endif

#ifdef DEBUG
    /* Check if status is valid */
336
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
337
    {
338 339 340
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
341 342 343
#endif

    /* Remove reservation flag */
344
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
345 346

#ifdef DEBUG_VIDEO
347
    /* Send subpicture informations */
348 349 350 351
    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
352 353 354
#endif
}

355
/*****************************************************************************
356
 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
357 358
 *****************************************************************************
 * This function create a reserved subpicture in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
359
 * A null pointer is returned if the function fails. This method provides an
360
 * already allocated zone of memory in the spu data fields. It needs locking
361 362 363
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
364
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
365
{
366 367 368
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
369 370 371 372

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

373 374
    /*
     * Look for an empty place
375 376 377
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
378 379 380 381 382 383 384
        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) )
            {
                /* Memory size do match or is smaller : memory will not be reallocated,
385
                 * and function can end immediately - this is the best possible case,
386
                 * since no memory allocation needs to be done */
387
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
388
#ifdef DEBUG_VIDEO
389 390
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
391
#endif
392 393 394 395 396 397 398 399 400 401
                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];
            }
        }
402
        else if( (p_free_subpic == NULL) &&
403 404
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
405
            /* Subpicture is empty and ready for allocation */
406 407 408 409 410 411
            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 ) )
412
    {
413 414
        /* No free subpicture or matching destroyed subpicture has been found, but
         * a destroyed subpicture is still avalaible */
415
        free( p_destroyed_subpic->p_data );
416 417 418 419 420 421 422 423 424 425 426
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
427 428
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
429 430 431 432
            break;
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
433 434 435
            p_free_subpic->p_data   =  NULL;
            break;
#endif
436 437 438 439 440 441
        }

        if( p_free_subpic->p_data != NULL )
        {                    /* Copy subpicture informations, set some default values */
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
442
            p_free_subpic->i_size                      = i_size;
443
            p_free_subpic->i_x                         = 0;
444
            p_free_subpic->i_y                         = 0;
445 446
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
447 448
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
449 450 451 452
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
453 454 455 456
            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 ) );
457
        }
458

459
#ifdef DEBUG_VIDEO
460
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
461 462 463 464
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
465

466 467 468 469
    /* 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
470 471
}

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

491
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
492 493

#ifdef DEBUG_VIDEO
494
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
495 496 497
#endif
}

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

523
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
524
    intf_DbgMsg("picture %p\n", p_pic);
525
#endif
526
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
527 528
}

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

542
    vlc_mutex_lock( &p_vout->picture_lock );
543
    p_pic->date = date;
544 545
    switch( p_pic->i_status )
    {
546
    case RESERVED_PICTURE:
547
        p_pic->i_status = RESERVED_DATED_PICTURE;
548
        break;
549 550
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
551
        break;
552
#ifdef DEBUG
553 554 555
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
556 557 558 559
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
560
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
561
#endif
562
    vlc_mutex_unlock( &p_vout->picture_lock );
563 564
}

565
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
566
 * vout_CreatePicture: allocate a picture in the video output heap.
567 568
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
569
 * A null pointer is returned if the function fails. This method provides an
570
 * already allocated zone of memory in the picture data fields. It needs locking
571 572 573
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
574
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
575
{
576 577 578 579
    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
580

Vincent Seguin's avatar
Vincent Seguin committed
581
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
582
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
583

584 585
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
586
     */
587
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
588
    {
589 590 591
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
592
             * that if width and type are the same for two pictures, chroma_width
593
             * should also be the same */
594 595 596 597 598
            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
599 600
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
601
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
602
#ifdef DEBUG_VIDEO
603 604
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
605
#endif
606 607 608 609 610 611 612 613 614 615
                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];
            }
        }
616
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
617 618
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
619
            /* Picture is empty and ready for allocation */
620
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
621
        }
Michel Kaempf's avatar
Michel Kaempf committed
622 623
    }

Vincent Seguin's avatar
Vincent Seguin committed
624 625
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
626
    {
627 628
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
629 630
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
631 632
    }

Vincent Seguin's avatar
Vincent Seguin committed
633 634 635 636 637 638 639
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
640
        {
641 642
        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
643
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
644
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
645 646
            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;
647
            break;
648 649
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
650 651
            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
652 653
            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;
654
            break;
655 656
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
657 658
            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
659 660
            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;
661
            break;
Vincent Seguin's avatar
Vincent Seguin committed
662 663
#ifdef DEBUG
        default:
664
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
665 666 667
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
668 669
        }

Vincent Seguin's avatar
Vincent Seguin committed
670
        if( p_free_picture->p_data != NULL )
671
        {
672
            /* Copy picture informations, set some default values */
673 674
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
675
            p_free_picture->i_matrix_coefficients       = 1;
676 677
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
678
            p_free_picture->i_chroma_width              = i_chroma_width;
679
            p_free_picture->i_display_horizontal_offset = 0;
680
            p_free_picture->i_display_vertical_offset   = 0;
681 682
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
683 684
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
Vincent Seguin's avatar
Vincent Seguin committed
685 686 687 688
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
689 690 691 692
            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
693
        }
694

695
#ifdef DEBUG_VIDEO
696
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
697
#endif
Vincent Seguin's avatar
Vincent Seguin committed
698
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
699
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
700
    }
701

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

708
/*****************************************************************************
709
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
710
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
711
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
712 713
 * 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 !
714 715
 * This function does not need locking since reserved pictures are ignored by
 * the output thread.
716
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
717
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
718
{
719
#ifdef DEBUG
720
   /* Check if picture status is valid */
721
   if( (p_pic->i_status != RESERVED_PICTURE) &&
722 723
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
724
   {
725 726
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
727 728
#endif

Vincent Seguin's avatar
Vincent Seguin committed
729
    p_pic->i_status = DESTROYED_PICTURE;
730 731

#ifdef DEBUG_VIDEO
732
    intf_DbgMsg("picture %p\n", p_pic);
733
#endif
Michel Kaempf's avatar
Michel Kaempf committed
734 735
}

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

#ifdef DEBUG_VIDEO
748
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
749
#endif
750 751

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

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

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

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

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

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

784
/*****************************************************************************
785
 * vout_SetBuffers: set buffers adresses
786 787
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
788
 * adresses.
789
 *****************************************************************************/
790
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
791 792
{
    /* No picture previously */
793 794 795 796 797 798 799 800
    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;
801 802

    /* The first area covers all the screen */
803 804 805 806 807 808 809 810
    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 */
811 812
    p_vout->p_buffer[0].p_data = p_buf1;
    p_vout->p_buffer[1].p_data = p_buf2;
813 814
}

Vincent Seguin's avatar
Vincent Seguin committed
815 816 817 818 819 820 821 822 823 824 825 826 827
/*****************************************************************************
 * vout_Pixel2RGB: return red, green and blue from pixel value
 *****************************************************************************
 * Return color values, in 0-255 range, of the decomposition of a pixel. This
 * is a slow routine and should only be used for initialization phase.
 *****************************************************************************/
void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
{
    *pi_red =   i_pixel & p_vout->i_red_mask;
    *pi_green = i_pixel & p_vout->i_green_mask;
    *pi_blue =  i_pixel & p_vout->i_blue_mask;
}

Michel Kaempf's avatar
Michel Kaempf committed
828 829
/* following functions are local */

Vincent Seguin's avatar
Vincent Seguin committed
830 831 832
/*****************************************************************************
 * BinaryLog: computes the base 2 log of a binary value
 *****************************************************************************
833
 * This functions is used by MaskToShift, to get a bit index from a binary