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