vout_subpictures.c 54.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 */
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 98 99 100 101 102 103 104
/* Subpicture rendered flag
 * FIXME ? it could be moved to private ? */
#define SUBPICTURE_RENDERED  (0x1000)
#if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
#   error SUBPICTURE_RENDERED too low
#endif

/*****************************************************************************
 * heap managment
 *****************************************************************************/
static void SpuHeapInit( spu_heap_t *p_heap )
Laurent Aimar's avatar
Laurent Aimar committed
105
{
Laurent Aimar's avatar
Laurent Aimar committed
106 107 108
    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
109

Laurent Aimar's avatar
Laurent Aimar committed
110 111 112 113 114 115
        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
116
{
Laurent Aimar's avatar
Laurent Aimar committed
117 118 119
    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
120

Laurent Aimar's avatar
Laurent Aimar committed
121 122
        if( e->p_subpicture )
            continue;
Laurent Aimar's avatar
Laurent Aimar committed
123

Laurent Aimar's avatar
Laurent Aimar committed
124 125 126 127 128 129
        e->p_subpicture = p_subpic;
        e->b_reject = false;
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
Laurent Aimar's avatar
Laurent Aimar committed
130

Laurent Aimar's avatar
Laurent Aimar committed
131 132 133
static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
{
    spu_heap_entry_t *e = &p_heap->p_entry[i_index];
134

Laurent Aimar's avatar
Laurent Aimar committed
135 136
    if( e->p_subpicture )
        subpicture_Delete( e->p_subpicture );
137

Laurent Aimar's avatar
Laurent Aimar committed
138 139
    e->p_subpicture = NULL;
}
Laurent Aimar's avatar
Laurent Aimar committed
140

Laurent Aimar's avatar
Laurent Aimar committed
141 142 143 144 145
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
146

Laurent Aimar's avatar
Laurent Aimar committed
147 148 149 150 151 152 153 154
        if( e->p_subpicture != p_subpic )
            continue;

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

Laurent Aimar's avatar
Laurent Aimar committed
156 157 158 159 160 161 162 163 164
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 );
    }
}
165

166 167 168 169 170
struct filter_owner_sys_t
{
    spu_t *p_spu;
    int i_channel;
};
171

Laurent Aimar's avatar
Laurent Aimar committed
172 173 174 175 176 177
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 );
178

Laurent Aimar's avatar
Laurent Aimar committed
179 180
    vlc_object_release( p_filter );
}
181

Laurent Aimar's avatar
Laurent Aimar committed
182 183 184
static picture_t *spu_new_video_buffer( filter_t *p_filter )
{
    const video_format_t *p_fmt = &p_filter->fmt_out.video;
185

Laurent Aimar's avatar
Laurent Aimar committed
186 187 188 189
    VLC_UNUSED(p_filter);
    return picture_NewFromFormat( p_fmt );
}
static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
190
{
Laurent Aimar's avatar
Laurent Aimar committed
191 192 193
    VLC_UNUSED(p_filter);
    picture_Release( p_picture );
}
194

Laurent Aimar's avatar
Laurent Aimar committed
195 196 197 198 199
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;
200

Laurent Aimar's avatar
Laurent Aimar committed
201 202 203 204 205 206 207
    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;
}
208

