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 $
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 <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
33 #include <videolan/vlc.h>
36 #include "video_output.h"
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
44 *****************************************************************************/
45 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_picture )
47 vlc_mutex_lock( &p_vout->picture_lock );
48 switch( p_picture->i_status )
50 case RESERVED_PICTURE:
51 p_picture->i_status = RESERVED_DISP_PICTURE;
53 case RESERVED_DATED_PICTURE:
54 p_picture->i_status = READY_PICTURE;
58 intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
64 intf_DbgMsg("picture %p", p_picture);
66 vlc_mutex_unlock( &p_vout->picture_lock );
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
75 *****************************************************************************/
76 void vout_DatePicture( vout_thread_t *p_vout,
77 picture_t *p_picture, mtime_t date )
80 char psz_date[ MSTRTIME_MAX_SIZE ]; /* date */
83 vlc_mutex_lock( &p_vout->picture_lock );
84 p_picture->date = date;
85 switch( p_picture->i_status )
87 case RESERVED_PICTURE:
88 p_picture->i_status = RESERVED_DATED_PICTURE;
90 case RESERVED_DISP_PICTURE:
91 p_picture->i_status = READY_PICTURE;
95 intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
101 intf_DbgMsg("picture %p, display date: %s", p_picture, mstrtime( psz_date, p_picture->date) );
103 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
119 int i_picture; /* picture index */
120 picture_t * p_picture;
121 picture_t * p_free_picture = NULL; /* first free picture */
124 vlc_mutex_lock( &p_vout->picture_lock );
127 * Look for an empty place. We start at 1 because the first
128 * directbuffer is reserved for memcpy()ed pictures.
131 i_picture < I_RENDERPICTURES && p_free_picture == NULL;
134 p_picture = PP_RENDERPICTURE[ i_picture ];
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 ) )
145 switch( p_picture->i_status )
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;
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;
158 p_vout->i_heap_size++;
159 vlc_mutex_unlock( &p_vout->picture_lock );
163 /* Picture is empty and ready for allocation */
164 p_free_picture = p_picture;
175 if( p_free_picture != NULL )
177 vout_AllocatePicture( p_free_picture,
178 p_vout->render.i_width, p_vout->render.i_height,
179 p_vout->render.i_chroma );
181 if( p_free_picture->i_planes )
183 /* Copy picture information, set some default values */
184 p_free_picture->i_status = RESERVED_PICTURE;
185 p_free_picture->i_type = MEMORY_PICTURE;
187 p_free_picture->i_refcount = 0;
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;
193 p_free_picture->i_matrix_coefficients = 1;
195 p_vout->i_heap_size++;
199 /* Memory allocation failed : set picture as empty */
200 p_free_picture->i_status = FREE_PICTURE;
201 p_free_picture = NULL;
203 intf_ErrMsg( "vout error: picture allocation failed" );
206 vlc_mutex_unlock( &p_vout->picture_lock );
208 /* Initialize mutex */
209 vlc_mutex_init( &(p_free_picture->lock_deccount) );
211 return( p_free_picture );
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 );
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 )
230 vlc_mutex_lock( &p_vout->picture_lock );
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) )
238 intf_ErrMsg( "error: picture %p has invalid status %d",
239 p_picture, p_picture->i_status );
243 p_picture->i_status = DESTROYED_PICTURE;
244 p_vout->i_heap_size--;
246 /* destroy the lock that had been initialized in CreatePicture */
247 vlc_mutex_destroy( &(p_picture->lock_deccount) );
249 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
260 vlc_mutex_lock( &p_vout->picture_lock );
261 p_picture->i_refcount++;
264 intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
267 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
277 vlc_mutex_lock( &p_vout->picture_lock );
278 p_picture->i_refcount--;
281 if( p_picture->i_refcount < 0 )
283 intf_DbgMsg( "error: refcount < 0" );
284 p_picture->i_refcount = 0;
288 if( ( p_picture->i_refcount == 0 ) &&
289 ( p_picture->i_status == DISPLAYED_PICTURE ) )
291 p_picture->i_status = DESTROYED_PICTURE;
292 p_vout->i_heap_size--;
296 intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
299 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
314 if( p_picture == NULL )
321 if( p_picture->i_type == DIRECT_PICTURE )
323 if( p_picture->i_refcount )
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 )
330 //printf("memcpy (refcount != 0)\n");
331 /* We have subtitles. First copy the picture to
332 * the spare direct buffer, then render the
334 for( i_index = 0 ; i_index < p_picture->i_planes ; i_index++ )
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 );
342 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
344 return PP_OUTPUTPICTURE[0];
347 /* No subtitles, picture is in a directbuffer so
348 * we can display it directly even if it is still
350 //printf("direct (refcount == 0)\n");
354 /* Picture is in a direct buffer but isn't used by the
355 * decoder. We can safely render subtitles on it and
357 //printf("direct (refcount == 0)\n");
358 vout_RenderSubPictures( p_vout, p_picture, p_subpic );
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 )
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++ )
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 );
378 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
380 return PP_OUTPUTPICTURE[0];
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
387 /* This usually means software YUV, or hardware YUV with a
388 * different chroma. */
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] );
394 vout_RenderSubPictures( p_vout, p_picture, p_subpic );
396 return &p_vout->p_picture[0];
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 )
408 if( p_vout->b_scale )
411 *pi_height = i_height;
415 *pi_width = MIN( i_width, p_vout->render.i_width );
416 *pi_height = MIN( i_height, p_vout->render.i_height );
419 if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
421 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
425 *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
428 if( *pi_width > i_width )
431 *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
434 if( *pi_height > i_height )
436 *pi_height = i_height;
437 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
440 *pi_x = ( i_width - *pi_width ) / 2;
441 *pi_y = ( i_height - *pi_height ) / 2;
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 )
454 #define P p_picture->planes
455 int i_bytes, i_index;
457 /* Calculate coordinates */
460 case FOURCC_YV12: /* YUV 420: 1,1/4,1/4 samples per pixel */
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;
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;
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;
493 intf_ErrMsg( "vout error: unknown chroma type %d", i_chroma );
494 p_picture->i_planes = 0;
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++ )
501 i_bytes += P[ i_index ].i_bytes;
504 P[ 0 ].p_data = memalign( 16, i_bytes );
506 if( P[ 0 ].p_data == NULL )
508 p_picture->i_planes = 0;
512 /* Fill the p_data field for each plane */
513 for( i_index = 1; i_index < p_picture->i_planes; i_index++ )
515 P[ i_index ].p_data = P[ i_index-1 ].p_data + P[ i_index-1 ].i_bytes;