video_output.c 80.5 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 15 16 17 18 19 20 21 22 23 24
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * 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-1307, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

27
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
28
 * Preamble
29
 *****************************************************************************/
30 31 32 33
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */
Vincent Seguin's avatar
Vincent Seguin committed
34

Michel Kaempf's avatar
Michel Kaempf committed
35 36 37
#include "common.h"
#include "config.h"
#include "mtime.h"
38
#include "threads.h"
39
#include "plugins.h"
Michel Kaempf's avatar
Michel Kaempf committed
40 41
#include "video.h"
#include "video_output.h"
Vincent Seguin's avatar
Vincent Seguin committed
42
#include "video_text.h"
43
#include "video_yuv.h"
44

Vincent Seguin's avatar
Vincent Seguin committed
45
#include "intf_msg.h"
46
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
47

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

79
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
80
 * vout_CreateThread: creates a new video output thread
81
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
82 83 84
 * 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.
85
 * If not, it will be updated using one of the THREAD_* constants.
86
 *****************************************************************************/
87 88
vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
                          int i_width, int i_height, int *pi_status, int i_method )
Michel Kaempf's avatar
Michel Kaempf committed
89
{
90 91 92
    vout_thread_t * p_vout;                             /* thread descriptor */
    int             i_status;                               /* thread status */
    int             i_index;               /* index for array initialization */
93
    char *          psz_method;
Michel Kaempf's avatar
Michel Kaempf committed
94

95
    /* Allocate descriptor */
96
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
97
    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
98
    if( p_vout == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
99
    {
100
        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
Michel Kaempf's avatar
Michel Kaempf committed
101 102
        return( NULL );
    }
103

104
    /* Request an interface plugin */
105
    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
106
    p_vout->p_vout_plugin = RequestPlugin( "vout", psz_method );
107

108
    if( !p_vout->p_vout_plugin )
109
    {
110
        intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
111 112
        free( p_vout );
        return( NULL );
113
    }
114 115

    /* Get plugins */
116 117 118 119 120 121
    p_vout->p_sys_create =  GetPluginFunction( p_vout->p_vout_plugin, "vout_SysCreate" );
    p_vout->p_sys_init =    GetPluginFunction( p_vout->p_vout_plugin, "vout_SysInit" );
    p_vout->p_sys_end =     GetPluginFunction( p_vout->p_vout_plugin, "vout_SysEnd" );
    p_vout->p_sys_destroy = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDestroy" );
    p_vout->p_sys_manage =  GetPluginFunction( p_vout->p_vout_plugin, "vout_SysManage" );
    p_vout->p_sys_display = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDisplay" );
122

123
    /* Initialize thread properties - thread id and locks will be initialized
124
     * later */
125
    p_vout->b_die               = 0;
126
    p_vout->b_error             = 0;
127 128
    p_vout->b_active            = 0;
    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
129
    *p_vout->pi_status          = THREAD_CREATE;
130

131
    /* Initialize some fields used by the system-dependant method - these fields will
132
     * probably be modified by the method, and are only preferences */
133
    p_vout->i_changes           = 0;
134 135
    p_vout->i_width             = i_width;
    p_vout->i_height            = i_height;
136
    p_vout->i_bytes_per_line    = i_width * 2;
137 138
    p_vout->i_screen_depth      = 15;
    p_vout->i_bytes_per_pixel   = 2;
139
    p_vout->f_gamma             = VOUT_GAMMA;
140

Vincent Seguin's avatar
Vincent Seguin committed
141
    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
142
    p_vout->b_info              = 0;
143 144
    p_vout->b_interface         = 0;
    p_vout->b_scale             = 0;
145 146

    p_vout->p_set_palette       = SetPalette;
147

Vincent Seguin's avatar
Vincent Seguin committed
148
    intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
149
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
150
                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
151

152 153
    /* Initialize idle screen */
    p_vout->last_display_date   = mdate();
154
    p_vout->last_display_date   = 0;
155 156
    p_vout->last_idle_date      = 0;

157 158
#ifdef STATS
    /* Initialize statistics fields */
159 160 161
    p_vout->render_time         = 0;
    p_vout->c_fps_samples       = 0;
#endif
162 163 164 165

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

166
    /* Initialize pictures and subpictures - translation tables and functions
167
     * will be initialized later in InitThread */
168 169
    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
    {
170 171 172 173
        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
174
    }
175
    p_vout->i_pictures = 0;
176 177 178

    /* Initialize synchronization informations */
    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
179

180 181
    /* Create and initialize system-dependant method - this function issues its
     * own error messages */
182
    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
Michel Kaempf's avatar
Michel Kaempf committed
183
    {
184
        TrashPlugin( p_vout->p_vout_plugin );
185 186
        free( p_vout );
        return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
187
    }
Vincent Seguin's avatar
Vincent Seguin committed
188
    intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
189
                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
Vincent Seguin's avatar
Vincent Seguin committed
190 191 192 193 194 195 196 197
                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 );

    /* Calculate shifts from system-updated masks */
    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 );