Laurent Aimar's avatar
Laurent Aimar committed
209 210 211
static void SpuRenderCreateAndLoadText( spu_t *p_spu )
{
    filter_t *p_text;
212

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

Laurent Aimar's avatar
Laurent Aimar committed
215 216 217 218 219
    p_spu->p->p_text =
    p_text        = vlc_custom_create( p_spu, sizeof(filter_t),
                                       VLC_OBJECT_GENERIC, "spu text" );
    if( !p_text )
        return;
220

Laurent Aimar's avatar
Laurent Aimar committed
221 222
    p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
    p_text->p_owner->p_spu = p_spu;
223

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

Laurent Aimar's avatar
Laurent Aimar committed
226 227 228 229 230
    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;
231

Laurent Aimar's avatar
Laurent Aimar committed
232
    p_text->pf_get_attachments = spu_get_attachments;
233

Laurent Aimar's avatar
Laurent Aimar committed
234
    vlc_object_attach( p_text, p_spu );
235

Laurent Aimar's avatar
Laurent Aimar committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    /* 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 );
254 255
}

Laurent Aimar's avatar
Laurent Aimar committed
256 257 258
static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
                                     vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
                                     bool b_resize )
259
{
Laurent Aimar's avatar
Laurent Aimar committed
260
    filter_t *p_scale;
261

Laurent Aimar's avatar
Laurent Aimar committed
262 263 264 265
    p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
                                 VLC_OBJECT_GENERIC, "scale" );
    if( !p_scale )
        return NULL;
266

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

Laurent Aimar's avatar
Laurent Aimar committed
272 273 274 275
    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;
276

Laurent Aimar's avatar
Laurent Aimar committed
277 278
    p_scale->pf_video_buffer_new = spu_new_video_buffer;
    p_scale->pf_video_buffer_del = spu_del_video_buffer;
279

Laurent Aimar's avatar
Laurent Aimar committed
280 281
    vlc_object_attach( p_scale, p_obj );
    p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
282

Laurent Aimar's avatar
Laurent Aimar committed
283 284 285 286 287 288 289 290 291
    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),
292
                                            VLC_CODEC_YUVA, VLC_CODEC_RGBA, true );
Laurent Aimar's avatar
Laurent Aimar committed
293 294 295 296
    /* 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 );
297 298
}

Laurent Aimar's avatar
Laurent Aimar committed
299 300 301
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 )
302
{
Laurent Aimar's avatar
Laurent Aimar committed
303
    filter_t *p_text = p_spu->p->p_text;
304

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

Laurent Aimar's avatar
Laurent Aimar committed
307 308
    if( !p_text || !p_text->p_module )
        goto exit;
309

Laurent Aimar's avatar
Laurent Aimar committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    /* 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 );
337
    }
Laurent Aimar's avatar
Laurent Aimar committed
338
    else if( p_text->pf_render_text )
339
    {
Laurent Aimar's avatar
Laurent Aimar committed
340
        p_text->pf_render_text( p_text, p_region, p_region );
341
    }
Laurent Aimar's avatar
Laurent Aimar committed
342 343 344 345
    *pb_rerender_text = var_GetBool( p_text, "text-rerender" );

exit:
    p_region->i_align |= SUBPICTURE_RENDERED;
346 347
}

348
/**
Laurent Aimar's avatar
Laurent Aimar committed
349
 * A few scale functions helpers.
350 351
 */

Laurent Aimar's avatar
Laurent Aimar committed
352 353 354 355 356 357
#define SCALE_UNIT (1000)
typedef struct
{
    int w;
    int h;
} spu_scale_t;
358

Laurent Aimar's avatar
Laurent Aimar committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
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;
392 393
}

394
/**
Laurent Aimar's avatar
Laurent Aimar committed
395
 * A few area functions helpers
396
 */
Laurent Aimar's avatar
Laurent Aimar committed
397
typedef struct
Sam Hocevar's avatar
 
Sam Hocevar committed
398
{
Laurent Aimar's avatar
Laurent Aimar committed
399 400 401 402
    int i_x;
    int i_y;
    int i_width;
    int i_height;
403

Laurent Aimar's avatar
Laurent Aimar committed
404 405
    spu_scale_t scale;
} spu_area_t;
406

