vout_subpictures.c 55.2 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;

Laurent Aimar's avatar
Laurent Aimar committed
68 69 70 71 72 73
static void SpuHeapInit( spu_heap_t * );
static int  SpuHeapPush( spu_heap_t *, subpicture_t * );
static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
static int  SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
static void SpuHeapClean( spu_heap_t *p_heap );

74 75
struct spu_private_t
{
76
    vlc_mutex_t lock;   /* lock to protect all followings fields */
77
    vlc_object_t *p_input;
78 79 80

    spu_heap_t heap;

81 82 83 84 85 86 87 88 89 90 91
    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 */

92
    /* Subpiture filters */
Laurent Aimar's avatar
Laurent Aimar committed
93
    char           *psz_chain_update;
94
    vlc_mutex_t    chain_lock;
95
    filter_chain_t *p_chain;
96 97 98

    /* */
    mtime_t i_last_sort_date;
99 100
};

Laurent Aimar's avatar
Laurent Aimar committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* */
typedef struct
{
    int w;
    int h;
} spu_scale_t;
static spu_scale_t spu_scale_create( int w, int h );
static spu_scale_t spu_scale_unit(void );
static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
static int spu_scale_w( int v, const spu_scale_t s );
static int spu_scale_h( int v, const spu_scale_t s );
static int spu_invscale_w( int v, const spu_scale_t s );
static int spu_invscale_h( int v, const spu_scale_t s );

typedef struct
{
    int i_x;
    int i_y;
    int i_width;
    int i_height;

    spu_scale_t scale;
} spu_area_t;

static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
static spu_area_t spu_area_scaled( spu_area_t );
static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
static bool spu_area_overlap( spu_area_t, spu_area_t );

130

131 132 133
/* Subpicture rendered flag
 * FIXME ? it could be moved to private ? */
#define SUBPICTURE_RENDERED  (0x1000)
Laurent Aimar's avatar
Laurent Aimar committed
134
#if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
135 136 137
#   error SUBPICTURE_RENDERED too low
#endif

Laurent Aimar's avatar
Laurent Aimar committed
138 139
#define SCALE_UNIT (1000)

140
static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
Laurent Aimar's avatar
Laurent Aimar committed
141 142 143
static int SubpictureCmp( const void *s0, const void *s1 );

static void SpuRenderRegion( spu_t *,
144
                             subpicture_region_t **, spu_area_t *,
Laurent Aimar's avatar
Laurent Aimar committed
145 146 147
                             subpicture_t *, subpicture_region_t *,
                             const spu_scale_t scale_size,
                             const video_format_t *p_fmt,
148 149
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date );
150

151 152 153
static void UpdateSPU   ( spu_t *, vlc_object_t * );
static int  CropCallback( vlc_object_t *, char const *,
                          vlc_value_t, vlc_value_t, void * );
154

155
/* Buffer allocation for SPU filter (blend, scale, ...) */
156 157 158 159 160 161 162
struct filter_owner_sys_t
{
    spu_t *p_spu;
    int i_channel;
};
static int spu_get_attachments( filter_t *,
                                input_attachment_t ***, int * );
163 164
static picture_t *spu_new_video_buffer( filter_t * );
static void spu_del_video_buffer( filter_t *, picture_t * );
165

166
/* Buffer aloccation fir SUB filter */
167 168
static int SubFilterAllocationInit( filter_t *, void * );
static void SubFilterAllocationClean( filter_t * );
169

170
/* */
Laurent Aimar's avatar
Laurent Aimar committed
171 172
static void SpuRenderCreateAndLoadText( spu_t * );
static void SpuRenderCreateAndLoadScale( spu_t * );
173
static void FilterRelease( filter_t *p_filter );
174

Laurent Aimar's avatar
Laurent Aimar committed
175 176 177
/*****************************************************************************
 * Public API
 *****************************************************************************/
178 179

