video_output.c 79.9 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

18 19
#include <dlfcn.h>                                                /* plugins */

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

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

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

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

88 89 90 91 92 93 94 95 96
    /* Initialize method-dependent functions */
    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );

    psz_plugin = malloc( sizeof("./video_output/vout_.so") + strlen(psz_method) );
    sprintf( psz_plugin, "./video_output/vout_%s.so", psz_method );

    p_vout->p_vout_plugin = dlopen( psz_plugin, RTLD_LAZY );
    
    if( p_vout->p_vout_plugin == NULL )
97
    {
98 99 100 101
        intf_ErrMsg( "error: could not open video plugin %s\n", psz_method );
        free( psz_plugin );
        free( p_vout );
        return( NULL );
102
    }
103 104 105 106 107 108 109 110 111
    free( psz_plugin );

    /* Get plugins */
    p_vout->p_sys_create =  dlsym(p_vout->p_vout_plugin, "vout_SysCreate");
    p_vout->p_sys_init =    dlsym(p_vout->p_vout_plugin, "vout_SysInit");
    p_vout->p_sys_end =     dlsym(p_vout->p_vout_plugin, "vout_SysEnd");
    p_vout->p_sys_destroy = dlsym(p_vout->p_vout_plugin, "vout_SysDestroy");
    p_vout->p_sys_manage =  dlsym(p_vout->p_vout_plugin, "vout_SysManage");
    p_vout->p_sys_display = dlsym(p_vout->p_vout_plugin, "vout_SysDisplay");
112

113
    /* Initialize thread properties - thread id and locks will be initialized
114
     * later */
115
    p_vout->b_die               = 0;
116
    p_vout->b_error             = 0;
117 118
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
119
    *p_vout->pi_status          = THREAD_CREATE;
120

121
    /* Initialize some fields used by the system-dependant method - these fields will
122
     * probably be modified by the method, and are only preferences */
123
    p_vout->i_changes           = 0;
124 125
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
126
    p_vout->i_bytes_per_line    = i_width * 2;
127 128
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
129
    p_vout->f_gamma             = VOUT_GAMMA;
130

Vincent Seguin's avatar
Vincent Seguin committed
131
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
132
    p_vout->b_info              = 0;
133 134
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
135 136

    p_vout->p_set_palette       = SetPalette;
137

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

142 143
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
144
    p_vout->last_display_date   = 0;
145 146
    p_vout->last_idle_date      = 0;

147 148
#ifdef STATS
    /* Initialize statistics fields */
149 150 151
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
152 153 154 155

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

156
    /* Initialize pictures and subpictures - translation tables and functions
157
     * will be initialized later in InitThread */
158 159
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
160 161 162 163
        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;
164
    }
165 166 167 168
    p_vout->i_pictures = 0;    

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

170 171
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
172
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
173
    {
174 175 176
        dlclose( p_vout->p_vout_plugin );
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
177
    }
Vincent Seguin's avatar
Vincent Seguin committed
178
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
179
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
180 181 182 183 184 185 186 187
                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 );

188
    /* Set some useful colors */
189
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
190 191
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
192
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
193

194 195
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
196
    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
197 198
    if( p_vout->p_default_font == NULL )
    {
199
        p_vout->p_sys_destroy( p_vout );
200
        dlclose( p_vout->p_vout_plugin );
201 202 203 204
        free( p_vout );
        return( NULL );
    }
    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
