dvbsub.c 46.1 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * dvbsub.c : DVB subtitles decoder thread
 *****************************************************************************
 * Copyright (C) 2003 ANEVIA
5
6
 * Copyright (C) 2003-2004 VideoLAN
 * $Id$
7
8
 *
 * Authors: Damien LUCAS <damien.lucas@anevia.com>
9
 *          Laurent Aimar <fenrir@via.ecp.fr>
10
 *          Gildas Bazin <gbazin@videolan.org>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/decoder.h>

33
34
#include "vlc_bits.h"

35
36
//#define DEBUG_DVBSUB 1

37
38
39
40
41
42
43
/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
static int  Open ( vlc_object_t *p_this );
static void Close( vlc_object_t *p_this );

vlc_module_begin();
bigben's avatar
bigben committed
44
    set_description( _("DVB subtitles decoder") );
45
46
47
48
    set_capability( "decoder", 50 );
    set_callbacks( Open, Close );
vlc_module_end();

49
50
51
52
53
54
55
56
57
/****************************************************************************
 * Local structures
 ****************************************************************************
 * Those structures refer closely to the ETSI 300 743 Object model
 ****************************************************************************/

/* Storage of a RLE entry */
typedef struct dvbsub_rle_s
{
58
59
60
    uint16_t                i_num;
    int                     i_color_code;
    int                     i_bpp;
61
62
63
64
    uint8_t                 y;
    uint8_t                 cr;
    uint8_t                 cb;
    uint8_t                 t;
65
66
    struct dvbsub_rle_s     *p_next;

67
68
69
70
} dvbsub_rle_t;

/* A subpicture image is a list of codes
 * We need to store the length of each line since nothing specify in
71
 * the standard that all lines should have the same length
72
73
74
 * WARNING: We assume here that a spu is less than 576 lines high */
typedef struct
{
75
76
77
78
    uint16_t                i_rows;
    uint16_t                i_cols[576];
    dvbsub_rle_t            *p_last;
    dvbsub_rle_t            *p_codes;
79

80
81
82
83
84
} dvbsub_image_t;

/* The object definition gives the position of the object in a region */
typedef struct dvbsub_objectdef_s
{
85
86
87
88
89
90
91
    uint16_t                  i_id;
    uint8_t                   i_type;
    uint8_t                   i_provider;
    uint16_t                  i_x;
    uint16_t                  i_y;
    uint8_t                   i_fg_pc;
    uint8_t                   i_bg_pc;
92

93
94
} dvbsub_objectdef_t;

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* An object is constituted of 2 images (for interleaving) */
typedef struct dvbsub_object_s
{
    uint16_t                i_id;
    uint8_t                 i_version_number;
    uint8_t                 i_coding_method;
    vlc_bool_t              b_non_modify_color;
    dvbsub_image_t         *topfield;
    dvbsub_image_t         *bottomfield;
    struct dvbsub_object_s *p_next;

} dvbsub_object_t;

/* The object definition gives the position of the object in a region */
typedef struct dvbsub_regiondef_s
{
    uint16_t                  i_id;
    uint16_t                  i_x;
    uint16_t                  i_y;

} dvbsub_regiondef_t;

117
/* The Region is an aera on the image
118
 * with a list of the object definitions associated and a CLUT */
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
typedef struct dvbsub_region_s
{
    uint8_t                 i_id;
    uint8_t                 i_version_number;
    vlc_bool_t              b_fill;
    uint16_t                i_x;
    uint16_t                i_y;
    uint16_t                i_width;
    uint16_t                i_height;
    uint8_t                 i_level_comp;
    uint8_t                 i_depth;
    uint8_t                 i_clut;
    uint8_t                 i_8bp_code;
    uint8_t                 i_4bp_code;
    uint8_t                 i_2bp_code;
134
135
136
137
138

    int                     i_object_defs;
    dvbsub_objectdef_t      *p_object_defs;

    struct dvbsub_region_s  *p_next;
139

140
141
142
143
144
145
146
147
148
} dvbsub_region_t;

