vout_pictures.c 22.8 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4
/*****************************************************************************
 * vout_pictures.c : picture management functions
 *****************************************************************************
 * Copyright (C) 2000 VideoLAN
5
 * $Id: vout_pictures.c,v 1.27 2002/06/01 12:32:02 sam Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *
 * 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, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */

33
#include <vlc/vlc.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
34 35 36 37

#include "video.h"
#include "video_output.h"

38 39 40
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
41
static void vout_CopyPicture( vout_thread_t *, picture_t *, picture_t * );
42

Sam Hocevar's avatar
 
Sam Hocevar committed
43 44 45 46 47 48 49
/*****************************************************************************
 * vout_DisplayPicture: display a picture
 *****************************************************************************
 * Remove the reservation flag of a picture, which will cause it to be ready for
 * display. The picture won't be displayed until vout_DatePicture has been
 * called.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
50
void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
51 52
{
    vlc_mutex_lock( &p_vout->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
53
    switch( p_pic->i_status )
Sam Hocevar's avatar
 
Sam Hocevar committed
54 55
    {
    case RESERVED_PICTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
56
        p_pic->i_status = RESERVED_DISP_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
57 58
        break;
    case RESERVED_DATED_PICTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
59
        p_pic->i_status = READY_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
60 61
        break;
    default:
62 63
        msg_Err( p_vout, "picture to display %p has invalid status %d",
                         p_pic, p_pic->i_status );
Sam Hocevar's avatar
 
Sam Hocevar committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
        break;
    }

    vlc_mutex_unlock( &p_vout->picture_lock );
}

/*****************************************************************************
 * vout_DatePicture: date a picture
 *****************************************************************************
 * Remove the reservation flag of a picture, which will cause it to be ready
 * for display. The picture won't be displayed until vout_DisplayPicture has
 * been called.
 *****************************************************************************/
void vout_DatePicture( vout_thread_t *p_vout,
Sam Hocevar's avatar
 
Sam Hocevar committed
78
                       picture_t *p_pic, mtime_t date )
Sam Hocevar's avatar
 
