1 /*****************************************************************************
2 * spudec.c : SPU decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: spudec.c,v 1.25 2003/09/02 20:19:26 gbazin 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>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int OpenDecoder ( vlc_object_t * );
40 static int RunDecoder ( decoder_fifo_t * );
41 static int InitThread ( spudec_thread_t * );
42 static void EndThread ( spudec_thread_t * );
43 static vout_thread_t *FindVout( spudec_thread_t *);
45 /*****************************************************************************
47 *****************************************************************************/
49 set_description( _("DVD subtitles decoder") );
50 set_capability( "decoder", 50 );
51 set_callbacks( OpenDecoder, NULL );
54 /*****************************************************************************
55 * OpenDecoder: probe the decoder and return score
56 *****************************************************************************
57 * Tries to launch a decoder and return score so that the interface is able
59 *****************************************************************************/
60 static int OpenDecoder( vlc_object_t *p_this )
62 decoder_t *p_dec = (decoder_t*)p_this;
64 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('s','p','u',' ')
65 && p_dec->p_fifo->i_fourcc != VLC_FOURCC('s','p','u','b') )
70 p_dec->pf_run = RunDecoder;
75 /*****************************************************************************
76 * RunDecoder: this function is called just after the thread is created
77 *****************************************************************************/
78 static int RunDecoder( decoder_fifo_t * p_fifo )
80 spudec_thread_t * p_spudec;
82 /* Allocate the memory needed to store the thread's structure */
83 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
85 if ( p_spudec == NULL )
87 msg_Err( p_fifo, "out of memory" );
88 DecoderError( p_fifo );
93 * Initialize the thread properties
95 p_spudec->p_vout = NULL;
96 p_spudec->p_fifo = p_fifo;
99 * Initialize thread and free configuration
101 p_spudec->p_fifo->b_error = InitThread( p_spudec );
104 * Main loop - it is not executed if an error occured during
107 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
109 if( E_(SyncPacket)( p_spudec ) )
114 /* Find/Wait for a video output */
115 p_spudec->p_vout = FindVout( p_spudec );
116 if( p_spudec->p_vout)
118 E_(ParsePacket)( p_spudec );
119 vlc_object_release( p_spudec->p_vout );
126 if( p_spudec->p_fifo->b_error )
128 DecoderError( p_spudec->p_fifo );
131 EndThread( p_spudec );
136 EndThread( p_spudec );
140 /* following functions are local */
142 /*****************************************************************************
143 * InitThread: initialize spu decoder thread
144 *****************************************************************************
145 * This function is called from RunThread and performs the second step of the
146 * initialization. It returns 0 on success. Note that the thread's flag are not
147 * modified inside this function.
148 *****************************************************************************/
149 static int InitThread( spudec_thread_t *p_spudec )
153 /* Call InitBitstream anyway so p_spudec->bit_stream is in a known
154 * state before calling CloseBitstream */
155 i_ret = InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo,
158 /* Check for a video output */
159 p_spudec->p_vout = FindVout( p_spudec );
161 if( !p_spudec->p_vout )
166 /* It was just a check */
167 vlc_object_release( p_spudec->p_vout );
168 p_spudec->p_vout = NULL;
173 /*****************************************************************************
174 * FindVout: Find a vout or wait for one to be created.
175 *****************************************************************************/
176 static vout_thread_t *FindVout( spudec_thread_t *p_spudec )
178 vout_thread_t *p_vout = NULL;
180 /* Find an available video output */
183 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
188 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
196 msleep( VOUT_OUTMEM_SLEEP );
203 /*****************************************************************************
204 * EndThread: thread destruction
205 *****************************************************************************
206 * This function is called when the thread ends after a sucessful
208 *****************************************************************************/
209 static void EndThread( spudec_thread_t *p_spudec )
211 if( p_spudec->p_vout != NULL
212 && p_spudec->p_vout->p_subpicture != NULL )
214 subpicture_t * p_subpic;
217 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
219 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
221 if( p_subpic != NULL &&
222 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
223 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
225 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
230 CloseBitstream( &p_spudec->bit_stream );