parse.c 22.4 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * parse.c: SPU parser
 *****************************************************************************
 * Copyright (C) 2000-2001 VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
Laurent Aimar's avatar
Laurent Aimar committed
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
9 10 11 12 13
 *
 * 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.
Laurent Aimar's avatar
Laurent Aimar committed
14
 *
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * 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>

#include "spudec.h"

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
37 38
static int  ParseControlSeq  ( decoder_t *, subpicture_t * );
static int  ParseRLE         ( decoder_t *, subpicture_t * );
39 40 41 42 43 44

static void DestroySPU       ( subpicture_t * );

static void UpdateSPU        ( subpicture_t *, vlc_object_t * );
static int  CropCallback     ( vlc_object_t *, char const *,
                               vlc_value_t, vlc_value_t, void * );
45 46 47 48 49

/*****************************************************************************
 * AddNibble: read a nibble from a source packet and add it to our integer.
 *****************************************************************************/
static inline unsigned int AddNibble( unsigned int i_code,
50
                                      uint8_t *p_src, int *pi_index )
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
    if( *pi_index & 0x1 )
    {
        return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
    }
    else
    {
        return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
    }
}

/*****************************************************************************
 * ParsePacket: parse an SPU packet and send it to the video output
 *****************************************************************************
 * This function parses the SPU packet and, if valid, sends it to the
 * video output.
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
68
void E_(ParsePacket)( decoder_t *p_dec)
69
{
Laurent Aimar's avatar
Laurent Aimar committed
70
    decoder_sys_t *p_sys = p_dec->p_sys;
71

Laurent Aimar's avatar
Laurent Aimar committed
72
    subpicture_t  *p_spu;
73 74

    /* Allocate the subpicture internal data. */
75 76
    p_spu = vout_CreateSubPicture( p_sys->p_vout, SUBT1_CHAN, TEXT_CONTENT,
                                   MEMORY_SUBPICTURE );
77 78 79 80 81
    if( p_spu == NULL )
    {
        return;
    }

82 83 84 85
    /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
     * expand the RLE stuff so that we won't need to read nibbles later
     * on. This will speed things up a lot. Plus, we'll only need to do
     * this stupid interlacing stuff once. */
Laurent Aimar's avatar
Laurent Aimar committed
86
    p_spu->p_sys = malloc( sizeof( subpicture_sys_t ) + 4*p_sys->i_rle_size );
87 88

    /* Fill the p_spu structure */
Laurent Aimar's avatar
Laurent Aimar committed
89
    vlc_mutex_init( p_dec, &p_spu->p_sys->lock );
90

91
    p_spu->pf_render = E_(RenderSPU);
92 93
    p_spu->pf_destroy = DestroySPU;
    p_spu->p_sys->p_data = (uint8_t*)p_spu->p_sys + sizeof( subpicture_sys_t );
94 95 96 97 98 99
    p_spu->p_sys->b_palette = VLC_FALSE;

    p_spu->p_sys->pi_alpha[0] = 0x00;
    p_spu->p_sys->pi_alpha[1] = 0x0f;
    p_spu->p_sys->pi_alpha[2] = 0x0f;
    p_spu->p_sys->pi_alpha[3] = 0x0f;
100

101 102
    p_spu->p_sys->b_crop = VLC_FALSE;

103
    /* Get display time now. If we do it later, we may miss the PTS. */
Laurent Aimar's avatar
Laurent Aimar committed
104
    p_spu->p_sys->i_pts = p_sys->i_pts;
105

106
    /* Attach to our input thread */
Laurent Aimar's avatar
Laurent Aimar committed
107
    p_spu->p_sys->p_input = vlc_object_find( p_dec,
108 109 110 111 112 113 114 115 116 117 118
                                             VLC_OBJECT_INPUT, FIND_PARENT );
    if( p_spu->p_sys->p_input )
    {
        vlc_value_t val;

        if( !var_Get( p_spu->p_sys->p_input, "highlight-mutex", &val ) )
        {
            vlc_mutex_t *p_mutex = val.p_address;

            vlc_mutex_lock( p_mutex );
            UpdateSPU( p_spu, VLC_OBJECT(p_spu->p_sys->p_input) );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
119

120 121 122 123 124 125
            var_AddCallback( p_spu->p_sys->p_input,
                             "highlight", CropCallback, p_spu );
            vlc_mutex_unlock( p_mutex );
        }
    }

126
    /* Getting the control part */
Laurent Aimar's avatar
Laurent Aimar committed
127
    if( ParseControlSeq( p_dec, p_spu ) )