Laurent Aimar's avatar
Laurent Aimar committed
407 408 409 410
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
411
}
Laurent Aimar's avatar
Laurent Aimar committed
412
static spu_area_t spu_area_scaled( spu_area_t a )
413
{
Laurent Aimar's avatar
Laurent Aimar committed
414 415
    if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
        return a;
416

Laurent Aimar's avatar
Laurent Aimar committed
417 418
    a.i_x = spu_scale_w( a.i_x, a.scale );
    a.i_y = spu_scale_h( a.i_y, a.scale );
419

Laurent Aimar's avatar
Laurent Aimar committed
420 421
    a.i_width  = spu_scale_w( a.i_width,  a.scale );
    a.i_height = spu_scale_h( a.i_height, a.scale );
422

Laurent Aimar's avatar
Laurent Aimar committed
423 424 425 426 427 428 429
    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;
430

Laurent Aimar's avatar
Laurent Aimar committed
431
    a = spu_area_scaled( a );
432

Laurent Aimar's avatar
Laurent Aimar committed
433 434
    a.i_x = spu_invscale_w( a.i_x, s );
    a.i_y = spu_invscale_h( a.i_y, s );
435

Laurent Aimar's avatar
Laurent Aimar committed
436 437
    a.i_width  = spu_invscale_w( a.i_width, s );
    a.i_height = spu_invscale_h( a.i_height, s );
438

Laurent Aimar's avatar
Laurent Aimar committed
439 440 441 442 443 444 445
    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;
446

Laurent Aimar's avatar
Laurent Aimar committed
447 448
    a = spu_area_scaled( a );
    b = spu_area_scaled( b );
449

Laurent Aimar's avatar
Laurent Aimar committed
450 451 452
    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
453

Laurent Aimar's avatar
Laurent Aimar committed
454 455 456 457 458 459 460 461 462
/**
 * 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;
463

Laurent Aimar's avatar
Laurent Aimar committed
464 465 466
    /* 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
467
    {
Laurent Aimar's avatar
Laurent Aimar committed
468 469
        b_ok = true;
        for( int i = 0; i < i_sub; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
470
        {
Laurent Aimar's avatar
Laurent Aimar committed
471
            spu_area_t sub = spu_area_scaled( p_sub[i] );
472

Laurent Aimar's avatar
Laurent Aimar committed
473 474
            if( !spu_area_overlap( a, sub ) )
                continue;
475

Laurent Aimar's avatar
Laurent Aimar committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
            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;
            }
496

Laurent Aimar's avatar
Laurent Aimar committed
497 498
            b_ok = false;
            break;
Laurent Aimar's avatar
Laurent Aimar committed
499
        }
Laurent Aimar's avatar
Laurent Aimar committed
500
    } while( !b_ok );
501

Laurent Aimar's avatar
Laurent Aimar committed
502 503 504
    if( b_moved )
        *p_dst = spu_area_unscaled( a, p_dst->scale );
}
505

506

Laurent Aimar's avatar
Laurent Aimar committed
507 508 509
static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
{
  spu_area_t a = spu_area_scaled( *p_area );
510

Laurent Aimar's avatar
Laurent Aimar committed
511 512 513 514 515
  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;
516

Laurent Aimar's avatar
Laurent Aimar committed
517 518 519 520 521
  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;
522

Laurent Aimar's avatar
Laurent Aimar committed
523 524
  *p_area = spu_area_unscaled( a, p_area->scale );
}
525

Laurent Aimar's avatar
Laurent Aimar committed
526 527 528 529 530 531 532 533 534 535
/**
 * Place a region
 */
static void SpuRegionPlace( int *pi_x, int *pi_y,
                            const subpicture_t *p_subpic,
                            const subpicture_region_t *p_region )
{
    const int i_delta_x = p_region->i_x;
    const int i_delta_y = p_region->i_y;
    int i_x, i_y;
536

Laurent Aimar's avatar
Laurent Aimar committed
537 538 539 540 541 542 543 544 545 546 547 548
    assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
    if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
    {
        i_y = i_delta_y;
    }
    else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
    {
        i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
    }
    else
    {
        i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
Laurent Aimar's avatar
Laurent Aimar committed
549
    }
550

Laurent Aimar's avatar
Laurent Aimar committed
551 552 553 554 555 556 557 558 559 560 561 562
    if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
    {
        i_x = i_delta_x;
    }
    else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
    {
        i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
    }
    else
    {
        i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
    }
563

Laurent Aimar's avatar
Laurent Aimar committed
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    if( p_subpic->b_absolute )
    {
        i_x = i_delta_x;
        i_y = i_delta_y;
    }

    *pi_x = i_x;
    *pi_y = i_y;
}

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

Laurent Aimar's avatar
Laurent Aimar committed
616
/*****************************************************************************
617
 * SpuSortSubpictures: find the subpictures to display
Laurent Aimar's avatar
Laurent Aimar committed
618 619 620 621 622 623 624 625
 *****************************************************************************
 * 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.
 *****************************************************************************/
626 627 628 629
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
630
{
Laurent Aimar's avatar
Laurent Aimar committed
631 632
    spu_private_t *p_sys = p_spu->p;
    subpicture_t *p_subpic = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
633

Laurent Aimar's avatar
Laurent Aimar committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    /* 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
654 655
    /* 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
656
    for( int i = 0; i < i_channel_count; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
657
    {
Laurent Aimar's avatar
Laurent Aimar committed
658
        const int i_channel = pi_channel[i];
659 660 661 662
        subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
        bool         pb_available_late[VOUT_MAX_SUBPICTURES];
        int          i_available = 0;

663
        mtime_t      start_date = render_subtitle_date;
664
        mtime_t      ephemer_subtitle_date = 0;
665
        mtime_t      ephemer_osd_date = 0;
666 667
        int64_t      i_ephemer_subtitle_order = INT64_MIN;
        int64_t      i_ephemer_system_order = INT64_MIN;
Laurent Aimar's avatar
Laurent Aimar committed
668 669
        int i_index;

670
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
671 672 673 674
        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;
675 676
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
677 678 679 680 681 682 683 684 685 686 687 688 689

            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;
            }
690
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
691 692
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
693 694 695 696 697
            {
                /* Too early, come back next monday */
                continue;
            }

698
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
699 700 701
            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 )
            {
702
                *pi_ephemer_date = p_current->i_start;
703 704 705
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
706

707
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
708

709
            b_late = b_stop_valid && p_current->i_stop <= render_date;
710 711

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
712
             * in case picture that should not be displayed anymore (display_time)
713
             * overlap with a picture to be displayed (p_current->i_start)  */
714
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
715 716 717 718 719 720
                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
721 722
        }

