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

36 37 38 39
#ifdef STATS
#   include <sys/times.h>
#endif

Michel Kaempf's avatar
Michel Kaempf committed
40
#include "config.h"
41 42
#include "common.h"
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
43
#include "mtime.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
44 45
#include "modules.h"

Michel Kaempf's avatar
Michel Kaempf committed
46 47
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
48
#include "video_text.h"
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
49
#include "video_spu.h"
50
#include "video_yuv.h"
51

Vincent Seguin's avatar
Vincent Seguin committed
52
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
53

54
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
55

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

87
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
88
 * vout_CreateThread: creates a new video output thread
89
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
90 91 92
 * 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.
93
 * If not, it will be updated using one of the THREAD_* constants.
94
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
95
vout_thread_t * vout_CreateThread   ( int *pi_status )
Michel Kaempf's avatar
Michel Kaempf committed
96
{
97 98 99
    vout_thread_t * p_vout;                             /* thread descriptor */
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
Michel Kaempf's avatar
Michel Kaempf committed
100

101
    /* Allocate descriptor */
Michel Kaempf's avatar
Michel Kaempf committed
102
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
103
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
104
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
105 106
        intf_ErrMsg( "vout error: vout thread creation returned %s",
                     strerror(ENOMEM) );
Michel Kaempf's avatar
Michel Kaempf committed
107 108
        return( NULL );
    }
109

Sam Hocevar's avatar
 
Sam Hocevar committed
110 111 112
    /* Choose the best module */
    p_vout->p_module = module_Need( p_main->p_bank,
                                    MODULE_CAPABILITY_VOUT, NULL );
113

Sam Hocevar's avatar
 
Sam Hocevar committed
114
    if( p_vout->p_module == NULL )
Sam Hocevar's avatar
Sam Hocevar committed
115
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
116
        intf_ErrMsg( "vout error: no suitable vout module" );
Sam Hocevar's avatar
Sam Hocevar committed
117 118 119 120
        free( p_vout );
        return( NULL );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134
#define f p_vout->p_module->p_functions->vout.functions.vout
    p_vout->pf_create     = f.pf_create;
    p_vout->pf_init       = f.pf_init;
    p_vout->pf_end        = f.pf_end;
    p_vout->pf_destroy    = f.pf_destroy;
    p_vout->pf_manage     = f.pf_manage;
    p_vout->pf_display    = f.pf_display;
    p_vout->pf_setpalette = f.pf_setpalette;
#undef f

    if( p_vout->pf_setpalette == NULL )
    {
        p_vout->pf_setpalette = SetPalette;
    }
Sam Hocevar's avatar
Sam Hocevar committed
135

136
    /* Initialize thread properties - thread id and locks will be initialized
137
     * later */
138
    p_vout->b_die               = 0;
139
    p_vout->b_error             = 0;
140 141
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
142
    *p_vout->pi_status          = THREAD_CREATE;
143

144 145 146
    /* 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
147
    p_vout->i_changes             = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
148 149 150 151 152
    p_vout->i_width               = main_GetIntVariable( VOUT_WIDTH_VAR,
                                                         VOUT_WIDTH_DEFAULT );
    p_vout->i_height              = main_GetIntVariable( VOUT_HEIGHT_VAR,
                                                         VOUT_HEIGHT_DEFAULT );
    p_vout->i_bytes_per_line      = p_vout->i_width * 2;
Renaud Dartus's avatar
 
Renaud Dartus committed
153 154 155
    p_vout->i_screen_depth        = 15;
    p_vout->i_bytes_per_pixel     = 2;
    p_vout->f_gamma               = VOUT_GAMMA;
156
    p_vout->b_need_render         = 1;
Renaud Dartus's avatar
 
Renaud Dartus committed
157 158

    p_vout->b_grayscale           = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
Sam Hocevar's avatar
 
Sam Hocevar committed
159
                                                     VOUT_GRAYSCALE_DEFAULT );
Renaud Dartus's avatar
 
Renaud Dartus committed
160 161 162
    p_vout->b_info                = 0;
    p_vout->b_interface           = 0;
    p_vout->b_scale               = 1;
163

Sam Hocevar's avatar
 
Sam Hocevar committed
164
    intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)",
Sam Hocevar's avatar
Sam Hocevar committed
165 166
                 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 );
167

168
    /* Initialize idle screen */
