vout_pictures.c 16.3 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3
/*****************************************************************************
 * vout_pictures.c : picture management functions
 *****************************************************************************
4
 * Copyright (C) 2000-2004 the VideoLAN team
5
 * $Id$
Sam Hocevar's avatar
 
Sam Hocevar committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
 
Sam Hocevar committed
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
32
#include <assert.h>
33

34
#include <vlc_common.h>
35
#include <libvlc.h>
zorglub's avatar
zorglub committed
36 37
#include <vlc_vout.h>
#include <vlc_osd.h>
38
#include <vlc_filter.h>
39 40
#include <vlc_image.h>
#include <vlc_block.h>
Laurent Aimar's avatar
Laurent Aimar committed
41
#include <vlc_picture_fifo.h>
42
#include <vlc_picture_pool.h>
43

44
#include "vout_pictures.h"
45
#include "vout_internal.h"
46

47
/**
Laurent Aimar's avatar
Laurent Aimar committed
48 49
 * It retreives a picture from the vout or NULL if no pictures are
 * available yet.
50
 *
Laurent Aimar's avatar
Laurent Aimar committed
51
 * You MUST call vout_PutPicture or vout_ReleasePicture on it.
52
 *
Laurent Aimar's avatar
Laurent Aimar committed
53 54
 * You may use vout_HoldPicture(paired with vout_ReleasePicture) to keep a
 * read-only reference.
55
 */
Laurent Aimar's avatar
Laurent Aimar committed
56
picture_t *vout_GetPicture( vout_thread_t *p_vout )
Sam Hocevar's avatar
 
