video_output.c 83.3 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
 * thread, and destroy a previously oppened video output thread.
 *****************************************************************************
 * Copyright (C) 2000 VideoLAN
8
 * $Id: video_output.c,v 1.120 2001/04/25 20:54:07 massiot Exp $
9
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
10
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
11 12 13 14 15
 *
 * 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
16
 *
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22 23 24
 * 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.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

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

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

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

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

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

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

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

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

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

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135
#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
136

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

145 146 147
    /* 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
148
    p_vout->i_changes             = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
149 150 151 152 153
    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
154 155 156
    p_vout->i_screen_depth        = 15;
    p_vout->i_bytes_per_pixel     = 2;
    p_vout->f_gamma               = VOUT_GAMMA;
157
    p_vout->b_need_render         = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
158 159
    p_vout->b_YCbr                = 0;
    
Renaud Dartus's avatar
 
Renaud Dartus committed
160
    p_vout->b_grayscale           = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
Sam Hocevar's avatar
 
Sam Hocevar committed
161
                                                     VOUT_GRAYSCALE_DEFAULT );
Renaud Dartus's avatar
 
Renaud Dartus committed
162 163 164
    p_vout->b_info                = 0;
    p_vout->b_interface           = 0;
    p_vout->b_scale               = 1;
165

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

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

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

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

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

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

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

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

225 226
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
Sam Hocevar's avatar
 
Sam Hocevar committed
227
    p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
228 229
    if( p_vout->p_default_font == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
230
        intf_ErrMsg( "vout error: could not load default font" );
231
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
232 233

    p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
234 235
    if( p_vout->p_large_font == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
236
        intf_ErrMsg( "vout error: could not load large font" );
237
    }
Vincent Seguin's avatar
Vincent Seguin committed
238

Michel Kaempf's avatar
Michel Kaempf committed
239
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
240
    vlc_mutex_init( &p_vout->picture_lock );
241 242
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
243

244 245
    if( vlc_thread_create( &p_vout->thread_id, "video output",
                           (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
246
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
247
        intf_ErrMsg("vout error: %s", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
248
        vout_UnloadFont( p_vout->p_default_font );
249
        vout_UnloadFont( p_vout->p_large_font );
Sam Hocevar's avatar
 
Sam Hocevar committed
250
        p_vout->pf_destroy( p_vout );
Michel Kaempf's avatar
Michel Kaempf committed
251 252
        free( p_vout );
        return( NULL );
253
    }
Michel Kaempf's avatar
Michel Kaempf committed
254

Sam Hocevar's avatar
 
Sam Hocevar committed
255 256
    intf_Msg( "vout: video display initialized (%dx%d, %d/%d bpp)",
              p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
257
              p_vout->i_bytes_per_pixel * 8 );
258

Michel Kaempf's avatar
Michel Kaempf committed
259 260 261 262
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
263
        {
Michel Kaempf's avatar
Michel Kaempf committed
264
            msleep( THREAD_SLEEP );
265
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
266 267 268
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
269 270
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
271 272 273 274
    }
    return( p_vout );
}

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

    /* Set status */
Sam Hocevar's avatar
 
Sam Hocevar committed
288
    intf_DbgMsg("");
Michel Kaempf's avatar
Michel Kaempf committed
289
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
290 291
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
292 293 294 295
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
296
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
297 298 299 300
    {
        do
        {
            msleep( THREAD_SLEEP );
301 302
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
303 304 305
    }
}

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

#ifdef DEBUG
    /* Check if status is valid */
322
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
323
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
324
        intf_DbgMsg("error: subpicture %p has invalid status %d", p_subpic,
325 326
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
327 328 329
#endif

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

332
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
Sam Hocevar committed
333
    /* Send subpicture information */
Sam Hocevar's avatar
 
Sam Hocevar committed
334
    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s",
335 336 337
                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
338 339 340
#endif
}

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

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

359 360
    /*
     * Look for an empty place
361
     */
362
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
363
    {
364 365 366 367 368 369
        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
370 371 372 373
                /* 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 */
374
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
375
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
376
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)",
377
                            &p_vout->p_subpicture[i_subpic] );