128 129
    {
        /* There was a parse error, delete the subpicture */
Laurent Aimar's avatar
Laurent Aimar committed
130
        vout_DestroySubPicture( p_sys->p_vout, p_spu );
131 132 133
        return;
    }

Laurent Aimar's avatar
Laurent Aimar committed
134 135
     /* We try to display it */
    if( ParseRLE( p_dec, p_spu ) )
136 137
    {
        /* There was a parse error, delete the subpicture */
Laurent Aimar's avatar
Laurent Aimar committed
138
        vout_DestroySubPicture( p_sys->p_vout, p_spu );
139 140 141
        return;
    }

Laurent Aimar's avatar
Laurent Aimar committed
142 143
    msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
             p_sys->i_spu_size,
144 145 146
             p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );

    /* SPU is finished - we can ask the video output to display it */
Laurent Aimar's avatar
Laurent Aimar committed
147
    vout_DisplaySubPicture( p_sys->p_vout, p_spu );
148

149
    /* TODO: do stuff! */
150 151 152
}

/*****************************************************************************
153
 * ParseControlSeq: parse all SPU control sequences
154 155 156 157 158
 *****************************************************************************
 * This is the most important part in SPU decoding. We get dates, palette
 * information, coordinates, and so on. For more information on the
 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
159
static int ParseControlSeq( decoder_t *p_dec, subpicture_t * p_spu )
160
{
Laurent Aimar's avatar
Laurent Aimar committed
161 162
    decoder_sys_t *p_sys = p_dec->p_sys;

163
    /* Our current index in the SPU packet */
Laurent Aimar's avatar
Laurent Aimar committed
164
    unsigned int i_index = p_sys->i_rle_size + 4;
165 166

    /* The next start-of-control-sequence index and the previous one */
167
    unsigned int i_next_seq = 0, i_cur_seq = 0;
168

169
    /* Command and date */
170
    uint8_t i_command = SPU_CMD_END;
171
    mtime_t date = 0;
172

173
    unsigned int i, pi_alpha[4];
174 175 176

    /* Initialize the structure */
    p_spu->i_start = p_spu->i_stop = 0;
177
    p_spu->b_ephemer = VLC_FALSE;
178 179 180

    do
    {
Laurent Aimar's avatar
Laurent Aimar committed
181 182 183 184 185 186 187 188 189
        if( (int)i_index >= p_sys->i_spu_size + 1 )
        {
            /* sanity
             * XXX only on test by loop as p_sys->buffer is bigger than needed
             * to avoid checking at each access
             */
            break;
        }

190 191 192
        /* If we just read a command sequence, read the next one;
         * otherwise, go on with the commands of the current sequence. */
        if( i_command == SPU_CMD_END )
193
        {
194
            /* Get the control sequence date */
Laurent Aimar's avatar
Laurent Aimar committed
195 196
            date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
            /* FIXME How to access i_rate
197
                    * p_spudec->bit_stream.p_pes->i_rate / DEFAULT_RATE;
Laurent Aimar's avatar
Laurent Aimar committed
198 199
            */

200 201
            /* Next offset */
            i_cur_seq = i_index;
Laurent Aimar's avatar
Laurent Aimar committed
202 203
            i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );

204 205 206
            /* Skip what we just read */
            i_index += 4;
        }
Laurent Aimar's avatar
Laurent Aimar committed
207 208 209

        i_command = p_sys->buffer[i_index++];

210 211 212
        switch( i_command )
        {
        case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
213
            p_spu->i_start = p_spu->p_sys->i_pts + date;
214 215
            p_spu->b_ephemer = VLC_TRUE;
            break;
216

217 218
        /* Convert the dates in seconds to PTS values */
        case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
219
            p_spu->i_start = p_spu->p_sys->i_pts + date;
220 221 222
            break;

        case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
223
            p_spu->i_stop = p_spu->p_sys->i_pts + date;
224
            break;
225

226 227 228
        case SPU_CMD_SET_PALETTE:

            /* 03xxxx (palette) */
229
            if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
230
            {
Laurent Aimar's avatar
Laurent Aimar committed
231
                unsigned int idx[4];
232 233

                p_spu->p_sys->b_palette = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
234 235 236 237 238 239

                idx[0] = (p_sys->buffer[i_index+0]>>4)&0x0f;
                idx[1] = (p_sys->buffer[i_index+0])&0x0f;
                idx[2] = (p_sys->buffer[i_index+1]>>4)&0x0f;
                idx[3] = (p_sys->buffer[i_index+1])&0x0f;

240 241
                for( i = 0; i < 4 ; i++ )
                {
242
                    uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
243 244 245 246 247 248 249 250 251 252

                    /* FIXME: this job should be done sooner */
                    p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
                    p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
                    p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
                }
            }
            i_index += 2;

            break;
253

254
        case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
Laurent Aimar's avatar
Laurent Aimar committed
255 256 257 258
            pi_alpha[3] = (p_sys->buffer[i_index+0]>>4)&0x0f;
            pi_alpha[2] = (p_sys->buffer[i_index+0])&0x0f;
            pi_alpha[1] = (p_sys->buffer[i_index+1]>>4)&0x0f;
            pi_alpha[0] = (p_sys->buffer[i_index+1])&0x0f;
259 260 261 262

            /* Ignore blank alpha palette. Sometimes spurious blank
             * alpha palettes are present - dunno why. */
            if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
263
            {
264 265 266 267 268 269 270
                p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
                p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
                p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
                p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
            }
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
271
                msg_Warn( p_dec, "ignoring blank alpha palette" );
272 273
            }

