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>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *);
43 static int ParseRLE ( decoder_t *, subpicture_t *, subpicture_data_t *);
44 static void Render ( decoder_t *, subpicture_t *, subpicture_data_t *);
46 /*****************************************************************************
47 * AddNibble: read a nibble from a source packet and add it to our integer.
48 *****************************************************************************/
49 static inline unsigned int AddNibble( unsigned int i_code,
50 uint8_t *p_src, unsigned int *pi_index )
54 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
58 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
62 /*****************************************************************************
63 * ParsePacket: parse an SPU packet and send it to the video output
64 *****************************************************************************
65 * This function parses the SPU packet and, if valid, sends it to the
67 *****************************************************************************/
68 subpicture_t * ParsePacket( decoder_t *p_dec )
70 decoder_sys_t *p_sys = p_dec->p_sys;
71 subpicture_data_t *p_spu_data;
74 /* Allocate the subpicture internal data. */
75 p_spu = p_dec->pf_spu_buffer_new( p_dec );
76 if( !p_spu ) return NULL;
78 p_spu->b_pausable = true;
80 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
81 * expand the RLE stuff so that we won't need to read nibbles later
82 * on. This will speed things up a lot. Plus, we'll only need to do
83 * this stupid interlacing stuff once. */
84 p_spu_data = malloc( sizeof(subpicture_data_t) + 4 * p_sys->i_rle_size );
85 p_spu_data->p_data = (uint8_t *)p_spu_data + sizeof(subpicture_data_t);
86 p_spu_data->b_palette = false;
87 p_spu_data->b_auto_crop = false;
88 p_spu_data->i_y_top_offset = 0;
89 p_spu_data->i_y_bottom_offset = 0;
91 p_spu_data->pi_alpha[0] = 0x00;
92 p_spu_data->pi_alpha[1] = 0x0f;
93 p_spu_data->pi_alpha[2] = 0x0f;
94 p_spu_data->pi_alpha[3] = 0x0f;
96 /* Get display time now. If we do it later, we may miss the PTS. */
97 p_spu_data->i_pts = p_sys->i_pts;
99 p_spu->i_original_picture_width =
100 p_dec->fmt_in.subs.spu.i_original_frame_width;
101 p_spu->i_original_picture_height =
102 p_dec->fmt_in.subs.spu.i_original_frame_height;
104 /* Getting the control part */
105 if( ParseControlSeq( p_dec, p_spu, p_spu_data ) )
107 /* There was a parse error, delete the subpicture */
108 p_dec->pf_spu_buffer_del( p_dec, p_spu );
112 /* We try to display it */
113 if( ParseRLE( p_dec, p_spu, p_spu_data ) )
115 /* There was a parse error, delete the subpicture */
116 p_dec->pf_spu_buffer_del( p_dec, p_spu );
121 msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
123 p_spu_data->pi_offset[0], p_spu_data->pi_offset[1] );
126 Render( p_dec, p_spu, p_spu_data );
132 /*****************************************************************************
133 * ParseControlSeq: parse all SPU control sequences
134 *****************************************************************************
135 * This is the most important part in SPU decoding. We get dates, palette
136 * information, coordinates, and so on. For more information on the
137 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
138 *****************************************************************************/
139 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
140 subpicture_data_t *p_spu_data )
142 decoder_sys_t *p_sys = p_dec->p_sys;
144 /* Our current index in the SPU packet */
145 unsigned int i_index = p_sys->i_rle_size + 4;
147 /* The next start-of-control-sequence index and the previous one */
148 unsigned int i_next_seq = 0, i_cur_seq = 0;
150 /* Command and date */
151 uint8_t i_command = SPU_CMD_END;
154 if( !p_spu || !p_spu_data )
157 /* Initialize the structure */
158 p_spu->i_start = p_spu->i_stop = 0;
159 p_spu->b_ephemer = false;
161 for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
163 /* If we just read a command sequence, read the next one;
164 * otherwise, go on with the commands of the current sequence. */
165 if( i_command == SPU_CMD_END )
167 if( i_index + 4 > p_sys->i_spu_size )
169 msg_Err( p_dec, "overflow in SPU command sequence" );
173 /* Get the control sequence date */
174 date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
175 /* FIXME How to access i_rate
176 * p_spudec->bit_stream.p_pes->i_rate / DEFAULT_RATE;
181 i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
183 if( i_next_seq > p_sys->i_spu_size )
185 msg_Err( p_dec, "overflow in SPU next command sequence" );
189 /* Skip what we just read */
193 i_command = p_sys->buffer[i_index];
197 case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
198 p_spu->i_start = p_spu_data->i_pts + date;
199 p_spu->b_ephemer = true;
203 /* Convert the dates in seconds to PTS values */
204 case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
205 p_spu->i_start = p_spu_data->i_pts + date;
209 case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
210 p_spu->i_stop = p_spu_data->i_pts + date;
214 case SPU_CMD_SET_PALETTE:
215 /* 03xxxx (palette) */
216 if( i_index + 3 > p_sys->i_spu_size )
218 msg_Err( p_dec, "overflow in SPU command" );
222 if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
227 p_spu_data->b_palette = true;
229 idx[0] = (p_sys->buffer[i_index+1]>>4)&0x0f;
230 idx[1] = (p_sys->buffer[i_index+1])&0x0f;
231 idx[2] = (p_sys->buffer[i_index+2]>>4)&0x0f;
232 idx[3] = (p_sys->buffer[i_index+2])&0x0f;
234 for( i = 0; i < 4 ; i++ )
236 uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
238 /* FIXME: this job should be done sooner */
239 p_spu_data->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
240 p_spu_data->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
241 p_spu_data->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
248 case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
249 if( i_index + 3 > p_sys->i_spu_size )
251 msg_Err( p_dec, "overflow in SPU command" );
255 p_spu_data->pi_alpha[3] = (p_sys->buffer[i_index+1]>>4)&0x0f;
256 p_spu_data->pi_alpha[2] = (p_sys->buffer[i_index+1])&0x0f;
257 p_spu_data->pi_alpha[1] = (p_sys->buffer[i_index+2]>>4)&0x0f;
258 p_spu_data->pi_alpha[0] = (p_sys->buffer[i_index+2])&0x0f;
263 case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
264 if( i_index + 7 > p_sys->i_spu_size )
266 msg_Err( p_dec, "overflow in SPU command" );
270 p_spu->i_x = (p_sys->buffer[i_index+1]<<4)|
271 ((p_sys->buffer[i_index+2]>>4)&0x0f);
272 p_spu->i_width = (((p_sys->buffer[i_index+2]&0x0f)<<8)|
273 p_sys->buffer[i_index+3]) - p_spu->i_x + 1;
275 p_spu->i_y = (p_sys->buffer[i_index+4]<<4)|
276 ((p_sys->buffer[i_index+5]>>4)&0x0f);
277 p_spu->i_height = (((p_sys->buffer[i_index+5]&0x0f)<<8)|
278 p_sys->buffer[i_index+6]) - p_spu->i_y + 1;
280 /* Auto crop fullscreen subtitles */
281 if( p_spu->i_height > 250 )
282 p_spu_data->b_auto_crop = true;
287 case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
288 if( i_index + 5 > p_sys->i_spu_size )
290 msg_Err( p_dec, "overflow in SPU command" );
294 p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+1]) - 4;
295 p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+3]) - 4;
299 case SPU_CMD_END: /* ff (end) */
303 default: /* xx (unknown command) */
304 msg_Warn( p_dec, "unknown SPU command 0x%.2x", i_command );
305 if( i_index + 1 < i_next_seq )
307 /* There is at least one other command sequence */
308 if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
310 /* This is consistent. Skip to that command sequence. */
311 i_index = i_next_seq;
315 /* There were other commands. */
316 msg_Warn( p_dec, "cannot recover, dropping subtitle" );
322 /* We were in the last command sequence. Stop parsing by
323 * pretending we met an SPU_CMD_END command. */
324 i_command = SPU_CMD_END;
329 /* We need to check for quit commands here */
330 if( !vlc_object_alive (p_dec) )
335 if( i_command == SPU_CMD_END && i_index != i_next_seq )
341 /* Check that the next sequence index matches the current one */
342 if( i_next_seq != i_cur_seq )
344 msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
345 i_next_seq, i_cur_seq );
349 if( i_index > p_sys->i_spu_size )
351 msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
352 i_index, p_sys->i_spu_size );
356 if( !p_spu->i_start )
358 msg_Err( p_dec, "no `start display' command" );
362 if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
364 /* This subtitle will live for 5 seconds or until the next subtitle */
365 p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
366 p_spu->b_ephemer = true;
369 /* Get rid of padding bytes */
370 if( p_sys->i_spu_size > i_index + 1 )
372 /* Zero or one padding byte are quite usual
373 * More than one padding byte - this is very strange, but
374 * we can ignore them. */
375 msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
376 p_sys->i_spu_size - i_index );
379 /* Successfully parsed ! */
383 /*****************************************************************************
384 * ParseRLE: parse the RLE part of the subtitle
385 *****************************************************************************
386 * This part parses the subtitle graphical data and stores it in a more
387 * convenient structure for later decoding. For more information on the
388 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
389 *****************************************************************************/
390 static int ParseRLE( decoder_t *p_dec, subpicture_t * p_spu,
391 subpicture_data_t *p_spu_data )
393 decoder_sys_t *p_sys = p_dec->p_sys;
394 uint8_t *p_src = &p_sys->buffer[4];
398 unsigned int i_width = p_spu->i_width;
399 unsigned int i_height = p_spu->i_height;
400 unsigned int i_x, i_y;
402 uint16_t *p_dest = (uint16_t *)p_spu_data->p_data;
404 /* The subtitles are interlaced, we need two offsets */
405 unsigned int i_id = 0; /* Start on the even SPU layer */
406 unsigned int pi_table[ 2 ];
407 unsigned int *pi_offset;
410 bool b_empty_top = true;
411 unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
412 unsigned int i_transparent_code = 0;
414 /* Colormap statistics */
416 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
418 pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
419 pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
421 for( i_y = 0 ; i_y < i_height ; i_y++ )
423 pi_offset = pi_table + i_id;
425 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
427 i_code = AddNibble( 0, p_src, pi_offset );
431 i_code = AddNibble( i_code, p_src, pi_offset );
435 i_code = AddNibble( i_code, p_src, pi_offset );
439 i_code = AddNibble( i_code, p_src, pi_offset );
441 if( i_code < 0x0100 )
443 /* If the 14 first bits are set to 0, then it's a
444 * new line. We emulate it. */
445 if( i_code < 0x0004 )
447 i_code |= ( i_width - i_x ) << 2;
451 /* We have a boo boo ! */
452 msg_Err( p_dec, "unknown RLE code "
461 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
463 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
464 i_code >> 2, i_x, i_y, i_width, i_height );
468 /* Try to find the border color */
469 if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
471 i_border = i_code & 0x3;
472 stats[i_border] += i_code >> 2;
475 /* Auto crop subtitles (a lot more optimized) */
476 if( p_spu_data->b_auto_crop )
480 /* We assume that if the first line is transparent, then
481 * it is using the palette index for the
482 * (background) transparent color */
483 if( (i_code >> 2) == i_width &&
484 p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
486 i_transparent_code = i_code;
490 p_spu_data->b_auto_crop = false;
494 if( i_code == i_transparent_code )
498 /* This is a blank top line, we skip it */
503 /* We can't be sure the current lines will be skipped,
504 * so we store the code just in case. */
511 /* We got a valid code, store it */
514 /* Valid code means no blank line */
516 i_skipped_bottom = 0;
525 /* Check that we didn't go too far */
528 msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
532 /* Byte-align the stream */
533 if( *pi_offset & 0x1 )
542 /* We shouldn't get any padding bytes */
545 msg_Err( p_dec, "padding bytes found in RLE sequence" );
546 msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
547 "want to help debugging this" );
549 /* Skip them just in case */
550 while( i_y < i_height )
552 *p_dest++ = i_width << 2;
560 msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
561 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
564 /* Crop if necessary */
565 if( i_skipped_top || i_skipped_bottom )
568 int i_y = p_spu->i_y + i_skipped_top;
569 int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
571 p_spu_data->i_y_top_offset = i_skipped_top;
572 p_spu_data->i_y_bottom_offset = i_skipped_bottom;
574 msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
575 p_spu->i_width, i_height, p_spu->i_x, i_y );
579 /* Handle color if no palette was found */
580 if( !p_spu_data->b_palette )
582 int i, i_inner = -1, i_shade = -1;
584 /* Set the border color */
585 p_spu_data->pi_yuv[i_border][0] = 0x00;
586 p_spu_data->pi_yuv[i_border][1] = 0x80;
587 p_spu_data->pi_yuv[i_border][2] = 0x80;
590 /* Find the inner colors */
591 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
599 for( ; i < 4 && i_shade == -1 ; i++ )
603 if( stats[i] > stats[i_inner] )
615 /* Set the inner color */
618 p_spu_data->pi_yuv[i_inner][0] = 0xff;
619 p_spu_data->pi_yuv[i_inner][1] = 0x80;
620 p_spu_data->pi_yuv[i_inner][2] = 0x80;
623 /* Set the anti-aliasing color */
626 p_spu_data->pi_yuv[i_shade][0] = 0x80;
627 p_spu_data->pi_yuv[i_shade][1] = 0x80;
628 p_spu_data->pi_yuv[i_shade][2] = 0x80;
632 msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
633 i_border, i_inner, i_shade );
640 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
641 subpicture_data_t *p_spu_data )
644 int i_x, i_y, i_len, i_color, i_pitch;
645 uint16_t *p_source = (uint16_t *)p_spu_data->p_data;
648 /* Create a new subpicture region */
649 memset( &fmt, 0, sizeof(video_format_t) );
650 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
651 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
652 fmt.i_width = fmt.i_visible_width = p_spu->i_width;
653 fmt.i_height = fmt.i_visible_height = p_spu->i_height -
654 p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
655 fmt.i_x_offset = fmt.i_y_offset = 0;
656 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
657 if( !p_spu->p_region )
659 msg_Err( p_dec, "cannot allocate SPU region" );
663 p_spu->p_region->i_x = 0;
664 p_spu->p_region->i_y = p_spu_data->i_y_top_offset;
665 p_p = p_spu->p_region->picture.p->p_pixels;
666 i_pitch = p_spu->p_region->picture.p->i_pitch;
669 fmt.p_palette->i_entries = 4;
670 for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
672 fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
673 fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
674 fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
675 fmt.p_palette->palette[i_x][3] =
676 p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
677 p_spu_data->pi_alpha[i_x] << 4;
680 /* Draw until we reach the bottom of the subtitle */
681 for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
683 /* Draw until we reach the end of the line */
684 for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
686 /* Get the RLE part, then draw the line */
687 i_color = *p_source & 0x3;
688 i_len = *p_source++ >> 2;
689 memset( p_p + i_x + i_y, i_color, i_len );