video_output.c 83.1 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
    /* Initialize statistics fields */
180
    p_vout->c_fps_samples       = 0;
181 182 183 184

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

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

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

    /* Calculate shifts from system-updated masks */
214 215 216 217 218 219
    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
220

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

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

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

271 272 273
    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 );
274

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

498
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
499 500

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

738
   p_pic->i_status = DESTROYED_PICTURE;
739
   p_vout->i_pictures--;
740 741

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

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

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

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

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

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

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

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

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

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