video_output.c 80.8 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 15 16 17 18 19 20 21 22 23 24
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

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

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

Michel Kaempf's avatar
Michel Kaempf committed
37
#include "config.h"
38 39
#include "common.h"
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
40
#include "mtime.h"
41
#include "plugins.h"
Michel Kaempf's avatar
Michel Kaempf committed
42 43
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
44
#include "video_text.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 */
117 118 119 120 121 122
    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" );
123

124
    /* Initialize thread properties - thread id and locks will be initialized
125
     * later */
126
    p_vout->b_die               = 0;
127
    p_vout->b_error             = 0;
128 129
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
130
    *p_vout->pi_status          = THREAD_CREATE;
131

132
    /* Initialize some fields used by the system-dependant method - these fields will
133
     * probably be modified by the method, and are only preferences */
134
    p_vout->i_changes           = 0;
135 136
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
137
    p_vout->i_bytes_per_line    = i_width * 2;
138 139
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
140
    p_vout->f_gamma             = VOUT_GAMMA;
141

Vincent Seguin's avatar
Vincent Seguin committed
142
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
143
    p_vout->b_info              = 0;
144 145
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
146 147

    p_vout->p_set_palette       = SetPalette;
148

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

153 154
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
155
    p_vout->last_display_date   = 0;
156 157
    p_vout->last_idle_date      = 0;

158 159
#ifdef STATS
    /* Initialize statistics fields */
160 161 162
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
163 164 165 166

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

167
    /* Initialize pictures and subpictures - translation tables and functions
168
     * will be initialized later in InitThread */
169 170
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
171 172 173 174
        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;
175
    }
176
    p_vout->i_pictures = 0;
177 178 179

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

181 182
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
183
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
184
    {
185
        TrashPlugin( p_vout->vout_plugin );
186 187
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
188
    }
Vincent Seguin's avatar
Vincent Seguin committed
189
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
190
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
191 192 193 194 195 196 197 198
                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 );

199
    /* Set some useful colors */
200
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
201 202
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
203
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
204

205 206
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
207 208 209 210 211
    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
212 213
    if( p_vout->p_default_font == NULL )
    {
214
        p_vout->p_sys_destroy( p_vout );
215
        TrashPlugin( p_vout->vout_plugin );
216 217 218
        free( p_vout );
        return( NULL );
    }
219 220 221 222 223
    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
224 225
    if( p_vout->p_large_font == NULL )
    {
226
        vout_UnloadFont( p_vout->p_default_font );
227
        p_vout->p_sys_destroy( p_vout );
228
        TrashPlugin( p_vout->vout_plugin );
229 230 231
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
232

Michel Kaempf's avatar
Michel Kaempf committed
233
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
234
    vlc_mutex_init( &p_vout->picture_lock );
235 236 237
    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
238
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
239
    {
240
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
241
        vout_UnloadFont( p_vout->p_default_font );
242
        vout_UnloadFont( p_vout->p_large_font );
243
        p_vout->p_sys_destroy( p_vout );
244
        TrashPlugin( p_vout->vout_plugin );
Michel Kaempf's avatar
Michel Kaempf committed
245 246
        free( p_vout );
        return( NULL );
247
    }
Michel Kaempf's avatar
Michel Kaempf committed
248

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

Michel Kaempf's avatar
Michel Kaempf committed
252 253 254 255
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
256
        {
Michel Kaempf's avatar
Michel Kaempf committed
257
            msleep( THREAD_SLEEP );
258
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
259 260 261
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
262 263
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
264 265 266 267
    }
    return( p_vout );
}

268
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
269
 * vout_DestroyThread: destroys a previously created thread
270 271
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
272
 * The function will request a destruction of the specified thread. If pi_error
273
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
274
 * update using one of the THREAD_* constants.
275
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
276
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
277 278
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
279 280

    /* Set status */
281
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
282
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
283 284
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
285 286 287 288
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
289
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
290 291 292 293
    {
        do
        {
            msleep( THREAD_SLEEP );
294 295
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
296 297 298
    }
}

299
/*****************************************************************************
300
 * vout_DisplaySubPicture: display a subpicture unit
301 302
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
303
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
304
 * the output thread if is reserved.
305
 *****************************************************************************/
