video_output.c 85.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
static void     RenderInfo        ( vout_thread_t *p_vout );
77
static void     Synchronize       ( vout_thread_t *p_vout, s64 i_delay );
78 79 80 81
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 );
82 83
static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
                                    u16 *green, u16 *blue, u16 *transp );
Michel Kaempf's avatar
Michel Kaempf committed
84

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

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

112
    p_vout->p_set_palette       = SetPalette;
113

114 115
    /* Get a suitable video plugin */
    for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
116
    {
117 118 119 120 121 122
        /* 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
123 124 125 126 127 128 129
                /* ... 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;
                }
130 131
            }
        }
132
    }
133

Sam Hocevar's avatar
Sam Hocevar committed
134 135 136 137 138 139 140 141 142 143
    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 );

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

152 153 154
    /* 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
155 156 157 158 159 160 161 162 163
    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
164
                                                       VOUT_GRAYSCALE_DEFAULT );
Renaud Dartus's avatar
 
Renaud Dartus committed
165 166 167 168
    p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockC;
    p_vout->b_info                = 0;
    p_vout->b_interface           = 0;
    p_vout->b_scale               = 1;
169

Sam Hocevar's avatar
Sam Hocevar committed
170 171 172
    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 );
173

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

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

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

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

Sam Hocevar's avatar
Sam Hocevar committed
202
    /* Initialize synchronization information */
203
    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
204

205 206
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
207
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
Michel Kaempf's avatar
Michel Kaempf committed
208
    {
209 210
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
211
    }
212 213 214 215 216 217
    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
218 219

    /* Calculate shifts from system-updated masks */
220 221 222 223 224 225
    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
226

227
    /* Set some useful colors */
228
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
229 230
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
231
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
232

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

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

277 278 279
    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 );
280

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

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

    /* Set status */
310
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
311
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
312 313
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
314 315 316 317
    /* Request thread destruction */
    p_vout->b_die = 1;

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

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

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

    /* Remove reservation flag */
352
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
353 354

#ifdef DEBUG_VIDEO
Sam Hocevar's avatar
Sam Hocevar committed
355
    /* Send subpicture information */
356 357 358 359
    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
360 361 362
#endif
}

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

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

381 382
    /*
     * Look for an empty place
383
     */
384
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
385
    {
386 387 388 389 390 391
        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
392 393 394 395
                /* 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 */
396
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
397
#ifdef DEBUG_VIDEO
398 399
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
400
#endif
401 402 403 404 405 406 407 408 409 410
                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];
            }
        }
411
        else if( (p_free_subpic == NULL) &&
412 413
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
414
            /* Subpicture is empty and ready for allocation */
415 416 417 418
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

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

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

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

472
#ifdef DEBUG_VIDEO
473
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
474 475 476 477
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
478

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

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

504
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
505 506

#ifdef DEBUG_VIDEO
507
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
508 509 510
#endif
}

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

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

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

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

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

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

Vincent Seguin's avatar
Vincent Seguin committed
594
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
595
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
596

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

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

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

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

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

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

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

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

744
   p_pic->i_status = DESTROYED_PICTURE;
745
   p_vout->i_pictures--;
746 747

#ifdef DEBUG_VIDEO
748
   intf_DbgMsg("picture %p\n", p_pic);
749
#endif
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_VIDEO
765
    intf_DbgMsg("picture %p refcount=%d\n", 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 782 783 784

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
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
    {
791
        p_pic->i_status = DESTROYED_PICTURE;
792
        p_vout->i_pictures--;
Michel Kaempf's avatar
Michel Kaempf committed
793
    }
794 795

#ifdef DEBUG_VIDEO
796
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
797
#endif
798 799

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

802
/*****************************************************************************
803
 * vout_SetBuffers: set buffers adresses
804