#undef spu_Create
180
/**
181
 * Creates the subpicture unit
182
 *
183
 * \param p_this the parent object which creates the subpicture unit
184
 */
185
spu_t *spu_Create( vlc_object_t *p_this )
186
{
187 188
    spu_t *p_spu;
    spu_private_t *p_sys;
189

190 191 192 193
    p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
                               VLC_OBJECT_GENERIC, "subpicture" );
    if( !p_spu )
        return NULL;
194
    vlc_object_attach( p_spu, p_this );
195 196 197 198 199

    /* Initialize spu fields */
    p_spu->p = p_sys = (spu_private_t*)&p_spu[1];

    /* Initialize private fields */
200 201 202 203
    vlc_mutex_init( &p_sys->lock );

    SpuHeapInit( &p_sys->heap );

204 205 206
    p_sys->p_text = NULL;
    p_sys->p_scale = NULL;
    p_sys->p_scale_yuvp = NULL;
207

208
    p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
209

210
    /* Register the default subpicture channel */
Laurent Aimar's avatar
Laurent Aimar committed
211
    p_sys->i_channel = SPU_DEFAULT_CHANNEL + 1;
212

Laurent Aimar's avatar
Laurent Aimar committed
213
    p_sys->psz_chain_update = NULL;
214
    vlc_mutex_init( &p_sys->chain_lock );
215
    p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
216 217
                                       SubFilterAllocationInit,
                                       SubFilterAllocationClean,
218
                                       p_spu );
219 220 221 222 223

    /* Load text and scale module */
    SpuRenderCreateAndLoadText( p_spu );
    SpuRenderCreateAndLoadScale( p_spu );

224 225 226
    /* */
    p_sys->i_last_sort_date = -1;

227 228 229
    return p_spu;
}

230
/**
231
 * Destroy the subpicture unit
232
 *
233
 * \param p_this the parent object which destroys the subpicture unit
234
 */
235
void spu_Destroy( spu_t *p_spu )
236
{
237
    spu_private_t *p_sys = p_spu->p;
238

239 240
    if( p_sys->p_text )
        FilterRelease( p_sys->p_text );
241

242 243
    if( p_sys->p_scale_yuvp )
        FilterRelease( p_sys->p_scale_yuvp );
244

245 246
    if( p_sys->p_scale )
        FilterRelease( p_sys->p_scale );
247

248
    filter_chain_Delete( p_sys->p_chain );
249
    vlc_mutex_destroy( &p_sys->chain_lock );
Laurent Aimar's avatar
Laurent Aimar committed
250
    free( p_sys->psz_chain_update );
251

252 253 254 255
    /* Destroy all remaining subpictures */
    SpuHeapClean( &p_sys->heap );

    vlc_mutex_destroy( &p_sys->lock );
256

257
    vlc_object_release( p_spu );
258 259
}

260 261 262
/**
 * Attach/Detach the SPU from any input
 *
263
 * \param p_this the object in which to destroy the subpicture unit
264 265
 * \param b_attach to select attach or detach
 */
266
void spu_Attach( spu_t *p_spu, vlc_object_t *p_input, bool b_attach )
267 268
{
    if( b_attach )
269
    {
270
        UpdateSPU( p_spu, p_input );
271
        var_Create( p_input, "highlight", VLC_VAR_BOOL );
272
        var_AddCallback( p_input, "highlight", CropCallback, p_spu );
273 274

        vlc_mutex_lock( &p_spu->p->lock );
275 276 277 278 279 280
        p_spu->p->p_input = p_input;

        FilterRelease( p_spu->p->p_text );
        p_spu->p->p_text = NULL;
        SpuRenderCreateAndLoadText( p_spu );

281
        vlc_mutex_unlock( &p_spu->p->lock );
282 283 284
    }
    else
    {
285 286 287 288
        vlc_mutex_lock( &p_spu->p->lock );
        p_spu->p->p_input = NULL;
        vlc_mutex_unlock( &p_spu->p->lock );

289 290 291
        /* Delete callbacks */
        var_DelCallback( p_input, "highlight", CropCallback, p_spu );
        var_Destroy( p_input, "highlight" );
292 293 294
    }
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
/**
 * Inform the SPU filters of mouse event
 */
int spu_ProcessMouse( spu_t *p_spu,
                      const vlc_mouse_t *p_mouse,
                      const video_format_t *p_fmt )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->chain_lock );
    filter_chain_MouseEvent( p_sys->p_chain, p_mouse, p_fmt );
    vlc_mutex_unlock( &p_sys->chain_lock );

    return VLC_SUCCESS;
}

