vout_subpictures.c 52.8 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3
/*****************************************************************************
 * vout_subpictures.c : subpicture management functions
 *****************************************************************************
4
 * Copyright (C) 2000-2007 the VideoLAN team
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
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
 
Sam Hocevar committed
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33 34 35
#include <assert.h>
#include <limits.h>

36
#include <vlc_common.h>
37 38
#include <vlc_modules.h>
#include <vlc_input.h>
Clément Stenac's avatar
Clément Stenac committed
39 40
#include <vlc_vout.h>
#include <vlc_filter.h>
41
#include <vlc_spu.h>
42

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
43
#include "../libvlc.h"
Laurent Aimar's avatar
Laurent Aimar committed
44
#include "vout_internal.h"
45
#include "../misc/subpicture.h"
46

47 48 49
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
50

51
/* Number of simultaneous subpictures */
52
#define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
53

Laurent Aimar's avatar
Laurent Aimar committed
54
/* */
55 56 57 58 59 60 61 62 63 64 65 66
typedef struct
{
    subpicture_t *p_subpicture;
    bool          b_reject;
} spu_heap_entry_t;

typedef struct
{
    spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];

} spu_heap_t;

67 68
struct spu_private_t
{
69
    vlc_mutex_t lock;   /* lock to protect all followings fields */
70
    vlc_object_t *p_input;
71 72 73

    spu_heap_t heap;

74 75 76 77 78 79 80 81 82 83 84
    int i_channel;             /**< number of subpicture channels registered */
    filter_t *p_text;                              /**< text renderer module */
    filter_t *p_scale_yuvp;                     /**< scaling module for YUVP */
    filter_t *p_scale;                    /**< scaling module (all but YUVP) */
    bool b_force_crop;                     /**< force cropping of subpicture */
    int i_crop_x, i_crop_y, i_crop_width, i_crop_height;       /**< cropping */

    int i_margin;                        /**< force position of a subpicture */
    bool b_force_palette;             /**< force palette of subpicture */
    uint8_t palette[4][4];                               /**< forced palette */

85
    /* Subpiture filters */
Laurent Aimar's avatar
Laurent Aimar committed
86
    char           *psz_chain_update;
87
    vlc_mutex_t    chain_lock;
88
    filter_chain_t *p_chain;
89 90 91

    /* */
    mtime_t i_last_sort_date;
92 93
};

Laurent Aimar's avatar
Laurent Aimar committed
94 95 96 97
/*****************************************************************************
 * heap managment
 *****************************************************************************/
static void SpuHeapInit( spu_heap_t *p_heap )
Laurent Aimar's avatar
Laurent Aimar committed
98
{
Laurent Aimar's avatar
Laurent Aimar committed
99 100 101
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
Laurent Aimar's avatar
Laurent Aimar committed
102

Laurent Aimar's avatar
Laurent Aimar committed
103 104 105 106 107 108
        e->p_subpicture = NULL;
        e->b_reject = false;
    }
}

