1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_pictures.c,v 1.16 2002/02/19 00:50:20 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;
57 intf_ErrMsg( "error: picture %p has invalid status %d",
58 p_pic, p_pic->i_status );
62 vlc_mutex_unlock( &p_vout->picture_lock );
65 /*****************************************************************************
66 * vout_DatePicture: date a picture
67 *****************************************************************************
68 * Remove the reservation flag of a picture, which will cause it to be ready
69 * for display. The picture won't be displayed until vout_DisplayPicture has
71 *****************************************************************************/
72 void vout_DatePicture( vout_thread_t *p_vout,
73 picture_t *p_pic, mtime_t date )
75 vlc_mutex_lock( &p_vout->picture_lock );
77 switch( p_pic->i_status )
79 case RESERVED_PICTURE:
80 p_pic->i_status = RESERVED_DATED_PICTURE;
82 case RESERVED_DISP_PICTURE:
83 p_pic->i_status = READY_PICTURE;
86 intf_ErrMsg( "error: picture %p has invalid status %d",
87 p_pic, p_pic->i_status );
91 vlc_mutex_unlock( &p_vout->picture_lock );
94 /*****************************************************************************
95 * vout_CreatePicture: allocate a picture in the video output heap.
96 *****************************************************************************
97 * This function creates a reserved image in the video output heap.
98 * A null pointer is returned if the function fails. This method provides an
99 * already allocated zone of memory in the picture data fields. It needs locking
100 * since several pictures can be created by several producers threads.
101 *****************************************************************************/
102 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
103 boolean_t b_progressive,
104 boolean_t b_top_field_first,
105 boolean_t b_repeat_first_field )
107 int i_pic; /* picture index */
109 picture_t * p_freepic = NULL; /* first free picture */
112 vlc_mutex_lock( &p_vout->picture_lock );
115 * Look for an empty place. We start at 1 because the first
116 * directbuffer is reserved for memcpy()ed pictures.
118 for( i_pic = 0; i_pic < I_RENDERPICTURES && p_freepic == NULL; i_pic++ )
120 p_pic = PP_RENDERPICTURE[ i_pic ];
122 /* If the picture we found is a memory buffer, and we have enough
123 * pictures in the stack, and we might have enough room later for
124 * a direct buffer, skip it. If no other pictures are found, the
125 * video decoder will try again later. */
126 if( p_vout->b_direct && ( p_vout->output.i_pictures > 5 )
127 && ( p_pic->i_type != DIRECT_PICTURE ) )
132 switch( p_pic->i_status )
134 case DESTROYED_PICTURE:
135 /* Memory will not be reallocated, and function can end
136 * immediately - this is the best possible case, since no
137 * memory allocation needs to be done */
138 p_pic->i_status = RESERVED_PICTURE;
139 p_pic->i_refcount = 0;
141 p_pic->b_progressive = b_progressive;
142 p_pic->b_repeat_first_field = b_repeat_first_field;
143 p_pic->b_top_field_first = b_top_field_first;
145 p_vout->i_heap_size++;
146 vlc_mutex_unlock( &p_vout->picture_lock );
150 /* Picture is empty and ready for allocation */
162 if( p_freepic != NULL )
164 vout_AllocatePicture( p_freepic,
165 p_vout->render.i_width, p_vout->render.i_height,
166 p_vout->render.i_chroma );
168 if( p_freepic->i_planes )
170 /* Copy picture information, set some default values */
171 p_freepic->i_status = RESERVED_PICTURE;
172 p_freepic->i_type = MEMORY_PICTURE;
174 p_freepic->i_refcount = 0;
176 p_freepic->b_progressive = b_progressive;
177 p_freepic->b_repeat_first_field = b_repeat_first_field;
178 p_freepic->b_top_field_first = b_top_field_first;
180 p_freepic->i_matrix_coefficients = 1;
182 p_vout->i_heap_size++;
186 /* Memory allocation failed : set picture as empty */
187 p_freepic->i_status = FREE_PICTURE;
190 intf_ErrMsg( "vout error: picture allocation failed" );
193 vlc_mutex_unlock( &p_vout->picture_lock );
195 /* Initialize mutex */
196 vlc_mutex_init( &(p_freepic->lock_deccount) );
201 /* No free or destroyed picture could be found, but the decoder
202 * will try again in a while. */
203 vlc_mutex_unlock( &p_vout->picture_lock );
208 /*****************************************************************************
209 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
210 *****************************************************************************
211 * This function frees a previously reserved picture or a permanent
212 * picture. It is meant to be used when the construction of a picture aborted.
213 * Note that the picture will be destroyed even if it is linked !
214 *****************************************************************************/
215 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
217 vlc_mutex_lock( &p_vout->picture_lock );
220 /* Check if picture status is valid */
221 if( (p_pic->i_status != RESERVED_PICTURE) &&
222 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
223 (p_pic->i_status != RESERVED_DISP_PICTURE) )
225 intf_ErrMsg( "error: picture %p has invalid status %d",
226 p_pic, p_pic->i_status );
230 p_pic->i_status = DESTROYED_PICTURE;
231 p_vout->i_heap_size--;
233 /* destroy the lock that had been initialized in CreatePicture */
234 vlc_mutex_destroy( &(p_pic->lock_deccount) );
236 vlc_mutex_unlock( &p_vout->picture_lock );
239 /*****************************************************************************
240 * vout_LinkPicture: increment reference counter of a picture
241 *****************************************************************************
242 * This function increments the reference counter of a picture in the video
243 * heap. It needs a lock since several producer threads can access the picture.
244 *****************************************************************************/
245 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
247 vlc_mutex_lock( &p_vout->picture_lock );
249 vlc_mutex_unlock( &p_vout->picture_lock );
252 /*****************************************************************************
253 * vout_UnlinkPicture: decrement reference counter of a picture
254 *****************************************************************************
255 * This function decrement the reference counter of a picture in the video heap.
256 *****************************************************************************/
257 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
259 vlc_mutex_lock( &p_vout->picture_lock );
262 if( p_pic->i_refcount < 0 )
264 intf_ErrMsg( "vout error: picture refcount is %i", p_pic->i_refcount );
265 p_pic->i_refcount = 0;
268 if( ( p_pic->i_refcount == 0 ) &&
269 ( p_pic->i_status == DISPLAYED_PICTURE ) )
271 p_pic->i_status = DESTROYED_PICTURE;
272 p_vout->i_heap_size--;
275 vlc_mutex_unlock( &p_vout->picture_lock );
278 /*****************************************************************************
279 * vout_RenderPicture: render a picture
280 *****************************************************************************
281 * This function chooses whether the current picture needs to be copied
282 * before rendering, does the subpicture magic, and tells the video output
283 * thread which direct buffer needs to be displayed.
284 *****************************************************************************/
285 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
286 subpicture_t *p_subpic )
295 if( p_pic->i_type == DIRECT_PICTURE )
297 if( p_pic->i_refcount )
299 /* Picture is in a direct buffer and is still in use,
300 * we need to copy it to another direct buffer before
301 * displaying it if there are subtitles. */
302 if( p_subpic != NULL )
304 /* We have subtitles. First copy the picture to
305 * the spare direct buffer, then render the
307 vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
309 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
311 return PP_OUTPUTPICTURE[0];
314 /* No subtitles, picture is in a directbuffer so
315 * we can display it directly even if it is still
320 /* Picture is in a direct buffer but isn't used by the
321 * decoder. We can safely render subtitles on it and
323 vout_RenderSubPictures( p_vout, p_pic, p_subpic );
328 /* Not a direct buffer. We either need to copy it to a direct buffer,
329 * or render it if the chroma isn't the same. */
330 if( p_vout->b_direct )
332 /* Picture is not in a direct buffer, but is exactly the
333 * same size as the direct buffers. A memcpy() is enough,
334 * then render the subtitles. */
335 vout_CopyPicture( p_pic, PP_OUTPUTPICTURE[0] );
337 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
339 return PP_OUTPUTPICTURE[0];
342 /* Picture is not in a direct buffer, and needs to be converted to
343 * another size/chroma. Then the subtitles need to be rendered as
344 * well. This usually means software YUV, or hardware YUV with a
345 * different chroma. */
347 /* Convert image to the first direct buffer */
348 p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
350 /* Render subpictures on the first direct buffer */
351 vout_RenderSubPictures( p_vout, &p_vout->p_picture[0], p_subpic );
353 return &p_vout->p_picture[0];
356 /*****************************************************************************
357 * vout_PlacePicture: calculate image window coordinates
358 *****************************************************************************
359 * This function will be accessed by plugins. It calculates the relative
360 * position of the output window and the image window.
361 *****************************************************************************/
362 void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
363 int *pi_x, int *pi_y, int *pi_width, int *pi_height )
365 if( (i_width <= 0) || (i_height <=0) )
367 *pi_width = *pi_height = *pi_x = *pi_y = 0;
372 if( p_vout->b_scale )
375 *pi_height = i_height;
379 *pi_width = MIN( i_width, p_vout->render.i_width );
380 *pi_height = MIN( i_height, p_vout->render.i_height );
383 if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
385 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
389 *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
392 if( *pi_width > i_width )
395 *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
398 if( *pi_height > i_height )
400 *pi_height = i_height;
401 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
404 *pi_x = ( i_width - *pi_width ) / 2;
405 *pi_y = ( i_height - *pi_height ) / 2;
408 /*****************************************************************************
409 * vout_AllocatePicture: allocate a new picture in the heap.
410 *****************************************************************************
411 * This function allocates a fake direct buffer in memory, which can be
412 * used exactly like a video buffer. The video output thread then manages
413 * how it gets displayed.
414 *****************************************************************************/
415 void vout_AllocatePicture( picture_t *p_pic,
416 int i_width, int i_height, u32 i_chroma )
418 int i_bytes, i_index;
420 /* Store default values */
421 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
423 p_pic->p[i_index].p_pixels = NULL;
424 p_pic->p[i_index].b_margin = 0;
425 p_pic->p[i_index].i_pixel_bytes = 1;
428 /* Calculate coordinates */
434 p_pic->p[ Y_PLANE ].i_lines = i_height;
435 p_pic->p[ Y_PLANE ].i_pitch = i_width;
436 p_pic->p[ U_PLANE ].i_lines = i_height / 2;
437 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
438 p_pic->p[ V_PLANE ].i_lines = i_height / 2;
439 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
444 p_pic->p[ Y_PLANE ].i_lines = i_height;
445 p_pic->p[ Y_PLANE ].i_pitch = i_width;
446 p_pic->p[ U_PLANE ].i_lines = i_height;
447 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
448 p_pic->p[ V_PLANE ].i_lines = i_height;
449 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
454 p_pic->p[ Y_PLANE ].i_lines = i_height;
455 p_pic->p[ Y_PLANE ].i_pitch = i_width;
456 p_pic->p[ U_PLANE ].i_lines = i_height;
457 p_pic->p[ U_PLANE ].i_pitch = i_width;
458 p_pic->p[ V_PLANE ].i_lines = i_height;
459 p_pic->p[ V_PLANE ].i_pitch = i_width;
464 p_pic->p->i_lines = i_height;
465 p_pic->p->i_pitch = i_width;
466 p_pic->p->i_pixel_bytes = 4;
471 p_pic->p->i_lines = i_height;
472 p_pic->p->i_pitch = i_width * 2;
473 p_pic->p->i_pixel_bytes = 2;
474 p_pic->p->i_red_mask = 0x001f;
475 p_pic->p->i_green_mask = 0x03e0;
476 p_pic->p->i_blue_mask = 0x7c00;
481 p_pic->p->i_lines = i_height;
482 p_pic->p->i_pitch = i_width * 2;
483 p_pic->p->i_pixel_bytes = 2;
484 p_pic->p->i_red_mask = 0x001f;
485 p_pic->p->i_green_mask = 0x07e0;
486 p_pic->p->i_blue_mask = 0xf800;
491 p_pic->p->i_lines = i_height;
492 p_pic->p->i_pitch = i_width * 4;
493 p_pic->p->i_pixel_bytes = 4;
494 p_pic->p->i_red_mask = 0xff0000;
495 p_pic->p->i_green_mask = 0x00ff00;
496 p_pic->p->i_blue_mask = 0x0000ff;
501 intf_ErrMsg( "vout error: unknown chroma type 0x%.8x (%4.4s)",
502 i_chroma, (char*)&i_chroma );
507 /* Calculate how big the new image should be */
508 for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
510 i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
513 p_pic->p_data = memalign( 16, i_bytes );
515 if( p_pic->p_data == NULL )
521 /* Fill the p_pixels field for each plane */
522 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
524 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
526 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
527 + p_pic->p[i_index-1].i_lines
528 * p_pic->p[i_index-1].i_pitch;