198
    /* Set some useful colors */
199
    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
Vincent Seguin's avatar
Vincent Seguin committed
200 201
    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
    p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
202
    p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
203

204 205
    /* Load fonts - fonts must be initialized after the system method since
     * they may be dependant on screen depth and other thread properties */
206
    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
207 208
    if( p_vout->p_default_font == NULL )
    {
209
        p_vout->p_sys_destroy( p_vout );
210
        TrashPlugin( p_vout->p_vout_plugin );
211 212 213 214
        free( p_vout );
        return( NULL );
    }
    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
Vincent Seguin's avatar
Vincent Seguin committed
215 216
    if( p_vout->p_large_font == NULL )
    {
217
        vout_UnloadFont( p_vout->p_default_font );
218
        p_vout->p_sys_destroy( p_vout );
219
        TrashPlugin( p_vout->p_vout_plugin );
220 221 222
        free( p_vout );
        return( NULL );
    }
Vincent Seguin's avatar
Vincent Seguin committed
223

Michel Kaempf's avatar
Michel Kaempf committed
224
    /* Create thread and set locks */
Vincent Seguin's avatar
Vincent Seguin committed
225
    vlc_mutex_init( &p_vout->picture_lock );
226 227 228
    vlc_mutex_init( &p_vout->subpicture_lock );
    vlc_mutex_init( &p_vout->change_lock );
    vlc_mutex_lock( &p_vout->change_lock );
Vincent Seguin's avatar
Vincent Seguin committed
229
    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
Michel Kaempf's avatar
Michel Kaempf committed
230
    {
231
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
Vincent Seguin's avatar
Vincent Seguin committed
232
        vout_UnloadFont( p_vout->p_default_font );
233
        vout_UnloadFont( p_vout->p_large_font );
234
        p_vout->p_sys_destroy( p_vout );
235
        TrashPlugin( p_vout->p_vout_plugin );
Michel Kaempf's avatar
Michel Kaempf committed
236 237
        free( p_vout );
        return( NULL );
238
    }
Michel Kaempf's avatar
Michel Kaempf committed
239

240
    intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
Vincent Seguin's avatar
Vincent Seguin committed
241
             p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
242

Michel Kaempf's avatar
Michel Kaempf committed
243 244 245 246
    /* If status is NULL, wait until the thread is created */
    if( pi_status == NULL )
    {
        do
247
        {
Michel Kaempf's avatar
Michel Kaempf committed
248
            msleep( THREAD_SLEEP );
249
        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
Michel Kaempf's avatar
Michel Kaempf committed
250 251 252
                && (i_status != THREAD_FATAL) );
        if( i_status != THREAD_READY )
        {
253 254
            return( NULL );
        }
Michel Kaempf's avatar
Michel Kaempf committed
255 256 257 258
    }
    return( p_vout );
}

259
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
260
 * vout_DestroyThread: destroys a previously created thread
261 262
 *****************************************************************************
 * Destroy a terminated thread.
Michel Kaempf's avatar
Michel Kaempf committed
263
 * The function will request a destruction of the specified thread. If pi_error
264
 * is NULL, it will return once the thread is destroyed. Else, it will be
Michel Kaempf's avatar
Michel Kaempf committed
265
 * update using one of the THREAD_* constants.