205 206
    if( p_vout->p_large_font == NULL )
    {
207
        vout_UnloadFont( p_vout->p_default_font );
208
        p_vout->p_sys_destroy( p_vout );
209
        dlclose( p_vout->p_vout_plugin );
210 211 212
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
213

Michel Kaempf's avatar
Michel Kaempf committed
214
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
215
    vlc_mutex_init( &p_vout->picture_lock );
216 217 218
    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
219
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
220
    {
221
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
222
        vout_UnloadFont( p_vout->p_default_font );
223
        vout_UnloadFont( p_vout->p_large_font );
224
        p_vout->p_sys_destroy( p_vout );
225
        dlclose( p_vout->p_vout_plugin );
Michel Kaempf's avatar
Michel Kaempf committed
226 227
        free( p_vout );
        return( NULL );
228
    }
Michel Kaempf's avatar
Michel Kaempf committed
229

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

Michel Kaempf's avatar
Michel Kaempf committed
233 234 235 236
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
237
        {
Michel Kaempf's avatar
Michel Kaempf committed
238
            msleep( THREAD_SLEEP );
239
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
240 241 242
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
243 244
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
245 246 247 248
    }
    return( p_vout );
}

249
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
250
 * vout_DestroyThread: destroys a previously created thread
251 252
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
253
 * The function will request a destruction of the specified thread. If pi_error
254
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
255
 * update using one of the THREAD_* constants.
256
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
257
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
258 259
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
260 261

    /* Set status */
262
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
263
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
264 265
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
266 267 268 269
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
270
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
271 272 273 274
    {
        do
        {
            msleep( THREAD_SLEEP );
275 276
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
277 278 279
    }
}

280
/*****************************************************************************
281
 * vout_DisplaySubPicture: display a subpicture unit
282 283
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
284
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
285
 * the output thread if is reserved.
286
 *****************************************************************************/
287
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
288 289
{
#ifdef DEBUG_VIDEO
290 291
    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
292 293 294 295
#endif

#ifdef DEBUG
    /* Check if status is valid */
296
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
297
    {
298 299 300
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
301 302 303
#endif

    /* Remove reservation flag */
304
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
305 306

#ifdef DEBUG_VIDEO
307
    /* Send subpicture informations */
308 309 310 311
    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
312 313 314
#endif
}

315
/*****************************************************************************
316
 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
317 318
 *****************************************************************************
 * This function create a reserved subpicture in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
319
 * A null pointer is returned if the function fails. This method provides an
320
 * already allocated zone of memory in the spu data fields. It needs locking
321 322 323
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
324
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
325
{
326 327 328
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
329 330 331 332

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

333 334
    /*
     * Look for an empty place
335 336 337
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
338 339 340 341 342 343 344
        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,
345
                 * and function can end immediately - this is the best possible case,
346
                 * since no memory allocation needs to be done */
347
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
348
#ifdef DEBUG_VIDEO
349 350
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
351
#endif
352 353 354 355 356 357 358 359 360 361
                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];
            }
        }
362
        else if( (p_free_subpic == NULL) &&
363 364
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
365
            /* Subpicture is empty and ready for allocation */
366 367 368 369 370 371
            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 ) )
372
    {
373 374
        /* No free subpicture or matching destroyed subpicture has been found, but
         * a destroyed subpicture is still avalaible */
375
        free( p_destroyed_subpic->p_data );
376 377 378 379 380 381 382 383 384 385 386
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
387 388
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
389 390 391 392
            break;
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
393 394 395
            p_free_subpic->p_data   =  NULL;
            break;
#endif
396 397 398 399 400 401
        }

        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;
402
            p_free_subpic->i_size                      = i_size;
403
            p_free_subpic->i_x                         = 0;
404
            p_free_subpic->i_y                         = 0;
405 406
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
407 408
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
409 410 411 412
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
413 414 415 416
            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 ) );
417
        }
418

419
#ifdef DEBUG_VIDEO
420
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
421 422 423 424
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
425

426 427 428 429
    /* 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
430 431
}

432
/*****************************************************************************
433
 * vout_DestroySubPicture: remove a subpicture from the heap
434
 *****************************************************************************
435
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
436
 * It is meant to be used when the construction of a picture aborted.
437
 * This function does not need locking since reserved subpictures are ignored
438
 * by the output thread.
439
 *****************************************************************************/
