dvbsub.c 82.7 KB
Newer Older
1
/*****************************************************************************
2 3
 * dvbsub.c : DVB subtitles decoder
 *            DVB subtitles encoder (developed for Anevia, www.anevia.com)
4 5
 *****************************************************************************
 * Copyright (C) 2003 ANEVIA
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
6
 * Copyright (C) 2003-2009 VLC authors and VideoLAN
7
 * $Id$
8
 *
Gildas Bazin's avatar
Gildas Bazin committed
9 10
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *          Damien LUCAS <damien.lucas@anevia.com>
11
 *          Laurent Aimar <fenrir@via.ecp.fr>
12
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
13
 *          Derk-Jan Hartman <hartman #at# videolan dot org>
14
 *          Simon Hailes <simon _a_ screen.subtitling.com>
15
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17 18
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
19 20 21 22
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
23 24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
25
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program; if not, write to the Free Software Foundation, Inc.,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
28
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29
 *****************************************************************************/
30

31 32
/*****************************************************************************
 * Preamble
33 34 35
 *
 * FIXME:
 * DVB subtitles coded as strings of characters are not handled correctly.
36
 * The character codes in the string should actually be indexes referring to a
37
 * character table identified in the subtitle descriptor.
38 39
 *
 * The spec is quite vague in this area, but what is meant is perhaps that it
40 41
 * refers to the character index in the codepage belonging to the language
 * specified in the subtitle descriptor. Potentially it's designed for widechar
42
 * (but not for UTF-*) codepages.
43
 *****************************************************************************
44 45
 *
 *****************************************************************************
46
 * Notes on DDS (Display Definition Segment)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * -----------------------------------------
 * DDS (Display Definition Segment) tells the decoder how the subtitle image
 * relates to the video image.
 * For SD, the subtitle image is always considered to be for display at
 * 720x576 (although it's assumed that for NTSC, this is 720x480, this
 * is not documented well) Also, for SD, the subtitle image is drawn 'on
 * the glass' (i.e. after video scaling, letterbox, etc.)
 * For 'HD' (subs marked type 0x14/0x24 in PSI), a DDS must be present,
 * and the subs area is drawn onto the video area (scales if necessary).
 * The DDS tells the decoder what resolution the subtitle images were
 * intended for, and hence how to scale the subtitle images for a
 * particular video size
 * i.e. if HD video is presented as letterbox, the subs will be in the
 * same place on the video as if the video was presented on an HD set
 * indeed, if the HD video was pillarboxed by the decoder, the subs may
 * be cut off as well as the video. The intent here is that the subs can
 * be placed accurately on the video - something which was missed in the
 * original spec.
65
 *
66 67 68 69
 * A DDS may also specify a window - this is where the subs images are moved so that the (0,0)
 * origin of decode is offset.
 ********************************************************************************************/

70 71 72 73
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

74
#include <vlc_common.h>
75
#include <vlc_plugin.h>
76 77
#include <vlc_codec.h>
#include <vlc_sout.h>
78

79
#include <vlc_bits.h>
80

81
/* #define DEBUG_DVBSUB 1 */
82

83 84
#define POSX_TEXT N_("Decoding X coordinate")
#define POSX_LONGTEXT N_("X coordinate of the rendered subtitle")
85

86
#define POSY_TEXT N_("Decoding Y coordinate")
87
#define POSY_LONGTEXT N_("Y coordinate of the rendered subtitle")
88 89 90 91 92

#define POS_TEXT N_("Subpicture position")
#define POS_LONGTEXT N_( \
  "You can enforce the subpicture position on the video " \
  "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
93
  "also use combinations of these values, e.g. 6=top-right).")
94

95 96 97 98
#define ENC_POSX_TEXT N_("Encoding X coordinate")
#define ENC_POSX_LONGTEXT N_("X coordinate of the encoded subtitle" )
#define ENC_POSY_TEXT N_("Encoding Y coordinate")
#define ENC_POSY_LONGTEXT N_("Y coordinate of the encoded subtitle" )
99