723 724 725 726 727 728 729 730
        /* 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
731
        {
732 733
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
734

735 736
            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;
737
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
738

739 740 741
            /* 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
742
            {
743 744 745 746 747
                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
748
            }
749 750 751

            if( b_rejet )
                SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
752
            else
753
                SubpictureChain( &p_subpic, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
754
        }
Laurent Aimar's avatar
Laurent Aimar committed
755
    }
756

757
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
758

Laurent Aimar's avatar
Laurent Aimar committed
759
    return p_subpic;
Laurent Aimar's avatar
Laurent Aimar committed
760 761
}

Laurent Aimar's avatar
Laurent Aimar committed
762 763 764 765 766 767 768 769 770 771


/**
 * 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,
772
                             const vlc_fourcc_t *p_chroma_list,
Laurent Aimar's avatar
Laurent Aimar committed
773 774 775
                             const video_format_t *p_fmt,
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date )
776 777 778
{
    spu_private_t *p_sys = p_spu->p;

Laurent Aimar's avatar
Laurent Aimar committed
779 780 781 782 783
    video_format_t fmt_original = p_region->fmt;
    bool b_rerender_text = false;
    bool b_restore_format = false;
    int i_x_offset;
    int i_y_offset;
784

Laurent Aimar's avatar
Laurent Aimar committed
785 786
    video_format_t region_fmt;
    picture_t *p_region_picture;
787

Laurent Aimar's avatar
Laurent Aimar committed
788 789 790
    /* Invalidate area by default */
    *p_dst_area = spu_area_create( 0,0, 0,0, scale_size );
    *pp_dst     = NULL;
791

Laurent Aimar's avatar
Laurent Aimar committed
792 793 794 795 796 797 798 799 800 801 802
    /* 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) */
        SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
                       i_min_scale_ratio, render_date );
        b_restore_format = b_rerender_text;

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

Laurent Aimar's avatar
Laurent Aimar committed
805 806 807 808 809 810 811 812
    /* 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;
813

Laurent Aimar's avatar
Laurent Aimar committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
    /* 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 )
834
    {
Laurent Aimar's avatar
Laurent Aimar committed
835 836
        SpuAreaFixOverlap( p_dst_area, p_subtitle_area, i_subtitle_area,
                           p_region->i_align );
837 838
    }

Laurent Aimar's avatar
Laurent Aimar committed
839 840 841
    /* 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;
842

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

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

Laurent Aimar's avatar
Laurent Aimar committed
851 852 853 854 855 856
    /* 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
857
    {
Laurent Aimar's avatar
Laurent Aimar committed
858 859
        video_palette_t *p_palette = p_region->fmt.p_palette;
        video_palette_t palette;
Laurent Aimar's avatar
Laurent Aimar committed
860

Laurent Aimar's avatar
Laurent Aimar committed
861 862 863 864 865 866 867
        /* 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
868
        {
Laurent Aimar's avatar
Laurent Aimar committed
869 870 871 872 873 874 875
            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
876
        }
Laurent Aimar's avatar
Laurent Aimar committed
877
        *p_palette = palette;
Laurent Aimar's avatar
Laurent Aimar committed
878 879
    }

Laurent Aimar's avatar
Laurent Aimar committed
880 881 882
    /* */
    region_fmt = p_region->fmt;
    p_region_picture = p_region->p_picture;
Laurent Aimar's avatar
Laurent Aimar committed
883

884 885 886 887 888 889
    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
890

