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