vout_subpictures.c 53 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>
Clément Stenac's avatar
Clément Stenac committed
37 38 39
#include <vlc_vout.h>
#include <vlc_block.h>
#include <vlc_filter.h>
40
#include <vlc_spu.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
#include "../libvlc.h"
Laurent Aimar's avatar
Laurent Aimar committed
42
#include "vout_internal.h"
43
#include <vlc_image.h>
44
#include <vlc_input.h>
45
#include <vlc_modules.h>
46
#include "../misc/subpicture.h"
47

48 49 50
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
51

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

Laurent Aimar's avatar
Laurent Aimar committed
55
/* */
56 57 58 59 60 61 62 63 64 65 66 67
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;

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

    spu_heap_t heap;

75 76 77 78 79 80 81 82 83 84 85
    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 */

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

    /* */
    mtime_t i_last_sort_date;
93 94
};

Laurent Aimar's avatar
Laurent Aimar committed
95 96 97 98 99 100 101 102 103 104 105
/* 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
106
{
Laurent Aimar's avatar
Laurent Aimar committed
107 108 109
    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
110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
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 337
    /* 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 );
338
    }
Laurent Aimar's avatar
Laurent Aimar committed
339
    else if( p_text->pf_render_text )
340
    {
Laurent Aimar's avatar
Laurent Aimar committed
341
        p_text->pf_render_text( p_text, p_region, p_region );
342
    }
Laurent Aimar's avatar
Laurent Aimar committed
343 344 345 346
    *pb_rerender_text = var_GetBool( p_text, "text-rerender" );

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
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 392
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;
393 394
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

507

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

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
527 528 529 530 531 532 533 534 535 536
/**
 * 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;
537

Laurent Aimar's avatar
Laurent Aimar committed
538 539 540 541 542 543 544 545 546 547 548 549
    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
550
    }
551

Laurent Aimar's avatar
Laurent Aimar committed
552 553 554 555 556 557 558 559 560 561 562 563
    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;
    }
564

Laurent Aimar's avatar
Laurent Aimar committed
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 614
    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;
615 616
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Laurent Aimar's avatar
Laurent Aimar committed
763 764 765 766 767 768 769 770 771 772 773 774 775


/**
 * 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,
                             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 834

    /* 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 )
835
    {
Laurent Aimar's avatar
Laurent Aimar committed
836 837
        SpuAreaFixOverlap( p_dst_area, p_subtitle_area, i_subtitle_area,
                           p_region->i_align );
838 839
    }

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

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

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

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

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

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


Laurent Aimar's avatar
Laurent Aimar committed
886 887 888 889 890 891 892
    /* 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 ) ) &&
        ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
    {
        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
893

Laurent Aimar's avatar
Laurent Aimar committed
894 895 896 897 898
        /* 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
899

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

Laurent Aimar's avatar
Laurent Aimar committed
905 906 907
            /* Check forced palette changes */
            if( b_changed_palette )
                b_changed = true;
Laurent Aimar's avatar
Laurent Aimar committed
908

Laurent Aimar's avatar
Laurent Aimar committed
909 910 911 912 913 914
            if( b_changed )
            {
                subpicture_region_private_Delete( p_private );
                p_region->p_private = NULL;
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
915

Laurent Aimar's avatar
Laurent Aimar committed
916 917 918 919
        /* 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
920

Laurent Aimar's avatar
Laurent Aimar committed
921 922
            picture_t *p_picture = p_region->p_picture;
            picture_Hold( p_picture );
Laurent Aimar's avatar
Laurent Aimar committed
923

Laurent Aimar's avatar
Laurent Aimar committed
924 925 926 927
            /* Convert YUVP to YUVA/RGBA first for better scaling quality */
            if( b_using_palette )
            {
                filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
928

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

Laurent Aimar's avatar
Laurent Aimar committed
931 932 933
                /* TODO converting to RGBA for RGB video output is better */
                p_scale_yuvp->fmt_out.video = p_region->fmt;
                p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
934

Laurent Aimar's avatar
Laurent Aimar committed
935 936 937 938 939 940 941 942 943
                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 );
                }
            }
944

Laurent Aimar's avatar
Laurent Aimar committed
945 946 947 948 949 950 951
            /* Conversion(except from YUVP)/Scaling */
            if( p_picture &&
                ( p_picture->format.i_width != i_dst_width ||
                  p_picture->format.i_height != i_dst_height ) )
            {
                p_scale->fmt_in.video = p_picture->format;
                p_scale->fmt_out.video = p_picture->format;
Laurent Aimar's avatar
Laurent Aimar committed
952

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

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

Laurent Aimar's avatar
Laurent Aimar committed
961 962 963 964
                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
965

Laurent Aimar's avatar
Laurent Aimar committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
            /* */
            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 );
                }
            }
        }
985

Laurent Aimar's avatar
Laurent Aimar committed
986 987 988 989 990 991
        /* 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
992
    }
993

Laurent Aimar's avatar
Laurent Aimar committed
994 995
    /* Force cropping if requested */
    if( b_force_crop )
996
    {
Laurent Aimar's avatar
Laurent Aimar committed
997 998 999 1000
        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 );
1001

Laurent Aimar's avatar
Laurent Aimar committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
        /* 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 );
1021

Laurent Aimar's avatar
Laurent Aimar committed
1022 1023 1024 1025
            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;
1026

Laurent Aimar's avatar
Laurent Aimar committed
1027 1028 1029 1030
            i_x_offset = __MAX( i_x, 0 );
            i_y_offset = __MAX( i_y, 0 );
        }
    }
1031

Laurent Aimar's avatar
Laurent Aimar committed
1032 1033
    subpicture_region_t *p_dst = *pp_dst = subpicture_region_New( &region_fmt );
    if( p_dst )
Laurent Aimar's avatar
Laurent Aimar committed
1034
    {
Laurent Aimar's avatar
Laurent Aimar committed
1035 1036 1037 1038 1039 1040 1041 1042 1043
        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
1044

Laurent Aimar's avatar
Laurent Aimar committed
1045 1046 1047 1048 1049
            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
1050 1051
    }

Laurent Aimar's avatar
Laurent Aimar committed
1052 1053
exit:
    if( b_rerender_text )
Laurent Aimar's avatar
Laurent Aimar committed
1054
    {
Laurent Aimar's avatar
Laurent Aimar committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
        /* 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
1071
    }
Laurent Aimar's avatar
Laurent Aimar committed
1072 1073
    if( b_restore_format )
        p_region->fmt = fmt_original;
Laurent Aimar's avatar
Laurent Aimar committed
1074
}
1075

Laurent Aimar's avatar
Laurent Aimar committed
1076 1077 1078 1079 1080 1081 1082 1083 1084
/**
 * This function renders all sub picture units in the list.
 */
static subpicture_t *SpuRenderSubpictures( spu_t *p_spu,
                                           const video_format_t *p_fmt_dst,
                                           subpicture_t *p_subpic_list,
                                           const video_format_t *p_fmt_src,
                                           mtime_t render_subtitle_date,
                                           mtime_t render_osd_date )
1085
{
Laurent Aimar's avatar
Laurent Aimar committed
1086
    spu_private_t *p_sys = p_spu->p;
1087

Laurent Aimar's avatar
Laurent Aimar committed
1088 1089
    const int i_source_video_width  = p_fmt_src->i_width;
    const int i_source_video_height = p_fmt_src->i_height;
1090

Laurent Aimar's avatar
Laurent Aimar committed
1091 1092
    unsigned int i_subpicture;
    subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
1093

Laurent Aimar's avatar
Laurent Aimar committed
1094 1095