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 *****************************************************************************/
43 static int ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *);
44 static int ParseRLE ( decoder_t *, subpicture_t *, subpicture_data_t *);
45 static void Render ( decoder_t *, subpicture_t *, subpicture_data_t *);
47 /*****************************************************************************
48 * AddNibble: read a nibble from a source packet and add it to our integer.
49 *****************************************************************************/
50 static inline unsigned int AddNibble( unsigned int i_code,
51 uint8_t *p_src, unsigned int *pi_index )
55 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
59 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
63 /*****************************************************************************
64 * ParsePacket: parse an SPU packet and send it to the video output
65 *****************************************************************************
66 * This function parses the SPU packet and, if valid, sends it to the
68 *****************************************************************************/
69 subpicture_t * ParsePacket( decoder_t *p_dec )
71 decoder_sys_t *p_sys = p_dec->p_sys;
72 subpicture_data_t *p_spu_data;
75 /* Allocate the subpicture internal data. */
76 p_spu = p_dec->pf_spu_buffer_new( p_dec );
77 if( !p_spu ) return NULL;
79 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
80 * expand the RLE stuff so that we won't need to read nibbles later
81 * on. This will speed things up a lot. Plus, we'll only need to do
82 * this stupid interlacing stuff once. */
83 p_spu_data = malloc( sizeof(subpicture_data_t) + 4 * p_sys->i_rle_size );
84 p_spu_data->p_data = (uint8_t *)p_spu_data + sizeof(subpicture_data_t);
85 p_spu_data->b_palette = false;
86 p_spu_data->b_auto_crop = false;
87 p_spu_data->i_y_top_offset = 0;
88 p_spu_data->i_y_bottom_offset = 0;
90 p_spu_data->pi_alpha[0] = 0x00;
91 p_spu_data->pi_alpha[1] = 0x0f;
92 p_spu_data->pi_alpha[2] = 0x0f;
93 p_spu_data->pi_alpha[3] = 0x0f;
95 /* Get display time now. If we do it later, we may miss the PTS. */
96 p_spu_data->i_pts = p_sys->i_pts;
98 p_spu->i_original_picture_width =
99 p_dec->fmt_in.subs.spu.i_original_frame_width;
100 p_spu->i_original_picture_height =
101 p_dec->fmt_in.subs.spu.i_original_frame_height;
103 /* Getting the control part */
104 if( ParseControlSeq( p_dec, p_spu, p_spu_data ) )
106 /* There was a parse error, delete the subpicture */
107 p_dec->pf_spu_buffer_del( p_dec, p_spu );
111 /* We try to display it */
112 if( ParseRLE( p_dec, p_spu, p_spu_data ) )
114 /* There was a parse error, delete the subpicture */
115 p_dec->pf_spu_buffer_del( p_dec, p_spu );
120 msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
122 p_spu_data->pi_offset[0], p_spu_data->pi_offset[1] );
125 Render( p_dec, p_spu, p_spu_data );
131 /*****************************************************************************
132 * ParseControlSeq: parse all SPU control sequences
133 *****************************************************************************
134 * This is the most important part in SPU decoding. We get dates, palette
135 * information, coordinates, and so on. For more information on the
136 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
137 *****************************************************************************/
138 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
139 subpicture_data_t *p_spu_data )
141 decoder_sys_t *p_sys = p_dec->p_sys;
143 /* Our current index in the SPU packet */
144 unsigned int i_index = p_sys->i_rle_size + 4;
146 /* The next start-of-control-sequence index and the previous one */
147 unsigned int i_next_seq = 0, i_cur_seq = 0;
149 /* Command and date */
150 uint8_t i_command = SPU_CMD_END;
153 if( !p_spu || !p_spu_data )
156 /* Initialize the structure */
157 p_spu->i_start = p_spu->i_stop = 0;
158 p_spu->b_ephemer = false;
160 for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
162 /* If we just read a command sequence, read the next one;
163 * otherwise, go on with the commands of the current sequence. */
164 if( i_command == SPU_CMD_END )
166 if( i_index + 4 > p_sys->i_spu_size )
168 msg_Err( p_dec, "overflow in SPU command sequence" );
172 /* Get the control sequence date */
173 date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
175 date = date * p_sys->i_rate / INPUT_RATE_DEFAULT;
179 i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
181 if( i_next_seq > p_sys->i_spu_size )
183 msg_Err( p_dec, "overflow in SPU next command sequence" );
187 /* Skip what we just read */
191 i_command = p_sys->buffer[i_index];
195 case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
196 p_spu->i_start = p_spu_data->i_pts + date;
197 p_spu->b_ephemer = true;
201 /* Convert the dates in seconds to PTS values */
202 case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
203 p_spu->i_start = p_spu_data->i_pts + date;
207 case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
208 p_spu->i_stop = p_spu_data->i_pts + date;
212 case SPU_CMD_SET_PALETTE:
213 /* 03xxxx (palette) */
214 if( i_index + 3 > p_sys->i_spu_size )
216 msg_Err( p_dec, "overflow in SPU command" );
220 if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
225 p_spu_data->b_palette = true;
227 idx[0] = (p_sys->buffer[i_index+1]>>4)&0x0f;
228 idx[1] = (p_sys->buffer[i_index+1])&0x0f;
229 idx[2] = (p_sys->buffer[i_index+2]>>4)&0x0f;
230 idx[3] = (p_sys->buffer[i_index+2])&0x0f;
232 for( i = 0; i < 4 ; i++ )
234 uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
236 /* FIXME: this job should be done sooner */
237 p_spu_data->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
238 p_spu_data->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
239 p_spu_data->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
246 case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
247 if( i_index + 3 > p_sys->i_spu_size )
249 msg_Err( p_dec, "overflow in SPU command" );
253 p_spu_data->pi_alpha[3] = (p_sys->buffer[i_index+1]>>4)&0x0f;
254 p_spu_data->pi_alpha[2] = (p_sys->buffer[i_index+1])&0x0f;
255 p_spu_data->pi_alpha[1] = (p_sys->buffer[i_index+2]>>4)&0x0f;
256 p_spu_data->pi_alpha[0] = (p_sys->buffer[i_index+2])&0x0f;
261 case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
262 if( i_index + 7 > p_sys->i_spu_size )
264 msg_Err( p_dec, "overflow in SPU command" );
268 p_spu->i_x = (p_sys->buffer[i_index+1]<<4)|
269 ((p_sys->buffer[i_index+2]>>4)&0x0f);
270 p_spu->i_width = (((p_sys->buffer[i_index+2]&0x0f)<<8)|
271 p_sys->buffer[i_index+3]) - p_spu->i_x + 1;
273 p_spu->i_y = (p_sys->buffer[i_index+4]<<4)|
274 ((p_sys->buffer[i_index+5]>>4)&0x0f);
275 p_spu->i_height = (((p_sys->buffer[i_index+5]&0x0f)<<8)|
276 p_sys->buffer[i_index+6]) - p_spu->i_y + 1;
278 /* Auto crop fullscreen subtitles */
279 if( p_spu->i_height > 250 )
280 p_spu_data->b_auto_crop = true;
285 case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
286 if( i_index + 5 > p_sys->i_spu_size )
288 msg_Err( p_dec, "overflow in SPU command" );
292 p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+1]) - 4;
293 p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+3]) - 4;
297 case SPU_CMD_END: /* ff (end) */
301 default: /* xx (unknown command) */
302 msg_Warn( p_dec, "unknown SPU command 0x%.2x", i_command );
303 if( i_index + 1 < i_next_seq )
305 /* There is at least one other command sequence */
306 if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
308 /* This is consistent. Skip to that command sequence. */
309 i_index = i_next_seq;
313 /* There were other commands. */
314 msg_Warn( p_dec, "cannot recover, dropping subtitle" );
320 /* We were in the last command sequence. Stop parsing by
321 * pretending we met an SPU_CMD_END command. */
322 i_command = SPU_CMD_END;
327 /* We need to check for quit commands here */
328 if( !vlc_object_alive (p_dec) )
333 if( i_command == SPU_CMD_END && i_index != i_next_seq )
339 /* Check that the next sequence index matches the current one */
340 if( i_next_seq != i_cur_seq )
342 msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
343 i_next_seq, i_cur_seq );
347 if( i_index > p_sys->i_spu_size )
349 msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
350 i_index, p_sys->i_spu_size );
354 if( !p_spu->i_start )
356 msg_Err( p_dec, "no `start display' command" );
360 if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
362 /* This subtitle will live for 5 seconds or until the next subtitle */
363 p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
364 p_spu->b_ephemer = true;
367 /* Get rid of padding bytes */
368 if( p_sys->i_spu_size > i_index + 1 )
370 /* Zero or one padding byte are quite usual
371 * More than one padding byte - this is very strange, but
372 * we can ignore them. */
373 msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
374 p_sys->i_spu_size - i_index );
377 /* Successfully parsed ! */
381 /*****************************************************************************
382 * ParseRLE: parse the RLE part of the subtitle
383 *****************************************************************************
384 * This part parses the subtitle graphical data and stores it in a more
385 * convenient structure for later decoding. For more information on the
386 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
387 *****************************************************************************/
388 static int ParseRLE( decoder_t *p_dec, subpicture_t * p_spu,
389 subpicture_data_t *p_spu_data )
391 decoder_sys_t *p_sys = p_dec->p_sys;
392 uint8_t *p_src = &p_sys->buffer[4];
396 unsigned int i_width = p_spu->i_width;
397 unsigned int i_height = p_spu->i_height;
398 unsigned int i_x, i_y;
400 uint16_t *p_dest = (uint16_t *)p_spu_data->p_data;
402 /* The subtitles are interlaced, we need two offsets */
403 unsigned int i_id = 0; /* Start on the even SPU layer */
404 unsigned int pi_table[ 2 ];
405 unsigned int *pi_offset;
408 bool b_empty_top = true;
409 unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
410 unsigned int i_transparent_code = 0;
412 /* Colormap statistics */
414 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
416 pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
417 pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
419 for( i_y = 0 ; i_y < i_height ; i_y++ )
421 pi_offset = pi_table + i_id;
423 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
425 i_code = AddNibble( 0, p_src, pi_offset );
429 i_code = AddNibble( i_code, p_src, pi_offset );
433 i_code = AddNibble( i_code, p_src, pi_offset );
437 i_code = AddNibble( i_code, p_src, pi_offset );
439 if( i_code < 0x0100 )
441 /* If the 14 first bits are set to 0, then it's a
442 * new line. We emulate it. */
443 if( i_code < 0x0004 )
445 i_code |= ( i_width - i_x ) << 2;
449 /* We have a boo boo ! */
450 msg_Err( p_dec, "unknown RLE code "
459 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
461 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
462 i_code >> 2, i_x, i_y, i_width, i_height );
466 /* Try to find the border color */
467 if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
469 i_border = i_code & 0x3;
470 stats[i_border] += i_code >> 2;
473 /* Auto crop subtitles (a lot more optimized) */
474 if( p_spu_data->b_auto_crop )
478 /* We assume that if the first line is transparent, then
479 * it is using the palette index for the
480 * (background) transparent color */
481 if( (i_code >> 2) == i_width &&
482 p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
484 i_transparent_code = i_code;
488 p_spu_data->b_auto_crop = false;
492 if( i_code == i_transparent_code )
496 /* This is a blank top line, we skip it */
501 /* We can't be sure the current lines will be skipped,
502 * so we store the code just in case. */
509 /* We got a valid code, store it */
512 /* Valid code means no blank line */
514 i_skipped_bottom = 0;
523 /* Check that we didn't go too far */
526 msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
530 /* Byte-align the stream */
531 if( *pi_offset & 0x1 )
540 /* We shouldn't get any padding bytes */
543 msg_Err( p_dec, "padding bytes found in RLE sequence" );
544 msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
545 "want to help debugging this" );
547 /* Skip them just in case */
548 while( i_y < i_height )
550 *p_dest++ = i_width << 2;
558 msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
559 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
562 /* Crop if necessary */
563 if( i_skipped_top || i_skipped_bottom )
566 int i_y = p_spu->i_y + i_skipped_top;
567 int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
569 p_spu_data->i_y_top_offset = i_skipped_top;
570 p_spu_data->i_y_bottom_offset = i_skipped_bottom;
572 msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
573 p_spu->i_width, i_height, p_spu->i_x, i_y );
577 /* Handle color if no palette was found */
578 if( !p_spu_data->b_palette )
580 int i, i_inner = -1, i_shade = -1;
582 /* Set the border color */
583 p_spu_data->pi_yuv[i_border][0] = 0x00;
584 p_spu_data->pi_yuv[i_border][1] = 0x80;
585 p_spu_data->pi_yuv[i_border][2] = 0x80;
588 /* Find the inner colors */
589 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
597 for( ; i < 4 && i_shade == -1 ; i++ )
601 if( stats[i] > stats[i_inner] )
613 /* Set the inner color */
616 p_spu_data->pi_yuv[i_inner][0] = 0xff;
617 p_spu_data->pi_yuv[i_inner][1] = 0x80;
618 p_spu_data->pi_yuv[i_inner][2] = 0x80;
621 /* Set the anti-aliasing color */
624 p_spu_data->pi_yuv[i_shade][0] = 0x80;
625 p_spu_data->pi_yuv[i_shade][1] = 0x80;
626 p_spu_data->pi_yuv[i_shade][2] = 0x80;
630 msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
631 i_border, i_inner, i_shade );
638 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
639 subpicture_data_t *p_spu_data )
642 int i_x, i_y, i_len, i_color, i_pitch;
643 uint16_t *p_source = (uint16_t *)p_spu_data->p_data;
646 /* Create a new subpicture region */
647 memset( &fmt, 0, sizeof(video_format_t) );
648 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
649 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
650 fmt.i_width = fmt.i_visible_width = p_spu->i_width;
651 fmt.i_height = fmt.i_visible_height = p_spu->i_height -
652 p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
653 fmt.i_x_offset = fmt.i_y_offset = 0;
654 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
655 if( !p_spu->p_region )
657 msg_Err( p_dec, "cannot allocate SPU region" );
661 p_spu->p_region->i_x = 0;
662 p_spu->p_region->i_y = p_spu_data->i_y_top_offset;
663 p_p = p_spu->p_region->picture.p->p_pixels;
664 i_pitch = p_spu->p_region->picture.p->i_pitch;
667 fmt.p_palette->i_entries = 4;
668 for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
670 fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
671 fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
672 fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
673 fmt.p_palette->palette[i_x][3] =
674 p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
675 p_spu_data->pi_alpha[i_x] << 4;
678 /* Draw until we reach the bottom of the subtitle */
679 for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
681 /* Draw until we reach the end of the line */
682 for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
684 /* Get the RLE part, then draw the line */
685 i_color = *p_source & 0x3;
686 i_len = *p_source++ >> 2;
687 memset( p_p + i_x + i_y, i_color, i_len );