Sam Hocevar committed
79 80
{
    vlc_mutex_lock( &p_vout->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
81 82
    p_pic->date = date;
    switch( p_pic->i_status )
Sam Hocevar's avatar
 
Sam Hocevar committed
83 84
    {
    case RESERVED_PICTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
85
        p_pic->i_status = RESERVED_DATED_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
86 87
        break;
    case RESERVED_DISP_PICTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
88
        p_pic->i_status = READY_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
89 90
        break;
    default:
91 92
        msg_Err( p_vout, "picture to date %p has invalid status %d",
                         p_pic, p_pic->i_status );
Sam Hocevar's avatar
 
Sam Hocevar committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106
        break;
    }

    vlc_mutex_unlock( &p_vout->picture_lock );
}

/*****************************************************************************
 * vout_CreatePicture: allocate a picture in the video output heap.
 *****************************************************************************
 * This function creates a reserved image in the video output heap.
 * A null pointer is returned if the function fails. This method provides an
 * already allocated zone of memory in the picture data fields. It needs locking
 * since several pictures can be created by several producers threads.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
107
picture_t *vout_CreatePicture( vout_thread_t *p_vout,
108 109 110
                               vlc_bool_t b_progressive,
                               vlc_bool_t b_top_field_first,
                               vlc_bool_t b_repeat_first_field )
Sam Hocevar's avatar
 
Sam Hocevar committed
111
{
Sam Hocevar's avatar
 
Sam Hocevar committed
112 113 114
    int         i_pic;                                      /* picture index */
    picture_t * p_pic;
    picture_t * p_freepic = NULL;                      /* first free picture */
Sam Hocevar's avatar
 
Sam Hocevar committed
115 116 117 118 119

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

    /*
Sam Hocevar's avatar
 
Sam Hocevar committed
120
     * Look for an empty place. We start at 1 because the first
Sam Hocevar's avatar
 
Sam Hocevar committed
121 122
     * directbuffer is reserved for memcpy()ed pictures.
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
123
    for( i_pic = 0; i_pic < I_RENDERPICTURES && p_freepic == NULL; i_pic++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
124
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
125
        p_pic = PP_RENDERPICTURE[ i_pic ];
Sam Hocevar's avatar
 
Sam Hocevar committed
126

Sam Hocevar's avatar
 
Sam Hocevar committed
127 128 129 130 131
        /* If the picture we found is a memory buffer, and we have enough
         * pictures in the stack, and we might have enough room later for
         * a direct buffer, skip it. If no other pictures are found, the
         * video decoder will try again later. */
        if( p_vout->b_direct && ( p_vout->output.i_pictures > 5 )
Sam Hocevar's avatar
 
Sam Hocevar committed
132
             && ( p_pic->i_type != DIRECT_PICTURE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
133 134 135 136
        {
            break;
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
137
        switch( p_pic->i_status )
Sam Hocevar's avatar
 
Sam Hocevar committed
138
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
139 140 141 142
            case DESTROYED_PICTURE:
                /* Memory will not be reallocated, and function can end
                 * immediately - this is the best possible case, since no
                 * memory allocation needs to be done */
Sam Hocevar's avatar
 
Sam Hocevar committed
143 144
                p_pic->i_status   = RESERVED_PICTURE;
                p_pic->i_refcount = 0;
145
                p_pic->b_force    = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
146

Sam Hocevar's avatar
 
Sam Hocevar committed
147 148 149
                p_pic->b_progressive        = b_progressive;
                p_pic->b_repeat_first_field = b_repeat_first_field;
                p_pic->b_top_field_first    = b_top_field_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
150

Sam Hocevar's avatar
 
Sam Hocevar committed
151
                p_vout->i_heap_size++;
Sam Hocevar's avatar
 
Sam Hocevar committed
152
                vlc_mutex_unlock( &p_vout->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
153
                return( p_pic );
Sam Hocevar's avatar
 
Sam Hocevar committed
154

Sam Hocevar's avatar
 
Sam Hocevar committed
155
            case FREE_PICTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
156
                /* Picture is empty and ready for allocation */
Sam Hocevar's avatar
 
Sam Hocevar committed
157
                p_freepic = p_pic;
Sam Hocevar's avatar
 
Sam Hocevar committed
158 159 160 161 162
                break;

            default:
                break;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
163 164 165 166 167
    }

    /*
     * Prepare picture
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
168
    if( p_freepic != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
169
    {
170
        vout_AllocatePicture( p_vout, p_freepic,
Sam Hocevar's avatar
 
Sam Hocevar committed
171 172
                              p_vout->render.i_width, p_vout->render.i_height,
                              p_vout->render.i_chroma );
Sam Hocevar's avatar
 
Sam Hocevar committed
173

Sam Hocevar's avatar
 
Sam Hocevar committed
174
        if( p_freepic->i_planes )
Sam Hocevar's avatar
 
Sam Hocevar committed
175 176
        {
            /* Copy picture information, set some default values */
Sam Hocevar's avatar
 
Sam Hocevar committed
177 178
            p_freepic->i_status   = RESERVED_PICTURE;
            p_freepic->i_type     = MEMORY_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
179

Sam Hocevar's avatar
 
Sam Hocevar committed
180
            p_freepic->i_refcount = 0;
181
            p_freepic->b_force = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
182

Sam Hocevar's avatar
 
Sam Hocevar committed
183 184 185
            p_freepic->b_progressive        = b_progressive;
            p_freepic->b_repeat_first_field = b_repeat_first_field;
            p_freepic->b_top_field_first    = b_top_field_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
186

Sam Hocevar's avatar
 
Sam Hocevar committed
187
            p_freepic->i_matrix_coefficients = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
188

Sam Hocevar's avatar
 
Sam Hocevar committed
189
            p_vout->i_heap_size++;
Sam Hocevar's avatar
 
Sam Hocevar committed
190 191 192 193
        }
        else
        {
            /* Memory allocation failed : set picture as empty */
Sam Hocevar's avatar
 
Sam Hocevar committed
194 195
            p_freepic->i_status = FREE_PICTURE;
            p_freepic = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
196

197
            msg_Err( p_vout, "picture allocation failed" );
Sam Hocevar's avatar
 
Sam Hocevar committed
198 199 200 201
        }

        vlc_mutex_unlock( &p_vout->picture_lock );

Sam Hocevar's avatar
 
Sam Hocevar committed
202
        return( p_freepic );
Sam Hocevar's avatar
 
Sam Hocevar committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }

    /* No free or destroyed picture could be found, but the decoder
     * will try again in a while. */
    vlc_mutex_unlock( &p_vout->picture_lock );

    return( NULL );
}

/*****************************************************************************
 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
 *****************************************************************************
 * This function frees a previously reserved picture or a permanent
 * 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 !
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
219
void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
220 221 222 223 224
{
    vlc_mutex_lock( &p_vout->picture_lock );

#ifdef DEBUG
    /* Check if picture status is valid */
Sam Hocevar's avatar
 
Sam Hocevar committed
225 226 227
    if( (p_pic->i_status != RESERVED_PICTURE) &&
        (p_pic->i_status != RESERVED_DATED_PICTURE) &&
        (p_pic->i_status != RESERVED_DISP_PICTURE) )
Sam Hocevar's avatar
 
Sam Hocevar committed
228
    {
229 230
        msg_Err( p_vout, "picture to destroy %p has invalid status %d",
                         p_pic, p_pic->i_status );
Sam Hocevar's avatar
 
Sam Hocevar committed
231 232 233
    }
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
234
    p_pic->i_status = DESTROYED_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
235
    p_vout->i_heap_size--;
Sam Hocevar's avatar
 
Sam Hocevar committed
236 237 238 239 240 241 242 243 244 245

    vlc_mutex_unlock( &p_vout->picture_lock );
}