100 101
static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
static const char *const ppsz_pos_descriptions[] =
102 103
{ N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
  N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
104

105 106 107
/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
108 109 110 111
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
static subpicture_t *Decode( decoder_t *, block_t ** );

112
#ifdef ENABLE_SOUT
113 114 115
static int OpenEncoder  ( vlc_object_t * );
static void CloseEncoder( vlc_object_t * );
static block_t *Encode  ( encoder_t *, subpicture_t * );
116
#endif
117

118
vlc_module_begin ()
119
#   define DVBSUB_CFG_PREFIX "dvbsub-"
120
    set_description( N_("DVB subtitles decoder") )
121
    set_shortname( N_("DVB subtitles") )
122 123 124 125
    set_capability( "decoder", 50 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_SCODEC )
    set_callbacks( Open, Close )
126

127
    add_integer( DVBSUB_CFG_PREFIX "position", 8, POS_TEXT, POS_LONGTEXT, true )
128
        change_integer_list( pi_pos_values, ppsz_pos_descriptions )
129 130
    add_integer( DVBSUB_CFG_PREFIX "x", -1, POSX_TEXT, POSX_LONGTEXT, false )
    add_integer( DVBSUB_CFG_PREFIX "y", -1, POSY_TEXT, POSY_LONGTEXT, false )
131

132
#ifdef ENABLE_SOUT
133
#   define ENC_CFG_PREFIX "sout-dvbsub-"
134 135 136 137 138
    add_submodule ()
    set_description( N_("DVB subtitles encoder") )
    set_capability( "encoder", 100 )
    set_callbacks( OpenEncoder, CloseEncoder )

139 140
    add_integer( ENC_CFG_PREFIX "x", -1, ENC_POSX_TEXT, ENC_POSX_LONGTEXT, false )
    add_integer( ENC_CFG_PREFIX "y", -1, ENC_POSY_TEXT, ENC_POSY_LONGTEXT, false )
141
#endif
142
vlc_module_end ()
143

144
static const char *const ppsz_enc_options[] = { "x", "y", NULL };
145

146 147 148 149 150 151
/****************************************************************************
 * Local structures
 ****************************************************************************
 * Those structures refer closely to the ETSI 300 743 Object model
 ****************************************************************************/

152
/* The object definition gives the position of the object in a region [7.2.5] */
153 154
typedef struct dvbsub_objectdef_s
{
Gildas Bazin's avatar
Gildas Bazin committed
155 156 157 158 159 160
    int i_id;
    int i_type;
    int i_x;
    int i_y;
    int i_fg_pc;
    int i_bg_pc;
161
    char *psz_text; /* for string of characters objects */
162

163 164
} dvbsub_objectdef_t;

Gildas Bazin's avatar
Gildas Bazin committed
165 166
/* The entry in the palette CLUT */
typedef struct
167
{
Gildas Bazin's avatar
Gildas Bazin committed
168 169 170 171
    uint8_t                 Y;
    uint8_t                 Cr;
    uint8_t                 Cb;
    uint8_t                 T;
172

Gildas Bazin's avatar
Gildas Bazin committed
173
} dvbsub_color_t;
174

175 176 177 178 179 180 181 182 183
/* The displays dimensions [7.2.1] */
typedef struct dvbsub_display_s
{
    uint8_t                 i_id;
    uint8_t                 i_version;

    int                     i_width;
    int                     i_height;

184
    bool                    b_windowed;
185
    /* these values are only relevant if windowed */
186 187 188 189 190 191 192 193
    int                     i_x;
    int                     i_y;
    int                     i_max_x;
    int                     i_max_y;

} dvbsub_display_t;

/* [7.2.4] */
Gildas Bazin's avatar
Gildas Bazin committed
194
typedef struct dvbsub_clut_s
195
{
Gildas Bazin's avatar
Gildas Bazin committed
196 197 198 199 200
    uint8_t                 i_id;
    uint8_t                 i_version;
    dvbsub_color_t          c_2b[4];
    dvbsub_color_t          c_4b[16];
    dvbsub_color_t          c_8b[256];
201

Gildas Bazin's avatar
Gildas Bazin committed
202 203 204
    struct dvbsub_clut_s    *p_next;

} dvbsub_clut_t;
205

206
/* The Region is an aera on the image [7.2.3]
207
 * with a list of the object definitions associated and a CLUT */
208 209
typedef struct dvbsub_region_s
{
Gildas Bazin's avatar
Gildas Bazin committed
210 211 212 213 214 215 216 217 218
    int i_id;
    int i_version;
    int i_x;
    int i_y;
    int i_width;
    int i_height;
    int i_level_comp;
    int i_depth;
    int i_clut;
219

Gildas Bazin's avatar
Gildas Bazin committed
220
    uint8_t *p_pixbuf;
221

Gildas Bazin's avatar
Gildas Bazin committed
222 223
    int                    i_object_defs;
    dvbsub_objectdef_t     *p_object_defs;
224

Gildas Bazin's avatar
Gildas Bazin committed
225
    struct dvbsub_region_s *p_next;
226

Gildas Bazin's avatar
Gildas Bazin committed
227
} dvbsub_region_t;
228

Gildas Bazin's avatar
Gildas Bazin committed
229 230
/* The object definition gives the position of the object in a region */
typedef struct dvbsub_regiondef_s
231
{
Gildas Bazin's avatar
Gildas Bazin committed
232 233 234
    int i_id;
    int i_x;
    int i_y;
235

Gildas Bazin's avatar
Gildas Bazin committed
236
} dvbsub_regiondef_t;
237

238
/* The page defines the list of regions [7.2.2] */
239 240
typedef struct
{
Gildas Bazin's avatar
Gildas Bazin committed
241
    int i_id;
242
    int i_timeout; /* in seconds */
Gildas Bazin's avatar
Gildas Bazin committed
243 244
    int i_state;
    int i_version;
245

Gildas Bazin's avatar
Gildas Bazin committed
246 247 248 249
    int                i_region_defs;
    dvbsub_regiondef_t *p_region_defs;

} dvbsub_page_t;
250

251 252
struct decoder_sys_t
{
253
    bs_t               bs;
254 255

    /* Decoder internal data */
256 257 258 259 260 261 262 263 264 265 266 267 268
    int                i_id;
    int                i_ancillary_id;
    mtime_t            i_pts;

    bool               b_absolute;
    int                i_spu_position;
    int                i_spu_x;
    int                i_spu_y;

    bool               b_page;
    dvbsub_page_t      *p_page;
    dvbsub_region_t    *p_regions;
    dvbsub_clut_t      *p_cluts;
269
    /* this is very small, so keep forever */
270 271
    dvbsub_display_t   display;
    dvbsub_clut_t      default_clut;
272 273 274
};


275 276
/* List of different SEGMENT TYPES */
/* According to EN 300-743, table 2 */
277 278 279 280
#define DVBSUB_ST_PAGE_COMPOSITION      0x10
#define DVBSUB_ST_REGION_COMPOSITION    0x11
#define DVBSUB_ST_CLUT_DEFINITION       0x12
#define DVBSUB_ST_OBJECT_DATA           0x13
281
#define DVBSUB_ST_DISPLAY_DEFINITION    0x14
282 283
#define DVBSUB_ST_ENDOFDISPLAY          0x80
#define DVBSUB_ST_STUFFING              0xff
284 285
/* List of different OBJECT TYPES */
/* According to EN 300-743, table 6 */
286 287 288
#define DVBSUB_OT_BASIC_BITMAP          0x00
#define DVBSUB_OT_BASIC_CHAR            0x01
#define DVBSUB_OT_COMPOSITE_STRING      0x02
289
/* Pixel DATA TYPES */
290
/* According to EN 300-743, table 9 */
291 292 293 294 295 296 297
#define DVBSUB_DT_2BP_CODE_STRING       0x10
#define DVBSUB_DT_4BP_CODE_STRING       0x11
#define DVBSUB_DT_8BP_CODE_STRING       0x12
#define DVBSUB_DT_24_TABLE_DATA         0x20
#define DVBSUB_DT_28_TABLE_DATA         0x21
#define DVBSUB_DT_48_TABLE_DATA         0x22
#define DVBSUB_DT_END_LINE              0xf0
298 299
/* List of different Page Composition Segment state */
/* According to EN 300-743, 7.2.1 table 3 */
300
#define DVBSUB_PCS_STATE_ACQUISITION    0x01
301
#define DVBSUB_PCS_STATE_CHANGE         0x02
302 303 304 305

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
306 307 308 309 310 311
static void decode_segment( decoder_t *, bs_t * );
static void decode_page_composition( decoder_t *, bs_t * );
static void decode_region_composition( decoder_t *, bs_t * );
static void decode_object( decoder_t *, bs_t * );
static void decode_display_definition( decoder_t *, bs_t * );
static void decode_clut( decoder_t *, bs_t * );
312
static void free_all( decoder_t * );
313

314
static void default_clut_init( decoder_t * );
315
static void default_dds_init( decoder_t * );
316

Gildas Bazin's avatar
Gildas Bazin committed
317 318
static subpicture_t *render( decoder_t * );

319
/*****************************************************************************
320
 * Open: probe the decoder and return score
321 322 323 324
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to chose.
 *****************************************************************************/
325
static int Open( vlc_object_t *p_this )
326
{
327
    decoder_t     *p_dec = (decoder_t *) p_this;
328
    decoder_sys_t *p_sys;
329
    int i_posx, i_posy;
330

331
    if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
332 333 334
    {
        return VLC_EGENERIC;
    }
335

336
    p_dec->pf_decode_sub = Decode;
337
    p_sys = p_dec->p_sys = calloc( 1, sizeof(decoder_sys_t) );
338 339
    if( !p_sys )
        return VLC_ENOMEM;
340

341
    p_sys->i_pts          = VLC_TS_INVALID;
342 343
    p_sys->i_id           = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
    p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
344

345
    p_sys->p_regions      = NULL;
Gildas Bazin's avatar
Gildas Bazin committed
346 347
    p_sys->p_cluts        = NULL;
    p_sys->p_page         = NULL;
348 349 350

    /* configure for SD res in case DDS is not present */
    default_dds_init( p_dec );
351

352 353 354 355
    p_sys->i_spu_position = var_CreateGetInteger( p_this,
                                    DVBSUB_CFG_PREFIX "position" );
    i_posx = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "x" );
    i_posy = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "y" );
