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