/*****************************************************************************
 * vout_LinkPicture: increment reference counter of a picture
 *****************************************************************************
 * This function increments the reference counter of a picture in the video
 * heap. It needs a lock since several producer threads can access the picture.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
246
void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
247 248
{
    vlc_mutex_lock( &p_vout->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
249
    p_pic->i_refcount++;
Sam Hocevar's avatar
 
Sam Hocevar committed
250 251 252 253 254 255 256 257
    vlc_mutex_unlock( &p_vout->picture_lock );
}

/*****************************************************************************
 * vout_UnlinkPicture: decrement reference counter of a picture
 *****************************************************************************
 * This function decrement the reference counter of a picture in the video heap.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
258
void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
259 260
{
    vlc_mutex_lock( &p_vout->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
261
    p_pic->i_refcount--;
Sam Hocevar's avatar
 
Sam Hocevar committed
262

Sam Hocevar's avatar
 
Sam Hocevar committed
263
    if( p_pic->i_refcount < 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
264
    {
265
        msg_Err( p_vout, "picture refcount is %i", p_pic->i_refcount );
Sam Hocevar's avatar
 
Sam Hocevar committed
266
        p_pic->i_refcount = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
267 268
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
269 270
    if( ( p_pic->i_refcount == 0 ) &&
        ( p_pic->i_status == DISPLAYED_PICTURE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
271
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
272
        p_pic->i_status = DESTROYED_PICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
273
        p_vout->i_heap_size--;
Sam Hocevar's avatar
 
Sam Hocevar committed
274 275 276 277 278 279 280 281 282 283 284 285
    }

    vlc_mutex_unlock( &p_vout->picture_lock );
}

/*****************************************************************************
 * vout_RenderPicture: render a picture
 *****************************************************************************
 * This function chooses whether the current picture needs to be copied
 * before rendering, does the subpicture magic, and tells the video output
 * thread which direct buffer needs to be displayed.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
286
picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
Sam Hocevar's avatar
 
Sam Hocevar committed
287 288
                                                       subpicture_t *p_subpic )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
289
    if( p_pic == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
290 291 292 293 294 295
    {
        /* XXX: subtitles */

        return NULL;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
