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