356

357
    /* Check if subpicture position was overridden */
358
    p_sys->b_absolute = true;
359 360
    p_sys->i_spu_x = p_sys->i_spu_y = 0;

361
    if( ( i_posx >= 0 ) && ( i_posy >= 0 ) )
362
    {
363
        p_sys->b_absolute = true;
364 365
        p_sys->i_spu_x = i_posx;
        p_sys->i_spu_y = i_posy;
366
    }
367

368 369
    p_dec->fmt_out.i_cat = SPU_ES;
    p_dec->fmt_out.i_codec = 0;
370

371 372
    default_clut_init( p_dec );

373 374
    return VLC_SUCCESS;
}
375 376 377 378 379 380 381 382 383

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*) p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

384 385
    var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
    var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
386
    var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
387

388
    free_all( p_dec );
389
    free( p_sys );
390 391
}

392
/*****************************************************************************
393
 * Decode:
394
 *****************************************************************************/
395
static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
396
{
397 398
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t       *p_block;
399
    subpicture_t  *p_spu = NULL;
400

401
    if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
402 403 404
    p_block = *pp_block;
    *pp_block = NULL;

405 406 407 408 409
    /* configure for SD res in case DDS is not present */
    /* a change of PTS is a good indication we must get a new DDS */
    if (p_sys->i_pts != p_block->i_pts)
        default_dds_init( p_dec );

410
    p_sys->i_pts = p_block->i_pts;
411
    if( p_sys->i_pts <= VLC_TS_INVALID )
412
    {
Gildas Bazin's avatar
Gildas Bazin committed
413 414 415
#ifdef DEBUG_DVBSUB
        /* Some DVB channels send stuffing segments in non-dated packets so
         * don't complain too loudly. */
416
        msg_Warn( p_dec, "non dated subtitle" );
Gildas Bazin's avatar
Gildas Bazin committed
417
#endif
418
        block_Release( p_block );
419
        return NULL;
420 421
    }

422
    bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
423

424
    if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
425
    {
426 427
        msg_Dbg( p_dec, "invalid data identifier" );
        block_Release( p_block );
428
        return NULL;
429
    }
430

431
    if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
432 433 434
    {
        msg_Dbg( p_dec, "invalid subtitle stream id" );
        block_Release( p_block );
435
        return NULL;
436
    }
437

Gildas Bazin's avatar
Gildas Bazin committed
438
#ifdef DEBUG_DVBSUB
439
    msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
Gildas Bazin's avatar
Gildas Bazin committed
440 441
#endif

442
    p_sys->b_page = false;
443
    while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
444 445 446
    {
        decode_segment( p_dec, &p_sys->bs );
    }
447

448
    if( ( bs_read( &p_sys->bs, 8 ) & 0x3f ) != 0x3f ) /* End marker */
449 450 451
    {
        msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
        block_Release( p_block );
452
        return NULL;
453
    }
454

455
    /* Check if the page is to be displayed */
456 457
    if( p_sys->p_page && p_sys->b_page )
        p_spu = render( p_dec );
458 459

    block_Release( p_block );
460 461

    return p_spu;
462
}
463