378
#endif
379 380 381 382 383 384 385 386 387 388
                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];
            }
        }
389
        else if( (p_free_subpic == NULL) &&
390 391
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
392
            /* Subpicture is empty and ready for allocation */
393 394 395 396
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

Sam Hocevar's avatar
Sam Hocevar committed
397
    /* If no free subpictures are available, use a destroyed subpicture */
398
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
399
    {
Sam Hocevar's avatar
Sam Hocevar committed
400
        /* No free subpicture or matching destroyed subpictures have been
Sam Hocevar's avatar
NEWS:  
Sam Hocevar committed
401
         * found, but a destroyed subpicture is still avalaible */
402
        free( p_destroyed_subpic->p_data );
403 404 405 406 407 408 409 410 411 412 413
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
414 415
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
416
            break;
417 418 419
        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
            p_free_subpic->p_data = malloc( i_size );
            break;
420 421
#ifdef DEBUG
        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
422
            intf_DbgMsg("error: unknown subpicture type %d", i_type );
423 424 425
            p_free_subpic->p_data   =  NULL;
            break;
#endif
426 427 428
        }

        if( p_free_subpic->p_data != NULL )
Sam Hocevar's avatar
Sam Hocevar committed
429 430
        {
            /* Copy subpicture information, set some default values */
431 432
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
433
            p_free_subpic->i_size                      = i_size;
434
            p_free_subpic->i_x                         = 0;
435
            p_free_subpic->i_y                         = 0;
436 437
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
438 439
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
440 441 442 443
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
444 445 446
            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
447 448
            intf_ErrMsg( "vout error: spu allocation returned %s",
                         strerror( ENOMEM ) );
449
        }
450

451
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
452
        intf_DbgMsg("subpicture %p (in free subpicture slot)", p_free_subpic );
453 454 455 456
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
457

458
    /* No free or destroyed subpicture could be found */
Sam Hocevar's avatar
 
Sam Hocevar committed
459
    intf_DbgMsg( "warning: subpicture heap is full" );
460 461
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
462 463
}

464
/*****************************************************************************
465
 * vout_DestroySubPicture: remove a subpicture from the heap
466
 *****************************************************************************
467
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
468
 * It is meant to be used when the construction of a picture aborted.
469
 * This function does not need locking since reserved subpictures are ignored
470
 * by the output thread.
471
 *****************************************************************************/