306
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
307 308
{
#ifdef DEBUG_VIDEO
309 310
    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
311 312 313 314
#endif

#ifdef DEBUG
    /* Check if status is valid */
315
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
316
    {
317 318 319
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
320 321 322
#endif

    /* Remove reservation flag */
323
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
324 325

#ifdef DEBUG_VIDEO
326
    /* Send subpicture informations */
327 328 329 330
    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
331 332 333
#endif
}

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

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

352 353
    /*
     * Look for an empty place
354 355 356
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
357 358 359 360 361 362 363
        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,
364
                 * and function can end immediately - this is the best possible case,
365
                 * since no memory allocation needs to be done */
366
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
367
#ifdef DEBUG_VIDEO
368 369
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
370
#endif
371 372 373 374 375 376 377 378 379 380
                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];
            }
        }
381
        else if( (p_free_subpic == NULL) &&
382 383
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
384
            /* Subpicture is empty and ready for allocation */
385 386 387 388 389 390
            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 ) )
391
    {
392 393
        /* No free subpicture or matching destroyed subpicture has been found, but
         * a destroyed subpicture is still avalaible */
394
        free( p_destroyed_subpic->p_data );
395 396 397 398 399 400 401 402 403 404 405
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
406 407
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
408 409 410 411
            break;
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
412 413 414
            p_free_subpic->p_data   =  NULL;
            break;
#endif
415 416 417 418 419 420
        }

        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;
421
            p_free_subpic->i_size                      = i_size;
422
            p_free_subpic->i_x                         = 0;
423
            p_free_subpic->i_y                         = 0;
424 425
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
426 427
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
428 429 430 431
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
432 433 434 435
            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 ) );
436
        }
437

438
#ifdef DEBUG_VIDEO
439
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
440 441 442 443
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
444

445 446 447 448
    /* 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
449 450
}

451
/*****************************************************************************
452
 * vout_DestroySubPicture: remove a subpicture from the heap
453
 *****************************************************************************
454
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
455
 * It is meant to be used when the construction of a picture aborted.
456
 * This function does not need locking since reserved subpictures are ignored
457
 * by the output thread.
458
 *****************************************************************************/
459
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
460 461
{
#ifdef DEBUG
462 463
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
464
   {
465 466 467
       intf_DbgMsg("error: subpicture %p has invalid status %d\n",
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
468 469
#endif

470
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
471 472

#ifdef DEBUG_VIDEO
473
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
474 475 476
#endif
}

477
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
478
 * vout_DisplayPicture: display a picture
479
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
480
 * Remove the reservation flag of a picture, which will cause it to be ready for
481
 * display. The picture won't be displayed until vout_DatePicture has been
482
 * called.
483
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
484
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
485
{
486 487
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
488
    {
489
    case RESERVED_PICTURE:
490
        p_pic->i_status = RESERVED_DISP_PICTURE;
491
        break;
492 493
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
494
        break;
495
#ifdef DEBUG
496 497 498
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
499
#endif
500
    }
Michel Kaempf's avatar
Michel Kaempf committed
501

502
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
503
    intf_DbgMsg("picture %p\n", p_pic);
504
#endif
505
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
506 507
}

508
/*****************************************************************************
509
 * vout_DatePicture: date a picture
510
 *****************************************************************************
511
 * Remove the reservation flag of a picture, which will cause it to be ready for
512
 * display. The picture won't be displayed until vout_DisplayPicture has been
513
 * called.
514
 *****************************************************************************/
515 516
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
517
#ifdef DEBUG_VIDEO
518
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
519 520
#endif

521
    vlc_mutex_lock( &p_vout->picture_lock );
522
    p_pic->date = date;
523 524
    switch( p_pic->i_status )
    {
525
    case RESERVED_PICTURE:
526
        p_pic->i_status = RESERVED_DATED_PICTURE;
527
        break;
528 529
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
530
        break;
531
#ifdef DEBUG
532 533 534
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
535 536 537 538
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
539
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
540
#endif
541
    vlc_mutex_unlock( &p_vout->picture_lock );
542 543
}

544
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
545
 * vout_CreatePicture: allocate a picture in the video output heap.
546 547
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
548
 * A null pointer is returned if the function fails. This method provides an
549
 * already allocated zone of memory in the picture data fields. It needs locking
550 551 552
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
553
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
554
{
555 556 557 558
    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
559

Vincent Seguin's avatar
Vincent Seguin committed
560
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
561
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
562

563 564
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
565
     */
566
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
567
    {
568 569 570
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
571
             * that if width and type are the same for two pictures, chroma_width
572
             * should also be the same */
573 574 575 576 577
            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
578 579
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
580
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
581
                p_vout->i_pictures++;
582
#ifdef DEBUG_VIDEO
583 584
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
585
#endif
586 587 588 589 590 591 592 593 594 595
                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];
            }
        }
596
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
597 598
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
599
            /* Picture is empty and ready for allocation */
600
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
601
        }