266
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
267
void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
268 269
{
    int     i_status;                                       /* thread status */
Michel Kaempf's avatar
Michel Kaempf committed
270 271

    /* Set status */
272
    intf_DbgMsg("\n");
Michel Kaempf's avatar
Michel Kaempf committed
273
    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
274 275
    *p_vout->pi_status = THREAD_DESTROY;

Michel Kaempf's avatar
Michel Kaempf committed
276 277 278 279
    /* Request thread destruction */
    p_vout->b_die = 1;

    /* If status is NULL, wait until thread has been destroyed */
280
    if( pi_status == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
281 282 283 284
    {
        do
        {
            msleep( THREAD_SLEEP );
285 286
        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
                && (i_status != THREAD_FATAL) );
Michel Kaempf's avatar
Michel Kaempf committed
287 288 289
    }
}

290
/*****************************************************************************
291
 * vout_DisplaySubPicture: display a subpicture unit
292 293
 *****************************************************************************
 * Remove the reservation flag of an subpicture, which will cause it to be ready
294
 * for display. The picture does not need to be locked, since it is ignored by
Vincent Seguin's avatar
Vincent Seguin committed
295
 * the output thread if is reserved.
296
 *****************************************************************************/
297
void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
298 299
{
#ifdef DEBUG_VIDEO
300 301
    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
302 303 304 305
#endif

#ifdef DEBUG
    /* Check if status is valid */
306
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
307
    {
308 309 310
        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
                    p_subpic->i_status );
    }
Vincent Seguin's avatar
Vincent Seguin committed
311 312 313
#endif

    /* Remove reservation flag */
314
    p_subpic->i_status = READY_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
315 316

#ifdef DEBUG_VIDEO
317
    /* Send subpicture informations */
318 319 320 321
    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
322 323 324
#endif
}

325
/*****************************************************************************
326
 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
327 328
 *****************************************************************************
 * This function create a reserved subpicture in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
329
 * A null pointer is returned if the function fails. This method provides an
330
 * already allocated zone of memory in the spu data fields. It needs locking
331 332 333
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
334
                                     int i_size )
Vincent Seguin's avatar
Vincent Seguin committed
335
{
336 337 338
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
339 340 341 342

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

343 344
    /*
     * Look for an empty place
345 346 347
     */
    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
    {
348 349 350 351 352 353 354
        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) )
            {
                /* Memory size do match or is smaller : memory will not be reallocated,
355
                 * and function can end immediately - this is the best possible case,
356
                 * since no memory allocation needs to be done */
357
                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
358
#ifdef DEBUG_VIDEO
359 360
                intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                            &p_vout->p_subpicture[i_subpic] );
361
#endif
362 363 364 365 366 367 368 369 370 371
                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];
            }
        }
372
        else if( (p_free_subpic == NULL) &&
373 374
                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
        {
375
            /* Subpicture is empty and ready for allocation */
376 377 378 379 380 381
            p_free_subpic = &p_vout->p_subpicture[i_subpic];
        }
    }

    /* If no free subpicture is available, use a destroyed subpicture */
    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
382
    {
383 384
        /* No free subpicture or matching destroyed subpicture has been found, but
         * a destroyed subpicture is still avalaible */
385
        free( p_destroyed_subpic->p_data );
386 387 388 389 390 391 392 393 394 395 396
        p_free_subpic = p_destroyed_subpic;
    }

    /*
     * Prepare subpicture
     */
    if( p_free_subpic != NULL )
    {
        /* Allocate memory */
        switch( i_type )
        {
397 398
        case TEXT_SUBPICTURE:                             /* text subpicture */
            p_free_subpic->p_data = malloc( i_size + 1 );
399 400 401 402
            break;
#ifdef DEBUG
        default:
            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
403 404 405
            p_free_subpic->p_data   =  NULL;
            break;
#endif
406 407 408 409 410 411
        }

        if( p_free_subpic->p_data != NULL )
        {                    /* Copy subpicture informations, set some default values */
            p_free_subpic->i_type                      = i_type;
            p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
412
            p_free_subpic->i_size                      = i_size;
413
            p_free_subpic->i_x                         = 0;
414
            p_free_subpic->i_y                         = 0;
415 416
            p_free_subpic->i_width                     = 0;
            p_free_subpic->i_height                    = 0;
417 418
            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
419 420 421 422
        }
        else
        {
            /* Memory allocation failed : set subpicture as empty */
423 424 425 426
            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
            p_free_subpic->i_status =  FREE_SUBPICTURE;
            p_free_subpic =            NULL;
            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
427
        }
428

429
#ifdef DEBUG_VIDEO
430
        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
431 432 433 434
#endif
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return( p_free_subpic );
    }
435

436 437 438 439
    /* No free or destroyed subpicture could be found */
    intf_DbgMsg( "warning: heap is full\n" );
    vlc_mutex_unlock( &p_vout->subpicture_lock );
    return( NULL );
Vincent Seguin's avatar
Vincent Seguin committed
440 441
}