311
/**
312
 * Display a subpicture
313
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
314 315
 * Remove the reservation flag of a subpicture, which will cause it to be
 * ready for display.
316
 * \param p_spu the subpicture unit object
317 318
 * \param p_subpic the subpicture to display
 */
319
void spu_PutSubpicture( spu_t *p_spu, subpicture_t *p_subpic )
Sam Hocevar's avatar
 
Sam Hocevar committed
320
{
321
    spu_private_t *p_sys = p_spu->p;
Sam Hocevar's avatar
 
Sam Hocevar committed
322

Laurent Aimar's avatar
Laurent Aimar committed
323 324 325
    /* SPU_DEFAULT_CHANNEL always reset itself */
    if( p_subpic->i_channel == SPU_DEFAULT_CHANNEL )
        spu_ClearChannel( p_spu, SPU_DEFAULT_CHANNEL );
326 327 328 329 330 331 332 333

    /* p_private is for spu only and cannot be non NULL here */
    for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
        assert( r->p_private == NULL );

    /* */
    vlc_mutex_lock( &p_sys->lock );
    if( SpuHeapPush( &p_sys->heap, p_subpic ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
334
    {
335 336 337 338
        vlc_mutex_unlock( &p_sys->lock );
        msg_Err( p_spu, "subpicture heap full" );
        subpicture_Delete( p_subpic );
        return;
Sam Hocevar's avatar
 
Sam Hocevar committed
339
    }
340
    vlc_mutex_unlock( &p_sys->lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
341 342
}

Laurent Aimar's avatar
Laurent Aimar committed
343 344 345
/**
 * This function renders all sub picture units in the list.
 */
346 347 348 349 350 351
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 )
352
{
Laurent Aimar's avatar
Laurent Aimar committed
353
    spu_private_t *p_sys = p_spu->p;
354

Laurent Aimar's avatar
Laurent Aimar committed
355 356
    const int i_source_video_width  = p_fmt_src->i_width;
    const int i_source_video_height = p_fmt_src->i_height;
357

Laurent Aimar's avatar
Laurent Aimar committed
358 359
    unsigned int i_subpicture;
    subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
360

Laurent Aimar's avatar
Laurent Aimar committed
361 362 363 364
    unsigned int i_subtitle_region_count;
    spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
    spu_area_t *p_subtitle_area;
    int i_subtitle_area;
365

Laurent Aimar's avatar
Laurent Aimar committed
366 367 368 369 370 371 372
    /* Preprocess subpictures */
    i_subpicture = 0;
    i_subtitle_region_count = 0;
    for( subpicture_t * p_subpic = p_subpic_list;
            p_subpic != NULL;
                p_subpic = p_subpic->p_next )
    {
373 374 375
        subpicture_Update( p_subpic,
                           p_fmt_src, p_fmt_dst,
                           p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
376

Laurent Aimar's avatar
Laurent Aimar committed
377 378 379 380 381 382
        /* */
        if( p_subpic->b_subtitle )
        {
            for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
                i_subtitle_region_count++;
        }
383

Laurent Aimar's avatar
Laurent Aimar committed
384 385 386
        /* */
        pp_subpicture[i_subpicture++] = p_subpic;
    }
387

Laurent Aimar's avatar
Laurent Aimar committed
388 389
    /* Be sure we have at least 1 picture to process */
    if( i_subpicture <= 0 )
390 391
        return NULL;

392
    subpicture_t *p_output = subpicture_New( NULL );
393

Laurent Aimar's avatar
Laurent Aimar committed
394 395 396
    /* Now order subpicture array
     * XXX The order is *really* important for overlap subtitles positionning */
    qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
Laurent Aimar's avatar
Laurent Aimar committed
397

Laurent Aimar's avatar
Laurent Aimar committed
398 399 400 401 402
    /* Allocate area array for subtitle overlap */
    i_subtitle_area = 0;
    p_subtitle_area = p_subtitle_area_buffer;
    if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
        p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
403

Laurent Aimar's avatar
Laurent Aimar committed
404 405 406 407 408
    /* Process all subpictures and regions (in the right order) */
    for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
    {
        subpicture_t *p_subpic = pp_subpicture[i_index];
        subpicture_region_t *p_region;
409

Laurent Aimar's avatar
Laurent Aimar committed
410 411
        if( !p_subpic->p_region )
            continue;
412

Laurent Aimar's avatar
Laurent Aimar committed
413 414 415 416 417 418 419 420 421
        /* FIXME when possible use a better rendering size than source size
         * (max of display size and source size for example) FIXME */
        int i_render_width  = p_subpic->i_original_picture_width;
        int i_render_height = p_subpic->i_original_picture_height;
        if( !i_render_width || !i_render_height )
        {
            if( i_render_width != 0 || i_render_height != 0 )
                msg_Err( p_spu, "unsupported original picture size %dx%d",
                         i_render_width, i_render_height );
422

Laurent Aimar's avatar
Laurent Aimar committed
423 424 425
            p_subpic->i_original_picture_width  = i_render_width = i_source_video_width;
            p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
        }
426

Laurent Aimar's avatar
Laurent Aimar committed
427 428 429 430
        if( p_sys->p_text )
        {
            p_sys->p_text->fmt_out.video.i_width          =
            p_sys->p_text->fmt_out.video.i_visible_width  = i_render_width;
431

Laurent Aimar's avatar
Laurent Aimar committed
432 433 434
            p_sys->p_text->fmt_out.video.i_height         =
            p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
        }
435

Laurent Aimar's avatar
Laurent Aimar committed
436 437 438
        /* Compute scaling from picture to source size */
        spu_scale_t scale = spu_scale_createq( i_source_video_width,  i_render_width,
                                               i_source_video_height, i_render_height );
439

Laurent Aimar's avatar
Laurent Aimar committed
440 441 442
        /* Update scaling from source size to display size(p_fmt_dst) */
        scale.w = scale.w * p_fmt_dst->i_width  / i_source_video_width;
        scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
443

Laurent Aimar's avatar
Laurent Aimar committed
444 445 446 447 448 449
        /* Set default subpicture aspect ratio
         * FIXME if we only handle 1 aspect ratio per picture, why is it set per
         * region ? */
        p_region = p_subpic->p_region;
        if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
        {
Laurent Aimar's avatar
Laurent Aimar committed
450 451
            p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
            p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
Laurent Aimar's avatar
Laurent Aimar committed
452
        }
453

Laurent Aimar's avatar
Laurent Aimar committed
454 455 456 457 458 459 460 461 462
        /* Take care of the aspect ratio */
        if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
            p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
        {
            /* FIXME FIXME what about region->i_x/i_y ? */
            scale.w = scale.w *
                (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
                p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
        }
463

Laurent Aimar's avatar
Laurent Aimar committed
464 465 466 467 468 469 470
        /* Render all regions
         * We always transform non absolute subtitle into absolute one on the
         * first rendering to allow good subtitle overlap support.
         */
        for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
        {
            spu_area_t area;
471

Laurent Aimar's avatar
Laurent Aimar committed
472 473 474
            /* Check scale validity */
            if( scale.w <= 0 || scale.h <= 0 )
                continue;
475

Laurent Aimar's avatar
Laurent Aimar committed
476
            /* */
477 478
            subpicture_region_t *p_render;
            SpuRenderRegion( p_spu, &p_render, &area,
Laurent Aimar's avatar
Laurent Aimar committed
479
                             p_subpic, p_region, scale, p_fmt_dst,
480
                             p_subtitle_area, i_subtitle_area,
481
                             p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
482 483 484 485 486 487 488
            if( p_render )
            {
                subpicture_region_t **pp_last = &p_output->p_region;
                while( *pp_last )
                    pp_last = &(*pp_last)->p_next;
                *pp_last = p_render;
            }
489

Laurent Aimar's avatar
Laurent Aimar committed
490 491 492
            if( p_subpic->b_subtitle )
            {
                area = spu_area_unscaled( area, scale );
493 494 495 496 497
                if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
                {
                    p_region->i_x = area.i_x;
                    p_region->i_y = area.i_y;
                }
Laurent Aimar's avatar
Laurent Aimar committed
498 499 500 501
                if( p_subtitle_area )
                    p_subtitle_area[i_subtitle_area++] = area;
            }
        }
502 503
        if( p_subpic->b_subtitle )
            p_subpic->b_absolute = true;
Laurent Aimar's avatar
Laurent Aimar committed
504
    }
505

Laurent Aimar's avatar
Laurent Aimar committed
506 507 508
    /* */
    if( p_subtitle_area != p_subtitle_area_buffer )
        free( p_subtitle_area );
509

510
    return p_output;
511 512
}

Laurent Aimar's avatar
Laurent Aimar committed
513
/*****************************************************************************
514
 * SpuSortSubpictures: find the subpictures to display
Laurent Aimar's avatar
Laurent Aimar committed
515 516 517 518 519 520 521 522
 *****************************************************************************
 * 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.
 *****************************************************************************/
523 524 525 526
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
527
{
Laurent Aimar's avatar
Laurent Aimar committed
528 529
    spu_private_t *p_sys = p_spu->p;
    subpicture_t *p_subpic = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
530

Laurent Aimar's avatar
Laurent Aimar committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    /* 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
551 552
    /* 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
553
    for( int i = 0; i < i_channel_count; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
554
    {
Laurent Aimar's avatar
Laurent Aimar committed
555
        const int i_channel = pi_channel[i];
556 557 558 559
        subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
        bool         pb_available_late[VOUT_MAX_SUBPICTURES];
        int          i_available = 0;

560
        mtime_t      start_date = render_subtitle_date;
561
        mtime_t      ephemer_subtitle_date = 0;
562
        mtime_t      ephemer_osd_date = 0;
563 564
        int64_t      i_ephemer_subtitle_order = INT64_MIN;
        int64_t      i_ephemer_system_order = INT64_MIN;
Laurent Aimar's avatar
Laurent Aimar committed
565 566
        int i_index;

567
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
568 569 570 571
        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;
572 573
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
574 575 576 577 578 579 580 581 582 583 584 585 586

            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;
            }
587
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
588 589
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
590 591 592 593 594
            {
                /* Too early, come back next monday */
                continue;
            }

595
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
596 597 598
            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 )
            {
599
                *pi_ephemer_date = p_current->i_start;
600 601 602
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
603

604
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
605

606
            b_late = b_stop_valid && p_current->i_stop <= render_date;
607 608

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
609
             * in case picture that should not be displayed anymore (display_time)
610
             * overlap with a picture to be displayed (p_current->i_start)  */
611
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
612 613 614 615 616 617
                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
618 619
        }

620 621 622 623 624 625 626 627
        /* 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
628
        {
629 630
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
631

632 633
            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;
634
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
635

636 637 638
            /* 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
639
            {
640 641 642 643 644
                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
645
            }
646 647 648

            if( b_rejet )
                SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
649
            else
650
                SubpictureChain( &p_subpic, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
651
        }
Laurent Aimar's avatar
Laurent Aimar committed
652
    }
653

654
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
655

Laurent Aimar's avatar
Laurent Aimar committed
656
    return p_subpic;
Laurent Aimar's avatar
Laurent Aimar committed
657 658
}

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
subpicture_t *spu_Render( spu_t *p_spu,
                          const video_format_t *p_fmt_dst,
                          const video_format_t *p_fmt_src,
                          mtime_t render_subtitle_date,
                          mtime_t render_osd_date,
                          bool b_subtitle_only )
{
    spu_private_t *p_sys = p_spu->p;

    /* Update sub-filter chain */
    vlc_mutex_lock( &p_sys->lock );
    char *psz_chain_update = p_sys->psz_chain_update;
    p_sys->psz_chain_update = NULL;
    vlc_mutex_unlock( &p_sys->lock );

    vlc_mutex_lock( &p_sys->chain_lock );
    if( psz_chain_update )
    {
        filter_chain_Reset( p_sys->p_chain, NULL, NULL );

        filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );

        free( psz_chain_update );
    }
    /* Run subpicture filters */
    filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
    vlc_mutex_unlock( &p_sys->chain_lock );

    /* Get the sorted list of subpicture to render */
    vlc_mutex_lock( &p_sys->lock );

    subpicture_t *p_list = SpuSortSubpictures( p_spu,
                                               render_subtitle_date,
                                               render_osd_date,
                                               b_subtitle_only );
    if( !p_list )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }

    /* Render the current list of subpictures */
    subpicture_t *p_render = SpuRenderSubpictures( p_spu,
                                                   p_fmt_dst,
                                                   p_list,
                                                   p_fmt_src,
                                                   render_subtitle_date,
                                                   render_osd_date );
    vlc_mutex_unlock( &p_sys->lock );

    return p_render;
}

Laurent Aimar's avatar
Laurent Aimar committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->lock );
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
        subpicture_t *p_current = p_entry->p_subpicture;

        if( p_current && p_current->b_subtitle )
        {
            if( p_current->i_start > 0 )
                p_current->i_start += i_duration;
            if( p_current->i_stop > 0 )
                p_current->i_stop += i_duration;
        }
    }
    vlc_mutex_unlock( &p_sys->lock );
}