Sam Hocevar committed
57 58
{
    /* Get lock */
59
    vlc_mutex_lock( &p_vout->p->picture_lock );
60 61 62
    picture_t *p_pic = picture_pool_Get(p_vout->p->decoder_pool);
    if (p_pic) {
        picture_Reset(p_pic);
63
        p_pic->p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
64
    }
65
    vlc_mutex_unlock( &p_vout->p->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
66

67
    return p_pic;
Sam Hocevar's avatar
 
Sam Hocevar committed
68 69
}

Laurent Aimar's avatar
Laurent Aimar committed
70 71 72 73 74 75 76 77 78
/**
 * It gives to the vout a picture to be displayed.
 *
 * The given picture MUST comes from vout_GetPicture.
 *
 * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
 * read/used.
 */
void vout_PutPicture( vout_thread_t *p_vout, picture_t *p_pic )
79
{
80
    vlc_mutex_lock( &p_vout->p->picture_lock );
81

Laurent Aimar's avatar
Laurent Aimar committed
82 83
    p_pic->p_next = NULL;
    picture_fifo_Push(p_vout->p->decoder_fifo, p_pic);
84 85

    vlc_cond_signal( &p_vout->p->picture_wait );
86
    vlc_mutex_unlock( &p_vout->p->picture_lock );
87 88
}

89
/**
Laurent Aimar's avatar
Laurent Aimar committed
90
 * It releases a picture retreived by vout_GetPicture.
91
 */
Laurent Aimar's avatar
Laurent Aimar committed
92
void vout_ReleasePicture( vout_thread_t *p_vout, picture_t *p_pic  )
Sam Hocevar's avatar
 
Sam Hocevar committed
93
{
94
    vlc_mutex_lock( &p_vout->p->picture_lock );
Laurent Aimar's avatar
Laurent Aimar committed
95 96 97 98

    picture_Release( p_pic );

    vlc_cond_signal( &p_vout->p->picture_wait );
99
    vlc_mutex_unlock( &p_vout->p->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
100 101
}

102
/**
Laurent Aimar's avatar
Laurent Aimar committed
103 104
 * It increment the reference counter of a picture retreived by
 * vout_GetPicture.
105
 */
Laurent Aimar's avatar
Laurent Aimar committed
106
void vout_HoldPicture( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
107
{
108
    vlc_mutex_lock( &p_vout->p->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
109

Laurent Aimar's avatar
Laurent Aimar committed
110 111
    picture_Hold( p_pic );

112
    vlc_mutex_unlock( &p_vout->p->picture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
113 114
}

115 116 117
/**
 * Allocate a new picture in the heap.
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
118 119 120
 * 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.
121
 */
122 123 124 125
static int vout_AllocatePicture( picture_t *p_pic,
                                 vlc_fourcc_t i_chroma,
                                 int i_width, int i_height,
                                 int i_sar_num, int i_sar_den )
Sam Hocevar's avatar
 
Sam Hocevar committed
126
{
127
    /* Make sure the real dimensions are a multiple of 16 */
128 129
    if( picture_Setup( p_pic, i_chroma, i_width, i_height,
                       i_sar_num, i_sar_den ) != VLC_SUCCESS )
130
        return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
131 132

    /* Calculate how big the new image should be */
133
    size_t i_bytes = 0;
134
    for( int i = 0; i < p_pic->i_planes; i++ )
135
    {
136 137 138 139
        const plane_t *p = &p_pic->p[i];

        if( p->i_pitch <= 0 || p->i_lines <= 0 ||
            p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
140
        {
141 142
            p_pic->i_planes = 0;
            return VLC_ENOMEM;
143
        }
144
        i_bytes += p->i_pitch * p->i_lines;
145
    }
gbazin's avatar
 
gbazin committed
146

147 148
    uint8_t *p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
    if( !p_data )
gbazin's avatar
 
gbazin committed
149 150
    {
        p_pic->i_planes = 0;
151
        return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
152 153 154
    }

    /* Fill the p_pixels field for each plane */
155
    p_pic->p[0].p_pixels = p_data;
156
    for( int i = 1; i < p_pic->i_planes; i++ )
gbazin's avatar
 
gbazin committed
157
    {
158 159
        p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
                                                        p_pic->p[i-1].i_pitch ];
gbazin's avatar
 
gbazin committed
160
    }
161 162

    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
163 164
}

165 166 167 168 169 170 171 172 173
/*****************************************************************************
 *
 *****************************************************************************/
static void PictureReleaseCallback( picture_t *p_picture )
{
    if( --p_picture->i_refcount > 0 )
        return;
    picture_Delete( p_picture );
}
174

Laurent Aimar's avatar
Laurent Aimar committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
/*****************************************************************************
 *
 *****************************************************************************/
void picture_Reset( picture_t *p_picture )
{
    /* */
    p_picture->date = VLC_TS_INVALID;
    p_picture->b_force = false;
    p_picture->b_progressive = false;
    p_picture->i_nb_fields = 0;
    p_picture->b_top_field_first = false;
    picture_CleanupQuant( p_picture );
}

189 190 191
/*****************************************************************************
 *
 *****************************************************************************/
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
typedef struct
{
    unsigned     i_plane_count;
    struct
    {
        struct
        {
            unsigned i_num;
            unsigned i_den;
        } w;
        struct
        {
            unsigned i_num;
            unsigned i_den;
        } h;
    } p[VOUT_MAX_PLANES];
    unsigned i_pixel_size;

} chroma_description_t;

#define PLANAR(n, w_den, h_den) \
    { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
#define PACKED(size) \
    { 1, { {{1,1}, {1,1}} }, size }

static const struct
{
    vlc_fourcc_t            p_fourcc[5];
    chroma_description_t    description;
} p_chromas[] = {
    { { VLC_CODEC_I411, 0 },                                 PLANAR(3, 4, 1) },
    { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 },                  PLANAR(3, 4, 4) },
    { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
    { { VLC_CODEC_I422, VLC_CODEC_J422, 0 },                 PLANAR(3, 2, 1) },
    { { VLC_CODEC_I440, VLC_CODEC_J440, 0 },                 PLANAR(3, 1, 2) },
    { { VLC_CODEC_I444, VLC_CODEC_J444, 0 },                 PLANAR(3, 1, 1) },
    { { VLC_CODEC_YUVA, 0 },                                 PLANAR(4, 1, 1) },

    { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
    { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
    { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 },                               PACKED(2) },
    { { VLC_CODEC_RGB24, 0 },                                                PACKED(3) },
    { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 },                                PACKED(4) },

    { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },

    { {0}, { 0, {}, 0 } }
};

#undef PACKED
#undef PLANAR

static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
{
    for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
    {
        const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
        for( unsigned j = 0; p_fourcc[j]; j++ )
        {
            if( p_fourcc[j] == i_fourcc )
                return &p_chromas[i].description;
        }
    }
    return NULL;
}

static int LCM( int a, int b )
{
    return a * b / GCD( a, b );
}

263 264
int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
                   int i_width, int i_height, int i_sar_num, int i_sar_den )
265 266
{
    /* Store default values */
267 268
    p_picture->i_planes = 0;
    for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
269
    {
270 271 272
        plane_t *p = &p_picture->p[i];
        p->p_pixels = NULL;
        p->i_pixel_pitch = 0;
273 274 275
    }

    p_picture->pf_release = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
276
    p_picture->p_release_sys = NULL;
277 278 279 280 281 282
    p_picture->i_refcount = 0;

    p_picture->i_qtype = QTYPE_NONE;
    p_picture->i_qstride = 0;
    p_picture->p_q = NULL;

283
    video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
284
                        i_sar_num, i_sar_den );
285

286 287 288 289
    const chroma_description_t *p_dsc =
        vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
    if( !p_dsc )
        return VLC_EGENERIC;
290

291 292 293 294 295 296 297 298 299 300
    /* We want V (width/height) to respect:
        (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
        (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
       Which is respected if you have
       V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
    */
    int i_modulo_w = 1;
    int i_modulo_h = 1;
    int i_ratio_h  = 1;
    for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
301
    {
302 303 304 305 306
        i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
        i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
        if( i_ratio_h < p_dsc->p[i].h.i_den )
            i_ratio_h = p_dsc->p[i].h.i_den;
    }
307

308 309 310 311 312 313
    const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
    const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
    const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
    for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
    {
        plane_t *p = &p_picture->p[i];
314

315 316 317 318 319
        p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
        p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
        p->i_pitch         = i_width_aligned * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
        p->i_visible_pitch = i_width * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
        p->i_pixel_pitch   = p_dsc->i_pixel_size;
320

321
        assert( (p->i_pitch % 16) == 0 );
322
    }
323
    p_picture->i_planes  = p_dsc->i_plane_count;
324 325 326 327

    return VLC_SUCCESS;
}

328 329 330
/*****************************************************************************
 *
 *****************************************************************************/
331
picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
332
{
333 334 335 336
    video_format_t fmt = *p_fmt;

    /* It is needed to be sure all informations are filled */
    video_format_Setup( &fmt, p_fmt->i_chroma,
337
                              p_fmt->i_width, p_fmt->i_height,
Laurent Aimar's avatar
Laurent Aimar committed
338
                              p_fmt->i_sar_num, p_fmt->i_sar_den );
339 340

    /* */
ivoire's avatar
ivoire committed
341
    picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
342 343 344
    if( !p_picture )
        return NULL;

345
    if( p_resource )
346
    {
347 348
        if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
                           fmt.i_sar_num, fmt.i_sar_den ) )
349 350 351 352 353
        {
            free( p_picture );
            return NULL;
        }
        p_picture->p_sys = p_resource->p_sys;
354

355 356 357 358 359 360 361 362 363
        for( int i = 0; i < p_picture->i_planes; i++ )
        {
            p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
            p_picture->p[i].i_lines  = p_resource->p[i].i_lines;
            p_picture->p[i].i_pitch  = p_resource->p[i].i_pitch;
        }
    }
    else
    {
364
        if( vout_AllocatePicture( p_picture,
365 366
                                  fmt.i_chroma, fmt.i_width, fmt.i_height,
                                  fmt.i_sar_num, fmt.i_sar_den ) )
367 368 369 370 371 372 373
        {
            free( p_picture );
            return NULL;
        }
    }
    /* */
    p_picture->format = fmt;
374 375 376 377 378
    p_picture->i_refcount = 1;
    p_picture->pf_release = PictureReleaseCallback;

    return p_picture;
}
379 380 381 382
picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
{
    return picture_NewFromResource( p_fmt, NULL );
}
383
picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
384 385 386 387
{
    video_format_t fmt;

    memset( &fmt, 0, sizeof(fmt) );
388
    video_format_Setup( &fmt, i_chroma, i_width, i_height,
389
                        i_sar_num, i_sar_den );
390 391 392

    return picture_NewFromFormat( &fmt );
}
393 394 395 396 397 398 399

/*****************************************************************************
 *
 *****************************************************************************/
void picture_Delete( picture_t *p_picture )
{
    assert( p_picture && p_picture->i_refcount == 0 );
Laurent Aimar's avatar
Laurent Aimar committed
400
    assert( p_picture->p_release_sys == NULL );
401

402
    free( p_picture->p_q );
403 404 405 406 407 408 409 410 411 412
    free( p_picture->p_data_orig );
    free( p_picture->p_sys );
    free( p_picture );
}

/*****************************************************************************
 *
 *****************************************************************************/
void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
{
413 414 415
    int i;

    for( i = 0; i < p_src->i_planes ; i++ )
416 417
        plane_CopyPixels( p_dst->p+i, p_src->p+i );
}
418

419 420 421 422 423 424
void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
{
    const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
                                     p_src->i_visible_pitch );
    const unsigned i_height = __MIN( p_dst->i_visible_lines,
                                     p_src->i_visible_lines );
425

426 427 428 429 430 431 432 433 434 435 436 437
    if( p_src->i_pitch == p_dst->i_pitch )
    {
        /* There are margins, but with the same width : perfect ! */
        vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
                    p_src->i_pitch * i_height );
    }
    else
    {
        /* We need to proceed line by line */
        uint8_t *p_in = p_src->p_pixels;
        uint8_t *p_out = p_dst->p_pixels;
        int i_line;
438

439 440 441 442 443 444 445 446
        assert( p_in );
        assert( p_out );

        for( i_line = i_height; i_line--; )
        {
            vlc_memcpy( p_out, p_in, i_width );
            p_in += p_src->i_pitch;
            p_out += p_dst->i_pitch;
447 448 449
        }
    }
}
Laurent Aimar's avatar
Laurent Aimar committed
450