/* The page defines the list of regions */
typedef struct
{
    uint16_t              i_id;
    uint8_t               i_timeout;
    uint8_t               i_state;
    uint8_t               i_version_number;
149

150
151
    uint8_t               i_region_defs;
    dvbsub_regiondef_t    *p_region_defs;
152

153
} dvbsub_page_t;
154
155
156
157
158
159
160
161

/* The entry in the palette CLUT */
typedef struct
{
    uint8_t                 Y;
    uint8_t                 Cr;
    uint8_t                 Cb;
    uint8_t                 T;
162

163
164
165
166
167
168
169
} dvbsub_color_t;

/* */
typedef struct
{
    uint8_t                 i_id;
    uint8_t                 i_version_number;
170
171
172
    dvbsub_color_t          c_2b[4];
    dvbsub_color_t          c_4b[16];
    dvbsub_color_t          c_8b[256];
173

174
175
} dvbsub_clut_t;

176
typedef struct dvbsub_render_s
177
{
178
179
180
181
    uint16_t               i_x;
    uint16_t               i_y;
    dvbsub_image_t         *p_rle_top;
    dvbsub_image_t         *p_rle_bot;
182

183
    struct dvbsub_render_s *p_next;
184

185
} dvbsub_render_t;
186

187
188
struct subpicture_sys_t
{
189
    dvbsub_render_t         *p_objects;  /* Linked list of objects to render */
190
};
191
192
193

struct decoder_sys_t
{
194
195
196
197
198
199
200
201
202
    vout_thread_t   *p_vout;

    bs_t            bs;

    /* Decoder internal data */
    int i_id;
    int i_ancillary_id;

    mtime_t i_pts;
203

204
205
206
    dvbsub_page_t   *p_page;
    dvbsub_region_t *p_regions;
    dvbsub_object_t *p_objects;
207

208
209
210
211
212
    dvbsub_clut_t   *p_clut[256];
    dvbsub_clut_t   default_clut;

    subpicture_t    *p_spu;
    int             i_subpic_channel;
213
214
215
};


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// List of different SEGMENT TYPES
// According to EN 300-743, table 2
#define DVBSUB_ST_PAGE_COMPOSITION      0x10
#define DVBSUB_ST_REGION_COMPOSITION    0x11
#define DVBSUB_ST_CLUT_DEFINITION       0x12
#define DVBSUB_ST_OBJECT_DATA           0x13
#define DVBSUB_ST_ENDOFDISPLAY          0x80
#define DVBSUB_ST_STUFFING              0xff
// List of different OBJECT TYPES
// According to EN 300-743, table 6
#define DVBSUB_OT_BASIC_BITMAP          0x00
#define DVBSUB_OT_BASIC_CHAR            0x01
#define DVBSUB_OT_COMPOSITE_STRING      0x02
// Pixel DATA TYPES
// According to EN 300-743, table 9
#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
238
239
240
241
// List of different Page Composition Segment state
// According to EN 300-743, 7.2.1 table 3
#define DVBSUB_PCS_STATE_ACQUISITION    0x01
#define DVBSUB_PCS_STATE_CHANGE         0x10
242
243
244
245

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
246
247
248
249
250
251
252
static void Decode( decoder_t *, block_t ** );

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_clut( decoder_t *, bs_t * );
253

254
255
static void free_objects( decoder_t * );
static void free_all( decoder_t * );
256

257
258
static void render( decoder_t *, vout_thread_t * );
static void default_clut_init( decoder_t * );
259
260

/*****************************************************************************
261
 * Open: probe the decoder and return score
262
263
264
265
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to chose.
 *****************************************************************************/