169
    p_vout->last_display_date   = 0;
170
    p_vout->last_idle_date      = 0;
171
    p_vout->init_display_date   = mdate();
172
    p_vout->render_time         = 10000;
173

174
    /* Initialize statistics fields */
175
    p_vout->c_fps_samples       = 0;
176 177 178 179

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

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

194 195
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
Sam Hocevar's avatar
 
Sam Hocevar committed
196
    if( p_vout->pf_create( p_vout ) )
Michel Kaempf's avatar
Michel Kaempf committed
197
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
198
        module_Unneed( p_main->p_bank, p_vout->p_module );
199 200
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
201
    }
202
    intf_DbgMsg( "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
Sam Hocevar's avatar
 
Sam Hocevar committed
203
                 "masks: 0x%x/0x%x/0x%x",
204 205 206 207
                 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
208 209

    /* Calculate shifts from system-updated masks */
210 211 212 213 214 215
    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
216

217
    /* Set some useful colors */
218
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
219 220
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
221
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
222

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

Michel Kaempf's avatar
Michel Kaempf committed
251
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
252
    vlc_mutex_init( &p_vout->picture_lock );
253 254
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
255

256 257
    if( vlc_thread_create( &p_vout->thread_id, "video output",
                           (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
258
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
259
        intf_ErrMsg("vout error: %s", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
260
        vout_UnloadFont( p_vout->p_default_font );
261
        vout_UnloadFont( p_vout->p_large_font );
Sam Hocevar's avatar
 
Sam Hocevar committed
262
        p_vout->pf_destroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
263 264
        free( p_vout );
        return( NULL );
265
    }
Michel Kaempf's avatar
Michel Kaempf committed
266

Sam Hocevar's avatar
 
Sam Hocevar committed
267 268
    intf_Msg( "vout: video display initialized (%dx%d, %d/%d bpp)",
              p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
269
              p_vout->i_bytes_per_pixel * 8 );
270

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

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

    /* Set status */
Sam Hocevar's avatar
 
Sam Hocevar committed
300
    intf_DbgMsg("");
Michel Kaempf's avatar
Michel Kaempf committed
301
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
302 303
    *p_vout->pi_status = THREAD_DESTROY;

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

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

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

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

    /* Remove reservation flag */
342
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
343

344
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
Sam Hocevar committed
345
    /* Send subpicture information */
Sam Hocevar's avatar
 
Sam Hocevar committed
346
    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s",
347 348 349
                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
350 351 352
#endif
}

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

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

371 372
    /*
     * Look for an empty place
373
     */
374
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
375
    {
376 377 378 379 380 381
        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
382 383 384 385
                /* 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 */
386
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
387
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
388
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)",
389
                            &p_vout->p_subpicture[i_subpic] );
390
#endif
391 392 393 394 395 396 397 398 399 400
                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];
            }
        }
401
        else if( (p_free_subpic == NULL) &&
402 403
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
404
            /* Subpicture is empty and ready for allocation */
405 406 407 408
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

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

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

        if( p_free_subpic->p_data != NULL )
Sam Hocevar's avatar
Sam Hocevar committed
441 442
        {
            /* Copy subpicture information, set some default values */
443 444
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
445
            p_free_subpic->i_size                      = i_size;
446
            p_free_subpic->i_x                         = 0;
447
            p_free_subpic->i_y                         = 0;
448 449
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
450 451
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
452 453 454 455
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
456 457 458
            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
459 460
            intf_ErrMsg( "vout error: spu allocation returned %s",
                         strerror( ENOMEM ) );
461
        }
462

463
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
464
        intf_DbgMsg("subpicture %p (in free subpicture slot)", p_free_subpic );
465 466 467 468
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
469

470
    /* No free or destroyed subpicture could be found */
Sam Hocevar's avatar
 
Sam Hocevar committed
471
    intf_DbgMsg( "warning: subpicture heap is full" );
472 473
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
474 475
}

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