Laurent Aimar's avatar
Laurent Aimar committed
891 892 893
    /* 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 ) ) &&
894 895
        ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
          b_using_palette || b_convert_chroma) )
Laurent Aimar's avatar
Laurent Aimar committed
896 897 898
    {
        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
899

Laurent Aimar's avatar
Laurent Aimar committed
900 901 902 903 904
        /* 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
905

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

Laurent Aimar's avatar
Laurent Aimar committed
911 912 913
            /* Check forced palette changes */
            if( b_changed_palette )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
914

915 916 917
            if( b_convert_chroma && p_private->fmt.i_chroma != p_chroma_list[0] )
                b_changed = true;

Laurent Aimar's avatar
Laurent Aimar committed
918 919 920 921 922 923
            if( b_changed )
            {
                subpicture_region_private_Delete( p_private );
                p_region->p_private = NULL;
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
924

Laurent Aimar's avatar
Laurent Aimar committed
925 926 927 928
        /* 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
929

Laurent Aimar's avatar
Laurent Aimar committed
930 931
            picture_t *p_picture = p_region->p_picture;
            picture_Hold( p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
932

Laurent Aimar's avatar
Laurent Aimar committed
933 934 935 936
            /* Convert YUVP to YUVA/RGBA first for better scaling quality */
            if( b_using_palette )
            {
                filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
937

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

Laurent Aimar's avatar
Laurent Aimar committed
940
                p_scale_yuvp->fmt_out.video = p_region->fmt;
941
                p_scale_yuvp->fmt_out.video.i_chroma = p_chroma_list[0];
942

Laurent Aimar's avatar
Laurent Aimar committed
943 944 945 946 947 948 949 950 951
                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 );
                }
            }
952

Laurent Aimar's avatar
Laurent Aimar committed
953 954 955
            /* Conversion(except from YUVP)/Scaling */
            if( p_picture &&
                ( p_picture->format.i_width != i_dst_width ||
956 957
                  p_picture->format.i_height != i_dst_height ||
                  ( b_convert_chroma && !b_using_palette ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
958 959 960
            {
                p_scale->fmt_in.video = p_picture->format;
                p_scale->fmt_out.video = p_picture->format;
961 962 963
                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
964

Laurent Aimar's avatar
Laurent Aimar committed
965 966
                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
967

Laurent Aimar's avatar
Laurent Aimar committed
968 969 970 971
                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
972

Laurent Aimar's avatar
Laurent Aimar committed
973 974 975 976
                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
977

Laurent Aimar's avatar
Laurent Aimar committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
            /* */
            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 );
                }
            }
        }
997

Laurent Aimar's avatar
Laurent Aimar committed
998 999 1000 1001 1002 1003
        /* 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
1004
    }
1005

Laurent Aimar's avatar
Laurent Aimar committed
1006 1007
    /* Force cropping if requested */
    if( b_force_crop )
1008
    {
Laurent Aimar's avatar
Laurent Aimar committed
1009 1010 1011 1012
        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 );
1013

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

Laurent Aimar's avatar
Laurent Aimar committed
1034 1035 1036 1037
            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;
1038

Laurent Aimar's avatar
Laurent Aimar committed
1039 1040 1041 1042
            i_x_offset = __MAX( i_x, 0 );
            i_y_offset = __MAX( i_y, 0 );
        }
    }
1043

Laurent Aimar's avatar
Laurent Aimar committed
1044 1045
    subpicture_region_t *p_dst = *pp_dst = subpicture_region_New( &region_fmt );
    if( p_dst )
Laurent Aimar's avatar
Laurent Aimar committed
1046
    {
Laurent Aimar's avatar
Laurent Aimar committed
1047 1048 1049 1050 1051 1052 1053 1054 1055
        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
1056

Laurent Aimar's avatar
Laurent Aimar committed
1057 1058 1059 1060 1061
            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
1062 1063
    }

Laurent Aimar's avatar
Laurent Aimar committed
1064 1065
exit:
    if( b_rerender_text )
Laurent Aimar's avatar
Laurent Aimar committed
1066
    {
Laurent Aimar's avatar
Laurent Aimar committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        /* 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->i_align &= ~SUBPICTURE_RENDERED;
Laurent Aimar's avatar
Laurent Aimar committed
1083
    }
Laurent Aimar's avatar
Laurent Aimar committed
1084 1085
    if( b_restore_format )
        p_region->fmt = fmt_original;