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