vout_subpictures.c 28.8 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3
/*****************************************************************************
 * vout_subpictures.c : subpicture management functions
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
4
 * Copyright (C) 2000-2004 VideoLAN
5
 * $Id$
Sam Hocevar's avatar
 
Sam Hocevar committed
6 7 8
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
9
 *          Gildas Bazin <gbazin@videolan.org>
Sam Hocevar's avatar
 
Sam Hocevar committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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 <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
#include "vlc_block.h"
36
#include "vlc_video.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
37
#include "video_output.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "vlc_filter.h"
#include "osd.h"

static void UpdateSPU        ( vout_thread_t *, vlc_object_t * );
static int  CropCallback     ( vlc_object_t *, char const *,
                               vlc_value_t, vlc_value_t, void * );

static subpicture_t *spu_new_buffer( filter_t * );
static void spu_del_buffer( filter_t *, subpicture_t * );

/**
 * Initialise the subpicture decoder unit
 *
 * \param p_vout the vout in which to create the subpicture unit
 */
void vout_InitSPU( vout_thread_t *p_vout )
{
    int i_index;

    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
    {
        p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
        p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
    }

    p_vout->p_blend = NULL;
64
    p_vout->p_text = NULL;
65 66 67 68 69 70

    p_vout->i_crop_x = p_vout->i_crop_y =
        p_vout->i_crop_width = p_vout->i_crop_height = 0;
    p_vout->b_force_alpha = VLC_FALSE;
    p_vout->b_force_crop = VLC_FALSE;

71
    vout_AttachSPU( p_vout, VLC_TRUE );
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

/**
 * Destroy the subpicture decoder unit
 *
 * \param p_vout the vout in which to destroy the subpicture unit
 */
void vout_DestroySPU( vout_thread_t *p_vout )
{
    int i_index;

    /* Destroy all remaining subpictures */
    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
    {
        if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
        {
            vout_DestroySubPicture( p_vout,
                                    &p_vout->p_subpicture[i_index] );
        }
    }

    if( p_vout->p_blend )
    {
        if( p_vout->p_blend->p_module )
            module_Unneed( p_vout->p_blend, p_vout->p_blend->p_module );

        vlc_object_detach( p_vout->p_blend );
        vlc_object_destroy( p_vout->p_blend );
    }

102 103 104 105 106 107 108 109 110
    if( p_vout->p_text )
    {
        if( p_vout->p_text->p_module )
            module_Unneed( p_vout->p_text, p_vout->p_text->p_module );

        vlc_object_detach( p_vout->p_text );
        vlc_object_destroy( p_vout->p_text );
    }

111 112 113 114 115 116 117 118 119 120 121 122 123
    vout_AttachSPU( p_vout, VLC_FALSE );
}

/**
 * Attach/Detach the SPU from any input
 *
 * \param p_vout the vout in which to destroy the subpicture unit
 * \param b_attach to select attach or detach
 */
void vout_AttachSPU( vout_thread_t *p_vout, vlc_bool_t b_attach )
{
    vlc_object_t *p_input;

124
    p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
125 126 127
    if( !p_input ) return;

    if( b_attach )
128
    {
129 130 131 132 133 134 135
        UpdateSPU( p_vout, VLC_OBJECT(p_input) );
        var_AddCallback( p_input, "highlight", CropCallback, p_vout );
        vlc_object_release( p_input );
    }
    else
    {
        /* Delete callback */
136 137 138
        var_DelCallback( p_input, "highlight", CropCallback, p_vout );
        vlc_object_release( p_input );
    }
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

/**
 * Create a subpicture region
 *
 * \param p_this vlc_object_t
 * \param p_fmt the format that this subpicture region should have
 */
subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
                                         video_format_t *p_fmt )
{
    subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
    memset( p_region, 0, sizeof(subpicture_region_t) );
    p_region->p_next = 0;
    p_region->fmt = *p_fmt;
155 156 157 158 159 160 161 162 163 164
    p_region->psz_text = 0;

    if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
        p_fmt->p_palette = p_region->fmt.p_palette =
            malloc( sizeof(video_palette_t) );
    else p_fmt->p_palette = p_region->fmt.p_palette = NULL;

    p_region->picture.p_data_orig = 0;

    if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
165 166 167 168 169 170 171

    vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
                          p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );

    if( !p_region->picture.i_planes )
    {
        free( p_region );
172
        free( p_fmt->p_palette );
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        return NULL;
    }

    return p_region;
}