442
/*****************************************************************************
443
 * vout_DestroySubPicture: remove a subpicture from the heap
444
 *****************************************************************************
445
 * This function frees a previously reserved subpicture.
Vincent Seguin's avatar
Vincent Seguin committed
446
 * It is meant to be used when the construction of a picture aborted.
447
 * This function does not need locking since reserved subpictures are ignored
448
 * by the output thread.
449
 *****************************************************************************/
450
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Vincent Seguin's avatar
Vincent Seguin committed
451 452
{
#ifdef DEBUG
453 454
   /* Check if status is valid */
   if( p_subpic->i_status != RESERVED_SUBPICTURE )
Vincent Seguin's avatar
Vincent Seguin committed
455
   {
456 457 458
       intf_DbgMsg("error: subpicture %p has invalid status %d\n",
                   p_subpic, p_subpic->i_status );
   }
Vincent Seguin's avatar
Vincent Seguin committed
459 460
#endif

461
    p_subpic->i_status = DESTROYED_SUBPICTURE;
Vincent Seguin's avatar
Vincent Seguin committed
462 463

#ifdef DEBUG_VIDEO
464
    intf_DbgMsg("subpicture %p\n", p_subpic);
Vincent Seguin's avatar
Vincent Seguin committed
465 466 467
#endif
}

468
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
469
 * vout_DisplayPicture: display a picture
470
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
471
 * Remove the reservation flag of a picture, which will cause it to be ready for
472
 * display. The picture won't be displayed until vout_DatePicture has been
473
 * called.
474
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
475
void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
476
{
477 478
    vlc_mutex_lock( &p_vout->picture_lock );
    switch( p_pic->i_status )
479
    {
480
    case RESERVED_PICTURE:
481
        p_pic->i_status = RESERVED_DISP_PICTURE;
482
        break;
483 484
    case RESERVED_DATED_PICTURE:
        p_pic->i_status = READY_PICTURE;
485
        break;
486
#ifdef DEBUG
487 488 489
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
490
#endif
491
    }
Michel Kaempf's avatar
Michel Kaempf committed
492

493
#ifdef DEBUG_VIDEO
Vincent Seguin's avatar
Vincent Seguin committed
494
    intf_DbgMsg("picture %p\n", p_pic);
495
#endif
496
    vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
497 498
}

499
/*****************************************************************************
500
 * vout_DatePicture: date a picture
501
 *****************************************************************************
502
 * Remove the reservation flag of a picture, which will cause it to be ready for
503
 * display. The picture won't be displayed until vout_DisplayPicture has been
504
 * called.
505
 *****************************************************************************/
506 507
void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
{
Vincent Seguin's avatar
Vincent Seguin committed
508
#ifdef DEBUG_VIDEO
509
    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
Vincent Seguin's avatar
Vincent Seguin committed
510 511
#endif

512
    vlc_mutex_lock( &p_vout->picture_lock );
513
    p_pic->date = date;
514 515
    switch( p_pic->i_status )
    {
516
    case RESERVED_PICTURE:
517
        p_pic->i_status = RESERVED_DATED_PICTURE;
518
        break;
519 520
    case RESERVED_DISP_PICTURE:
        p_pic->i_status = READY_PICTURE;
521
        break;
522
#ifdef DEBUG
523 524 525
    default:
        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
        break;
526 527 528 529
#endif
    }

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

535
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
536
 * vout_CreatePicture: allocate a picture in the video output heap.
537 538
 *****************************************************************************
 * This function create a reserved image in the video output heap.
Vincent Seguin's avatar
Vincent Seguin committed
539
 * A null pointer is returned if the function fails. This method provides an
540
 * already allocated zone of memory in the picture data fields. It needs locking
541 542 543
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
544
                               int i_width, int i_height )
Michel Kaempf's avatar
Michel Kaempf committed
545
{
546 547 548 549
    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
550

Vincent Seguin's avatar
Vincent Seguin committed
551
    /* Get lock */
Vincent Seguin's avatar
Vincent Seguin committed
552
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
553

554 555
    /*
     * Look for an empty place
Vincent Seguin's avatar
Vincent Seguin committed
556
     */
557
    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
Vincent Seguin's avatar
Vincent Seguin committed
558
    {
559 560 561
        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
        {
            /* Picture is marked for destruction, but is still allocated - note
562
             * that if width and type are the same for two pictures, chroma_width
563
             * should also be the same */
564 565 566 567 568
            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
569 570
                 * can end immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
571
                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
572
                p_vout->i_pictures++;
573
#ifdef DEBUG_VIDEO
574 575
                intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                            &p_vout->p_picture[i_picture] );
576
#endif
577 578 579 580 581 582 583 584 585 586
                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];
            }
        }
