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>
35 #include <vlc_codec.h>
36 #include <vlc_input.h>
40 /*****************************************************************************
42 *****************************************************************************/
51 static int ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *,
53 static int ParseRLE ( decoder_t *, subpicture_data_t *,
54 const spu_properties_t * );
55 static void Render ( decoder_t *, subpicture_t *, subpicture_data_t *,
56 const spu_properties_t * );
58 /*****************************************************************************
59 * AddNibble: read a nibble from a source packet and add it to our integer.
60 *****************************************************************************/
61 static inline unsigned int AddNibble( unsigned int i_code,
62 uint8_t *p_src, unsigned int *pi_index )
66 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
70 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
74 /*****************************************************************************
75 * ParsePacket: parse an SPU packet and send it to the video output
76 *****************************************************************************
77 * This function parses the SPU packet and, if valid, sends it to the
79 *****************************************************************************/
80 subpicture_t * ParsePacket( decoder_t *p_dec )
82 decoder_sys_t *p_sys = p_dec->p_sys;
83 subpicture_data_t *p_spu_data;
85 spu_properties_t spu_properties;
87 /* Allocate the subpicture internal data. */
88 p_spu = decoder_NewSubpicture( p_dec );
89 if( !p_spu ) return NULL;
91 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
92 * expand the RLE stuff so that we won't need to read nibbles later
93 * on. This will speed things up a lot. Plus, we'll only need to do
94 * this stupid interlacing stuff once. */
95 p_spu_data = malloc( sizeof(subpicture_data_t) + 4 * p_sys->i_rle_size );
96 p_spu_data->p_data = (uint8_t *)p_spu_data + sizeof(subpicture_data_t);
97 p_spu_data->b_palette = false;
98 p_spu_data->b_auto_crop = false;
99 p_spu_data->i_y_top_offset = 0;
100 p_spu_data->i_y_bottom_offset = 0;
102 p_spu_data->pi_alpha[0] = 0x00;
103 p_spu_data->pi_alpha[1] = 0x0f;
104 p_spu_data->pi_alpha[2] = 0x0f;
105 p_spu_data->pi_alpha[3] = 0x0f;
107 /* Get display time now. If we do it later, we may miss the PTS. */
108 p_spu_data->i_pts = p_sys->i_pts;
110 p_spu->i_original_picture_width =
111 p_dec->fmt_in.subs.spu.i_original_frame_width;
112 p_spu->i_original_picture_height =
113 p_dec->fmt_in.subs.spu.i_original_frame_height;
115 memset( &spu_properties, 0, sizeof(spu_properties) );
117 /* Getting the control part */
118 if( ParseControlSeq( p_dec, p_spu, p_spu_data, &spu_properties ) )
120 /* There was a parse error, delete the subpicture */
121 decoder_DeleteSubpicture( p_dec, p_spu );
126 /* We try to display it */
127 if( ParseRLE( p_dec, p_spu_data, &spu_properties ) )
129 /* There was a parse error, delete the subpicture */
130 decoder_DeleteSubpicture( p_dec, p_spu );
136 msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
138 p_spu_data->pi_offset[0], p_spu_data->pi_offset[1] );
141 Render( p_dec, p_spu, p_spu_data, &spu_properties );
147 /*****************************************************************************
148 * ParseControlSeq: parse all SPU control sequences
149 *****************************************************************************
150 * This is the most important part in SPU decoding. We get dates, palette
151 * information, coordinates, and so on. For more information on the
152 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
153 *****************************************************************************/
154 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
155 subpicture_data_t *p_spu_data, spu_properties_t *p_spu_properties )
157 decoder_sys_t *p_sys = p_dec->p_sys;
159 /* Our current index in the SPU packet */
160 unsigned int i_index = p_sys->i_rle_size + 4;
162 /* The next start-of-control-sequence index and the previous one */
163 unsigned int i_next_seq = 0, i_cur_seq = 0;
165 /* Command and date */
166 uint8_t i_command = SPU_CMD_END;
169 if( !p_spu || !p_spu_data )
172 /* Initialize the structure */
173 p_spu->i_start = p_spu->i_stop = 0;
174 p_spu->b_ephemer = false;
176 for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
178 /* If we just read a command sequence, read the next one;
179 * otherwise, go on with the commands of the current sequence. */
180 if( i_command == SPU_CMD_END )
182 if( i_index + 4 > p_sys->i_spu_size )
184 msg_Err( p_dec, "overflow in SPU command sequence" );
188 /* Get the control sequence date */
189 date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
193 i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
195 if( i_next_seq > p_sys->i_spu_size )
197 msg_Err( p_dec, "overflow in SPU next command sequence" );
201 /* Skip what we just read */
205 i_command = p_sys->buffer[i_index];
209 case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
210 p_spu->i_start = p_spu_data->i_pts + date;
211 p_spu->b_ephemer = true;
215 /* Convert the dates in seconds to PTS values */
216 case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
217 p_spu->i_start = p_spu_data->i_pts + date;
221 case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
222 p_spu->i_stop = p_spu_data->i_pts + date;
226 case SPU_CMD_SET_PALETTE:
227 /* 03xxxx (palette) */
228 if( i_index + 3 > p_sys->i_spu_size )
230 msg_Err( p_dec, "overflow in SPU command" );
234 if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
239 p_spu_data->b_palette = true;
241 idx[0] = (p_sys->buffer[i_index+1]>>4)&0x0f;
242 idx[1] = (p_sys->buffer[i_index+1])&0x0f;
243 idx[2] = (p_sys->buffer[i_index+2]>>4)&0x0f;
244 idx[3] = (p_sys->buffer[i_index+2])&0x0f;
246 for( i = 0; i < 4 ; i++ )
248 uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
250 /* FIXME: this job should be done sooner */
251 p_spu_data->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
252 p_spu_data->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
253 p_spu_data->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
260 case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
261 if( i_index + 3 > p_sys->i_spu_size )
263 msg_Err( p_dec, "overflow in SPU command" );
267 p_spu_data->pi_alpha[3] = (p_sys->buffer[i_index+1]>>4)&0x0f;
268 p_spu_data->pi_alpha[2] = (p_sys->buffer[i_index+1])&0x0f;
269 p_spu_data->pi_alpha[1] = (p_sys->buffer[i_index+2]>>4)&0x0f;
270 p_spu_data->pi_alpha[0] = (p_sys->buffer[i_index+2])&0x0f;
275 case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
276 if( i_index + 7 > p_sys->i_spu_size )
278 msg_Err( p_dec, "overflow in SPU command" );
282 p_spu_properties->i_x = (p_sys->buffer[i_index+1]<<4)|
283 ((p_sys->buffer[i_index+2]>>4)&0x0f);
284 p_spu_properties->i_width = (((p_sys->buffer[i_index+2]&0x0f)<<8)|
285 p_sys->buffer[i_index+3]) - p_spu_properties->i_x + 1;
287 p_spu_properties->i_y = (p_sys->buffer[i_index+4]<<4)|
288 ((p_sys->buffer[i_index+5]>>4)&0x0f);
289 p_spu_properties->i_height = (((p_sys->buffer[i_index+5]&0x0f)<<8)|
290 p_sys->buffer[i_index+6]) - p_spu_properties->i_y + 1;
292 /* Auto crop fullscreen subtitles */
293 if( p_spu_properties->i_height > 250 )
294 p_spu_data->b_auto_crop = true;
299 case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
300 if( i_index + 5 > p_sys->i_spu_size )
302 msg_Err( p_dec, "overflow in SPU command" );
306 p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+1]) - 4;
307 p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+3]) - 4;
311 case SPU_CMD_END: /* ff (end) */
315 default: /* xx (unknown command) */
316 msg_Warn( p_dec, "unknown SPU command 0x%.2x", i_command );
317 if( i_index + 1 < i_next_seq )
319 /* There is at least one other command sequence */
320 if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
322 /* This is consistent. Skip to that command sequence. */
323 i_index = i_next_seq;
327 /* There were other commands. */
328 msg_Warn( p_dec, "cannot recover, dropping subtitle" );
334 /* We were in the last command sequence. Stop parsing by
335 * pretending we met an SPU_CMD_END command. */
336 i_command = SPU_CMD_END;
341 /* We need to check for quit commands here */
342 if( !vlc_object_alive (p_dec) )
347 if( i_command == SPU_CMD_END && i_index != i_next_seq )
353 /* Check that the next sequence index matches the current one */
354 if( i_next_seq != i_cur_seq )
356 msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
357 i_next_seq, i_cur_seq );
361 if( i_index > p_sys->i_spu_size )
363 msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
364 i_index, p_sys->i_spu_size );
368 if( !p_spu->i_start )
370 msg_Err( p_dec, "no `start display' command" );
374 if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
376 /* This subtitle will live for 5 seconds or until the next subtitle */
377 p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
378 p_spu->b_ephemer = true;
381 /* Get rid of padding bytes */
382 if( p_sys->i_spu_size > i_index + 1 )
384 /* Zero or one padding byte are quite usual
385 * More than one padding byte - this is very strange, but
386 * we can ignore them. */
387 msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
388 p_sys->i_spu_size - i_index );
391 /* Successfully parsed ! */
395 /*****************************************************************************
396 * ParseRLE: parse the RLE part of the subtitle
397 *****************************************************************************
398 * This part parses the subtitle graphical data and stores it in a more
399 * convenient structure for later decoding. For more information on the
400 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
401 *****************************************************************************/
402 static int ParseRLE( decoder_t *p_dec,
403 subpicture_data_t *p_spu_data,
404 const spu_properties_t *p_spu_properties )
406 decoder_sys_t *p_sys = p_dec->p_sys;
407 uint8_t *p_src = &p_sys->buffer[4];
411 unsigned int i_width = p_spu_properties->i_width;
412 unsigned int i_height = p_spu_properties->i_height;
413 unsigned int i_x, i_y;
415 uint16_t *p_dest = (uint16_t *)p_spu_data->p_data;
417 /* The subtitles are interlaced, we need two offsets */
418 unsigned int i_id = 0; /* Start on the even SPU layer */
419 unsigned int pi_table[ 2 ];
420 unsigned int *pi_offset;
423 bool b_empty_top = true;
424 unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
425 unsigned int i_transparent_code = 0;
427 /* Colormap statistics */
429 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
431 pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
432 pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
434 for( i_y = 0 ; i_y < i_height ; i_y++ )
436 pi_offset = pi_table + i_id;
438 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
440 i_code = AddNibble( 0, p_src, pi_offset );
444 i_code = AddNibble( i_code, p_src, pi_offset );
448 i_code = AddNibble( i_code, p_src, pi_offset );
452 i_code = AddNibble( i_code, p_src, pi_offset );
454 if( i_code < 0x0100 )
456 /* If the 14 first bits are set to 0, then it's a
457 * new line. We emulate it. */
458 if( i_code < 0x0004 )
460 i_code |= ( i_width - i_x ) << 2;
464 /* We have a boo boo ! */
465 msg_Err( p_dec, "unknown RLE code "
474 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
476 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
477 i_code >> 2, i_x, i_y, i_width, i_height );
481 /* Try to find the border color */
482 if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
484 i_border = i_code & 0x3;
485 stats[i_border] += i_code >> 2;
488 /* Auto crop subtitles (a lot more optimized) */
489 if( p_spu_data->b_auto_crop )
493 /* We assume that if the first line is transparent, then
494 * it is using the palette index for the
495 * (background) transparent color */
496 if( (i_code >> 2) == i_width &&
497 p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
499 i_transparent_code = i_code;
503 p_spu_data->b_auto_crop = false;
507 if( i_code == i_transparent_code )
511 /* This is a blank top line, we skip it */
516 /* We can't be sure the current lines will be skipped,
517 * so we store the code just in case. */
524 /* We got a valid code, store it */
527 /* Valid code means no blank line */
529 i_skipped_bottom = 0;
538 /* Check that we didn't go too far */
541 msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
545 /* Byte-align the stream */
546 if( *pi_offset & 0x1 )
555 /* We shouldn't get any padding bytes */
558 msg_Err( p_dec, "padding bytes found in RLE sequence" );
559 msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
560 "want to help debugging this" );
562 /* Skip them just in case */
563 while( i_y < i_height )
565 *p_dest++ = i_width << 2;
573 msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
574 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
577 /* Crop if necessary */
578 if( i_skipped_top || i_skipped_bottom )
581 int i_y = p_spu->i_y + i_skipped_top;
582 int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
584 p_spu_data->i_y_top_offset = i_skipped_top;
585 p_spu_data->i_y_bottom_offset = i_skipped_bottom;
587 msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
588 p_spu->i_width, i_height, p_spu->i_x, i_y );
592 /* Handle color if no palette was found */
593 if( !p_spu_data->b_palette )
595 int i, i_inner = -1, i_shade = -1;
597 /* Set the border color */
598 p_spu_data->pi_yuv[i_border][0] = 0x00;
599 p_spu_data->pi_yuv[i_border][1] = 0x80;
600 p_spu_data->pi_yuv[i_border][2] = 0x80;
603 /* Find the inner colors */
604 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
612 for( ; i < 4 && i_shade == -1 ; i++ )
616 if( stats[i] > stats[i_inner] )
628 /* Set the inner color */
631 p_spu_data->pi_yuv[i_inner][0] = 0xff;
632 p_spu_data->pi_yuv[i_inner][1] = 0x80;
633 p_spu_data->pi_yuv[i_inner][2] = 0x80;
636 /* Set the anti-aliasing color */
639 p_spu_data->pi_yuv[i_shade][0] = 0x80;
640 p_spu_data->pi_yuv[i_shade][1] = 0x80;
641 p_spu_data->pi_yuv[i_shade][2] = 0x80;
645 msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
646 i_border, i_inner, i_shade );
653 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
654 subpicture_data_t *p_spu_data,
655 const spu_properties_t *p_spu_properties )
658 int i_x, i_y, i_len, i_color, i_pitch;
659 uint16_t *p_source = (uint16_t *)p_spu_data->p_data;
661 video_palette_t palette;
663 /* Create a new subpicture region */
664 memset( &fmt, 0, sizeof(video_format_t) );
665 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
666 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
667 fmt.i_width = fmt.i_visible_width = p_spu_properties->i_width;
668 fmt.i_height = fmt.i_visible_height = p_spu_properties->i_height -
669 p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
670 fmt.i_x_offset = fmt.i_y_offset = 0;
671 fmt.p_palette = &palette;
672 fmt.p_palette->i_entries = 4;
673 for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
675 fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
676 fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
677 fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
678 fmt.p_palette->palette[i_x][3] =
679 p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
680 p_spu_data->pi_alpha[i_x] << 4;
683 p_spu->p_region = subpicture_region_New( &fmt );
684 if( !p_spu->p_region )
686 msg_Err( p_dec, "cannot allocate SPU region" );
690 p_spu->p_region->i_x = p_spu_properties->i_x;
691 p_spu->p_region->i_y = p_spu_properties->i_y + p_spu_data->i_y_top_offset;
692 p_p = p_spu->p_region->p_picture->p->p_pixels;
693 i_pitch = p_spu->p_region->p_picture->p->i_pitch;
695 /* Draw until we reach the bottom of the subtitle */
696 for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
698 /* Draw until we reach the end of the line */
699 for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
701 /* Get the RLE part, then draw the line */
702 i_color = *p_source & 0x3;
703 i_len = *p_source++ >> 2;
704 memset( p_p + i_x + i_y, i_color, i_len );