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