587
        else if( (p_free_picture == NULL) &&
Vincent Seguin's avatar
Vincent Seguin committed
588 589
                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
        {
590
            /* Picture is empty and ready for allocation */
591
            p_free_picture = &p_vout->p_picture[i_picture];
Vincent Seguin's avatar
Vincent Seguin committed
592
        }
Michel Kaempf's avatar
Michel Kaempf committed
593 594
    }

Vincent Seguin's avatar
Vincent Seguin committed
595 596
    /* If no free picture is available, use a destroyed picture */
    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
597
    {
598 599
        /* No free picture or matching destroyed picture has been found, but
         * a destroyed picture is still avalaible */
600 601
        free( p_destroyed_picture->p_data );
        p_free_picture = p_destroyed_picture;
Michel Kaempf's avatar
Michel Kaempf committed
602 603
    }

Vincent Seguin's avatar
Vincent Seguin committed
604 605 606 607 608 609 610
    /*
     * Prepare picture
     */
    if( p_free_picture != NULL )
    {
        /* Allocate memory */
        switch( i_type )
Michel Kaempf's avatar
Michel Kaempf committed
611
        {
612 613
        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
614
            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
615
            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
Vincent Seguin's avatar
Vincent Seguin committed
616 617
            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;
618
            break;
619 620
        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
            i_chroma_width = i_width / 2;
621 622
            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
623 624
            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;
625
            break;
626 627
        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
            i_chroma_width = i_width;
628 629
            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
630 631
            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;
632
            break;
Vincent Seguin's avatar
Vincent Seguin committed
633 634
#ifdef DEBUG
        default:
635
            intf_DbgMsg("error: unknown picture type %d\n", i_type );
636 637 638
            p_free_picture->p_data   =  NULL;
            break;
#endif
Michel Kaempf's avatar
Michel Kaempf committed
639 640
        }

Vincent Seguin's avatar
Vincent Seguin committed
641
        if( p_free_picture->p_data != NULL )
642
        {
643
            /* Copy picture informations, set some default values */
644 645
            p_free_picture->i_type                      = i_type;
            p_free_picture->i_status                    = RESERVED_PICTURE;
646
            p_free_picture->i_matrix_coefficients       = 1;
647 648
            p_free_picture->i_width                     = i_width;
            p_free_picture->i_height                    = i_height;
649
            p_free_picture->i_chroma_width              = i_chroma_width;
650
            p_free_picture->i_display_horizontal_offset = 0;
651
            p_free_picture->i_display_vertical_offset   = 0;
652 653
            p_free_picture->i_display_width             = i_width;
            p_free_picture->i_display_height            = i_height;
654 655
            p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
            p_free_picture->i_refcount                  = 0;
656
            p_vout->i_pictures++;
Vincent Seguin's avatar
Vincent Seguin committed
657 658 659 660
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
661 662 663 664
            p_free_picture->i_type   =  EMPTY_PICTURE;
            p_free_picture->i_status =  FREE_PICTURE;
            p_free_picture =            NULL;
            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
Vincent Seguin's avatar
Vincent Seguin committed
665
        }
666

667
#ifdef DEBUG_VIDEO
668
        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
669
#endif
Vincent Seguin's avatar
Vincent Seguin committed
670
        vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
671
        return( p_free_picture );
Michel Kaempf's avatar
Michel Kaempf committed
672
    }
673

674
    /* No free or destroyed picture could be found */
675
    intf_DbgMsg( "warning: heap is full\n" );
Vincent Seguin's avatar
Vincent Seguin committed
676
    vlc_mutex_unlock( &p_vout->picture_lock );
Vincent Seguin's avatar
Vincent Seguin committed
677
    return( NULL );
Michel Kaempf's avatar
Michel Kaempf committed
678 679
}

680
/*****************************************************************************
681
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
682
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
683
 * This function frees a previously reserved picture or a permanent
Vincent Seguin's avatar
Vincent Seguin committed
684 685
 * 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 !
686
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
687
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Michel Kaempf's avatar
Michel Kaempf committed
688
{
689
   vlc_mutex_lock( &p_vout->picture_lock );
690

691
#ifdef DEBUG
692
   /* Check if picture status is valid */
693
   if( (p_pic->i_status != RESERVED_PICTURE) &&
694 695
       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
       (p_pic->i_status != RESERVED_DISP_PICTURE) )
696
   {
697 698
       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
   }
699 700
#endif