/**
 * Destroy a subpicture region
 *
 * \param p_this vlc_object_t
 * \param p_region the subpicture region to destroy
 */
void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
{
    if( !p_region ) return;
    if( p_region->picture.p_data_orig ) free( p_region->picture.p_data_orig );
    if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
190
    if( p_region->psz_text ) free( p_region->psz_text );
191 192
    free( p_region );
}
Sam Hocevar's avatar
 
Sam Hocevar committed
193

194 195 196
/**
 * Display a subpicture unit
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
197 198
 * Remove the reservation flag of a subpicture, which will cause it to be
 * ready for display.
199 200 201
 * \param p_vout the video output this subpicture should be displayed on
 * \param p_subpic the subpicture to display
 */
202
void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
Sam Hocevar's avatar
 
Sam Hocevar committed
203
{
Sam Hocevar's avatar
 
Sam Hocevar committed
204
    int         i_margin;
Sam Hocevar's avatar
 
Sam Hocevar committed
205 206 207 208

    /* Check if status is valid */
    if( p_subpic->i_status != RESERVED_SUBPICTURE )
    {
209 210
        msg_Err( p_vout, "subpicture %p has invalid status #%d",
                         p_subpic, p_subpic->i_status );
Sam Hocevar's avatar
 
Sam Hocevar committed
211 212
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
213 214
    /* If the user requested an SPU margin, we force the position after
     * having checked that it was a valid value. */
215
    i_margin = config_GetInt( p_vout, "spumargin" );
Sam Hocevar's avatar
 
Sam Hocevar committed
216 217 218

    if( i_margin >= 0 )
    {
219 220
        if( p_subpic->i_height + (unsigned int)i_margin
                                                 <= p_vout->output.i_height )
Sam Hocevar's avatar
 
Sam Hocevar committed
221 222 223 224 225 226
        {
            p_subpic->i_y = p_vout->output.i_height
                             - i_margin - p_subpic->i_height;
        }
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
227 228 229 230
    /* Remove reservation flag */
    p_subpic->i_status = READY_SUBPICTURE;
}

231 232 233
/**
 * Allocate a subpicture in the video output heap.
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
234 235 236 237
 * This function create a reserved subpicture 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 spu data fields. It needs locking
 * since several pictures can be created by several producers threads.
238
 * \param p_vout the vout in which to create the subpicture
239
 * \param i_channel the channel this subpicture should belong to
240 241 242
 * \param i_type the type of the subpicture
 * \return NULL on error, a reserved subpicture otherwise
 */
243
subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_channel,
244
                                     int i_type )
Sam Hocevar's avatar
 
Sam Hocevar committed
245 246
{
    int                 i_subpic;                        /* subpicture index */
247
    subpicture_t *      p_subpic = NULL;            /* first free subpicture */
Sam Hocevar's avatar
 
Sam Hocevar committed
248

249 250
    /* Clear the default channel before writing into it */
    if( i_channel == DEFAULT_CHAN )
251
    {
252
        vout_ClearOSDChannel( p_vout, DEFAULT_CHAN );
253 254
    }

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

Sam Hocevar's avatar
 
Sam Hocevar committed
258 259 260
    /*
     * Look for an empty place
     */
261
    p_subpic = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
262 263
    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
    {
264
        if( p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
265 266
        {
            /* Subpicture is empty and ready for allocation */
267 268 269
            p_subpic = &p_vout->p_subpicture[i_subpic];
            p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
270 271 272
        }
    }

273 274
    /* If no free subpicture could be found */
    if( p_subpic == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
275
    {
276
        msg_Err( p_vout, "subpicture heap is full" );
Sam Hocevar's avatar
 
Sam Hocevar committed
277
        vlc_mutex_unlock( &p_vout->subpicture_lock );
278
        return NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
279
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
280

281
    /* Copy subpicture information, set some default values */
282
    p_subpic->i_channel = i_channel;
283 284
    p_subpic->i_type    = i_type;
    p_subpic->i_status  = RESERVED_SUBPICTURE;
Sam Hocevar's avatar
 
Sam Hocevar committed
285

286 287 288 289 290 291 292 293
    p_subpic->i_start   = 0;
    p_subpic->i_stop    = 0;
    p_subpic->b_ephemer = VLC_FALSE;

    p_subpic->i_x       = 0;
    p_subpic->i_y       = 0;
    p_subpic->i_width   = 0;
    p_subpic->i_height  = 0;
294 295 296 297 298
    p_subpic->b_absolute= VLC_TRUE;
    p_subpic->i_flags   = 0;
    p_subpic->pf_render = 0;
    p_subpic->pf_destroy= 0;
    p_subpic->p_sys     = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
299

300 301 302 303 304 305
    /* Remain last subpicture displayed in DEFAULT_CHAN */
    if( i_channel == DEFAULT_CHAN )
    {
        p_vout->p_default_channel = p_subpic;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
306
    vlc_mutex_unlock( &p_vout->subpicture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
307

308 309 310
    p_subpic->pf_create_region = __spu_CreateRegion;
    p_subpic->pf_destroy_region = __spu_DestroyRegion;

311
    return p_subpic;
Sam Hocevar's avatar
 
Sam Hocevar committed
312 313
}

314 315 316
/**
 * Remove a subpicture from the heap
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
317 318 319 320
 * This function frees a previously reserved subpicture.
 * It is meant to be used when the construction of a picture aborted.
 * This function does not need locking since reserved subpictures are ignored
 * by the output thread.
321
 */
Sam Hocevar's avatar
 
Sam Hocevar committed
322 323
void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
324 325 326 327 328 329 330 331 332 333
    /* Get lock */
    vlc_mutex_lock( &p_vout->subpicture_lock );

    /* There can be race conditions so we need to check the status */
    if( p_subpic->i_status == FREE_SUBPICTURE )
    {
        vlc_mutex_unlock( &p_vout->subpicture_lock );
        return;
    }

334 335 336 337 338 339 340 341
    /* Check if status is valid */
    if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
           && ( p_subpic->i_status != READY_SUBPICTURE ) )
    {
        msg_Err( p_vout, "subpicture %p has invalid status %d",
                         p_subpic, p_subpic->i_status );
    }

342 343 344 345 346 347 348
    while( p_subpic->p_region )
    {
        subpicture_region_t *p_region = p_subpic->p_region;
        p_subpic->p_region = p_region->p_next;
        spu_DestroyRegion( p_vout, p_region );
    }

349 350 351 352 353 354
    if( p_subpic->pf_destroy )
    {
        p_subpic->pf_destroy( p_subpic );
    }

    p_subpic->i_status = FREE_SUBPICTURE;
Gildas Bazin's avatar
 
Gildas Bazin committed
355 356

    vlc_mutex_unlock( &p_vout->subpicture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
357 358 359 360 361
}

/*****************************************************************************
 * vout_RenderSubPictures: render a subpicture list
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
362
 * This function renders all sub picture units in the list.
Sam Hocevar's avatar
 
Sam Hocevar committed
363
 *****************************************************************************/
364 365
void vout_RenderSubPictures( vout_thread_t *p_vout, picture_t *p_pic_dst,
                             picture_t *p_pic_src, subpicture_t *p_subpic )
Sam Hocevar's avatar
 
Sam Hocevar committed
366
{
Gildas Bazin's avatar
 
Gildas Bazin committed
367 368 369 370 371
    /* Get lock */
    vlc_mutex_lock( &p_vout->subpicture_lock );

    /* Check i_status again to make sure spudec hasn't destroyed the subpic */
    while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
372
    {
373 374
        subpicture_region_t *p_region = p_subpic->p_region;

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        /* Load the blending module */
        if( !p_vout->p_blend && p_region )
        {
            p_vout->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
            vlc_object_attach( p_vout->p_blend, p_vout );
            p_vout->p_blend->fmt_out.video.i_x_offset =
                p_vout->p_blend->fmt_out.video.i_y_offset = 0;
            p_vout->p_blend->fmt_out.video.i_aspect =
                p_vout->render.i_aspect;
            p_vout->p_blend->fmt_out.video.i_chroma =
                p_vout->output.i_chroma;
    
            p_vout->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
    
            p_vout->p_blend->p_module =
                module_Need( p_vout->p_blend, "video blending", 0, 0 );
        }
    
        /* Load the text rendering module */
        if( !p_vout->p_text && p_region )
        {
            p_vout->p_text = vlc_object_create( p_vout, sizeof(filter_t) );
            vlc_object_attach( p_vout->p_text, p_vout );
    
            p_vout->p_text->fmt_out.video.i_width =
                p_vout->p_text->fmt_out.video.i_visible_width =
                    p_vout->output.i_width;
            p_vout->p_text->fmt_out.video.i_height =
                p_vout->p_text->fmt_out.video.i_visible_height =
                    p_vout->output.i_height;
    
            p_vout->p_text->pf_spu_buffer_new = spu_new_buffer;
            p_vout->p_text->pf_spu_buffer_del = spu_del_buffer;
    
            p_vout->p_text->p_module =
                module_Need( p_vout->p_text, "text renderer", 0, 0 );
        }

413 414
        if( p_subpic->pf_render )
        {
415
            p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
416 417 418 419 420 421 422
        }
        else while( p_region && p_vout->p_blend &&
                    p_vout->p_blend->pf_video_blend )
        {
            int i_x_offset = p_region->i_x + p_subpic->i_x;
            int i_y_offset = p_region->i_y + p_subpic->i_y;

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
            if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
            {
                if( p_vout->p_text && p_vout->p_text->p_module &&
                    p_vout->p_text->pf_render_string )
                {
                    /* TODO: do it in a less hacky way
                     * (modify text renderer API) */
                    subpicture_t *p_spu;
                    subpicture_region_t tmp_region;
                    block_t *p_new_block =
                        block_New( p_vout, strlen(p_region->psz_text) + 1 );

                    if( p_new_block )
                    {
                        memcpy( p_new_block->p_buffer, p_region->psz_text,
                                p_new_block->i_buffer );
                        p_new_block->i_pts = p_new_block->i_dts =
                            p_subpic->i_start;
                        p_new_block->i_length =
                            p_subpic->i_start - p_subpic->i_stop;
                        p_spu = p_vout->p_text->pf_render_string(
                            p_vout->p_text, p_new_block );

                        if( p_spu )
                        {
                            tmp_region = *p_region;
                            *p_region = *p_spu->p_region;
                            p_region->p_next = tmp_region.p_next;
                            *p_spu->p_region = tmp_region;
                            p_vout->p_text->pf_spu_buffer_del( p_vout->p_text,
                                                               p_spu );
                        }
                    }
                }
            }

459 460
            if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
            {
461 462
                i_y_offset = p_vout->output.i_height - p_region->fmt.i_height -
                    p_subpic->i_y;
463 464 465
            }
            else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
            {
466
                i_y_offset = p_vout->output.i_height / 2 -
467 468 469 470 471
                    p_region->fmt.i_height / 2;
            }

            if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
            {
472 473
                i_x_offset = p_vout->output.i_width - p_region->fmt.i_width -
                    p_subpic->i_x;
474 475 476
            }
            else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
            {
477
                i_x_offset = p_vout->output.i_width / 2 -
478 479 480 481 482 483 484 485 486 487 488 489 490 491
                    p_region->fmt.i_width / 2;
            }

            if( p_subpic->b_absolute )
            {
                i_x_offset = p_region->i_x + p_subpic->i_x;
                i_y_offset = p_region->i_y + p_subpic->i_y;
            }

            p_vout->p_blend->fmt_in.video = p_region->fmt;

            /* Force cropping if requested */
            if( p_vout->b_force_crop )
            {
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
                video_format_t *p_fmt = &p_vout->p_blend->fmt_in.video;

                /* Find the intersection */
                if( p_vout->i_crop_x + p_vout->i_crop_width <= i_x_offset ||
                    i_x_offset + (int)p_fmt->i_visible_width <
                        p_vout->i_crop_x ||
                    p_vout->i_crop_y + p_vout->i_crop_height <= i_y_offset ||
                    i_y_offset + (int)p_fmt->i_visible_height <
                        p_vout->i_crop_y )
                {
                    /* No intersection */
                    p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
                }
                else
                {
                    int i_x, i_y, i_x_end, i_y_end;
                    i_x = __MAX( p_vout->i_crop_x, i_x_offset );
                    i_y = __MAX( p_vout->i_crop_y, i_y_offset );
                    i_x_end = __MIN( p_vout->i_crop_x + p_vout->i_crop_width,
                                   i_x_offset + (int)p_fmt->i_visible_width );
                    i_y_end = __MIN( p_vout->i_crop_y + p_vout->i_crop_height,
                                   i_y_offset + (int)p_fmt->i_visible_height );

                    p_fmt->i_x_offset = i_x - i_x_offset;
                    p_fmt->i_y_offset = i_y - i_y_offset;
                    p_fmt->i_visible_width = i_x_end - i_x;
                    p_fmt->i_visible_height = i_y_end - i_y;

                    i_x_offset = i_x;
                    i_y_offset = i_y;
                }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            }

            /* Force palette if requested */
            if( p_vout->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
                p_vout->p_blend->fmt_in.video.i_chroma )
            {
                p_vout->p_blend->fmt_in.video.p_palette->palette[0][3] =
                    p_vout->pi_alpha[0];
                p_vout->p_blend->fmt_in.video.p_palette->palette[1][3] =
                    p_vout->pi_alpha[1];
                p_vout->p_blend->fmt_in.video.p_palette->palette[2][3] =
                    p_vout->pi_alpha[2];
                p_vout->p_blend->fmt_in.video.p_palette->palette[3][3] =
                    p_vout->pi_alpha[3];
            }

539 540 541 542 543 544 545 546 547
            /* Update the output picture size */
            p_vout->p_blend->fmt_out.video.i_width =
                p_vout->p_blend->fmt_out.video.i_visible_width =
                    p_vout->output.i_width;
            p_vout->p_blend->fmt_out.video.i_height =
                p_vout->p_blend->fmt_out.video.i_visible_height =
                    p_vout->output.i_height;

           p_vout->p_blend->pf_video_blend( p_vout->p_blend, p_pic_dst,
548
                p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
549 550 551 552

            p_region = p_region->p_next;
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
553 554
        p_subpic = p_subpic->p_next;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
555 556

    vlc_mutex_unlock( &p_vout->subpicture_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
}

/*****************************************************************************
 * vout_SortSubPictures: find the subpictures to display
 *****************************************************************************
 * This function parses all subpictures and decides which ones need to be
 * displayed. This operation does not need lock, since only READY_SUBPICTURE
 * are handled. If no picture has been selected, display_date will depend on
 * the subpicture.
 * We also check for ephemer DVD subpictures (subpictures that have
 * to be removed if a newer one is available), which makes it a lot
 * more difficult to guess if a subpicture has to be rendered or not.
 *****************************************************************************/
subpicture_t *vout_SortSubPictures( vout_thread_t *p_vout,
                                    mtime_t display_date )
{
    int i_index;
    subpicture_t *p_subpic     = NULL;
    subpicture_t *p_ephemer    = NULL;
    mtime_t       ephemer_date = 0;

    /* We get an easily parsable chained list of subpictures which
     * ends with NULL since p_subpic was initialized to NULL. */
    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
    {
        if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
        {
            /* If it is a DVD subpicture, check its date */
Sam Hocevar's avatar
 
Sam Hocevar committed
585
            if( p_vout->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
586
            {
587 588
                if( !p_vout->p_subpicture[i_index].b_ephemer
                     && display_date > p_vout->p_subpicture[i_index].i_stop )
Sam Hocevar's avatar
 
Sam Hocevar committed
589 590 591 592 593 594 595
                {
                    /* Too late, destroy the subpic */
                    vout_DestroySubPicture( p_vout,
                                    &p_vout->p_subpicture[i_index] );
                    continue;
                }

596 597
                if( display_date
                     && display_date < p_vout->p_subpicture[i_index].i_start )
Sam Hocevar's avatar
 
Sam Hocevar committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
                {
                    /* Too early, come back next monday */
                    continue;
                }

                /* If this is an ephemer subpic, see if it's the
                 * youngest we have */
                if( p_vout->p_subpicture[i_index].b_ephemer )
                {
                    if( p_ephemer == NULL )
                    {
                        p_ephemer = &p_vout->p_subpicture[i_index];
                        continue;
                    }

                    if( p_vout->p_subpicture[i_index].i_start
                                                     < p_ephemer->i_start )
                    {
                        /* Link the previous ephemer subpicture and
                         * replace it with the current one */
                        p_ephemer->p_next = p_subpic;
                        p_subpic = p_ephemer;
                        p_ephemer = &p_vout->p_subpicture[i_index];

                        /* If it's the 2nd youngest subpicture,
                         * register its date */
                        if( !ephemer_date
                              || ephemer_date > p_subpic->i_start )
                        {
                            ephemer_date = p_subpic->i_start;
                        }

                        continue;
                    }
                }

                p_vout->p_subpicture[i_index].p_next = p_subpic;
                p_subpic = &p_vout->p_subpicture[i_index];

637 638
                /* If it's the 2nd youngest subpicture, register its date */
                if( !ephemer_date || ephemer_date > p_subpic->i_start )
Sam Hocevar's avatar
 
Sam Hocevar committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
                {
                    ephemer_date = p_subpic->i_start;
                }
            }
            /* If it's not a DVD subpicture, just register it */
            else
            {
                p_vout->p_subpicture[i_index].p_next = p_subpic;
                p_subpic = &p_vout->p_subpicture[i_index];
            }
        }
    }

    /* If we found an ephemer subpicture, check if it has to be
     * displayed */
    if( p_ephemer != NULL )
    {
656
        if( p_ephemer->i_start <= ephemer_date )
Sam Hocevar's avatar
 
Sam Hocevar committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
        {
            /* Ephemer subpicture has lived too long */
            vout_DestroySubPicture( p_vout, p_ephemer );
        }
        else
        {
            /* Ephemer subpicture can still live a bit */
            p_ephemer->p_next = p_subpic;
            return p_ephemer;
        }
    }

    return p_subpic;
}

672 673 674 675 676 677 678
/*****************************************************************************
 * vout_RegisterOSDChannel: register an OSD channel
 *****************************************************************************
 * This function affects an ID to an OSD channel
 *****************************************************************************/
int vout_RegisterOSDChannel( vout_thread_t *p_vout )
{
679 680
    msg_Dbg( p_vout, "Registering OSD channel, ID: %i",
             p_vout->i_channel_count + 1 );
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    return ++p_vout->i_channel_count;
}

/*****************************************************************************
 * vout_ClearOSDChannel: clear an OSD channel
 *****************************************************************************
 * This function destroys the subpictures which belong to the OSD channel
 * corresponding to i_channel_id.
 *****************************************************************************/
void vout_ClearOSDChannel( vout_thread_t *p_vout, int i_channel )
{
    int                 i_subpic;                        /* subpicture index */
    subpicture_t *      p_subpic = NULL;            /* first free subpicture */

    if( i_channel == DEFAULT_CHAN )
    {
        if( p_vout->p_default_channel != NULL )
        {
            vout_DestroySubPicture( p_vout, p_vout->p_default_channel );
        }
        p_vout->p_default_channel = NULL;
        return;
    }

    vlc_mutex_lock( &p_vout->subpicture_lock );

    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
    {
        p_subpic = &p_vout->p_subpicture[i_subpic];
        if( p_subpic->i_status == FREE_SUBPICTURE
            || ( p_subpic->i_status != RESERVED_SUBPICTURE
                 && p_subpic->i_status != READY_SUBPICTURE ) )
        {
            continue;
        }
716

717 718
        if( p_subpic->i_channel == i_channel )
        {
719 720 721 722 723 724 725
            while( p_subpic->p_region )
            {
                subpicture_region_t *p_region = p_subpic->p_region;
                p_subpic->p_region = p_region->p_next;
                spu_DestroyRegion( p_vout, p_region );
            }

726 727 728 729 730 731 732 733 734 735
            if( p_subpic->pf_destroy )
            {
                p_subpic->pf_destroy( p_subpic );
            }
            p_subpic->i_status = FREE_SUBPICTURE;
        }
    }

    vlc_mutex_unlock( &p_vout->subpicture_lock );
}
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

/*****************************************************************************
 * UpdateSPU: update subpicture settings
 *****************************************************************************
 * This function is called from CropCallback and at initialization time, to
 * retrieve crop information from the input.
 *****************************************************************************/
static void UpdateSPU( vout_thread_t *p_vout, vlc_object_t *p_object )
{
    vlc_value_t val;

    p_vout->b_force_alpha = VLC_FALSE;
    p_vout->b_force_crop = VLC_FALSE;

    if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;

    p_vout->b_force_crop = VLC_TRUE;
    var_Get( p_object, "x-start", &val );
    p_vout->i_crop_x = val.i_int;
    var_Get( p_object, "y-start", &val );
    p_vout->i_crop_y = val.i_int;
    var_Get( p_object, "x-end", &val );
    p_vout->i_crop_width = val.i_int - p_vout->i_crop_x;
    var_Get( p_object, "y-end", &val );
    p_vout->i_crop_height = val.i_int - p_vout->i_crop_y;

#if 0
    if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
    {
        int i;
        for( i = 0; i < 4; i++ )
        {
            p_vout->pi_color[i] = ((uint8_t *)val.p_address)[i];
        }
    }
#endif

    if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
    {
        int i;
        for( i = 0; i < 4; i++ )
        {
            p_vout->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
            p_vout->pi_alpha[i] = p_vout->pi_alpha[i] == 0xf ?
                0xff : p_vout->pi_alpha[i] << 4;
        }
        p_vout->b_force_alpha = VLC_TRUE;
    }

    msg_Dbg( p_vout, "crop: %i,%i,%i,%i, alpha: %i",
             p_vout->i_crop_x, p_vout->i_crop_y,
             p_vout->i_crop_width, p_vout->i_crop_height,
             p_vout->b_force_alpha );
}

/*****************************************************************************
 * CropCallback: called when the highlight properties are changed
 *****************************************************************************
 * This callback is called from the input thread when we need cropping
 *****************************************************************************/
static int CropCallback( vlc_object_t *p_object, char const *psz_var,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    UpdateSPU( (vout_thread_t *)p_data, p_object );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Buffers allocation callbacks for the filters
 *****************************************************************************/
static subpicture_t *spu_new_buffer( filter_t *p_filter )
{
808 809 810 811 812 813
    subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
    memset( p_subpic, 0, sizeof(subpicture_t) );
    p_subpic->b_absolute = VLC_TRUE;

    p_subpic->pf_create_region = __spu_CreateRegion;
    p_subpic->pf_destroy_region = __spu_DestroyRegion;
814

815
    return p_subpic;
816 817
}

818
static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
819
{
820 821 822 823 824 825 826 827
    while( p_subpic->p_region )
    {
        subpicture_region_t *p_region = p_subpic->p_region;
        p_subpic->p_region = p_region->p_next;
        p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
    }

    free( p_subpic );
828
}