Laurent Aimar's avatar
Laurent Aimar committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
int spu_RegisterChannel( spu_t *p_spu )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->lock );
    int i_channel = p_sys->i_channel++;
    vlc_mutex_unlock( &p_sys->lock );

    return i_channel;
}

void spu_ClearChannel( spu_t *p_spu, int i_channel )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->lock );

    for( int i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
    {
        spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
        subpicture_t *p_subpic = p_entry->p_subpicture;

        if( !p_subpic )
            continue;
        if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == SPU_DEFAULT_CHANNEL ) )
            continue;

        /* You cannot delete subpicture outside of spu_SortSubpictures */
        p_entry->b_reject = true;
    }

    vlc_mutex_unlock( &p_sys->lock );
}

767 768 769 770 771 772 773 774 775 776 777 778
void spu_ChangeFilters( spu_t *p_spu, const char *psz_filters )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->lock );

    free( p_sys->psz_chain_update );
    p_sys->psz_chain_update = strdup( psz_filters );

    vlc_mutex_unlock( &p_sys->lock );
}

Laurent Aimar's avatar
Laurent Aimar committed
779 780 781 782 783 784 785 786 787
void spu_ChangeMargin( spu_t *p_spu, int i_margin )
{
    spu_private_t *p_sys = p_spu->p;

    vlc_mutex_lock( &p_sys->lock );
    p_sys->i_margin = i_margin;
    vlc_mutex_unlock( &p_sys->lock );
}

