vout_subpictures.c 55.3 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 */
86 87 88 89 90 91
    char           *psz_source_chain_update;
    vlc_mutex_t    source_chain_lock;
    filter_chain_t *p_source_chain;
    char           *psz_filter_chain_update;
    vlc_mutex_t    filter_chain_lock;
    filter_chain_t *p_filter_chain;
92 93 94

    /* */
    mtime_t i_last_sort_date;
95 96
};

Laurent Aimar's avatar
Laurent Aimar committed
97 98 99 100
/*****************************************************************************
 * heap managment
 *****************************************************************************/
static void SpuHeapInit( spu_heap_t *p_heap )
Laurent Aimar's avatar
Laurent Aimar committed
101
{
Laurent Aimar's avatar
Laurent Aimar committed
102 103 104
    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
105

Laurent Aimar's avatar
Laurent Aimar committed
106 107 108 109 110 111
        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
112
{
Laurent Aimar's avatar
Laurent Aimar committed
113 114 115
    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
116

Laurent Aimar's avatar
Laurent Aimar committed
117 118
        if( e->p_subpicture )
            continue;
Laurent Aimar's avatar
Laurent Aimar committed
119

Laurent Aimar's avatar
Laurent Aimar committed
120 121 122 123 124 125
        e->p_subpicture = p_subpic;
        e->b_reject = false;
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
Laurent Aimar's avatar
Laurent Aimar committed
126

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

Laurent Aimar's avatar
Laurent Aimar committed
131 132
    if( e->p_subpicture )
        subpicture_Delete( e->p_subpicture );
133

Laurent Aimar's avatar
Laurent Aimar committed
134 135
    e->p_subpicture = NULL;
}
Laurent Aimar's avatar
Laurent Aimar committed
136

Laurent Aimar's avatar
Laurent Aimar committed
137 138 139 140 141
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
142

Laurent Aimar's avatar
Laurent Aimar committed
143 144 145 146 147 148 149 150
        if( e->p_subpicture != p_subpic )
            continue;

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

Laurent Aimar's avatar
Laurent Aimar committed
152 153 154 155 156 157 158 159 160
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 );
    }
}
161

162 163 164 165 166
struct filter_owner_sys_t
{
    spu_t *p_spu;
    int i_channel;
};
167

Laurent Aimar's avatar
Laurent Aimar committed
168 169 170 171 172 173
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 );
174

Laurent Aimar's avatar
Laurent Aimar committed
175 176
    vlc_object_release( p_filter );
}
177

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

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

Laurent Aimar's avatar
Laurent Aimar committed
191 192 193 194 195
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;
196

Laurent Aimar's avatar
Laurent Aimar committed
197 198 199 200 201 202 203
    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;
}
204

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

Laurent Aimar's avatar
Laurent Aimar committed
212 213
    p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
    p_text->p_owner->p_spu = p_spu;
214

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

Laurent Aimar's avatar
Laurent Aimar committed
217 218 219 220 221
    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;
222

Laurent Aimar's avatar
Laurent Aimar committed
223
    p_text->pf_get_attachments = spu_get_attachments;
224

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

    /* 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 );
232 233

    return p_text;
234 235
}

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

Laurent Aimar's avatar
Laurent Aimar committed
245 246 247 248
    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;
249

Laurent Aimar's avatar
Laurent Aimar committed
250 251 252 253
    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;
254

Laurent Aimar's avatar
Laurent Aimar committed
255 256
    p_scale->pf_video_buffer_new = spu_new_video_buffer;
    p_scale->pf_video_buffer_del = spu_del_video_buffer;
257

Laurent Aimar's avatar
Laurent Aimar committed
258 259
    vlc_object_attach( p_scale, p_obj );
    p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
260

Laurent Aimar's avatar
Laurent Aimar committed
261 262
    return p_scale;
}
263

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

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

Laurent Aimar's avatar
Laurent Aimar committed
272
    if( !p_text || !p_text->p_module )
273
        return;
274

Laurent Aimar's avatar
Laurent Aimar committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    /* 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 );
300
    }
Laurent Aimar's avatar
Laurent Aimar committed
301
    else if( p_text->pf_render_text )
302
    {
Laurent Aimar's avatar
Laurent Aimar committed
303
        p_text->pf_render_text( p_text, p_region, p_region );
304
    }
Laurent Aimar's avatar
Laurent Aimar committed
305
    *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
306 307
}

308
/**
Laurent Aimar's avatar
Laurent Aimar committed
309
 * A few scale functions helpers.
310 311
 */

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

