]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
* ALL: the first libvlc commit.
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: vout_subpictures.c,v 1.14 2002/06/01 12:32:02 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34
35 #include "video.h"
36 #include "video_output.h"
37
38 /*****************************************************************************
39  * vout_DisplaySubPicture: display a subpicture unit
40  *****************************************************************************
41  * Remove the reservation flag of a subpicture, which will cause it to be
42  * ready for display.
43  *****************************************************************************/
44 void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
45 {
46     int         i_margin;
47
48     /* Check if status is valid */
49     if( p_subpic->i_status != RESERVED_SUBPICTURE )
50     {
51         msg_Err( p_vout, "subpicture %p has invalid status #%d",
52                          p_subpic, p_subpic->i_status );
53     }
54
55     /* If the user requested an SPU margin, we force the position after
56      * having checked that it was a valid value. */
57     i_margin = config_GetInt( p_vout, "spumargin" );
58
59     if( i_margin >= 0 )
60     {
61         if( p_subpic->i_height + i_margin <= p_vout->output.i_height )
62         {
63             p_subpic->i_y = p_vout->output.i_height
64                              - i_margin - p_subpic->i_height;
65         }
66     }
67
68     /* Remove reservation flag */
69     p_subpic->i_status = READY_SUBPICTURE;
70 }
71
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,
81                                      int i_size )
82 {
83     int                 i_subpic;                        /* subpicture index */
84     subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
85     subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
86
87     /* Get lock */
88     vlc_mutex_lock( &p_vout->subpicture_lock );
89
90     /*
91      * Look for an empty place
92      */
93     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
94     {
95         if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
96         {
97             /* Subpicture is marked for destruction, but is still allocated */
98             if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
99                 (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
100             {
101                 /* Memory size do match or is smaller : memory will not be
102                  * reallocated, and function can end immediately - this is
103                  * the best possible case, since no memory allocation needs
104                  * to be done */
105                 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
106                 vlc_mutex_unlock( &p_vout->subpicture_lock );
107                 return( &p_vout->p_subpicture[i_subpic] );
108             }
109             else if( p_destroyed_subpic == NULL )
110             {
111                 /* Memory size do not match, but subpicture index will be kept
112                  * in case we find no other place */
113                 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
114             }
115         }
116         else if( (p_free_subpic == NULL) &&
117                  (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
118         {
119             /* Subpicture is empty and ready for allocation */
120             p_free_subpic = &p_vout->p_subpicture[i_subpic];
121         }
122     }
123
124     /* If no free subpictures are available, use a destroyed subpicture */
125     if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
126     {
127         /* No free subpicture or matching destroyed subpictures have been
128          * found, but a destroyed subpicture is still available */
129         free( p_destroyed_subpic->p_sys_orig );
130         p_free_subpic = p_destroyed_subpic;
131     }
132
133     /* If no free or destroyed subpicture could be found */
134     if( p_free_subpic == NULL )
135     {
136         msg_Err( p_vout, "subpicture heap is full" );
137         vlc_mutex_unlock( &p_vout->subpicture_lock );
138         return( NULL );
139     }
140
141     p_free_subpic->p_sys =
142         vlc_memalign( &p_free_subpic->p_sys_orig, 16, i_size );
143
144     if( p_free_subpic->p_sys != NULL )
145     {
146         /* Copy subpicture information, set some default values */
147         p_free_subpic->i_type   = i_type;
148         p_free_subpic->i_status = RESERVED_SUBPICTURE;
149         p_free_subpic->i_size   = i_size;
150         p_free_subpic->i_x      = 0;
151         p_free_subpic->i_y      = 0;
152         p_free_subpic->i_width  = 0;
153         p_free_subpic->i_height = 0;
154     }
155     else
156     {
157         /* Memory allocation failed : set subpicture as empty */
158         msg_Err( p_vout, "out of memory" );
159         p_free_subpic->i_type   = EMPTY_SUBPICTURE;
160         p_free_subpic->i_status = FREE_SUBPICTURE;
161         p_free_subpic           = NULL;
162     }
163
164     vlc_mutex_unlock( &p_vout->subpicture_lock );
165
166     return( p_free_subpic );
167 }
168
169 /*****************************************************************************
170  * vout_DestroySubPicture: remove a subpicture from the heap
171  *****************************************************************************
172  * This function frees a previously reserved subpicture.
173  * It is meant to be used when the construction of a picture aborted.
174  * This function does not need locking since reserved subpictures are ignored
175  * by the output thread.
176  *****************************************************************************/
177 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
178 {
179    /* Check if status is valid */
180    if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
181           && ( p_subpic->i_status != READY_SUBPICTURE ) )
182    {
183        msg_Err( p_vout, "subpicture %p has invalid status %d",
184                         p_subpic, p_subpic->i_status );
185    }
186
187     p_subpic->i_status = DESTROYED_SUBPICTURE;
188 }
189
190 /*****************************************************************************
191  * vout_RenderSubPictures: render a subpicture list
192  *****************************************************************************
193  * This function renders all sub picture units in the list.
194  *****************************************************************************/
195 void vout_RenderSubPictures( vout_thread_t *p_vout, picture_t *p_pic,
196                              subpicture_t *p_subpic )
197 {
198     while( p_subpic != NULL )
199     {
200         p_subpic->pf_render( p_vout, p_pic, p_subpic );
201         p_subpic = p_subpic->p_next;
202     }
203 }
204
205 /*****************************************************************************
206  * vout_SortSubPictures: find the subpictures to display
207  *****************************************************************************
208  * This function parses all subpictures and decides which ones need to be
209  * displayed. This operation does not need lock, since only READY_SUBPICTURE
210  * are handled. If no picture has been selected, display_date will depend on
211  * the subpicture.
212  * We also check for ephemer DVD subpictures (subpictures that have
213  * to be removed if a newer one is available), which makes it a lot
214  * more difficult to guess if a subpicture has to be rendered or not.
215  *****************************************************************************/
216 subpicture_t *vout_SortSubPictures( vout_thread_t *p_vout,
217                                     mtime_t display_date )
218 {
219     int i_index;
220     subpicture_t *p_subpic     = NULL;
221     subpicture_t *p_ephemer    = NULL;
222     mtime_t       ephemer_date = 0;
223
224     /* We get an easily parsable chained list of subpictures which
225      * ends with NULL since p_subpic was initialized to NULL. */
226     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
227     {
228         if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
229         {
230             /* If it is a DVD subpicture, check its date */
231             if( p_vout->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
232             {
233                 if( display_date > p_vout->p_subpicture[i_index].i_stop )
234                 {
235                     /* Too late, destroy the subpic */
236                     vout_DestroySubPicture( p_vout,
237                                     &p_vout->p_subpicture[i_index] );
238                     continue;
239                 }
240
241                 if( display_date < p_vout->p_subpicture[i_index].i_start )
242                 {
243                     /* Too early, come back next monday */
244                     continue;
245                 }
246
247                 /* If this is an ephemer subpic, see if it's the
248                  * youngest we have */
249                 if( p_vout->p_subpicture[i_index].b_ephemer )
250                 {
251                     if( p_ephemer == NULL )
252                     {
253                         p_ephemer = &p_vout->p_subpicture[i_index];
254                         continue;
255                     }
256
257                     if( p_vout->p_subpicture[i_index].i_start
258                                                      < p_ephemer->i_start )
259                     {
260                         /* Link the previous ephemer subpicture and
261                          * replace it with the current one */
262                         p_ephemer->p_next = p_subpic;
263                         p_subpic = p_ephemer;
264                         p_ephemer = &p_vout->p_subpicture[i_index];
265
266                         /* If it's the 2nd youngest subpicture,
267                          * register its date */
268                         if( !ephemer_date
269                               || ephemer_date > p_subpic->i_start )
270                         {
271                             ephemer_date = p_subpic->i_start;
272                         }
273
274                         continue;
275                     }
276                 }
277
278                 p_vout->p_subpicture[i_index].p_next = p_subpic;
279                 p_subpic = &p_vout->p_subpicture[i_index];
280
281                 /* If it's the 2nd youngest subpicture, register its date */                    if( !ephemer_date || ephemer_date > p_subpic->i_start )
282                 {
283                     ephemer_date = p_subpic->i_start;
284                 }
285             }
286             /* If it's not a DVD subpicture, just register it */
287             else
288             {
289                 p_vout->p_subpicture[i_index].p_next = p_subpic;
290                 p_subpic = &p_vout->p_subpicture[i_index];
291             }
292         }
293     }
294
295     /* If we found an ephemer subpicture, check if it has to be
296      * displayed */
297     if( p_ephemer != NULL )
298     {
299         if( p_ephemer->i_start < ephemer_date )
300         {
301             /* Ephemer subpicture has lived too long */
302             vout_DestroySubPicture( p_vout, p_ephemer );
303         }
304         else
305         {
306             /* Ephemer subpicture can still live a bit */
307             p_ephemer->p_next = p_subpic;
308             return p_ephemer;
309         }
310     }
311
312     return p_subpic;
313 }
314