1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_pictures.c,v 1.8 2002/01/04 14:01:35 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_pic )
47 vlc_mutex_lock( &p_vout->picture_lock );
48 switch( p_pic->i_status )
50 case RESERVED_PICTURE:
51 p_pic->i_status = RESERVED_DISP_PICTURE;
53 case RESERVED_DATED_PICTURE:
54 p_pic->i_status = READY_PICTURE;
58 intf_ErrMsg( "error: picture %p has invalid status %d",
59 p_pic, p_pic->i_status );
65 intf_DbgMsg("picture %p", p_pic);
67 vlc_mutex_unlock( &p_vout->picture_lock );
70 /*****************************************************************************
71 * vout_DatePicture: date a picture
72 *****************************************************************************
73 * Remove the reservation flag of a picture, which will cause it to be ready
74 * for display. The picture won't be displayed until vout_DisplayPicture has
76 *****************************************************************************/
77 void vout_DatePicture( vout_thread_t *p_vout,
78 picture_t *p_pic, mtime_t date )
81 char psz_date[ MSTRTIME_MAX_SIZE ]; /* date */
84 vlc_mutex_lock( &p_vout->picture_lock );
86 switch( p_pic->i_status )
88 case RESERVED_PICTURE:
89 p_pic->i_status = RESERVED_DATED_PICTURE;
91 case RESERVED_DISP_PICTURE:
92 p_pic->i_status = READY_PICTURE;
96 intf_ErrMsg( "error: picture %p has invalid status %d",
97 p_pic, p_pic->i_status );
103 intf_DbgMsg( "picture %p, display date: %s",
104 p_pic, mstrtime( psz_date, p_pic->date) );
106 vlc_mutex_unlock( &p_vout->picture_lock );
109 /*****************************************************************************
110 * vout_CreatePicture: allocate a picture in the video output heap.
111 *****************************************************************************
112 * This function creates a reserved image in the video output heap.
113 * A null pointer is returned if the function fails. This method provides an
114 * already allocated zone of memory in the picture data fields. It needs locking
115 * since several pictures can be created by several producers threads.
116 *****************************************************************************/
117 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
118 boolean_t b_progressive,
119 boolean_t b_top_field_first,
120 boolean_t b_repeat_first_field )
122 int i_pic; /* picture index */
124 picture_t * p_freepic = NULL; /* first free picture */
127 vlc_mutex_lock( &p_vout->picture_lock );
130 * Look for an empty place. We start at 1 because the first
131 * directbuffer is reserved for memcpy()ed pictures.
133 for( i_pic = 0; i_pic < I_RENDERPICTURES && p_freepic == NULL; i_pic++ )
135 p_pic = PP_RENDERPICTURE[ i_pic ];
137 /* If the picture we found is a memory buffer, and we might have
138 * enough room later for a direct buffer, skip it. If no other
139 * pictures are found, the video decoder will try again later. */
140 if( p_vout->b_direct && ( p_vout->output.i_pictures > 3 )
141 && ( p_pic->i_type != DIRECT_PICTURE ) )
146 switch( p_pic->i_status )
148 case DESTROYED_PICTURE:
149 /* Memory will not be reallocated, and function can end
150 * immediately - this is the best possible case, since no
151 * memory allocation needs to be done */
152 p_pic->i_status = RESERVED_PICTURE;
153 p_pic->i_refcount = 0;
155 p_pic->b_progressive = b_progressive;
156 p_pic->b_repeat_first_field = b_repeat_first_field;
157 p_pic->b_top_field_first = b_top_field_first;
159 p_vout->i_heap_size++;
160 vlc_mutex_unlock( &p_vout->picture_lock );
164 /* Picture is empty and ready for allocation */
176 if( p_freepic != NULL )
178 vout_AllocatePicture( p_freepic,
179 p_vout->render.i_width, p_vout->render.i_height,
180 p_vout->render.i_chroma );
182 if( p_freepic->i_planes )
184 /* Copy picture information, set some default values */
185 p_freepic->i_status = RESERVED_PICTURE;
186 p_freepic->i_type = MEMORY_PICTURE;
188 p_freepic->i_refcount = 0;
190 p_freepic->b_progressive = b_progressive;
191 p_freepic->b_repeat_first_field = b_repeat_first_field;
192 p_freepic->b_top_field_first = b_top_field_first;
194 p_freepic->i_matrix_coefficients = 1;
196 p_vout->i_heap_size++;
200 /* Memory allocation failed : set picture as empty */
201 p_freepic->i_status = FREE_PICTURE;
204 intf_ErrMsg( "vout error: picture allocation failed" );
207 vlc_mutex_unlock( &p_vout->picture_lock );
209 /* Initialize mutex */
210 vlc_mutex_init( &(p_freepic->lock_deccount) );
215 /* No free or destroyed picture could be found, but the decoder
216 * will try again in a while. */
217 vlc_mutex_unlock( &p_vout->picture_lock );
222 /*****************************************************************************
223 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
224 *****************************************************************************
225 * This function frees a previously reserved picture or a permanent
226 * picture. It is meant to be used when the construction of a picture aborted.
227 * Note that the picture will be destroyed even if it is linked !
228 *****************************************************************************/
229 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
231 vlc_mutex_lock( &p_vout->picture_lock );
234 /* Check if picture status is valid */
235 if( (p_pic->i_status != RESERVED_PICTURE) &&
236 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
237 (p_pic->i_status != RESERVED_DISP_PICTURE) )
239 intf_ErrMsg( "error: picture %p has invalid status %d",
240 p_pic, p_pic->i_status );
244 p_pic->i_status = DESTROYED_PICTURE;
245 p_vout->i_heap_size--;
247 /* destroy the lock that had been initialized in CreatePicture */
248 vlc_mutex_destroy( &(p_pic->lock_deccount) );
250 vlc_mutex_unlock( &p_vout->picture_lock );
253 /*****************************************************************************
254 * vout_LinkPicture: increment reference counter of a picture
255 *****************************************************************************
256 * This function increments the reference counter of a picture in the video
257 * heap. It needs a lock since several producer threads can access the picture.
258 *****************************************************************************/
259 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
261 vlc_mutex_lock( &p_vout->picture_lock );
265 intf_DbgMsg( "picture %p refcount=%d", p_pic, p_pic->i_refcount );
268 vlc_mutex_unlock( &p_vout->picture_lock );
271 /*****************************************************************************
272 * vout_UnlinkPicture: decrement reference counter of a picture
273 *****************************************************************************
274 * This function decrement the reference counter of a picture in the video heap.
275 *****************************************************************************/
276 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
278 vlc_mutex_lock( &p_vout->picture_lock );
282 if( p_pic->i_refcount < 0 )
284 intf_DbgMsg( "error: refcount < 0" );
285 p_pic->i_refcount = 0;
289 if( ( p_pic->i_refcount == 0 ) &&
290 ( p_pic->i_status == DISPLAYED_PICTURE ) )
292 p_pic->i_status = DESTROYED_PICTURE;
293 p_vout->i_heap_size--;
297 intf_DbgMsg( "picture %p refcount=%d", p_pic, p_pic->i_refcount );
300 vlc_mutex_unlock( &p_vout->picture_lock );
303 /*****************************************************************************
304 * vout_RenderPicture: render a picture
305 *****************************************************************************
306 * This function chooses whether the current picture needs to be copied
307 * before rendering, does the subpicture magic, and tells the video output
308 * thread which direct buffer needs to be displayed.
309 *****************************************************************************/
310 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
311 subpicture_t *p_subpic )
320 if( p_pic->i_type == DIRECT_PICTURE )
322 if( p_pic->i_refcount )
324 /* Picture is in a direct buffer and is still in use,
325 * we need to copy it to another direct buffer before
326 * displaying it if there are subtitles. */
327 if( p_subpic != NULL )
329 //printf("memcpy (refcount != 0)\n");
330 /* We have subtitles. First copy the picture to
331 * the spare direct buffer, then render the
333 vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
335 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
337 return PP_OUTPUTPICTURE[0];
340 /* No subtitles, picture is in a directbuffer so
341 * we can display it directly even if it is still
343 //printf("direct (refcount == 0)\n");
347 /* Picture is in a direct buffer but isn't used by the
348 * decoder. We can safely render subtitles on it and
350 //printf("direct (refcount == 0)\n");
351 vout_RenderSubPictures( p_vout, p_pic, p_subpic );
356 /* Not a direct buffer. We either need to copy it to a direct buffer,
357 * or render it if the chroma isn't the same. */
358 if( p_vout->b_direct )
360 /* Picture is not in a direct buffer, but is exactly the
361 * same size as the direct buffers. A memcpy() is enough,
362 * then render the subtitles. */
363 //printf("memcpy (not a direct buffer)\n");
364 vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
366 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
368 return PP_OUTPUTPICTURE[0];
371 /* Picture is not in a direct buffer, and needs to be converted to
372 * another size/chroma. Then the subtitles need to be rendered as
375 /* This usually means software YUV, or hardware YUV with a
376 * different chroma. */
378 /* XXX: render to the first direct buffer */
379 //printf("render (not a direct buffer)\n");
380 p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
382 vout_RenderSubPictures( p_vout, p_pic, p_subpic );
384 return &p_vout->p_picture[0];
387 /*****************************************************************************
388 * vout_PlacePicture: calculate image window coordinates
389 *****************************************************************************
390 * This function will be accessed by plugins. It calculates the relative
391 * position of the output window and the image window.
392 *****************************************************************************/
393 void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
394 int *pi_x, int *pi_y, int *pi_width, int *pi_height )
396 if( p_vout->b_scale )
399 *pi_height = i_height;
403 *pi_width = MIN( i_width, p_vout->render.i_width );
404 *pi_height = MIN( i_height, p_vout->render.i_height );
407 if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
409 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
413 *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
416 if( *pi_width > i_width )
419 *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
422 if( *pi_height > i_height )
424 *pi_height = i_height;
425 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
428 *pi_x = ( i_width - *pi_width ) / 2;
429 *pi_y = ( i_height - *pi_height ) / 2;
432 /*****************************************************************************
433 * vout_AllocatePicture: allocate a new picture in the heap.
434 *****************************************************************************
435 * This function allocates a fake direct buffer in memory, which can be
436 * used exactly like a video buffer. The video output thread then manages
437 * how it gets displayed.
438 *****************************************************************************/
439 void vout_AllocatePicture( picture_t *p_pic,
440 int i_width, int i_height, u32 i_chroma )
442 int i_bytes, i_index;
444 /* Store default values */
445 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
447 p_pic->p[i_index].p_pixels = NULL;
448 p_pic->p[i_index].b_margin = 0;
449 p_pic->p[i_index].i_pixel_bytes = 1;
452 /* Calculate coordinates */
458 p_pic->p[ Y_PLANE ].i_lines = i_height;
459 p_pic->p[ Y_PLANE ].i_pitch = i_width;
460 p_pic->p[ U_PLANE ].i_lines = i_height / 2;
461 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
462 p_pic->p[ V_PLANE ].i_lines = i_height / 2;
463 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
468 p_pic->p[ Y_PLANE ].i_lines = i_height;
469 p_pic->p[ Y_PLANE ].i_pitch = i_width;
470 p_pic->p[ U_PLANE ].i_lines = i_height;
471 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
472 p_pic->p[ V_PLANE ].i_lines = i_height;
473 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
478 p_pic->p[ Y_PLANE ].i_lines = i_height;
479 p_pic->p[ Y_PLANE ].i_pitch = i_width;
480 p_pic->p[ U_PLANE ].i_lines = i_height;
481 p_pic->p[ U_PLANE ].i_pitch = i_width;
482 p_pic->p[ V_PLANE ].i_lines = i_height;
483 p_pic->p[ V_PLANE ].i_pitch = i_width;
488 p_pic->p->i_lines = i_height;
489 p_pic->p->i_pitch = i_width;
494 p_pic->p->i_lines = i_height;
495 p_pic->p->i_pitch = i_width * 2;
496 p_pic->p->i_pixel_bytes = 2;
497 p_pic->p->i_red_mask = 0x001f;
498 p_pic->p->i_green_mask = 0x03e0;
499 p_pic->p->i_blue_mask = 0x7c00;
504 p_pic->p->i_lines = i_height;
505 p_pic->p->i_pitch = i_width * 2;
506 p_pic->p->i_pixel_bytes = 2;
507 p_pic->p->i_red_mask = 0x001f;
508 p_pic->p->i_green_mask = 0x07e0;
509 p_pic->p->i_blue_mask = 0xf800;
514 intf_ErrMsg( "vout error: unknown chroma type %.8x", i_chroma );
519 /* Calculate how big the new image should be */
520 for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
522 i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
525 p_pic->p_data = memalign( 16, i_bytes );
527 if( p_pic->p_data == NULL )
533 /* Fill the p_pixels field for each plane */
534 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
536 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
538 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
539 + p_pic->p[i_index-1].i_lines
540 * p_pic->p[i_index-1].i_pitch;