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