]> git.sesse.net Git - vlc/blob - modules/codec/spudec/parse.c
* ./configure.ac.in: removed -W in favour of -Wtraditional.
[vlc] / modules / codec / spudec / parse.c
1 /*****************************************************************************
2  * parse.c: SPU parser
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: parse.c,v 1.6 2002/12/06 16:34:05 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33
34 #include "spudec.h"
35
36 /*****************************************************************************
37  * Local prototypes.
38  *****************************************************************************/
39 static int  ParseControlSeq  ( spudec_thread_t *, subpicture_t * );
40 static int  ParseRLE         ( spudec_thread_t *, subpicture_t *, uint8_t * );
41
42 static void DestroySPU       ( subpicture_t * );
43
44 static void UpdateSPU        ( subpicture_t *, vlc_object_t * );
45 static int  CropCallback     ( vlc_object_t *, char const *,
46                                vlc_value_t, vlc_value_t, void * );
47
48 /*****************************************************************************
49  * AddNibble: read a nibble from a source packet and add it to our integer.
50  *****************************************************************************/
51 static inline unsigned int AddNibble( unsigned int i_code,
52                                       uint8_t *p_src, int *pi_index )
53 {
54     if( *pi_index & 0x1 )
55     {
56         return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
57     }
58     else
59     {
60         return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
61     }
62 }
63
64 /*****************************************************************************
65  * SyncPacket: get in sync with the stream
66  *****************************************************************************
67  * This function makes a few sanity checks and returns 0 if it looks like we
68  * are at the beginning of a subpicture packet.
69  *****************************************************************************/
70 int E_(SyncPacket)( spudec_thread_t *p_spudec )
71 {
72     /* Re-align the buffer on an 8-bit boundary */
73     RealignBits( &p_spudec->bit_stream );
74
75     /* The total SPU packet size, often bigger than a PS packet */
76     p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
77
78     /* The RLE stuff size (remove 4 because we just read 32 bits) */
79     p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
80
81     /* If the values we got are a bit strange, skip packet */
82     if( !p_spudec->i_spu_size
83          || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
84     {
85         return VLC_EGENERIC;
86     }
87
88     RemoveBits( &p_spudec->bit_stream, 16 );
89
90     return VLC_SUCCESS;
91 }
92
93 /*****************************************************************************
94  * ParsePacket: parse an SPU packet and send it to the video output
95  *****************************************************************************
96  * This function parses the SPU packet and, if valid, sends it to the
97  * video output.
98  *****************************************************************************/
99 void E_(ParsePacket)( spudec_thread_t *p_spudec )
100 {
101     subpicture_t * p_spu;
102     uint8_t      * p_src;
103     unsigned int   i_offset;
104     mtime_t        i_pts;
105
106     msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
107                                p_spudec->i_spu_size );
108
109     /* We cannot display a subpicture with no date */
110     NextPTS( &p_spudec->bit_stream, &i_pts, NULL );
111     if( i_pts == 0 )
112     {
113         msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
114         return;
115     }
116
117     /* Allocate the subpicture internal data. */
118     p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE );
119
120     if( p_spu == NULL )
121     {
122         return;
123     }
124
125     /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
126      * expand the RLE stuff so that we won't need to read nibbles later
127      * on. This will speed things up a lot. Plus, we'll only need to do
128      * this stupid interlacing stuff once. */
129     p_spu->p_sys = malloc( sizeof( subpicture_sys_t )
130                             + p_spudec->i_rle_size * 4 );
131
132     if( p_spu->p_sys == NULL )
133     {
134         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
135         return;
136     }
137
138     /* Fill the p_spu structure */
139     vlc_mutex_init( p_spudec->p_fifo, &p_spu->p_sys->lock );
140
141     p_spu->pf_render = E_(RenderSPU);
142     p_spu->pf_destroy = DestroySPU;
143     p_spu->p_sys->p_data = (uint8_t*)p_spu->p_sys + sizeof( subpicture_sys_t );
144     p_spu->p_sys->b_palette = VLC_FALSE;
145
146     p_spu->p_sys->pi_alpha[0] = 0x00;
147     p_spu->p_sys->pi_alpha[1] = 0x0f;
148     p_spu->p_sys->pi_alpha[2] = 0x0f;
149     p_spu->p_sys->pi_alpha[3] = 0x0f;
150
151     p_spu->p_sys->b_crop = VLC_FALSE;
152
153     /* Get display time now. If we do it later, we may miss the PTS. */
154     p_spu->p_sys->i_pts = i_pts;
155
156     /* Attach to our input thread */
157     p_spu->p_sys->p_input = vlc_object_find( p_spudec->p_fifo,
158                                              VLC_OBJECT_INPUT, FIND_PARENT );
159     if( p_spu->p_sys->p_input )
160     {
161         vlc_value_t val;
162
163         if( !var_Get( p_spu->p_sys->p_input, "highlight-mutex", &val ) )
164         {
165             vlc_mutex_t *p_mutex = val.p_address;
166
167             vlc_mutex_lock( p_mutex );
168             UpdateSPU( p_spu, VLC_OBJECT(p_spu->p_sys->p_input) );
169             var_AddCallback( p_spu->p_sys->p_input,
170                              "highlight", CropCallback, p_spu );
171             vlc_mutex_unlock( p_mutex );
172         }
173     }
174
175     /* Allocate the temporary buffer we will parse */
176     p_src = malloc( p_spudec->i_rle_size );
177
178     if( p_src == NULL )
179     {
180         msg_Err( p_spudec->p_fifo, "out of memory" );
181         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
182         return;
183     }
184
185     /* Get RLE data */
186     for( i_offset = 0; i_offset < p_spudec->i_rle_size;
187          i_offset += SPU_CHUNK_SIZE )
188     {
189         GetChunk( &p_spudec->bit_stream, p_src + i_offset,
190                   ( i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size ) ?
191                   SPU_CHUNK_SIZE : p_spudec->i_rle_size - i_offset );
192
193         /* Abort subtitle parsing if we were requested to stop */
194         if( p_spudec->p_fifo->b_die )
195         {
196             free( p_src );
197             vout_DestroySubPicture( p_spudec->p_vout, p_spu );
198             return;
199         }
200     }
201
202 #if 0
203     /* Dump the subtitle info */
204     intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
205 #endif
206
207     /* Getting the control part */
208     if( ParseControlSeq( p_spudec, p_spu ) )
209     {
210         /* There was a parse error, delete the subpicture */
211         free( p_src );
212         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
213         return;
214     }
215
216     /* At this point, no more GetBit() command is needed, so we have all
217      * the data we need to tell whether the subtitle is valid. Thus we
218      * try to display it and we ignore b_die. */
219
220     if( ParseRLE( p_spudec, p_spu, p_src ) )
221     {
222         /* There was a parse error, delete the subpicture */
223         free( p_src );
224         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
225         return;
226     }
227
228     msg_Dbg( p_spudec->p_fifo, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
229              p_spudec->i_spu_size,
230              p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
231
232     /* SPU is finished - we can ask the video output to display it */
233     vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
234
235     /* TODO: do stuff! */
236
237     /* Clean up */
238     free( p_src );
239 }
240
241 /*****************************************************************************
242  * ParseControlSeq: parse all SPU control sequences
243  *****************************************************************************
244  * This is the most important part in SPU decoding. We get dates, palette
245  * information, coordinates, and so on. For more information on the
246  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
247  *****************************************************************************/
248 static int ParseControlSeq( spudec_thread_t *p_spudec,
249                                   subpicture_t * p_spu )
250 {
251     /* Our current index in the SPU packet */
252     unsigned int i_index = p_spudec->i_rle_size + 4;
253
254     /* The next start-of-control-sequence index and the previous one */
255     unsigned int i_next_seq = 0, i_cur_seq = 0;
256
257     /* Command and date */
258     uint8_t i_command = SPU_CMD_END;
259     mtime_t date = 0;
260
261     unsigned int i, pi_alpha[4];
262
263     /* Initialize the structure */
264     p_spu->i_start = p_spu->i_stop = 0;
265     p_spu->b_ephemer = VLC_FALSE;
266
267     do
268     {
269         /* If we just read a command sequence, read the next one;
270          * otherwise, go on with the commands of the current sequence. */
271         if( i_command == SPU_CMD_END )
272         {
273             /* Get the control sequence date */
274             date = GetBits( &p_spudec->bit_stream, 16 );
275
276             /* Next offset */
277             i_cur_seq = i_index;
278             i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
279
280             /* Skip what we just read */
281             i_index += 4;
282         }
283
284         i_command = GetBits( &p_spudec->bit_stream, 8 );
285         i_index++;
286
287         switch( i_command )
288         {
289         case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
290             p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
291             p_spu->b_ephemer = VLC_TRUE;
292             break;
293
294         /* Convert the dates in seconds to PTS values */
295         case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
296             p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
297             break;
298
299         case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
300             p_spu->i_stop = p_spu->p_sys->i_pts + ( date * 11000 );
301             break;
302
303         case SPU_CMD_SET_PALETTE:
304
305             /* 03xxxx (palette) */
306             if( p_spudec->p_fifo->p_demux_data
307                  && *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
308             {
309                 uint32_t i_color;
310
311                 p_spu->p_sys->b_palette = VLC_TRUE;
312                 for( i = 0; i < 4 ; i++ )
313                 {
314                     i_color = ((uint32_t*)((char*)p_spudec->p_fifo->
315                                 p_demux_data + sizeof(int)))[
316                                   GetBits(&p_spudec->bit_stream, 4) ];
317
318                     /* FIXME: this job should be done sooner */
319 #ifndef WORDS_BIGENDIAN
320                     p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
321                     p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
322                     p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
323 #else
324                     p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>8) & 0xff;
325                     p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>24) & 0xff;
326                     p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>16) & 0xff;
327 #endif
328                 }
329             }
330             else
331             {
332                 RemoveBits( &p_spudec->bit_stream, 16 );
333             }
334             i_index += 2;
335
336             break;
337
338         case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
339             pi_alpha[3] = GetBits( &p_spudec->bit_stream, 4 );
340             pi_alpha[2] = GetBits( &p_spudec->bit_stream, 4 );
341             pi_alpha[1] = GetBits( &p_spudec->bit_stream, 4 );
342             pi_alpha[0] = GetBits( &p_spudec->bit_stream, 4 );
343
344             /* Ignore blank alpha palette. Sometimes spurious blank
345              * alpha palettes are present - dunno why. */
346             if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
347             {
348                 p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
349                 p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
350                 p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
351                 p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
352             }
353             else
354             {
355                 msg_Warn( p_spudec->p_fifo, "ignoring blank alpha palette" );
356             }
357
358             i_index += 2;
359             break;
360
361         case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
362             p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
363             p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
364                               - p_spu->i_x + 1;
365
366             p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
367             p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
368                                - p_spu->i_y + 1;
369
370             i_index += 6;
371             break;
372
373         case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
374             p_spu->p_sys->pi_offset[0] =
375                 GetBits( &p_spudec->bit_stream, 16 ) - 4;
376
377             p_spu->p_sys->pi_offset[1] =
378                 GetBits( &p_spudec->bit_stream, 16 ) - 4;
379
380             i_index += 4;
381             break;
382
383         case SPU_CMD_END: /* ff (end) */
384             break;
385
386         default: /* xx (unknown command) */
387             msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
388                                        i_command );
389             return VLC_EGENERIC;
390         }
391
392         /* We need to check for quit commands here */
393         if( p_spudec->p_fifo->b_die )
394         {
395             return VLC_EGENERIC;
396         }
397
398     } while( i_command != SPU_CMD_END || i_index == i_next_seq );
399
400     /* Check that the next sequence index matches the current one */
401     if( i_next_seq != i_cur_seq )
402     {
403         msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
404                                    i_next_seq, i_cur_seq );
405         return VLC_EGENERIC;
406     }
407
408     if( i_index > p_spudec->i_spu_size )
409     {
410         msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
411                                    i_index, p_spudec->i_spu_size );
412         return VLC_EGENERIC;
413     }
414
415     if( !p_spu->i_start )
416     {
417         msg_Err( p_spudec->p_fifo, "no `start display' command" );
418     }
419
420     if( !p_spu->i_stop && !p_spu->b_ephemer )
421     {
422         /* This subtitle will live for 5 seconds or until the next subtitle */
423         p_spu->i_stop = p_spu->i_start + 500 * 11000;
424         p_spu->b_ephemer = VLC_TRUE;
425     }
426
427     /* Get rid of padding bytes */
428     switch( p_spudec->i_spu_size - i_index )
429     {
430         /* Zero or one padding byte, quite usual */
431         case 1:
432             RemoveBits( &p_spudec->bit_stream, 8 );
433             i_index++;
434         case 0:
435             break;
436
437         /* More than one padding byte - this is very strange, but
438          * we can deal with it */
439         default:
440             msg_Warn( p_spudec->p_fifo,
441                       "%i padding bytes, we usually get 0 or 1 of them",
442                       p_spudec->i_spu_size - i_index );
443
444             while( i_index < p_spudec->i_spu_size )
445             {
446                 RemoveBits( &p_spudec->bit_stream, 8 );
447                 i_index++;
448             }
449
450             break;
451     }
452
453     /* Successfully parsed ! */
454     return VLC_SUCCESS;
455 }
456
457 /*****************************************************************************
458  * ParseRLE: parse the RLE part of the subtitle
459  *****************************************************************************
460  * This part parses the subtitle graphical data and stores it in a more
461  * convenient structure for later decoding. For more information on the
462  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
463  *****************************************************************************/
464 static int ParseRLE( spudec_thread_t *p_spudec,
465                      subpicture_t * p_spu, uint8_t * p_src )
466 {
467     unsigned int i_code;
468
469     unsigned int i_width = p_spu->i_width;
470     unsigned int i_height = p_spu->i_height;
471     unsigned int i_x, i_y;
472
473     uint16_t *p_dest = (uint16_t *)p_spu->p_sys->p_data;
474
475     /* The subtitles are interlaced, we need two offsets */
476     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
477     unsigned int  pi_table[ 2 ];
478     unsigned int *pi_offset;
479
480 #if 0 /* cropping */
481     vlc_bool_t b_empty_top = VLC_TRUE,
482                b_empty_bottom = VLC_FALSE;
483     unsigned int i_skipped_top = 0,
484                  i_skipped_bottom = 0;
485 #endif
486
487     /* Colormap statistics */
488     int i_border = -1;
489     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
490
491     pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
492     pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
493
494     for( i_y = 0 ; i_y < i_height ; i_y++ )
495     {
496         pi_offset = pi_table + i_id;
497
498         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
499         {
500             i_code = AddNibble( 0, p_src, pi_offset );
501
502             if( i_code < 0x04 )
503             {
504                 i_code = AddNibble( i_code, p_src, pi_offset );
505
506                 if( i_code < 0x10 )
507                 {
508                     i_code = AddNibble( i_code, p_src, pi_offset );
509
510                     if( i_code < 0x040 )
511                     {
512                         i_code = AddNibble( i_code, p_src, pi_offset );
513
514                         if( i_code < 0x0100 )
515                         {
516                             /* If the 14 first bits are set to 0, then it's a
517                              * new line. We emulate it. */
518                             if( i_code < 0x0004 )
519                             {
520                                 i_code |= ( i_width - i_x ) << 2;
521                             }
522                             else
523                             {
524                                 /* We have a boo boo ! */
525                                 msg_Err( p_spudec->p_fifo, "unknown RLE code "
526                                          "0x%.4x", i_code );
527                                 return VLC_EGENERIC;
528                             }
529                         }
530                     }
531                 }
532             }
533
534             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
535             {
536                 msg_Err( p_spudec->p_fifo,
537                          "out of bounds, %i at (%i,%i) is out of %ix%i",
538                          i_code >> 2, i_x, i_y, i_width, i_height );
539                 return VLC_EGENERIC;
540             }
541
542             /* Try to find the border color */
543             if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
544             {
545                 i_border = i_code & 0x3;
546                 stats[i_border] += i_code >> 2;
547             }
548
549 #if 0 /* cropping */
550             if( (i_code >> 2) == i_width
551                  && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
552             {
553                 if( b_empty_top )
554                 {
555                     /* This is a blank top line, we skip it */
556                     i_skipped_top++;
557                 }
558                 else
559                 {
560                     /* We can't be sure the current lines will be skipped,
561                      * so we store the code just in case. */
562                     *p_dest++ = i_code;
563
564                     b_empty_bottom = VLC_TRUE;
565                     i_skipped_bottom++;
566                 }
567             }
568             else
569             {
570                 /* We got a valid code, store it */
571                 *p_dest++ = i_code;
572
573                 /* Valid code means no blank line */
574                 b_empty_top = VLC_FALSE;
575                 b_empty_bottom = VLC_FALSE;
576                 i_skipped_bottom = 0;
577             }
578 #else
579             *p_dest++ = i_code;
580 #endif
581         }
582
583         /* Check that we didn't go too far */
584         if( i_x > i_width )
585         {
586             msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
587                                        i_x, i_width );
588             return VLC_EGENERIC;
589         }
590
591         /* Byte-align the stream */
592         if( *pi_offset & 0x1 )
593         {
594             (*pi_offset)++;
595         }
596
597         /* Swap fields */
598         i_id = ~i_id & 0x1;
599     }
600
601     /* We shouldn't get any padding bytes */
602     if( i_y < i_height )
603     {
604         msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
605         msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
606                                    "want to help debugging this" );
607
608         /* Skip them just in case */
609         while( i_y < i_height )
610         {
611             *p_dest++ = i_width << 2;
612             i_y++;
613         }
614
615         return VLC_EGENERIC;
616     }
617
618     msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
619              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
620
621 #if 0 /* cropping */
622     /* Crop if necessary */
623     if( i_skipped_top || i_skipped_bottom )
624     {
625         p_spu->i_y += i_skipped_top;
626         p_spu->i_height -= i_skipped_top + i_skipped_bottom;
627
628         msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
629                  p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
630     }
631 #endif
632
633     /* Handle color if no palette was found */
634     if( !p_spu->p_sys->b_palette )
635     {
636         int i, i_inner = -1, i_shade = -1;
637
638         /* Set the border color */
639         p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
640         p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
641         p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
642         stats[i_border] = 0;
643
644         /* Find the inner colors */
645         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
646         {
647             if( stats[i] )
648             {
649                 i_inner = i;
650             }
651         }
652
653         for(       ; i < 4 && i_shade == -1 ; i++ )
654         {
655             if( stats[i] )
656             {
657                 if( stats[i] > stats[i_inner] )
658                 {
659                     i_shade = i_inner;
660                     i_inner = i;
661                 }
662                 else
663                 {
664                     i_shade = i;
665                 }
666             }
667         }
668
669         /* Set the inner color */
670         if( i_inner != -1 )
671         {
672             p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
673             p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
674             p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
675         }
676
677         /* Set the anti-aliasing color */
678         if( i_shade != -1 )
679         {
680             p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
681             p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
682             p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
683         }
684
685         msg_Dbg( p_spudec->p_fifo,
686                  "using custom palette (border %i, inner %i, shade %i)",
687                  i_border, i_inner, i_shade );
688     }
689
690     return VLC_SUCCESS;
691 }
692
693 /*****************************************************************************
694  * DestroySPU: subpicture destructor
695  *****************************************************************************/
696 static void DestroySPU( subpicture_t *p_spu )
697 {
698     if( p_spu->p_sys->p_input )
699     {
700         /* Detach from our input thread */
701         var_DelCallback( p_spu->p_sys->p_input, "highlight",
702                          CropCallback, p_spu );
703         vlc_object_release( p_spu->p_sys->p_input );
704     }
705
706     vlc_mutex_destroy( &p_spu->p_sys->lock );
707     free( p_spu->p_sys );
708 }
709
710 /*****************************************************************************
711  * UpdateSPU: update subpicture settings
712  *****************************************************************************
713  * This function is called from CropCallback and at initialization time, to
714  * retrieve crop information from the input.
715  *****************************************************************************/
716 static void UpdateSPU( subpicture_t *p_spu, vlc_object_t *p_object )
717 {
718     vlc_value_t val;
719
720     if( var_Get( p_object, "highlight", &val ) )
721     {
722         return;
723     }
724
725     p_spu->p_sys->b_crop = val.b_bool;
726     if( !p_spu->p_sys->b_crop )
727     {
728         return;
729     }
730
731     var_Get( p_object, "x-start", &val );
732     p_spu->p_sys->i_x_start = val.i_int;
733     var_Get( p_object, "y-start", &val );
734     p_spu->p_sys->i_y_start = val.i_int;
735     var_Get( p_object, "x-end", &val );
736     p_spu->p_sys->i_x_end = val.i_int;
737     var_Get( p_object, "y-end", &val );
738     p_spu->p_sys->i_y_end = val.i_int;
739
740 #if 0
741     if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
742     {
743         p_spu->p_sys->pi_color[0] = ((uint8_t *)val.p_address)[0];
744         p_spu->p_sys->pi_color[1] = ((uint8_t *)val.p_address)[1];
745         p_spu->p_sys->pi_color[2] = ((uint8_t *)val.p_address)[2];
746         p_spu->p_sys->pi_color[3] = ((uint8_t *)val.p_address)[3];
747     }
748 #endif
749
750     if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
751     {
752         p_spu->p_sys->pi_alpha[0] = ((uint8_t *)val.p_address)[0];
753         p_spu->p_sys->pi_alpha[1] = ((uint8_t *)val.p_address)[1];
754         p_spu->p_sys->pi_alpha[2] = ((uint8_t *)val.p_address)[2];
755         p_spu->p_sys->pi_alpha[3] = ((uint8_t *)val.p_address)[3];
756     }
757 }
758
759 /*****************************************************************************
760  * CropCallback: called when the highlight properties are changed
761  *****************************************************************************
762  * This callback is called from the input thread when we need cropping
763  *****************************************************************************/
764 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
765                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
766 {
767     UpdateSPU( (subpicture_t *)p_data, p_object );
768
769     return VLC_SUCCESS;
770 }
771