266
static int Open( vlc_object_t *p_this )
267
{
268
    decoder_t     *p_dec = (decoder_t *) p_this;
269
    decoder_sys_t *p_sys;
270
    int i;
271

gbazin's avatar
   
gbazin committed
272
    if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
273
274
275
    {
        return VLC_EGENERIC;
    }
276

277
    p_dec->pf_decode_sub = Decode;
278
    p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
279

280
281
282
283
284
285
286
287
288
    p_sys->i_pts          = 0;
    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;
    p_sys->p_page         = NULL;
    p_sys->p_regions      = NULL;
    p_sys->p_objects      = NULL;
    p_sys->p_vout         = NULL;
    p_sys->p_spu          = NULL;
    for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
289
290
291

    es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );

292
293
    default_clut_init( p_dec );

294
295
    return VLC_SUCCESS;
}
296
297
298
299
300
301
302
303
304
305
306

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

    if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
    {
307
        subpicture_t *p_subpic;
308
309
310
311
312
        int i_subpic;
        for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
        {
            p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
            if( p_subpic != NULL &&
313
314
                ( p_subpic->i_status == RESERVED_SUBPICTURE ||
                  p_subpic->i_status == READY_SUBPICTURE ) )
315
316
317
318
319
320
            {
                vout_DestroySubPicture( p_sys->p_vout, p_subpic );
            }
        }
    }

321
    free_all( p_dec );
322
    free( p_sys );
323
324
}

325
/*****************************************************************************
326
 * Decode:
327
 *****************************************************************************/
328
static void Decode( decoder_t *p_dec, block_t **pp_block )
329
{
330
331
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t       *p_block;
332
    vout_thread_t *p_last_vout;
333
334

    if( pp_block == NULL || *pp_block == NULL )
335
    {
336
        return;
337
    }
338
339
340
    p_block = *pp_block;
    *pp_block = NULL;

341
342
    p_sys->i_pts = p_block->i_pts;
    if( p_sys->i_pts <= 0 )
343
    {
344
345
346
347
348
        msg_Warn( p_dec, "non dated subtitle" );
        block_Release( p_block );
        return;
    }

349
350
351
    bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );

    if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
352
    {
353
354
355
356
        msg_Dbg( p_dec, "invalid data identifier" );
        block_Release( p_block );
        return;
    }
357

358
359
360
361
362
363
    if( bs_read( &p_sys->bs, 8 ) != 0x20 && 0 ) /* Subtitle stream id */
    {
        msg_Dbg( p_dec, "invalid subtitle stream id" );
        block_Release( p_block );
        return;
    }
364

365
366
367
368
    while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
    {
        decode_segment( p_dec, &p_sys->bs );
    }
369

370
371
372
373
374
375
    if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
    {
        msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
        block_Release( p_block );
        return;
    }
376

377
378
379
380
    p_last_vout = p_sys->p_vout;
    if( ( p_sys->p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT,
                                           FIND_ANYWHERE ) ) )
    {
381
382
        if( p_last_vout != p_sys->p_vout )
        {
383
            p_sys->i_subpic_channel =
384
385
386
                vout_RegisterOSDChannel( p_sys->p_vout );
        }

387
        /* Check if the page is to be displayed */
388
        if( p_sys->p_page ) render( p_dec, p_sys->p_vout );
389
390

        vlc_object_release( p_sys->p_vout );
391
    }
392
393
394
#ifdef DEBUG_DVBSUB
    else if( p_sys->p_page ) render( p_dec, NULL );
#endif
395
396

    block_Release( p_block );
397
}
398

399
/* following functions are local */
400

401
/*****************************************************************************
402
 * default_clut_init: default clut as defined in EN 300-743 section 10
403
 *****************************************************************************/