static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
Laurent Aimar's avatar
Laurent Aimar committed
109
{
Laurent Aimar's avatar
Laurent Aimar committed
110 111 112
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
Laurent Aimar's avatar
Laurent Aimar committed
113

Laurent Aimar's avatar
Laurent Aimar committed
114 115
        if( e->p_subpicture )
            continue;
Laurent Aimar's avatar
Laurent Aimar committed
116

Laurent Aimar's avatar
Laurent Aimar committed
117 118 119 120 121 122
        e->p_subpicture = p_subpic;
        e->b_reject = false;
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
Laurent Aimar's avatar
Laurent Aimar committed
123

Laurent Aimar's avatar
Laurent Aimar committed
124 125 126
static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
{
    spu_heap_entry_t *e = &p_heap->p_entry[i_index];
127

Laurent Aimar's avatar
Laurent Aimar committed
128 129
    if( e->p_subpicture )
        subpicture_Delete( e->p_subpicture );
130

Laurent Aimar's avatar
Laurent Aimar committed
131 132
    e->p_subpicture = NULL;
}
Laurent Aimar's avatar
Laurent Aimar committed
133

Laurent Aimar's avatar
Laurent Aimar committed
134 135 136 137 138
static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
{
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
Laurent Aimar's avatar
Laurent Aimar committed
139

Laurent Aimar's avatar
Laurent Aimar committed
140 141 142 143 144 145 146 147
        if( e->p_subpicture != p_subpic )
            continue;

        SpuHeapDeleteAt( p_heap, i );
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
148

Laurent Aimar's avatar
Laurent Aimar committed
149 150 151 152 153 154 155 156 157
static void SpuHeapClean( spu_heap_t *p_heap )
{
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
        if( e->p_subpicture )
            subpicture_Delete( e->p_subpicture );
    }
}
158

159 160 161 162 163
struct filter_owner_sys_t
{
    spu_t *p_spu;
    int i_channel;
};
164

Laurent Aimar's avatar
Laurent Aimar committed
165 166 167 168 169 170
static void FilterRelease( filter_t *p_filter )
{
    if( p_filter->p_module )
        module_unneed( p_filter, p_filter->p_module );
    if( p_filter->p_owner )
        free( p_filter->p_owner );
171

Laurent Aimar's avatar
Laurent Aimar committed
172 173
    vlc_object_release( p_filter );
}
174

Laurent Aimar's avatar
Laurent Aimar committed
175 176 177
static picture_t *spu_new_video_buffer( filter_t *p_filter )
{
    const video_format_t *p_fmt = &p_filter->fmt_out.video;
178

Laurent Aimar's avatar
Laurent Aimar committed
179 180 181 182
    VLC_UNUSED(p_filter);
    return picture_NewFromFormat( p_fmt );
}
static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
183
{
Laurent Aimar's avatar
Laurent Aimar committed
184 185 186
    VLC_UNUSED(p_filter);
    picture_Release( p_picture );
}
187

Laurent Aimar's avatar
Laurent Aimar committed
188 189 190 191 192
static int spu_get_attachments( filter_t *p_filter,
                                input_attachment_t ***ppp_attachment,
                                int *pi_attachment )
{
    spu_t *p_spu = p_filter->p_owner->p_spu;
193

Laurent Aimar's avatar
Laurent Aimar committed
194 195 196 197 198 199 200
    int i_ret = VLC_EGENERIC;
    if( p_spu->p->p_input )
        i_ret = input_Control( (input_thread_t*)p_spu->p->p_input,
                               INPUT_GET_ATTACHMENTS,
                               ppp_attachment, pi_attachment );
    return i_ret;
}
201

202
static filter_t *SpuRenderCreateAndLoadText( spu_t *p_spu )
Laurent Aimar's avatar
Laurent Aimar committed
203
{
204 205
    filter_t *p_text = vlc_custom_create( p_spu, sizeof(*p_text),
                                          VLC_OBJECT_GENERIC, "spu text" );
Laurent Aimar's avatar
Laurent Aimar committed
206
    if( !p_text )
207
        return NULL;
208

Laurent Aimar's avatar
Laurent Aimar committed
209 210
    p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
    p_text->p_owner->p_spu = p_spu;
211

Laurent Aimar's avatar
Laurent Aimar committed
212
    es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
213

Laurent Aimar's avatar
Laurent Aimar committed
214 215 216 217 218
    es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
    p_text->fmt_out.video.i_width =
    p_text->fmt_out.video.i_visible_width = 32;
    p_text->fmt_out.video.i_height =
    p_text->fmt_out.video.i_visible_height = 32;
219

Laurent Aimar's avatar
Laurent Aimar committed
220
    p_text->pf_get_attachments = spu_get_attachments;
221

Laurent Aimar's avatar
Laurent Aimar committed
222
    vlc_object_attach( p_text, p_spu );
223
    p_text->p_module = module_need( p_text, "text renderer", "$text-renderer", false );
Laurent Aimar's avatar
Laurent Aimar committed
224 225 226 227 228

    /* Create a few variables used for enhanced text rendering */
    var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
    var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
    var_Create( p_text, "scale", VLC_VAR_INTEGER );
229 230

    return p_text;
231 232
}

233 234 235
static filter_t *SpuRenderCreateAndLoadScale( vlc_object_t *p_obj,
                                              vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
                                              bool b_resize )
236
{
237 238
    filter_t *p_scale = vlc_custom_create( p_obj, sizeof(*p_scale),
                                           VLC_OBJECT_GENERIC, "scale" );
Laurent Aimar's avatar
Laurent Aimar committed
239 240
    if( !p_scale )
        return NULL;
241

Laurent Aimar's avatar
Laurent Aimar committed
242 243 244 245
    es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
    p_scale->fmt_in.video.i_chroma = i_src_chroma;
    p_scale->fmt_in.video.i_width =
    p_scale->fmt_in.video.i_height = 32;
246

Laurent Aimar's avatar
Laurent Aimar committed
247 248 249 250
    es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
    p_scale->fmt_out.video.i_chroma = i_dst_chroma;
    p_scale->fmt_out.video.i_width =
    p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
251

Laurent Aimar's avatar
Laurent Aimar committed
252 253
    p_scale->pf_video_buffer_new = spu_new_video_buffer;
    p_scale->pf_video_buffer_del = spu_del_video_buffer;
254

Laurent Aimar's avatar
Laurent Aimar committed
255 256
    vlc_object_attach( p_scale, p_obj );
    p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
257

Laurent Aimar's avatar
Laurent Aimar committed
258 259
    return p_scale;
}
260

Laurent Aimar's avatar
Laurent Aimar committed
261
static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
262
                           subpicture_region_t *p_region,
263
                           mtime_t render_date )
