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