404
static void default_clut_init( decoder_t *p_dec )
405
{
406
407
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t i;
408

409
410
411
#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;
412

413
414
415
416
    /* 4 entries CLUT */
    for( i = 0; i < 4; i++ )
    {
        uint8_t R = 0, G = 0, B = 0, T = 0;
417

418
419
420
421
        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;
422

423
424
425
426
        p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
        p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
        p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
        p_sys->default_clut.c_2b[i].T = T;
427
    }
428
429

    /* 16 entries CLUT */
430
431
    for( i = 0; i < 16; i++ )
    {
432
        uint8_t R = 0, G = 0, B = 0, T = 0;
433

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        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;
        }
453

454
455
456
457
458
        p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
        p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
        p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
        p_sys->default_clut.c_4b[i].T = T;
    }
459

460
461
462
    /* 256 entries CLUT (TODO) */
    memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
}
463

464
static void decode_segment( decoder_t *p_dec, bs_t *s )
465
{
466
    decoder_sys_t *p_sys = p_dec->p_sys;
467
468
469
470
    int i_type;
    int i_page_id;
    int i_size;

471
    /* sync_byte (already checked) */
472
473
474
475
476
477
478
479
480
481
482
    bs_skip( s, 8 );

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

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

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

483
    if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
484
    {
485
486
487
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "subtitle skipped (page id: %i)", i_page_id );
#endif
488
        bs_skip( s,  8 * ( 2 + i_size ) );
489
490
        return;
    }
491

492
493
494
495
496
497
498
#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

499
    switch( i_type )
500
    {
501
    case DVBSUB_ST_PAGE_COMPOSITION:
502
#ifdef DEBUG_DVBSUB
503
        msg_Dbg( p_dec, "decode_page_composition" );
504
#endif
505
        decode_page_composition( p_dec, s );
506
        break;
507
508

    case DVBSUB_ST_REGION_COMPOSITION:
509
#ifdef DEBUG_DVBSUB
510
        msg_Dbg( p_dec, "decode_region_composition" );
511
#endif
512
        decode_region_composition( p_dec, s );
513
        break;
514
515

    case DVBSUB_ST_CLUT_DEFINITION:
516
#ifdef DEBUG_DVBSUB
517
        msg_Dbg( p_dec, "decode_clut" );
518
#endif
519
        decode_clut( p_dec, s );
520
        break;
521

522
523
    case DVBSUB_ST_OBJECT_DATA:
#ifdef DEBUG_DVBSUB
524
        msg_Dbg( p_dec, "decode_object" );
525
#endif
526
        decode_object( p_dec, s );
527
        break;
528

529
530
    case DVBSUB_ST_ENDOFDISPLAY:
#ifdef DEBUG_DVBSUB
531
        msg_Dbg( p_dec, "end of display" );
532
#endif
533
        bs_skip( s,  8 * ( 2 + i_size ) );
534
        break;
535

536
    case DVBSUB_ST_STUFFING:
537
538
539
540
541
542
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "skip stuffing" );
#endif
        bs_skip( s,  8 * ( 2 + i_size ) );
        break;

543
544
545
546
    default:
        msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
        bs_skip( s,  8 * ( 2 + i_size ) );
        break;
547
548
549
    }
}