464
/* following functions are local */
465

466
/*****************************************************************************
467
 * default_clut_init: default clut as defined in EN 300-743 section 10
468
 *****************************************************************************/
469
static void default_clut_init( decoder_t *p_dec )
470
{
471 472
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t i;
473

474 475 476
#define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
#define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
#define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
477

478 479 480 481
    /* 4 entries CLUT */
    for( i = 0; i < 4; i++ )
    {
        uint8_t R = 0, G = 0, B = 0, T = 0;
482

483 484 485 486
        if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
        else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
        else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
        else R = G = B = 0x7F;
487

488 489
        p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
        p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
490
        p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
491
        p_sys->default_clut.c_2b[i].T = T;
492
    }
493 494

    /* 16 entries CLUT */
495 496
    for( i = 0; i < 16; i++ )
    {
497
        uint8_t R = 0, G = 0, B = 0, T = 0;
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
        if( !(i & 0x8) )
        {
            if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
            {
                T = 0xFF;
            }
            else
            {
                R = (i & 0x1) ? 0xFF : 0;
                G = (i & 0x2) ? 0xFF : 0;
                B = (i & 0x4) ? 0xFF : 0;
            }
        }
        else
        {
            R = (i & 0x1) ? 0x7F : 0;
            G = (i & 0x2) ? 0x7F : 0;
            B = (i & 0x4) ? 0x7F : 0;
        }
518

519
        p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
520 521
        p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
        p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
522 523
        p_sys->default_clut.c_4b[i].T = T;
    }
524

525
    /* 256 entries CLUT */
526 527
    memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
}
528

529
static void decode_segment( decoder_t *p_dec, bs_t *s )
530
{
531
    decoder_sys_t *p_sys = p_dec->p_sys;
532 533 534 535
    int i_type;
    int i_page_id;
    int i_size;

536
    /* sync_byte (already checked) */
537
    bs_skip( s, 8 );
538 539

    /* segment type */
540
    i_type = bs_read( s, 8 );
541 542

    /* page id */
543
    i_page_id = bs_read( s, 16 );
544 545

    /* segment size */
546
    i_size = bs_show( s, 16 );
547

548 549
    if( ( i_page_id != p_sys->i_id ) &&
        ( i_page_id != p_sys->i_ancillary_id ) )
550
    {
551
#ifdef DEBUG_DVBSUB
552 553
        msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
                 i_page_id, p_sys->i_id );
554
#endif
555
        bs_skip( s,  8 * ( 2 + i_size ) );
556 557
        return;
    }
