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