440
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
441 442
{
#ifdef DEBUG
443 444
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
445
   {
446 447 448
       intf_DbgMsg("error: subpicture %p has invalid status %d\n",
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
449 450
#endif

451
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
452 453

#ifdef DEBUG_VIDEO
454
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
455 456 457
#endif
}

458
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
459
 * vout_DisplayPicture: display a picture
460
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
461
 * Remove the reservation flag of a picture, which will cause it to be ready for
462
 * display. The picture won't be displayed until vout_DatePicture has been
463
 * called.
464
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
465
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
466
{
467 468
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
469
    {
470
    case RESERVED_PICTURE:
471
        p_pic->i_status = RESERVED_DISP_PICTURE;
472
        break;
473 474
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
475
        break;
476
#ifdef DEBUG
477 478 479
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
480
#endif
481
    }
Michel Kaempf's avatar
Michel Kaempf committed
482

483
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
484
    intf_DbgMsg("picture %p\n", p_pic);
485
#endif
486
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
487 488
}

489
/*****************************************************************************
490
 * vout_DatePicture: date a picture
491
 *****************************************************************************
492
 * Remove the reservation flag of a picture, which will cause it to be ready for
493
 * display. The picture won't be displayed until vout_DisplayPicture has been
494
 * called.
495
 *****************************************************************************/
496 497
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
498
#ifdef DEBUG_VIDEO
499
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
500 501
#endif

502
    vlc_mutex_lock( &p_vout->picture_lock );
503
    p_pic->date = date;
504 505
    switch( p_pic->i_status )
    {
506
    case RESERVED_PICTURE:
507
        p_pic->i_status = RESERVED_DATED_PICTURE;
508
        break;
509 510
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
511
        break;
512
#ifdef DEBUG
513 514 515
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
516 517 518 519
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
520
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
521
#endif
522
    vlc_mutex_unlock( &p_vout->picture_lock );
523 524
}

525
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
526
 * vout_CreatePicture: allocate a picture in the video output heap.
527 528
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
529
 * A null pointer is returned if the function fails. This method provides an
530
 * already allocated zone of memory in the picture data fields. It needs locking
531 532 533
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
534
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
535
{
536 537 538 539
    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
540

Vincent Seguin's avatar
Vincent Seguin committed
541
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
542
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
543

544 545
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
546
     */
547
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
548
    {
549 550 551
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
552
             * that if width and type are the same for two pictures, chroma_width
553
             * should also be the same */
554 555 556 557 558
            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
559 560
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
561
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
562
                p_vout->i_pictures++;                
563
#ifdef DEBUG_VIDEO
564 565
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
566
#endif
567 568 569 570 571 572 573 574 575 576
                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];
            }
        }
577
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
578 579
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
580
            /* Picture is empty and ready for allocation */
581
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
582
        }
Michel Kaempf's avatar
Michel Kaempf committed
583 584
    }

Vincent Seguin's avatar
Vincent Seguin committed
585 586
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
587
    {
588 589
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
590 591
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
592 593
    }

Vincent Seguin's avatar
Vincent Seguin committed
594 595 596 597 598 599 600
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
601
        {
602 603
        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
604
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
605
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
606 607
            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;
608
            break;
609 610
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
611 612
            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
613 614
            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;
615
            break;
616 617
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
618 619
            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
620 621
            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;
622
            break;
Vincent Seguin's avatar
Vincent Seguin committed
623 624
#ifdef DEBUG
        default:
625
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
626 627 628
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
629 630
        }

Vincent Seguin's avatar
Vincent Seguin committed
631
        if( p_free_picture->p_data != NULL )
632
        {
633
            /* Copy picture informations, set some default values */
634 635
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
636
            p_free_picture->i_matrix_coefficients       = 1;
637 638
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
639
            p_free_picture->i_chroma_width              = i_chroma_width;
640
            p_free_picture->i_display_horizontal_offset = 0;
641
            p_free_picture->i_display_vertical_offset   = 0;
642 643
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
644 645
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
646
            p_vout->i_pictures++;            
Vincent Seguin's avatar
Vincent Seguin committed
647 648 649 650
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
651 652 653 654
            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
655
        }
656

