vout_subpictures.c 62.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 40
#include <vlc_vout.h>
#include <vlc_block.h>
#include <vlc_filter.h>
#include <vlc_osd.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

45 46 47
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
48

49
/* Number of simultaneous subpictures */
50
#define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
51

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

71 72
struct spu_private_t
{
73 74 75 76
    vlc_mutex_t lock;   /* lock to protect all followings fields */

    spu_heap_t heap;

77 78 79 80 81 82 83 84 85 86 87 88
    int i_channel;             /**< number of subpicture channels registered */
    filter_t *p_blend;                            /**< alpha blending module */
    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 */

89
    /* Subpiture filters */
Laurent Aimar's avatar
Laurent Aimar committed
90
    char           *psz_chain_update;
91
    vlc_mutex_t    chain_lock;
92
    filter_chain_t *p_chain;
93 94 95

    /* */
    mtime_t i_last_sort_date;
96 97
};

98 99 100 101 102 103
/* */
struct subpicture_region_private_t
{
    video_format_t fmt;
    picture_t      *p_picture;
};
Laurent Aimar's avatar
Laurent Aimar committed
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 130 131 132 133 134 135
static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
static void SpuRegionPrivateDelete( subpicture_region_private_t * );

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

136

137 138 139
/* Subpicture rendered flag
 * FIXME ? it could be moved to private ? */
#define SUBPICTURE_RENDERED  (0x1000)
Laurent Aimar's avatar
Laurent Aimar committed
140
#if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
141 142 143
#   error SUBPICTURE_RENDERED too low
#endif

Laurent Aimar's avatar
Laurent Aimar committed
144 145
#define SCALE_UNIT (1000)

146 147 148 149
static void SubpictureUpdate( subpicture_t *,
                              const video_format_t *p_fmt_src,
                              const video_format_t *p_fmt_dst,
                              mtime_t i_ts );
150
static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
Laurent Aimar's avatar
Laurent Aimar committed
151 152 153 154 155 156 157
static int SubpictureCmp( const void *s0, const void *s1 );

static void SpuRenderRegion( spu_t *,
                             picture_t *p_pic_dst, spu_area_t *,
                             subpicture_t *, subpicture_region_t *,
                             const spu_scale_t scale_size,
                             const video_format_t *p_fmt,
158 159
                             const spu_area_t *p_subtitle_area, int i_subtitle_area,
                             mtime_t render_date );
160

161 162 163
static void UpdateSPU   ( spu_t *, vlc_object_t * );
static int  CropCallback( vlc_object_t *, char const *,
                          vlc_value_t, vlc_value_t, void * );
164 165
static int MarginCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
166

167
/* Buffer allocation for SPU filter (blend, scale, ...) */
168 169
static subpicture_t *spu_new_buffer( filter_t * );
static void spu_del_buffer( filter_t *, subpicture_t * );
170 171
static picture_t *spu_new_video_buffer( filter_t * );
static void spu_del_video_buffer( filter_t *, picture_t * );
172

173
/* Buffer aloccation fir SUB filter */
174 175 176
static int SubFilterCallback( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );

177 178
static int SubFilterAllocationInit( filter_t *, void * );
static void SubFilterAllocationClean( filter_t * );
179

180
/* */
Laurent Aimar's avatar
Laurent Aimar committed
181 182
static void SpuRenderCreateAndLoadText( spu_t * );
static void SpuRenderCreateAndLoadScale( spu_t * );
183
static void FilterRelease( filter_t *p_filter );
184

Laurent Aimar's avatar
Laurent Aimar committed
185 186 187
/*****************************************************************************
 * Public API
 *****************************************************************************/
188 189

#undef spu_Create
190
/**
191
 * Creates the subpicture unit
192
 *
193
 * \param p_this the parent object which creates the subpicture unit
194
 */
195
spu_t *spu_Create( vlc_object_t *p_this )
196
{
197 198
    spu_t *p_spu;
    spu_private_t *p_sys;
199

200 201 202 203
    p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
                               VLC_OBJECT_GENERIC, "subpicture" );
    if( !p_spu )
        return NULL;
