1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: parse.c,v 1.4 2002/11/06 18:07:57 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memcpy(), memset() */
32 #include <vlc/decoder.h>
35 # include <unistd.h> /* getpid() */
38 #ifdef WIN32 /* getpid() for win32 is located in process.h */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int ParseControlSeq ( spudec_thread_t *, subpicture_t * );
48 static int ParseRLE ( spudec_thread_t *, subpicture_t *, uint8_t * );
50 static void DestroySPU ( subpicture_t * );
52 static void UpdateSPU ( subpicture_t *, vlc_object_t * );
53 static int CropCallback ( vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void * );
56 /*****************************************************************************
57 * AddNibble: read a nibble from a source packet and add it to our integer.
58 *****************************************************************************/
59 static inline unsigned int AddNibble( unsigned int i_code,
60 uint8_t *p_src, int *pi_index )
64 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
68 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
72 /*****************************************************************************
73 * SyncPacket: get in sync with the stream
74 *****************************************************************************
75 * This function makes a few sanity checks and returns 0 if it looks like we
76 * are at the beginning of a subpicture packet.
77 *****************************************************************************/
78 int E_(SyncPacket)( spudec_thread_t *p_spudec )
80 /* Re-align the buffer on an 8-bit boundary */
81 RealignBits( &p_spudec->bit_stream );
83 /* The total SPU packet size, often bigger than a PS packet */
84 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
86 /* The RLE stuff size (remove 4 because we just read 32 bits) */
87 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
89 /* If the values we got are a bit strange, skip packet */
90 if( !p_spudec->i_spu_size
91 || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
96 RemoveBits( &p_spudec->bit_stream, 16 );
101 /*****************************************************************************
102 * ParsePacket: parse an SPU packet and send it to the video output
103 *****************************************************************************
104 * This function parses the SPU packet and, if valid, sends it to the
106 *****************************************************************************/
107 void E_(ParsePacket)( spudec_thread_t *p_spudec )
109 subpicture_t * p_spu;
111 unsigned int i_offset;
114 msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
115 p_spudec->i_spu_size );
117 /* We cannot display a subpicture with no date */
118 NextPTS( &p_spudec->bit_stream, &i_pts, NULL );
121 msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
125 /* Allocate the subpicture internal data. */
126 p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE );
133 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
134 * expand the RLE stuff so that we won't need to read nibbles later
135 * on. This will speed things up a lot. Plus, we'll only need to do
136 * this stupid interlacing stuff once. */
137 p_spu->p_sys = malloc( sizeof( subpicture_sys_t )
138 + p_spudec->i_rle_size * 4 );
140 if( p_spu->p_sys == NULL )
142 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
146 /* Fill the p_spu structure */
147 vlc_mutex_init( p_spudec->p_fifo, &p_spu->p_sys->lock );
149 p_spu->pf_render = E_(RenderSPU);
150 p_spu->pf_destroy = DestroySPU;
151 p_spu->p_sys->p_data = (uint8_t*)p_spu->p_sys + sizeof( subpicture_sys_t );
152 p_spu->p_sys->b_palette = VLC_FALSE;
154 p_spu->p_sys->pi_alpha[0] = 0x00;
155 p_spu->p_sys->pi_alpha[1] = 0x0f;
156 p_spu->p_sys->pi_alpha[2] = 0x0f;
157 p_spu->p_sys->pi_alpha[3] = 0x0f;
159 p_spu->p_sys->b_crop = VLC_FALSE;
161 /* Get display time now. If we do it later, we may miss the PTS. */
162 p_spu->p_sys->i_pts = i_pts;
164 /* Attach to our input thread */
165 p_spu->p_sys->p_input = vlc_object_find( p_spudec->p_fifo,
166 VLC_OBJECT_INPUT, FIND_PARENT );
167 if( p_spu->p_sys->p_input )
171 if( !var_Get( p_spu->p_sys->p_input, "highlight-mutex", &val ) )
173 vlc_mutex_t *p_mutex = val.p_address;
175 vlc_mutex_lock( p_mutex );
176 UpdateSPU( p_spu, VLC_OBJECT(p_spu->p_sys->p_input) );
177 var_AddCallback( p_spu->p_sys->p_input,
178 "highlight", CropCallback, p_spu );
179 vlc_mutex_unlock( p_mutex );
183 /* Allocate the temporary buffer we will parse */
184 p_src = malloc( p_spudec->i_rle_size );
188 msg_Err( p_spudec->p_fifo, "out of memory" );
189 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
194 for( i_offset = 0; i_offset < p_spudec->i_rle_size;
195 i_offset += SPU_CHUNK_SIZE )
197 GetChunk( &p_spudec->bit_stream, p_src + i_offset,
198 ( i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size ) ?
199 SPU_CHUNK_SIZE : p_spudec->i_rle_size - i_offset );
201 /* Abort subtitle parsing if we were requested to stop */
202 if( p_spudec->p_fifo->b_die )
205 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
211 /* Dump the subtitle info */
212 intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
215 /* Getting the control part */
216 if( ParseControlSeq( p_spudec, p_spu ) )
218 /* There was a parse error, delete the subpicture */
220 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
224 /* At this point, no more GetBit() command is needed, so we have all
225 * the data we need to tell whether the subtitle is valid. Thus we
226 * try to display it and we ignore b_die. */
228 if( ParseRLE( p_spudec, p_spu, p_src ) )
230 /* There was a parse error, delete the subpicture */
232 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
236 msg_Dbg( p_spudec->p_fifo, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
237 p_spudec->i_spu_size,
238 p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
240 /* SPU is finished - we can ask the video output to display it */
241 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
243 /* TODO: do stuff! */
249 /*****************************************************************************
250 * ParseControlSeq: parse all SPU control sequences
251 *****************************************************************************
252 * This is the most important part in SPU decoding. We get dates, palette
253 * information, coordinates, and so on. For more information on the
254 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
255 *****************************************************************************/
256 static int ParseControlSeq( spudec_thread_t *p_spudec,
257 subpicture_t * p_spu )
259 /* Our current index in the SPU packet */
260 int i_index = p_spudec->i_rle_size + 4;
262 /* The next start-of-control-sequence index and the previous one */
263 int i_next_seq = 0, i_cur_seq = 0;
265 /* Command and date */
266 uint8_t i_command = SPU_CMD_END;
271 /* Initialize the structure */
272 p_spu->i_start = p_spu->i_stop = 0;
273 p_spu->b_ephemer = VLC_FALSE;
277 /* If we just read a command sequence, read the next one;
278 * otherwise, go on with the commands of the current sequence. */
279 if( i_command == SPU_CMD_END )
281 /* Get the control sequence date */
282 date = GetBits( &p_spudec->bit_stream, 16 );
286 i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
288 /* Skip what we just read */
292 i_command = GetBits( &p_spudec->bit_stream, 8 );
297 case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
298 p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
299 p_spu->b_ephemer = VLC_TRUE;
302 /* Convert the dates in seconds to PTS values */
303 case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
304 p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
307 case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
308 p_spu->i_stop = p_spu->p_sys->i_pts + ( date * 11000 );
311 case SPU_CMD_SET_PALETTE:
313 /* 03xxxx (palette) */
314 if( p_spudec->p_fifo->p_demux_data
315 && *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
319 p_spu->p_sys->b_palette = VLC_TRUE;
320 for( i = 0; i < 4 ; i++ )
322 i_color = ((uint32_t*)((char*)p_spudec->p_fifo->
323 p_demux_data + sizeof(int)))[
324 GetBits(&p_spudec->bit_stream, 4) ];
326 /* FIXME: this job should be done sooner */
327 #ifndef WORDS_BIGENDIAN
328 p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
329 p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
330 p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
332 p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>8) & 0xff;
333 p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>24) & 0xff;
334 p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>16) & 0xff;
340 RemoveBits( &p_spudec->bit_stream, 16 );
346 case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
347 pi_alpha[3] = GetBits( &p_spudec->bit_stream, 4 );
348 pi_alpha[2] = GetBits( &p_spudec->bit_stream, 4 );
349 pi_alpha[1] = GetBits( &p_spudec->bit_stream, 4 );
350 pi_alpha[0] = GetBits( &p_spudec->bit_stream, 4 );
352 /* Ignore blank alpha palette. Sometimes spurious blank
353 * alpha palettes are present - dunno why. */
354 if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
356 p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
357 p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
358 p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
359 p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
363 msg_Warn( p_spudec->p_fifo, "ignoring blank alpha palette" );
369 case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
370 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
371 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
374 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
375 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
381 case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
382 p_spu->p_sys->pi_offset[0] =
383 GetBits( &p_spudec->bit_stream, 16 ) - 4;
385 p_spu->p_sys->pi_offset[1] =
386 GetBits( &p_spudec->bit_stream, 16 ) - 4;
391 case SPU_CMD_END: /* ff (end) */
394 default: /* xx (unknown command) */
395 msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
400 /* We need to check for quit commands here */
401 if( p_spudec->p_fifo->b_die )
406 } while( i_command != SPU_CMD_END || i_index == i_next_seq );
408 /* Check that the next sequence index matches the current one */
409 if( i_next_seq != i_cur_seq )
411 msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
412 i_next_seq, i_cur_seq );
416 if( i_index > p_spudec->i_spu_size )
418 msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
419 i_index, p_spudec->i_spu_size );
423 if( !p_spu->i_start )
425 msg_Err( p_spudec->p_fifo, "no `start display' command" );
428 if( !p_spu->i_stop && !p_spu->b_ephemer )
430 /* This subtitle will live for 5 seconds or until the next subtitle */
431 p_spu->i_stop = p_spu->i_start + 500 * 11000;
432 p_spu->b_ephemer = VLC_TRUE;
435 /* Get rid of padding bytes */
436 switch( p_spudec->i_spu_size - i_index )
438 /* Zero or one padding byte, quite usual */
440 RemoveBits( &p_spudec->bit_stream, 8 );
445 /* More than one padding byte - this is very strange, but
446 * we can deal with it */
448 msg_Warn( p_spudec->p_fifo,
449 "%i padding bytes, we usually get 0 or 1 of them",
450 p_spudec->i_spu_size - i_index );
452 while( i_index < p_spudec->i_spu_size )
454 RemoveBits( &p_spudec->bit_stream, 8 );
461 /* Successfully parsed ! */
465 /*****************************************************************************
466 * ParseRLE: parse the RLE part of the subtitle
467 *****************************************************************************
468 * This part parses the subtitle graphical data and stores it in a more
469 * convenient structure for later decoding. For more information on the
470 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
471 *****************************************************************************/
472 static int ParseRLE( spudec_thread_t *p_spudec,
473 subpicture_t * p_spu, uint8_t * p_src )
477 unsigned int i_width = p_spu->i_width;
478 unsigned int i_height = p_spu->i_height;
479 unsigned int i_x, i_y;
481 uint16_t *p_dest = (uint16_t *)p_spu->p_sys->p_data;
483 /* The subtitles are interlaced, we need two offsets */
484 unsigned int i_id = 0; /* Start on the even SPU layer */
485 unsigned int pi_table[ 2 ];
486 unsigned int *pi_offset;
489 vlc_bool_t b_empty_top = VLC_TRUE,
490 b_empty_bottom = VLC_FALSE;
491 unsigned int i_skipped_top = 0,
492 i_skipped_bottom = 0;
495 /* Colormap statistics */
497 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
499 pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
500 pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
502 for( i_y = 0 ; i_y < i_height ; i_y++ )
504 pi_offset = pi_table + i_id;
506 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
508 i_code = AddNibble( 0, p_src, pi_offset );
512 i_code = AddNibble( i_code, p_src, pi_offset );
516 i_code = AddNibble( i_code, p_src, pi_offset );
520 i_code = AddNibble( i_code, p_src, pi_offset );
522 if( i_code < 0x0100 )
524 /* If the 14 first bits are set to 0, then it's a
525 * new line. We emulate it. */
526 if( i_code < 0x0004 )
528 i_code |= ( i_width - i_x ) << 2;
532 /* We have a boo boo ! */
533 msg_Err( p_spudec->p_fifo, "unknown RLE code "
542 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
544 msg_Err( p_spudec->p_fifo,
545 "out of bounds, %i at (%i,%i) is out of %ix%i",
546 i_code >> 2, i_x, i_y, i_width, i_height );
550 /* Try to find the border color */
551 if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
553 i_border = i_code & 0x3;
554 stats[i_border] += i_code >> 2;
558 if( (i_code >> 2) == i_width
559 && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
563 /* This is a blank top line, we skip it */
568 /* We can't be sure the current lines will be skipped,
569 * so we store the code just in case. */
572 b_empty_bottom = VLC_TRUE;
578 /* We got a valid code, store it */
581 /* Valid code means no blank line */
582 b_empty_top = VLC_FALSE;
583 b_empty_bottom = VLC_FALSE;
584 i_skipped_bottom = 0;
591 /* Check that we didn't go too far */
594 msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
599 /* Byte-align the stream */
600 if( *pi_offset & 0x1 )
609 /* We shouldn't get any padding bytes */
612 msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
613 msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
614 "want to help debugging this" );
616 /* Skip them just in case */
617 while( i_y < i_height )
619 *p_dest++ = i_width << 2;
626 msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
627 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
630 /* Crop if necessary */
631 if( i_skipped_top || i_skipped_bottom )
633 p_spu->i_y += i_skipped_top;
634 p_spu->i_height -= i_skipped_top + i_skipped_bottom;
636 msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
637 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
641 /* Handle color if no palette was found */
642 if( !p_spu->p_sys->b_palette )
644 int i, i_inner = -1, i_shade = -1;
646 /* Set the border color */
647 p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
648 p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
649 p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
652 /* Find the inner colors */
653 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
661 for( ; i < 4 && i_shade == -1 ; i++ )
665 if( stats[i] > stats[i_inner] )
677 /* Set the inner color */
680 p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
681 p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
682 p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
685 /* Set the anti-aliasing color */
688 p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
689 p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
690 p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
693 msg_Dbg( p_spudec->p_fifo,
694 "using custom palette (border %i, inner %i, shade %i)",
695 i_border, i_inner, i_shade );
701 /*****************************************************************************
702 * DestroySPU: subpicture destructor
703 *****************************************************************************/
704 static void DestroySPU( subpicture_t *p_spu )
706 if( p_spu->p_sys->p_input )
708 /* Detach from our input thread */
709 var_DelCallback( p_spu->p_sys->p_input, "highlight",
710 CropCallback, p_spu );
711 vlc_object_release( p_spu->p_sys->p_input );
714 vlc_mutex_destroy( &p_spu->p_sys->lock );
715 free( p_spu->p_sys );
718 /*****************************************************************************
719 * UpdateSPU: update subpicture settings
720 *****************************************************************************
721 * This function is called from CropCallback and at initialization time, to
722 * retrieve crop information from the input.
723 *****************************************************************************/
724 static void UpdateSPU( subpicture_t *p_spu, vlc_object_t *p_object )
728 if( var_Get( p_object, "highlight", &val ) )
733 p_spu->p_sys->b_crop = val.b_bool;
734 if( !p_spu->p_sys->b_crop )
739 var_Get( p_object, "x-start", &val );
740 p_spu->p_sys->i_x_start = val.i_int;
741 var_Get( p_object, "y-start", &val );
742 p_spu->p_sys->i_y_start = val.i_int;
743 var_Get( p_object, "x-end", &val );
744 p_spu->p_sys->i_x_end = val.i_int;
745 var_Get( p_object, "y-end", &val );
746 p_spu->p_sys->i_y_end = val.i_int;
749 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
751 p_spu->p_sys->pi_color[0] = ((uint8_t *)val.p_address)[0];
752 p_spu->p_sys->pi_color[1] = ((uint8_t *)val.p_address)[1];
753 p_spu->p_sys->pi_color[2] = ((uint8_t *)val.p_address)[2];
754 p_spu->p_sys->pi_color[3] = ((uint8_t *)val.p_address)[3];
758 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
760 p_spu->p_sys->pi_alpha[0] = ((uint8_t *)val.p_address)[0];
761 p_spu->p_sys->pi_alpha[1] = ((uint8_t *)val.p_address)[1];
762 p_spu->p_sys->pi_alpha[2] = ((uint8_t *)val.p_address)[2];
763 p_spu->p_sys->pi_alpha[3] = ((uint8_t *)val.p_address)[3];
767 /*****************************************************************************
768 * CropCallback: called when the highlight properties are changed
769 *****************************************************************************
770 * This callback is called from the input thread when we need cropping
771 *****************************************************************************/
772 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
773 vlc_value_t oldval, vlc_value_t newval, void *p_data )
775 UpdateSPU( (subpicture_t *)p_data, p_object );