472
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
473 474
{
#ifdef DEBUG
475 476
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
477
   {
Sam Hocevar's avatar
 
Sam Hocevar committed
478
       intf_DbgMsg("error: subpicture %p has invalid status %d",
479 480
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
481 482
#endif

483
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
484

485
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
486
    intf_DbgMsg("subpicture %p", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
487 488 489
#endif
}

490
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
491
 * vout_DisplayPicture: display a picture
492
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
493
 * Remove the reservation flag of a picture, which will cause it to be ready for
494
 * display. The picture won't be displayed until vout_DatePicture has been
495
 * called.
496
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
497
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
498
{
499 500
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
501
    {
502
    case RESERVED_PICTURE:
503
        p_pic->i_status = RESERVED_DISP_PICTURE;
504
        break;
505 506
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
507
        break;
508
#ifdef DEBUG
509
    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
510
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
511
        break;
512
#endif
513
    }
Michel Kaempf's avatar
Michel Kaempf committed
514

515
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
516
    intf_DbgMsg("picture %p", p_pic);
517
#endif
518
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
519 520
}

521
/*****************************************************************************
522
 * vout_DatePicture: date a picture
523
 *****************************************************************************
524
 * Remove the reservation flag of a picture, which will cause it to be ready for
525
 * display. The picture won't be displayed until vout_DisplayPicture has been
526
 * called.
527
 *****************************************************************************/
528 529
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
530
#ifdef DEBUG_VOUT
531
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
532 533
#endif

534
    vlc_mutex_lock( &p_vout->picture_lock );
535
    p_pic->date = date;
536 537
    switch( p_pic->i_status )
    {
538
    case RESERVED_PICTURE:
539
        p_pic->i_status = RESERVED_DATED_PICTURE;
540
        break;
541 542
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
543
        break;
544
#ifdef DEBUG
545
    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
546
        intf_DbgMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
547
        break;
548 549 550
#endif
    }

551
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
552
    intf_DbgMsg("picture %p, display date: %s", p_pic, mstrtime( psz_date, p_pic->date) );
553
#endif
554
    vlc_mutex_unlock( &p_vout->picture_lock );
555 556
}

557
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
558
 * vout_CreatePicture: allocate a picture in the video output heap.
559 560
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
561
 * A null pointer is returned if the function fails. This method provides an
562
 * already allocated zone of memory in the picture data fields. It needs locking
563 564 565
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
566
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
567
{
568 569 570 571
    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
572

Vincent Seguin's avatar
Vincent Seguin committed
573
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
574
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
575

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

Vincent Seguin's avatar
Vincent Seguin committed
617 618
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
619
    {
620 621
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
622 623
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
624 625
    }

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

Vincent Seguin's avatar
Vincent Seguin committed
663
        if( p_free_picture->p_data != NULL )
664
        {
Sam Hocevar's avatar
Sam Hocevar committed
665
            /* Copy picture information, set some default values */
666 667
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
668
            p_free_picture->i_matrix_coefficients       = 1;
669 670
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
671
            p_free_picture->i_chroma_width              = i_chroma_width;
672 673 674
            p_free_picture->i_size                      = i_width * i_height;
            p_free_picture->i_chroma_size               = i_chroma_width
                                                            * i_height;
675
            p_free_picture->i_display_horizontal_offset = 0;
676
            p_free_picture->i_display_vertical_offset   = 0;
677 678
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
679 680
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
681
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
682 683 684 685
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
686 687 688
            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
689 690
            intf_ErrMsg( "vout error: picture allocation returned %s",
                         strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
691
        }
692

693
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
694
        intf_DbgMsg("picture %p (in free picture slot)", p_free_picture );
695
#endif
Vincent Seguin's avatar
Vincent Seguin committed
696
        vlc_mutex_unlock( &p_vout->picture_lock );
Henri Fallon's avatar
 
Henri Fallon committed
697 698 699 700

        /* Initialize mutex */
        vlc_mutex_init( &(p_free_picture->lock_deccount) );
        
Vincent Seguin's avatar
Vincent Seguin committed
701
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
702
    }
703

704
    /* No free or destroyed picture could be found */
Sam Hocevar's avatar
 
Sam Hocevar committed
705
    intf_DbgMsg( "warning: picture heap is full" );
Vincent Seguin's avatar
Vincent Seguin committed
706
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
707
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
708 709
}

710
/*****************************************************************************
711
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
712
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
713
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
714 715
 * 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 !
716
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
717
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
718
{
Henri Fallon's avatar
 
Henri Fallon committed
719
    vlc_mutex_lock( &p_vout->picture_lock );
720

721
#ifdef DEBUG
Henri Fallon's avatar
 
Henri Fallon committed
722 723 724 725 726 727 728
    /* 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 );
    }
729 730
#endif

Henri Fallon's avatar
 
Henri Fallon committed
731 732
    p_pic->i_status = DESTROYED_PICTURE;
    p_vout->i_pictures--;
733

734
#ifdef DEBUG_VOUT
Henri Fallon's avatar
 
Henri Fallon committed
735
    intf_DbgMsg("picture %p", p_pic);
736
#endif
Henri Fallon's avatar
 
Henri Fallon committed
737

Henri Fallon's avatar
 
Henri Fallon committed
738 739
    /* destroy the lock that had been initialized in CreatePicture */
    vlc_mutex_destroy( &(p_pic->lock_deccount) );
Henri Fallon's avatar
 
Henri Fallon committed
740
   
Henri Fallon's avatar
 
Henri Fallon committed
741
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
742 743
}

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

755
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
756
    intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
757
#endif
758 759

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

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

772
#ifdef DEBUG_VOUT
773 774
    if( p_pic->i_refcount < 0 )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
775
        intf_DbgMsg("error: refcount < 0");
776 777
        p_pic->i_refcount = 0;
    }
778 779
#endif

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

786
#ifdef DEBUG_VOUT
Sam Hocevar's avatar
 
Sam Hocevar committed
787
    intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
788
#endif
789 790