1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_common.h>
38 #include <vlc_filter.h>
39 #include <vlc_image.h>
40 #include <vlc_block.h>
41 #include <vlc_picture_fifo.h>
42 #include <vlc_picture_pool.h>
44 #include "vout_pictures.h"
45 #include "vout_internal.h"
50 * Remove the reservation flag of a picture, which will cause it to be ready
53 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
55 vlc_mutex_lock( &p_vout->picture_lock );
57 if( p_pic->i_status == RESERVED_PICTURE )
59 p_pic->i_status = READY_PICTURE;
60 vlc_cond_signal( &p_vout->p->picture_wait );
64 msg_Err( p_vout, "picture to display %p has invalid status %d",
65 p_pic, p_pic->i_status );
67 p_vout->p->i_picture_qtype = p_pic->i_qtype;
69 vlc_mutex_unlock( &p_vout->picture_lock );
73 * Allocate a picture in the video output heap.
75 * This function creates a reserved image in the video output heap.
76 * A null pointer is returned if the function fails. This method provides an
77 * already allocated zone of memory in the picture data fields.
78 * It needs locking since several pictures can be created by several producers
81 int vout_CountPictureAvailable( vout_thread_t *p_vout )
86 vlc_mutex_lock( &p_vout->picture_lock );
87 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
89 picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
91 switch( p_pic->i_status )
93 case DESTROYED_PICTURE:
105 vlc_mutex_unlock( &p_vout->picture_lock );
110 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
112 bool b_top_field_first,
113 unsigned int i_nb_fields )
115 int i_pic; /* picture index */
117 picture_t * p_freepic = NULL; /* first free picture */
120 vlc_mutex_lock( &p_vout->picture_lock );
123 * Look for an empty place in the picture heap.
125 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
127 p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
130 switch( p_pic->i_status )
132 case DESTROYED_PICTURE:
133 /* Memory will not be reallocated, and function can end
134 * immediately - this is the best possible case, since no
135 * memory allocation needs to be done */
136 p_pic->i_status = RESERVED_PICTURE;
137 p_pic->i_refcount = 0;
140 p_pic->b_progressive = b_progressive;
141 p_pic->i_nb_fields = i_nb_fields;
142 p_pic->b_top_field_first = b_top_field_first;
144 p_vout->i_heap_size++;
145 p_vout->render.i_last_used_pic =
146 ( p_vout->render.i_last_used_pic + i_pic + 1 )
148 vlc_mutex_unlock( &p_vout->picture_lock );
152 /* Picture is empty and ready for allocation */
153 p_vout->render.i_last_used_pic =
154 ( p_vout->render.i_last_used_pic + i_pic + 1 )
167 if( p_freepic != NULL )
169 vout_AllocatePicture( VLC_OBJECT(p_vout),
170 p_freepic, p_vout->render.i_chroma,
171 p_vout->render.i_width, p_vout->render.i_height,
172 p_vout->render.i_aspect );
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;
179 p_freepic->b_slow = 0;
181 p_freepic->i_refcount = 0;
182 p_freepic->b_force = 0;
184 p_freepic->b_progressive = b_progressive;
185 p_freepic->i_nb_fields = i_nb_fields;
186 p_freepic->b_top_field_first = b_top_field_first;
188 p_vout->i_heap_size++;
192 /* Memory allocation failed : set picture as empty */
193 p_freepic->i_status = FREE_PICTURE;
196 msg_Err( p_vout, "picture allocation failed" );
199 vlc_mutex_unlock( &p_vout->picture_lock );
204 /* No free or destroyed picture could be found, but the decoder
205 * will try again in a while. */
206 vlc_mutex_unlock( &p_vout->picture_lock );
212 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
214 vlc_assert_locked( &p_vout->picture_lock );
216 p_picture->i_status = DESTROYED_PICTURE;
217 p_vout->i_heap_size--;
218 picture_CleanupQuant( p_picture );
220 vlc_cond_signal( &p_vout->p->picture_wait );
224 * Remove a permanent or reserved picture from the heap
226 * This function frees a previously reserved picture or a permanent
227 * picture. It is meant to be used when the construction of a picture aborted.
228 * Note that the picture will be destroyed even if it is linked !
230 * TODO remove it, vout_DropPicture should be used instead
232 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
235 /* Check if picture status is valid */
236 vlc_mutex_lock( &p_vout->picture_lock );
237 if( p_pic->i_status != RESERVED_PICTURE )
239 msg_Err( p_vout, "picture to destroy %p has invalid status %d",
240 p_pic, p_pic->i_status );
242 vlc_mutex_unlock( &p_vout->picture_lock );
245 vout_DropPicture( p_vout, p_pic );
249 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
251 vlc_assert_locked( &p_vout->picture_lock );
252 if( p_picture->i_refcount > 0 )
254 /* Pretend we displayed the picture, but don't destroy
255 * it since the decoder might still need it. */
256 p_picture->i_status = DISPLAYED_PICTURE;
260 /* Destroy the picture without displaying it */
261 DestroyPicture( p_vout, p_picture );
266 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic )
268 vlc_mutex_lock( &p_vout->picture_lock );
270 if( p_pic->i_status == READY_PICTURE )
272 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
274 vlc_cond_signal( &p_vout->p->picture_wait );
278 vout_UsePictureLocked( p_vout, p_pic );
281 vlc_mutex_unlock( &p_vout->picture_lock );
285 * Increment reference counter of a picture
287 * This function increments the reference counter of a picture in the video
288 * heap. It needs a lock since several producer threads can access the picture.
290 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
292 vlc_mutex_lock( &p_vout->picture_lock );
294 vlc_mutex_unlock( &p_vout->picture_lock );
298 * Decrement reference counter of a picture
300 * This function decrement the reference counter of a picture in the video heap
302 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
304 vlc_mutex_lock( &p_vout->picture_lock );
306 if( p_pic->i_refcount > 0 )
309 msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
310 p_pic, p_pic->i_refcount );
312 if( p_pic->i_refcount == 0 && p_pic->i_status == DISPLAYED_PICTURE )
313 DestroyPicture( p_vout, p_pic );
315 vlc_mutex_unlock( &p_vout->picture_lock );
318 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
320 if( p_picture->pf_lock )
321 return p_picture->pf_lock( p_vout, p_picture );
324 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
326 if( p_picture->pf_unlock )
327 p_picture->pf_unlock( p_vout, p_picture );
333 * This function chooses whether the current picture needs to be copied
334 * before rendering, does the subpicture magic, and tells the video output
335 * thread which direct buffer needs to be displayed.
337 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
338 subpicture_t *p_subpic, mtime_t render_date )
343 if( p_pic->i_type == DIRECT_PICTURE )
345 /* Picture is in a direct buffer. */
347 if( p_subpic != NULL )
349 /* We have subtitles. First copy the picture to
350 * the spare direct buffer, then render the
352 if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
355 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
357 spu_RenderSubpictures( p_vout->p_spu,
358 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
359 p_subpic, &p_vout->fmt_in, render_date );
361 vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
363 return PP_OUTPUTPICTURE[0];
366 /* No subtitles, picture is in a directbuffer so
367 * we can display it directly (even if it is still
372 /* Not a direct buffer. We either need to copy it to a direct buffer,
373 * or render it if the chroma isn't the same. */
374 if( p_vout->p->b_direct )
376 /* Picture is not in a direct buffer, but is exactly the
377 * same size as the direct buffers. A memcpy() is enough,
378 * then render the subtitles. */
380 if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
383 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
384 spu_RenderSubpictures( p_vout->p_spu,
385 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
386 p_subpic, &p_vout->fmt_in, render_date );
388 vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
390 return PP_OUTPUTPICTURE[0];
393 /* Picture is not in a direct buffer, and needs to be converted to
394 * another size/chroma. Then the subtitles need to be rendered as
395 * well. This usually means software YUV, or hardware YUV with a
396 * different chroma. */
398 if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
400 /* The picture buffer is in slow memory. We'll use
401 * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
402 * one for subpictures rendering. */
403 picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
404 if( p_tmp_pic->i_status == FREE_PICTURE )
406 vout_AllocatePicture( VLC_OBJECT(p_vout),
407 p_tmp_pic, p_vout->fmt_out.i_chroma,
408 p_vout->fmt_out.i_width,
409 p_vout->fmt_out.i_height,
410 p_vout->fmt_out.i_aspect );
411 p_tmp_pic->i_type = MEMORY_PICTURE;
412 p_tmp_pic->i_status = RESERVED_PICTURE;
415 /* Convert image to the first direct buffer */
416 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
417 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
419 /* Render subpictures on the first direct buffer */
420 spu_RenderSubpictures( p_vout->p_spu,
421 p_tmp_pic, &p_vout->fmt_out,
422 p_subpic, &p_vout->fmt_in, render_date );
424 if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
427 picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
431 if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
434 /* Convert image to the first direct buffer */
435 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
436 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
438 /* Render subpictures on the first direct buffer */
439 spu_RenderSubpictures( p_vout->p_spu,
440 &p_vout->p_picture[0], &p_vout->fmt_out,
441 p_subpic, &p_vout->fmt_in, render_date );
444 vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
446 return &p_vout->p_picture[0];
450 * Calculate image window coordinates
452 * This function will be accessed by plugins. It calculates the relative
453 * position of the output window and the image window.
455 void vout_PlacePicture( const vout_thread_t *p_vout,
456 unsigned int i_width, unsigned int i_height,
457 unsigned int *restrict pi_x,
458 unsigned int *restrict pi_y,
459 unsigned int *restrict pi_width,
460 unsigned int *restrict pi_height )
462 if( i_width <= 0 || i_height <= 0 )
464 *pi_width = *pi_height = *pi_x = *pi_y = 0;
468 if( p_vout->b_autoscale )
471 *pi_height = i_height;
475 int i_zoom = p_vout->i_zoom;
476 /* be realistic, scaling factor confined between .2 and 10. */
477 if( i_zoom > 10 * ZOOM_FP_FACTOR ) i_zoom = 10 * ZOOM_FP_FACTOR;
478 else if( i_zoom < ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
480 unsigned int i_original_width, i_original_height;
482 if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
484 i_original_width = p_vout->fmt_in.i_visible_width *
485 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
486 i_original_height = p_vout->fmt_in.i_visible_height;
490 i_original_width = p_vout->fmt_in.i_visible_width;
491 i_original_height = p_vout->fmt_in.i_visible_height *
492 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
495 /* On windows, inner video window exceeding container leads to black screen */
496 *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
497 *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
499 *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
500 *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
504 int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
505 *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
506 int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
507 *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
509 if( i_scaled_width <= 0 || i_scaled_height <= 0 )
511 msg_Warn( p_vout, "ignoring broken aspect ratio" );
512 i_scaled_width = *pi_width;
513 i_scaled_height = *pi_height;
516 if( i_scaled_width > *pi_width )
517 *pi_height = i_scaled_height;
519 *pi_width = i_scaled_width;
521 switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
523 case VOUT_ALIGN_LEFT:
526 case VOUT_ALIGN_RIGHT:
527 *pi_x = i_width - *pi_width;
530 *pi_x = ( i_width - *pi_width ) / 2;
533 switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
538 case VOUT_ALIGN_BOTTOM:
539 *pi_y = i_height - *pi_height;
542 *pi_y = ( i_height - *pi_height ) / 2;
547 * Allocate a new picture in the heap.
549 * This function allocates a fake direct buffer in memory, which can be
550 * used exactly like a video buffer. The video output thread then manages
551 * how it gets displayed.
553 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
554 vlc_fourcc_t i_chroma,
555 int i_width, int i_height, int i_aspect )
557 int i_bytes, i_index, i_width_aligned, i_height_aligned;
559 /* Make sure the real dimensions are a multiple of 16 */
560 i_width_aligned = (i_width + 15) >> 4 << 4;
561 i_height_aligned = (i_height + 15) >> 4 << 4;
563 if( picture_Setup( p_pic, i_chroma,
564 i_width, i_height, i_aspect ) != VLC_SUCCESS )
570 /* Calculate how big the new image should be */
571 i_bytes = p_pic->format.i_bits_per_pixel *
572 i_width_aligned * i_height_aligned / 8;
574 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
576 if( p_pic->p_data == NULL )
582 /* Fill the p_pixels field for each plane */
583 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
585 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
587 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
588 p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
595 * Compare two chroma values
597 * This function returns 1 if the two fourcc values given as argument are
598 * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
600 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
602 static const vlc_fourcc_t p_I420[] = {
603 VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
605 static const vlc_fourcc_t p_I422[] = {
606 VLC_CODEC_I422, VLC_CODEC_J422, 0
608 static const vlc_fourcc_t p_I440[] = {
609 VLC_CODEC_I440, VLC_CODEC_J440, 0
611 static const vlc_fourcc_t p_I444[] = {
612 VLC_CODEC_I444, VLC_CODEC_J444, 0
614 static const vlc_fourcc_t *pp_fcc[] = {
615 p_I420, p_I422, p_I440, p_I444, NULL
619 i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
620 i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
622 /* If they are the same, they are the same ! */
623 if( i_chroma == i_amorhc )
626 /* Check for equivalence classes */
627 for( int i = 0; pp_fcc[i] != NULL; i++ )
631 for( int j = 0; pp_fcc[i][j] != 0; j++ )
633 if( i_chroma == pp_fcc[i][j] )
635 if( i_amorhc == pp_fcc[i][j] )
638 if( b_fcc1 && b_fcc2 )
644 /*****************************************************************************
646 *****************************************************************************/
647 static void PictureReleaseCallback( picture_t *p_picture )
649 if( --p_picture->i_refcount > 0 )
651 picture_Delete( p_picture );
654 /*****************************************************************************
656 *****************************************************************************/
657 void picture_Reset( picture_t *p_picture )
660 p_picture->date = VLC_TS_INVALID;
661 p_picture->b_force = false;
662 p_picture->b_progressive = false;
663 p_picture->i_nb_fields = 0;
664 p_picture->b_top_field_first = false;
665 picture_CleanupQuant( p_picture );
668 /*****************************************************************************
670 *****************************************************************************/
671 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
673 int i_index, i_width_aligned, i_height_aligned;
675 /* Store default values */
676 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
678 p_picture->p[i_index].p_pixels = NULL;
679 p_picture->p[i_index].i_pixel_pitch = 1;
682 p_picture->pf_release = NULL;
683 p_picture->p_release_sys = NULL;
684 p_picture->pf_lock = NULL;
685 p_picture->pf_unlock = NULL;
686 p_picture->i_refcount = 0;
688 p_picture->i_qtype = QTYPE_NONE;
689 p_picture->i_qstride = 0;
690 p_picture->p_q = NULL;
692 video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
694 /* Make sure the real dimensions are a multiple of 16 */
695 i_width_aligned = (i_width + 15) >> 4 << 4;
696 i_height_aligned = (i_height + 15) >> 4 << 4;
698 /* Calculate coordinates */
699 switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
702 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
703 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
704 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
705 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
706 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
707 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
708 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
709 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
710 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
711 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
712 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
713 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
714 p_picture->i_planes = 3;
718 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
719 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
720 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
721 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
722 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
723 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
724 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
725 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
726 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
727 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
728 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
729 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
730 p_picture->i_planes = 3;
736 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
737 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
738 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
739 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
740 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
741 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
742 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
743 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
744 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
745 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
746 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
747 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
748 p_picture->i_planes = 3;
753 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
754 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
755 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
756 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
757 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
758 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
759 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
760 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
761 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
762 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
763 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
764 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
765 p_picture->i_planes = 3;
770 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
771 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
772 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
773 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
774 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
775 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
776 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
777 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
778 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
779 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
780 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
781 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
782 p_picture->i_planes = 3;
787 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
788 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
789 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
790 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
791 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
792 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
793 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
794 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
795 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
796 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
797 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
798 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
799 p_picture->i_planes = 3;
803 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
804 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
805 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
806 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
807 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
808 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
809 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
810 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
811 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
812 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
813 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
814 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
815 p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
816 p_picture->p[ A_PLANE ].i_visible_lines = i_height;
817 p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
818 p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
819 p_picture->i_planes = 4;
823 p_picture->p->i_lines = i_height_aligned;
824 p_picture->p->i_visible_lines = i_height;
825 p_picture->p->i_pitch = i_width_aligned;
826 p_picture->p->i_visible_pitch = i_width;
827 p_picture->p->i_pixel_pitch = 8;
828 p_picture->i_planes = 1;
832 p_picture->p->i_lines = i_height_aligned;
833 p_picture->p->i_visible_lines = i_height;
834 p_picture->p->i_pitch = i_width_aligned;
835 p_picture->p->i_visible_pitch = i_width;
836 p_picture->p->i_pixel_pitch = 4;
837 p_picture->i_planes = 1;
844 p_picture->p->i_lines = i_height_aligned;
845 p_picture->p->i_visible_lines = i_height;
846 p_picture->p->i_pitch = i_width_aligned * 2;
847 p_picture->p->i_visible_pitch = i_width * 2;
848 p_picture->p->i_pixel_pitch = 4;
849 p_picture->i_planes = 1;
853 p_picture->p->i_lines = i_height_aligned;
854 p_picture->p->i_visible_lines = i_height;
855 p_picture->p->i_pitch = i_width_aligned;
856 p_picture->p->i_visible_pitch = i_width;
857 p_picture->p->i_pixel_pitch = 1;
858 p_picture->i_planes = 1;
861 case VLC_CODEC_RGB15:
862 p_picture->p->i_lines = i_height_aligned;
863 p_picture->p->i_visible_lines = i_height;
864 p_picture->p->i_pitch = i_width_aligned * 2;
865 p_picture->p->i_visible_pitch = i_width * 2;
866 p_picture->p->i_pixel_pitch = 2;
867 p_picture->i_planes = 1;
870 case VLC_CODEC_RGB16:
871 p_picture->p->i_lines = i_height_aligned;
872 p_picture->p->i_visible_lines = i_height;
873 p_picture->p->i_pitch = i_width_aligned * 2;
874 p_picture->p->i_visible_pitch = i_width * 2;
875 p_picture->p->i_pixel_pitch = 2;
876 p_picture->i_planes = 1;
879 case VLC_CODEC_RGB24:
880 p_picture->p->i_lines = i_height_aligned;
881 p_picture->p->i_visible_lines = i_height;
882 p_picture->p->i_pitch = i_width_aligned * 3;
883 p_picture->p->i_visible_pitch = i_width * 3;
884 p_picture->p->i_pixel_pitch = 3;
885 p_picture->i_planes = 1;
888 case VLC_CODEC_RGB32:
890 p_picture->p->i_lines = i_height_aligned;
891 p_picture->p->i_visible_lines = i_height;
892 p_picture->p->i_pitch = i_width_aligned * 4;
893 p_picture->p->i_visible_pitch = i_width * 4;
894 p_picture->p->i_pixel_pitch = 4;
895 p_picture->i_planes = 1;
900 p_picture->p->i_lines = i_height_aligned;
901 p_picture->p->i_visible_lines = i_height;
902 p_picture->p->i_pitch = i_width_aligned;
903 p_picture->p->i_visible_pitch = i_width;
904 p_picture->p->i_pixel_pitch = 1;
905 p_picture->i_planes = 1;
909 p_picture->i_planes = 0;
916 /*****************************************************************************
918 *****************************************************************************/
919 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
921 video_format_t fmt = *p_fmt;
923 /* It is needed to be sure all informations are filled */
924 video_format_Setup( &fmt, p_fmt->i_chroma,
925 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
928 picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
934 if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
939 p_picture->p_sys = p_resource->p_sys;
941 for( int i = 0; i < p_picture->i_planes; i++ )
943 p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
944 p_picture->p[i].i_lines = p_resource->p[i].i_lines;
945 p_picture->p[i].i_pitch = p_resource->p[i].i_pitch;
950 if( __vout_AllocatePicture( NULL, p_picture,
951 fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
958 p_picture->format = fmt;
959 p_picture->i_refcount = 1;
960 p_picture->pf_release = PictureReleaseCallback;
961 p_picture->i_status = RESERVED_PICTURE;
965 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
967 return picture_NewFromResource( p_fmt, NULL );
969 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
973 memset( &fmt, 0, sizeof(fmt) );
974 video_format_Setup( &fmt, i_chroma, i_width, i_height, i_aspect );
976 return picture_NewFromFormat( &fmt );
979 /*****************************************************************************
981 *****************************************************************************/
982 void picture_Delete( picture_t *p_picture )
984 assert( p_picture && p_picture->i_refcount == 0 );
985 assert( p_picture->p_release_sys == NULL );
987 free( p_picture->p_q );
988 free( p_picture->p_data_orig );
989 free( p_picture->p_sys );
993 /*****************************************************************************
995 *****************************************************************************/
996 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1000 for( i = 0; i < p_src->i_planes ; i++ )
1001 plane_CopyPixels( p_dst->p+i, p_src->p+i );
1004 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1006 const unsigned i_width = __MIN( p_dst->i_visible_pitch,
1007 p_src->i_visible_pitch );
1008 const unsigned i_height = __MIN( p_dst->i_visible_lines,
1009 p_src->i_visible_lines );
1011 if( p_src->i_pitch == p_dst->i_pitch )
1013 /* There are margins, but with the same width : perfect ! */
1014 vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1015 p_src->i_pitch * i_height );
1019 /* We need to proceed line by line */
1020 uint8_t *p_in = p_src->p_pixels;
1021 uint8_t *p_out = p_dst->p_pixels;
1027 for( i_line = i_height; i_line--; )
1029 vlc_memcpy( p_out, p_in, i_width );
1030 p_in += p_src->i_pitch;
1031 p_out += p_dst->i_pitch;
1036 /*****************************************************************************
1038 *****************************************************************************/
1039 int picture_Export( vlc_object_t *p_obj,
1041 video_format_t *p_fmt,
1042 picture_t *p_picture,
1043 vlc_fourcc_t i_format,
1044 int i_override_width, int i_override_height )
1047 video_format_t fmt_in = p_picture->format;
1048 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1051 fmt_in.i_sar_den = 1;
1055 video_format_t fmt_out;
1056 memset( &fmt_out, 0, sizeof(fmt_out) );
1058 fmt_out.i_sar_den = 1;
1059 fmt_out.i_chroma = i_format;
1061 /* compute original width/height */
1062 unsigned int i_original_width;
1063 unsigned int i_original_height;
1064 if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1066 i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1067 i_original_height = fmt_in.i_height;
1071 i_original_width = fmt_in.i_width;
1072 i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1076 fmt_out.i_width = ( i_override_width < 0 ) ?
1077 i_original_width : i_override_width;
1078 fmt_out.i_height = ( i_override_height < 0 ) ?
1079 i_original_height : i_override_height;
1081 /* scale if only one direction is provided */
1082 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1084 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1085 * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1087 else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1089 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height
1090 * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1093 image_handler_t *p_image = image_HandlerCreate( p_obj );
1095 block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1097 image_HandlerDelete( p_image );
1100 return VLC_EGENERIC;
1103 p_block->i_dts = p_picture->date;
1107 *pp_image = p_block;
1112 /*****************************************************************************
1114 *****************************************************************************/
1115 struct picture_fifo_t
1119 picture_t **pp_last;
1122 static void PictureFifoReset( picture_fifo_t *p_fifo )
1124 p_fifo->p_first = NULL;
1125 p_fifo->pp_last = &p_fifo->p_first;
1127 static void PictureFifoPush( picture_fifo_t *p_fifo, picture_t *p_picture )
1129 assert( !p_picture->p_next );
1130 *p_fifo->pp_last = p_picture;
1131 p_fifo->pp_last = &p_picture->p_next;
1133 static picture_t *PictureFifoPop( picture_fifo_t *p_fifo )
1135 picture_t *p_picture = p_fifo->p_first;
1139 p_fifo->p_first = p_picture->p_next;
1140 if( !p_fifo->p_first )
1141 p_fifo->pp_last = &p_fifo->p_first;
1146 picture_fifo_t *picture_fifo_New(void)
1148 picture_fifo_t *p_fifo = malloc( sizeof(*p_fifo) );
1152 vlc_mutex_init( &p_fifo->lock );
1153 PictureFifoReset( p_fifo );
1157 void picture_fifo_Push( picture_fifo_t *p_fifo, picture_t *p_picture )
1159 vlc_mutex_lock( &p_fifo->lock );
1160 PictureFifoPush( p_fifo, p_picture );
1161 vlc_mutex_unlock( &p_fifo->lock );
1163 picture_t *picture_fifo_Pop( picture_fifo_t *p_fifo )
1165 vlc_mutex_lock( &p_fifo->lock );
1166 picture_t *p_picture = PictureFifoPop( p_fifo );
1167 vlc_mutex_unlock( &p_fifo->lock );
1171 picture_t *picture_fifo_Peek( picture_fifo_t *p_fifo )
1173 vlc_mutex_lock( &p_fifo->lock );
1174 picture_t *p_picture = p_fifo->p_first;
1176 picture_Hold( p_picture );
1177 vlc_mutex_unlock( &p_fifo->lock );
1181 void picture_fifo_Flush( picture_fifo_t *p_fifo, mtime_t i_date, bool b_below )
1183 picture_t *p_picture;
1185 vlc_mutex_lock( &p_fifo->lock );
1187 p_picture = p_fifo->p_first;
1188 PictureFifoReset( p_fifo );
1191 PictureFifoReset( &tmp );
1195 picture_t *p_next = p_picture->p_next;
1197 p_picture->p_next = NULL;
1198 if( ( b_below && p_picture->date <= i_date ) ||
1199 ( !b_below && p_picture->date >= i_date ) )
1200 PictureFifoPush( &tmp, p_picture );
1202 PictureFifoPush( p_fifo, p_picture );
1205 vlc_mutex_unlock( &p_fifo->lock );
1209 picture_t *p_picture = PictureFifoPop( &tmp );
1212 picture_Release( p_picture );
1215 void picture_fifo_OffsetDate( picture_fifo_t *p_fifo, mtime_t i_delta )
1217 vlc_mutex_lock( &p_fifo->lock );
1218 for( picture_t *p_picture = p_fifo->p_first; p_picture != NULL; )
1220 p_picture->date += i_delta;
1221 p_picture = p_picture->p_next;
1223 vlc_mutex_unlock( &p_fifo->lock );
1225 void picture_fifo_Delete( picture_fifo_t *p_fifo )
1227 picture_fifo_Flush( p_fifo, INT64_MAX, true );
1228 vlc_mutex_destroy( &p_fifo->lock );
1231 /*****************************************************************************
1233 *****************************************************************************/
1234 struct picture_release_sys_t
1237 void (*pf_release)( picture_t * );
1238 picture_release_sys_t *p_release_sys;
1244 struct picture_pool_t
1249 picture_t **pp_picture;
1252 static void PicturePoolPictureRelease( picture_t * );
1254 picture_pool_t *picture_pool_New( int i_picture, picture_t *pp_picture[] )
1256 picture_pool_t *p_pool = calloc( 1, sizeof(*p_pool) );
1261 p_pool->i_picture = i_picture;
1262 p_pool->pp_picture = calloc( p_pool->i_picture, sizeof(*p_pool->pp_picture) );
1264 for( int i = 0; i < i_picture; i++ )
1266 picture_t *p_picture = pp_picture[i];
1268 /* The pool must be the only owner of the picture */
1269 assert( p_picture->i_refcount == 1 );
1271 /* Install the new release callback */
1272 picture_release_sys_t *p_release_sys = malloc( sizeof(*p_release_sys) );
1273 p_release_sys->pf_release = p_picture->pf_release;
1274 p_release_sys->p_release_sys = p_picture->p_release_sys;
1275 p_release_sys->i_tick = 0;
1277 p_picture->i_refcount = 0;
1278 p_picture->pf_release = PicturePoolPictureRelease;
1279 p_picture->p_release_sys = p_release_sys;
1282 p_pool->pp_picture[i] = p_picture;
1287 picture_pool_t *picture_pool_NewFromFormat( const video_format_t *p_fmt, int i_picture )
1289 picture_t *pp_picture[i_picture];
1291 for( int i = 0; i < i_picture; i++ )
1293 pp_picture[i] = picture_New( p_fmt->i_chroma,
1294 p_fmt->i_width, p_fmt->i_height,
1296 if( !pp_picture[i] )
1299 picture_pool_t *p_pool = picture_pool_New( i_picture, pp_picture );
1306 for( int i = 0; i < i_picture; i++ )
1308 if( !pp_picture[i] )
1310 picture_Release( pp_picture[i] );
1315 void picture_pool_Delete( picture_pool_t *p_pool )
1317 for( int i = 0; i < p_pool->i_picture; i++ )
1319 picture_t *p_picture = p_pool->pp_picture[i];
1320 picture_release_sys_t *p_release_sys = p_picture->p_release_sys;
1322 assert( p_picture->i_refcount == 0 );
1324 /* Restore old release callback */
1325 p_picture->i_refcount = 1;
1326 p_picture->pf_release = p_release_sys->pf_release;
1327 p_picture->p_release_sys = p_release_sys->p_release_sys;
1329 picture_Release( p_picture );
1331 free( p_release_sys );
1336 picture_t *picture_pool_Get( picture_pool_t *p_pool )
1338 for( int i = 0; i < p_pool->i_picture; i++ )
1340 picture_t *p_picture = p_pool->pp_picture[i];
1342 if( p_picture->i_refcount <= 0 )
1344 p_picture->p_release_sys->i_tick = p_pool->i_tick++;
1345 picture_Hold( p_picture );
1352 void picture_pool_NonEmpty( picture_pool_t *p_pool, bool b_reset )
1354 picture_t *p_old = NULL;
1356 for( int i = 0; i < p_pool->i_picture; i++ )
1358 picture_t *p_picture = p_pool->pp_picture[i];
1361 p_picture->i_refcount = 0;
1362 else if( p_picture->i_refcount == 0 )
1364 else if( !p_old || p_picture->p_release_sys->i_tick < p_old->p_release_sys->i_tick )
1367 if( !b_reset && p_old )
1368 p_old->i_refcount = 0;
1371 static void PicturePoolPictureRelease( picture_t *p_picture )
1373 assert( p_picture->i_refcount > 0 );
1375 if( --p_picture->i_refcount > 0 )
1378 /* Nothing to do for the moment */