274 275
            i_index += 2;
            break;
276

277
        case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
Laurent Aimar's avatar
Laurent Aimar committed
278 279 280 281
            p_spu->i_x = (p_sys->buffer[i_index+0]<<4)|
                         ((p_sys->buffer[i_index+1]>>4)&0x0f);
            p_spu->i_width = (((p_sys->buffer[i_index+1]&0x0f)<<8)|
                              p_sys->buffer[i_index+2]) - p_spu->i_x + 1;
282

Laurent Aimar's avatar
Laurent Aimar committed
283 284 285 286
            p_spu->i_y = (p_sys->buffer[i_index+3]<<4)|
                         ((p_sys->buffer[i_index+4]>>4)&0x0f);
            p_spu->i_height = (((p_sys->buffer[i_index+4]&0x0f)<<8)|
                              p_sys->buffer[i_index+5]) - p_spu->i_y + 1;
287
            
288 289 290 291
            i_index += 6;
            break;

        case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
Laurent Aimar's avatar
Laurent Aimar committed
292 293
            p_spu->p_sys->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+0]) - 4;
            p_spu->p_sys->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+2]) - 4;
294 295 296 297 298 299 300
            i_index += 4;
            break;

        case SPU_CMD_END: /* ff (end) */
            break;

        default: /* xx (unknown command) */
301
            msg_Warn( p_dec, "unknown command 0x%.2x", i_command );
302 303 304 305
            return VLC_EGENERIC;
        }

        /* We need to check for quit commands here */
Laurent Aimar's avatar
Laurent Aimar committed
306
        if( p_dec->b_die )
307 308 309 310 311
        {
            return VLC_EGENERIC;
        }

    } while( i_command != SPU_CMD_END || i_index == i_next_seq );
312 313 314 315

    /* Check that the next sequence index matches the current one */
    if( i_next_seq != i_cur_seq )
    {
Laurent Aimar's avatar
Laurent Aimar committed
316 317
        msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
                 i_next_seq, i_cur_seq );
318
        return VLC_EGENERIC;
319 320
    }

Laurent Aimar's avatar
Laurent Aimar committed
321
    if( (int)i_index > p_sys->i_spu_size )
322
    {
Laurent Aimar's avatar
Laurent Aimar committed
323 324
        msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
                 i_index, p_sys->i_spu_size );
325
        return VLC_EGENERIC;
326 327 328 329
    }

    if( !p_spu->i_start )
    {
Laurent Aimar's avatar
Laurent Aimar committed
330
        msg_Err( p_dec, "no `start display' command" );
331 332
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
333
    if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
334 335
    {
        /* This subtitle will live for 5 seconds or until the next subtitle */
Laurent Aimar's avatar
Laurent Aimar committed
336 337
        p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
        /* FIXME how to access i_rate ?
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
338
                        * p_spudec->bit_stream.p_pes->i_rate / DEFAULT_RATE;
Laurent Aimar's avatar
Laurent Aimar committed
339
        */
340
        p_spu->b_ephemer = VLC_TRUE;
341 342 343
    }

    /* Get rid of padding bytes */
Laurent Aimar's avatar
Laurent Aimar committed
344
    if( p_sys->i_spu_size > (int)i_index + 1 )
345
    {
Laurent Aimar's avatar
Laurent Aimar committed
346 347
        /* Zero or one padding byte, are quite usual
         * More than one padding byte - this is very strange, but
348
         * we can deal with it */
Laurent Aimar's avatar
Laurent Aimar committed
349 350
        msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
                  p_sys->i_spu_size - i_index );
351 352 353
    }

    /* Successfully parsed ! */
354
    return VLC_SUCCESS;
355 356 357 358 359 360 361 362 363
}