451 452 453
/*****************************************************************************
 *
 *****************************************************************************/
454 455 456 457 458
int picture_Export( vlc_object_t *p_obj,
                    block_t **pp_image,
                    video_format_t *p_fmt,
                    picture_t *p_picture,
                    vlc_fourcc_t i_format,
459
                    int i_override_width, int i_override_height )
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
    /* */
    video_format_t fmt_in = p_picture->format;
    if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
    {
        fmt_in.i_sar_num =
        fmt_in.i_sar_den = 1;
    }

    /* */
    video_format_t fmt_out;
    memset( &fmt_out, 0, sizeof(fmt_out) );
    fmt_out.i_sar_num =
    fmt_out.i_sar_den = 1;
    fmt_out.i_chroma  = i_format;

476 477 478 479
    /* compute original width/height */
    unsigned int i_original_width;
    unsigned int i_original_height;
    if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
480
    {
481 482
        i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
        i_original_height = fmt_in.i_height;
483 484 485
    }
    else
    {
486 487 488 489 490
        i_original_width =  fmt_in.i_width;
        i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
    }

    /* */
491 492 493 494
    fmt_out.i_width  = ( i_override_width < 0 ) ?
                       i_original_width : i_override_width;
    fmt_out.i_height = ( i_override_height < 0 ) ?
                       i_original_height : i_override_height;
495 496 497 498 499 500 501 502 503 504 505

    /* scale if only one direction is provided */
    if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
    {
        fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
                     * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
    }
    else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
    {
        fmt_out.i_width  = fmt_in.i_width * fmt_out.i_height
                     * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    }

    image_handler_t *p_image = image_HandlerCreate( p_obj );

    block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );

    image_HandlerDelete( p_image );

    if( !p_block )
        return VLC_EGENERIC;

    p_block->i_pts =
    p_block->i_dts = p_picture->date;

    if( p_fmt )
        *p_fmt = fmt_out;
    *pp_image = p_block;

    return VLC_SUCCESS;
}