558

559 560 561
    if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
        ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
        ( i_page_id == p_sys->i_ancillary_id ) )
562 563 564 565
    {
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
#endif
566
        bs_skip( s,  8 * ( 2 + i_size ) );
567 568 569
        return;
    }

570 571 572 573 574 575 576
#ifdef DEBUG_DVBSUB
    if( i_page_id == p_sys->i_id )
        msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
    else
        msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
#endif

577
    switch( i_type )
578
    {
579
    case DVBSUB_ST_PAGE_COMPOSITION:
580
#ifdef DEBUG_DVBSUB
581
        msg_Dbg( p_dec, "decode_page_composition" );
582
#endif
583
        decode_page_composition( p_dec, s );
584
        break;
585 586

    case DVBSUB_ST_REGION_COMPOSITION:
587
#ifdef DEBUG_DVBSUB
588
        msg_Dbg( p_dec, "decode_region_composition" );
589
#endif
590
        decode_region_composition( p_dec, s );
591
        break;
592 593

    case DVBSUB_ST_CLUT_DEFINITION:
594
#ifdef DEBUG_DVBSUB
595
        msg_Dbg( p_dec, "decode_clut" );
596
#endif
597
        decode_clut( p_dec, s );
598
        break;
599

600 601
    case DVBSUB_ST_OBJECT_DATA:
#ifdef DEBUG_DVBSUB
602
        msg_Dbg( p_dec, "decode_object" );
603
#endif
604
        decode_object( p_dec, s );
605
        break;
606

607 608 609 610 611 612 613
    case DVBSUB_ST_DISPLAY_DEFINITION:
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "decode_display_definition" );
#endif
        decode_display_definition( p_dec, s );
        break;

614 615
    case DVBSUB_ST_ENDOFDISPLAY:
#ifdef DEBUG_DVBSUB
616
        msg_Dbg( p_dec, "end of display" );
617
#endif
618
        bs_skip( s,  8 * ( 2 + i_size ) );
619
        break;
620

621
    case DVBSUB_ST_STUFFING:
622 623 624
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "skip stuffing" );
#endif
625
        bs_skip( s,  8 * ( 2 + i_size ) );
626 627
        break;

628 629
    default:
        msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
630
        bs_skip( s,  8 * ( 2 + i_size ) );
631
        break;
632 633 634
    }
}

635
static void decode_clut( decoder_t *p_dec, bs_t *s )
636
{
637 638 639
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint16_t      i_segment_length;
    uint16_t      i_processed_length;
640
    dvbsub_clut_t *p_clut, *p_next;
Gildas Bazin's avatar
Gildas Bazin committed
641
    int           i_id, i_version;
642

643 644 645
    i_segment_length = bs_read( s, 16 );
    i_id             = bs_read( s, 8 );
    i_version        = bs_read( s, 4 );
646

Gildas Bazin's avatar
Gildas Bazin committed
647 648 649 650 651 652 653
    /* Check if we already have this clut */
    for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
    {
        if( p_clut->i_id == i_id ) break;
    }

    /* Check version number */
654
    if( p_clut && ( p_clut->i_version == i_version ) )
655
    {
656
        /* Nothing to do */
657
        bs_skip( s, 8 * i_segment_length - 12 );
658
        return;
659
    }
660

Gildas Bazin's avatar
Gildas Bazin committed
661
    if( !p_clut )
662
    {
Gildas Bazin's avatar
Gildas Bazin committed
663 664 665
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "new clut: %i", i_id );
#endif
666 667 668
        p_clut = malloc( sizeof( dvbsub_clut_t ) );
        if( !p_clut )
            return;
Gildas Bazin's avatar
Gildas Bazin committed
669 670
        p_clut->p_next = p_sys->p_cluts;
        p_sys->p_cluts = p_clut;
671
    }
672

673
    /* Initialize to default clut */
674
    p_next = p_clut->p_next;
675
    *p_clut = p_sys->default_clut;
676
    p_clut->p_next = p_next;
677

678
    /* We don't have this version of the CLUT: Parse it */
Gildas Bazin's avatar
Gildas Bazin committed
679 680
    p_clut->i_version = i_version;
    p_clut->i_id = i_id;
681
    bs_skip( s, 4 ); /* Reserved bits */
682 683
    i_processed_length = 2;
    while( i_processed_length < i_segment_length )