/*****************************************************************************
 * ParseRLE: parse the RLE part of the subtitle
 *****************************************************************************
 * This part parses the subtitle graphical data and stores it in a more
 * convenient structure for later decoding. For more information on the
 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
364
static int ParseRLE( decoder_t *p_dec, subpicture_t * p_spu )
365
{
Laurent Aimar's avatar
Laurent Aimar committed
366 367 368
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t       *p_src = &p_sys->buffer[4];

369 370 371 372 373 374
    unsigned int i_code;

    unsigned int i_width = p_spu->i_width;
    unsigned int i_height = p_spu->i_height;
    unsigned int i_x, i_y;

375
    uint16_t *p_dest = (uint16_t *)p_spu->p_sys->p_data;
376 377 378 379 380 381

    /* The subtitles are interlaced, we need two offsets */
    unsigned int  i_id = 0;                   /* Start on the even SPU layer */
    unsigned int  pi_table[ 2 ];
    unsigned int *pi_offset;

382
#if 0 /* cropping */
383 384
    vlc_bool_t b_empty_top = VLC_TRUE,
               b_empty_bottom = VLC_FALSE;
385 386
    unsigned int i_skipped_top = 0,
                 i_skipped_bottom = 0;
387
#endif
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

    /* Colormap statistics */
    int i_border = -1;
    int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;

    pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
    pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;

    for( i_y = 0 ; i_y < i_height ; i_y++ )
    {
        pi_offset = pi_table + i_id;

        for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
        {
            i_code = AddNibble( 0, p_src, pi_offset );

            if( i_code < 0x04 )
            {
                i_code = AddNibble( i_code, p_src, pi_offset );

                if( i_code < 0x10 )
                {
                    i_code = AddNibble( i_code, p_src, pi_offset );

                    if( i_code < 0x040 )
                    {
                        i_code = AddNibble( i_code, p_src, pi_offset );

                        if( i_code < 0x0100 )
                        {
                            /* If the 14 first bits are set to 0, then it's a
                             * new line. We emulate it. */
                            if( i_code < 0x0004 )
                            {
                                i_code |= ( i_width - i_x ) << 2;
                            }
                            else
                            {
                                /* We have a boo boo ! */
Laurent Aimar's avatar
Laurent Aimar committed
427
                                msg_Err( p_dec, "unknown RLE code "
428
                                         "0x%.4x", i_code );
429
                                return VLC_EGENERIC;
430 431 432 433 434 435 436 437
                            }
                        }
                    }
                }
            }

            if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
            {
Laurent Aimar's avatar
Laurent Aimar committed
438
                msg_Err( p_dec,
439 440
                         "out of bounds, %i at (%i,%i) is out of %ix%i",
                         i_code >> 2, i_x, i_y, i_width, i_height );
441
                return VLC_EGENERIC;
442 443 444 445 446 447 448 449 450
            }

            /* Try to find the border color */
            if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
            {
                i_border = i_code & 0x3;
                stats[i_border] += i_code >> 2;
            }

451
#if 0 /* cropping */
452 453 454 455 456 457 458 459 460 461 462 463 464 465
            if( (i_code >> 2) == i_width
                 && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
            {
                if( b_empty_top )
                {
                    /* This is a blank top line, we skip it */
                    i_skipped_top++;
                }
                else
                {
                    /* We can't be sure the current lines will be skipped,
                     * so we store the code just in case. */
                    *p_dest++ = i_code;

466
                    b_empty_bottom = VLC_TRUE;
467 468 469 470 471 472 473 474 475
                    i_skipped_bottom++;
                }
            }
            else
            {
                /* We got a valid code, store it */
                *p_dest++ = i_code;

                /* Valid code means no blank line */
476 477
                b_empty_top = VLC_FALSE;
                b_empty_bottom = VLC_FALSE;
478 479
                i_skipped_bottom = 0;
            }
480 481 482
#else
            *p_dest++ = i_code;
#endif
483 484 485 486 487
        }

        /* Check that we didn't go too far */
        if( i_x > i_width )
        {
Laurent Aimar's avatar
Laurent Aimar committed
488 489
            msg_Err( p_dec, "i_x overflowed, %i > %i",
                     i_x, i_width );
490
            return VLC_EGENERIC;
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        }

        /* Byte-align the stream */
        if( *pi_offset & 0x1 )
        {
            (*pi_offset)++;
        }

        /* Swap fields */
        i_id = ~i_id & 0x1;
    }

    /* We shouldn't get any padding bytes */
    if( i_y < i_height )
    {
Laurent Aimar's avatar
Laurent Aimar committed
506 507 508
        msg_Err( p_dec, "padding bytes found in RLE sequence" );
        msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
                        "want to help debugging this" );
509 510 511 512 513 514 515 516

        /* Skip them just in case */
        while( i_y < i_height )
        {
            *p_dest++ = i_width << 2;
            i_y++;
        }

517
        return VLC_EGENERIC;
518 519
    }