204
    vlc_object_attach( p_spu, p_this );
205 206 207 208 209

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

    /* Initialize private fields */
210 211 212 213
    vlc_mutex_init( &p_sys->lock );

    SpuHeapInit( &p_sys->heap );

214 215 216 217
    p_sys->p_blend = NULL;
    p_sys->p_text = NULL;
    p_sys->p_scale = NULL;
    p_sys->p_scale_yuvp = NULL;
218

219
    p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
220

221
    /* Register the default subpicture channel */
222
    p_sys->i_channel = 2;
223

Laurent Aimar's avatar
Laurent Aimar committed
224
    p_sys->psz_chain_update = NULL;
225
    vlc_mutex_init( &p_sys->chain_lock );
226
    p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
227 228
                                       SubFilterAllocationInit,
                                       SubFilterAllocationClean,
229
                                       p_spu );
230 231 232 233 234

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

235 236 237
    /* */
    p_sys->i_last_sort_date = -1;

238 239 240 241 242 243 244 245 246 247 248
    return p_spu;
}

/**
 * Initialise the subpicture unit
 *
 * \param p_spu the subpicture unit object
 */
int spu_Init( spu_t *p_spu )
{
    var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249
    var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
Laurent Aimar's avatar
Laurent Aimar committed
250
    var_TriggerCallback( p_spu, "sub-filter" );
251

252
    return VLC_SUCCESS;
253
}
Antoine Cellerier's avatar
 
Antoine Cellerier committed
254

255
/**
256
 * Destroy the subpicture unit
257
 *
258
 * \param p_this the parent object which destroys the subpicture unit
259
 */
260
void spu_Destroy( spu_t *p_spu )
261
{
262
    spu_private_t *p_sys = p_spu->p;
263

264 265
    var_DelCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );

266
    if( p_sys->p_blend )
267
        filter_DeleteBlend( p_sys->p_blend );
268 269 270

    if( p_sys->p_text )
        FilterRelease( p_sys->p_text );
271

272 273
    if( p_sys->p_scale_yuvp )
        FilterRelease( p_sys->p_scale_yuvp );
274

275 276
    if( p_sys->p_scale )
        FilterRelease( p_sys->p_scale );
277

278
    filter_chain_Delete( p_sys->p_chain );
279
    vlc_mutex_destroy( &p_sys->chain_lock );
Laurent Aimar's avatar
Laurent Aimar committed
280
    free( p_sys->psz_chain_update );
281

282 283 284 285
    /* Destroy all remaining subpictures */
    SpuHeapClean( &p_sys->heap );

    vlc_mutex_destroy( &p_sys->lock );
286

287
    vlc_object_release( p_spu );
288 289
}

290 291 292
/**
 * Attach/Detach the SPU from any input
 *
293
 * \param p_this the object in which to destroy the subpicture unit
294 295
 * \param b_attach to select attach or detach
 */