296
    if( p_pic->i_type == DIRECT_PICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
297
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
298
        if( p_pic->i_refcount )
Sam Hocevar's avatar
 
Sam Hocevar committed
299 300 301 302 303 304 305 306 307
        {
            /* Picture is in a direct buffer and is still in use,
             * we need to copy it to another direct buffer before
             * displaying it if there are subtitles. */
            if( p_subpic != NULL )
            {
                /* We have subtitles. First copy the picture to
                 * the spare direct buffer, then render the
                 * subtitles. */
308
                vout_CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
Sam Hocevar's avatar
 
Sam Hocevar committed
309

Sam Hocevar's avatar
 
Sam Hocevar committed
310
                vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
Sam Hocevar's avatar
 
Sam Hocevar committed
311

Sam Hocevar's avatar
 
Sam Hocevar committed
312
                return PP_OUTPUTPICTURE[0];
Sam Hocevar's avatar
 
Sam Hocevar committed
313 314 315 316 317
            }

            /* No subtitles, picture is in a directbuffer so
             * we can display it directly even if it is still
             * in use. */
Sam Hocevar's avatar
 
Sam Hocevar committed
318
            return p_pic;
Sam Hocevar's avatar
 
Sam Hocevar committed
319 320 321 322 323
        }

        /* Picture is in a direct buffer but isn't used by the
         * decoder. We can safely render subtitles on it and
         * display it. */
Sam Hocevar's avatar
 
Sam Hocevar committed
324
        vout_RenderSubPictures( p_vout, p_pic, p_subpic );
Sam Hocevar's avatar
 
Sam Hocevar committed
325

Sam Hocevar's avatar
 
Sam Hocevar committed
326
        return p_pic;
Sam Hocevar's avatar
 
Sam Hocevar committed
327 328 329 330
    }

    /* Not a direct buffer. We either need to copy it to a direct buffer,
     * or render it if the chroma isn't the same. */
Sam Hocevar's avatar
 
Sam Hocevar committed
331
    if( p_vout->b_direct )
Sam Hocevar's avatar
 
Sam Hocevar committed
332 333 334 335
    {
        /* Picture is not in a direct buffer, but is exactly the
         * same size as the direct buffers. A memcpy() is enough,
         * then render the subtitles. */
336
        vout_CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
Sam Hocevar's avatar
 
Sam Hocevar committed
337

Sam Hocevar's avatar
 
Sam Hocevar committed
338
        vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
Sam Hocevar's avatar
 
Sam Hocevar committed
339

Sam Hocevar's avatar
 
Sam Hocevar committed
340
        return PP_OUTPUTPICTURE[0];
Sam Hocevar's avatar
 
Sam Hocevar committed
341 342 343 344
    }

    /* Picture is not in a direct buffer, and needs to be converted to
     * another size/chroma. Then the subtitles need to be rendered as
Sam Hocevar's avatar
 
Sam Hocevar committed
345
     * well. This usually means software YUV, or hardware YUV with a
Sam Hocevar's avatar
 
Sam Hocevar committed
346 347
     * different chroma. */

Sam Hocevar's avatar
 
Sam Hocevar committed
348
    /* Convert image to the first direct buffer */
Sam Hocevar's avatar
 
Sam Hocevar committed
349
    p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
Sam Hocevar's avatar
 
Sam Hocevar committed
350

Sam Hocevar's avatar
 
Sam Hocevar committed
351 352 353
    /* Render subpictures on the first direct buffer */
    vout_RenderSubPictures( p_vout, &p_vout->p_picture[0], p_subpic );

Sam Hocevar's avatar
 
Sam Hocevar committed
354 355 356
    return &p_vout->p_picture[0];
}