701
   p_pic->i_status = DESTROYED_PICTURE;
702
   p_vout->i_pictures--;
703 704

#ifdef DEBUG_VIDEO
705
   intf_DbgMsg("picture %p\n", p_pic);
706
#endif
707
   vlc_mutex_unlock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
708 709
}

710
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
711
 * vout_LinkPicture: increment reference counter of a picture
712
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
713
 * This function increment the reference counter of a picture in the video
714
 * heap. It needs a lock since several producer threads can access the picture.
715
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
716 717
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
718
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
719
    p_pic->i_refcount++;
720 721

#ifdef DEBUG_VIDEO
722
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
723
#endif
724 725

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

728
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
729
 * vout_UnlinkPicture: decrement reference counter of a picture
730
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
731
 * This function decrement the reference counter of a picture in the video heap.
732
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
733 734
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
{
Vincent Seguin's avatar
Vincent Seguin committed
735
    vlc_mutex_lock( &p_vout->picture_lock );
Michel Kaempf's avatar
Michel Kaempf committed
736
    p_pic->i_refcount--;
737 738 739 740 741

#ifdef DEBUG_VIDEO
    if( p_pic->i_refcount < 0 )
    {
        intf_DbgMsg("error: refcount < 0\n");
742 743
        p_pic->i_refcount = 0;
    }
744 745
#endif

Vincent Seguin's avatar
Vincent Seguin committed
746
    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
Michel Kaempf's avatar
Michel Kaempf committed
747
    {
748
        p_pic->i_status = DESTROYED_PICTURE;
749
        p_vout->i_pictures--;
Michel Kaempf's avatar
Michel Kaempf committed
750
    }
751 752

#ifdef DEBUG_VIDEO
753
    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
754
#endif
755 756

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

759
/*****************************************************************************
760
 * vout_SetBuffers: set buffers adresses
761 762
 *****************************************************************************
 * This function is called by system drivers to set buffers video memory
763
 * adresses.
764
 *****************************************************************************/
765
void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
766 767
{
    /* No picture previously */
768 769 770 771 772 773 774 775
    p_vout->p_buffer[0].i_pic_x =         0;
    p_vout->p_buffer[0].i_pic_y =         0;
    p_vout->p_buffer[0].i_pic_width =     0;
    p_vout->p_buffer[0].i_pic_height =    0;
    p_vout->p_buffer[1].i_pic_x =         0;
    p_vout->p_buffer[1].i_pic_y =         0;
    p_vout->p_buffer[1].i_pic_width =     0;
    p_vout->p_buffer[1].i_pic_height =    0;
776 777

    /* The first area covers all the screen */
778 779 780 781 782 783 784 785
    p_vout->p_buffer[0].i_areas =                 1;
    p_vout->p_buffer[0].pi_area_begin[0] =        0;
    p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
    p_vout->p_buffer[1].i_areas =                 1;
    p_vout->p_buffer[1].pi_area_begin[0] =        0;
    p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;

    /* Set adresses */
786 787
    p_vout->p_buffer[0].p_data = p_buf1;
    p_vout->p_buffer[1].p_data = p_buf2;
788 789
}

Vincent Seguin's avatar
Vincent Seguin committed
790 791 792 793 794 795 796 797 798 799 800 801 802
/*****************************************************************************
 * vout_Pixel2RGB: return red, green and blue from pixel value
 *****************************************************************************
 * Return color values, in 0-255 range, of the decomposition of a pixel. This
 * is a slow routine and should only be used for initialization phase.
 *****************************************************************************/
void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
{
    *pi_red =   i_pixel & p_vout->i_red_mask;
    *pi_green = i_pixel & p_vout->i_green_mask;
    *pi_blue =  i_pixel & p_vout->i_blue_mask;
}

Michel Kaempf's avatar
Michel Kaempf committed
803 804
/* following functions are local */

Vincent Seguin's avatar
Vincent Seguin committed
805 806 807
/*****************************************************************************
 * BinaryLog: computes the base 2 log of a binary value
 *****************************************************************************
808
 * This functions is used by MaskToShift, to get a bit index from a binary
Vincent Seguin's avatar
Vincent Seguin committed
809 810 811 812 813 814 815
 * value.
 *****************************************************************************/
static int BinaryLog(u32 i)
{
    int i_log;

    i_log = 0;
816 817
    if (i & 0xffff0000)
    {
Vincent Seguin's avatar
Vincent Seguin committed
818
        i_log = 16;
819 820 821
    }
    if (i & 0xff00ff00)
    {