vout_subpictures.c 53.9 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

Laurent Aimar's avatar
Laurent Aimar committed
202 203 204
static void SpuRenderCreateAndLoadText( spu_t *p_spu )
{
    filter_t *p_text;
205

Laurent Aimar's avatar
Laurent Aimar committed
206
    assert( !p_spu->p->p_text );
207

Laurent Aimar's avatar
Laurent Aimar committed
208 209 210 211 212
    p_spu->p->p_text =
    p_text        = vlc_custom_create( p_spu, sizeof(filter_t),
                                       VLC_OBJECT_GENERIC, "spu text" );
    if( !p_text )
        return;
213

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
225
    p_text->pf_get_attachments = spu_get_attachments;
226

Laurent Aimar's avatar
Laurent Aimar committed
227
    vlc_object_attach( p_text, p_spu );
228

Laurent Aimar's avatar
Laurent Aimar committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
     * same than these 2 calls ? */
    char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
    if( psz_modulename && *psz_modulename )
    {
        p_text->p_module = module_need( p_text, "text renderer",
                                        psz_modulename, true );
    }
    free( psz_modulename );

    if( !p_text->p_module )
        p_text->p_module = module_need( p_text, "text renderer", NULL, false );

    /* Create a few variables used for enhanced text rendering */
    var_Create( p_text, "spu-duration", VLC_VAR_TIME );
    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 );
247 248
}

Laurent Aimar's avatar
Laurent Aimar committed
249 250 251
static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
                                     vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
                                     bool b_resize )
252
{
Laurent Aimar's avatar
Laurent Aimar committed
253
    filter_t *p_scale;
254

Laurent Aimar's avatar
Laurent Aimar committed
255 256 257 258
    p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
                                 VLC_OBJECT_GENERIC, "scale" );
    if( !p_scale )
        return NULL;
259

Laurent Aimar's avatar
Laurent Aimar committed
260 261 262 263
    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;
264

Laurent Aimar's avatar
Laurent Aimar committed
265 266 267 268
    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;
269

Laurent Aimar's avatar
Laurent Aimar committed
270 271
    p_scale->pf_video_buffer_new = spu_new_video_buffer;
    p_scale->pf_video_buffer_del = spu_del_video_buffer;
272

Laurent Aimar's avatar
Laurent Aimar committed
273 274
    vlc_object_attach( p_scale, p_obj );
    p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
275

Laurent Aimar's avatar
Laurent Aimar committed
276 277 278 279 280 281 282 283 284
    return p_scale;
}
static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
{
    assert( !p_spu->p->p_scale );
    assert( !p_spu->p->p_scale_yuvp );
    /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
     * yuva/rgba */
    p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
285
                                            VLC_CODEC_YUVA, VLC_CODEC_RGBA, true );
Laurent Aimar's avatar
Laurent Aimar committed
286 287 288 289
    /* This one is used for YUVP to YUVA/RGBA without scaling
     * FIXME rename it */
    p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
                                                 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
290 291
}

Laurent Aimar's avatar
Laurent Aimar committed
292 293 294
static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
                           subpicture_t *p_subpic, subpicture_region_t *p_region,
                           int i_min_scale_ratio, mtime_t render_date )
295
{
Laurent Aimar's avatar
Laurent Aimar committed
296
    filter_t *p_text = p_spu->p->p_text;
297

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

Laurent Aimar's avatar
Laurent Aimar committed
300
    if( !p_text || !p_text->p_module )
301
        return;
302

Laurent Aimar's avatar
Laurent Aimar committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    /* 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-duration", p_subpic->i_stop - p_subpic->i_start );
    var_SetTime( p_text, "spu-elapsed", render_date );
    var_SetBool( p_text, "text-rerender", false );
    var_SetInteger( p_text, "scale", i_min_scale_ratio );

    if( p_text->pf_render_html && p_region->psz_html )
    {
        p_text->pf_render_html( p_text, p_region, p_region );
330
    }
Laurent Aimar's avatar
Laurent Aimar committed
331
    else if( p_text->pf_render_text )
332
    {
Laurent Aimar's avatar
Laurent Aimar committed
333
        p_text->pf_render_text( p_text, p_region, p_region );
334
    }
Laurent Aimar's avatar
Laurent Aimar committed
335
    *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
336 337
}

338
/**
Laurent Aimar's avatar
Laurent Aimar committed
339
 * A few scale functions helpers.
340 341
 */

Laurent Aimar's avatar
Laurent Aimar committed
342 343 344 345 346 347
#define SCALE_UNIT (1000)
typedef struct
{
    int w;
    int h;
} spu_scale_t;
348

Laurent Aimar's avatar
Laurent Aimar committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
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 );
}
static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
{
    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;
382 383
}