Sam Hocevar's avatar
 
Sam Hocevar committed
357 358 359 360 361 362 363 364 365
/*****************************************************************************
 * vout_PlacePicture: calculate image window coordinates
 *****************************************************************************
 * This function will be accessed by plugins. It calculates the relative
 * position of the output window and the image window.
 *****************************************************************************/
void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
                        int *pi_x, int *pi_y, int *pi_width, int *pi_height )
{
gbazin's avatar
 
gbazin committed
366
    if( (i_width <= 0) || (i_height <=0) )
gbazin's avatar
 
gbazin committed
367 368 369
    {
        *pi_width = *pi_height = *pi_x = *pi_y = 0;

gbazin's avatar
 
gbazin committed
370
        return;
gbazin's avatar
 
gbazin committed
371
    }
gbazin's avatar
 
gbazin committed
372

Sam Hocevar's avatar
 
Sam Hocevar committed
373 374 375 376 377 378 379
    if( p_vout->b_scale )
    {
        *pi_width = i_width;
        *pi_height = i_height;
    }
    else
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
380 381
        *pi_width = __MIN( i_width, p_vout->render.i_width );
        *pi_height = __MIN( i_height, p_vout->render.i_height );
Sam Hocevar's avatar
 
Sam Hocevar committed
382 383
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
384
    if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
Sam Hocevar's avatar
 
Sam Hocevar committed
385
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
386
        *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
Sam Hocevar's avatar
 
Sam Hocevar committed
387 388 389
    }
    else
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
390
        *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
Sam Hocevar's avatar
 
Sam Hocevar committed
391 392 393 394 395
    }

    if( *pi_width > i_width )
    {
        *pi_width = i_width;
Sam Hocevar's avatar
 
Sam Hocevar committed
396
        *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
Sam Hocevar's avatar
 
Sam Hocevar committed
397 398 399 400 401
    }

    if( *pi_height > i_height )
    {
        *pi_height = i_height;
Sam Hocevar's avatar
 
Sam Hocevar committed
402
        *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
Sam Hocevar's avatar
 
Sam Hocevar committed
403 404 405 406 407 408
    }

    *pi_x = ( i_width - *pi_width ) / 2;
    *pi_y = ( i_height - *pi_height ) / 2;
}

Sam Hocevar's avatar
 
Sam Hocevar committed
409
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
410
 * vout_AllocatePicture: allocate a new picture in the heap.
Sam Hocevar's avatar
 
Sam Hocevar committed
411 412 413 414 415
 *****************************************************************************
 * This function allocates a fake direct buffer in memory, which can be
 * used exactly like a video buffer. The video output thread then manages
 * how it gets displayed.
 *****************************************************************************/
416
void vout_AllocatePicture( vout_thread_t *p_vout, picture_t *p_pic,
Sam Hocevar's avatar
 
Sam Hocevar committed
417
                           int i_width, int i_height, u32 i_chroma )
Sam Hocevar's avatar
 