296
void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
297 298 299
{
    vlc_object_t *p_input;

300
    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
301 302
    if( !p_input )
        return;
303 304

    if( b_attach )
305
    {
306
        UpdateSPU( p_spu, VLC_OBJECT(p_input) );
307
        var_Create( p_input, "highlight", VLC_VAR_BOOL );
308
        var_AddCallback( p_input, "highlight", CropCallback, p_spu );
309
        var_AddCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
310 311 312 313 314

        vlc_mutex_lock( &p_spu->p->lock );
        p_spu->p->i_margin = var_GetInteger( p_input, "sub-margin" );
        vlc_mutex_unlock( &p_spu->p->lock );

315 316 317 318
        vlc_object_release( p_input );
    }
    else
    {
319
        /* Delete callbacks */
320
        var_DelCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
321 322
        var_DelCallback( p_input, "highlight", CropCallback, p_spu );
        var_Destroy( p_input, "highlight" );
323 324 325 326
        vlc_object_release( p_input );
    }
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/**
 * 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;
}

343
/**
344
 * Display a subpicture
345
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
346 347
 * Remove the reservation flag of a subpicture, which will cause it to be
 * ready for display.
348
 * \param p_spu the subpicture unit object
349 350
 * \param p_subpic the subpicture to display
 */
351
void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
Sam Hocevar's avatar
 
Sam Hocevar committed
352
{
353
    spu_private_t *p_sys = p_spu->p;
Sam Hocevar's avatar
 
Sam Hocevar committed
354

Laurent Aimar's avatar
Laurent Aimar committed
355 356 357
    /* SPU_DEFAULT_CHANNEL always reset itself */
    if( p_subpic->i_channel == SPU_DEFAULT_CHANNEL )
        spu_ClearChannel( p_spu, SPU_DEFAULT_CHANNEL );
358 359 360 361 362 363 364 365

    /* 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
366
    {
367 368 369 370
        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
371
    }
372
    vlc_mutex_unlock( &p_sys->lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
373 374
}

Laurent Aimar's avatar
Laurent Aimar committed
375 376 377 378 379 380
/**
 * This function renders all sub picture units in the list.
 */
void spu_RenderSubpictures( spu_t *p_spu,
                            picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
                            subpicture_t *p_subpic_list,
381
                            const video_format_t *p_fmt_src,
382
                            mtime_t render_subtitle_date )
383
{
Laurent Aimar's avatar
Laurent Aimar committed
384
    spu_private_t *p_sys = p_spu->p;
385

386
    const mtime_t render_osd_date = mdate();
387

Laurent Aimar's avatar
Laurent Aimar committed
388 389
    const int i_source_video_width  = p_fmt_src->i_width;
    const int i_source_video_height = p_fmt_src->i_height;
390

Laurent Aimar's avatar
Laurent Aimar committed
391 392
    unsigned int i_subpicture;
    subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
393

Laurent Aimar's avatar
Laurent Aimar committed
394 395 396 397
    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;
398

Laurent Aimar's avatar
Laurent Aimar committed
399
    vlc_mutex_lock( &p_sys->lock );
400

Laurent Aimar's avatar
Laurent Aimar committed
401 402 403 404 405 406 407
    /* 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 )
    {
408 409 410
        SubpictureUpdate( p_subpic,
                          p_fmt_src, p_fmt_dst,
                          p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
411

Laurent Aimar's avatar
Laurent Aimar committed
412 413 414 415 416 417
        /* */
        if( p_subpic->b_subtitle )
        {
            for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
                i_subtitle_region_count++;
        }
418

Laurent Aimar's avatar
Laurent Aimar committed
419 420 421
        /* */
        pp_subpicture[i_subpicture++] = p_subpic;
    }
422

Laurent Aimar's avatar
Laurent Aimar committed
423 424
    /* Be sure we have at least 1 picture to process */
    if( i_subpicture <= 0 )
425
    {
Laurent Aimar's avatar
Laurent Aimar committed
426 427
        vlc_mutex_unlock( &p_sys->lock );
        return;
428 429
    }

Laurent Aimar's avatar
Laurent Aimar committed
430 431 432
    /* 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
433

Laurent Aimar's avatar
Laurent Aimar committed
434 435 436 437 438
    /* 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) );
439

Laurent Aimar's avatar
Laurent Aimar committed
440 441
    /* Create the blending module */
    if( !p_sys->p_blend )
442
        p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
443

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

Laurent Aimar's avatar
Laurent Aimar committed
450 451
        if( !p_subpic->p_region )
            continue;
452

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

Laurent Aimar's avatar
Laurent Aimar committed
463 464 465
            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;
        }
466

Laurent Aimar's avatar
Laurent Aimar committed
467 468 469 470
        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;
471

Laurent Aimar's avatar
Laurent Aimar committed
472 473 474
            p_sys->p_text->fmt_out.video.i_height         =
            p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
        }
475