684
    {
685 686 687 688
        uint8_t y, cb, cr, t;
        uint8_t i_id;
        uint8_t i_type;

689 690
        i_id = bs_read( s, 8 );
        i_type = bs_read( s, 3 );
691

692
        bs_skip( s, 4 );
693

694
        if( bs_read( s, 1 ) )
695
        {
696 697 698 699
            y  = bs_read( s, 8 );
            cr = bs_read( s, 8 );
            cb = bs_read( s, 8 );
            t  = bs_read( s, 8 );
700
            i_processed_length += 6;
701 702 703
        }
        else
        {
704 705 706 707
            y  = bs_read( s, 6 ) << 2;
            cr = bs_read( s, 4 ) << 4;
            cb = bs_read( s, 4 ) << 4;
            t  = bs_read( s, 2 ) << 6;
708 709 710
            i_processed_length += 4;
        }

Gildas Bazin's avatar
Gildas Bazin committed
711 712 713 714 715 716 717 718 719
        /* We are not entirely compliant here as full transparency is indicated
         * with a luma value of zero, not a transparency value of 0xff
         * (full transparency would actually be 0xff + 1). */
        if( y == 0 )
        {
            cr = cb = 0;
            t  = 0xff;
        }

720
        /* According to EN 300-743 section 7.2.3 note 1, type should
Gildas Bazin's avatar
Gildas Bazin committed
721
         * not have more than 1 bit set to one, but some streams don't
722
         * respect this note. */
723
        if( ( i_type & 0x04 ) && ( i_id < 4 ) )
724
        {
725 726 727 728
            p_clut->c_2b[i_id].Y = y;
            p_clut->c_2b[i_id].Cr = cr;
            p_clut->c_2b[i_id].Cb = cb;
            p_clut->c_2b[i_id].T = t;
729
        }
730
        if( ( i_type & 0x02 ) && ( i_id < 16 ) )
731
        {
732 733 734 735
            p_clut->c_4b[i_id].Y = y;
            p_clut->c_4b[i_id].Cr = cr;
            p_clut->c_4b[i_id].Cb = cb;
            p_clut->c_4b[i_id].T = t;
736
        }
737
        if( i_type & 0x01 )
738
        {
739 740 741 742
            p_clut->c_8b[i_id].Y = y;
            p_clut->c_8b[i_id].Cr = cr;
            p_clut->c_8b[i_id].Cb = cb;
            p_clut->c_8b[i_id].T = t;
743
        }
744 745 746
    }
}

747
static void decode_page_composition( decoder_t *p_dec, bs_t *s )
748
{
749
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
Gildas Bazin committed
750
    int i_version, i_state, i_segment_length, i_timeout, i;
751

Gildas Bazin's avatar
Gildas Bazin committed
752
    /* A page is composed by 0 or more region */
753 754 755 756 757
    i_segment_length = bs_read( s, 16 );
    i_timeout = bs_read( s, 8 );
    i_version = bs_read( s, 4 );
    i_state = bs_read( s, 2 );
    bs_skip( s, 2 ); /* Reserved */
758

759 760 761 762 763 764 765 766
    if( i_state == DVBSUB_PCS_STATE_CHANGE )
    {
        /* End of an epoch, reset decoder buffer */
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "page composition mode change" );
#endif
        free_all( p_dec );
    }
767 768
    else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
             ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
769 770
    {
        /* Not a full PCS, we need to wait for one */
Gildas Bazin's avatar
Gildas Bazin committed
771
        msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
772

773 774
#if 0
        /* Try to start decoding even without an acquisition page */
775
        bs_skip( s,  8 * (i_segment_length - 2) );
776
        return;
Gildas Bazin's avatar
Gildas Bazin committed
777
#endif
778 779
    }

Gildas Bazin's avatar
Gildas Bazin committed
780
#ifdef DEBUG_DVBSUB
Gildas Bazin's avatar
Gildas Bazin committed
781
    if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
Gildas Bazin's avatar
Gildas Bazin committed
782 783
        msg_Dbg( p_dec, "acquisition page composition" );
#endif
784

785
    /* Check version number */
786
    if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
787
    {
788
        bs_skip( s,  8 * (i_segment_length - 2) );
789 790 791 792 793 794
        return;
    }
    else if( p_sys->p_page )
    {
        if( p_sys->p_page->i_region_defs )
            free( p_sys->p_page->p_region_defs );
795
        p_sys->p_page->p_region_defs = NULL;
796 797
        p_sys->p_page->i_region_defs = 0;
    }
798

799
    if( !p_sys->p_page )
800
    {
Gildas Bazin's avatar
Gildas Bazin committed
801 802 803
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "new page" );
#endif
804 805
        /* Allocate a new page */
        p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
806 807
        if( !p_sys->p_page )
            return;
808 809
    }

Gildas Bazin's avatar
Gildas Bazin committed
810
    p_sys->p_page->i_version = i_version;
811
    p_sys->p_page->i_timeout = i_timeout;
812
    p_sys->b_page = true;
813 814 815 816 817 818 819

    /* Number of regions */
    p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;

    if( p_sys->p_page->i_region_defs == 0 ) return;

    p_sys->p_page->p_region_defs =