Laurent Aimar's avatar
Laurent Aimar committed
319 320 321 322 323 324 325 326 327 328 329 330 331
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 );
}
332
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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
{
    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;
352 353
}

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

Laurent Aimar's avatar
Laurent Aimar committed
364 365
    spu_scale_t scale;
} spu_area_t;
366

Laurent Aimar's avatar
Laurent Aimar committed
367 368 369 370
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
371
}
Laurent Aimar's avatar
Laurent Aimar committed
372
static spu_area_t spu_area_scaled( spu_area_t a )
373
{
Laurent Aimar's avatar
Laurent Aimar committed
374 375
    if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
        return a;
376

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

Laurent Aimar's avatar
Laurent Aimar committed
380 381
    a.i_width  = spu_scale_w( a.i_width,  a.scale );
    a.i_height = spu_scale_h( a.i_height, a.scale );
382

Laurent Aimar's avatar
Laurent Aimar committed
383 384 385 386 387 388 389
    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;
390

Laurent Aimar's avatar
Laurent Aimar committed
391
    a = spu_area_scaled( a );
392

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

Laurent Aimar's avatar
Laurent Aimar committed
396 397
    a.i_width  = spu_invscale_w( a.i_width, s );
    a.i_height = spu_invscale_h( a.i_height, s );
398

Laurent Aimar's avatar
Laurent Aimar committed
399 400 401 402 403 404 405
    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;
406

Laurent Aimar's avatar
Laurent Aimar committed
407 408
    a = spu_area_scaled( a );
    b = spu_area_scaled( b );
409

Laurent Aimar's avatar
Laurent Aimar committed
410 411 412
    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
413

Laurent Aimar's avatar
Laurent Aimar committed
414 415 416 417 418 419 420 421 422
/**
 * 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;
423

Laurent Aimar's avatar
Laurent Aimar committed
424 425 426
    /* 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
427
    {
Laurent Aimar's avatar
Laurent Aimar committed
428 429
        b_ok = true;
        for( int i = 0; i < i_sub; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
430
        {
Laurent Aimar's avatar
Laurent Aimar committed
431
            spu_area_t sub = spu_area_scaled( p_sub[i] );
432

Laurent Aimar's avatar
Laurent Aimar committed
433 434
            if( !spu_area_overlap( a, sub ) )
                continue;
435

Laurent Aimar's avatar
Laurent Aimar committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
            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;
            }
456

Laurent Aimar's avatar
Laurent Aimar committed
457 458
            b_ok = false;
            break;
Laurent Aimar's avatar
Laurent Aimar committed
459
        }
Laurent Aimar's avatar
Laurent Aimar committed
460
    } while( !b_ok );
461

Laurent Aimar's avatar
Laurent Aimar committed
462 463 464
    if( b_moved )
        *p_dst = spu_area_unscaled( a, p_dst->scale );
}
465

466

Laurent Aimar's avatar
Laurent Aimar committed
467 468 469
static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
{
  spu_area_t a = spu_area_scaled( *p_area );
470

Laurent Aimar's avatar
Laurent Aimar committed
471 472 473 474 475
  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;
476

Laurent Aimar's avatar
Laurent Aimar committed
477 478 479 480 481
  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;
482

Laurent Aimar's avatar
Laurent Aimar committed
483 484
  *p_area = spu_area_unscaled( a, p_area->scale );
}
485

Laurent Aimar's avatar
Laurent Aimar committed
486 487 488 489 490 491 492 493
/**
 * 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 );
494
    if( p_subpic->b_absolute )
Laurent Aimar's avatar
Laurent Aimar committed
495
    {
496 497
        *pi_x = p_region->i_x;
        *pi_y = p_region->i_y;
Laurent Aimar's avatar
Laurent Aimar committed
498 499 500
    }
    else
    {
501 502 503 504 505 506 507 508 509 510 511 512
        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;
        }
513

514 515 516 517 518 519 520 521 522 523 524 525
        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
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 561 562 563
    }
}

/**
 * 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
564
/*****************************************************************************
565
 * SpuSelectSubpictures: find the subpictures to display
Laurent Aimar's avatar
Laurent Aimar committed
566 567 568 569 570 571 572 573
 *****************************************************************************
 * 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.
 *****************************************************************************/
574 575 576 577 578 579
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
580
{
Laurent Aimar's avatar
Laurent Aimar committed
581
    spu_private_t *p_sys = p_spu->p;
582 583 584

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

Laurent Aimar's avatar
Laurent Aimar committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    /* 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;
    }

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

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

621
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
622 623 624 625
        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;
626 627
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
628 629 630 631 632 633 634 635 636 637 638 639 640

            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;
            }
641
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
642 643
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
644 645 646 647 648
            {
                /* Too early, come back next monday */
                continue;
            }

649
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
650 651 652
            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 )
            {
653
                *pi_ephemer_date = p_current->i_start;
654 655 656
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
657

658
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
659

660
            b_late = b_stop_valid && p_current->i_stop <= render_date;
661 662

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
663
             * in case picture that should not be displayed anymore (display_time)
664
             * overlap with a picture to be displayed (p_current->i_start)  */
665
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
666 667 668 669 670 671
                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
672 673
        }