264
{
Laurent Aimar's avatar
Laurent Aimar committed
265
    filter_t *p_text = p_spu->p->p_text;
266

Laurent Aimar's avatar
Laurent Aimar committed
267
    assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
268

Laurent Aimar's avatar
Laurent Aimar committed
269
    if( !p_text || !p_text->p_module )
270
        return;
271

Laurent Aimar's avatar
Laurent Aimar committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    /* Setup 3 variables which can be used to render
     * time-dependent text (and effects). The first indicates
     * the total amount of time the text will be on screen,
     * the second the amount of time it has already been on
     * screen (can be a negative value as text is layed out
     * before it is rendered) and the third is a feedback
     * variable from the renderer - if the renderer sets it
     * then this particular text is time-dependent, eg. the
     * visual progress bar inside the text in karaoke and the
     * text needs to be rendered multiple times in order for
     * the effect to work - we therefore need to return the
     * region to its original state at the end of the loop,
     * instead of leaving it in YUVA or YUVP.
     * Any renderer which is unaware of how to render
     * time-dependent text can happily ignore the variables
     * and render the text the same as usual - it should at
     * least show up on screen, but the effect won't change
     * the text over time.
     */
    var_SetTime( p_text, "spu-elapsed", render_date );
    var_SetBool( p_text, "text-rerender", false );

    if( p_text->pf_render_html && p_region->psz_html )
    {
        p_text->pf_render_html( p_text, p_region, p_region );
297
    }
Laurent Aimar's avatar
Laurent Aimar committed
298
    else if( p_text->pf_render_text )
299
    {
Laurent Aimar's avatar
Laurent Aimar committed
300
        p_text->pf_render_text( p_text, p_region, p_region );
301
    }
Laurent Aimar's avatar
Laurent Aimar committed
302
    *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
303 304
}

305
/**
Laurent Aimar's avatar
Laurent Aimar committed
306
 * A few scale functions helpers.
307 308
 */

Laurent Aimar's avatar
Laurent Aimar committed
309 310 311 312 313 314
#define SCALE_UNIT (1000)
typedef struct
{
    int w;
    int h;
} spu_scale_t;
315

Laurent Aimar's avatar
Laurent Aimar committed
316 317 318 319 320 321 322 323 324 325 326 327 328
static spu_scale_t spu_scale_create( int w, int h )
{
    spu_scale_t s = { .w = w, .h = h };
    if( s.w <= 0 )
        s.w = SCALE_UNIT;
    if( s.h <= 0 )
        s.h = SCALE_UNIT;
    return s;
}
static spu_scale_t spu_scale_unit( void )
{
    return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
}
329
static spu_scale_t spu_scale_createq( int64_t wn, int64_t wd, int64_t hn, int64_t hd )
Laurent Aimar's avatar
Laurent Aimar committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
{
    return spu_scale_create( wn * SCALE_UNIT / wd,
                             hn * SCALE_UNIT / hd );
}
static int spu_scale_w( int v, const spu_scale_t s )
{
    return v * s.w / SCALE_UNIT;
}
static int spu_scale_h( int v, const spu_scale_t s )
{
    return v * s.h / SCALE_UNIT;
}
static int spu_invscale_w( int v, const spu_scale_t s )
{
    return v * SCALE_UNIT / s.w;
}
static int spu_invscale_h( int v, const spu_scale_t s )
{
    return v * SCALE_UNIT / s.h;
349 350
}

351
/**
Laurent Aimar's avatar
Laurent Aimar committed
352
 * A few area functions helpers
353
 */
Laurent Aimar's avatar
Laurent Aimar committed
354
typedef struct
Sam Hocevar's avatar
 
Sam Hocevar committed
355
{
Laurent Aimar's avatar
Laurent Aimar committed
356 357 358 359
    int i_x;
    int i_y;
    int i_width;
    int i_height;
360

Laurent Aimar's avatar
Laurent Aimar committed
361 362
    spu_scale_t scale;
} spu_area_t;
363

Laurent Aimar's avatar
Laurent Aimar committed
364 365 366 367
static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
{
    spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
    return a;
Sam Hocevar's avatar
 
Sam Hocevar committed
368
}
Laurent Aimar's avatar
Laurent Aimar committed
369
static spu_area_t spu_area_scaled( spu_area_t a )
370
{
Laurent Aimar's avatar
Laurent Aimar committed
371 372
    if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
        return a;
373

Laurent Aimar's avatar
Laurent Aimar committed
374 375
    a.i_x = spu_scale_w( a.i_x, a.scale );
    a.i_y = spu_scale_h( a.i_y, a.scale );
376

Laurent Aimar's avatar
Laurent Aimar committed
377 378
    a.i_width  = spu_scale_w( a.i_width,  a.scale );
    a.i_height = spu_scale_h( a.i_height, a.scale );
379

Laurent Aimar's avatar
Laurent Aimar committed
380 381 382 383 384 385 386
    a.scale = spu_scale_unit();
    return a;
}
static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
{
    if( a.scale.w == s.w && a.scale.h == s.h )
        return a;
387

Laurent Aimar's avatar
Laurent Aimar committed
388
    a = spu_area_scaled( a );
389

Laurent Aimar's avatar
Laurent Aimar committed
390 391
    a.i_x = spu_invscale_w( a.i_x, s );
    a.i_y = spu_invscale_h( a.i_y, s );
392

Laurent Aimar's avatar
Laurent Aimar committed
393 394
    a.i_width  = spu_invscale_w( a.i_width, s );
    a.i_height = spu_invscale_h( a.i_height, s );
395

Laurent Aimar's avatar
Laurent Aimar committed
396 397 398 399 400 401 402
    a.scale = s;
    return a;
}
static bool spu_area_overlap( spu_area_t a, spu_area_t b )
{
    const int i_dx = 0;
    const int i_dy = 0;
403

Laurent Aimar's avatar
Laurent Aimar committed
404 405
    a = spu_area_scaled( a );
    b = spu_area_scaled( b );
406

Laurent Aimar's avatar
Laurent Aimar committed
407 408 409
    return  __MAX( a.i_x-i_dx, b.i_x ) < __MIN( a.i_x+a.i_width +i_dx, b.i_x+b.i_width  ) &&
            __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
}
Laurent Aimar's avatar
Laurent Aimar committed
410