384
/**
Laurent Aimar's avatar
Laurent Aimar committed
385
 * A few area functions helpers
386
 */
Laurent Aimar's avatar
Laurent Aimar committed
387
typedef struct
Sam Hocevar's avatar
 
Sam Hocevar committed
388
{
Laurent Aimar's avatar
Laurent Aimar committed
389 390 391 392
    int i_x;
    int i_y;
    int i_width;
    int i_height;
393

Laurent Aimar's avatar
Laurent Aimar committed
394 395
    spu_scale_t scale;
} spu_area_t;
396

Laurent Aimar's avatar
Laurent Aimar committed
397 398 399 400
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
401
}
Laurent Aimar's avatar
Laurent Aimar committed
402
static spu_area_t spu_area_scaled( spu_area_t a )
403
{
Laurent Aimar's avatar
Laurent Aimar committed
404 405
    if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
        return a;
406

Laurent Aimar's avatar
Laurent Aimar committed
407 408
    a.i_x = spu_scale_w( a.i_x, a.scale );
    a.i_y = spu_scale_h( a.i_y, a.scale );
409

Laurent Aimar's avatar
Laurent Aimar committed
410 411
    a.i_width  = spu_scale_w( a.i_width,  a.scale );
    a.i_height = spu_scale_h( a.i_height, a.scale );
412

Laurent Aimar's avatar
Laurent Aimar committed
413 414 415 416 417 418 419
    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;
420

Laurent Aimar's avatar
Laurent Aimar committed
421
    a = spu_area_scaled( a );
422

Laurent Aimar's avatar
Laurent Aimar committed
423 424
    a.i_x = spu_invscale_w( a.i_x, s );
    a.i_y = spu_invscale_h( a.i_y, s );
425

Laurent Aimar's avatar
Laurent Aimar committed
426 427
    a.i_width  = spu_invscale_w( a.i_width, s );
    a.i_height = spu_invscale_h( a.i_height, s );
428

Laurent Aimar's avatar
Laurent Aimar committed
429 430 431 432 433 434 435
    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;
436

Laurent Aimar's avatar
Laurent Aimar committed
437 438
    a = spu_area_scaled( a );
    b = spu_area_scaled( b );
439

Laurent Aimar's avatar
Laurent Aimar committed
440 441 442
    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
443

Laurent Aimar's avatar
Laurent Aimar committed
444 445 446 447 448 449 450 451 452
/**
 * 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;
453

Laurent Aimar's avatar
Laurent Aimar committed
454 455 456
    /* 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
457
    {
Laurent Aimar's avatar
Laurent Aimar committed
458 459
        b_ok = true;
        for( int i = 0; i < i_sub; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
460
        {
Laurent Aimar's avatar
Laurent Aimar committed
461
            spu_area_t sub = spu_area_scaled( p_sub[i] );
462

Laurent Aimar's avatar
Laurent Aimar committed
463 464
            if( !spu_area_overlap( a, sub ) )
                continue;
465

Laurent Aimar's avatar
Laurent Aimar committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
            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;
            }
486

Laurent Aimar's avatar
Laurent Aimar committed
487 488
            b_ok = false;
            break;
Laurent Aimar's avatar
Laurent Aimar committed
489
        }
Laurent Aimar's avatar
Laurent Aimar committed
490
    } while( !b_ok );
491

Laurent Aimar's avatar
Laurent Aimar committed
492 493 494
    if( b_moved )
        *p_dst = spu_area_unscaled( a, p_dst->scale );
}
495

496

Laurent Aimar's avatar
Laurent Aimar committed
497 498 499
static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
{
  spu_area_t a = spu_area_scaled( *p_area );
500

Laurent Aimar's avatar
Laurent Aimar committed
501 502 503 504 505
  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;
506

Laurent Aimar's avatar
Laurent Aimar committed
507 508 509 510 511
  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;
512

Laurent Aimar's avatar
Laurent Aimar committed
513 514
  *p_area = spu_area_unscaled( a, p_area->scale );
}
515

Laurent Aimar's avatar
Laurent Aimar committed
516 517 518 519 520 521 522 523
/**
 * 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 );
524
    if( p_subpic->b_absolute )
Laurent Aimar's avatar
Laurent Aimar committed
525
    {
526 527
        *pi_x = p_region->i_x;
        *pi_y = p_region->i_y;
Laurent Aimar's avatar
Laurent Aimar committed
528 529 530
    }
    else
    {
531 532 533 534 535 536 537 538 539 540 541 542
        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;
        }
543

544 545 546 547 548 549 550 551 552 553 554 555
        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
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    }
}

/**
 * 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;
}

static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
{
    p_subpic->p_next = *pp_head;

    *pp_head = p_subpic;
599 600
}

Laurent Aimar's avatar
Laurent Aimar committed
601
/*****************************************************************************
602
 * SpuSortSubpictures: find the subpictures to display
Laurent Aimar's avatar
Laurent Aimar committed
603 604 605 606 607 608 609 610
 *****************************************************************************
 * 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.
 *****************************************************************************/
611 612 613 614
static subpicture_t *SpuSortSubpictures( spu_t *p_spu,
                                         mtime_t render_subtitle_date,
                                         mtime_t render_osd_date,
                                         bool b_subtitle_only )
Laurent Aimar's avatar
Laurent Aimar committed
615
{
Laurent Aimar's avatar
Laurent Aimar committed
616 617
    spu_private_t *p_sys = p_spu->p;
    subpicture_t *p_subpic = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
618

Laurent Aimar's avatar
Laurent Aimar committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    /* 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;
    }

Laurent Aimar's avatar
Laurent Aimar committed
639 640
    /* We get an easily parsable chained list of subpictures which
     * ends with NULL since p_subpic was initialized to NULL. */
Laurent Aimar's avatar
Laurent Aimar committed
641
    for( int i = 0; i < i_channel_count; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
642
    {
Laurent Aimar's avatar
Laurent Aimar committed
643
        const int i_channel = pi_channel[i];
644 645 646 647
        subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
        bool         pb_available_late[VOUT_MAX_SUBPICTURES];
        int          i_available = 0;

648
        mtime_t      start_date = render_subtitle_date;
649
        mtime_t      ephemer_subtitle_date = 0;
650
        mtime_t      ephemer_osd_date = 0;
651 652
        int64_t      i_ephemer_subtitle_order = INT64_MIN;
        int64_t      i_ephemer_system_order = INT64_MIN;
Laurent Aimar's avatar
Laurent Aimar committed
653 654
        int i_index;

655
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
656 657 658 659
        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;
660 661
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
662 663 664 665 666 667 668 669 670 671 672 673 674

            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;
            }
675
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
676 677
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
678 679 680 681 682
            {
                /* Too early, come back next monday */
                continue;
            }

683
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
684 685 686
            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 )
            {
687
                *pi_ephemer_date = p_current->i_start;
688 689 690
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
691

692
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
693

694
            b_late = b_stop_valid && p_current->i_stop <= render_date;
695 696

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
697
             * in case picture that should not be displayed anymore (display_time)
698
             * overlap with a picture to be displayed (p_current->i_start)  */
699
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
700 701 702 703 704 705
                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
706 707
        }

708 709 710 711 712 713 714 715
        /* 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
716
        {
717 718
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
719

720 721
            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;
722
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
723

724 725 726
            /* 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
727
            {
728 729 730 731 732
                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
733
            }
734 735 736

            if( b_rejet )
                SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
737
            else
738
                SubpictureChain( &p_subpic, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
739
        }
Laurent Aimar's avatar
Laurent Aimar committed
740
    }
741

742
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
743

Laurent Aimar's avatar
Laurent Aimar committed
744
    return p_subpic;
Laurent Aimar's avatar
Laurent Aimar committed
745 746
}

Laurent Aimar's avatar
Laurent Aimar committed
747 748 749 750 751 752 753 754 755 756


/**
 * 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,
757
                             const vlc_fourcc_t *p_chroma_list,
Laurent Aimar's avatar
Laurent Aimar committed
758 759 760
                             const video_format_t *p_fmt,
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date )
761 762 763
{
    spu_private_t *p_sys = p_spu->p;

Laurent Aimar's avatar
Laurent Aimar committed
764
    video_format_t fmt_original = p_region->fmt;
765
    bool b_restore_text = false;
Laurent Aimar's avatar
Laurent Aimar committed
766 767
    int i_x_offset;
    int i_y_offset;
768

Laurent Aimar's avatar
Laurent Aimar committed
769 770
    video_format_t region_fmt;
    picture_t *p_region_picture;
771

Laurent Aimar's avatar
Laurent Aimar committed
772 773 774
    /* Invalidate area by default */
    *p_dst_area = spu_area_create( 0,0, 0,0, scale_size );
    *pp_dst     = NULL;
775

Laurent Aimar's avatar
Laurent Aimar committed
776 777 778 779
    /* Render text region */
    if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
    {
        const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
780
        SpuRenderText( p_spu, &b_restore_text, p_subpic, p_region,
Laurent Aimar's avatar
Laurent Aimar committed
781 782 783 784 785
                       i_min_scale_ratio, render_date );

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

Laurent Aimar's avatar
Laurent Aimar committed
788 789 790 791 792 793 794 795
    /* 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;
796

Laurent Aimar's avatar
Laurent Aimar committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    /* 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 )
817
    {
Laurent Aimar's avatar
Laurent Aimar committed
818 819
        SpuAreaFixOverlap( p_dst_area, p_subtitle_area, i_subtitle_area,
                           p_region->i_align );
820 821
    }

Laurent Aimar's avatar
Laurent Aimar committed
822 823 824
    /* 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;
825

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

Laurent Aimar's avatar
Laurent Aimar committed
830 831 832
    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
833

Laurent Aimar's avatar
Laurent Aimar committed
834 835 836 837 838 839
    /* 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
840
    {
Laurent Aimar's avatar
Laurent Aimar committed
841 842
        video_palette_t *p_palette = p_region->fmt.p_palette;
        video_palette_t palette;
Laurent Aimar's avatar
Laurent Aimar committed
843

Laurent Aimar's avatar
Laurent Aimar committed
844 845 846 847 848 849 850
        /* 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
851
        {
Laurent Aimar's avatar
Laurent Aimar committed
852 853 854 855 856 857 858
            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
859
        }
Laurent Aimar's avatar
Laurent Aimar committed
860
        *p_palette = palette;
Laurent Aimar's avatar
Laurent Aimar committed
861 862
    }

Laurent Aimar's avatar
Laurent Aimar committed
863 864 865
    /* */
    region_fmt = p_region->fmt;
    p_region_picture = p_region->p_picture;
Laurent Aimar's avatar
Laurent Aimar committed
866

867 868 869 870 871 872
    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
873

Laurent Aimar's avatar
Laurent Aimar committed
874 875 876
    /* 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 ) ) &&
877 878
        ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
          b_using_palette || b_convert_chroma) )
Laurent Aimar's avatar
Laurent Aimar committed
879 880 881
    {
        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
882

Laurent Aimar's avatar
Laurent Aimar committed
883 884 885 886 887
        /* 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
888

Laurent Aimar's avatar
Laurent Aimar committed
889 890 891 892
            /* 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
893

Laurent Aimar's avatar
Laurent Aimar committed
894 895 896
            /* Check forced palette changes */
            if( b_changed_palette )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
897

898 899 900
            if( b_convert_chroma && p_private->fmt.i_chroma != p_chroma_list[0] )
                b_changed = true;

Laurent Aimar's avatar
Laurent Aimar committed
901 902 903 904 905 906
            if( b_changed )
            {
                subpicture_region_private_Delete( p_private );
                p_region->p_private = NULL;
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
907

Laurent Aimar's avatar
Laurent Aimar committed
908 909 910 911
        /* 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
912

Laurent Aimar's avatar
Laurent Aimar committed
913 914
            picture_t *p_picture = p_region->p_picture;
            picture_Hold( p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
915

Laurent Aimar's avatar
Laurent Aimar committed
916 917 918 919
            /* Convert YUVP to YUVA/RGBA first for better scaling quality */
            if( b_using_palette )
            {
                filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
920

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

Laurent Aimar's avatar
Laurent Aimar committed
923
                p_scale_yuvp->fmt_out.video = p_region->fmt;
924
                p_scale_yuvp->fmt_out.video.i_chroma = p_chroma_list[0];
925

Laurent Aimar's avatar
Laurent Aimar committed
926 927 928 929 930 931 932 933 934
                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 );
                }
            }
935

Laurent Aimar's avatar
Laurent Aimar committed
936 937 938
            /* Conversion(except from YUVP)/Scaling */
            if( p_picture &&
                ( p_picture->format.i_width != i_dst_width ||
939 940
                  p_picture->format.i_height != i_dst_height ||
                  ( b_convert_chroma && !b_using_palette ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
941 942 943
            {
                p_scale->fmt_in.video = p_picture->format;
                p_scale->fmt_out.video = p_picture->format;
944 945 946
                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
947

Laurent Aimar's avatar
Laurent Aimar committed
948 949
                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
950

Laurent Aimar's avatar
Laurent Aimar committed
951 952 953 954
                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
955

Laurent Aimar's avatar
Laurent Aimar committed
956 957 958 959
                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
960

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

Laurent Aimar's avatar
Laurent Aimar committed
981 982 983 984 985 986
        /* 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
987
    }
988

Laurent Aimar's avatar
Laurent Aimar committed
989 990
    /* Force cropping if requested */
    if( b_force_crop )
991
    {
Laurent Aimar's avatar
Laurent Aimar committed
992 993 994 995
        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 );
996

Laurent Aimar's avatar
Laurent Aimar committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        /* 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 );
1016

Laurent Aimar's avatar
Laurent Aimar committed
1017 1018 1019 1020
            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;
1021

Laurent Aimar's avatar
Laurent Aimar committed
1022 1023 1024 1025
            i_x_offset = __MAX( i_x, 0 );
            i_y_offset = __MAX( i_y, 0 );
        }
    }
1026

Laurent Aimar's avatar
Laurent Aimar committed
1027 1028
    subpicture_region_t *p_dst = *pp_dst = subpicture_region_New( &region_fmt );
    if( p_dst )
Laurent Aimar's avatar
Laurent Aimar committed
1029
    {
Laurent Aimar's avatar
Laurent Aimar committed
1030 1031 1032 1033 1034 1035 1036 1037 1038
        p_dst->i_x       = i_x_offset;
        p_dst->i_y       = i_y_offset;
        p_dst->i_align   = 0;
        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
1039

Laurent Aimar's avatar
Laurent Aimar committed
1040 1041 1042 1043 1044
            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
1045 1046
    }

Laurent Aimar's avatar
Laurent Aimar committed
1047
exit:
1048
    if( b_restore_text )
Laurent Aimar's avatar
Laurent Aimar committed
1049
    {
Laurent Aimar's avatar
Laurent Aimar committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
        /* 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;
Laurent Aimar's avatar