550
static void decode_clut( decoder_t *p_dec, bs_t *s )
551
{
552
553
554
555
556
557
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint16_t      i_segment_length;
    uint16_t      i_processed_length;
    dvbsub_clut_t *p_clut;
    uint8_t       i_clut_id;
    uint8_t       i_version_number;
558
559
560
561
562

    i_segment_length = bs_read( s, 16 );
    i_clut_id        = bs_read( s, 8 );
    i_version_number = bs_read( s, 4 );

563
564
565
566
    /* Check that this id doesn't not already exist with the same version
     * number and allocate memory if necessary */
    if( p_sys->p_clut[i_clut_id] != NULL &&
        p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
567
    {
568
569
570
        /* Nothing to do */
        bs_skip( s,  8 * i_segment_length - 12 );
        return;
571
    }
572
573

    if( !p_sys->p_clut[i_clut_id] )
574
    {
575
        p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
576
    }
577
578

    p_clut = p_sys->p_clut[i_clut_id];
579

580
    /* We don't have this version of the CLUT: Parse it */
581
    p_clut->i_version_number = i_version_number;
582
583
584
    bs_skip( s, 4 ); /* Reserved bits */
    i_processed_length = 2;
    while( i_processed_length < i_segment_length )
585
    {
586
587
588
589
590
591
592
593
594
595
        uint8_t y, cb, cr, t;
        uint8_t i_id;
        uint8_t i_type;

        i_id = bs_read( s, 8 );
        i_type = bs_read( s, 3 );

        bs_skip( s, 4 );

        if( bs_read( s, 1 ) )
596
        {
597
598
599
600
601
            y  = bs_read( s, 8 );
            cr = bs_read( s, 8 );
            cb = bs_read( s, 8 );
            t  = bs_read( s, 8 );
            i_processed_length += 6;
602
603
604
        }
        else
        {
605
606
607
608
609
610
611
612
            y  = bs_read( s, 6 );
            cr = bs_read( s, 4 );
            cb = bs_read( s, 4 );
            t  = bs_read( s, 2 );
            i_processed_length += 4;
        }

        /* According to EN 300-743 section 7.2.3 note 1, type should
613
614
         * not have more than 1 bit set to one, but some strams don't
         * respect this note. */
615
616
617

        if( i_type&0x04)
        {
618
619
620
621
            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;
622
623
624
        }
        if( i_type&0x02)
        {
625
626
627
628
            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;
629
630
631
        }
        if( i_type & 0x01)
        {
632
633
634
635
            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;
636
        }
637
638
639
    }
}

640
static void decode_page_composition( decoder_t *p_dec, bs_t *s )
641
{
642
    decoder_sys_t *p_sys = p_dec->p_sys;
643
644
645
646
647
648
    unsigned int i_version_number;
    unsigned int i_state;
    unsigned int i_segment_length;
    uint8_t i_timeout;
    unsigned int i;

649
    /* A page is composed by one or more region */
650

651
    i_segment_length = bs_read( s, 16 );
652
653
654
    i_timeout = bs_read( s, 8 );
    i_version_number = bs_read( s, 4 );
    i_state = bs_read( s, 2 );
655
656
    bs_skip( s, 2 ); /* Reserved */

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    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 );
    }
    else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
    {
        /* Not a full PCS, we need to wait for one */
        return;
    }

    if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
    {
        /* Make sure we clean up regularly our objects list.
         * Is it the best place to do this ? */
        free_objects( p_dec );
    }
677

678
679
680
681
682
683
684
685
686
687
688
689
690
    /* Check version number */
    if( p_sys->p_page &&
        p_sys->p_page->i_version_number == i_version_number )
    {
        bs_skip( s,  8 * (i_segment_length - 2) );
        return;
    }
    else if( p_sys->p_page )
    {
        if( p_sys->p_page->i_region_defs )
            free( p_sys->p_page->p_region_defs );
        p_sys->p_page->i_region_defs = 0;
    }
691

692
    if( !p_sys->p_page )
693
    {
694
695
        /* Allocate a new page */
        p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
696
697
    }

698
699
700
701
702
703
704
705
706
707
708
    p_sys->p_page->i_version_number = i_version_number;
    p_sys->p_page->i_timeout = i_timeout;

    /* 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 =
        malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
    for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
709
    {
710
        p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
711
        bs_skip( s, 8 ); /* Reserved */
712
713
714
715
716
717
718
719
        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 );

#ifdef DEBUG_DVBSUB
        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 );
#endif
720
721
722
    }
}