820
        malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_regiondef_t) );
821
    if( p_sys->p_page->p_region_defs )
822
    {
823 824
        for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
        {
825 826 827 828
            p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
            bs_skip( s, 8 ); /* Reserved */
            p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
            p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
829 830

#ifdef DEBUG_DVBSUB
831 832 833
            msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
                    i, p_sys->p_page->p_region_defs[i].i_x,
                    p_sys->p_page->p_region_defs[i].i_y );
834
#endif
835
        }
836 837 838
    }
}

839
static void decode_region_composition( decoder_t *p_dec, bs_t *s )
840
{
841 842
    decoder_sys_t *p_sys = p_dec->p_sys;
    dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
Gildas Bazin's avatar
Gildas Bazin committed
843 844 845
    int i_segment_length, i_processed_length, i_id, i_version;
    int i_width, i_height, i_level_comp, i_depth, i_clut;
    int i_8_bg, i_4_bg, i_2_bg;
846
    bool b_fill;
847

848 849 850
    i_segment_length = bs_read( s, 16 );
    i_id = bs_read( s, 8 );
    i_version = bs_read( s, 4 );
851 852 853 854

    /* Check if we already have this region */
    for( p_region = p_sys->p_regions; p_region != NULL;
         p_region = p_region->p_next )
855
    {
856
        pp_region = &p_region->p_next;
Gildas Bazin's avatar
Gildas Bazin committed
857
        if( p_region->i_id == i_id ) break;
858 859
    }

860
    /* Check version number */
861
    if( p_region && ( p_region->i_version == i_version ) )
862
    {
863
        bs_skip( s, 8 * (i_segment_length - 1) - 4 );
864
        return;
865
    }
866

867
    if( !p_region )
868
    {
869
#ifdef DEBUG_DVBSUB
Gildas Bazin's avatar
Gildas Bazin committed
870
        msg_Dbg( p_dec, "new region: %i", i_id );
871
#endif
872
        p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
873 874 875 876 877
        if( !p_region )
            return;
        p_region->p_object_defs = NULL;
        p_region->p_pixbuf = NULL;
        p_region->p_next = NULL;
878
    }
879 880

    /* Region attributes */
Gildas Bazin's avatar
Gildas Bazin committed
881 882
    p_region->i_id = i_id;
    p_region->i_version = i_version;
883
    b_fill = bs_read( s, 1 );
884
    bs_skip( s, 3 ); /* Reserved */
Gildas Bazin's avatar
Gildas Bazin committed
885

886 887
    i_width = bs_read( s, 16 );
    i_height = bs_read( s, 16 );
888 889 890
#ifdef DEBUG_DVBSUB
    msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
#endif
891 892 893 894
    i_level_comp = bs_read( s, 3 );
    i_depth = bs_read( s, 3 );
    bs_skip( s, 2 ); /* Reserved */
    i_clut = bs_read( s, 8 );
Gildas Bazin's avatar
Gildas Bazin committed
895

896 897 898 899
    i_8_bg = bs_read( s, 8 );
    i_4_bg = bs_read( s, 4 );
    i_2_bg = bs_read( s, 2 );
    bs_skip( s, 2 ); /* Reserved */
900 901 902

    /* Free old object defs */
    while( p_region->i_object_defs )
903
        free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
904

905
    free( p_region->p_object_defs );
906
    p_region->p_object_defs = NULL;
907

Gildas Bazin's avatar
Gildas Bazin committed
908
    /* Extra sanity checks */
909 910
    if( ( p_region->i_width != i_width ) ||
        ( p_region->i_height != i_height ) )
Gildas Bazin's avatar
Gildas Bazin committed
911 912 913
    {
        if( p_region->p_pixbuf )
        {
914 915
            msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
                     p_region->i_width, p_region->i_height, i_width, i_height );
Gildas Bazin's avatar
Gildas Bazin committed
916 917 918
            free( p_region->p_pixbuf );
        }

919
        p_region->p_pixbuf = xmalloc( i_height * i_width );
920
        p_region->i_depth = 0;
921
        b_fill = true;
Gildas Bazin's avatar
Gildas Bazin committed
922
    }
923 924 925 926
    if( p_region->i_depth &&
        ( ( p_region->i_depth != i_depth ) ||
          ( p_region->i_level_comp != i_level_comp ) ||
          ( p_region->i_clut != i_clut) ) )
Gildas Bazin's avatar
Gildas Bazin committed
927 928 929 930 931 932 933
    {
        msg_Dbg( p_dec, "region parameters changed (not allowed)" );
    }

    /* Erase background of region */
    if( b_fill )
    {
934 935
        int i_background = ( p_region->i_depth == 1 ) ? i_2_bg :
            ( ( p_region->i_depth == 2 ) ? i_4_bg : i_8_bg );
Gildas Bazin's avatar
Gildas Bazin committed
936 937 938 939 940 941 942 943 944
        memset( p_region->p_pixbuf, i_background, i_width * i_height );
    }

    p_region->i_width = i_width;
    p_region->i_height = i_height;
    p_region->i_level_comp = i_level_comp;
    p_region->i_depth = i_depth;
    p_region->i_clut = i_clut;