Laurent Aimar's avatar
Laurent Aimar committed
476 477 478
        /* 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 );
479

Laurent Aimar's avatar
Laurent Aimar committed
480 481 482
        /* 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;
483

Laurent Aimar's avatar
Laurent Aimar committed
484 485 486 487 488 489
        /* 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
490 491
            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
492
        }
493

Laurent Aimar's avatar
Laurent Aimar committed
494 495 496 497 498 499 500 501 502
        /* 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;
        }
503

Laurent Aimar's avatar
Laurent Aimar committed
504 505 506 507 508 509 510
        /* 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;
511

Laurent Aimar's avatar
Laurent Aimar committed
512 513 514
            /* Check scale validity */
            if( scale.w <= 0 || scale.h <= 0 )
                continue;
515

Laurent Aimar's avatar
Laurent Aimar committed
516 517 518
            /* */
            SpuRenderRegion( p_spu, p_pic_dst, &area,
                             p_subpic, p_region, scale, p_fmt_dst,
519
                             p_subtitle_area, i_subtitle_area,
520
                             p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
521

Laurent Aimar's avatar
Laurent Aimar committed
522 523 524
            if( p_subpic->b_subtitle )
            {
                area = spu_area_unscaled( area, scale );
525 526 527 528 529
                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
530 531 532 533
                if( p_subtitle_area )
                    p_subtitle_area[i_subtitle_area++] = area;
            }
        }
534 535
        if( p_subpic->b_subtitle )
            p_subpic->b_absolute = true;
Laurent Aimar's avatar
Laurent Aimar committed
536
    }
537

Laurent Aimar's avatar
Laurent Aimar committed
538 539 540
    /* */
    if( p_subtitle_area != p_subtitle_area_buffer )
        free( p_subtitle_area );
541

Laurent Aimar's avatar
Laurent Aimar committed
542
    vlc_mutex_unlock( &p_sys->lock );
543 544
}

Laurent Aimar's avatar
Laurent Aimar committed
545 546 547 548 549 550 551 552 553 554
/*****************************************************************************
 * spu_SortSubpictures: find the subpictures to display
 *****************************************************************************
 * 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.
 *****************************************************************************/
555
subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
556
                                   bool b_subtitle_only )
Laurent Aimar's avatar
Laurent Aimar committed
557
{
Laurent Aimar's avatar
Laurent Aimar committed
558 559 560
    spu_private_t *p_sys = p_spu->p;
    int i_channel;
    subpicture_t *p_subpic = NULL;
561
    const mtime_t render_osd_date = mdate();
562

Laurent Aimar's avatar
Laurent Aimar committed
563 564 565 566 567 568
    /* 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 );

569
    vlc_mutex_lock( &p_sys->chain_lock );
Laurent Aimar's avatar
Laurent Aimar committed
570 571 572 573 574 575 576 577
    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 );
    }
Laurent Aimar's avatar
Laurent Aimar committed
578
    /* Run subpicture filters */
579
    filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
580
    vlc_mutex_unlock( &p_sys->chain_lock );
Laurent Aimar's avatar
Laurent Aimar committed
581

Laurent Aimar's avatar
Laurent Aimar committed
582
    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
583

Laurent Aimar's avatar
Laurent Aimar committed
584 585 586
    /* We get an easily parsable chained list of subpictures which
     * ends with NULL since p_subpic was initialized to NULL. */
    for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
Laurent Aimar's avatar
Laurent Aimar committed
587
    {
588 589 590 591
        subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
        bool         pb_available_late[VOUT_MAX_SUBPICTURES];
        int          i_available = 0;

592
        mtime_t      start_date = render_subtitle_date;
593
        mtime_t      ephemer_subtitle_date = 0;
594
        mtime_t      ephemer_osd_date = 0;
595 596
        int64_t      i_ephemer_subtitle_order = INT64_MIN;
        int64_t      i_ephemer_system_order = INT64_MIN;
Laurent Aimar's avatar
Laurent Aimar committed
597 598
        int i_index;

599
        /* Select available pictures */
Laurent Aimar's avatar
Laurent Aimar committed
600 601 602 603
        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;
604 605
            bool b_stop_valid;
            bool b_late;
Laurent Aimar's avatar
Laurent Aimar committed
606 607 608 609 610 611 612 613 614 615 616 617 618

            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;
            }
619
            const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
620 621
            if( render_date &&
                render_date < p_current->i_start )
Laurent Aimar's avatar
Laurent Aimar committed
622 623 624 625 626
            {
                /* Too early, come back next monday */
                continue;
            }

627
            mtime_t *pi_ephemer_date  = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
628 629 630
            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 )
            {
631
                *pi_ephemer_date = p_current->i_start;
632 633 634
                if( p_current->i_order > *pi_ephemer_order )
                    *pi_ephemer_order = p_current->i_order;
            }