723
static void decode_region_composition( decoder_t *p_dec, bs_t *s )
724
{
725
726
727
728
729
730
    decoder_sys_t *p_sys = p_dec->p_sys;
    dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
    int i_segment_length;
    int i_processed_length;
    int i_region_id;
    int i_version_number;
731
732
733

    i_segment_length = bs_read( s, 16 );
    i_region_id = bs_read( s, 8 );
734
735
736
737
738
    i_version_number = bs_read( s, 4 );

    /* Check if we already have this region */
    for( p_region = p_sys->p_regions; p_region != NULL;
         p_region = p_region->p_next )
739
    {
740
741
        pp_region = &p_region->p_next;
        if( p_region->i_id == i_region_id ) break;
742
743
    }

744
745
746
    /* Check version number */
    if( p_region &&
        p_region->i_version_number == i_version_number )
747
    {
748
        bs_skip( s,  8 * (i_segment_length - 1) - 4 );
749
        return;
750
    }
751
752
753
754
755
    else if( p_region )
    {
        if( p_region->i_object_defs )
            free( p_region->p_object_defs );
    }
756

757
    if( !p_region )
758
    {
759
760
761
762
763
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "new region: %i", i_region_id );
#endif
        p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
        p_region->p_next = NULL;
764
    }
765
766

    /* Region attributes */
767
768
    p_region->i_id = i_region_id;
    p_region->i_version_number = i_version_number;
769
770
771
772
773
774
775
776
777
778
779
780
    p_region->b_fill           = bs_read( s, 1 );
    bs_skip( s, 3 ); /* Reserved */
    p_region->i_width          = bs_read( s, 16 );
    p_region->i_height         = bs_read( s, 16 );
    p_region->i_level_comp     = bs_read( s, 3 );
    p_region->i_depth          = bs_read( s, 3 );
    bs_skip( s, 2 ); /* Reserved */
    p_region->i_clut           = bs_read( s, 8 );
    p_region->i_8bp_code       = bs_read( s, 8 );
    p_region->i_4bp_code       = bs_read( s, 4 );
    p_region->i_2bp_code       = bs_read( s, 2 );
    bs_skip( s, 2 ); /* Reserved */
781
782
    p_region->p_object_defs    = NULL;
    p_region->i_object_defs    = 0;
783

784
    /* List of objects in the region */
785
786
    i_processed_length = 10;
    while( i_processed_length < i_segment_length )
787
    {
788
789
        dvbsub_objectdef_t *p_obj;

790
        /* We create a new object */
791
792
793
794
        p_region->i_object_defs++;
        p_region->p_object_defs =
            realloc( p_region->p_object_defs,
                     sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
795
796

        /* We parse object properties */
797
        p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
798
799
800
        p_obj->i_id         = bs_read( s, 16 );
        p_obj->i_type       = bs_read( s, 2 );
        p_obj->i_provider   = bs_read( s, 2 );
801
        p_obj->i_x          = bs_read( s, 12 );
802
        bs_skip( s, 4 ); /* Reserved */
803
        p_obj->i_y          = bs_read( s, 12 );
804
805
806
807
808
809
810
811
812
813

        i_processed_length += 6;

        if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
            p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
        {
            p_obj->i_fg_pc =  bs_read( s, 8 );
            p_obj->i_bg_pc =  bs_read( s, 8 );
            i_processed_length += 2;
        }
814
815
816
    }
}

817
818
819
820
static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t * );
static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t * );
static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t * );
821