Sam Hocevar committed
418
{
Sam Hocevar's avatar
 
Sam Hocevar committed
419
    int i_bytes, i_index;
Sam Hocevar's avatar
 
Sam Hocevar committed
420

Sam Hocevar's avatar
 
Sam Hocevar committed
421 422 423 424 425 426 427 428
    /* Store default values */
    for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
    {
        p_pic->p[i_index].p_pixels = NULL;
        p_pic->p[i_index].b_margin = 0;
        p_pic->p[i_index].i_pixel_bytes = 1;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
429
    /* Calculate coordinates */
Sam Hocevar's avatar
 
Sam Hocevar committed
430
    switch( i_chroma )
Sam Hocevar's avatar
 
Sam Hocevar committed
431
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
432
        case FOURCC_YV12:
Sam Hocevar's avatar
 
Sam Hocevar committed
433 434
        case FOURCC_I420:
        case FOURCC_IYUV:
Sam Hocevar's avatar
 
Sam Hocevar committed
435 436
            p_pic->p[ Y_PLANE ].i_lines = i_height;
            p_pic->p[ Y_PLANE ].i_pitch = i_width;
437
            p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
438 439
            p_pic->p[ U_PLANE ].i_lines = i_height / 2;
            p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
440
            p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
441 442
            p_pic->p[ V_PLANE ].i_lines = i_height / 2;
            p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
443
            p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
444
            p_pic->i_planes = 3;
Sam Hocevar's avatar
 
Sam Hocevar committed
445 446
            break;

Sam Hocevar's avatar
 
Sam Hocevar committed
447 448 449
        case FOURCC_I422:
            p_pic->p[ Y_PLANE ].i_lines = i_height;
            p_pic->p[ Y_PLANE ].i_pitch = i_width;
450
            p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
451 452
            p_pic->p[ U_PLANE ].i_lines = i_height;
            p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
453
            p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
454 455
            p_pic->p[ V_PLANE ].i_lines = i_height;
            p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
456
            p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
457
            p_pic->i_planes = 3;
Sam Hocevar's avatar
 
Sam Hocevar committed
458 459
            break;

Sam Hocevar's avatar
 
Sam Hocevar committed
460 461 462
        case FOURCC_I444:
            p_pic->p[ Y_PLANE ].i_lines = i_height;
            p_pic->p[ Y_PLANE ].i_pitch = i_width;
463
            p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
464 465
            p_pic->p[ U_PLANE ].i_lines = i_height;
            p_pic->p[ U_PLANE ].i_pitch = i_width;
466
            p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
467 468
            p_pic->p[ V_PLANE ].i_lines = i_height;
            p_pic->p[ V_PLANE ].i_pitch = i_width;
469
            p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
470 471 472 473 474 475
            p_pic->i_planes = 3;
            break;

        case FOURCC_Y211:
            p_pic->p->i_lines = i_height;
            p_pic->p->i_pitch = i_width;
476
            p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
477
            p_pic->p->i_pixel_bytes = 4;
Sam Hocevar's avatar
 
Sam Hocevar committed
478 479 480
            p_pic->i_planes = 1;
            break;

481 482 483 484 485 486 487 488
        case FOURCC_YUY2:
            p_pic->p->i_lines = i_height;
            p_pic->p->i_pitch = i_width * 2;
            p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
            p_pic->p->i_pixel_bytes = 4;
            p_pic->i_planes = 1;
            break;

Sam Hocevar's avatar
 
Sam Hocevar committed
489 490 491
        case FOURCC_RV15:
            p_pic->p->i_lines = i_height;
            p_pic->p->i_pitch = i_width * 2;
492
            p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
493
            p_pic->p->i_pixel_bytes = 2;
494
/* FIXME: p_heap isn't always reachable
Sam Hocevar's avatar
 
Sam Hocevar committed
495 496
            p_pic->p_heap->i_rmask = 0x001f;
            p_pic->p_heap->i_gmask = 0x03e0;
497
            p_pic->p_heap->i_bmask = 0x7c00; */
Sam Hocevar's avatar
 
Sam Hocevar committed
498 499 500 501 502 503
            p_pic->i_planes = 1;
            break;

        case FOURCC_RV16:
            p_pic->p->i_lines = i_height;
            p_pic->p->i_pitch = i_width * 2;
504
            p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
505
            p_pic->p->i_pixel_bytes = 2;
506
/* FIXME: p_heap isn't always reachable
Sam Hocevar's avatar
 
Sam Hocevar committed
507 508
            p_pic->p_heap->i_rmask = 0x001f;
            p_pic->p_heap->i_gmask = 0x07e0;
509
            p_pic->p_heap->i_bmask = 0xf800; */
Sam Hocevar's avatar
 
Sam Hocevar committed
510
            p_pic->i_planes = 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
511 512
            break;

Sam Hocevar's avatar
 
Sam Hocevar committed
513 514 515
        case FOURCC_RV32:
            p_pic->p->i_lines = i_height;
            p_pic->p->i_pitch = i_width * 4;
516
            p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
517
            p_pic->p->i_pixel_bytes = 4;
518
/* FIXME: p_heap isn't always reachable
Sam Hocevar's avatar
 
Sam Hocevar committed
519 520
            p_pic->p_heap->i_rmask = 0xff0000;
            p_pic->p_heap->i_gmask = 0x00ff00;
521
            p_pic->p_heap->i_bmask = 0x0000ff; */
Sam Hocevar's avatar
 
Sam Hocevar committed
522 523 524
            p_pic->i_planes = 1;
            break;

Sam Hocevar's avatar
 
Sam Hocevar committed
525
        default:
526 527
            msg_Err( p_vout, "unknown chroma type 0x%.8x (%4.4s)",
                             i_chroma, (char*)&i_chroma );
Sam Hocevar's avatar
 
Sam Hocevar committed
528
            p_pic->i_planes = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
529 530 531
            return;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
532
    /* Calculate how big the new image should be */
Sam Hocevar's avatar
 
Sam Hocevar committed
533
    for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
534
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
535
        i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
536
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
537

538
    p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
Sam Hocevar's avatar
 
Sam Hocevar committed
539

Sam Hocevar's avatar
 
Sam Hocevar committed
540
    if( p_pic->p_data == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
541
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
542
        p_pic->i_planes = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
543 544
        return;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
545

Sam Hocevar's avatar
 
Sam Hocevar committed
546 547 548 549
    /* Fill the p_pixels field for each plane */
    p_pic->p[ 0 ].p_pixels = p_pic->p_data;

    for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
550
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
551 552 553
        p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
                                          + p_pic->p[i_index-1].i_lines
                                             * p_pic->p[i_index-1].i_pitch;
Sam Hocevar's avatar
 
Sam Hocevar committed
554 555 556
    }
}

557 558 559 560 561 562 563 564 565
/* Following functions are local */

/*****************************************************************************
 * vout_CopyPicture: copy a picture to another one
 *****************************************************************************
 * This function takes advantage of the image format, and reduces the
 * number of calls to memcpy() to the minimum. Source and destination
 * images must have same width, height, and chroma.
 *****************************************************************************/
566 567
static void vout_CopyPicture( vout_thread_t * p_vout,
                              picture_t *p_src, picture_t *p_dest )
568 569 570 571 572 573 574 575 576 577 578 579 580
{
    int i;

    for( i = 0; i < p_src->i_planes ; i++ )
    {
        if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
        {
            if( p_src->p[i].b_margin )
            {
                /* If p_src->b_margin is set, p_dest->b_margin must be set */
                if( p_dest->p[i].b_hidden )
                {
                    /* There are margins, but they are hidden : perfect ! */
581 582
                    p_vout->p_vlc->pf_memcpy(
                                 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
583 584 585 586 587 588 589 590 591 592 593
                                 p_src->p[i].i_pitch * p_src->p[i].i_lines );
                    continue;
                }
                else
                {
                    /* We can't directly copy the margin. Too bad. */
                }
            }
            else
            {
                /* Same pitch, no margins : perfect ! */
594 595 596
                p_vout->p_vlc->pf_memcpy(
                                 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
                                 p_src->p[i].i_pitch * p_src->p[i].i_lines );
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
                continue;
            }
        }
        else
        {
            /* Pitch values are different */
        }

        /* We need to proceed line by line */
        {
            u8 *p_in = p_src->p[i].p_pixels, *p_out = p_dest->p[i].p_pixels;
            int i_line;

            for( i_line = p_src->p[i].i_lines; i_line--; )
            {
612 613
                p_vout->p_vlc->pf_memcpy( p_out, p_in,
                                          p_src->p[i].i_visible_bytes );
614 615 616 617 618 619 620
                p_in += p_src->p[i].i_pitch;
                p_out += p_dest->p[i].i_pitch;
            }
        }
    }
}