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