945
    /* List of objects in the region */
946 947
    i_processed_length = 10;
    while( i_processed_length < i_segment_length )
948
    {
949 950
        dvbsub_objectdef_t *p_obj;

951
        /* We create a new object */
952
        p_region->i_object_defs++;
953
        p_region->p_object_defs = xrealloc( p_region->p_object_defs,
954
                     sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
955 956

        /* We parse object properties */
957
        p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
958 959 960 961 962 963
        p_obj->i_id         = bs_read( s, 16 );
        p_obj->i_type       = bs_read( s, 2 );
        bs_skip( s, 2 ); /* Provider */
        p_obj->i_x          = bs_read( s, 12 );
        bs_skip( s, 4 ); /* Reserved */
        p_obj->i_y          = bs_read( s, 12 );
964
        p_obj->psz_text     = NULL;
965 966 967

        i_processed_length += 6;

968 969
        if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
            ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
970
        {
971 972
            p_obj->i_fg_pc =  bs_read( s, 8 );
            p_obj->i_bg_pc =  bs_read( s, 8 );
973 974
            i_processed_length += 2;
        }
975 976 977
    }
}

978
/* ETSI 300 743 [7.2.1] */
979
static void decode_display_definition( decoder_t *p_dec, bs_t *s )
980 981 982 983 984 985
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint16_t      i_segment_length;
    uint16_t      i_processed_length = 40;
    int           i_version;

986 987
    i_segment_length = bs_read( s, 16 );
    i_version        = bs_read( s, 4 );
988 989

    /* Check version number */
990
    if( p_sys->display.i_version == i_version )
991 992
    {
        /* The definition did not change */
993
        bs_skip( s, 8*i_segment_length - 4 );
994 995 996 997 998 999
        return;
    }

#ifdef DEBUG_DVBSUB
    msg_Dbg( p_dec, "new display definition: %i", i_version );
#endif
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009
    /* We don't have this version of the display definition: Parse it */
    p_sys->display.i_version = i_version;
    p_sys->display.b_windowed = bs_read( s, 1 );
    bs_skip( s, 3 ); /* Reserved bits */
    p_sys->display.i_width = bs_read( s, 16 )+1;
    p_sys->display.i_height = bs_read( s, 16 )+1;

    if( p_sys->display.b_windowed )
    {
1010
#ifdef DEBUG_DVBSUB
1011
        msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1012
#endif
1013 1014 1015 1016 1017 1018
        /* Coordinates are measured from the top left corner */
        p_sys->display.i_x     = bs_read( s, 16 );
        p_sys->display.i_max_x = bs_read( s, 16 );
        p_sys->display.i_y     = bs_read( s, 16 );
        p_sys->display.i_max_y = bs_read( s, 16 );
        i_processed_length += 64;
1019
    }
1020 1021 1022 1023 1024 1025 1026 1027 1028
    else
    {
        /* if not windowed, setup the window variables to good defaults */
        /* not necessary, but to avoid future confusion.. */
        p_sys->display.i_x     = 0;
        p_sys->display.i_max_x = p_sys->display.i_width-1;
        p_sys->display.i_y     = 0;
        p_sys->display.i_max_y = p_sys->display.i_height-1;
    }
1029 1030 1031

    if( i_processed_length != i_segment_length*8 )
    {
1032 1033
        msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
                 i_processed_length / 8 , i_segment_length );
1034 1035 1036
    }

#ifdef DEBUG_DVBSUB
1037
    msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1038 1039
             p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
    if( p_sys->display.b_windowed )
1040
        msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1041
                 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1042 1043 1044
#endif
}

Gildas Bazin's avatar
Gildas Bazin committed
1045 1046
static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
                                 uint8_t *, int );
1047 1048 1049
static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1050

1051
static void decode_object( decoder_t *p_dec, bs_t *s )
1052
{
Gildas Bazin's avatar
Gildas Bazin committed
1053 1054 1055
    decoder_sys_t *p_sys = p_dec->p_sys;
    dvbsub_region_t *p_region;
    int i_segment_length, i_coding_method, i_version, i_id, i;
1056
    bool b_non_modify_color;
Gildas Bazin's avatar
Gildas Bazin committed
1057

1058 1059 1060
    /* ETSI 300-743 paragraph 7.2.4
     * sync_byte, segment_type and page_id have already been processed.
     */
1061 1062 1063 1064
    i_segment_length = bs_read( s, 16 );
    i_id             = bs_read( s, 16 );
    i_version        = bs_read( s, 4 );
    i_coding_method  = bs_read( s, 2 );
1065

1066
    if( i_coding_method > 1 )
1067
    {
1068
        msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1069
        bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1070 1071
        return;
    }
1072