1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: parse.c,v 1.1 2002/08/16 03:07:56 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 ParseControlSequences( spudec_thread_t *, subpicture_t * );
48 static int ParseRLE ( spudec_thread_t *, subpicture_t *, u8 * );
50 /*****************************************************************************
51 * AddNibble: read a nibble from a source packet and add it to our integer.
52 *****************************************************************************/
53 static inline unsigned int AddNibble( unsigned int i_code,
54 u8 *p_src, int *pi_index )
58 return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
62 return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
66 /*****************************************************************************
67 * SyncPacket: get in sync with the stream
68 *****************************************************************************
69 * This function makes a few sanity checks and returns 0 if it looks like we
70 * are at the beginning of a subpicture packet.
71 *****************************************************************************/
72 int E_(SyncPacket)( spudec_thread_t *p_spudec )
74 /* Re-align the buffer on an 8-bit boundary */
75 RealignBits( &p_spudec->bit_stream );
77 /* The total SPU packet size, often bigger than a PS packet */
78 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
80 /* The RLE stuff size (remove 4 because we just read 32 bits) */
81 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
83 /* If the values we got are a bit strange, skip packet */
84 if( !p_spudec->i_spu_size
85 || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
90 RemoveBits( &p_spudec->bit_stream, 16 );
95 /*****************************************************************************
96 * ParsePacket: parse an SPU packet and send it to the video output
97 *****************************************************************************
98 * This function parses the SPU packet and, if valid, sends it to the
100 *****************************************************************************/
101 void E_(ParsePacket)( spudec_thread_t *p_spudec )
103 subpicture_t * p_spu;
105 unsigned int i_offset;
107 msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
108 p_spudec->i_spu_size );
110 /* We cannot display a subpicture with no date */
111 if( p_spudec->p_fifo->p_first->i_pts == 0 )
113 msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
117 /* Allocate the subpicture internal data. */
118 p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE,
119 sizeof( subpicture_sys_t )
120 + p_spudec->i_rle_size * 4 );
121 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
122 * expand the RLE stuff so that we won't need to read nibbles later
123 * on. This will speed things up a lot. Plus, we'll only need to do
124 * this stupid interlacing stuff once. */
131 /* Fill the p_spu structure */
132 p_spu->pf_render = E_(RenderSPU);
133 p_spu->p_sys->p_data = (u8*)p_spu->p_sys + sizeof( subpicture_sys_t );
134 p_spu->p_sys->b_palette = 0;
136 /* Get display time now. If we do it later, we may miss the PTS. */
137 p_spu->p_sys->i_pts = p_spudec->p_fifo->p_first->i_pts;
139 /* Allocate the temporary buffer we will parse */
140 p_src = malloc( p_spudec->i_rle_size );
144 msg_Err( p_spudec->p_fifo, "out of memory" );
145 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
150 for( i_offset = 0; i_offset < p_spudec->i_rle_size;
151 i_offset += SPU_CHUNK_SIZE )
153 GetChunk( &p_spudec->bit_stream, p_src + i_offset,
154 ( i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size ) ?
155 SPU_CHUNK_SIZE : p_spudec->i_rle_size - i_offset );
157 /* Abort subtitle parsing if we were requested to stop */
158 if( p_spudec->p_fifo->b_die )
161 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
167 /* Dump the subtitle info */
168 intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
171 /* Getting the control part */
172 if( ParseControlSequences( p_spudec, p_spu ) )
174 /* There was a parse error, delete the subpicture */
176 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
180 /* At this point, no more GetBit() command is needed, so we have all
181 * the data we need to tell whether the subtitle is valid. Thus we
182 * try to display it and we ignore b_die. */
184 if( ParseRLE( p_spudec, p_spu, p_src ) )
186 /* There was a parse error, delete the subpicture */
188 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
192 msg_Dbg( p_spudec->p_fifo, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
193 p_spudec->i_spu_size,
194 p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
196 /* SPU is finished - we can ask the video output to display it */
197 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
203 /*****************************************************************************
204 * ParseControlSequences: parse all SPU control sequences
205 *****************************************************************************
206 * This is the most important part in SPU decoding. We get dates, palette
207 * information, coordinates, and so on. For more information on the
208 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
209 *****************************************************************************/
210 static int ParseControlSequences( spudec_thread_t *p_spudec,
211 subpicture_t * p_spu )
213 /* Our current index in the SPU packet */
214 int i_index = p_spudec->i_rle_size + 4;
216 /* The next start-of-control-sequence index and the previous one */
217 int i_next_seq, i_cur_seq;
219 /* Command time and date */
225 /* XXX: temporary variables */
226 vlc_bool_t b_force_display = 0;
228 /* Initialize the structure */
229 p_spu->i_start = p_spu->i_stop = 0;
230 p_spu->b_ephemer = 0;
234 /* Get the control sequence date */
235 i_date = GetBits( &p_spudec->bit_stream, 16 );
239 i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
241 /* Skip what we just read */
246 i_command = GetBits( &p_spudec->bit_stream, 8 );
251 case SPU_CMD_FORCE_DISPLAY:
253 /* 00 (force displaying) */
254 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
259 /* Convert the dates in seconds to PTS values */
260 case SPU_CMD_START_DISPLAY:
262 /* 01 (start displaying) */
263 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
267 case SPU_CMD_STOP_DISPLAY:
269 /* 02 (stop displaying) */
270 p_spu->i_stop = p_spu->p_sys->i_pts + ( i_date * 11000 );
274 case SPU_CMD_SET_PALETTE:
276 /* 03xxxx (palette) */
277 if( p_spudec->p_fifo->p_demux_data &&
278 *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
282 p_spu->p_sys->b_palette = 1;
283 for( i = 0; i < 4 ; i++ )
285 i_color = ((u32*)((char*)p_spudec->p_fifo->
286 p_demux_data + sizeof(int)))[
287 GetBits(&p_spudec->bit_stream, 4) ];
289 /* FIXME: this job should be done sooner */
290 #ifndef WORDS_BIGENDIAN
291 p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
292 p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
293 p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
295 p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>8) & 0xff;
296 p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>24) & 0xff;
297 p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>16) & 0xff;
303 RemoveBits( &p_spudec->bit_stream, 16 );
309 case SPU_CMD_SET_ALPHACHANNEL:
311 /* 04xxxx (alpha channel) */
312 pi_alpha[3] = GetBits( &p_spudec->bit_stream, 4 );
313 pi_alpha[2] = GetBits( &p_spudec->bit_stream, 4 );
314 pi_alpha[1] = GetBits( &p_spudec->bit_stream, 4 );
315 pi_alpha[0] = GetBits( &p_spudec->bit_stream, 4 );
317 /* Ignore blank alpha palette. Sometimes spurious blank
318 * alpha palettes are present - dunno why. */
319 if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
321 p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
322 p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
323 p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
324 p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
328 msg_Warn( p_spudec->p_fifo,
329 "ignoring blank alpha palette" );
336 case SPU_CMD_SET_COORDINATES:
338 /* 05xxxyyyxxxyyy (coordinates) */
339 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
340 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
343 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
344 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
351 case SPU_CMD_SET_OFFSETS:
353 /* 06xxxxyyyy (byte offsets) */
354 p_spu->p_sys->pi_offset[0] =
355 GetBits( &p_spudec->bit_stream, 16 ) - 4;
357 p_spu->p_sys->pi_offset[1] =
358 GetBits( &p_spudec->bit_stream, 16 ) - 4;
371 /* xx (unknown command) */
372 msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
377 /* We need to check for quit commands here */
378 if( p_spudec->p_fifo->b_die )
383 } while( i_command != SPU_CMD_END );
385 } while( i_index == i_next_seq );
387 /* Check that the next sequence index matches the current one */
388 if( i_next_seq != i_cur_seq )
390 msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
391 i_next_seq, i_cur_seq );
395 if( i_index > p_spudec->i_spu_size )
397 msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
398 i_index, p_spudec->i_spu_size );
402 if( !p_spu->i_start )
404 msg_Err( p_spudec->p_fifo, "no `start display' command" );
409 /* This subtitle will live for 5 seconds or until the next subtitle */
410 p_spu->i_stop = p_spu->i_start + 500 * 11000;
411 p_spu->b_ephemer = 1;
414 /* Get rid of padding bytes */
415 switch( p_spudec->i_spu_size - i_index )
417 /* Zero or one padding byte, quite usual */
419 RemoveBits( &p_spudec->bit_stream, 8 );
424 /* More than one padding byte - this is very strange, but
425 * we can deal with it */
427 msg_Warn( p_spudec->p_fifo,
428 "%i padding bytes, we usually get 0 or 1 of them",
429 p_spudec->i_spu_size - i_index );
431 while( i_index < p_spudec->i_spu_size )
433 RemoveBits( &p_spudec->bit_stream, 8 );
440 if( b_force_display )
442 msg_Err( p_spudec->p_fifo, "\"force display\" command" );
443 msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
444 "want to help debugging this" );
447 /* Successfully parsed ! */
451 /*****************************************************************************
452 * ParseRLE: parse the RLE part of the subtitle
453 *****************************************************************************
454 * This part parses the subtitle graphical data and stores it in a more
455 * convenient structure for later decoding. For more information on the
456 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
457 *****************************************************************************/
458 static int ParseRLE( spudec_thread_t *p_spudec,
459 subpicture_t * p_spu, u8 * p_src )
463 unsigned int i_width = p_spu->i_width;
464 unsigned int i_height = p_spu->i_height;
465 unsigned int i_x, i_y;
467 u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
469 /* The subtitles are interlaced, we need two offsets */
470 unsigned int i_id = 0; /* Start on the even SPU layer */
471 unsigned int pi_table[ 2 ];
472 unsigned int *pi_offset;
474 vlc_bool_t b_empty_top = 1,
476 unsigned int i_skipped_top = 0,
477 i_skipped_bottom = 0;
479 /* Colormap statistics */
481 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
483 pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
484 pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
486 for( i_y = 0 ; i_y < i_height ; i_y++ )
488 pi_offset = pi_table + i_id;
490 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
492 i_code = AddNibble( 0, p_src, pi_offset );
496 i_code = AddNibble( i_code, p_src, pi_offset );
500 i_code = AddNibble( i_code, p_src, pi_offset );
504 i_code = AddNibble( i_code, p_src, pi_offset );
506 if( i_code < 0x0100 )
508 /* If the 14 first bits are set to 0, then it's a
509 * new line. We emulate it. */
510 if( i_code < 0x0004 )
512 i_code |= ( i_width - i_x ) << 2;
516 /* We have a boo boo ! */
517 msg_Err( p_spudec->p_fifo, "unknown RLE code "
526 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
528 msg_Err( p_spudec->p_fifo,
529 "out of bounds, %i at (%i,%i) is out of %ix%i",
530 i_code >> 2, i_x, i_y, i_width, i_height );
534 /* Try to find the border color */
535 if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
537 i_border = i_code & 0x3;
538 stats[i_border] += i_code >> 2;
541 if( (i_code >> 2) == i_width
542 && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
546 /* This is a blank top line, we skip it */
551 /* We can't be sure the current lines will be skipped,
552 * so we store the code just in case. */
561 /* We got a valid code, store it */
564 /* Valid code means no blank line */
567 i_skipped_bottom = 0;
571 /* Check that we didn't go too far */
574 msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
579 /* Byte-align the stream */
580 if( *pi_offset & 0x1 )
589 /* We shouldn't get any padding bytes */
592 msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
593 msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
594 "want to help debugging this" );
596 /* Skip them just in case */
597 while( i_y < i_height )
599 *p_dest++ = i_width << 2;
606 msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
607 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
609 /* Crop if necessary */
610 if( i_skipped_top || i_skipped_bottom )
612 p_spu->i_y += i_skipped_top;
613 p_spu->i_height -= i_skipped_top + i_skipped_bottom;
615 msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
616 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
619 /* Handle color if no palette was found */
620 if( !p_spu->p_sys->b_palette )
622 int i, i_inner = -1, i_shade = -1;
624 /* Set the border color */
625 p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
626 p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
627 p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
630 /* Find the inner colors */
631 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
639 for( ; i < 4 && i_shade == -1 ; i++ )
643 if( stats[i] > stats[i_inner] )
655 /* Set the inner color */
658 p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
659 p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
660 p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
663 /* Set the anti-aliasing color */
666 p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
667 p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
668 p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
671 msg_Dbg( p_spudec->p_fifo,
672 "using custom palette (border %i, inner %i, shade %i)",
673 i_border, i_inner, i_shade );