788
/* */
Laurent Aimar's avatar
Laurent Aimar committed
789
static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
790
{
Laurent Aimar's avatar
Laurent Aimar committed
791 792 793
    p_subpic->p_next = *pp_head;

    *pp_head = p_subpic;
794
}
Laurent Aimar's avatar
Laurent Aimar committed
795 796 797 798 799

/*****************************************************************************
 * heap managment
 *****************************************************************************/
static void SpuHeapInit( spu_heap_t *p_heap )
800
{
Laurent Aimar's avatar
Laurent Aimar committed
801 802 803
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
804

Laurent Aimar's avatar
Laurent Aimar committed
805 806 807 808
        e->p_subpicture = NULL;
        e->b_reject = false;
    }
}
809

Laurent Aimar's avatar
Laurent Aimar committed
810 811 812
static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
{
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
813
    {
Laurent Aimar's avatar
Laurent Aimar committed
814
        spu_heap_entry_t *e = &p_heap->p_entry[i];
815

Laurent Aimar's avatar
Laurent Aimar committed
816 817
        if( e->p_subpicture )
            continue;
818

Laurent Aimar's avatar
Laurent Aimar committed
819 820 821 822 823 824
        e->p_subpicture = p_subpic;
        e->b_reject = false;
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
825

Laurent Aimar's avatar
Laurent Aimar committed
826 827 828
static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
{
    spu_heap_entry_t *e = &p_heap->p_entry[i_index];
829

Laurent Aimar's avatar
Laurent Aimar committed
830 831
    if( e->p_subpicture )
        subpicture_Delete( e->p_subpicture );
832

Laurent Aimar's avatar
Laurent Aimar committed
833 834
    e->p_subpicture = NULL;
}
835

Laurent Aimar's avatar
Laurent Aimar committed
836
static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
Laurent Aimar's avatar
Laurent Aimar committed
837
{
Laurent Aimar's avatar
Laurent Aimar committed
838
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
839
    {
Laurent Aimar's avatar
Laurent Aimar committed
840
        spu_heap_entry_t *e = &p_heap->p_entry[i];
Laurent Aimar's avatar
Laurent Aimar committed
841

Laurent Aimar's avatar
Laurent Aimar committed
842 843 844 845 846
        if( e->p_subpicture != p_subpic )
            continue;

        SpuHeapDeleteAt( p_heap, i );
        return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
847
    }
Laurent Aimar's avatar
Laurent Aimar committed
848 849
    return VLC_EGENERIC;
}
Laurent Aimar's avatar
Laurent Aimar committed
850

Laurent Aimar's avatar
Laurent Aimar committed
851 852 853
static void SpuHeapClean( spu_heap_t *p_heap )
{
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
854
    {
Laurent Aimar's avatar
Laurent Aimar committed
855 856 857
        spu_heap_entry_t *e = &p_heap->p_entry[i];
        if( e->p_subpicture )
            subpicture_Delete( e->p_subpicture );
Laurent Aimar's avatar
Laurent Aimar committed
858
    }
Laurent Aimar's avatar
Laurent Aimar committed
859
}
860

Laurent Aimar's avatar
Laurent Aimar committed
861
static void FilterRelease( filter_t *p_filter )
862
{
Laurent Aimar's avatar
Laurent Aimar committed
863 864
    if( p_filter->p_module )
        module_unneed( p_filter, p_filter->p_module );
865 866
    if( p_filter->p_owner )
        free( p_filter->p_owner );
867

Laurent Aimar's avatar
Laurent Aimar committed
868
    vlc_object_release( p_filter );
869 870
}

Laurent Aimar's avatar
Laurent Aimar committed
871 872 873
static void SpuRenderCreateAndLoadText( spu_t *p_spu )
{
    filter_t *p_text;
874

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

Laurent Aimar's avatar
Laurent Aimar committed
877 878 879 880 881
    p_spu->p->p_text =
    p_text        = vlc_custom_create( p_spu, sizeof(filter_t),
                                       VLC_OBJECT_GENERIC, "spu text" );
    if( !p_text )
        return;
Laurent Aimar's avatar
Laurent Aimar committed
882

883 884 885
    p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
    p_text->p_owner->p_spu = p_spu;

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

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

894
    p_text->pf_get_attachments = spu_get_attachments;
895

Laurent Aimar's avatar
Laurent Aimar committed
896
    vlc_object_attach( p_text, p_spu );
Laurent Aimar's avatar
Laurent Aimar committed
897

Laurent Aimar's avatar
Laurent Aimar committed
898 899 900 901 902 903 904 905 906
    /* 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 );
907

Laurent Aimar's avatar
Laurent Aimar committed
908 909
    if( !p_text->p_module )
        p_text->p_module = module_need( p_text, "text renderer", NULL, false );
910

Laurent Aimar's avatar
Laurent Aimar committed
911 912 913 914 915 916
    /* 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 );
}
917

Laurent Aimar's avatar
Laurent Aimar committed
918 919 920 921 922
static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
                                     vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
                                     bool b_resize )
{
    filter_t *p_scale;
923

Laurent Aimar's avatar
Laurent Aimar committed
924 925 926 927
    p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
                                 VLC_OBJECT_GENERIC, "scale" );
    if( !p_scale )
        return NULL;
928

Laurent Aimar's avatar
Laurent Aimar committed
929 930 931 932
    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;
Laurent Aimar's avatar
Laurent Aimar committed
933

Laurent Aimar's avatar
Laurent Aimar committed
934 935 936 937
    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;
Laurent Aimar's avatar
Laurent Aimar committed
938

939 940
    p_scale->pf_video_buffer_new = spu_new_video_buffer;
    p_scale->pf_video_buffer_del = spu_del_video_buffer;
Laurent Aimar's avatar
Laurent Aimar committed
941

Laurent Aimar's avatar
Laurent Aimar committed
942
    vlc_object_attach( p_scale, p_obj );
943
    p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
Laurent Aimar's avatar
Laurent Aimar committed
944

Laurent Aimar's avatar
Laurent Aimar committed
945 946 947 948 949 950 951 952 953
    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),
954
                                            VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
Laurent Aimar's avatar
Laurent Aimar committed
955 956 957
    /* 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),
958
                                                 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
Laurent Aimar's avatar
Laurent Aimar committed
959
}
Laurent Aimar's avatar
Laurent Aimar committed
960

Laurent Aimar's avatar
Laurent Aimar committed
961 962
static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
                           subpicture_t *p_subpic, subpicture_region_t *p_region,
963
                           int i_min_scale_ratio, mtime_t render_date )
Laurent Aimar's avatar
Laurent Aimar committed
964 965
{
    filter_t *p_text = p_spu->p->p_text;
Laurent Aimar's avatar
Laurent Aimar committed
966

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

Laurent Aimar's avatar
Laurent Aimar committed
969 970
    if( !p_text || !p_text->p_module )
        goto exit;
Laurent Aimar's avatar
Laurent Aimar committed
971

Laurent Aimar's avatar
Laurent Aimar committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    /* 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 );
992
    var_SetTime( p_text, "spu-elapsed", render_date );
Laurent Aimar's avatar
Laurent Aimar committed
993 994
    var_SetBool( p_text, "text-rerender", false );
    var_SetInteger( p_text, "scale", i_min_scale_ratio );
Laurent Aimar's avatar
Laurent Aimar committed
995

Laurent Aimar's avatar
Laurent Aimar committed
996
    if( p_text->pf_render_html && p_region->psz_html )
Laurent Aimar's avatar
Laurent Aimar committed
997
    {
Laurent Aimar's avatar
Laurent Aimar committed
998
        p_text->pf_render_html( p_text, p_region, p_region );
Laurent Aimar's avatar
Laurent Aimar committed
999
    }
Laurent Aimar's avatar
Laurent Aimar committed
1000
    else if( p_text->pf_render_text )
Laurent Aimar's avatar
Laurent Aimar committed
1001
    {
Laurent Aimar's avatar
Laurent Aimar committed
1002
        p_text->pf_render_text( p_text, p_region, p_region );