822
static void decode_object( decoder_t *p_dec, bs_t *s )
823
{
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
    decoder_sys_t   *p_sys = p_dec->p_sys;
    dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
    int i_segment_length;
    int i_version_number;
    int i_coding_method;
    int i_obj_id;

    i_segment_length   = bs_read( s, 16 );
    i_obj_id           = bs_read( s, 16 );
    i_version_number   = bs_read( s, 4 );
    i_coding_method    = bs_read( s, 2 );

    if( i_coding_method )
    {
        /* TODO: DVB subtitling as characters */
        msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
        bs_skip( s,  8 * (i_segment_length - 2) - 6 );
        return;
    }
843

844
845
846
847
848
849
    /* Check if we already have this region */
    for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
    {
        pp_obj = &p_obj->p_next;
        if( p_obj->i_id == i_obj_id ) break;
    }
850

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
    /* Check version number */
    if( p_obj && p_obj->i_version_number == i_version_number )
    {
        bs_skip( s,  8 * (i_segment_length - 2) - 6 );
        return;
    }
    else if( p_obj )
    {
        /* Clean structure */
    }

    if( !p_obj )
    {
#ifdef DEBUG_DVBSUB
        msg_Dbg( p_dec, "new object: %i", i_obj_id );
#endif
        p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
        p_obj->p_next = NULL;
    }
870

871
872
873
874
    p_obj->i_id               = i_obj_id;
    p_obj->i_version_number   = i_version_number;
    p_obj->i_coding_method    = i_coding_method;
    p_obj->b_non_modify_color = bs_read( s, 1 );
875
876
877
    bs_skip( s, 1 ); /* Reserved */

    if( p_obj->i_coding_method == 0x00 )
878
    {
879
880
        uint16_t i_topfield_length;
        uint16_t i_bottomfield_length;
881

882
883
        i_topfield_length    = bs_read( s, 16 );
        i_bottomfield_length = bs_read( s, 16 );
884

885
        p_obj->topfield =
886
            dvbsub_parse_pdata( p_dec, s, i_topfield_length );
887
        p_obj->bottomfield =
888
            dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
889
890
891
    }
    else
    {
892
        /* TODO: DVB subtitling as characters */
893
    }
894
}
895

896
static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
897
                                           uint16_t length )
898
899
{
    dvbsub_image_t* p_image;
900
901
902
    uint16_t i_processed_length = 0;
    uint16_t i_lines = 0;
    uint16_t i_cols_last = 0;
903

904
905
    p_image = malloc( sizeof(dvbsub_image_t) );
    p_image->p_last = NULL;
906

907
    memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
908

909
    /* Let's parse it a first time to determine the size of the buffer */
910
    while( i_processed_length < length)
911
    {
912
913
        i_processed_length++;

914
        switch( bs_read( s, 8 ) )
915
916
        {
            case 0x10:
917
918
                i_processed_length +=
                    dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines], p_image );
919
920
                break;
            case 0x11:
921
                i_processed_length +=
922
                    dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines], p_image );
923
924
                break;
            case 0x12:
925
926
                i_processed_length +=
                    dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines], p_image );
927
928
929
930
                break;
            case 0x20:
            case 0x21:
            case 0x22:
931
                /* We don't use map tables */
932
933
                break;
            case 0xf0:
934
                i_lines++; /* End of line code */
935
936
937
                break;
        }
    }
938

939
940
    p_image->i_rows =  i_lines;
    p_image->i_cols[i_lines] = i_cols_last;
941
942
943
944
945
946
947

    /* Check word-aligned bits */
    if( bs_show( s, 8 ) == 0x00 )
    {
        bs_skip( s, 8 );
    }

948
949
950
    return p_image;
}

951
952
static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
                          int i_bpp )
