1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: spu_decoder.c,v 1.39 2001/05/01 12:22:18 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 *****************************************************************************/
29 #include <unistd.h> /* getpid() */
30 #ifdef WIN32 /* getpid() for win32 is located in process.h */
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* memcpy(), memset() */
44 #include "stream_control.h"
45 #include "input_ext-dec.h"
48 #include "video_output.h"
50 #include "spu_decoder.h"
52 #include "main.h" /* XXX: remove this later */
54 /*****************************************************************************
56 *****************************************************************************/
57 static int InitThread ( spudec_thread_t * );
58 static void RunThread ( spudec_thread_t * );
59 static void ErrorThread ( spudec_thread_t * );
60 static void EndThread ( spudec_thread_t * );
62 static int SyncPacket ( spudec_thread_t * );
63 static void ParsePacket ( spudec_thread_t * );
64 static int ParseControlSequences( spudec_thread_t *, subpicture_t * );
65 static int ParseRLE ( u8 *, subpicture_t * );
67 /*****************************************************************************
68 * spudec_CreateThread: create a spu decoder thread
69 *****************************************************************************/
70 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
72 spudec_thread_t * p_spudec;
74 /* Allocate the memory needed to store the thread's structure */
75 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
77 if ( p_spudec == NULL )
79 intf_ErrMsg( "spudec error: not enough memory "
80 "for spudec_CreateThread() to create the new thread" );
85 * Initialize the thread properties
87 p_spudec->p_config = p_config;
89 p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
91 /* XXX: The vout request and fifo opening will eventually be here */
92 if( p_spudec->p_vout == NULL )
94 if( p_main->p_vout == NULL )
96 intf_Msg( "vpar: no vout present, spawning one" );
97 p_main->p_vout = vout_CreateThread( NULL );
100 p_spudec->p_vout = p_main->p_vout;
103 /* Spawn the spu decoder thread */
104 if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
105 (vlc_thread_func_t)RunThread, (void *)p_spudec) )
107 intf_ErrMsg( "spudec error: can't spawn spu decoder thread" );
112 return( p_spudec->thread_id );
115 /* following functions are local */
117 /*****************************************************************************
118 * InitThread: initialize spu decoder thread
119 *****************************************************************************
120 * This function is called from RunThread and performs the second step of the
121 * initialization. It returns 0 on success. Note that the thread's flag are not
122 * modified inside this function.
123 *****************************************************************************/
124 static int InitThread( spudec_thread_t *p_spudec )
126 p_spudec->p_config->decoder_config.pf_init_bit_stream(
127 &p_spudec->bit_stream,
128 p_spudec->p_config->decoder_config.p_decoder_fifo, NULL, NULL );
130 /* Mark thread as running and return */
134 /*****************************************************************************
135 * RunThread: spu decoder thread
136 *****************************************************************************
137 * spu decoder thread. This function only returns when the thread is
139 *****************************************************************************/
140 static void RunThread( spudec_thread_t *p_spudec )
142 intf_WarnMsg( 1, "spudec: spu decoder thread %i spawned", getpid() );
145 * Initialize thread and free configuration
147 p_spudec->p_fifo->b_error = InitThread( p_spudec );
150 * Main loop - it is not executed if an error occured during
153 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
155 if( !SyncPacket( p_spudec ) )
157 ParsePacket( p_spudec );
164 if( p_spudec->p_fifo->b_error )
166 ErrorThread( p_spudec );
170 intf_WarnMsg( 1, "spudec: destroying spu decoder thread %i", getpid() );
171 EndThread( p_spudec );
174 /*****************************************************************************
175 * ErrorThread: RunThread() error loop
176 *****************************************************************************
177 * This function is called when an error occured during thread main's loop. The
178 * thread can still receive feed, but must be ready to terminate as soon as
180 *****************************************************************************/
181 static void ErrorThread( spudec_thread_t *p_spudec )
183 /* We take the lock, because we are going to read/write the start/end
184 * indexes of the decoder fifo */
185 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
187 /* Wait until a `die' order is sent */
188 while( !p_spudec->p_fifo->b_die )
190 /* Trash all received PES packets */
191 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
193 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
194 DECODER_FIFO_START(*p_spudec->p_fifo) );
195 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
198 /* Waiting for the input thread to put new PES packets in the fifo */
199 vlc_cond_wait( &p_spudec->p_fifo->data_wait,
200 &p_spudec->p_fifo->data_lock );
203 /* We can release the lock before leaving */
204 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
207 /*****************************************************************************
208 * EndThread: thread destruction
209 *****************************************************************************
210 * This function is called when the thread ends after a sucessful
212 *****************************************************************************/
213 static void EndThread( spudec_thread_t *p_spudec )
215 free( p_spudec->p_config );
219 /*****************************************************************************
220 * SyncPacket: get in sync with the stream
221 *****************************************************************************
222 * This function makes a few sanity checks and returns 0 if it looks like we
223 * are at the beginning of a subpicture packet.
224 *****************************************************************************/
225 static int SyncPacket( spudec_thread_t *p_spudec )
227 /* Re-align the buffer on an 8-bit boundary */
228 RealignBits( &p_spudec->bit_stream );
230 /* The total SPU packet size, often bigger than a PS packet */
231 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
233 /* The RLE stuff size (remove 4 because we just read 32 bits) */
234 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
236 /* If the values we got are a bit strange, skip packet */
237 if( p_spudec->i_rle_size >= p_spudec->i_spu_size )
242 RemoveBits( &p_spudec->bit_stream, 16 );
247 /*****************************************************************************
248 * ParsePacket: parse an SPU packet and send it to the video output
249 *****************************************************************************
250 * This function parses the SPU packet and, if valid, sends it to the
252 *****************************************************************************/
253 static void ParsePacket( spudec_thread_t *p_spudec )
255 subpicture_t * p_spu;
258 /* We cannot display a subpicture with no date */
259 if( DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts == 0 )
264 /* Allocate the subpicture internal data. */
265 p_spu = vout_CreateSubPicture( p_spudec->p_vout, DVD_SUBPICTURE,
266 p_spudec->i_rle_size * 4 );
267 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
268 * expand the RLE stuff so that we won't need to read nibbles later
269 * on. This will speed things up a lot. Plus, we'll only need to do
270 * this stupid interlacing stuff once. */
277 /* Get display time now. If we do it later, we may miss a PTS. */
278 p_spu->begin_date = p_spu->end_date
279 = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
281 /* Allocate the temporary buffer we will parse */
282 p_src = malloc( p_spudec->i_rle_size );
286 intf_ErrMsg( "spudec error: could not allocate p_src" );
287 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
292 GetChunk( &p_spudec->bit_stream, p_src, p_spudec->i_rle_size );
295 /* Dump the subtitle info */
296 intf_WarnHexDump( 0, p_spu->p_data, p_spudec->i_rle_size );
299 /* Getting the control part */
300 if( ParseControlSequences( p_spudec, p_spu ) )
302 /* There was a parse error, delete the subpicture */
304 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
308 if( ParseRLE( p_src, p_spu ) )
310 /* There was a parse error, delete the subpicture */
312 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
316 intf_WarnMsg( 1, "spudec: got a valid %ix%i subtitle at (%i,%i), "
317 "RLE offsets: 0x%x 0x%x",
318 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y,
319 p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
321 /* SPU is finished - we can ask the video output to display it */
322 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
328 /*****************************************************************************
329 * ParseControlSequences: parse all SPU control sequences
330 *****************************************************************************
331 * This is the most important part in SPU decoding. We get dates, palette
332 * information, coordinates, and so on. For more information on the
333 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
334 *****************************************************************************/
335 static int ParseControlSequences( spudec_thread_t *p_spudec,
336 subpicture_t * p_spu )
338 /* Our current index in the SPU packet */
339 int i_index = p_spudec->i_rle_size + 4;
341 /* The next start-of-control-sequence index and the previous one */
342 int i_next_index = 0, i_prev_index;
344 /* Command time and date */
350 /* Get the control sequence date */
351 i_date = GetBits( &p_spudec->bit_stream, 16 );
354 i_prev_index = i_next_index;
355 i_next_index = GetBits( &p_spudec->bit_stream, 16 );
357 /* Skip what we just read */
362 i_command = GetBits( &p_spudec->bit_stream, 8 );
367 case SPU_CMD_FORCE_DISPLAY:
369 /* 00 (force displaying) */
372 /* Convert the dates in seconds to PTS values */
373 case SPU_CMD_START_DISPLAY:
375 /* 01 (start displaying) */
376 p_spu->begin_date += ( i_date * 11000 );
380 case SPU_CMD_STOP_DISPLAY:
382 /* 02 (stop displaying) */
383 p_spu->end_date += ( i_date * 11000 );
387 case SPU_CMD_SET_PALETTE:
389 /* 03xxxx (palette) - trashed */
390 RemoveBits( &p_spudec->bit_stream, 16 );
395 case SPU_CMD_SET_ALPHACHANNEL:
397 /* 04xxxx (alpha channel) - trashed */
398 RemoveBits( &p_spudec->bit_stream, 16 );
403 case SPU_CMD_SET_COORDINATES:
405 /* 05xxxyyyxxxyyy (coordinates) */
406 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
407 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
410 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
411 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
418 case SPU_CMD_SET_OFFSETS:
420 /* 06xxxxyyyy (byte offsets) */
421 p_spu->type.spu.i_offset[0] =
422 GetBits( &p_spudec->bit_stream, 16 ) - 4;
424 p_spu->type.spu.i_offset[1] =
425 GetBits( &p_spudec->bit_stream, 16 ) - 4;
438 /* xx (unknown command) */
439 intf_ErrMsg( "spudec error: unknown command 0x%.2x",
444 } while( i_command != SPU_CMD_END );
446 } while( i_index == i_next_index );
448 /* Check that the last index matches the previous one */
449 if( i_next_index != i_prev_index )
451 intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
452 i_next_index, i_prev_index );
456 if( i_index > p_spudec->i_spu_size )
458 intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
459 i_index, p_spudec->i_spu_size );
463 /* Get rid of padding bytes */
464 switch( p_spudec->i_spu_size - i_index )
468 RemoveBits( &p_spudec->bit_stream, 8 );
473 /* Zero or one padding byte, quite usual */
479 /* More than one padding byte - this is very strange, but
480 * we can deal with it */
481 intf_WarnMsg( 2, "spudec warning: %i padding bytes, we usually "
483 p_spudec->i_spu_size - i_index );
485 while( i_index < p_spudec->i_spu_size )
487 RemoveBits( &p_spudec->bit_stream, 8 );
494 /* Successfully parsed ! */
498 /*****************************************************************************
499 * ParseRLE: parse the RLE part of the subtitle
500 *****************************************************************************
501 * This part parses the subtitle graphical data and stores it in a more
502 * convenient structure for later decoding. For more information on the
503 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
504 *****************************************************************************/
505 static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
508 unsigned int i_id = 0;
510 unsigned int i_width = p_spu->i_width;
511 unsigned int i_height = p_spu->i_height;
512 unsigned int i_x, i_y;
514 u16 *p_dest = (u16 *)p_spu->p_data;
516 /* The subtitles are interlaced, we need two offsets */
517 unsigned int pi_table[ 2 ];
518 unsigned int *pi_offset;
520 pi_table[ 0 ] = p_spu->type.spu.i_offset[ 0 ] << 1;
521 pi_table[ 1 ] = p_spu->type.spu.i_offset[ 1 ] << 1;
523 for( i_y = 0 ; i_y < i_height ; i_y++ )
525 pi_offset = pi_table + i_id;
527 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
529 i_code = AddNibble( 0, p_src, pi_offset );
533 i_code = AddNibble( i_code, p_src, pi_offset );
537 i_code = AddNibble( i_code, p_src, pi_offset );
541 i_code = AddNibble( i_code, p_src, pi_offset );
543 if( i_code < 0x0100 )
545 /* If the 14 first bits are set to 0, then it's a
546 * new line. We emulate it. */
547 if( i_code < 0x0004 )
549 i_code |= ( i_width - i_x ) << 2;
553 /* We have a boo boo ! */
554 intf_ErrMsg( "spudec error: unknown RLE code "
563 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
565 intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
567 i_code >> 2, i_x, i_y, i_width, i_height);
571 /* We got a valid code, store it */
575 /* Check that we didn't go too far */
578 intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
583 /* Byte-align the stream */
584 if( *pi_offset & 0x1 )
593 /* FIXME: we shouldn't need these padding bytes */
594 while( i_y < i_height )
596 *p_dest++ = i_width << 2;