]> git.sesse.net Git - vlc/blob - modules/codec/spudec/parse.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / codec / spudec / parse.c
1 /*****************************************************************************
2  * parse.c: SPU parser
3  *****************************************************************************
4  * Copyright (C) 2000-2001, 2005, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_codec.h>
36 #include <vlc_input.h>
37
38 #include "spudec.h"
39
40 /*****************************************************************************
41  * Local prototypes.
42  *****************************************************************************/
43 typedef struct
44 {
45     int i_width;
46     int i_height;
47     int i_x;
48     int i_y;
49 } spu_properties_t;
50
51 typedef struct
52 {
53     int   pi_offset[2];                              /* byte offsets to data */
54     uint16_t *p_data;
55
56     /* Color information */
57     bool b_palette;
58     uint8_t    pi_alpha[4];
59     uint8_t    pi_yuv[4][3];
60
61     /* Auto crop fullscreen subtitles */
62     bool b_auto_crop;
63     int i_y_top_offset;
64     int i_y_bottom_offset;
65
66 } subpicture_data_t;
67
68 static int  ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *,
69                              spu_properties_t *, mtime_t i_pts );
70 static int  ParseRLE       ( decoder_t *, subpicture_data_t *,
71                              const spu_properties_t * );
72 static void Render         ( decoder_t *, subpicture_t *, subpicture_data_t *,
73                              const spu_properties_t * );
74
75 /*****************************************************************************
76  * AddNibble: read a nibble from a source packet and add it to our integer.
77  *****************************************************************************/
78 static inline unsigned int AddNibble( unsigned int i_code,
79                                       const uint8_t *p_src, unsigned int *pi_index )
80 {
81     if( *pi_index & 0x1 )
82     {
83         return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
84     }
85     else
86     {
87         return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
88     }
89 }
90
91 /*****************************************************************************
92  * ParsePacket: parse an SPU packet and send it to the video output
93  *****************************************************************************
94  * This function parses the SPU packet and, if valid, sends it to the
95  * video output.
96  *****************************************************************************/
97 subpicture_t * ParsePacket( decoder_t *p_dec )
98 {
99     decoder_sys_t *p_sys = p_dec->p_sys;
100     subpicture_t *p_spu;
101     subpicture_data_t spu_data;
102     spu_properties_t spu_properties;
103
104     /* Allocate the subpicture internal data. */
105     p_spu = decoder_NewSubpicture( p_dec );
106     if( !p_spu ) return NULL;
107
108     p_spu->i_original_picture_width =
109         p_dec->fmt_in.subs.spu.i_original_frame_width;
110     p_spu->i_original_picture_height =
111         p_dec->fmt_in.subs.spu.i_original_frame_height;
112
113     /* Getting the control part */
114     if( ParseControlSeq( p_dec, p_spu, &spu_data, &spu_properties, p_sys->i_pts ) )
115     {
116         /* There was a parse error, delete the subpicture */
117         decoder_DeleteSubpicture( p_dec, p_spu );
118         return NULL;
119     }
120
121     /* we are going to expand the RLE stuff so that we won't need to read
122      * nibbles later on. This will speed things up a lot. Plus, we'll only
123      * need to do this stupid interlacing stuff once.
124      *
125      * Rationale for the "p_spudec->i_rle_size * 4*sizeof(*spu_data.p_data)":
126      *  one byte gaves two nibbles and may be used twice (once per field)
127      * generating 4 codes.
128      */
129     spu_data.p_data = malloc( sizeof(*spu_data.p_data) * 2 * 2 * p_sys->i_rle_size );
130
131     /* We try to display it */
132     if( ParseRLE( p_dec, &spu_data, &spu_properties ) )
133     {
134         /* There was a parse error, delete the subpicture */
135         decoder_DeleteSubpicture( p_dec, p_spu );
136         free( spu_data.p_data );
137         return NULL;
138     }
139
140 #ifdef DEBUG_SPUDEC
141     msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
142              p_sys->i_spu_size,
143              spu_data.pi_offset[0], spu_data.pi_offset[1] );
144 #endif
145
146     Render( p_dec, p_spu, &spu_data, &spu_properties );
147     free( spu_data.p_data );
148
149     return p_spu;
150 }
151
152 /*****************************************************************************
153  * ParseControlSeq: parse all SPU control sequences
154  *****************************************************************************
155  * This is the most important part in SPU decoding. We get dates, palette
156  * information, coordinates, and so on. For more information on the
157  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
158  *****************************************************************************/
159 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
160                             subpicture_data_t *p_spu_data, spu_properties_t *p_spu_properties, mtime_t i_pts )
161 {
162     decoder_sys_t *p_sys = p_dec->p_sys;
163
164     /* Our current index in the SPU packet */
165     unsigned int i_index = p_sys->i_rle_size + 4;
166
167     /* The next start-of-control-sequence index and the previous one */
168     unsigned int i_next_seq = 0, i_cur_seq = 0;
169
170     /* Command and date */
171     uint8_t i_command = SPU_CMD_END;
172     mtime_t date = 0;
173     bool b_cmd_offset = false;
174     bool b_cmd_alpha = false;
175     subpicture_data_t spu_data_cmd;
176
177     if( !p_spu || !p_spu_data )
178         return VLC_EGENERIC;
179
180     /* Create working space for spu data */
181     memset( &spu_data_cmd, 0, sizeof(spu_data_cmd) );
182     spu_data_cmd.pi_offset[0] = -1;
183     spu_data_cmd.pi_offset[1] = -1;
184     spu_data_cmd.p_data = NULL;
185     spu_data_cmd.b_palette = false;
186     spu_data_cmd.b_auto_crop = false;
187     spu_data_cmd.i_y_top_offset = 0;
188     spu_data_cmd.i_y_bottom_offset = 0;
189     spu_data_cmd.pi_alpha[0] = 0x00;
190     spu_data_cmd.pi_alpha[1] = 0x0f;
191     spu_data_cmd.pi_alpha[2] = 0x0f;
192     spu_data_cmd.pi_alpha[3] = 0x0f;
193
194     /* Initialize the structure */
195     p_spu->i_start = p_spu->i_stop = 0;
196     p_spu->b_ephemer = false;
197
198     memset( p_spu_properties, 0, sizeof(*p_spu_properties) );
199
200     /* */
201     *p_spu_data = spu_data_cmd;
202
203     for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
204     {
205         /* If we just read a command sequence, read the next one;
206          * otherwise, go on with the commands of the current sequence. */
207         if( i_command == SPU_CMD_END )
208         {
209             if( i_index + 4 > p_sys->i_spu_size )
210             {
211                 msg_Err( p_dec, "overflow in SPU command sequence" );
212                 return VLC_EGENERIC;
213             }
214
215             /* */
216             b_cmd_offset = false;
217             b_cmd_alpha = false;
218             /* Get the control sequence date */
219             date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
220
221             /* Next offset */
222             i_cur_seq = i_index;
223             i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
224
225             if( i_next_seq > p_sys->i_spu_size )
226             {
227                 msg_Err( p_dec, "overflow in SPU next command sequence" );
228                 return VLC_EGENERIC;
229             }
230
231             /* Skip what we just read */
232             i_index += 4;
233         }
234
235         i_command = p_sys->buffer[i_index];
236
237         switch( i_command )
238         {
239         case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
240             p_spu->i_start = i_pts + date;
241             p_spu->b_ephemer = true;
242             i_index += 1;
243             break;
244
245         /* Convert the dates in seconds to PTS values */
246         case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
247             p_spu->i_start = i_pts + date;
248             i_index += 1;
249             break;
250
251         case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
252             p_spu->i_stop = i_pts + date;
253             i_index += 1;
254             break;
255
256         case SPU_CMD_SET_PALETTE:
257             /* 03xxxx (palette) */
258             if( i_index + 3 > p_sys->i_spu_size )
259             {
260                 msg_Err( p_dec, "overflow in SPU command" );
261                 return VLC_EGENERIC;
262             }
263
264             if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
265             {
266                 unsigned int idx[4];
267                 int i;
268
269                 spu_data_cmd.b_palette = true;
270
271                 idx[0] = (p_sys->buffer[i_index+1]>>4)&0x0f;
272                 idx[1] = (p_sys->buffer[i_index+1])&0x0f;
273                 idx[2] = (p_sys->buffer[i_index+2]>>4)&0x0f;
274                 idx[3] = (p_sys->buffer[i_index+2])&0x0f;
275
276                 for( i = 0; i < 4 ; i++ )
277                 {
278                     uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
279
280                     /* FIXME: this job should be done sooner */
281                     spu_data_cmd.pi_yuv[3-i][0] = (i_color>>16) & 0xff;
282                     spu_data_cmd.pi_yuv[3-i][1] = (i_color>>0) & 0xff;
283                     spu_data_cmd.pi_yuv[3-i][2] = (i_color>>8) & 0xff;
284                 }
285             }
286
287             i_index += 3;
288             break;
289
290         case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
291             if( i_index + 3 > p_sys->i_spu_size )
292             {
293                 msg_Err( p_dec, "overflow in SPU command" );
294                 return VLC_EGENERIC;
295             }
296
297             b_cmd_alpha = true;
298             spu_data_cmd.pi_alpha[3] = (p_sys->buffer[i_index+1]>>4)&0x0f;
299             spu_data_cmd.pi_alpha[2] = (p_sys->buffer[i_index+1])&0x0f;
300             spu_data_cmd.pi_alpha[1] = (p_sys->buffer[i_index+2]>>4)&0x0f;
301             spu_data_cmd.pi_alpha[0] = (p_sys->buffer[i_index+2])&0x0f;
302
303             i_index += 3;
304             break;
305
306         case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
307             if( i_index + 7 > p_sys->i_spu_size )
308             {
309                 msg_Err( p_dec, "overflow in SPU command" );
310                 return VLC_EGENERIC;
311             }
312
313             p_spu_properties->i_x = (p_sys->buffer[i_index+1]<<4)|
314                          ((p_sys->buffer[i_index+2]>>4)&0x0f);
315             p_spu_properties->i_width = (((p_sys->buffer[i_index+2]&0x0f)<<8)|
316                               p_sys->buffer[i_index+3]) - p_spu_properties->i_x + 1;
317
318             p_spu_properties->i_y = (p_sys->buffer[i_index+4]<<4)|
319                          ((p_sys->buffer[i_index+5]>>4)&0x0f);
320             p_spu_properties->i_height = (((p_sys->buffer[i_index+5]&0x0f)<<8)|
321                               p_sys->buffer[i_index+6]) - p_spu_properties->i_y + 1;
322
323             /* Auto crop fullscreen subtitles */
324             if( p_spu_properties->i_height > 250 )
325                 p_spu_data->b_auto_crop = true;
326
327             i_index += 7;
328             break;
329
330         case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
331             if( i_index + 5 > p_sys->i_spu_size )
332             {
333                 msg_Err( p_dec, "overflow in SPU command" );
334                 return VLC_EGENERIC;
335             }
336
337             b_cmd_offset = true;
338             p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+1]) - 4;
339             p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+3]) - 4;
340             i_index += 5;
341             break;
342
343         case SPU_CMD_END: /* ff (end) */
344             if( b_cmd_offset )
345             {
346                 /* It seems that palette and alpha from the block having
347                  * the cmd offset have to be used
348                  * XXX is it all ? */
349                 p_spu_data->b_palette = spu_data_cmd.b_palette;
350                 if( spu_data_cmd.b_palette )
351                     memcpy( p_spu_data->pi_yuv, spu_data_cmd.pi_yuv, sizeof(spu_data_cmd.pi_yuv) );
352                 if( b_cmd_alpha )
353                     memcpy( p_spu_data->pi_alpha, spu_data_cmd.pi_alpha, sizeof(spu_data_cmd.pi_alpha) );
354             }
355
356             i_index += 1;
357             break;
358
359         default: /* xx (unknown command) */
360             msg_Warn( p_dec, "unknown SPU command 0x%.2x", i_command );
361             if( i_index + 1 < i_next_seq )
362             {
363                  /* There is at least one other command sequence */
364                  if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
365                  {
366                      /* This is consistent. Skip to that command sequence. */
367                      i_index = i_next_seq;
368                  }
369                  else
370                  {
371                      /* There were other commands. */
372                      msg_Warn( p_dec, "cannot recover, dropping subtitle" );
373                      return VLC_EGENERIC;
374                  }
375             }
376             else
377             {
378                 /* We were in the last command sequence. Stop parsing by
379                  * pretending we met an SPU_CMD_END command. */
380                 i_command = SPU_CMD_END;
381                 i_index++;
382             }
383         }
384
385         /* */
386         if( i_command == SPU_CMD_END && i_index != i_next_seq )
387             break;
388     }
389
390     /* Check that the next sequence index matches the current one */
391     if( i_next_seq != i_cur_seq )
392     {
393         msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
394                  i_next_seq, i_cur_seq );
395         return VLC_EGENERIC;
396     }
397
398     if( i_index > p_sys->i_spu_size )
399     {
400         msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
401                  i_index, p_sys->i_spu_size );
402         return VLC_EGENERIC;
403     }
404
405     const int i_spu_size = p_sys->i_spu - 4;
406     if( p_spu_data->pi_offset[0] < 0 || p_spu_data->pi_offset[0] >= i_spu_size ||
407         p_spu_data->pi_offset[1] < 0 || p_spu_data->pi_offset[1] >= i_spu_size )
408     {
409         msg_Err( p_dec, "invalid offset values" );
410         return VLC_EGENERIC;
411     }
412
413     if( !p_spu->i_start )
414     {
415         msg_Err( p_dec, "no `start display' command" );
416         return VLC_EGENERIC;
417     }
418
419     if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
420     {
421         /* This subtitle will live for 5 seconds or until the next subtitle */
422         p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
423         p_spu->b_ephemer = true;
424     }
425
426     /* Get rid of padding bytes */
427     if( p_sys->i_spu_size > i_index + 1 )
428     {
429         /* Zero or one padding byte are quite usual
430          * More than one padding byte - this is very strange, but
431          * we can ignore them. */
432         msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
433                   p_sys->i_spu_size - i_index );
434     }
435
436     /* Successfully parsed ! */
437     return VLC_SUCCESS;
438 }
439
440 /*****************************************************************************
441  * ParseRLE: parse the RLE part of the subtitle
442  *****************************************************************************
443  * This part parses the subtitle graphical data and stores it in a more
444  * convenient structure for later decoding. For more information on the
445  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
446  *****************************************************************************/
447 static int ParseRLE( decoder_t *p_dec,
448                      subpicture_data_t *p_spu_data,
449                      const spu_properties_t *p_spu_properties )
450 {
451     decoder_sys_t *p_sys = p_dec->p_sys;
452
453     const unsigned int i_width = p_spu_properties->i_width;
454     const unsigned int i_height = p_spu_properties->i_height;
455     unsigned int i_x, i_y;
456
457     uint16_t *p_dest = p_spu_data->p_data;
458
459     /* The subtitles are interlaced, we need two offsets */
460     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
461     unsigned int  pi_table[ 2 ];
462     unsigned int *pi_offset;
463
464     /* Cropping */
465     bool b_empty_top = true;
466     unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
467     unsigned int i_transparent_code = 0;
468  
469     /* Colormap statistics */
470     int i_border = -1;
471     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
472
473     pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
474     pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
475
476     for( i_y = 0 ; i_y < i_height ; i_y++ )
477     {
478         unsigned int i_code;
479         pi_offset = pi_table + i_id;
480
481         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
482         {
483             i_code = 0;
484             for( unsigned int i_min = 1; i_min <= 0x40 && i_code < i_min; i_min <<= 2 )
485             {
486                 if( (*pi_offset >> 1) >= p_sys->i_spu_size )
487                 {
488                     msg_Err( p_dec, "out of bounds while reading rle" );
489                     return VLC_EGENERIC;
490                 }
491                 i_code = AddNibble( i_code, &p_sys->buffer[4], pi_offset );
492             }
493             if( i_code < 0x0004 )
494             {
495                 /* If the 14 first bits are set to 0, then it's a
496                  * new line. We emulate it. */
497                 i_code |= ( i_width - i_x ) << 2;
498             }
499
500             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
501             {
502                 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
503                          i_code >> 2, i_x, i_y, i_width, i_height );
504                 return VLC_EGENERIC;
505             }
506
507             /* Try to find the border color */
508             if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
509             {
510                 i_border = i_code & 0x3;
511                 stats[i_border] += i_code >> 2;
512             }
513
514             /* Auto crop subtitles (a lot more optimized) */
515             if( p_spu_data->b_auto_crop )
516             {
517                 if( !i_y )
518                 {
519                     /* We assume that if the first line is transparent, then
520                      * it is using the palette index for the
521                      * (background) transparent color */
522                     if( (i_code >> 2) == i_width &&
523                         p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
524                     {
525                         i_transparent_code = i_code;
526                     }
527                     else
528                     {
529                         p_spu_data->b_auto_crop = false;
530                     }
531                 }
532
533                 if( i_code == i_transparent_code )
534                 {
535                     if( b_empty_top )
536                     {
537                         /* This is a blank top line, we skip it */
538                       i_skipped_top++;
539                     }
540                     else
541                     {
542                         /* We can't be sure the current lines will be skipped,
543                          * so we store the code just in case. */
544                       *p_dest++ = i_code;
545                       i_skipped_bottom++;
546                     }
547                 }
548                 else
549                 {
550                     /* We got a valid code, store it */
551                     *p_dest++ = i_code;
552
553                     /* Valid code means no blank line */
554                     b_empty_top = false;
555                     i_skipped_bottom = 0;
556                 }
557             }
558             else
559             {
560                 *p_dest++ = i_code;
561             }
562         }
563
564         /* Check that we didn't go too far */
565         if( i_x > i_width )
566         {
567             msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
568             return VLC_EGENERIC;
569         }
570
571         /* Byte-align the stream */
572         if( *pi_offset & 0x1 )
573         {
574             (*pi_offset)++;
575         }
576
577         /* Swap fields */
578         i_id = ~i_id & 0x1;
579     }
580
581     /* We shouldn't get any padding bytes */
582     if( i_y < i_height )
583     {
584         msg_Err( p_dec, "padding bytes found in RLE sequence" );
585         msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
586                         "want to help debugging this" );
587
588         /* Skip them just in case */
589         while( i_y < i_height )
590         {
591             *p_dest++ = i_width << 2;
592             i_y++;
593         }
594
595         return VLC_EGENERIC;
596     }
597
598 #ifdef DEBUG_SPUDEC
599     msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
600              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
601 #endif
602
603     /* Crop if necessary */
604     if( i_skipped_top || i_skipped_bottom )
605     {
606 #ifdef DEBUG_SPUDEC
607         int i_y = p_spu->i_y + i_skipped_top;
608         int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
609 #endif
610         p_spu_data->i_y_top_offset = i_skipped_top;
611         p_spu_data->i_y_bottom_offset = i_skipped_bottom;
612 #ifdef DEBUG_SPUDEC
613         msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
614                  p_spu->i_width, i_height, p_spu->i_x, i_y );
615 #endif
616     }
617  
618     /* Handle color if no palette was found */
619     if( !p_spu_data->b_palette )
620     {
621         int i, i_inner = -1, i_shade = -1;
622
623         /* Set the border color */
624         p_spu_data->pi_yuv[i_border][0] = 0x00;
625         p_spu_data->pi_yuv[i_border][1] = 0x80;
626         p_spu_data->pi_yuv[i_border][2] = 0x80;
627         stats[i_border] = 0;
628
629         /* Find the inner colors */
630         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
631         {
632             if( stats[i] )
633             {
634                 i_inner = i;
635             }
636         }
637
638         for(       ; i < 4 && i_shade == -1 ; i++ )
639         {
640             if( stats[i] )
641             {
642                 if( stats[i] > stats[i_inner] )
643                 {
644                     i_shade = i_inner;
645                     i_inner = i;
646                 }
647                 else
648                 {
649                     i_shade = i;
650                 }
651             }
652         }
653
654         /* Set the inner color */
655         if( i_inner != -1 )
656         {
657             p_spu_data->pi_yuv[i_inner][0] = 0xff;
658             p_spu_data->pi_yuv[i_inner][1] = 0x80;
659             p_spu_data->pi_yuv[i_inner][2] = 0x80;
660         }
661
662         /* Set the anti-aliasing color */
663         if( i_shade != -1 )
664         {
665             p_spu_data->pi_yuv[i_shade][0] = 0x80;
666             p_spu_data->pi_yuv[i_shade][1] = 0x80;
667             p_spu_data->pi_yuv[i_shade][2] = 0x80;
668         }
669
670 #ifdef DEBUG_SPUDEC
671         msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
672                  i_border, i_inner, i_shade );
673 #endif
674     }
675
676     return VLC_SUCCESS;
677 }
678
679 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
680                     subpicture_data_t *p_spu_data,
681                     const spu_properties_t *p_spu_properties )
682 {
683     uint8_t *p_p;
684     int i_x, i_y, i_len, i_color, i_pitch;
685     const uint16_t *p_source = p_spu_data->p_data;
686     video_format_t fmt;
687     video_palette_t palette;
688
689     /* Create a new subpicture region */
690     memset( &fmt, 0, sizeof(video_format_t) );
691     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
692     fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
693     fmt.i_width = fmt.i_visible_width = p_spu_properties->i_width;
694     fmt.i_height = fmt.i_visible_height = p_spu_properties->i_height -
695         p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
696     fmt.i_x_offset = fmt.i_y_offset = 0;
697     fmt.p_palette = &palette;
698     fmt.p_palette->i_entries = 4;
699     for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
700     {
701         fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
702         fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
703         fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
704         fmt.p_palette->palette[i_x][3] =
705             p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
706             p_spu_data->pi_alpha[i_x] << 4;
707     }
708
709     p_spu->p_region = subpicture_region_New( &fmt );
710     if( !p_spu->p_region )
711     {
712         msg_Err( p_dec, "cannot allocate SPU region" );
713         return;
714     }
715
716     p_spu->p_region->i_x = p_spu_properties->i_x;
717     p_spu->p_region->i_y = p_spu_properties->i_y + p_spu_data->i_y_top_offset;
718     p_p = p_spu->p_region->p_picture->p->p_pixels;
719     i_pitch = p_spu->p_region->p_picture->p->i_pitch;
720
721     /* Draw until we reach the bottom of the subtitle */
722     for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
723     {
724         /* Draw until we reach the end of the line */
725         for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
726         {
727             /* Get the RLE part, then draw the line */
728             i_color = *p_source & 0x3;
729             i_len = *p_source++ >> 2;
730             memset( p_p + i_x + i_y, i_color, i_len );
731         }
732     }
733 }