674 675 676 677 678 679 680 681
        /* 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
682
        {
683 684
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
685

686 687
            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;
688
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
689

690 691 692
            /* 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
693
            {
694 695 696 697 698
                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
699
            }
700 701 702

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

708
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
709 710
}

Laurent Aimar's avatar
Laurent Aimar committed
711 712 713 714 715 716 717 718 719 720


/**
 * 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,
721
                             const vlc_fourcc_t *p_chroma_list,
Laurent Aimar's avatar
Laurent Aimar committed
722 723 724
                             const video_format_t *p_fmt,
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date )
725 726 727
{
    spu_private_t *p_sys = p_spu->p;

Laurent Aimar's avatar
Laurent Aimar committed
728
    video_format_t fmt_original = p_region->fmt;
729
    bool b_restore_text = false;
Laurent Aimar's avatar
Laurent Aimar committed
730 731
    int i_x_offset;
    int i_y_offset;
732

Laurent Aimar's avatar
Laurent Aimar committed
733 734
    video_format_t region_fmt;
    picture_t *p_region_picture;
735

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
751 752 753 754 755 756 757 758
    /* 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;
759

Laurent Aimar's avatar
Laurent Aimar committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    /* 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 )
780
    {
Laurent Aimar's avatar
Laurent Aimar committed
781 782
        SpuAreaFixOverlap( p_dst_area, p_subtitle_area, i_subtitle_area,
                           p_region->i_align );
783 784
    }

Laurent Aimar's avatar
Laurent Aimar committed
785 786 787
    /* 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;
788

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

Laurent Aimar's avatar
Laurent Aimar committed
793 794 795
    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
796

Laurent Aimar's avatar
Laurent Aimar committed
797 798 799 800 801 802
    /* 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
803
    {
Laurent Aimar's avatar
Laurent Aimar committed
804 805
        video_palette_t *p_palette = p_region->fmt.p_palette;
        video_palette_t palette;
Laurent Aimar's avatar
Laurent Aimar committed
806

Laurent Aimar's avatar
Laurent Aimar committed
807 808 809 810 811 812 813
        /* 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
814
        {
Laurent Aimar's avatar
Laurent Aimar committed
815 816 817 818 819 820 821
            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
822
        }
Laurent Aimar's avatar
Laurent Aimar committed
823
        *p_palette = palette;
Laurent Aimar's avatar
Laurent Aimar committed
824 825
    }

Laurent Aimar's avatar
Laurent Aimar committed
826 827 828
    /* */
    region_fmt = p_region->fmt;
    p_region_picture = p_region->p_picture;
Laurent Aimar's avatar
Laurent Aimar committed
829

830 831 832 833 834 835
    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
836