Laurent Aimar's avatar
Laurent Aimar committed
635

636
            b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
637

638
            b_late = b_stop_valid && p_current->i_stop <= render_date;
639 640

            /* start_date will be used for correct automatic overlap support
Laurent Aimar's avatar
Laurent Aimar committed
641
             * in case picture that should not be displayed anymore (display_time)
642
             * overlap with a picture to be displayed (p_current->i_start)  */
643
            if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
644 645 646 647 648 649
                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
650 651
        }

652 653 654 655 656 657 658 659
        /* 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
660
        {
661 662
            subpicture_t *p_current = p_available_subpic[i_index];
            bool b_late = pb_available_late[i_index];
Laurent Aimar's avatar
Laurent Aimar committed
663

664 665
            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;
666
            const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
667

668 669 670
            /* 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
671
            {
672 673 674 675 676
                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
677
            }
678 679 680

            if( b_rejet )
                SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
681
            else
682
                SubpictureChain( &p_subpic, p_current );
Laurent Aimar's avatar
Laurent Aimar committed
683
        }
Laurent Aimar's avatar
Laurent Aimar committed
684
    }
685

686
    p_sys->i_last_sort_date = render_subtitle_date;
Laurent Aimar's avatar
Laurent Aimar committed
687
    vlc_mutex_unlock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
688

Laurent Aimar's avatar
Laurent Aimar committed
689
    return p_subpic;
Laurent Aimar's avatar
Laurent Aimar committed
690 691
}

Laurent Aimar's avatar
Laurent Aimar committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
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
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
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 );
}

Laurent Aimar's avatar
Laurent Aimar committed
747 748 749
/*****************************************************************************
 * subpicture_t allocation
 *****************************************************************************/
750 751 752 753 754 755 756
struct subpicture_private_t
{
    video_format_t src;
    video_format_t dst;
};

subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
757
{
Laurent Aimar's avatar
Laurent Aimar committed
758 759 760
    subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
    if( !p_subpic )
        return NULL;
761

Laurent Aimar's avatar
Laurent Aimar committed
762 763 764 765 766 767 768
    p_subpic->i_order    = 0;
    p_subpic->b_absolute = true;
    p_subpic->b_fade     = false;
    p_subpic->b_subtitle = false;
    p_subpic->i_alpha    = 0xFF;
    p_subpic->p_region   = NULL;

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
    if( p_upd )
    {
        subpicture_private_t *p_private = malloc( sizeof(*p_private) );
        if( !p_private )
        {
            free( p_subpic );
            return NULL;
        }
        video_format_Init( &p_private->src, 0 );
        video_format_Init( &p_private->dst, 0 );

        p_subpic->updater   = *p_upd;
        p_subpic->p_private = p_private;
    }
    else
    {
        p_subpic->p_private = NULL;

        p_subpic->updater.pf_validate = NULL;
        p_subpic->updater.pf_update   = NULL;
        p_subpic->updater.pf_destroy  = NULL;
        p_subpic->updater.p_sys       = NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
792
    return p_subpic;
793
}
Laurent Aimar's avatar
Laurent Aimar committed
794

795
void subpicture_Delete( subpicture_t *p_subpic )
796
{
Laurent Aimar's avatar
Laurent Aimar committed
797 798 799
    subpicture_region_ChainDelete( p_subpic->p_region );
    p_subpic->p_region = NULL;

800 801 802 803
    if( p_subpic->updater.pf_destroy )
        p_subpic->updater.pf_destroy( p_subpic );

    free( p_subpic->p_private );
Laurent Aimar's avatar
Laurent Aimar committed
804
    free( p_subpic );
805
}
Laurent Aimar's avatar
Laurent Aimar committed
806 807

static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
808
{
Laurent Aimar's avatar
Laurent Aimar committed
809 810 811
    p_subpic->p_next = *pp_head;

    *pp_head = p_subpic;
812
}
Laurent Aimar's avatar
Laurent Aimar committed
813

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
                                         picture_t *p_picture, vlc_fourcc_t i_chroma )
{
    /* */
    video_format_t fmt_in = p_picture->format;

    /* */
    video_format_t fmt_out;
    fmt_out = fmt_in;
    fmt_out.i_chroma = i_chroma;

    /* */
    image_handler_t *p_image = image_HandlerCreate( p_obj );
    if( !p_image )
        return NULL;

    picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );

    image_HandlerDelete( p_image );

    if( !p_pip )
        return NULL;

