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 $
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 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strerror() */
41 #include "video_output.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static void NewPicture ( vout_thread_t *, picture_t * );
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
54 *****************************************************************************/
55 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_picture )
57 vlc_mutex_lock( &p_vout->picture_lock );
58 switch( p_picture->i_status )
60 case RESERVED_PICTURE:
61 p_picture->i_status = RESERVED_DISP_PICTURE;
63 case RESERVED_DATED_PICTURE:
64 p_picture->i_status = READY_PICTURE;
68 intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
74 intf_DbgMsg("picture %p", p_picture);
76 vlc_mutex_unlock( &p_vout->picture_lock );
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
85 *****************************************************************************/
86 void vout_DatePicture( vout_thread_t *p_vout,
87 picture_t *p_picture, mtime_t date )
90 char psz_date[ MSTRTIME_MAX_SIZE ]; /* date */
93 vlc_mutex_lock( &p_vout->picture_lock );
94 p_picture->date = date;
95 switch( p_picture->i_status )
97 case RESERVED_PICTURE:
98 p_picture->i_status = RESERVED_DATED_PICTURE;
100 case RESERVED_DISP_PICTURE:
101 p_picture->i_status = READY_PICTURE;
105 intf_ErrMsg("error: picture %p has invalid status %d", p_picture, p_picture->i_status );
111 intf_DbgMsg("picture %p, display date: %s", p_picture, mstrtime( psz_date, p_picture->date) );
113 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
126 int i_picture; /* picture index */
127 picture_t * p_picture;
128 picture_t * p_free_picture = NULL; /* first free picture */
131 vlc_mutex_lock( &p_vout->picture_lock );
134 * Look for an empty place. XXX: we start at 1 because the first
135 * directbuffer is reserved for memcpy()ed pictures.
137 for( i_picture = 0; i_picture < I_RENDERPICTURES; i_picture++ )
139 p_picture = PP_RENDERPICTURE[ i_picture ];
141 switch( p_picture->i_status )
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++;
150 vlc_mutex_unlock( &p_vout->picture_lock );
154 if( p_free_picture == NULL )
156 /* Picture is empty and ready for allocation */
157 p_free_picture = p_picture;
169 if( p_free_picture != NULL )
171 NewPicture( p_vout, p_free_picture );
173 if( p_free_picture->i_planes )
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++;
184 /* Memory allocation failed : set picture as empty */
185 p_free_picture->i_status = FREE_PICTURE;
186 p_free_picture = NULL;
188 intf_ErrMsg( "vout error: picture allocation failed" );
191 vlc_mutex_unlock( &p_vout->picture_lock );
193 /* Initialize mutex */
194 vlc_mutex_init( &(p_free_picture->lock_deccount) );
196 return( p_free_picture );
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 );
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 )
215 vlc_mutex_lock( &p_vout->picture_lock );
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) )
223 intf_ErrMsg( "error: picture %p has invalid status %d",
224 p_picture, p_picture->i_status );
228 p_picture->i_status = DESTROYED_PICTURE;
229 p_vout->i_heap_size--;
231 /* destroy the lock that had been initialized in CreatePicture */
232 vlc_mutex_destroy( &(p_picture->lock_deccount) );
234 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
245 vlc_mutex_lock( &p_vout->picture_lock );
246 p_picture->i_refcount++;
249 intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
252 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
262 vlc_mutex_lock( &p_vout->picture_lock );
263 p_picture->i_refcount--;
266 if( p_picture->i_refcount < 0 )
268 intf_DbgMsg( "error: refcount < 0" );
269 p_picture->i_refcount = 0;
273 if( ( p_picture->i_refcount == 0 ) &&
274 ( p_picture->i_status == DISPLAYED_PICTURE ) )
276 p_picture->i_status = DESTROYED_PICTURE;
277 p_vout->i_heap_size--;
281 intf_DbgMsg( "picture %p refcount=%d", p_picture, p_picture->i_refcount );
284 vlc_mutex_unlock( &p_vout->picture_lock );
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 )
299 if( p_picture == NULL )
306 if( p_picture->i_type == DIRECT_PICTURE )
308 if( p_picture->i_refcount )
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 )
315 /* We have subtitles. First copy the picture to
316 * the spare direct buffer, then render the
318 for( i_index = 0 ; i_index < p_picture->i_planes ; i_index++ )
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 );
326 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
328 return PP_OUTPUTPICTURE[0];
331 /* No subtitles, picture is in a directbuffer so
332 * we can display it directly even if it is still
337 /* Picture is in a direct buffer but isn't used by the
338 * decoder. We can safely render subtitles on it and
340 vout_RenderSubPictures( p_vout, p_picture, p_subpic );
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 )
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++ )
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 );
359 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
361 return PP_OUTPUTPICTURE[0];
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
368 /* This usually means software YUV, or hardware YUV with a
369 * different chroma. */
371 /* XXX: render to direct buffer */
373 vout_RenderSubPictures( p_vout, p_picture, p_subpic );
375 return &p_vout->p_picture[0];
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 )
389 if( p_vout->b_scale )
392 *pi_height = i_height;
396 *pi_width = MIN( i_width, p_vout->render.i_width );
397 *pi_height = MIN( i_height, p_vout->render.i_height );
400 switch( p_vout->render.i_aspect )
403 i_ratio = 900 * 4 / 3;
406 case AR_16_9_PICTURE:
407 i_ratio = 900 * 16 / 9;
410 case AR_221_1_PICTURE:
411 i_ratio = 900 * 221 / 100;
414 case AR_SQUARE_PICTURE:
416 i_ratio = 900 * p_vout->render.i_width
417 / p_vout->render.i_height;
421 if( 900 * *pi_width / *pi_height < i_ratio )
423 *pi_width = *pi_height * i_ratio / 900;
427 *pi_height = *pi_width * 900 / i_ratio;
430 if( *pi_width > i_width )
433 *pi_height = 900 * *pi_width / i_ratio;
436 if( *pi_height > i_height )
438 *pi_height = i_height;
439 *pi_width = *pi_height * i_ratio / 900;
442 *pi_x = ( i_width - *pi_width ) / 2;
443 *pi_y = ( i_height - *pi_height ) / 2;
446 /* Following functions are local */
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 )
459 p_picture->i_size = p_vout->render.i_width
460 * p_vout->render.i_height;
462 /* Calculate coordinates */
463 switch( p_vout->render.i_chroma )
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;
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;
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;
481 intf_ErrMsg( "vout error: unknown chroma type %d",
482 p_vout->render.i_chroma );
483 p_picture->i_planes = 0;
487 /* Allocate memory */
488 switch( p_vout->render.i_chroma )
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 */
494 i_data_size = p_picture->i_size + 2 * p_picture->i_chroma_size;
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 );
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;
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;
512 p_picture->i_planes = 3;
517 p_picture->i_planes = 0;