1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vout_pictures.c,v 1.39 2003/04/27 23:16:35 gbazin 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 <stdlib.h> /* free() */
29 #include <stdio.h> /* sprintf() */
30 #include <string.h> /* strerror() */
35 #include "video_output.h"
37 #include "vout_pictures.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static void CopyPicture( vout_thread_t *, picture_t *, picture_t * );
44 /*****************************************************************************
45 * vout_DisplayPicture: display a picture
46 *****************************************************************************
47 * Remove the reservation flag of a picture, which will cause it to be ready for
48 * display. The picture won't be displayed until vout_DatePicture has been
50 *****************************************************************************/
51 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
53 vlc_mutex_lock( &p_vout->picture_lock );
54 switch( p_pic->i_status )
56 case RESERVED_PICTURE:
57 p_pic->i_status = RESERVED_DISP_PICTURE;
59 case RESERVED_DATED_PICTURE:
60 p_pic->i_status = READY_PICTURE;
63 msg_Err( p_vout, "picture to display %p has invalid status %d",
64 p_pic, p_pic->i_status );
68 vlc_mutex_unlock( &p_vout->picture_lock );
71 /*****************************************************************************
72 * vout_DatePicture: date a picture
73 *****************************************************************************
74 * Remove the reservation flag of a picture, which will cause it to be ready
75 * for display. The picture won't be displayed until vout_DisplayPicture has
77 *****************************************************************************/
78 void vout_DatePicture( vout_thread_t *p_vout,
79 picture_t *p_pic, mtime_t date )
81 vlc_mutex_lock( &p_vout->picture_lock );
83 switch( p_pic->i_status )
85 case RESERVED_PICTURE:
86 p_pic->i_status = RESERVED_DATED_PICTURE;
88 case RESERVED_DISP_PICTURE:
89 p_pic->i_status = READY_PICTURE;
92 msg_Err( p_vout, "picture to date %p has invalid status %d",
93 p_pic, p_pic->i_status );
97 vlc_mutex_unlock( &p_vout->picture_lock );
100 /*****************************************************************************
101 * vout_CreatePicture: allocate a picture in the video output heap.
102 *****************************************************************************
103 * This function creates a reserved image in the video output heap.
104 * A null pointer is returned if the function fails. This method provides an
105 * already allocated zone of memory in the picture data fields. It needs locking
106 * since several pictures can be created by several producers threads.
107 *****************************************************************************/
108 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
109 vlc_bool_t b_progressive,
110 vlc_bool_t b_top_field_first,
111 vlc_bool_t b_repeat_first_field )
113 int i_pic; /* picture index */
115 picture_t * p_freepic = NULL; /* first free picture */
118 vlc_mutex_lock( &p_vout->picture_lock );
121 * Look for an empty place in the picture heap.
123 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
125 p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
128 switch( p_pic->i_status )
130 case DESTROYED_PICTURE:
131 /* Memory will not be reallocated, and function can end
132 * immediately - this is the best possible case, since no
133 * memory allocation needs to be done */
134 p_pic->i_status = RESERVED_PICTURE;
135 p_pic->i_refcount = 0;
138 p_pic->b_progressive = b_progressive;
139 p_pic->b_repeat_first_field = b_repeat_first_field;
140 p_pic->b_top_field_first = b_top_field_first;
142 p_vout->i_heap_size++;
143 p_vout->render.i_last_used_pic =
144 ( p_vout->render.i_last_used_pic + i_pic + 1 )
146 vlc_mutex_unlock( &p_vout->picture_lock );
150 /* Picture is empty and ready for allocation */
151 p_vout->render.i_last_used_pic =
152 ( p_vout->render.i_last_used_pic + i_pic + 1 )
165 if( p_freepic != NULL )
167 vout_AllocatePicture( p_vout, p_freepic,
168 p_vout->render.i_width, p_vout->render.i_height,
169 p_vout->render.i_chroma );
171 if( p_freepic->i_planes )
173 /* Copy picture information, set some default values */
174 p_freepic->i_status = RESERVED_PICTURE;
175 p_freepic->i_type = MEMORY_PICTURE;
177 p_freepic->i_refcount = 0;
178 p_freepic->b_force = 0;
180 p_freepic->b_progressive = b_progressive;
181 p_freepic->b_repeat_first_field = b_repeat_first_field;
182 p_freepic->b_top_field_first = b_top_field_first;
184 p_freepic->i_matrix_coefficients = 1;
186 p_vout->i_heap_size++;
190 /* Memory allocation failed : set picture as empty */
191 p_freepic->i_status = FREE_PICTURE;
194 msg_Err( p_vout, "picture allocation failed" );
197 vlc_mutex_unlock( &p_vout->picture_lock );
202 /* No free or destroyed picture could be found, but the decoder
203 * will try again in a while. */
204 vlc_mutex_unlock( &p_vout->picture_lock );
209 /*****************************************************************************
210 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
211 *****************************************************************************
212 * This function frees a previously reserved picture or a permanent
213 * picture. It is meant to be used when the construction of a picture aborted.
214 * Note that the picture will be destroyed even if it is linked !
215 *****************************************************************************/
216 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
218 vlc_mutex_lock( &p_vout->picture_lock );
221 /* Check if picture status is valid */
222 if( (p_pic->i_status != RESERVED_PICTURE) &&
223 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
224 (p_pic->i_status != RESERVED_DISP_PICTURE) )
226 msg_Err( p_vout, "picture to destroy %p has invalid status %d",
227 p_pic, p_pic->i_status );
231 p_pic->i_status = DESTROYED_PICTURE;
232 p_vout->i_heap_size--;
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_pic )
245 vlc_mutex_lock( &p_vout->picture_lock );
247 vlc_mutex_unlock( &p_vout->picture_lock );
250 /*****************************************************************************
251 * vout_UnlinkPicture: decrement reference counter of a picture
252 *****************************************************************************
253 * This function decrement the reference counter of a picture in the video heap.
254 *****************************************************************************/
255 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
257 vlc_mutex_lock( &p_vout->picture_lock );
260 if( p_pic->i_refcount < 0 )
262 msg_Err( p_vout, "picture %p refcount is %i",
263 p_pic, p_pic->i_refcount );
264 p_pic->i_refcount = 0;
267 if( ( p_pic->i_refcount == 0 ) &&
268 ( p_pic->i_status == DISPLAYED_PICTURE ) )
270 p_pic->i_status = DESTROYED_PICTURE;
271 p_vout->i_heap_size--;
274 vlc_mutex_unlock( &p_vout->picture_lock );
277 /*****************************************************************************
278 * vout_RenderPicture: render a picture
279 *****************************************************************************
280 * This function chooses whether the current picture needs to be copied
281 * before rendering, does the subpicture magic, and tells the video output
282 * thread which direct buffer needs to be displayed.
283 *****************************************************************************/
284 picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
285 subpicture_t *p_subpic )
294 if( p_pic->i_type == DIRECT_PICTURE )
296 if( !p_vout->render.b_allow_modify_pics || p_pic->i_refcount )
298 /* Picture is in a direct buffer and is still in use,
299 * we need to copy it to another direct buffer before
300 * displaying it if there are subtitles. */
301 if( p_subpic != NULL )
303 /* We have subtitles. First copy the picture to
304 * the spare direct buffer, then render the
306 CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
308 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
310 return PP_OUTPUTPICTURE[0];
313 /* No subtitles, picture is in a directbuffer so
314 * we can display it directly even if it is still
319 /* Picture is in a direct buffer but isn't used by the
320 * decoder. We can safely render subtitles on it and
322 vout_RenderSubPictures( p_vout, p_pic, p_subpic );
327 /* Not a direct buffer. We either need to copy it to a direct buffer,
328 * or render it if the chroma isn't the same. */
329 if( p_vout->b_direct )
331 /* Picture is not in a direct buffer, but is exactly the
332 * same size as the direct buffers. A memcpy() is enough,
333 * then render the subtitles. */
335 if( PP_OUTPUTPICTURE[0]->pf_lock )
336 if( PP_OUTPUTPICTURE[0]->pf_lock( p_vout, PP_OUTPUTPICTURE[0] ) )
338 if( PP_OUTPUTPICTURE[0]->pf_unlock )
339 PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
344 CopyPicture( p_vout, p_pic, PP_OUTPUTPICTURE[0] );
346 vout_RenderSubPictures( p_vout, PP_OUTPUTPICTURE[0], p_subpic );
348 if( PP_OUTPUTPICTURE[0]->pf_unlock )
349 PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
351 return PP_OUTPUTPICTURE[0];
354 /* Picture is not in a direct buffer, and needs to be converted to
355 * another size/chroma. Then the subtitles need to be rendered as
356 * well. This usually means software YUV, or hardware YUV with a
357 * different chroma. */
359 if( p_vout->p_picture[0].pf_lock )
360 if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
363 /* Convert image to the first direct buffer */
364 p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
366 /* Render subpictures on the first direct buffer */
367 vout_RenderSubPictures( p_vout, &p_vout->p_picture[0], p_subpic );
369 if( p_vout->p_picture[0].pf_unlock )
370 p_vout->p_picture[0].pf_unlock( p_vout, &p_vout->p_picture[0] );
372 return &p_vout->p_picture[0];
375 /*****************************************************************************
376 * vout_PlacePicture: calculate image window coordinates
377 *****************************************************************************
378 * This function will be accessed by plugins. It calculates the relative
379 * position of the output window and the image window.
380 *****************************************************************************/
381 void vout_PlacePicture( vout_thread_t *p_vout,
382 unsigned int i_width, unsigned int i_height,
383 unsigned int *pi_x, unsigned int *pi_y,
384 unsigned int *pi_width, unsigned int *pi_height )
386 if( (i_width <= 0) || (i_height <=0) )
388 *pi_width = *pi_height = *pi_x = *pi_y = 0;
393 if( p_vout->b_scale )
396 *pi_height = i_height;
400 *pi_width = __MIN( i_width, p_vout->render.i_width );
401 *pi_height = __MIN( i_height, p_vout->render.i_height );
404 if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
406 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
410 *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
413 if( *pi_width > i_width )
416 *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
419 if( *pi_height > i_height )
421 *pi_height = i_height;
422 *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
425 *pi_x = ( i_width - *pi_width ) / 2;
426 *pi_y = ( i_height - *pi_height ) / 2;
429 /*****************************************************************************
430 * vout_AllocatePicture: allocate a new picture in the heap.
431 *****************************************************************************
432 * This function allocates a fake direct buffer in memory, which can be
433 * used exactly like a video buffer. The video output thread then manages
434 * how it gets displayed.
435 *****************************************************************************/
436 void vout_AllocatePicture( vout_thread_t *p_vout, picture_t *p_pic,
437 int i_width, int i_height, vlc_fourcc_t i_chroma )
439 int i_bytes, i_index;
441 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, i_width, i_height, i_chroma );
443 /* Calculate how big the new image should be */
444 for( i_bytes = 0, i_index = 0; i_index < p_pic->i_planes; i_index++ )
446 i_bytes += p_pic->p[ i_index ].i_lines * p_pic->p[ i_index ].i_pitch;
449 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
451 if( p_pic->p_data == NULL )
457 /* Fill the p_pixels field for each plane */
458 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
460 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
462 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels
463 + p_pic->p[i_index-1].i_lines
464 * p_pic->p[i_index-1].i_pitch;
468 /*****************************************************************************
469 * vout_InitPicture: initialise the picture_t fields given chroma/size.
470 *****************************************************************************
471 * This function initializes most of the picture_t fields given a chroma and
472 * size. It makes the assumption that stride == width.
473 *****************************************************************************/
474 void vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
475 int i_width, int i_height, vlc_fourcc_t i_chroma )
479 /* Store default values */
480 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
482 p_pic->p[i_index].p_pixels = NULL;
483 p_pic->p[i_index].i_pixel_pitch = 1;
486 /* Calculate coordinates */
490 p_pic->p[ Y_PLANE ].i_lines = i_height;
491 p_pic->p[ Y_PLANE ].i_pitch = i_width;
492 p_pic->p[ Y_PLANE ].i_visible_pitch = p_pic->p[ Y_PLANE ].i_pitch;
493 p_pic->p[ U_PLANE ].i_lines = i_height;
494 p_pic->p[ U_PLANE ].i_pitch = i_width / 4;
495 p_pic->p[ U_PLANE ].i_visible_pitch = p_pic->p[ U_PLANE ].i_pitch;
496 p_pic->p[ V_PLANE ].i_lines = i_height;
497 p_pic->p[ V_PLANE ].i_pitch = i_width / 4;
498 p_pic->p[ V_PLANE ].i_visible_pitch = p_pic->p[ V_PLANE ].i_pitch;
503 p_pic->p[ Y_PLANE ].i_lines = i_height;
504 p_pic->p[ Y_PLANE ].i_pitch = i_width;
505 p_pic->p[ Y_PLANE ].i_visible_pitch = p_pic->p[ Y_PLANE ].i_pitch;
506 p_pic->p[ U_PLANE ].i_lines = i_height / 4;
507 p_pic->p[ U_PLANE ].i_pitch = i_width / 4;
508 p_pic->p[ U_PLANE ].i_visible_pitch = p_pic->p[ U_PLANE ].i_pitch;
509 p_pic->p[ V_PLANE ].i_lines = i_height / 4;
510 p_pic->p[ V_PLANE ].i_pitch = i_width / 4;
511 p_pic->p[ V_PLANE ].i_visible_pitch = p_pic->p[ V_PLANE ].i_pitch;
518 p_pic->p[ Y_PLANE ].i_lines = i_height;
519 p_pic->p[ Y_PLANE ].i_pitch = i_width;
520 p_pic->p[ Y_PLANE ].i_visible_pitch = p_pic->p[ Y_PLANE ].i_pitch;
521 p_pic->p[ U_PLANE ].i_lines = i_height / 2;
522 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
523 p_pic->p[ U_PLANE ].i_visible_pitch = p_pic->p[ U_PLANE ].i_pitch;
524 p_pic->p[ V_PLANE ].i_lines = i_height / 2;
525 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
526 p_pic->p[ V_PLANE ].i_visible_pitch = p_pic->p[ V_PLANE ].i_pitch;
531 p_pic->p[ Y_PLANE ].i_lines = i_height;
532 p_pic->p[ Y_PLANE ].i_pitch = i_width;
533 p_pic->p[ Y_PLANE ].i_visible_pitch = p_pic->p[ Y_PLANE ].i_pitch;
534 p_pic->p[ U_PLANE ].i_lines = i_height;
535 p_pic->p[ U_PLANE ].i_pitch = i_width / 2;
536 p_pic->p[ U_PLANE ].i_visible_pitch = p_pic->p[ U_PLANE ].i_pitch;
537 p_pic->p[ V_PLANE ].i_lines = i_height;
538 p_pic->p[ V_PLANE ].i_pitch = i_width / 2;
539 p_pic->p[ V_PLANE ].i_visible_pitch = p_pic->p[ V_PLANE ].i_pitch;
544 p_pic->p[ Y_PLANE ].i_lines = i_height;
545 p_pic->p[ Y_PLANE ].i_pitch = i_width;
546 p_pic->p[ Y_PLANE ].i_visible_pitch = p_pic->p[ Y_PLANE ].i_pitch;
547 p_pic->p[ U_PLANE ].i_lines = i_height;
548 p_pic->p[ U_PLANE ].i_pitch = i_width;
549 p_pic->p[ U_PLANE ].i_visible_pitch = p_pic->p[ U_PLANE ].i_pitch;
550 p_pic->p[ V_PLANE ].i_lines = i_height;
551 p_pic->p[ V_PLANE ].i_pitch = i_width;
552 p_pic->p[ V_PLANE ].i_visible_pitch = p_pic->p[ V_PLANE ].i_pitch;
557 p_pic->p->i_lines = i_height;
558 p_pic->p->i_pitch = i_width;
559 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
560 p_pic->p->i_pixel_pitch = 4;
565 p_pic->p->i_lines = i_height;
566 p_pic->p->i_pitch = i_width * 2;
567 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
568 p_pic->p->i_pixel_pitch = 4;
573 p_pic->p->i_lines = i_height;
574 p_pic->p->i_pitch = i_width;
575 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
576 p_pic->p->i_pixel_pitch = 1;
581 p_pic->p->i_lines = i_height;
582 p_pic->p->i_pitch = i_width * 2;
583 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
584 p_pic->p->i_pixel_pitch = 2;
585 /* FIXME: p_heap isn't always reachable
586 p_pic->p_heap->i_rmask = 0x001f;
587 p_pic->p_heap->i_gmask = 0x03e0;
588 p_pic->p_heap->i_bmask = 0x7c00; */
593 p_pic->p->i_lines = i_height;
594 p_pic->p->i_pitch = i_width * 2;
595 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
596 p_pic->p->i_pixel_pitch = 2;
597 /* FIXME: p_heap isn't always reachable
598 p_pic->p_heap->i_rmask = 0x001f;
599 p_pic->p_heap->i_gmask = 0x07e0;
600 p_pic->p_heap->i_bmask = 0xf800; */
605 p_pic->p->i_lines = i_height;
606 p_pic->p->i_pitch = i_width * 3;
607 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
608 p_pic->p->i_pixel_pitch = 3;
609 /* FIXME: p_heap isn't always reachable
610 p_pic->p_heap->i_rmask = 0xff0000;
611 p_pic->p_heap->i_gmask = 0x00ff00;
612 p_pic->p_heap->i_bmask = 0x0000ff; */
617 p_pic->p->i_lines = i_height;
618 p_pic->p->i_pitch = i_width * 4;
619 p_pic->p->i_visible_pitch = p_pic->p->i_pitch;
620 p_pic->p->i_pixel_pitch = 4;
621 /* FIXME: p_heap isn't always reachable
622 p_pic->p_heap->i_rmask = 0xff0000;
623 p_pic->p_heap->i_gmask = 0x00ff00;
624 p_pic->p_heap->i_bmask = 0x0000ff; */
629 msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
630 i_chroma, (char*)&i_chroma );
637 /*****************************************************************************
638 * vout_ChromaCmp: compare two chroma values
639 *****************************************************************************
640 * This function returns 1 if the two fourcc values given as argument are
641 * the same format (eg. UYVY / UYNV) or almost the same format (eg. I420/YV12)
642 *****************************************************************************/
643 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
645 /* If they are the same, they are the same ! */
646 if( i_chroma == i_amorhc )
651 /* Check for equivalence classes */
699 /* Following functions are local */
701 /*****************************************************************************
702 * CopyPicture: copy a picture to another one
703 *****************************************************************************
704 * This function takes advantage of the image format, and reduces the
705 * number of calls to memcpy() to the minimum. Source and destination
706 * images must have same width (hence i_visible_pitch), height, and chroma.
707 *****************************************************************************/
708 static void CopyPicture( vout_thread_t * p_vout,
709 picture_t *p_src, picture_t *p_dest )
713 for( i = 0; i < p_src->i_planes ; i++ )
715 if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
717 /* There are margins, but with the same width : perfect ! */
718 p_vout->p_vlc->pf_memcpy(
719 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
720 p_src->p[i].i_pitch * p_src->p[i].i_lines );
724 /* We need to proceed line by line */
725 uint8_t *p_in = p_src->p[i].p_pixels;
726 uint8_t *p_out = p_dest->p[i].p_pixels;
729 for( i_line = p_src->p[i].i_lines; i_line--; )
731 p_vout->p_vlc->pf_memcpy( p_out, p_in,
732 p_src->p[i].i_visible_pitch );
733 p_in += p_src->p[i].i_pitch;
734 p_out += p_dest->p[i].i_pitch;
738 p_dest->date = p_src->date;