837
    subpicture_t *p_subpic = subpicture_New( NULL );
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    if( !p_subpic )
    {
         picture_Release( p_pip );
         return NULL;
    }

    p_subpic->i_original_picture_width  = fmt_out.i_width;
    p_subpic->i_original_picture_height = fmt_out.i_height;

    fmt_out.i_sar_num =
    fmt_out.i_sar_den = 0;

    p_subpic->p_region = subpicture_region_New( &fmt_out );
    if( p_subpic->p_region )
    {
        picture_Release( p_subpic->p_region->p_picture );
        p_subpic->p_region->p_picture = p_pip;
    }
    else
    {
        picture_Release( p_pip );
    }
    return p_subpic;
}

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static void SubpictureUpdate( subpicture_t *p_subpicture,
                              const video_format_t *p_fmt_src,
                              const video_format_t *p_fmt_dst,
                              mtime_t i_ts )
{
    subpicture_updater_t *p_upd = &p_subpicture->updater;
    subpicture_private_t *p_private = p_subpicture->p_private;

    if( !p_upd->pf_validate )
        return;
    if( !p_upd->pf_validate( p_subpicture,
                          !video_format_IsSimilar( p_fmt_src,
                                                   &p_private->src ), p_fmt_src,
                          !video_format_IsSimilar( p_fmt_dst,
                                                   &p_private->dst ), p_fmt_dst,
                          i_ts ) )
        return;

    subpicture_region_ChainDelete( p_subpicture->p_region );
    p_subpicture->p_region = NULL;

    p_upd->pf_update( p_subpicture, p_fmt_src, p_fmt_dst, i_ts );

    video_format_Clean( &p_private->src );
    video_format_Clean( &p_private->dst );

    video_format_Copy( &p_private->src, p_fmt_src );
    video_format_Copy( &p_private->dst, p_fmt_dst );
}

Laurent Aimar's avatar
Laurent Aimar committed
893 894 895 896
/*****************************************************************************
 * subpicture_region_t allocation
 *****************************************************************************/
subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
897
{
Laurent Aimar's avatar
Laurent Aimar committed
898 899 900
    subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
    if( !p_region )
        return NULL;
901

Laurent Aimar's avatar
Laurent Aimar committed
902 903
    p_region->fmt = *p_fmt;
    p_region->fmt.p_palette = NULL;
904
    if( p_fmt->i_chroma == VLC_CODEC_YUVP )
Laurent Aimar's avatar
Laurent Aimar committed
905 906 907 908 909 910 911 912 913 914 915
    {
        p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
        if( p_fmt->p_palette )
            *p_region->fmt.p_palette = *p_fmt->p_palette;
    }
    p_region->i_alpha = 0xff;
    p_region->p_next = NULL;
    p_region->p_private = NULL;
    p_region->psz_text = NULL;
    p_region->p_style = NULL;
    p_region->p_picture = NULL;
916

917
    if( p_fmt->i_chroma == VLC_CODEC_TEXT )
Laurent Aimar's avatar
Laurent Aimar committed
918
        return p_region;
919

920
    p_region->p_picture = picture_NewFromFormat( p_fmt );
Laurent Aimar's avatar
Laurent Aimar committed
921 922
    if( !p_region->p_picture )
    {
923
        free( p_region->fmt.p_palette );
Laurent Aimar's avatar
Laurent Aimar committed
924 925 926
        free( p_region );
        return NULL;
    }
927

Laurent Aimar's avatar
Laurent Aimar committed
928
    return p_region;
929
}
Laurent Aimar's avatar
Laurent Aimar committed
930 931 932

