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