1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_subpictures.c,v 1.20 2003/06/26 12:19:59 sam 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"
37 /*****************************************************************************
38 * vout_DisplaySubPicture: display a subpicture unit
39 *****************************************************************************
40 * Remove the reservation flag of a subpicture, which will cause it to be
42 *****************************************************************************/
43 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
47 /* Check if status is valid */
48 if( p_subpic->i_status != RESERVED_SUBPICTURE )
50 msg_Err( p_vout, "subpicture %p has invalid status #%d",
51 p_subpic, p_subpic->i_status );
54 /* If the user requested an SPU margin, we force the position after
55 * having checked that it was a valid value. */
56 i_margin = config_GetInt( p_vout, "spumargin" );
60 if( p_subpic->i_height + (unsigned int)i_margin
61 <= p_vout->output.i_height )
63 p_subpic->i_y = p_vout->output.i_height
64 - i_margin - p_subpic->i_height;
68 /* Remove reservation flag */
69 p_subpic->i_status = READY_SUBPICTURE;
72 /*****************************************************************************
73 * vout_CreateSubPicture: allocate a subpicture in the video output heap.
74 *****************************************************************************
75 * This function create a reserved subpicture in the video output heap.
76 * A null pointer is returned if the function fails. This method provides an
77 * already allocated zone of memory in the spu data fields. It needs locking
78 * since several pictures can be created by several producers threads.
79 *****************************************************************************/
80 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type )
82 int i_subpic; /* subpicture index */
83 subpicture_t * p_subpic = NULL; /* first free subpicture */
86 vlc_mutex_lock( &p_vout->subpicture_lock );
89 * Look for an empty place
91 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
93 if( p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
95 /* Subpicture is empty and ready for allocation */
96 p_subpic = &p_vout->p_subpicture[i_subpic];
97 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
102 /* If no free subpicture could be found */
103 if( p_subpic == NULL )
105 msg_Err( p_vout, "subpicture heap is full" );
106 vlc_mutex_unlock( &p_vout->subpicture_lock );
110 /* Copy subpicture information, set some default values */
111 p_subpic->i_type = i_type;
112 p_subpic->i_status = RESERVED_SUBPICTURE;
114 p_subpic->i_start = 0;
115 p_subpic->i_stop = 0;
116 p_subpic->b_ephemer = VLC_FALSE;
120 p_subpic->i_width = 0;
121 p_subpic->i_height = 0;
123 vlc_mutex_unlock( &p_vout->subpicture_lock );
128 /*****************************************************************************
129 * vout_DestroySubPicture: remove a subpicture from the heap
130 *****************************************************************************
131 * This function frees a previously reserved subpicture.
132 * It is meant to be used when the construction of a picture aborted.
133 * This function does not need locking since reserved subpictures are ignored
134 * by the output thread.
135 *****************************************************************************/
136 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
139 vlc_mutex_lock( &p_vout->subpicture_lock );
141 /* There can be race conditions so we need to check the status */
142 if( p_subpic->i_status == FREE_SUBPICTURE )
144 vlc_mutex_unlock( &p_vout->subpicture_lock );
148 /* Check if status is valid */
149 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
150 && ( p_subpic->i_status != READY_SUBPICTURE ) )
152 msg_Err( p_vout, "subpicture %p has invalid status %d",
153 p_subpic, p_subpic->i_status );
156 if( p_subpic->pf_destroy )
158 p_subpic->pf_destroy( p_subpic );
161 p_subpic->i_status = FREE_SUBPICTURE;
163 vlc_mutex_unlock( &p_vout->subpicture_lock );
166 /*****************************************************************************
167 * vout_RenderSubPictures: render a subpicture list
168 *****************************************************************************
169 * This function renders all sub picture units in the list.
170 *****************************************************************************/
171 void vout_RenderSubPictures( vout_thread_t *p_vout, picture_t *p_pic,
172 subpicture_t *p_subpic )
175 vlc_mutex_lock( &p_vout->subpicture_lock );
177 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
178 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
180 p_subpic->pf_render( p_vout, p_pic, p_subpic );
181 p_subpic = p_subpic->p_next;
184 vlc_mutex_unlock( &p_vout->subpicture_lock );
187 /*****************************************************************************
188 * vout_SortSubPictures: find the subpictures to display
189 *****************************************************************************
190 * This function parses all subpictures and decides which ones need to be
191 * displayed. This operation does not need lock, since only READY_SUBPICTURE
192 * are handled. If no picture has been selected, display_date will depend on
194 * We also check for ephemer DVD subpictures (subpictures that have
195 * to be removed if a newer one is available), which makes it a lot
196 * more difficult to guess if a subpicture has to be rendered or not.
197 *****************************************************************************/
198 subpicture_t *vout_SortSubPictures( vout_thread_t *p_vout,
199 mtime_t display_date )
202 subpicture_t *p_subpic = NULL;
203 subpicture_t *p_ephemer = NULL;
204 mtime_t ephemer_date = 0;
206 /* We get an easily parsable chained list of subpictures which
207 * ends with NULL since p_subpic was initialized to NULL. */
208 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
210 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
212 /* If it is a DVD subpicture, check its date */
213 if( p_vout->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
215 if( !p_vout->p_subpicture[i_index].b_ephemer
216 && display_date > p_vout->p_subpicture[i_index].i_stop )
218 /* Too late, destroy the subpic */
219 vout_DestroySubPicture( p_vout,
220 &p_vout->p_subpicture[i_index] );
225 && display_date < p_vout->p_subpicture[i_index].i_start )
227 /* Too early, come back next monday */
231 /* If this is an ephemer subpic, see if it's the
232 * youngest we have */
233 if( p_vout->p_subpicture[i_index].b_ephemer )
235 if( p_ephemer == NULL )
237 p_ephemer = &p_vout->p_subpicture[i_index];
241 if( p_vout->p_subpicture[i_index].i_start
242 < p_ephemer->i_start )
244 /* Link the previous ephemer subpicture and
245 * replace it with the current one */
246 p_ephemer->p_next = p_subpic;
247 p_subpic = p_ephemer;
248 p_ephemer = &p_vout->p_subpicture[i_index];
250 /* If it's the 2nd youngest subpicture,
251 * register its date */
253 || ephemer_date > p_subpic->i_start )
255 ephemer_date = p_subpic->i_start;
262 p_vout->p_subpicture[i_index].p_next = p_subpic;
263 p_subpic = &p_vout->p_subpicture[i_index];
265 /* If it's the 2nd youngest subpicture, register its date */ if( !ephemer_date || ephemer_date > p_subpic->i_start )
267 ephemer_date = p_subpic->i_start;
270 /* If it's not a DVD subpicture, just register it */
273 p_vout->p_subpicture[i_index].p_next = p_subpic;
274 p_subpic = &p_vout->p_subpicture[i_index];
279 /* If we found an ephemer subpicture, check if it has to be
281 if( p_ephemer != NULL )
283 if( p_ephemer->i_start < ephemer_date )
285 /* Ephemer subpicture has lived too long */
286 vout_DestroySubPicture( p_vout, p_ephemer );
290 /* Ephemer subpicture can still live a bit */
291 p_ephemer->p_next = p_subpic;