953
{
954
    if( p->p_last != NULL )
955
    {
956
        p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
957
        p->p_last = p->p_last->p_next;
958
    }
959
960
    else
    {
961
        p->p_codes = malloc( sizeof(dvbsub_rle_t) );
962
963
964
        p->p_last = p->p_codes;
    }
    p->p_last->i_num = num;
965

966
    p->p_last->i_color_code = color;
967
    p->p_last->i_bpp = i_bpp;
968
969
970
    p->p_last->p_next = NULL;
}

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
                                  dvbsub_image_t* p_image )
{
    uint16_t i_processed = 0;
    vlc_bool_t b_stop = 0;
    uint16_t i_count = 0;
    uint8_t i_color = 0;

    while( !b_stop )
    {
        i_processed += 2;
        if( (i_color = bs_read( s, 2 )) != 0x00 )
        {
            (*p)++;

            /* Add 1 pixel */
            add_rle_code( p_image, 1, i_color, 2 );
        }
        else
        {
            i_processed++;
            if( bs_read( s, 1 ) == 0x00 )         // Switch1
            {
                i_count = 3 + bs_read( s, 3 );
                (*p) += i_count ;
                i_color = bs_read( s, 2 );
                add_rle_code( p_image, i_count, i_color, 2 );
                i_processed += 5;
            }
            else
            {
                i_processed++;
                if( bs_read( s, 1 ) == 0x00 )     //Switch2
                {
                    i_processed += 2;
                    switch( bs_read( s, 2 ) )     //Switch3
                    {
                    case 0x00:
                        b_stop=1;
                        break;
                    case 0x01:
                        add_rle_code( p_image, 2, 0, 2 );
                        break;
                    case 0x02:
                        i_count =  12 + bs_read( s, 4 );
                        i_color = bs_read( s, 2 );
                        (*p) += i_count;
                        i_processed += 6;
                        add_rle_code( p_image, i_count, i_color, 2 );
                        break;
                    case 0x03:
                        i_count =  29 + bs_read( s, 8 );
                        i_color = bs_read( s, 2 );
                        (*p) += i_count;
                        i_processed += 10;
                        add_rle_code( p_image, i_count, i_color, 2 );
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }

    bs_align( s );

    return ( i_processed + 7 ) / 8 ;
}

static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
1042
                                  dvbsub_image_t* p_image )
1043
{
1044
1045
    uint16_t i_processed = 0;
    vlc_bool_t b_stop = 0;
1046
    uint16_t i_count = 0;
1047
    uint8_t i_color = 0;
1048

1049
    while( !b_stop )
1050
    {
1051
        if( (i_color = bs_read( s, 4 )) != 0x00 )
1052
1053
1054
        {
            (*p)++;
            i_processed+=4;
1055

1056
1057
            /* Add 1 pixel */
            add_rle_code( p_image, 1, i_color, 4 );
1058
1059
1060
        }
        else
        {
1061
            if( bs_read( s, 1 ) == 0x00 )           // Switch1
1062
            {
1063
                if( bs_show( s, 3 ) != 0x00 )
1064
                {
1065
                    i_count = 2 + bs_read( s, 3 );
1066
                    (*p) += i_count ;
1067
                    add_rle_code( p_image, i_count, 0x00, 4 );
1068
1069
1070
                }
                else
                {
1071
                    bs_skip( s, 3 );
1072
1073
1074
1075
1076
1077
                    b_stop=1;
                }
                i_processed += 8;
            }
            else
            {
1078
                if( bs_read( s, 1 ) == 0x00)        //Switch2
1079
                {
1080
1081
                    i_count =  4 + bs_read( s, 2 );
                    i_color = bs_read( s, 4 );
1082
1083
                    (*p) += i_count;
                    i_processed += 12;
1084
                    add_rle_code( p_image, i_count, i_color, 4 );
1085
1086
1087
                }
                else
                {
1088
                    switch ( bs_read( s, 2 ) )     //Switch3
1089
1090
1091
1092
                    {
                        case 0x0:
                            (*p)++;
                            i_processed += 8;
1093
                            add_rle_code( p_image, 1, 0x00, 4 );
1094
1095
1096
1097
                            break;
                        case 0x1:
                            (*p)+=2;
                            i_processed += 8;
1098
                            add_rle_code( p_image, 2, 0x00, 4 );
1099
1100
                            break;
                        case 0x2:
1101
1102
                             i_count = 9 + bs_read( s, 4 );
                             i_color = bs_read( s, 4 );
1103
1104
                             (*p)+= i_count;
                             i_processed += 16;
1105
                             add_rle_code( p_image, i_count, i_color, 4 );
1106
1107
                             break;
                        case 0x3: