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

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

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

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

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

Renaud Dartus's avatar
 
Renaud Dartus committed
50 51 52
#include "vdec_idct.h"
#include "video_decoder.h"

53
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
54
 * Local prototypes
55
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
56 57
static int      BinaryLog         ( u32 i );
static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
58 59 60 61 62
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 );
63
static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
64 65 66 67 68 69 70 71 72 73 74
                                    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 );
Renaud Dartus's avatar
 
Renaud Dartus committed
75
static int      RenderSplash      ( vout_thread_t *p_vout );
76 77 78 79 80
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 );
81 82
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
83

84
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
85
 * vout_CreateThread: creates a new video output thread
86
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
87 88 89
 * 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.
90
 * If not, it will be updated using one of the THREAD_* constants.
91
 *****************************************************************************/
92
vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
93 94
                          int i_width, int i_height, int *pi_status,
                          int i_method, void *p_data )
Michel Kaempf's avatar
Michel Kaempf committed
95
{
96
    vout_thread_t * p_vout;                             /* thread descriptor */
97
    typedef void    ( vout_getplugin_t ) ( vout_thread_t * p_vout );
98 99
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
Sam Hocevar's avatar
Sam Hocevar committed
100
    int             i_best_index = 0, i_best_score = 0;
Michel Kaempf's avatar
Michel Kaempf committed
101

102
    /* Allocate descriptor */
103
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
104
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
105
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
106
    {
107
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
Michel Kaempf's avatar
Michel Kaempf committed
108 109
        return( NULL );
    }
110

111
    p_vout->p_set_palette       = SetPalette;
112

113 114
    /* Get a suitable video plugin */
    for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
115
    {
116 117 118 119 120 121
        /* If there's a plugin in p_info ... */
        if( p_main->p_bank->p_info[ i_index ] != NULL )
        {
            /* ... and if this plugin provides the functions we want ... */
            if( p_main->p_bank->p_info[ i_index ]->vout_GetPlugin != NULL )
            {
Sam Hocevar's avatar
Sam Hocevar committed
122 123 124 125 126 127 128
                /* ... and if this plugin has a good score ... */
                if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
                {
                    /* ... then take it */
                    i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
                    i_best_index = i_index;
                }
129 130
            }
        }
131
    }
132

Sam Hocevar's avatar
Sam Hocevar committed
133 134 135 136 137 138 139 140 141 142
    if( i_best_score == 0 )
    {
        free( p_vout );
        return( NULL );
    }

    /* Get the plugin functions */
    ( (vout_getplugin_t *)
      p_main->p_bank->p_info[ i_best_index ]->vout_GetPlugin )( p_vout );

143
    /* Initialize thread properties - thread id and locks will be initialized
144
     * later */
145
    p_vout->b_die               = 0;
146
    p_vout->b_error             = 0;
147 148
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
149
    *p_vout->pi_status          = THREAD_CREATE;
150

151 152 153
    /* Initialize some fields used by the system-dependant method - these
     * fields will probably be modified by the method, and are only
     * preferences */
Renaud Dartus's avatar
 
Renaud Dartus committed
154 155 156 157 158 159 160 161 162
    p_vout->i_changes             = 0;
    p_vout->i_width               = i_width;
    p_vout->i_height              = i_height;
    p_vout->i_bytes_per_line      = i_width * 2;
    p_vout->i_screen_depth        = 15;
    p_vout->i_bytes_per_pixel     = 2;
    p_vout->f_gamma               = VOUT_GAMMA;

    p_vout->b_grayscale           = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
Sam Hocevar's avatar
Sam Hocevar committed
163
                                                       VOUT_GRAYSCALE_DEFAULT );
Renaud Dartus's avatar
 
Renaud Dartus committed
164 165 166 167
    p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockC;
    p_vout->b_info                = 0;
    p_vout->b_interface           = 0;
    p_vout->b_scale               = 1;
168

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

173
    /* Initialize idle screen */
174
    p_vout->last_display_date   = 0;
175
    p_vout->last_idle_date      = 0;
176
    p_vout->init_display_date   = mdate();
177
    p_vout->render_time         = 10000;
178

179 180
#ifdef STATS
    /* Initialize statistics fields */
181 182
    p_vout->c_fps_samples       = 0;
#endif
183 184 185 186

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

187
    /* Initialize pictures and subpictures - translation tables and functions
188
     * will be initialized later in InitThread */
189 190
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
191 192
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
193 194 195
    }
    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
    {
196 197
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
198
    }
199
    p_vout->i_pictures = 0;
200

201 202
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
203
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
Michel Kaempf's avatar
Michel Kaempf committed
204
    {
205 206
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
207
    }
208 209 210 211 212 213
    intf_DbgMsg( "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
                 "masks: 0x%x/0x%x/0x%x\n",
                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
                 p_vout->i_red_mask, p_vout->i_green_mask,
                 p_vout->i_blue_mask );
Vincent Seguin's avatar
Vincent Seguin committed
214 215

    /* Calculate shifts from system-updated masks */
216 217 218 219 220 221
    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 );
Vincent Seguin's avatar
Vincent Seguin committed
222

223
    /* Set some useful colors */
224
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
225 226
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
227
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
228

229 230
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
231
    p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
232 233
    if( p_vout->p_default_font == NULL )
    {
234
        p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
235
    }
Vincent Seguin's avatar
Vincent Seguin committed
236 237
    if( p_vout->p_default_font == NULL )
    {
238
        intf_ErrMsg( "vout error: could not load default font\n" );
239
        p_vout->p_sys_destroy( p_vout );
240 241 242
        free( p_vout );
        return( NULL );
    }
243
    p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
244 245
    if( p_vout->p_large_font == NULL )
    {
246
        p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
247
    }
Vincent Seguin's avatar
Vincent Seguin committed
248 249
    if( p_vout->p_large_font == NULL )
    {
250
        intf_ErrMsg( "vout error: could not load large font\n" );
251
        vout_UnloadFont( p_vout->p_default_font );
252
        p_vout->p_sys_destroy( p_vout );
253 254 255
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
256

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

273 274 275
    intf_Msg( "Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
              p_vout->i_height, p_vout->i_screen_depth,
              p_vout->i_bytes_per_pixel * 8 );
276

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

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

    /* Set status */
306
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
307
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
308 309
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
310 311 312 313
    /* Request thread destruction */
    p_vout->b_die = 1;

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

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

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

    /* Remove reservation flag */
348
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
349 350

#ifdef DEBUG_VIDEO
Sam Hocevar's avatar
Sam Hocevar committed
351
    /* Send subpicture information */
352 353 354 355
    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
356 357 358
#endif
}

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

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

377 378
    /*
     * Look for an empty place
379
     */
380
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
381
    {
382 383 384 385 386 387
        if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
        {
            /* Subpicture is marked for destruction, but is still allocated */
            if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
                (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
            {
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
388 389 390 391
                /* Memory size do match or is smaller : memory will not be
                 * reallocated, and function can end immediately - this is
                 * the best possible case, since no memory allocation needs
                 * to be done */
392
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
393
#ifdef DEBUG_VIDEO
394 395
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
396
#endif
397 398 399 400 401 402 403 404 405 406
                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];
            }
        }
407
        else if( (p_free_subpic == NULL) &&
408 409
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
410
            /* Subpicture is empty and ready for allocation */
411 412 413 414
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

Sam Hocevar's avatar
Sam Hocevar committed
415
    /* If no free subpictures are available, use a destroyed subpicture */
416
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
417
    {
Sam Hocevar's avatar
Sam Hocevar committed
418
        /* No free subpicture or matching destroyed subpictures have been
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
419
         * found, but a destroyed subpicture is still avalaible */
420
        free( p_destroyed_subpic->p_data );
421 422 423 424 425 426 427 428 429 430 431
        p_free_subpic = p_destroyed_subpic;
    }

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

        if( p_free_subpic->p_data != NULL )
Sam Hocevar's avatar
Sam Hocevar committed
447 448
        {
            /* Copy subpicture information, set some default values */
449 450
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
451
            p_free_subpic->i_size                      = i_size;
452
            p_free_subpic->i_x                         = 0;
453
            p_free_subpic->i_y                         = 0;
454 455
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
456 457
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
458 459 460 461
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
462 463 464
            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
            p_free_subpic->i_status =  FREE_SUBPICTURE;
            p_free_subpic =            NULL;
Sam Hocevar's avatar
Sam Hocevar committed
465
            intf_ErrMsg("spu warning: %s\n", strerror( ENOMEM ) );
466
        }
467

468
#ifdef DEBUG_VIDEO
469
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
470 471 472 473
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
474

475
    /* No free or destroyed subpicture could be found */
Sam Hocevar's avatar
Sam Hocevar committed
476
    intf_DbgMsg( "warning: subpicture heap is full\n" );
477 478
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
479 480
}

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

500
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
501 502

#ifdef DEBUG_VIDEO
503
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
504 505 506
#endif
}

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

532
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
533
    intf_DbgMsg("picture %p\n", p_pic);
534
#endif
535
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
536 537
}

538
/*****************************************************************************
539
 * vout_DatePicture: date a picture
540
 *****************************************************************************
541
 * Remove the reservation flag of a picture, which will cause it to be ready for
542
 * display. The picture won't be displayed until vout_DisplayPicture has been
543
 * called.
544
 *****************************************************************************/
545 546
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
547
#ifdef DEBUG_VIDEO
548
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
549 550
#endif

551
    vlc_mutex_lock( &p_vout->picture_lock );
552
    p_pic->date = date;
553 554
    switch( p_pic->i_status )
    {
555
    case RESERVED_PICTURE:
556
        p_pic->i_status = RESERVED_DATED_PICTURE;
557
        break;
558 559
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
560
        break;
561
#ifdef DEBUG
562 563 564
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
565 566 567 568
#endif
    }

#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
569
    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
570
#endif
571
    vlc_mutex_unlock( &p_vout->picture_lock );
572 573
}

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

Vincent Seguin's avatar
Vincent Seguin committed
590
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
591
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
592

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

Vincent Seguin's avatar
Vincent Seguin committed
634 635
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
636
    {
637 638
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
639 640
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
641 642
    }

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

Vincent Seguin's avatar
Vincent Seguin committed
680
        if( p_free_picture->p_data != NULL )
681
        {
Sam Hocevar's avatar
Sam Hocevar committed
682
            /* Copy picture information, set some default values */
683 684
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
685
            p_free_picture->i_matrix_coefficients       = 1;
686 687
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
688
            p_free_picture->i_chroma_width              = i_chroma_width;
689
            p_free_picture->i_display_horizontal_offset = 0;
690
            p_free_picture->i_display_vertical_offset   = 0;
691 692
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
693 694
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
695
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
696 697 698 699
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
700 701 702
            p_free_picture->i_type   =  EMPTY_PICTURE;
            p_free_picture->i_status =  FREE_PICTURE;
            p_free_picture =            NULL;
Sam Hocevar's avatar
Sam Hocevar committed
703
            intf_ErrMsg("vout warning: %s\n", strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
704
        }
705

706
#ifdef DEBUG_VIDEO
707
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
708
#endif
Vincent Seguin's avatar
Vincent Seguin committed
709
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
710
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
711
    }
712

713
    /* No free or destroyed picture could be found */
Sam Hocevar's avatar
Sam Hocevar committed
714
    intf_DbgMsg( "warning: picture heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
715
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
716
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
717 718
}

719
/*****************************************************************************
720
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
721
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
722
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
723 724
 * 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 !
725
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
726
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
727
{
728
   vlc_mutex_lock( &p_vout->picture_lock );
729

730
#ifdef DEBUG
731
   /* Check if picture status is valid */
732
   if( (p_pic->i_status != RESERVED_PICTURE) &&
733 734
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
735
   {
736 737
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
738 739
#endif

740
   p_pic->i_status = DESTROYED_PICTURE;
741
   p_vout->i_pictures--;
742 743

#ifdef DEBUG_VIDEO
744
   intf_DbgMsg("picture %p\n", p_pic);
745
#endif
746
   vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
747 748
}

749
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
750
 * vout_LinkPicture: increment reference counter of a picture
751
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
752
 * This function increment the reference counter of a picture in the video
753
 * heap. It needs a lock since several producer threads can access the picture.
754
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
755 756
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
757
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
758
    p_pic->i_refcount++;
759 760

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

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

767
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
768
 * vout_UnlinkPicture: decrement reference counter of a picture
769
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
770
 * This function decrement the reference counter of a picture in the video heap.
771
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
772 773
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
774
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
775
    p_pic->i_refcount--;
776 777 778 779 780

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
781 782
        p_pic->i_refcount = 0;
    }
783 784
#endif

Vincent Seguin's avatar
Vincent Seguin committed
785
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
786
    {
787
        p_pic->i_status = DESTROYED_PICTURE;
788
        p_vout->i_pictures--;
Michel Kaempf's avatar
Michel Kaempf committed
789
    }
790 791

#ifdef DEBUG_VIDEO
792
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
793
#endif
794 795

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

798
/*****************************************************************************
799
 * vout_SetBuffers: set buffers adresses
800 801
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
802
 * adresses.
803
 *****************************************************************************/
804
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
<