1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_subpictures.c,v 1.23 2003/12/09 19:15:03 yoann Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <stdio.h> /* sprintf() */
30 #include <string.h> /* strerror() */
34 #include "vlc_video.h"
35 #include "video_output.h"
38 * Display a subpicture unit
40 * Remove the reservation flag of a subpicture, which will cause it to be
42 * \param p_vout the video output this subpicture should be displayed on
43 * \param p_subpic the subpicture to display
45 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
49 /* Check if status is valid */
50 if( p_subpic->i_status != RESERVED_SUBPICTURE )
52 msg_Err( p_vout, "subpicture %p has invalid status #%d",
53 p_subpic, p_subpic->i_status );
56 /* If the user requested an SPU margin, we force the position after
57 * having checked that it was a valid value. */
58 i_margin = config_GetInt( p_vout, "spumargin" );
62 if( p_subpic->i_height + (unsigned int)i_margin
63 <= p_vout->output.i_height )
65 p_subpic->i_y = p_vout->output.i_height
66 - i_margin - p_subpic->i_height;
70 /* Remove reservation flag */
71 p_subpic->i_status = READY_SUBPICTURE;
75 * Allocate a subpicture in the video output heap.
77 * This function create a reserved subpicture in the video output heap.
78 * A null pointer is returned if the function fails. This method provides an
79 * already allocated zone of memory in the spu data fields. It needs locking
80 * since several pictures can be created by several producers threads.
81 * \param p_vout the vout in which to create the subpicture
82 * \param i_type the type of the subpicture
83 * \return NULL on error, a reserved subpicture otherwise
85 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type )
87 int i_subpic; /* subpicture index */
88 subpicture_t * p_subpic = NULL; /* first free subpicture */
91 vlc_mutex_lock( &p_vout->subpicture_lock );
94 * Look for an empty place
96 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
98 if( p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
100 /* Subpicture is empty and ready for allocation */
101 p_subpic = &p_vout->p_subpicture[i_subpic];
102 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
107 /* If no free subpicture could be found */
108 if( p_subpic == NULL )
110 msg_Err( p_vout, "subpicture heap is full" );
111 vlc_mutex_unlock( &p_vout->subpicture_lock );
115 /* Copy subpicture information, set some default values */
116 p_subpic->i_type = i_type;
117 p_subpic->i_status = RESERVED_SUBPICTURE;
119 p_subpic->i_start = 0;
120 p_subpic->i_stop = 0;
121 p_subpic->b_ephemer = VLC_FALSE;
125 p_subpic->i_width = 0;
126 p_subpic->i_height = 0;
128 vlc_mutex_unlock( &p_vout->subpicture_lock );
134 * Remove a subpicture from the heap
136 * This function frees a previously reserved subpicture.
137 * It is meant to be used when the construction of a picture aborted.
138 * This function does not need locking since reserved subpictures are ignored
139 * by the output thread.
141 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
144 vlc_mutex_lock( &p_vout->subpicture_lock );
146 /* There can be race conditions so we need to check the status */
147 if( p_subpic->i_status == FREE_SUBPICTURE )
149 vlc_mutex_unlock( &p_vout->subpicture_lock );
153 /* Check if status is valid */
154 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
155 && ( p_subpic->i_status != READY_SUBPICTURE ) )
157 msg_Err( p_vout, "subpicture %p has invalid status %d",
158 p_subpic, p_subpic->i_status );
161 if( p_subpic->pf_destroy )
163 p_subpic->pf_destroy( p_subpic );
166 if( p_subpic == p_vout->p_last_osd_message )
168 p_vout->p_last_osd_message = NULL;
171 p_subpic->i_status = FREE_SUBPICTURE;
173 vlc_mutex_unlock( &p_vout->subpicture_lock );
176 /*****************************************************************************
177 * vout_RenderSubPictures: render a subpicture list
178 *****************************************************************************
179 * This function renders all sub picture units in the list.
180 *****************************************************************************/
181 void vout_RenderSubPictures( vout_thread_t *p_vout, picture_t *p_pic,
182 subpicture_t *p_subpic )
185 vlc_mutex_lock( &p_vout->subpicture_lock );
187 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
188 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
190 p_subpic->pf_render( p_vout, p_pic, p_subpic );
191 p_subpic = p_subpic->p_next;
194 vlc_mutex_unlock( &p_vout->subpicture_lock );
197 /*****************************************************************************
198 * vout_SortSubPictures: find the subpictures to display
199 *****************************************************************************
200 * This function parses all subpictures and decides which ones need to be
201 * displayed. This operation does not need lock, since only READY_SUBPICTURE
202 * are handled. If no picture has been selected, display_date will depend on
204 * We also check for ephemer DVD subpictures (subpictures that have
205 * to be removed if a newer one is available), which makes it a lot
206 * more difficult to guess if a subpicture has to be rendered or not.
207 *****************************************************************************/
208 subpicture_t *vout_SortSubPictures( vout_thread_t *p_vout,
209 mtime_t display_date )
212 subpicture_t *p_subpic = NULL;
213 subpicture_t *p_ephemer = NULL;
214 mtime_t ephemer_date = 0;
216 /* We get an easily parsable chained list of subpictures which
217 * ends with NULL since p_subpic was initialized to NULL. */
218 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
220 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
222 /* If it is a DVD subpicture, check its date */
223 if( p_vout->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
225 if( !p_vout->p_subpicture[i_index].b_ephemer
226 && display_date > p_vout->p_subpicture[i_index].i_stop )
228 /* Too late, destroy the subpic */
229 vout_DestroySubPicture( p_vout,
230 &p_vout->p_subpicture[i_index] );
235 && display_date < p_vout->p_subpicture[i_index].i_start )
237 /* Too early, come back next monday */
241 /* If this is an ephemer subpic, see if it's the
242 * youngest we have */
243 if( p_vout->p_subpicture[i_index].b_ephemer )
245 if( p_ephemer == NULL )
247 p_ephemer = &p_vout->p_subpicture[i_index];
251 if( p_vout->p_subpicture[i_index].i_start
252 < p_ephemer->i_start )
254 /* Link the previous ephemer subpicture and
255 * replace it with the current one */
256 p_ephemer->p_next = p_subpic;
257 p_subpic = p_ephemer;
258 p_ephemer = &p_vout->p_subpicture[i_index];
260 /* If it's the 2nd youngest subpicture,
261 * register its date */
263 || ephemer_date > p_subpic->i_start )
265 ephemer_date = p_subpic->i_start;
272 p_vout->p_subpicture[i_index].p_next = p_subpic;
273 p_subpic = &p_vout->p_subpicture[i_index];
275 /* If it's the 2nd youngest subpicture, register its date */ if( !ephemer_date || ephemer_date > p_subpic->i_start )
277 ephemer_date = p_subpic->i_start;
280 /* If it's not a DVD subpicture, just register it */
283 p_vout->p_subpicture[i_index].p_next = p_subpic;
284 p_subpic = &p_vout->p_subpicture[i_index];
289 /* If we found an ephemer subpicture, check if it has to be
291 if( p_ephemer != NULL )
293 if( p_ephemer->i_start < ephemer_date )
295 /* Ephemer subpicture has lived too long */
296 vout_DestroySubPicture( p_vout, p_ephemer );
300 /* Ephemer subpicture can still live a bit */
301 p_ephemer->p_next = p_subpic;