495
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
496

497
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
498
    intf_DbgMsg("subpicture %p", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
499 500 501
#endif
}

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

527
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
528
    intf_DbgMsg("picture %p", p_pic);
529
#endif
530
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
531 532
}

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

546
    vlc_mutex_lock( &p_vout->picture_lock );
547
    p_pic->date = date;
548 549
    switch( p_pic->i_status )
    {
550
    case RESERVED_PICTURE:
551
        p_pic->i_status = RESERVED_DATED_PICTURE;
552
        break;
553 554
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
555
        break;
556
#ifdef DEBUG
557
    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
558
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
559
        break;
560 561 562
#endif
    }

563
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
564
    intf_DbgMsg("picture %p, display date: %s", p_pic, mstrtime( psz_date, p_pic->date) );
565
#endif
566
    vlc_mutex_unlock( &p_vout->picture_lock );
567 568
}

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

Vincent Seguin's avatar
Vincent Seguin committed
585
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
586
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
587

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

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

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

Vincent Seguin's avatar
Vincent Seguin committed
675
        if( p_free_picture->p_data != NULL )
676
        {
Sam Hocevar's avatar
Sam Hocevar committed
677
            /* Copy picture information, set some default values */
678 679
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
680
            p_free_picture->i_matrix_coefficients       = 1;
681 682
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
683
            p_free_picture->i_chroma_width              = i_chroma_width;
684
            p_free_picture->i_display_horizontal_offset = 0;
685
            p_free_picture->i_display_vertical_offset   = 0;
686 687
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
688 689
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
690
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
691 692 693 694
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
695 696 697
            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
698 699
            intf_ErrMsg( "vout error: picture allocation returned %s",
                         strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
700
        }
701

702
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
703
        intf_DbgMsg("picture %p (in free picture slot)", p_free_picture );
704
#endif
Vincent Seguin's avatar
Vincent Seguin committed
705
        vlc_mutex_unlock( &p_vout->picture_lock );
Henri Fallon's avatar
 
Henri Fallon committed
706 707 708 709

        /* Initialize mutex */
        vlc_mutex_init( &(p_free_picture->lock_deccount) );
        
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" );
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
{
Henri Fallon's avatar
 
Henri Fallon committed
728
    vlc_mutex_lock( &p_vout->picture_lock );
729

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

Henri Fallon's avatar
 
Henri Fallon committed
740 741
    p_pic->i_status = DESTROYED_PICTURE;
    p_vout->i_pictures--;
742

743
#ifdef DEBUG_VOUT
Henri Fallon's avatar
 
Henri Fallon committed
744
    intf_DbgMsg("picture %p", p_pic);
745
#endif
Henri Fallon's avatar
 
Henri Fallon committed
746

Henri Fallon's avatar
 
Henri Fallon committed
747 748
    /* destroy the lock that had been initialized in CreatePicture */
    vlc_mutex_destroy( &(p_pic->lock_deccount) );
Henri Fallon's avatar
 
Henri Fallon committed
749
   
Henri Fallon's avatar
 
Henri Fallon committed
750
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
751 752
}

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

764
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
765
    intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
766
#endif
767 768

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

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

781
#ifdef DEBUG_VOUT
782 783
    if( p_pic->i_refcount < 0 )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
784
        intf_DbgMsg("error: refcount < 0");
785 786
        p_pic->i_refcount = 0;
    }
787 788
#endif

Vincent Seguin's avatar
Vincent Seguin committed
789
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
790
    {