1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_pictures.c,v 1.27 2002/06/01 12:32:02 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() */
36 #include "video_output.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static void vout_CopyPicture( vout_thread_t *, picture_t *, picture_t * );
43 /*****************************************************************************
44 * vout_DisplayPicture: display a picture
45 *****************************************************************************
46 * Remove the reservation flag of a picture, which will cause it to be ready for
47 * display. The picture won't be displayed until vout_DatePicture has been
49 *****************************************************************************/
50 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
52 vlc_mutex_lock( &p_vout->picture_lock );
53 switch( p_pic->i_status )
55 case RESERVED_PICTURE:
56 p_pic->i_status = RESERVED_DISP_PICTURE;
58 case RESERVED_DATED_PICTURE:
59 p_pic->i_status = READY_PICTURE;
62 msg_Err( p_vout, "picture to display %p has invalid status %d",
63 p_pic, p_pic->i_status );
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 )
80 vlc_mutex_lock( &p_vout->picture_lock );
82 switch( p_pic->i_status )
84 case RESERVED_PICTURE:
85 p_pic->i_status = RESERVED_DATED_PICTURE;
87 case RESERVED_DISP_PICTURE:
88 p_pic->i_status = READY_PICTURE;
91 msg_Err( p_vout, "picture to date %p has invalid status %d",
92 p_pic, p_pic->i_status );
96 vlc_mutex_unlock( &p_vout->picture_lock );
99 /*****************************************************************************
100 * vout_CreatePicture: allocate a picture in the video output heap.
101 *****************************************************************************
102 * This function creates a reserved image in the video output heap.
103 * A null pointer is returned if the function fails. This method provides an
104 * already allocated zone of memory in the picture data fields. It needs locking
105 * since several pictures can be created by several producers threads.
106 *****************************************************************************/
107 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
108 vlc_bool_t b_progressive,
109 vlc_bool_t b_top_field_first,
110 vlc_bool_t b_repeat_first_field )
112 int i_pic; /* picture index */
114 picture_t * p_freepic = NULL; /* first free picture */
117 vlc_mutex_lock( &p_vout->picture_lock );
120 * Look for an empty place. We start at 1 because the first
121 * directbuffer is reserved for memcpy()ed pictures.
123 for( i_pic = 0; i_pic < I_RENDERPICTURES && p_freepic == NULL; i_pic++ )
125 p_pic = PP_RENDERPICTURE[ i_pic ];
127 /* If the picture we found is a memory buffer, and we have enough
128 * pictures in the stack, and we might have enough room later for
129 * a direct buffer, skip it. If no other pictures are found, the
130 * video decoder will try again later. */
131 if( p_vout->b_direct && ( p_vout->output.i_pictures > 5 )
132 && ( p_pic->i_type != DIRECT_PICTURE ) )
137 switch( p_pic->i_status )
139 case DESTROYED_PICTURE:
140 /* Memory will not be reallocated, and function can end
141 * immediately - this is the best possible case, since no
142 * memory allocation needs to be done */
143 p_pic->i_status = RESERVED_PICTURE;
144 p_pic->i_refcount = 0;
147 p_pic->b_progressive = b_progressive;
148 p_pic->b_repeat_first_field = b_repeat_first_field;
149 p_pic->b_top_field_first = b_top_field_first;
151 p_vout->i_heap_size++;
152 vlc_mutex_unlock( &p_vout->picture_lock );
156 /* Picture is empty and ready for allocation */
168 if( p_freepic != NULL )
170 vout_AllocatePicture( p_vout, p_freepic,
171 p_vout->render.i_width, p_vout->render.i_height,
172 p_vout->render.i_chroma );
174 if( p_freepic->i_planes )
176 /* Copy picture information, set some default values */
177 p_freepic->i_status = RESERVED_PICTURE;
178 p_freepic->i_type = MEMORY_PICTURE;
180 p_freepic->i_refcount = 0;
181 p_freepic->b_force = 0;
183 p_freepic->b_progressive = b_progressive;
184 p_freepic->b_repeat_first_field = b_repeat_first_field;
185 p_freepic->b_top_field_first = b_top_field_first;
187 p_freepic->i_matrix_coefficients = 1;
189 p_vout->i_heap_size++;
193 /* Memory allocation failed : set picture as empty */
194 p_freepic->i_status = FREE_PICTURE;
197 msg_Err( p_vout, "picture allocation failed" );
200 vlc_mutex_unlock( &p_vout->picture_lock );
205 /* No free or destroyed picture could be found, but the decoder
206 * will try again in a while. */
207 vlc_mutex_unlock( &p_vout->picture_lock );
212 /*****************************************************************************
213 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
214 *****************************************************************************
215 * This function frees a previously reserved picture or a permanent
216 * picture. It is meant to be used when the construction of a picture aborted.
217 * Note that the picture will be destroyed even if it is linked !
218 *****************************************************************************/
219 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
221 vlc_mutex_lock( &p_vout->picture_lock );
224 /* Check if picture status is valid */
225 if( (p_pic->i_status != RESERVED_PICTURE) &&
226 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
227 (p_pic->i_status != RESERVED_DISP_PICTURE) )
229 msg_Err( p_vout, "picture to destroy %p has invalid status %d",
230 p_pic, p_pic->i_status );
234 p_pic->i_status = DESTROYED_PICTURE;
235 p_vout->i_heap_size--;
237 vlc_mutex_unlock( &p_vout->picture_lock );
240 /*****************************************************************************
241 * vout_LinkPicture: increment reference counter of a picture
242 *****************************************************************************
243 * This function increments the reference counter of a picture in the video
244 * heap. It needs a lock since several producer threads can access the picture.
245 *****************************************************************************/
246 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
248 vlc_mutex_lock( &p_vout->picture_lock );
250 vlc_mutex_unlock( &p_vout->picture_lock );
253 /*****************************************************************************
254 * vout_UnlinkPicture: decrement reference counter of a picture
255 *****************************************************************************
256 * This function decrement the reference counter of a picture in the video heap.
257 *****************************************************************************/
258 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
260 vlc_mutex_lock( &p_vout->picture_lock );
263 if( p_pic->i_refcount < 0 )
265 msg_Err( p_vout, "picture refcount is %i", p_pic->i_refcount );
266 p_pic->i_refcount = 0;
269 if( ( p_pic->i_refcount == 0 ) &&
270 ( p_pic->i_status == DISPLAYED_PICTURE ) )
272 p_pic->i_status = DESTROYED_PICTURE;
273 p_vout->i_heap_size--;
276 vlc_mutex_unlock( &p_vout->picture_lock );
279 /*****************************************************************************
280 * vout_RenderPicture: render a picture
281 *****************************************************************************
282 * This function chooses whether the current picture needs to be copied
283 * before rendering, does the subpicture magic, and tells the video output
284 * thread which direct buffer needs to be displayed.
285 *****************************************************************************/
286 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
287 subpicture_t *p_subpic )
296 if( p_pic->i_type == DIRECT_PICTURE )
298 if( p_pic->i_refcount )
300 /* Picture is in a direct buffer and is still in use,
301 * we need to copy it to another direct buffer before
302 * displaying it if there are subtitles. */
303 if( p_subpic != NULL )
305 /* We have subtitles. First copy the picture to
306 * the spare direct buffer, then render the
308 vout_CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
310 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
312 return PP_OUTPUTPICTURE[0];
315 /* No subtitles, picture is in a directbuffer so
316 * we can display it directly even if it is still
321 /* Picture is in a direct buffer but isn't used by the
322 * decoder. We can safely render subtitles on it and
324 vout_RenderSubPictures( p_vout, p_pic, p_subpic );
329 /* Not a direct buffer. We either need to copy it to a direct buffer,
330 * or render it if the chroma isn't the same. */
331 if( p_vout->b_direct )
333 /* Picture is not in a direct buffer, but is exactly the
334 * same size as the direct buffers. A memcpy() is enough,
335 * then render the subtitles. */
336 vout_CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
338 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
340 return PP_OUTPUTPICTURE[0];
343 /* Picture is not in a direct buffer, and needs to be converted to
344 * another size/chroma. Then the subtitles need to be rendered as
345 * well. This usually means software YUV, or hardware YUV with a
346 * different chroma. */
348 /* Convert image to the first direct buffer */
349 p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
351 /* Render subpictures on the first direct buffer */
352 vout_RenderSubPictures( p_vout, &p_vout->p_picture[0], p_subpic );
354 return &p_vout->p_picture[0];
357 /*****************************************************************************
358 * vout_PlacePicture: calculate image window coordinates
359 *****************************************************************************
360 * This function will be accessed by plugins. It calculates the relative
361 * position of the output window and the image window.
362 *****************************************************************************/
363 void vout_PlacePicture( vout_thread_t *p_vout, int i_width, int i_height,
364 int *pi_x, int *pi_y, int *pi_width, int *pi_height )
366 if( (i_width <= 0) || (i_height <=0) )
368 *pi_width = *pi_height = *pi_x = *pi_y = 0;
373 if( p_vout->b_scale )
376 *pi_height = i_height;
380 *pi_width = __MIN( i_width, p_vout->render.i_width );
381 *pi_height = __MIN( i_height, p_vout->render.i_height );
384 if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
386 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
390 *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
393 if( *pi_width > i_width )
396 *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
399 if( *pi_height > i_height )
401 *pi_height = i_height;
402 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
405 *pi_x = ( i_width - *pi_width ) / 2;
406 *pi_y = ( i_height - *pi_height ) / 2;
409 /*****************************************************************************
410 * vout_AllocatePicture: allocate a new picture in the heap.
411 *****************************************************************************
412 * This function allocates a fake direct buffer in memory, which can be
413 * used exactly like a video buffer. The video output thread then manages
414 * how it gets displayed.
415 *****************************************************************************/
416 void vout_AllocatePicture( vout_thread_t *p_vout, picture_t *p_pic,
417 int i_width, int i_height, u32 i_chroma )
419 int i_bytes, i_index;
421 /* Store default values */
422 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
424 p_pic->p[i_index].p_pixels = NULL;
425 p_pic->p[i_index].b_margin = 0;
426 p_pic->p[i_index].i_pixel_bytes = 1;
429 /* Calculate coordinates */
435 p_pic->p[ Y_PLANE ].i_lines = i_height;
436 p_pic->p[ Y_PLANE ].i_pitch = i_width;
437 p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
438 p_pic->p[ U_PLANE ].i_lines = i_height / 2;
439 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
440 p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
441 p_pic->p[ V_PLANE ].i_lines = i_height / 2;
442 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
443 p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
448 p_pic->p[ Y_PLANE ].i_lines = i_height;
449 p_pic->p[ Y_PLANE ].i_pitch = i_width;
450 p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
451 p_pic->p[ U_PLANE ].i_lines = i_height;
452 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
453 p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
454 p_pic->p[ V_PLANE ].i_lines = i_height;
455 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
456 p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
461 p_pic->p[ Y_PLANE ].i_lines = i_height;
462 p_pic->p[ Y_PLANE ].i_pitch = i_width;
463 p_pic->p[ Y_PLANE ].i_visible_bytes = p_pic->p[ Y_PLANE ].i_pitch;
464 p_pic->p[ U_PLANE ].i_lines = i_height;
465 p_pic->p[ U_PLANE ].i_pitch = i_width;
466 p_pic->p[ U_PLANE ].i_visible_bytes = p_pic->p[ U_PLANE ].i_pitch;
467 p_pic->p[ V_PLANE ].i_lines = i_height;
468 p_pic->p[ V_PLANE ].i_pitch = i_width;
469 p_pic->p[ V_PLANE ].i_visible_bytes = p_pic->p[ V_PLANE ].i_pitch;
474 p_pic->p->i_lines = i_height;
475 p_pic->p->i_pitch = i_width;
476 p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
477 p_pic->p->i_pixel_bytes = 4;
482 p_pic->p->i_lines = i_height;
483 p_pic->p->i_pitch = i_width * 2;
484 p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
485 p_pic->p->i_pixel_bytes = 4;
490 p_pic->p->i_lines = i_height;
491 p_pic->p->i_pitch = i_width * 2;
492 p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
493 p_pic->p->i_pixel_bytes = 2;
494 /* FIXME: p_heap isn't always reachable
495 p_pic->p_heap->i_rmask = 0x001f;
496 p_pic->p_heap->i_gmask = 0x03e0;
497 p_pic->p_heap->i_bmask = 0x7c00; */
502 p_pic->p->i_lines = i_height;
503 p_pic->p->i_pitch = i_width * 2;
504 p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
505 p_pic->p->i_pixel_bytes = 2;
506 /* FIXME: p_heap isn't always reachable
507 p_pic->p_heap->i_rmask = 0x001f;
508 p_pic->p_heap->i_gmask = 0x07e0;
509 p_pic->p_heap->i_bmask = 0xf800; */
514 p_pic->p->i_lines = i_height;
515 p_pic->p->i_pitch = i_width * 4;
516 p_pic->p->i_visible_bytes = p_pic->p->i_pitch;
517 p_pic->p->i_pixel_bytes = 4;
518 /* FIXME: p_heap isn't always reachable
519 p_pic->p_heap->i_rmask = 0xff0000;
520 p_pic->p_heap->i_gmask = 0x00ff00;
521 p_pic->p_heap->i_bmask = 0x0000ff; */
526 msg_Err( p_vout, "unknown chroma type 0x%.8x (%4.4s)",
527 i_chroma, (char*)&i_chroma );
532 /* Calculate how big the new image should be */
533 for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
535 i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
538 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
540 if( p_pic->p_data == NULL )
546 /* Fill the p_pixels field for each plane */
547 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
549 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
551 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
552 + p_pic->p[i_index-1].i_lines
553 * p_pic->p[i_index-1].i_pitch;
557 /* Following functions are local */
559 /*****************************************************************************
560 * vout_CopyPicture: copy a picture to another one
561 *****************************************************************************
562 * This function takes advantage of the image format, and reduces the
563 * number of calls to memcpy() to the minimum. Source and destination
564 * images must have same width, height, and chroma.
565 *****************************************************************************/
566 static void vout_CopyPicture( vout_thread_t * p_vout,
567 picture_t *p_src, picture_t *p_dest )
571 for( i = 0; i < p_src->i_planes ; i++ )
573 if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
575 if( p_src->p[i].b_margin )
577 /* If p_src->b_margin is set, p_dest->b_margin must be set */
578 if( p_dest->p[i].b_hidden )
580 /* There are margins, but they are hidden : perfect ! */
581 p_vout->p_vlc->pf_memcpy(
582 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
583 p_src->p[i].i_pitch * p_src->p[i].i_lines );
588 /* We can't directly copy the margin. Too bad. */
593 /* Same pitch, no margins : perfect ! */
594 p_vout->p_vlc->pf_memcpy(
595 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
596 p_src->p[i].i_pitch * p_src->p[i].i_lines );
602 /* Pitch values are different */
605 /* We need to proceed line by line */
607 u8 *p_in = p_src->p[i].p_pixels, *p_out = p_dest->p[i].p_pixels;
610 for( i_line = p_src->p[i].i_lines; i_line--; )
612 p_vout->p_vlc->pf_memcpy( p_out, p_in,
613 p_src->p[i].i_visible_bytes );
614 p_in += p_src->p[i].i_pitch;
615 p_out += p_dest->p[i].i_pitch;