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;
68 p_vout->p->b_picture_interlaced = !p_pic->b_progressive;
70 vlc_mutex_unlock( &p_vout->picture_lock );
74 * Allocate a picture in the video output heap.
76 * This function creates a reserved image in the video output heap.
77 * A null pointer is returned if the function fails. This method provides an
78 * already allocated zone of memory in the picture data fields.
79 * It needs locking since several pictures can be created by several producers
82 int vout_CountPictureAvailable( vout_thread_t *p_vout )
87 vlc_mutex_lock( &p_vout->picture_lock );
88 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
90 picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
92 switch( p_pic->i_status )
94 case DESTROYED_PICTURE:
106 vlc_mutex_unlock( &p_vout->picture_lock );
111 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
113 bool b_top_field_first,
114 unsigned int i_nb_fields )
116 int i_pic; /* picture index */
118 picture_t * p_freepic = NULL; /* first free picture */
121 vlc_mutex_lock( &p_vout->picture_lock );
124 * Look for an empty place in the picture heap.
126 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
128 p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
131 switch( p_pic->i_status )
133 case DESTROYED_PICTURE:
134 /* Memory will not be reallocated, and function can end
135 * immediately - this is the best possible case, since no
136 * memory allocation needs to be done */
137 p_pic->i_status = RESERVED_PICTURE;
138 p_pic->i_refcount = 0;
141 p_pic->b_progressive = b_progressive;
142 p_pic->i_nb_fields = i_nb_fields;
143 p_pic->b_top_field_first = b_top_field_first;
145 p_vout->i_heap_size++;
146 p_vout->render.i_last_used_pic =
147 ( p_vout->render.i_last_used_pic + i_pic + 1 )
149 vlc_mutex_unlock( &p_vout->picture_lock );
153 /* Picture is empty and ready for allocation */
154 p_vout->render.i_last_used_pic =
155 ( p_vout->render.i_last_used_pic + i_pic + 1 )
168 if( p_freepic != NULL )
170 vout_AllocatePicture( VLC_OBJECT(p_vout),
171 p_freepic, p_vout->render.i_chroma,
172 p_vout->render.i_width, p_vout->render.i_height,
173 p_vout->render.i_aspect );
175 if( p_freepic->i_planes )
177 /* Copy picture information, set some default values */
178 p_freepic->i_status = RESERVED_PICTURE;
179 p_freepic->i_type = MEMORY_PICTURE;
180 p_freepic->b_slow = 0;
182 p_freepic->i_refcount = 0;
183 p_freepic->b_force = 0;
185 p_freepic->b_progressive = b_progressive;
186 p_freepic->i_nb_fields = i_nb_fields;
187 p_freepic->b_top_field_first = b_top_field_first;
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 );
213 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
215 vlc_assert_locked( &p_vout->picture_lock );
217 p_picture->i_status = DESTROYED_PICTURE;
218 p_vout->i_heap_size--;
219 picture_CleanupQuant( p_picture );
221 vlc_cond_signal( &p_vout->p->picture_wait );
225 * Remove a permanent or reserved picture from the heap
227 * This function frees a previously reserved picture or a permanent
228 * picture. It is meant to be used when the construction of a picture aborted.
229 * Note that the picture will be destroyed even if it is linked !
231 * TODO remove it, vout_DropPicture should be used instead
233 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
236 /* Check if picture status is valid */
237 vlc_mutex_lock( &p_vout->picture_lock );
238 if( p_pic->i_status != RESERVED_PICTURE )
240 msg_Err( p_vout, "picture to destroy %p has invalid status %d",
241 p_pic, p_pic->i_status );
243 vlc_mutex_unlock( &p_vout->picture_lock );
246 vout_DropPicture( p_vout, p_pic );
250 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
252 vlc_assert_locked( &p_vout->picture_lock );
253 if( p_picture->i_refcount > 0 )
255 /* Pretend we displayed the picture, but don't destroy
256 * it since the decoder might still need it. */
257 p_picture->i_status = DISPLAYED_PICTURE;
261 /* Destroy the picture without displaying it */
262 DestroyPicture( p_vout, p_picture );
267 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic )
269 vlc_mutex_lock( &p_vout->picture_lock );
271 if( p_pic->i_status == READY_PICTURE )
273 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
275 vlc_cond_signal( &p_vout->p->picture_wait );
279 vout_UsePictureLocked( p_vout, p_pic );
282 vlc_mutex_unlock( &p_vout->picture_lock );
286 * Increment reference counter of a picture
288 * This function increments the reference counter of a picture in the video
289 * heap. It needs a lock since several producer threads can access the picture.
291 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
293 vlc_mutex_lock( &p_vout->picture_lock );
295 vlc_mutex_unlock( &p_vout->picture_lock );
299 * Decrement reference counter of a picture
301 * This function decrement the reference counter of a picture in the video heap
303 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
305 vlc_mutex_lock( &p_vout->picture_lock );
307 if( p_pic->i_refcount > 0 )
310 msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
311 p_pic, p_pic->i_refcount );
313 if( p_pic->i_refcount == 0 && p_pic->i_status == DISPLAYED_PICTURE )
314 DestroyPicture( p_vout, p_pic );
316 vlc_mutex_unlock( &p_vout->picture_lock );
319 static int vout_LockPicture( vout_thread_t *p_vout, picture_t *p_picture )
321 if( p_picture->pf_lock )
322 return p_picture->pf_lock( p_vout, p_picture );
325 static void vout_UnlockPicture( vout_thread_t *p_vout, picture_t *p_picture )
327 if( p_picture->pf_unlock )
328 p_picture->pf_unlock( p_vout, p_picture );
334 * This function chooses whether the current picture needs to be copied
335 * before rendering, does the subpicture magic, and tells the video output
336 * thread which direct buffer needs to be displayed.
338 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
339 subpicture_t *p_subpic, mtime_t render_date )
344 if( p_pic->i_type == DIRECT_PICTURE )
346 /* Picture is in a direct buffer. */
348 if( p_subpic != NULL )
350 /* We have subtitles. First copy the picture to
351 * the spare direct buffer, then render the
353 if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
356 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
358 spu_RenderSubpictures( p_vout->p_spu,
359 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
360 p_subpic, &p_vout->fmt_in, render_date );
362 vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
364 return PP_OUTPUTPICTURE[0];
367 /* No subtitles, picture is in a directbuffer so
368 * we can display it directly (even if it is still
373 /* Not a direct buffer. We either need to copy it to a direct buffer,
374 * or render it if the chroma isn't the same. */
375 if( p_vout->p->b_direct )
377 /* Picture is not in a direct buffer, but is exactly the
378 * same size as the direct buffers. A memcpy() is enough,
379 * then render the subtitles. */
381 if( vout_LockPicture( p_vout, PP_OUTPUTPICTURE[0] ) )
384 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
385 spu_RenderSubpictures( p_vout->p_spu,
386 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
387 p_subpic, &p_vout->fmt_in, render_date );
389 vout_UnlockPicture( p_vout, PP_OUTPUTPICTURE[0] );
391 return PP_OUTPUTPICTURE[0];
394 /* Picture is not in a direct buffer, and needs to be converted to
395 * another size/chroma. Then the subtitles need to be rendered as
396 * well. This usually means software YUV, or hardware YUV with a
397 * different chroma. */
399 if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
401 /* The picture buffer is in slow memory. We'll use
402 * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
403 * one for subpictures rendering. */
404 picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
405 if( p_tmp_pic->i_status == FREE_PICTURE )
407 vout_AllocatePicture( VLC_OBJECT(p_vout),
408 p_tmp_pic, p_vout->fmt_out.i_chroma,
409 p_vout->fmt_out.i_width,
410 p_vout->fmt_out.i_height,
411 p_vout->fmt_out.i_aspect );
412 p_tmp_pic->i_type = MEMORY_PICTURE;
413 p_tmp_pic->i_status = RESERVED_PICTURE;
416 /* Convert image to the first direct buffer */
417 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
418 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
420 /* Render subpictures on the first direct buffer */
421 spu_RenderSubpictures( p_vout->p_spu,
422 p_tmp_pic, &p_vout->fmt_out,
423 p_subpic, &p_vout->fmt_in, render_date );
425 if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
428 picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
432 if( vout_LockPicture( p_vout, &p_vout->p_picture[0] ) )
435 /* Convert image to the first direct buffer */
436 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
437 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
439 /* Render subpictures on the first direct buffer */
440 spu_RenderSubpictures( p_vout->p_spu,
441 &p_vout->p_picture[0], &p_vout->fmt_out,
442 p_subpic, &p_vout->fmt_in, render_date );
445 vout_UnlockPicture( p_vout, &p_vout->p_picture[0] );
447 return &p_vout->p_picture[0];
451 * Calculate image window coordinates
453 * This function will be accessed by plugins. It calculates the relative
454 * position of the output window and the image window.
456 void vout_PlacePicture( const vout_thread_t *p_vout,
457 unsigned int i_width, unsigned int i_height,
458 unsigned int *restrict pi_x,
459 unsigned int *restrict pi_y,
460 unsigned int *restrict pi_width,
461 unsigned int *restrict pi_height )
463 if( i_width <= 0 || i_height <= 0 )
465 *pi_width = *pi_height = *pi_x = *pi_y = 0;
469 if( p_vout->b_autoscale )
472 *pi_height = i_height;
476 int i_zoom = p_vout->i_zoom;
477 /* be realistic, scaling factor confined between .2 and 10. */
478 if( i_zoom > 10 * ZOOM_FP_FACTOR ) i_zoom = 10 * ZOOM_FP_FACTOR;
479 else if( i_zoom < ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
481 unsigned int i_original_width, i_original_height;
483 if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
485 i_original_width = p_vout->fmt_in.i_visible_width *
486 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
487 i_original_height = p_vout->fmt_in.i_visible_height;
491 i_original_width = p_vout->fmt_in.i_visible_width;
492 i_original_height = p_vout->fmt_in.i_visible_height *
493 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
496 /* On windows, inner video window exceeding container leads to black screen */
497 *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
498 *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
500 *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
501 *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
505 int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
506 *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
507 int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
508 *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
510 if( i_scaled_width <= 0 || i_scaled_height <= 0 )
512 msg_Warn( p_vout, "ignoring broken aspect ratio" );
513 i_scaled_width = *pi_width;
514 i_scaled_height = *pi_height;
517 if( i_scaled_width > *pi_width )
518 *pi_height = i_scaled_height;
520 *pi_width = i_scaled_width;
522 switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
524 case VOUT_ALIGN_LEFT:
527 case VOUT_ALIGN_RIGHT:
528 *pi_x = i_width - *pi_width;
531 *pi_x = ( i_width - *pi_width ) / 2;
534 switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
539 case VOUT_ALIGN_BOTTOM:
540 *pi_y = i_height - *pi_height;
543 *pi_y = ( i_height - *pi_height ) / 2;
548 * Allocate a new picture in the heap.
550 * This function allocates a fake direct buffer in memory, which can be
551 * used exactly like a video buffer. The video output thread then manages
552 * how it gets displayed.
554 int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
555 vlc_fourcc_t i_chroma,
556 int i_width, int i_height, int i_aspect )
559 int i_index, i_width_aligned, i_height_aligned;
561 /* Make sure the real dimensions are a multiple of 16 */
562 i_width_aligned = (i_width + 15) >> 4 << 4;
563 i_height_aligned = (i_height + 15) >> 4 << 4;
565 if( picture_Setup( p_pic, i_chroma,
566 i_width, i_height, i_aspect ) != VLC_SUCCESS )
572 /* Calculate how big the new image should be */
573 size_t i_bytes = (size_t)p_pic->format.i_bits_per_pixel *
574 i_width_aligned * i_height_aligned / 8;
576 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
578 if( p_pic->p_data == NULL )
584 /* Fill the p_pixels field for each plane */
585 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
587 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
589 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
590 p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
597 * Compare two chroma values
599 * This function returns 1 if the two fourcc values given as argument are
600 * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
602 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
604 static const vlc_fourcc_t p_I420[] = {
605 VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
607 static const vlc_fourcc_t p_I422[] = {
608 VLC_CODEC_I422, VLC_CODEC_J422, 0
610 static const vlc_fourcc_t p_I440[] = {
611 VLC_CODEC_I440, VLC_CODEC_J440, 0
613 static const vlc_fourcc_t p_I444[] = {
614 VLC_CODEC_I444, VLC_CODEC_J444, 0
616 static const vlc_fourcc_t *pp_fcc[] = {
617 p_I420, p_I422, p_I440, p_I444, NULL
621 i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
622 i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
624 /* If they are the same, they are the same ! */
625 if( i_chroma == i_amorhc )
628 /* Check for equivalence classes */
629 for( int i = 0; pp_fcc[i] != NULL; i++ )
633 for( int j = 0; pp_fcc[i][j] != 0; j++ )
635 if( i_chroma == pp_fcc[i][j] )
637 if( i_amorhc == pp_fcc[i][j] )
640 if( b_fcc1 && b_fcc2 )
646 /*****************************************************************************
648 *****************************************************************************/
649 static void PictureReleaseCallback( picture_t *p_picture )
651 if( --p_picture->i_refcount > 0 )
653 picture_Delete( p_picture );
656 /*****************************************************************************
658 *****************************************************************************/
659 void picture_Reset( picture_t *p_picture )
662 p_picture->date = VLC_TS_INVALID;
663 p_picture->b_force = false;
664 p_picture->b_progressive = false;
665 p_picture->i_nb_fields = 0;
666 p_picture->b_top_field_first = false;
667 picture_CleanupQuant( p_picture );
670 /*****************************************************************************
672 *****************************************************************************/
673 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
675 int i_index, i_width_aligned, i_height_aligned;
677 /* Store default values */
678 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
680 p_picture->p[i_index].p_pixels = NULL;
681 p_picture->p[i_index].i_pixel_pitch = 1;
684 p_picture->pf_release = NULL;
685 p_picture->p_release_sys = NULL;
686 p_picture->pf_lock = NULL;
687 p_picture->pf_unlock = NULL;
688 p_picture->i_refcount = 0;
690 p_picture->i_qtype = QTYPE_NONE;
691 p_picture->i_qstride = 0;
692 p_picture->p_q = NULL;
694 video_format_Setup( &p_picture->format, i_chroma, i_width, i_height, i_aspect );
696 /* Make sure the real dimensions are a multiple of 16 */
697 i_width_aligned = (i_width + 15) >> 4 << 4;
698 i_height_aligned = (i_height + 15) >> 4 << 4;
700 /* Calculate coordinates */
701 switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
704 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
705 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
706 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
707 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
708 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
709 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
710 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
711 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
712 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
713 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
714 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
715 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
716 p_picture->i_planes = 3;
720 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
721 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
722 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
723 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
724 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
725 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
726 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
727 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
728 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
729 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
730 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
731 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
732 p_picture->i_planes = 3;
738 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
739 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
740 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
741 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
742 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
743 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
744 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
745 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
746 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
747 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
748 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
749 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
750 p_picture->i_planes = 3;
755 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
756 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
757 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
758 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
759 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
760 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
761 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
762 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
763 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
764 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
765 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
766 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
767 p_picture->i_planes = 3;
772 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
773 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
774 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
775 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
776 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
777 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
778 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
779 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
780 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
781 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
782 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
783 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
784 p_picture->i_planes = 3;
789 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
790 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
791 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
792 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
793 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
794 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
795 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
796 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
797 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
798 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
799 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
800 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
801 p_picture->i_planes = 3;
805 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
806 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
807 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
808 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
809 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
810 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
811 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
812 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
813 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
814 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
815 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
816 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
817 p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
818 p_picture->p[ A_PLANE ].i_visible_lines = i_height;
819 p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
820 p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
821 p_picture->i_planes = 4;
825 p_picture->p->i_lines = i_height_aligned;
826 p_picture->p->i_visible_lines = i_height;
827 p_picture->p->i_pitch = i_width_aligned;
828 p_picture->p->i_visible_pitch = i_width;
829 p_picture->p->i_pixel_pitch = 1;
830 p_picture->i_planes = 1;
834 p_picture->p->i_lines = i_height_aligned;
835 p_picture->p->i_visible_lines = i_height;
836 p_picture->p->i_pitch = i_width_aligned;
837 p_picture->p->i_visible_pitch = i_width;
838 p_picture->p->i_pixel_pitch = 4;
839 p_picture->i_planes = 1;
846 p_picture->p->i_lines = i_height_aligned;
847 p_picture->p->i_visible_lines = i_height;
848 p_picture->p->i_pitch = i_width_aligned * 2;
849 p_picture->p->i_visible_pitch = i_width * 2;
850 p_picture->p->i_pixel_pitch = 2;
851 p_picture->i_planes = 1;
855 p_picture->p->i_lines = i_height_aligned;
856 p_picture->p->i_visible_lines = i_height;
857 p_picture->p->i_pitch = i_width_aligned;
858 p_picture->p->i_visible_pitch = i_width;
859 p_picture->p->i_pixel_pitch = 1;
860 p_picture->i_planes = 1;
863 case VLC_CODEC_RGB15:
864 p_picture->p->i_lines = i_height_aligned;
865 p_picture->p->i_visible_lines = i_height;
866 p_picture->p->i_pitch = i_width_aligned * 2;
867 p_picture->p->i_visible_pitch = i_width * 2;
868 p_picture->p->i_pixel_pitch = 2;
869 p_picture->i_planes = 1;
872 case VLC_CODEC_RGB16:
873 p_picture->p->i_lines = i_height_aligned;
874 p_picture->p->i_visible_lines = i_height;
875 p_picture->p->i_pitch = i_width_aligned * 2;
876 p_picture->p->i_visible_pitch = i_width * 2;
877 p_picture->p->i_pixel_pitch = 2;
878 p_picture->i_planes = 1;
881 case VLC_CODEC_RGB24:
882 p_picture->p->i_lines = i_height_aligned;
883 p_picture->p->i_visible_lines = i_height;
884 p_picture->p->i_pitch = i_width_aligned * 3;
885 p_picture->p->i_visible_pitch = i_width * 3;
886 p_picture->p->i_pixel_pitch = 3;
887 p_picture->i_planes = 1;
890 case VLC_CODEC_RGB32:
892 p_picture->p->i_lines = i_height_aligned;
893 p_picture->p->i_visible_lines = i_height;
894 p_picture->p->i_pitch = i_width_aligned * 4;
895 p_picture->p->i_visible_pitch = i_width * 4;
896 p_picture->p->i_pixel_pitch = 4;
897 p_picture->i_planes = 1;
902 p_picture->p->i_lines = i_height_aligned;
903 p_picture->p->i_visible_lines = i_height;
904 p_picture->p->i_pitch = i_width_aligned;
905 p_picture->p->i_visible_pitch = i_width;
906 p_picture->p->i_pixel_pitch = 1;
907 p_picture->i_planes = 1;
911 p_picture->i_planes = 0;
918 /*****************************************************************************
920 *****************************************************************************/
921 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
923 video_format_t fmt = *p_fmt;
925 /* It is needed to be sure all informations are filled */
926 video_format_Setup( &fmt, p_fmt->i_chroma,
927 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
930 picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
936 if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
941 p_picture->p_sys = p_resource->p_sys;
943 for( int i = 0; i < p_picture->i_planes; i++ )
945 p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
946 p_picture->p[i].i_lines = p_resource->p[i].i_lines;
947 p_picture->p[i].i_pitch = p_resource->p[i].i_pitch;
952 if( __vout_AllocatePicture( NULL, p_picture,
953 fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect ) )
960 p_picture->format = fmt;
961 p_picture->i_refcount = 1;
962 p_picture->pf_release = PictureReleaseCallback;
963 p_picture->i_status = RESERVED_PICTURE;
967 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
969 return picture_NewFromResource( p_fmt, NULL );
971 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_aspect )
975 memset( &fmt, 0, sizeof(fmt) );
976 video_format_Setup( &fmt, i_chroma, i_width, i_height, i_aspect );
978 return picture_NewFromFormat( &fmt );
981 /*****************************************************************************
983 *****************************************************************************/
984 void picture_Delete( picture_t *p_picture )
986 assert( p_picture && p_picture->i_refcount == 0 );
987 assert( p_picture->p_release_sys == NULL );
989 free( p_picture->p_q );
990 free( p_picture->p_data_orig );
991 free( p_picture->p_sys );
995 /*****************************************************************************
997 *****************************************************************************/
998 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1002 for( i = 0; i < p_src->i_planes ; i++ )
1003 plane_CopyPixels( p_dst->p+i, p_src->p+i );
1006 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1008 const unsigned i_width = __MIN( p_dst->i_visible_pitch,
1009 p_src->i_visible_pitch );
1010 const unsigned i_height = __MIN( p_dst->i_visible_lines,
1011 p_src->i_visible_lines );
1013 if( p_src->i_pitch == p_dst->i_pitch )
1015 /* There are margins, but with the same width : perfect ! */
1016 vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1017 p_src->i_pitch * i_height );
1021 /* We need to proceed line by line */
1022 uint8_t *p_in = p_src->p_pixels;
1023 uint8_t *p_out = p_dst->p_pixels;
1029 for( i_line = i_height; i_line--; )
1031 vlc_memcpy( p_out, p_in, i_width );
1032 p_in += p_src->i_pitch;
1033 p_out += p_dst->i_pitch;
1038 /*****************************************************************************
1040 *****************************************************************************/
1041 int picture_Export( vlc_object_t *p_obj,
1043 video_format_t *p_fmt,
1044 picture_t *p_picture,
1045 vlc_fourcc_t i_format,
1046 int i_override_width, int i_override_height )
1049 video_format_t fmt_in = p_picture->format;
1050 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1053 fmt_in.i_sar_den = 1;
1057 video_format_t fmt_out;
1058 memset( &fmt_out, 0, sizeof(fmt_out) );
1060 fmt_out.i_sar_den = 1;
1061 fmt_out.i_chroma = i_format;
1063 /* compute original width/height */
1064 unsigned int i_original_width;
1065 unsigned int i_original_height;
1066 if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1068 i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1069 i_original_height = fmt_in.i_height;
1073 i_original_width = fmt_in.i_width;
1074 i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1078 fmt_out.i_width = ( i_override_width < 0 ) ?
1079 i_original_width : i_override_width;
1080 fmt_out.i_height = ( i_override_height < 0 ) ?
1081 i_original_height : i_override_height;
1083 /* scale if only one direction is provided */
1084 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1086 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1087 * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1089 else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1091 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height
1092 * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1095 image_handler_t *p_image = image_HandlerCreate( p_obj );
1097 block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1099 image_HandlerDelete( p_image );
1102 return VLC_EGENERIC;
1105 p_block->i_dts = p_picture->date;
1109 *pp_image = p_block;