Laurent Aimar's avatar
Laurent Aimar committed
520
    msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
521 522
             p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );

523
#if 0 /* cropping */
524 525 526 527 528 529 530 531 532
    /* Crop if necessary */
    if( i_skipped_top || i_skipped_bottom )
    {
        p_spu->i_y += i_skipped_top;
        p_spu->i_height -= i_skipped_top + i_skipped_bottom;

        msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
                 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
    }
533
#endif
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

    /* Handle color if no palette was found */
    if( !p_spu->p_sys->b_palette )
    {
        int i, i_inner = -1, i_shade = -1;

        /* Set the border color */
        p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
        p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
        p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
        stats[i_border] = 0;

        /* Find the inner colors */
        for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
        {
            if( stats[i] )
            {
                i_inner = i;
            }
        }

        for(       ; i < 4 && i_shade == -1 ; i++ )
        {
            if( stats[i] )
            {
                if( stats[i] > stats[i_inner] )
                {
                    i_shade = i_inner;
                    i_inner = i;
                }
                else
                {
                    i_shade = i;
                }
            }
        }

        /* Set the inner color */
        if( i_inner != -1 )
        {
            p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
            p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
            p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
        }

        /* Set the anti-aliasing color */
        if( i_shade != -1 )
        {
            p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
            p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
            p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
        }

Laurent Aimar's avatar
Laurent Aimar committed
587
        msg_Dbg( p_dec,
588 589 590 591
                 "using custom palette (border %i, inner %i, shade %i)",
                 i_border, i_inner, i_shade );
    }

592
    return VLC_SUCCESS;
593 594
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
/*****************************************************************************
 * DestroySPU: subpicture destructor
 *****************************************************************************/
static void DestroySPU( subpicture_t *p_spu )
{
    if( p_spu->p_sys->p_input )
    {
        /* Detach from our input thread */
        var_DelCallback( p_spu->p_sys->p_input, "highlight",
                         CropCallback, p_spu );
        vlc_object_release( p_spu->p_sys->p_input );
    }

    vlc_mutex_destroy( &p_spu->p_sys->lock );
    free( p_spu->p_sys );
}

/*****************************************************************************
 * UpdateSPU: update subpicture settings
 *****************************************************************************
 * This function is called from CropCallback and at initialization time, to
 * retrieve crop information from the input.
 *****************************************************************************/
static void UpdateSPU( subpicture_t *p_spu, vlc_object_t *p_object )
{
    vlc_value_t val;

    if( var_Get( p_object, "highlight", &val ) )
    {
        return;
    }

    p_spu->p_sys->b_crop = val.b_bool;
    if( !p_spu->p_sys->b_crop )
    {
        return;
    }

    var_Get( p_object, "x-start", &val );
    p_spu->p_sys->i_x_start = val.i_int;
    var_Get( p_object, "y-start", &val );
    p_spu->p_sys->i_y_start = val.i_int;
    var_Get( p_object, "x-end", &val );
    p_spu->p_sys->i_x_end = val.i_int;
    var_Get( p_object, "y-end", &val );
    p_spu->p_sys->i_y_end = val.i_int;

#if 0
    if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
    {
        p_spu->p_sys->pi_color[0] = ((uint8_t *)val.p_address)[0];
        p_spu->p_sys->pi_color[1] = ((uint8_t *)val.p_address)[1];
        p_spu->p_sys->pi_color[2] = ((uint8_t *)val.p_address)[2];
        p_spu->p_sys->pi_color[3] = ((uint8_t *)val.p_address)[3];
    }
#endif

    if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
    {
        p_spu->p_sys->pi_alpha[0] = ((uint8_t *)val.p_address)[0];
        p_spu->p_sys->pi_alpha[1] = ((uint8_t *)val.p_address)[1];
        p_spu->p_sys->pi_alpha[2] = ((uint8_t *)val.p_address)[2];
        p_spu->p_sys->pi_alpha[3] = ((uint8_t *)val.p_address)[3];
    }
}

/*****************************************************************************
 * CropCallback: called when the highlight properties are changed
 *****************************************************************************
 * This callback is called from the input thread when we need cropping
 *****************************************************************************/
static int CropCallback( vlc_object_t *p_object, char const *psz_var,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    UpdateSPU( (subpicture_t *)p_data, p_object );

    return VLC_SUCCESS;
}