1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2000-2001, 2005, 2006 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_codec.h>
35 #include <vlc_input.h>
39 /*****************************************************************************
41 *****************************************************************************/
52 mtime_t i_pts; /* presentation timestamp */
54 int pi_offset[2]; /* byte offsets to data */
57 /* Color information */
62 /* Auto crop fullscreen subtitles */
65 int i_y_bottom_offset;
69 static int ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_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 * );
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 )
84 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
88 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
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
97 *****************************************************************************/
98 subpicture_t * ParsePacket( decoder_t *p_dec )
100 decoder_sys_t *p_sys = p_dec->p_sys;
102 subpicture_data_t spu_data;
103 spu_properties_t spu_properties;
105 /* Allocate the subpicture internal data. */
106 p_spu = decoder_NewSubpicture( p_dec );
107 if( !p_spu ) return NULL;
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;
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;
121 /* Get display time now. If we do it later, we may miss the PTS. */
122 spu_data.i_pts = p_sys->i_pts;
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;
129 memset( &spu_properties, 0, sizeof(spu_properties) );
131 /* Getting the control part */
132 if( ParseControlSeq( p_dec, p_spu, &spu_data, &spu_properties ) )
134 /* There was a parse error, delete the subpicture */
135 decoder_DeleteSubpicture( p_dec, p_spu );
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.
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.
147 spu_data.p_data = malloc( sizeof(*spu_data.p_data) * 2 * 2 * p_sys->i_rle_size );
149 /* We try to display it */
150 if( ParseRLE( p_dec, &spu_data, &spu_properties ) )
152 /* There was a parse error, delete the subpicture */
153 decoder_DeleteSubpicture( p_dec, p_spu );
154 free( spu_data.p_data );
159 msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
161 spu_data.pi_offset[0], spu_data.pi_offset[1] );
164 Render( p_dec, p_spu, &spu_data, &spu_properties );
165 free( spu_data.p_data );
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 )
180 decoder_sys_t *p_sys = p_dec->p_sys;
182 /* Our current index in the SPU packet */
183 unsigned int i_index = p_sys->i_rle_size + 4;
185 /* The next start-of-control-sequence index and the previous one */
186 unsigned int i_next_seq = 0, i_cur_seq = 0;
188 /* Command and date */
189 uint8_t i_command = SPU_CMD_END;
192 if( !p_spu || !p_spu_data )
195 /* Initialize the structure */
196 p_spu->i_start = p_spu->i_stop = 0;
197 p_spu->b_ephemer = false;
199 for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
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 )
205 if( i_index + 4 > p_sys->i_spu_size )
207 msg_Err( p_dec, "overflow in SPU command sequence" );
211 /* Get the control sequence date */
212 date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
216 i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
218 if( i_next_seq > p_sys->i_spu_size )
220 msg_Err( p_dec, "overflow in SPU next command sequence" );
224 /* Skip what we just read */
228 i_command = p_sys->buffer[i_index];
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;
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;
244 case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
245 p_spu->i_stop = p_spu_data->i_pts + date;
249 case SPU_CMD_SET_PALETTE:
250 /* 03xxxx (palette) */
251 if( i_index + 3 > p_sys->i_spu_size )
253 msg_Err( p_dec, "overflow in SPU command" );
257 if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
262 p_spu_data->b_palette = true;
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;
269 for( i = 0; i < 4 ; i++ )
271 uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
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;
283 case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
284 if( i_index + 3 > p_sys->i_spu_size )
286 msg_Err( p_dec, "overflow in SPU command" );
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;
298 case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
299 if( i_index + 7 > p_sys->i_spu_size )
301 msg_Err( p_dec, "overflow in SPU command" );
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;
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;
315 /* Auto crop fullscreen subtitles */
316 if( p_spu_properties->i_height > 250 )
317 p_spu_data->b_auto_crop = true;
322 case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
323 if( i_index + 5 > p_sys->i_spu_size )
325 msg_Err( p_dec, "overflow in SPU command" );
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;
334 case SPU_CMD_END: /* ff (end) */
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 )
342 /* There is at least one other command sequence */
343 if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
345 /* This is consistent. Skip to that command sequence. */
346 i_index = i_next_seq;
350 /* There were other commands. */
351 msg_Warn( p_dec, "cannot recover, dropping subtitle" );
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;
364 /* We need to check for quit commands here */
365 if( !vlc_object_alive (p_dec) )
370 if( i_command == SPU_CMD_END && i_index != i_next_seq )
376 /* Check that the next sequence index matches the current one */
377 if( i_next_seq != i_cur_seq )
379 msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
380 i_next_seq, i_cur_seq );
384 if( i_index > p_sys->i_spu_size )
386 msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
387 i_index, p_sys->i_spu_size );
391 if( !p_spu->i_start )
393 msg_Err( p_dec, "no `start display' command" );
397 if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
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;
404 /* Get rid of padding bytes */
405 if( p_sys->i_spu_size > i_index + 1 )
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 );
414 /* Successfully parsed ! */
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 )
429 decoder_sys_t *p_sys = p_dec->p_sys;
430 uint8_t *p_src = &p_sys->buffer[4];
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;
438 uint16_t *p_dest = p_spu_data->p_data;
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;
446 bool b_empty_top = true;
447 unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
448 unsigned int i_transparent_code = 0;
450 /* Colormap statistics */
452 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
454 pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
455 pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
457 for( i_y = 0 ; i_y < i_height ; i_y++ )
459 pi_offset = pi_table + i_id;
461 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
463 i_code = AddNibble( 0, p_src, pi_offset );
467 i_code = AddNibble( i_code, p_src, pi_offset );
471 i_code = AddNibble( i_code, p_src, pi_offset );
475 i_code = AddNibble( i_code, p_src, pi_offset );
477 if( i_code < 0x0100 )
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 )
483 i_code |= ( i_width - i_x ) << 2;
487 /* We have a boo boo ! */
488 msg_Err( p_dec, "unknown RLE code "
497 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
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 );
504 /* Try to find the border color */
505 if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
507 i_border = i_code & 0x3;
508 stats[i_border] += i_code >> 2;
511 /* Auto crop subtitles (a lot more optimized) */
512 if( p_spu_data->b_auto_crop )
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 )
522 i_transparent_code = i_code;
526 p_spu_data->b_auto_crop = false;
530 if( i_code == i_transparent_code )
534 /* This is a blank top line, we skip it */
539 /* We can't be sure the current lines will be skipped,
540 * so we store the code just in case. */
547 /* We got a valid code, store it */
550 /* Valid code means no blank line */
552 i_skipped_bottom = 0;
561 /* Check that we didn't go too far */
564 msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
568 /* Byte-align the stream */
569 if( *pi_offset & 0x1 )
578 /* We shouldn't get any padding bytes */
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" );
585 /* Skip them just in case */
586 while( i_y < i_height )
588 *p_dest++ = i_width << 2;
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 );
600 /* Crop if necessary */
601 if( i_skipped_top || i_skipped_bottom )
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);
607 p_spu_data->i_y_top_offset = i_skipped_top;
608 p_spu_data->i_y_bottom_offset = i_skipped_bottom;
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 );
615 /* Handle color if no palette was found */
616 if( !p_spu_data->b_palette )
618 int i, i_inner = -1, i_shade = -1;
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;
626 /* Find the inner colors */
627 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
635 for( ; i < 4 && i_shade == -1 ; i++ )
639 if( stats[i] > stats[i_inner] )
651 /* Set the inner color */
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;
659 /* Set the anti-aliasing color */
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;
668 msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
669 i_border, i_inner, i_shade );
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 )
681 int i_x, i_y, i_len, i_color, i_pitch;
682 const uint16_t *p_source = p_spu_data->p_data;
684 video_palette_t palette;
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++ )
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;
706 p_spu->p_region = subpicture_region_New( &fmt );
707 if( !p_spu->p_region )
709 msg_Err( p_dec, "cannot allocate SPU region" );
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;
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 )
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 )
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 );