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