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