/* */
void subpicture_region_Delete( subpicture_region_t *p_region )
933
{
Laurent Aimar's avatar
Laurent Aimar committed
934 935
    if( !p_region )
        return;
936

Laurent Aimar's avatar
Laurent Aimar committed
937 938
    if( p_region->p_private )
        SpuRegionPrivateDelete( p_region->p_private );
939

Laurent Aimar's avatar
Laurent Aimar committed
940 941
    if( p_region->p_picture )
        picture_Release( p_region->p_picture );
942

Laurent Aimar's avatar
Laurent Aimar committed
943 944 945 946
    free( p_region->fmt.p_palette );

    free( p_region->psz_text );
    free( p_region->psz_html );
basOS G's avatar
basOS G committed
947 948
    if( p_region->p_style )
        text_style_Delete( p_region->p_style );
Laurent Aimar's avatar
Laurent Aimar committed
949
    free( p_region );
950 951
}

Laurent Aimar's avatar
Laurent Aimar committed
952 953 954 955 956 957
/* */
void subpicture_region_ChainDelete( subpicture_region_t *p_head )
{
    while( p_head )
    {
        subpicture_region_t *p_next = p_head->p_next;
958

Laurent Aimar's avatar
Laurent Aimar committed
959
        subpicture_region_Delete( p_head );
960

Laurent Aimar's avatar
Laurent Aimar committed
961 962
        p_head = p_next;
    }
963 964 965 966
}



Laurent Aimar's avatar
Laurent Aimar committed
967 968 969 970
/*****************************************************************************
 * heap managment
 *****************************************************************************/
static void SpuHeapInit( spu_heap_t *p_heap )
971
{
Laurent Aimar's avatar
Laurent Aimar committed
972 973 974
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
    {
        spu_heap_entry_t *e = &p_heap->p_entry[i];
975

Laurent Aimar's avatar
Laurent Aimar committed
976 977 978 979
        e->p_subpicture = NULL;
        e->b_reject = false;
    }
}
980

Laurent Aimar's avatar
Laurent Aimar committed
981 982 983
static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
{
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
984
    {
Laurent Aimar's avatar
Laurent Aimar committed
985
        spu_heap_entry_t *e = &p_heap->p_entry[i];
986

Laurent Aimar's avatar
Laurent Aimar committed
987 988
        if( e->p_subpicture )
            continue;
989

Laurent Aimar's avatar
Laurent Aimar committed
990 991 992 993 994 995
        e->p_subpicture = p_subpic;
        e->b_reject = false;
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}
996

Laurent Aimar's avatar
Laurent Aimar committed
997 998 999
static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
{
    spu_heap_entry_t *e = &p_heap->p_entry[i_index];
1000

Laurent Aimar's avatar
Laurent Aimar committed
1001 1002
    if( e->p_subpicture )
        subpicture_Delete( e->p_subpicture );
1003

Laurent Aimar's avatar
Laurent Aimar committed
1004 1005
    e->p_subpicture = NULL;
}
1006

Laurent Aimar's avatar
Laurent Aimar committed
1007
static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
Laurent Aimar's avatar
Laurent Aimar committed
1008
{
Laurent Aimar's avatar
Laurent Aimar committed
1009
    for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
1010
    {
Laurent Aimar's avatar
Laurent Aimar committed
1011
        spu_heap_entry_t *e = &p_heap->p_entry[i];
Laurent Aimar's avatar
Laurent Aimar committed
1012

Laurent Aimar's avatar
Laurent Aimar committed
1013 1014 1015 1016 1017
        if( e->p_subpicture != p_subpic )
            continue;

        SpuHeapDeleteAt( p_heap,