Laurent Aimar's avatar
Laurent Aimar committed
837 838 839
    /* 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 ) ) &&
840 841
        ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
          b_using_palette || b_convert_chroma) )
Laurent Aimar's avatar
Laurent Aimar committed
842 843 844
    {
        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
845

Laurent Aimar's avatar
Laurent Aimar committed
846 847 848 849 850
        /* 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
851

Laurent Aimar's avatar
Laurent Aimar committed
852 853 854 855
            /* 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
856

Laurent Aimar's avatar
Laurent Aimar committed
857 858 859
            /* Check forced palette changes */
            if( b_changed_palette )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
860

861 862 863
            if( b_convert_chroma && p_private->fmt.i_chroma != p_chroma_list[0] )
                b_changed = true;

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

Laurent Aimar's avatar
Laurent Aimar committed
871 872 873 874
        /* 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
875

Laurent Aimar's avatar
Laurent Aimar committed
876 877
            picture_t *p_picture = p_region->p_picture;
            picture_Hold( p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
878

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

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

Laurent Aimar's avatar
Laurent Aimar committed
886
                p_scale_yuvp->fmt_out.video = p_region->fmt;
887
                p_scale_yuvp->fmt_out.video.i_chroma = p_chroma_list[0];
888

Laurent Aimar's avatar
Laurent Aimar committed
889 890 891 892 893 894 895 896 897
                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 );
                }
            }
898

Laurent Aimar's avatar
Laurent Aimar committed
899 900 901
            /* Conversion(except from YUVP)/Scaling */
            if( p_picture &&
                ( p_picture->format.i_width != i_dst_width ||
902 903
                  p_picture->format.i_height != i_dst_height ||
                  ( b_convert_chroma && !b_using_palette ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
904 905 906
            {
                p_scale->fmt_in.video = p_picture->format;
                p_scale->fmt_out.video = p_picture->format;
907 908 909
                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
910

Laurent Aimar's avatar
Laurent Aimar committed
911 912
                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
913

Laurent Aimar's avatar
Laurent Aimar committed
914 915 916 917
                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
918

Laurent Aimar's avatar
Laurent Aimar committed
919 920 921 922
                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
923

Laurent Aimar's avatar
Laurent Aimar committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
            /* */
            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 );
                }
            }
        }
943

Laurent Aimar's avatar
Laurent Aimar committed
944 945 946 947 948 949
        /* 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
950
    }
951

Laurent Aimar's avatar
Laurent Aimar committed
952 953
    /* Force cropping if requested */
    if( b_force_crop )
954
    {
Laurent Aimar's avatar
Laurent Aimar committed
955 956 957 958
        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 );
959

Laurent Aimar's avatar
Laurent Aimar committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
        /* 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 );
979

Laurent Aimar's avatar
Laurent Aimar committed
980 981 982 983
            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;
984

Laurent Aimar's avatar
Laurent Aimar committed
985 986 987 988
            i_x_offset = __MAX( i_x, 0 );
            i_y_offset = __MAX( i_y, 0 );
        }
    }
989

Laurent Aimar's avatar
Laurent Aimar committed
990 991
    subpicture_region_t *p_dst = *pp_dst = subpicture_region_New( &region_fmt );
    if( p_dst )
Laurent Aimar's avatar
Laurent Aimar committed
992
    {
Laurent Aimar's avatar
Laurent Aimar committed
993 994 995
        p_dst->i_x       = i_x_offset;
        p_dst->i_y       = i_y_offset;
        p_dst->i_align   = 0;
996 997
        if( p_dst->p_picture )
            picture_Release( p_dst->p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
998 999 1000 1001 1002 1003
        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
1004

Laurent Aimar's avatar
Laurent Aimar committed
1005 1006 1007 1008 1009
            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
1010 1011
    }

Laurent Aimar's avatar
Laurent Aimar committed
1012
exit:
1013
    if( b_restore_text )
Laurent Aimar's avatar
Laurent Aimar committed
1014
    {
Laurent Aimar's avatar
Laurent Aimar committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
        /* 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;
1031
    }
Laurent Aimar's avatar
Laurent Aimar committed
1032
}
1033

Laurent Aimar's avatar
Laurent Aimar committed
1034 1035 1036 1037
/**
 * This function renders all sub picture units in the list.
 */
static subpicture_t *SpuRenderSubpictures( spu_t *p_spu,
1038 1039
                                           unsigned int i_subpicture,
                                           subpicture_t **pp_subpicture,
1040
                                           const vlc_fourcc_t *p_chroma_list,