]> git.sesse.net Git - vlc/blob - src/video_output/vout_pictures.c
caa5cf3dcfcc8d08143f5aaa67dd37b4698f31e2
[vlc] / src / video_output / vout_pictures.c
1 /*****************************************************************************
2  * vout_pictures.c : picture management functions
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: vout_pictures.c,v 1.2 2001/12/13 12:47:17 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 "defs.h"
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34
35 #include "common.h"
36 #include "intf_msg.h"
37 #include "threads.h"
38 #include "mtime.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static void     NewPicture        ( vout_thread_t *, picture_t * );
47
48 /*****************************************************************************
49  * vout_DisplayPicture: display a picture
50  *****************************************************************************
51  * Remove the reservation flag of a picture, which will cause it to be ready for
52  * display. The picture won't be displayed until vout_DatePicture has been
53  * called.
54  *****************************************************************************/
55 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_picture )
56 {
57     vlc_mutex_lock( &p_vout->picture_lock );
58     switch( p_picture->i_status )
59     {
60     case RESERVED_PICTURE:
61         p_picture->i_status = RESERVED_DISP_PICTURE;
62         break;
63     case RESERVED_DATED_PICTURE:
64         p_picture->i_status = READY_PICTURE;
65         break;
66 #ifdef DEBUG
67     default:
68         intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
69         break;
70 #endif
71     }
72
73 #ifdef TRACE_VOUT
74     intf_DbgMsg("picture %p", p_picture);
75 #endif
76     vlc_mutex_unlock( &p_vout->picture_lock );
77 }
78
79 /*****************************************************************************
80  * vout_DatePicture: date a picture
81  *****************************************************************************
82  * Remove the reservation flag of a picture, which will cause it to be ready
83  * for display. The picture won't be displayed until vout_DisplayPicture has
84  * been called.
85  *****************************************************************************/
86 void vout_DatePicture( vout_thread_t *p_vout,
87                        picture_t *p_picture, mtime_t date )
88 {
89 #ifdef TRACE_VOUT
90     char        psz_date[ MSTRTIME_MAX_SIZE ];                       /* date */
91 #endif
92
93     vlc_mutex_lock( &p_vout->picture_lock );
94     p_picture->date = date;
95     switch( p_picture->i_status )
96     {
97     case RESERVED_PICTURE:
98         p_picture->i_status = RESERVED_DATED_PICTURE;
99         break;
100     case RESERVED_DISP_PICTURE:
101         p_picture->i_status = READY_PICTURE;
102         break;
103 #ifdef DEBUG
104     default:
105         intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
106         break;
107 #endif
108     }
109
110 #ifdef TRACE_VOUT
111     intf_DbgMsg("picture %p, display date: %s", p_picture, mstrtime( psz_date, p_picture->date) );
112 #endif
113     vlc_mutex_unlock( &p_vout->picture_lock );
114 }
115
116 /*****************************************************************************
117  * vout_CreatePicture: allocate a picture in the video output heap.
118  *****************************************************************************
119  * This function creates a reserved image in the video output heap.
120  * A null pointer is returned if the function fails. This method provides an
121  * already allocated zone of memory in the picture data fields. It needs locking
122  * since several pictures can be created by several producers threads.
123  *****************************************************************************/
124 picture_t *vout_CreatePicture( vout_thread_t *p_vout )
125 {
126     int         i_picture;                                  /* picture index */
127     picture_t * p_picture;
128     picture_t * p_free_picture = NULL;                 /* first free picture */
129
130     /* Get lock */
131     vlc_mutex_lock( &p_vout->picture_lock );
132
133     /*
134      * Look for an empty place. XXX: we start at 1 because the first
135      * directbuffer is reserved for memcpy()ed pictures.
136      */
137     for( i_picture = 0; i_picture < I_RENDERPICTURES; i_picture++ )
138     {
139         p_picture = PP_RENDERPICTURE[ i_picture ];
140
141         switch( p_picture->i_status )
142         {
143             case DESTROYED_PICTURE:
144                 /* Memory will not be reallocated, and function can end
145                  * immediately - this is the best possible case, since no
146                  * memory allocation needs to be done */
147                 p_picture->i_status = RESERVED_PICTURE;
148                 p_vout->i_heap_size++;
149
150                 vlc_mutex_unlock( &p_vout->picture_lock );
151                 return( p_picture );
152
153             case FREE_PICTURE:
154                 if( p_free_picture == NULL )
155                 {
156                     /* Picture is empty and ready for allocation */
157                     p_free_picture = p_picture;
158                 }
159                 break;
160
161             default:
162                 break;
163         }
164     }
165
166     /*
167      * Prepare picture
168      */
169     if( p_free_picture != NULL )
170     {
171         NewPicture( p_vout, p_free_picture );
172
173         if( p_free_picture->i_planes )
174         {
175             /* Copy picture information, set some default values */
176             p_free_picture->i_status = RESERVED_PICTURE;
177             p_free_picture->i_type = MEMORY_PICTURE;
178             p_free_picture->i_matrix_coefficients = 1;
179             p_free_picture->i_refcount = 0;
180             p_vout->i_heap_size++;
181         }
182         else
183         {
184             /* Memory allocation failed : set picture as empty */
185             p_free_picture->i_status = FREE_PICTURE;
186             p_free_picture = NULL;
187
188             intf_ErrMsg( "vout error: picture allocation failed" );
189         }
190
191         vlc_mutex_unlock( &p_vout->picture_lock );
192
193         /* Initialize mutex */
194         vlc_mutex_init( &(p_free_picture->lock_deccount) );
195
196         return( p_free_picture );
197     }
198
199     /* No free or destroyed picture could be found, but the decoder
200      * will try again in a while. */
201     vlc_mutex_unlock( &p_vout->picture_lock );
202
203     return( NULL );
204 }
205
206 /*****************************************************************************
207  * vout_DestroyPicture: remove a permanent or reserved picture from the heap
208  *****************************************************************************
209  * This function frees a previously reserved picture or a permanent
210  * picture. It is meant to be used when the construction of a picture aborted.
211  * Note that the picture will be destroyed even if it is linked !
212  *****************************************************************************/
213 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
214 {
215     vlc_mutex_lock( &p_vout->picture_lock );
216
217 #ifdef DEBUG
218     /* Check if picture status is valid */
219     if( (p_picture->i_status != RESERVED_PICTURE) &&
220         (p_picture->i_status != RESERVED_DATED_PICTURE) &&
221         (p_picture->i_status != RESERVED_DISP_PICTURE) )
222     {
223         intf_ErrMsg( "error: picture %p has invalid status %d",
224                      p_picture, p_picture->i_status );
225     }
226 #endif
227
228     p_picture->i_status = DESTROYED_PICTURE;
229     p_vout->i_heap_size--;
230
231     /* destroy the lock that had been initialized in CreatePicture */
232     vlc_mutex_destroy( &(p_picture->lock_deccount) );
233
234     vlc_mutex_unlock( &p_vout->picture_lock );
235 }
236
237 /*****************************************************************************
238  * vout_LinkPicture: increment reference counter of a picture
239  *****************************************************************************
240  * This function increments the reference counter of a picture in the video
241  * heap. It needs a lock since several producer threads can access the picture.
242  *****************************************************************************/
243 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_picture )
244 {
245     vlc_mutex_lock( &p_vout->picture_lock );
246     p_picture->i_refcount++;
247
248 #ifdef TRACE_VOUT
249     intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
250 #endif
251
252     vlc_mutex_unlock( &p_vout->picture_lock );
253 }
254
255 /*****************************************************************************
256  * vout_UnlinkPicture: decrement reference counter of a picture
257  *****************************************************************************
258  * This function decrement the reference counter of a picture in the video heap.
259  *****************************************************************************/
260 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_picture )
261 {
262     vlc_mutex_lock( &p_vout->picture_lock );
263     p_picture->i_refcount--;
264
265 #ifdef TRACE_VOUT
266     if( p_picture->i_refcount < 0 )
267     {
268         intf_DbgMsg( "error: refcount < 0" );
269         p_picture->i_refcount = 0;
270     }
271 #endif
272
273     if( ( p_picture->i_refcount == 0 ) &&
274         ( p_picture->i_status == DISPLAYED_PICTURE ) )
275     {
276         p_picture->i_status = DESTROYED_PICTURE;
277         p_vout->i_heap_size--;
278     }
279
280 #ifdef TRACE_VOUT
281     intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
282 #endif
283
284     vlc_mutex_unlock( &p_vout->picture_lock );
285 }
286
287 /*****************************************************************************
288  * vout_RenderPicture: render a picture
289  *****************************************************************************
290  * This function chooses whether the current picture needs to be copied
291  * before rendering, does the subpicture magic, and tells the video output
292  * thread which direct buffer needs to be displayed.
293  *****************************************************************************/
294 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_picture,
295                                                        subpicture_t *p_subpic )
296 {
297     int i_index;
298
299     if( p_picture == NULL )
300     {
301         /* XXX: subtitles */
302
303         return NULL;
304     }
305
306     if( p_picture->i_type == DIRECT_PICTURE )
307     {
308         if( p_picture->i_refcount )
309         {
310             /* Picture is in a direct buffer and is still in use,
311              * we need to copy it to another direct buffer before
312              * displaying it if there are subtitles. */
313             if( p_subpic != NULL )
314             {
315                 /* We have subtitles. First copy the picture to
316                  * the spare direct buffer, then render the
317                  * subtitles. */
318                 for( i_index = 0 ; i_index < p_picture->i_planes ; i_index++ )
319                 {
320                     p_main->fast_memcpy(
321                         PP_OUTPUTPICTURE[0]->planes[ i_index ].p_data,
322                         p_picture->planes[ i_index ].p_data,
323                         p_picture->planes[ i_index ].i_bytes );
324                 }
325
326                 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
327
328                 return PP_OUTPUTPICTURE[0];
329             }
330
331             /* No subtitles, picture is in a directbuffer so
332              * we can display it directly even if it is still
333              * in use. */
334             return p_picture;
335         }
336
337         /* Picture is in a direct buffer but isn't used by the
338          * decoder. We can safely render subtitles on it and
339          * display it. */
340         vout_RenderSubPictures( p_vout, p_picture, p_subpic );
341
342         return p_picture;
343     }
344
345     /* Not a direct buffer. We either need to copy it to a direct buffer,
346      * or render it if the chroma isn't the same. */
347     if( p_vout->b_direct )
348     {
349         /* Picture is not in a direct buffer, but is exactly the
350          * same size as the direct buffers. A memcpy() is enough,
351          * then render the subtitles. */
352         for( i_index = 0; i_index < p_picture->i_planes; i_index++ )
353         {
354             p_main->fast_memcpy( PP_OUTPUTPICTURE[0]->planes[ i_index ].p_data,
355                                  p_picture->planes[ i_index ].p_data,
356                                  p_picture->planes[ i_index ].i_bytes );
357         }
358
359         vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
360
361         return PP_OUTPUTPICTURE[0];
362     }
363
364     /* Picture is not in a direct buffer, and needs to be converted to
365      * another size/chroma. Then the subtitles need to be rendered as
366      * well. */
367
368     /* This usually means software YUV, or hardware YUV with a
369      * different chroma. */
370
371     /* XXX: render to direct buffer */
372
373     vout_RenderSubPictures( p_vout, p_picture, p_subpic );
374
375     return &p_vout->p_picture[0];
376 }
377
378 /*****************************************************************************
379  * vout_PlacePicture: calculate image window coordinates
380  *****************************************************************************
381  * This function will be accessed by plugins. It calculates the relative
382  * position of the output window and the image window.
383  *****************************************************************************/
384 void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
385                         int *pi_x, int *pi_y, int *pi_width, int *pi_height )
386 {
387     int i_ratio;
388
389     if( p_vout->b_scale )
390     {
391         *pi_width = i_width;
392         *pi_height = i_height;
393     }
394     else
395     {
396         *pi_width = MIN( i_width, p_vout->render.i_width );
397         *pi_height = MIN( i_height, p_vout->render.i_height );
398     }
399
400     switch( p_vout->render.i_aspect )
401     {
402         case AR_3_4_PICTURE:
403             i_ratio = 900 * 4 / 3;
404             break;
405
406         case AR_16_9_PICTURE:
407             i_ratio = 900 * 16 / 9;
408             break;
409
410         case AR_221_1_PICTURE:
411             i_ratio = 900 * 221 / 100;
412             break;
413
414         case AR_SQUARE_PICTURE:
415         default:
416             i_ratio = 900 * p_vout->render.i_width
417                           / p_vout->render.i_height;
418             break;
419     }
420
421     if( 900 * *pi_width / *pi_height < i_ratio )
422     {
423         *pi_width = *pi_height * i_ratio / 900;
424     }
425     else
426     {
427         *pi_height = *pi_width * 900 / i_ratio;
428     }
429
430     if( *pi_width > i_width )
431     {
432         *pi_width = i_width;
433         *pi_height = 900 * *pi_width / i_ratio;
434     }
435
436     if( *pi_height > i_height )
437     {
438         *pi_height = i_height;
439         *pi_width = *pi_height * i_ratio / 900;
440     }
441
442     *pi_x = ( i_width - *pi_width ) / 2;
443     *pi_y = ( i_height - *pi_height ) / 2;
444 }
445
446 /* Following functions are local */
447
448 /*****************************************************************************
449  * NewPicture: allocate a new picture in the heap.
450  *****************************************************************************
451  * This function allocates a fake direct buffer in memory, which can be
452  * used exactly like a video buffer. The video output thread then manages
453  * how it gets displayed.
454  *****************************************************************************/
455 static void NewPicture( vout_thread_t *p_vout, picture_t *p_picture )
456 {
457     int i_data_size = 0;
458
459     p_picture->i_size = p_vout->render.i_width
460                          * p_vout->render.i_height;
461
462     /* Calculate coordinates */
463     switch( p_vout->render.i_chroma )
464     {
465         case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
466             p_picture->i_chroma_size = p_picture->i_size / 4;
467             p_picture->i_chroma_width = p_vout->render.i_width / 2;
468             break;
469
470         case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
471             p_picture->i_chroma_size = p_picture->i_size / 2;
472             p_picture->i_chroma_width = p_vout->render.i_width / 2;
473             break;
474
475         case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
476             p_picture->i_chroma_size = p_picture->i_size;
477             p_picture->i_chroma_width = p_vout->render.i_width;
478             break;
479
480         default:
481             intf_ErrMsg( "vout error: unknown chroma type %d",
482                          p_vout->render.i_chroma );
483             p_picture->i_planes = 0;
484             return;
485     }
486
487     /* Allocate memory */
488     switch( p_vout->render.i_chroma )
489     {
490         case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
491         case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
492         case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
493
494             i_data_size = p_picture->i_size + 2 * p_picture->i_chroma_size;
495
496             /* The Y plane */
497             p_picture->planes[ Y_PLANE ].i_bytes =
498                  p_picture->i_size * sizeof(pixel_data_t);
499             p_picture->planes[ Y_PLANE ].p_data =
500                  memalign( 16, i_data_size * sizeof(pixel_data_t) * 4 );
501             /* The U plane */
502             p_picture->planes[ U_PLANE ].i_bytes =
503                  p_picture->i_chroma_size * sizeof(pixel_data_t);
504             p_picture->planes[ U_PLANE ].p_data =
505                  p_picture->planes[ Y_PLANE ].p_data + p_picture->i_size;
506             /* The V plane */
507             p_picture->planes[ V_PLANE ].i_bytes =
508                  p_picture->i_chroma_size * sizeof(pixel_data_t);
509             p_picture->planes[ V_PLANE ].p_data =
510                  p_picture->planes[ U_PLANE ].p_data + p_picture->i_chroma_size;
511
512             p_picture->i_planes = 3;
513
514             break;
515
516         default:
517             p_picture->i_planes = 0;
518
519             break;
520     }
521 }
522