657
#ifdef DEBUG_VIDEO
658
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
659
#endif
Vincent Seguin's avatar
Vincent Seguin committed
660
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
661
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
662
    }
663

664
    /* No free or destroyed picture could be found */
665
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
666
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
667
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
668 669
}

670
/*****************************************************************************
671
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
672
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
673
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
674 675
 * 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 !
676
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
677
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
678
{
679 680
   vlc_mutex_lock( &p_vout->picture_lock );   

681
#ifdef DEBUG
682
   /* Check if picture status is valid */
683
   if( (p_pic->i_status != RESERVED_PICTURE) &&
684 685
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
686
   {
687 688
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
689 690
#endif

691 692
   p_pic->i_status = DESTROYED_PICTURE;
   p_vout->i_pictures--;    
693 694

#ifdef DEBUG_VIDEO
695
   intf_DbgMsg("picture %p\n", p_pic);
696
#endif
697
   vlc_mutex_unlock( &p_vout->picture_lock );   
Michel Kaempf's avatar
Michel Kaempf committed
698 699
}

700
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
701
 * vout_LinkPicture: increment reference counter of a picture
702
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
703
 * This function increment the reference counter of a picture in the video
704
 * heap. It needs a lock since several producer threads can access the picture.
705
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
706 707
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
708
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
709
    p_pic->i_refcount++;
710 711

#ifdef DEBUG_VIDEO
712
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
713
#endif
714 715

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

718
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
719
 * vout_UnlinkPicture: decrement reference counter of a picture
720
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
721
 * This function decrement the reference counter of a picture in the video heap.
722
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
723 724
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
725
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
726
    p_pic->i_refcount--;
727 728 729 730 731

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
732 733
        p_pic->i_refcount = 0;
    }
734 735
#endif

Vincent Seguin's avatar
Vincent Seguin committed
736
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
737
    {
738
        p_pic->i_status = DESTROYED_PICTURE;
739
        p_vout->i_pictures--;        
Michel Kaempf's avatar
Michel Kaempf committed
740
    }
741 742

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

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

749
/*****************************************************************************
750
 * vout_SetBuffers: set buffers adresses
751 752
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
753
 * adresses.
754
 *****************************************************************************/
755
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
756 757
{
    /* No picture previously */
758 759 760 761 762 763 764 765
    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;
766 767

    /* The first area covers all the screen */
768 769 770 771 772 773 774 775
    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 */
776 777
    p_vout->p_buffer[0].p_data = p_buf1;
    p_vout->p_buffer[1].p_data = p_buf2;
778 779
}

Vincent Seguin's avatar
Vincent Seguin committed
780 781 782 783 784 785 786 787 788 789 790 791 792
/*****************************************************************************
 * 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
793 794
/* following functions are local */

Vincent Seguin's avatar
Vincent Seguin committed
795 796 797
/*****************************************************************************
 * BinaryLog: computes the base 2 log of a binary value
 *****************************************************************************
798
 * This functions is used by MaskToShift, to get a bit index from a binary
Vincent Seguin's avatar
Vincent Seguin committed
799 800 801 802 803 804 805
 * value.
 *****************************************************************************/
static int BinaryLog(u32 i)
{
    int i_log;

    i_log = 0;
806 807
    if (i & 0xffff0000)
    {
Vincent Seguin's avatar
Vincent Seguin committed
808
        i_log = 16;
809 810 811
    }
    if (i & 0xff00ff00)
    {
Vincent Seguin's avatar
Vincent Seguin committed
812
        i_log += 8;
813 814 815
    }
    if (i & 0xf0f0f0f0)
    {
Vincent Seguin's avatar
Vincent Seguin committed
816
        i_log += 4;
817 818 819
    }
    if (i & 0xcccccccc)
    {
Vincent Seguin's avatar
Vincent Seguin committed
820
        i_log += 2;
821 822 823
    }
    if (i & 0xaaaaaaaa)
    {
Vincent Seguin's avatar
Vincent Seguin committed
824
        i_log++;
825
    }