1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2000-2001, 2005, 2006 VLC authors and VideoLAN
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_codec.h>
35 #include <vlc_input.h>
39 /*****************************************************************************
41 *****************************************************************************/
52 int pi_offset
[2]; /* byte offsets to data */
55 /* Color information */
60 /* Auto crop fullscreen subtitles */
63 int i_y_bottom_offset
;
67 static int ParseControlSeq( decoder_t
*, subpicture_t
*, subpicture_data_t
*,
68 spu_properties_t
*, mtime_t i_pts
);
69 static int ParseRLE ( decoder_t
*, subpicture_data_t
*,
70 const spu_properties_t
* );
71 static void Render ( decoder_t
*, subpicture_t
*, subpicture_data_t
*,
72 const spu_properties_t
* );
74 /*****************************************************************************
75 * AddNibble: read a nibble from a source packet and add it to our integer.
76 *****************************************************************************/
77 static inline unsigned int AddNibble( unsigned int i_code
,
78 const uint8_t *p_src
, unsigned int *pi_index
)
82 return( i_code
<< 4 | ( p_src
[(*pi_index
)++ >> 1] & 0xf ) );
86 return( i_code
<< 4 | p_src
[(*pi_index
)++ >> 1] >> 4 );
90 /*****************************************************************************
91 * ParsePacket: parse an SPU packet and send it to the video output
92 *****************************************************************************
93 * This function parses the SPU packet and, if valid, sends it to the
95 *****************************************************************************/
96 subpicture_t
* ParsePacket( decoder_t
*p_dec
)
98 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
100 subpicture_data_t spu_data
;
101 spu_properties_t spu_properties
;
103 /* Allocate the subpicture internal data. */
104 p_spu
= decoder_NewSubpicture( p_dec
, NULL
);
105 if( !p_spu
) return NULL
;
107 p_spu
->i_original_picture_width
=
108 p_dec
->fmt_in
.subs
.spu
.i_original_frame_width
;
109 p_spu
->i_original_picture_height
=
110 p_dec
->fmt_in
.subs
.spu
.i_original_frame_height
;
112 /* Getting the control part */
113 if( ParseControlSeq( p_dec
, p_spu
, &spu_data
, &spu_properties
, p_sys
->i_pts
) )
115 /* There was a parse error, delete the subpicture */
116 subpicture_Delete( p_spu
);
120 /* we are going to expand the RLE stuff so that we won't need to read
121 * nibbles later on. This will speed things up a lot. Plus, we'll only
122 * need to do this stupid interlacing stuff once.
124 * Rationale for the "p_spudec->i_rle_size * 4*sizeof(*spu_data.p_data)":
125 * one byte gaves two nibbles and may be used twice (once per field)
126 * generating 4 codes.
128 spu_data
.p_data
= malloc( sizeof(*spu_data
.p_data
) * 2 * 2 * p_sys
->i_rle_size
);
130 /* We try to display it */
131 if( ParseRLE( p_dec
, &spu_data
, &spu_properties
) )
133 /* There was a parse error, delete the subpicture */
134 subpicture_Delete( p_spu
);
135 free( spu_data
.p_data
);
140 msg_Dbg( p_dec
, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
142 spu_data
.pi_offset
[0], spu_data
.pi_offset
[1] );
145 Render( p_dec
, p_spu
, &spu_data
, &spu_properties
);
146 free( spu_data
.p_data
);
151 /*****************************************************************************
152 * ParseControlSeq: parse all SPU control sequences
153 *****************************************************************************
154 * This is the most important part in SPU decoding. We get dates, palette
155 * information, coordinates, and so on. For more information on the
156 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
157 *****************************************************************************/
158 static int ParseControlSeq( decoder_t
*p_dec
, subpicture_t
*p_spu
,
159 subpicture_data_t
*p_spu_data
, spu_properties_t
*p_spu_properties
, mtime_t i_pts
)
161 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
163 /* Our current index in the SPU packet */
164 unsigned int i_index
;
166 /* The next start-of-control-sequence index and the previous one */
167 unsigned int i_next_seq
= 0, i_cur_seq
= 0;
169 /* Command and date */
170 uint8_t i_command
= SPU_CMD_END
;
172 bool b_cmd_offset
= false;
173 bool b_cmd_alpha
= false;
174 subpicture_data_t spu_data_cmd
;
176 if( !p_spu
|| !p_spu_data
)
179 /* Create working space for spu data */
180 memset( &spu_data_cmd
, 0, sizeof(spu_data_cmd
) );
181 spu_data_cmd
.pi_offset
[0] = -1;
182 spu_data_cmd
.pi_offset
[1] = -1;
183 spu_data_cmd
.p_data
= NULL
;
184 spu_data_cmd
.b_palette
= false;
185 spu_data_cmd
.b_auto_crop
= false;
186 spu_data_cmd
.i_y_top_offset
= 0;
187 spu_data_cmd
.i_y_bottom_offset
= 0;
188 spu_data_cmd
.pi_alpha
[0] = 0x00;
189 spu_data_cmd
.pi_alpha
[1] = 0x0f;
190 spu_data_cmd
.pi_alpha
[2] = 0x0f;
191 spu_data_cmd
.pi_alpha
[3] = 0x0f;
193 /* Initialize the structure */
194 p_spu
->i_start
= p_spu
->i_stop
= 0;
195 p_spu
->b_ephemer
= false;
197 memset( p_spu_properties
, 0, sizeof(*p_spu_properties
) );
200 *p_spu_data
= spu_data_cmd
;
202 for( i_index
= 4 + p_sys
->i_rle_size
; i_index
< p_sys
->i_spu_size
; )
204 /* If we just read a command sequence, read the next one;
205 * otherwise, go on with the commands of the current sequence. */
206 if( i_command
== SPU_CMD_END
)
208 if( i_index
+ 4 > p_sys
->i_spu_size
)
210 msg_Err( p_dec
, "overflow in SPU command sequence" );
215 b_cmd_offset
= false;
217 /* Get the control sequence date */
218 date
= (mtime_t
)GetWBE( &p_sys
->buffer
[i_index
] ) * 11000;
222 i_next_seq
= GetWBE( &p_sys
->buffer
[i_index
+2] );
224 if( i_next_seq
> p_sys
->i_spu_size
)
226 msg_Err( p_dec
, "overflow in SPU next command sequence" );
230 /* Skip what we just read */
234 i_command
= p_sys
->buffer
[i_index
];
238 case SPU_CMD_FORCE_DISPLAY
: /* 00 (force displaying) */
239 p_spu
->i_start
= i_pts
+ date
;
240 p_spu
->b_ephemer
= true;
241 /* ignores picture date as display start time
242 * works around non displayable (offset by few ms)
243 * spu menu over still frame in SPU_Select */
244 p_spu
->b_subtitle
= false;
248 /* Convert the dates in seconds to PTS values */
249 case SPU_CMD_START_DISPLAY
: /* 01 (start displaying) */
250 p_spu
->i_start
= i_pts
+ date
;
254 case SPU_CMD_STOP_DISPLAY
: /* 02 (stop displaying) */
255 p_spu
->i_stop
= i_pts
+ date
;
259 case SPU_CMD_SET_PALETTE
:
260 /* 03xxxx (palette) */
261 if( i_index
+ 3 > p_sys
->i_spu_size
)
263 msg_Err( p_dec
, "overflow in SPU command" );
267 if( p_dec
->fmt_in
.subs
.spu
.palette
[0] == 0xBeeF )
272 spu_data_cmd
.b_palette
= true;
274 idx
[0] = (p_sys
->buffer
[i_index
+1]>>4)&0x0f;
275 idx
[1] = (p_sys
->buffer
[i_index
+1])&0x0f;
276 idx
[2] = (p_sys
->buffer
[i_index
+2]>>4)&0x0f;
277 idx
[3] = (p_sys
->buffer
[i_index
+2])&0x0f;
279 for( i
= 0; i
< 4 ; i
++ )
281 uint32_t i_color
= p_dec
->fmt_in
.subs
.spu
.palette
[1+idx
[i
]];
283 /* FIXME: this job should be done sooner */
284 spu_data_cmd
.pi_yuv
[3-i
][0] = (i_color
>>16) & 0xff;
285 spu_data_cmd
.pi_yuv
[3-i
][1] = (i_color
>>0) & 0xff;
286 spu_data_cmd
.pi_yuv
[3-i
][2] = (i_color
>>8) & 0xff;
293 case SPU_CMD_SET_ALPHACHANNEL
: /* 04xxxx (alpha channel) */
294 if( i_index
+ 3 > p_sys
->i_spu_size
)
296 msg_Err( p_dec
, "overflow in SPU command" );
300 if(!p_sys
->b_disabletrans
)
301 { /* If we want to use original transparency values */
303 spu_data_cmd
.pi_alpha
[3] = (p_sys
->buffer
[i_index
+1]>>4)&0x0f;
304 spu_data_cmd
.pi_alpha
[2] = (p_sys
->buffer
[i_index
+1])&0x0f;
305 spu_data_cmd
.pi_alpha
[1] = (p_sys
->buffer
[i_index
+2]>>4)&0x0f;
306 spu_data_cmd
.pi_alpha
[0] = (p_sys
->buffer
[i_index
+2])&0x0f;
312 case SPU_CMD_SET_COORDINATES
: /* 05xxxyyyxxxyyy (coordinates) */
313 if( i_index
+ 7 > p_sys
->i_spu_size
)
315 msg_Err( p_dec
, "overflow in SPU command" );
319 p_spu_properties
->i_x
= (p_sys
->buffer
[i_index
+1]<<4)|
320 ((p_sys
->buffer
[i_index
+2]>>4)&0x0f);
321 p_spu_properties
->i_width
= (((p_sys
->buffer
[i_index
+2]&0x0f)<<8)|
322 p_sys
->buffer
[i_index
+3]) - p_spu_properties
->i_x
+ 1;
324 p_spu_properties
->i_y
= (p_sys
->buffer
[i_index
+4]<<4)|
325 ((p_sys
->buffer
[i_index
+5]>>4)&0x0f);
326 p_spu_properties
->i_height
= (((p_sys
->buffer
[i_index
+5]&0x0f)<<8)|
327 p_sys
->buffer
[i_index
+6]) - p_spu_properties
->i_y
+ 1;
329 /* Auto crop fullscreen subtitles */
330 if( p_spu_properties
->i_height
> 250 )
331 p_spu_data
->b_auto_crop
= true;
336 case SPU_CMD_SET_OFFSETS
: /* 06xxxxyyyy (byte offsets) */
337 if( i_index
+ 5 > p_sys
->i_spu_size
)
339 msg_Err( p_dec
, "overflow in SPU command" );
344 p_spu_data
->pi_offset
[0] = GetWBE(&p_sys
->buffer
[i_index
+1]) - 4;
345 p_spu_data
->pi_offset
[1] = GetWBE(&p_sys
->buffer
[i_index
+3]) - 4;
349 case SPU_CMD_END
: /* ff (end) */
352 /* It seems that palette and alpha from the block having
353 * the cmd offset have to be used
355 p_spu_data
->b_palette
= spu_data_cmd
.b_palette
;
356 if( spu_data_cmd
.b_palette
)
357 memcpy( p_spu_data
->pi_yuv
, spu_data_cmd
.pi_yuv
, sizeof(spu_data_cmd
.pi_yuv
) );
359 memcpy( p_spu_data
->pi_alpha
, spu_data_cmd
.pi_alpha
, sizeof(spu_data_cmd
.pi_alpha
) );
365 default: /* xx (unknown command) */
366 msg_Warn( p_dec
, "unknown SPU command 0x%.2x", i_command
);
367 if( i_index
+ 1 < i_next_seq
)
369 /* There is at least one other command sequence */
370 if( p_sys
->buffer
[i_next_seq
- 1] == SPU_CMD_END
)
372 /* This is consistent. Skip to that command sequence. */
373 i_index
= i_next_seq
;
377 /* There were other commands. */
378 msg_Warn( p_dec
, "cannot recover, dropping subtitle" );
384 /* We were in the last command sequence. Stop parsing by
385 * pretending we met an SPU_CMD_END command. */
386 i_command
= SPU_CMD_END
;
392 if( i_command
== SPU_CMD_END
&& i_index
!= i_next_seq
)
396 /* Check that the next sequence index matches the current one */
397 if( i_next_seq
!= i_cur_seq
)
399 msg_Err( p_dec
, "index mismatch (0x%.4x != 0x%.4x)",
400 i_next_seq
, i_cur_seq
);
404 if( i_index
> p_sys
->i_spu_size
)
406 msg_Err( p_dec
, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
407 i_index
, p_sys
->i_spu_size
);
411 const int i_spu_size
= p_sys
->i_spu
- 4;
412 if( p_spu_data
->pi_offset
[0] < 0 || p_spu_data
->pi_offset
[0] >= i_spu_size
||
413 p_spu_data
->pi_offset
[1] < 0 || p_spu_data
->pi_offset
[1] >= i_spu_size
)
415 msg_Err( p_dec
, "invalid offset values" );
419 if( !p_spu
->i_start
)
421 msg_Err( p_dec
, "no `start display' command" );
425 if( p_spu
->i_stop
<= p_spu
->i_start
&& !p_spu
->b_ephemer
)
427 /* This subtitle will live for 5 seconds or until the next subtitle */
428 p_spu
->i_stop
= p_spu
->i_start
+ (mtime_t
)500 * 11000;
429 p_spu
->b_ephemer
= true;
432 /* Get rid of padding bytes */
433 if( p_sys
->i_spu_size
> i_index
+ 1 )
435 /* Zero or one padding byte are quite usual
436 * More than one padding byte - this is very strange, but
437 * we can ignore them. */
438 msg_Warn( p_dec
, "%i padding bytes, we usually get 0 or 1 of them",
439 p_sys
->i_spu_size
- i_index
);
442 /* Successfully parsed ! */
446 /*****************************************************************************
447 * ParseRLE: parse the RLE part of the subtitle
448 *****************************************************************************
449 * This part parses the subtitle graphical data and stores it in a more
450 * convenient structure for later decoding. For more information on the
451 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
452 *****************************************************************************/
453 static int ParseRLE( decoder_t
*p_dec
,
454 subpicture_data_t
*p_spu_data
,
455 const spu_properties_t
*p_spu_properties
)
457 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
459 const unsigned int i_width
= p_spu_properties
->i_width
;
460 const unsigned int i_height
= p_spu_properties
->i_height
;
461 unsigned int i_x
, i_y
;
463 uint16_t *p_dest
= p_spu_data
->p_data
;
465 /* The subtitles are interlaced, we need two offsets */
466 unsigned int i_id
= 0; /* Start on the even SPU layer */
467 unsigned int pi_table
[ 2 ];
468 unsigned int *pi_offset
;
471 bool b_empty_top
= true;
472 unsigned int i_skipped_top
= 0, i_skipped_bottom
= 0;
473 unsigned int i_transparent_code
= 0;
475 /* Colormap statistics */
477 int stats
[4]; stats
[0] = stats
[1] = stats
[2] = stats
[3] = 0;
479 pi_table
[ 0 ] = p_spu_data
->pi_offset
[ 0 ] << 1;
480 pi_table
[ 1 ] = p_spu_data
->pi_offset
[ 1 ] << 1;
482 for( i_y
= 0 ; i_y
< i_height
; i_y
++ )
485 pi_offset
= pi_table
+ i_id
;
487 for( i_x
= 0 ; i_x
< i_width
; i_x
+= i_code
>> 2 )
490 for( unsigned int i_min
= 1; i_min
<= 0x40 && i_code
< i_min
; i_min
<<= 2 )
492 if( (*pi_offset
>> 1) >= p_sys
->i_spu_size
)
494 msg_Err( p_dec
, "out of bounds while reading rle" );
497 i_code
= AddNibble( i_code
, &p_sys
->buffer
[4], pi_offset
);
499 if( i_code
< 0x0004 )
501 /* If the 14 first bits are set to 0, then it's a
502 * new line. We emulate it. */
503 i_code
|= ( i_width
- i_x
) << 2;
506 if( ( (i_code
>> 2) + i_x
+ i_y
* i_width
) > i_height
* i_width
)
508 msg_Err( p_dec
, "out of bounds, %i at (%i,%i) is out of %ix%i",
509 i_code
>> 2, i_x
, i_y
, i_width
, i_height
);
513 /* Try to find the border color */
514 if( p_spu_data
->pi_alpha
[ i_code
& 0x3 ] != 0x00 )
516 i_border
= i_code
& 0x3;
517 stats
[i_border
] += i_code
>> 2;
520 /* Auto crop subtitles (a lot more optimized) */
521 if( p_spu_data
->b_auto_crop
)
525 /* We assume that if the first line is transparent, then
526 * it is using the palette index for the
527 * (background) transparent color */
528 if( (i_code
>> 2) == i_width
&&
529 p_spu_data
->pi_alpha
[ i_code
& 0x3 ] == 0x00 )
531 i_transparent_code
= i_code
;
535 p_spu_data
->b_auto_crop
= false;
539 if( i_code
== i_transparent_code
)
543 /* This is a blank top line, we skip it */
548 /* We can't be sure the current lines will be skipped,
549 * so we store the code just in case. */
556 /* We got a valid code, store it */
559 /* Valid code means no blank line */
561 i_skipped_bottom
= 0;
570 /* Check that we didn't go too far */
573 msg_Err( p_dec
, "i_x overflowed, %i > %i", i_x
, i_width
);
577 /* Byte-align the stream */
578 if( *pi_offset
& 0x1 )
587 /* We shouldn't get any padding bytes */
590 msg_Err( p_dec
, "padding bytes found in RLE sequence" );
591 msg_Err( p_dec
, "send mail to <sam@zoy.org> if you "
592 "want to help debugging this" );
594 /* Skip them just in case */
595 while( i_y
< i_height
)
597 *p_dest
++ = i_width
<< 2;
605 msg_Dbg( p_dec
, "valid subtitle, size: %ix%i, position: %i,%i",
606 p_spu
->i_width
, p_spu
->i_height
, p_spu
->i_x
, p_spu
->i_y
);
609 /* Crop if necessary */
610 if( i_skipped_top
|| i_skipped_bottom
)
613 int i_y
= p_spu
->i_y
+ i_skipped_top
;
614 int i_height
= p_spu
->i_height
- (i_skipped_top
+ i_skipped_bottom
);
616 p_spu_data
->i_y_top_offset
= i_skipped_top
;
617 p_spu_data
->i_y_bottom_offset
= i_skipped_bottom
;
619 msg_Dbg( p_dec
, "cropped to: %ix%i, position: %i,%i",
620 p_spu
->i_width
, i_height
, p_spu
->i_x
, i_y
);
624 /* Handle color if no palette was found */
625 if( !p_spu_data
->b_palette
)
627 int i
, i_inner
= -1, i_shade
= -1;
629 /* Set the border color */
632 p_spu_data
->pi_yuv
[i_border
][0] = 0x00;
633 p_spu_data
->pi_yuv
[i_border
][1] = 0x80;
634 p_spu_data
->pi_yuv
[i_border
][2] = 0x80;
638 /* Find the inner colors */
639 for( i
= 0 ; i
< 4 && i_inner
== -1 ; i
++ )
647 for( ; i
< 4 && i_shade
== -1 ; i
++ )
651 if( stats
[i
] > stats
[i_inner
] )
663 /* Set the inner color */
666 p_spu_data
->pi_yuv
[i_inner
][0] = 0xff;
667 p_spu_data
->pi_yuv
[i_inner
][1] = 0x80;
668 p_spu_data
->pi_yuv
[i_inner
][2] = 0x80;
671 /* Set the anti-aliasing color */
674 p_spu_data
->pi_yuv
[i_shade
][0] = 0x80;
675 p_spu_data
->pi_yuv
[i_shade
][1] = 0x80;
676 p_spu_data
->pi_yuv
[i_shade
][2] = 0x80;
680 msg_Dbg( p_dec
, "using custom palette (border %i, inner %i, shade %i)",
681 i_border
, i_inner
, i_shade
);
688 static void Render( decoder_t
*p_dec
, subpicture_t
*p_spu
,
689 subpicture_data_t
*p_spu_data
,
690 const spu_properties_t
*p_spu_properties
)
693 int i_x
, i_y
, i_len
, i_color
, i_pitch
;
694 const uint16_t *p_source
= p_spu_data
->p_data
;
696 video_palette_t palette
;
698 /* Create a new subpicture region */
699 video_format_Init( &fmt
, VLC_CODEC_YUVP
);
700 fmt
.i_sar_num
= 0; /* 0 means use aspect ratio of background video */
702 fmt
.i_width
= fmt
.i_visible_width
= p_spu_properties
->i_width
;
703 fmt
.i_height
= fmt
.i_visible_height
= p_spu_properties
->i_height
-
704 p_spu_data
->i_y_top_offset
- p_spu_data
->i_y_bottom_offset
;
705 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
706 fmt
.p_palette
= &palette
;
707 fmt
.p_palette
->i_entries
= 4;
708 for( i_x
= 0; i_x
< fmt
.p_palette
->i_entries
; i_x
++ )
710 fmt
.p_palette
->palette
[i_x
][0] = p_spu_data
->pi_yuv
[i_x
][0];
711 fmt
.p_palette
->palette
[i_x
][1] = p_spu_data
->pi_yuv
[i_x
][1];
712 fmt
.p_palette
->palette
[i_x
][2] = p_spu_data
->pi_yuv
[i_x
][2];
713 fmt
.p_palette
->palette
[i_x
][3] = p_spu_data
->pi_alpha
[i_x
] * 0x11;
716 p_spu
->p_region
= subpicture_region_New( &fmt
);
717 if( !p_spu
->p_region
)
719 fmt
.p_palette
= NULL
;
720 video_format_Clean( &fmt
);
721 msg_Err( p_dec
, "cannot allocate SPU region" );
725 p_spu
->p_region
->i_x
= p_spu_properties
->i_x
;
726 p_spu
->p_region
->i_y
= p_spu_properties
->i_y
+ p_spu_data
->i_y_top_offset
;
727 p_p
= p_spu
->p_region
->p_picture
->p
->p_pixels
;
728 i_pitch
= p_spu
->p_region
->p_picture
->p
->i_pitch
;
730 /* Draw until we reach the bottom of the subtitle */
731 for( i_y
= 0; i_y
< (int)fmt
.i_height
* i_pitch
; i_y
+= i_pitch
)
733 /* Draw until we reach the end of the line */
734 for( i_x
= 0 ; i_x
< (int)fmt
.i_width
; i_x
+= i_len
)
736 /* Get the RLE part, then draw the line */
737 i_color
= *p_source
& 0x3;
738 i_len
= *p_source
++ >> 2;
739 memset( p_p
+ i_x
+ i_y
, i_color
, i_len
);
743 fmt
.p_palette
= NULL
;
744 video_format_Clean( &fmt
);