Michel Kaempf's avatar
Michel Kaempf committed
602 603
    }

Vincent Seguin's avatar
Vincent Seguin committed
604 605
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
606
    {
607 608
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
609 610
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
611 612
    }

Vincent Seguin's avatar
Vincent Seguin committed
613 614 615 616 617 618 619
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
620
        {
621 622
        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
623
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
624
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
625 626
            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;
627
            break;
628 629
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
630 631
            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
632 633
            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;
634
            break;
635 636
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
637 638
            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
639 640
            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;
641
            break;
Vincent Seguin's avatar
Vincent Seguin committed
642 643
#ifdef DEBUG
        default:
644
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
645 646 647
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
648 649
        }

Vincent Seguin's avatar
Vincent Seguin committed
650
        if( p_free_picture->p_data != NULL )
651
        {
652
            /* Copy picture informations, set some default values */
653 654
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
655
            p_free_picture->i_matrix_coefficients       = 1;
656 657
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
658
            p_free_picture->i_chroma_width              = i_chroma_width;
659
            p_free_picture->i_display_horizontal_offset = 0;
660
            p_free_picture->i_display_vertical_offset   = 0;
661 662
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
663 664
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
665
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
666 667 668 669
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
670 671 672 673
            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
674
        }
675

676
#ifdef DEBUG_VIDEO
677
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
678
#endif
Vincent Seguin's avatar
Vincent Seguin committed
679
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
680
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
681
    }
682

683
    /* No free or destroyed picture could be found */
684
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
685
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
686
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
687 688
}

689
/*****************************************************************************
690
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
691
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
692
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
693 694
 * 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 !
695
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
696
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
697
{
698
   vlc_mutex_lock( &p_vout->picture_lock );
699

700
#ifdef DEBUG
701
   /* Check if picture status is valid */
702
   if( (p_pic->i_status != RESERVED_PICTURE) &&
703 704
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
705
   {
706 707
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
708 709
#endif

710
   p_pic->i_status = DESTROYED_PICTURE;
711
   p_vout->i_pictures--;
712 713

#ifdef DEBUG_VIDEO
714
   intf_DbgMsg("picture %p\n", p_pic);
715
#endif
716
   vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
717 718
}

719
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
720
 * vout_LinkPicture: increment reference counter of a picture
721
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
722
 * This function increment the reference counter of a picture in the video
723
 * heap. It needs a lock since several producer threads can access the picture.
724
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
725 726
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
727
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
728
    p_pic->i_refcount++;
729 730

#ifdef DEBUG_VIDEO
731
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
732
#endif
733 734

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

737
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
738
 * vout_UnlinkPicture: decrement reference counter of a picture
739
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
740
 * This function decrement the reference counter of a picture in the video heap.
741
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
742 743
void vout_UnlinkPicture( 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 748 749 750

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
751 752
        p_pic->i_refcount = 0;
    }
753 754
#endif

Vincent Seguin's avatar
Vincent Seguin committed
755
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
756
    {
757
        p_pic->i_status = DESTROYED_PICTURE;
758
        p_vout->i_pictures--;
Michel Kaempf's avatar
Michel Kaempf committed
759
    }
760 761

#ifdef DEBUG_VIDEO
762
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
763
#endif
764 765

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

768
/*****************************************************************************
769
 * vout_SetBuffers: set buffers adresses
770 771
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
772
 * adresses.
773
 *****************************************************************************/
774
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
775 776
{
    /* No picture previously */
777 778 779 780 781 782 783 784
    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;
785 786

    /* The first area covers all the screen */
787 788 789 790 791 792 793 794
    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 */
795 796
    p_vout->p_buffer[0].p_data = p_buf1;
    p_vout->p_buffer[1].p_data = p_buf2;
797 798
}

Vincent Seguin's avatar
Vincent Seguin committed
799 800 801 802 803 804 805 806 807 808 809 810 811
/*****************************************************************************
 * 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
812 813
/* following functions are local */

Vincent Seguin's avatar
Vincent Seguin committed
814 815 816
/*****************************************************************************
 * BinaryLog: computes the base 2 log of a binary value
 *****************************************************************************
817
 * This functions is used by MaskToShift, to get a bit index from a binary
Vincent Seguin's avatar
Vincent Seguin committed
818 819 820 821 822 823 824
 * value.
 *****************************************************************************/
static int BinaryLog(u32 i)
{
    int i_log;

    i_log = 0;
825 826
    if (i