Laurent Aimar's avatar
Laurent Aimar committed
411 412 413 414 415 416 417 418 419
/**
 * Avoid area overlapping
 */
static void SpuAreaFixOverlap( spu_area_t *p_dst,
                               const spu_area_t *p_sub, int i_sub, int i_align )
{
    spu_area_t a = spu_area_scaled( *p_dst );
    bool b_moved = false;
    bool b_ok;
420

Laurent Aimar's avatar
Laurent Aimar committed
421 422 423
    /* Check for overlap
     * XXX It is not fast O(n^2) but we should not have a lot of region */
    do
Laurent Aimar's avatar
Laurent Aimar committed
424
    {
Laurent Aimar's avatar
Laurent Aimar committed
425 426
        b_ok = true;
        for( int i = 0; i < i_sub; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
427
        {
Laurent Aimar's avatar
Laurent Aimar committed
428
            spu_area_t sub = spu_area_scaled( p_sub[i] );
429

Laurent Aimar's avatar
Laurent Aimar committed
430 431
            if( !spu_area_overlap( a, sub ) )
                continue;
432

Laurent Aimar's avatar
Laurent Aimar committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            if( i_align & SUBPICTURE_ALIGN_TOP )
            {
                /* We go down */
                int i_y = sub.i_y + sub.i_height;
                a.i_y = i_y;
                b_moved = true;
            }
            else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
            {
                /* We go up */
                int i_y = sub.i_y - a.i_height;
                a.i_y = i_y;
                b_moved = true;
            }
            else
            {
                /* TODO what to do in this case? */
                //fprintf( stderr, "Overlap with unsupported alignment\n" );
                break;
            }
453

Laurent Aimar's avatar
Laurent Aimar committed
454 455
            b_ok = false;
            break;
Laurent Aimar's avatar
Laurent Aimar committed
456
        }
Laurent Aimar's avatar
Laurent Aimar committed
457
    } while( !b_ok );
458

Laurent Aimar's avatar
Laurent Aimar committed
459 460 461
    if( b_moved )
        *p_dst = spu_area_unscaled( a, p_dst->scale );
}
462

463

Laurent Aimar's avatar
Laurent Aimar committed
464 465 466
static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
{
  spu_area_t a = spu_area_scaled( *p_area );
467

Laurent Aimar's avatar
Laurent Aimar committed
468 469 470 471 472
  const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
  if( i_error_x > 0 )
      a.i_x -= i_error_x;
  if( a.i_x < 0 )
      a.i_x = 0;
473

Laurent Aimar's avatar
Laurent Aimar committed
474 475 476 477 478
  const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
  if( i_error_y > 0 )
      a.i_y -= i_error_y;
  if( a.i_y < 0 )
      a.i_y = 0;
479

Laurent Aimar's avatar
Laurent Aimar committed
480 481
  *p_area = spu_area_unscaled( a, p_area->scale );
}
482

Laurent Aimar's avatar
Laurent Aimar committed
483 484 485 486 487 488 489 490
/**
 * Place a region
 */
static void SpuRegionPlace( int *pi_x, int *pi_y,
                            const subpicture_t *p_subpic,
                            const subpicture_region_t *p_region )
{
    assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
491
    if( p_subpic->b_absolute )
Laurent Aimar's avatar
Laurent Aimar committed
492
    {
493 494
        *pi_x = p_region->i_x;
        *pi_y = p_region->i_y;
Laurent Aimar's avatar
Laurent Aimar committed
495 496 497
    }
    else
    {
498 499 500 501 502 503 504 505 506 507 508 509
        if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
        {
            *pi_y = p_region->i_y;
        }
        else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
        {
            *pi_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - p_region->i_y;
        }
        else
        {
            *pi_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
        }
510

511 512 513 514 515 516 517 518 519 520 521 522
        if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
        {
            *pi_x = p_region->i_x;
        }
        else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
        {
            *pi_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - p_region->i_x;
        }
        else
        {
            *pi_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
        }
Laurent Aimar's avatar
Laurent Aimar committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    }
}

/**
 * This function compares two 64 bits integers.
 * It can be used by qsort.
 */
static int IntegerCmp( int64_t i0, int64_t i1 )
{
    return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
}
/**
 * This function compares 2 subpictures using the following properties
 * (ordered by priority)
 * 1. absolute positionning
 * 2. start time
 * 3. creation order (per channel)
 *
 * It can be used by qsort.
 *
 * XXX spu_RenderSubpictures depends heavily on this order.
 */
static int SubpictureCmp( const void *s0, const void *s1 )
{
    subpicture_t *p_subpic0 = *(subpicture_t**)s0;
    subpicture_t *p_subpic1 = *(subpicture_t**)s1;
    int r;

    r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
    if( !r )
        r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
    if( !r )
        r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
    if( !r )
        r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
    return r;
}

Laurent Aimar's avatar
Laurent Aimar committed
561
/*****************************************************************************
562
 * SpuSelectSubpictures: find the subpictures to display
Laurent Aimar's avatar
Laurent Aimar committed
563 564 565 566 567 568 569 570
 *****************************************************************************
 * This function parses all subpictures and decides which ones need to be
 * displayed. 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.
 *****************************************************************************/
571 572 573 574 575 576
static void SpuSelectSubpictures( spu_t *p_spu,
                                  unsigned int *pi_subpicture,
                                  subpicture_t **pp_subpicture,
                                  mtime_t render_subtitle_date,
                                  mtime_t render_osd_date,
                                  bool b_subtitle_only )
Laurent Aimar's avatar
Laurent Aimar committed
577
{
Laurent Aimar's avatar
Laurent Aimar committed
578
    spu_private_t *p_sys = p_spu->p;
579 580 581

    /* */
    *pi_subpicture = 0;
Laurent Aimar's avatar
Laurent Aimar committed
582

Laurent Aimar's avatar
Laurent Aimar committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    /* Create a list of channels */
    int pi_channel[VOUT_MAX_SUBPICTURES];
    int i_channel_count = 0;

    for( int i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
    {
        spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
        if( !p_entry->p_subpicture || p_entry->b_reject )
            continue;
        const int i_channel = p_entry->p_subpicture->i_channel;
        int i;
        for( i = 0; i < i_channel_count; i++ )
        {
            if( pi_channel[i] == i_channel )
                break;
        }
        if( i_channel_count <= i )
            pi_channel[i_channel_count++] = i_channel;
    }

603
    /* Fill up the pp_subpicture arrays with relevent pictures */
Laurent Aimar's avatar
Laurent Aimar committed
604
    for( int i = 0; i < i_channel_count; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
605
    {
Laurent Aimar's avatar
Laurent Aimar committed
606
        const int i_channel = pi_channel[i];
607 608 609 610
        subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
        bool         pb_available_late[VOUT_MAX_SUBPICTURES];
        int          i_available = 0;

611
        mtime_t      start_date = render_subtitle_date;
612
        mtime_t      ephemer_subtitle_date = 0;
613
        mtime_t      ephemer_osd_date = 0;
614 615
        int64_t      i_ephemer_subtitle_order = INT64_MIN;
        int64_t      i_ephemer_system_order = INT64_MIN;
Laurent Aimar's avatar
Laurent Aimar committed
616 617
        int i_index;

618
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
619 620 621 622
        for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
        {
            spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
            subpicture_t *p_current = p_entry->p_subpicture;
623 624
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
625 626 627 628 629 630 631 632 633 634 635 636 637

            if( !p_current || p_entry->b_reject )
            {
                if( p_entry->b_reject )
                    SpuHeapDeleteAt( &p_sys->heap, i_index );
                continue;
            }

            if( p_current->i_channel != i_channel ||
                ( b_subtitle_only && !p_current->b_subtitle ) )
            {
                continue;
            }
638
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
639 640
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
641 642 643 644 645
            {
                /* Too early, come back next monday */
                continue;
            }

646
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
647 648 649
            int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
            if( p_current->i_start >= *pi_ephemer_date )
            {
650
                *pi_ephemer_date = p_current->i_start;
651 652 653
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
654

655
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
656

657
            b_late = b_stop_valid && p_current->i_stop <= render_date;
658 659

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
660
             * in case picture that should not be displayed anymore (display_time)
661
             * overlap with a picture to be displayed (p_current->i_start)  */
662
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
663 664 665 666 667 668
                start_date = p_current->i_start;

            /* */
            p_available_subpic[i_available] = p_current;
            pb_available_late[i_available] = b_late;
            i_available++;
Laurent Aimar's avatar
Laurent Aimar committed
669 670
        }

671 672 673 674 675 676 677 678
        /* Only forced old picture display at the transition */
        if( start_date < p_sys->i_last_sort_date )
            start_date = p_sys->i_last_sort_date;
        if( start_date <= 0 )
            start_date = INT64_MAX;

        /* Select pictures to be displayed */
        for( i_index = 0; i_index < i_available; i_index++ )
Laurent Aimar's avatar
Laurent Aimar committed
679
        {
680 681
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
682

683 684
            const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
            const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
685
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
686

687 688 689
            /* Destroy late and obsolete ephemer subpictures */
            bool b_rejet = b_late && p_current->i_stop <= stop_date;
            if( p_current->b_ephemer )
Laurent Aimar's avatar
Laurent Aimar committed
690
            {
691 692 693 694 695
                if( p_current->i_start < ephemer_date )
                    b_rejet = true;
                else if( p_current->i_start == ephemer_date &&
                         p_current->i_order < i_ephemer_order )
                    b_rejet = true;
Laurent Aimar's avatar
Laurent Aimar committed
696
            }
697 698 699

            if( b_rejet )
                SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
700
            else
701
                pp_subpicture[(*pi_subpicture)++] = p_current;
Laurent Aimar's avatar
Laurent Aimar committed
702
        }
Laurent Aimar's avatar
Laurent Aimar committed
703
    }
704

705
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
706 707
}

Laurent Aimar's avatar
Laurent Aimar committed
708 709 710 711 712 713 714 715 716 717


/**
 * It will transform the provided region into another region suitable for rendering.
 */

static void SpuRenderRegion( spu_t *p_spu,
                             subpicture_region_t **pp_dst, spu_area_t *p_dst_area,
                             subpicture_t *p_subpic, subpicture_region_t *p_region,
                             const spu_scale_t scale_size,
718
                             const vlc_fourcc_t *p_chroma_list,
Laurent Aimar's avatar
Laurent Aimar committed
719 720 721
                             const video_format_t *p_fmt,
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date )
722 723 724
{
    spu_private_t *p_sys = p_spu->p;

Laurent Aimar's avatar
Laurent Aimar committed
725
    video_format_t fmt_original = p_region->fmt;
726
    bool b_restore_text = false;
Laurent Aimar's avatar
Laurent Aimar committed
727 728
    int i_x_offset;
    int i_y_offset;
729

Laurent Aimar's avatar
Laurent Aimar committed
730 731
    video_format_t region_fmt;
    picture_t *p_region_picture;
732

Laurent Aimar's avatar
Laurent Aimar committed
733 734 735
    /* Invalidate area by default */
    *p_dst_area = spu_area_create( 0,0, 0,0, scale_size );
    *pp_dst     = NULL;
736

Laurent Aimar's avatar
Laurent Aimar committed
737 738 739
    /* Render text region */
    if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
    {
740
        SpuRenderText( p_spu, &b_restore_text, p_region,
741
                       render_date );
Laurent Aimar's avatar
Laurent Aimar committed
742 743 744 745

        /* Check if the rendering has failed ... */
        if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
            goto exit;
746 747
    }

Laurent Aimar's avatar
Laurent Aimar committed
748 749 750 751 752 753 754 755
    /* Force palette if requested
     * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
     * instead of only the right one (being the dvd spu).
     */
    const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
    const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
    const bool b_force_crop    = b_force_palette && p_sys->b_force_crop;
    bool b_changed_palette     = false;
756

Laurent Aimar's avatar
Laurent Aimar committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    /* Compute the margin which is expressed in destination pixel unit
     * The margin is applied only to subtitle and when no forced crop is
     * requested (dvd menu) */
    int i_margin_y = 0;
    if( !b_force_crop && p_subpic->b_subtitle )
        i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );

    /* Place the picture
     * We compute the position in the rendered size */
    SpuRegionPlace( &i_x_offset, &i_y_offset,
                    p_subpic, p_region );

    /* Save this position for subtitle overlap support
     * it is really important that there are given without scale_size applied */
    *p_dst_area = spu_area_create( i_x_offset, i_y_offset,
                               p_region->fmt.i_width, p_region->fmt.i_height,
                               scale_size );

    /* Handle overlapping subtitles when possible */
    if( p_subpic->b_subtitle && !p_subpic->b_absolute )
777
    {
Laurent Aimar's avatar
Laurent Aimar committed
778 779
        SpuAreaFixOverlap( p_dst_area, p_subtitle_area, i_subtitle_area,
                           p_region->i_align );
780 781
    }

Laurent Aimar's avatar
Laurent Aimar committed
782 783 784
    /* we copy the area: for the subtitle overlap support we want
     * to only save the area without margin applied */
    spu_area_t restrained = *p_dst_area;
785

Laurent Aimar's avatar
Laurent Aimar committed
786 787 788
    /* apply margin to subtitles and correct if they go over the picture edge */
    if( p_subpic->b_subtitle )
        restrained.i_y -= i_margin_y;
789

Laurent Aimar's avatar
Laurent Aimar committed
790 791 792
    spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
                                          spu_scale_unit() );
    SpuAreaFitInside( &restrained, &display );
Laurent Aimar's avatar
Laurent Aimar committed
793

Laurent Aimar's avatar
Laurent Aimar committed
794 795 796 797 798 799
    /* Fix the position for the current scale_size */
    i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
    i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );

    /* */
    if( b_force_palette )
Laurent Aimar's avatar
Laurent Aimar committed
800
    {
Laurent Aimar's avatar
Laurent Aimar committed
801 802
        video_palette_t *p_palette = p_region->fmt.p_palette;
        video_palette_t palette;
Laurent Aimar's avatar
Laurent Aimar committed
803

Laurent Aimar's avatar
Laurent Aimar committed
804 805 806 807 808 809 810
        /* We suppose DVD palette here */
        palette.i_entries = 4;
        for( int i = 0; i < 4; i++ )
            for( int j = 0; j < 4; j++ )
                palette.palette[i][j] = p_sys->palette[i][j];

        if( p_palette->i_entries == palette.i_entries )
Laurent Aimar's avatar
Laurent Aimar committed
811
        {
Laurent Aimar's avatar
Laurent Aimar committed
812 813 814 815 816 817 818
            for( int i = 0; i < p_palette->i_entries; i++ )
                for( int j = 0; j < 4; j++ )
                    b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
        }
        else
        {
            b_changed_palette = true;
Laurent Aimar's avatar
Laurent Aimar committed
819
        }
Laurent Aimar's avatar
Laurent Aimar committed
820
        *p_palette = palette;
Laurent Aimar's avatar
Laurent Aimar committed
821 822
    }

Laurent Aimar's avatar
Laurent Aimar committed
823 824 825
    /* */
    region_fmt = p_region->fmt;
    p_region_picture = p_region->p_picture;
Laurent Aimar's avatar
Laurent Aimar committed
826

827 828 829 830 831 832
    bool b_convert_chroma = true;
    for( int i = 0; p_chroma_list[i] && b_convert_chroma; i++ )
    {
        if( region_fmt.i_chroma == p_chroma_list[i] )
            b_convert_chroma = false;
    }
Laurent Aimar's avatar
Laurent Aimar committed
833

Laurent Aimar's avatar
Laurent Aimar committed
834 835 836
    /* Scale from rendered size to destination size */
    if( p_sys->p_scale && p_sys->p_scale->p_module &&
        ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
837 838
        ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
          b_using_palette || b_convert_chroma) )
Laurent Aimar's avatar
Laurent Aimar committed
839 840 841
    {
        const unsigned i_dst_width  = spu_scale_w( p_region->fmt.i_width, scale_size );
        const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
Laurent Aimar's avatar
Laurent Aimar committed
842

Laurent Aimar's avatar
Laurent Aimar committed
843 844 845 846 847
        /* Destroy the cache if unusable */
        if( p_region->p_private )
        {
            subpicture_region_private_t *p_private = p_region->p_private;
            bool b_changed = false;
Laurent Aimar's avatar
Laurent Aimar committed
848

Laurent Aimar's avatar
Laurent Aimar committed
849 850 851 852
            /* Check resize changes */
            if( i_dst_width  != p_private->fmt.i_width ||
                i_dst_height != p_private->fmt.i_height )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
853

Laurent Aimar's avatar
Laurent Aimar committed
854 855 856
            /* Check forced palette changes */
            if( b_changed_palette )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
857

858 859 860
            if( b_convert_chroma && p_private->fmt.i_chroma != p_chroma_list[0] )
                b_changed = true;

Laurent Aimar's avatar
Laurent Aimar committed
861 862 863 864 865 866
            if( b_changed )
            {
                subpicture_region_private_Delete( p_private );
                p_region->p_private = NULL;
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
867

Laurent Aimar's avatar
Laurent Aimar committed
868 869 870 871
        /* Scale if needed into cache */
        if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
        {
            filter_t *p_scale = p_sys->p_scale;
Laurent Aimar's avatar
Laurent Aimar committed
872

Laurent Aimar's avatar
Laurent Aimar committed
873 874
            picture_t *p_picture = p_region->p_picture;
            picture_Hold( p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
875

Laurent Aimar's avatar
Laurent Aimar committed
876 877 878 879
            /* Convert YUVP to YUVA/RGBA first for better scaling quality */
            if( b_using_palette )
            {
                filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
880

Laurent Aimar's avatar
Laurent Aimar committed
881
                p_scale_yuvp->fmt_in.video = p_region->fmt;
882

Laurent Aimar's avatar
Laurent Aimar committed
883
                p_scale_yuvp->fmt_out.video = p_region->fmt;
884
                p_scale_yuvp->fmt_out.video.i_chroma = p_chroma_list[0];
885

Laurent Aimar's avatar
Laurent Aimar committed
886 887 888 889 890 891 892 893 894
                p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
                if( !p_picture )
                {
                    /* Well we will try conversion+scaling */
                    msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
                             (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
                             (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
                }
            }
895

Laurent Aimar's avatar
Laurent Aimar committed
896 897 898
            /* Conversion(except from YUVP)/Scaling */
            if( p_picture &&
                ( p_picture->format.i_width != i_dst_width ||
899 900
                  p_picture->format.i_height != i_dst_height ||
                  ( b_convert_chroma && !b_using_palette ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
901 902 903
            {
                p_scale->fmt_in.video = p_picture->format;
                p_scale->fmt_out.video = p_picture->format;
904 905 906
                if( b_convert_chroma )
                    p_scale->fmt_out.i_codec        =
                    p_scale->fmt_out.video.i_chroma = p_chroma_list[0];
Laurent Aimar's avatar
Laurent Aimar committed
907

Laurent Aimar's avatar
Laurent Aimar committed
908 909
                p_scale->fmt_out.video.i_width = i_dst_width;
                p_scale->fmt_out.video.i_height = i_dst_height;
Laurent Aimar's avatar
Laurent Aimar committed
910

Laurent Aimar's avatar
Laurent Aimar committed
911 912 913 914
                p_scale->fmt_out.video.i_visible_width =
                    spu_scale_w( p_region->fmt.i_visible_width, scale_size );
                p_scale->fmt_out.video.i_visible_height =
                    spu_scale_h( p_region->fmt.i_visible_height, scale_size );
Laurent Aimar's avatar
Laurent Aimar committed
915

Laurent Aimar's avatar
Laurent Aimar committed
916 917 918 919
                p_picture = p_scale->pf_video_filter( p_scale, p_picture );
                if( !p_picture )
                    msg_Err( p_spu, "scaling failed" );
            }
Laurent Aimar's avatar
Laurent Aimar committed
920

Laurent Aimar's avatar
Laurent Aimar committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
            /* */
            if( p_picture )
            {
                p_region->p_private = subpicture_region_private_New( &p_picture->format );
                if( p_region->p_private )
                {
                    p_region->p_private->p_picture = p_picture;
                    if( !p_region->p_private->p_picture )
                    {
                        subpicture_region_private_Delete( p_region->p_private );
                        p_region->p_private = NULL;
                    }
                }
                else
                {
                    picture_Release( p_picture );
                }
            }
        }
940

Laurent Aimar's avatar
Laurent Aimar committed
941 942 943 944 945 946
        /* And use the scaled picture */
        if( p_region->p_private )
        {
            region_fmt = p_region->p_private->fmt;
            p_region_picture = p_region->p_private->p_picture;
        }
Laurent Aimar's avatar
Laurent Aimar committed
947
    }
948

Laurent Aimar's avatar
Laurent Aimar committed
949 950
    /* Force cropping if requested */
    if( b_force_crop )
951
    {
Laurent Aimar's avatar
Laurent Aimar committed
952 953 954 955
        int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
        int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
        int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
        int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
956

Laurent Aimar's avatar
Laurent Aimar committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
        /* Find the intersection */
        if( i_crop_x + i_crop_width <= i_x_offset ||
            i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
            i_crop_y + i_crop_height <= i_y_offset ||
            i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
        {
            /* No intersection */
            region_fmt.i_visible_width =
            region_fmt.i_visible_height = 0;
        }
        else
        {
            int i_x, i_y, i_x_end, i_y_end;
            i_x = __MAX( i_crop_x, i_x_offset );
            i_y = __MAX( i_crop_y, i_y_offset );
            i_x_end = __MIN( i_crop_x + i_crop_width,
                           i_x_offset + (int)region_fmt.i_visible_width );
            i_y_end = __MIN( i_crop_y + i_crop_height,
                           i_y_offset + (int)region_fmt.i_visible_height );
976

Laurent Aimar's avatar
Laurent Aimar committed
977 978 979 980
            region_fmt.i_x_offset = i_x - i_x_offset;
            region_fmt.i_y_offset = i_y - i_y_offset;
            region_fmt.i_visible_width = i_x_end - i_x;
            region_fmt.i_visible_height = i_y_end - i_y;
981

Laurent Aimar's avatar
Laurent Aimar committed
982 983 984 985
            i_x_offset = __MAX( i_x, 0 );
            i_y_offset = __MAX( i_y, 0 );
        }
    }
986

Laurent Aimar's avatar
Laurent Aimar committed
987 988
    subpicture_region_t *p_dst = *pp_dst = subpicture_region_New( &region_fmt );
    if( p_dst )
Laurent Aimar's avatar
Laurent Aimar committed
989
    {
Laurent Aimar's avatar
Laurent Aimar committed
990 991 992
        p_dst->i_x       = i_x_offset;
        p_dst->i_y       = i_y_offset;
        p_dst->i_align   = 0;
993 994
        if( p_dst->p_picture )
            picture_Release( p_dst->p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
995 996 997 998 999 1000
        p_dst->p_picture = picture_Hold( p_region_picture );
        int i_fade_alpha = 255;
        if( p_subpic->b_fade )
        {
            mtime_t fade_start = ( p_subpic->i_stop +
                                   p_subpic->i_start ) / 2;
Laurent Aimar's avatar
Laurent Aimar committed
1001

Laurent Aimar's avatar
Laurent Aimar committed
1002 1003 1004 1005 1006
            if( fade_start <= render_date && fade_start < p_subpic->i_stop )
                i_fade_alpha = 255 * ( p_subpic->i_stop - render_date ) /
                                     ( p_subpic->i_stop - fade_start );
        }
        p_dst->i_alpha   = i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
Laurent Aimar's avatar
Laurent Aimar committed
1007 1008
    }

Laurent Aimar's avatar
Laurent Aimar committed
1009
exit:
1010
    if( b_restore_text )
Laurent Aimar's avatar
Laurent Aimar committed
1011
    {
Laurent Aimar's avatar
Laurent Aimar committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
        /* Some forms of subtitles need to be re-rendered more than
         * once, eg. karaoke. We therefore restore the region to its
         * pre-rendered state, so the next time through everything is
         * calculated again.
         */
        if( p_region->p_picture )
        {
            picture_Release( p_region->p_picture );
            p_region->p_picture = NULL;
        }
        if( p_region->p_private )
        {
            subpicture_region_private_Delete( p_region->p_private );
            p_region->p_private = NULL;
        }
        p_region->fmt = fmt_original;
1028
    }
Laurent Aimar's avatar
Laurent Aimar committed
1029
}
1030

Laurent Aimar's avatar
Laurent Aimar committed
1031 1032 1033 1034
/**
 * This function renders all sub picture units in the list.
 */
static subpicture_t *SpuRenderSubpictures( spu_t *p_spu,
1035 1036
                                           unsigned int i_subpicture,
                                           subpicture_t **pp_subpicture,